Spaces:
Running
Running
Download src/core/compose.js from VectorW/xlerobot-sim: direct link, hf CLI and curl.
- Browser
- Download file 2.95 kB
-
https://huggingface.co/spaces/VectorW/xlerobot-sim/resolve/main/src/core/compose.js
- Command line
-
hf download hf://spaces/VectorW/xlerobot-sim/src/core/compose.js
-
curl -L -o compose.js https://huggingface.co/spaces/VectorW/xlerobot-sim/resolve/main/src/core/compose.js
2.95 kB
| // 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); | |
| } | |