XformCommonAPI#

What Is XformCommonAPI?#

XformCommonAPI is a non-applied API schema of the OpenUSD framework. Today, we’re diving into this API to understand its utility in the 3D content creation pipeline.

This API facilitates the authoring and retrieval of a common set of operations with a single translation, rotation, scale and pivot that is generally compatible with import and export into many tools. It’s designed to simplify the interchange of these transformations.

How Does It Work?#

The API provides methods to get and set these transformations at specific times–for instance, it allows the retrieval of transformation vectors at any given frame or time code, ensuring precise control over the simulation process.

There’s another way to author and retrieve translations – through the UsdGeomXformable function. Xformable prims support arbitrary sequences of transformations, which gives power users a lot of flexibility. A user could place two rotations on a “Planet” prim, allowing them to control revolution and rotation around two different pivots on the same prim. This is powerful, but complicates simple queries like “What is the position of an object at time 101.0?”

Working With Python#

This example verifies prim compatibility with XformCommonAPI and demonstrates its use.

 1from pxr import Usd, UsdGeom
 2
 3# Create a stage and define a prim path
 4stage = Usd.Stage.CreateNew('example.usda')
 5prim = UsdGeom.Xform.Define(stage, '/ExamplePrim')
 6
 7# Check if the XformCommonAPI is compatible with the prim using the bool operator 
 8if not (xform_api := UsdGeom.XformCommonAPI(prim)):
 9    raise Exception("Prim not compatible with XformCommonAPI")
10
11# Set transformations
12xform_api.SetTranslate((10.0, 20.0, 30.0))
13xform_api.SetRotate((45.0, 0.0, 90.0), UsdGeom.XformCommonAPI.RotationOrderXYZ)
14xform_api.SetScale((2.0, 2.0, 2.0))

These functions demonstrate how to apply translations, rotations, and scaling to a 3D object using the XformCommonAPI. We can get a transformation matrix from the xformable prim that works with any xformOp order using the GetLocalTransformation method.

Examples#

Example 1: XformCommonAPI - Transforms and Inheritance#

In this example, we will use the XformCommonAPI to translate, rotate, and scale a parent Xform, then show how a child under that parent inherits those transforms while a similar child under a separate prim hierarchy does not.

 1from pxr import Usd, UsdGeom, Gf
 2
 3file_path = "_assets/xformcommonapi.usda"
 4stage = Usd.Stage.CreateNew(file_path)
 5
 6# A root transform group we will move and rotate
 7world = UsdGeom.Xform.Define(stage, "/World")
 8parent = UsdGeom.Xform.Define(stage, world.GetPath().AppendPath("Parent_Prim"))
 9
10# Parent Translate, Rotate, Scale using XformCommonAPI
11parent_xform_api = UsdGeom.XformCommonAPI(parent)
12parent_xform_api.SetTranslate(Gf.Vec3d(5, 0, 3))
13parent_xform_api.SetRotate(Gf.Vec3f(90, 0, 0))
14parent_xform_api.SetScale(Gf.Vec3f(3.0, 3.0, 3.0))
15
16
17child_translation = Gf.Vec3d(2, 0, 0)
18
19# Child A - inherits parent transforms
20child_a_cone = UsdGeom.Cone.Define(stage, parent.GetPath().AppendChild("Child_A"))
21child_a_xform_api = UsdGeom.XformCommonAPI(child_a_cone)
22child_a_xform_api.SetTranslate(child_translation)  # Parent_Prim transform + local placement
23
24# Child B - "/World/Alt_Parent/Child_B" does NOT inherit Parent_Prim transforms
25alt_parent = UsdGeom.Xform.Define(stage, world.GetPath().AppendChild("Alt_Parent"))
26child_b_cone = UsdGeom.Cone.Define(stage, alt_parent.GetPath().AppendChild("Child_B"))
27child_b_xform_api = UsdGeom.XformCommonAPI(child_b_cone)
28child_b_xform_api.SetTranslate(child_translation)  # local placement only
29
30# Inspect the authored Xform Operation Order
31print("Parent xformOpOrder:", UsdGeom.Xformable(parent).GetXformOpOrderAttr().Get())
32print("Alt_Parent xformOpOrder:", UsdGeom.Xformable(alt_parent).GetXformOpOrderAttr().Get())
33print("Child A xformOpOrder:", UsdGeom.Xformable(child_a_cone).GetXformOpOrderAttr().Get())
34print("Child B xformOpOrder:", UsdGeom.Xformable(child_b_cone).GetXformOpOrderAttr().Get())
35
36stage.Save()
Parent xformOpOrder: [xformOp:translate, xformOp:rotateXYZ, xformOp:scale]
Alt_Parent xformOpOrder: None
Child A xformOpOrder: [xformOp:translate]
Child B xformOpOrder: [xformOp:translate]
Loading your model...
_assets/xformcommonapi.usda
        
    

XformCommonAPI is used to set and get transform components such as scale, rotation, scale-rotate pivot and translation. Even though these are considered attributes, it is best to go through XformCommonAPI when editting transformation values. XformCommonAPI is a great way to bootstrap setting up new transformations. Future modules will dive into advanced usage of xformOps.

Key Takeaways#

The XformCommonAPI provides the preferred way for authoring and retrieving a standard set of component transformations including scale, rotation, scale- rotate pivot and translation.

The goal of the API is to enhance, reconfigure or adapt each structure without changing the entire system. This approach allows for flexibility and customization by focusing on the individual parts rather than the whole. This is done by limiting the set of allowed basic operations and by specifying the order in which they are applied.