Maya 4.0 API Quick Programming Tutorial
Perhaps you have just been asked to write a data exporter for Maya. Odds are you are not working for one of those huge long term game projects that can afford to have several people dedicated 100% to writing export tools, but rather you have a few days to put together a usable tool that will rip some animated character models out of a .mb file. You went to look at all 5000 manuals that ship with Maya, and surprise! None of them appear to be Maya programming manuals.
If you have not done it already you should install a copy of Maya on your system. Install everything the artists will be using. At first I thought maybe all I needed to install was the "developers kit" part of Maya. You should save yourself the trouble and install everthing, including the dev kit. Make sure to install all the documentation too. The API is not documented in the paper manuals, but is in the help files.
In this article I have put together all the things you need to know to get that tool done fast. You can worry about the finer points of the Maya API later.
Maya data exporting tools can be written either as plug-ins or as external programs. With a plug-in you could easily make a module that presents an artist with a button right inside of Maya that they simply press and a data file pops out of Maya in a format compatible with your game engine. The other way is to have the artists save everything as a native Maya file, and to write an external tool that can be run later, maybe as part of your make system, to convert the Maya (.mb or .ma ) files to your own format. Most of the Maya programming examples are of the plug-in type. This article will show you how to write an "application"-style exporter.
Maya is essentially a set of DLLs that modify a specialized database. On top of that there are a whole bunch of plugins and .mel scripts. The Maya program itself appears to be a thin user interface veneer to control the real system. Writing a stand alone application means simply linking with the same Maya DLLs as the Maya program itself uses.
Using Maya's own DLL's to interpret Maya files is the best way. Don't attempt to write your own Maya file parser from scratch. Its tempting at first, when you don't understand the seemingly bizarre Maya API, and its wierd data structures, and the data you want it tantalizingly easy to see in a Maya ascii file. ( You can save any file in an ascii version and just browse through the data structures and sure enough you will find all your vertices, transforms and meshes all pretty clearly expressed in the file ). The problem with writing your own file parser is that once you do that you will have to keep expanding your file parser forever to support more and more of Maya's features, and your artists are sure to be frustrated if they can't use a feature simply because your homegrown file reader is not yet ready to process that data type. Besides its a complete waste of time, because even though the API is bizarre, its not that hard to understand and is pretty forgiving. The downside is that by doing this, any computer that needs to run the exporter must have a Maya license. ( Hey Alias|Wavefront, how about a low cost license to Maya for those workstations that only need to run an exporter? )
A Simple Example
Its easy to write a small program that tells Maya to load a file and let you wander through its data structures:
// initialize Maya, before any Maya operations are performed.
// You might do this in main for example:
MStatus stat = MLibrary::initialize ( "myExporter" );
if ( !stat )
// figure out the file name to open
char* fileName = getFileNameToLoad();
// prepare Maya to read a new scene file
MFileIO::newFile( true );
// read the scene file
stat = MFileIO::open( fileName );
if ( !stat )
// remove any temporary data created by Maya while loading
// things like the undo list which we won't be using
stat = MGlobal::executeCommand( "delete -ch" );
if ( !stat )
// iterate through all the nodes in the DAG, and print out their names
MItDag dagIter( MItDag::kBreadthFirst, MFn::kInvalid, &stat );
for ( ; !dagIter.isDone(); dagIter.next())
stat = dagIter.getPath( dagPath );
cerr << "Found DAG Node: "
// now shut down Maya, or if you want to process another file,
// just make another call to MFileIO::newFile(), and MFileIO::open()
Compile it, and link it with these .DLLs:
Foundation.lib OpenMaya.libAs you use more of Maya's features in your exporter you will need to link with more of its .DLLs. You can either let the link errors be your guide, or you can just link with everything and forget about it. Be aware that linking with everything may mean that people running your exporter may need to have a license to the full Maya, instead of just a license for Maya Builder.