I have written a python script that creates a instancer prim with thousands of copies of a prototype object, which also has a bound material. I tried to use a primvars to modify the diffuse color of the instancer prim to assign a different color to each point instance, but so far I was unsuccessful - the instances all have the same color defined in the shader prim. Can someone tell me exactly what I’m doing wrong? Here short test version of the code:
from pxr import Usd, UsdGeom, UsdShade, Sdf, Gf
stage = omni.usd.get_context().get_stage()
sphere = UsdGeom.Sphere.Define(stage, “/World/PrototypeSphere”)
material = UsdShade.Material.Define(stage, “/World/Materials/RedMaterial”)
shader = UsdShade.Shader.Define(stage, “/World/Materials/RedMaterial/Shader”)
shader.CreateIdAttr(“UsdPreviewSurface”)
shader.CreateInput(“diffuseColor”, Sdf.ValueTypeNames.Color3f).Set((1.0, 0.0, 0.0))
material.CreateSurfaceOutput().ConnectToSource(shader.ConnectableAPI(), ‘surface’)
UsdShade.MaterialBindingAPI(sphere).Bind(material)
instancer = UsdGeom.PointInstancer.Define(stage, “/World/Instancer”)
instancer.CreatePrototypesRel().AddTarget(sphere.GetPath())
instancer.CreateProtoIndicesAttr().Set([0,0,0,0])
positions = [(0, 0, 0), (2, 0, 0), (4, 0, 0),(6, 0, 0)]
colors = [(1.0, 1.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 1.0), (1.0, 0.0, 0.0)]
instancer.CreateIdsAttr(range(0,len(positions)))
instancer.CreatePositionsAttr().Set(positions)
primvar = UsdGeom.PrimvarsAPI(instancer).CreatePrimvar(“input:diffuseColor”, Sdf.ValueTypeNames.Color3fArray)
primvar.Set(colors)