Implementation:8beeeaaat Touchdesigner mcp Safe Serialize
| Knowledge Sources | |
|---|---|
| Domains | Serialization, JSON |
| Last Updated | 2026-10-28 00:00 GMT |
Overview
Concrete tool for recursively converting arbitrary TouchDesigner Python objects — including TD operators, Page, and Result wrappers — into JSON-safe primitives.
Description
safe_serialize is a recursive serializer that walks an object and produces a structure composed only of JSON-safe types (None, int, float, bool, str, list, dict). It special-cases Result objects (unwrapping success/data or error), TouchDesigner operators (calling obj.eval() for parameter values, or obj.path for OP references), Page objects, and generic objects via __dict__. Anything that cannot be converted falls back to str(obj). This is what allows the controller to json.dumps the response regardless of what the service layer returns.
Usage
Call safe_serialize on any service return value before passing it to json.dumps in the HTTP response path.
Code Reference
Source Location
- Repository: 8beeeaaat_Touchdesigner-mcp
- File: td/modules/utils/serialization.py
- Lines: 1-57
Signature
def safe_serialize(obj: Any) -> Any: ...
Import
from utils.serialization import safe_serialize
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| obj | Any | Yes | Any Python object (TD OP, Result, dict, list, primitive, custom class) |
Outputs
| Name | Type | Description |
|---|---|---|
| return | Any | JSON-safe structure: None, int, float, bool, str, list, dict |
Usage Examples
Serialize a service Result containing TD operators
from utils.serialization import safe_serialize
import json
result = {
"success": True,
"data": {
"nodePath": "/project1/geo1",
"parameters": [op("/project1/geo1").par.tx], # TD Par object
},
}
safe = safe_serialize(result)
# safe["data"]["parameters"][0] -> evaluated par value (float) or path string
json_str = json.dumps(safe)
Serialize a raw TD operator
# An OP with a .path attribute -> returns its path string
path_str = safe_serialize(op("/project1/geo1"))
assert path_str == "/project1/geo1"