Houdini 22.0 Character KineFX

User-defined functions

Create your own functions in APEX Script.

On this page

In APEX Script, user-defined functions are represented as subnet nodes in the APEX network view. The contents of the subnet node contains the function logic.

APEX Script code:

# Function definition
def test(a: Int, b: Int):
    c: Int = a + b
    return c

# Function call
x = test(1, 2)
Function subnet node (left); subnet contents (right)

Users can define their own functions in APEX Script similar to Python:

Function arguments

Arguments are passed into a function through a comma-separated list of values within a set of parentheses “()”. These arguments follow the function name. The number of arguments in the function call must match the number of arguments in the function definition. It is good practice to annotate the arguments in the function defintion:

# Function definition
def test(a: Int, b: Int):
    c: Int = a + b
    return c

# Function call
x = test(1, 2)

Function with no arguments:

# Function definition
def test():
    a = 1.5
    return a

# Function call; result is x = 1.5
x = test()

You can specify default values for function arguments in the function definition. If the function is called without arguments, the default values are used:

# Function definition
def foo(a: String = 'test', b: Int = Int(Float(1.5))):
    return a, b

# Function call; result is x = 'test', y = 1
x, y = foo()

You can call a function with keyword arguments, which are arguments with the syntax <key>=<value>, where <key> is the name of the argument. When calling a function using all keyword arguments, the order of the arguments does not matter.

Note

If you use both keyword and positional (non-keyword) arguments in a function call, keyword arguments must be placed after positional arguments.

# Function definition
def test(a: Int, b: Int):
    c: Int = a + b
    return c

# Function call with keyword arguments
x = test(b=1, a=2)

Keyword arguments that contain __dot__ are replaced with '.'. This allows APEX Script to handle rigs with geometry names like Base.skel and Base.shp:

# Function takes in Base.skel and returns Base.shp
def test(Base__dot__skel: Geometry):
    Base__dot__shp = Geometry()
    return Base__dot__shp

# Graph input is Base.skel
geo = BindInput(Base__dot__skel=Geometry())
x: Geometry = test(geo)
Rig geometry names in subnet inputs and outputs

Function return values

A function can return multiple values, and you can specify the return types following the “->” after the argument list. Function return types can be specified with parentheses “()” or square brackets “[]”:

def test(a: Vector3, b: Float) -> tuple[Vector3, Float]:
    a = a * b
    b = b + 1.0
    return a, b

x, y = test(a=(3.0, 3.0, 3.0), b=5.5)

In the function return type, you can specify a name for the output port of a subgraph or subnet. Use square brackets to do this. In the example below, the subnet output port b is renamed to outputy:

def test(a: Vector3, b: Float) -> tuple[Vector3, Float[outputy]]:
    a = a * b
    b = b + 1.0
    return a, b

x, y = test(a=(3.0, 3.0, 3.0), b=5.5)

BindOutput(x, y)
Subnet with renamed output port
Subnet contents with renamed output port

If you want to ignore certain return values, assign those values to an underscore (_):

def test(a: Int):
    a1: Int = a + 1
    a2: Int = a + 2
    a3: Int = a + 3
    a4: Int = a + 4
    return a1, a2, a3, a4

_, _, x, _ = test(1)

More function examples

Expressions in function calls:

def foo(a: String, b: Int):
    return a, b

m = 3
n = 4
p = 'test'

# Result: x = 'test3', y = 7
x, y = foo(p+String(m), m+n)

Functions can be nested inside other functions:

def foo(a: Int):
    return a+2

# Call foo within this function
def bar(a: Int) -> Int:
    return foo(a)+4

# Result: x1 = 3, x2 = 7
x1 = foo(1)
x2 = bar(1)

Functions can be daisy-chained:

geo.computeTransform().transform(xform=Matrix4())

Saving functions as subgraphs

Functions can be saved to disk as subgraphs using the APEX Script SOP. These subgraphs can then be used in APEX Script code as global functions. Any subgraphs you have, including ones built by other users, could be used as functions in APEX Script.

