Handy Python Modules

Maybe we should have a thread of useful python modules and examples that we know work in touchdesigner and how to use them?

Heres a couple I’ve been using that people might find handy:

Requests
easy_install requests

An HTTP Library thats particularly useful for file uploading (Apparently the Web DAT does this but if it does I don’t have a clue how to use it.)

import sys

# set the following to the requests module path
sys.path.append("C:/Python33/Lib/site-packages/requests-2.3.0-py3.3.egg")

import requests

# set php/whatever script to upload to
url = 'http://yoursite.com/upload.php'
files = {'file': open('C:/thisismyfile.jpg', 'rb')}
with requests.Session() as session:
    r = session.post(url, files=files, stream=True)

smtplib
Standard Library

Send email from within TouchDesigner, the following example sends an email from a gmail account.


import smtplib

gmail_user = "yourname@gmail.com"
gmail_pwd = "yourpassword"
FROM = 'yourname@gmail.com'
TO = ['afriend@something.com'] #must be a list
SUBJECT = "Hello from TouchDesigner"
TEXT = "Hows it going?"

# Prepare actual message
message = """\From: %s\nTo: %s\nSubject: %s\n\n%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
try:
     #server = smtplib.SMTP(SERVER) 
     server = smtplib.SMTP("smtp.gmail.com", 587) #or port 465 doesn't seem to work!
     server.ehlo()
     server.starttls()
     server.login(gmail_user, gmail_pwd)
     server.sendmail(FROM, TO, message)
     #server.quit()
     server.close()
except:
     print("failed")

Thanks for the smtplib sample Richard.

Any easy way to have this not hang touch while it’s connecting to the server?
Else I’m used to using the subprocess module and launch this externally.

Well you can put it into another thread although Malcolm says that anything op related that accesses another thread is very dangerous and may crash, if it’s not accessing other ops it might be safe? I’ve been running a seperate toe file with any long scripts in them just to be safe.

Hello…

I tried this options but doesn´t work…

Any other idea?

Regards!