Skip to content

Commit b362cb4

Browse files
saran-tcopybara-github
authored andcommitted
Expose a handle for the Python viewer.
This change also requires user scripts to explicitly synchronize changes to physics state to the viewer. The Simulate class was reconfigured so that certain UI events are handled during this sync operation, outside of the render loop on the main thread. These correspond to operations that require access to the full mjModel/mjData. To support other, more interactive operations (e.g. camera movements), a new mjvSceneState struct is introduced which captures only the portion of the physics state required for scene re-rendering. The mjvSceneState is updated from mjModel/mjData during the viewer sync operation, and is significantly cheaper than a full mj_copyModel and mj_copyData. Fixes #796 PiperOrigin-RevId: 525723636 Change-Id: Id08d0210a2c067d5afe85e2bf104f276aeddd75e
1 parent 4f5da9c commit b362cb4

34 files changed

+4642
-1086
lines changed

doc/APIreference/APItypes.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,17 @@ This structure contains everything needed to render the 3D scene in OpenGL.
814814
.. mujoco-include:: mjvScene
815815

816816

817+
.. _mjvSceneState:
818+
819+
mjvSceneState
820+
~~~~~~~~~~~~~
821+
822+
This structure contains the portions of :ref:`mjModel` and :ref:`mjData` that are required for
823+
various ``mjv_*`` functions.
824+
825+
.. mujoco-include:: mjvScene
826+
827+
817828
.. _mjvFigure:
818829

819830
mjvFigure

doc/APIreference/functions.rst

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,6 +1332,15 @@ mjv_moveCamera
13321332

13331333
Move camera with mouse; action is mjtMouse.
13341334

1335+
.. _mjv_moveCameraFromState:
1336+
1337+
mjv_moveCameraFromState
1338+
~~~~~~~~~~~~~~~~~~~~~~~
1339+
1340+
.. mujoco-include:: mjv_moveCameraFromState
1341+
1342+
Move camera with mouse given a scene state; action is mjtMouse.
1343+
13351344
.. _mjv_movePerturb:
13361345

13371346
mjv_movePerturb
@@ -1341,6 +1350,15 @@ mjv_movePerturb
13411350

13421351
Move perturb object with mouse; action is mjtMouse.
13431352

1353+
.. _mjv_movePerturbFromState:
1354+
1355+
mjv_movePerturbFromState
1356+
~~~~~~~~~~~~~~~~~~~~~~~~
1357+
1358+
.. mujoco-include:: mjv_movePerturbFromState
1359+
1360+
Move perturb object with mouse given a scene state; action is mjtMouse.
1361+
13441362
.. _mjv_moveModel:
13451363

13461364
mjv_moveModel
@@ -1483,6 +1501,51 @@ mjv_updateScene
14831501

14841502
Update entire scene given model state.
14851503

1504+
.. _mjv_updateSceneFromState:
1505+
1506+
mjv_updateSceneFromState
1507+
~~~~~~~~~~~~~~~~~~~~~~~~
1508+
1509+
.. mujoco-include:: mjv_updateSceneFromState
1510+
1511+
Update entire scene from a scene state, return the number of new mjWARN_VGEOMFULL warnings.
1512+
1513+
.. _mjv_defaultSceneState:
1514+
1515+
mjv_defaultSceneState
1516+
~~~~~~~~~~~~~~~~~~~~~
1517+
1518+
.. mujoco-include:: mjv_defaultSceneState
1519+
1520+
Set default scene state.
1521+
1522+
.. _mjv_makeSceneState:
1523+
1524+
mjv_makeSceneState
1525+
~~~~~~~~~~~~~~~~~~
1526+
1527+
.. mujoco-include:: mjv_makeSceneState
1528+
1529+
Allocate resources and initialize a scene state object.
1530+
1531+
.. _mjv_freeSceneState:
1532+
1533+
mjv_freeSceneState
1534+
~~~~~~~~~~~~~~~~~~
1535+
1536+
.. mujoco-include:: mjv_freeSceneState
1537+
1538+
Free scene state.
1539+
1540+
.. _mjv_updateSceneState:
1541+
1542+
mjv_updateSceneState
1543+
~~~~~~~~~~~~~~~~~~~~
1544+
1545+
.. mujoco-include:: mjv_updateSceneState
1546+
1547+
Update a scene state from model and data.
1548+
14861549
.. _mjv_addGeoms:
14871550

14881551
mjv_addGeoms
@@ -1572,6 +1635,15 @@ mjr_freeContext
15721635

15731636
Free resources in custom OpenGL context, set to default.
15741637

1638+
.. _mjr_resizeOffscreen:
1639+
1640+
mjr_resizeOffscreen
1641+
~~~~~~~~~~~~~~~~~~~
1642+
1643+
.. mujoco-include:: mjr_resizeOffscreen
1644+
1645+
Resize offscreen buffers.
1646+
15751647
.. _mjr_uploadTexture:
15761648

15771649
mjr_uploadTexture

doc/changelog.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ Python bindings
3535
state concurrently with the internal ``mj_forward``, resulting in e.g.
3636
`MuJoCo stack overflow error <https://github.com/deepmind/mujoco/issues/783>`_
3737
or `segmentation fault <https://github.com/deepmind/mujoco/issues/790>`_.
38+
- The ``viewer.launch_passive`` function now returns a handle which can be used to interact with the viewer. The passive
39+
viewer now also requires an explicit call to ``sync`` on its handle to pick up any update to the physics state. This
40+
is to avoid race conditions that can result in visual artifacts. See :ref:`documentation<PyViewer>` for details.
41+
- The ``viewer.launch_repl`` function has been removed since its functionality is superceded by ``launch_passive``.
3842
- Added a small number of missing struct fields discovered through the new ``introspect`` metadata.
3943

4044
Bug fixes
@@ -102,7 +106,7 @@ Python bindings
102106
#. Added ``viewer.launch_passive`` which launches the interactive viewer in a passive, non-blocking mode. Calls to
103107
``launch_passive`` return immediately, allowing user code to continue execution, with the viewer automatically
104108
reflecting any changes to the physics state. (Note that this functionality is currently in experimental/beta stage,
105-
and is not yet described in our :ref:`viewer documentation<PyViewer>`.)
109+
and is not yet described in our :ref:`viewer documentation<PyViewer>`.)
106110
#. Added the ``mjpython`` launcher for macOS, which is required for ``viewer.launch_passive`` to function there.
107111
#. Removed ``efc_`` fields from joint indexers. Since the introduction of arena memory, these fields now have dynamic
108112
sizes that change between time steps depending on the number of active constraints, breaking strict correspondence

0 commit comments

Comments
 (0)