API reference¶
MjSim: Basic simulation¶
-
mujoco_py.load_model_from_path(path)¶ Loads model from path.
-
mujoco_py.load_model_from_xml(xml_string)¶ Loads and returns a PyMjModel model from a string containing XML markup. Saves the XML string used to create the returned model in model.xml.
-
mujoco_py.load_model_from_mjb(path)¶ Loads and returns a PyMjModel model from bytes encoded MJB. MJB is a MuJoCo-custom format that includes assets like meshes/textures.
-
class
mujoco_py.MjSim(model, data=None, nsubsteps=1, udd_callback=None)¶ MjSim represents a running simulation including its state.
Similar to Gym’s
MujocoEnv, it internally wraps aPyMjModeland aPyMjData.Parameters: model :
PyMjModelThe model to simulate.
data :
PyMjDataOptional container for the simulation state. Will be created if
None.nsubsteps : int
Optional number of MuJoCo steps to run for every call to
step(). Buffers will be swapped only once per step.udd_callback : fn(
MjSim) -> dictOptional callback for user-defined dynamics. At every call to
step(), it receives an MjSim objectsimcontaining the current user-defined dynamics state insim.udd_state, and returns the nextudd_stateafter applying the user-defined dynamics. This is useful e.g. for reward functions that operate over functions of historical state.Attributes
Methods
-
get_state()¶ Returns a copy of the simulator state.
-
render()¶ Renders view from a camera and returns image as an numpy.ndarray.
Args: - width (int): desired image width. - height (int): desired image height. - camera_name (str): name of camera in model. If None, the free
camera will be used.- depth (bool): if True, also return depth buffer
Returns: - rgb (uint8 array): image buffer from camera - depth (float array): depth buffer from camera (only returned
if depth=True)
-
reset()¶ Resets the simulation data and clears buffers.
-
save()¶ Saves the simulator model and state to a file as either a MuJoCo XML or MJB file. The current state is saved as a keyframe in the model file. This is useful for debugging using MuJoCo’s simulate utility.
Note that this doesn’t save the UDD-state which is part of MjSimState, since that’s not supported natively by MuJoCo. If you want to save the model together with the UDD-state, you should use the get_xml or get_mjb methods on MjModel together with MjSim.get_state and save them with e.g. pickle.
Args: - file (IO stream): stream to write model to. - format: format to use (either ‘xml’ or ‘mjb’) - keep_inertials (bool): if False, removes all <inertial>
properties derived automatically for geoms by MuJoco. Note that this removes ones that were provided by the user as well.
-
set_state()¶ Sets the state from an MjSimState. If the MjSimState was previously unflattened from a numpy array, consider set_state_from_flattened, as the defensive copy is a substantial overhead in an inner loop.
Args: - value (MjSimState): the desired state. - call_forward: optionally call sim.forward(). Called by default if
the udd_callback is set.
-
set_state_from_flattened()¶ This helper method sets the state from an array without requiring a defensive copy.
-
-
class
mujoco_py.MjSimState¶ Represents a snapshot of the simulator’s state.
This includes time, qpos, qvel, act, and udd_state.
Attributes
Methods
PyMjData: Time-dependent data¶
PyMjData and related classes are automatically generated from the MuJoCo C header files. For more information on this process, see Autogenerated wrappers. Their structure therefore directly follows the MuJoCo structs.
-
class
mujoco_py.PyMjData¶ -
Attributes
-
act¶
-
act_dot¶
-
active_contacts_efc_pos¶
-
actuator_force¶
-
actuator_length¶
-
actuator_moment¶
-
actuator_velocity¶
-
body_jacp¶
-
body_jacr¶
-
body_xmat¶
-
body_xpos¶
-
body_xquat¶
-
body_xvelp¶
-
body_xvelr¶
-
cacc¶
-
cam_xmat¶
-
cam_xpos¶
-
cdof¶
-
cdof_dot¶
-
cfrc_ext¶
-
cfrc_int¶
-
cinert¶
-
contact¶
-
crb¶
-
ctrl¶
-
cvel¶
-
efc_AR¶
-
efc_AR_colind¶
-
efc_AR_rowadr¶
-
efc_AR_rownnz¶
-
efc_D¶
-
efc_J¶
-
efc_JT¶
-
efc_JT_colind¶
-
efc_JT_rowadr¶
-
efc_JT_rownnz¶
-
efc_J_colind¶
-
efc_J_rowadr¶
-
efc_J_rownnz¶
-
efc_R¶
-
efc_aref¶
-
efc_b¶
-
efc_diagApprox¶
-
efc_force¶
-
efc_frictionloss¶
-
efc_id¶
-
efc_margin¶
-
efc_solimp¶
-
efc_solref¶
-
efc_state¶
-
efc_type¶
-
efc_vel¶
-
energy¶
-
geom_jacp¶
-
geom_jacr¶
-
geom_xmat¶
-
geom_xpos¶
-
geom_xvelp¶
-
geom_xvelr¶
-
light_xdir¶
-
light_xpos¶
-
maxuse_con¶
-
maxuse_efc¶
-
maxuse_stack¶
-
mocap_pos¶
-
mocap_quat¶
-
nbuffer¶
-
ncon¶
-
ne¶
-
nefc¶
-
nf¶
-
nstack¶
-
pstack¶
-
qLD¶
-
qLDiagInv¶
-
qLDiagSqrtInv¶
-
qM¶
-
qacc¶
-
qacc_unc¶
-
qacc_warmstart¶
-
qfrc_actuator¶
-
qfrc_applied¶
-
qfrc_bias¶
-
qfrc_constraint¶
-
qfrc_inverse¶
-
qfrc_passive¶
-
qfrc_unc¶
-
qpos¶
-
qvel¶
-
sensordata¶
-
set_joint_qpos¶
-
set_joint_qvel¶
-
set_mocap_pos¶
-
set_mocap_quat¶
-
site_jacp¶
-
site_jacr¶
-
site_xmat¶
-
site_xpos¶
-
site_xvelp¶
-
site_xvelr¶
-
solver¶
-
solver_fwdinv¶
-
solver_iter¶
-
solver_nnz¶
-
subtree_angmom¶
-
subtree_com¶
-
subtree_linvel¶
-
ten_length¶
-
ten_moment¶
-
ten_velocity¶
-
ten_wrapadr¶
-
ten_wrapnum¶
-
time¶
-
timer¶
-
userdata¶
-
warning¶
-
wrap_obj¶
-
wrap_xpos¶
-
xanchor¶
-
xaxis¶
-
xfrc_applied¶
-
ximat¶
-
xipos¶
Methods
-
get_body_jacp(name)¶ Get the entry in
jacpcorresponding to the body with the given name
-
get_body_jacr(name)¶ Get the entry in
jacrcorresponding to the body with the given name
-
get_body_ximat(name)¶ Get the entry in
ximatcorresponding to the body with the given name
-
get_body_xipos(name)¶ Get the entry in
xiposcorresponding to the body with the given name
-
get_body_xmat(name)¶ Get the entry in
xmatcorresponding to the body with the given name
-
get_body_xpos(name)¶ Get the entry in
xposcorresponding to the body with the given name
-
get_body_xquat(name)¶ Get the entry in
xquatcorresponding to the body with the given name
-
get_body_xvelp(name)¶ Get the entry in
xvelpcorresponding to the body with the given name
-
get_body_xvelr(name)¶ Get the entry in
xvelrcorresponding to the body with the given name
-
get_cam_xmat(name)¶ Get the entry in
xmatcorresponding to the cam with the given name
-
get_cam_xpos(name)¶ Get the entry in
xposcorresponding to the cam with the given name
-
get_camera_xmat(name)¶ Get the entry in
xmatcorresponding to the camera with the given name
-
get_camera_xpos(name)¶ Get the entry in
xposcorresponding to the camera with the given name
-
get_geom_jacp(name)¶ Get the entry in
jacpcorresponding to the geom with the given name
-
get_geom_jacr(name)¶ Get the entry in
jacrcorresponding to the geom with the given name
-
get_geom_xmat(name)¶ Get the entry in
xmatcorresponding to the geom with the given name
-
get_geom_xpos(name)¶ Get the entry in
xposcorresponding to the geom with the given name
-
get_geom_xvelp(name)¶ Get the entry in
xvelpcorresponding to the geom with the given name
-
get_geom_xvelr(name)¶ Get the entry in
xvelrcorresponding to the geom with the given name
-
get_joint_qpos(name)¶ Get the entry in
qposcorresponding to the joint with the given name
-
get_joint_qvel(name)¶ Get the entry in
qvelcorresponding to the joint with the given name
-
get_joint_xanchor(name)¶ Get the entry in
xanchorcorresponding to the joint with the given name
-
get_joint_xaxis(name)¶ Get the entry in
xaxiscorresponding to the joint with the given name
-
get_light_xdir(name)¶ Get the entry in
xdircorresponding to the light with the given name
-
get_light_xpos(name)¶ Get the entry in
xposcorresponding to the light with the given name
-
get_mocap_pos(name)¶ Get the entry in
poscorresponding to the mocap with the given name
-
get_mocap_quat(name)¶ Get the entry in
quatcorresponding to the mocap with the given name
-
get_site_jacp(name)¶ Get the entry in
jacpcorresponding to the site with the given name
-
get_site_jacr(name)¶ Get the entry in
jacrcorresponding to the site with the given name
-
get_site_xmat(name)¶ Get the entry in
xmatcorresponding to the site with the given name
-
get_site_xpos(name)¶ Get the entry in
xposcorresponding to the site with the given name
-
get_site_xvelp(name)¶ Get the entry in
xvelpcorresponding to the site with the given name
-
get_site_xvelr(name)¶ Get the entry in
xvelrcorresponding to the site with the given name
-
MjSimPool: Batched simulation¶
-
class
mujoco_py.MjSimPool¶ Keeps a pool of multiple MjSims and enables stepping them quickly in parallel.
Parameters: sims : list of
MjSimList of simulators that make up the pool.
nsubsteps:
Number of substeps to run on
step(). The individual simulators’nsubstepwill be ignored.Attributes
Methods
-
static
create_from_sim()¶ Create an
MjSimPoolby cloning the providedsima total ofnsimstimes. Returns the createdMjSimPool.Parameters: sim :
MjSimThe prototype to clone.
nsims : int
Number of clones to create.
-
forward()¶ Calls
mj_forwardon all simulations in parallel. Ifnsimsis specified, than only the firstnsimssimulator are forwarded.
-
reset()¶ Resets all simulations in pool. If
nsimsis specified, than only the firstnsimssimulators are reset.
-
step()¶ Calls
mj_stepon all simulations in parallel, withnsubstepsas specified when the pool was created.If
nsimsis specified, than only the firstnsimssimulator are stepped.
-
static
MjViewer: 3D rendering¶
-
class
mujoco_py.MjViewerBasic(sim)[source]¶ A simple display GUI showing the scene of an
MjSimwith a mouse-movable camera.MjViewerextends this class to provide more sophisticated playback and interaction controls.Parameters: sim :
MjSimThe simulator to display.
Attributes
Methods
-
class
mujoco_py.MjViewer(sim)[source]¶ Extends
MjViewerBasicto add video recording, interactive time and interaction controls.The key bindings are as follows:
- TAB: Switch between MuJoCo cameras.
- H: Toggle hiding all GUI components.
- SPACE: Pause/unpause the simulation.
- RIGHT: Advance simulation by one step.
- V: Start/stop video recording.
- T: Capture screenshot.
- I: Drop into
ipdbdebugger. - S/F: Decrease/Increase simulation playback speed.
- C: Toggle visualization of contact forces (off by default).
- D: Enable/disable frame skipping when rendering lags behind real time.
- R: Toggle transparency of geoms.
- M: Toggle display of mocap bodies.
Parameters: sim :
MjSimThe simulator to display.
Attributes
Methods