MJCF

Also known as: MuJoCo XML format

On this page 7

MJCF (the MuJoCo XML format) is the native modeling language of the MuJoCo physics engine: a single XML document that describes bodies, joints, geometry, actuators, sensors and solver settings for a complete physics simulation. In robotics simulation it plays the role that URDF plays in ROS tooling, but it was designed around a physics engine first, so it expresses contact parameters, tendons, equality constraints and numerical options that a pure kinematic description cannot hold.

An MJCF file is not executed directly. The MuJoCo compiler parses the XML and produces a compact, optimized mjModel structure, which the simulator then steps forward in time. Understanding this model-compile-simulate pipeline explains most of the format’s design choices, including its default classes and its automatic computation of inertia.

How an MJCF model is structured

An MJCF model is a tree of nested bodies inside a root element named mujoco, plus several global sections that configure the compiler, the physics options and the elements attached to that tree. The kinematic tree lives in `worldbody`: every body is declared inside its parent, and a joint declared in a body defines the motion of that body relative to its parent. This nesting replaces the explicit parent and child references used by URDF.

The top-level sections are optional and can appear in almost any order. The following table summarizes the sections a robotics engineer meets most often.

SectionPurposeTypical content
compilerControls how the XML is parsedAngle units, mesh directory, inertia rules
optionSets global physics parametersTimestep, integrator, gravity, solver
defaultDefines reusable attribute classesJoint damping, geom friction, actuator gains
assetDeclares shared resourcesMeshes, textures, materials, height fields
worldbodyHolds the kinematic treeBodies, joints, geoms, sites, cameras, lights
actuatorMaps controls to forcesMotors, position servos, muscles
sensorDefines simulated measurementsJoint position, IMU, force, touch
equalityAdds extra constraintsClosed loops, welds, coupled joints
Main top-level sections of an MJCF file

Inside a body, geoms carry collision and visual geometry, sites mark named reference frames for sensors and tendons, and joints of type hinge, slide, ball or free define degrees of freedom. A body without any joint is rigidly welded to its parent, which is a frequent source of surprise for newcomers who expect a default floating base.

Physics and contact parameters in MJCF

MJCF exposes the numerical behavior of the solver directly in the model file, which is its most important difference from description formats built for visualization. The option element sets the timestep, with a default of 0.002 s, the integrator (Euler, RK4, implicit or implicitfast), the friction cone type and the constraint solver (Newton, CG or PGS).

MuJoCo models contact as a soft constraint rather than as a rigid impulse. Each geom pair can tune how stiff and how damped that constraint is, and the parameters below determine whether a gripper holds an object, slips or jitters.

AttributeDefaultEffect on simulation
condim3Contact dimensionality: 1 frictionless, 3 sliding, 4 torsional, 6 rolling
friction1, 0.005, 0.0001Sliding, torsional and rolling coefficients
solref0.02, 1Time constant and damping ratio of the constraint
solimp0.9, 0.95, 0.001, 0.5, 2Impedance curve that controls softness versus penetration
margin0Distance at which a contact becomes active
contype and conaffinity1 and 1Bit masks that filter which geoms can collide
Contact attributes that shape simulated behavior

A practical rule is that the solref time constant should be at least twice the timestep; shorter values make the constraint stiffer than the integrator can resolve and produce energy gain and chattering. Mass and inertia can be specified explicitly with an inertial element or computed from geom shapes with a default density of 1000 kg per cubic meter, which is convenient but often unrealistic for hollow robot links.

MJCF compared with URDF and SDFormat

MJCF, URDF and SDFormat all describe robots in XML, but they target different layers of the tool chain. URDF is the lingua franca of ROS for kinematics and visualization, SDFormat describes whole worlds for Gazebo, and MJCF describes a model for one specific engine with full control over its physics.

