# TypeScript SDK

Write scenes as type-checked TypeScript with @motion-sdk/scene. Generated types, typed helpers, path-checked patches and the engine's validator.

`@motion-sdk/scene` lets you and your coding agent write scenes in TypeScript instead of hand-written JSON. The types are generated from the Rust spec, so a misspelled field, a wrong preset or a missing keyframe value fails to compile, and every field shows its docs on hover. What you build is still the plain [scene document](/docs/concepts/scene-document). `JSON.stringify` it, pass it to the player or render it with the CLI.

## Installation

`@motion-sdk/scene` isn't on npm yet. In the repository it's a workspace package:

```bash title="Terminal"
npm install
```

```json title="package.json"
{
  "dependencies": {
    "@motion-sdk/scene": "0.1.0",
    "@motion-sdk/core": "0.1.0"
  }
}
```

The package has no runtime dependencies and ships TypeScript source. Use it from Node 24 (type stripping), Vite, Next.js or any bundler.

## Define a Scene

`defineScene` takes a `Scene` and returns it unchanged. It exists for the types: autocomplete for every field, docs on hover, and errors for anything the engine would reject.

```ts title="scene.ts"
import { defineScene, key, mesh, spring, text, track } from "@motion-sdk/scene"

export default defineScene({
  canvas: { width: 1080, height: 1080, fps: 30 },
  materials: { chrome: { preset: "chrome" } },
  nodes: {
    orb: { mesh: mesh({ sphere: { radius: 1 } }, { material: "chrome" }) },
    title: { transform: { position: [0, -1.8, 0] }, text: text("hello", { size: 0.5 }) },
  },
  timeline: {
    duration: 3,
    tracks: {
      bob: track("orb", "position", [key(0, [0, 0, 0]), key(1.5, [0, 0.6, 0], spring({ damping: 10 })), key(3, [0, 0, 0])]),
      fade: track("title", "opacity", [key(0, 0), key(0.6, 1, "ease_out_cubic")]),
    },
  },
})
```

Plain object literals work too. `satisfies Scene` gives the same checks without any helper:

```ts title="scene.ts"
import type { Scene } from "@motion-sdk/scene"

export default {
  nodes: { orb: { mesh: { geometry: { sphere: {} }, material: "preset:chrome" } } },
} satisfies Scene
```

Render it like any other scene:

```bash title="Terminal"
node -e 'import("./scene.ts").then((m) => console.log(JSON.stringify(m.default)))' > scene.json
motion-sdk render scene.json -t 1 -o frame.png
```

## What the Compiler Catches

The generated types are exact. Single-key variants such as geometry, lights and easing are exclusive, colors are hex strings, and vectors are tuples.

```ts title="agent.ts"
defineScene({
  nodes: { orb: { transform: { positon: [0, 1, 0] } } },
  materials: { chrome: { preset: "chrom" } },
  timeline: { tracks: { bob: track("orb", "position", [key(0, 1)]) } },
})
```

```txt title="tsc"
error TS2561: Object literal may only specify known properties, but 'positon' does not exist in type 'Transform'. Did you mean to write 'position'?
error TS2820: Type '"chrom"' is not assignable to type 'MaterialPreset | null | undefined'. Did you mean '"chrome"'?
error TS2322: Type 'TypedKeyframe<1>' is not assignable to type 'TypedKeyframe<Vec3>'.
  Type 'number' is not assignable to type 'Vec3'.
```

These messages are enough for an agent to fix the code without reading the docs. Run `tsc --noEmit` after each edit.

## Helpers

Every helper returns plain JSON. They add types, not behaviour.

| Helper | Returns | Description |
| --- | --- | --- |
| `defineScene(scene)` | `Scene` | A complete scene. |
| `node(node)` | `Node` | A node: mesh, text, particles, group or prefab placement. |
| `mesh(geometry, options?)` | `Mesh` | A geometry with a material name and shadow options. |
| `shape(shape, options?)` | `Mesh` | A 2D vector shape (rect, circle, ellipse, polygon, bezier). |
| `text(content, options?)` | `Text` | A text block. |
| `material(material)` | `Material` | A material for `scene.materials`. |
| `light(light)` | `Light` | A light for `scene.lighting.lights`. |
| `camera(camera)` | `Camera` | The scene camera. |
| `track(target, property, keys, options?)` | `Track` | A timeline track. Key values are typed by the property: `[x, y, z]` for `position`, a number for `opacity`, a hex color for `color`. |
| `key(t, value, easing?)` | `Keyframe` | A keyframe at `t` seconds. |
| `spring(params?)` | `Easing` | A damped spring easing. |
| `cubicBezier(x1, y1, x2, y2)` | `Easing` | A CSS-style bezier easing. |
| `steps(n)` | `Easing` | Jump in `n` steps. |
| `patch.add` / `replace` / `remove` / `move` / `copy` / `test` | `PatchOp` | RFC 6902 ops with checked paths. See below. |

