Getting Started¶
Abstract
MuJoCo Mojo is a high-level Python framework designed to turn static MuJoCo simulations into robust, scalable, and stochastic experiment pipelines.
Installation¶
Install mujoco-mojo using your preferred package manager. We recommend uv for speed and dependency management.
Optional Extras
Some features require extra packages:
mujoco-mojo[optimize]for Optuna-based optimizationmujoco-mojo[mesh-decomp]for CoACD mesh decompositionmujoco-mojo[sensai]for the Dojo's SensAI assistantmujoco-mojo[docs]to build this documentation on your own machine (see Reading the Docs Offline)
Install mujoco-mojo[all] to get all of them.
Reading the Docs Offline¶
This documentation ships with the package, so you can read the version that matches what you have installed without leaving your machine:
Open the address it prints. The first time, it builds the site for your version into ~/.mujoco-mojo/docs/v<version>, which takes a moment and needs the docs extra (pip install mujoco-mojo[docs]). After that it serves the saved copy at once, with no extra packages.
mujoco-mojo docs buildbuilds without serving. Add--rebuildto build again.mujoco-mojo docs listshows which versions are built and where, including sites that older installs built.mujoco-mojo docs serve --version 2.7.1serves one of them.
The package carries the pages, the search, and the API reference. Two things need a connection:
- Screenshots and other large images link to GitHub at the tag of your version, so they only show when you are online.
- Links to other projects' documentation, such as Python's and pydantic's, are only made if you were online when the site was built.
Core Concepts¶
Before writing code, it is helpful to understand the three pillars of a Mojo project:
- The MojoModel: This is your "Source of Truth." It contains the
mjcfobject (your model) and thestochasobject (distributions and random variables). - This class relies upon
pydanticV2 to statically type and validate entries. - The Generate-Runtime Pattern: Mojo separates Generation (building the XML and sampling random values) from Runtime (the physics loop where forces and logic are applied).
Example: Generate and Runtime Functions
- Request-Based Telemetry: You don't (have to) manually log data. Instead, you "Request" that specific geoms, sites, or custom forces be tracked by the
SignalManager.
The MJCF Object Model¶
The mojo.mjcf object is designed to be isomorphic to the MuJoCo XML schema.
You Can Do It!
If you know how to write an XML file, you know how to use mojo.mjcf.
It is recommended to reference the official XML Reference for specifics on the XML schema.
<worldbody>becomesmojo.WorldBody().<body name="box">becomesmojo.Body(name=mojo.BodyName("box")).- Names such as
mojo.BodyNameare really strings, typed this way so your type checker catches mix-ups, such as aBodyNamewhere aSiteNameis needed. - Attributes like
posandrgbaare strictly typed using NumPy arrays and Pydantic validation.
This design ensures that there is "no magic". You are simply building a MuJoCo model using a strongly-typed Python API instead of a fragile string-based XML file.
Warning: Default Values
Some attributes in this package make use of default values. Wherever possible, the default values match what is stated in the XML Reference.
The to_xml method found in all XMLModel (which is most objects in mujoco_mojo.mjcf) has an argument (exclude_default) which will omit serializing fields set as default.
If your use case depends on a default value, pin it instead. MuJoCo may change its defaults and this package may fall behind, which would leave you using a "default" that is no longer the default.
Random Values and Distributions¶
Mojo treats stochasticity as a first-class citizen. Instead of using np.random directly, you define a Distribution and sample it through the MojoModel.
Note: Extra Reading
For additional details on the stochastic tools provided, see the stochas documentation
Example: Sampling a Distribution with MojoModel
Why use Mojo's sampling?¶
- Reproducibility: Every draw is anchored to a global seed.
- Named Values: Every random draw is saved as a
NamedValue. This means your results database automatically knows exactly what "spring_stiffness" was used for Trial #42. - It also helps to ensure you do not accidentally overwrite or modify a value.
- Global Overrides: You can run a job and tell Mojo to ignore the distribution and force a specific
NamedValuefor testing.
MuJoCo Syntax Highlighting¶
MuJoCo does not currently ship first-party Python typing stubs. To enable proper autocomplete and static type checking (Pylance, Pyright, or ty), generate local stubs using pybind11-stubgen.
Thanks to the work by @kevinzakka and @mluogh-xdof for figuring all this out!
- From your project root, generate stubs into a local
typings/directory:
pybind11-stubgen mujoco -o typings/ --numpy-array-wrap-with-annotated
pybind11-stubgen mujoco.viewer -o typings/ --numpy-array-wrap-with-annotated
pybind11-stubgen mujoco.studio -o typings/ --numpy-array-wrap-with-annotated
- Then point your type checker at them in
pyproject.toml. Pylance in VSCode picks uptypings/on its own, but a checker run from the command line or a pre-commit hook needs to be told. For Pyright:
For ty, which finds a .venv in the project root on its own:
Tip: Scaffold a New Project
Before diving into the generate script, run mujoco-mojo init in a new directory to get a working project skeleton (simulation.py, run.sh, and reloaded.sh) pre-wired and ready to edit.
See the Initializing a Project guide for details.
Success
This concludes our basic overview of the core concepts to MuJoCo Mojo. The next guide will cover how to get started with assembling your first model with the generate script.