# AnimComponentLayer

Class · category: Animation

Source: https://github.com/playcanvas/engine/blob/f059dc005842f76e052cdf44d8370c8c7ec475ac/src/framework/components/anim/component-layer.js#L34

An AnimComponentLayer is one layer of an [AnimComponent](https://api.playcanvas.com/engine/classes/AnimComponent.md). It runs the state machine that
the [AnimStateGraph](https://api.playcanvas.com/engine/classes/AnimStateGraph.md) defines for that layer and contributes the result to the entity's
final pose with a [weight](https://api.playcanvas.com/engine/classes/AnimComponentLayer.md#weight), either overwriting the layers beneath it or adding to them
according to `blendType`. A [mask](https://api.playcanvas.com/engine/classes/AnimComponentLayer.md#mask) limits which nodes of the hierarchy the layer
animates, which is how an upper-body action plays on top of a full-body locomotion layer. The
first layer is [AnimComponent#baseLayer](https://api.playcanvas.com/engine/classes/AnimComponent.md#baselayer); add more with [AnimComponent#addLayer](https://api.playcanvas.com/engine/classes/AnimComponent.md#addlayer).

Playback is per layer: [play](https://api.playcanvas.com/engine/classes/AnimComponentLayer.md#play) starts a named state, [transition](https://api.playcanvas.com/engine/classes/AnimComponentLayer.md#transition) blends to another
state over a given time, [pause](https://api.playcanvas.com/engine/classes/AnimComponentLayer.md#pause) and [reset](https://api.playcanvas.com/engine/classes/AnimComponentLayer.md#reset) act on the current state, and
[activeState](https://api.playcanvas.com/engine/classes/AnimComponentLayer.md#activestate), [activeStateProgress](https://api.playcanvas.com/engine/classes/AnimComponentLayer.md#activestateprogress) and [transitioning](https://api.playcanvas.com/engine/classes/AnimComponentLayer.md#transitioning) report where the
layer is. [assignAnimation](https://api.playcanvas.com/engine/classes/AnimComponentLayer.md#assignanimation) binds an [AnimTrack](https://api.playcanvas.com/engine/classes/AnimTrack.md) to a state, or to a node inside a
blend tree using a dotted path, and [blendToWeight](https://api.playcanvas.com/engine/classes/AnimComponentLayer.md#blendtoweight) fades the whole layer in or out.

**Example**

```ts
// Fade in an upper-body layer and play its 'Wave' state over the base layer
const layer = entity.anim.findAnimationLayer('UpperBody');
layer.blendToWeight(1, 0.3);
layer.play('Wave');
```

## Accessors

### activeState

```ts
get activeState(): string
```

Gets the currently active state name.

### activeStateCurrentTime

```ts
get activeStateCurrentTime(): number
set activeStateCurrentTime(time: number)
```

Gets the active state's time in seconds.

### activeStateDuration

```ts
get activeStateDuration(): number
```

Gets the currently active states duration.

### activeStateProgress

```ts
get activeStateProgress(): number
```

Gets the currently active state's progress as a value normalized by the state's animation
duration. Looped animations will return values greater than 1.

### mask

```ts
get mask(): any
set mask(value: any)
```

Gets the mask of bones which should be animated or ignored by this layer.

### name

```ts
get name(): string
```

Returns the name of the layer.

### playable

```ts
get playable(): boolean
```

Returns true if a state graph has been loaded and all states in the graph have been assigned
animation tracks.

### playing

```ts
get playing(): boolean
set playing(value: boolean)
```

Gets whether this layer is currently playing.

### previousState

```ts
get previousState(): string | null
```

Gets the previously active state name.

### states

```ts
get states(): string[]
```

Gets all available states in this layers state graph.

### transitioning

```ts
get transitioning(): boolean
```

Gets whether the anim component layer is currently transitioning between states.

### transitionProgress

```ts
get transitionProgress(): number | null
```

Gets the progress, if the anim component layer is currently transitioning between states.
Otherwise returns null.

### weight

```ts
get weight(): number
set weight(value: number)
```

Sets the blending weight of this layer.

## Methods

### assignAnimation

```ts
assignAnimation(nodePath: string, animTrack: AnimTrack, speed?: number, loop?: boolean): void
```

Assigns an animation track to a state or blend tree node in the current graph. If a state
for the given nodePath doesn't exist, it will be created. If all states nodes are linked and
the [AnimComponent#activate](https://api.playcanvas.com/engine/classes/AnimComponent.md#activate) value was set to true then the component will begin
playing.

**Parameters**

- `nodePath` (`string`): Either the state name or the path to a blend tree node that this
  animation should be associated with. Each section of a blend tree path is split using a
  period (`.`) therefore state names should not include this character (e.g "MyStateName" or
  "MyStateName.BlendTreeNode").
- `animTrack` ([`AnimTrack`](https://api.playcanvas.com/engine/classes/AnimTrack.md)): The animation track that will be assigned to this state and
  played whenever this state is active.
- `speed` (`number`, optional): Update the speed of the state you are assigning an animation to.
  Defaults to 1.
- `loop` (`boolean`, optional): Update the loop property of the state you are assigning an
  animation to. Defaults to true.

### blendToWeight

```ts
blendToWeight(weight: number, time: number): void
```

Blend from the current weight value to the provided weight value over a given amount of time.

**Parameters**

- `weight` (`number`): The new weight value to blend to.
- `time` (`number`): The duration of the blend in seconds.

### getAnimationAsset

```ts
getAnimationAsset(stateName: string): { asset: number }
```

Returns an object holding the animation asset id that is associated with the given state.

**Parameters**

- `stateName` (`string`): The name of the state to get the asset for.

**Returns** `{ asset: number }`: An object containing the animation asset id associated with the given state.

### pause

```ts
pause(): void
```

Pause the animation in the current state.

### play

```ts
play(name?: string): void
```

Start playing the animation in the current state.

**Parameters**

- `name` (`string`, optional): If provided, will begin playing from the start of the state with
  this name.

### rebind

```ts
rebind(): void
```

Rebind any animations in the layer to the currently present components and model of the anim
components entity.

### removeNodeAnimations

```ts
removeNodeAnimations(nodeName: string): void
```

Removes animations from a node in the loaded state graph.

**Parameters**

- `nodeName` (`string`): The name of the node that should have its animation tracks removed.

### reset

```ts
reset(): void
```

Reset the animation component to its initial state, including all parameters. The system
will be paused.

### transition

```ts
transition(to: string, time?: number, transitionOffset?: number): void
```

Transition to any state in the current layers graph. Transitions can be instant or take an
optional blend time.

**Parameters**

- `to` (`string`): The state that this transition will transition to.
- `time` (`number`, optional, default `0`): The duration of the transition in seconds. Defaults to 0.
- `transitionOffset` (`number`, optional, default `null`): If provided, the destination state will begin playing
  its animation at this time. Given in normalized time, based on the states duration & must be
  between 0 and 1. Defaults to null.
