Get the Parent Path for a Prim Path#
If you want have a Prim path and you want to get the Prim path of its parent, you can use Sdf.Path.GetParentPath().
from pxr import Sdf
def get_parent_path(prim_path: Sdf.Path) -> Sdf.Path:
parent_path = prim_path.GetParentPath()
return parent_path
#############
# Full Usage
#############
from pxr import Usd, UsdGeom
# Create an in-memory Stage with /World Xform prim as the default prim
stage: Usd.Stage = Usd.Stage.CreateInMemory()
default_prim: Usd.Prim = UsdGeom.Xform.Define(stage, Sdf.Path("/World")).GetPrim()
stage.SetDefaultPrim(default_prim)
cone_prim = UsdGeom.Cone.Define(stage, Sdf.Path("/World/Cone")).GetPrim()
# Given Sdf.Path('/World/Cone') for my_prim_path, parent_path will contain Sdf.Path('/World')
parent_path = get_parent_path(cone_prim.GetPrimPath())
usda = stage.GetRootLayer().ExportToString()
print(usda)
assert parent_path == default_prim.GetPrimPath()
This is an example USDA result from adding a Cone to the a World Prim.
#usda 1.0
(
defaultPrim = "World"
)
def Xform "World"
{
def Cone "Cone"
{
}
}