Creating a subnet in APEX Script

   900   5   1
User Avatar
Member
24 posts
Joined: Aug. 2017
Online
Hello,

I'm looking for some suggestions on how to put this code in a function inside an APEX Script Component script:

mids = graph.findNodes( "*Mid_L" )
tips = graph.findNodes( "*Tip_L")

for mid in mids:
    midName = mid.name()
    prefix = midName.replace("Mid_L", "")
    
    for tip in tips :
        tipName = tip.name()
        tipPrefix = tipName.replace("Tip_L", "")

        if prefix == tipPrefix :
            mid.r_out.connect( tip.r_in )

Ideally, I would like to define the function like this:

def restrictRotations( graph : ApexGraphHandle, midsGrep : String,
                       tipsGrep : String, midSuffix : String,
                       tipSuffix: String ) :
    mids = graph.findNodes( midsGrep )
    tips = graph.findNodes( tipsGrep )

    for mid in mids:
        midName = mid.name()
        prefix = midName.replace(midSuffix, "")
        
        for tip in tips :
            tipName = tip.name()
            tipPrefix = tipName.replace(tipSuffix, "")

            if prefix == tipPrefix :
                mid.r_out.connect( tip.r_in )
    return graph

But I cannot figure out how to update the graph in the outer scope.

In general, I'm still struggling a bit when it comes to creating imperative logic where I've got values that I would love to easily "connect" to inputs ( I have been updating the nodes via the graph.updateParmNode() function, but this is a bit unwieldy ).

As it is now, I'm simply using two APEX Script nodes as input to autorig nodes, one for the left hand, and one for the right. Clearly, not ideal. Works like a charm, but I'm hating the duplication of code.

Thanks for the help!
Edited by cwrenniks - May 12, 2025 23:14:22
User Avatar
Staff
114 posts
Joined: May 2021
Offline
you just need to call your function as you would in python:


def MyFunc(graph: ApexGraphHandle, my_name: String= 'test'):
    graph.addNode(callback='TransformObject', name=my_name)
    return graph, node

# lets assume this here is your outer graph
graph = ApexGraphHandle()

# we now call the function. We can use the function even in the object oriented style
my_new_node = graph.myFunc('testname')
User Avatar
Member
24 posts
Joined: Aug. 2017
Online
Oh interesting. So let's say one of the parameters was a Matrix4. How would I pass the Matrix4 for something like the xform_out of a transform object to that function? Is it as easy as:

def MyFunc(graph: ApexGraphHandle, my_name: String= 'test', xform : Matrix4):
    graph.addNode(callback='TransformObject', name=my_name)
    return graph, node

# lets assume this here is your outer graph
graph = ApexGraphHandle()

transObj = graph.addNode("hello", "TransformObject")

# we now call the function. We can use the function even in the object oriented style
my_new_node = graph.myFunc('testname', transObj.xform )
Edited by cwrenniks - May 13, 2025 17:44:40
User Avatar
Staff
114 posts
Joined: May 2021
Offline
no the transform object is a node and the transfobj.xform is a port not the xform value this is an essential difference.

https://www.sidefx.com/docs/houdini/character/kinefx/apexscriptfunctions.html [www.sidefx.com]

that page here should have a lot of good examples to see how to work with apex script and functions
User Avatar
Member
24 posts
Joined: Aug. 2017
Online
Awesome, really appreciate the help. I've read through those docs you pointed out numerous times. Would be great to add the fact if you create a user defined function, make sure it starts with a capital letter, and make sure the first parameter is of type ApexGraphHandle, you can use it as you've instructed. That was the missing ingredient! I've now combined the two scripts into one! Thanks again!

def RestrictRotations( graph : ApexGraphHandle, midGlob : String, tipGlob : String,
                        midSuffix : String, tipSuffix : String ) :
    mids = graph.findNodes( midGlob )
    tips = graph.findNodes( tipGlob )

    for mid in mids:
        midName = mid.name()
        prefix = midName.replace(midSuffix, "")
        
        for tip in tips :
            tipName = tip.name()
            tipPrefix = tipName.replace(tipSuffix, "")

            if prefix == tipPrefix :
                mid.r_out.connect( tip.r_in )
    return graph


graph.restrictRotations( "*Mid_L", "*Tip_L", "Mid_L", "Tip_L")
graph.restrictRotations( "*Mid_R", "*Tip_R", "Mid_R", "Tip_R")
User Avatar
Staff
114 posts
Joined: May 2021
Offline
ah yeah that is more in the realm of a bug. This will be fixed with h21
  • Quick Links