Creating a subnet in APEX Script

   1137   5   1
User Avatar
Member
24 posts
Joined: 8月 2017
オフライン
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 - 2025年5月12日 23:14:22
User Avatar
スタッフ
121 posts
Joined: 5月 2021
オフライン
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: 8月 2017
オフライン
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 - 2025年5月13日 17:44:40
User Avatar
スタッフ
121 posts
Joined: 5月 2021
オフライン
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: 8月 2017
オフライン
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
スタッフ
121 posts
Joined: 5月 2021
オフライン
ah yeah that is more in the realm of a bug. This will be fixed with h21
  • Quick Links