# Rigging

IK chains, constraints and planted feet, so a few goals drive a whole character.

Instead of keying every joint, key a few goals and let the engine work out the joints. `ik` on a chain's end (an ankle, a wrist) turns the bones above it so the end reaches a goal node. `constrain` pins a node to another, turns it toward one or clamps its turn. `plant` holds a goal still while a foot is on the ground, so feet can't slide.

```json
{
  "nodes": {
    "thigh": { "transform": { "position": [0, 2, 0] } },
    "shin": { "parent": "thigh", "transform": { "position": [0, -1, 0] } },
    "ankle": { "parent": "shin", "transform": { "position": [0, -1, 0] }, "ik": { "target": "foot", "bend": "ccw" } },
    "foot": { "plant": [[0.2, 0.8]] }
  },
  "timeline": {
    "duration": 1,
    "loop": "once",
    "tracks": {
      "step": { "target": "foot", "property": "position", "keyframes": [{ "t": 0, "value": [-0.6, 0.2, 0], "easing": "linear" }, { "t": 1, "value": [0.6, 0.2, 0] }] }
    }
  }
}
```

The solve runs every frame after the keyed pose, tracks and state machines. The keyed rotations on the chain are its starting pose, and goals win. Each solve turns a bone about its own origin and carries everything below it, so skinned shapes, SVG limbs and children follow.

## IK

`ik` goes on the chain's end node:

- `target`: the node to reach. Usually an empty goal node at the top level, keyed on the timeline or held by `plant`. It can't move with the chain it drives.
- `chain`: how many bones above the node turn. 1 aims the parent at the goal. 2 (the default) bends the parent and grandparent, such as thigh and shin or upper arm and forearm, solved exactly. Longer chains (tails, tentacles, spines) are solved with FABRIK.
- `bend`: `"ccw"` or `"cw"`, the way a 2D chain bends at its joints as seen on screen. By default it keeps the way the keyed pose bends (counter-clockwise when straight).
- `pole`: for 3D limbs, a node the middle joint points toward (a knee or elbow hint). The chain then bends in 3D instead of the screen plane. A pole in front of a front-facing character makes its knees bend toward the camera, so a lifted foot foreshortens the leg instead of splaying the knee.
- `soft`: eases into full reach over this fraction of the chain's length (0 to 0.5), so knees and elbows don't snap straight. The end then stops just short of far goals.
- `mix`: 0 to 1, how much the solve wins over the keyed pose. It blends joint angles. Animate it with an `ik_mix` track on the end node to switch between IK and keyed motion.

A goal out of reach straightens the chain toward it.

On an SVG limb (a bone with `joints`), put `ik` on the last joint through `with`, and the part and its joints bend: `"with": {"leg.joint2": {"ik": {"target": "foot"}}}` turns `leg` at the hip and `leg.joint1` at the knee.

## Constraints

`constrain` is a list applied in order:

- `{"attach": {"to": "hand"}}` moves the node with `hand` as if it were parented there in the rest pose. Its own keyed motion still plays on top, so a spear can tilt in a moving hand. `"keep": "snap"` puts the node's origin on the target's.
- `{"aim": {"at": "fly"}}` turns the node so its +x (or `"axis": "y"`) points at `fly`, plus `offset` degrees.
- `{"limit": {"rotation": [-30, 40]}}` clamps the node's turn relative to its parent, in degrees.

Each takes `influence` (0 to 1) to blend with the unconstrained pose. A node's constraints run after its parents' and after any IK or constraint that moves the nodes it reads. An `attach` to a hand waits for the arm's IK, and a hand's IK to a spear's grip waits for the spear's `attach`.

## Planted Feet

`plant` on a top-level goal is a list of `[start, end]` windows in seconds. Inside each window the goal holds the position it has at `start`, whatever its keys do, so a foot driven by IK stays put. Smooth and spring tracks on the goal are baked first, so nothing overshoots through a hold. Scene `"ground": {"y": -1.6}` keeps planted goals from going below the floor.

`walk()` in TypeScript writes the goals, windows and body motion for you: see [Walk cycles](/docs/guides/walk-cycle).

## Checking

[`motion-sdk verify`](/docs/agents/verify) has four rig checks, measured on screen:

- `planted`: the foot slides at most `tol_px` (default 4) during a window. Fails with `foot_slid`.
- `reaches`: an IK end sits on its goal (default 2 px). Fails with `ik_unreached`.
- `attached`: a node keeps its offset from another (default 2 px). Fails with `drifted`.
- `lifts`: a swing foot rises at least `min` (a fraction of the frame height, default 0.02) during a swing. Fails with `low_lift`.

