A VariantSet is a composition arc that acts like a 'switchable reference'. It allows a content creator to package discrete alternatives (e.g., different colors, materials, or shapes) that a consumer can switch between non-destructively.
To use them:
- Create a
VariantSet on a Prim using GetVariantSets().AddVariantSet(name). - Add individual variants using
AddVariant(name). - Use
SetVariantSelection(name) to choose the active variant. - Use
GetVariantEditContext() as a context manager to define the scene description that belongs to that specific variant.
from pxr import Usd, UsdGeom
stage = Usd.Stage.CreateInMemory('VariantExample.usda')
variantSphere = stage.OverridePrim('/variantSphere')
variantSphere.GetReferences().AddReference('./SampleLayer.usda', '/MySphere')
# Access the prim to add variants
variantXform = stage.GetPrimAtPath('/variantSphere')
colorAttr = UsdGeom.Sphere.Get(stage, '/variantSphere/MeshData').GetDisplayColorAttr()
# Add the VariantSet
colorVariants = variantXform.GetVariantSets().AddVariantSet('ColorsRGB')
# Define variants
for color_name, color_val in [('red', [(1,0,0)]), ('green', [(0,1,0)]), ('blue', [(0,0,1)])]:
colorVariants.AddVariant(color_name)
colorVariants.SetVariantSelection(color_name)
with colorVariants.GetVariantEditContext():
colorAttr.Set(color_val)
# Select a specific variant
colorVariants.SetVariantSelection('green')