【问题标题】:use external python script to open maya and run another script inside maya使用外部 python 脚本打开 Maya 并在 Maya 中运行另一个脚本
【发布时间】:2015-02-10 19:34:06
【问题描述】:

是否可以从 windows 中的命令提示符(或 linux 中的 bash)调用脚本来打开 Maya,然后在 Maya 中运行自定义脚本(可能每次运行时都会更改)?我正在寻找比更改 userSetup 文件然后运行 ​​Maya 更优雅的东西。

这里的目标是能够打开一个 .mb 文件,运行脚本来定位场景,设置一组通用的灯光,然后将场景渲染到特定的位置和文件类型。我希望能够将其设置为计划任务,以检查目录中的任何新场景文件,然后打开 Maya 并继续。

感谢您的帮助!

【问题讨论】:

  • 您希望 Maya 以批处理模式而不是 gui 模式运行吗?为此,我会这样做。

标签: python bash batch-file maya pymel


【解决方案1】:

对于这样的事情,您可以使用 Maya 独立而不是完整的 UI 模式。它更快。它非常适合像这样的批量计划作业。 Maya 独立版只是 Maya 在没有 GUI 的情况下运行。独立初始化 Maya 后,您可以导入和调用所需的任何脚本,作为原始调用脚本的一部分。从这里开始是一个例子:(随意将其用作参考/修改它以满足您的需求)

在您的脚本中,您首先独立初始化 Maya。

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

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

这将使 Maya 运行。现在我们打开和/或导入所有必要的文件(例如灯光、模型等)

# 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 渲染器来渲染它

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 解释器 (Mayapy) 运行。

请查看文档以了解用于更多选项的所有命令,尤其是: cmds.file() cmds.viewFit() cmds.loadPlugin() Subprocess and Popen

另外,由于 Python 的强大功能,您可以使用 sched (docs) 之类的模块来安排此方法在您的 Python 代码中的运行。

希望这很有用。玩得开心。干杯。

【讨论】:

    【解决方案2】:

    很大程度上取决于您需要做什么。

    如果您想运行一个可以访问 Maya 功能的脚本,您可以运行一个 Maya 独立实例,如 Kartik 的回答中所述。与您的 maya 安装在同一文件夹中的 mayapy 二进制文件是 Maya python 解释器,您可以像运行 python.exe 一样直接运行它,Mayapy 具有与常规 python 解释器相同的命令标志。

    在 mayapy 会话中,一旦您调用 standalone.initialize(),您将拥有一个正在运行的 Maya 会话 - 除了少数例外,就好像您在常规 Maya 会话中的脚本选项卡中运行一样。

    要强制 Maya 在启动时运行特定脚本,您可以调用 -c 标志,就像在 python 中一样。例如,您可以像这样启动一个 maya 并打印出一个空场景的内容(注意:我假设 mayapy.exe 在您的路径上。您也可以直接 CD 到 maya bin 目录)。

    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 - 实际上是命令行版本的 maya - 使用 -i 标志:这将启动 mayapy 并为您提供命令提示符:

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

    它再次为您启动独立,但保持会话继续进行,而不是运行命令并退出。

    要运行脚本文件,只需将文件作为参数传入即可。在这种情况下,您需要按照 Kartik 的建议进行操作,并在脚本中包含 standalone.initalize()。然后用

    调用它
    mayapy path/to/script.py
    

    要抑制 userSetup,您可以创建一个名为 MAYA_SKIP_USERSETUP_PY 的环境变量并将其设置为非零值,这将在不运行 usersetup 的情况下加载 Maya。您还可以在运行 mayap 之前更改环境变量或路径变量;例如,我可以使用这两个 bash 别名从两个不同的环境运行 mayapys(在 Windows 中,您将使用 SET 而不是 EXPORT 来更改环境变量):

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

    This blog post 包含一个 python 模块,用于根据需要在不同环境中启动 Mayapy 实例。

    如果您想与来自另一个环境的正在运行的 Maya 交互 - 例如,如果您尝试从手持设备或 C 程序远程控制它 - 您可以使用 Maya commandPort 通过 TCP 处理简单请求.对于更复杂的情况,您可以设置自己的基本远程服务like this,或使用预先存在的python RPC 模块,如RPyCZeroMQ

    【讨论】:

    • 这里有一些非常甜蜜的宝石!谢谢@Theodox。特别是。喜欢有关 ZeroMQ 的建议,以及您关于多 Mayapy 狂热的博文。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-20
    • 1970-01-01
    • 2019-06-25
    • 1970-01-01
    相关资源
    最近更新 更多