`validate` reports [`ik_chain_too_long`](/docs/reference/errors#ik_chain_too_long), [`rig_bad_target`](/docs/reference/errors#rig_bad_target) and [`plant_not_root`](/docs/reference/errors#plant_not_root).

## In TypeScript

[`@motion-sdk/scene`](/docs/concepts/typescript) has helpers that return the plain JSON:

```ts
import { attach, defineScene, goal, limb, limit, svg } from "@motion-sdk/scene";

const art = `<svg viewBox="0 0 100 200"><g id="leg" data-pivot="50 100"><path d="M50 100 L50 200" stroke="#333" stroke-width="12"/></g></svg>`;
const hero = limb(svg(art, { width: 2, bones: { leg: { joints: [[50, 150], [50, 196]] } } }), "leg", { target: "foot", bend: "ccw" });

export default defineScene({
  nodes: {
    hero,
    foot: goal([0.2, -1.8], { plant: [[0, 0.5]] }),
    spear: { constrain: [attach("hero"), limit([-20, 20])] },
  },
});
```

- `goal(at, options)`: an empty goal node.
- `ik(target, options)`: an `ik` block.
- `limb(drawing, part, options)`: puts `ik` on an SVG limb's last joint.
- `attach(to)`, `aim(at)`, `limit([min, max])`: constraints.
- `lag(track, { delay, overshoot, target })`: follow-through. It copies a track, trails it by `delay` seconds and springs it to overshoot by about `overshoot`, for a plume, a sleeve or a spear tip.
- `hand.tracks(rig, [hand.pose("fist", 0), hand.pose("point", 1)])`: finger poses (`open`, `relaxed`, `fist`, `point`, `grip`, `pinch`) as `bend` tracks per finger.
- `walk()` and `crowd()`: see [Walk cycles](/docs/guides/walk-cycle).

See [`examples/ts/rig-walk.ts`](https://github.com/brainfish-ai/motion/blob/main/examples/ts/rig-walk.ts): a spear bearer marching toward the camera on planted feet, with the hand held to the spear.

## Where it Runs

Rigging is in the CLI and the browser's authoring and WebGL2 bundles. `createPlayer` sees `ik`, `constrain`, `plant` or `ground` and loads the authoring bundle. The lean WebGPU player leaves it out.

## API Reference

### Ik

Inverse kinematics: turn the bones above this node (the chain's end, e.g. an ankle) so it reaches `target`. Without a `pole` the chain bends in the screen plane (2D rigs).

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `bend` | `Bend` | — | Which way a 2D chain bends at its joints, as seen on screen. Default: the way the keyed pose already bends (counter-clockwise when straight). |
| `chain` | `integer (1 to 16)` | — | Bones above this node that turn: 1 aims the parent, 2 (default) bends parent and grandparent (thigh and shin, upper arm and forearm), more are solved with FABRIK. |
| `iterations` | `integer (1 to 64)` | — | FABRIK passes for chains longer than 2 (default 12). |
| `mix` | `number (0 to 1)` | — | How much the solve wins over the keyed pose, 0..1 (blends joint angles). Animate it with an `ik_mix` track to switch between IK and keyed (FK) motion. |
| `pole` | `string` | — | 3D limbs: a node the middle joint points toward (a knee or elbow hint). The chain then bends in 3D instead of the screen plane. |
| `soft` | `number (0 to 0.5)` | — | Ease into full reach over this fraction of the chain's length (0..0.5), so knees and elbows don't snap straight. The end then stops just short of far goals. |
| `target` (required) | `string` | — | Node to reach: its world position (usually an empty goal node keyed on the timeline, or one that `plant` holds still). |
### Bend

Bend direction of a 2D chain at its joints.

| Value | Description |
| --- | --- |
| `"cw"` | Clockwise: each bone turns right from the one before. |
| `"ccw"` | Counter-clockwise: each bone turns left from the one before. |
### Constraint

One constraint on a node; a node's `constrain` list applies in order, after its parents' and after any IK that moves it.

- `{"attach": Attach}`: Move with another node as if parented to it (a spear in a hand, a hand on an oar).

- `{"aim": Aim}`: Turn so an axis points at another node (eyes, heads, turrets).

- `{"limit": Limit}`: Clamp the node's in-plane turn relative to its parent.
### Attach

Move with `to` as if parented to it in the rest pose; the node's own keyed motion still plays on top.

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `influence` | `number (0 to 1)` | — | 0..1: how far the node follows (0 = not at all). |
| `keep` | `Keep` | — | `offset` (default) keeps where the node sits relative to `to` in the rest pose; `snap` puts its origin on `to`'s origin. |
| `to` (required) | `string` | — | Node to follow. |
### Keep

What `attach` keeps from the rest pose.

| Value | Description |
| --- | --- |
| `"offset"` | The node's offset and turn relative to the target in the rest pose. |
| `"snap"` | Only the turn: the node's origin sits on the target's origin. |
### Aim

Turn in the screen plane so `axis` points at `at` (Blender's Damped Track).

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `at` (required) | `string` | — | Node to point at. |
| `axis` | `AimAxis` | — | The node's own axis that points (default `x`). |
| `influence` | `number (0 to 1)` | — | 0..1: how far it turns toward the target. |
| `offset` | `number` | — | Extra turn in degrees on top (positive is counter-clockwise). |
### AimAxis

The local axis an `aim` points.

| Value | Description |
| --- | --- |
| `"x"` | Local +X. |
| `"y"` | Local +Y. |
### Limit

Keep the node's in-plane rotation relative to its parent within `rotation` (applied after IK, so a solved knee can't bend backwards).

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `influence` | `number (0 to 1)` | — | 0..1: how strongly the limit holds. |
| `rotation` (required) | `[x, y]` | — | `[min, max]` degrees about Z, relative to the parent (positive is counter-clockwise). |
### Ground

The floor for planted goals: `plant` goals' position keys never go below `y`.

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `y` (required) | `number` | — | World height of the floor. |
