1

我正在使用 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;
 }
 ...

我成功获得了顶点、法线、纹理坐标。但是如何获取叠加在当前多边形上的材质呢?并且伴随着材质的质感。

4

1 回答 1

1

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

    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
    }
于 2018-10-26T23:25:59.560 回答