Animation

Inherits: Resource < RefCounted < Object

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

Description

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.

GDScriptC#

  1. # This creates an animation that makes the node "Enemy" move to the right by
  2. # 100 pixels in 0.5 seconds.
  3. var animation = Animation.new()
  4. var track_index = animation.add_track(Animation.TYPE_VALUE)
  5. animation.track_set_path(track_index, "Enemy:position:x")
  6. animation.track_insert_key(track_index, 0.0, 0)
  7. animation.track_insert_key(track_index, 0.5, 100)
  1. // This creates an animation that makes the node "Enemy" move to the right by
  2. // 100 pixels in 0.5 seconds.
  3. var animation = new Animation();
  4. int trackIndex = animation.AddTrack(Animation.TrackType.Value);
  5. animation.TrackSetPath(trackIndex, "Enemy:position:x");
  6. animation.TrackInsertKey(trackIndex, 0.0f, 0);
  7. animation.TrackInsertKey(trackIndex, 0.5f, 100);

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.

Tutorials

Properties

float

length

1.0

LoopMode

loop_mode

0

float

step

0.1

Methods

int

add_track ( TrackType type, int at_position=-1 )

StringName

animation_track_get_key_animation ( int track_idx, int key_idx ) const

int

animation_track_insert_key ( int track_idx, float time, StringName animation )

void

animation_track_set_key_animation ( int track_idx, int key_idx, StringName animation )

float

audio_track_get_key_end_offset ( int track_idx, int key_idx ) const

float

audio_track_get_key_start_offset ( int track_idx, int key_idx ) const

Resource

audio_track_get_key_stream ( int track_idx, int key_idx ) const

int

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

bool

audio_track_is_use_blend ( int track_idx ) const

void

audio_track_set_key_end_offset ( int track_idx, int key_idx, float offset )

void

audio_track_set_key_start_offset ( int track_idx, int key_idx, float offset )

void

audio_track_set_key_stream ( int track_idx, int key_idx, Resource stream )

void

audio_track_set_use_blend ( int track_idx, bool enable )

Vector2

bezier_track_get_key_in_handle ( int track_idx, int key_idx ) const

Vector2

bezier_track_get_key_out_handle ( int track_idx, int key_idx ) const

float

bezier_track_get_key_value ( int track_idx, int key_idx ) const

int

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

float

bezier_track_interpolate ( int track_idx, float time ) const

void

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

void

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

void

bezier_track_set_key_value ( int track_idx, int key_idx, float value )

int

blend_shape_track_insert_key ( int track_idx, float time, float amount )

float

blend_shape_track_interpolate ( int track_idx, float time_sec ) const

void

clear ( )

void

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

void

copy_track ( int track_idx, Animation to_animation )

int

find_track ( NodePath path, TrackType type ) const

int

get_track_count ( ) const

StringName

method_track_get_name ( int track_idx, int key_idx ) const

Array

method_track_get_params ( int track_idx, int key_idx ) const

int

position_track_insert_key ( int track_idx, float time, Vector3 position )

Vector3

position_track_interpolate ( int track_idx, float time_sec ) const

void

remove_track ( int track_idx )

int

rotation_track_insert_key ( int track_idx, float time, Quaternion rotation )

Quaternion

rotation_track_interpolate ( int track_idx, float time_sec ) const

int

scale_track_insert_key ( int track_idx, float time, Vector3 scale )

Vector3

scale_track_interpolate ( int track_idx, float time_sec ) const

int

track_find_key ( int track_idx, float time, FindMode find_mode=0 ) const

bool

track_get_interpolation_loop_wrap ( int track_idx ) const

InterpolationType

track_get_interpolation_type ( int track_idx ) const

int

track_get_key_count ( int track_idx ) const

float

track_get_key_time ( int track_idx, int key_idx ) const

float

track_get_key_transition ( int track_idx, int key_idx ) const

Variant

track_get_key_value ( int track_idx, int key_idx ) const

NodePath

track_get_path ( int track_idx ) const

TrackType

track_get_type ( int track_idx ) const

int

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

bool

track_is_compressed ( int track_idx ) const

bool

track_is_enabled ( int track_idx ) const

bool

