Implementation:8beeeaaat Touchdesigner mcp buildGetTopImageScript
| Knowledge Sources | |
|---|---|
| Domains | Image_Capture, Media_Encoding |
| Last Updated | 2026-10-28 22:13 GMT |
Overview
Concrete tool for building a Python script that captures a TouchDesigner TOP node's output as a base64-encoded JPEG, with optional temporary downscale via `td.resolutionTOP` and guaranteed cleanup in a `finally` block.
Description
The buildGetTopImageScript function in `getTopImageScript.ts` is a pure TypeScript function that generates a Python script string. It has no dedicated API endpoint — the generated script is sent over the `execute_python_script` channel. The script validates the target node (exists, is a TOP), optionally creates a temporary `resolutionTOP` for downscaling (preserving aspect ratio), captures via `saveByteArray('.jpg')`, base64-encodes the result, and destroys the temp node in a `finally` block. OP type constants are referenced via `td.resolutionTOP` (not the bare global) for cross-version `.tox` compatibility (issue #185 fix).
Usage
Call this function when you need to:
- Build the Python script for the `get_top_image` MCP tool.
- Capture a TOP node's output with optional downscaling.
- Understand the script generation pattern for tools that reuse the `exec_python_script` channel.
This function is used internally by the `get_top_image` tool definition in `toolDefinitions.ts`. It is not exposed as a standalone MCP tool.
Code Reference
Source Location
- Repository: touchdesigner-mcp
- File: src/features/tools/pythonScripts/getTopImageScript.ts
- Lines: L30-88
Signature
export interface GetTopImageScriptParams {
nodePath: string;
maxSize?: number;
}
export function buildGetTopImageScript({
nodePath,
maxSize,
}: GetTopImageScriptParams): string;
Import
import { buildGetTopImageScript } from "./pythonScripts/getTopImageScript";
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| nodePath | string | Yes | TOP node path (e.g. `/project1/moviefilein1`) |
| maxSize | number | No | Optional max longer-side pixels (1-4096); undefined = no downscale |
Outputs
| Name | Type | Description |
|---|---|---|
| (return) | string | Python script that captures the TOP as base64 JPEG and assigns to `result` |
Usage Examples
Build the Capture Script
import { buildGetTopImageScript } from "./pythonScripts/getTopImageScript";
// Build a script that captures at original resolution
const script = buildGetTopImageScript({
nodePath: "/project1/moviefilein1",
});
// Send `script` over the exec_python_script channel
// Build a script with downscaling (max 512px on the longer side)
const downscaled = buildGetTopImageScript({
nodePath: "/project1/moviefilein1",
maxSize: 512,
});
Generated Script (Simplified)
# The generated Python script (simplified for illustration)
import base64
import td
node = op("/project1/moviefilein1")
if node is None:
raise Exception("Node not found")
if node.family != 'TOP':
raise Exception("Not a TOP")
tmp_top = None
source_top = node
try:
if max_size is not None and max(node.width, node.height) > max_size:
# Create temp resolutionTOP for downscale
tmp_top = node.parent().create(td.resolutionTOP, '__mcp_tmp_res__' + node.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
byte_array = source_top.saveByteArray('.jpg')
if not byte_array:
raise Exception("saveByteArray returned no data")
result = base64.b64encode(bytes(byte_array)).decode('ascii')
finally:
if tmp_top is not None:
tmp_top.destroy()