Principle:8beeeaaat Touchdesigner mcp Non Overlapping Grid Layout
| Knowledge Sources | |
|---|---|
| Domains | Layout, Geometry |
| Last Updated | 2026-10-28 22:13 GMT |
Overview
A deterministic grid-placement algorithm that finds the first non-overlapping cell for a newly created node by scanning a row-major grid with fixed clearance, ensuring no two nodes ever overlap in the network editor while keeping existing nodes in place.
Description
When nodes are created in a visual node graph, they appear at a default position that may overlap existing nodes. This principle solves the placement problem with a pure-geometry approach:
- Row-major grid scan: cells are numbered left-to-right, wrapping every `cols` columns, then moving down a row. The first cell whose box clears all existing boxes (with at least `gap` clearance) is chosen.
- Deterministic: given the same set of existing boxes and the same new box size, the algorithm always returns the same position. This makes node placement reproducible.
- Existing nodes stay put: the algorithm never moves an existing node. It only finds a free cell for the new one.
- Safety bound: the scan is bounded by a maximum cell count (100,000) to prevent infinite loops. If no free cell is found within the bound, a `RuntimeError` is raised.
- Pure geometry: the algorithm has no dependency on the TouchDesigner runtime (`import td` is absent). This allows unit testing outside the live environment.
Usage
Use this principle when:
- Placing newly created nodes in a visual network editor without overlap.
- Designing a layout system where determinism and reproducibility matter.
- Testing placement logic outside the host application (pure function, no runtime dependency).
Opt out by supplying explicit `nodeX`/`nodeY` coordinates — the algorithm is skipped entirely.
Theoretical Basis
Grid cell geometry:
Cell k is placed at:
Overlap test: two boxes a and b overlap if they are closer than `margin` on both axes. With `margin = gap`, touching exactly at `gap` distance is treated as free (strict `<`).
# Abstract algorithm description (NOT real implementation)
def boxes_overlap(a, b, margin=0.0):
ax, ay, aw, ah = a
bx, by, bw, bh = b
return (ax < bx + bw + margin
and bx < ax + aw + margin
and ay < by + bh + margin
and by < ay + ah + margin)
def first_free_cell(existing, width, height, cols=5, gap=20.0, origin=(0,0)):
for k in range(MAX_CELLS):
box = cell_box(k, width, height, cols, gap, origin)
if all(not boxes_overlap(box, e, gap) for e in existing):
return (box[0], box[1])
raise RuntimeError("no free cell within safety bound")
Defaults: `cols=5`, `gap=20.0`, `origin=(0.0, 0.0)`, `MAX_CELLS=100,000`.
Practical Guide
- The algorithm expects boxes as `(x, y, width, height)` tuples where `(x, y)` is the lower-left corner and `y` grows downward (TouchDesigner node space convention).
- When calling from `create_node`, existing boxes are gathered from `parent_node.children` excluding the new node.
- If the safety bound is hit, the error is caught by the caller and logged — node creation still succeeds, the node just stays at its default position.