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 Directed Acyclic Graphic(DAG). In my case, I just need to locate those DAG nodes that store the mesh data. We can traverse the DAG using the iterator 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);
- }
- }
- 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...
- }
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:
This plugin works similar to the Dynamica Plugin (In fact, I learnt a lot from it.), except mine can just define simple shapes with only spheres, boxes and capsule shapes. And my plugin cannot do physics simulation inside Maya, it is just for defining the collision shapes. Those collision shapes (sphere/box/capsule) are just sub-class of MPxLocatorNode by overriding the draw() methods with some openGL calls to render the corresponding shapes.
In conclusions, extracting mesh data directly from Maya is not that hard. We can get all the data such as vertex normals, UV sets and key frame data from Maya and do not need to worry about the data loss during export through another formats, especially animation data. Also Maya provides a convenient API to get those data and it is easy to learn. After familiar with the Maya API, I can also write another plugin to define the collision shapes. Next time when you need to export mesh data, you may consider to extract them directly from the modeling package rather than parsing a file format.
Reference:
[1] MAYA API How-To: http://ewertb.soundlinker.com/api/api.018.php
[2] Maya Exporter Factfile: http://nccastaff.bournemouth.ac.uk/jmacey/RobTheBloke/www/research/index.htm
[3] Rob The Bloke: http://nccastaff.bournemouth.ac.uk/jmacey/RobTheBloke/www/
[4] http://www.vfxoverflow.com/questions/add-remove-framelayouts-in-a-window-using-mel
[5] http://bulletphysics.org/mediawiki-1.5.8/index.php/Maya_Dynamica_Plugin
沒有留言:
發佈留言