Motion SDK

Expressions

A small, deterministic expression language for prefab parameters and copies.

Inside a prefab or a repeat, any string that starts with = is an expression. It's evaluated when the scene is expanded, so the renderer only ever sees plain values.

81 bars from one prefab, heights from sin and cos of the grid indices, colours picked per copy.
{
  "camera": { "position": [0, 9, 14], "target": [0, 0, 0], "fov": 40 },
  "lighting": { "rig": "neon_rim" },
  "environment": { "background": { "solid": "#07060c" } },
  "post": { "bloom": 0.9 },
  "prefabs": {
    "bar": {
      "params": { "h": 1, "c": "#ff2ec4" },
      "nodes": {
        "col": {
          "transform": { "position": [0, "=h/2", 0], "scale": [0.6, "=h", 0.6] },
          "mesh": { "geometry": { "box": {} }, "material": "preset:neon" },
          "color": "=c"
        }
      }
    }
  },
  "nodes": {
    "bars": {
      "use": "bar",
      "repeat": { "grid": [9, 1, 9], "spacing": [1, 1, 1] },
      "transform": { "position": [-4, 0, -4] },
      "params": {
        "h": "=0.3 + 2*abs(sin(ix*0.7) * cos(iz*0.7))",
        "c": "=pick(i % 3, '#ff2ec4', '#00e5ff', '#f2c230')"
      }
    }
  }
}
81 bars from one prefab, heights from sin and cos of the grid indices, colours picked per copy. apps/docs/examples/expressions.json · render it with motion-sdk render apps/docs/examples/expressions.json -t 1 -o expressions.png

Variables

  • Every prefab parameter by name, like h or c.
  • i: the copy's index. n: the number of copies.
  • ix, iy, iz: the copy's grid indices.

Operators and Values

+ - * / %, comparisons == != < <= > >=, && || !, [arrays] and 'strings'.

Functions

FunctionResult
floor(x), ceil(x), round(x), abs(x), sqrt(x)Rounding and roots
pow(x, y), min(a, b), max(a, b), clamp(x, lo, hi), lerp(a, b, t)Arithmetic
sin(x), cos(x)Trigonometry, in radians
pick(i, a, b, c, ...)The i-th choice (from 0), wrapping around
if(cond, a, b)a when cond is true, else b
len(list)Length of an array or string
rand(), rand(k), rand(lo, hi), rand(lo, hi, k)A seeded random number in [0, 1) or [lo, hi). Vary k for independent streams
str(x)A number as a string

Nested Prefabs

Expressions in a nested prefab's params are evaluated with the outer copy's i. Write == instead of = to defer one to the inner prefab.