我想知道如何在c++中对对象应用颜色和着色(使用maya-api映射2D纹理(,这是我的代码:
MString MNEUT_FILE("G:/repos_cpp/data/eye_base.obj");
MGlobal::executeCommand(MString("file -import -namespace "EYETemp" -mergeNamespacesOnClash true") + """ + MNEUT_FILE + """);
MGlobal::executeCommand(MString("select "EYETemp:*""));
MGlobal::executeCommand(MString("rename ") + "eyeNeutral");
stat = MGlobal::executeCommand(MString("shadingNode -asShader lambert -name eyeball"));
stat = MGlobal::executeCommand(MString("sets -renderable true -noSurfaceShader true -empty -name lambert3SG"));
//stat = MGlobal::executeCommand(MString("defaultNavigation -connectToExisting -source eyeball -destination lambert3SG"));
stat = MGlobal::executeCommand(MString("connectAttr -f eyeball.outColor lambert3SG.surfaceShader"));
stat = MGlobal::executeCommand(MString("shadingNode -asTexture file -name eyeball_TGA"));
stat = MGlobal::executeCommand(MString("connectAttr -f eyeball_TGA.outColor eyeball.color"));
stat = MGlobal::executeCommand(MString("setAttr -type "string" eyeball_TGA.fileTextureName "G:/repos_cpp/data/eyeball.TGA""));
MGlobal::executeCommand(MString("select -r eyeNeutralShape"));
stat = MGlobal::executeCommand(MString("hyperShade -assign lambert3SG"));
if (!stat)
{
cout << "error::8n";
}
stat = MGlobal::executeCommand(MString("sets -e -fe lambert3SG eyeNeutralShape"));
if (!stat)
{
cout << "error::9n";
}
当我运行这个代码时;错误::8";永远存在!你能告诉我怎么解决吗。非常感谢!
您只检查最后一个MGlobal::executeCommand
的状态,所以您不知道它实际失败的地方。您也不会检查错误文本来获得错误的解释。
考虑制作一个包装器,用于检查所有调用:
#include <stdexcept>
MCommandResult MayaCmd(MString cmd) {
MCommandResult retval;
std::cout << "Executing MayaCmd: " << cmd << 'n';
auto stat = MGlobal::executeCommand(cmd, retval);
if(!stat) throw std::runtime_error(stat.errorString().asChar()); // or asUTF8()
return retval;
}
然后,您应该能够毫不在意地执行命令,并在发生异常时立即获得带有解释的异常。
示例:
try {
MayaCmd(MString("file -import -namespace "EYETemp" -mergeNamespacesOnClash true") + """ + MNEUT_FILE + """);
MayaCmd("select "EYETemp:*"");
MayaCmd("rename eyeNeutral");
MayaCmd("shadingNode -asShader lambert -name eyeball");
MayaCmd("sets -renderable true -noSurfaceShader true -empty -name lambert3SG");
//MayaCmd("defaultNavigation -connectToExisting -source eyeball -destination lambert3SG");
MayaCmd("connectAttr -f eyeball.outColor lambert3SG.surfaceShader");
MayaCmd("shadingNode -asTexture file -name eyeball_TGA");
MayaCmd("connectAttr -f eyeball_TGA.outColor eyeball.color");
MayaCmd("setAttr -type "string" eyeball_TGA.fileTextureName "G:/repos_cpp/data/eyeball.TGA"");
MayaCmd("select -r eyeNeutralShape");
MayaCmd("hyperShade -assign lambert3SG");
MayaCmd("sets -e -fe lambert3SG eyeNeutralShape");
}
catch(const std::exception& ex) {
std::cout << "Exception: " << ex.what() << 'n';
}
我还怀疑应该与hyperShade -assign
一起使用的名称是由前面的某个命令(可能是shadingNode
(返回的名称。示例:
auto ShaderNode = MayaCmd("shadingNode ...").stringResult();
...
MayaCmd("hyperShade -assign " + ShaderNode);