Writing Maya Plugins

Hey iDevBlogADay,

A while ago we at Limbic decided we need a tighter integration with the content creation tools and the artists. Before, we had stand-alone scripts and programs that would convert common format like .obj into an engine format. However, this doesn’t work very well when the rendering gets more complex, like adding skinning, forward kinematic animations, multiple texture sets, etc. The choices were to look at Maya or 3dsmax, but since 3dsmax is only available on Windows, we decided to go with Maya.

So we started to learn Maya, build up knowledge about the APIs and the setting up a work environment. This turned out to take way longer than expected, as Maya is a quite powerful but somewhat chaotic application. To ease the pain for others, I’d like to post about some of my findings now and then. Todays post will be about the different ways to write a plugin.

There are 3 different ways to write code for Maya: MEL, C++ and Python. Let me go into a few pros and cons for each of these:

  • MEL: this is the original scripting language of Maya. Hence, the support and documentation is quite good, and there are a couple of examples out on the internet. However, it’s a new language to learn, with a syntax that is somewhat Bash-like (which I’m not a huge fan of), and it’s considerably slow, especially when doing operations on many small elements (like vertices, etc). It’s very interesting to note that MEL is used a lot in the UI commands. It seems like (but I haven’t confirmed this) that the entire history in Maya is actually stored as MEL commands, and they can be undone. MEL turnaround time is very fast, you can type in commands directly in the script editor, or write scripts and reload them on the fly. No compilation required.
  • C++: this is the high performance alternative, since it’s the only way to write native code for Maya. It is also the most cumbersome to develop with, as it requires an external IDE, compilation of the plugins. And don’t forget about the cross-platform portability issues (different compilers, different platform APIs, compiling 2 different binaries, etc). It also only directly supports the Maya API (as far as I know, but I’m probably wrong), which is not so well documented and is very low level compared to other APIs (like PyMEL, MEL, etc). That said, it can come in handy when doing a lot of grind work, like working with a lot of vertices/faces, or writing some realtime plugins, but in general I wouldn’t recommend it at least for the first trial-and-error plugin development. It may be the right solution for experienced plugin developers though.
  • Python: I must say I’m slightly biased when it comes to this. I’ve been using Python for many years, it’s used in all of our server systems and we have countless python tools and scripts to ease the day to day work. On the first look, it seemed like Python is  not very well integrated, but that’s actually not true. Python can be used like MEL as a command line script inside Maya, and to write plugins and scripts. These can even be run without running Maya (same goes for C++ I think). The real attractive thing about Python in Maya, though, is the blend between MELs high level abstraction and ease of use and C++ low-level integration. The first is achieved with PyMel, which is a python wrapper of all the MEL commands. The second is achieved by exposing the OpenMaya API, which is essentially the same API that C++ works with as well. Both can be used interchangeably in the same script. It’ll obviously never reach C++ performance levels, but the fast turnaround time,  flexibility and portability nailed it for me.
To sum this up, I’m using Python for almost everything in Maya, and some MEL script if it’s substantially easier (eg. to reload a script and run a command). For the rest of this post, I’ll just post some hints and links on how to more easily use Python with Maya:
  • On MacOS, simply place a python plugin in  /Users/<User>/Library/Preferences/Autodesk/maya/plug-ins/ . It is sadly somewhat undocumented, and may change from version to version, but it works great for Maya 2012. Once put there, the plugin will show up in the Window > Settings > Plugin Manager.
  • Use the following MEL script to reload a plugin on the fly and then execute the command (great for testing exporters like in this example):
unloadPlugin "<pluginname.py>";
loadPlugin "<pluginname.py>";
file -force -options "" -typ "<File Translator Name>" -pr -es "<path>";
  • The Maya API reference for 2012 is available here. Note that this can be used for both the C++ API and the OpenMaya Python API.
  • The PyMEL documentation for 2012 can be found here and the epydoc is here. It also contains a reference for OpenMaya
  • There are some useful examples here and here (the second link really helped me a lot, it’s straight forward to port the examples to Python OpenMaya).
  • The 2012 SDK can be downloaded from here
I hope this will help a few fellow developers out there, and I’m glad if it can save you some time. It sure would’ve saved me some time 🙂
Cheers,
Volker

2 comments on “Writing Maya Plugins

  1. Pingback: Sprint to the Finish « Thesis Journal

Leave a Reply

Your email address will not be published. Required fields are marked *

*

39,874 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>