0dfa4b509a
This change replaces fixed callback protocols (e.g., ViewerGuiHook, ViewerUpdateHook, SimEventHandler) with a general-purpose, priority-based message and event handling mechanism. Key changes: - Handler decorator and registry: Introduced the `@messages.handler(priority=...)` decorator and `HandlerRegistry` (`handler_registry.py`). Methods marked as handlers are automatically discovered and dispatched by priority (CRITICAL, USER, LIBRARY, INTERNAL) or method resolution order. - Local lifecycle events: Added `ViewerAppInitEvent`, `BuildGuiEvent`, and `UpdateEvent` to `messages.py`. Custom GUI rendering and per-frame update logic can now be implemented as standard event handlers without needing separate interface protocols. - Streamlined launch and app APIs: Replaced individual hook and handler arguments in `launch_passive`, `ViewerApp`, and `ViewerHandle` with unified `viewer_handlers` and `sim_handlers` lists. - Module restructuring: Extracted simulation-side message handling and `ViewerHandle` from `sim_app.py` into a dedicated `viewer_handle.py` module, removing `sim_app.py`. - Sample updates: Migrated existing examples (such as `implot.py`) to use the new handler pattern and lifecycle events. PiperOrigin-RevId: 941729322 Change-Id: I93e7c0edf0a13a8dc854f9e2083451f7c25a1825
230 lines
6.5 KiB
Python
230 lines
6.5 KiB
Python
# Copyright 2026 DeepMind Technologies Limited
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# https://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
"""Messages and Channels for Studio."""
|
|
|
|
import dataclasses
|
|
import enum
|
|
import inspect
|
|
from typing import Any, Callable, Protocol, runtime_checkable
|
|
|
|
import mujoco
|
|
from mujoco.experimental.studio import sim
|
|
import numpy as np
|
|
|
|
# -----------------------------------------------------------------------------
|
|
# Message and Channel types for passing data between the sim and the viewer.
|
|
# Concrete messages are defined later in this file, there is one per Event;
|
|
# add more if needed in your script.
|
|
# Concrete Channel implementations live in the launch_* modules.
|
|
# -----------------------------------------------------------------------------
|
|
|
|
|
|
class Message:
|
|
"""Base class for data sent between the sim and the viewer.
|
|
|
|
Derive from Snapshot or Event, depending on the desired delivery semantics.
|
|
"""
|
|
|
|
|
|
class Snapshot(Message):
|
|
"""Base class for latest-wins, droppable messages.
|
|
|
|
Each new snapshot replaces the previous one, so the receiver always sees the
|
|
most recent value. Intermediate values may be silently dropped, making
|
|
snapshots suitable for frequently updated state where only the latest value
|
|
matters.
|
|
"""
|
|
|
|
|
|
class Event(Message):
|
|
"""Base class for reliable, ordered, never dropped messages.
|
|
|
|
Events are queued in order and every event is delivered exactly once. Use
|
|
events for discrete actions that must not be lost.
|
|
"""
|
|
|
|
|
|
@runtime_checkable
|
|
class SnapshotChannel(Protocol):
|
|
"""Channel to transport Snapshot messages with latest-wins semantics."""
|
|
|
|
def put(self, value: Snapshot) -> None:
|
|
"""Overwrites the latest snapshot of the same type.
|
|
|
|
After calling put() it is the caller's responsibility to ensure it no longer
|
|
holds a reference to the snapshot.
|
|
|
|
Args:
|
|
value: The snapshot to put into the channel.
|
|
"""
|
|
...
|
|
|
|
def get(self) -> list[Snapshot]:
|
|
"""Returns the latest pending snapshots of each type."""
|
|
...
|
|
|
|
def close(self) -> None:
|
|
"""Releases resources held by the channel."""
|
|
...
|
|
|
|
|
|
@runtime_checkable
|
|
class EventChannel(Protocol):
|
|
"""Channel to transport Event messages in an ordered stream, nothing dropped."""
|
|
|
|
def put(self, value: Event) -> None:
|
|
"""Puts an event into the channel, appending to the stream."""
|
|
...
|
|
|
|
def get(self) -> list[Event]:
|
|
"""Returns all pending events, in order."""
|
|
...
|
|
|
|
def close(self) -> None:
|
|
"""Releases resources held by the channel."""
|
|
...
|
|
|
|
|
|
# -----------------------------------------------------------------------------
|
|
# Concrete Event and Snapshot types.
|
|
# -----------------------------------------------------------------------------
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class StateSnapshot(Snapshot):
|
|
"""A snapshot message that transports a MuJoCo state."""
|
|
|
|
state: np.ndarray
|
|
state_sig: int
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class ResetEvent(Event):
|
|
"""An event requesting to reset the simulation."""
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class ModelEvent(Event):
|
|
"""An event that transports a MuJoCo model."""
|
|
|
|
model: mujoco.MjModel
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class MjOptionSnapshot(Snapshot):
|
|
"""A snapshot sending mjOption state from viewer to sim each frame."""
|
|
|
|
opt: mujoco.MjOption
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class PerturbEvent(Event):
|
|
"""Carries perturbation forces from the viewer to the simulation."""
|
|
|
|
state: np.ndarray
|
|
state_sig: int
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class StepControlSnapshot(Snapshot):
|
|
"""A snapshot sending step control state from viewer to sim each frame."""
|
|
|
|
pause_state: sim.PauseState
|
|
speed: float
|
|
noise_scale: float
|
|
noise_rate: float
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class ExitEvent(Event):
|
|
"""An event requesting to exit."""
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Message handling decorator.
|
|
# ---------------------------------------------------------------------------
|
|
|
|
_HANDLER_INFO_ATTR = '_studio_handler_info'
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class _HandlerInfo:
|
|
"""Metadata stamped on a decorated method."""
|
|
|
|
priority: int
|
|
message_type: type[Message]
|
|
|
|
|
|
class Priority(enum.IntEnum):
|
|
"""Execution priority levels for message handlers.
|
|
|
|
Handlers with higher values execute first. When multiple handlers share the
|
|
same priority, their relative order is undefined.
|
|
"""
|
|
|
|
INTERNAL = 1 # For built-in handlers.
|
|
LIBRARY = 10 # For library extensions.
|
|
USER = 100 # For user extensions. Default when no priority is specified.
|
|
CRITICAL = 1000 # For handlers that must run before everything else.
|
|
|
|
|
|
def handler(
|
|
fn: Callable[..., Any] | None = None, *, priority: int = Priority.USER
|
|
) -> Callable[..., Any]:
|
|
"""Decorator to mark a class method as a message handler.
|
|
|
|
The class method must accept exactly two arguments: self and a message event.
|
|
e.g., ``@handler`` or ``@handler(priority=...)``.
|
|
|
|
Args:
|
|
fn: The method to stamp with handler metadata.
|
|
priority: The priority of the handler.
|
|
|
|
Returns:
|
|
The stamped method.
|
|
"""
|
|
|
|
def stamp(target: Callable[..., Any]) -> Callable[..., Any]:
|
|
fn_name = getattr(target, '__name__', str(target))
|
|
params = list(inspect.signature(target).parameters.values())
|
|
if len(params) != 2:
|
|
raise TypeError(
|
|
f'{fn_name} must accept exactly two arguments, got {len(params)}'
|
|
)
|
|
|
|
message_type = params[1].annotation
|
|
if message_type is inspect.Parameter.empty:
|
|
raise TypeError(
|
|
f'{fn_name}: second parameter must have a type annotation'
|
|
)
|
|
|
|
if not (
|
|
isinstance(message_type, type) and issubclass(message_type, Message)
|
|
):
|
|
raise TypeError(
|
|
f'{fn_name}: second parameter type annotation {message_type} is not'
|
|
' a Message subclass'
|
|
)
|
|
|
|
info = _HandlerInfo(priority=priority, message_type=message_type)
|
|
setattr(target, _HANDLER_INFO_ATTR, info)
|
|
return target
|
|
|
|
# Used without parens: e.g., @handler
|
|
if fn is not None:
|
|
return stamp(fn)
|
|
|
|
# Used with parens: e.g., @handler() or @handler(priority=...)
|
|
return stamp
|