# Plugins

Extend Motion SDK with opt-in packages that add agent skills, typed scene helpers, CLI commands and verify checks, starting with img2scene, which rebuilds a prop from a photo.

A plugin is a separate package you install when you need it. It can add four things:

- **Skills.** `SKILL.md` files that teach an agent a workflow.
- **Helpers.** Typed functions that build `@motion-sdk/scene` objects.
- **CLI commands.** `motion-sdk <plugin> ...` runs a native `motion-sdk-<plugin>` binary.
- **Checks.** Custom checks for [`motion-sdk verify`](/docs/agents/verify).

The engine doesn't change. Plugins add no new primitives, and the player, authoring and WebGL2 bundles stay the same size whether you use plugins or not.

## The Contract

```ts title="my-plugin/src/index.ts"
import { definePlugin } from "@motion-sdk/scene/plugin";
import { expect } from "@motion-sdk/scene";

export default definePlugin({
  name: "spin",                     // lowercase, digits, dashes
  version: "0.1.0",
  description: "Spin props on a turntable.",
  package: "@acme/motion-plugin-spin",
  skills: [{ name: "motion-sdk-spin", path: "skills/motion-sdk-spin/SKILL.md", description: "Load to show a prop off." }],
  helpers: { turns: (n: number) => n * 360 },
  cli: {
    bin: "motion-sdk-spin",
    commands: [{ name: "go", usage: "motion-sdk spin go scene.json", description: "Add a turntable track." }],
  },
  checks: { inFrame: (node: string) => expect(node).staysInFrame() },
});
```

`definePlugin` checks the manifest when the plugin loads and throws a `PluginError` with a `fix`. These are the rules:

- The `name` must be lowercase letters, digits and dashes, and `version` must be semver.
- Skills are named `motion-sdk-*`, and their paths are relative.
- `cli.bin` must be `motion-sdk-<name>`, and every command's usage must start with `motion-sdk <name>`.
- Helpers and checks must be functions.

The literal types survive, so `plugin.helpers.turns` is typed.

Your app registers the plugins it uses:

```ts title="motion.ts"
import { registerPlugins } from "@motion-sdk/scene/plugin";
import img2scene from "@motion-sdk/plugin-img2scene";

export const motion = registerPlugins(img2scene);
motion.helpers.img2scene.referenceCamera({ distance: 9, elevation: 10 });
motion.checks.img2scene.propInFrame("mug");
motion.skills();     // every skill with its plugin and package
motion.commands();   // every CLI command
motion.agentsNote(); // a Markdown block for AGENTS.md
```

Helpers and checks are namespaced by plugin. `registerPlugins` refuses two plugins with the same name or the same skill name.

## Commands

The CLI finds plugins the way `cargo` and `git` find subcommands. An executable named `motion-sdk-<name>`, either next to `motion-sdk` or on `PATH`, runs as `motion-sdk <name> ...`. It gets the same arguments, stdio and exit code, and `MOTION_SDK_BIN` is set so it can call back into `motion-sdk`.

```bash title="Terminal"
motion-sdk plugins             # name and path of each one
motion-sdk plugins --json
motion-sdk img2scene info      # runs motion-sdk-img2scene info
```

A plugin binary can be written in any language. A Rust one can link the engine crates, the way `motion-sdk-img2scene` links the render and export crates to render stills.

## Skills for Agents

Ship skills in the package under `skills/<name>/SKILL.md`, in the same format as the [core skills](/docs/agents/skills). Paste `motion.agentsNote()` into your project's `AGENTS.md`. It lists each skill's path under `node_modules` and each command, so an agent knows they exist. In this repo, `skills.sh.json` also lists the plugin skill folders, so `npx skills add` installs them.

## img2scene

`@motion-sdk/plugin-img2scene` is the first plugin. It takes one photo of a hard-surface prop and turns it into a typed scene file.

