MujocoStateReader#
- class torchrl.render.backends.MujocoStateReader[source]#
Reads simulator state from TorchRL-native and Gym MuJoCo environments.
The reader keeps simulator state separate from policy observations. It accepts environments exposing a
get_state()method that returns a TensorDict or mapping with a"qpos"entry, as well as TorchRLGymWrapperinstances around Gymnasium MuJoCo environments.Examples
>>> from types import SimpleNamespace >>> import numpy as np >>> from torchrl.render.backends import MujocoStateReader >>> env = SimpleNamespace( ... data=SimpleNamespace( ... qpos=np.array([0.0, 1.0]), ... qvel=np.array([0.5, 0.0]), ... time=0.25, ... ) ... ) >>> state = MujocoStateReader().capture(env) >>> state["qpos"].tolist() [0.0, 1.0]
- capture(env: Any) TensorDictBase[source]#
Returns a detached snapshot of the environment’s MuJoCo state.
- Parameters:
env – TorchRL-native MuJoCo environment, Gym-backed MuJoCo environment, or object exposing MuJoCo
datadirectly.- Returns:
A TensorDict containing
qposand any availableqvel,act,ctrl,mocap_pos,mocap_quat, andtimeentries.- Raises:
TypeError – If the environment does not expose a supported state.
KeyError – If the exposed state does not contain
qpos.