Prims

Working with Prims is a more complicated since Prims are extremely powerful objects in USD. Prims are referenced by their path in the stage, which is a string in the form of /Prim/ChildPrim. / is a special prim known as the root prim in a stage.

To get a reference to a prim at a path use stage_ref.GetPrimAtPath(path):

from pxr import Usd

stage_ref = Usd.Stage.Open('stage.usda')
prim = stage_ref.GetPrimAtPath('/Prim')
print(prim.GetName()) # prints "Prim"
print(prim.GetPrimPath()) # prints "/Prim"

To define a new prim use stage_ref.DefinePrim(path):

from pxr import Usd

stage_ref = Usd.Stage.Open('stage.usda')
prim = stage_ref.DefinePrim('/UnTypedPrim')
print(prim.GetName()) # prints "UnTypedPrim"

To define a new prim with a type use stage_ref.DefinePrim(path, type_name) or you can use your Type’s SomeType.Define(stage_ref, path) method:

from pxr import Usd, UsdGeom
stage_ref = Usd.Stage.Open('stage.usda')
prim = stage_ref.DefinePrim('/XformPrim', 'Xform')

# Above we have a Usd.Prim, if we want to access all the Xform's types natively, we need to get an Xform instance of our prim

xform = UsdGeom.Xform(prim)
print(xform.GetPath()) # prints "/XformPrim"

# it is often better to use the Define() method of your type right away, since it returns your typed instance rather than a Usd.Prim instance

xform_faster = UsdGeom.Xform.Define(stage_ref, '/AnotherXformPrim')

To delete a prim from the current edit layer (please refer to the documentation about RemovePrim for details) you can use stage_ref.RemovePrim(path):

from pxr import Usd

stage_ref = Usd.Stage.Open('stage.usda')
prim = stage_ref.DefinePrim('/UnTypedPrim')

if stage_ref.RemovePrim('/UnTypedPrim'):
print('/UnTypedPrim removed')

# if you try to access the prim object, it will still reference path but it is expired

if (prim.IsValid()):
    print('{} is valid'.format(prim.GetName()))
else:
    print('{} is not valid'.format(prim.GetName()))

# the above will print "UnTypedPrim is not valid"