How to run a python script node?

   853   8   0
User Avatar
Member
115 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
115 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
218 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.
IT-teacher (8y) / just for fun TD & artist / Debian11 XFCE / Houdini / Zbrush (Wine) / Blender / Python / VNC / networks / two small kids / BSc CS:CG / Stockholm
User Avatar
Member
115 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
218 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)

IT-teacher (8y) / just for fun TD & artist / Debian11 XFCE / Houdini / Zbrush (Wine) / Blender / Python / VNC / networks / two small kids / BSc CS:CG / Stockholm
User Avatar
Member
218 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?
IT-teacher (8y) / just for fun TD & artist / Debian11 XFCE / Houdini / Zbrush (Wine) / Blender / Python / VNC / networks / two small kids / BSc CS:CG / Stockholm
tamte
Member
7192 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
218 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
IT-teacher (8y) / just for fun TD & artist / Debian11 XFCE / Houdini / Zbrush (Wine) / Blender / Python / VNC / networks / two small kids / BSc CS:CG / Stockholm
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
  • Quick Links