Add from_xml_string to MuJoCo WASM bindings.
This change introduces new functions to load an `mjModel` from an XML string. Two overloads are provided: one that creates a temporary VFS and one that uses a MjVFS arg. Also updated other places where `from_xml_string` should be used: demos, readme, etc. PiperOrigin-RevId: 882581762 Change-Id: I8428196f8f56ae9630fcd4f0bb6763cdc1c43da0
This commit is contained in:
committed by
Copybara-Service
parent
df0bc58578
commit
b294d15558
+1
-1
@@ -307,7 +307,7 @@ The function `mjv_updateScene` populates an `mjvScene` object with information
|
||||
from `mjModel` and `mjData`.
|
||||
```typescript
|
||||
// Create instances of the necessary structs.
|
||||
const model = mujoco.MjModel.loadFromXML(xmlContent);
|
||||
const model = mujoco.MjModel.from_xml_string(xmlContent);
|
||||
const data = new mujoco.MjData(model);
|
||||
const scene = new mujoco.MjvScene(model, 1000);
|
||||
const option = new mujoco.MjvOption();
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
|
||||
#include <emscripten.h>
|
||||
#include <emscripten/bind.h>
|
||||
#include <emscripten/em_asm.h>
|
||||
#include <emscripten/val.h>
|
||||
|
||||
#include <algorithm>
|
||||
@@ -8361,6 +8362,39 @@ std::unique_ptr<MjModel> mj_loadModel_wrapper(std::string filename, const MjVFS&
|
||||
return std::unique_ptr<MjModel>(new MjModel(model));
|
||||
}
|
||||
|
||||
std::unique_ptr<MjModel> from_xml_string_wrapper_1(const std::string& xml) {
|
||||
mjVFS vfs;
|
||||
mj_defaultVFS(&vfs);
|
||||
const char* filename = "model.xml";
|
||||
int add_result = mj_addBufferVFS(&vfs, filename, xml.c_str(), xml.length());
|
||||
if (add_result != 0) {
|
||||
mj_deleteVFS(&vfs);
|
||||
mju_error("Could not add XML string to VFS: %d", add_result);
|
||||
}
|
||||
char error[1000];
|
||||
mjModel* model = mj_loadXML(filename, &vfs, error, sizeof(error));
|
||||
mj_deleteVFS(&vfs);
|
||||
if (!model) {
|
||||
mju_error("Loading error: %s\n", error);
|
||||
}
|
||||
return std::unique_ptr<MjModel>(new MjModel(model));
|
||||
}
|
||||
|
||||
std::unique_ptr<MjModel> from_xml_string_wrapper_2(const std::string& xml, const MjVFS& vfs) {
|
||||
std::string filename = "model.xml";
|
||||
int add_result = mj_addBufferVFS(vfs.get(), filename.c_str(), xml.c_str(), xml.length());
|
||||
if (add_result != 0) {
|
||||
mju_error("Could not add XML string to VFS: %d", add_result);
|
||||
}
|
||||
char error[1000];
|
||||
mjModel* model = mj_loadXML(filename.c_str(), vfs.get(), error, sizeof(error));
|
||||
mj_deleteFileVFS(vfs.get(), filename.c_str());
|
||||
if (!model) {
|
||||
mju_error("Loading error: %s\n", error);
|
||||
}
|
||||
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));
|
||||
@@ -11596,12 +11630,16 @@ EMSCRIPTEN_BINDINGS(mujoco_bindings) {
|
||||
.property("useexisting", &MjLROpt::useexisting, &MjLROpt::set_useexisting, reference())
|
||||
.property("uselimit", &MjLROpt::uselimit, &MjLROpt::set_uselimit, reference());
|
||||
emscripten::class_<MjModel>("MjModel")
|
||||
// mj_loadXML is deprecated and will be removed in a future release
|
||||
.class_function("mj_loadXML", emscripten::select_overload<std::unique_ptr<MjModel>(std::string)>(&mj_loadXML_wrapper_1))
|
||||
.class_function("from_xml_path", emscripten::select_overload<std::unique_ptr<MjModel>(std::string)>(&mj_loadXML_wrapper_1))
|
||||
.class_function("mj_loadXML", emscripten::select_overload<std::unique_ptr<MjModel>(std::string, const MjVFS&)>(&mj_loadXML_wrapper_2))
|
||||
.class_function("from_xml_path", emscripten::select_overload<std::unique_ptr<MjModel>(std::string, const MjVFS&)>(&mj_loadXML_wrapper_2))
|
||||
// mj_loadModel is deprecated and will be removed in a future release
|
||||
.class_function("mj_loadModel", &mj_loadModel_wrapper)
|
||||
.class_function("from_binary_path", &mj_loadModel_wrapper)
|
||||
.class_function("from_xml_string", emscripten::select_overload<std::unique_ptr<MjModel>(const std::string&)>(&from_xml_string_wrapper_1))
|
||||
.class_function("from_xml_string", emscripten::select_overload<std::unique_ptr<MjModel>(const std::string&, const MjVFS&)>(&from_xml_string_wrapper_2))
|
||||
.class_function("from_xml_path", emscripten::select_overload<std::unique_ptr<MjModel>(std::string)>(&mj_loadXML_wrapper_1))
|
||||
.class_function("from_xml_path", emscripten::select_overload<std::unique_ptr<MjModel>(std::string, const MjVFS&)>(&mj_loadXML_wrapper_2))
|
||||
.constructor<const MjModel &>()
|
||||
// Binds the functions on MjModel that return accessors.
|
||||
#define X_ACCESSOR(NAME, Name, OBJTYPE, field_name, nfield) \
|
||||
@@ -13382,6 +13420,8 @@ EMSCRIPTEN_BINDINGS(mujoco_bindings) {
|
||||
// as using std::optional<MjVFS> caused memory errors due to missing copy/move constructors.
|
||||
function("mj_compile", emscripten::select_overload<std::unique_ptr<MjModel>(const MjSpec&)>(&mj_compile_wrapper_1));
|
||||
function("mj_compile", emscripten::select_overload<std::unique_ptr<MjModel>(const MjSpec&, const MjVFS&)>(&mj_compile_wrapper_2));
|
||||
function("from_xml_string", emscripten::select_overload<std::unique_ptr<MjModel>(const std::string&)>(&from_xml_string_wrapper_1));
|
||||
function("from_xml_string", emscripten::select_overload<std::unique_ptr<MjModel>(const std::string&, const MjVFS&)>(&from_xml_string_wrapper_2));
|
||||
|
||||
emscripten::class_<WasmBuffer<float>>("FloatBuffer")
|
||||
.constructor<int>()
|
||||
|
||||
@@ -549,29 +549,46 @@ def _build_struct_bindings(
|
||||
MJDATA_ACCESSORS
|
||||
#undef X_ACCESSOR""".lstrip())
|
||||
elif w == "MjModel":
|
||||
builder.line(
|
||||
"// mj_loadXML is deprecated and will be removed in a future release"
|
||||
)
|
||||
builder.line(
|
||||
'.class_function("mj_loadXML",'
|
||||
" emscripten::select_overload<std::unique_ptr<MjModel>(std::string)>(&mj_loadXML_wrapper_1))"
|
||||
)
|
||||
builder.line(
|
||||
'.class_function("from_xml_path",'
|
||||
" emscripten::select_overload<std::unique_ptr<MjModel>(std::string)>(&mj_loadXML_wrapper_1))"
|
||||
)
|
||||
builder.line(
|
||||
'.class_function("mj_loadXML",'
|
||||
" emscripten::select_overload<std::unique_ptr<MjModel>(std::string,"
|
||||
" const MjVFS&)>(&mj_loadXML_wrapper_2))"
|
||||
)
|
||||
builder.line(
|
||||
'.class_function("from_xml_path",'
|
||||
" emscripten::select_overload<std::unique_ptr<MjModel>(std::string,"
|
||||
" const MjVFS&)>(&mj_loadXML_wrapper_2))"
|
||||
)
|
||||
f2 = common.wrapped_function_name(
|
||||
introspect_functions.FUNCTIONS["mj_loadModel"]
|
||||
)
|
||||
builder.line(
|
||||
"// mj_loadModel is deprecated and will be removed in a future"
|
||||
" release"
|
||||
)
|
||||
builder.line(f'.class_function("mj_loadModel", &{f2})')
|
||||
builder.line(f'.class_function("from_binary_path", &{f2})')
|
||||
builder.line(
|
||||
'.class_function("from_xml_string",'
|
||||
" emscripten::select_overload<std::unique_ptr<MjModel>(const"
|
||||
" std::string&)>(&from_xml_string_wrapper_1))"
|
||||
)
|
||||
builder.line(
|
||||
'.class_function("from_xml_string",'
|
||||
" emscripten::select_overload<std::unique_ptr<MjModel>(const"
|
||||
" std::string&, const MjVFS&)>(&from_xml_string_wrapper_2))"
|
||||
)
|
||||
builder.line(
|
||||
'.class_function("from_xml_path",'
|
||||
" emscripten::select_overload<std::unique_ptr<MjModel>(std::string)>(&mj_loadXML_wrapper_1))"
|
||||
)
|
||||
builder.line(
|
||||
'.class_function("from_xml_path",'
|
||||
" emscripten::select_overload<std::unique_ptr<MjModel>(std::string,"
|
||||
" const MjVFS&)>(&mj_loadXML_wrapper_2))"
|
||||
)
|
||||
builder.line(".constructor<const MjModel &>()")
|
||||
builder.line("""
|
||||
// Binds the functions on MjModel that return accessors.
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
|
||||
#include <emscripten.h>
|
||||
#include <emscripten/bind.h>
|
||||
#include <emscripten/em_asm.h>
|
||||
#include <emscripten/val.h>
|
||||
|
||||
#include <algorithm>
|
||||
@@ -746,6 +747,39 @@ std::unique_ptr<MjModel> mj_loadModel_wrapper(std::string filename, const MjVFS&
|
||||
return std::unique_ptr<MjModel>(new MjModel(model));
|
||||
}
|
||||
|
||||
std::unique_ptr<MjModel> from_xml_string_wrapper_1(const std::string& xml) {
|
||||
mjVFS vfs;
|
||||
mj_defaultVFS(&vfs);
|
||||
const char* filename = "model.xml";
|
||||
int add_result = mj_addBufferVFS(&vfs, filename, xml.c_str(), xml.length());
|
||||
if (add_result != 0) {
|
||||
mj_deleteVFS(&vfs);
|
||||
mju_error("Could not add XML string to VFS: %d", add_result);
|
||||
}
|
||||
char error[1000];
|
||||
mjModel* model = mj_loadXML(filename, &vfs, error, sizeof(error));
|
||||
mj_deleteVFS(&vfs);
|
||||
if (!model) {
|
||||
mju_error("Loading error: %s\n", error);
|
||||
}
|
||||
return std::unique_ptr<MjModel>(new MjModel(model));
|
||||
}
|
||||
|
||||
std::unique_ptr<MjModel> from_xml_string_wrapper_2(const std::string& xml, const MjVFS& vfs) {
|
||||
std::string filename = "model.xml";
|
||||
int add_result = mj_addBufferVFS(vfs.get(), filename.c_str(), xml.c_str(), xml.length());
|
||||
if (add_result != 0) {
|
||||
mju_error("Could not add XML string to VFS: %d", add_result);
|
||||
}
|
||||
char error[1000];
|
||||
mjModel* model = mj_loadXML(filename.c_str(), vfs.get(), error, sizeof(error));
|
||||
mj_deleteFileVFS(vfs.get(), filename.c_str());
|
||||
if (!model) {
|
||||
mju_error("Loading error: %s\n", error);
|
||||
}
|
||||
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));
|
||||
@@ -845,6 +879,8 @@ EMSCRIPTEN_BINDINGS(mujoco_bindings) {
|
||||
// as using std::optional<MjVFS> caused memory errors due to missing copy/move constructors.
|
||||
function("mj_compile", emscripten::select_overload<std::unique_ptr<MjModel>(const MjSpec&)>(&mj_compile_wrapper_1));
|
||||
function("mj_compile", emscripten::select_overload<std::unique_ptr<MjModel>(const MjSpec&, const MjVFS&)>(&mj_compile_wrapper_2));
|
||||
function("from_xml_string", emscripten::select_overload<std::unique_ptr<MjModel>(const std::string&)>(&from_xml_string_wrapper_1));
|
||||
function("from_xml_string", emscripten::select_overload<std::unique_ptr<MjModel>(const std::string&, const MjVFS&)>(&from_xml_string_wrapper_2));
|
||||
|
||||
emscripten::class_<WasmBuffer<float>>("FloatBuffer")
|
||||
.constructor<int>()
|
||||
|
||||
+1
-10
@@ -205,10 +205,7 @@ class App {
|
||||
}
|
||||
|
||||
loadModel(xmlContent: string) {
|
||||
// Write xml as a file so that mujoco can find it
|
||||
(mujoco as any).FS.writeFile('/working/model.xml', xmlContent);
|
||||
|
||||
this.mjModel = mujoco.MjModel.mj_loadXML('/working/model.xml');
|
||||
this.mjModel = mujoco.MjModel.from_xml_string(xmlContent);
|
||||
if (!app.mjModel) {
|
||||
throw new Error('Failed to load model');
|
||||
}
|
||||
@@ -437,8 +434,6 @@ function setupWindowEvents() {
|
||||
// Tip: put "window.dispatchEvent(new Event('unload'))" in the console to test
|
||||
window.addEventListener('unload', () => {
|
||||
app.dispose();
|
||||
|
||||
(mujoco as any).FS.unmount('/working');
|
||||
});
|
||||
|
||||
window.addEventListener('keydown', (event) => {
|
||||
@@ -464,10 +459,6 @@ async function main() {
|
||||
try {
|
||||
mujoco = await loadMujoco();
|
||||
|
||||
// Set up emscripten virtual file system
|
||||
(mujoco as any).FS.mkdir('/working');
|
||||
(mujoco as any).FS.mount((mujoco as any).MEMFS, {root: '.'}, '/working');
|
||||
|
||||
app = new App();
|
||||
|
||||
setupWindowEvents();
|
||||
|
||||
@@ -2603,4 +2603,56 @@ describe('MuJoCo WASM Bindings', () => {
|
||||
}
|
||||
});
|
||||
|
||||
it('should load a model from an XML string', () => {
|
||||
let model: MjModel|null = null;
|
||||
try {
|
||||
model = mujoco.from_xml_string(TEST_XML);
|
||||
assertExists(model);
|
||||
expect(model.nbody).toBe(5);
|
||||
expect(model.ngeom).toBe(3);
|
||||
} finally {
|
||||
model?.delete();
|
||||
}
|
||||
});
|
||||
|
||||
it('should load a model from an XML string with VFS', () => {
|
||||
const xml = `
|
||||
<mujoco>
|
||||
<asset>
|
||||
<mesh file="cube.obj"/>
|
||||
</asset>
|
||||
<worldbody>
|
||||
<geom type="mesh" mesh="cube"/>
|
||||
</worldbody>
|
||||
</mujoco>`;
|
||||
|
||||
const cube1 = `
|
||||
v -1 -1 1
|
||||
v 1 -1 1
|
||||
v -1 1 1
|
||||
v 1 1 1
|
||||
v -1 1 -1
|
||||
v 1 1 -1
|
||||
v -1 -1 -1
|
||||
v 1 -1 -1`;
|
||||
|
||||
let model: MjModel|null = null;
|
||||
let vfs: MjVFS|null = null;
|
||||
try {
|
||||
vfs = new mujoco.MjVFS();
|
||||
vfs.addBuffer('cube.obj', new TextEncoder().encode(cube1));
|
||||
assertExists(vfs);
|
||||
|
||||
model = mujoco.from_xml_string(xml, vfs);
|
||||
assertExists(model);
|
||||
expect(model.nmesh).toBe(1);
|
||||
|
||||
const meshId =
|
||||
mujoco.mj_name2id(model, mujoco.mjtObj.mjOBJ_MESH.value, 'cube');
|
||||
expect(meshId).toBeGreaterThanOrEqual(0);
|
||||
} finally {
|
||||
model?.delete();
|
||||
vfs?.delete();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -20,9 +20,6 @@ declare function loadMujoco(): Promise<MainModule>;
|
||||
async function main() {
|
||||
const mujoco: MainModule = await loadMujoco();
|
||||
|
||||
(mujoco as any).FS.mkdir('/working');
|
||||
(mujoco as any).FS.mount((mujoco as any).MEMFS, {root: '.'}, '/working');
|
||||
|
||||
const xmlContent = `
|
||||
<mujoco model="Box falling">
|
||||
<option viscosity="1"/>
|
||||
@@ -37,13 +34,12 @@ async function main() {
|
||||
</worldbody>
|
||||
</mujoco>`;
|
||||
|
||||
(mujoco as any).FS.writeFile('/working/hello.xml', xmlContent);
|
||||
let model: MjModel|undefined;
|
||||
let data: MjData|undefined;
|
||||
|
||||
try {
|
||||
console.log('Hello world!: Loading model');
|
||||
model = mujoco.MjModel.mj_loadXML('/working/hello.xml');
|
||||
model = mujoco.MjModel.from_xml_string(xmlContent);
|
||||
if (!model) {
|
||||
throw new Error('Failed to load model');
|
||||
}
|
||||
@@ -57,7 +53,6 @@ async function main() {
|
||||
} finally {
|
||||
model?.delete();
|
||||
data?.delete();
|
||||
(mujoco as any).FS.unmount('/working');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user