# Writing a comp

A comp file is a TypeScript module that does `export default` of the result of `comp()`. The following has the same content as the template (`lamino schema --template shot.comp.ts`) and puts CG fireworks over a night plate.

```ts
import { comp, read } from "lamino";
import { glow, lightWrap, grain } from "lamino/fx";

export default comp("SHOW_010", { fps: [24000, 1001], res: [3840, 2160], frames: [1001, 1120] }, () => {
  // Live-action plate. DPX is log footage, so declare the input colour space
  const plate = read("$SHOTS/SHOW/010/plate/A001_C003.####.dpx", {
    colorspace: "ARRI LogC4",
    missing: "error",
  });

  // CG fireworks. The beauty layer of the EXR, premultiplied
  const fw = read("$SHOTS/SHOW/010/render/fireworks_v012.####.exr", {
    layer: "beauty",
    alpha: "premult",
  });

  // Glow: blur the scene-linear parts above 1.0 at several scales and add them back (radius is in width units)
  const lit = glow(fw, { threshold: 1.0, radius: 0.04 });
  // Move slightly right and scale up by 5% (x is in width units)
  const fx = lit.transform({ x: 0.02, scale: 1.05 });

  // Wrap the plate's light around the edges of the fireworks, then place them over the plate
  // bg.over(fg): the receiver is the background (B), the argument is the foreground (A)
  const out = plate
    .over(lightWrap(fx, plate, { size: 0.01, amount: 0.4 }))
    .grade({ lift: -0.01, gain: [1.0, 0.98, 1.02] });

  // Match the plate's grain. The seed is fixed, so the picture is the same every time
  return grain(out, { amount: 0.03, size: 1.2, seed: 7 });
});
```

## The basic shape

The three arguments of `comp(id, settings, build)` define the comp.

- `id`: the name of the comp.
- `settings`: `fps` (`[numerator, denominator]` or a number), `res` (the full-resolution size in pixels), `frames` (the frame range), `par` (the pixel aspect ratio). This is the only place where pixel counts are written.
- `build`: a function that assembles the nodes and returns the final output node. Returning anything other than a node gives `E_SCRIPT_NO_DEFAULT`.

This file runs only once, to make the IR. It does not run per frame.

## Connecting nodes

A node can be called as a function or as a method. `blur(img, { size: 0.01 })` and `img.blur({ size: 0.01 })` make the same node. Every function whose first argument is an image, including those in `lamino/fx` and `lamino/aov`, can also be called as a method.

Mind the direction in merges. The function form `over(a, b)` is "A over B", and the first argument is the foreground. In the method form `bg.over(fg)`, the receiver is the background and the argument is the foreground. `in` and `out` have no method form; use `inside(a, b)` and `outside(a, b)`.

## Node ids

A node's id is the name used to point at the node from outside, as in `probe --node lit` or `override set lit.radius 0.03`. Writing `const lit = glow(...)` makes the variable name `lit` the id. Nodes inside a preset or precomp get the parent's name in front: the nodes inside `const wrap = lightWrap(...)` become `wrap/blur`, `wrap/merge` and so on. To set it explicitly, use `.id("name")`. When the same id appears twice, `_2` is appended and `L_ID_GENERATED` is reported.

## Animation

An animatable parameter takes one of the following instead of a number.

```ts
import { keys, t, ease } from "lamino";

// Keyframes: [frame, value, ease]. The ease applies to the interval that arrives at that key
const fade = fx.opacity(keys([[1001, 0], [1012, 1, ease.out]]));

// Function: computes the value from the frame number. It is computed in advance over the comp's frames range
const drift = fx.transform({ x: t((f) => 0.02 * Math.sin((f - 1001) / 12)) });
```

A comp that uses `t()` writes `frames`. If it is left out, it is estimated from the range of the image sequences read, and `L_ESTIMATED` is reported. No node reads a clock, so anything that should change over time, such as the `evolution` of a noise, is driven by you with `t()`. For random numbers, use `random(seed)`.

## Splitting into parts

It is code, so repetition becomes functions, and shared things go in `lib/` and are brought in with `import`.

- **Precomps**: a file that does `export default` of `comp(id, settings, (params) => node)` can be called from another comp as a function, as in `import vol from "./volumetrics.comp"; vol({ density: 0.8 })`. It is expanded into the parent's IR, and its node ids get `vol/` in front.
- **Shader nodes**: `import src from "./look.glsl"` brings GLSL in as a string, and `img.shader({ source: src, params: { ... } })` calls it.
- **Data**: read tables and text with `json()`, `table()` and `readText()`, and make nodes in a loop. `.json` and `.csv` files cannot be brought in with `import` (`E_SCRIPT_IMPORT`).
- **Bake**: wrap a heavy subgraph in `bake(node, name)`. Once it has been rendered at full resolution, later proxy evaluations read the baked result.

All the functions and methods are in [the reference, "Script API"](/docs/reference/script-api/), and the parameters of each node are in [the node chapters](/docs/reference/nodes-lamino/).
