# GltfAnimation

Artifact: [androidx.xr.scenecore:scenecore](https://developer.android.com/jetpack/androidx/releases/xr-scenecore) [View Source](https://cs.android.com/search?q=file:androidx/xr/scenecore/GltfAnimation.kt+class:androidx.xr.scenecore.GltfAnimation) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#1.0.0)

*** ** * ** ***


```
public final class GltfAnimation
```

<br />

*** ** * ** ***

`https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation` represents an animation in a `https://developer.android.com/reference/androidx/xr/scenecore/GltfModelEntity`.

Multiple animations can play simultaneously. If multiple animations affect the same node, the animation with the **highest index** in the glTF file takes precedence. Specifically, only the state of the highest-index animation is reflected in the scene for a given frame. Updates from other lower-index animations (such as node transforms) for the same target will be overwritten.

A `https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation` cannot be used once its associated `https://developer.android.com/reference/androidx/xr/scenecore/GltfModelEntity` has been destroyed.

## Summary

| ### Nested types |
|---|
| `public final class https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation.AnimationState` Specifies the current animation state of this `https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation`. |

| ### Public methods |
|---|---|
| `final void` | `https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation#addAnimationStateListener(java.util.function.Consumer)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/function/Consumer.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation.AnimationState> listener )` Registers a listener to be invoked on the main thread when the animation state of the `https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation` changes. |
| `final void` | `https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation#addAnimationStateListener(java.util.concurrent.Executor,java.util.function.Consumer)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/concurrent/Executor.html executor, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/function/Consumer.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation.AnimationState> listener )` Registers a listener to be invoked when the animation state of this `https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation` changes. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation.AnimationState` | `@https://developer.android.com/reference/androidx/annotation/MainThread https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation#getAnimationState()()` The current state of this animation. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/time/Duration.html` | `@https://developer.android.com/reference/androidx/annotation/RequiresApi(value = 26) https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation#getDuration()()` The duration of this animation. |
| `final int` | `https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation#getIndex()()` The index of this animation in the source glTF model. |
| `final boolean` | `@https://developer.android.com/reference/androidx/annotation/MainThread https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation#getLoop()()` Whether this animation should loop when playing. |
| `final https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation#getName()()` The name of this animation, or `null` if the animation is unnamed. |
| `final float` | `@https://developer.android.com/reference/androidx/annotation/MainThread https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation#getSpeed()()` The playback speed multiplier for this animation. |
| `final void` | `@https://developer.android.com/reference/androidx/annotation/MainThread https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation#pause()()` Pauses this animation. |
| `final void` | `https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation#removeAnimationStateListener(java.util.function.Consumer)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/function/Consumer.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation.AnimationState> listener )` Unregisters a previously registered animation state update listener. |
| `final void` | `@https://developer.android.com/reference/androidx/annotation/MainThread https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation#resume()()` Resumes this animation. |
| `final void` | `@https://developer.android.com/reference/androidx/annotation/RequiresApi(value = 26) @https://developer.android.com/reference/androidx/annotation/MainThread [seekTo](https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation#seekTo(java.time.Duration))(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/time/Duration.html time)` **This method is deprecated.** Seeking animation is no longer supported. |
| `final void` | `@https://developer.android.com/reference/androidx/annotation/MainThread @https://developer.android.com/reference/androidx/xr/scenecore/ExperimentalGltfAnimationApi https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation#setLoop(kotlin.Boolean)(boolean value)` Whether this animation should loop when playing. |
| `final void` | `@https://developer.android.com/reference/androidx/annotation/MainThread https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation#setSpeed(kotlin.Float)(float value)` The playback speed multiplier for this animation. |
| `final void` | `@https://developer.android.com/reference/androidx/annotation/MainThread https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation#start()()` Starts playing this animation. |
| `final void` | `@https://developer.android.com/reference/androidx/annotation/RequiresApi(value = 26) @https://developer.android.com/reference/androidx/xr/scenecore/ExperimentalGltfAnimationApi @https://developer.android.com/reference/androidx/annotation/MainThread [start](https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation#start(androidx.xr.scenecore.GltfAnimationStartOptions))(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimationStartOptions options)` **This method is deprecated.** Use GltfAnimation.loop and GltfAnimation.speed properties with parameterless start() instead. |
| `final void` | `@https://developer.android.com/reference/androidx/annotation/MainThread https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation#stop()()` Stops this animation. |

## Public methods

### addAnimationStateListener

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#1.0.0)

```
public final void addAnimationStateListener(
    @NonNull Consumer<@NonNull GltfAnimation.AnimationState> listener
)
```

Registers a listener to be invoked on the main thread when the animation state of the `https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation` changes.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/function/Consumer.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation.AnimationState> listener` | The listener to invoke when the state changes. It receives the new `https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation.AnimationState`. |

### addAnimationStateListener

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#1.0.0)

```
public final void addAnimationStateListener(
    @NonNull Executor executor,
    @NonNull Consumer<@NonNull GltfAnimation.AnimationState> listener
)
```

Registers a listener to be invoked when the animation state of this `https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation` changes.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/concurrent/Executor.html executor` | The executor on which the listener will be invoked. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/function/Consumer.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation.AnimationState> listener` | The listener to invoke when the state changes. It receives the new `https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation.AnimationState`. |

### getAnimationState

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#1.0.0)

```
@MainThread
public final @NonNull GltfAnimation.AnimationState getAnimationState()
```

The current state of this animation.

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation.AnimationState` |   |

### getDuration

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#1.0.0)

```
@RequiresApi(value = 26)
public final @NonNull Duration getDuration()
```

The duration of this animation.

### getIndex

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#1.0.0)

```
public final int getIndex()
```

The index of this animation in the source glTF model.

### getLoop

Added in [1.1.0-alpha01](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#1.1.0-alpha01)

```
@MainThread
public final boolean getLoop()
```

Whether this animation should loop when playing.

The default value is `false` (playback does not loop). When looping is disabled (`false`) and playback reaches the end of the animation, the animation state transitions to `https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation.AnimationState#STOPPED()`, while remaining clamped at the final frame pose.

Changes to the loop configuration only take effect during `https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation#start()`. Modifying this property while the animation is actively playing is currently not supported. The setter will remain annotated with `https://developer.android.com/reference/androidx/xr/scenecore/ExperimentalGltfAnimationApi` until dynamic loop updates during active playback are supported.

### getName

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#1.0.0)

```
public final String getName()
```

The name of this animation, or `null` if the animation is unnamed.

### getSpeed

Added in [1.1.0-alpha01](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#1.1.0-alpha01)

```
@MainThread
public final float getSpeed()
```

The playback speed multiplier for this animation.

This can be changed while the animation is playing or paused.

The default playback speed is `1.0f`. The speed multiplier determines the playback rate:

- **1.0:** Normal speed.

- **\> 1.0:** Faster playback.

- **\> 0.0 and \< 1.0:** Slower playback (e.g., 0.5 is half speed).

- **0.0:** Freezes the animation at the current frame while keeping it active (unlike pausing).

- **\< 0.0:** Plays the animation in reverse.

**Note on Reverse Playback:** When playing in reverse without looping (`https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation#loop()` set to `false`), starting the animation from the beginning causes it to reach the end of playback immediately. Enable looping via `https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation#loop()` (setting `loop` to `true`) to play the animation in reverse continuously.

### pause

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#1.0.0)

```
@MainThread
public final void pause()
```

Pauses this animation.

This freezes the animation at the current frame and transitions the animation state to `https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation.AnimationState#PAUSED()`. Use `https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation#resume()` to continue playback.

Note: Calling `https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation#start()` while in the `https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation.AnimationState#PAUSED()` state will reset the playback time to the start of the animation, and transitions the state to `https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation.AnimationState#PLAYING()`.

### removeAnimationStateListener

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#1.0.0)

```
public final void removeAnimationStateListener(
    @NonNull Consumer<@NonNull GltfAnimation.AnimationState> listener
)
```

Unregisters a previously registered animation state update listener.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/function/Consumer.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation.AnimationState> listener` | The listener to remove. |

### resume

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#1.0.0)

```
@MainThread
public final void resume()
```

Resumes this animation.

This continues the animation from the point where it was paused and transitions the animation state to `https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation.AnimationState#PLAYING()`.

Note: Calling `https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation#resume()` while in the `https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation.AnimationState#PLAYING()` and `https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation.AnimationState#STOPPED()` state will have no effect.

### seekTo

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#1.0.0) Deprecated in [1.1.0-alpha01](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#1.1.0-alpha01)

```
@RequiresApi(value = 26)
@MainThread
public final void [seekTo](https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation#seekTo(java.time.Duration))(@NonNull Duration time)
```

> [!CAUTION]
> **This method is deprecated.**   
> Seeking animation is no longer supported.

Seeks this animation to the specified `https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation#seekTo(java.time.Duration)` offset.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/time/Duration.html time` | The offset from the beginning of the animation. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html` | if `https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation#seekTo(java.time.Duration)` is negative. |

### setLoop

Added in [1.1.0-alpha01](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#1.1.0-alpha01)

```
@MainThread
@ExperimentalGltfAnimationApi
public final void setLoop(boolean value)
```

Whether this animation should loop when playing.

The default value is `false` (playback does not loop). When looping is disabled (`false`) and playback reaches the end of the animation, the animation state transitions to `https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation.AnimationState#STOPPED()`, while remaining clamped at the final frame pose.

Changes to the loop configuration only take effect during `https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation#start()`. Modifying this property while the animation is actively playing is currently not supported. The setter will remain annotated with `https://developer.android.com/reference/androidx/xr/scenecore/ExperimentalGltfAnimationApi` until dynamic loop updates during active playback are supported.

### setSpeed

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#1.0.0)

```
@MainThread
public final void setSpeed(float value)
```

The playback speed multiplier for this animation.

This can be changed while the animation is playing or paused.

The default playback speed is `1.0f`. The speed multiplier determines the playback rate:

- **1.0:** Normal speed.

- **\> 1.0:** Faster playback.

- **\> 0.0 and \< 1.0:** Slower playback (e.g., 0.5 is half speed).

- **0.0:** Freezes the animation at the current frame while keeping it active (unlike pausing).

- **\< 0.0:** Plays the animation in reverse.

**Note on Reverse Playback:** When playing in reverse without looping (`https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation#loop()` set to `false`), starting the animation from the beginning causes it to reach the end of playback immediately. Enable looping via `https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation#loop()` (setting `loop` to `true`) to play the animation in reverse continuously.

### start

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#1.0.0)

```
@MainThread
public final void start()
```

Starts playing this animation.

This transitions the animation state to `https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation.AnimationState#PLAYING()`. By default, the animation plays once at normal speed (`1.0f`) without looping unless configured otherwise via `https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation#loop()` and `https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation#speed()`. Calling `https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation#start()` while the animation is currently playing or paused will restart playback from the beginning.

### start

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#1.0.0) Deprecated in [1.1.0-alpha01](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#1.1.0-alpha01)

```
@RequiresApi(value = 26)
@ExperimentalGltfAnimationApi
@MainThread
public final void [start](https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation#start(androidx.xr.scenecore.GltfAnimationStartOptions))(@NonNull GltfAnimationStartOptions options)
```

> [!CAUTION]
> **This method is deprecated.**   
> Use GltfAnimation.loop and GltfAnimation.speed properties with parameterless start() instead, use this instead:
>
> ```
> apply { loop = options.shouldLoop; speed = options.speed }.start()
> ```

Starts playing this animation with the specified `https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation#start(androidx.xr.scenecore.GltfAnimationStartOptions)`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimationStartOptions options` | Configuration options for starting the animation. |

### stop

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#1.0.0)

```
@MainThread
public final void stop()
```

Stops this animation.

This resets the playback time to 0 and transitions the animation state to `https://developer.android.com/reference/androidx/xr/scenecore/GltfAnimation.AnimationState#STOPPED()`. If this animation is not currently playing or pausing, this method has no effect.