Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mujoco rendering produces black image for every MjModel instance called after the first #792

Open
sriddle97 opened this issue Mar 6, 2024 · 6 comments

Comments

@sriddle97
Copy link

sriddle97 commented Mar 6, 2024

I am running a python program that iterates through multiple mujoco simulations using a different xml file each time. This requires me to execute the following lines for each iteration of the loop:

model_mj = mujoco.MjModel.from_xml_path(file_path)
data_mj = mujoco.MjData(model_mj)
renderer = mujoco.Renderer(model_mj, 400, 900)
mujoco.mj_forward(model_mj,data_mj)
renderer.update_scene(data_mj, camera='fixed')
media.show_image(renderer.render())

The rendering of the first instance of model_mj looks as it should when show_image is executed. However, when show_image is executed in every subsequent iteration, the rendering shows a black screen rather than the proper model. I know the model is still being run in the physics engine as I get expected numerical data outputs from data_mj regardless of the image being correct or blank.

In the past I was able to fix this issue by running each iteration individually and restarting the python kernel manually each time. I assume this was just a roundabout way of restarting the mujoco simulation each time as well. However, I cannot do this when I need the python code to continue running for the following iterations. Is there a way to restart the mujoco environment within the code or at very least some way to get the images to render properly without having to restart the python code? While I am able to get the same numerical data from my simulation, the rendered video is a vital part of my analysis for this project.

Any help would be appreciated.

@sriddle97
Copy link
Author

I've tried running mujoco.mj_resetData(model_mj, data_mj) at the end of each loop but it did not fix the issue.

@saran-t
Copy link

saran-t commented Mar 7, 2024

This looks like a question for https://github.com/google-deepmind/mujoco rather than mujoco-py.

@hqm666
Copy link

hqm666 commented Jun 3, 2024

Have you solved this problem? I have met the same situation recently.

@ruiqiwang96
Copy link

This happened to me too. Running the example notebook on google colab works fine, but running it on my PC shows black image from the second time. Renderer gives expected image the first time only, after that it's all black. Since the model works fine and media works fine, I reckon the problem lies in renderer. The use of renderer replies on GPU and it needs to turn on headless rendering model. Based on the example code to set up the EGL on google colab, I confirmed I do have "libEGL_nvidia.so.0" on my PC and created the missing json file / ICD config "10_nvidia.json". Problem solved!

@saran-t
Copy link

saran-t commented Jun 22, 2024

For future reference:

  • The code snippets suggests that this issue relates to the "new" MuJoCo bindings (released in 2022). You should post questions on google-deepmind/mujoco. This repo is for OpenAI's old "mujoco-py" that's no longer maintained or developed. MuJoCo developers don't monitor issues here.

  • You mentioning media.show_image suggests that you're trying to visualise interactively. You should check out official Python viewer which is going to give you a lot more than just renders.

@sriddle97
Copy link
Author

@hqm666 I've figured out a fix. I have each of my simulations saved as a separate xml file and the paths for these files stored in an array. Each time I iterate to the next sim I call model = mujoco.MjModel.from_xml_path(array[i]) and then the following:
renderer = mujoco.Renderer(model)

Then after running the sim and generating the frames each timestep I run the following:
media.write_video(vid_names, frames, fps=framerate)
renderer.close()

The renderer.close() after media.write_video() should "clear the cache" so to speak and make way for the next mujoco.Renderer() command when the next sim is iterated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants