findFileTexturesCmd.cpp
#include <maya/MSimple.h>
#include <maya/MObject.h>
#include <maya/MGlobal.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MSelectionList.h>
#include <maya/MString.h>
#include <maya/MObjectArray.h>
#include <maya/MPlug.h>
#include <maya/MItSelectionList.h>
#include <maya/MItDependencyGraph.h>
#include <maya/MFnStringData.h>
#include <maya/MIOStream.h>
void dumpInfo( MObject fileNode,
MFnDependencyNode& nodeFn,
MObjectArray& nodePath )
{
MObject currentNode;
MObject fileAttr = nodeFn.attribute("fileTextureName");
MPlug plugToFile( fileNode, fileAttr );
MFnDependencyNode dgFn;
MStatus stat;
cerr << "Name: " << nodeFn.name() << endl;
MObject fnameValue;
stat = plugToFile.getValue( fnameValue );
if ( !stat ) {
stat.perror("error getting value from plug");
} else {
MFnStringData stringFn( fnameValue );
cerr << "Texture: " << stringFn.string() << endl;
}
cerr << "Path: ";
for ( int i = nodePath.length()-1; i >= 0; i-- ) {
currentNode = nodePath[i];
dgFn.setObject( currentNode );
cerr << dgFn.name() << "(" << dgFn.typeName() << ")";
if ( i > 0)
cerr << " ->\n ";
}
cerr << endl;
}
DeclareSimpleCommand( findFileTextures, PLUGIN_COMPANY, "3.0");
MStatus findFileTextures::doIt( const MArgList& args )
{
MSelectionList list;
MStatus status;
if ( args.length() > 0 ) {
MString argStr;
unsigned last = args.length();
for ( unsigned i = 0; i < last; i++ ) {
args.get( i, argStr );
list.add( argStr );
}
} else {
MGlobal::getActiveSelectionList( list );
}
MObject node;
MFnDependencyNode nodeFn,dgNodeFnSet;
MItDependencyGraph* dgIt;
MObject currentNode;
MObject thisNode;
MObjectArray nodePath;
for ( MItSelectionList iter( list ); !iter.isDone(); iter.next() ) {
iter.getDependNode( node );
nodeFn.setObject( node );
MObject iogAttr = nodeFn.attribute( "instObjGroups", &status);
if ( !status ) {
cerr << nodeFn.name() << ": is not a renderable object, skipping\n";
continue;
}
MPlug iogPlug( node, iogAttr );
MPlugArray iogConnections;
iogPlug.elementByLogicalIndex(0).connectedTo( iogConnections, false, true, &status );
if ( !status ) {
cerr << nodeFn.name() << ": is not in a shading group, skipping\n";
continue;
}
bool foundATexture = false;
for ( unsigned int i=0; i<iogConnections.length(); i++ ) {
currentNode = iogConnections[i].node();
dgIt = new MItDependencyGraph( currentNode,
MFn::kFileTexture,
MItDependencyGraph::kUpstream,
MItDependencyGraph::kBreadthFirst,
MItDependencyGraph::kNodeLevel,
&status );
if ( !status ) {
delete dgIt;
continue;
}
dgIt->disablePruningOnFilter();
for ( ; ! dgIt->isDone(); dgIt->next() ) {
thisNode = dgIt->thisNode();
dgNodeFnSet.setObject( thisNode );
status = dgIt->getNodePath( nodePath );
if ( !status ) {
status.perror("getNodePath");
continue;
}
nodePath.append(node);
dumpInfo( thisNode, dgNodeFnSet, nodePath );
foundATexture = true;
}
delete dgIt;
}
if ( !foundATexture ) {
cerr << nodeFn.name() << ": is not connected to a file texture\n";
}
}
return MS::kSuccess;
}