|
hi,all
i assign a texuture to a file node and output to a surface shader, then assign the surface shader to a cube.
Then I try to use the following code to traverse the scene to get the texture name of each mesh,but fail, the texture name is empty. :(
here is the code, the code is part of gpExport ,and you can find it here: http://florian.loitsch.com/gpExport/
-----------------------------------------------------------------------------------------
std::vector
MaterialExtractor::extractMaterials(const MObject& shaderSet) const
{
std::vector materials;
MFnDependencyNode depNode(shaderSet)
MPlug shaderPlug = depNode.findPlug("surfaceShader")
if (shaderPlug.isNull()) return materials; // empty vector
MPlugArray shaderPlugSources;
shaderPlug.connectedTo(shaderPlugSources, true, false)
for (uint32 i = 0; i < shaderPlugSources.length() ++i) > {
Material currentMaterial;
MPlug currentShaderPlugSource = shaderPlugSources;
MObject shaderNode = currentShaderPlugSource.node()
if (shaderNode.hasFn(MFn::kPhong))
fillMaterial(¤tMaterial,
shaderNode,
“phong")
else if (shaderNode.hasFn(MFn::kBlinn))
fillMaterial(¤tMaterial,
shaderNode,
“blinn")
else if (shaderNode.hasFn(MFn::kLambert))
fillMaterial(¤tMaterial,
shaderNode,
“lambert")
else
{
// don’t know how to extract it
continue;
}
currentMaterial.texturePaths = extractTextures(currentShaderPlugSource)
// !!!-----> i output the texturePaths , it’s empty.<---------!!! >
materials.push_back(currentMaterial)
}
return materials;
}
/*
* for every mesh gets its shader-set, and extracts all materials.
*/
// I'm heavily referencing Ogre's Material-export…
// (credits, where credits are due)
std::vector
MaterialExtractor::ExtractMaterials(const MDagPath& mayaMeshPath) const
{
uint32 instanceNumber = mayaMeshPath.instanceNumber()
// get the connected shaders (= materials)
MObjectArray shaderSets;
MIntArray shaderVertexIndices;
MFnMesh mayaMesh(mayaMeshPath, &status)
checkStatus("couldn’t access mesh")
std::vector result;
mayaMesh.getConnectedShaders(instanceNumber,
shaderSets,
shaderVertexIndices)
for (uint32 i = 0; i < shaderSets.length() ++i) > {
std::vector currentMaterials =
extractMaterials(shaderSets)
result.insert(result.end(),
currentMaterials.begin(),
currentMaterials.end())
}
return result;
}
------------------------------------------------------------------------------------------
On the other hand, I can get the texture names of a mesh with MEL like this:
listConnections -p 1 -scn 0 -s 0 pCubeShape2;
// Result: surfaceShader1SG.dagSetMembers[0] //
connectionInfo -sfd surfaceShader1SG.surfaceShader;
// Result: surfaceShader1.outColor //
connectionInfo -sfd surfaceShader1.outColor;
// Result: file2.outColor //
getAttr file2.fileTextureName;
// Result: E:/MyDocuments/My Pictures/cup.BMP //
but, how to do it with maya api?
|