Execute Maya script without lauching Maya

前言

開發Maya Tool有兩種方式,參考

  1. maya.standalone
    不用lauch Maya,直接用mayapy執行。
    UI可以用PyQT做。

  2. Maya plugin
    根據官方規定開發格式的py檔,藉由Maya command load到Maya plugin,不過要使用一樣需要lauch Maya,這個網路上已經有相當多介紹了。

測試

主要是參考下面兩篇文章
https://avtechartist.wordpress.com/2014/02/12/mayapy-interpreter-python-in-maya-standalone/
http://www.chadvernon.com/blog/resources/python-scripting-for-maya-artists/python-in-maya/

鬼打牆

不過測試總會出現以下ERROR

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
File read in 0.016 seconds.
Traceback (most recent call last):
File "/home/chi/Desktop/src/PycharmProjects/maya/test.py", line 51, in <module>
main()
File "/home/chi/Desktop/src/PycharmProjects/maya/test.py", line 26, in main
cmds.file(fileToOpen, type='mayaAscii', open=True, prompt=False)
RuntimeError: file: /home/chi/maya/test2.ma line 27: The camera 'perspShape' has no '.ai_translator' attribute.
file: /home/chi/maya/test2.ma line 27: setAttr: No object matches name: .ai_translator
file: /home/chi/maya/test2.ma line 44: The camera 'topShape' has no '.ai_translator' attribute.
file: /home/chi/maya/test2.ma line 44: setAttr: No object matches name: .ai_translator
file: /home/chi/maya/test2.ma line 60: The camera 'frontShape' has no '.ai_translator' attribute.
file: /home/chi/maya/test2.ma line 60: setAttr: No object matches name: .ai_translator
file: /home/chi/maya/test2.ma line 77: The camera 'sideShape' has no '.ai_translator' attribute.
file: /home/chi/maya/test2.ma line 77: setAttr: No object matches name: .ai_translator
file: /home/chi/maya/test2.ma line 90: The mesh 'pCubeShape1' has no '.ai_translator' attribute.
file: /home/chi/maya/test2.ma line 90: setAttr: No object matches name: .ai_translator
Error reading file.
Error reading file.

原本想說會不會是因為我的Maya是教育版,所以每次要load/save scene如果是在Maya都會跳出一個dialog,因此影響了從cammand load檔案。

所以根據這篇的作法取消dialog,在load file的地方把prompt設為False。
cmds.file(fileToOpen, type='mayaBinary', open=True, prompt=False)
結果如果輸入的檔案是mb就會成功,ma就會失敗,而且error一樣存在…

這個問題搞了我整整一天,最後放棄用maya.cmds的寫法。


update 1:
後來根據這個網站的說法,只要有import pymel只要有就OK了,實際測試也真的成功了!
所以鬼打牆終於有解了!

成功寫法

後來我改了code,參考這個寫法,讓程式使用pymel.core來load檔案而不是用maya.cmds

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import os
from sys import argv
import maya.standalone
maya.standalone.initialize(name='python')
import pymel.core as pm
def polycount(wfile):
poly_list = pm.ls(geometry=True)
pm.select(poly_list, replace=True)
polys = pm.polyEvaluate(triangle=True)
# list no. of triangles in output text file
wfile.write("No. of Triangles: " + str(polys) + "\n")
def main():
fileToOpen = argv[1]
filePath = argv[2]
#pm.openFile(fileToOpen, force=True,type='mayaBinary') # If not forced, that in returns an non_saved error
pm.openFile(fileToOpen, force=True, type='mayaAscii')
fileId = open(filePath, "w")
polycount(fileId)
fileId.close()
if __name__ == "__main__":
main()
maya.standalone.uninitialize()
os._exit(0)

在shell中執行類似下面的command:mayapy python_path maya_scene_file output

1
'/usr/autodesk/maya2017/bin/mayapy' '/home/chi/Desktop/src/PycharmProjects/maya/test2.py' '/home/chi/maya/test2.ma' '/home/chi/maya/output'

如果把mayapy的位置加到MAYA_LOCATION,就不用每次都打一大串,就可以直接用mayapy打指令了。

如果沒有加os._exit(0)最後就會出現pymel: hard exiting to avoid mayapy crash...雖然還是能夠跑成功,參考

目標

https://vimeo.com/178309963

參考

  1. Standalone Scripts是在Maya Python API 1.0裡的
    http://help.autodesk.com/view/MAYAUL/2017/CHS/?guid=__files_Maya_Python_API_Using_the_Maya_Python_API_htm
    可以參考官方範例

  2. https://www.quora.com/How-do-I-execute-Maya-script-without-lauching-Maya

======

  1. https://stackoverflow.com/a/44056189

  2. http://help.autodesk.com/cloudhelp/2016/CHS/Maya-Tech-Docs/PyMel/standalone.html

  3. http://discourse.techart.online/t/initialize-maya-standalone-from-cmd-prompt-with-mayapymanager/5795

  4. https://knowledge.autodesk.com/support/maya/learn-explore/caas/CloudHelp/cloudhelp/2016/ENU/Maya/files/GUID-83799297-C629-48A8-BCE4-061D3F275215-htm.html

  5. http://discourse.techart.online/t/maya-python-cant-get-maya-to-initialize-through-external-interpreter-s/1352