exportJointClusterDataCmd.cpp
#include <math.h>
#include <maya/MPxCommand.h>
#include <maya/MStatus.h>
#include <maya/MArgList.h>
#include <maya/MFnPlugin.h>
#include <maya/MObject.h>
#include <maya/MGlobal.h>
#include <maya/MDagPath.h>
#include <maya/MItSelectionList.h>
#include <maya/MSelectionList.h>
#include <maya/MFloatArray.h>
#include <maya/MObjectArray.h>
#include <maya/MItDependencyNodes.h>
#include <maya/MItGeometry.h>
#include <maya/MFnWeightGeometryFilter.h>
#include <maya/MFnGeometryFilter.h>
#include <maya/MItGeometry.h>
#include <maya/MPlug.h>
#include <maya/MPlugArray.h>
#include <maya/MFnSet.h>
#include <maya/MIOStream.h>
#define CheckError(stat,msg) \
if ( MS::kSuccess != stat ) { \
displayError(msg); \
continue; \
}
class exportJointClusterData : public MPxCommand
{
public:
exportJointClusterData();
virtual ~exportJointClusterData();
MStatus parseArgs( const MArgList& args );
MStatus doIt ( const MArgList& args );
MStatus redoIt ();
MStatus undoIt ();
bool isUndoable() const;
MObject jointForCluster(MObject& jointCluster) const;
static void* creator();
private:
FILE* file;
};
exportJointClusterData::exportJointClusterData():
file(NULL)
{
}
exportJointClusterData::~exportJointClusterData() {}
void* exportJointClusterData::creator()
{
return new exportJointClusterData;
}
bool exportJointClusterData::isUndoable() const
{
return false;
}
MStatus exportJointClusterData::undoIt()
{
return MS::kSuccess;
}
MStatus exportJointClusterData::parseArgs( const MArgList& args )
{
MStatus stat;
MString arg;
MString fileName;
const MString fileFlag ("-f");
const MString fileFlagLong ("-file");
for ( unsigned int i = 0; i < args.length(); i++ ) {
arg = args.asString( i, &stat );
if (!stat)
continue;
if ( arg == fileFlag || arg == fileFlagLong ) {
if (i == args.length()-1) {
arg += ": must specify a file name";
displayError(arg);
return MS::kFailure;
}
i++;
args.get(i, fileName);
}
else {
arg += ": unknown argument";
displayError(arg);
return MS::kFailure;
}
}
file = fopen(fileName.asChar(),"wb");
if (!file) {
MString openError("Could not open: ");
openError += fileName;
displayError(openError);
stat = MS::kFailure;
}
return stat;
}
MObject exportJointClusterData::jointForCluster(MObject& jointCluster) const
{
MObject result;
MFnDependencyNode fnNode(jointCluster);
MObject attrJoint = fnNode.attribute("matrix");
MPlug pJointPlug(jointCluster,attrJoint);
MPlugArray conns;
if (pJointPlug.connectedTo(conns,true,false)) {
result = conns[0].node();
}
return result;
}
MStatus exportJointClusterData::doIt( const MArgList& args )
{
MStatus stat = parseArgs(args);
if (stat != MS::kSuccess) {
return stat;
}
unsigned int jcCount = 0;
MItDependencyNodes iter( MFn::kJointCluster);
for ( ; !iter.isDone(); iter.next() ) {
MObject object = iter.item();
MFnWeightGeometryFilter jointCluster(object);
MObject joint = jointForCluster(object);
if (joint.isNull()) {
displayError("Joint is not attached to cluster.");
continue;
}
MObject deformSet = jointCluster.deformerSet(&stat);
CheckError(stat,"Error getting deformer set.");
MFnSet setFn(deformSet, &stat);
CheckError(stat,"Error getting deformer set fn.");
MSelectionList clusterSetList;
stat = setFn.getMembers(clusterSetList, true);
CheckError(stat,"Could not make member list with getMembers.");
MFnDependencyNode fnJoint(joint);
fprintf(file,"%s %u\n",fnJoint.name().asChar(),
clusterSetList.length());
for (unsigned int kk = 0; kk < clusterSetList.length(); ++kk) {
MDagPath skinpath;
MObject components;
MFloatArray weights;
clusterSetList.getDagPath(kk,skinpath,components);
jointCluster.getWeights(skinpath,components,weights);
fprintf(file,
"%s %u\n",skinpath.partialPathName().asChar(),
weights.length());
unsigned counter =0;
MItGeometry gIter(skinpath,components);
for ( ; !gIter.isDone() &&
counter < weights.length(); gIter.next()) {
fprintf(file,"%d %f\n",gIter.index(),weights[counter]);
counter++;
}
}
jcCount++;
}
fclose(file);
if (0 == jcCount) {
displayError("No jointClusters found in this scene.");
return MS::kFailure;
}
return MS::kSuccess;
}
MStatus exportJointClusterData::redoIt()
{
clearResult();
setResult( (int) 1);
return MS::kSuccess;
}
MStatus initializePlugin( MObject obj )
{
MStatus status;
MFnPlugin plugin( obj, PLUGIN_COMPANY, "3.0", "Any");
status = plugin.registerCommand( "exportJointClusterData", exportJointClusterData::creator );
if (!status) {
status.perror("registerCommand");
return status;
}
return status;
}
MStatus uninitializePlugin( MObject obj )
{
MStatus status;
MFnPlugin plugin( obj );
status = plugin.deregisterCommand( "exportJointClusterData" );
if (!status) {
status.perror("deregisterCommand");
}
return status;
}