Attributes#
What is an Attribute?#
Attributes are the most common type of property that you’ll work with when creating scenes. An attribute can have one specific data type, such as a number, text, or a vector. Each attribute can have a default value, and it can also have different values at different points in time, called timeSamples.
How Does It Work?#
Attributes are name-value pairs (often referred to as key-value pairs) that store data associated with a prim.
Any given attribute has a single, defined data type associated with it. Each attribute is defined with the type of data that it can hold. A single attribute can represent various types of properties, such as the vertices of a piece of geometry, the diffuse color of a material, or the mass of an object. These are typically defined through the Sdf
library.
Some common examples of attributes include:
Visibility - Controls the visibility of a prim in the scene.
Display color - Specifies the display color applied to a geometric prim.
Extent - Defines the boundaries of a geometric prim.
Attributes can be authored and stored within USD layers, which are files that describe different aspects of a scene. When a USD stage is composed, the attribute values from various layers are combined according to specific composition rules, allowing for flexible scene assembly.
Attributes can be animated by providing multiple keyframed values over time. OpenUSD’s timeSampling model ensures efficient storage and interpretation of animated data. We will learn more about timeSamples in the TimeCodes and TimeSamples lesson.
Working With Python#
To work with attributes in OpenUSD, we will generally use schema-specific APIs. Each schema-specific API has a function to grab its own attributes. Review the following examples to learn more.
1# Get the radius value of sphere_prim that is of type UsdGeom.Sphere
2sphere_prim.GetRadiusAttr().Get()
3
4# Set the double-sided property of the prim
5sphere_prim.GetDoubleSidedAttr().Set(True)
While there’s also a dedicated UsdAttribute
API, in general, it’s preferred to use the schema-specific methods, if they exist, as they are more clear and
less brittle. You can learn more about how to work with each specific schema on OpenUSD’s documentation.
Examples#
Example 1: Retrieving Properties of a Prim#
Properties
are the other kind of namespace object in OpenUSD. Whereas prims provide the organization and indexing for a composed scene, properties contain the “real data”.
There are two types of properties: attributes
and relationships
.
To retrieve the properties of a prim, we would use the GetProperties
method. For this demonstration we will be using GetPropertyNames()
instead to retrieve the names of the properties. This will not grab the properties themselves, but a list of the names of the properties. Use GetProperties
to retrieve the properties themselves.
Note
Relationships are only lightly discussed in this less. We’ll talk about relationships again in the Relationships lesson.
1from pxr import Usd, UsdGeom, Gf
2
3file_path = "_assets/attributes_ex1.usda"
4
5stage: Usd.Stage = Usd.Stage.CreateNew(file_path)
6
7world_xform: UsdGeom.Xform = UsdGeom.Xform.Define(stage, "/World")
8
9# Define a sphere under the World xForm:
10sphere: UsdGeom.Sphere = UsdGeom.Sphere.Define(stage, world_xform.GetPath().AppendPath("Sphere"))
11
12# Define a cube under the World xForm and set it to be 5 units away from the sphere:
13cube: UsdGeom.Cube = UsdGeom.Cube.Define(stage, world_xform.GetPath().AppendPath("Cube"))
14UsdGeom.XformCommonAPI(cube).SetTranslate(Gf.Vec3d(5, 0, 0))
15
16# Get the property names of the cube prim:
17cube_prop_names = cube.GetPrim().GetPropertyNames()
18
19# Print the property names:
20for prop_name in cube_prop_names:
21 print(prop_name)
22
23stage.Save()
doubleSided
extent
orientation
primvars:displayColor
primvars:displayOpacity
proxyPrim
purpose
size
visibility
xformOp:translate
xformOpOrder
Example 2: Getting Attribute Values#
Attributes
are the most common type of property authored in most USD scenes.
An example of a simple attribute that describes the radius of a sphere:
1def Sphere "Sphere"{
2 double radius = 10
3}
We interact with attributes through the UsdAttribute
API.
Each prim type has their own set of properties and corresponding functions to retrieve them. Since our sphere is of type UsdGeom.Sphere
, we can use the schema-specific API to get and set the radius attribute.
GetRadiusAttr()
will return a UsdAttribute
object that can be used to modify the attribute. Which means it will not retrieve the value of the attribute. To get the value of an attribute, use the Get()
method.
For example, to get the value of the radius attribute, we would use the following snippet.
1sphere_prim.GetRadiusAttr().Get()
Let’s use the Get()
method for the radius
, displayColor
, and extent
attributes.
Since we have not explicitly authored any attribute values, Get()
will return the fallback value that was defined in the schema.
Note
The attribute values will not show up in .usda
, however the values are coming from the fallback value defined in the sphere schema. USD is applying value resolution to retrieve the values.
1from pxr import Usd, UsdGeom
2
3file_path = "_assets/attributes_ex2.usda"
4stage: Usd.Stage = Usd.Stage.CreateNew(file_path)
5
6world_xform: UsdGeom.Xform = UsdGeom.Xform.Define(stage, "/World")
7
8sphere: UsdGeom.Sphere = UsdGeom.Sphere.Define(stage, world_xform.GetPath().AppendPath("Sphere"))
9cube: UsdGeom.Cube = UsdGeom.Cube.Define(stage, world_xform.GetPath().AppendPath("Cube"))
10UsdGeom.XformCommonAPI(cube).SetTranslate(Gf.Vec3d(5, 0, 0))
11
12# Get the attributes of the cube prim
13cube_attrs = cube.GetPrim().GetAttributes()
14for attr in cube_attrs:
15 print(attr)
16
17# Get the size, display color, and extent attributes of the cube
18cube_size: Usd.Attribute = cube.GetSizeAttr()
19cube_displaycolor: Usd.Attribute = cube.GetDisplayColorAttr()
20cube_extent: Usd.Attribute = cube.GetExtentAttr()
21
22print(f"Size: {cube_size.Get()}")
23print(f"Display Color: {cube_displaycolor.Get()}")
24print(f"Extent: {cube_extent.Get()}")
25
26stage.Save()
Usd.Prim(</World/Cube>).GetAttribute('doubleSided')
Usd.Prim(</World/Cube>).GetAttribute('extent')
Usd.Prim(</World/Cube>).GetAttribute('orientation')
Usd.Prim(</World/Cube>).GetAttribute('primvars:displayColor')
Usd.Prim(</World/Cube>).GetAttribute('primvars:displayOpacity')
Usd.Prim(</World/Cube>).GetAttribute('purpose')
Usd.Prim(</World/Cube>).GetAttribute('size')
Usd.Prim(</World/Cube>).GetAttribute('visibility')
Usd.Prim(</World/Cube>).GetAttribute('xformOp:translate')
Usd.Prim(</World/Cube>).GetAttribute('xformOpOrder')
Size: 2.0
Display Color: None
Extent: [(-1, -1, -1), (1, 1, 1)]
Example 3: Setting Attribute Values#
In the last example, we used the Get()
method to retrieve the value of the attribute. To set the values, we use the Set()
method.
Here is an example of setting a value to the radius attribute.
1sphere_prim.GetRadiusAttr().Set(100.0)
When run, it will modify the sphere in the example scene to look like this:
1def Sphere "Sphere"{
2 double radius = 100
3}
Based on our last modification, if we were to use Get()
it would return 100
.
When getting attribute values, USD will apply value resolution, since we authored a default value. The Get()
method will retrieve the value of the attribute. To set the values, we use the Set()
method. This will resolve to the authored value rather than the fallback value from the sphere schema.
Now let’s modify the radius
, displayColor
, and extent
attributes of the sphere by using Set()
.
1from pxr import Usd, UsdGeom
2
3file_path = "_assets/attributes_ex3.usda"
4stage: Usd.Stage = Usd.Stage.CreateNew(file_path)
5
6world_xform: UsdGeom.Xform = UsdGeom.Xform.Define(stage, "/World")
7
8sphere: UsdGeom.Sphere = UsdGeom.Sphere.Define(stage, world_xform.GetPath().AppendPath("Sphere"))
9cube: UsdGeom.Cube = UsdGeom.Cube.Define(stage, world_xform.GetPath().AppendPath("Cube"))
10UsdGeom.XformCommonAPI(cube).SetTranslate(Gf.Vec3d(5,0,0))
11
12# Get the size, display color, and extent attributes of the sphere
13cube_size: Usd.Attribute = cube.GetSizeAttr()
14cube_displaycolor: Usd.Attribute = cube.GetDisplayColorAttr()
15cube_extent: Usd.Attribute = cube.GetExtentAttr()
16
17# Modify the radius, extent, and display color attributes:
18cube_size.Set(cube_size.Get() * 2)
19cube_extent.Set(cube_extent.Get() * 2)
20cube_displaycolor.Set([(0.0, 1.0, 0.0)])
21
22stage.Save()
Key Takeaways#
In summary,
Attributes are values with a name and data type that define the properties of prims in a USD scene.
Attributes are the primary means of storing data in USD.
Each attribute has a single, defined data type.
Attributes are authored and stored within USD layers, enabling efficient scene composition.
Attributes can be animated by providing keyframed values over time.
They can be queried, modified and animated using the USD API.
Understanding attributes is essential for creating rich and detailed 3D scenes, enabling efficient collaboration and interoperability across various tools and pipelines.