Variable Scope with Python in Python Module node

   2366   2   2
User Avatar
Member
2036 posts
Joined: Sept. 2015
Online
I have a pair of IF statements that I am using in a Python Node.

It appears that Python in the Python Node considers variables created within an IF statement to be out of scope if one attemps to use it outside the IF statement; almost like variables declared within a c function.

I googled a python search and apparently this is typically allowed in Python, at least from what I read. Meaning that an IF statement shouldn't block a variable created within it from being used elsewhere like immediately after that IF statement block.

It seems that the python node may have it's own Python rules?

In the first pair of IF statements below Houdini complains that “New_Positions” is not defined:

if KF_START_A == hou.frame():
New_Positions = hou.session.Current_Positions_HUB_A()

if ( ( KF_START_A < hou.frame()) and ( KF_STOP_A > hou.frame()) ):
hou.session.Translate_Square_Corners_ppmm(List, New_Positions, 1)


However, if I set “New_Positions” in the IF statement where it's going to be used then Houdini is “Happy”:


if KF_START_A == hou.frame():
# Old place where New_Position was

if ( ( KF_START_A < hou.frame()) and ( KF_STOP_A > hou.frame()) ):
New_Positions = hou.session.Current_Positions_HUB_A()
hou.session.Translate_Square_Corners_ppmm(List, New_Positions, 1)


A second not closely related question but while I am at it.

I can't seem to block out large blocks of code with triple quotes.

Apparently you can do this with Python, but it doesn't seem to work in the Python Source Editor ( only pound symbol for single lines )

Any comments are appreciated - Thank you
User Avatar
Member
818 posts
Joined: Sept. 2013
Offline
BabaJ
It appears that Python in the Python Node considers variables created within an IF statement to be out of scope if one attemps to use it outside the IF statement; almost like variables declared within a c function.

I googled a python search and apparently this is typically allowed in Python, at least from what I read. Meaning that an IF statement shouldn't block a variable created within it from being used elsewhere like immediately after that IF statement block.

It seems that the python node may have it's own Python rules?

This seems to work for me. See the attached .hip file. Since the variable is initialized at the “start” frame, whenever you start a new session, you always have to go to the “start” frame so that the variable is initialized. Otherwise, the variable will not be defined, and you would see the Python error.

BabaJ
I can't seem to block out large blocks of code with triple quotes.

Apparently you can do this with Python, but it doesn't seem to work in the Python Source Editor ( only pound symbol for single lines )

Hm, this also seems to work for me in the Python Source Editor.

Attachments:
python.hip (50.5 KB)

Andrew / アンドリュー
User Avatar
Member
2036 posts
Joined: Sept. 2015
Online
Hi awong,

Yeah, thanks for the reply.

And yes when I have the frames set to the begining the error message goes away. Don't know why I didn't think to check that as I am in the habit of resetting to the start when I make changes anyways; maybe there was just a glitch at the time or I just thought I did when I didn't.

As for the commenting out blocks of code.
Yeah I opened your file and I get the same thing. The code blocks out to green. But I was assuming that means it will be treated as a string and not something blocked out.

My understanding of Python is that you can define a string through any combination pairs of ‘ or “ and as singles, doubles or triples. In all cases in the Python Source Editor it turns to green.

When you use the ”#" symbol it will block out in yellow. So when I use triple quotes I get the typed blocked out as green and seeing that am assuming it’s going to treat it as a string, perhaps introducing some bugs that may not be apparent?

Thanks again for your comment.
  • Quick Links