exportSkinClusterDataCmd.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/MDagPathArray.h>
#include <maya/MItSelectionList.h>
#include <maya/MSelectionList.h>
#include <maya/MIntArray.h>
#include <maya/MDoubleArray.h>
#include <maya/MObjectArray.h>
#include <maya/MItDependencyNodes.h>
#include <maya/MItGeometry.h>
#include <maya/MFnSkinCluster.h>
#include <maya/MFnSingleIndexedComponent.h>
#include <maya/MIOStream.h>
#define CheckError(stat,msg) \
if ( MS::kSuccess != stat ) { \
displayError(msg); \
continue; \
}
class exportSkinClusterData : public MPxCommand
{
public:
exportSkinClusterData();
virtual ~exportSkinClusterData();
MStatus parseArgs( const MArgList& args );
MStatus doIt ( const MArgList& args );
MStatus redoIt ();
MStatus undoIt ();
bool isUndoable() const;
static void* creator();
private:
FILE* file;
};
exportSkinClusterData::exportSkinClusterData():
file(NULL)
{
}
exportSkinClusterData::~exportSkinClusterData() {}
void* exportSkinClusterData::creator()
{
return new exportSkinClusterData;
}
bool exportSkinClusterData::isUndoable() const
{
return false;
}
MStatus exportSkinClusterData::undoIt()
{
return MS::kSuccess;
}
MStatus exportSkinClusterData::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;
}
MStatus exportSkinClusterData::doIt( const MArgList& args )
{
MStatus stat = parseArgs(args);
if (stat != MS::kSuccess) {
return stat;
}
unsigned int count = 0;
MItDependencyNodes iter( MFn::kInvalid);
for ( ; !iter.isDone(); iter.next() ) {
MObject object = iter.item();
if (object.apiType() == MFn::kSkinClusterFilter) {
count++;
MFnSkinCluster skinCluster(object);
MDagPathArray infs;
MStatus stat;
unsigned int nInfs = skinCluster.influenceObjects(infs, &stat);
CheckError(stat,"Error getting influence objects.");
if (0 == nInfs) {
stat = MS::kFailure;
CheckError(stat,"Error: No influence objects found.");
}
unsigned int nGeoms = skinCluster.numOutputConnections();
for (unsigned int ii = 0; ii < nGeoms; ++ii) {
unsigned int index = skinCluster.indexForOutputConnection(ii,&stat);
CheckError(stat,"Error getting geometry index.");
MDagPath skinPath;
stat = skinCluster.getPathAtIndex(index,skinPath);
CheckError(stat,"Error getting geometry path.");
MItGeometry gIter(skinPath);
fprintf(file,
"%s %d %u\n",skinPath.partialPathName().asChar(),
gIter.count(),
nInfs);
for (unsigned int kk = 0; kk < nInfs; ++kk) {
fprintf(file,"%s ",infs[kk].partialPathName().asChar());
}
fprintf(file,"\n");
for ( ; !gIter.isDone(); gIter.next() ) {
MObject comp = gIter.component(&stat);
CheckError(stat,"Error getting component.");
MDoubleArray wts;
unsigned int infCount;
stat = skinCluster.getWeights(skinPath,comp,wts,infCount);
CheckError(stat,"Error getting weights.");
if (0 == infCount) {
stat = MS::kFailure;
CheckError(stat,"Error: 0 influence objects.");
}
fprintf(file,"%d ",gIter.index());
for (unsigned int jj = 0; jj < infCount ; ++jj ) {
fprintf(file,"%f ",wts[jj]);
}
fprintf(file,"\n");
}
}
}
}
if (0 == count) {
displayError("No skinClusters found in this scene.");
}
fclose(file);
return MS::kSuccess;
}
MStatus exportSkinClusterData::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( "exportSkinClusterData", exportSkinClusterData::creator );
if (!status) {
status.perror("registerCommand");
return status;
}
return status;
}
MStatus uninitializePlugin( MObject obj )
{
MStatus status;
MFnPlugin plugin( obj );
status = plugin.deregisterCommand( "exportSkinClusterData" );
if (!status) {
status.perror("deregisterCommand");
}
return status;
}