# C API: Imported clips

Your own glTF, FBX and BVH clips, retargeted onto the avatar rig and played by key like stock clips. The guide, with
the naming table and the retarget, is [Bring your own animation](../../guides/import.md). Structs:
`sona_import_options`, `sona_import_info`, `sona_bone_map`, and the opaque `sona_anim_file`.

## sona_import_defaults

```c
void sona_import_defaults(sona_import_options *o);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `o` | `sona_import_options *` | The [`sona_import_options`](../fields.md#sona_import_options) to fill with the defaults | required |

**Returns** Nothing.

**Example** (also in [Bring your own animation](../../guides/import.md)):

```c
sona_import_options o;
sona_import_info info;
sona_anim_file *f;
char why[256];

sona_import_defaults(&o);
o.key = "wave_big";                                  /* NULL: the take's own name */
if (sona_anim_file_open("wave.fbx", &o, &f, why, sizeof why) != SONA_OK) { puts(why); return; }
while (sona_clip_import(ctx, f, &o, &info) == SONA_PENDING)   /* waits for the rig data, read through the sources */
  sona_update(ctx, 1.0 / 60, &view, NULL);
sona_anim_file_free(f);                              /* the clip is kept; the file is not needed any more */

sona_avatar_crossfade(avatar, "wave_big", NULL, 0.25f);
```

Zeroes the options and sets `size`.

## sona_import_formats

```c
uint32_t sona_import_formats(void);
```

**Returns** `uint32_t`.

**Example**:

```c
uint32_t x_got = sona_import_formats();
printf("sona_import_formats: %lld\n", (long long)x_got);
```

`1 << SONA_ANIM_GLTF`, `_FBX` and `_BVH` for each format this build reads.

## sona_anim_file_load

```c
sona_result sona_anim_file_load(const void *bytes, size_t len, const sona_import_options *opts, sona_anim_file **out,
                                char *why, size_t why_cap);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `bytes` | `const void *` | The file's bytes | required |
| `len` | `size_t` | How many bytes there are | - |
| `opts` | `const sona_import_options *` | The options, a [`sona_import_options`](../fields.md#sona_import_options) | `NULL` allowed (below) |
| `out` | `sona_anim_file **` | Receives the new animation file | required |
| `why` | `char *` | Receives what was refused, NUL-terminated; may be NULL | `NULL`: not written |
| `why_cap` | `size_t` | How many bytes `why` holds | - |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_UNSUPPORTED`, `SONA_E_LIMIT`, `SONA_E_MEMORY`, `SONA_E_CORRUPT` ([what each means](../../troubleshooting.md#error-codes)).

Parses a clip file in memory, no ctx needed. `opts` may be NULL; its `format`, `map` and `flags` are used (the
mapping `sona_anim_file_bone` reports). `SONA_E_CORRUPT`, `SONA_E_UNSUPPORTED` or `SONA_E_LIMIT` with the reason in
`why`. A `.gltf` whose buffers are other files needs `sona_anim_file_open`.

## sona_anim_file_open

```c
sona_result sona_anim_file_open(const char *path, const sona_import_options *opts, sona_anim_file **out,
                                char *why, size_t why_cap);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `path` | `const char *` | A file path | required |
| `opts` | `const sona_import_options *` | The options, a [`sona_import_options`](../fields.md#sona_import_options) | required |
| `out` | `sona_anim_file **` | Receives the new animation file | required |
| `why` | `char *` | Receives what was refused, NUL-terminated; may be NULL | `NULL`: not written |
| `why_cap` | `size_t` | How many bytes `why` holds | - |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_UNSUPPORTED`, `SONA_E_LIMIT`, `SONA_E_MEMORY`, `SONA_E_IO`, `SONA_E_CORRUPT` ([what each means](../../troubleshooting.md#error-codes)).

**Example** (also in [Bring your own animation](../../guides/import.md)):

```c
sona_import_options o;
sona_import_info info;
sona_anim_file *f;
char why[256];

sona_import_defaults(&o);
o.key = "wave_big";                                  /* NULL: the take's own name */
if (sona_anim_file_open("wave.fbx", &o, &f, why, sizeof why) != SONA_OK) { puts(why); return; }
while (sona_clip_import(ctx, f, &o, &info) == SONA_PENDING)   /* waits for the rig data, read through the sources */
  sona_update(ctx, 1.0 / 60, &view, NULL);
sona_anim_file_free(f);                              /* the clip is kept; the file is not needed any more */

sona_avatar_crossfade(avatar, "wave_big", NULL, 0.25f);
```

The same from a path (`SONA_E_IO` when it cannot be read). A BVH take is named after the file.

## sona_anim_file_free

```c
void sona_anim_file_free(sona_anim_file *f);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `f` | `sona_anim_file *` | The animation file | required |

**Returns** Nothing.

**Example** (also in [Bring your own animation](../../guides/import.md)):

```c
sona_import_options o;
sona_import_info info;
sona_anim_file *f;
char why[256];

sona_import_defaults(&o);
o.key = "wave_big";                                  /* NULL: the take's own name */
if (sona_anim_file_open("wave.fbx", &o, &f, why, sizeof why) != SONA_OK) { puts(why); return; }
while (sona_clip_import(ctx, f, &o, &info) == SONA_PENDING)   /* waits for the rig data, read through the sources */
  sona_update(ctx, 1.0 / 60, &view, NULL);
sona_anim_file_free(f);                              /* the clip is kept; the file is not needed any more */

sona_avatar_crossfade(avatar, "wave_big", NULL, 0.25f);
```

Imported clips do not need the file once `sona_clip_import` returned.

## sona_anim_file_takes

```c
uint32_t sona_anim_file_takes(const sona_anim_file *f);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `f` | `const sona_anim_file *` | The animation file | required |

**Returns** `uint32_t`.

How many takes (animations) the file holds.

## sona_anim_file_take

```c
const char *sona_anim_file_take(const sona_anim_file *f, uint32_t index, float *seconds);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `f` | `const sona_anim_file *` | The animation file | required |
| `index` | `uint32_t` | Which one, from 0 | - |
| `seconds` | `float *` | Receives the take's length, seconds | required |

**Returns** `const char *`.

A take's name and length; `NULL` past the last.

## sona_anim_file_bones

```c
uint32_t sona_anim_file_bones(const sona_anim_file *f);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `f` | `const sona_anim_file *` | The animation file | required |

**Returns** `uint32_t`.

How many bones the file's skeleton has.

## sona_anim_file_bone

```c
const char *sona_anim_file_bone(const sona_anim_file *f, uint32_t index, int32_t *parent, int32_t *joint);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `f` | `const sona_anim_file *` | The animation file | required |
| `index` | `uint32_t` | Which one, from 0 | - |
| `parent` | `int32_t *` | Receives the parent bone's index, -1 for a root | required |
| `joint` | `int32_t *` | Receives the rig joint the bone drives, -1 for none | required |

**Returns** `const char *`.

A bone's name, its parent (a lower index, or -1) and the avatar joint it drives (-1 none). `NULL` past the last.

## sona_clip_import

```c
sona_result sona_clip_import(sona_ctx *ctx, const sona_anim_file *f, const sona_import_options *opts,
                             sona_import_info *info);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `sona_ctx *` | The context | required |
| `f` | `const sona_anim_file *` | The animation file | required |
| `opts` | `const sona_import_options *` | The options, a [`sona_import_options`](../fields.md#sona_import_options) | required |
| `info` | `sona_import_info *` | Receives what the file holds; may be NULL | `NULL`: not written |

**Returns** `SONA_OK`, `SONA_PENDING` while it is still working. **Errors** `SONA_E_ARGUMENT`, `SONA_E_NOT_FOUND`, `SONA_E_LIMIT`, `SONA_E_MEMORY`, `SONA_E_GATE` ([what each means](../../troubleshooting.md#error-codes)).

**Example** (also in [Bring your own animation](../../guides/import.md)):

```c
sona_import_options o;
sona_import_info info;
sona_anim_file *f;
char why[256];

sona_import_defaults(&o);
o.key = "wave_big";                                  /* NULL: the take's own name */
if (sona_anim_file_open("wave.fbx", &o, &f, why, sizeof why) != SONA_OK) { puts(why); return; }
while (sona_clip_import(ctx, f, &o, &info) == SONA_PENDING)   /* waits for the rig data, read through the sources */
  sona_update(ctx, 1.0 / 60, &view, NULL);
sona_anim_file_free(f);                              /* the clip is kept; the file is not needed any more */

sona_avatar_crossfade(avatar, "wave_big", NULL, 0.25f);
```

Retargets one take and keeps it under its key (replacing an imported clip of that key). `SONA_PENDING` until the
rig data has been read: call again after `sona_update`. `SONA_E_ARGUMENT` for a stock key, an id, an unknown take or
a map entry naming no joint; `SONA_E_NOT_FOUND` when no bone matches a joint or the file has no animation;
`SONA_E_LIMIT` past 1024 clips. `sona_last_error` has the reason.

## sona_clip_imported_count

```c
uint32_t sona_clip_imported_count(const sona_ctx *ctx);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `const sona_ctx *` | The context | required |

**Returns** `uint32_t`.

**Example**:

```c
uint32_t x_got = sona_clip_imported_count(ctx);
printf("sona_clip_imported_count: %lld\n", (long long)x_got);
```

How many clips have been imported into the ctx.

## sona_clip_imported_get

```c
sona_result sona_clip_imported_get(const sona_ctx *ctx, uint32_t index, sona_import_info *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `const sona_ctx *` | The context | required |
| `index` | `uint32_t` | Which one, from 0 | - |
| `out` | `sona_import_info *` | Receives the [`sona_import_info`](../fields.md#sona_import_info) | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_NOT_FOUND` ([what each means](../../troubleshooting.md#error-codes)).

**Example**:

```c
sona_import_info x_import_info;
memset(&x_import_info, 0, sizeof x_import_info);
x_import_info.size = sizeof x_import_info;
sona_result result = sona_clip_imported_get(ctx, 0, &x_import_info);
if (result != SONA_OK) printf("sona_clip_imported_get: %s\n", sona_result_name(result));
```

`SONA_E_NOT_FOUND` past the last.

## sona_clip_imported_find

```c
sona_result sona_clip_imported_find(const sona_ctx *ctx, const char *key, sona_import_info *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `const sona_ctx *` | The context | required |
| `key` | `const char *` | The key, as it was given when made | required |
| `out` | `sona_import_info *` | Receives the [`sona_import_info`](../fields.md#sona_import_info) | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_NOT_FOUND` ([what each means](../../troubleshooting.md#error-codes)).

**Example**:

```c
sona_import_info x_import_info;
memset(&x_import_info, 0, sizeof x_import_info);
x_import_info.size = sizeof x_import_info;
sona_result result = sona_clip_imported_find(ctx, "GenericWave", &x_import_info);
if (result != SONA_OK) printf("sona_clip_imported_find: %s\n", sona_result_name(result));
```

The imported clip with this key; `SONA_E_NOT_FOUND` when there is none.

## sona_clip_imported_remove

```c
sona_result sona_clip_imported_remove(sona_ctx *ctx, const char *key);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `sona_ctx *` | The context | required |
| `key` | `const char *` | The key, as it was given when made | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_NOT_FOUND` ([what each means](../../troubleshooting.md#error-codes)).

**Example**:

```c
sona_result result = sona_clip_imported_remove(ctx, "GenericWave");
if (result != SONA_OK) printf("sona_clip_imported_remove: %s\n", sona_result_name(result));
```

Avatars playing it finish with their own reference; new plays of the key fail with `SONA_E_ARGUMENT`.
