Skip to main content

AnimationPlayer

Class

Controls animation playback.

  • Available on all platforms
Platform details: Snapchat, SPECS, Camera Kit Web, Camera Kit Mobile fully supported.

Platform details

  • Snapchat — Fully supported.

  • SPECS — Fully supported.

  • Camera Kit Web — Fully supported.

  • Camera Kit Mobile — Fully supported.

Controls animation playback. The component takes in a list of AnimationClips, and allows you to play, stop, resume, subscribe to animation events, and more.

Index

Example


//Transition between two Animation Clips `firstClipName` and `secondClipName`
//on tap by alternating secondClip weight between 0 or 1 within some `transitionInSec`,
//ensuring that secondClip is processed after firstClip as to blend the animations correctly.

//@input Component.AnimationPlayer animationPlayer
//@input number transitionInSec = 1
//@input string firstClipName
//@input string secondClipName

// Keep track of current state of transition.
let isTransitioning = false;
let accumulatedTime = 0;
let secondClipTargetWeight = 1;

// Store a reference to the clips so we don't
// have to keep asking engine.
let firstClip, secondClip;

// *********************************************************
// * Events that triggers and responds to the transitions.
// *********************************************************/

// Trigger the transition to the new clip.
const startTransition = script.createEvent("TapEvent");
startTransition.bind(triggerTransition);

const onUpdateEvent = script.createEvent("UpdateEvent");
onUpdateEvent.bind(interpolateBetweenAnimations);

// *********************************************************
// * Define how transitioning works.
// *********************************************************/

// Play the first clip immediately.
// and make sure clips are in the right order.
function init() {
script.animationPlayer.playClipAt(script.firstClipName, 0);

// Clips are processed in order. Thus, if we want to transition
// to a second clip, we need to make sure the second clip
// is processed after the first clip.
putClipAtEndIfNeeded(script.animationPlayer, script.secondClipName);

// Store references to the clips.
firstClip = script.animationPlayer.getClip(script.firstClipName);
secondClip = script.animationPlayer.getClip(script.secondClipName);
}

// Trigger the transition to the new clip.
function triggerTransition() {
// Only transition, if we're not in the middle of transitioning.
if (!isTransitioning) {
// If the target weight is 0, that means the clip is already playing
// so we don't need to tell it to play again.
if (secondClipTargetWeight === 1) {
script.animationPlayer.playClipAt(script.secondClipName, 0);
}

accumulatedTime = 0;
isTransitioning = true;

// Begin the update loop that will transition the clips.
onUpdateEvent.enabled = isTransitioning;
}
}

// Blend the weight of a second clip to the first clip,
// based on the time elapsed in transition.
function interpolateBetweenAnimations () {
if (isTransitioning) {
// Keep track of how many seconds elapsed since the start of the transition.
accumulatedTime += getDeltaTime();

// Once time has elapsed beyond our total transition time
// we can finalize our weight and callback.
if (accumulatedTime >= script.transitionInSec) {
secondClip.weight = secondClipTargetWeight;

onTransitionEnd();

return;
}

// Set the current weight of the clips based on % between how long we've beeen transitioning for
// and how long we want total transition to take.
let currentWeight = accumulatedTime / script.transitionInSec ;
secondClip.weight = lerp(1-secondClipTargetWeight, secondClipTargetWeight, currentWeight);
}
}

function onTransitionEnd() {
// Set our target weight to be the opposite.
secondClipTargetWeight = 1 - secondClipTargetWeight;

// Enable triggering transition, and disable the update loop.
isTransitioning = false;
onUpdateEvent.enabled = isTransitioning;
}

// *********************************************************
// * Helpers
// *********************************************************/

function putClipAtEndIfNeeded(animationPlayer, clipName) {
const clips = animationPlayer.getAnimationClips();
const currentOrderOfClips = clips.map(function(clip) {return clip.name;});
const clipIndex = currentOrderOfClips.indexOf(clipName);

if (clipIndex != clips.length - 1) {
if (clipIndex < 0) {
print("Clip with that name is not found in the provided Animation Player.");
return;
}

const clonedClip = clips[clipIndex].clone(clipName);
animationPlayer.removeClip(clipName);
animationPlayer.addClip(clonedClip);
}
}

function lerp(a, b, t) {
return a * (1.0 - t) + b * t;
}

// *********************************************************
// * Initialize the script!
// *********************************************************/

init();

Extends

Properties

clips

readonly clips: AnimationClip[]

Array of animation clips

Since

Lens Scripting Version 253


enabled

enabled: boolean

If disabled, the Component will stop enacting its behavior.

Default Value

true

Inherited from

Component.enabled


isBeingDestroyed

readonly isBeingDestroyed: boolean

Returns true if this Component is currently being destroyed. Becomes true as soon as destruction begins via Component#destroy or destruction of its owning SceneObject, before any OnDestroyEvent callbacks fire.

Since

Lens Scripting Version 364

Example

if (!script.isBeingDestroyed) {
// Component is still fully alive
}

Inherited from

Component.isBeingDestroyed


isEnabledInHierarchy

readonly isEnabledInHierarchy: boolean

Returns true if this Component, its SceneObject, and all of that SceneObjects parents are enabled.

Inherited from

Component.isEnabledInHierarchy


onEvent

readonly onEvent: event1<AnimationPlayerOnEventArgs, void>

Bind a function to listen to the specified events emitted by AnimationAsset events. See here.

Since

Lens Scripting Version 265


sceneObject

readonly sceneObject: SceneObject

The SceneObject this component is on.

Inherited from

Component.sceneObject


uniqueIdentifier

readonly uniqueIdentifier: string

Since

Lens Scripting Version 176

Inherited from

Component.uniqueIdentifier

Methods

addClip()

addClip(clip): void

Adds a clip to the player. If one exists, replace existing clip.

Parameters

ParameterType
clipAnimationClip

Returns

void

Since

Lens Scripting Version 253


destroy()

destroy(): void

Destroys the component.

Returns

void

Inherited from

Component.destroy


forceUpdate()

forceUpdate(deltaTime): void

Updates the animation player forcing sampling, resulting in the setting of transforms and firing of animation events.

Parameters

ParameterType
deltaTimenumber

Returns

void

Since

Lens Scripting Version 266


forceUpdateClip()

forceUpdateClip(name, deltaTime): void

Updates a single named clip on the animation player, forcing sampling of that clip and resulting in the setting of transforms and firing of animation events for it. Behaves like AnimationPlayer.forceUpdate but restricts the update to the clip identified by name.

Parameters

ParameterType
namestring
deltaTimenumber

Returns

void

Since

Lens Scripting Version 381

Example

//@input Component.AnimationPlayer animationPlayer
script.animationPlayer.forceUpdateClip("run", getDeltaTime());

getActiveClips()

getActiveClips(): string[]

Get currently playing clips.

Returns

string[]

Since

Lens Scripting Version 253


getClip()

getClip(name): AnimationClip

Tries to get a clip from the player, returns null if it does not exist.

Parameters

ParameterType
namestring

Returns

AnimationClip

Since

Lens Scripting Version 253


getClipCurrentTime()

getClipCurrentTime(name): number

Returns the current time for a clip.

Parameters

ParameterType
namestring

Returns

number

Since

Lens Scripting Version 253


getClipEnabled()

getClipEnabled(name): boolean

Returns if a clip is enabled for playback.

Parameters

ParameterType
namestring

Returns

boolean

Since

Lens Scripting Version 253


getClipIsPlaying()

getClipIsPlaying(name): boolean

Returns if a clip is playing.

Parameters

ParameterType
namestring

Returns

boolean

Since

Lens Scripting Version 253


getInactiveClips()

getInactiveClips(): string[]

Get currently inactive clips.

Returns

string[]

Since

Lens Scripting Version 253


getSceneObject()

getSceneObject(): SceneObject

Returns the SceneObject the component is attached to.

Returns

SceneObject

Inherited from

Component.getSceneObject


getTransform()

getTransform(): Transform

Returns the Transform this component is attached to.

Returns

Transform

Inherited from

Component.getTransform


getTypeName()

static getTypeName(): TypeName<AnimationPlayer>

Returns the fully-qualified JavaScript type name for this class (for example, "Component.Camera"). This is a static accessor (no instance required) and returns the same value as the instance getTypeName().

Returns

TypeName<AnimationPlayer>

Overrides

Component.getTypeName


getTypeName()

getTypeName(): string

Returns the name of this object's type.

Returns

string

Inherited from

Component.getTypeName


isOfType()

isOfType(type): boolean

Returns true if the object matches or derives from the passed in type.

Parameters

ParameterType
typestring

Returns

boolean

Inherited from

Component.isOfType


isSame()

isSame(other): boolean

Returns true if this object is the same as other. Useful for checking if two references point to the same thing.

Parameters

ParameterType
otherScriptObject

Returns

boolean

Inherited from

Component.isSame


pauseAll()

pauseAll(): void

Pauses all clips.

Returns

void

Since

Lens Scripting Version 253


pauseClip()

pauseClip(name): void

Pause the clip with name.

Parameters

ParameterType
namestring

Returns

void

Since

Lens Scripting Version 253


playAll()

playAll(): void

Plays all clips.

Returns

void

Since

Lens Scripting Version 253


playClip()

playClip(name): void

Plays the named clip on the Animation Player.

Parameters

ParameterType
namestring

Returns

void

Since

Lens Scripting Version 309


playClipAt()

playClipAt(name, time): void

Plays clip with the given name and starting from the given time.

Parameters

ParameterType
namestring
timenumber

Returns

void

Since

Lens Scripting Version 253


removeClip()

removeClip(name): void

Removes a clip from the player.

Parameters

ParameterType
namestring

Returns

void

Since

Lens Scripting Version 253


resumeAll()

resumeAll(): void

Resumes all clips.

Returns

void

Since

Lens Scripting Version 253


resumeClip()

resumeClip(name): void

Resumes clip with name.

Parameters

ParameterType
namestring

Returns

void

Since

Lens Scripting Version 253


setClipEnabled()

setClipEnabled(name, enabled): void

Sets the clip to be enabled.

Parameters

ParameterType
namestring
enabledboolean

Returns

void

Since

Lens Scripting Version 253


stopAll()

stopAll(): void

Stops all clips and resets time to t = 0.

Returns

void

Since

Lens Scripting Version 253


stopClip()

stopClip(name): void

Stops the clip and resets time to t = 0.

Parameters

ParameterType
namestring

Returns

void

Since

Lens Scripting Version 253

See also