The [skill](https://github.com/brainfish-ai/motion/blob/main/packages/plugin-img2scene/skills/motion-sdk-img2scene/SKILL.md) walks the agent through three passes: a grey-clay blockout, then form, then materials. Each pass is validated, rendered and compared with the photo, then patched.

```bash title="Terminal"
cargo build --release -p motion-sdk-cli -p motion-sdk-img2scene
motion-sdk img2scene compare scene.json reference.png --sheet pass-1.png
```

`compare` renders a still at the photo's size through the normal headless render path, with no browser. It prints a JSON report:

- **`silhouette`.** `iou` as framed. `iou_aligned` with both subjects centred and scaled the same, which measures shape alone. `missing` and `extra` shares, the offset and the scale.
- **`palette`.** Weighted CIEDE2000 between the two k-means palettes, with each colour pair.
- **`phash`.** A 63-bit DCT perceptual hash and its similarity.
- **`score` and `hints`.** A rough 0 to 1 score, and the biggest differences written as edits, such as "subject is 19% too small: move the camera closer".

It also writes a sheet showing the photo, the render and an overlay. In the overlay, green is both, red is missing and blue is extra. Palette strips sit underneath. The agent reads the hints, looks at the sheet, and decides what to fix.

From TypeScript, `compare(scene, "reference.png")` in `@motion-sdk/plugin-img2scene/node` returns the typed report. The helpers are:

- `propScene` frames a scene like its photo: the photo's canvas size, a flat backdrop, no vignette or bloom, and a studio light rig.
- `referenceCamera` places the camera by distance, elevation and azimuth.
- `frameHeight` turns photo fractions into world units.
- `turntable` adds a turntable track.
- The checks `propInFrame` and `matchesFraming` need no GPU.

### Demos

Two original objects, each rebuilt in three passes. The photo, every pass's TS and JSON, the sheets and `metrics.json` are in [`packages/plugin-img2scene/demos`](https://github.com/brainfish-ai/motion/tree/main/packages/plugin-img2scene/demos).

| Demo | Pass | Score | IoU | Palette ΔE |
| --- | --- | --- | --- | --- |
| Ceramic mug | 1 blockout | 0.653 | 0.596 | 26.6 |
| | 2 form | 0.766 | 0.848 | 26.4 |
| | 3 materials | 0.89 | 0.895 | 3.5 |
| Desk lamp | 1 blockout | 0.431 | 0.291 | 8.9 |
| | 2 form | 0.658 | 0.631 | 7.5 |
| | 3 materials | 0.687 | 0.721 | 6.4 |

![Mug, pass 3: reference, render and overlay](/plugins/img2scene/mug/pass-3-materials.png)

![Desk lamp, pass 3: reference, render and overlay](/plugins/img2scene/desk-lamp/pass-3-materials.png)

> **Note:** Comparing a silhouette, a palette and a perceptual hash between a photo and a render follows ideas from the img2threejs project (Apache 2.0). img2scene is a separate implementation for Motion SDK's renderer.

## Publishing a plugin

Plugin packages live under `packages/plugin-*` and publish as `@motion-sdk/<dir>`. The first one is `@motion-sdk/plugin-img2scene`. They are public, and a publish from GitHub Actions includes npm provenance.

The [publish plugin](https://github.com/brainfish-ai/motion/actions/workflows/publish-plugin.yml) workflow packs every plugin on a PR that touches one. The same pack step also runs in the `ts sdk` CI job. Neither job publishes. A real publish happens only from a version tag, or from a manual run of the workflow with **dry_run** left unchecked.

The native CLI (`motion-sdk-<name>`) is a Rust crate, not part of the npm tarball. Install it from the repo (`cargo build --release -p motion-sdk-img2scene`) and leave it next to `motion-sdk` or on `PATH`. The tarball carries the TypeScript helpers, the checks and the `skills/*/SKILL.md` files. Demos and tests stay in the repo.

### Package manifest

Each plugin's `package.json` must be public and provenance-ready:

- no `"private": true`
- `"license"`, `"files": ["src", "skills", ...]`
- `"repository.url"` set to `https://github.com/brainfish-ai/motion.git` (npm provenance checks this)
- `"publishConfig": { "access": "public", "provenance": true }`

`scripts/pack-plugin.mjs` checks all of that, plus the tarball contents. Run it locally with `node scripts/pack-plugin.mjs` or `node scripts/pack-plugin.mjs plugin-img2scene`.

### First-time npm setup

The repo has no `NPM_TOKEN` secret and the workflow does not use one. Later publishes use [npm trusted publishing](https://docs.npmjs.com/trusted-publishers/) over OIDC. A trusted publisher can only be added on a package that already exists, so the very first version of a new name is a one-time bootstrap.

1. Create the `@motion-sdk` organization on [npmjs.com](https://www.npmjs.com/org/create) if it is not there yet, with 2FA on the owner account, and make sure you can publish under that scope.
2. `@motion-sdk/scene` is a peer of every plugin and is **not on npm yet**. Publishing a plugin first is fine, but people who install it from npm will need `@motion-sdk/scene` from this repo until that package is published too.
3. From a clean checkout of the commit you want to release (do this on a machine where you are logged in, not from this workflow):

   ```bash
   npm login
   cd packages/plugin-img2scene
   npm publish --access public --provenance
   ```

   Or stage it, then approve, which also creates the package: `npm stage publish` and `npm stage approve`.
4. On [the package's settings](https://www.npmjs.com/package/@motion-sdk/plugin-img2scene) → **Trusted Publisher** → GitHub Actions, add:

   | Field | Value |
   | --- | --- |
   | Organization or user | `brainfish-ai` |
   | Repository | `motion` |
   | Workflow filename | `publish-plugin.yml` |

   Leave the environment name blank (the workflow does not use a GitHub Environment). Allow `npm publish`. The first successful publish through this connection must happen within 2 days, or the configuration expires and you will need to add it again.
5. You do not need an `NPM_TOKEN` repository secret. After trusted publishing works, you can turn on "Require two-factor authentication and disallow tokens" on the package.

### Later releases

Bump the `version` in that plugin's `package.json` (and in any tag you cut) and merge to `main`. Then, on the release commit:

```bash
git tag plugin-img2scene-v0.1.1
git push origin plugin-img2scene-v0.1.1
```

The tag must be `plugin-<name>-v<semver>` and the version must match `package.json`. The workflow typechecks, tests, packs, refuses to overwrite a version that is already on npm, and runs `npm publish --access public --provenance`.

To pack without publishing, open **Actions → publish plugin → Run workflow**, set the package to `plugin-img2scene`, and leave **dry_run** checked. Uncheck it only when you mean to publish, and only after the trusted publisher is in place.
