MuJoCo Physics Simulator

3.6.0 · active · verified Sat Apr 11

MuJoCo (Multi-Joint dynamics with Contact) is a physics engine developed by Google DeepMind, used for research and development in robotics, biomechanics, and other areas. The Python bindings allow programmatic interaction with models, simulation, and data. It releases frequently, with minor version updates typically every few weeks to months.

Warnings

Install

Imports

Quickstart

This example loads a basic XML model of a sphere dropping onto a plane and simulates it for 100 steps, printing its initial and final vertical position. It demonstrates the core `MjModel`, `MjData`, and `mj_step` functionalities.

import mujoco
import numpy as np

# Create a simple XML model string for a sphere on a plane
xml_string = """
<mujoco model="sphere_drop">
  <worldbody>
    <geom name="floor" type="plane" size="0 0 0.1" material="grid"/>
    <body name="sphere" pos="0 0 1">
      <geom name="ball" type="sphere" size="0.1" material="sphere_mat"/>
    </body>
  </worldbody>
  <asset>
    <texture type="2d" name="grid" builtin="checker" width="512" height="512" rgb1="0.1 0.2 0.3" rgb2="0.2 0.3 0.4" mark="edge" markrgb="0.8 0.8 0.8"/>
    <material name="grid" texture="grid" texrepeat="1 1" texuniform="true"/>
    <material name="sphere_mat" rgba="0.7 0 0 1"/>
  </asset>
</mujoco>
"""

# Load the model and create data
model = mujoco.MjModel.from_xml_string(xml_string)
data = mujoco.MjData(model)

# Simulate for 100 steps
print(f"Initial sphere position: {data.qpos[2]:.3f}") # Assuming Z is the 3rd element of qpos
for i in range(100):
    mujoco.mj_step(model, data)

print(f"Final sphere position: {data.qpos[2]:.3f}")

view raw JSON →