ShadingConnection.cpp
#include <maya/MObject.h>
#include <maya/MColor.h>
#include <maya/MString.h>
#include <maya/MStringArray.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MPlug.h>
#include <maya/MPlugArray.h>
#include <assert.h>
#include "ShadingConnection.h"
ShadingConnection::ShadingConnection(MObject shaderObj, MString shapeName, MString attribute )
{
this->m_shaderObj = shaderObj;
this->m_shapeName = shapeName;
this->m_directConnection = true;
if (attribute != "")
traverseAttribute(attribute);
}
ShadingConnection::TYPE ShadingConnection::traverseAttribute(MString attributeName)
{
MStatus status;
MFnDependencyNode shaderNode(m_shaderObj);
MPlug plug = shaderNode.findPlug( attributeName, &status );
assert(status);
this->m_attributeName = attributeName;
return analyzePlug(plug);
}
ShadingConnection::TYPE ShadingConnection::analyzePlug(MPlug plug)
{
MStatus status;
MPlugArray connectedElements;
plug.connectedTo( connectedElements, true, false, &status );
assert(status);
if (connectedElements.length() == 0)
{
unsigned int numChildren = plug.numChildren();
assert(numChildren == 3);
float red, green, blue;
plug.child(0).getValue(red);
plug.child(1).getValue(green);
plug.child(2).getValue(blue);
return setConstantColor(MColor(red, green, blue, 1.0));
}
MObject connectedObject = connectedElements[0].node(&status);
assert(status);
if (connectedObject.hasFn(MFn::kTripleShadingSwitch))
{
return traverseTripleShadingSwitch(connectedObject);
}
return setTexture(connectedObject);
}
ShadingConnection::TYPE ShadingConnection::traverseTripleShadingSwitch(MObject connectedObject)
{
m_directConnection = false;
MFnDependencyNode node(connectedObject);
MStatus status;
MPlug inputPlug = node.findPlug( "input" );
unsigned int numElements = inputPlug.numElements();
unsigned int numChildren = inputPlug.numChildren();
assert( numChildren >= 2);
for (unsigned int index = 0; index < numElements; index++)
{
MPlug inTexturePlug = inputPlug.elementByPhysicalIndex(index).child(0);
MPlug inShapePlug = inputPlug.elementByPhysicalIndex(index).child(1);
MPlugArray inShapeConnections;
inShapePlug.connectedTo( inShapeConnections, true, false, &status );
unsigned int numCorrespondingShapes = inShapeConnections.length();
for (unsigned int shapeNum = 0; shapeNum < numCorrespondingShapes; shapeNum++)
{
MObject inShapeObj = inShapeConnections[0].node(&status);
assert(status);
MFnDependencyNode inShape(inShapeObj);
MString inShapeName = inShape.name();
if (inShapeName == m_shapeName)
return analyzePlug(inTexturePlug);
}
}
MPlug defaultPlug = node.findPlug("default");
return analyzePlug(defaultPlug);
}
ShadingConnection::TYPE ShadingConnection::type()
{
return m_type;
}
MColor ShadingConnection::constantColor()
{
assert(m_type == CONSTANT_COLOR);
return m_constantColor;
}
MObject ShadingConnection::texture()
{
assert(m_type == TEXTURE);
return m_texture;
}
MObject ShadingConnection::shaderObj()
{
return m_shaderObj;
}
MString ShadingConnection::shaderName()
{
MStatus stat;
MFnDependencyNode shaderNode(m_shaderObj, &stat);
assert(stat);
return shaderNode.name();
}
MString ShadingConnection::attributeName()
{
return m_attributeName;
}
MString ShadingConnection::shapeName()
{
return m_shapeName;
}
ShadingConnection::TYPE ShadingConnection::setConstantColor(MColor col)
{
m_constantColor = col;
m_type = CONSTANT_COLOR;
m_texture = MObject();
return m_type;
}
ShadingConnection::TYPE ShadingConnection::setTexture(MObject texture)
{
m_texture = texture;
m_type = TEXTURE;
m_constantColor = MColor();
return m_type;
}
bool ShadingConnection::isDirectConnection()
{
return m_directConnection;
}