Principle:8beeeaaat Touchdesigner mcp TOP Image Capture
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Image_Capture, Media_Encoding |
| Last Updated | 2026-10-28 22:13 GMT |
Overview
A pattern for capturing the current output of a real-time image-processing node (TOP) as a base64-encoded JPEG, using a temporary downscale node for size control and guaranteed cleanup, without a dedicated API endpoint.
Description
Capturing a still image from a real-time node graph presents several challenges:
- No dedicated endpoint: the runtime may not have a "capture image" API. Instead, the capture is performed by generating a Python script that runs in the script execution channel, reads the TOP's pixel data, and returns it as a base64 string.
- Size control via temporary node: when the TOP's resolution exceeds a maximum, a temporary `resolutionTOP` is chained onto the source to downscale (preserving aspect ratio). This temp node is created in the source's parent, configured with `outputresolution='custom'` and computed `resolutionw`/`resolutionh`, and used as the capture source instead.
- Guaranteed cleanup: the temporary node is destroyed in a `finally` block so the project is left unmodified even if capture fails. This is critical — a leaked temp node would alter the user's network.
- OP type reference compatibility: the temporary node's OP type is referenced via `td.resolutionTOP` (not the bare `resolutionTOP` global) because the bare global is only injected into the exec namespace by newer `.tox` packages. Using `td.resolutionTOP` works against both older and newer deployments.
- Validation: the script validates that the target node exists and is a TOP (`node.family == 'TOP'`) before proceeding. If `saveByteArray` returns empty data, an exception is raised.
Usage
Use this principle when:
- Capturing a snapshot of a TOP node's current output for display to an AI agent.
- Downscaling a high-resolution TOP before capture to limit payload size.
- Building an image capture tool that must not modify the user's project (temp nodes cleaned up).
Theoretical Basis
# Abstract algorithm description (NOT real implementation)
node = op(node_path)
if node is None or node.family != 'TOP':
raise Exception("invalid target")
tmp_top = None
source_top = node
try:
if max_size and max(width, height) > max_size:
# Create temp resolutionTOP for downscale
tmp_top = parent.create(td.resolutionTOP, tmp_name)
tmp_top.inputConnectors[0].connect(node)
tmp_top.par.outputresolution = 'custom'
tmp_top.par.resolutionw = new_w
tmp_top.par.resolutionh = new_h
source_top = tmp_top
# Capture as JPEG
byte_array = source_top.saveByteArray('.jpg')
if not byte_array:
raise Exception("capture failed")
# Base64 encode for transport
result = base64.b64encode(bytes(byte_array)).decode('ascii')
finally:
if tmp_top is not None:
tmp_top.destroy()
The downscale formula preserves aspect ratio:
Practical Guide
- The `maxSize` parameter controls the longer side; the shorter side is computed to preserve aspect ratio.
- Always use `try/finally` for temp node cleanup — even a capture failure must not leak nodes.
- Reference OP types via `td.resolutionTOP`, not the bare global, for cross-version compatibility.
- The result is assigned to a `result` variable, which the script executor extracts as the return value.
Related Pages
Implemented By
Uses Heuristic
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment