Creating collections using Python

   1310   3   0
User Avatar
Member
11 posts
Joined: Jan. 2015
Offline
Hi
I'm trying to create collection using following code:

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

from pxr import Usd

c = Usd.CollectionAPI(stage.GetPrimAtPath('/collections'), 'dupxo')

inc = c.CreateIncludesRel()
inc.AddTarget('/sphere1')
c.CreateExcludesRel()
c.CreateExpansionRuleAttr(Usd.Tokens.explicitOnly)
c.CreateIncludeRootAttr()

In scene graph, it looks like the collection gets created. But it doesn't appear in primitives field menu. What am I missing in my script?

Attachments:
mgraph.png (22.3 KB)
menu.png (11.8 KB)

User Avatar
Member
11 posts
Joined: Jan. 2015
Offline
As a workaround I created a Collection operator to generate empty collection and a Python node to populate collection with data. Then I made an HDA from it. Not an ideal solution, but it works fine.
User Avatar
Staff
4525 posts
Joined: July 2005
Offline
You python code isn't correctly creating the CollectionAPI. To add a new collection to a prim, you need to call UsdCollecitonAPI::Apply (https://openusd.org/dev/api/class_usd_collection_a_p_i.html#a4c9c1226d01a049864cc0d437900c458). This adds the metadata to the prim indicating that the collection exists. The code you've written just creates some attributes that are associated with a collection with a particular name, but doesn't actually register the collection instance on the prim.
User Avatar
Member
11 posts
Joined: Jan. 2015
Offline
It works. Thanks a bunch.
  • Quick Links