# C API: Skeleton maps and VRM export

The guides are [VRM and glTF export](../../guides/vrm.md) (the file, the flattening, the expressions, what was checked)
and [Skeleton maps](../../guides/skeleton-maps.md) (the five tables). New structs: `sona_vrm_face`,
`sona_export_options` and `sona_export_report`; the last two start with a `size` field, so a later version can grow
them. `sona_struct_size` and `sona_struct_offset` know all three.

## sona_skeleton_bone

```c
const char *sona_skeleton_bone(uint32_t skeleton, uint32_t joint);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `skeleton` | `uint32_t` | A `sona_skeleton_kind` | - |
| `joint` | `uint32_t` | A joint index, from `sona_joint_find` | - |

**Returns** `const char *`.

**Example** (also in [Skeleton maps: Unity Humanoid, VRM and the Unreal mannequins](../../guides/skeleton-maps.md)):

```c
const char *sona_skeleton_bone(uint32_t skeleton, uint32_t joint);   /* NULL: that skeleton has no such bone */
int32_t sona_skeleton_joint(uint32_t skeleton, const char *bone);    /* -1: none; names match exactly */
```

The bone a joint is in a `sona_skeleton_kind` (`SONA_SKELETON_UNITY`, `_VRM`, `_VRM0`, `_UE4`, `_UE5`). `NULL` for a joint
that skeleton has no bone for (the shape, prop and special joints), past the last joint or for an unknown
skeleton. A static string; any thread.

## sona_skeleton_joint

```c
int32_t sona_skeleton_joint(uint32_t skeleton, const char *bone);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `skeleton` | `uint32_t` | A `sona_skeleton_kind` | - |
| `bone` | `const char *` | A bone name of that skeleton | required |

**Returns** `int32_t`.

**Example** (also in [Skeleton maps: Unity Humanoid, VRM and the Unreal mannequins](../../guides/skeleton-maps.md)):

```c
const char *sona_skeleton_bone(uint32_t skeleton, uint32_t joint);   /* NULL: that skeleton has no such bone */
int32_t sona_skeleton_joint(uint32_t skeleton, const char *bone);    /* -1: none; names match exactly */
```

The joint a bone of that skeleton is, matched exactly; -1 when none.

## sona_vrm_expression_name

```c
const char *sona_vrm_expression_name(uint32_t expression);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `expression` | `uint32_t` | A VRM expression index | - |

**Returns** `const char *`.

**Example**:

```c
const char * x_got = sona_vrm_expression_name(1);
printf("sona_vrm_expression_name: %s\n", x_got ? "a value" : "none");
```

The VRM 1.0 preset name of a `sona_vrm_expression` (`"aa"` ... `"lookRight"`); `NULL` past the last.

## sona_vrm_face_default

```c
sona_result sona_vrm_face_default(uint32_t expression, sona_vrm_face *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `expression` | `uint32_t` | A VRM expression index | - |
| `out` | `sona_vrm_face *` | Receives the [`sona_vrm_face`](../fields.md#sona_vrm_face) | required |

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

**Example**:

```c
sona_vrm_face x_vrm_face;
memset(&x_vrm_face, 0, sizeof x_vrm_face);
sona_result result = sona_vrm_face_default(1, &x_vrm_face);
if (result != SONA_OK) printf("sona_vrm_face_default: %s\n", sona_result_name(result));
```

The face an expression shows by default: a shape index per face part, in `sona_avatar_expression`'s order with
left meaning the avatar's own left, 255 for a part it leaves alone. `SONA_E_ARGUMENT` past the last expression.

## sona_export_defaults

```c
void sona_export_defaults(sona_export_options *o);
```

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

**Returns** Nothing.

**Example** (also in [FBX, Roblox and clips in the export](../../guides/export-fbx.md)):

```c
sona_export_options o;
uint8_t *fbx = NULL;
size_t fbx_len = 0;
sona_export_defaults(&o);
o.format = SONA_EXPORT_FBX;
o.flags = SONA_EXPORT_CLIPS;          /* the clip it plays, as an animation on the skeleton */
if (sona_export_avatar(avatar, &o, &fbx, &fbx_len, NULL) == SONA_OK) {
  FILE *f = fopen("avatar.fbx", "wb");
  if (f) { fwrite(fbx, 1, fbx_len, f); fclose(f); }
  sona_free(ctx, fbx);
}
```

Sets `size`, VRM 1.0, Unity bone names, MToon, no flags, every string `NULL` and the most restrictive licence.

## sona_export_avatar

```c
sona_result sona_export_avatar(sona_avatar *a, const sona_export_options *o, uint8_t **out, size_t *out_len,
                               sona_export_report *report);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `o` | `const sona_export_options *` | The options, a [`sona_export_options`](../fields.md#sona_export_options) | `NULL`: the defaults |
| `out` | `uint8_t **` | Receives the bytes; free them with `sona_free` | required |
| `out_len` | `size_t *` | Receives how many bytes `*out` holds | required |
| `report` | `sona_export_report *` | Receives what the call did; may be NULL | `NULL`: not written |

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

**Example** (also in [FBX, Roblox and clips in the export](../../guides/export-fbx.md)):

```c
sona_export_options o;
uint8_t *fbx = NULL;
size_t fbx_len = 0;
sona_export_defaults(&o);
o.format = SONA_EXPORT_FBX;
o.flags = SONA_EXPORT_CLIPS;          /* the clip it plays, as an animation on the skeleton */
if (sona_export_avatar(avatar, &o, &fbx, &fbx_len, NULL) == SONA_OK) {
  FILE *f = fopen("avatar.fbx", "wb");
  if (f) { fwrite(fbx, 1, fbx_len, f); fclose(f); }
  sona_free(ctx, fbx);
}
```

The avatar as one binary glTF, VRM 1.0 or VRM 0.x file in `*out` (free with `sona_free`). `o` `NULL`: the defaults;
fields past `o->size` take their defaults. `report` may be `NULL`; when given, its `size` must be set, and only that
much is written. Runs its slow parts on the ctx's workers; call it from the thread that calls `sona_update`.
`SONA_E_NOT_READY` before READY, `SONA_E_DESTROYED`, `SONA_E_ARGUMENT` for a bad format, bone names, face table or
a `size` of 0, `SONA_E_MEMORY`.