track_is_imported ( int track_idx ) const

void

track_move_down ( int track_idx )

void

track_move_to ( int track_idx, int to_idx )

void

track_move_up ( int track_idx )

void

track_remove_key ( int track_idx, int key_idx )

void

track_remove_key_at_time ( int track_idx, float time )

void

track_set_enabled ( int track_idx, bool enabled )

void

track_set_imported ( int track_idx, bool imported )

void

track_set_interpolation_loop_wrap ( int track_idx, bool interpolation )

void

track_set_interpolation_type ( int track_idx, InterpolationType interpolation )

void

track_set_key_time ( int track_idx, int key_idx, float time )

void

track_set_key_transition ( int track_idx, int key_idx, float transition )

void

track_set_key_value ( int track_idx, int key, Variant value )

void

track_set_path ( int track_idx, NodePath path )

void

track_swap ( int track_idx, int with_idx )

UpdateMode

value_track_get_update_mode ( int track_idx ) const

Variant

value_track_interpolate ( int track_idx, float time_sec ) const

void

value_track_set_update_mode ( int track_idx, UpdateMode mode )


Enumerations

enum TrackType:

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.

TrackType TYPE_POSITION_3D = 1

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

TrackType TYPE_ROTATION_3D = 2

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

TrackType TYPE_SCALE_3D = 3

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

TrackType TYPE_BLEND_SHAPE = 4

Blend shape track.

TrackType TYPE_METHOD = 5

Method tracks call functions with given arguments per key.

TrackType 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).

TrackType 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.

TrackType TYPE_ANIMATION = 8

Animation tracks play animations in other AnimationPlayer nodes.


enum InterpolationType:

InterpolationType INTERPOLATION_NEAREST = 0

No interpolation (nearest value).

InterpolationType INTERPOLATION_LINEAR = 1

Linear interpolation.

InterpolationType 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.

InterpolationType INTERPOLATION_LINEAR_ANGLE = 3

Linear interpolation with shortest path rotation.

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

InterpolationType INTERPOLATION_CUBIC_ANGLE = 4

Cubic interpolation with shortest path rotation.

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


enum UpdateMode:

UpdateMode UPDATE_CONTINUOUS = 0

Update between keyframes and hold the value.

UpdateMode UPDATE_DISCRETE = 1

Update at the keyframes.

UpdateMode UPDATE_CAPTURE = 2

Same as linear interpolation, but also interpolates from the current value (i.e. dynamically at runtime) if the first key isn’t at 0 seconds.


enum LoopMode:

LoopMode LOOP_NONE = 0

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

LoopMode LOOP_LINEAR = 1

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

LoopMode LOOP_PINGPONG = 2

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


enum LoopedFlag:

LoopedFlag LOOPED_FLAG_NONE = 0

This flag indicates that the animation proceeds without any looping.

LoopedFlag LOOPED_FLAG_END = 1

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

LoopedFlag LOOPED_FLAG_START = 2

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


enum FindMode:

FindMode FIND_MODE_NEAREST = 0

Finds the nearest time key.

FindMode FIND_MODE_APPROX = 1

Finds only the key with approximating the time.

FindMode FIND_MODE_EXACT = 2

Finds only the key with matching the time.


Property Descriptions

float length = 1.0

  • void set_length ( float value )

  • float get_length ( )

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.


LoopMode loop_mode = 0

Determines the behavior of both ends of the animation timeline during animation playback. This is used for correct interpolation of animation cycles, and for hinting the player that it must restart the animation.


float step = 0.1

  • void set_step ( float value )

  • float get_step ( )

The animation step value.


Method Descriptions

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

Adds a track to the Animation.


StringName animation_track_get_key_animation ( int track_idx, int key_idx ) const

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


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

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


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

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


float audio_track_get_key_end_offset ( int track_idx, int key_idx ) 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.


float audio_track_get_key_start_offset ( int track_idx, int key_idx ) 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.


Resource audio_track_get_key_stream ( int track_idx, int key_idx ) const

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


