Jump to content
Sign in to follow this  
Atom

Run Python When Button Is Clicked? (Callback Script)

Recommended Posts

I have NULL with a python script inside with a single print statement. At the object level of the NULL I have add a Button to the parameter interface.

 

How do I link the code to the button?

What do I put in the callback script field?

 

Thanks

Share this post


Link to post
Share on other sites

Hmm... thank you for the link from 2008 but I think things have changed. That looks like spoofing python using Hscript. But now we have the full option of running Python natively.

 

I mentioned a simple print statement in the first post to simplify the problem but I do need to import and run and entire module from a button click.

 

 

The path to my python module looks like this:

/obj/null1/python1/python

 

I drop that into the callback field, switch from Hscript to Python and when I click the button I get an error.

SyntaxError: ('invalid syntax', ('<stdin>', 1, 1, '/obj/null1/python1/python\n'))

 

edit

.

.

.

If I change the statement in the callback field to an exec() I get a little further.

exec("import python1")

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
ImportError: No module named python1
 
However if I use the full path to the node I get.
exec("import /obj/null1/python1")
 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
SyntaxError: ('invalid syntax', ('<string>', 1, 8, 'import /obj/null1/python1\n'))
 
How the heck do I supply a path to my python module for inclusion..?
Edited by Atom

Share this post


Link to post
Share on other sites

so you added a python module called "python1" to your null object with the button, yeah?  honestly, i'm not sure how you'd reference custom python module.  however, if you just create the normal "python module", then it's very easy to call functions out of that.

 

if you have a function defined in your python module called, say, "foo()" you could call that by putting this in your callback script (setting the language to python):

 

hou.phm().foo()

 

that should do it.  phm() is a shortcut to retrieve the python module from the current node.  long form would be "hou.pwd().hdaModule()" which does the same thing.

Share this post


Link to post
Share on other sites

I have NULL with a python script inside with a single print statement.

 

For me it sound like:" I have a c++ source code somewhere in my car".  :mellow:  Where exactly did you put your script and why?

 

 

The path to my python module looks like this:

/obj/null1/python1/python

 

 

How is that?. There is no such thing in houdini as "Python Module SOP". Python SOP can contain python code, but it shouldn't be considered for storing python code unrelated to geometry, this operator belongs to.

I think you misunderstood python in houdini completely. For storing python code in hip file, use hou.session module (Windows -> Python Source Editor). For storing python in operator, make HDA and put python there. 

Edited by Stalkerx777

Share this post


Link to post
Share on other sites

I don't think I misunderstand python completely. I already have a scene generator working in the Python Source Editor. Perhaps HDA is the way to go, but what I am trying to accomplish is to simply add some interface controls to my existing script. I thought if I could just place it in a NULL and then add a button that would work. Then I could I add some parameters to the interface to route to my existing variables in the script.

 

NOTE: My code is not generating geometry within the node, it is creating an entire scene. For example I expect to be able to issue a hou.createNode() anywhere within Houdini. Even if it is considered a poor practice, it should work, correct? So within the example image shown below I could put a line of code that created a subnet at the /obj level. That, of course, has nothing to do with the SOP level of the NULL where the code is executing. I am just looking for a container with a button and some parameters. The Source Editor Window does not offer that ASFAIK.

 

My Steps So Far:

Create a NULL.

Dive Inside.

Delete what is there.

Add a Python Node.

 

So the button sits one level up, and so would the parameters. The script sits inside. Why is this asking for the moon?

 

Houdini clearly supports this setup because I can create it. I just need a way to call my main() and reference external parameters.

post-12295-0-19562600-1418997100_thumb.j

 

@fathom

 

 

 if you just create the normal "python module", then it's very easy to call functions out of that.

I thought I was creating a normal python module using the steps I mentioned above? What do you call a normal python module and where does it reside within the Houdini scene? I already have code within the Python Source Editor is that what you mean?

Edited by Atom

Share this post


Link to post
Share on other sites
I thought I was creating a normal python module using the steps I mentioned above? What do you call a normal python module and where does it reside within the Houdini scene? I already have code within the Python Source Editor is that what you mean?

 

That's what i mean when i say you misunderstood python in houdini.  :)

Read what python module is: http://www.sidefx.com/docs/houdini13.0/hom/assetscripts

 

Just create digital asset out of your null, add python module section, and make any magic you want.

Share this post


Link to post
Share on other sites

So what good is the callback script? I still don't get it. If I can only put one line in the callback field what good is it?

Share this post


Link to post
Share on other sites

@fathom

 

 

I thought I was creating a normal python module using the steps I mentioned above? What do you call a normal python module and where does it reside within the Houdini scene? I already have code within the Python Source Editor is that what you mean?

 

 

i misunderstood what you had done.  i thought you had created a python module inside of a digital asset but had given it a custom name (not the generic "python module" name).  now i understand that you created a python sop.  a digital asset is really the way to go here.

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  

×