Jump to content
Sign in to follow this  
Drughi

Python from string parameter

Recommended Posts

Hi,

I have a string paramter on a node containing some python code and a button wich should execute the code. How do I write a callback script on the button so it executes the code stored as string in the other paramter?

Share this post


Link to post
Share on other sites

See this
The eval() function is what you are looking for

stringCode = node.parm("someCodeParm").eval()
eval(stringCode) 

As for calling this code when pressing a button, is this inside an HDA ?

Edited by Alain2131

Share this post


Link to post
Share on other sites
16 hours ago, Alain2131 said:

See this
The eval() function is what you are looking for


stringCode = node.parm("someCodeParm").eval()
eval(stringCode) 

As for calling this code when pressing a button, is this inside an HDA ?

Ok, so now my Buttons callback script is: eval(kwargs['node'].parm('python').eval())

I've set the Python code string Tags to script_callback_language: python and editorlang: python.

Just for testing I've just added: print "Yeay, it worked"

But I'm getting the error:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
SyntaxError: ('invalid syntax', ('<string>', 1, 5, 'print "Yeay, it worked"'))

What am I doing wrong?

Edited by Drughi

Share this post


Link to post
Share on other sites

Okay, I see now
My bad, here's the "right-er" info here

In fact, it's "exec" that you want :)

exec(kwargs['node'].parm('python').eval())

See below for the test I did

image.thumb.png.80253e46af511d72155343f9569805c5.png

Edited by Alain2131
  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×