Add mj_loadBinary binding to WASM.

This change adds an Emscripten binding for loading binary MuJoCo models (`.mjb`) from the virtual filesystem. The new binding, `mj_loadBinary`, takes a filename and an `MjVFS` object, enabling the loading of models that reference assets within the VFS.

PiperOrigin-RevId: 859740455
Change-Id: I9f9d8bbba0b57422e2356867e4282a1b04566475
This commit is contained in:
Google DeepMind
2026-01-22 13:06:48 -08:00
committed by Copybara-Service
parent 61f13cd8f1
commit e1992e62d7
5 changed files with 110 additions and 3 deletions
+10
View File
@@ -8017,6 +8017,15 @@ void mj_saveModel_wrapper(const MjModel& m, const StringOrNull& filename, const
mj_saveModel(m.get(), filename_.data(), buffer_.data(), static_cast<int>(buffer_.size()));
}
std::unique_ptr<MjModel> mj_loadModel_wrapper(std::string filename, const MjVFS& vfs) {
mjModel *model = mj_loadModel(filename.c_str(), vfs.get());
if (!model) {
printf("mj_loadModel: failed to load from mjb");
return nullptr;
}
return std::unique_ptr<MjModel>(new MjModel(model));
}
std::unique_ptr<MjSpec> parseXMLString_wrapper(const std::string &xml) {
char error[1000];
mjSpec *ptr = mj_parseXMLString(xml.c_str(), nullptr, error, sizeof(error));
@@ -11206,6 +11215,7 @@ EMSCRIPTEN_BINDINGS(mujoco_bindings) {
.property("uselimit", &MjLROpt::uselimit, &MjLROpt::set_uselimit, reference());
emscripten::class_<MjModel>("MjModel")
.class_function("mj_loadXML", &mj_loadXML_wrapper, take_ownership())
.class_function("mj_loadBinary", &mj_loadModel_wrapper, take_ownership())
.constructor<const MjModel &>()
// Binds the functions on MjModel that return accessors.
#define X_ACCESSOR(NAME, Name, OBJTYPE, field_name, nfield) \
+1 -1
View File
@@ -74,6 +74,7 @@ _SKIPPED_CLASS_METHODS: tuple[str, ...] = (
"mj_deleteModel",
"mj_deleteSpec",
"mj_deleteVFS",
"mj_loadModel",
"mj_loadXML",
"mj_makeData",
"mj_makeSpec",
@@ -139,7 +140,6 @@ _SKIPPED_MEMORY_FUNCTIONS: tuple[str, ...] = (
# go/keep-sorted start
"mj_freeLastXML",
"mj_freeStack",
"mj_loadModel",
"mj_loadModelBuffer",
"mj_markStack",
"mj_stackAllocByte",
+6 -2
View File
@@ -508,10 +508,14 @@ def _build_struct_bindings(
builder.line(".constructor<MjModel *>()")
builder.line(".constructor<const MjModel &, const MjData &>()")
elif w == "MjModel":
fn = common.wrapped_function_name(
f1 = common.wrapped_function_name(
introspect_functions.FUNCTIONS["mj_loadXML"]
)
builder.line(f'.class_function("mj_loadXML", &{fn}, take_ownership())')
builder.line(f'.class_function("mj_loadXML", &{f1}, take_ownership())')
f2 = common.wrapped_function_name(
introspect_functions.FUNCTIONS["mj_loadModel"]
)
builder.line(f'.class_function("mj_loadBinary", &{f2}, take_ownership())')
builder.line(".constructor<const MjModel &>()")
builder.line("""
// Binds the functions on MjModel that return accessors.
+9
View File
@@ -535,6 +535,15 @@ void mj_saveModel_wrapper(const MjModel& m, const StringOrNull& filename, const
mj_saveModel(m.get(), filename_.data(), buffer_.data(), static_cast<int>(buffer_.size()));
}
std::unique_ptr<MjModel> mj_loadModel_wrapper(std::string filename, const MjVFS& vfs) {
mjModel *model = mj_loadModel(filename.c_str(), vfs.get());
if (!model) {
printf("mj_loadModel: failed to load from mjb");
return nullptr;
}
return std::unique_ptr<MjModel>(new MjModel(model));
}
std::unique_ptr<MjSpec> parseXMLString_wrapper(const std::string &xml) {
char error[1000];
mjSpec *ptr = mj_parseXMLString(xml.c_str(), nullptr, error, sizeof(error));