Creating Prims with Python in LOP

   2583   5   2
User Avatar
Member
120 posts
Joined: Jan. 2012
Offline
Hi,

Can I get some help to get started with Python in LOPs.

I would like to create Volume Prim with Python Script LOP and assign it some fields( VDB paths).
I found this method in USD API docs, but not sure who to translate it to python
https://graphics.pixar.com/usd/docs/api/class_usd_vol_volume.html#af88d8076106e2b55bcc5bdcec3fc5e23 [graphics.pixar.com]

This is failing miserably on the last line.

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

vol = stage.DefinePrim('/vol', 'Volume')
vol.CreateFieldRelationship('somename', 'somepath')

AttributeError: 'Prim' object has no attribute 'CreateFieldRelationship'
Edited by tas3d - April 29, 2021 16:28:30
Michal Tas Maciejewski @ www.vfxtricks.com
User Avatar
Staff
4438 posts
Joined: July 2005
Offline
DefinePrim returns a Usd.Prim, which, as the error says, doesn't have a CreateFieldRelationship method.
You need a line in between those two that does `vol = UsdVol.Volume(vol)` to create a UsdVol.Volume object based on the Usd.Prim. This is a very common idiom in USD's python (and C++) APIs. This is because, I think, the base Usd.Prim object can be "interpreted" as a bunch of different object types at the same time. So there is no single class type that can encapsulate everything that a given Usd.Prim can do.
User Avatar
Member
120 posts
Joined: Jan. 2012
Offline
Thanks a lot! I think I got a hang of it now.
Here is snippet for creating Volume with VDB fields inside.

from pxr import UsdVol

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

volume = stage.DefinePrim('/volume', 'Volume')
volume = UsdVol.Volume(volume)

density = stage.DefinePrim('/volume/density', 'OpenVDBAsset')
density.GetAttribute('filePath').Set('Y:/eggsplosion.$F4.vdb')
density.GetAttribute('fieldName').Set('density')

volume.CreateFieldRelationship('density', density.GetPath())
Edited by tas3d - April 29, 2021 22:45:14
Michal Tas Maciejewski @ www.vfxtricks.com
User Avatar
Staff
4438 posts
Joined: July 2005
Offline
Of course there's also the Volume LOP for this
But I assume you have your reasons.
User Avatar
Member
120 posts
Joined: Jan. 2012
Offline
Hey Mark,

Volumes work fine, now I would like to programmatically reference multiple USD files.
Pretty much what Reference/Sublayer LOPs do.

Any pointers for classes and methods from API that will do the job?
https://graphics.pixar.com/usd/docs/api/index.html [graphics.pixar.com]
Michal Tas Maciejewski @ www.vfxtricks.com
User Avatar
Member
120 posts
Joined: Jan. 2012
Offline
I asked too early.

Found these and learned about importance of Default Primitive

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

prim = stage.OverridePrim('/test')
prim.GetReferences().AddReference(filePath)
Michal Tas Maciejewski @ www.vfxtricks.com
  • Quick Links