Skip to main content
Inspiring
February 14, 2022
Answered

Need to run a js script automatically from system commands (python subprocess)

  • February 14, 2022
  • 1 reply
  • 1497 views

Hi,  I need a solution to open a specific project file and then execute a js script.  This needs to be an automated process executed through python in Maya.  The script is a list of instructions which updates a template project with shot and camera information which it reads from a json file. This script works correctly when run manually with the template project already open.  But I need an automated process (client's request) to eliminate steps for artists.


I've tried the following without success:
os.system("path/projectFile.aep")

works on some computers and not others. When it works, it opens the correct project file but can't execute the script.


subprocess.run([app,"path/projectFile.aep" ])

will open the correct project file (though I have not tested it on other machines)

 

app = systemPath/AfterFX.exe
js_file = path/automationSript.js

subprocess.run([app, "path/projectFile.aep", "var a = new File("+js_file+" ;, a.open();, eval(a.read());]) 

opens after efffects to the correct project file but does not execute the js script.

 

subprocess.run([app,"var a = new File("+js_file+" ;, a.open();, eval(a.read());]) 

opens after efffects but fails when trying to execute the js script.

Caveats:
The correct project file needs to be opened first before the script executes.  This is because the scipt uses the file path of the current project to locate the json file with all the necessary data, and the script updates pre-built comps, layers, sliders etc within that project file.  This template is editable and customizable by client's supervisors and needs to maintain that flexibility, so the automation script can't build all that from scratch.  It needs to be run after that project file is open in the application.
Please help.
thanks

This topic has been closed for replies.
Correct answer Justin Taylor-Hyper Brew

You need to use a file object, not just a path

app.open(new File(project_file))

 

1 reply

Inspiring
February 14, 2022

I'm getting closer with:

project_file = "path/projectFile.aep"


subprocess.Popen([app ,'-s', "app.open('"+ project_file+"'); var a = new File('"+script_file+"'); a.open(); eval(a.read()); app.exitAfterLaunchAndEval = false;"])

 

but...

subprocess.Popen([app ,'-s', "app.open('"+ project_file+"')])
This fails because it says my project_file is not a file or folder.  It is, and the path and filename are correct, I've verified it by running:  os.system(project_file)
Please help me understand this error.

Still need help, thanks

 

Justin Taylor-Hyper Brew
Community Expert
Community Expert
February 15, 2022

You need to use a file object, not just a path

app.open(new File(project_file))