Skip to main content

Utilities to programatically create MuJoCo XML configuration files

Project description

mjxml

Programmatic, type-checked helpers for composing MuJoCo XML. Define assets and worldbody elements in Python (with Pydantic validation), serialize to XML, and write files you can load in MuJoCo.

Highlights

  • Strong typing and validation via Pydantic v2.
  • Easy XML serialization: to_xml(), to_xml_str(), write().
  • Assets: TextureAsset, MeshAsset, HFieldAsset, ModelAsset.
  • Bodies: Geom (with many MuJoCo attributes), WorldBody base.
  • Utilities: Defaults blocks, rotation helpers, and runtime type protocols.

Requirements

  • Python >= 3.13
  • Runtime deps: numpy, pydantic

Install

From PyPI:

pip install mjxml

Development

This project relies on uv. To setup a virtual environment, just do:

uv sync --dev
export PYTHONPATH=src

Quickstart

The snippets below sketch a typical flow: create assets, place them in the worldbody, and export a MuJoCo XML file.

1. Define assets with validation

from mjxml.asset.texture import TextureAsset
from mjxml.asset.mesh import MeshAsset

texture = TextureAsset(
    name="tex_grid",
    file="albedo.png",
    type="2d",
    colorspace="srgb",
    content_type="image/png",
    nchannel=3,
)

mesh = MeshAsset(
    name="m_cube",
    file="cube.obj",
    content_type="model/obj",
    scale=[1.0, 1.0, 1.0],
    inertia="exact",
)

Each asset only emits the attributes you set; Pydantic validators ensure parameters such as nchannel or scale stay within MuJoCo’s requirements.

2. Create bodies and geoms

from mjxml.body import Body, Geom

body = Body(name="torso")
body.add(
    Geom(
        name="box1",
        type="box",
        size=[0.5, 0.5, 0.5],
        material=None,
        rgba=[0.8, 0.2, 0.2, 1.0],
        friction=[0.5, 0.1, 0.01],
    )
)

Geoms automatically infer fields such as condim from the friction vector length and warn when MuJoCo would ignore an attribute.

3. Assemble a complete MujocoModel

from mjxml.model import MujocoModel

model = MujocoModel(name="demo")
model.add_asset(texture)
model.add_asset(mesh)
model.add_body(body)

root = model.to_xml()
print(model.to_xml_str())
model.write("scene.xml")

MujocoModel deduplicates identical assets, appends worldbody, actuator, and contact sections, and returns a ready-to-save ElementTree root.

Testing

Run the unit tests with pytest.

pytest -q -s

Status

This repository is under active development; expect minor API adjustments. A high-level MujocoModel aggregator is planned but not yet included.

Roadmap

  • [✅] High-level Wrapper: MujocoModel class to aggregate assets, worldbody, and other sections.
  • [✅] Body Elements: Support for joint, site, camera.
  • [=] Sections: Support for actuator, sensor, equality, contact, tendon.
  • [❌] IO: Parsing existing XML files for round-trip editing.
  • [❌] Docs: Comprehensive API documentation and more examples.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mjxml-0.2.1.tar.gz (22.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mjxml-0.2.1-py3-none-any.whl (32.4 kB view details)

Uploaded Python 3

File details

Details for the file mjxml-0.2.1.tar.gz.

File metadata

  • Download URL: mjxml-0.2.1.tar.gz
  • Upload date:
  • Size: 22.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for mjxml-0.2.1.tar.gz
Algorithm Hash digest
SHA256 b6bd93c3c007364ee30e7267cffd3519d7cc37c8e4116a4153a2a829bff2ebbf
MD5 bf177ceb2c1e8adb6ff8446910ae1b36
BLAKE2b-256 32406e2d9999db67766a208b31ccb536797d461e1155d90f2d8f16631c2335f5

See more details on using hashes here.

File details

Details for the file mjxml-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: mjxml-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 32.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for mjxml-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 03f165ad41024d9d3a3676916e81d4aed2f4e4e09bbf169c9fe1a766850a4833
MD5 963aaf5e19dab55ab23129a3a4d5d23a
BLAKE2b-256 12b80601df01ccb7a8fef3a658fdef106100b80d964e71ebb7420f853eddcbed

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page