Skip to content

Animation Graph

Updated

The Animation Graph component (comp-animation-graph) is a state machine over the animation layers of the entity it is on. A state machine is a set of named situations, called states, of which exactly one is current at any time, plus rules, called transitions, that say how to get from one state to another. For a character, the states might be Idle, Walking and Waving; for a door, Closed and Open.

Each state runs actions on layers as it is entered and as it is left: play Walk, stop Idle. Transitions move between states when a bus signal arrives, and only then. Use a graph when animations change in response to something: a door that opens on a button, a character that waves when greeted, a panel that reveals and hides. The graph replaces the bookkeeping code you would otherwise write in a script; a script then only needs to send signals.

  • A graph plays layers of its own entity: the layers of an Animation Timeline and the clips of an Animation Clip. Read those pages first.
  • Transitions listen for signals on the bus. Signals come from Signal Emitters, from a script’s emit, and from timeline signal keys. See Scripting.
  • Scripts reach the component with getComponent(AnimationGraphComponent).
Prop Type Default Description
entry string '' The state it starts in; empty, the first state.
signals array of { name } [] The signals the graph tool offers for transitions. No effect at runtime.
states array of states [] Its states.
transitions array of transitions [] Its transitions.
Field Type Default Description
name string required Unique within the graph.
enter array of actions [] Run as the state is entered.
exit array of actions [] Run as a transition leaves the state.
x, y, locked, color Layout and appearance in the graph tool. No effect at runtime.
Field Type Applies to Description
type 'play' | 'stop' | 'pause' | 'resume' | 'seek' all What it does to the layer.
layer string all The layer’s name. The graph looks in the entity’s Animation Timeline first, then its Animation Clip.
time number seek Seconds from 0.
fade number play, stop Seconds to blend in or out. Absent, the layer’s default (0.2 s for a clip, none for a timeline layer).
repeat 'once' | 'repeat' | 'pingpong' play How this play repeats. Absent, once, or the Animation Clip’s repeat for a clip.
repeatCount number play How many times a repeating play plays in all, a whole number from 1. Absent, forever.
Field Type Default Description
from, to string required The states it leaves and enters. At most one transition each way between two states.
signals array of { name } [] The bus signals that take it: the only thing that does.
delay number 0 Seconds from its signal to taking it. During the delay the state carries on; another transition’s signal takes that one instead.

Bad data (two states with one name, a transition to a state that does not exist, an unknown action type) is refused when the props are set, with a TypeError. A signal is always an object { name }; a plain string is refused.

  • The graph leads. While an enabled graph is on the entity, in play, only its states play the entity’s layers. The clip’s and the timeline’s autoplay play nothing.
  • Entering a state. As its entity starts, the graph enters entry and runs its enter actions. Taking a transition runs the current state’s exit actions, then the next state’s enter actions, and sends AnimationGraph::state-change.
  • Signals. A signal takes the transition out of the current state that lists it. With no such transition, it is ignored.
  • One graph or all. A bus signal (from emit) reaches every graph listening for it. graph.emit(signal) reaches this graph only.
  • Crossfades. An exit stop and an enter play with the same fade blend the two layers over one window.
  • Moving on when a layer ends. Transitions take signals only. For a timeline layer, put a signal key at its end. For a clip, listen for AnimationClip::finished in a script and call graph.emit.
  • Late layers. A model’s clips exist only once it loads. When they arrive, the current state’s enter actions run again on them.
  • Restarts. Switching the entity off leaves the state; switching it back on starts again from entry.
Member What it does
graph.emit(signal) Sends signal to this graph only, as if heard on the bus.
graph.state The name of the current state; undefined until the graph runs.

The global emit(signal) reaches every graph listening for that signal.

Event EventType Data When
AnimationGraph::state-change EventType.AnimationGraph.StateChange { state, previous } The graph enters a state. previous is '' for the first state.

It goes through the Signal Emitters on the graph’s entity.

The examples share one character: a model with clips Idle, Walk and Wave, an Animation Clip, a Signal Emitter with signal: 'mike', and an Animation Graph:

{
"entry": "Idle",
"states": [
{ "name": "Idle", "enter": [{ "type": "play", "layer": "Idle", "fade": 0.3 }] },
{ "name": "Walking", "enter": [{ "type": "play", "layer": "Walk", "fade": 0.3, "repeat": "repeat" }] },
{ "name": "Waving", "enter": [{ "type": "play", "layer": "Wave", "fade": 0.2, "repeat": "once" }] }
],
"transitions": [
{ "from": "Idle", "to": "Walking", "signals": [{ "name": "mike/walk" }] },
{ "from": "Walking", "to": "Idle", "signals": [{ "name": "mike/stop" }] },
{ "from": "Idle", "to": "Waving", "signals": [{ "name": "greet" }] },
{ "from": "Waving", "to": "Idle", "signals": [{ "name": "mike/done" }] }
]
}

