maya-tool-development

Maya tool development – an overview

This article will go through Maya tool development options. We will look at both API and scripting, where main focus will be on scripting comparing MEL, maya.cmds, and PyMel.

Maya API

The API is designed to be as high performing as possible. In the end, your code results in a Maya plug-in. Maya ships with 3 choices for API programming:

  1. C++ API
  2. maya.OpenMaya (Python API 1.0)
  3. maya.api.OpenMaya (Python API 2.0) – Faster & more Pythonic. Newer and still under development.

Next, lets look at Maya scripting.

Maya Scripting

Scripting is a very fast way to develop tools and pipelines and it is iteration friendly. We have three choices when it comes to scripting.

  1. MEL
  2. maya.cmds
  3. pymel

Let’s start by looking at MEL.

MEL

Maya Embedded Language (MEL) has been a part of Maya since it’s initial release in 1998.

Having worked for a long time with MEL, it took me a while to leave the safety of what I knew to embrace Python. Once I did, there was no going back. If you do have to go back – you would feel very sad inside 😀 .

Below is a quick look at the syntax of MEL.

Next, we are going to look at the 2 different python scripting choices Maya offers, but first we will take a look at all the nice things we get by just switching to Python.

Python

A Python interpreter was added in Maya 8.5 and it brought so many capabilities that MEL scripters had not previously had.

One of Python’s main features is listed in PEP 20 – The Zen Of Python, which is “readability counts”. The truth is, the more readable your code is, the faster you and everyone else can work. Some of the other points listed in PEP 20 are not bad either 🙂

  • Readability Counts
  • Simple is better than complex
  • Complex is better than complicated
  • Flat is better than nested
  • Sparse is better than dense
  • There should be one – and preferably only one – obvious way to do it
  • Although that way may not be obvious at first unless you’re Dutch
  • If the implementation is hard to explain, it’s a bad idea
  • If the implementation is easy to explain, it may be a good idea

Below are a few Python favorites that I could not live without. If you look at the standard Python library, you realize the breadth of things you can do with Python out of the box. If there is something that you can not do, someone has written a library that you can download and use from the Python package index. It is mind numbing how many packages there are out there. There are countless articles out there listing the “best” ones if you Google “best Python package 2017“.

String Formatting

String formatting is great, it allows you to have a better overview over the string you are building (compared to concatenation), as well as it can easily cast to different data types.

List Comprehension

I use list comprehension on a daily basis. I wrote a more in-depth post about list comprehension here. The syntax can be seen below.

Complete File System Package

os.walk – traverses directory structures and while doing so give you three parts of the current path in the loop – filename, directory, and root (full path to the filename). Typically tech artists do a lot of directory crawling when we build pipelines and work flows and os.walk is invaluable for this purpose.

Built in JSON, XML & SQL Libraries

Having the ability to use the most common data container formats is key. Element Tree in Python rocks and is very straightforward and easy to use.

In the example below, we parse an XML file, get the tree iterator, get an attribute value, eval getting an attribute (Python trick to enable us to write Python variable types like dictionaries and lists straight into the XML and when evaluated it casts into a the correct type), setting an attribute value, and writing the XML back out.

maya.cmds

Autodesk introduced maya.cmds in Maya 8.5 and it is the module that ships with Maya that wraps all the MEL commands to Maya’s Python interpreter. The syntax of this module is very MEL like and not very “Pythonic”. Because of this, a movement was born to create a better Python implementation called PyMel.

To clearly illustrate the MEL like structure of maya.cmds, I put the MEL command as a comment before each of the maya.cmds equivalents.

PyMel

PyMel has shipped with Maya since Maya 2013 and is a “Pythonic” implementation of all the Maya commands. The PyMel classes are object oriented and are very powerful due to the API hybridization that they bring.

The heart of PyMel is the PyNode, which is what you get when you store a selection or do other types of queries. PyMel can cast all of Maya’s object types into a PyNode object that has a tie to the API node itself. Attached to this object is a wide array of methods that are relevant to the type of node that you are dealing with.

Check out the syntax of pymel below.

If you have not checked out pymel and are coding with maya.cmds, do yourself a favor and have a look at the documentation here.


That wraps up this overview of Maya tool development.

Are you already using PyMel? Love it or hate it? Be social and drop a comment below – I always like hearing from fellow tech artists.

Spread the knowledge

About the Author

Christian Akesson

Facebook Twitter Google+

Senior Technical Artist at Daybreak Games. Maya user since beta 1 in 1999.