robot-usds / scripts /build_g1.py
mishig's picture
mishig HF Staff
Add Unitree G1 (g1_body29_hand14)
755a212 verified
Raw History Blame Contribute Delete
2.76 kB
"""Unitree G1 USD from newton-assets' unitree_g1, with the links of g1_body29_hand14.urdf."""
import json
import sys
import numpy as np
from pxr import Gf, Usd, UsdGeom, UsdPhysics
import robotusd as ru
SOURCE, URDF, OUT = sys.argv[1:4]
layer = ru.flatten(SOURCE)
stage = Usd.Stage.Open(layer)
def body(stage, name):
return next(p for p in stage.Traverse() if p.HasAPI(UsdPhysics.RigidBodyAPI) and p.GetName() == name)
# Menagerie stands the robot on the ground; the URDF has its root link, the pelvis, at the origin.
def to_origin(stage):
for op in UsdGeom.Xformable(body(stage, "pelvis")).GetOrderedXformOps():
if op.GetOpType() == UsdGeom.XformOp.TypeTranslate:
op.Set(Gf.Vec3d(0, 0, 0))
to_origin(stage)
root = UsdGeom.ModelAPI(stage.GetDefaultPrim())
root.SetExtentsHint(root.ComputeExtentsHint(UsdGeom.BBoxCache(Usd.TimeCode.Default(), UsdGeom.Imageable.GetOrderedPurposeTokens())))
# Bodies and joints already carry the URDF's names, and its other fixed links (pelvis contour, head,
# logo, palms, IMUs) are meshes or sites at their frames. The camera and lidar mounts are missing.
rest = ru.urdf_fk(URDF, {})
torso = body(stage, "torso_link")
for link in ("d435_link", "mid360_link"):
ru.add_frame(stage, str(torso.GetPath()), link, rest[link])
layer.documentation = (
"Unitree G1 (29 DoF body, Dex3-1 hands) for LeRobot. Modified by LeRobot from newton-physics/newton-assets "
"unitree_g1 g1_29dof_with_hand_rev_1_0 (BSD-3-Clause, converted from MuJoCo Menagerie): flattened into one "
"layer, moved to put the pelvis at the origin like the g1_body29_hand14 URDF's root link, d435_link and "
"mid360_link frames added where the URDF puts them."
)
layer.customLayerData = {
**layer.customLayerData,
"lerobot:source": "https://github.com/newton-physics/newton-assets/tree/0deda9b1a68d1c202a6115a78ce0ff61ac861e00/unitree_g1/usd_structured",
"lerobot:urdf": "https://huggingface.co/buckets/lerobot/robot-urdfs/resolve/g1/g1_body29_hand14.urdf",
}
layer.Export(OUT)
out = Usd.Stage.Open(OUT)
report = ru.check_against_urdf(out, URDF)
report["root_offset_mm"] = float(np.abs(ru.np_matrix(UsdGeom.XformCache().GetLocalToWorldTransform(body(out, "pelvis"))) - rest["pelvis"]).max() * 1000)
source = Usd.Stage.Open(SOURCE)
source.SetEditTarget(source.GetSessionLayer())
to_origin(source)
report["visual_bounds_vs_source_mm"] = ru.compare_visual_bounds(source, out)
drawn = [p for p in out.Traverse() if p.IsA(UsdGeom.Mesh) and UsdGeom.Imageable(p).ComputePurpose() == "default"]
report["meshes"] = len(drawn)
report["triangles"] = int(sum(np.maximum(np.array(UsdGeom.Mesh(p).GetFaceVertexCountsAttr().Get()) - 2, 0).sum() for p in drawn))
print(json.dumps(report, indent=1, default=str))