Up to date

This page is up to date for Godot 4.0. If you still find outdated information, please open an issue.

AnimationTree

Inherits: Node < Object

A node used for advanced animation transitions in an AnimationPlayer.

Description

A node used for advanced animation transitions in an AnimationPlayer.

Note: When linked with an AnimationPlayer, several properties and methods of the corresponding AnimationPlayer will not function as expected. Playback and transitions should be handled using only the AnimationTree and its constituent AnimationNode(s). The AnimationPlayer node should be used solely for adding, deleting, and editing animations.

Tutorials

Properties

bool

active

false

NodePath

advance_expression_base_node

NodePath(“.”)

NodePath

anim_player

NodePath(“”)

int

audio_max_polyphony

32

AnimationProcessCallback

process_callback

1

NodePath

root_motion_track

NodePath(“”)

AnimationNode

tree_root

Methods

Variant

_post_process_key_value ( Animation animation, int track, Variant value, Object object, int object_idx ) virtual const

void

advance ( float delta )

Vector3

get_root_motion_position ( ) const

Vector3

get_root_motion_position_accumulator ( ) const

Quaternion

get_root_motion_rotation ( ) const

Quaternion

get_root_motion_rotation_accumulator ( ) const

Vector3

get_root_motion_scale ( ) const

Vector3

get_root_motion_scale_accumulator ( ) const


Signals

animation_finished ( StringName anim_name )

Notifies when an animation finished playing.

Note: This signal is not emitted if an animation is looping or aborted. Also be aware of the possibility of unseen playback by sync and xfade.


animation_player_changed ( )

Emitted when the anim_player is changed.


animation_started ( StringName anim_name )

Notifies when an animation starts playing.

Note: This signal is not emitted if an animation is looping or playbacked from the middle. Also be aware of the possibility of unseen playback by sync and xfade.


Enumerations

enum AnimationProcessCallback:

AnimationProcessCallback ANIMATION_PROCESS_PHYSICS = 0

The animations will progress during the physics frame (i.e. Node._physics_process).

AnimationProcessCallback ANIMATION_PROCESS_IDLE = 1

The animations will progress during the idle frame (i.e. Node._process).

AnimationProcessCallback ANIMATION_PROCESS_MANUAL = 2

The animations will only progress manually (see advance).


Property Descriptions

bool active = false

  • void set_active ( bool value )

  • bool is_active ( )

If true, the AnimationTree will be processing.


NodePath advance_expression_base_node = NodePath(".")

  • void set_advance_expression_base_node ( NodePath value )

  • NodePath get_advance_expression_base_node ( )

The path to the Node used to evaluate the AnimationNode Expression if one is not explicitly specified internally.


NodePath anim_player = NodePath("")

  • void set_animation_player ( NodePath value )

  • NodePath get_animation_player ( )

The path to the AnimationPlayer used for animating.


int audio_max_polyphony = 32

  • void set_audio_max_polyphony ( int value )

  • int get_audio_max_polyphony ( )

The number of possible simultaneous sounds for each of the assigned AudioStreamPlayers.

For example, if this value is 32 and the animation has two audio tracks, the two AudioStreamPlayers assigned can play simultaneously up to 32 voices each.


AnimationProcessCallback process_callback = 1

The process mode of this AnimationTree. See AnimationProcessCallback for available modes.


NodePath root_motion_track = NodePath("")

  • void set_root_motion_track ( NodePath value )

  • NodePath get_root_motion_track ( )

The path to the Animation track used for root motion. Paths must be valid scene-tree paths to a node, and must be specified starting from the parent node of the node that will reproduce the animation. To specify a track that controls properties or bones, append its name after the path, separated by ":". For example, "character/skeleton:ankle" or "character/mesh:transform/local".

If the track has type Animation.TYPE_POSITION_3D, Animation.TYPE_ROTATION_3D or Animation.TYPE_SCALE_3D the transformation will be canceled visually, and the animation will appear to stay in place. See also get_root_motion_position, get_root_motion_rotation, get_root_motion_scale and RootMotionView.


AnimationNode tree_root

The root animation node of this AnimationTree. See AnimationNode.


Method Descriptions

Variant _post_process_key_value ( Animation animation, int track, Variant value, Object object, int object_idx ) virtual const

A virtual function for processing after key getting during playback.


void advance ( float delta )

Manually advance the animations by the specified time (in seconds).


Vector3 get_root_motion_position ( ) const

Retrieve the motion delta of position with the root_motion_track as a Vector3 that can be used elsewhere.

If root_motion_track is not a path to a track of type Animation.TYPE_POSITION_3D, returns Vector3(0, 0, 0).

See also root_motion_track and RootMotionView.

The most basic example is applying position to CharacterBody3D:

GDScript

  1. var current_rotation: Quaternion
  2. func _process(delta):
  3. if Input.is_action_just_pressed("animate"):
  4. current_rotation = get_quaternion()
  5. state_machine.travel("Animate")
  6. var velocity: Vector3 = current_rotation * animation_tree.get_root_motion_position() / delta
  7. set_velocity(velocity)
  8. move_and_slide()

By using this in combination with get_root_motion_position_accumulator, you can apply the root motion position more correctly to account for the rotation of the node.

GDScript

  1. func _process(delta):
  2. if Input.is_action_just_pressed("animate"):
  3. state_machine.travel("Animate")
  4. set_quaternion(get_quaternion() * animation_tree.get_root_motion_rotation())
  5. var velocity: Vector3 = (animation_tree.get_root_motion_rotation_accumulator().inverse() * get_quaternion()) * animation_tree.get_root_motion_position() / delta
  6. set_velocity(velocity)
  7. move_and_slide()

Vector3 get_root_motion_position_accumulator ( ) const

Retrieve the blended value of the position tracks with the root_motion_track as a Vector3 that can be used elsewhere.

This is useful in cases where you want to respect the initial key values of the animation.

For example, if an animation with only one key Vector3(0, 0, 0) is played in the previous frame and then an animation with only one key Vector3(1, 0, 1) is played in the next frame, the difference can be calculated as follows:

GDScript

  1. var prev_root_motion_position_accumulator: Vector3
  2. func _process(delta):
  3. if Input.is_action_just_pressed("animate"):
  4. state_machine.travel("Animate")
  5. var current_root_motion_position_accumulator: Vector3 = animation_tree.get_root_motion_position_accumulator()
  6. var difference: Vector3 = current_root_motion_position_accumulator - prev_root_motion_position_accumulator
  7. prev_root_motion_position_accumulator = current_root_motion_position_accumulator
  8. transform.origin += difference

However, if the animation loops, an unintended discrete change may occur, so this is only useful for some simple use cases.


Quaternion get_root_motion_rotation ( ) const

Retrieve the motion delta of rotation with the root_motion_track as a Quaternion that can be used elsewhere.

If root_motion_track is not a path to a track of type Animation.TYPE_ROTATION_3D, returns Quaternion(0, 0, 0, 1).

See also root_motion_track and RootMotionView.

The most basic example is applying rotation to CharacterBody3D:

GDScript

  1. func _process(delta):
  2. if Input.is_action_just_pressed("animate"):
  3. state_machine.travel("Animate")
  4. set_quaternion(get_quaternion() * animation_tree.get_root_motion_rotation())

Quaternion get_root_motion_rotation_accumulator ( ) const

Retrieve the blended value of the rotation tracks with the root_motion_track as a Quaternion that can be used elsewhere.

This is necessary to apply the root motion position correctly, taking rotation into account. See also get_root_motion_position.

Also, this is useful in cases where you want to respect the initial key values of the animation.

For example, if an animation with only one key Quaternion(0, 0, 0, 1) is played in the previous frame and then an animation with only one key Quaternion(0, 0.707, 0, 0.707) is played in the next frame, the difference can be calculated as follows:

GDScript

  1. var prev_root_motion_rotation_accumulator: Quaternion
  2. func _process(delta):
  3. if Input.is_action_just_pressed("animate"):
  4. state_machine.travel("Animate")
  5. var current_root_motion_rotation_accumulator: Quaternion = animation_tree.get_root_motion_Quaternion_accumulator()
  6. var difference: Quaternion = prev_root_motion_rotation_accumulator.inverse() * current_root_motion_rotation_accumulator
  7. prev_root_motion_rotation_accumulator = current_root_motion_rotation_accumulator
  8. transform.basis *= difference

However, if the animation loops, an unintended discrete change may occur, so this is only useful for some simple use cases.


Vector3 get_root_motion_scale ( ) const

Retrieve the motion delta of scale with the root_motion_track as a Vector3 that can be used elsewhere.

If root_motion_track is not a path to a track of type Animation.TYPE_SCALE_3D, returns Vector3(0, 0, 0).

See also root_motion_track and RootMotionView.

The most basic example is applying scale to CharacterBody3D:

GDScript

  1. var current_scale: Vector3 = Vector3(1, 1, 1)
  2. var scale_accum: Vector3 = Vector3(1, 1, 1)
  3. func _process(delta):
  4. if Input.is_action_just_pressed("animate"):
  5. current_scale = get_scale()
  6. scale_accum = Vector3(1, 1, 1)
  7. state_machine.travel("Animate")
  8. scale_accum += animation_tree.get_root_motion_scale()
  9. set_scale(current_scale * scale_accum)

Vector3 get_root_motion_scale_accumulator ( ) const

Retrieve the blended value of the scale tracks with the root_motion_track as a Vector3 that can be used elsewhere.

For example, if an animation with only one key Vector3(1, 1, 1) is played in the previous frame and then an animation with only one key Vector3(2, 2, 2) is played in the next frame, the difference can be calculated as follows:

GDScript

  1. var prev_root_motion_scale_accumulator: Vector3
  2. func _process(delta):
  3. if Input.is_action_just_pressed("animate"):
  4. state_machine.travel("Animate")
  5. var current_root_motion_scale_accumulator: Vector3 = animation_tree.get_root_motion_scale_accumulator()
  6. var difference: Vector3 = current_root_motion_scale_accumulator - prev_root_motion_scale_accumulator
  7. prev_root_motion_scale_accumulator = current_root_motion_scale_accumulator
  8. transform.basis = transform.basis.scaled(difference)

However, if the animation loops, an unintended discrete change may occur, so this is only useful for some simple use cases.