google-deepmind / mujoco

Multi-Joint dynamics with Contact. A general purpose physics simulator.
https://mujoco.org
Apache License 2.0
8.25k stars 823 forks source link

MuJoCo viewer doesn't allow to change parameters in the "Visualization" panel when launched in passive mode #2158

Open stepanove opened 1 month ago

stepanove commented 1 month ago

Intro

Hi!

I'm just a robotics and software development enthusiast experimenting with MuJoCo.

My setup

MuJoCo 3.0.0-3.2.4, Python 3.12, Debian 12 (docker image "python:3.12-bookworm")

What's happening? What did you expect?

I noticed a strange behavior of the MuJoCo viewer in passive mode. I'm experimenting with an example from the docs. Initially I faced the problem that I can't change the scale of the model elements (joints, contact forces, etc). When I adjust the values in the Visualization/Scale panel, it changes for a moment and then resets. But it looks like all the parameters in the "Visualization" panel are affected. If I launch the viewer in active mode, it works as expected. I tried a few versions down to 3.0.0, the behavior the same.

https://github.com/user-attachments/assets/4e7a2841-10f8-4e28-8c64-7598929a2c36

Steps for reproduction

  1. Run the code below.
  2. Try to change some parameters in the "Visualization" panel.

Minimal model for reproduction

No response

Code required for reproduction

import time
import mujoco
import mujoco.viewer

xml = """
<mujoco>
  <worldbody>
    <light name="top" pos="0 0 1"/>
    <body name="box_and_sphere" euler="0 0 -30">
      <joint name="swing" type="hinge" axis="1 -1 0" pos="-.2 -.2 -.2"/>
      <geom name="red_box" type="box" size=".2 .2 .2" rgba="1 0 0 1"/>
      <geom name="green_sphere" pos=".2 .2 .2" size=".1" rgba="0 1 0 1"/>
    </body>
  </worldbody>
</mujoco>
"""
model = mujoco.MjModel.from_xml_string(xml)
data = mujoco.MjData(model)

with mujoco.viewer.launch_passive(model, data) as viewer:
  # Close the viewer automatically after 30 wall-seconds.
  start = time.time()
  while viewer.is_running() and time.time() - start < 30:
    step_start = time.time()

    # mj_step can be replaced with code that also evaluates
    # a policy and applies a control signal before stepping the physics.
    mujoco.mj_step(model, data)

    # Example modification of a viewer option: toggle contact points every two seconds.
    with viewer.lock():
      viewer.opt.flags[mujoco.mjtVisFlag.mjVIS_CONTACTPOINT] = int(data.time % 2)

    # Pick up changes to the physics state, apply perturbations, update options from GUI.
    viewer.sync()

    # Rudimentary time keeping, will drift relative to wall clock.
    time_until_next_step = model.opt.timestep - (time.time() - step_start)
    if time_until_next_step > 0:
      time.sleep(time_until_next_step)

Confirmations