Saving and Loading Custom Parameters in TouchDesigner
When I'm creating systems in TouchDesigner I'll often make use of the Custom Parameters feature in a Comp, allowing me to create a settings style interface for that Comp. What I also will want to do is save out those settings and have them load in automatically. There's a few ways to this including TDJSON.addParametersFromJSONOp but sometimes you just want a simple system so here's the code I use which saves and load the parameters to a file.
import json
from pathlib import Path
def saveParamValues(comp, filepath):
data = {p.name: p.eval() for p in comp.customPars}
with open(filepath, 'w') as f:
json.dump(data, f, indent=2)
def loadParamValues(comp, filepath):
if Path(filepath).exists():
with open(filepath) as f:
data = json.load(f)
for name, val in data.items():
if hasattr(comp.par, name):
getattr(comp.par, name).val = val
I put this code in an Execute DAT and have the Save method triggered on Exit and the Load method triggered on Start. You could also add a Parameter Execute DAT and have the Save triggered each time a parameter changes.
I've now made this code into it's own component which I can drop into any other component and have it save and load the parents components settings automatically.