| On this page |
A function is a block of code that performs a specific operation. In APEX Script, you can use the built-in APEX functions (described on this page) or create your own functions.
The library of built-in functions supported by APEX Script can be found in the APEX nodes index, the APEX network view tab menu, or the autocomplete feature when typing in the Snippet parameter of the APEX Script SOP or
APEX AutoRig Component SOP. These built-in functions can be called using either a function-based syntax or an object-oriented syntax.
Function-based vs object-oriented syntax ¶
In the function-based syntax, you call the APEX node as a function, prefixed with its namespace. In the object-oriented syntax, the first argument of a function is the “object” the function operates on, and it’s placed before the function call. The namespace is omitted in the object-oriented syntax.
arr1 = ['aaa']
# Function-based syntax using the APEX and array namespaces
arr2 = apex.array.append(arr1, 'bbb')
# Object-oriented syntax
arr1.append('ccc')Note
The first letter of the function call to an APEX node can be uppercase or lowercase. For example, calling graph.AddNode() is the same as calling graph.addNode().
Function-based syntax ¶
The example below calls the append() function using the APEX and array namespaces. The first argument of append() is an in-place port, so the input array,
x, is updated:
x = ['aaa'] x = apex.array.append(x, 'bbb') # Result: x = ['aaa', 'bbb']
If the result of append() is assigned to a different variable, the original array is not updated:
x = ['aaa'] y = apex.array.append(x, 'bbb') # Result: x = ['aaa'], y = ['aaa', 'bbb']
Functions without a namespace (for example, Sine()) do not need to be prefixed with the APEX namespace:
# With the APEX namespace a = apex.sine(1.0) # Without the APEX namespace b = sine(1.0)
Object-oriented syntax ¶
In the object-oriented syntax, the first argument of a function is the “object” the function operates on, and it’s placed before the function call. This object is then passed to the function as the first argument. If this first argument is an in-place port, it is updated in-place:
x = ['aaa']
x.append('bbb')
# Result: x = ['aaa', 'bbb']If the object-oriented append() is assigned to a variable, the variable is set to the 2nd return value of append():
x = ['aaa']
idx = x.append('bbb')
# Result: x = ['aaa', 'bbb'], idx = 1If the first argument of a function is not an in-place port, the argument is not updated:
x = ['aaa','bbb','ccc'] y = x.get(1) # Result: x = ['aaa', 'bbb', 'ccc'], y = 'bbb'
Function versions ¶
When an APEX Script SOP is first created, the current Houdini version is recorded as part of the APEX Script header text (in the Application Version parameter). When you call a function in APEX Script, the latest version of the function in that Houdini version is used. You can also call an exact version of an APEX function - for example,
IntBitMaskToBool.v2_0() calls the second version of IntBitMaskToBool. See the special function HoudiniVersion() for more information on how to use a specific version of an APEX function.
More function examples ¶
Call a function with arguments:
box = apex.sop.box(t=Vector3(1,1,1), scale=Float(2))
Functions that operate on different types ¶
Functions that operate on different types of data are called templated functions. For example, geo::SetPrimAttribValue<T> is a templated function, where <T> could be one of several different types. Templated functions can be specified in the following ways:
Use the function name suffixed by the type, for example, add “_String” to the function name:
geo.setPrimAttribValue_String(prim, 'name', 'test')
Use the valuetype argument to specify the type, valuetype=<type>:
geo.setPrimAttribValue(prim, 'name', 'test', valuetype=String)
The valuetype argument can be a list or tuple:
b = Convert(a, valuetype=(Float, Int)) c = Convert(b, valuetype=[Int, Bool])
Special functions ¶
APEX Script provides special functions that allow you to perform various graph operations, including the ability to add information to graph nodes, use a specific version of APEX nodes, and invoke graphs from within a graph.
Function |
Description |
||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
BeginMetadata, EndMetadata |
Sets metadata on the block begin and end nodes for loops and if blocks (for example, y = 0
for x in range(3):
# Set metadata on ForBegin node
BeginMetadata(__name='test_begin', __color=(1,0,1), __tags=['tag1', 'tag2'])
y = y + 1
# Set metadata on ForEnd node
EndMetadata(__name='test_end', __pos=(1,1,1), __tags=['out_tag'])
|
||||||||||||||
BindInput, BindOutput |
Defines the inputs and outputs of a graph. For more information, see this example for how to use BindInput() and BindOutput(), and see specifying graph inputs for example uses of BindInput(). |
||||||||||||||
HoudiniVersion |
Sets the version of the APEX functions to use. For example, You could also specify the Houdini version in the header template:
Two other arguments are available for use with the HoudiniVersion() function -
If HoudiniVersion() is not present, this is the same as calling |
||||||||||||||
InputMetadata, OutputMetadata |
Sets metadata on the graph input (parms) and output nodes inside a subnet. The available metadata are listed in the special function arguments. def test(a,b):
# Set metadata on subnet input node
InputMetadata(__name='abc', __pos=(1,0,0), __properties={'test': 1.5})
c: Float = a + b
# Set metadata on subnet output node
OutputMetadata(__color=(1,0,1), __tags=['out_tag1','out_tag2'])
return c
x = test(1,2)
|
||||||||||||||
InvokeOutputs |
See invoking a graph. |
||||||||||||||
SetGraph |
|||||||||||||||
StickyNote |
Creates sticky notes in the graph. The arguments listed below, in addition to the special function arguments, can be used to set certain properties on the sticky note:
Example:
|
||||||||||||||
|
Creates another identifier for a variable without making a copy. This could introduce graph sorting issues, so use with caution. Create an alias of a variable: x = 1.5 y = _portalias(x) # y = 1.5 BindOutput(y) Create an alias of a variable that is assigned to a function: def test():
x = 1
return x
m = test()
m_alias = _portalias(m)
# m_alias = 1, n = 3
n = m_alias + 2
Use an alias in a function: def test(a: Int, b: Int):
c: Int = a + b
d = _portalias(c)
d += 2
return d
# m = 5
m = test(1, 2)
|
Invoking another graph ¶
InvokeOutputs() is a special function that is used to execute another graph. When you have an input that is a graph, InvokeOutputs() is used to execute that graph input.
In this example, InvokeOutputs() executes the function foo:
def foo (a: Int, b: Int) -> tuple[Int, Int]:
return b, a
x: Int
y: Int
x, y = InvokeOutputs(foo, parms={"a": 1, "b": 2}, outputs=["output:a", "output:b"])
The syntax of InvokeOutputs() is:
InvokeOutputs(<function>, parms=<dict>, outputs=[array of strings])
where:
-
parms - Dictionary of parameter values to pass into the function.
-
outputs - An array of strings that specify the values returned by the function. The strings are in the format
<output_node>:<output_port>, where<output_node>is the output node in the function graph (function foo, in our case).
The outputs argument is matched up with the function return values. In the example above:
-
output:ais set to function return valuea. Variablexis then set tooutput:a. -
output:bis set to function return valueb. Variableyis then set tooutput:b.
If the function return statement contains an expression, set the outputs argument to result<x>, where x is the position of the function return value starting at 0:
def foo(a: Int):
return a+1
x: Int = InvokeOutputs(foo, parms={"a": 1}, outputs=["output:result0"])
# Different way of specifying the return value type
y = Int(InvokeOutputs(foo, parms={"a": 2}, outputs=["output:result0"]))If the function return statement contains multiple expressions, the order of the return values is important. For the return values that are not expressions, the corresponding outputs argument must be set to output:<variable>:
# Function return values using expressions (a+b and a*b)
def foo(a: Int, b: Int):
return a+b, a, a*b
x: Int
y: Int
z: Int
# Function returns a+b in output:result0, and
# a*b in output:result2
x, y, z = InvokeOutputs(foo, parms={"a": 1, "b": 2}, outputs=["output:result0", "output:a", "output:result2"])Tip
It is more readable and less error-prone for a function to return only variables and not expressions.
Retrieve only certain values from the function:
def foo(a: Int, b: Int):
return a+b, a, a*b
x: Int
# Only retrieves the a*b return value from function foo
x = InvokeOutputs(foo, parms={"a": 1, "b": 2}, outputs=["output:result2"])Associating APEX node IDs to a graph ¶
If you have multiple graphs, you can use the special function SetGraph() to associate node IDs to a particular graph:
graph1: ApexGraphHandle = BindInput() graph2 = ApexGraphHandle() my_node: ApexNodeID = BindInput() # Associate my_node with graph2 my_node.SetGraph(graph2)
Associate multiple node IDs to different graphs:
graph1: ApexGraphHandle = BindInput()
graph2 = ApexGraphHandle()
node_arr = ApexNodeIDArray()
# Append APEX node IDs to array
for i in range(4):
node_arr.append(Value_ApexNodeID_())
# Associate node ID in node_arr[1] to graph2
x = node_arr[1]
x.SetGraph(graph2)
# Associate node ID in node_arr[2] to graph1
# Different way of calling SetGraph()
y = node_arr[2]
setGraph(y, graph1)Special function arguments ¶
The special function arguments listed below can be used to specify certain information on a node:
Argument |
Type |
Description |
|---|---|---|
|
|
The name of the node. |
|
|
The color of the node. |
|
|
The position of the node in the graph. |
|
|
The properties stored on the node. |
|
|
The tags stored on the node. |
Example:
def abc(a: Vector3, b: Float):
a = a * b
b = b + 1.0
return a, b
# Subnet node name is set to the function name
subnet_a = abc(a=(3.0, 3.0, 3.0), b=5.5)
# Special function arguments used to set the name and color of the subnet node
subnet_b = abc(a=(3.0, 3.0, 3.0), b=5.5, __name='test_b', __color=(1,0,0))