Advertisement
thureinfree

xform to rect light

Nov 13th, 2024 (edited)
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.78 KB | None | 0 0
  1. from pxr import Usd, UsdGeom, UsdLux
  2.  
  3. # Assume `stage` is your USD stage object.
  4. # Specify the parent prim path containing the xform children you want to convert.
  5. parent_path = "/path/to/parent"  # Replace with the actual path
  6. parent_prim = stage.GetPrimAtPath(parent_path)
  7.  
  8. # Define a new prim to hold all converted lights
  9. lights_group_path = "/lights"
  10. lights_group = stage.DefinePrim(lights_group_path, "Xform")
  11. print(f"Created new lights group at {lights_group_path}")
  12.  
  13. # Function to recursively process each child prim and convert leaf nodes to RectLight
  14. def process_prim(prim):
  15.     # Check if the prim has children; if it does, process them recursively
  16.     children = prim.GetChildren()
  17.    
  18.     if not children:
  19.         # Leaf node: convert to RectLight
  20.         light_path = f"{lights_group_path}/{prim.GetName()}"
  21.         print(f"Attempting to create RectLight at {light_path}")
  22.        
  23.         try:
  24.             light_prim = UsdLux.RectLight.Define(stage, light_path)
  25.             print(f"Successfully created RectLight at {light_path}")
  26.         except Exception as e:
  27.             print(f"Failed to create RectLight at {light_path}: {e}")
  28.             return
  29.  
  30.         # Transfer any xform attributes from the original leaf Xform to the new RectLight prim
  31.         xformable = UsdGeom.Xformable(prim)
  32.         xform_ops = xformable.GetOrderedXformOps()
  33.         print(f"Found {len(xform_ops)} xform ops for {prim.GetPath()}")
  34.  
  35.         # Get the Xformable of the new light prim
  36.         light_xformable = UsdGeom.Xformable(light_prim)
  37.  
  38.         for op in xform_ops:
  39.             # Check if the transform operation already exists on the light prim
  40.             existing_ops = light_xformable.GetOrderedXformOps()
  41.             existing_op = next((eo for eo in existing_ops if eo.GetOpType() == op.GetOpType()), None)
  42.  
  43.             if existing_op:
  44.                 # Reuse existing operation
  45.                 print(f"Using existing transform operation {op.GetOpType()} for {light_path}")
  46.                 existing_op.Set(op.Get())
  47.             else:
  48.                 # Add new operation if it doesn't exist
  49.                 print(f"Adding new transform operation {op.GetOpType()} for {light_path}")
  50.                 new_op = light_xformable.AddXformOp(op.GetOpType())
  51.                 new_op.Set(op.Get())
  52.  
  53.         # Optional: Print debug statement if removing the original leaf xform prim
  54.         print(f"Removing original leaf xform prim at {prim.GetPath()}")
  55.         stage.RemovePrim(prim.GetPath())
  56.     else:
  57.         # If not a leaf node, process each child recursively
  58.         for child in children:
  59.             process_prim(child)
  60.  
  61. # Start processing from the parent prim
  62. process_prim(parent_prim)
  63.  
  64. print("Conversion complete. All leaf xforms are now RectLight prims under the new lights group.")
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement