How to run a python script node?

   4999   9   1
User Avatar
Member
117 posts
Joined: Feb. 2015
Offline
I created a python script node in the obj, but how do I start it??
lets say it says Print("run silly script")

I'm just trying to find a easy way in my scene to have a local script to this scene that I can trigger somehow.
I want to load external libraries and do all kinds of things to my scene.

I can:
1. Create a button on the UI, but I do not want to use that since this script has setup for each scene while buttons are global.
2. use Python Source Editor, But then it starts the script every time I start the level which I do not want
3. I can make a python SOP, but that will run every time I enable the node, so very easy to trigger the script by mistake and I have a large batch script that I run

Frustrating! It feels like they forgot to add the Execute/Run/Play button on the script nodes.

Any ideas where best to place scripts like this
User Avatar
Member
117 posts
Joined: Feb. 2015
Offline
I found this tutorial which shows how to add my own Run button. Works I guess but a bit fiddly.

https://www.tokeru.com/cgwiki/index.php?title=HoudiniPython#Make_a_general_python_input_window [www.tokeru.com]
User Avatar
Member
313 posts
Joined: Oct. 2016
Offline
It is similar to the this thread. https://www.sidefx.com/forum/topic/86275/ [www.sidefx.com]

It can call a specific function and that is necessary.

exec(kwargs.parm('code').eval());myFuncrion()

So I see with Houdini that scripts get appended with .eval() or .cook(force=True) to ”run”.

I tested the mentioned method and it is quite simple actually and does work, i.e. if all you want is some Python, a function, and then a button to call it.
Interested in character concepts, modeling, rigging, and animation. Related tool dev with Py and VEX.
User Avatar
Member
117 posts
Joined: Feb. 2015
Offline
Hiya SWest.
I implemented the button approach above. It works quite well. I think only downside I see is that if I give this to my artists they would either have to create this setup every time the want to use the scripts or more likely they would have to open a file with this node in it and copy from it.
User Avatar
Member
313 posts
Joined: Oct. 2016
Offline
Well, I agree with your point. Maybe there is another method that is easier for artists.

In my own workflow I'm using the above method, but the difference is how it is applied. Learning these things took weeks, with a lot of moments fiddling in the dark, and there was not very good help available.

However, if you can define your goals and how you want to do things, and you have enough time and do the effort, you will likely make progress.

Attachments:
out.mp4 (9.5 MB)

Interested in character concepts, modeling, rigging, and animation. Related tool dev with Py and VEX.
User Avatar
Member
313 posts
Joined: Oct. 2016
Offline
Here are some basic ideas.

A simple thing you could do is this.

Put your code in a script or python node.

Then make a single shelf button that will first pick the selected node, validate that it is s script node, and finally just run it.

The same shelf button will run them all, based on selection.

Another method could be just make a shelf button that will add the ”run” button to each of your script nodes.

Typically software uses selection and button or just tool and application (clicks). So your artists might be happy with something like that maybe?
Interested in character concepts, modeling, rigging, and animation. Related tool dev with Py and VEX.
User Avatar
Member
8747 posts
Joined: July 2007
Offline
You can also create a HDA with button and string Parm for your code
And let button callback execute your code from string parm

Having it as HDA will make it easier to make accessible to all your artists and also potentially keep extending if needed
Tomas Slancik
FX Supervisor
Method Studios, NY
User Avatar
Member
313 posts
Joined: Oct. 2016
Offline
Yes! After posting I started thinking about this a bit more. As tamte wrote I would also do it, and make sure the HDA is available to all artists. This is basically how HDAs should work. However, you could put all your code in that HDA, even as hidden strings.

Alternatively, if you do not want to include the code in the actual HDA you could use the script node to read files that you could place next to the HDA file location or somewhere accessible. Actually that node is not even needed. In my shelf I have one import button that will read my entire "generic" module that hold most of my scripts that are not supposed to be included in any HDA. It will also copy necessary scripts to nodes if needed. The shelf buttons only make calls to various functions in that code (because I do not want to spread out my "source" code everywhere). A single button could both include and run a function also. It just depends on how you like your work to be (as long as you solve the core problems and needs I guess). As a side note, in my real job (this is not) my choice of methods will basically determine my own stress level and the amount of related issues that will show up, so finding methods that keep that under control is recommended.

Additionally it is very recommended to start new things in a long term direction, because it is not effective to have to redo things almost from scratch. If things are very basic in the beginning does not matter, with some time it will be possible to improve it. Knowing that what you do will be used for a long time adds to your motivation to do it as well. The opposite is also true.

In the UI parm editor (promoted parms) there exist a bunch of useful things, for example drop down menus. I could easily imagine you provide just a drop down list for artists, and based on the choice there will be different checkboxes, sliders, node selections and whatever. Then the "run" button will then execute whatever code is chosen and the settings. The settings can just be sent as arguments (or parameters if you prefer) to your functions.

There might be a lot of learning involved, but if you can just get something barely working out there then you can upgrade in small steps over a long period. That is usually how I like to do things.

Edit: However you do it, as I had to learn from someone to not forget the arguments, I would consider not only running scripts but also making some settings related to each of them. Just "running" scripts is too basic. Alternatively, you can make your code "context aware" so that based on what is selected and found by the code, different changes are done.

Edit2: Here's how I import my generic module. It just works, however it is not built for a network, however that is simple.

from os.path import join
from sys import path
from importlib import reload

#hipFilePath = os.path.dirname( str(hou.hipFile.path()) )
#print(hipFilePath)

# mymodule_dir = os.path.join( hipFilePath, '..', 'a', 'b' )
# myModuleDir = os.path.join( hipFilePath, 'py' )
myModuleDir = join( '/home','username', 'houdini19.0', 'py', 'generic' )
# print(myModuleDir)

path.append( myModuleDir )

import generic
hou.session.generic = generic

def refresh():
    reload(generic)

refresh()

# just a typical button

#import generic

hou.session.generic.myFunction(arg1,arg2)
Edited by SWest - Oct. 31, 2022 04:50:54
Interested in character concepts, modeling, rigging, and animation. Related tool dev with Py and VEX.
User Avatar
Member
1 posts
Joined: July 2022
Offline
Steps to Run a Python script from Node. js
Run a simple python script. Create a new folder .

Send parameters to python script. Most of the time python scripts require to pass a number of parameters.

Get a JSON data set from python.

Regards,
Rachel Gomez
User Avatar
Member
146 posts
Joined: June 2016
Offline
I know this is pretty old post, still posting my method here so it may help people in future.

Python has this function called exec(string) which executes given string as a python code.

So you can simply do these steps

  1. create any node
  2. gear icon > edit parameter interface > create a multine string parm > optionally set it to python language for syntax highlight
  3. create a button > add a this in callback
    exec(hou.pwd().parm("your_string_parameter").eval())
    

It will get the string from multiline string parameter and runs it as python code.
obviously you can point this to parm from any node and run from that node.
this could be very handy when you want to store python codes in hip file itself. very much portable!

Attachments:
Screenshot from 2024-05-31 15-51-02.png (89.4 KB)

Mohan Pugaz
movfx
https://www.instagram.com/movfx/ [www.instagram.com]
https://www.youtube.com/channel/@_movfx
  • Quick Links