execute external python script

Hi

I was wondering if there is a way to execute an external python file from touch.

My script.py is external and not in a touch file.

I just want to find a line of command inside a execute (on start) to run this external script.

Is it possible? Thanks
Capture.JPG
Capture2.JPG

Would it be an option to read in the script as a text and then run it from within Touch?

it could be an option.

But i would like to know first if it possible or not to execute an external .py file that is not in touch with a command line in touch somehow.

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

Hi,

I wanted to resurrect this question:

My script looks like

def number(): return 3

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

[code]import sys

sys.path.append(/path/to/script)[/code]

Thanks!

Tricksy request here…

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)

1 Like

Hey Matthew, I was following your example here and the attached file’s Execute DAT gives errors for onStart(), any ideas?
errors.png

Hrmmmm.

Try changing that onstart DAT to be:

# 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.

1 Like