Attribute shorthand in Python expressions

   1633   2   1
User Avatar
Member
15 posts
Joined: Feb. 2016
Offline
Hey guys, quick question:

Is there anything similar to the
`@attribname`
shorthand when writing python expressions on parameters in TOPs? Currently I've been using
pdg.workItem().stringAttribValue()
etc.


Oh and aside question - when using the shorthand syntax in a python script TOP, sometimes it finds the attributes just fine, others it only works with
work_item.attribValue("someattributename")
. Are there any rules to follow when using
"`@attrib`"
syntax in a python script TOP? So far I just do trial and error, and if it doesn't cook I switch to the long way.

Thanks!
User Avatar
Staff
585 posts
Joined: May 2014
Offline
Backtick expansion, e.g. expression like `@attrib`, are not part of the Python script. They're part of Houdini's string parameter evaluation, and get filled in when PDG evaluates the script parm on the node and stores the script contents on the work item. That occurs when the work items are being generated. If the attribute doesn't exist when the item is being generated, then the @attrib access will evaluate to nothing.

You can also use pdg.workItem() to access attributes. That returns back an attribute object, such as a pdg.AttributeFloat [www.sidefx.com] – the type of the object it returns depends on the type of the attribute. You can then use the array bracket operator to access values, for example:

pdg.workItem()['position'][1]
Edited by tpetrick - Sept. 7, 2020 17:47:41
User Avatar
Member
15 posts
Joined: Feb. 2016
Offline
Great, thank you for the explanation!

I will give the example snippet a try.
  • Quick Links