Flattening keeping payloads as payloads

Hi all,

I’m trying to profile a few things in our scenes, and one thing I’d like to compare is to remove the majority of the files and flatten all in one file, but I’d like to keep payloads as payloads, so the actual geometry is not flattened.

I’ve tried a few combination of FlattenLayerStack and copying roots of layers, but I either flatten too much, or not enough.

About the “not enough” part, I think I could spend a bit more time there and be more creative about how I’m doing it,
but before I get in this rabbit hole ( I was thinking of going through all the layers, stitching them together ), is there already something I can use for this “intent” ?


Not sure how to avoid the rabbit-hole, because what’s going to work best is going to be highly dependent on your composition and asset structure… if you are making payloads directly from your scene to your assets, then maybe recursive FlattenLayerStack works (i.e. do it separately for each asset, also)? Or maybe fully Flatten() each asset (though you’ll lose VariantSets then) ?

But this isn’t something we attempt to provide helpers for.

Wouldn’t this essentially be:

  • Load stage with payloads disabled
  • Export the flattened stage
  • Import the flattened stage to new layer
  • Transfer all “payloads” from PrimSpecs on the original layer to the new layer?

Since the composed flattened stage without the payloads load would basically be what you want, but then with your “prim_spec.payloadList” opinions added back into it?


from pxr import Usd, Sdf

stage = Usd.Stage.Open(filepath, load=Usd.Stage.LoadNone)

layer = Sdf.CreateAnonymous()

for prim in stage.Traverse():
    if not prim.HasAuthoredPayloads():
    # Not sure how to "merge" these into the singular root layer
    # on the new stage - likely you might need to use "UsdUtils.StitchInfo"
    # to merge the specs?
    payloads = get_authored_payloads(prim)  # implement this
    new_prim_spec = layer.GetPrimAtPath(prim.Path())
    new_prim_spec.payloadList.prependedItems[:] = payloads  # implement this; set it however `get_authored_payloads` would return it in the correct way

Or what in particular would make this more complicated?

Not necessarily. Recall that LIVRPS is recursive, which means that payload opinions can be intricately layered with opinions from non- payload arcs. And the ”S” Specilaizes arcs are weaker than all other arcs, but flattening will raise their strength higher than that of a payload. If you want to preserve any arcs robustly, the best you can do is flatten the root layerStack.