PythonModule Variables

   1640   2   1
User Avatar
Member
133 posts
Joined: July 2005
Offline
Hey Guys:

In my PythonModule callback script, I'd like to define a few variables to be used by multiple functions. These variables are evaluations of the parms on the otl. Is it kosher to define these variables outside of a function? Will there be a potential conflict if I have two instances of an otl? Here's an example:

# I don't want to have to define the same variables multiple times.

inst = hou.pwd()
parm1 = inst.parm('parm1')
parm2 = inst.parm('parm2')

def func1():
print parm1
print parm2

def func2():
print parm1
print parm2

Assuming I'm passing in kwargs as an argument, I'd have to re-define the variables in each function.

# I suspect this is safer, though more cumbersome.

def func1(inst):
parm1 = inst.parm('parm1')
parm2 = inst.parm('parm2')

print parm1
print parm2

def func2(inst):
parm1 = inst.parm('parm1')
parm2 = inst.parm('parm2')

print parm1
print parm2
Francisco Rodriguez
Effects Animator | Walt Disney Animation Studios
User Avatar
Member
1907 posts
Joined: Nov. 2006
Offline
I would avoid the first method. Python Modules are node instance independent and using hou.pwd() inside it can be unpredictable. If you constantly need access to the same parms in different functions I'd just write a function that returned those parameters and use it in your various functions. Trying to stash the values outside of a function would also probably run into issues with the values possibly being incorrect and out of date.
Graham Thompson, Technical Artist @ Rockstar Games
User Avatar
Member
133 posts
Joined: July 2005
Offline
Awesome. Just as I suspected. I've been getting erratic behavior using that method. Especially ‘NoneType’ errors and other times Houdini would think that the instance has been deleted. I'll clean up my code right away! Thanks again!
Francisco Rodriguez
Effects Animator | Walt Disney Animation Studios
  • Quick Links