FIXED: Touchdesigner crashes on close when using external python libraries

Hello,

I have a conda environment setup and use a execute dat to add it to the python sys.path in TouchDesigner.

When I close a project that uses external libraries imported from the environment (for example jist “import torch” in the python console), TouchDesigner crashes (becomes unresponsive, I have to Force Quit).

Here is the code I am using to add the environment to the path:

# me - this DAT
# 
# frame - the current frame
# state - True if the timeline is paused
# 
# Make sure the corresponding toggle is enabled in the Execute DAT.

import sys
import os
import platform
 
def onStart():
	user = 'user' # Update accordingly
 
	condaEnv = 'td' # Update accordingly
 
	if platform.system() == 'Windows':
		if sys.version_info.major >= 3 and sys.version_info.minor >= 8:
			"""
			Double check all the following paths, it could be that your anaconda 'envs' folder is not in your User folder depending on your conda install settings and conda version.
			"""
			os.add_dll_directory('C:/Users/'+user+'/miniconda3/envs/'+condaEnv+'/DLLs')
			os.add_dll_directory('C:/Users/'+user+'/miniconda3/envs/'+condaEnv+'/Library/bin')
		else:
			"""
			Double check all the following paths, it could be that your anaconda 'envs' folder is not in your User folder depending on your conda install settings and conda version.
			"""
			# Not the most elegant solution, but we need to control load order
			os.environ['PATH'] = 'C:/Users/'+user+'/miniconda3/envs/'+condaEnv+'/DLLs' + os.pathsep + os.environ['PATH']
			os.environ['PATH'] = 'C:/Users/'+user+'/miniconda3/envs/'+condaEnv+'/Library/bin' + os.pathsep + os.environ['PATH']
 
		sys.path = ['C:/Users/'+user+'/miniconda3/envs/'+condaEnv+'/Lib/site-packages'] + sys.path
 
	else:
		"""
		MacOS users should include path to .dlybs / MacOS binaries, site-packages
		"""
		os.environ['PATH'] = '/Users/'+user+'/miniconda3/envs/'+condaEnv+'/lib' + os.pathsep + os.environ['PATH']
		os.environ['PATH'] = '/Users/'+user+'/miniconda3/envs/'+condaEnv+'/bin' + os.pathsep + os.environ['PATH']
		# The following path might need editing (python3.9) based on the python version used with conda
		sys.path = ['/Users/'+user+'/miniconda3/envs/'+condaEnv+'/lib/python3.11/site-packages'] + sys.path
 
	return

def onCreate():
	return

def onExit():
	return

def onFrameStart(frame):
	return

def onFrameEnd(frame):
	return

def onPlayStateChange(state):
	return

def onDeviceChange():
	return

def onProjectPreSave():
	return

def onProjectPostSave():
	return

	

If no libraries have been imported, TouchDesigner closes properly.
I am using a Macbook M1.

Hi @sensormusik,

could you let us know the TouchDesigner version you are using?
Have you experienced this with torch only or could see this with other libraries as well?

cheers
Markus

I am using the non-commercial 2023.11340 build. At first I suspected that it would crash on any modules that aren’t shipped with TD, but upon further testing, modules like Pillow, setuptools, pyyaml worked. torch does not and neither does diffusers. I will try to test some more modules tonight to narrow down the culprit.

Hi @sensormusik,

thanks for the report, this is confirmed, logged and we will be looking at it.

cheers
Markus

Hi @sensormusik

Can you confirm which process you used on MacOS to install those libraries ? Anaconda ? Brew ? Vanilla python ?

Can you confirm you made sure you use everything ARM / M1 and you are not relying on Rosetta anywhere in the pipeline?

Best,
Michel

Sorry for replying so late.

I installed the libraries using miniconda (I followed this article: Anaconda - Managing Python Environments and 3rd-Party Libraries in TouchDesigner | Derivative).

I am using the ARM versions of Touchdesigner and Miniconda (and therefore Python). The following libraries are installed in the conda environement:

accelerate                0.25.0                   pypi_0    pypi
bzip2                     1.0.8                h93a5062_5    conda-forge
ca-certificates           2023.11.17           hf0a4a13_0    conda-forge
certifi                   2023.11.17               pypi_0    pypi
charset-normalizer        3.3.2                    pypi_0    pypi
diffusers                 0.24.0                   pypi_0    pypi
filelock                  3.13.1                   pypi_0    pypi
fsspec                    2023.12.2                pypi_0    pypi
huggingface-hub           0.20.1                   pypi_0    pypi
idna                      3.6                      pypi_0    pypi
importlib-metadata        7.0.1                    pypi_0    pypi
jinja2                    3.1.2                    pypi_0    pypi
libexpat                  2.5.0                hb7217d7_1    conda-forge
libffi                    3.4.2                h3422bc3_5    conda-forge
libsqlite                 3.44.2               h091b4b1_0    conda-forge
libzlib                   1.2.13               h53f4e23_5    conda-forge
markupsafe                2.1.3                    pypi_0    pypi
mpmath                    1.3.0                    pypi_0    pypi
ncurses                   6.4                  h463b476_2    conda-forge
networkx                  3.2.1                    pypi_0    pypi
numpy                     1.26.2                   pypi_0    pypi
openssl                   3.2.0                h0d3ecfb_1    conda-forge
packaging                 23.2                     pypi_0    pypi
pillow                    10.1.0                   pypi_0    pypi
pip                       23.3.2             pyhd8ed1ab_0    conda-forge
psutil                    5.9.7                    pypi_0    pypi
python                    3.11.1          h1456518_0_cpython    conda-forge
pyyaml                    6.0.1                    pypi_0    pypi
readline                  8.2                  h92ec313_1    conda-forge
regex                     2023.12.25               pypi_0    pypi
requests                  2.31.0                   pypi_0    pypi
safetensors               0.4.1                    pypi_0    pypi
setuptools                68.2.2             pyhd8ed1ab_0    conda-forge
sympy                     1.12                     pypi_0    pypi
tk                        8.6.13               h5083fa2_1    conda-forge
tokenizers                0.15.0                   pypi_0    pypi
torch                     2.1.2                    pypi_0    pypi
tqdm                      4.66.1                   pypi_0    pypi
transformers              4.36.2                   pypi_0    pypi
typing-extensions         4.9.0                    pypi_0    pypi
tzdata                    2023d                h0c530f3_0    conda-forge
urllib3                   2.1.0                    pypi_0    pypi
wheel                     0.42.0             pyhd8ed1ab_0    conda-forge
xz                        5.2.6                h57fd34a_0    conda-forge
zipp                      3.17.0                   pypi_0    pypi

Hi @sensormusik

I went back to look at this and I was able to recreate the issue loading the diffusers library on MacOS ARM and quitting TouchDesigner.

I will investigate further with a developer.

Best,
Michel

1 Like

Hi @sensormusik - thanks for the report. This is fixed in the next release of TouchDesigner.