int audio_track_insert_key ( int track_idx, float time, Resource stream, float start_offset=0, float end_offset=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.


bool audio_track_is_use_blend ( int track_idx ) const

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


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

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.


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

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.


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

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


void audio_track_set_use_blend ( int track_idx, bool enable )

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


Vector2 bezier_track_get_key_in_handle ( int track_idx, int key_idx ) const

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


Vector2 bezier_track_get_key_out_handle ( int track_idx, int key_idx ) const

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


float bezier_track_get_key_value ( int track_idx, int key_idx ) const

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


int bezier_track_insert_key ( int track_idx, float time, float value, Vector2 in_handle=Vector2(0, 0), Vector2 out_handle=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.


float bezier_track_interpolate ( int track_idx, float time ) const

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


void bezier_track_set_key_in_handle ( int track_idx, int key_idx, Vector2 in_handle, float balanced_value_time_ratio=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.


void bezier_track_set_key_out_handle ( int track_idx, int key_idx, Vector2 out_handle, float balanced_value_time_ratio=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.


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

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.


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

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


float blend_shape_track_interpolate ( int track_idx, float time_sec ) 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.


void clear ( )

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


void compress ( int page_size=8192, int fps=120, float split_tolerance=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.


void copy_track ( int track_idx, Animation to_animation )

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


int find_track ( NodePath path, TrackType type ) const

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


int get_track_count ( ) const

Returns the amount of tracks in the animation.


StringName method_track_get_name ( int track_idx, int key_idx ) const

Returns the method name of a method track.


Array method_track_get_params ( int track_idx, int key_idx ) const

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


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

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


Vector3 position_track_interpolate ( int track_idx, float time_sec ) const

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


void remove_track ( int track_idx )

Removes a track by specifying the track index.


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

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


Quaternion rotation_track_interpolate ( int track_idx, float time_sec ) const

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


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

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


Vector3 scale_track_interpolate ( int track_idx, float time_sec ) const

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


int track_find_key ( int track_idx, float time, FindMode find_mode=0 ) const

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


bool track_get_interpolation_loop_wrap ( int track_idx ) const

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


InterpolationType track_get_interpolation_type ( int track_idx ) const

Returns the interpolation type of a given track.


int track_get_key_count ( int track_idx ) const

Returns the number of keys in a given track.


float track_get_key_time ( int track_idx, int key_idx ) const

Returns the time at which the key is located.


float track_get_key_transition ( int track_idx, int key_idx ) const

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


Variant track_get_key_value ( int track_idx, int key_idx ) const

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


NodePath track_get_path ( int track_idx ) const

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


TrackType track_get_type ( int track_idx ) const

Gets the type of a track.


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

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


bool track_is_compressed ( int track_idx ) const

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


bool track_is_enabled ( int track_idx ) const

Returns true if the track at index track_idx is enabled.


bool track_is_imported ( int track_idx ) const

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


void track_move_down ( int track_idx )

Moves a track down.


void track_move_to ( int track_idx, int to_idx )

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


void track_move_up ( int track_idx )

Moves a track up.


void track_remove_key ( int track_idx, int key_idx )

Removes a key by index in a given track.


void track_remove_key_at_time ( int track_idx, float time )

Removes a key at time in a given track.


void track_set_enabled ( int track_idx, bool enabled )

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


void track_set_imported ( int track_idx, bool imported )

Sets the given track as imported or not.


void track_set_interpolation_loop_wrap ( int track_idx, bool interpolation )

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


void track_set_interpolation_type ( int track_idx, InterpolationType interpolation )

Sets the interpolation type of a given track.


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

Sets the time of an existing key.


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

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


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

Sets the value of an existing key.


void track_set_path ( int track_idx, NodePath path )

Sets the path of a track. 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. 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".


void track_swap ( int track_idx, int with_idx )

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


UpdateMode value_track_get_update_mode ( int track_idx ) const

Returns the update mode of a value track.


Variant value_track_interpolate ( int track_idx, float time_sec ) const

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


void value_track_set_update_mode ( int track_idx, UpdateMode mode )

Sets the update mode (see UpdateMode) of a value track.

Previous Next


© Copyright 2014-present Juan Linietsky, Ariel Manzur and the Godot community (CC BY 3.0). Revision 53e837c6.

Built with Sphinx using a theme provided by Read the Docs.