Maya API C++:从 poly 获取 material

Maya API C++: get material from poly

我正在使用 Maya API C ++ 为 Maya 2018 编写插件。我已经成功获得了一个网格,并创建了一个迭代器来遍历所有的多边形:

 ...
 MFnMesh fnMesh(mdagPath, &stat); 
 if (MS::kSuccess != stat)
 {
    // error get mesh
    return MS::kFailure;
 }
 MItMeshPolygon polyIter(mdagPath, mComponent, &stat);
 if (MS::kSuccess != stat) 
 {
    // error create iterator
    return MS::kFailure;
 }
 ...

我成功获取了顶点、法线、纹理坐标。但是如何得到叠加在当前多边形上的material呢?以及 material 纹理。

我得到所有 materials,然后我得到当前 material 中的所有多边形:

    MStatus stat = MStatus::kSuccess;
    MSpace::Space space = MSpace::kWorld; 
    MFnSet fnSet(set, &stat); 
    if (stat == MS::kFailure) 
    {
        MStreamUtils::stdErrorStream() << "ERROR: MFnSet::MFnSet\n";
        return MStatus::kNotImplemented;
    }
    MItMeshPolygon polyIter(mdagPath, comp, &stat); // iterator on vertex
    if ((stat == MS::kFailure)) 
    {
        MStreamUtils::stdErrorStream() << "ERROR: Can't create poly iterator!\n";
        return MStatus::kNotImplemented;
    }
    MFnDependencyNode fnNode(set); 
    MPlug shaderPlug = fnNode.findPlug("surfaceShader");
    MPlugArray connectedPlugs;
    if (shaderPlug.isNull())
    {
        MStreamUtils::stdErrorStream() << "ERROR: Can't find material!\n";
        return MStatus::kNotImplemented;
    }
    shaderPlug.connectedTo(connectedPlugs, true, false);
    MFnDependencyNode fnDN(connectedPlugs[0].node());

    MItDependencyGraph dgIt(shaderPlug, MFn::kFileTexture, 
        MItDependencyGraph::kUpstream,
        MItDependencyGraph::kBreadthFirst,
        MItDependencyGraph::kNodeLevel,
        &stat);
    if (stat == MS::kFailure)
    {
        MStreamUtils::stdErrorStream() << "ERROR: Can't load graph textures\n";
        return MStatus::kNotImplemented;
    }
    dgIt.disablePruningOnFilter();
    if (dgIt.isDone()) 
    {
        MStreamUtils::stdErrorStream() << "Warning: Material " << fnDN.name().asChar() << " not textured\n";
        //return MStatus::kNotImplemented;
    }

    // no need, here we get the texture
    MObject textureNode = dgIt.thisNode();
    MPlug filenamePlug = MFnDependencyNode(textureNode).findPlug("fileTextureName");
    MString textureName; // name texture + path
    filenamePlug.getValue(textureName);
    (; !polyIter.isDone(); polyIter.next())
    {
        // here get poly and perform actions on it
    }