"""Save a runnable native model from a live training scene.""" from __future__ import annotations import hashlib import xml.etree.ElementTree as ET from pathlib import Path import mujoco import numpy as np from mjlab.utils.spec import non_default_option_fields from mjlab.utils.xml import fix_spec_xml, strip_buffer_textures from .platform._serialization import spec_xml def _asset_bytes(spec: mujoco.MjSpec, filename: str, tag: str) -> bytes: directory = Path(spec.texturedir if tag == "texture" else spec.meshdir) if tag not in {"mesh", "texture", "hfield", "skin"}: directory = Path(".") relative = directory / filename for key in dict.fromkeys((relative.as_posix(), filename)): if key in spec.assets: return spec.assets[key] path = Path(spec.modelfiledir) / relative if not path.is_file(): raise ValueError(f"Cannot export native model asset {filename!r} from {path}") return path.read_bytes() def _localize_assets(xml: ET.Element, spec: mujoco.MjSpec, sources: dict, output: Path) -> None: compiler = xml.find("compiler") if compiler is not None: for field in ("meshdir", "texturedir", "assetdir", "strippath"): compiler.attrib.pop(field, None) for element in xml.findall("./asset/*"): name = element.get("name", "") prefix = name.split("/", 1)[0] if "/" in name else None source = sources.get(prefix, spec) for key, filename in element.attrib.copy().items(): if not key.startswith("file"): continue contents = _asset_bytes(source, filename, element.tag) digest = hashlib.sha256(contents).hexdigest() target = Path("assets") / (digest + Path(filename).suffix.lower()) (output / target).parent.mkdir(exist_ok=True) (output / target).write_bytes(contents) element.set(key, target.as_posix()) def write_native_model(env, output: Path, layout: dict) -> None: """Export the compiled scene's configured dynamics and authored reset keyframe.""" from .platform.bundle import write_json spec = env.scene.spec.copy() live = env.sim.mj_model # The simulation config is applied after compiling the spec. Carry its # effective integrator and timestep into the standalone XML. fields = set(non_default_option_fields(spec.option)) | set(non_default_option_fields(live.opt)) for field in fields: setattr(spec.option, field, getattr(live.opt, field)) strip_buffer_textures(spec) spec.compile() xml = ET.fromstring(fix_spec_xml(spec_xml(spec))) sources = {name: entity.spec for name, entity in env.scene.entities.items()} _localize_assets(xml, spec, sources, output) ET.indent(xml, space=" ") path = output / "robot.xml" ET.ElementTree(xml).write(path, encoding="utf-8", xml_declaration=True) model = mujoco.MjModel.from_xml_path(str(path)) for name in ("nq", "nv", "nu", "nbody", "njnt", "ngeom"): if getattr(model, name) != getattr(live, name): raise ValueError(f"Native model export changed {name}") for name in ( "jnt_range", "jnt_axis", "dof_armature", "dof_damping", "qpos0", "key_qpos", "body_mass", "body_inertia", "body_pos", "body_quat", "geom_size", "geom_pos", "geom_quat", "actuator_forcerange", ): # Preserve the existing contract checks even if high precision native # serialization is unavailable. Lossy or reordered models must fail. if not np.allclose(getattr(model, name), getattr(live, name), rtol=5e-6, atol=1e-9): raise ValueError(f"Native model export changed {name}") if not np.isclose(model.opt.timestep, layout["control"]["sim_dt"], rtol=0, atol=1e-12): raise ValueError("Native model timestep differs from the policy control contract") root_body = model.body(env.scene["robot"].indexing.root_body_id).name write_json( output / "controller.json", { "kind": "rl_onnx", "policy": "actor.onnx", "layout": "layout.json", "root_body": root_body, }, ) controls = next( ( getattr(term, "controls", None) for term in env.cfg.commands.values() if getattr(term, "controls", None) is not None ), None, ) if controls is not None: (output / "controls.yaml").write_bytes(controls.source.read_bytes())