|
6 | 6 | import imageio
|
7 | 7 | import mujoco
|
8 | 8 | import numpy as np
|
| 9 | +from packaging.version import Version |
9 | 10 |
|
10 | 11 | from gymnasium.logger import warn
|
11 | 12 |
|
12 | 13 |
|
| 14 | +# The marker API changed in MuJoCo 3.2.0, so we check the mujoco version and set a flag that |
| 15 | +# determines which function we use when adding markers to the scene. |
| 16 | +_MUJOCO_MARKER_LEGACY_MODE = Version(mujoco.__version__) < Version("3.2.0") |
| 17 | + |
| 18 | + |
13 | 19 | def _import_egl(width, height):
|
14 | 20 | from mujoco.egl import GLContext
|
15 | 21 |
|
@@ -88,8 +94,37 @@ def add_marker(self, **marker_params):
|
88 | 94 |
|
89 | 95 | def _add_marker_to_scene(self, marker: dict):
|
90 | 96 | if self.scn.ngeom >= self.scn.maxgeom:
|
91 |
| - raise RuntimeError("Ran out of geoms. maxgeom: %d" % self.scn.maxgeom) |
| 97 | + raise RuntimeError(f"Ran out of geoms. maxgeom: {self.scn.maxgeom}") |
| 98 | + |
| 99 | + if _MUJOCO_MARKER_LEGACY_MODE: # Old API for markers requires special handling |
| 100 | + self._legacy_add_marker_to_scene(marker) |
| 101 | + else: |
| 102 | + geom_type = marker.get("type", mujoco.mjtGeom.mjGEOM_SPHERE) |
| 103 | + size = marker.get("size", np.array([0.01, 0.01, 0.01])) |
| 104 | + pos = marker.get("pos", np.array([0.0, 0.0, 0.0])) |
| 105 | + mat = marker.get("mat", np.eye(3).flatten()) |
| 106 | + rgba = marker.get("rgba", np.array([1.0, 1.0, 1.0, 1.0])) |
| 107 | + mujoco.mjv_initGeom( |
| 108 | + self.scn.geoms[self.scn.ngeom], |
| 109 | + geom_type, |
| 110 | + size=size, |
| 111 | + pos=pos, |
| 112 | + mat=mat, |
| 113 | + rgba=rgba, |
| 114 | + ) |
| 115 | + |
| 116 | + self.scn.ngeom += 1 |
92 | 117 |
|
| 118 | + def _legacy_add_marker_to_scene(self, marker: dict): |
| 119 | + """Add a marker to the scene compatible with older versions of MuJoCo. |
| 120 | +
|
| 121 | + MuJoCo 3.2 introduced breaking changes to the visual geometries API. To maintain |
| 122 | + compatibility with older versions, we use the legacy API when an older version of MuJoCo is |
| 123 | + detected. |
| 124 | +
|
| 125 | + Args: |
| 126 | + marker: A dictionary containing the marker parameters. |
| 127 | + """ |
93 | 128 | g = self.scn.geoms[self.scn.ngeom]
|
94 | 129 | # default values.
|
95 | 130 | g.dataid = -1
|
@@ -130,8 +165,6 @@ def _add_marker_to_scene(self, marker: dict):
|
130 | 165 | else:
|
131 | 166 | raise ValueError("mjtGeom doesn't have field %s" % key)
|
132 | 167 |
|
133 |
| - self.scn.ngeom += 1 |
134 |
| - |
135 | 168 | def close(self):
|
136 | 169 | """Override close in your rendering subclass to perform any necessary cleanup
|
137 | 170 | after env.close() is called.
|
|
0 commit comments