rohanpsingh / mujoco-python-viewer

Simple renderer for use with MuJoCo (>=2.1.2) Python Bindings.
BSD 2-Clause "Simplified" License
163 stars 24 forks source link

How about glfw.window_hint(glfw.VISIBLE, 1) explicitly in the constructor? #47

Open Qualot opened 1 month ago

Qualot commented 1 month ago

Thank you for the helpful software!

Our team uses mujoco.renderer and this MujocoViewer at the same time. mujoco.renderer is for segmentation/depth rendering.

After the initialization of a renderer, The viewer window does not appear.

I think that the constructor of renderer executes the following code https://github.com/google-deepmind/mujoco/blob/2.3.7/python/mujoco/renderer.py#L80 https://github.com/google-deepmind/mujoco/blob/2.3.7/python/mujoco/glfw/__init__.py#L25

This is the MWE.

import mujoco
import mujoco_viewer
import glfw

model = mujoco.MjModel.from_xml_path('humanoid.xml')
data = mujoco.MjData(model)

renderer = mujoco.Renderer(model)

#Commenting out this line hides the viewer window
glfw.window_hint(glfw.VISIBLE, 1)

# create the viewer object
viewer = mujoco_viewer.MujocoViewer(model, data)

# simulate and render
for _ in range(10000):
    if viewer.is_alive:
        mujoco.mj_step(model, data)
        viewer.render()
    else:
        break

# close
viewer.close()

I think adding the line of glfw.window_hint(glfw.VISIBLE, 1) like https://github.com/rohanpsingh/mujoco-python-viewer/blob/main/mujoco_viewer/mujoco_viewer.py#L46-L47 would solve this problem of unexpected hidden window.

It should be noted that using renderer and viewer.render() in the loop would require the context switching as the following issue. https://github.com/rohanpsingh/mujoco-python-viewer/issues/35

On the other hand, I guess that the frame buffer part does not need modifications because https://github.com/rohanpsingh/mujoco-python-viewer/blob/main/mujoco_viewer/mujoco_viewer.py#L49-L50 automatically sets the active buffer to the default window.

What do you think of these? If these sound reasonable, I would send a PR.