Is it possible to call a script from the command prompt in windows (or bash in linux) to open Maya and then subsequently run a custom script (possibly changing each time its run) inside Maya? I am searching for something a bit more elegant than changing the userSetup file and then running Maya.
The goal here is to be able to open a .mb file, run a script to position the scene inside, setup a generic set of lights and then render the scene to a specific place and file type. I want to be able to set this up as a scheduled task to check for any new scene files in a directory and then open maya and go.
Thanks for the help!
A lot depends on what you need to do.
If you want to run a script that has access to Maya functionality, you can run a Maya standalone instance as in Kartik's answer. The
mayapy
binary installed in the same folder as your maya is the Maya python interpreter, you can run it directly the same way you'd runpython.exe
Mayapy has the same command flags as a regular python interpreter.Inside a mayapy session, once you call
standalone.initialize()
you will have a running Maya session - with a few exceptions, it is as if you were running inside a script tab in a regular maya session.To force Maya to run a particular script on startup, you can call the
-c
flag, just the way you would in python. For example, you can start up a maya and print out the contents of an empty scene like this (note: I'm assumingmayapy.exe
is on your path. You can just CD to the maya bin directory too).You can run mayapy interactively - effectively a command line version of maya - using the -i flag: This will start mayapy and give you a command prompt:
which again starts the standalone for you but keeps the session going instead of running a command and quitting.
To run a script file, just pass in the file as an argument. In that case you'd want to do as Kartik suggests and include the
standalone.initalize()
in the script. Then call it withTo suppress the userSetup, you can create an environmnet variable called
MAYA_SKIP_USERSETUP_PY
and set it to a non-zero value, that will load maya without running usersetup. You can also change environment varialbes or path variables before running the mayap; for example I can run mayapys from two different environments with these two bash aliases (in windows you'd use SET instead of EXPORT to change the env vars):This blog post includes a python module for spinning up Mayapy instances with different environments as needed.
If you want to interact with a running maya from another envrionment - say, if you're trying to remote control it from a handheld device or a C program - you can use the Maya commandPort to handle simple requests via TCP. For more complex situations you could set up a basic remoting service like this of your own, or use a pre-exiating python RPC module like RPyC or ZeroMQ
For something like this you can use Maya standalone instead of the full blown UI mode. It is faster. It is ideal for batch scheduled jobs like these. Maya standalone is just Maya running without the GUI. Once you have initialized your Maya standalone, you can import and call any scripts you want, as part of the original calling script. To start you off here is an example: (Feel free to use this as a reference/modify it to meet your needs)
In your script you first initialize Maya standalone.
That will get Maya running. Now we open and/or import all the files necessary (egs. lights, models etc.)
Now we save this file out and call Maya Batch renderer to render it out
That's it! Of Course, your script will have to be run using Maya's Python interpreter (Mayapy).
Do check out the docs for all the commands used for more options, esp.: cmds.file() cmds.viewFit() cmds.loadPlugin() Subprocess and Popen
PLUS, because of the awesomeness of Python, you can use modules like
sched
(docs) to schedule the running of this method in your Python code.Hope this was useful. Have fun with this. Cheers.