LOPs - primvars creation

from pxr import Usd, UsdGeom, Sdf

# Get the current USD stage
stage = hou.pwd().editableStage()

# Loop through all primitives in the USD stage
for prim in stage.Traverse():
# Check if this primitive is an Xform and has '/Prototypes' in its path (prototype)
if prim.IsA(UsdGeom.Xform) and "/Prototypes" in prim.GetPath().pathString:
proto_path = prim.GetPath().pathString # Get the prototype path

# Extract the last part of the path to use as the envTreesID value
proto_index = proto_path.split("/")[-1] # This is the value you want to assign
mesh_index = proto_path.split("/")[-4] # This is the value you want to assign

# Create and set the primvar as a string
attr = prim.CreateAttribute("primvars:dneg:envTreesID", Sdf.ValueTypeNames.String)
attr.Set(proto_index)
# Loop through the children of this Xform (prototype) to find meshes
for child_prim in prim.GetChildren():
# Check if the child primitive is a mesh (UsdGeom.Mesh)
if child_prim.IsA(UsdGeom.Mesh):
# Create and set the primvar on the mesh with the correct envTreesID value
mesh_attr = child_prim.CreateAttribute("primvars:dneg:envTreesID", Sdf.ValueTypeNames.String)
mesh_attr.Set(mesh_index) # Set the correct envTreesID value

print("Assigned the correct envTreesID value to meshes within prototypes.")