Whats the intended way to split python scripts into different files for touchdesigner?

Hey everyone,

I´m currently implementing a self created API in python for the Touchdesigner Web Server Dat. For several reasons regarding readability and maintainability I need to split up the code into different files. However, I´ve stumbled across some problems:

1.: Whenever I change something in a submodule I have to restart Touchdesigner in order to make sure that the changes are really applied.
2.: I cannot find a way to access the Touchdesigner specific python logic, like op-referencing etc.

I´m quite sure, that I´m not the first one, who wants to split his code into several files and runs into this problems. Can you give me a hint how the wanted workflow looks like to solve these problems?

Thanks in adavance!
Fynn

You are using textDATs with Sync To File on?

There are some instances where Python modules might not know to re-initialize. If you can post examples of this, please post (or send to ivan@derivative.ca) with instructions to reproduce. At worst in the meantime, you can use a script to force cook all relevant modules after changes. op(‘yourModule’).cook(force=True)

This is related to the thing in Python consoles where if you change a module file, you have to reimport it to get the changes.

Hey Ivan,

thanks for your fast response!
test.zip (9.9 KB)
I´ve created a small example project with only a webserver, two scripts, a moviefile and a lfo.
Webserver.py is loaded in the Dat for the webserver callback functions with Sync To File enabled and gets called as it should. No problems if anything changes here. However, right in line 5 of webserver.py I import the second file, touchdesigner.py. In this file it seems like I cannot use the op(…) methods to gain access to the touchdesigner operators. Also, if anything changes here, I have to reload the project file. I will try to use the force cook however and see if that helps.

My general idea is to seperate the implementation of the api we are using (-> webserver.py), which shouldnt change from project to project, and the touchdesigner specific implementation, which might look different, depending on what touchdesigner is used for (-> touchdesigner.py). Is there a way to achieve this?

Best regards,
Fynn

Without looking at your file, I think the best workflow is to load your touchdesigner.py into a second text DAT placed at the same network level as your Webserver DAT (alternatively in any parent‘s local/modules COMP)

Name that DAT touchdesigner and now the import statement in your Webserver should automatically import from that DAT.

if you turn on file sync it will auto update and also reload the module if it changes.

2 Likes

testAK.toe (4.5 KB)

Awesome, that works fine for me. Thank you so much!

1 Like