#include <maya/MIOStream.h>
#include <maya/MSimple.h>
#include <maya/MString.h>
#include <maya/MFnMesh.h>
#include <maya/MFnSet.h>
#include <maya/MItDependencyGraph.h>
#include <maya/MItMeshPolygon.h>
#include <maya/MSelectionList.h>
#include <maya/MGlobal.h>
#include <maya/MDagPath.h>
#include <maya/MObjectArray.h>
#include <maya/MPlug.h>
MObject findShader( MObject& setNode )
{
    MFnDependencyNode fnNode(setNode);
    MPlug shaderPlug = fnNode.findPlug("surfaceShader");
            
    if (!shaderPlug.isNull()) {         
        MPlugArray connectedPlugs;
        bool asSrc = false;
        bool asDst = true;
        shaderPlug.connectedTo( connectedPlugs, asDst, asSrc );
        if (connectedPlugs.length() != 1)
            cerr << "Error getting shader\n";
        else 
            return connectedPlugs[0].node();
    }           
    
    return MObject::kNullObj;
}
DeclareSimpleCommand( findTexturesPerPolygon, PLUGIN_COMPANY, "3.0" );
MStatus findTexturesPerPolygon::doIt( const MArgList& )
{
    
    
    MStatus status;
    MDagPath path;
    MObject cmp;
    MSelectionList slist;
    MGlobal::getActiveSelectionList(slist);
    slist.getDagPath(0, path, cmp);
    
    
    
    
    path.extendToShape();
    
    
    
    int instanceNum = 0;
    if (path.isInstanced())
        instanceNum = path.instanceNumber();
    
    
    
    MFnMesh fnMesh(path);
    MObjectArray sets;
    MObjectArray comps;
    if (!fnMesh.getConnectedSetsAndMembers(instanceNum, sets, comps, true))
        cerr << "ERROR: MFnMesh::getConnectedSetsAndMembers\n";
    
    
    
    
    for ( unsigned i=0; i<sets.length(); i++ ) {
        MObject set = sets[i];
        MObject comp = comps[i];
        MFnSet fnSet( set, &status );
        if (status == MS::kFailure) {
            cerr << "ERROR: MFnSet::MFnSet\n";
            continue;
        }
        
        MItMeshPolygon piter(path, comp, &status);
        if ((status == MS::kFailure) || comp.isNull())
            continue;
        
        
        
        
        
        MObject shaderNode = findShader(set);
        if (shaderNode == MObject::kNullObj)
            continue;
        MPlug colorPlug = MFnDependencyNode(shaderNode).findPlug("color", &status);
        if (status == MS::kFailure)
            continue;
        MItDependencyGraph dgIt(colorPlug, MFn::kFileTexture,
                           MItDependencyGraph::kUpstream, 
                           MItDependencyGraph::kBreadthFirst,
                           MItDependencyGraph::kNodeLevel, 
                           &status);
        if (status == MS::kFailure)
            continue;
        
        dgIt.disablePruningOnFilter();
        
        
        if (dgIt.isDone())
            continue;
          
        
        
        MObject textureNode = dgIt.thisNode();
        MPlug filenamePlug = MFnDependencyNode(textureNode).findPlug("fileTextureName");
        MString textureName;
        filenamePlug.getValue(textureName);
        cerr << "Set: " << fnSet.name() << endl;
        cerr << "Texture Node Name: " << MFnDependencyNode(textureNode).name() << endl;
        cerr << "Texture File Name: " << textureName.asChar() << endl;
        
        
        
        for ( ; !piter.isDone(); piter.next() )
            cerr << "    poly component: " << piter.index() << endl;
    }
    return MS::kSuccess;
}