# 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)

*** ** * ** ***


```
class GltfAnimation
```

<br />

*** ** * ** ***

`https://developer.android.com/reference/kotlin/androidx/xr/scenecore/GltfAnimation` represents an animation in a `https://developer.android.com/reference/kotlin/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/kotlin/androidx/xr/scenecore/GltfAnimation` cannot be used once its associated `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/GltfModelEntity` has been destroyed.

## Summary

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

| ### Public functions |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/GltfAnimation#addAnimationStateListener(java.util.function.Consumer)( listener: https://developer.android.com/reference/java/util/function/Consumer.html<https://developer.android.com/reference/kotlin/androidx/xr/scenecore/GltfAnimation.AnimationState> )` Registers a listener to be invoked on the main thread when the animation state of the `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/GltfAnimation` changes. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/GltfAnimation#addAnimationStateListener(java.util.concurrent.Executor,java.util.function.Consumer)( executor: https://developer.android.com/reference/java/util/concurrent/Executor.html, listener: https://developer.android.com/reference/java/util/function/Consumer.html<https://developer.android.com/reference/kotlin/androidx/xr/scenecore/GltfAnimation.AnimationState> )` Registers a listener to be invoked when the animation state of this `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/GltfAnimation` changes. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread https://developer.android.com/reference/kotlin/androidx/xr/scenecore/GltfAnimation#pause()()` Pauses this animation. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/GltfAnimation#removeAnimationStateListener(java.util.function.Consumer)( listener: https://developer.android.com/reference/java/util/function/Consumer.html<https://developer.android.com/reference/kotlin/androidx/xr/scenecore/GltfAnimation.AnimationState> )` Unregisters a previously registered animation state update listener. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread https://developer.android.com/reference/kotlin/androidx/xr/scenecore/GltfAnimation#resume()()` Resumes this animation. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/RequiresApi(value = 26) @https://developer.android.com/reference/kotlin/androidx/annotation/MainThread [seekTo](https://developer.android.com/reference/kotlin/androidx/xr/scenecore/GltfAnimation#seekTo(java.time.Duration))(time: https://developer.android.com/reference/java/time/Duration.html)` **This function is deprecated.** Seeking animation is no longer supported. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread https://developer.android.com/reference/kotlin/androidx/xr/scenecore/GltfAnimation#start()()` Starts playing this animation. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/RequiresApi(value = 26) @https://developer.android.com/reference/kotlin/androidx/xr/scenecore/ExperimentalGltfAnimationApi @https://developer.android.com/reference/kotlin/androidx/annotation/MainThread [start](https://developer.android.com/reference/kotlin/androidx/xr/scenecore/GltfAnimation#start(androidx.xr.scenecore.GltfAnimationStartOptions))(options: https://developer.android.com/reference/kotlin/androidx/xr/scenecore/GltfAnimationStartOptions)` **This function is deprecated.** Use GltfAnimation.loop and GltfAnimation.speed properties with parameterless start() instead. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread https://developer.android.com/reference/kotlin/androidx/xr/scenecore/GltfAnimation#stop()()` Stops this animation. |

| ### Public properties |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/GltfAnimation.AnimationState` | `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/GltfAnimation#animationState()` The current state of this animation. |
| `https://developer.android.com/reference/java/time/Duration.html` | `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/GltfAnimation#duration()` The duration of this animation. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/GltfAnimation#index()` The index of this animation in the source glTF model. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/GltfAnimation#loop()` Whether this animation should loop when playing. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html?` | `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/GltfAnimation#name()` The name of this animation, or `null` if the animation is unnamed. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` | `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/GltfAnimation#speed()` The playback speed multiplier for this animation. |

## Public functions

### addAnimationStateListener

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

```
fun addAnimationStateListener(
    listener: Consumer<GltfAnimation.AnimationState>
): Unit
```

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

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

### addAnimationStateListener

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

```
fun addAnimationStateListener(
    executor: Executor,
    listener: Consumer<GltfAnimation.AnimationState>
): Unit
```

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

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

### pause

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

```
@MainThread
fun pause(): Unit
```

Pauses this animation.

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

Note: Calling `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/GltfAnimation#start()` while in the `https://developer.android.com/reference/kotlin/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/kotlin/androidx/xr/scenecore/GltfAnimation.AnimationState#PLAYING()`.

### removeAnimationStateListener

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

```
fun removeAnimationStateListener(
    listener: Consumer<GltfAnimation.AnimationState>
): Unit
```

Unregisters a previously registered animation state update listener.

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

### resume

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

```
@MainThread
fun resume(): Unit
```

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/kotlin/androidx/xr/scenecore/GltfAnimation.AnimationState#PLAYING()`.

Note: Calling `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/GltfAnimation#resume()` while in the `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/GltfAnimation.AnimationState#PLAYING()` and `https://developer.android.com/reference/kotlin/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
fun [seekTo](https://developer.android.com/reference/kotlin/androidx/xr/scenecore/GltfAnimation#seekTo(java.time.Duration))(time: Duration): Unit
```

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

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

| Parameters |
|---|---|
| `time: https://developer.android.com/reference/java/time/Duration.html` | 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/kotlin/androidx/xr/scenecore/GltfAnimation#seekTo(java.time.Duration)` is negative. |

### start

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

```
@MainThread
fun start(): Unit
```

Starts playing this animation.

This transitions the animation state to `https://developer.android.com/reference/kotlin/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/kotlin/androidx/xr/scenecore/GltfAnimation#loop()` and `https://developer.android.com/reference/kotlin/androidx/xr/scenecore/GltfAnimation#speed()`. Calling `https://developer.android.com/reference/kotlin/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
fun [start](https://developer.android.com/reference/kotlin/androidx/xr/scenecore/GltfAnimation#start(androidx.xr.scenecore.GltfAnimationStartOptions))(options: GltfAnimationStartOptions): Unit
```

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

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

| Parameters |
|---|---|
| `options: https://developer.android.com/reference/kotlin/androidx/xr/scenecore/GltfAnimationStartOptions` | 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
fun stop(): Unit
```

Stops this animation.

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

## Public properties

### animationState

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

```
val animationState: GltfAnimation.AnimationState
```

The current state of this animation.

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

### duration

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

```
val duration: Duration
```

The duration of this animation.

### index

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

```
val index: Int
```

The index of this animation in the source glTF model.

### loop

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

```
var loop: Boolean
```

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/kotlin/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/kotlin/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/kotlin/androidx/xr/scenecore/ExperimentalGltfAnimationApi` until dynamic loop updates during active playback are supported.

### name

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

```
val name: String?
```

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

### speed

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

```
var speed: Float
```

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/kotlin/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/kotlin/androidx/xr/scenecore/GltfAnimation#loop()` (setting `loop` to `true`) to play the animation in reverse continuously.