Issue with Recording Videos in Mujoco Environment

Jan 8, 2024

Error with env.render()

I encountered issues while attempting to record videos of the robot’s operation and found problems with rendering:

  1. Rendering with no image (After some googling, it seemed like something was unset, rendering worked but the image was completely black…)
  2. # Failed to load OpenGL (If I didn’t unset that thing, this problem occurred)

I found a solution mentioned in an issue here:

There have been multiple issues which relate to the same error (#598, #187, #390) but unfortunately none of them worked for me. What worked was to change this line in <site_packages>/gym/envs/mujoco/mujoco_env.py to:

self.viewer = mujoco_py.MjRenderContextOffscreen(self.sim, None, -1)

You can find the location of <site_packages> in your system by using pip show gym.

If using Gymnasium, find gymnasium/envs/mujoco/mujoco_env.py in <site_packages> and modify self.viewer = mujoco_py.MjRenderContextOffscreen(self.sim, None, -1) on this line.

Some version information:

gym                       0.26.2                   pypi_0    pypi
gym-notices               0.0.8                    pypi_0    pypi
gymnasium                 0.26.3                   pypi_0    pypi
gymnasium-notices         0.0.1                    pypi_0    pypi
mujoco                    3.1.1                    pypi_0    pypi
mujoco-py                 2.1.2.14                 pypi_0    pypi