Setting varmaps with python

   4724   4   1
User Avatar
Member
26 posts
Joined: Dec. 2012
Offline
I’ve been learning on how to use Python for Houdini and I’m trying to create a Python Sop Operator that gets the attributes and creates a Local Variable/varmap for each of them, as some Sop nodes don’t do that automatically. So far, I’ve been able to figure out how to query in the geo with their attributes as seen below. However I’m now a little stuck in figuring out how to get those very attributes and create varmaps from them. In particular, I’m looking for what functions I can use to help with that.

# This code is called when instances of this SOP cook.
node = hou.pwd()
geo = node.geometry()

# Query the geo:
if geo.points():
point_list = geo.points()
if geo.prims():
prim_list = geo.prims()
Vincent Balmori
User Avatar
Member
617 posts
Joined: Aug. 2008
Offline
you could use a
addNameVariable vexpression.
User Avatar
Member
26 posts
Joined: Dec. 2012
Offline
Thank you, but for this in particular I'm trying to see how I can do it in Python.

The function I'm trying out to set up the local variables/varmaps is `.setGlobal Attributes()`, but mainly the latter. I thought I thought I understood this function, but every time I execute I get his error when I MMB. So I would like to know if I'm using this function correctly (the scene is attached below):

OperationFailed: The attempted operation failed.
No attribute with this name exists
for attr in attr_list:
name = attr.name()
geo.setGlobalAttribValue(name, name.upper() )

Attachments:
VarmapPython_Test.hipnc (160.7 KB)

Vincent Balmori
User Avatar
Member
8592 posts
Joined: July 2007
Offline
here is an example of creating variable mappings for all incoming attributes on the geometry

geo = hou.pwd().geometry()

# get all attribs
attribs = geo.pointAttribs() + geo.primAttribs() + geo.vertexAttribs()

# populate varmap list with variable mappings
varmaps =

# create varmap if doesn't exist
if geo.findGlobalAttrib(“varmap”) is None:
geo.addAttrib(hou.attribType.Global, “varmap”, “”)

# store varmap list into varmap detail attribute
geo.setGlobalAttribValue(“varmap”, varmaps )
Tomas Slancik
FX Supervisor
Method Studios, NY
User Avatar
Member
26 posts
Joined: Dec. 2012
Offline
Awesome thank you! I learned a lot from looking at that code.

Update: So the only part of the code I'm not fully clear about is the `for attrib in attribs`. So you are accessing the ‘attribs’ module and setting the ‘attrib’ values or is that a For-Loop? I tried looking through the Help browser for something like that, but with no luck.

Also, the way it is formatted by being positioned at the end of that line is something I haven't seen before, so I'm wondering what it does in a Python context?
Vincent Balmori
  • Quick Links