// Load a scene in Node from assets/, composed exactly as the page composes it (src/core/compose.js). // Used by the checks, the demonstration generator and the policy evaluation in tools/. import { readFileSync, readdirSync } from "node:fs"; import { dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; import loadMujoco from "@mujoco/mujoco"; import { writeScene } from "../src/core/compose.js"; import { Sim } from "../src/core/sim.js"; import { XLeRobot } from "../src/robots/xlerobot.js"; import { G1Robot } from "../src/robots/g1.js"; import { workClock } from "../src/robots/nav.js"; // Long searches slice their work by estimates here, not by the machine's clock: a run comes out the same // however busy the machine is (src/robots/nav.js, workClock). let worked = 0; workClock.now = (ms = 0) => (worked += ms); export const ROOT = join(dirname(fileURLToPath(import.meta.url)), ".."); export const ASSETS = join(ROOT, "assets"); let mujocoPromise = null; export function mujocoModule() { mujocoPromise ??= loadMujoco(); return mujocoPromise; } export function environments() { return JSON.parse(readFileSync(join(ASSETS, "environments", "index.json"), "utf8")); } export function robotMeta(id) { return JSON.parse(readFileSync(join(ASSETS, "robots", id, "robot.json"), "utf8")); } function filesUnder(dir, prefix = "") { const out = []; for (const entry of readdirSync(dir, { withFileTypes: true })) { const rel = prefix ? `${prefix}/${entry.name}` : entry.name; if (entry.isDirectory()) out.push(...filesUnder(join(dir, entry.name), rel)); else out.push(rel); } return out; } /** * Compose and compile `envId` with robot `robotId`, with the objects the scene asks for: the robot's * ("robot"), its own file, or those in its XML. `objects: "robot"` puts the robot's objects in any scene * instead of the scene's own file (the pick policy's demonstrations use MuJoCo-GS-Web's tabletop * objects); `objects: "none"` leaves both out. */ export async function loadScene(envId = "tabletop", robotId = "xlerobot", { controlHz = 50, objects } = {}) { const mujoco = await mujocoModule(); const env = environments().find((e) => e.id === envId); if (!env) throw new Error(`unknown environment ${envId}`); const meta = robotMeta(robotId); const robotDir = join(ASSETS, "robots", robotId); const meshDir = join(robotDir, "assets"); const choice = objects ?? (env.objects === "robot" ? "robot" : "scene"); const robot = { name: robotId, xml: readFileSync(join(robotDir, meta.xml), "utf8"), meshes: filesUnder(meshDir).map((f) => [f, readFileSync(join(meshDir, f))]), objectsXml: choice === "robot" && meta.objects ? readFileSync(join(robotDir, meta.objects), "utf8") : null, }; const envDir = join(ASSETS, "environments"); const ownObjects = choice === "scene" && env.objects?.endsWith(".xml") ? [env.objects] : []; const includes = [...(env.decor ? [env.decor] : []), ...ownObjects]; const envFiles = [...(env.files ?? []), ...includes].map((f) => [f, readFileSync(join(envDir, dirname(env.xml), f))]); const path = writeScene(mujoco, "/scene", { xml: readFileSync(join(envDir, env.xml), "utf8"), files: envFiles, includes }, robot, { modelName: `${envId}_${robotId}`, }); const model = mujoco.MjModel.from_xml_path(path); const sim = new Sim(mujoco, model, { controlHz }); const driver = meta.controller === "g1" ? new G1Robot(sim, meta, JSON.parse(readFileSync(join(robotDir, meta.policy.config), "utf8"))) : new XLeRobot(sim, meta); return { mujoco, sim, robot: driver, env, meta, robotDir }; }