# Clips and animation

An avatar moves by playing clips on its own clock. A clip is a named animation of the avatar's skeleton and its
face: the clips 360sona.com's editor plays, by key (`GenericWave`, `Stand0`: `sona clips` lists them), the 100 clips
of the stock clip library by name (`wave`, `laugh_female`), the clips that come with a prop, and clips you import
from glTF, FBX or BVH. Every frame `sona_update` moves each avatar's clock by `dt` and poses it
from what it plays.

```sh
sona clips
sona render seed:vega -o wave.png --clip GenericWave --time 1.5 --size 512x640 --gate
```

`sona` is the SDK's command-line tool ([what it needs](sources.md)).

## Play, crossfade and queue

Play a clip and it starts at once; crossfade to it and the avatar blends from the pose it has into the new clip
over the seconds you give; queue it and it starts when the current clip ends.

| Call | What it does |
|---|---|
| `sona_avatar_play(a, clip, opts)` | Plays a clip now, and empties the queue |
| `sona_avatar_crossfade(a, clip, opts, seconds)` | Blends from what plays into the clip over `seconds` |
| `sona_avatar_queue(a, clip, opts, fade)` | Plays the clip when the current one ends, up to `SONA_QUEUE_MAX` (8) waiting |
| `sona_avatar_play_stock(a, id, opts, fade)` | A stock clip by its id, in the variant made for the avatar's body |

`sona_play_options` gives `loop` (default 1), `speed` (default 1) and `from`, seconds into the clip; `NULL` takes
those defaults. A clip asked for before the avatar is `READY` is remembered and starts when it appears. The names a
call takes are a clip key, a raw clip id, `"idle"`, `"still"` (no clip) or `"prop"` (the held prop's own clip).

## The stock clips

The stock clip library's 100 clips have a name, a constant (`SONA_STOCK_WAVE`), a family and tags: idle, emote, reaction, look,
travel and more ([the clip table](../guides/clips.md#the-clip-table)). Some come in a man's and a woman's
variant (`laugh_male`, `laugh_female`); `sona_avatar_play_stock` takes either and plays the one made for the body.
`sona_stock_clips_tagged` lists every clip with a tag, for a random emote or an idle picker.

The procedural idle lays blinks, glances, small weight shifts and breathing over whatever plays
(`sona_avatar_idle`), so a standing avatar never looks frozen.

## Layers, masks and state machines

Above the clip on its clock, an avatar can run layers: up to `SONA_LAYER_MAX` (8), each playing a clip or a blend
space on a mask of the body (`SONA_MASK_UPPER_BODY`, `SONA_MASK_LEFT_ARM`, ...), with a weight. A state machine of up
to `SONA_STATE_MAX` (32) states and `SONA_TRANSITION_MAX` (64) transitions picks a layer's clip from parameters your
game sets each frame, as an engine's animator does ([layers and states](../guides/layers.md)).

## After the clip

What the clip and its crossfades give is then changed by joint overrides your game sets (`sona_avatar_override`),
by the layers, and by inverse kinematics: look-at, hands on a target, feet on the ground ([IK](../guides/ik.md)).
Hair and loose clothes sway ([sway](../guides/sway.md)), and props are posed last, from the solved joints, so a
prop moves exactly with the hand that holds it ([props](../guides/props.md)).

The face is posed apart from the body: expressions, lip sync and face tracking each set its parts
([lip sync](../guides/lipsync.md), [face tracking](../guides/face-tracking.md)).

## Clip events and root motion

`sona_avatar_clip_events` lists what happened on an avatar's clip since the last call: started, looped, ended, a
left or right footfall, or a mark you set at a time in the clip (up to `SONA_CLIP_EVENT_MAX`, 32, are held). Clips
that travel (`walk_forward`, `run_forward`) move the root: `sona_avatar_root_motion` with
`SONA_ROOT_MOTION_EXTRACT` keeps the avatar in place and collects the travel for `sona_avatar_take_root_motion`, so
your game moves it, and `SONA_ROOT_MOTION_APPLY` moves it for you.

## Your own clips

glTF, FBX and BVH animations are retargeted onto the avatar's skeleton at import, by bone name, with maps for Unity
Humanoid, VRM, the Unreal mannequins and Mixamo ([imported clips](../guides/import.md),
[skeleton maps](../guides/skeleton-maps.md)).

## Related

- [Clips: the stock clip library](../guides/clips.md): every stock clip and the procedural idle.
- [Animation layers and states](../guides/layers.md).
- [Two-avatar interactions](../guides/interactions.md): handshakes, hugs, high fives, sitting.
