# C API: 3D print export

Posed avatars as one watertight, manifold solid for a 3D printer: every part drawn is skinned as the last `sona_update` posed it and rebuilt from a distance field, so overlapping shells unite, inner faces and holes go, every part is at least `min_wall_mm` thick, and an optional base joins the feet. Millimetres, Z up, right-handed; an avatar at turn 0 faces -Y. CPU only, in the calling thread. The guide, with the pipeline, the options table and the independent checks, is [3D print export](../../guides/print3d.md).

`sona_print_options` (64 bytes): `{size, flags, height_mm, mm_per_metre, min_wall_mm, voxel_mm, close_mm, base_height_mm, base_margin_mm, base_colour, max_voxels, reserved[5]}`. Flags: `SONA_PRINT_BASE`, `SONA_PRINT_BASE_SQUARE`, `SONA_PRINT_NO_COLOUR`, `SONA_PRINT_LARGEST_ONLY`, `SONA_PRINT_NO_PROPS`. Formats: `SONA_PRINT_STL` (0), `SONA_PRINT_3MF` (1). `sona_print_mesh` (96 bytes, 84 with 4-byte pointers) and `sona_print_report` (112 bytes) carry a `size` the caller sets. `sona_print` is opaque.

## sona_print_defaults

```c
void sona_print_defaults(sona_print_options *o);
```

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

**Returns** Nothing.

**Example**:

```c
sona_print_options x_print_options;
memset(&x_print_options, 0, sizeof x_print_options);
x_print_options.size = sizeof x_print_options;
sona_print_defaults(&x_print_options);
```

`size` set, 120 mm tall, 1.2 mm walls, voxel from the wall, `close_mm` 0.6, a 3 mm base 4 mm round the feet in `0xff3c3c3c` (when `SONA_PRINT_BASE` is set), at most 24,000,000 grid points. NULL does nothing.

## sona_print_avatars

```c
sona_result sona_print_avatars(sona_avatar *const *avatars, uint32_t count, const sona_print_options *o, sona_print **out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `avatars` | `sona_avatar *const *` | The avatars, `count` of them | required |
| `count` | `uint32_t` | How many entries the array holds | - |
| `o` | `const sona_print_options *` | The options, a [`sona_print_options`](../fields.md#sona_print_options) | `NULL`: the defaults |
| `out` | `sona_print **` | Receives the new solid | required |

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

**Example** (also in [3D print export](../../guides/print3d.md)):

```c
sona_play_options pl = { 0, 1.0f, 1.25f };          /* no loop, speed 1, from 1.25 s */
sona_avatar_play(a, "GenericWave", &pl);
sona_update(ctx, 0, NULL, NULL);
sona_avatar_stop(a);
sona_update(ctx, 0, NULL, NULL);
sona_print_avatars(&a, 1, &opts, &print);
```

One solid from `count` READY avatars of one ctx, as posed by the last `sona_update`, in their placements. To print a clip frame: `sona_avatar_play` with `from` at the time, `sona_update`, `sona_avatar_stop`, `sona_update`. NULL `o`: the defaults. `SONA_E_NOT_READY`, `SONA_E_ARGUMENT` (NULL arguments, bad options, two ctxs), `SONA_E_NOT_FOUND` (nothing with area), `SONA_E_LIMIT` (no grid fits `max_voxels`), `SONA_E_MEMORY`.

## sona_print_triangles

```c
sona_result sona_print_triangles(sona_ctx *ctx, const float *positions, uint32_t vertex_count, const uint32_t *indices,
                                 uint32_t triangle_count, const uint32_t *colours, const sona_print_options *o,
                                 sona_print **out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ctx` | `sona_ctx *` | The context | `NULL` allowed (below) |
| `positions` | `const float *` | Three floats a vertex | required |
| `vertex_count` | `uint32_t` | How many vertices `positions` holds | - |
| `indices` | `const uint32_t *` | Three vertex indexes a triangle | required |
| `triangle_count` | `uint32_t` | How many triangles `indices` holds | - |
| `colours` | `const uint32_t *` | A colour per vertex, 0xAARRGGBB; NULL: none | `NULL`: none |
| `o` | `const sona_print_options *` | The options, a [`sona_print_options`](../fields.md#sona_print_options) | required |
| `out` | `sona_print **` | Receives the new solid | required |

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

The same from any triangles, in metres in the SDK's frame, any winding, colours 0xAARRGGBB per vertex or NULL. `ctx` may be NULL (malloc).

## sona_print_get

```c
sona_result sona_print_get(const sona_print *p, sona_print_mesh *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `p` | `const sona_print *` | The solid | required |
| `out` | `sona_print_mesh *` | Receives the [`sona_print_mesh`](../fields.md#sona_print_mesh) | required |

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

The solid: counts, shells, volume, area, positions, sRGB colours per vertex, indices (counter-clockwise from outside), bounds (`lo[2]` is 0, centred on x = y = 0) and the scale, wall and grid used. Pointers live as long as `p`. `SONA_E_ARGUMENT` when `out->size` is too small.

## sona_print_encode

```c
sona_result sona_print_encode(sona_print *p, uint32_t format, const uint8_t **out, size_t *out_len);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `p` | `sona_print *` | The solid | required |
| `format` | `uint32_t` | The file format to write | - |
| `out` | `const uint8_t **` | Receives the bytes; they belong to the solid | required |
| `out_len` | `size_t *` | Receives how many bytes `*out` holds | required |

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

Binary STL, or 3MF with a colour per vertex (the materials extension's colour group). The bytes live in `p` until the next encode or `sona_print_free`. Deterministic.

## sona_print_free

```c
void sona_print_free(sona_print *p);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `p` | `sona_print *` | The solid | required |

**Returns** Nothing.

NULL does nothing.

## sona_print_check

```c
sona_result sona_print_check(const float *positions, uint32_t vertex_count, const uint32_t *indices,
                             uint32_t triangle_count, sona_print_report *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `positions` | `const float *` | Three floats a vertex | required |
| `vertex_count` | `uint32_t` | How many vertices `positions` holds | - |
| `indices` | `const uint32_t *` | Three vertex indexes a triangle | required |
| `triangle_count` | `uint32_t` | How many triangles `indices` holds | - |
| `out` | `sona_print_report *` | Receives the [`sona_print_report`](../fields.md#sona_print_report) | required |

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

Checks any mesh: edges with one or more than two triangles, inconsistent winding, pinched vertices, degenerate and crossing triangles, shells, Euler characteristic, volume, area and bounds; `ok` is 1 when it would print as one closed solid per shell. Pure, no ctx.
