# FBX, Roblox and clips in the export

`sona_export_avatar` writes an avatar as a file for another tool: glTF, VRM 1.0, VRM 0.x, FBX, and an FBX laid out for
Roblox. This page covers the two FBX formats and exporting the avatar's clips with the figure; [VRM and glTF](vrm.md)
covers the rest. An FBX with the avatar's clips:

```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);
}
```

```cpp
sona_export_options o;
sona_export_defaults(&o);
o.format = SONA_EXPORT_FBX;
o.flags = SONA_EXPORT_CLIPS;               // the clip it plays, as an animation on the skeleton
uint8_t *fbx = nullptr;
size_t fbx_len = 0;
if (sona_export_avatar(avatar.raw(), &o, &fbx, &fbx_len, nullptr) == SONA_OK) {
  std::ofstream("avatar.fbx", std::ios::binary).write(reinterpret_cast<const char *>(fbx), (std::streamsize)fbx_len);
  sona_free(ctx.raw(), fbx);
}
```

```csharp
// the clip it plays, as an animation on the skeleton
var o = new SonaExportOptions();
o.Format = (SonaExportFormat)Native.SONA_EXPORT_FBX;
o.Flags = (SonaExportFlags)Native.SONA_EXPORT_CLIPS;
if (avatar.TryExport(o, out var fbx, out var report)) File.WriteAllBytes("avatar.fbx", fbx);
```

```rust
let o = ExportOptions { format: sona_sys::SONA_EXPORT_FBX, flags: sona_sys::SONA_EXPORT_CLIPS, ..Default::default() };
let (fbx, _report) = avatar.export(&o)?;      // the clip it plays, as an animation on the skeleton
std::fs::write("avatar.fbx", fbx).ok();
```

```python
from sona._native import SONA_EXPORT_CLIPS

fbx, report = avatar.export(format="fbx", flags=SONA_EXPORT_CLIPS)   # the clip it plays, as an animation
open("avatar.fbx", "wb").write(fbx)
```

The avatar must be `SONA_AVATAR_READY` (`SONA_E_NOT_READY` before). The formats:

| Format | What |
|---|---|
| `SONA_EXPORT_FBX` | binary FBX 7.4: the same avatar as the glTF, skinned, with its pictures inside, for engines and DCC tools |
| `SONA_EXPORT_ROBLOX` | an FBX for Roblox Studio's avatar import: R15 part names, a T-pose, no props, no clips, accessories left off |

and one flag:

| Flag | What |
|---|---|
| `SONA_EXPORT_CLIPS` | GLB and FBX: the clips the avatar plays as animations on the skeleton (refused with VRM) |

## The FBX

The FBX holds the same figure as the GLB: Blender's binary encoding, one Model per node
(bones as LimbNodes, with a skeleton attribute), a Video and a Texture per picture with the PNG inside, phong
materials, one Geometry per mesh with its normals and uvs and a material per polygon, a Skin with a Cluster per bone,
a BindPose, and per clip an AnimationStack with curves for each moved bone. Metres, Y up, facing +Z, 100 centimetres
to the unit. The creator and the scene info carry "Powered by 360sona". Arrays are not compressed. A library built
without the glTF clip importer answers `SONA_E_UNSUPPORTED` for FBX and Roblox.

## Roblox

`SONA_EXPORT_ROBLOX` is the web editor's Roblox preset as an FBX: the T-pose, no prop, no clips, and hair, hats,
glasses, earrings, wristwear and rings left off (Roblox's accessories, fitted in Studio with the Accessory Fitting
Tool). The bones carrying R15's parts take their names (`LowerTorso` the hips, `UpperTorso` the chest, `Head`,
`LeftUpperArm`, `LeftLowerArm`, `LeftHand`, `LeftUpperLeg`, `LeftLowerLeg`, `LeftFoot` and the right ones) under a
`HumanoidRootPart` root; the others keep their Unity names. Xbox 360 avatars' faces are painted on, with no eye or
mouth geometry, so Studio's Avatar Auto Setup cannot build Dynamic Head expressions from it.

## Clips

With `SONA_EXPORT_CLIPS` every clip the avatar plays goes in: its own clip, then the clip of each layer that plays
one (a layer at weight 0 counts, so a host loads clips there only to export them). Each is sampled at its own frame
rate: every bone's turn from its parent (the file's bones have no rest rotation, so that is its rotation) and the
hips' place, as LINEAR glTF channels, named by the clip's key (a stock clip by its library name). Bones a clip never
turns get no channel.
