Metadata-Version: 2.5
Name: mjcf-to-urdf
Version: 0.1.0
Summary: Convert MuJoCo MJCF to URDF, string in and string out, using the compiled model for correct inertias
Project-URL: Repository, https://github.com/jeff-hykin/mjcf-to-urdf
Author-email: Jeff Hykin <jeff.hykin@gmail.com>
License-Expression: MIT
License-File: LICENSE
Keywords: mjcf,mujoco,robotics,urdf
Requires-Python: >=3.10
Requires-Dist: mujoco>=3.3
Requires-Dist: numpy
Description-Content-Type: text/markdown

# mjcf-to-urdf

Convert a MuJoCo MJCF robot to URDF, string in and string out, with no files written. Masses and inertias come from MuJoCo's compiled model (so inertia MuJoCo infers from geoms is right), links keep the MJCF body names, and mesh files are referenced as-is (never re-exported), with MuJoCo's mesh re-centering, `scale`, `refpos` and `refquat` folded into each mesh's `<origin>`/`scale`.

```sh
pip install mjcf-to-urdf
```

```python
from mjcf_to_urdf import mjcf_to_urdf, mjcf_file_to_urdf, mjcf_spec_to_urdf

urdf = mjcf_file_to_urdf("robot/scene.xml")           # mesh paths stay relative to robot/, as in the MJCF
urdf = mjcf_to_urdf(xml_text, mesh_dir="robot/meshes")  # mesh_dir replaces the MJCF's meshdir
urdf = mjcf_to_urdf(xml_text, assets={"meshes/base.stl": stl_bytes})
urdf = mjcf_spec_to_urdf(mujoco.MjSpec.from_file("robot.xml"))
```

## What maps to what

- **Bodies** become links named after the body; a body's link frame is its MuJoCo frame moved to its joint's anchor (identical when the joint sits at the body origin, as in most robots).
- **Joints**: hinge → `revolute` (`continuous` when unlimited), slide → `prismatic`, ball → three `continuous` joints about x, y, z (`<name>_x/_y/_z`), free on the root body → dropped (the root link is the floating base), free elsewhere → `floating`. A body with several joints gets massless intermediate links `<joint>_link`; bodies with one joint keep a single link. Limits come from `range` (shifted by `ref`), effort from `actuatorfrcrange`, an actuator's `forcerange`, or a motor's `ctrlrange`, times its gear. Velocity is never written (MuJoCo has no joint velocity limit).
- **Inertials**: compiled mass, center of mass and principal inertia (`ipos`, `iquat` → origin rpy, diagonal inertia).
- **Geoms**: box, sphere, cylinder, mesh, and capsule as a cylinder plus a sphere at each end. Colliding geoms (`contype`/`conaffinity` nonzero) become `<collision>`; non-colliding geoms in a default-visible group become `<visual>` with the material/geom `rgba`. A link with no visual geoms is drawn with its visible colliding geoms.
- **Root**: the URDF root is the single body under the worldbody; a `world` link is added only when there are several, or the robot is attached to the world by a joint.

Not converted: planes, ellipsoids, heightfields, SDF and inline-vertex meshes (skipped with a warning), ball-joint cone limits, tendons, equality constraints, sites, cameras, sensors, textures, and non-uniform mesh scale combined with `refquat`. Loading the URDF back into MuJoCo needs `<compiler boundmass=... boundinertia=...>` when a body has several joints, since MuJoCo rejects massless moving links.

## Checked against MuJoCo

Tests load every URDF back into MuJoCo and compare link poses at random joint configurations, total mass and composite inertia against the original MJCF (`uv run pytest`; `-m "not network"` skips the robots cloned from GitHub). `uv run python tests/compare.py` compares with [mjcf-urdf-simple-converter](https://github.com/Yasu31/mjcf_urdf_simple_converter) 0.7.3:

| robot | converter | joints | max link pos err (m) | max link rot err (rad) | mass err (rel) | inertia err (rel) | links | visual+collision |
|---|---|---|---|---|---|---|---|---|
| franka_emika_panda (11 bodies, 9 joints) | mjcf-to-urdf | 9 | 1.8e-09 | 8.4e-08 | 0.0e+00 | 2.0e-10 | 11 | 81 |
| | mjcf-urdf-simple-converter | 7 | 8.5e-16 | 6.3e-08 | 5.2e-04 | 3.7e-04 | 21 | 71 |
| Deep Robotics M20 (17 bodies, 17 joints) | mjcf-to-urdf | 16 | 1.7e-16 | 4.2e-08 | 0.0e+00 | 8.2e-12 | 17 | 44 |
| | mjcf-urdf-simple-converter | 16 | 0.0e+00 | 4.2e-08 | 4.9e-04 | 6.4e-04 | 35 | 17 |
| unitree_go2 (13 bodies, 13 joints) | mjcf-to-urdf | 12 | 6.9e-17 | 4.7e-08 | 0.0e+00 | 4.5e-10 | 13 | 56 |
| | mjcf-urdf-simple-converter | 12 | 0.0e+00 | 4.7e-08 | 8.5e-04 | 1.1e-03 | 27 | 33 |
| universal_robots_ur5e (7 bodies, 6 joints) | mjcf-to-urdf | 6 | 3.0e-09 | 5.2e-08 | 0.0e+00 | 3.0e-09 | 7 | 45 |
| | mjcf-urdf-simple-converter | 6 | 2.5e-16 | 5.2e-08 | 2.9e-04 | 5.8e-04 | 14 | 20 |

mjcf-urdf-simple-converter drops slide joints (the panda's fingers) and primitive geoms, adds a dummy link per joint, and writes re-exported OBJ meshes to disk. Errors near 1e-8 are the 9 significant digits written.
