td_run_python
Run Python code inside TouchDesigner to control operators, create TOPs, set parameters, and position nodes using full path references.
Instructions
Execute Python code inside TouchDesigner. Use op() to reference operators. Examples:
op('/project1/container1') to get an operator
op('/project1').create(constantTOP, 'my_top') to create a TOP
op('/project1/my_top').par.colorr = 1.0 to set a parameter
op('/project1/my_top').nodeX = 200 to position in the network Always use full paths starting with /project1.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | Python code to execute in TouchDesigner |
Implementation Reference
- server.js:39-57 (registration)The 'td_run_python' tool is registered using server.tool() with schema (z.object) and handler.
server.tool( "td_run_python", `Execute Python code inside TouchDesigner. Use op() to reference operators. Examples: - op('/project1/container1') to get an operator - op('/project1').create(constantTOP, 'my_top') to create a TOP - op('/project1/my_top').par.colorr = 1.0 to set a parameter - op('/project1/my_top').nodeX = 200 to position in the network Always use full paths starting with /project1.`, { code: z.string().describe("Python code to execute in TouchDesigner"), }, async ({ code }) => { const result = await tdExecute(code); return { content: [{ type: "text", text: result }], }; } ); - server.js:48-50 (schema)Input schema: code is a required string, described as 'Python code to execute in TouchDesigner'.
{ code: z.string().describe("Python code to execute in TouchDesigner"), }, - server.js:51-56 (handler)Handler function: receives { code }, calls tdExecute(code), and returns { content: [{ type: 'text', text: result }] }.
async ({ code }) => { const result = await tdExecute(code); return { content: [{ type: "text", text: result }], }; } - server.js:14-26 (helper)Helper function tdExecute(code): sends Python code via HTTP POST to TouchDesigner's Web Server DAT at localhost:9980.
async function tdExecute(code) { try { const res = await fetch(`${TD_HOST}:${TD_PORT}`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ script: code }), }); const text = await res.text(); return text; } catch (err) { return `Error connecting to TouchDesigner: ${err.message}. Is TD running with the Web Server DAT on port ${TD_PORT}?`; } }