dagPoseInfoCmd.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/MPlug.h>
#include <maya/MPlugArray.h>
#include <maya/MTransformationMatrix.h>
#include <maya/MMatrix.h>
#include <maya/MFnMatrixData.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MFnAttribute.h>
#include <maya/MIOStream.h>
#define CheckError(stat,msg) \
if ( MS::kSuccess != stat ) { \
displayError(msg); \
continue; \
}
class dagPoseInfo : public MPxCommand
{
public:
dagPoseInfo();
virtual ~dagPoseInfo();
MStatus parseArgs( const MArgList& args );
MStatus doIt ( const MArgList& args );
MStatus redoIt ();
MStatus undoIt ();
bool isUndoable() const;
static void* creator();
private:
void printDagPoseInfo(MObject& dagPoseNode, unsigned index);
bool findDagPose(MObject& jointNode);
FILE* file;
};
dagPoseInfo::dagPoseInfo():
file(NULL)
{
}
dagPoseInfo::~dagPoseInfo() {}
void* dagPoseInfo::creator()
{
return new dagPoseInfo;
}
bool dagPoseInfo::isUndoable() const
{
return false;
}
MStatus dagPoseInfo::undoIt()
{
return MS::kSuccess;
}
MStatus dagPoseInfo::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;
}
void dagPoseInfo::printDagPoseInfo(MObject& dagPoseNode, unsigned index)
{
MFnDependencyNode nDagPose(dagPoseNode);
fprintf(file,"%s\n",nDagPose.name().asChar());
MObject aWorldMatrix = nDagPose.attribute("worldMatrix");
MPlug pWorldMatrix(dagPoseNode,aWorldMatrix);
pWorldMatrix.selectAncestorLogicalIndex(index,aWorldMatrix);
MObject aMatrix = nDagPose.attribute("xformMatrix");
MPlug pMatrix(dagPoseNode,aMatrix);
pMatrix.selectAncestorLogicalIndex(index,aMatrix);
MObject worldMatrix, xformMatrix;
MStatus status = pWorldMatrix.getValue(worldMatrix);
if (MS::kSuccess != status) {
displayError("Problem retrieving world matrix.");
} else {
bool foundMatrix = 0;
MFnMatrixData dMatrix(worldMatrix);
MMatrix wMatrix = dMatrix.matrix(&status);
if (MS::kSuccess == status) {
foundMatrix = 1;
unsigned jj,kk;
fprintf(file,"worldMatrix\n");
for (jj = 0; jj < 4; ++jj) {
for (kk = 0; kk < 4; ++kk) {
double val = wMatrix(jj,kk);
fprintf(file,"%f ",val);
}
fprintf(file,"\n");
}
}
if (!foundMatrix) {
displayError("Error getting world matrix data.");
}
}
status = pMatrix.getValue(xformMatrix);
if (MS::kSuccess != status) {
displayError("Problem retrieving xform matrix.");
} else {
bool foundMatrix = 0;
MFnMatrixData dMatrix(xformMatrix);
if (dMatrix.isTransformation()) {
MTransformationMatrix xform = dMatrix.transformation(&status);
if (MS::kSuccess == status) {
foundMatrix = 1;
MMatrix xformAsMatrix = xform.asMatrix();
unsigned jj,kk;
fprintf(file,"matrix\n");
for (jj = 0; jj < 4; ++jj) {
for (kk = 0; kk < 4; ++kk) {
double val = xformAsMatrix(jj,kk);
fprintf(file,"%f ",val);
}
fprintf(file,"\n");
}
}
}
if (!foundMatrix) {
displayError("Error getting local matrix data.");
}
}
}
bool dagPoseInfo::findDagPose(MObject& jointNode)
{
bool rtn = 0;
MStatus status;
MFnDependencyNode fnJoint(jointNode);
MObject aBindPose = fnJoint.attribute("bindPose",&status);
if (MS::kSuccess == status) {
unsigned connLength = 0;
MPlugArray connPlugs;
MPlug pBindPose(jointNode,aBindPose);
pBindPose.connectedTo(connPlugs,false,true);
connLength = connPlugs.length();
for (unsigned ii = 0; ii < connLength; ++ii) {
if (connPlugs[ii].node().apiType() == MFn::kDagPose) {
MObject aMember = connPlugs[ii].attribute();
MFnAttribute fnAttr(aMember);
if (fnAttr.name() == "worldMatrix") {
unsigned jointIndex = connPlugs[ii].logicalIndex();
fprintf(file,"%s\n",fnJoint.name().asChar());
MObject jointObject = connPlugs[ii].node();
printDagPoseInfo(jointObject,jointIndex);
rtn = 1;
}
}
}
}
return rtn;
}
MStatus dagPoseInfo::doIt( const MArgList& args )
{
MStatus stat = parseArgs(args);
if (stat != MS::kSuccess) {
return stat;
}
unsigned int count = 0;
MSelectionList slist;
MGlobal::getActiveSelectionList( slist );
MItSelectionList itr( slist );
for (; !itr.isDone(); itr.next() )
{
MObject depNode;
itr.getDependNode(depNode);
if (depNode.apiType() == MFn::kJoint) {
if (findDagPose(depNode)) {
count++;
}
}
}
fclose(file);
if (0 == count) {
displayError("No poses were found on the selected joints.");
return MS::kFailure;
}
return MS::kSuccess;
}
MStatus dagPoseInfo::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( "dagPoseInfo", dagPoseInfo::creator );
if (!status) {
status.perror("registerCommand");
return status;
}
return status;
}
MStatus uninitializePlugin( MObject obj )
{
MStatus status;
MFnPlugin plugin( obj );
status = plugin.deregisterCommand( "dagPoseInfo" );
if (!status) {
status.perror("deregisterCommand");
}
return status;
}