Motion Canvas Agent Plugin
Full programmatic control of a running Motion Canvas editor via HTTP. Seek to any frame, capture screenshots, inspect the scene graph, change resolution/fps/background, and trigger rendering — all without browser automation.
Architecture
Agent (curl/script) ──HTTP──▶ Vite Server (agent-plugin.ts) ──HMR WS──▶ Browser (agent-client.ts)
│ │
▼ ▼
screenshots/ Player + Renderer
(PNG files) + ProjectMetadata
Setup
1. Copy plugin files into your project
assets/agent-plugin.ts → project root
assets/agent-client.ts → src/
2. vite.config.ts
import {defineConfig} from 'vite';
import motionCanvas from '@motion-canvas/vite-plugin';
import {agentPlugin} from './agent-plugin';
export default defineConfig({
plugins: [
motionCanvas(),
agentPlugin({screenshotDir: './screenshots'}),
],
});
3. project.ts
import {makeProject} from '@motion-canvas/core';
import {agentClient} from './agent-client';
export default makeProject({
plugins: [agentClient()],
scenes: [...],
});
4. Start + open browser
npm start
# Open http://localhost:9000 in a browser
HTTP API Reference
Base URL: http://localhost:9000/__agent
Playback Control
| Method |
Endpoint |
Body |
Response |
GET |
/status |
— |
{connected, frame, duration, fps, paused, sceneName, errors} |
POST |
/seek |
{frame: 120} |
{ok, frame} |
POST |
/next-frame |
— |
{ok, frame} |
POST |
/prev-frame |
— |
{ok, frame} |
POST |
/play |
— |
{ok} |
POST |
/pause |
— |
{ok} |
Screenshots
| Method |
Endpoint |
Body |
Response |
POST |
/screenshot |
{name: "my-shot"} |
{ok, path, frame} |
POST |
/screenshot-base64 |
— |
{ok, data, frame} |
Settings
| Method |
Endpoint |
Body |
Response |
GET |
/settings |
— |
{background, range, size, previewFps, previewScale, renderingFps, renderingScale} |
POST |
/settings/background |
{color: "#0F172A"} |
{ok} |
POST |
/settings/range |
{start: 0, end: 300} |
{ok} |
POST |
/settings/size |
{width: 1920, height: 1080} |
{ok} |
POST |
/settings/preview-fps |
{fps: 30} |
{ok} |
POST |
/settings/preview-scale |
{scale: 0.5} |
{ok} |
POST |
/settings/rendering-fps |
{fps: 60} |
{ok} |
POST |
/settings/rendering-scale |
{scale: 2} |
{ok} |
Rendering
| Method |
Endpoint |
Body |
Response |
POST |
/render |
{fps?, range?} |
{ok, message} |
POST |
/render/abort |
— |
{ok, message} |
Scene Info
| Method |
Endpoint |
Body |
Response |
GET |
/scenes |
— |
{scenes: [{name}], currentScene} |
GET |
/scene-graph |
— |
{scene, graph} (recursive node tree) |
GET |
/threads |
— |
{scene, slides: [{name, id}]} |
Errors
| Method |
Endpoint |
Body |
Response |
GET |
/errors |
— |
{errors: [{message, stack}]} |
POST |
/clear-errors |
— |
{ok} |
Scene Graph Format
GET /__agent/scene-graph returns a recursive tree:
{
"scene": "my-scene",
"graph": {
"type": "View2D",
"key": "my-scene/View2D[1]",
"position": {"x": 960, "y": 540},
"size": {"width": 1920, "height": 1080},
"children": [
{
"type": "Rect",
"position": {"x": 0, "y": 0},
"size": {"width": 400, "height": 300},
"fill": "#1E293B",
"children": [
{
"type": "Txt",
"text": "Hello World"
}
]
}
]
}
}
Each node includes: type, key, position, size, opacity (if not 1), fill, text (if applicable), and children.
AI Agent Workflow
# 1. Check connection
curl -s localhost:9000/__agent/status
# 2. Set resolution
curl -s -X POST localhost:9000/__agent/settings/size \
-H "Content-Type: application/json" -d '{"width": 1920, "height": 1080}'
# 3. Seek to a frame
curl -s -X POST localhost:9000/__agent/seek \
-H "Content-Type: application/json" -d '{"frame": 60}'
# 4. Screenshot
curl -s -X POST localhost:9000/__agent/screenshot \
-H "Content-Type: application/json" -d '{"name": "check-1"}'
# → Read ./screenshots/check-1.png
# 5. Inspect scene graph
curl -s localhost:9000/__agent/scene-graph
# 6. Check which scene we're in
curl -s localhost:9000/__agent/scenes
# 7. Check errors
curl -s localhost:9000/__agent/errors
# 8. Change background
curl -s -X POST localhost:9000/__agent/settings/background \
-H "Content-Type: application/json" -d '{"color": "#000000"}'
# 9. Trigger render
curl -s -X POST localhost:9000/__agent/render
Security
- Localhost only: The agent API runs on the Vite dev server which binds to localhost by default. Do not expose it to the network (avoid
--host 0.0.0.0 in production)
- Screenshot paths: The screenshot name is sanitized — path separators and dots are stripped, and the resolved path is verified to stay inside the configured
screenshotDir
- Scene graph text: Text content extracted from the scene graph is sanitized (control characters stripped, truncated to 100 chars). Treat scene graph data as untrusted when processing it
How It Works
- Runtime plugin (
agentClient()) uses Plugin.player(), Plugin.renderer(), and Plugin.project() callbacks to capture the Player, Renderer, and Project instances
- Player provides seek, frame stepping, playback control, current scene, duration
- ProjectMetadata provides
shared (background, range, size), preview (fps, scale), and rendering (fps, scale, exporter) settings via .get() / .set()
- Renderer provides
render(settings) and abort() for export
- Scene graph is traversed via
scene.getView().peekChildren() recursively, serializing type, position, size, fill, text, opacity
- Communication uses Vite's HMR WebSocket
Files
| File |
Copy to |
Purpose |
assets/agent-plugin.ts |
Project root |
Vite plugin — HTTP endpoints + WS relay |
assets/agent-client.ts |
src/ |
Runtime plugin — Player/Renderer/Project control + canvas capture |
1---2name: motion-canvas-agent3description: Agent tooling for Motion Canvas — seek, screenshot, scene graph inspection, settings control, and rendering via HTTP API. Requires a browser with the editor open.4---567# Motion Canvas Agent Plugin89Full programmatic control of a running Motion Canvas editor via HTTP. Seek to any frame, capture screenshots, inspect the scene graph, change resolution/fps/background, and trigger rendering — all without browser automation.1011## Architecture1213```14Agent (curl/script) ──HTTP──▶ Vite Server (agent-plugin.ts) ──HMR WS──▶ Browser (agent-client.ts)15 │ │16 ▼ ▼17 screenshots/ Player + Renderer18 (PNG files) + ProjectMetadata19```2021## Setup2223### 1. Copy plugin files into your project2425- `assets/agent-plugin.ts` → project root26- `assets/agent-client.ts` → `src/`2728### 2. vite.config.ts2930```ts31import {defineConfig} from 'vite';32import motionCanvas from '@motion-canvas/vite-plugin';33import {agentPlugin} from './agent-plugin';3435export default defineConfig({36 plugins: [37 motionCanvas(),38 agentPlugin({screenshotDir: './screenshots'}),39 ],40});41```4243### 3. project.ts4445```ts46import {makeProject} from '@motion-canvas/core';47import {agentClient} from './agent-client';4849export default makeProject({50 plugins: [agentClient()],51 scenes: [...],52});53```5455### 4. Start + open browser5657```bash58npm start59# Open http://localhost:9000 in a browser60```6162## HTTP API Reference6364Base URL: `http://localhost:9000/__agent`6566### Playback Control6768| Method | Endpoint | Body | Response |69|--------|----------|------|----------|70| `GET` | `/status` | — | `{connected, frame, duration, fps, paused, sceneName, errors}` |71| `POST` | `/seek` | `{frame: 120}` | `{ok, frame}` |72| `POST` | `/next-frame` | — | `{ok, frame}` |73| `POST` | `/prev-frame` | — | `{ok, frame}` |74| `POST` | `/play` | — | `{ok}` |75| `POST` | `/pause` | — | `{ok}` |7677### Screenshots7879| Method | Endpoint | Body | Response |80|--------|----------|------|----------|81| `POST` | `/screenshot` | `{name: "my-shot"}` | `{ok, path, frame}` |82| `POST` | `/screenshot-base64` | — | `{ok, data, frame}` |8384### Settings8586| Method | Endpoint | Body | Response |87|--------|----------|------|----------|88| `GET` | `/settings` | — | `{background, range, size, previewFps, previewScale, renderingFps, renderingScale}` |89| `POST` | `/settings/background` | `{color: "#0F172A"}` | `{ok}` |90| `POST` | `/settings/range` | `{start: 0, end: 300}` | `{ok}` |91| `POST` | `/settings/size` | `{width: 1920, height: 1080}` | `{ok}` |92| `POST` | `/settings/preview-fps` | `{fps: 30}` | `{ok}` |93| `POST` | `/settings/preview-scale` | `{scale: 0.5}` | `{ok}` |94| `POST` | `/settings/rendering-fps` | `{fps: 60}` | `{ok}` |95| `POST` | `/settings/rendering-scale` | `{scale: 2}` | `{ok}` |9697### Rendering9899| Method | Endpoint | Body | Response |100|--------|----------|------|----------|101| `POST` | `/render` | `{fps?, range?}` | `{ok, message}` |102| `POST` | `/render/abort` | — | `{ok, message}` |103104### Scene Info105106| Method | Endpoint | Body | Response |107|--------|----------|------|----------|108| `GET` | `/scenes` | — | `{scenes: [{name}], currentScene}` |109| `GET` | `/scene-graph` | — | `{scene, graph}` (recursive node tree) |110| `GET` | `/threads` | — | `{scene, slides: [{name, id}]}` |111112### Errors113114| Method | Endpoint | Body | Response |115|--------|----------|------|----------|116| `GET` | `/errors` | — | `{errors: [{message, stack}]}` |117| `POST` | `/clear-errors` | — | `{ok}` |118119## Scene Graph Format120121`GET /__agent/scene-graph` returns a recursive tree:122123```json124{125 "scene": "my-scene",126 "graph": {127 "type": "View2D",128 "key": "my-scene/View2D[1]",129 "position": {"x": 960, "y": 540},130 "size": {"width": 1920, "height": 1080},131 "children": [132 {133 "type": "Rect",134 "position": {"x": 0, "y": 0},135 "size": {"width": 400, "height": 300},136 "fill": "#1E293B",137 "children": [138 {139 "type": "Txt",140 "text": "Hello World"141 }142 ]143 }144 ]145 }146}147```148149Each node includes: `type`, `key`, `position`, `size`, `opacity` (if not 1), `fill`, `text` (if applicable), and `children`.150151## AI Agent Workflow152153```bash154# 1. Check connection155curl -s localhost:9000/__agent/status156157# 2. Set resolution158curl -s -X POST localhost:9000/__agent/settings/size \159 -H "Content-Type: application/json" -d '{"width": 1920, "height": 1080}'160161# 3. Seek to a frame162curl -s -X POST localhost:9000/__agent/seek \163 -H "Content-Type: application/json" -d '{"frame": 60}'164165# 4. Screenshot166curl -s -X POST localhost:9000/__agent/screenshot \167 -H "Content-Type: application/json" -d '{"name": "check-1"}'168# → Read ./screenshots/check-1.png169170# 5. Inspect scene graph171curl -s localhost:9000/__agent/scene-graph172173# 6. Check which scene we're in174curl -s localhost:9000/__agent/scenes175176# 7. Check errors177curl -s localhost:9000/__agent/errors178179# 8. Change background180curl -s -X POST localhost:9000/__agent/settings/background \181 -H "Content-Type: application/json" -d '{"color": "#000000"}'182183# 9. Trigger render184curl -s -X POST localhost:9000/__agent/render185```186187## Security188189- **Localhost only**: The agent API runs on the Vite dev server which binds to localhost by default. Do not expose it to the network (avoid `--host 0.0.0.0` in production)190- **Screenshot paths**: The screenshot name is sanitized — path separators and dots are stripped, and the resolved path is verified to stay inside the configured `screenshotDir`191- **Scene graph text**: Text content extracted from the scene graph is sanitized (control characters stripped, truncated to 100 chars). Treat scene graph data as untrusted when processing it192193## How It Works194195- **Runtime plugin** (`agentClient()`) uses `Plugin.player()`, `Plugin.renderer()`, and `Plugin.project()` callbacks to capture the Player, Renderer, and Project instances196- **Player** provides seek, frame stepping, playback control, current scene, duration197- **ProjectMetadata** provides `shared` (background, range, size), `preview` (fps, scale), and `rendering` (fps, scale, exporter) settings via `.get()` / `.set()`198- **Renderer** provides `render(settings)` and `abort()` for export199- **Scene graph** is traversed via `scene.getView().peekChildren()` recursively, serializing type, position, size, fill, text, opacity200- **Communication** uses Vite's HMR WebSocket201202## Files203204| File | Copy to | Purpose |205|------|---------|---------|206| `assets/agent-plugin.ts` | Project root | Vite plugin — HTTP endpoints + WS relay |207| `assets/agent-client.ts` | `src/` | Runtime plugin — Player/Renderer/Project control + canvas capture |