Use an external python script to open maya and run another script inside maya

Is it possible to invoke a script from the command line on Windows (or bash on linux) to open Maya and then run a custom script (maybe every time it starts) inside Maya? I am looking for something more elegant than modifying the userSetup file and then launching Maya.

The goal here is to open the .mb file, run the script to place the scene inside, set up a common set of lights, and then render the scene in a specific place and file type. I want to be able to configure this as a scheduled task to check for any new scene files in a directory, and then open maya and go.

Thanks for the help!

+4
source share
2 answers

For something like this, you can use the Maya stand-alone mode instead of the full-scale user interface mode. It's faster. It is ideal for periodic jobs like these. Maya is autonomous - it's just Maya, working without a GUI. After initializing the Maya stand-alone system, you can import and call any scripts you want as part of the original script call. For starters, you can give an example: (Feel free to use this as a link / change it to suit your needs)

In your script, you first initialize the standalone version of Maya.

import maya.standalone
maya.standalone.initialize("Python")

import maya.cmds as cmds
cmds.loadPlugin("Mayatomr") # Load all plugins you might need

This will launch Maya. Now we open and / or import all the necessary files (for example, lights, models, etc.).

# full path to your Maya file to OPEN
maya_file_to_open = r"C:/Where/Ever/Your/Maya_Scene_Files/Are/your_main_maya_file.mb"

# Open your file
opened_file = cmds.file(maya_file_to_open, o=True)

# full path to your Maya file to IMPORT
maya_file_to_import = r"C:/Where/Ever/Your/Maya_Scene_Files/Are/your_maya_file.mb"

# Have a namespace if you want (recommended)
namespace = "SomeNamespaceThatIsNotAnnoying" 

# Import the file. the variable "nodes" will hold the names of all nodes imported, just in case.
nodes = cmds.file(maya_file_to_import, i=True,
                          renameAll=True,
                          mergeNamespacesOnClash=False,
                          namespace=namespace,
                          returnNewNodes=True,
                          options="v=0;",
                          type="mayaBinary" # any file type you want. this is just an example.
                          )

#TODO: Do all your scene setup/ positioning etc. if needed here...
#Tip: you can use cmds.viewFit(cam_name, fitFactor=1) to fit your camera on to selected objects

Maya Batch renderer

render_file = "C:/Where/Ever/Your/Maya_Scene_Files/Are/your_RENDER_file.mb"
cmds.file(rename=render_file)
cmds.file(force=True, save=True, options='v=1;p=17', type='mayaBinary')

import sys
from os import path
from subprocess import Popen

render_project = r"C:/Where/Ever/YourRenderProjectFolder"
renderer_folder = path.split(sys.executable)[0]
renderer_exec_name = "Render"
params = [renderer_exec_name]
params += ['-percentRes', '75']
params += ['-alpha', '0']
params += ['-proj', render_project]
params += ['-r', 'mr']
params += [render_file]
p = Popen(params, cwd=renderer_folder)
stdout, stderr = p.communicate()

! , script Maya Python (Mayapy).

, , : cmds.file() cmds.viewFit() cmds.loadPlugin() Popen

PLUS, - Python , sched (docs), Python.

, . . .

+8

, .

script, Maya, Maya, Kartik. mayapy, , , -, , . python.exe Mayapy , python.

mayapy, standalone.initialize(), Maya - , , script .

Maya script , -c, python. , , (: , mayapy.exe . CD -).

mayapy -c 'import maya.standalone; maya.standalone.initialize(); import maya.cmds as cmds; print cmds.ls()'
>>> [u'time1', u'sequenceManager1', u'renderPartition', u'renderGlobalsList1', u'defaultLightList1', u'defaultShaderList1', u'postProcessList1', u'defaultRenderUtilityList1', u'defaultRenderingList1', u'lightList1', u'defaultTextureList1', u'lambert1', u'particleCloud1', u'initialShadingGroup', u'initialParticleSE', u'initialMaterialInfo', u'shaderGlow1', u'dof1', u'defaultRenderGlobals', u'defaultRenderQuality', u'defaultResolution', u'defaultLightSet', u'defaultObjectSet', u'defaultViewColorManager', u'hardwareRenderGlobals', u'hardwareRenderingGlobals', u'characterPartition', u'defaultHardwareRenderGlobals', u'lightLinker1', u'persp', u'perspShape', u'top', u'topShape', u'front', u'frontShape', u'side', u'sideShape', u'hyperGraphInfo', u'hyperGraphLayout', u'globalCacheControl', u'brush1', u'strokeGlobals', u'ikSystem', u'layerManager', u'defaultLayer', u'renderLayerManager', u'defaultRenderLayer']

mayapy - - -i: mayapy :

mayapy -i -c \"import maya.standalone; maya.standalone.initialize()\""

, .

script, . , , standalone.initalize() script.

mayapy path/to/script.py

userSetup, environmentmnet MAYA_SKIP_USERSETUP_PY , . ; , mayapys bash ( SET EXPORT env vars):

alias mp_zip="export MAYA_DEV=;mayapy -i -c \"import maya.standalone;    maya.standalone.initialize()\""
alias mp_std="export MAYA_DEV=C:/UL/tools/python/ulmaya;export ZOMBUILD='C:/ul/tools/python/dist/ulmaya.zip';mayapy -i -c \"import maya.standalone; maya.standalone.initialize()\""

python Mayapy .

envrionment - , C, Maya commandPort TCP. , RPC python, RPyC ZeroMQ

+3

All Articles