To save a function as a subgraph:

  1. In an APEX Script SOP, turn on the Subgraphs parameter.

  2. In the Subgraphs section, define the function in the Subgraph snippet parameter and put the @subgraph decorator above the function definition.

    For example:

    @subgraph
    def test(a: Int, b: Int):
        c: Int = a + b
        return c
    
  3. The subgraph must be saved as a .bgeo file in @/apexgraph, where @ expands to the directories in the HOUDINI_PATH. If you don’t specify the directory, the subgraph will be saved to $HOME/apexgraph. You can set the subgraph location and filename in the Geometry File parameter or the subgraph decorator argument, @subgraph(<file_path>).

  4. Click the Save Subgraphs button.

In the same APEX Script SOP, or in a new APEX Script SOP, you can now use the saved subgraph as a function when writing APEX Script code.

Decorators

A decorator performs an operation on another function and is specified with the @ symbol. For example:

@subgraph
@namespace('my_namespace')
@safeguard_inputs(True)
def test(a: Int, b: Int):
    c: Int = a + b
    return c

The available decorators are:

Subgraph

The @subgraph decorator allows the function that directly follows it to be saved as a subgraph. The subgraph is saved as a packed subgraph - it is stored as a primitive in a packed folder.

The @subgraph decorator takes in an optional argument, <file_path>, and also supports the use of keyword-only arguments:

@subgraph([<file_path>], name=<name>, hidden=[True|False], ...)

Argument

Type

Description

name

String

The name assigned to the subgraph. This is the name primitive attribute on the packed subgraph.

hidden

Bool

If set to True, hides the subgraph from the tab menu in the APEX network view. This value is added to the properties attribute of the graph geometry (unpacked subgraph).

You can also specify custom keyword-only arguments, which are added to the properties attribute of the graph geometry.

Example:

The @subgraph decorator below names the subgraph testnode::3.0. Without the name keyword argument, the subgraph would be named test:

@subgraph('$HOME/houdini21.0/apexgraph/subgraph_test.bgeo', 
    name='testnode::3.0', 
    hidden=False, 
    extrainfo=123)
def test(a: Int, b: Int):
c: Int = a + b
return c

After saving the subgraph, you can use the subgraph in the following ways:

  • Search for the subgraph testnode in the APEX network view:

  • Use the subgraph as a function in an APEX Script snippet:

    s = testnode(1,2)
    

To see the properties of the subgraph, you’ll need to unpack the graph:

  1. Use the File SOP to bring in the subgraph:

    • Set Geometry File to the subgraph .bgeo file. In our example, $HOME/houdini21.0/apexgraph/subgraph_test.bgeo.

    • In the geometry spreadsheet, click Primitives on the top toolbar. The subgraph name testnode::3.0 is listed as the name of the packed subgraph.

  2. The subgraph is in a packed folder, so to see its properties, unpack it using an Unpack Folder SOP:

    • Set Extract Pattern to the name of the subgraph, /testnode::3.0.

    • In the geometry spreadsheet, click Detail on the top toolbar. The following values are listed in the properties attribute of the subgraph:

      {
          "extrainfo": 123,
          "hidden": 0
      }
      

Namespace

You can specify a namespace for a function with the @namespace decorator. After saving a function as a subgraph, you can call the function using <namespace>.<function>. The syntax for the @namespace decorator is:

@namespace('<namespace>')

For example, you can define the following subgraph in the APEX Script SOP, Subgraphs section:

@subgraph
@namespace('my_namespace')
def test(a: Int, b: Int):
    c: Int = a + b
    return c

After saving the function as a subgraph, you can call the subgraph function with:

x = my_namespace.test(1,2)

Safeguard inputs

The @safeguard_inputs decorator helps prevent graph sorting issues by making copies of function input arguments where necessary. The syntax for the @safeguard_inputs decorator is:

@safeguard_inputs(True|False)

When @safeguard_inputs is set to True, APEX Script makes a copy of a function input argument (adds a Value node to the graph) if:

  • The input argument feeds into an in-place port.

    and

  • The name of that input does not match any output names on the function.

