| On this page |
APEX Script allows you to define the inputs and outputs of a graph, access graph elements like nodes and ports, and perform graph operations. See APEX Script for graph building for an introduction to APEX Script.
Specifying graph inputs and outputs ¶
BindInput() and BindOutput() are special functions that define the inputs and outputs of a graph. See this example for how to use BindInput() and BindOutput().
The examples below show how to specify different types of graph inputs:
# Initialize float value
f = BindInput(3.1)
# Uninitialized string type
s: String = BindInput()
# Initialize to empty array
a = BindInput(DictArray())
# Default dictionary key-value pair: {'<key>': <value>}
d = BindInput({'abc': 1.5})
# Create multiple graph inputs
x, y, z = BindInput(2.6, 'test', Dict())
# Type conversion when initializing
i = BindInput(Int(Float(1.5)))Note
If you don’t specify a type for the graph input, a warning will be raised. Only do the following if you know exactly how you will be using the variable:
a = BindInput()
The input port a that is defined in this way is not displayed in the APEX network view because its type is ambiguous.
You can rename the input and output ports on a graph using keyword arguments (arguments with the syntax <key>=<value>):
i: Int = BindInput(int_test=3) x,y = BindInput(2, my_str='hello') BindOutput(y, int_test=i)
Note
Keyword arguments must be placed after positional (non-keyword) arguments.
Keyword arguments that contain __dot__ are replaced with '.'. This allows APEX Script to handle rigs with geometry names like Base.skel and Base.shp:
x = BindInput(Base__dot__shp=Geometry()) Base__dot__skel: Geometry = BindInput() BindOutput(Base__dot__skel, Base__dot__shp=x)
Specifying dictionary inputs ¶
In this example, we use values from two dictionaries as inputs to an APEX Script SOP:
Attribute Adjust Dictionary SOP
The Attribute Adjust Dictionary SOP defines a dictionary of input values. We set the following values for the two SOPs:
input_dictionary1
-
Attribute Name =
parms1. By default, Attribute Class is set to Detail, so we have a detail attribute calledparms1. -
In the Set Values section, we set two dictionary entries:
-
Key =
int1, Type = Integer, Value =2 -
Key =
abc, Type = String, Value =hi
-
input_dictionary2
-
Attribute Name =
parms2 -
In the Set Values section:
-
Key =
int2, Type = Integer, Value =4 -
Key =
abc, Type = String, Value =hi again
-
To view the new parms1 and parms2 dictionaries in the geometry spreadsheet:
-
Select one of the Attribute Adjust Dictionary SOPs.
-
At the top of a pane, click the
New Tab icon and select New Pane Tab Type ▸ Inspectors ▸ Geometry Spreadsheet.
-
Select
Detail on the top toolbar:
parms1: {"abc": "hi", "int1": 2 } parms2: {"abc": "hi again", "int2": 4 }
-
We have the following code in the Snippet parameter:
int1: Int = BindInput() int2: Int = BindInput() abc: String = BindInput() BindOutput(int1,int2,abc)
Note
If you don’t bind the output or do anything with the input values, the graph will not displayed any logic because nothing is being done with the inputs.
-
Specify the input dictionaries. In the Invocation section:
-
Under Input1 Bindings, turn on Dictionary To Bind and specify the dictionary connected to the 1st input of the APEX Script SOP, in our case,
parms1(from the Attribute Adjust Dictionary SOP’s Attribute Name parameter). -
Under Input2 Bindings, turn on Dictionary To Bind and specify the dictionary connected to the 2nd input of the APEX Script SOP, in our case,
parms2.
The APEX Script SOP looks in the input dictionaries,
parms1andparms2, and tries to find entries with keysint1,int2, andabc. For keys that exist in multiple dictionaries, the latter dictionary entry is used. In our case, both dictionaries have an entry with keyabc, so theabcentry from the 2nd dictionary is used. In addition, a warning is raised on the APEX Script SOP. -
-
By default, the first entry of Output Dictionary Bindings is populated with the following:
-
Apex Outputs Group is the set to
output. This is the name of the output node in the graph. -
Output Attrib is set to
parms. We can set this to any name for the output dictionary.
To view the graph outputs in the geometry spreadsheet:
-
In the geometry spreadsheet, beside the node name, select Invoked from the drop-down menu.
-
Click
Detail on the top toolbar. The graph outputs are stored in the dictionary specified in Output Attrib, in our case,
parms:parms: {"abc": "hi again", "int1": 2, "int2": 4 }
-
Binding multiple outputs ¶
We can set the graph to have multiple input and output nodes by using BindInput and BindOutput functions with the special function argument __name to name nodes:
-
In an APEX Script SOP, we have the following code in the Snippet parameter:
# Default 'parms' name used f = BindInput(3.1) # 2nd input node is named 'input2' s: String = BindInput('abc', __name='input2') # Default 'output' name used BindOutput(f) # 2nd output node is named 'output2' BindOutput(s, __name='output2')
Graph with multiple input and output nodes -
By default, the first entry of Output Dictionary Bindings is filled out with the following:
-
Apex Outputs Group is the set to
output. This is the name of the output node in the graph. -
Output Attrib is set to
parms. We can set this to any name for the output dictionary.
To view the graph outputs in the geometry spreadsheet:
-
In the geometry spreadsheet, beside the node name, select Invoked from the drop-down menu.
-
Click
Detail on the top toolbar. Only the
parmsdictionary is displayed because we have only told the APEX Script SOP to output the values of theoutputnode.
-
-
To also output the
output2node values:-
Click
beside Output Dictionary Bindings.
-
For the new entry, set Apex Outputs Group to
output2and Output Attrib to, say,parms2.
On the geometry spreadsheet, there are now two output dictionaries displayed,
parmsandparms2:parms: {"f": 3.1 } parms2: {"s": "abc" } -
Graph nodes and ports ¶
APEX Script provides functions that allow you to perform various graph operations and access graph elements like nodes and ports. These functions are primarily used in rigging for building up the rig logic of a character. See the graph::* nodes for the graph operations that are available in APEX Script.
The syntax to find a graph node with a specific node name:
graph.<node_name>
The syntax to find a specific input or output port on a node:
graph.<node_name>.<port_name>_[in|out]
Connect two ports:
# Create graph
graph = ApexGraphHandle()
# Add two TransformObject nodes to graph
a = graph.addNode('test_a', 'TransformObject')
b = graph.addNode('test_b', 'TransformObject')
# Set port variables
p_a = graph.test_a.xform_out
p_b = graph.test_b.xform_in
# Connect ports
graph.addWire(p_a, p_b)Find all nodes based on a pattern:
# Create graph
graph = ApexGraphHandle()
# Add two TransformObject nodes to graph
a = graph.addNode('test_a', 'TransformObject')
b = graph.addNode('test_b', 'TransformObject')
# Find all nodes that begin with 'test'
n = graph.findNodes('test*')Find a port on a node:
# Create graph
graph = ApexGraphHandle()
# Add TransformObject node to graph
a = graph.addNode('test_a', 'TransformObject')
# Find input port 'xform' on node
p = graph.findPort(a, 'xform[in]')Get data on nodes and ports (see function mappings for how to use the data() function):
# Create graph
graph = ApexGraphHandle()
# Add TransformObject node to graph
a = graph.addNode('test_a', 'TransformObject')
# Get node data
(a_name, a_callback, a_pos, a_color, a_parms) = a.data()
# Output node data
BindOutput(a_name, a_callback, a_pos, a_color, a_parms)
# 'xform' output port of TransformObject node
p = a.xform_out
# Get port data
(p_name, p_alias, p_datatype, p_porttype, p_isconnected, p_ispromoted) = p.data()
# Output port data
BindOutput(p_name, p_alias, p_datatype, p_porttype, p_isconnected, p_ispromoted)See the list of ports returned from a function:
# x is an array of APEX port IDs
x = graph.matchPorts('*xform*')
# Initialize an array of strings
arr = StringArray()
# Add the port names to the array
for p in x:
p_name = p.data()
arr.append(p_name)
# Output the array of port names
BindOutput(arr)Variadic ports ¶
Variadic ports are ports that can take in multiple connections. As you make connections to a variadic port, subports are created on the node.
Variadic inputs ¶
In APEX Script, variadic inputs are stored in a dictionary with the following key-value pair:
-
key - subport name
-
value - subport value
The example below adds three values. The first argument is a regular input, and the second argument is a variadic input stored as a dictionary with two entries. The subport names of the variadic input are explicitly set:
x = 3
sum = Add_Int(1, {'bbb': 2, 'ccc': x})
If the variadic input is the last port on the node, there is no need to specify the subport names:
x = 3 sum = Add_Int(1, 2, x)
If no values are specified for the non-variadic ports, the variadic port name must be specified:
<function>(<variadic_port>={<subport1>:<value1>, <subport2>:<value2>, ...})
sum = Add_Int(b={'x': 4, 'y': 5})Variadic outputs ¶
To name the subports of a variadic output, use the keyword argument <variadic_arg_name>_out=[<subport_names>], where <subport_names> is an array of strings.
The example below gets the variadic outputs from IntBitMaskToBool:
input: Int = BindInput(8) # Variadic output subports named 'aa', 'bb', 'cc', 'dd' a,b,c,d = input.IntBitMaskToBool.v2_0(mask__out=['aa', 'bb', 'cc', 'dd']) # Output subport values BindOutput(input,a,b,c,d)
If the keyword argument is not provided, the subport names are set to the variable names:
input: Int = BindInput(8) # Variadic output subports named 'a', 'b', 'c', 'd' a,b,c,d = input.IntBitMaskToBool.v2_0() # Output subport values BindOutput(input,a,b,c,d)
Dictionary entries can be of any type, so to extract the entries of a dictionary using dict::Extract, you must first specify the types of the variables you want to extract the dictionary entries to:
# Specify dictionary entry types
a: Int
b: Float
c: Vector3
dict_test = {
'aa': 5,
'bb': 0.2,
'cc': (5,10,15)
}
a,b,c = dict_test.extract(args__out=['aa', 'bb', 'cc'])
BindOutput(a,b,c)
Converting graphs to APEX Script code ¶
The APEX Script and
APEX AutoRig Component SOPs provide the option to convert graphs to APEX Script code.
For the APEX Script SOP:
-
Connect an existing graph into the 2nd input of the APEX Script SOP.
-
In the Decompile section, set the drop-down menu to Second Input.
-
Click the Convert to Snippet button.
The APEX Script code will appear in the Snippet parameter.
See the special functions that may be added to the decompiled code.
How-to ¶
| To... | Do this |
|---|---|
|
See the nodes or ports returned from a function |
View a single port ID: # x is an APEX port ID name = x.data() BindOutput(name) View a list of port IDs: # x is an array of APEX port IDs
x = graph.matchPorts('*xform*')
# Initialize an array of strings
arr = StringArray()
# Add the port names to the array
for p in x:
p_name = p.data()
arr.append(p_name)
# Output the array of port names
BindOutput(arr)
To view the port names:
|