Catching outputs of external processes into Textport or DAT

I display here the results of my investigations about dealing with external programs (via command line) in Touch Designer. There may be other python things that one can implement further, and it may be improved in some ways like further processing of command outputs directly into the scripts, but it seems to work correctly as it is.

Supposing that you have a console program named: console.exe
at “C:\Program Files (x86)\console\console.exe”:

In Tscript, one can launch the console.exe application but capturing the output seems to only work with built-in windows shell commands:

[code]#Run a shell command and catch its output to Result DAT

cf "C:/Program Files (x86)/console/
system “cmd /c DIR” > Result

#Run the program but can’t catch its output

cf "C:/Program Files (x86)/console/
system “cmd /c console.exe” > Result [/code]

In Python, it is a bit longer, but it is possible to catch both application output and its errors messages (scroll the frame for all the code):

[code]import subprocess

#Command to run, with arguments

command = “C:/Program Files (x86)/console/console.exe arg1 arg2”

#Process the command in Python

process = subprocess.Popen(command.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)

#Outputs Error in textport and in Error DAT

for line in process.stderr:
print(line)
op(‘Error’).write(line)

#Outputs Console Output to Result DAT

for line in process.stdout:
print(line)
op(‘Result’).write(line)[/code]

For intricated and complex command lines, one can use shlex for parsing the arguments, if needed:

[code]import shlex, subprocess

#Command to run

command = “C:/Program Files (x86)/console/console.exe -m arg1 arg2 ‘path’”

#Process the command in Python

process = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)

#Outputs Error in textport and in Error DAT

for line in process.stderr:
print(line)
op(‘Error’).write(line)

#Outputs Console Output to Result DAT

for line in process.stdout:
print(line)
op(‘Result’).write(line)
[/code]

Any comment or technical remark appreciated.

Thanks for this! Since no comments, assume it works well?

Oh - digging more in the forum, would this block the main Touch loop?

Bruce