Hi Jacobite.
It is possible to import external modules in python and access them, provided they are 3.2 compatible.
Is that what you have in mind?
Here’s more information on that example: derivative.ca/wiki088/index. … ng_Modules
I’m trying to do this without reading the script as text and running it from within Touch. I’ve tried to import the file as per usual to python, does not work.
import script
print(script.number())
I have also tried adding the file location to my system path via
So a few things. Let’s say you have an external script / function:
[code]import sys
here’s your function
def HelloWorld():
# note that we need the return to be a string
return ‘12’
here you call your function and pass the results
to stdout.write()
sys.stdout.write(HelloWorld())[/code]
Notice that you need to use sys.stdout.write() to move your results into touch / another process. You’ll also notice that you need to encode your results as a string.
In touch you’ll end up with something like:
[code]import subprocess
this is the location of your script
SCRIPT = ‘{}/start-py.py’.format(project.folder)
here you call your subprocess call, you need to pass
python as an argument for what’s opening the file
as well as the file you want to open with python
you’ll capture the output of
val = subprocess.Popen([‘python’, SCRIPT], stdout=subprocess.PIPE, shell=True)
this is the raw value
print(val.communicate()[0])[/code]
Subprocess is used to execute a process in the windows shell - you need to pass out the application you expect to open the file, and then the path to the file. The results can be retrieved with .communicate() - though you’ll want the first item in the tuple.
You’ll notice that those results are prepended with a ‘b’ which tells you that the results are in bytes. To get the decoded val you ll need to do something like:
import subprocess
# this is the location of your script
SCRIPT = '{}/start-py.py'.format(project.folder)
# here you call your subprocess call, you need to pass
# python as an argument for what's opening the file
# as well as the file you want to open with python
# you'll capture the output of
val = subprocess.Popen(['python', SCRIPT], stdout=subprocess.PIPE, shell=True)
# the 'b' here represents the fact that the data
# is included in bytes, to strip this down to the
# contents of the return we need to decode the results
print(val.communicate()[0].decode('utf-8'))[/code]
Here’s an example file with external script to help pull apart what’s happening: subprocess.zip (4.54 KB)
# 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 subprocess
def onStart():
# this is the location of your script
SCRIPT = '{}/start-py.py'.format(project.folder)
# here you call your subprocess call, you need to pass
# python as an argument for what's opening the file
# as well as the file you want to open with python
# you'll capture the output of
val = subprocess.Popen(['python', SCRIPT], stdout=subprocess.PIPE, shell=True)
# the 'b' here represents the fact that the data
# is included in bytes, to strip this down to the
# contents of the return we need to decode the results
decode_val = val.communicate()[0].decode('utf-8')
print(decode_val, type(decode_val).__name__)
# use the returend value to set a constant in Touch
try:
op('constant1').par.value0 = float(decode_val)
except:
print(type(decode_val))
print('oh, bother - something went wrong with Python')
return
onStart()
I don’t normally like try and excepts, but this should be a way to get a result that won’t throw and error.
EDIT
I should point out that this approach assumes you have python 3+ installed on your machine, and that the path variable is correctly setup to accept ‘python’ as an argument. I’ve also only tested this on Windows, so results may be different on Mac.