Author: | Matthias Baas (mbaas@users.sourceforge.net) |
---|---|
Date: | 2006/10/10 |
Using the Maya/Python package it is possible to write true plug-ins in the Python programming language instead of C++. This tutorial demonstrates how a Python plugin is written and activated. It does not explain how to write Maya plug-ins in general but assumes that you already have some knowledge about writing Maya plug-ins in C++.
Writing plug-ins in Python virtually works the same as in C++. You implement your plug-in class that also must derive from one of the proxy classes (MPx...) and register/deregister them in the initializePlugin() / uninitializePlugin() functions. The plug-in is then loaded using the Python plug-in manager.
Here is the code for a Python plug-in that defines a new MEL command. For simplicity, the new command will just print some text so that we see that the code actually gets executed.
Save this code into a file spamcmd.py and place it into your maya/<version>/plug-ins directory (where you could also put C++ plug-ins). The Python plug-in manager will search the same locations where the regular plug-in manager searches for C++ plug-ins.
If you haven't already initialized the Maya/Python package then do so now by invoking py "import maya.initmaya"; in the Script Editor (see the manual). Once the package is initialized you can invoke the Python plug-in manager from the menu (Window -> Settings/Preferences -> Python Plug-in Manager...). You should now see a window similar to the following that displays the Python plug-in spamcmd.py somewhere:
Check the "loaded" check box to load the plug-in. If your Python plug-in would raise an exception you would see the Python traceback in the output window. If there has not been ano error you can open the MEL Script Editor and enter the command spam which will result in the text Spam & Eggs being printed into the output window.
One thing to note is the line
in the initializePlugin() / uninitializePlugin() functions. In contrast to the C++ class the Python MFnPlugin class only takes one argument which is the object that was passed to the sourcepy plug-in. The additional arguments such as vendor, version, etc. cannot be set because every Python plug-in runs in the same context as the sourcepy plug-in and, as far as Maya is concerned, they are no separate plug-ins.