python运用maya_使用外部python脚本打开Maya和运行在Maya中另一个脚本

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!

解决方案

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.

import maya.standalone

maya.standalone.initialize("Python")

import maya.cmds as cmds

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

That will get Maya running. Now we open and/or import all the files necessary (egs. 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

Now we save this file out and call Maya Batch renderer to render it out

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()

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值