How to have both the Sdf.Layer and the Usd.Stage available?

   3895   2   0
User Avatar
Member
23 posts
Joined: March 2020
Offline
Heyja,

Previously when I wanted both the layer and the stage available in a python LOP I'd just do

from pxr import Usd, Sdf

node = hou.pwd()
layer = node.editableLayer()
stage = Usd.Stage.Open(layer)

now I wanted to script something that I'd love to cache out after so thus put a layerbreak over the python lop but trying to reference an existing prim now with stage.GetPrimAtPath("/my/fav/prim") now throws an error that I'm referencing an invalid null prim and traversing the stage yes it seems to think the stage is empty.


I tried it the other way around as well with

from pxr import Usd, Sdf

node = hou.pwd()
stage = node.editableStage()
layer = stage.GetRootLayer()


but got some odd behaviour there with having a primspec copied with Sdf.CopySpec still visible in the stage upstream.



What's the correct way to deal with this?

thanks,
Florens
Edited by florens - Nov. 14, 2023 00:04:49
User Avatar
Staff
4565 posts
Joined: July 2005
Offline
You absolutely cannot use the first approach. It will never work, because what you're telling USD to do is create a brand new stage that contains _only_ the active layer. Meaning most of your scene won't be there. You might want to dig into the docs at https://www.sidefx.com/docs/houdini/solaris/about_lops.html [www.sidefx.com] if you want some information about how LOP stages are structured. The Scene Graph Layers pane can be really helpful here too.

Your other approach is close, but not quite. As the Python LOP documentation says:
You must not edit the root layer of the stage.

But of course a python LOP must be editing _some_ layer on the stage, right? That's the "active layer". When you do:
stage = node.editableStage()
Houdini sets the Edit Target of the stage to the active layer. So if you want access the the Sdf.Layer object for the active (editable) layer, you just have to go through the edit target:

layer = stage.GetEditTarget().GetLayer()
User Avatar
Member
23 posts
Joined: March 2020
Offline
Amazing thank you Mark! I'll be off cleaning up some of my hdas now
  • Quick Links