Running Python Scripts from KeyFrame Activation

   2776   2   1
User Avatar
Member
2048 posts
Joined: Sept. 2015
Offline
Hello,

So I've been successful in writing python functions in the Python Source Editor and then placing the name of the functions in the parameter edit boxes and having them run when the keyframes are played; basically the same as entering a HS script.

Part of the reason for doing this is to learn how to set up more complicated functions or rather have much more concurrent activity in my scene but wanting to have all that code more in a central place so the work flow is much easier by looking at my function at a glance to make adjustments relative to other things going on. It can get a bit tedious if I am having to do alot of clicking on nodes and going into each parameter box to adjust individual functions.

So now I feel I am on track to how I like to set up my “workflow” in regards to coding. The following code is what I started with to test how to do it. You can see I return one value with the shape() function. That function is placed in the edit box of the corresponding parameter so that when the Keyframes are played it gets the values. However, to test my intention of being able to do things “remotely”, I use a set function to apply the value to another objects parameter and as I hoped it worked. That other parameter box doesn't have any reference to the shape() function directly ie. I didn't place that function in its corresponding edit box.

So seeing it can be done “remotely” I was hoping to be able to have the functions run when the keyframes are played without having to even put the function in a single edit box.

Is there another way to get the function and or list of functions “triggered” to run when keyframes are played other than through their placement in the edit boxes?

I looked in the help files and the closest thing I came by was mentioning of Script SOP but in reference to when you cook and still didn't seem to indicate how I can do what I want.

Again this is more for how I would like to set up my work flow. Thanks for any input.


import hou
import math

mt_width = hou.parm('/obj/mt/ty')
mt_height = hou.parm('/obj/mt/tz')
mt_rx = hou.parm('/obj/mt/rx')
mt_sz = hou.parm('/obj/mt/sz')



mid_width = hou.parm('/obj/mid/ty')
mid_height = hou.parm('/obj/mid/tz')
mid_rx = hou.parm('/obj/mid/rx')
mid_sz = hou.parm('/obj/mid/sz')

mb_width = hou.parm('/obj/mb/ty')
mb_height = hou.parm('/obj/mb/tz')
mb_rx = hou.parm('/obj/mb/rx')
mb_sz = hou.parm('/obj/mb/sz')


def shape():
mt_rx = 90 * math.fabs(math.sin(math.radians( (hou.frame() + 49) ) ))
mb_rx.set( 90 * math.fabs(math.sin(math.radians( (hou.frame() + 49) ) )))

return mt_rx
User Avatar
Member
2572 posts
Joined: June 2008
Offline
You can create a Python node at the OBJ level and it will run the code every time the frame changes. Use an IF with hou.frame() to link code to run on specific frame #s.
Using Houdini Indie 20.0
Windows 11 64GB Ryzen 16 core.
nVidia 3050RTX 8BG RAM.
User Avatar
Member
2048 posts
Joined: Sept. 2015
Offline
Thank you Enivob,

This works.

But then I got the idea ( for workflow and decrease clutter ) why not just keep my definitions in a separate container from where I run the functions.

In the screenshot( A ) here prefixing the function with “hou.session” does the trick whether those definitions are in another python script node as in the screenshot or in my python source editor.

But I also wanted to try and eliminate the prefix “hou.session” as in the screenshot( B ) using:

hou.expressionGlobals() = try_this

Of course this only works if I “run” the functions, in this case “try_this” in the same python script node.

I've tried doing things like including “import a_different_python_script_node”,

and also trying to reference the whole other python script node with variations of hou.expressionGlobals() like:

#hou.expressionGlobals() = “/obj/Function_Definitions”
#hou.expressionGlobals() = ‘/obj/Function_Definitions’
#hou.expressionGlobals() = /obj/Function_Definitions

..and also in conjunction with different imports like:

import ‘/obj/Function_Definitions’

..with no success

Again this is more for myself learning how I can set up my work flows and I am just exploring possibilities.

I may just be asking for too much and am ok right now with knowing how I can set things up as it is, but….

Additional input is appreciated - Thank you

Attachments:
run_functions_B.jpg (203.1 KB)
run_functions_A.jpg (215.6 KB)

  • Quick Links