Motion SDK

Prefabs and Repeat

Write repeated parts once, as templates with parameters, placed many times.

A city, a crowd or a product grid should be a few prefabs, not hundreds of nodes. A prefab is a template of nodes with parameters; use places it and repeat makes copies.

A house prefab placed six times: a street of five with growing heights and cycled colours, and a landmark.
{
  "camera": { "position": [0, 7, 16], "target": [0, 1, 0], "fov": 40 },
  "lighting": { "rig": "golden_hour" },
  "environment": { "background": { "sky": {} } },
  "materials": {
    "ground": { "preset": "matte_clay", "color": "#7a8a6a" },
    "wall": { "preset": "stucco" },
    "roof": { "color": "#8a3a2a", "shading": "flat" }
  },
  "prefabs": {
    "house": {
      "params": { "w": 2, "h": 2, "c": "#e8dcc8" },
      "nodes": {
        "walls": {
          "transform": {
            "position": [0, "=h/2", 0],
            "scale": ["=w", "=h", "=w"]
          },
          "mesh": { "geometry": { "box": {} }, "material": "wall" },
          "color": "=c"
        },
        "roof": {
          "transform": {
            "position": [0, "=h+0.5", 0],
            "scale": ["=w*0.8", 1, "=w*0.8"],
            "rotation": [0, 45, 0]
          },
          "mesh": {
            "geometry": { "cone": { "radius": 1, "height": 1, "segments": 4 } },
            "material": "roof"
          }
        }
      }
    }
  },
  "nodes": {
    "ground": {
      "mesh": {
        "geometry": { "plane": { "size": [60, 60] } },
        "material": "ground"
      },
      "transform": { "rotation": [-90, 0, 0] }
    },
    "street": {
      "use": "house",
      "repeat": { "count": 5, "step": [3.2, 0, 0] },
      "transform": { "position": [-6.4, 0, 0] },
      "params": {
        "h": "=1.5+i*0.4",
        "c": "=pick(i, '#e8dcc8', '#f2c230', '#c8d8e8')"
      }
    },
    "landmark": {
      "use": "house",
      "transform": { "position": [0, 0, -5] },
      "params": { "w": 3.5, "h": 4 }
    }
  }
}
A house prefab placed six times: a street of five with growing heights and cycled colours, and a landmark. apps/docs/examples/prefabs.json · render it with motion-sdk render apps/docs/examples/prefabs.json -t 1 -o prefabs.png

Define

  • params: the parameters and their defaults.
  • nodes: template nodes. Any string starting with = is an expression.
  • root: fields for the placed node itself, like a follow.

Place

{"use": "house", "params": {...}, "with": {"walls": {...}}} places a prefab. with overrides fields on its template nodes.

Repeat

  • count and step: a row.
  • grid and spacing: a 3D grid.
  • each: one params object per copy.

Copies get ids <id>.<k>, like street.3.walls. Expressions see the copy's index i, the count n, and grid indices ix, iy and iz.

In the Browser

Templated scenes need the authoring bundle, which createPlayer picks automatically from the first scene. To ship one on the lean player, flatten it with motion-sdk expand scene.json -o scene.player.json. The output is a player artifact: it validates, but edit the original.

API Reference

Prefab

A reusable group of nodes with parameters.

PropertyTypeDefaultDescription
nodes*objectrequiredTemplate nodes keyed by id, written like scene nodes (any field may be an = expression). Templates without parent hang under the placed node; parent may name another template node. A template may itself use a prefab.
paramsobject—Parameters with their default values. Templates read them in = expressions ("size": "=[w, h, d]"); placements override them in params. Defaults may use literals and i/n/ix/iy/iz.
rootany—Fields for the placed node itself (e.g. a follow that reads params). The placement's own fields win; a root transform is added to the placement's (positions and rotations add, scales multiply).

Repeat

Copies of a node or prefab placement. Use exactly one of count, grid or each. Copy k gets the id <id>.<k>.

PropertyTypeDefaultDescription
countinteger—Number of copies; copy i is offset by i * step.
eachobject[]—One copy per entry; each entry's keys override the prefab params for that copy (values may be = expressions).
gridinteger[]—Copies along x, y, z, offset by [ix, iy, iz] * spacing.
spacing[x, y, z]—Grid cell size for grid.
step[x, y, z]—Position offset per copy for count and each.