LoginSignup
10
11

More than 5 years have passed since last update.

mayapy - Maya内Python

Last updated at Posted at 2016-01-02

これはなに?

Autodesk Maya 内に用意されたツール mayapy についてのメモです。
こちらの記事のMaya部分の続き的なものです

Maya, Houdini, blender, Nuke でのPythonインタープリタ
http://qiita.com/it_ks/items/ae1d0ae01d831c2fc9ae

mayapy

python.exeのMaya版というか。
Mayaの機能を召喚できるpython.exeというか。

ざっくり言うと、Mayaがインストールしてあるなら
(win環境で)別途Pythonをインストールしなくてもコレ使えばいいじゃない。

場所

winでの場所

C:\Program Files\Autodesk¥Maya{ver}\bin\mayapy.exe
Maya本体(maya.exe)と同じ並びにあります。
ダブルクリックすると黒窓が開き、普通にPythonインタープリタとして使えます。
mayapy_boot.png

OSXでの場所

あとで調べて書きます
([追記]コメントで教えていただきました!)

/Applications/Autodesk/maya{ver}/Maya.app/Contents/bin/mayapy

バージョン

Maya2016内蔵のmayapy.exeの
Pythonとしてのバージョンは2.7.6です。

確認

mayapy.exeの起動時にバージョンは表示されますが、Pythonコード内で取得したい場合は

version確認
import sys
sys.version

sys.version_info

あたりで。

init

Mayaの機能を使うためのモジュールが用意されていますが、
import後にイニシャライズする必要があります。
そのあたりはこちらで軽く触れています

オプション

起動時にスクリプトを与えて自動処理させたり、オプションを指定して挙動を替えたりできます。
どういうオプションが用意されているか確認するときは

"C:\Program Files\Autodesk\Maya2016\bin\mayapy.exe" -h

-hもしくは--helpを付けます。
するとこんな感じ(▼)

mayapy-help
usage: C:\Program Files\Autodesk\Maya2016\bin\mayapy.exe [option] ... [-c cmd | -m mod | file | -] [arg] ...

Options and arguments (and corresponding environment variables):
-B     : don't write .py[co] files on import; also PYTHONDONTWRITEBYTECODE=x
-c cmd : program passed in as string (terminates option list)
-d     : debug output from parser; also PYTHONDEBUG=x
-E     : ignore PYTHON* environment variables (such as PYTHONPATH)
-h     : print this help message and exit (also --help)
-i     : inspect interactively after running script; forces a prompt even
         if stdin does not appear to be a terminal; also PYTHONINSPECT=x
-m mod : run library module as a script (terminates option list)
-O     : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x
-OO    : remove doc-strings in addition to the -O optimizations
-R     : use a pseudo-random salt to make hash() values of various types be
         unpredictable between separate invocations of the interpreter, as
         a defense against denial-of-service attacks
-Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew
-s     : don't add user site directory to sys.path; also PYTHONNOUSERSITE
-S     : don't imply 'import site' on initialization
-t     : issue warnings about inconsistent tab usage (-tt: issue errors)
-u     : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x
         see man page for details on internal buffering relating to '-u'
-v     : verbose (trace import statements); also PYTHONVERBOSE=x
         can be supplied multiple times to increase verbosity
-V     : print the Python version number and exit (also --version)
-W arg : warning control; arg is action:message:category:module:lineno
         also PYTHONWARNINGS=arg
-x     : skip first line of source, allowing use of non-Unix forms of #!cmd
-3     : warn about Python 3.x incompatibilities that 2to3 cannot trivially fix
file   : program read from script file
-      : program read from stdin (default; interactive mode if a tty)
arg ...: arguments passed to program in sys.argv[1:]

Other environment variables:
PYTHONSTARTUP: file executed on interactive startup (no default)
PYTHONPATH   : ';'-separated list of directories prefixed to the
               default module search path.  The result is sys.path.
PYTHONHOME   : alternate <prefix> directory (or <prefix>;<exec_prefix>).
               The default module search path uses <prefix>\lib.
PYTHONCASEOK : ignore case in 'import' statements (Windows).
PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.
PYTHONHASHSEED: if this variable is set to 'random', the effect is the same
   as specifying the -R option: a random value is used to seed the hashes of
   str, bytes and datetime objects.  It can also be set to an integer
   in the range [0,4294967295] to get hash values with a predictable seed.

特に重要そうなのが

  • -c
  • -m
  • file

です。

-c

-c cmd : program passed in as string (terminates option list)
-cオプションに続けてPythonコマンドを書きます。
このオプションは、オプションリストの最後に使います(このオプションをつけるとオプションリストが終了します)

-m

-m mod : run library module as a script (terminates option list)
モジュールをスクリプトとして実行します。
このオプションは-cと同じく、オプションリストの最後に使います

file

file : program read from script file
mayapyに実行してほしいファイル(Pythonコードを記述した .py ファイル)を与えます。
なんだかんだで、一番良く使うのはこれかなと思います。

arg

arg ...: arguments passed to program in sys.argv[1:]
オプション以降に書いた項目は、sys.argvの2項め以降に格納されます。

例えば
"C:\Program Files\Autodesk\Maya2016\bin\mayapy.exe" -c "import sys;print sys.argv" test 1234 %date%
このようにすると、
['-c', 'test', '1234', '2015/12/29']
こうプリントされます。
"~"で囲まなくても文字列として扱われ、数字も文字列。%~%による変数も使えます。


備考

環境

参考

外部インタプリタから Python を使用する
http://help.autodesk.com/view/MAYAUL/2016/JPN/?guid=GUID-83799297-C629-48A8-BCE4-061D3F275215

複数のシーンファイルを一括で処理する方法
http://www.dfx.co.jp/dftalk/?p=17829

Maya Python help usage options and environment variables
http://www.cghelpline.com/article/maya-python-help-usage-options-and-environment-variables/133
 ※(▲)最終更新=2012-10-01 15:08:23

Pythonのコマンドラインオプション
http://docs.python.jp/2/using/cmdline.html

10
11
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
11