slopeShaderBehavior.cpp
#include <slopeShaderBehavior.h>
#include <maya/MGlobal.h>
#include <maya/MPlugArray.h>
#include <maya/MFnDagNode.h>
#include <maya/MObjectArray.h>
slopeShaderBehavior::slopeShaderBehavior()
{
}
slopeShaderBehavior::~slopeShaderBehavior()
{
}
void *slopeShaderBehavior::creator()
{
return new slopeShaderBehavior;
}
bool slopeShaderBehavior::shouldBeUsedFor( MObject &sourceNode, MObject &destinationNode,
MPlug &sourcePlug, MPlug &destinationPlug)
{
bool result = false;
if(sourceNode.hasFn(MFn::kLambert))
{
MObject shaderNode;
MFnDependencyNode src(sourceNode);
MPlugArray connections;
src.getConnections(connections);
for(unsigned i = 0; i < connections.length(); i++)
{
MPlugArray connectedPlugs;
connections[i].connectedTo(connectedPlugs, true, false);
for(unsigned j = 0; j < connectedPlugs.length(); j++)
{
if(MFnDependencyNode(connectedPlugs[j].node()).typeName() == "slopeShader")
{
shaderNode = connectedPlugs[j].node();
break;
}
}
if(!shaderNode.isNull())
{
if(destinationNode.hasFn(MFn::kMesh))
result = true;
break;
}
}
}
if(MFnDependencyNode(sourceNode).typeName() == "slopeShader")
{
if(destinationNode.hasFn(MFn::kLambert))
result = true;
else if(destinationNode.hasFn(MFn::kMesh))
result = true;
}
return result;
}
MStatus slopeShaderBehavior::connectNodeToNode( MObject &sourceNode, MObject &destinationNode, bool force )
{
MStatus result = MS::kFailure;
MFnDependencyNode src(sourceNode);
if(sourceNode.hasFn(MFn::kLambert))
{
MObject shaderNode;
MPlugArray connections;
MObjectArray shaderNodes;
shaderNodes.clear();
src.getConnections(connections);
unsigned i;
for(i = 0; i < connections.length(); i++)
{
MPlugArray connectedPlugs;
connections[i].connectedTo(connectedPlugs, true, false);
for(unsigned j = 0; j < connectedPlugs.length(); j++)
{
MObject currentnode = connectedPlugs[j].node();
if(MFnDependencyNode(currentnode).typeName() == "slopeShader")
{
shaderNodes.append(currentnode);
}
}
}
if(shaderNodes.length() > 0)
{
MFnDependencyNode dest(destinationNode);
if(destinationNode.hasFn(MFn::kMesh))
{
for(i = 0; i < shaderNodes.length(); i++)
{
MPlug srcPlug = dest.findPlug("worldMesh");
MPlug destPlug = MFnDependencyNode(shaderNodes[i]).findPlug("dirtyShaderPlug");
if(!srcPlug.isNull() && !destPlug.isNull())
{
MString cmd = "connectAttr -na ";
cmd += srcPlug.name() + " ";
cmd += destPlug.name();
MGlobal::executeCommand(cmd);
}
}
MObject shadingEngine = findShadingEngine(sourceNode);
if(!shadingEngine.isNull())
{
MString cmd = "sets -edit -forceElement ";
cmd += MFnDependencyNode(shadingEngine).name() + " ";
cmd += MFnDagNode(destinationNode).partialPathName();
result = MGlobal::executeCommand(cmd);
}
}
}
}
else if(src.typeName() == "slopeShader")
{
if(destinationNode.hasFn(MFn::kMesh))
{
MFnDependencyNode dest(destinationNode);
MPlug srcPlug = dest.findPlug("worldMesh");
MPlug destPlug = src.findPlug("dirtyShaderPlug");
if(!srcPlug.isNull() && !destPlug.isNull())
{
MString cmd = "connectAttr -na ";
cmd += srcPlug.name() + " ";
cmd += destPlug.name();
result = MGlobal::executeCommand(cmd);
}
}
}
return result;
}
MStatus slopeShaderBehavior::connectNodeToAttr( MObject &sourceNode, MPlug &destinationPlug, bool force )
{
MStatus result = MS::kFailure;
MFnDependencyNode src(sourceNode);
if(destinationPlug.node().hasFn(MFn::kLambert)) {
if(src.typeName() == "slopeShader")
{
MPlug srcPlug = src.findPlug("outColor");
if(!srcPlug.isNull() && !destinationPlug.isNull())
{
MString cmd = "connectAttr ";
cmd += srcPlug.name() + " ";
cmd += destinationPlug.name();
result = MGlobal::executeCommand(cmd);
}
}
} else {
MObject destinationNode = destinationPlug.node();
result = connectNodeToNode(sourceNode, destinationNode, force);
}
return result;
}
MObject slopeShaderBehavior::findShadingEngine(MObject &node)
{
MFnDependencyNode nodeFn(node);
MPlug srcPlug = nodeFn.findPlug("outColor");
MPlugArray nodeConnections;
srcPlug.connectedTo(nodeConnections, false, true);
for(unsigned i = 0; i < nodeConnections.length(); i++)
{
if(nodeConnections[i].node().hasFn(MFn::kShadingEngine))
return nodeConnections[i].node();
}
return MObject();
}