The primary hook for accessing the MuJoCo simulation API from any component inside <MujocoCanvas>.
Narrow on isReady, isPending, or isError to get type-safe access to the API and refs.
Usage
Return Value
useMujoco() returns a UseMujocoResult with three variants:
Loading
Error
Ready
The API Object
The api object (available when isReady is true) provides methods grouped into categories:
- Simulation Control —
reset(), setPaused(), setSpeed(), step(), getTime()
- State Management —
saveState(), restoreState(), setQpos(), applyKeyframe()
- Forces —
applyForce(), applyTorque(), setExternalForce()
- Model Introspection —
getBodies(), getJoints(), getSensors()
- Model Mutation —
setGravity(), setBodyMass(), setGeomFriction()
- Spatial Queries —
raycast(), project2DTo3D(), getCanvasSnapshot()
- Scene Management —
loadScene()
IK control is provided by the useIkController() hook — see useIkController and IK Control.
Camera animation is provided by the standalone useCameraAnimation() hook — see useCameraAnimation.
Direct Model/Data Access
For advanced use cases, access the raw MuJoCo model and data objects:
Direct model/data access bypasses the API’s safety checks. Be careful with array indexing and avoid writing to arrays that the provider manages (e.g., qfrc_applied is zeroed each frame).
Notes
- Must be called from a component inside
<MujocoCanvas> or <MujocoPhysics>
- The
api object is stable (same reference across renders when status doesn’t change)
- Use
isReady / isPending / isError for type-safe narrowing instead of comparing status strings