Add support for loading XML models with VFS in WASM bindings.

PiperOrigin-RevId: 874756898
Change-Id: If2c30aef4c690cf0852587261ca32a3b91448621
This commit is contained in:
Google DeepMind
2026-02-24 13:08:14 -08:00
committed by Copybara-Service
parent 09c7633a76
commit a83fa7299d
4 changed files with 79 additions and 10 deletions
+47 -2
View File
@@ -2512,7 +2512,7 @@ describe('MuJoCo WASM Bindings', () => {
vfs = new mujoco.MjVFS();
vfs.addBuffer(objFilename, new TextEncoder().encode(cube1));
binaryModel = mujoco.MjModel.mj_loadBinary(mjbFilename, vfs);
binaryModel = mujoco.MjModel.mj_loadModel(mjbFilename, vfs);
assertExists(binaryModel);
expect(mujoco.mj_sizeModel(binaryModel))
@@ -2542,7 +2542,7 @@ describe('MuJoCo WASM Bindings', () => {
const bufSize = mujoco.mj_sizeModel(model!);
vfs = new mujoco.MjVFS();
binaryModel = mujoco.MjModel.mj_loadBinary(mjbFilename, vfs);
binaryModel = mujoco.MjModel.mj_loadModel(mjbFilename, vfs);
assertExists(binaryModel);
expect(mujoco.mj_sizeModel(binaryModel)).toEqual(bufSize);
@@ -2558,4 +2558,49 @@ describe('MuJoCo WASM Bindings', () => {
}
});
it('should load XML with assets from 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`;
const xmlFilename = '/tmp/with_vfs.xml';
writeXMLFile(xmlFilename, xml);
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.MjModel.mj_loadXML(xmlFilename, 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();
unlinkXMLFile(xmlFilename);
}
});
});