Farama-Foundation / Gymnasium-Robotics

A collection of robotics simulation environments for reinforcement learning
https://robotics.farama.org/
MIT License
571 stars 91 forks source link

[Bug Report] Rendering issue for rgb_array render mode with gymnasium 1.0.0 #235

Closed violasox closed 1 month ago

violasox commented 1 month ago

Describe the bug Gymnasium 1.0.0 added the width and height parameters to the initialization method of the MujocoRenderer class. If no values are passed in, they default to None and this causes rendering to fail when the render_mode is set to rgb_array (at least for on-screen mode - I did not try rendering off-screen).

Code example

import gymnasium_robotics
import gymnasium as gym

def main():
    env = gym.make('FetchReach-v3', render_mode='rgb_array')
    env.reset()
    img_arr = env.render()

if __name__ == '__main__':
    main()

Stack trace:

Exception ignored in: <function GLContext.__del__ at 0x7f63e7e00c10>                                                                                                                                                                                                              
Traceback (most recent call last):                                                                                                                                                                                                                                                
  File "/home/violasox/venv/lib/python3.8/site-packages/mujoco/glfw/__init__.py", line 41, in __del__
    self.free()
  File "/home/violasox/venv/lib/python3.8/site-packages/mujoco/glfw/__init__.py", line 34, in free
    if self._context:
AttributeError: 'GLContext' object has no attribute '_context'
Traceback (most recent call last):
  File "sample.py", line 10, in <module>
    main()
  File "sample.py", line 7, in main
    img_arr = env.render()
  File "/home/violasox/venv/lib/python3.8/site-packages/gymnasium/core.py", line 332, in render
    return self.env.render()
  File "/home/violasox/venv/lib/python3.8/site-packages/gymnasium/wrappers/common.py", line 409, in render
    return super().render()
  File "/home/violasox/venv/lib/python3.8/site-packages/gymnasium/core.py", line 332, in render
    return self.env.render()
  File "/home/violasox/venv/lib/python3.8/site-packages/gymnasium/wrappers/common.py", line 301, in render
    return env_render_passive_checker(self.env)
  File "/home/violasox/venv/lib/python3.8/site-packages/gymnasium/utils/passive_env_checker.py", line 361, in env_render_passive_checker
    result = env.render()
  File "/home/kc/Research/empowerment/Gymnasium-Robotics-RenderFix/gymnasium_robotics/envs/robot_env.py", line 313, in render
    return self.mujoco_renderer.render(self.render_mode)
  File "/home/violasox/venv/lib/python3.8/site-packages/gymnasium/envs/mujoco/mujoco_rendering.py", line 706, in render
    viewer = self._get_viewer(render_mode=render_mode)
  File "/home/violasox/venv/lib/python3.8/site-packages/gymnasium/envs/mujoco/mujoco_rendering.py", line 730, in _get_viewer
    self.viewer = OffScreenViewer(
  File "/home/violasox/venv/lib/python3.8/site-packages/gymnasium/envs/mujoco/mujoco_rendering.py", line 155, in __init__
    super().__init__(model, data, width, height, max_geom, visual_options)
  File "/home/violasox/venv/lib/python3.8/site-packages/gymnasium/envs/mujoco/mujoco_rendering.py", line 53, in __init__
    self.viewport = mujoco.MjrRect(0, 0, width, height)
TypeError: __init__(): incompatible constructor arguments. The following argument types are supported:
    1. mujoco._render.MjrRect(left: int, bottom: int, width: int, height: int)

Invoked with: 0, 0, None, None

System Info

Additional context I was able to fix this issue by passing the height and width parameters in the BaseRobotEnv class to the MujocoRenderer in robot_env.py. I will submit a pull request for this fix.

Checklist