Implementation:8beeeaaat Touchdesigner mcp exec python script
| Knowledge Sources | |
|---|---|
| Domains | Code_Execution, Remote_Procedure_Call |
| Last Updated | 2026-10-28 22:13 GMT |
Overview
Concrete tool for executing a full Python script in TouchDesigner's runtime, in a namespace that mirrors the Textport, with single-line `eval` and multi-line `exec` semantics, capturing stdout/stderr and extracting a `result` variable.
Description
The exec_python_script method on `TouchDesignerApiService` builds a namespace that mirrors TouchDesigner's interactive Textport (`op`, `ops`, `me`, `parent`, `project`, `td` + all `__main__` globals like `absTime`, OP type names, `tdu`, `ui`), captures stdout and stderr via `contextlib.redirect_stdout`/`redirect_stderr`, and executes the script. Single-line expressions (no newline, no semicolon) are `eval`'d for a direct return value; multi-line scripts are `exec`'d with a `result` variable and last-line expression extraction. A `no_result_sentinel` (a module-level `object()`) distinguishes "no return value set" from "result is None."
Usage
Call this method when you need to:
- Apply a fix that is multi-step or conditional and cannot be expressed as a single method call (Node Error Investigation Step 3).
- Probe the API surface interactively (Python API Lookup Step 4) — e.g. `print(dir(op('/project1/text1')))` to confirm a method exists.
- Write the verified script using signatures confirmed in prior lookup steps (Python API Lookup Step 5).
- Capture a TOP image — the `get_top_image` tool reuses this channel with a generated script (TOP Image Capture workflow).
This method is exposed as the `execute_python_script` MCP tool.
Code Reference
Source Location
- Repository: touchdesigner-mcp
- File: td/modules/mcp/services/api_service.py
- Lines: L472-585 (public method); helper `_process_method_result` in the same file
Signature
class TouchDesignerApiService:
def exec_python_script(self, script: str) -> Result:
"""Execute a Python script directly in TouchDesigner.
Args:
script: Full Python source. The namespace mirrors the TD Textport:
op, ops, me, parent, project, td, and __main__ globals.
Single-line expressions are eval'd; multi-line scripts are
exec'd with a `result` variable and last-line extraction.
Returns:
Result: success_result with {result, stdout, stderr},
or raises Exception on execution failure.
"""
Import
from mcp.services.api_service import TouchDesignerApiService
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| script | str | Yes | Full Python source code to execute |
Outputs
| Name | Type | Description |
|---|---|---|
| result | Any | The script's return value (from `result` variable, `eval`, or last-line extraction); JSON-serialized |
| stdout | str | Captured stdout output during execution |
| stderr | str | Captured stderr output during execution |
Usage Examples
Single-Line Probe
from mcp.services.api_service import TouchDesignerApiService
service = TouchDesignerApiService()
# Single-line expressions are eval'd — returns the value directly
result = service.exec_python_script("op('/project1/text1').par.text")
if result["success"]:
print(f"Current text: {result['data']['result']}")
Multi-Line Script with Result
script = """
node = op('/project1/text1')
node.par.text = 'Hello MCP'
result = node.par.text.eval()
"""
result = service.exec_python_script(script)
if result["success"]:
print(f"Set text to: {result['data']['result']}")
print(f"stdout: {result['data']['stdout']}")
Interactive API Probe
# Confirm a method exists before using it in a larger script
probe = service.exec_python_script("hasattr(op('/project1/text1'), 'saveByteArray')")
if probe["success"] and probe["data"]["result"]:
print("saveByteArray is available")
Related Pages
Implements Principle
- Principle:8beeeaaat_Touchdesigner_mcp_Remote_Code_Execution
- Principle:8beeeaaat_Touchdesigner_mcp_Python_API_Lookup_Discipline