Let’s add a test function to our standalone script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
deftest(): import maya.cmds as cmds # full path to your Maya file to OPEN maya_file_to_open = r"C:\Users\Lei\Desktop\test.ma" # Open your file cmds.file(maya_file_to_open, o=True) # full path to your Maya file to IMPORT maya_file_to_import = r"C:\Users\Lei\Desktop\import.ma" # Import the file. the variable "nodes" will hold the names of all nodes imported, just in case. cmds.file(maya_file_to_import, i=True, type="mayaAscii") render = r"C:\Users\Lei\Desktop\te.ma" cmds.file(rename=render) cmds.file(force=True, save=True, options='v=1;p=17', type='mayaBinary') print('a')
This test function opens a maya file on my desktop and import a maya file and save out as another file.
Execute from mayapy
Execute from Command Line
to do so, make sure to add mayapy to your system environment variable.
in system variable: add C:\Program Files\Autodesk\Maya2018\bin to Path
open command prompt, and enter mayapy [directory/firstStandalone.py]
Execute using subprocess
you can do it without going to command line, but by executing another .py using subprocess
1 2 3 4 5 6 7
import subprocess
command = 'mayapy [directory/firstStandalone.py]' process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) process.wait()
print(process.returncode) # this return the message from cmd
Standalone Script with Arguments
Sometimes, we would want to pass arguments to our standalone script
maya_path = 'directory/mayapy.exe'# mayapy or full path to mayapy.exe script_path = 'directory/firstStandalone.py'
defadd_layer(file_names,layer_name): for file_name in file_names: command = r'mayapy {} {} {}'.format( script_path, file_name, layer_name ) process = subprocess.Popen( command, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) process.wait() print(process.returncode) # this return the message from cmd if __name__ == '__main__': # define a list of filenames to iterate through files = ['file1', 'file2', 'file3'] render_layer = 'a new render layer' # run procedure, assuming you've already defined it add_layer(files, render_layer)