# Export and download Model assets

Use this workflow when the user requests an exported/downloadable Sprite Sheet or WebM. Export first saves local browser edits and works in every Model status without changing visibility or locking edits. Owners can export repeatedly for free and continue editing. Clone is an optional independent copy using one monthly Model slot. Use existing user authorization for Export; never change visibility as part of preparation.

## Prepare and download

1. Resolve the requested Model, directions, Animations and format. Read the Model and `/animations` to confirm the requested clips exist and use their saved dimensions, FPS and `frameCount`.
2. Through the authenticated `pixat-auth.ps1 -Action request` helper, GET `/api/models/{id}/exports/{direction}/{animation}`. This returns ready `files` (filename, URL, MIME type, bytes, SHA-256), and `prepareUrls.sprite` / `prepareUrls.webm`.
3. If the required files already exist as the latest stored export, download them directly. Otherwise open the exact relevant `prepareUrls` value on the same PIXAT origin in the browser. The owner must be signed in there. The Studio Agent page automatically renders once and stores its output, showing download links. Keep the page open until it finishes. API GET alone does not render files; rendering runs in the browser using the same renderer as Studio.
4. Poll the manifest at a modest interval while preparation runs, for at most 60 seconds per direction/clip. Sprite preparation is complete only when both `sprite.png` and `sprite.json` appear; WebM requires `animation.webm`. On timeout, missing sign-in or a browser error, report the concrete blocker; do not keep navigating or retrying indefinitely.
5. Download each returned same-origin `/api/` file URL with the helper and an absolute `-OutputFile`. Use it for JSON too, to preserve exact bytes. Do not print binary responses, credentials, or Bearer tokens. Create the destination directory first.
6. Report actual saved file paths and the selected direction/clip, frame dimensions, count and FPS. Only claim files are ready after successful downloads. The manifest's bytes/SHA-256 can be used for integrity verification when requested. For multiple directions/clips, prepare and download each pair separately.

## Full Export Sprite · ZIP

Use **Full Export Sprite** when the user requests all directions and all Parts, or a complete Sprite bundle. In the signed-in owner's `/studio-agent/{modelId}`, click the grid/download icon titled **Full Export Sprite** in the header, or **Full Export Sprite · ZIP** in the Export dialog. This action ignores the direction/Animation checkboxes and creates one local download:

```text
pixat-full-sprite-{modelId}.zip
  manifest.json
  right/parts/{partId}.png
  right/animations/bind/sprite.png
  right/animations/bind/sprite.json
  right/animations/{animation}/sprite.png
  right/animations/{animation}/sprite.json
  ...each other mapped direction...
```

- Include every direction with mapped Parts and every Animation available in that direction, including the one-frame Bind pose. Skip directions without mapped Parts and list them in `manifest.json`; never invent missing directions or clips. Each clip's Sprite metadata gives its frame dimensions, layout, FPS and actual frameCount.
- Part PNGs are the intact transparent source pieces at original crop dimensions, before animation deformation. Legacy v1 uses its loaded Part images; atlas-only v2 reconstructs them from the exact rect and optional RLE mask in the client renderer. V2 export does not store cropped images on the server.
- `manifest.json` identifies the Model and each direction's Part IDs, relative filenames, dimensions, atlas rects, pivots and offsets, plus Animation file paths. Part/Animation folder names use URI encoding for safe filenames. It is export metadata, not a full editable Model backup.
- Preserve the preview direction/clip and dialog selections. Save local browser edits before rendering; the manifest has draft: false and the saved Model data. The ZIP is an asset bundle, while the source Model remains editable. Draft/Public/Private Models can be exported repeatedly.
- ZIP packing and Sprite rendering run in the browser from cached images, without revision polling or server rendering. Pause playback and editing until completion. Only the owner can use this action. The ZIP contains PNG + JSON, not WebM.
- The archive is limited to 256 MiB. If any required Part fails to load, an export fails, or the limit is exceeded, report the error and use the existing selected-direction export for a smaller bundle; do not claim a complete ZIP. A failed batch does not download a partial ZIP.
- On success the page downloads one ZIP and shows **ดาวน์โหลด Full Export Sprite · ZIP** as a fallback if automatic download is blocked. This is a browser-local Blob link, not an authenticated server artifact URL and not usable by the API helper. The link lasts in that page until another export replaces it or the page closes. For API-downloadable individual Sprite files, continue using the manifest/preparation workflow above. A local CLI/headless Export runner has not been implemented; do not claim the API can generate this ZIP without a browser.

Example download (use the manifest's actual file URL):

```powershell
& '<skill-directory>/scripts/pixat-auth.ps1' -Action request -Origin $pixatOrigin -Path $fileUrl -OutputFile 'D:/output/run-right.png'
```

## URL contract

- Manifest: `GET /api/models/{id}/exports/{direction}/{animation}`.
- Files: `GET /api/models/{id}/exports/{direction}/{animation}/sprite.png`, `sprite.json`, or `animation.webm`. File GET uses account/device authentication and `Content-Disposition: attachment`; these are not public share links.
- Preparation: `/studio-agent/{id}?direction=right&animation=run&export=sprite`. Use `export=webm` for WebM. Prefer the returned `prepareUrls` over building URLs manually. No secrets belong in these URLs.
- `404 export_not_ready`: no stored artifact for this direction/clip; prepare it in the browser.
- The browser stores generated output using authenticated `PUT` to the file endpoint with raw bytes, matching MIME type, CSRF (session only). No revision is required or stored. PNG/WebM limit: 64 MiB; metadata: 64 KiB.
- Export cache is owner-only, private storage outside the web root, separate per Model/direction/clip. No permission or publication settings change. The latest generated output replaces these files; regenerate explicitly after editing the Model. Legacy revision query parameters are ignored.
