In USD Python is there any (easy) way to take the USD stage and layer instances I may have and make a copy in memory of them?
I’m trying to keep a copy of a Houdini LOP node’s stage and layers in memory, and requery it again with different context option. However, since both share the same USD stage and Sdf layers in memory it means that cooking again basically seems to have updated the initial instance too.
# simplified pseudocode
lopnode: hou.LopNode
hou.setContextOption("shot", "sh010")
sh010 = lopnode.stage()
hou.setContextOption("shot", "sh020")
sh020 = lopnode.stage()
I want to be able to access both of the stages in the state of those context options.
I guess I could technically do something like:
stage: Usd.Stage
uuid = str(uuid.UUID())
root_layer = stage.GetRootLayer()
root_layer_clone = Sdf.Layer.CreateInMemory(root_layer.identifier + uuid)
root_layer.TransferContent(root_layer_clone)
for layer in stage.GetLayerStack():
clone = Sdf.Layer.CreateInMemory(layer.identifier + uuid)
layer.TransferContent(clone)
# But then I'd need to also remap ALL the layers from their original references to the new cloned in memory references?
# Plus I'd essentially only really want to clone the in-memory layers then that in this case are relevant to Houdini's graph (but that's maybe an optimization only)
clone_stage = Usd.Stage(root_layer_clone)
However, I’m not sure how to easily remap them correctly to their cloned once.
But even better, I’m sure I’m being stupid here and there must be a better way!