Attributes

Attributes are the workhorse of storing actual data inside a Prim. Attributes are often defined as part of Schemas to make it easier to access context-relevant data from within an instance of that Type.

For example, Xform typed Prims have an attribute called Purpose which is used to specify the purpose of an imageable prim. It contains one of the following values: [default, render, proxy, guide]

Now, you could get this attribute’s value in two ways. One, as a generic prim_ref.GetAttribute(name) call, but you would have to know that the exact name of the attribute you want is “purpose”, and you wouldn’t be able to get any code completion in an IDE that way.

The other way is to use the Xform Schema’s exposed function for getting the purpose, which is xform_ref.GetPurposeAttr(), which returns the same object, but will be typed in an IDE and does not depend on the underlying string name of the attribute.

Most often after you get an Attribute object, you will want to get the attribute’s actual value or set it. That can be done with attribute_ref.Get() to retrieve the value, and attribute_ref.Set(value) to set the value.

Note

attribute_ref.Set(value) requires passing the correct type of value as well, refer to table in USD Types to see how to construct the appropriate type.

Let’s see the code for getting an Attribute reference and getting its value:

from pxr import Usd, UsdGeom

stage_ref = Usd.Stage.Open('stage.usda')

# get a reference to the Xform instance as well as a generic Prim instance

xform_ref = UsdGeom.Xform.Define(stage_ref, '/XformPrim')
prim_ref = xform_ref.GetPrim()

# Get an attribute reference (not its value!)

purpose_from_xform_ref = xform_ref.GetPurposeAttr()
purpose_from_prim_ref = prim_ref.GetAttribute('purpose')

print(purpose_from_xform_ref == purpose_from_prim_ref) # prints True

# prints the actual attribute's value, in this case, one of [default, render, proxy, guide], since it is the Xform's actual Purpose attribute

print(purpose_from_xform_ref.Get())

To create an attribute that isn’t part of a Type’s namespace (or it is, but you want to create the attribute “manually”), you must pass the attribute name and its type to prim_ref.CreateAttribute(name, type).

Otherwise, most Types expose a Set-style command, for example xform_ref.SetPurposeAttr(value).

The name of an Attribute is a string. The type system for Attributes (and USD in general) is explained below.