mujoco-python
Skill15k starsChanged 3 months agoApache-2.0View on GitHub
Python simulation lifecycle (MjSpec, MjModel, MjData), simulation stepping (mj_step, mj_step1, mj_step2, mj_forward), named access (data.body, model.geom), bind(), contact and touch sensors, sensordata, keyframe resets, integrators, mju_ spatial math (quaternions, poses), energy diagnostics. Use for Python physics simulation, state access, stepping loops, sensors. Do NOT use for model building/editing (mujoco-spec-editing), headless rendering (mujoco-rendering), GPU/TPU (mujoco-accelerated), or GUI/viewers (mujoco-gui, mujoco-studio).
Trending
Quiet since August.thin dataabout the whole repository
Who's talking about google-deepmind/mujoco
- A Hacker News story“MuJoCo – Advanced Physics Simulation”Hacker News · Apr 19, 2026 · 116 points
- openai/mujoco-py“Cannot change site position programmatically when pos is set to zero in XML”GitHub · Mar 7, 2024 · 7 reactions
- dtcxzyw/llvm-opt-benchmark“Wishlist for new benchmarks”GitHub · Jan 17, 2024 · 5 reactions
- astral-sh/uv“MuJoCo `mjpython` fails with uvx”GitHub · Nov 8, 2024 · 4 reactions
- jrouwe/JoltPhysics“[Feature Request] Soft-soft collisions and self-collisions”GitHub · Dec 12, 2023 · 4 reactions
The biggest 5 of 13 mentions linking to the repository that our crawl found. Each line is their own headline.
See all 13
Talked about on GitHub, Hacker News and Bluesky (13 times).
On GitHub · 10
- Mar 7, 2024Cannot change site position programmatically when pos is set to zero in XML — openai/mujoco-py7 reactions
- Jan 17, 2024Wishlist for new benchmarks — dtcxzyw/llvm-opt-benchmarksays it tested it5 reactions · 15 comments
- Nov 8, 2024MuJoCo `mjpython` fails with uvx — astral-sh/uv4 reactions · 7 comments
- Dec 12, 2023[Feature Request] Soft-soft collisions and self-collisions — jrouwe/JoltPhysics4 reactions · 5 comments
- Jul 22, 2026passive adhesion — google-deepmind/mujoco_warp3 reactions · 1 comment
5 more on GitHub
- Aug 26, 2026Fix JAX FFI VMA propagation — NVIDIA/warp2 reactions · 6 comments
- Oct 31, 20251st party WASM bindings are now available for Alpha testing — zalo/mujoco_wasmsays it tested it2 reactions · 21 comments
- Aug 7, 2025reset_data — google-deepmind/mujoco_warp2 reactions · 1 comment
- Dec 27, 2025fix: CGL: try to init pixel format without explicit profile before falling back to profile selection — rust-windowing/glutin1 reaction · 3 comments
- Nov 8, 2024Fix glfw name when using system installed version — google-deepmind/mujoco_mpc1 reaction · 1 comment
On Hacker News · 2
- Apr 19, 2026MuJoCo – Advanced Physics Simulation116 points · 24 comments
- Oct 18, 2023MuJoCo 31 point
On Bluesky · 1
- Apr 22, 2026MuJoCo – Advanced Physics Simulation Discussion — @hn100.bsky.social1 likes and repost
In this repository5 other files it gives its agents.
- mujoco-acceleratedSkill
- mujoco-guiSkill
- mujoco-renderingSkill
- mujoco-spec-editingSkill
- mujoco-studioSkill
The file
We split this skill into the rules it gives an agent
4 rules. About 0 are common, 1 is distinctive, and 3 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
3
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 166
When building models programmatically with MjSpec, use bind() to map spec element handles directly to compiled data:
- Very specific: line 211
Before reading sensors or positions at time zero, call mj_forward() instead of mj_step().
- Distinctive: line 227
Use keyframes to reset configurations reliably:
- Very specific: line 397
Orientation keywords are mutually exclusive: When adding geoms, bodies, or sites, provide only one of quat, euler, axisangle, xyaxes, or …
≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
MuJoCo Python: Simulation, Sensors & Math
[!TIP]
Related skills:
- mujoco-spec-editing — Procedural model editing and composing scenes with
MjSpec.- mujoco-rendering — Offscreen image rendering, cameras, depth, and segmentation.
- mujoco-accelerated — GPU/TPU physics simulation with MJX (JAX) and MJWarp (CUDA).
- mujoco-studio — Interactive viewers, Studio viewer apps, and plugin architecture.
[!CAUTION]
- NEVER use
mj_name2id,model.name2id, or raw integer indexing (e.g.,data.qpos[0],data.xpos[body_id]) to look up elements by name. Always use named access (data.body('name').xpos) orbind()instead.- Do not use dm_control / PyMJCF lookup patterns.
model.find('body', 'name'),.find_body(), andphysics.nameddo not exist in MuJoCo 3.x. Use direct accessors:.body('name'),.geom('name')etc. onMjSpec,MjModel, andMjData.
1. Compilation Lifecycle: MjSpec → MjModel → MjData
MuJoCo models follow a three-stage lifecycle:
MjSpec ──spec.compile()──▶ MjModel ──MjData(model)──▶ MjData
│ │ │
│ (mutable blueprint) │ (compiled, mostly frozen) │ (simulation state)
│ │ │
└── spec.recompile(m, d) ─────┴────────────────────────────┘MjSpec: Mutable data structure defining the simulation elements and hierarchy.MjModel: Compiled, optimized physics model containing constant and precomputed parameters. Structural changes require recompilation.MjData: Dynamic simulation state (positions, velocities, forces, contacts, diagnostic buffers).
Python Lifecycle
import mujoco
# 1. Build or load spec
spec = mujoco.MjSpec()
body = spec.worldbody.add_body(name='pendulum', pos=[0, 0, 1])
body.add_geom(type=mujoco.mjtGeom.mjGEOM_SPHERE, size=[0.1])
body.add_joint(name='hinge', type=mujoco.mjtJoint.mjJNT_HINGE, axis=[0, 1, 0])
# 2. Compile to model and instantiate data
model = spec.compile()
data = mujoco.MjData(model)
mujoco.mj_forward(model, data)
# 3. Dynamic recompilation (preserves existing state)
body2 = spec.worldbody.add_body(name='ball2', pos=[1, 0, 1])
body2.add_geom(size=[0.1])
body2.add_freejoint()
model, data = spec.recompile(model, data) # Always reassign return valuesC++ Lifecycle
#include <mujoco/mujoco.h>
// 1. Create or load spec
mjSpec* spec = mj_makeSpec();
mjbBody* body = mj_addBody(spec, nullptr);
mj_addGeom(body, mjGEOM_SPHERE, 0.1, 0, 0);
// 2. Compile to model and create data
char error[1000] = {0};
mjModel* model = mj_compile(spec, error, sizeof(error));
mjData* data = mj_makeData(model);
// 3. Step or evaluate
mj_forward(model, data);
// 4. Clean up allocated resources
mj_deleteData(data);
mj_deleteModel(model);
mj_deleteSpec(spec);Loading and Serializing
# Direct model loading from file or string
model = mujoco.MjModel.from_xml_path('model.xml')
model = mujoco.MjModel.from_xml_string(xml_str)
model = mujoco.MjModel.from_binary_path('model.mjb') # Precompiled binary format
# Loading mutable spec from file or string
spec = mujoco.MjSpec.from_file('model.xml')
spec = mujoco.MjSpec.from_string(xml_str)
# Serialization
xml_string = spec.to_xml()
spec.encode('model.xml', model)[!CAUTION]
mujoco.load_model_from_pathdoes not exist in MuJoCo 3.x. Usemujoco.MjModel.from_xml_path(path)ormujoco.MjSpec.from_file(path).
2. Accessing State: Named Access & bind()
1. Named Access (on MjModel and MjData)
In Python, access elements by calling the element-type accessor with the element name:
# Reading state
torso_pos = data.body('torso').xpos # NumPy view (3,)
knee_angle = data.joint('knee').qpos # Shape depends on joint type
ball_size = model.geom('ball').size # NumPy view (3,)
# Writing controls and state
data.actuator('motor').ctrl = 1.0 # Writable view
data.joint('knee').qpos = 0.5Supported element accessors: body, geom, joint (alias jnt), site,
camera (alias cam), light, mesh, skin, texture (alias tex),
material (alias mat), pair, equality (alias eq), tendon (alias
ten), actuator, sensor, numeric, text, tuple, keyframe (alias
key).
[!WARNING]
Named access returns views, not copies. In a simulation loop, modifying the simulation changes the array in-place. If logging or storing history, always call
.copy():# ❌ WRONG — all history items will point to the same final state history.append(data.body('torso').xpos) # ✅ RIGHT — records distinct coordinates history.append(data.body('torso').xpos.copy())
2. bind() (Bridges MjSpec elements → MjModel / MjData)
When building models programmatically with MjSpec, use bind() to map spec
element handles directly to compiled data:
geom = spec.worldbody.add_geom(name='ball', size=[0.1])
joint = spec.worldbody.add_joint(name='hinge', type=mujoco.mjtJoint.mjJNT_HINGE)
model = spec.compile()
data = mujoco.MjData(model)
# Read through bind
radius = model.bind(geom).size[0]
ball_pos = data.bind(geom).xpos
# Write through bind — always use .set()
data.bind(joint).set('qpos', 0.5)
# Bind lists of spec elements in batch
joints = [spec.joint('j1'), spec.joint('j2')]
joint_angles = data.bind(joints).qpos3. Simulation Stepping & Execution Pipeline
Stepping Pipeline (mj_step vs mj_step1 / mj_step2)
mj_step(model, data): Advances the simulation by one timestep (model.opt.timestep). It computes kinematics, forces, collision constraints, accelerations, and numerical integration in one full pass.- Split Stepping (
mj_step1+mj_step2): Used when custom controller forces or external computations depend on the current kinematic state:
# 1. Advance position kinematics and sensor computations
mujoco.mj_step1(model, data)
# 2. Apply control torques or external forces based on current kinematics
data.ctrl[:] = compute_feedback_control(data.qpos, data.qvel)
data.qfrc_applied[:] = compute_external_forces(data)
# 3. Complete dynamic acceleration and integration
mujoco.mj_step2(model, data)Sensor and Kinematic Initialization (mj_forward)
Before reading sensors or positions at time zero, call mj_forward() instead of
mj_step(). mj_forward evaluates kinematics and sensor outputs from the
current state without advancing simulation time:
# ✅ RIGHT — initialize sensors and kinematics at t=0
mujoco.mj_forward(model, data)
initial_obs = data.sensordata.copy()
# Advance physics
for _ in range(num_steps):
mujoco.mj_step(model, data)Keyframe Resets & Restoring State
Use keyframes to reset configurations reliably:
# Python: reset to named keyframe
key_id = model.key('home').id
mujoco.mj_resetDataKeyframe(model, data, key_id)
mujoco.mj_forward(model, data) # Recompute kinematics and sensorsIn C++:
int key_id = mj_name2id(m, mjOBJ_KEY, "home");
if (key_id >= 0) {
mj_resetDataKeyframe(m, d, key_id);
mj_forward(m, d);
}Integrators & Numerical Stability
Configured via model.opt.integrator:
mjINT_EULER(default in earlier models): Standard semi-implicit Euler. Fast but can suffer from instability with stiff springs or damping.mjINT_RK4: 4th-order Runge-Kutta. Highly accurate for smooth, passive systems without contacts; not suitable for contact-rich scenes.mjINT_IMPLICIT/mjINT_IMPLICITFAST: Backward Euler with analytical or finite-difference derivatives. Provides superior numerical stability for stiff tendons, actuators, and complex contact manifolds.
4. Contact & Force Sensing: Declarative Sensors
Why data.contact Should NOT Be Used for Observations
data.contact is a variable-length buffer that changes size and ordering every
timestep based on active collision pairs:
# ❌ WRONG — variable size and non-deterministic order break ML policies
for c in data.contact:
if c.geom1 == target_id:
force = ...Declarative Contact Sensors
<contact> sensors declaratively filter contacts, reduce them to a fixed
number of slots, and write deterministic vectors directly into data.sensordata:
<sensor>
<!-- Detect contacts between gripper and object: fixed 3 slots -->
<contact name="grip_contact"
body1="gripper" body2="object"
num="3" data="found force normal"
reduce="maxforce"/>
<!-- Net wrench on an entire body -->
<contact name="object_net"
body1="object"
data="force torque"
reduce="netforce"/>
</sensor>Reading contact sensors in Python:
contact_data = data.sensor('grip_contact').data # Always fixed lengthTouch Sensors for Scalar Normal Force
For scalar surface contact forces (e.g., fingertips), use touch sensors
attached to a site. Make sure the site is large enough to encompass the geom:
site = body.add_site(name='fingertip', pos=[0, 0, 0.05], size=[0.02])
spec.add_sensor(
name='touch_sensor',
type=mujoco.mjtSensor.mjSENS_TOUCH,
objname=site.name,
objtype=mujoco.mjtObj.mjOBJ_SITE,
)5. Spatial Mathematics (mju_ Library)
MuJoCo provides an optimized spatial mathematics library under the mju_
namespace in both C++ and Python.
[!IMPORTANT]
MuJoCo quaternions are scalar-first:
[w, x, y, z]. SciPy and ROS use scalar-last:[x, y, z, w]. Swizzle components when converting:# MuJoCo -> SciPy/ROS scipy_quat = mj_quat[[1, 2, 3, 0]] # SciPy/ROS -> MuJoCo mj_quat = scipy_quat[[3, 0, 1, 2]]
Quaternion Operations
res = np.zeros(3)
mujoco.mju_rotVecQuat(res, vec, quat) # Rotate vector by quaternion
quat = np.zeros(4)
mujoco.mju_mat2Quat(quat, mat3x3) # 3x3 rotation matrix → quaternion
mujoco.mju_quat2Mat(mat, quat) # Quaternion → 3x3 matrix
mujoco.mju_axisAngle2Quat(quat, axis, angle) # Axis-angle → quaternion
mujoco.mju_euler2Quat(quat, euler, 'xyz') # Euler angles → quaternion
mujoco.mju_mulQuat(res_q, q1, q2) # Compose quaternions (q1 * q2)
mujoco.mju_negQuat(res_q, quat) # Quaternion conjugate (inverse)
mujoco.mju_quatZ2Vec(quat, vec) # Quaternion rotating Z-axis to vectorPose Operations
# Compose poses (pos1/quat1 then pos2/quat2)
mujoco.mju_mulPose(pos_res, quat_res, pos1, quat1, pos2, quat2)
# Invert pose
mujoco.mju_negPose(pos_res, quat_res, pos, quat)
# Transform 3D point by pose
mujoco.mju_trnVecPose(res, pos, quat, vec)6. Energy & Diagnostics
MuJoCo can continuously compute total mechanical energy to diagnose simulation stability:
# Enable energy monitoring
model.opt.enableflags |= mujoco.mjtEnableBit.mjENBL_ENERGY
mujoco.mj_step(model, data)
potential_energy = data.energy[0]
kinetic_energy = data.energy[1]
total_energy = potential_energy + kinetic_energyIn passive, unforced, undamped systems, total_energy must remain constant.
Growing energy indicates numerical integration instability (requiring smaller
timesteps or implicit integrators).
7. Compute Backend Selection
| Feature | C++ Engine (Default) | MJX (JAX) | MJWarp (CUDA) |
| Import | import mujoco | from mujoco import mjx | import mujoco_warp as mjw |
| Primary Use | Real-time control, interactive tools, full solver support | Differentiable physics, large batch RL | High-throughput batch rollouts |
| Hardware | CPU | GPU / TPU | NVIDIA GPU |
| Precision | float64 | float32 | float32 |
| Solvers | Newton, CG, PGS, noslip | Newton, CG | Newton, CG |
| Plugins | All plugins | Not supported | SDF only |
(For detailed GPU and TPU execution patterns, refer to mujoco-accelerated.)
8. Common Gotchas & Best Practices
- Computed fields are read-only: Fields like
data.xpos,data.xmat, anddata.xquatare output buffers. Never assign to them directly; change inputs (data.qpos,data.ctrl) and callmj_forward()ormj_step(). - Duplicate names are forbidden: Spec element names must be unique within each element type.
- Orientation keywords are mutually exclusive: When adding geoms, bodies,
or sites, provide only one of
quat,euler,axisangle,xyaxes, orzaxis. - Euler sequences: Lowercase characters denote intrinsic rotations
(
'xyz'), uppercase denote extrinsic rotations ('XYZ'). - Coordinate convention: +Z is up, gravity is
[0, 0, -9.81]. - Repeating controls with
nstep: Callingmj_step(model, data, nstep=N)repeats the current control acrossNsteps. For dynamic feedback control, step one timestep at a time.
9. Key References
Documentation
- Python Bindings Reference
- Simulation & Computation Guide
- C API Functions
- C API Types & Structs
Source Code Examples
- Python Bindings Tests
- C API Header
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

