Tools are very important in game production, especially when you are working with someone who cannot write code. In my project, I worked with 2 artists, so I need to write some tools to export their models to my engine. There are different choices to export the models, you can parseCOLLADA files… But I choose to extract it directly from the modeling package that the artists use – Writing Maya plugin to extract the model data.
To write Maya plugin for exporting models, we should know how data are stored in Maya first. Basically, Maya stores most of its data (e.g. meshes, transformation…) in a MItDag like this:
- MStatus status;
- MItDag dagIter( MItDag::kDepthFirst, MFn::kInvalid, &status );
- MDagPathArray meshPath; // store the DAG nodes that contains mesh
- for ( ; !dagIter.isDone(); dagIter.next())
- {
- MDagPath dagPath;
- status = dagIter.getPath( dagPath );
- if ( status )
- {
- MFnDagNode dagNode( dagPath, &status );
- // Filter out the DAG nodes that do not contain mesh
- if ( dagNode.isIntermediateObject()) continue;
- if ( !dagPath.hasFn( MFn::kMesh )) continue;
- if ( dagPath.hasFn( MFn::kTransform )) continue;
- meshPath.append(dagPath);
- }
- }
then, we can get the mesh data in the DAG nodes using the MFnMesh like this:
- for(int i=0; i< meshPath.length(); ++i)
- {
- MDagPath dagPath= meshPath[i];
- MFnMesh fnMesh( dagPath );
- MPointArray meshPoints;// store the position of vertices
- fnMesh.getPoints( meshPoints, MSpace::kWorld );
- // get more mesh data such as normals, UV…
- }
For the details of getting the mesh data, you may refer to objExport.cpp.
Another reason I choose to write plugin instead of parsing .fbx/COLLADA is because of extracting the animation data. In my project, I just need to export some simple animations which linear interpolates between key frames, and I would like to get the key frames defined by artists in Maya. I have tried using the FBX SDK but when exporting animation data, it bakes all the animation frames as key frames… Using COLLADA get even worse because I cannot find a good exporter for Maya on the Mac platform… So writing Maya plugin can get rid of all these problems and get the data I want. I can also write a script for artists to set the animation clip data: