MOD Class

From TouchDesigner 088 Wiki

The MOD class provides access to Module On Demand object, which allows DATs to be dynamically imported as modules. It can be accessed with the mod object, found in the automatically imported td module. Alternatively, one can use the regular python statement: import. Use of the import statement is limited to modules in the search path, where as the mod format allows complete statements in one line, which is more useful for entering expressions. Also note that DAT modules cannot be organized into packages as regular file system based python modules can be.


Members

The MOD object by default, also contains every reachable DAT module as a member (See Usage below).


Methods


Usage

There are three methods to import DATs as modules in a script.

  • import dat_name
Import the module defined by the DAT named dat_name. All regular import options are supported (from, as, etc). If a DAT is not found, the regular file system search path is then used.
Example: import add_months
  • m = mod.dat_name
Import the module defined by the DAT named dat_name.
Example: m = mod.add_months(1,2,3)
  • m = mod(path)
Similar to above, except a path to the DAT can be used.
Example: m = mod('mymods/adders').add_months(1,2,3)

Comparing Usage

Example using import:

import my_utils
a = my_utils.add(5,6)

Same example using mod:

a = mod.my_utils.add(5,6)

Example using mod outside the search path:

a = mod('/projects/utils/utils_a').add(5,6)

Notice however, that a single import statement will be faster than the case of multiple identical mod statements:

import my_utils
a = my_utils.add(5,6)
b = my_utils.add(5,6)
c = my_utils.add(5,6)

will execute faster than:

a = mod.my_utils.add(5,6)
b = mod.my_utils.add(5,6)
c = mod.my_utils.add(5,6)

However the above could be rewritten more efficiently like this, which would then execute at the same speed as the import statement:

m = mod.my_utils
a = m.add(5,6)
b = m.add(5,6)
c = m.add(5,6)

Search Path

The current component is searched first.

If the DAT is not found, the local/modules component of the current component is then searched.

Next the local/modules component of each parent is successively searched.

If the DAT is still not found, None is returned.