If @safeguard_inputs is set to False, APEX Script does not create copies of any function arguments. This could introduce graph sorting issues, so use caution when setting @safeguard_inputs to False.

If @safeguard_inputs is not included before a function, it is set to True by default.

KineFX

Overview

Preparing character elements

Rigging with APEX graphs

  • APEX graph basics

    Introduction to the APEX graph evaluation framework.

  • APEX graph debugger

    Inspect the values of an APEX graph’s ports within the APEX network view.

  • APEX Script basics for graph building

    An overview of APEX Script, a code interface for building APEX graphs.

  • Graphs for numerical operations

    Example graphs of numerical operations like adding two values, fetching multiple results, and retrieving information from geometry.

  • Graphs for viewport animation

    Example graphs of animate state functionality like creating controls and constraints, and defining joint hierarchies.

  • Graphs for deforming geometry

    Example of a SOP network and APEX graph for deforming a 3-joint tube geometry.

  • Graph metadata

    Information added to graph nodes and ports.

  • APEX path pattern

    A set of rules that allow you to filter and select elements within graphs, skeletons, geometry, and the character folder structure.

  • APEX graph nodes

    APEX nodes provide operations for building up the functionality of APEX graphs, which are used in KineFX to create character rigs and perform other geometry manipulation.

  • APEX updates for H22

    New and updated APEX graph nodes for Houdini 22.

Building rig graphs with APEX Script

Rigging with rig components

Animating in the viewport

  • Animation workflow

    Example networks of adding characters, props, characters, and animation to a scene.

  • Animating in LOPs

    Rig and animate characters and props in the context of a USD stage.

  • Working in the animate state

    Select and manipulate controls, define transform limits for controls, and add skin controls to a character.

  • Animate state menus and HUDs

    Menus, HUDs, toolbars, and options in the animate state.

  • Animate state hotkeys

    Hotkeys available in the animate state.

  • Posing

    Pose characters in the animation scene by using locators, copying and pasting controls, and mirroring poses.

  • Motion paths

    Visualize the motion of controls as curves in 3D space.

  • Constraints

    Create relationships between controls in the animate state.

  • Dynamic motion

    Add physically-accurate projectile motion to your animation in the animate state.

  • Ragdoll

    Add a ragdoll simulation to your animation in the animate state.

  • Full body IK

    Apply full body inverse kinematics to pose the character using high-level targets rather than manipulating individual joints.

  • Secondary motion

    Add physically-accurate effects like lag/overshoot, jiggle, and spring to your animation in the animate state.

  • Control selection sets

    A tree view of controls, selections, and selection sets for the animate state. Allows management of control selection and visibility.

  • Motion mixer

    Combine and edit the animation clips of multiple characters in a timeline-based view.

  • Character picker

    Create a 2D layout with simple buttons for selecting character controls in an animation scene.

  • Animation catalog

    Save, manage, and apply animations and single-frame poses to an animation scene.

  • Animation layers

    Create multiple layers of animation without affecting the original animation.

  • Animation toolbar

    A customizable toolbar consisting of simple slider tools that quickly apply modifications to animation controls and curves.

  • Animation bookmarks

    Frame markers that help organize and identify the different parts of your animation.

Customizing the animate state

SOP-based animation

  • Physics-based motion

    Add physically-accurate motion to your animation using the Projectile Motion and Pendulum Motion SOPs.

  • Ragdoll simulations

    Add a ragdoll simulation to your animation using the Ragdoll Solver SOP.

Deformation

  • Blend shapes

    Create blend shapes using blend shape SOP nodes.

Animation retargeting

Pre-H20

Panes

  • Motion mixer

    A timeline-based view of the animation clips and motion effects of multiple characters.

  • Character picker

    Create a 2D layout with simple buttons to select character controls in an animation scene.

  • Rig tree view

    A hierarchical view of character components, including packed character folders, KineFX rigs, and Object node rigs.

  • Animation editor

    View and edit animated parameters and their transitions between keyframes.

  • Geometry spreadsheet

    Displays details about the geometry selected in the network editor.

Appendix