CapabilityMJCFURDFSDFormat
Kinematic structureNested bodiesLinks and joints by referenceLinks and joints by reference
Closed kinematic loopsVia equality constraintsNot supportedSupported in format, engine-dependent
Actuator modelsBuilt in, several typesOnly transmission tagsThrough plugins
Tendons and musclesBuilt inNot supportedNot supported natively
Solver options in fileYesNoPartly, per engine
Multiple robots and worldYes, one sceneOne robot onlyYes, full worlds
Comparison of common robot description formats

MuJoCo can load a URDF and convert it to MJCF, and saving the compiled model produces an equivalent MJCF file. The conversion is a good starting point, but engineers usually add contact exclusions, actuators and default classes by hand, because URDF carries none of that information. Macro tools such as Xacro have no direct role in MJCF, which instead relies on include files and default classes for reuse.

Default classes, assets and model reuse

Default classes are the main mechanism for keeping large MJCF files maintainable. A class sets attribute values for joints, geoms, actuators or other elements, and any element can reference it with the class attribute; nested classes inherit and override their parent values. The childclass attribute on a body applies a class to its whole subtree.

Typical practice is to define separate classes for visual geoms and collision geoms, so that detailed meshes are rendered while simplified primitives handle contact. Assets keep meshes and textures in one place, and the include element splits a model into files, for example one file per robot and one scene file that combines them.

  • Use primitives for collision (capsules, boxes, spheres) wherever possible, because primitive contacts are faster and more stable than mesh contacts.
  • Remember that mesh geoms collide as convex hulls, so concave parts such as a hollow cup need decomposition into several convex pieces.
  • Set angle units explicitly in the compiler, because MJCF defaults to degrees while many converted models assume radians.
  • Enable autolimits so that joint and actuator ranges activate limits without a separate flag.

Actuators, sensors and common modeling mistakes

MJCF actuators connect a control signal to a joint, tendon or site through a gain, a bias and an optional activation state. Shortcut elements such as motor, position and velocity expand into the general actuator with preset parameters, which makes it possible to model torque-controlled and servo-controlled robots without writing a controller in code.

Most failed simulations trace back to a small set of modeling errors. The table below lists symptoms that experienced users recognize quickly.

SymptomLikely causeTypical fix
Robot explodes at startInitial interpenetration of geomsExclude adjacent pairs or fix the initial pose
Gripper drops objectsLow condim or soft contactUse condim 4 or 6 and stiffer solref
Joints rotate far too muchDegrees and radians confusedSet the compiler angle attribute
Base falls through floorMissing contype matchAlign the collision bit masks
Unstable, jittery motionTimestep too large for stiffnessReduce timestep or soften constraints
Frequent MJCF modeling problems

Sensors in MJCF are noise-free by default; the noise attribute is stored for user code but not applied by the engine. Any policy trained for real hardware therefore needs explicit noise injection and domain randomization, otherwise it learns to rely on perfect measurements that no physical robot provides.

In simple terms

MJCF is a recipe card for a virtual robot. It lists every part, how the parts are connected, what they weigh, how they touch other objects and which motors move them. The MuJoCo program reads this card and builds a moving digital copy of the robot.

Unlike some other robot files, which mostly describe shape, MJCF also says how the physics should behave: how slippery a surface is, how firm a collision feels and how small each time step should be. That is why researchers use it when realistic motion and contact matter more than pretty pictures.

A practical example

A research team training a robotic hand to rotate a cube starts from a vendor URDF, converts it to MJCF and then refines the file. They replace the finger meshes with capsule collision geoms, set condim to 4 on the fingertips so that torsional friction resists slipping, and add a position actuator for each of the 16 joints with gains matched to the real servos.

They keep the timestep at 0.002 s, define one default class for visual meshes and another for collision, and add a randomization script that varies friction by about 30 percent and cube mass between episodes. After roughly a day of parallel training, the learned policy transfers to the physical hand and completes repeated cube rotations, largely because the MJCF model captured contact behavior closely enough for the gap between simulation and reality to stay small.