hwPhongShaderBehavior.cpp
#include <maya/MIOStream.h>
#include <maya/MGlobal.h>
#include <maya/MPlugArray.h>
#include <maya/MFnDagNode.h>
#include <maya/MObjectArray.h>
#include "hwPhongShaderBehavior.h"
#define VERBOSE 0
hwPhongShaderBehavior::hwPhongShaderBehavior()
{
}
hwPhongShaderBehavior::~hwPhongShaderBehavior()
{
}
void *hwPhongShaderBehavior::creator()
{
return new hwPhongShaderBehavior;
}
bool hwPhongShaderBehavior::shouldBeUsedFor(
MObject & sourceNode,
MObject & destinationNode,
MPlug & ,
MPlug & )
{
bool result = false;
if ((MFnDependencyNode(sourceNode).typeName() == "hwPhongShader") &&
destinationNode.hasFn(MFn::kLambert))
result = true;
#if VERBOSE
cerr << "shouldBeUsedFor "<<MFnDependencyNode(sourceNode).name().asChar()
<< " " << MFnDependencyNode(destinationNode).name().asChar()
<< (result?" true\n":" false\n");
#endif
return result;
}
static MStatus connectAttr(
const MPlug &srcPlug,
const MPlug &destPlug,
bool force)
{
MStatus result = MS::kFailure;
if(!srcPlug.isNull() && !destPlug.isNull())
{
MString cmd = "connectAttr ";
if (force) cmd += "-f ";
cmd += srcPlug.name() + " " + destPlug.name();
result = MGlobal::executeCommand(cmd);
#if VERBOSE
cerr << cmd.asChar()<<"\n";
#endif
}
return result;
}
MStatus hwPhongShaderBehavior::connectNodeToNode(
MObject & sourceNode,
MObject & destinationNode,
bool force )
{
MStatus result = MS::kFailure;
MFnDependencyNode src(sourceNode);
if ((src.typeName() == "hwPhongShader") &&
destinationNode.hasFn(MFn::kLambert))
{
MFnDependencyNode dest(destinationNode);
result = connectAttr(src.findPlug("outColor"),
dest.findPlug("hardwareShader"), force);
}
#if VERBOSE
if (result != MS::kSuccess)
cerr << "connectNodeToNode "<<src.name().asChar()
<< " " << MFnDependencyNode(destinationNode).name().asChar()
<< " failed\n";
#endif
return result;
}
MStatus hwPhongShaderBehavior::connectNodeToAttr(
MObject & sourceNode,
MPlug & destinationPlug,
bool force )
{
MStatus result = MS::kFailure;
MFnDependencyNode src(sourceNode);
if ((src.typeName() == "hwPhongShader") &&
destinationPlug.node().hasFn(MFn::kLambert))
{
result = connectAttr(src.findPlug("outColor"), destinationPlug, force);
}
#if VERBOSE
if (result != MS::kSuccess)
cerr << "connectNodeToAttr "<<src.name().asChar()
<< " " << destinationPlug.name().asChar()
<< " failed\n";
#endif
return result;
}
MStatus hwPhongShaderBehavior::connectAttrToNode(
MPlug & sourcePlug,
MObject & destinationNode,
bool force )
{
MStatus result = MS::kFailure;
MObject sourceNode = sourcePlug.node();
MFnDependencyNode src(sourceNode);
if ((src.typeName() == "hwPhongShader") &&
destinationNode.hasFn(MFn::kLambert))
{
MFnDependencyNode dest(destinationNode);
result = connectAttr(sourcePlug, dest.findPlug("hardwareShader"),
force);
}
#if VERBOSE
if (result != MS::kSuccess)
cerr << "connectNodeAttrToNode "<<sourcePlug.name().asChar()
<< " " << MFnDependencyNode(destinationNode).name().asChar()
<< " failed\n";
#endif
return result;
}
MStatus hwPhongShaderBehavior::connectAttrToAttr(
MPlug & sourcePlug,
MPlug & destinationPlug,
bool force )
{
#if VERBOSE
cerr << "In connectAttrToAttr "<<sourcePlug.name().asChar()
<< " " << destinationPlug.name().asChar()
<< "\n";
#endif
return connectAttr(sourcePlug, destinationPlug, force);
}