Zig bindings for the MuJoCo physics engine.
This package provides a way to build and interact with MuJoCo using Zig direct C interop feature.
Important
_These bindings are still a WIP.
Note
Run all the commands in this README from the root directory of this project.
-
To compile everything run
zig buildzig build -Doptimize=ReleaseFast # use -Doptimize=ReleaseFast until patching logic for the upstream mujoco is added. # Zig builds with more flags by default. # To reproduce the issue in upstream mujoco use -fsanitize=undefined flag
-
To compile & run the native
simulateorstudiobinary# -Dsuppress-nonnull due to tiny issues with upstream .cc and zig being more strict. zig build run-simulate -Dsuppress-nonnull # Or run-studio
-
To use
simulatevia zig use thezsimulatemodule or the examplezviewerbinary# Use -Doptimize=ReleaseFast in case you want to skip all checks -Dsuppress-nonnull works too zig build run-zviewer -Doptimize=ReleaseFast -
To use
studiovia zig use thezstudiomodule or the examplezstudiobinary# Use -Doptimize=ReleaseFast in case you want to skip all checks -Dsuppress-nonnull works too but debug builds are much slower than the C/C++ version, still needs tests, not sure about the cause zig build run-zstudio -Doptimize=ReleaseFast -
To run the
lqrexample inside the zigzviewer
Note
This was originally built inside the upstream mujoco repository so edit the filepath to your mujoco/models directory or copy it here.
# Use -Doptimize=ReleaseFast or -Dsuppress-nonnull
zig build run-zlqr_viewer -Doptimize=ReleaseFast
# To add noise or any args use -- after the flags
zig build run-zlqr_viewer -Doptimize=ReleaseFast -- --noiseTests live in zig/test/<name>_test.zig and are run from the top-level directory:
zig build test # tests that need no display
zig build test-display # renderer and viewer tests (need a display)
zig build test-<name> # a single test, e.g. test-minimize
zig build test-models # sweep over model/ (not part of `test`)Tests build in Debug by default, so Zig's undefined-behaviour checks also run on the C/C++ code they reach.
MuJoCo's own code (engine, simulate, plugins) trips the nonnull-attribute
check on zero-length memcpy/memset calls with a null pointer. These are
harmless in practice, but the check aborts the run before the other checks can
report anything:
simulate/simulate.cc:2343:memcpyofeq_active_(empty vector),neq = 0src/engine/engine_core_smooth.c:1641:mju_zero(efc_force, nefc),nefc = 0src/engine/engine_io.c:896:mju_copyInt(remaining, ...)(empty vector),nv = 0
To get past them, skip that one check for MuJoCo's code:
zig build test -Dsuppress-nonnullIt is off by default, so a plain zig build test in Debug stops at the first
of these findings. Release builds don't have the undefined-behaviour checks
and are unaffected.
Builds everything (C/C++ and Zig) with ThreadSanitizer to find data races in MuJoCo's thread pool, the simulate physics/render threads, the passive viewer and the rollout workers:
zig build test -Dsanitize-thread-
Improve the developer experience.
-
Improve the documentation & testing
-
Enable .wasm builds from zig, waiting for the zig package to update to 0.17.0.
