"""Write the Unitree G1 robot, assets/robots/g1/, from mjswan's demo assets on Hugging Face. usage: python tools/make_g1.py mjswan (https://huggingface.co/ttktjmt/mjswan, Apache-2.0) ships a locomotion policy for the 29-DoF G1 trained with mjlab's velocity task, as ONNX with a JSON sidecar, and the MJCF it was trained against (MuJoCo Menagerie's G1, BSD-3-Clause, Unitree Robotics). This script downloads them at a pinned revision and writes the robot so MuJoCo runs the policy as mjlab trained it: - the 29 torque pass-through actuators become position actuators with the sidecar's stiffness (kp), damping (kv) and the original torque limits (forcerange), so MuJoCo's implicit integrator does the PD; - each joint's armature is mjlab's for its motor, which the gains were derived from: kp = armature * (2 pi * 10 Hz)^2, so armature = kp / (20 pi)^2; - a velocimeter and a gyro at the pelvis IMU site give the policy's base_lin_vel and base_ang_vel (mjlab's "robot/imu_lin_vel" and "robot/imu_ang_vel"); - the keyframe goes (scenes add objects, so its qpos would not fit); src/robots/g1.js poses the robot. The policy's observation (99 values) and action (29) are documented in src/robots/g1.js. """ import json import math import re import urllib.request from pathlib import Path REPO = "https://huggingface.co/ttktjmt/mjswan/resolve/64433677553ccb86c6bba76b5238f41da1f87523" OUT = Path(__file__).resolve().parents[1] / "assets" / "robots" / "g1" def fetch(path: str) -> bytes: with urllib.request.urlopen(f"{REPO}/{path}") as r: return r.read() def main() -> None: (OUT / "assets").mkdir(parents=True, exist_ok=True) config = json.loads(fetch("policies/locomotion.json")) (OUT / "locomotion.json").write_bytes(json.dumps(config, indent=2).encode() + b"\n") (OUT / "locomotion.onnx").write_bytes(fetch("policies/locomotion.onnx")) (OUT / "LICENSE").write_bytes(fetch("scenes/unitree_g1/LICENSE")) xml = fetch("scenes/unitree_g1/g1.xml").decode() meshes = sorted(set(re.findall(r'file="([^"]+)"', xml))) for name in meshes: target = OUT / "assets" / name if not target.exists(): target.write_bytes(fetch(f"scenes/unitree_g1/assets/{name}")) act = config["actions"]["joint_pos"] kp, kv = act["stiffness"], act["damping"] w = 2 * math.pi * 10.0 # Per-joint armature on each joint element. def joint(m: re.Match) -> str: tag = m.group(0) named = re.search(r'name="([^"]+)"', tag) if not named or named.group(1) not in kp: return tag # the default class, or a joint the policy does not drive armature = f'armature="{kp[named.group(1)] / w**2:.8f}"' if "armature=" in tag: return re.sub(r'armature="[^"]*"', armature, tag) return tag.replace("]*/>", joint, xml) # Position actuators with the policy's gains and the original torque limits. def actuator(m: re.Match) -> str: name, joint_name, lo, hi = m.group(1), m.group(2), m.group(3), m.group(4) return f'' xml, n = re.subn(r'', actuator, xml) assert n == 29, n xml = xml.replace("\n", "") xml = xml.replace("\n", "") sensors = ( ' \n' ' \n' ) xml = xml.replace(" \n", " \n" + sensors, 1) xml = re.sub(r"\s*.*?", "", xml, flags=re.S) header = ( "\n" ) (OUT / "g1.xml").write_text(header + xml, encoding="utf-8") print(f"wrote {OUT / 'g1.xml'}: {len(meshes)} meshes, 29 position actuators") if __name__ == "__main__": main()