Download tools/validate_simready.py from cfahlgren1/simready-halloween-candy: direct link, hf CLI and curl.
- Browser
- Download file 3.87 kB
-
https://huggingface.co/datasets/cfahlgren1/simready-halloween-candy/resolve/main/tools/validate_simready.py
- Command line
-
hf download hf://datasets/cfahlgren1/simready-halloween-candy/tools/validate_simready.py
-
curl -L -o validate_simready.py https://huggingface.co/datasets/cfahlgren1/simready-halloween-candy/resolve/main/tools/validate_simready.py
3.87 kB
| """Check USD props against the SimReady conventions this project relies on. | |
| Usage: python validate_simready.py <asset.usd> [...] | |
| Exits non-zero if any asset fails. | |
| """ | |
| import sys | |
| from pathlib import Path | |
| import numpy as np | |
| import trimesh | |
| from pxr import Usd, UsdGeom, UsdPhysics, UsdSemantics, UsdShade | |
| def _closed(mesh_prim) -> bool: | |
| mesh = UsdGeom.Mesh(mesh_prim) | |
| counts = np.array(mesh.GetFaceVertexCountsAttr().Get()) | |
| if not np.all(counts == 3): | |
| return True # only triangle meshes are checked for watertightness | |
| faces = np.array(mesh.GetFaceVertexIndicesAttr().Get()).reshape(-1, 3) | |
| return trimesh.Trimesh(np.array(mesh.GetPointsAttr().Get()), faces, process=True).is_watertight | |
| def validate(usd_path: Path) -> list[str]: | |
| problems = [] | |
| stage = Usd.Stage.Open(str(usd_path)) | |
| root = stage.GetDefaultPrim() | |
| if not root: | |
| return ["no defaultPrim"] | |
| if UsdGeom.GetStageMetersPerUnit(stage) != 1.0: | |
| problems.append(f"metersPerUnit is {UsdGeom.GetStageMetersPerUnit(stage)}, expected 1") | |
| if UsdGeom.GetStageUpAxis(stage) != UsdGeom.Tokens.z: | |
| problems.append("upAxis is not Z") | |
| if not any(UsdSemantics.LabelsAPI(root, name).GetLabelsAttr().Get() for name in UsdSemantics.LabelsAPI.GetDirectTaxonomies(root)): | |
| problems.append("no semantic labels on the root prim") | |
| prims = list(stage.Traverse(Usd.TraverseInstanceProxies())) | |
| if not any(p.HasAPI(UsdPhysics.RigidBodyAPI) for p in prims): | |
| problems.append("no PhysicsRigidBodyAPI") | |
| total_mass = 0.0 | |
| meshes = [p for p in prims if p.IsA(UsdGeom.Mesh)] | |
| colliders = [p for p in prims if p.HasAPI(UsdPhysics.CollisionAPI)] | |
| if not meshes: | |
| problems.append("no meshes") | |
| if not colliders: | |
| problems.append("no prims with PhysicsCollisionAPI") | |
| for prim in colliders: | |
| name = prim.GetName() | |
| mass = UsdPhysics.MassAPI(prim).GetMassAttr().Get() if prim.HasAPI(UsdPhysics.MassAPI) else None | |
| if not mass or mass <= 0: | |
| problems.append(f"{name}: collider has no positive physics:mass") | |
| else: | |
| total_mass += mass | |
| physics_material, _ = UsdShade.MaterialBindingAPI(prim).ComputeBoundMaterial("physics") | |
| friction = physics_material.GetPrim().GetAttribute("physics:dynamicFriction").Get() if physics_material else None | |
| if friction is None: | |
| problems.append(f"{name}: no physics material with friction") | |
| for prim in meshes: | |
| if not _closed(prim): | |
| problems.append(f"{prim.GetName()}: mesh is not watertight") | |
| variant_sets = root.GetVariantSets() | |
| wrapper_variants = variant_sets.GetVariantSet("wrapper").GetVariantNames() if variant_sets.HasVariantSet("wrapper") else [None] | |
| for variant in wrapper_variants: | |
| if variant is not None: | |
| variant_sets.GetVariantSet("wrapper").SetVariantSelection(variant) | |
| for shader in stage.Traverse(Usd.TraverseInstanceProxies()): | |
| file_attr = shader.GetAttribute("inputs:file") | |
| if not file_attr or not file_attr.Get(): | |
| continue | |
| resolved = file_attr.Get().resolvedPath | |
| if not resolved or not Path(resolved).exists(): | |
| problems.append(f"texture {file_attr.Get().path} does not resolve (variant {variant})") | |
| print(f"{'OK ' if not problems else 'FAIL'} {usd_path.name:42s} mass {total_mass * 1000:5.1f} g meshes {len(meshes)} colliders {len(colliders)} wrapper variants {len(wrapper_variants) if wrapper_variants != [None] else 0}") | |
| return problems | |
| def main(): | |
| failed = False | |
| for arg in sys.argv[1:]: | |
| problems = validate(Path(arg)) | |
| for problem in problems: | |
| print(f" - {problem}") | |
| failed |= bool(problems) | |
| sys.exit(1 if failed else 0) | |
| if __name__ == "__main__": | |
| main() | |