A script can add a graph like any component. This one gives a door with an open timeline layer a two-state graph, then opens and closes it on a shared signal.

DoorGraph.jx
#pragma lifecycle(startup)
function startup() {
const graph = new AnimationGraphComponent({
entry: 'Closed',
states: [
{ name: 'Closed', enter: [{ type: 'stop', layer: 'open', fade: 0.8 }] },
{ name: 'Open', enter: [{ type: 'play', layer: 'open' }] },
],
transitions: [
{ from: 'Closed', to: 'Open', signals: [{ name: 'door/toggle' }] },
{ from: 'Open', to: 'Closed', signals: [{ name: 'door/toggle' }], delay: 0.2 },
],
});
self.parent.addComponent(graph);
on('door/toggle', () => log('Door toggled'));
}
  • Both transitions listen for door/toggle; only the one leaving the current state is taken, so one signal toggles the door.
  • Entering Closed stops the open layer with a 0.8 second fade, which blends the door back to its pose from before the layer played.
  • The return transition waits 0.2 seconds after its signal.
  • Anything can now open every door with this graph: a button’s Signal Emitter with signal: 'door/toggle', or emit('door/toggle') from another script.

The script only translates input into signals. The graph decides what plays.

WalkSignals.jx
#pragma lifecycle(update)
const graph = self.getComponent(AnimationGraphComponent);
let walking = false;
function update() {
const held = Input.keyboard.isKeyPressed(Key.W);
if (held !== walking) {
walking = held;
graph.emit(walking ? 'mike/walk' : 'mike/stop');
}
if (Input.keyboard.isKeyReleased(Key.G)) graph.emit('greet');
}
  • graph.emit sends the signal to this character’s graph only. With the global emit, every graph listening for mike/walk would react.
  • The script sends a signal only when the key state changes, not every frame.
  • A signal with no transition out of the current state is ignored: pressing G while walking does nothing, because only Idle has a transition on greet.

Transitions take signals only, so a script turns the clip’s finished event into one. It also logs every state change.

WaveDone.jx
#pragma lifecycle(startup)
const graph = self.getComponent(AnimationGraphComponent);
function startup() {
on('mike', (event) => {
if (event.eventType === EventType.AnimationClip.Finished && event.data.clip === 'Wave') {
graph.emit('mike/done');
}
if (event.eventType === EventType.AnimationGraph.StateChange) {
log(event.data.previous || '(start)', '->', event.data.state);
}
});
}
  • The Waving state plays Wave with repeat: 'once', so the clip sends AnimationClip::finished at its end.
  • The script checks event.data.clip, because other one-shot clips would send the same event.
  • state-change carries previous and state; previous is an empty string for the first state, so || shows a readable label.

Any source can send a graph’s signals. This script, on a separate trigger entity, greets the character the first time the camera comes within three metres, and only when the character is idle.

GreetOnApproach.jx
#pragma lifecycle(startup, update)
const RADIUS = 3;
let graph;
let mike;
let greeted = false;
const eye = new THREE.Vector3();
const spot = new THREE.Vector3();
function startup() {
mike = findEntity('Mike');
graph = mike?.getComponent(AnimationGraphComponent);
}
function update() {
if (!graph || !camera) return;
camera.getWorldPosition(eye);
mike.getWorldPosition(spot);
const near = eye.distanceTo(spot) < RADIUS;
if (near && !greeted && graph.state === 'Idle') {
greeted = true;
graph.emit('greet');
}
if (!near) greeted = false;
}
  • findEntity runs once in startup, never in update, because looking up by name walks the scene.
  • camera is the camera the player sees through; it is undefined until rendering starts, so the script checks it. Both positions are read in world space with getWorldPosition, into vectors made once so nothing is allocated each frame.
  • graph.state tells whether the character is idle, so the greeting never interrupts a walk.
  • greeted resets once the player walks away, so the character greets again on the next approach.
  • Expecting autoplay with a graph. An enabled graph takes over in play: put the first animation in the entry state’s enter actions.
  • Waiting for a layer to end on its own. Transitions take signals only. Add a signal key at the end of a timeline layer, or turn AnimationClip::finished into a signal from a script.
  • Using emit for one character. The global emit reaches every graph listening for the signal. Use graph.emit to address one.
  • Signals written as strings. In props, a transition’s signals are objects: signals: [{ name: 'greet' }].
  • Layer names that do not exist. An action names a layer of the same entity. Check clip names with the AnimationClip.Ready event’s clips.
  • Testing in the editor. The editor never runs a graph.