Spaces:
Running
Running
File size: 2,952 Bytes
d72771b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | // Scene composition, as MuJoCo-GS-Web's SceneManager does it: the environment's XML with <include>
// elements for the robot and its objects as the first children of <mujoco>. The robot's XML is
// written next to the scene with meshdir pointing at ./assets/, where its meshes go.
//
// envhub/xlerobot_sim/scene.py composes scenes the same way, so the browser and Python compile the
// same model.
/** The environment XML with `includes` inserted right after the opening <mujoco> tag. */
export function composeScene(envXml, includes, modelName) {
const head = /<mujoco\b[^>]*>/.exec(envXml);
if (!head) throw new Error("The environment XML has no <mujoco> element");
let tag = head[0];
tag = /\bmodel="[^"]*"/.test(tag)
? tag.replace(/\bmodel="[^"]*"/, `model="${modelName}"`)
: tag.replace("<mujoco", `<mujoco model="${modelName}"`);
const inserted = includes.map((file) => `<include file="${file}"/>`).join("");
return envXml.slice(0, head.index) + tag + inserted + envXml.slice(head.index + head[0].length);
}
/** The robot XML with its meshes looked up in ./assets/ next to the scene. */
export function relocateMeshdir(robotXml) {
return robotXml.replace(/meshdir="[^"]*"/g, 'meshdir="./assets/"');
}
/**
* Write a composed scene into MuJoCo's virtual file system under `dir` and return the scene path.
* `robot` is {name, xml, meshes: [[file, bytes]], objectsXml?}; `env` is {xml, files?: [[path, bytes]],
* includes?: [path]}: `includes` are written files to include too (an environment's decor).
*/
export function writeScene(mujoco, dir, env, robot, { modelName = "scene" } = {}) {
const FS = mujoco.FS;
removeTree(FS, dir);
FS.mkdirTree(`${dir}/assets`);
const includes = [];
if (robot) {
FS.writeFile(`${dir}/${robot.name}.xml`, relocateMeshdir(robot.xml));
for (const [file, bytes] of robot.meshes) {
const path = `${dir}/assets/${file}`;
FS.mkdirTree(path.slice(0, path.lastIndexOf("/")));
FS.writeFile(path, bytes);
}
for (const [file, content] of robot.extraFiles ?? []) FS.writeFile(`${dir}/${file}`, relocateMeshdir(content));
includes.push(`${robot.name}.xml`);
if (robot.objectsXml) {
FS.writeFile(`${dir}/objects.xml`, robot.objectsXml);
includes.push("objects.xml");
}
}
for (const [path, bytes] of env.files ?? []) {
const full = `${dir}/${path}`;
FS.mkdirTree(full.slice(0, full.lastIndexOf("/")));
FS.writeFile(full, bytes);
}
includes.push(...(env.includes ?? []));
FS.writeFile(`${dir}/scene.xml`, composeScene(env.xml, includes, modelName));
return `${dir}/scene.xml`;
}
function removeTree(FS, path) {
let stat;
try {
stat = FS.stat(path);
} catch {
return; // nothing there
}
if (!FS.isDir(stat.mode)) {
FS.unlink(path);
return;
}
for (const name of FS.readdir(path)) {
if (name !== "." && name !== "..") removeTree(FS, `${path}/${name}`);
}
FS.rmdir(path);
}
|