#include <maya/MFnPlugin.h>
#include <maya/MIOStream.h>
#include <maya/MPxNode.h>
#include <maya/MString.h>
#include <maya/MTypeId.h>
#include <maya/MPlug.h>
#include <maya/MDataBlock.h>
#include <maya/MDataHandle.h>
#include <maya/MFnNumericAttribute.h>
#include <maya/MFloatVector.h>
class myComp : public MPxNode
{
public:
myComp();
virtual ~myComp();
virtual MStatus compute( const MPlug&, MDataBlock& );
virtual void postConstructor();
static void * creator();
static MStatus initialize();
static MTypeId id;
private:
static MObject aForegroundColor;
static MObject aBackgroundColor;
static MObject aBackColor;
static MObject aMask;
static MObject aOutColor;
static MObject aOutAlpha;
};
MTypeId myComp::id( 0x8100c );
MObject myComp::aForegroundColor;
MObject myComp::aBackgroundColor;
MObject myComp::aBackColor;
MObject myComp::aMask;
MObject myComp::aOutColor;
MObject myComp::aOutAlpha;
#define MAKE_INPUT(attr) \
CHECK_MSTATUS( attr.setKeyable(true)); \
CHECK_MSTATUS( attr.setStorable(true)); \
CHECK_MSTATUS( attr.setReadable(true)); \
CHECK_MSTATUS( attr.setWritable(true));
#define MAKE_OUTPUT(attr) \
CHECK_MSTATUS( attr.setKeyable(false)); \
CHECK_MSTATUS( attr.setStorable(false)); \
CHECK_MSTATUS( attr.setReadable(true)); \
CHECK_MSTATUS( attr.setWritable(false));
void myComp::postConstructor( )
{
setMPSafe(true);
}
myComp::myComp()
{
}
myComp::~myComp()
{
}
void * myComp::creator()
{
return new myComp();
}
MStatus myComp::initialize()
{
MFnNumericAttribute nAttr;
aForegroundColor = nAttr.createColor("foreground", "fg");
MAKE_INPUT(nAttr);
aBackgroundColor = nAttr.createColor("background", "bg");
MAKE_INPUT(nAttr);
CHECK_MSTATUS( nAttr.setDefault(1., 1., 1.));
aBackColor = nAttr.createColor("backColor", "bk");
MAKE_INPUT(nAttr);
CHECK_MSTATUS( nAttr.setDefault(1., 1., 1.));
aMask = nAttr.create( "mask", "ms", MFnNumericData::kFloat);
CHECK_MSTATUS( nAttr.setMin(0.f));
CHECK_MSTATUS( nAttr.setMax(1.f));
MAKE_INPUT(nAttr);
aOutColor = nAttr.createColor("outColor", "oc");
MAKE_OUTPUT(nAttr);
aOutAlpha = nAttr.create( "outAlpha", "oa", MFnNumericData::kFloat);
MAKE_OUTPUT(nAttr);
CHECK_MSTATUS( addAttribute(aForegroundColor) );
CHECK_MSTATUS( addAttribute(aBackgroundColor) );
CHECK_MSTATUS( addAttribute(aBackColor) );
CHECK_MSTATUS( addAttribute(aMask) );
CHECK_MSTATUS( addAttribute(aOutColor) );
CHECK_MSTATUS( addAttribute(aOutAlpha) );
CHECK_MSTATUS( attributeAffects(aForegroundColor, aOutColor) );
CHECK_MSTATUS( attributeAffects(aForegroundColor, aOutAlpha) );
CHECK_MSTATUS( attributeAffects(aBackgroundColor, aOutColor) );
CHECK_MSTATUS( attributeAffects(aBackgroundColor, aOutAlpha) );
CHECK_MSTATUS( attributeAffects(aBackColor, aOutColor) );
CHECK_MSTATUS( attributeAffects(aBackColor, aOutAlpha) );
CHECK_MSTATUS( attributeAffects(aMask, aOutColor) );
CHECK_MSTATUS( attributeAffects(aMask, aOutAlpha) );
return MS::kSuccess;
}
MStatus myComp::compute(const MPlug& plug, MDataBlock& block)
{
if((plug != aOutColor) && (plug.parent() != aOutColor) &&
(plug != aOutAlpha))
return MS::kUnknownParameter;
MFloatVector resultColor;
MFloatVector& fore = block.inputValue( aForegroundColor ).asFloatVector();
MFloatVector& back = block.inputValue( aBackgroundColor ).asFloatVector();
MFloatVector& bclr = block.inputValue( aBackColor ).asFloatVector();
float& alpha = block.inputValue( aMask ).asFloat();
if ( alpha > 0.99999f ) alpha = 1.f;
else if ( alpha < 0.00001 ) alpha = 0.f;
resultColor = fore + ((bclr - back) * (1.0f - alpha));
if (resultColor[0] < 0.f ) resultColor[0] = 0.f;
if (resultColor[1] < 0.f ) resultColor[1] = 0.f;
if (resultColor[2] < 0.f ) resultColor[2] = 0.f;
if (resultColor[0] > 1.f ) resultColor[0] = 1.f;
if (resultColor[1] > 1.f ) resultColor[1] = 1.f;
if (resultColor[2] > 1.f ) resultColor[2] = 1.f;
MDataHandle outColorHandle = block.outputValue( aOutColor );
MFloatVector& outColor = outColorHandle.asFloatVector();
outColor = resultColor;
outColorHandle.setClean();
MDataHandle outAlphaHandle = block.outputValue( aOutAlpha );
float& outAlpha = outAlphaHandle.asFloat();
outAlpha = ( resultColor.x + resultColor.y + resultColor.z ) / 3.0f;
outAlphaHandle.setClean();
return MS::kSuccess;
}
MStatus initializePlugin( MObject obj )
{
const MString UserClassify( "utility/color" );
MFnPlugin plugin( obj, PLUGIN_COMPANY, "4.5", "Any");
CHECK_MSTATUS( plugin.registerNode( "composite", myComp::id,
&myComp::creator, &myComp::initialize,
MPxNode::kDependNode, &UserClassify ) );
return MS::kSuccess;
}
MStatus uninitializePlugin( MObject obj )
{
MFnPlugin plugin( obj );
CHECK_MSTATUS( plugin.deregisterNode( myComp::id ) );
return MS::kSuccess;
}