Usage Examples#
Create and Use USD Stage Adapter#
from pxr import Usd, Sdf
from omni.kit.property.adapter.usd import UsdStageAdapter
# Create a USD stage
stage = Usd.Stage.CreateInMemory()
# Create a prim
prim = stage.DefinePrim("/World/Cube", "Cube")
attr = prim.CreateAttribute("radius", Sdf.ValueTypeNames.Float)
attr.Set(5.0)
# Create USD stage adapter
adapter = UsdStageAdapter(stage)
# Access adapter properties
print(f"Adapter name: {adapter.name}")
print(f"Read priority: {adapter.priority_read}")
print(f"Write priority: {adapter.priority_write}")
# Get prim adapter at path
prim_adapter = adapter.GetPrimAtPath(Sdf.Path("/World/Cube"))
if prim_adapter:
print(f"Found prim: {prim_adapter.prim.GetPath()}")
# Get attribute adapter at path
attr_path = Sdf.Path("/World/Cube.radius")
attr_adapter = adapter.GetAttributeAtPath(attr_path)
if attr_adapter:
print(f"Attribute value: {attr_adapter.attribute.Get()}")
Monitor USD Stage Changes with Change Tracker#
from pxr import Usd, Sdf
from omni.kit.property.adapter.usd import UsdStageAdapter
def on_stage_changed(notice, stage):
"""Callback function for USD stage changes"""
print(f"Stage changed: {len(notice.GetChangedInfoOnlyPaths())} paths affected")
for path in notice.GetChangedInfoOnlyPaths():
print(f" Changed path: {path}")
# Create a USD stage
stage = Usd.Stage.CreateInMemory()
adapter = UsdStageAdapter(stage)
# Create change tracker to monitor specific attributes and prims
attr_names = ["radius", "size"]
prim_paths = [Sdf.Path("/World")]
change_tracker = adapter.CreateChangeTracker(
attr_names=attr_names,
prim_paths=prim_paths,
callback=on_stage_changed
)
# Create some prims and modify attributes to trigger changes
prim = stage.DefinePrim("/World/Sphere", "Sphere")
attr = prim.CreateAttribute("radius", Sdf.ValueTypeNames.Float)
attr.Set(10.0)
# The change tracker will automatically detect and report changes
# Remember to clean up the tracker when done
change_tracker.destroy()
Work with USD Prim and Attribute Adapters#
from pxr import Usd, Sdf
from omni.kit.property.adapter.usd import UsdStageAdapter
# Create a USD stage with some content
stage = Usd.Stage.CreateInMemory()
cube_prim = stage.DefinePrim("/World/Cube", "Cube")
radius_attr = cube_prim.CreateAttribute("radius", Sdf.ValueTypeNames.Float)
radius_attr.Set(3.5)
# Create stage adapter
adapter = UsdStageAdapter(stage)
# Get prim adapter
prim_adapter = adapter.GetPrimAtPath(Sdf.Path("/World/Cube"))
print(f"Prim type: {prim_adapter.prim.GetTypeName()}")
# Get attribute adapter
attr_adapter = adapter.GetAttributeAtPath(Sdf.Path("/World/Cube.radius"))
print(f"Attribute name: {attr_adapter.attribute.GetName()}")
print(f"Attribute value: {attr_adapter.attribute.Get()}")
# Get prim from attribute adapter
parent_prim_adapter = attr_adapter.GetPrim()
print(f"Parent prim path: {parent_prim_adapter.prim.GetPath()}")
# Check property type
property_type = attr_adapter.GetPropertyType()
print(f"Property type: {property_type}")
Convert Time to USD Time Code#
from pxr import Usd, Sdf
from omni.kit.property.adapter.usd import UsdStageAdapter
# Create a USD stage
stage = Usd.Stage.CreateInMemory()
stage.SetTimeCodesPerSecond(24.0) # Set to 24 fps
# Create stage adapter
adapter = UsdStageAdapter(stage)
# Convert current time to USD time code
current_time = 1.5 # 1.5 seconds
time_code = adapter.get_frame_time_code(current_time)
print(f"Time {current_time}s converts to frame: {time_code}")
# Use the time code for time-varying attributes
prim = stage.DefinePrim("/World/AnimatedCube", "Cube")
attr = prim.CreateAttribute("size", Sdf.ValueTypeNames.Float)
print(attr.Get()) # 1.0
print(attr.GetNumTimeSamples()) # 0
attr.Set(5.0, time_code)
print(attr.GetNumTimeSamples()) # 1
print(attr.Get(time_code)) # 5.0