Interface to C API

When the user requires extra flexibility that MuJoCo-rs does not offer, direct FFI bindings to the C language structs and functions can be used. Most structs have their attributes public. The exceptions are the model-editing structs (mjs*, apart from the plain-data mjsOrientation, mjsElement and mjsAuthored) and the structs whose raw fields are unsafe to write directly (mjLogConfig, mjLogMessage, mjrRendererInfo). Read and write the fields of the mjs* structs, mjLogConfig and mjLogMessage through the accessors of the matching wrapper.

Direct FFI bindings are available inside the mujoco_rs::mujoco_c module.

Mixing between direct FFI bindings and the rest of MuJoCo-rs is also possible. Some MuJoCo-rs types are direct FFI aliases, while core simulation/runtime types (such as MjModel and MjData) are safe wrapper structs. To obtain a reference to the FFI type inside a wrapper, call either ffi() or unsafe ffi_mut(). For example, MjData::ffi_mut and MjModel::ffi can be used as arguments to mj_step.

Warning

ffi_mut() bypasses wrapper invariants. Incorrect writes through raw FFI fields can violate safety assumptions and may cause undefined behavior.

unsafe { mujoco_rs::mujoco_c::mj_step(model.ffi(), data.ffi_mut()) }

FFI bindings can also be used to read raw attributes directly:

model.ffi().nq