Implementation:8beeeaaat Touchdesigner mcp first free cell
| Knowledge Sources | |
|---|---|
| Domains | Layout, Geometry |
| Last Updated | 2026-10-28 22:13 GMT |
Overview
Concrete tool for computing the first non-overlapping grid cell position for a newly created node, using a pure-geometry row-major scan with no TouchDesigner runtime dependency.
Description
The first_free_cell function in `node_layout.py` takes a list of existing node boxes, the new node's dimensions, and grid parameters (columns, gap, origin), then scans row-major through grid cells until it finds one that clears all existing boxes with at least `gap` clearance. It is deliberately `import td`-free so it can be unit-tested outside TouchDesigner. Called from `_align_new_node` in `api_service.py` during node creation.
Usage
Call this function when you need to:
- Compute a non-overlapping position for a newly created node.
- Unit-test placement logic without a TouchDesigner runtime.
- Understand or debug why a node was placed at a specific position.
This function is called internally by `create_node`'s helper `_align_new_node` (Step 2 of the Node Creation and Auto-Layout workflow). It is not exposed as a standalone MCP tool.
Code Reference
Source Location
- Repository: touchdesigner-mcp
- File: td/modules/mcp/services/node_layout.py
- Lines: L41-60 (`first_free_cell`); helpers `boxes_overlap` at L16-29, `_cell_box` at L32-38
Signature
def first_free_cell(
existing,
width,
height,
cols=DEFAULT_COLS, # 5
gap=DEFAULT_GAP, # 20.0
origin=(0.0, 0.0),
):
"""Return (x, y) for the first grid cell that clears every existing box.
Args:
existing: List of (x, y, w, h) tuples — boxes of sibling nodes.
width: New node width.
height: New node height.
cols: Grid columns (default 5).
gap: Clearance between nodes (default 20.0).
origin: Grid origin (default (0.0, 0.0)).
Returns:
tuple[float, float]: (x, y) position for the new node.
Raises:
RuntimeError: If no free cell found within _MAX_CELLS (100,000).
"""
Import
from mcp.services.node_layout import first_free_cell
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| existing | list[tuple] | Yes | List of `(x, y, w, h)` boxes of sibling nodes |
| width | float | Yes | New node width |
| height | float | Yes | New node height |
| cols | int | No | Grid columns (default 5) |
| gap | float | No | Clearance between nodes (default 20.0) |
| origin | tuple | No | Grid origin (default (0.0, 0.0)) |
Outputs
| Name | Type | Description |
|---|---|---|
| (x, y) | tuple[float, float] | Lower-left corner position for the new node |
Usage Examples
Basic Placement
from mcp.services.node_layout import first_free_cell
# Existing nodes: two boxes already placed
existing = [(0, 0, 100, 100), (120, 0, 100, 100)]
# Find a free cell for a 100x100 node
x, y = first_free_cell(existing, width=100, height=100)
print(f"Place at ({x}, {y})") # (240, 0) — next cell in the row
Unit Test (No TouchDesigner Needed)
# This module has no `import td`, so it runs in plain Python
def test_first_cell_is_free_when_empty():
x, y = first_free_cell([], width=100, height=100)
assert (x, y) == (0.0, 0.0)
def test_no_overlap_with_existing():
existing = [(0, 0, 100, 100)]
x, y = first_free_cell(existing, width=100, height=100)
# Should be placed at the next cell, not overlapping
assert x >= 120 or y <= -120