# 3D Model Animations

3D models in *.glTF* and *.glb* format can include as many animations as you want in them. Animations tell the mesh how to move, by specifying a series of *keyframes* that are laid out over time, the mesh then blends from one pose to the other to simulate continuous movement.

Most 3D model animations are [*skeletal animations*](https://en.wikipedia.org/wiki/Skeletal_animation). These animations simplify the complex geometry of the model into a "stick figure", linking every vertex in the mesh to the closest *bone* in the *skeleton*. Modelers adjust the skeleton into different poses, and the mesh stretches and bends to follow these movements.

As an alternative, *vertex animations* animate a model without the need of a skeleton. These animations specify the position of each vertex in the model directly. Decentraland supports these animations as well.

See [Animations](https://github.com/decentraland/docs-creator/blob/main/creator/3d-modeling/animations/README.md) for details on how to create animations for a 3D model. Read [Shape components](/creator/scenes-sdk7/3d-content-essentials/shape-components.md) for instructions on how to import a 3D model to a scene.

{% hint style="info" %}
**💡 Tip**: Animations are usually better for moving something in place, not for changing the position of an entity. For example, you can set an animation to move a character's feet in place, but to change the location of the entity it's best to use the Transform component. See [Positioning entities](/creator/scenes-sdk7/3d-content-essentials/move-entities.md) for more details.
{% endhint %}

## Check a 3D model for animations

Not all *glTF* files include animations. To see if there are any available, you can do the following:

* If using [VS Code](https://code.visualstudio.com/)(recommended), install the *GLTF Tools* extension and view the contents of a glTF file there.
* Open the [Babylon Sandbox](https://sandbox.babylonjs.com/) site and drag the glTF file (and any *.jpg* or *.bin* dependencies) to the browser.
* Open the *.glTF* file with a text editor and scroll down till you find *"animations":*.

{% hint style="info" %}
**💡 Tip**: In *skeletal* animations, an animation name is often comprised of its armature name, an underscore and its animation name. For example `myArmature_animation1`.
{% endhint %}

## Automatic playing

If a 3D model includes any animations, the default behavior is that the first of these is always played on a loop.

To avoid this behavior, add an `Animator` component to the entity that has the model, and then handle the playing of animations explicitly. If an `Animator` component is present in the entity, all animations default to a `playing: false` state, and need to be manually played.

{% hint style="info" %}
**💡 Tip**: In the [Scene Editor](/creator/scene-editor/get-started/about-editor.md), you can add an **Animator** component visually. See [Add Components](/creator/scene-editor/build/components.md#add-components). You can also control animations in a no-code way via **Actions**, see [Make any item smart](/creator/scene-editor/interactivity/make-any-item-smart.md).
{% endhint %}

## Handle animations explicitly

An `Animator` component is used to access all the animations of the entity and can be used to explicitly tell the entity to play or stop an animation. The `Animator` component includes an array of `states`, this list must include one object for each one of the animations that the 3D model can perform. A single `Animator` can include as many states as needed.

```ts
// Create entity
const shark = engine.addEntity()

// Add a 3D model to it
GltfContainer.create(shark, {
	src: 'models/shark.glb',
})

Animator.create(shark, {
	states: [
		{
			clip: 'swim',
			playing: true,
			loop: true,
		},
	],
})
```

Each `state` object keeps track of if an animation is currently playing.

{% hint style="warning" %}
**📔 Note**: The `Animator` component must be imported via

> `import { Animator } from "@dcl/sdk/ecs"`

See [Imports](/creator/scenes-sdk7/getting-started/coding-scenes.md#imports) for how to handle these easily.
{% endhint %}

## Fetch an animation

Fetch a clip from the `Animator` by name using the `.Animator.getClip()` function. This function returns a mutable version of the animation state object.

```ts
const swimAnim = Animator.getClip(sharkEntity, 'swim')
```

`Animator.getClip` requires the following parameters:

* `entity`: The entity of the `Animator` component that you want to query.
* `clipName`: String for the name of the clip you want to fetch.

`Animator.getClip` fetches a mutable version of the animation state, so you can modify values freely on what this function returns.

```ts
const swimAnim = Animator.getClip(sharkEntity, 'swim')
swimAnim.looping = false
```

{% hint style="warning" %}
**📔 Note**: If you attempt to use `Animator.getClip()` to fetch a clip that exists in the 3D model, but is not listed in the `Animator` component, it returns `null`.
{% endhint %}

## Play an animation

The `.playing` field in an animation state determines if the animation is currently playing. Note that multiple animations may be playing in a single 3D model at the same time.

Use the `Animator.playSingleAnimation()` function on an `AnimationState` object.

```ts
Animator.playSingleAnimation(sharkEntity, 'swim')
```

If the entity was playing any other animations, `Animator.playSingleAnimation` stops them.

`Animator.playSingleAnimation` requires the following parameters:

* `entity`: The entity of the `Animator` component that you want to affect.
* `clipName`: String for the name of the clip you want to play.
* `resetCursor`: *(optional)* If *true*, it plays the animation from the start, even if the animation was previously paused. If *false*, it will keep playing the animation from where it was paused. Default: *true*.

```ts
Animator.playSingleAnimation(sharkEntity, 'swim', false)
```

The following table summarizes how `Animator.playSingleAnimation()` behaves, using different values for the `resetCursor` property:

|                            | `reset` = *false* (default)     | `reset` = *true*      |
| -------------------------- | ------------------------------- | --------------------- |
| **Currently playing**      | Has no effect.                  | Plays from the start. |
| **Paused**                 | Resumes from last frame played. | Plays from the start. |
| **Finished (Non-looping)** | Plays from the start.           | Plays from the start. |

## Looping animations

By default, animations are played in a loop that keeps repeating the animation forever.

Change this setting by setting the `loop` property in the `state` object.

```ts
Animator.create(shark, {
	states: [
		{
			clip: 'bite',
			playing: true,
			loop: false,
		},
	],
})
```

If `looping` is set to *false*, the animation plays just once and then stops, staying on the posture of the last frame.

## Stop an animation

To stop all animations that an entity is playing, use `Animator.stopAllAnimations()`.

```ts
Animator.stopAllAnimations(shark)
```

`Animator.stopAllAnimations` requires the following parameters:

* `entity`: The entity of the `Animator` component that you want to affect.
* `resetCursor`: *(optional)* If *true*, it returns to the posture in the first frame of the animation. If *false*, stays paused in its current posture. Default: *true*.

{% hint style="warning" %}
**📔 Note**: When playing an animation with `Animator.playSingleAnimation`, this function handles stopping all other animations behind the scenes. You don't need to explicitly stop other animations in that case.
{% endhint %}

When an animation finishes playing a non-looping animation, by default the 3D model remains in the last posture it had. To change this default behavior so that when the animation ends it goes back to the first posture, set the `shouldReset` property to *true*.

```ts
Animator.create(shark, {
	states: [
		{
			name: 'bite',
			clip: 'bite',
			playing: true,
			shouldReset: true,
			loop: true,
		},
	],
})
```

You can also use `Animator.stopAllAnimations()` at any time to explicitly set the posture back to the first frame in the animation.

{% hint style="warning" %}
**📔 Note**: Resetting the posture is an abrupt change. If you want to make the model transition smoothly tinto another posture, you can either:
{% endhint %}

## Handle multiple animations

If a 3D model has multiple animations packed into it, a single `Animator` component can deal with all of them.

```ts
// Create entity
const shark = engine.addEntity()

// Add a 3D model to it
GltfContainer.create(shark, {
	src: 'models/shark.glb'
})

Animator.create(shark, {
	states:[{
			clip: "swim",
			playing: true,
			loop: true
		}. {
			clip: "bite",
			playing: true,
			loop: true
		}
	]
})
```

In the example above, two animations are handled by separate `state` objects, and they are then both assigned to the same `Animator` component.

Each bone in an animation can only be affected by one animation at a time, unless these animations have a `weight` that adds up to a value of 1 or less.

If one animation only affects a character's legs, and another only affects a character's head, then they can be played at the same time without any issue. But if they both affect the character's legs, then you must either only play one at a time, or play them with lower `weight` values.

If in the above example, the `bite` animation only affects the shark's mouth, and the `swim` animation only affects the bones of the shark's spine, then they can both be played at the same time if they're on separate layers.

{% hint style="warning" %}
**📔 Note**: `Animator.playSingleAnim()` stops all other animations that the entity is currently playing. To play multiple animations at the same time, modify the `playing` property in the animation states manually.
{% endhint %}

## Animation speed

Change the speed at which an animation is played by changing the `speed` property. The value of the speed is 1 by default.

```ts
Animator.create(shark, {
	states: [
		{
			clip: 'swim',
			playing: true,
			loop: true,
			speed: 2,
		},
	],
})
```

Set the speed lower than 1 to play it slower, for example to 0.5 to play it at half the speed. Set it higher than 1 to play it faster, for example to 2 to play it at double the speed.

```ts
const swimAnim = Animator.getClip(sharkEntity, 'swim')

swimAnim.speed = 0.5
```

## Animation weight

The `weight` property allows a single model to carry out multiple animations on different layers at once, calculating a weighted average of all the movements involved in the animation. The value of `weight` determines how much importance that animation will be given in the average.

By default, `weight` is equal to *1*. The value of `weight` can't be any higher than *1*.

```ts
Animator.create(shark, {
	states: [
		{
			clip: 'swim',
			playing: true,
			loop: true,
			weight: 0.2,
		},
	],
})
```

The `weight` value of all active animations in an entity should add up to 1 at all times. If it adds up to less than 1, the weighted average will be using the default position of the armature for the remaining part of the calculation.

For example, in the code example above, we're playing the *swim* animation, that only has a `weight` of *0.2*. This swimming movement will be quite subtle: only 20% of the intensity that the animation defines. The remaining 80% of the calculation takes values from the default posture of the armature.

The `weight` property can be used in interesting ways, for example the `weight` property of *swim* could be set in proportion to how fast the shark is swimming, so you don't need to create multiple animations for fast and slow swimming.

You could also change the `weight` value gradually when starting and stopping an animation to give it a more natural transition and to avoid jumps from the default pose to the first pose in the animation.

{% hint style="warning" %}
**📔 Note**: The added `weight` value of all animations that are acting on a 3D model's bone can't be more than 1. If more than one animation is affecting the same bones at the same time, they need to have their weight set to values that add to less than 1.
{% endhint %}

```ts
const swimAnim = Animator.getClip(sharkEntity, 'swim')

swimAnim.weight = 0.5
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.decentraland.org/creator/scenes-sdk7/3d-content-essentials/3d-model-animations.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
