Working with .tosc files#

Once you have learned the structure of the .tosc file and tosclib, it becomes easier to focus on the algorithm of whatever you want to do rather than dealing with raw XML Element Tree functions and parsing.

The basic concept is to load a regular XML Element and create a new ElementTOSC instance with it. Then you can handle it with tosclib and regular xml.

We can start by loading a root Element and passing it’s first child to ElementTOSC.#
import tosclib as tosc

root = tosc.load("docs/demos/files/test.tosc")
parent = tosc.ElementTOSC(root[0])
Then we can use ElementTOSC methods to find navigate through the tree.#
target = tosc.ElementTOSC(parent.findChild("target"))
target.showProperty("color")
Finally we can modify the Elements, for example changing the ‘color’ Property.#
colors = {"r":"0", "g":"0", "b":"1", "a":"1"}
target.setProperty("color", params = colors)
Alternatively, we can use a shortcut instead to set the color property directly:#
target.setColor((1, 0, 0, 1))
target.showProperty("color")
In order to write back to .tosc, we use the original root Element with the tosclib write function.#
tosc.write(root, "docs/demos/files/out.tosc")

Classes and Functions#

Here are more details on the functions and methods we used:

tosclib.tosc.load(inputPath)[source]#

Reads a .tosc file and returns the XML root Element

Parameters

inputPath (str) –

Return type

xml.etree.ElementTree.Element

class tosclib.tosc.ElementTOSC[source]#

Contains a Node Element and its SubElements. Creates them if not found.

__init__(e)[source]#

Find SubElements on init

Parameters

e (ET.Element) – <node> Element

properties#

Find <properties>

Type

ET.Element

values#

Find <values>

Type

ET.Element

messages#

Find <messages>

Type

ET.Element

children#

Find <children>

Type

ET.Element

__new__(**kwargs)#
class tosclib.tosc.Property[source]

Struct like object to carry the property values

__init__(type, key, value=None, params=None)[source]
Parameters
__new__(**kwargs)
tosclib.tosc.ElementTOSC.createProperty(self, property)#
Parameters

property (tosclib.elements.Property) –

Return type

bool

tosclib.tosc.ElementTOSC.setColor(self, params)
Parameters

self (tosclib.tosc.ElementTOSC) –

tosclib.tosc.write(root, outputPath)[source]#

Encodes a root Element to .tosc

Parameters
Return type

bool