使用外部python脚本打开Maya和运行在Maya中另一个脚本 [英] use external python script to open maya and run another script inside maya

查看:3964
本文介绍了使用外部python脚本打开Maya和运行在Maya中另一个脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能调用从命令提示符窗口中的脚本(或在Linux中的bash),打开Maya和随后运行自定义脚本中玛雅(每次其运行可能改变)?我寻找的东西有点不是改变userSetup文件,然后运行玛雅更优雅。

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.

这里的目标是能够打开一个文件.mb,运行一个脚本里面的场景定位,建立一套通用的灯,然后渲染场景到一个特定的位置和文件类型。我希望能够到其设置为计划任务来检查目录中的任何新的场景文件,然后打开玛雅去。

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.

感谢您的帮助!

推荐答案


对于这样的事情,你可以使用Maya独立的,而不是完全成熟的UI模式。它比较快。它非常适合喜欢这些批处理调度作业。玛雅独立只是玛雅运行没有图形用户界面。一旦你已经初始化,您的玛雅独立,可以导入并调用任何你想要的脚本,作为原始调用脚本的一部分。要开始,你在这里下车是一个例子:(随意使用此作为参考/修改以满足您的需求)

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

这将得到玛雅运行。现在我们打开和/或进口的所有必要的文件(EGS,灯光,模型等)

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

这就是它!当然,你的脚本将不得不使用Maya的Python的跨preTER(Mayapy)中运行。

That's it! Of Course, your script will have to be run using Maya's Python interpreter (Mayapy).

做检查出文档的使用更多选项,ESP的所有命令:
cmds.file()
cmds.viewFit()
<一href=\"http://download.autodesk.com/us/maya/2009help/CommandsPython/loadPlugin.html\">cmds.loadPlugin()
子流程和POPEN

Do check out the docs for all the commands used for more options, esp.: cmds.file() cmds.viewFit() cmds.loadPlugin() Subprocess and Popen

另外,因为Python的迷死人,你可以使用的模块,比如章附表docs )安排此方法的运行在你的Python code。

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.

这篇关于使用外部python脚本打开Maya和运行在Maya中另一个脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