Every spec type is exported too: `Scene`, `Node`, `Material`, `Geometry`, `Track`, `Easing`, `Color`, `Vec3`, `PatchOp`, `Report`, `Issue` and the rest. See the [Scene reference](/docs/reference/scene) for their fields.

## Validate

The compiler checks shapes. The engine's validator checks everything else: dangling references, ranges, prefab expressions and the rules on the [errors page](/docs/reference/errors). `validate` runs the same validator as the player and the CLI, in Node or the browser, with no canvas or GPU.

```ts title="check.ts"
import { formatIssues, validate } from "@motion-sdk/core/validate"

const report = await validate(
  {
    materials: { chrome: { preset: "chrome" } },
    nodes: { orb: { mesh: { geometry: { sphere: {} }, material: "chrom" } } },
  },
  { final: true },
)
if (report.issues.length) console.log(formatIssues(report))
```

```txt title="Output"
warning unused /materials/chrome: material `chrome` is not used by any node (fix: remove it or reference it from a mesh/text node via "material")
error dangling_ref /nodes/orb/mesh/material: material `chrom` is not defined (the default material is used meanwhile) (fix: did you mean material `chrome`?)
```

Each issue has a JSON Pointer `path`, a stable `code`, a `message` and a `suggested_fix`. `final: true` treats dangling references as errors. Leave it off while a scene is still streaming in, because the missing value may arrive in a later patch.

| Option | Type | Default | Description |
| --- | --- | --- | --- |
| `final` | `boolean` | `false` | Validate a finished document. Dangling references become errors. |
| `wasm` | `BufferSource \| URL \| Response \| string` | next to the module | Where to load the authoring wasm from. |

`validate` loads the authoring wasm bundle. The lean player bundle doesn't change. Without the wasm, use the CLI: `motion-sdk validate scene.json --final --json` prints the same report. See [Validation](/docs/concepts/validation).

## Play It

`createPlayer` and `player.load` take a typed `Scene` or JSON text:

```ts title="main.ts"
import { createPlayer } from "@motion-sdk/core"
import scene from "./scene.ts"

const player = await createPlayer(canvas, scene)
```

## Streaming Patches

`patch` builds [RFC 6902 ops](/docs/reference/patch). The JSON Pointer is checked against the scene types, and the value is typed by its path.

```ts title="live.ts"
import { key, mesh, patch, track } from "@motion-sdk/scene"

player.patch([
  patch.replace("/materials/chrome/color", "#ffcc66"),
  patch.add("/nodes/ring", { mesh: mesh({ torus: { radius: 1.6, tube: 0.05 } }, { material: "chrome" }) }),
  patch.add("/timeline/tracks/ring_spin", track("ring", "rotation", [key(0, [90, 0, 0]), key(3, [90, 360, 0])])),
])
```

A typo in a path fails to compile:

```txt title="tsc"
error TS2345: Argument of type '"/nodes/orb/transfrom/position"' is not assignable to parameter of type '"not a scene path: /nodes/orb/transfrom/position"'.
```

`PathValue<"/nodes/orb/transform/position">` is the type at a path (`Vec3` here) if you build ops yourself. See [Streaming](/docs/concepts/streaming) for how the player applies them.

## Examples

- [`examples/ts/city-block.ts`](https://github.com/brainfish-ai/motion/blob/main/examples/ts/city-block.ts) is the city-block benchmark in TypeScript. A test checks it emits exactly `examples/benchmarks/city-block.json`.
- [`examples/ts/svg-painter.ts`](https://github.com/brainfish-ai/motion/blob/main/examples/ts/svg-painter.ts) draws a character in SVG and animates its parts by id, with typed ids (see [SVG Drawings](/docs/features/svg#in-typescript)).
- [`examples/ts/hello-orb.ts`](https://github.com/brainfish-ai/motion/blob/main/examples/ts/hello-orb.ts) has animation helpers and live-edit patches.
- [`apps/city-game/src/scene.ts`](https://github.com/brainfish-ai/motion/blob/main/apps/city-game/src/scene.ts) builds the whole city game scene with the generated types.

## How the Types Stay Current

`scripts/gen-types.mjs` turns `schema/*.schema.json` into `packages/scene/src/generated.ts`, and the schema is generated from the Rust spec. CI fails when either file is stale. After changing the spec:

```bash title="Terminal"
UPDATE_SCHEMA=1 cargo test -p motion-sdk-spec schema
npm run gen:types
```

CI also type-checks every committed example scene against `Scene`, so the types never reject a scene the engine accepts.
