""" Batch fix USD metadata for IS 5.0.0 compatibility. IS 4.5.0 auto-added 'Rotate:unitsResolve' and 'Scale:unitsResolve' xformOps to compensate for metersPerUnit/upAxis differences. IS 5.0.0 no longer does this, causing RigidObject physics to break (objects fly away). Fix: Change metersPerUnit to 1.0 and upAxis to Z in USD metadata. Vertex data stays unchanged (already in correct scale for the scene). Usage: # Scan only (dry run): python migerate/fix_usd_metadata.py --root workflows/simbox/example_assets --dry-run # Fix all: python migerate/fix_usd_metadata.py --root workflows/simbox/example_assets # Fix specific pattern: python migerate/fix_usd_metadata.py --root workflows/simbox/example_assets --pattern "Aligned_obj.usd" # Skip specific directories (e.g. robot models, curobo assets): python migerate/fix_usd_metadata.py --root . --skip curobo,robot # Restore from backups: python migerate/fix_usd_metadata.py --root workflows/simbox/example_assets --restore """ import argparse import glob import os import shutil import sys from pxr import Usd, UsdGeom def fix_usd_files(root: str, pattern: str = "*.usd", target_mpu: float = 1.0, target_up: str = "Z", dry_run: bool = False, skip: list = None): usd_files = glob.glob(os.path.join(root, "**", pattern), recursive=True) usd_files = [f for f in usd_files if not f.endswith(".bak")] usd_files.sort() skip = skip or [] print(f"Scanning {len(usd_files)} USD files under: {root}") if dry_run: print("[DRY RUN] No files will be modified.\n") print() fixed = 0 skipped = 0 skipped_dir = 0 errors = 0 for fpath in usd_files: rel = os.path.relpath(fpath, root) # Check skip patterns if any(s in rel for s in skip): print(f" SKIP (dir filter): {rel}") skipped_dir += 1 continue try: stage = Usd.Stage.Open(fpath) if stage is None: print(f" ERROR: Cannot open {rel}") errors += 1 continue mpu = UsdGeom.GetStageMetersPerUnit(stage) up = UsdGeom.GetStageUpAxis(stage) needs_fix = (mpu != target_mpu) or (up != target_up) if not needs_fix: print(f" OK: {rel} (mpu={mpu}, up={up})") skipped += 1 continue if dry_run: print(f" WOULD FIX: {rel} (mpu={mpu}, up={up} -> mpu={target_mpu}, up={target_up})") fixed += 1 continue # Backup bak = fpath + ".bak" if not os.path.exists(bak): shutil.copy2(fpath, bak) # Fix UsdGeom.SetStageMetersPerUnit(stage, target_mpu) UsdGeom.SetStageUpAxis(stage, UsdGeom.Tokens.z if target_up == "Z" else UsdGeom.Tokens.y) stage.GetRootLayer().Save() print(f" FIXED: {rel} (mpu={mpu}/{up} -> {target_mpu}/{target_up})") fixed += 1 except Exception as e: print(f" ERROR: {rel} -> {e}") errors += 1 print(f"\nDone: {fixed} {'would fix' if dry_run else 'fixed'}, {skipped} already OK, " f"{skipped_dir} skipped (dir filter), {errors} errors.") if not dry_run and fixed > 0: print("Backups saved as *.bak.") print("To restore: python migerate/fix_usd_metadata.py --root