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:
Google DeepMind
2026-03-12 07:35:08 -07:00
committed by Copybara-Service
parent df0bc58578
commit b294d15558
7 changed files with 159 additions and 28 deletions
+1 -10
View File
@@ -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();