Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from pxr import Usd, UsdGeom, UsdLux
- # Assume `stage` is your USD stage object.
- # Specify the parent prim path containing the xform children you want to convert.
- parent_path = "/path/to/parent" # Replace with the actual path
- parent_prim = stage.GetPrimAtPath(parent_path)
- # Define a new prim to hold all converted lights
- lights_group_path = "/lights"
- lights_group = stage.DefinePrim(lights_group_path, "Xform")
- print(f"Created new lights group at {lights_group_path}")
- # Function to recursively process each child prim and convert leaf nodes to RectLight
- def process_prim(prim):
- # Check if the prim has children; if it does, process them recursively
- children = prim.GetChildren()
- if not children:
- # Leaf node: convert to RectLight
- light_path = f"{lights_group_path}/{prim.GetName()}"
- print(f"Attempting to create RectLight at {light_path}")
- try:
- light_prim = UsdLux.RectLight.Define(stage, light_path)
- print(f"Successfully created RectLight at {light_path}")
- except Exception as e:
- print(f"Failed to create RectLight at {light_path}: {e}")
- return
- # Transfer any xform attributes from the original leaf Xform to the new RectLight prim
- xformable = UsdGeom.Xformable(prim)
- xform_ops = xformable.GetOrderedXformOps()
- print(f"Found {len(xform_ops)} xform ops for {prim.GetPath()}")
- # Get the Xformable of the new light prim
- light_xformable = UsdGeom.Xformable(light_prim)
- for op in xform_ops:
- # Check if the transform operation already exists on the light prim
- existing_ops = light_xformable.GetOrderedXformOps()
- existing_op = next((eo for eo in existing_ops if eo.GetOpType() == op.GetOpType()), None)
- if existing_op:
- # Reuse existing operation
- print(f"Using existing transform operation {op.GetOpType()} for {light_path}")
- existing_op.Set(op.Get())
- else:
- # Add new operation if it doesn't exist
- print(f"Adding new transform operation {op.GetOpType()} for {light_path}")
- new_op = light_xformable.AddXformOp(op.GetOpType())
- new_op.Set(op.Get())
- # Optional: Print debug statement if removing the original leaf xform prim
- print(f"Removing original leaf xform prim at {prim.GetPath()}")
- stage.RemovePrim(prim.GetPath())
- else:
- # If not a leaf node, process each child recursively
- for child in children:
- process_prim(child)
- # Start processing from the parent prim
- process_prim(parent_prim)
- print("Conversion complete. All leaf xforms are now RectLight prims under the new lights group.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement