Skip to content

Animation

Inherits: Resource < RefCounted < Object

Holds data that can be used to animate anything in the engine.

This resource holds data that can be used to animate anything in the engine. Animations are divided into tracks and each track must be linked to a node. The state of that node can be changed through time, by adding timed keys (events) to the track.

// This creates an animation that makes the node "Enemy" move to the right by
// 100 pixels in 2.0 seconds.
var animation = new Animation();
int trackIndex = animation.AddTrack(Animation.TrackType.Value);
animation.TrackSetPath(trackIndex, "Enemy:position:x");
animation.TrackInsertKey(trackIndex, 0.0f, 0);
animation.TrackInsertKey(trackIndex, 2.0f, 100);
animation.Length = 2.0f;

Animations are just data containers, and must be added to nodes such as an AnimationPlayer to be played back. Animation tracks have different types, each with its own set of dedicated methods. Check TrackType to see available types.

Note: For 3D position/rotation/scale, using the dedicated TYPE_POSITION_3D, TYPE_ROTATION_3D and TYPE_SCALE_3D track types instead of TYPE_VALUE is recommended for performance reasons.

Type Name Default
bool capture_included false
float length 1.0
LoopMode loop_mode 0
float step 0.033333335
Returns Method
void add_marker(name: StringName, time: float)
int add_track(type: TrackType, at_position: int = -1)
StringName animation_track_get_key_animation(track_idx: int, key_idx: int) const
int animation_track_insert_key(track_idx: int, time: float, animation: StringName)
void animation_track_set_key_animation(track_idx: int, key_idx: int, animation: StringName)
float audio_track_get_key_end_offset(track_idx: int, key_idx: int) const
float audio_track_get_key_start_offset(track_idx: int, key_idx: int) const
Resource audio_track_get_key_stream(track_idx: int, key_idx: int) const
int audio_track_insert_key(track_idx: int, time: float, stream: Resource, start_offset: float = 0, end_offset: float = 0)
bool audio_track_is_use_blend(track_idx: int) const
void audio_track_set_key_end_offset(track_idx: int, key_idx: int, offset: float)
void audio_track_set_key_start_offset(track_idx: int, key_idx: int, offset: float)
void audio_track_set_key_stream(track_idx: int, key_idx: int, stream: Resource)
void audio_track_set_use_blend(track_idx: int, enable: bool)
Vector2 bezier_track_get_key_in_handle(track_idx: int, key_idx: int) const
Vector2 bezier_track_get_key_out_handle(track_idx: int, key_idx: int) const
float bezier_track_get_key_value(track_idx: int, key_idx: int) const
int bezier_track_insert_key(track_idx: int, time: float, value: float, in_handle: Vector2 = Vector2(0, 0), out_handle: Vector2 = Vector2(0, 0))
float bezier_track_interpolate(track_idx: int, time: float) const
void bezier_track_set_key_in_handle(track_idx: int, key_idx: int, in_handle: Vector2, balanced_value_time_ratio: float = 1.0)
void bezier_track_set_key_out_handle(track_idx: int, key_idx: int, out_handle: Vector2, balanced_value_time_ratio: float = 1.0)
void bezier_track_set_key_value(track_idx: int, key_idx: int, value: float)
int blend_shape_track_insert_key(track_idx: int, time: float, amount: float)
float blend_shape_track_interpolate(track_idx: int, time_sec: float, backward: bool = false) const
void clear()
void compress(page_size: int = 8192, fps: int = 120, split_tolerance: float = 4.0)
void copy_track(track_idx: int, to_animation: Animation)
int find_track(path: EntityPath, type: TrackType) const
StringName get_marker_at_time(time: float) const
Color get_marker_color(name: StringName) const
PackedStringArray get_marker_names() const
float get_marker_time(name: StringName) const
StringName get_next_marker(time: float) const
StringName get_prev_marker(time: float) const
int get_track_count() const
bool has_marker(name: StringName) const
StringName method_track_get_name(track_idx: int, key_idx: int) const
Array method_track_get_params(track_idx: int, key_idx: int) const
void optimize(allowed_velocity_err: float = 0.01, allowed_angular_err: float = 0.01, precision: int = 3)
int position_track_insert_key(track_idx: int, time: float, position: Vector3)
Vector3 position_track_interpolate(track_idx: int, time_sec: float, backward: bool = false) const
void remove_marker(name: StringName)
void remove_track(track_idx: int)
int rotation_track_insert_key(track_idx: int, time: float, rotation: Quaternion)
Quaternion rotation_track_interpolate(track_idx: int, time_sec: float, backward: bool = false) const
int scale_track_insert_key(track_idx: int, time: float, scale: Vector3)
Vector3 scale_track_interpolate(track_idx: int, time_sec: float, backward: bool = false) const
void set_marker_color(name: StringName, color: Color)
int track_find_key(track_idx: int, time: float, find_mode: FindMode = 0, limit: bool = false, backward: bool = false) const
bool track_get_interpolation_loop_wrap(track_idx: int) const
InterpolationType track_get_interpolation_type(track_idx: int) const
int track_get_key_count(track_idx: int) const
float track_get_key_time(track_idx: int, key_idx: int) const
float track_get_key_transition(track_idx: int, key_idx: int) const
Variant track_get_key_value(track_idx: int, key_idx: int) const
EntityPath track_get_path(track_idx: int) const
TrackType track_get_type(track_idx: int) const
int track_insert_key(track_idx: int, time: float, key: Variant, transition: float = 1)
bool track_is_compressed(track_idx: int) const
bool track_is_enabled(track_idx: int) const
bool track_is_imported(track_idx: int) const
void track_move_down(track_idx: int)
void track_move_to(track_idx: int, to_idx: int)
void track_move_up(track_idx: int)
void track_remove_key(track_idx: int, key_idx: int)
void track_remove_key_at_time(track_idx: int, time: float)
void track_set_enabled(track_idx: int, enabled: bool)
void track_set_imported(track_idx: int, imported: bool)
void track_set_interpolation_loop_wrap(track_idx: int, interpolation: bool)
void track_set_interpolation_type(track_idx: int, interpolation: InterpolationType)
void track_set_key_time(track_idx: int, key_idx: int, time: float)
void track_set_key_transition(track_idx: int, key_idx: int, transition: float)
void track_set_key_value(track_idx: int, key: int, value: Variant)
void track_set_path(track_idx: int, path: EntityPath)
void track_swap(track_idx: int, with_idx: int)
UpdateMode value_track_get_update_mode(track_idx: int) const
Variant value_track_interpolate(track_idx: int, time_sec: float, backward: bool = false) const
void value_track_set_update_mode(track_idx: int, mode: UpdateMode)

TrackType

enum TrackType

TYPE_VALUE = 0

Value tracks set values in node properties, but only those which can be interpolated. For 3D position/rotation/scale, using the dedicated TYPE_POSITION_3D, TYPE_ROTATION_3D and TYPE_SCALE_3D track types instead of TYPE_VALUE is recommended for performance reasons.

TYPE_POSITION_3D = 1

3D position track (values are stored in Vector3s).

TYPE_ROTATION_3D = 2

3D rotation track (values are stored in Quaternions).

TYPE_SCALE_3D = 3

3D scale track (values are stored in Vector3s).

TYPE_BLEND_SHAPE = 4

Blend shape track.

TYPE_METHOD = 5

Method tracks call functions with given arguments per key.

TYPE_BEZIER = 6

Bezier tracks are used to interpolate a value using custom curves. They can also be used to animate sub-properties of vectors and colors (e.g. alpha value of a Color).

TYPE_AUDIO = 7

Audio tracks are used to play an audio stream with either type of AudioStreamPlayer. The stream can be trimmed and previewed in the animation.

TYPE_ANIMATION = 8

Animation tracks play animations in other AnimationPlayer nodes.

InterpolationType

enum InterpolationType

INTERPOLATION_NEAREST = 0

No interpolation (nearest value).

INTERPOLATION_LINEAR = 1

Linear interpolation.

INTERPOLATION_CUBIC = 2

Cubic interpolation. This looks smoother than linear interpolation, but is more expensive to interpolate. Stick to INTERPOLATION_LINEAR for complex 3D animations imported from external software, even if it requires using a higher animation framerate in return.

Note: If the last and first keys have different values and are close to the ends of the track, a huge overshoot in interpolation may occur if track_set_interpolation_loop_wrap() is set to true.

INTERPOLATION_LINEAR_ANGLE = 3

Linear interpolation with shortest path rotation.

Note: The result value is always normalized and may not match the key value.

INTERPOLATION_CUBIC_ANGLE = 4

Cubic interpolation with shortest path rotation.

Note: The result value is always normalized and may not match the key value.

INTERPOLATION_MAKIMA = 5

Modified Akima interpolation. Like INTERPOLATION_CUBIC, this produces a smooth curve through the keys, but it overshoots less and stays flat where three or more adjacent keys have the same value. It uses two keys on each side of the current segment, so it is slightly more expensive than cubic interpolation.

INTERPOLATION_MAKIMA_ANGLE = 6

Modified Akima interpolation with shortest path rotation.

Note: The result value is always normalized and may not match the key value.

UpdateMode

enum UpdateMode

UPDATE_CONTINUOUS = 0

Update between keyframes and hold the value.

UPDATE_DISCRETE = 1

Update at the keyframes.

UPDATE_CAPTURE = 2

Same as UPDATE_CONTINUOUS but works as a flag to capture the value of the current object and perform interpolation in some methods. See also AnimationMixer.capture(), AnimationPlayer.playback_auto_capture, and AnimationPlayer.play_with_capture().

LoopMode

enum LoopMode

LOOP_NONE = 0

At both ends of the animation, the animation will stop playing.

LOOP_LINEAR = 1

At both ends of the animation, the animation will be repeated without changing the playback direction.

LOOP_PINGPONG = 2

Repeats playback and reverse playback at both ends of the animation.

LoopedFlag

enum LoopedFlag

LOOPED_FLAG_NONE = 0

This flag indicates that the animation proceeds without any looping.

LOOPED_FLAG_END = 1

This flag indicates that the animation has reached the end of the animation and just after loop processed.

LOOPED_FLAG_START = 2

This flag indicates that the animation has reached the start of the animation and just after loop processed.

FindMode

enum FindMode

FIND_MODE_NEAREST = 0

Finds the nearest time key.

FIND_MODE_APPROX = 1

Finds only the key with approximating the time.

FIND_MODE_EXACT = 2

Finds only the key with matching the time.

capture_included

bool capture_included = false

Returns true if the capture track is included. This is a cached readonly value for performance.

length

float length = 1.0

The total length of the animation (in seconds).

Note: Length is not delimited by the last key, as this one may be before or after the end to ensure correct interpolation and looping.

loop_mode

LoopMode loop_mode = 0

Determines the behavior of both ends of the animation timeline during animation playback. This indicates whether and how the animation should be restarted, and is also used to correctly interpolate animation cycles.

step

float step = 0.033333335

The animation step value.

add_marker

void add_marker(name: StringName, time: float)

Adds a marker to this Animation.

add_track

int add_track(type: TrackType, at_position: int = -1)

Adds a track to the Animation.

animation_track_get_key_animation

StringName animation_track_get_key_animation(track_idx: int, key_idx: int) const

Returns the animation name at the key identified by key_idx. The track_idx must be the index of an Animation Track.

animation_track_insert_key

int animation_track_insert_key(track_idx: int, time: float, animation: StringName)

Inserts a key with value animation at the given time (in seconds). The track_idx must be the index of an Animation Track.

animation_track_set_key_animation

void animation_track_set_key_animation(track_idx: int, key_idx: int, animation: StringName)

Sets the key identified by key_idx to value animation. The track_idx must be the index of an Animation Track.

audio_track_get_key_end_offset

float audio_track_get_key_end_offset(track_idx: int, key_idx: int) const

Returns the end offset of the key identified by key_idx. The track_idx must be the index of an Audio Track.

End offset is the number of seconds cut off at the ending of the audio stream.

audio_track_get_key_start_offset

float audio_track_get_key_start_offset(track_idx: int, key_idx: int) const

Returns the start offset of the key identified by key_idx. The track_idx must be the index of an Audio Track.

Start offset is the number of seconds cut off at the beginning of the audio stream.

audio_track_get_key_stream

Resource audio_track_get_key_stream(track_idx: int, key_idx: int) const

Returns the audio stream of the key identified by key_idx. The track_idx must be the index of an Audio Track.

audio_track_insert_key

int audio_track_insert_key(track_idx: int, time: float, stream: Resource, start_offset: float = 0, end_offset: float = 0)

Inserts an Audio Track key at the given time in seconds. The track_idx must be the index of an Audio Track.

stream is the AudioStream resource to play. start_offset is the number of seconds cut off at the beginning of the audio stream, while end_offset is at the ending.

audio_track_is_use_blend

bool audio_track_is_use_blend(track_idx: int) const

Returns true if the track at track_idx will be blended with other animations.

audio_track_set_key_end_offset

void audio_track_set_key_end_offset(track_idx: int, key_idx: int, offset: float)

Sets the end offset of the key identified by key_idx to value offset. The track_idx must be the index of an Audio Track.

audio_track_set_key_start_offset

void audio_track_set_key_start_offset(track_idx: int, key_idx: int, offset: float)

Sets the start offset of the key identified by key_idx to value offset. The track_idx must be the index of an Audio Track.

audio_track_set_key_stream

void audio_track_set_key_stream(track_idx: int, key_idx: int, stream: Resource)

Sets the stream of the key identified by key_idx to value stream. The track_idx must be the index of an Audio Track.

audio_track_set_use_blend

void audio_track_set_use_blend(track_idx: int, enable: bool)

Sets whether the track will be blended with other animations. If true, the audio playback volume changes depending on the blend value.

bezier_track_get_key_in_handle

Vector2 bezier_track_get_key_in_handle(track_idx: int, key_idx: int) const

Returns the in handle of the key identified by key_idx. The track_idx must be the index of a Bezier Track.

bezier_track_get_key_out_handle

Vector2 bezier_track_get_key_out_handle(track_idx: int, key_idx: int) const

Returns the out handle of the key identified by key_idx. The track_idx must be the index of a Bezier Track.

bezier_track_get_key_value

float bezier_track_get_key_value(track_idx: int, key_idx: int) const

Returns the value of the key identified by key_idx. The track_idx must be the index of a Bezier Track.

bezier_track_insert_key

int bezier_track_insert_key(track_idx: int, time: float, value: float, in_handle: Vector2 = Vector2(0, 0), out_handle: Vector2 = Vector2(0, 0))

Inserts a Bezier Track key at the given time in seconds. The track_idx must be the index of a Bezier Track.

in_handle is the left-side weight of the added Bezier curve point, out_handle is the right-side one, while value is the actual value at this point.

bezier_track_interpolate

float bezier_track_interpolate(track_idx: int, time: float) const

Returns the interpolated value at the given time (in seconds). The track_idx must be the index of a Bezier Track.

bezier_track_set_key_in_handle

void bezier_track_set_key_in_handle(track_idx: int, key_idx: int, in_handle: Vector2, balanced_value_time_ratio: float = 1.0)

Sets the in handle of the key identified by key_idx to value in_handle. The track_idx must be the index of a Bezier Track.

bezier_track_set_key_out_handle

void bezier_track_set_key_out_handle(track_idx: int, key_idx: int, out_handle: Vector2, balanced_value_time_ratio: float = 1.0)

Sets the out handle of the key identified by key_idx to value out_handle. The track_idx must be the index of a Bezier Track.

bezier_track_set_key_value

void bezier_track_set_key_value(track_idx: int, key_idx: int, value: float)

Sets the value of the key identified by key_idx to the given value. The track_idx must be the index of a Bezier Track.

blend_shape_track_insert_key

int blend_shape_track_insert_key(track_idx: int, time: float, amount: float)

Inserts a key in a given blend shape track. Returns the key index.

blend_shape_track_interpolate

float blend_shape_track_interpolate(track_idx: int, time_sec: float, backward: bool = false) const

Returns the interpolated blend shape value at the given time (in seconds). The track_idx must be the index of a blend shape track.

Note: If backward is true, the keys are retrieved on the reversed timeline. See also value_track_interpolate().

clear

void clear()

Clear the animation (clear all tracks and reset all).

compress

void compress(page_size: int = 8192, fps: int = 120, split_tolerance: float = 4.0)

Compress the animation and all its tracks in-place. This will make track_is_compressed() return true once called on this Animation. Compressed tracks require less memory to be played, and are designed to be used for complex 3D animations (such as cutscenes) imported from external 3D software. Compression is lossy, but the difference is usually not noticeable in real world conditions.

Note: Compressed tracks have various limitations (such as not being editable from the editor), so only use compressed animations if you actually need them.

copy_track

void copy_track(track_idx: int, to_animation: Animation)

Adds a new track to to_animation that is a copy of the given track from this animation.

find_track

int find_track(path: EntityPath, type: TrackType) const

Returns the index of the specified track. If the track is not found, return -1.

get_marker_at_time

StringName get_marker_at_time(time: float) const

Returns the name of the marker located at the given time.

get_marker_color

Color get_marker_color(name: StringName) const

Returns the given marker’s color.

get_marker_names

PackedStringArray get_marker_names() const

Returns every marker in this Animation, sorted ascending by time.

get_marker_time

float get_marker_time(name: StringName) const

Returns the given marker’s time.

get_next_marker

StringName get_next_marker(time: float) const

Returns the closest marker that comes after the given time. If no such marker exists, an empty string is returned.

get_prev_marker

StringName get_prev_marker(time: float) const

Returns the closest marker that comes before the given time. If no such marker exists, an empty string is returned.

get_track_count

int get_track_count() const

Returns the amount of tracks in the animation.

has_marker

bool has_marker(name: StringName) const

Returns true if this Animation contains a marker with the given name.

method_track_get_name

StringName method_track_get_name(track_idx: int, key_idx: int) const

Returns the method name of a method track.

method_track_get_params

Array method_track_get_params(track_idx: int, key_idx: int) const

Returns the arguments values to be called on a method track for a given key in a given track.

optimize

void optimize(allowed_velocity_err: float = 0.01, allowed_angular_err: float = 0.01, precision: int = 3)

Optimize the animation and all its tracks in-place. This will preserve only as many keys as are necessary to keep the animation within the specified bounds.

position_track_insert_key

int position_track_insert_key(track_idx: int, time: float, position: Vector3)

Inserts a key in a given 3D position track. Returns the key index.

position_track_interpolate

Vector3 position_track_interpolate(track_idx: int, time_sec: float, backward: bool = false) const

Returns the interpolated position value at the given time (in seconds). The track_idx must be the index of a 3D position track.

Note: If backward is true, the keys are retrieved on the reversed timeline. See also value_track_interpolate().

remove_marker

void remove_marker(name: StringName)

Removes the marker with the given name from this Animation.

remove_track

void remove_track(track_idx: int)

Removes a track by specifying the track index.

rotation_track_insert_key

int rotation_track_insert_key(track_idx: int, time: float, rotation: Quaternion)

Inserts a key in a given 3D rotation track. Returns the key index.

rotation_track_interpolate

Quaternion rotation_track_interpolate(track_idx: int, time_sec: float, backward: bool = false) const

Returns the interpolated rotation value at the given time (in seconds). The track_idx must be the index of a 3D rotation track.

Note: If backward is true, the keys are retrieved on the reversed timeline. See also value_track_interpolate().

scale_track_insert_key

int scale_track_insert_key(track_idx: int, time: float, scale: Vector3)

Inserts a key in a given 3D scale track. Returns the key index.

scale_track_interpolate

Vector3 scale_track_interpolate(track_idx: int, time_sec: float, backward: bool = false) const

Returns the interpolated scale value at the given time (in seconds). The track_idx must be the index of a 3D scale track.

Note: If backward is true, the keys are retrieved on the reversed timeline. See also value_track_interpolate().

set_marker_color

void set_marker_color(name: StringName, color: Color)

Sets the given marker’s color.

track_find_key

int track_find_key(track_idx: int, time: float, find_mode: FindMode = 0, limit: bool = false, backward: bool = false) const

Finds the key index by time in a given track. Optionally, only find it if the approx/exact time is given.

If limit is true, it does not return keys outside the animation range.

If backward is true, the direction is reversed in methods that rely on one directional processing.

For example, in case find_mode is FIND_MODE_NEAREST, if there is no key in the current position just after seeked, the first key found is retrieved by searching before the position, but if backward is true, the first key found is retrieved after the position.

track_get_interpolation_loop_wrap

bool track_get_interpolation_loop_wrap(track_idx: int) const

Returns true if the track at track_idx wraps the interpolation loop. New tracks wrap the interpolation loop by default.

track_get_interpolation_type

InterpolationType track_get_interpolation_type(track_idx: int) const

Returns the interpolation type of a given track.

track_get_key_count

int track_get_key_count(track_idx: int) const

Returns the number of keys in a given track.

track_get_key_time

float track_get_key_time(track_idx: int, key_idx: int) const

Returns the time at which the key is located.

track_get_key_transition

float track_get_key_transition(track_idx: int, key_idx: int) const

Returns the transition curve (easing) for a specific key (see the built-in math function @GlobalScope.ease()).

track_get_key_value

Variant track_get_key_value(track_idx: int, key_idx: int) const

Returns the value of a given key in a given track.

track_get_path

EntityPath track_get_path(track_idx: int) const

Gets the path of a track. For more information on the path format, see track_set_path().

track_get_type

TrackType track_get_type(track_idx: int) const

Gets the type of a track.

track_insert_key

int track_insert_key(track_idx: int, time: float, key: Variant, transition: float = 1)

Inserts a generic key in a given track. Returns the key index.

track_is_compressed

bool track_is_compressed(track_idx: int) const

Returns true if the track is compressed, false otherwise. See also compress().

track_is_enabled

bool track_is_enabled(track_idx: int) const

Returns true if the track at index track_idx is enabled.

track_is_imported

bool track_is_imported(track_idx: int) const

Returns true if the given track is imported. Else, return false.

track_move_down

void track_move_down(track_idx: int)

Moves a track down.

track_move_to

void track_move_to(track_idx: int, to_idx: int)

Changes the index position of track track_idx to the one defined in to_idx.

track_move_up

void track_move_up(track_idx: int)

Moves a track up.

track_remove_key

void track_remove_key(track_idx: int, key_idx: int)

Removes a key by index in a given track.

track_remove_key_at_time

void track_remove_key_at_time(track_idx: int, time: float)

Removes a key at time in a given track.

track_set_enabled

void track_set_enabled(track_idx: int, enabled: bool)

Enables/disables the given track. Tracks are enabled by default.

track_set_imported

void track_set_imported(track_idx: int, imported: bool)

Sets the given track as imported or not.

track_set_interpolation_loop_wrap

void track_set_interpolation_loop_wrap(track_idx: int, interpolation: bool)

If true, the track at track_idx wraps the interpolation loop.

Note: If interpolation is true, the animation interpolates between the last and first key of the track. If the last key is placed exactly at the end of the animation, it is ignored as being placed outside the looped animation range.

track_set_interpolation_type

void track_set_interpolation_type(track_idx: int, interpolation: InterpolationType)

Sets the interpolation type of a given track.

track_set_key_time

void track_set_key_time(track_idx: int, key_idx: int, time: float)

Sets the time of an existing key.

track_set_key_transition

void track_set_key_transition(track_idx: int, key_idx: int, transition: float)

Sets the transition curve (easing) for a specific key (see the built-in math function @GlobalScope.ease()).

track_set_key_value

void track_set_key_value(track_idx: int, key: int, value: Variant)

Sets the value of an existing key.

track_set_path

void track_set_path(track_idx: int, path: EntityPath)

Sets the path of a track. Paths must be valid scene-tree paths to a node and must be specified starting from the AnimationMixer.root_node that will reproduce the animation. Tracks that control properties or bones must append their name after the path, separated by ":".

For example, "character/skeleton:ankle" or "character/mesh:transform/local".

track_swap

void track_swap(track_idx: int, with_idx: int)

Swaps the track track_idx’s index position with the track with_idx.

value_track_get_update_mode

UpdateMode value_track_get_update_mode(track_idx: int) const

Returns the update mode of a value track.

value_track_interpolate

Variant value_track_interpolate(track_idx: int, time_sec: float, backward: bool = false) const

Returns the interpolated value at the given time (in seconds). The track_idx must be the index of a value track.

If backward is true, the keys are retrieved on the reversed timeline, so the base key is the key after time_sec. This changes the result only for UPDATE_DISCRETE and INTERPOLATION_NEAREST; the other interpolation types give the same result as forward.

Note: AnimationMixer always passes false for continuous tracks with playback API.

value_track_set_update_mode

void value_track_set_update_mode(track_idx: int, mode: UpdateMode)

Sets the update mode of a value track.