In APEX Script, a function can be treated like a graph template to easily add logic to an existing graph. You can use APEX Script to write expressions and logic that is bundled up in a function, then insert this function directly into an existing graph to modify its functionality.
In APEX rigging, a character rig is represented as a graph. If you have a function that has, for example, a reverse foot or lookat functionality, you could insert this function into an incoming character rig graph to procedurally build up the character’s final rig.
Note
Calling a function simply executes the function logic in the current APEX Script. To use the function logic in another graph (like an incoming rig), the function must be inserted into the incoming graph.
Graph template example ¶
In this example, we start with an input rig graph that contains only an input and output node:
We then add a simple mathematical operation to the input rig graph using an APEX Script SOP:
-
In the APEX Script SOP, use the predefined headers to automatically add the APEX Script commands that take in and modify an input graph:
-
Turn on the Header parameter.
-
In the Header section, set Template to Graph.
-
-
Specify the input to the APEX Script SOP. In the Invocation section, under Input1 Bindings, turn on Bind To Geometry Parameter and set the parameter to
geo. -
To output the updated rig graph, set Bind Output Geometry to
output:geo. -
Add the following code to the Snippet parameter. The code adds function
testto the input graph as a subnet usinggraph::AddSubnet:
# Functionality to add to the input graph def test(a: vector3, b: float) -> tuple(vector3, float): a = a * b b = b + 1.0 return a, b # Add function 'test' to the input graph as a subnet named 'my_test' my_subnet = graph.addSubnet(test, 'my_test') # Connect subnet to the input node of the input graph my_subnet.a_in.promote('aa') my_subnet.b_in.promote('bb') # Connect subnet to the output node of the input graph my_subnet.a_out.promote('aa') my_subnet.b_out.promote('bb') -
To see the updated rig graph in the APEX network view:
-
On the APEX Script SOP, in the Visualizer section, set Show to Output 1.
or
-
Connect the 1st output of the APEX Script SOP to a
Null SOP, and select the Null SOP.
Updated rig graph Dive into the
my_testsubnet to view the graph logic of functiontestfrom the APEX Script code:
Contents of subnet 'my_test' -
Note
In the example above, graph::AddSubnet is used to add the function to the input graph as a subnet. You could also use graph::Merge to merge the function directly into the input graph without packing it into a subnet. However, the resulting graph could contain multiple graph input and output nodes, which you would then have to connect to the appropriate nodes. The cleaner approach is to use graph::AddSubnet.
Adding rig logic without graph templates ¶
Without using a function as a template, the APEX Script code to create the same logic as the previous example is much more convoluted. In this case, the only way to add functionality to an incoming rig graph would be to:
-
Add the individual mathematical nodes.
-
Script each node connection.
-
Pack up the above logic into a subnet.
-
Connect the subnet to the main graph.
Below is the same logic as the previous example without using a function as a template:
# Add nodes to graph
mult = graph.addNode('MultiplyExpr', 'Multiply<Vector3,Float>')
add = graph.addNode('AddExpr', 'Add<Float>')
val = graph.addNode('Value_right', 'Value<Float>', parms={'value':1.0})
# Connect Multiply node to graph input and output
mult.a_in.promote()
mult.b_in.promote('b')
mult.result_out.promote('a')
# Connect Add node to graph input and output
add.a_in.promote('b')
add.result_out.promote('b')
# Connect Value node to Add node
val.value_out.connect(add.b_in)
# Pack nodes (preserving the connections) into a subnet
# Output names of subnet is set to default values
node_arr: ApexNodeIDArray = [mult, add, val]
graph.packSubnet('my_test', node_arr)