OpenUSD Data Types#
The OpenUSD API Documentation has a list of USD Basic Data Types that are used for Attribute types. Type Name are provided by the Sdf.ValueTypeNames module, which is a useful resource for reference.
In order to pass a valid value for the type for the CreateAttribute(name, type) function, it needs to be a reference to a Sdf.ValueTypeNames.TypeName object. There are three ways of getting these:
Using Python Objects
The current values and how to construct an object that will function as that value are in a table here for convenience, please refer to the USD Basic Data Types page for an explanation on explicit definitions. The Value Type Token column denotes how each Type Name appears in a .usda file.
Tip
Gf.Vec3d/f/h instances below can be constructed with just a (float, float, float) tuple (and Vec2d/f/h ``, ``Vec4d/f/h as well), but the explicit declaration is passed below for completeness.
Note
[] denotes a list of types. The left column of this list was generated with the method from 1) above.
Type Name |
Value Type Token |
Python Constructor |
|---|---|---|
Asset |
asset |
|
AssetArray |
asset[] |
|
Bool |
bool |
|
BoolArray |
bool[] |
|
Color3d |
color3d |
|
Color3dArray |
color3d[] |
|
Color3f |
color3f |
|
Color3fArray |
color3f[] |
|
Color3h |
color3h |
|
Color3hArray |
color3h[] |
|
Color4d |
color4d |
|
Color4dArray |
color4d[] |
|
Color4f |
color4f |
|
Color4fArray |
color4f[] |
|
Color4h |
color4h |
|
Color4hArray |
color4h[] |
|
Double |
double |
|
Double2 |
double2 |
|
Double2Array |
double2[] |
|
Double3 |
double3 |
|
Double3Array |
double3[] |
|
Double4 |
double4 |
|
Double4Array |
double4[] |
|
DoubleArray |
double[] |
|
Float |
float |
|
Float2 |
float2 |
|
Float2Array |
float2[] |
|
Float3 |
float3 |
|
Float3Array |
float3[] |
|
Float4 |
float4 |
|
Float4Array |
float4[] |
|
FloatArray |
float[] |
|
Frame4d |
frame4d |
|
Frame4dArray |
frame4d[] |
|
Half |
half |
|
Half2 |
half2 |
|
Half2Array |
half2[] |
|
Half3 |
half3 |
|
Half3Array |
half3[] |
|
Half4 |
half4 |
|
Half4Array |
half4[] |
|
HalfArray |
half[] |
|
Int |
int |
|
Int2 |
int2 |
|
Int2Array |
int2[] |
|
Int3 |
int3 |
|
Int3Array |
int3[] |
|
Int4 |
int4 |
|
Int4Array |
int4[] |
|
Int64 |
int64 |
|
Int64Array |
int64[] |
|
IntArray |
int[] |
|
Matrix2d |
matrix2d |
|
Matrix2dArray |
matrix2d[] |
|
Matrix3d |
matrix3d |
|
Matrix3dArray |
matrx3d[] |
|
Matrix4d |
matrix4d |
|
Matrix4dArray |
matrix4d[] |
|
Normal3d |
normal3d |
|
Normal3dArray |
normal3d[] |
|
Normal3f |
normal3f |
|
Normal3fArray |
normal3f[] |
|
Normal3h |
normal3h |
|
Normal3hArray |
normal3h[] |
|
Point3d |
point3d |
|
Point3dArray |
point3d[] |
|
Point3f |
point3f |
|
Point3fArray |
point3f[] |
|
Point3h |
point3h |
|
Point3hArray |
point3h[] |
|
Quatd |
quatd |
|
QuatdArray |
quatd[] |
|
Quatf |
quatf |
|
QuatfArray |
quatf[] |
|
Quath |
quath |
|
QuathArray |
quath[] |
|
String |
string |
|
StringArray |
string[] |
|
TexCoord2d |
texCoord2d |
|
TexCoord2dArray |
texCoord2d[] |
|
TexCoord2f |
texCoord2f |
|
TexCoord2fArray |
texCoord2f[] |
|
TexCoord2h |
texCoord2h |
|
TexCoord2hArray |
texCoord2h[] |
|
TexCoord3d |
texCoord3d |
|
TexCoord3dArray |
texCoord3d[] |
|
TexCoord3f |
texCoord3f |
|
TexCoord3fArray |
texCoord3f[] |
|
TexCoord3h |
texCoord3h |
|
TexCoord3hArray |
texCoord3h[] |
|
TimeCode |
timecode |
|
TimeCodeArray |
timecode[] |
|
Token |
token |
|
TokenArray |
token[] |
|
UChar |
uchar |
|
UCharArray |
uchar[] |
|
UInt |
uint |
|
UInt64 |
uint64 |
|
UInt64Array |
uint64[] |
|
UIntArray |
uint[] |
|
Vector3d |
vector3d |
|
Vector3dArray |
vector3d[] |
|
Vector3f |
vector3f |
|
Vector3fArray |
vector3f[] |
|
Vector3h |
vector3h |
|
Vector3hArray |
vector3h[] |
|
Get Reference From a String
Use
Sdf.ValueTypeNames.Find(string)to get the reference from a string (valid inputs are the left column in the table above)Exact Value
Look up the exact value for TypeName in the Sdf.ValueTypeNames module ( Sdf.ValueTypeNames.Color3h for example is valid):
from pxr import Sdf
print(dir(Sdf.ValueTypeNames))
# output is: ['Asset', 'AssetArray', 'Bool', 'BoolArray', 'Color3d', 'Color3dArray', 'Color3f', 'Color3fArray', 'Color3h', 'Color3hArray', 'Color4d', 'Color4dArray', 'Color4f', ... snipped ]
Working with Attributes#
You can call the prim_ref.CreateAttribute(name, type) function to create the attribute, and we can use the information above to select a valid type. It returns a reference to the attribute created, which we can set with attribute_ref.Set(value), and again we can construct a valid value by looking up the constructor above.
from pxr import Usd, UsdGeom
stage_ref = Usd.Stage.Open('stage.usda')
# get a reference to the Xform instance, this works fine since it is also a Usd.Prim
xform_ref = UsdGeom.Xform.Define(stage, '/XformPrim')
# create an attribute reference, using an explicit reference to the type
weight_attr = xform_ref.CreateAttribute('weight', Sdf.ValueTypeNames.Float)
print(weight_attr.Get()) # prints empty string for default Float values, not 0!
print(weight_attr.Get() == None) # prints "True"
print(weight_attr.Get() == 0) # prints "False"
# to set an attribute we use the attribute_ref.Set(value) function
weight_attr.Set(42.3)
print(weight_attr.Get()) # prints "42.3"
# also, you can chain calls like so
print(xform_ref.GetPrim().GetAttribute('weight').Get()) # prints "42.3"