Concatenate a Prim Path#

If you want to concatenate two strings to extend a prim path, you can use Sdf.Path to store and manipulate prim and property paths. This is useful for extending a prim path with the name of a new child prim that you plan to create. In this snippet we use Sdf.Path.AppendPath(), but you can learn more about other append methods.

from pxr import Sdf

def concat_prim_path(prim_path: Sdf.Path, path_to_add: str) -> Sdf.Path:
    concat_path = prim_path.AppendPath(path_to_add)
    return concat_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)

# Concatenate the Paths
concatenated_prim_path: Sdf.Path = concat_prim_path(default_prim.GetPrimPath(), "Kitchen_set/Props_grp/North_grp/NorthWall_grp/MeasuringSpoon_1")

usda = stage.GetRootLayer().ExportToString()
print(usda)

assert concatenated_prim_path.pathString == "/World/Kitchen_set/Props_grp/North_grp/NorthWall_grp/MeasuringSpoon_1"