Working with prims, properties, and primvars: Worked Example — Data Modeling (NVIDIA-Certified Professional: OpenUSD Development)
Working with Prims, Properties, and Primvars in OpenUSD: A Step-by-Step Worked Example In the context of the NVIDIA-Certified Professional: OpenUSD...
Working with Prims, Properties, and Primvars in OpenUSD: A Step-by-Step Worked Example
In the context of the NVIDIA-Certified Professional: OpenUSD Development exam, mastering how to work with prims, properties, and primvars is essential for effective data modeling within the Universal Scene Description (USD) framework. This example illustrates a practical scenario where you create and manipulate these elements to build a simple 3D model representation.
Scenario:
You are tasked with defining a cube prim in a USD stage, assigning it a size property, and adding a primvar to control its color per vertex.
Step 1: Creating the USD Stage and Defining the Cube Prim
First, initialize a USD stage (in-memory or file-based) and define a prim of type Xform to represent the cube's transform node.
- Create a new stage: stage = Usd.Stage.CreateNew('cube.usda')
- Define the cube prim at path /Cube with type Xform: cubePrim = stage.DefinePrim('/Cube', 'Xform')
This prim acts as a container for the cube's properties and geometry.
Step 2: Adding a Size Property
Next, add a custom float property named size to specify the cube's edge length.
- Access the prim's attribute interface: sizeAttr = cubePrim.CreateAttribute('size', Sdf.ValueTypeNames.Float)
- Set the size value, e.g., 2.0 units: sizeAttr.Set(2.0)
This property can be used by downstream applications to scale or interpret the cube's dimensions.
Step 3: Defining Primvars for Per-Vertex Color
Primvars are specialized properties designed to store per-primitive or per-vertex data. Here, we add a color primvar to define vertex colors.
- Access the UsdGeom schema for the cube prim: cubeGeom = UsdGeom.Xformable(cubePrim)
- Create a primvar named displayColor with type Color3fArray and interpolation vertex: colorPrimvar = UsdGeom.PrimvarsAPI(cubePrim).CreatePrimvar('displayColor', Sdf.ValueTypeNames.Color3fArray, 'vertex')
- Set the color data as an array of RGB colors for each vertex. For a cube with 8 vertices, define 8 colors:
Example Color Array
colors = [Gf.Vec3f(1,0,0), Gf.Vec3f(0,1,0), Gf.Vec3f(0,0,1), Gf.Vec3f(1,1,0), Gf.Vec3f(1,0,1), Gf.Vec3f(0,1,1), Gf.Vec3f(0.5,0.5,0.5), Gf.Vec3f(1,1,1)]
Assign these colors to the primvar:
colorPrimvar.Set(colors)
Step 4: Saving and Verifying the Stage
Save the stage to disk and verify the structure:
- stage.GetRootLayer().Save()
- Inspect the cube.usda file to confirm the prim, attribute, and primvar are correctly defined.
Summary of Key Concepts Applied
- Prims represent scene graph nodes; here, the cube is an Xform prim.
- Properties such as size are custom attributes attached to prims.
- Primvars store data like per-vertex colors with specific interpolation, enabling detailed control over geometry appearance.
This example demonstrates the practical steps to create and manipulate prims, properties, and primvars in OpenUSD, a core skill for the NVIDIA-Certified Professional: OpenUSD Development exam.
More in this topic
Ready to test your knowledge?
Put what you've learned into practice with a quick quiz and track your progress.
Test your knowledge →