Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import hou
- from pxr import Usd, UsdGeom, Sdf, UsdShade
- # Access the USD stage from the LOPS context
- stage = hou.pwd().editableStage()
- def gather_mesh_prims(prim):
- """Recursively gather all mesh prims under the given prim."""
- mesh_prims = []
- if prim.IsA(UsdGeom.Mesh):
- mesh_prims.append(prim)
- for child in prim.GetAllChildren():
- mesh_prims.extend(gather_mesh_prims(child))
- return mesh_prims
- def get_bound_material(mesh_prim):
- """Get the material bound to the given mesh prim."""
- bindings = UsdShade.MaterialBindingAPI(mesh_prim)
- bound_material = bindings.ComputeBoundMaterial()[0]
- return bound_material
- def group_mesh_prims_by_material(root_prim):
- """Return a dictionary of mesh prims grouped by their bound materials."""
- all_mesh_prims = gather_mesh_prims(root_prim)
- mesh_prims_by_material = {}
- for mesh_prim in all_mesh_prims:
- bound_material = get_bound_material(mesh_prim)
- material_path = bound_material.GetPath() if bound_material else Sdf.Path.emptyPath
- if material_path not in mesh_prims_by_material:
- mesh_prims_by_material[material_path] = []
- mesh_prims_by_material[material_path].append(mesh_prim)
- return mesh_prims_by_material
- # Test with a root prim
- root_prim_path = "/geo"
- root_prim = stage.GetPrimAtPath(root_prim_path)
- # Get the mesh prims grouped by their bound materials
- mesh_prims_by_material = group_mesh_prims_by_material(root_prim)
- # Print the results
- for material_path, mesh_prims in mesh_prims_by_material.items():
- material_path_str = str(material_path) if material_path != Sdf.Path.emptyPath else "No Material"
- print(f"Material: {material_path_str}")
- for mesh_prim in mesh_prims:
- print(f" Mesh Prim: {mesh_prim.GetPath()}")
Advertisement
Advertisement