# Xbox 360 avatars in Python

To use Xbox 360 avatars in Python, use the `sona` package in the SDK download: open a `sona.Context` with your
licence, add the avatar from its manifest, then read its pose as plain data or render it with no window. It needs
Python 3.8 or newer.

![A Python script's run, and the avatar.png it saved: an avatar mid-wave](../img/one-tutorial-python.webp)

## Steps

1. Download the SDK for your platform ([getting it](../web-api.md#sdk-download)). It holds the `sona` package;
   set `SONA_LIB_DIR` if the package does not find the library on the system's path.
2. Open a `sona.Context` with your licence and a source: `("gate", None)` streams the item files from 360sona,
   `("dir", folder)` reads a folder of your own.
3. Add the avatar with `add_base64` and update until it has settled.
4. Play a clip, read the pose, or draw a picture with `sona.Headless`.

## Open a context and wait for the avatar

```python
with sona.Context(app_id=os.environ.get("SONA_APP_ID"), licence=licence,
                  sources=[source]) as ctx:
    avatar = ctx.add_base64(manifest)
    while not avatar.settled:
        ctx.update(1 / 60)
    if not avatar.ready:
        sys.exit("the avatar did not load")
```

## Read the pose

```python
avatar.play("GenericWave")
for _ in range(60):
    ctx.update(1 / 60)

pose = avatar.pose()
head = avatar.joint_world("Head")
print(len(pose.meshes), "meshes;", "head at", tuple(round(v, 3) for v in head[12:15]))
```

`joint_world` gives a row-vector matrix in metres; its last row holds the position. `pose` holds the meshes, the
materials, the textures and the 72-joint palette, valid until the next update.

## More

- [The Python tutorial](../tutorials/python.md) builds the whole script, which saves a picture of the wave.
- Every call of the binding is in the [bindings reference](../reference/bindings.md#python).
- Credit: show "Powered by 360sona" where your users see the avatars. `png_encode` puts the credit in a PNG's text
  chunks.
- 360sona is not affiliated with or endorsed by Microsoft.
