Skip to content

studio_plugins

MAX_OVERLAY_GEOMS module-attribute

Python
MAX_OVERLAY_GEOMS: int = _browser_geom_cap()

How many extra geoms the browser viewer draws. Anything past it is dropped. Unlimited when this mujoco build has no browser viewer.

MojoStepControl

Python
MojoStepControl(controls: ViewerControls)

Bases: SimPlugin

Stands in for Studio's step control and history plugins, neither of which can be used: they would step the physics themselves, and Mojo's runtime does that.

It passes what the viewer's buttons ask for to Reloaded's controller, and shows the controller's pause and speed on the viewer's widgets, so Studio and the terminal agree. It consumes the viewer's reset and reload requests, because the built in handlers would replace or reset the model that the runtime is stepping.

Source code in src/mujoco_mojo/utils/layers/reloaded/backends/studio_plugins.py
Python
def __init__(self, controls: ViewerControls) -> None:
    self._controls = controls
    self._shown: tuple[bool, float] | None = None
    """The pause and speed last sent to the viewer, so they are only sent again when they change."""
    self._warned_noise = False
    self._noise = (0.0, 0.0)
    """The control noise the viewer's panel holds. Reloaded adds none, but the panel's own value is sent back as it is, so the slider does not snap back under the hand that is dragging it."""

MojoOverlaySnapshot dataclass

Python
MojoOverlaySnapshot(
    arrows: tuple[ArrowConfig, ...] = (),
    lines: tuple[LineConfig, ...] = (),
)

Bases: Snapshot

The arrows and lines Mojo's runtime wants drawn over the model, on their way from the simulation thread to the viewer.

MojoOverlay

Python
MojoOverlay()

Bases: ViewerPlugin

Draws Mojo's force arrows, lines, and traces. The viewer thread owns the viewer's extra geoms, so they are made here, from the latest MojoOverlaySnapshot.

Source code in src/mujoco_mojo/utils/layers/reloaded/backends/studio_plugins.py
Python
def __init__(self) -> None:
    self._latest = MojoOverlaySnapshot()
    self._drawn: MojoOverlaySnapshot | None = None
    """What the geoms this plugin put in the viewer were made from."""
    self._geoms: list[mujoco.MjvGeom] = []
    """The geoms this plugin put in the viewer, so it only ever takes out its own."""
    self._warned_full = False

refresh

Python
refresh(extra_geoms: list[MjvGeom], model: MjModel) -> None

Brings the geoms this plugin put in extra_geoms up to date with the latest overlay, leaving any other plugin's geoms where they are. Does nothing when the overlay has not changed since.

Source code in src/mujoco_mojo/utils/layers/reloaded/backends/studio_plugins.py
Python
def refresh(self, extra_geoms: list[mujoco.MjvGeom], model: mujoco.MjModel) -> None:
    """Brings the geoms this plugin put in `extra_geoms` up to date with the latest overlay, leaving any other plugin's geoms where they are. Does nothing when the overlay has not changed since."""
    if self._latest is self._drawn:
        return
    mine = {id(geom) for geom in self._geoms}
    extra_geoms[:] = [g for g in extra_geoms if id(g) not in mine]
    self._geoms = []
    self._drawn = self._latest

    room = max(MAX_OVERLAY_GEOMS - len(extra_geoms), 0)
    wanted = len(self._latest.arrows) + len(self._latest.lines)
    if wanted > room and not self._warned_full:
        self._warned_full = True
        logger.warning(
            f"The viewer can draw {MAX_OVERLAY_GEOMS} extra shapes and {wanted} were asked for, so the oldest trail segments are left out."
        )
    for arrow in self._latest.arrows[:room]:
        geom = mujoco.MjvGeom()
        arrow.fill_geom(geom, model)
        self._geoms.append(geom)
    # lines include the trails of tracers, oldest segment first, so when there is no room for all of
    # them it is the oldest that go and the trail still reaches the current position
    room_for_lines = room - len(self._geoms)
    lines = self._latest.lines
    for line in lines[max(len(lines) - room_for_lines, 0) :]:
        geom = mujoco.MjvGeom()
        line.fill_geom(geom)
        self._geoms.append(geom)
    extra_geoms.extend(self._geoms)

studio_speed

Python
studio_speed(speed: float) -> float

The speed to show on Studio's slider for a Reloaded speed.

Studio's slider runs from 0.1 to 100 percent of real time, so anything faster than real time shows as 100 and manual stepping, which has no speed of its own, does too.

Source code in src/mujoco_mojo/utils/layers/reloaded/backends/studio_plugins.py
Python
def studio_speed(speed: float) -> float:
    """
    The speed to show on Studio's slider for a Reloaded `speed`.

    Studio's slider runs from 0.1 to 100 percent of real time, so anything faster than real time shows as 100 and manual stepping, which has no speed of its own, does too.
    """
    if speed <= 0:
        return _REAL_TIME_PERCENT
    return min(max(speed * _REAL_TIME_PERCENT, _SLOWEST_PERCENT), _REAL_TIME_PERCENT)

mojo_speed

Python
mojo_speed(percent: float) -> float

The Reloaded speed, a multiple of real time, for a Studio slider reading in percent.

Source code in src/mujoco_mojo/utils/layers/reloaded/backends/studio_plugins.py
Python
def mojo_speed(percent: float) -> float:
    """The Reloaded speed, a multiple of real time, for a Studio slider reading in percent."""
    return percent / _REAL_TIME_PERCENT