TDJSON

From TouchDesigner 099 Wiki
(Redirected from TDJson)

Summary

The TDJSON module provides a variety of Python utility functions for JSON features in TouchDesigner. Most of the functions deal with Custom parameters and Parameter Pages, but some are good for more general use. To use TDJSON put the following line at the top of your Python script:

TDJ = op.TDModules.mod.TDJSON

You can now use any of the following functions by calling:

TDJ.<function name>(...)

See also: JSON Overview

Functions

jsonToText(jsonObject)JSON text

Return a JSON object as text.
  • jsonObject - a JSONable Python object.

jsonToDat(jsonObject, dat)

Write a JSON object into a dat.
  • jsonObject - a JSONable Python object.
  • dat - a DAT to be filled with the JSON object. Other text will be overwritten.

textToJSON(text, orderedDict=True, showErrors=False)JSON object or None

Turn a JSON object stored as text into a Python object. Returns None if conversion fails unless showErrors is True.
  • text - a JSON text string.
  • orderedDict - (Optional) If True, the JSON object returned will be an OrderedDict (from core python module 'collections').
  • showErrors - (Optional) if True, raise an exception for conversion errors, otherwise return None if there is an error.

datToJSON(dat, orderedDict=True, showErrors=False)JSON object or None

Turn a JSON object stored as DAT text into a Python object. Returns None if conversion fails unless showErrors is True.
  • dat - a DAT containing a JSON text string.
  • orderedDict - (Optional) if True, the JSON object returned will be an OrderedDict (from core python module 'collections').
  • showErrors - (Optional) if True, raise an exception for conversion errors, otherwise return None if there is an error.

parameterToJSONPar(p, extraAttrs=None)JSON object

Convert a parameter or tuplet to a jsonable python dictionary. NOTE: a parameter that is a member of a multi-value tuplet will create a JSON object for the entire tuplet.
  • p - the parameter to convert.
  • extraAttrs - (Optional) a list or tuple of attribute names that are not normally stored as part of a parameter's definition e.g. 'val' and 'order'.

pageToJSONDict(page, extraAttrs=None)JSON object

Convert a page of parameters to a jsonable python dict. Format: {parameter name: {parameter attributes, ...}, ...}
  • page - the page to convert.
  • extraAttrs - (Optional) a list or tuple of attribute names that are not normally stored as part of a parameter's definition e.g. 'val' and 'order'.

opToJSONOp(op, extraAttrs=None)JSON object

Convert all custom parameter pages to a jsonable python dict. Format: {page name: {parameter name: {parameter attributes, ...}, ...}, ...}
  • op - the OP to convert.
  • extraAttrs - (Optional) a list or tuple of attribute names that are not normally stored as part of a parameter's definition e.g. 'val' and 'order'.

addParameterFromJSONDict(comp, jsonDict, replace=True, setValues=True, ignoreAttrErrors=False)list of newly created pars

Add a parameter to comp as defined in a parameter JSON dict.
  • comp - the COMP to add parameters to.
  • jsonDict - the parameter JSON dict.
  • replace - (Optional) If False, will cause exception if a parameter already exists.
  • setValues - (Optional) If True, values will be set to parameter's defaults, whether parameter already existed or not.
  • ignoreAttrErrors - (Optional) If True, no exceptions will be raised for invalid attributes in the JSON.

addParametersFromJSONList(comp, jsonList, replace=True, setValues=True, destroyOthers=False)

Add parameters to comp as defined in list of parameter JSON dicts.
  • comp - the COMP to add parameters to.
  • jsonList - the list of par JSON dicts.
  • replace - (Optional) If False, will cause exception if a parameter already exists.
  • setValues - (Optional) If True, values will be set to parameter's defaults, whether parameter already existed or not.
  • destroyOthers - (Optional) If True, pars and pages not in jsonList will be destroyed

addParametersFromJSONDict(comp, jsonDict, replace=True, setValues=True, destroyOthers=False)

Add parameters to comp as defined in dict of parameter JSON dicts.
  • comp - the COMP to add parameters to.
  • jsonDict - the dict of par JSON dicts.
  • replace - (Optional) If False, will cause exception if a parameter already exists.
  • setValues - (Optional) If True, values will be set to parameter's defaults, whether parameter already existed or not.
  • destroyOthers - (Optional) If True, pars and pages not in jsonDict will be destroyed

addParametersFromJSONOp(comp, jsonOp, replace=True, setValues=True, destroyOthers=False)

Add parameters to comp as defined in dict of page JSON dicts ala opToJSONOp.
  • comp - the COMP to add parameters to.
  • jsonOp - the dict of page JSON dicts.
  • replace - (Optional) If False, will cause exception if a parameter already exists.
  • setValues - (Optional) If True, values will be set to parameter's defaults, whether parameter already existed or not.
  • destroyOthers - (Optional) If True, pars and pages not in jsonOp will be destroyed

destroyOtherPagesAndParameters(comp, pageNames, parNames)

Destroys all pages and parameters on comp that are not found in pageNames or parNames.
  • comp - the COMP to destroy pages and parameters on.
  • pageNames - an iterable of names of pages to keep.
  • parNames - an iterable of names of pars to keep.