Phase 4 — Advanced
Phase 3 gave you the tools to build scripts that interact with users, process text, and produce output. Phase 4 goes deeper — into the capabilities of the SDK that require more careful handling, reward more careful thinking, and unlock the most powerful automation possibilities the platform offers.
This phase covers seven areas that sit at the edge of what scripts can do: AI-powered image operations, layer effects and adjustments, production-grade async patterns, a survey of the SDK modules, direct pixel manipulation, programmatic font and typography control, and connecting to Affinity from external programs via MCP. None of these are prerequisites for writing useful scripts — you could spend months working productively with only Phases 1 through 3. But each lesson here opens a category of work that simply is not possible without it.
The AI commands lesson is particularly significant. Affinity's generative capabilities — image generation, inpainting, background removal, depth mapping, colourisation — are available directly from JavaScript without any external API keys, quotas, or network dependencies. Scripts can drive them programmatically, chain them with export pipelines, and present them through dialogs in ways the standard UI does not support. Note: AI commands require a Canva account with an active Pro subscription — they are not available on free accounts.
Pixel processing is the other lesson that unlocks a qualitatively different class of scripts. Once you have direct read/write access to individual pixels, any image processing algorithm you can express mathematically becomes implementable: custom colour grading, histogram analysis, procedural texture generation, per-channel adjustments, and effects that depend on spatial relationships between pixels.
The pro patterns lesson ties the phase together — it covers the async techniques, error handling, and external communication that production scripts need when they move from personal experiments to tools used by a team.
Lessons
| Lesson | Topic | Key skill you'll gain |
|---|---|---|
| 11 — AI commands | generateImage, removeBackground, selectSubject | Driving Affinity's AI features from scripts |
| 12 — Layer effects | Shadows, glows, adjustments, filters | Applying and batching visual effects across many nodes |
| 13 — Pro patterns | Async pipelines, networking, timers, error handling | Writing scripts that are robust enough for production use |
| 14 — Special modules | Pixel access, fonts, brushes, buffers, metadata | Using the full breadth of the SDK's module library |
| 15 — Pixel processing | PixelReaderWriterRGBA8, Bitmap, NodeRenderingEngine | Direct per-pixel read and write on any raster layer |
| 16 — Fonts & typography | FontFamily, PANOSE, font distance, typography automation | Inspecting, classifying, and applying fonts programmatically |
| 17 — Raster adjustments | BrightnessContrast, Levels, Curves, HSL, all 19 types | Creating and modifying non-destructive adjustment layers |
| 18 — Align & distribute | GroupTransformData, GroupTransformType, GroupTransformAnchor | Aligning and distributing nodes on any axis programmatically |
What to expect
By the end of Phase 4 you should be able to write a script that:
- Generates an image from a text prompt and exports it in one pipeline
- Applies a consistent set of layer effects across every node in a document
- Processes every pixel of a raster layer to implement a custom image filter
- Enumerates all installed fonts, classifies them by type, and finds the closest substitute for a missing typeface
- Handles save failures on unsaved documents, network errors, and export failures gracefully
- Chains multiple async operations — execute, export, save — without blocking the UI
- Connects to Affinity from an external program via MCP and executes scripts remotely
You will also encounter the SDK's hardest-won lessons in this phase: that LineType.Dash is required before dashes render, that mustSaveAs must be checked before any save operation, and that pixel alpha is always px.alpha never px.a. These are not intuitive, they do not produce helpful error messages, and they are documented here precisely because they cost time to discover independently.