Create a New Stage#
A Stage object is a central container that serves as an entry point for USD data. A Stage holds the scene graph and associated prim data, allowing for easy access and manipulation of said prims. See the OpenUSD Terms & Concepts page to learn more about Stage functionalities.
Create a new Stage.
from pxr import Usd
def create_new_stage(path : str, stage_name : str = "my_new_stage.usd") -> Usd.Stage:
layer_path : str = path + stage_name
stage: Usd.Stage = Usd.Stage.CreateNew(layer_path)
return stage
##############
# Full Usage
##############
from pxr import UsdGeom, Sdf
# Create a stage
stage = create_new_stage("my_path/")
# Create and set /World Xform prim as the default prim
default_prim = UsdGeom.Xform.Define(stage, Sdf.Path("/World"))
stage.SetDefaultPrim(default_prim.GetPrim())
# Print the stage
print(stage)
# Save the stage
stage.Save()
# Export the stage to a new file. Serialization format can be changed based on provided file extenson (.usd, .usda, .usdc)
stage.Export("my_path/my_new_stage.usda")
assert stage.GetRootLayer().ExportToString() # .usda for the stage exists
assert Usd.Stage.Open("my_path/my_new_stage.usd") # Stage can be opened
assert stage.GetDefaultPrim().GetPath() == Sdf.Path("/World") # Default prim is set at corect path
Create a new Stage only in memory. These stages are anonymous layers and hence cannot be saved or opened.
To create a stage without a session layer, pass sessionLayer = None
as an argument to Usd.Stage.CreateInMemory()
from pxr import Usd, Tf
def create_stage_in_memory(identifier : str = "MyStage") -> Usd.Stage:
stage: Usd.Stage = Usd.Stage.CreateInMemory(identifier)
return stage
##############
# Full Usage
##############
from pxr import UsdGeom, Sdf
# Create an in-memory Stage
stage = create_stage_in_memory()
# Create and set /World Xform prim as the default prim
default_prim = UsdGeom.Xform.Define(stage, Sdf.Path("/World"))
stage.SetDefaultPrim(default_prim.GetPrim())
# Print the stage
print(stage)
# Export the stage to a new file. Serialization format can be changed based on provided file extenson (.usd, .usda, .usdc)
stage.Export("my_path/my_new_stage.usda")
assert stage.GetRootLayer().ExportToString() # .usda for the stage exists
assert stage.GetDefaultPrim().GetPath() == Sdf.Path("/World") # Default prim is set at corect path