Multi Line Python in Callback Script

   5781   3   1
User Avatar
Member
2042 posts
Joined: Sept. 2015
Offline
Hello,

I was wondering if anyone knew the syntax of getting multiple lines of python to execute in a toggle parameters callback section.

The following code works if only one ‘if’ statement is used, but not both:

if (kwargs['node'].parm("switch_toggle").eval() == True): kwargs['node'].parm("switch_selection").set(1)

or

if (kwargs['node'].parm("switch_toggle").eval() == False): kwargs['node'].parm("switch_selection").set(0)

From some searching I found some references to using the semi-colon as a line separater so to get both ‘if’ statements in the callback script I used the semi-colon like:

if (kwargs['node'].parm("switch_toggle").eval() == True): kwargs['node'].parm("switch_selection").set(1); if (kwargs['node'].parm("switch_toggle").eval() == False): kwargs['node'].parm("switch_selection").set(0)

But this did not work as I get a syntax error message.

I know I could create a definition in the python source editor or create an hda ( which is actually how I have it in the original context.)

But to keep this post short in explaining, I cannot in this case use the python source editor nor create an hda.

Does anyone know if it's possible to do what I want? Point me in the right direction syntax wise?

The help is much appreciated, Thanks.

P.S. the file attached is using just one of the ‘if’ statements to show what it is I wish to accomplish.
This also needs to be done in H15 ( not sure if things have changed for H16 ).
Edited by BabaJ - Feb. 23, 2017 11:51:44

Attachments:
Callback Script MultiLine.hiplc (49.1 KB)

User Avatar
Member
7820 posts
Joined: Sept. 2011
Offline
The basic form of an if then else block in python is “action 1 if condition else action 2”
for example:
kwargs['node'].parm('mode').set(1) if kwargs['node'].parm('checkbox').eval() else kwargs['node'].parm('mode').set(0)
Edited by jsmack - Feb. 23, 2017 12:10:19
User Avatar
Member
2042 posts
Joined: Sept. 2015
Offline
Thanks alot jsmack…I didn't have this concept of python “action 1 if condition else action 2”

I'll have to remember this.

I'm used to being able to start with the condition then do the action.

Like in my example I can just use only ‘if’s in a defined function in the python source editor like this:
def Toggle_Bypass_Switch(Node,Toggle,Switch):

if (Node.parm(Toggle).eval() == True):
Node.parm(Switch).set(1)

if (Node.parm(Toggle).eval() == False):
Node.parm(Switch).set(0)

Or doing something completely different ( result wise ) do something like this in a parameter expression:

Result = 0

if(hou.frame() == 5):
Result = 3.1

if(hou.frame() == 17):
Result = 5.1

if(hou.frame() == 37):
Result = 2.54

return Result
Edited by BabaJ - Feb. 23, 2017 14:17:30
User Avatar
Member
2042 posts
Joined: Sept. 2015
Offline
Oh..btw..your suggestion works for the callback script…thanks
  • Quick Links