Executing external python scripts with MAYA/PS

Hey all, I’m trying to fulfill a request from our engineers that an exporting script from Maya and Photoshop (separately) execute an external python script that operates on the exported files. I’ve been checking around without much luck yet, so I was wondering if anyone can nudge me in the right direction?

How can I (if possible) execute an external python script with MEL/Python in Maya? Can I also do this with javascript in Photoshop?

If anyone’s had experience with this kind of thing, I would really appreciate the guidance.

Assuming that it uses the same Python version (2.6 for Maya 2011 for example) and that the script is in your Python path, just import it and use it like any other python module.

If I’m understanding correctly you want

execfile(“somefile.py”, local_vars, global_vars)

this is gone in python 3k but the alternative is…

exec(compile(open(“somefile.py”).read(), “somefile.py”, ‘exec’), global_vars, local_vars)

-Dave

Just use the subprocess module (or os.popen* in Maya if you need the stdout/stderr, since Maya can’t use subprocess’ pipes) to launch the python exe with the script and args.

Excellent, thanks for the tips! :cool: Yes, I do have to pass arguments to the script, so I’ll give Rob and TD’s methods a shot tonight and post results when I get them. I’m getting up to speed on using Python more often for Maya scripting, and I’m still getting a feel for it.

Any ideas how/if it is possible to make this happen from within a photoshop script (using javascript)?

I think you have to use vbscript? I don’t think javascript can call out to the disk. But I may be misremembering some bad info.

You can call execute on a File object in JavaScript, but it just runs the default action on the file as though you’d double-clicked it in a file browser. There’s no way to check for execution success.

If you need to pass params to whatever you’re executing, about the only way to do that with ExtendScript JavaScript out of the box is to write a batch file to disk then execute that. Bleh…

If you need to open a pipe from JavaScript to capture app output and return values, or even just wait until execution has finished, it’s possible – but more work. I wrote a JavaScript extension that opens up a pipe to another app, and load that into my script using the ExternalObject object. You can read about how to write an extension in the Javascript Tools Guide. There’s sample code included with the ExtendScript SDK.

JavaScript has a few advantages. Writing a JavaScript Resource lets you control how your script appears in the menu with naming and categorization, and allows you to set enable conditions. There’s also action manager automation with parameters, which can be helpful if your script is run in a variety of contexts that the user wants to automate themselves by recording actions. JavaScript files can also be invoked using the Script Events Manager (on startup, open, close, etc) while VBScripts cannot.

The ExtendScript toolkit which ships with PhotoShop is a full-featured debugger for your JavaScripts. For VBScript you need a different debugger (you can use Visual Studio, but the setup is a pain so the experience is no where near as slick).

The biggest advantage, though, is that there’s many more JavaScript examples out there to learn from, which might save you some time. Very few people use VBScript with PhotoShop, and if they do they don’t advertise their work.

But if you don’t need any of those things, and you aren’t married to JavaScript, look into VBScript like Rob suggests.

Can you call back and forth between vbscript and javascript? I seem to remember someone’s code being able to call from vbscript into js?

One way, VBScript to Javascript.

Thanks for the tip Bronwen! I made some progress on that front yesterday. This might be a little inelegant, but yes, the File object in jsx has an execute method… so basically I’m writing to a file containing the call to python and the arguments, then executing it. This opens the file in the terminal (I’m in OSX, here) and runs the command in the shell. I’ll post some example code later that describes the solution, in case anybody is interested in future. This is my first shot at writing Photoshop tools, so it’s pretty exciting to me :):