Advertisement
thureinfree

list file path from usd shade

Nov 21st, 2024 (edited)
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.55 KB | None | 0 0
  1. node = hou.pwd()
  2. stage = node.editableStage()
  3.  
  4. # Add code to modify the stage.
  5. # Use drop down menu to select examples.
  6. from pxr import Usd, UsdShade
  7.  
  8. def find_unique_asset_paths(stage, prim_path):
  9.     # Open the stage and get the root prim
  10.     root_prim = stage.GetPrimAtPath(prim_path)
  11.     if not root_prim:
  12.         raise ValueError(f"Prim path '{prim_path}' does not exist in the stage.")
  13.    
  14.     # Set to store unique asset paths
  15.     unique_asset_paths = set()
  16.    
  17.     # Recursive function to traverse prims
  18.     def traverse_prim(prim):
  19.         for child in prim.GetAllChildren():
  20.             # Check if prim is a UsdShadeShader
  21.             if child.IsA(UsdShade.Shader):
  22.                 shader = UsdShade.Shader(child)
  23.                 # Check if the shader has an attribute called inputs:file
  24.                 file_attr = shader.GetInput("file")
  25.                 if file_attr:
  26.                     # Add the value to the set of unique paths
  27.                     asset_path = file_attr.Get()
  28.                     #if isinstance(asset_path, str):
  29.                     unique_asset_paths.add(asset_path.path)
  30.             # Traverse further into children
  31.             traverse_prim(child)
  32.    
  33.     # Start traversal from the root prim
  34.     traverse_prim(root_prim)
  35.    
  36.     # Log the unique asset paths
  37.     print("Unique USD Asset Paths:")
  38.     for path in unique_asset_paths:
  39.         print(path)
  40.    
  41.     return list(unique_asset_paths)
  42.  
  43. # Usage example
  44.  
  45. prim_path = "/master/shading"
  46.  
  47.  
  48. unique_paths = find_unique_asset_paths(stage, prim_path)
  49.  
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement