Experimental:TDJSON

From TouchDesigner Documentation
Revision as of 14:40, 21 October 2019 by Ivan (talk | contribs) (Created page with "=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...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Summary[edit]

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. When the documentation below refers to a "JSON object" it means a JSONable Python object, while "JSON text" is a text representation of such an object.

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, JSON overview

Functions[edit]

jsonToText(jsonObject)JSON text

Return a JSON object as JSON text with tab indents for readability.
  • jsonObject - a JSONable Python object.

jsonToDat(jsonObject, dat)

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

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

Turn JSON text into a JSON 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 JSON text stored in a DAT into a JSON 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, forceAttrLists=False)JSON object

Convert a custom parameter or tuplet to a JSONable Python dict. 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'.
  • forceAttrLists - (Optional) If True, all attributes will be stored in a list with the length of the tuplet

pageToJSONDict(page, extraAttrs=None, forceAttrLists=False)JSON object

Convert a custom page of parameters into 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'.
  • forceAttrLists - (Optional) If True, all attributes will be stored in a list with the length of the tuplet

opToJSONOp(op, extraAttrs=None, forceAttrLists=False, includeCustomPages=True, includeBuiltInPages=False)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'.
  • forceAttrLists - (Optional) If True, all attributes will be stored in a list with the length of the tuplet
  • includeCustomPages - (Optional) If True, include custom par pages
  • includeBuiltInPages - (Optional) If True, include builtin pages

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

Add a parameter to comp as defined in a parameter JSON object.
  • 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.
  • fixParNames - (Optional) If True, force correct capitalization.

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

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
  • newAtEnd - (Optional) If True, new parameters will be sorted to end of page. This should generally be False if you are using 'order' attribute in JSON
  • fixParNames - (Optional) If True, force correct capitalization.

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

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
  • newAtEnd - (Optional) If True, new parameters will be sorted to end of page. This should generally be False if you are using 'order' attribute in JSON
  • fixParNames - (Optional) If True, force correct capitalization.

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

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
  • newAtEnd - (Optional) If True, new parameters will be sorted to end of page. This should generally be False if you are using 'order' attribute in JSON
  • fixParNames - (Optional) If True, force correct capitalization.

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.

An Operator Family that manipulates text strings: multi-line text or tables. Multi-line text is often a command Script, but can be any multi-line text. Tables are rows and columns of cells, each containing a text string.

An Operator Family that contains its own Network inside. There are twelve 3D Object Component and eight 2D Panel Component types. See also Network Path.