#include <maya/MFnPlugin.h>
#include <maya/MString.h>
#include <maya/MPxCustomEvaluator.h>
#include <maya/MCustomEvaluatorClusterNode.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MPlug.h>
#include <maya/MProfiler.h>
#include <maya/MGraphNodeIterator.h>
#include <maya/MFnMatrixData.h>
#include <maya/MPlugArray.h>
#include <maya/MPxTransform.h>
#include <maya/MObjectHandle.h>
#include <map>
namespace
{
}
{
public:
typedef std::map<unsigned int, MObject> TargetMapHash;
virtual ~constraintEvaluator();
virtual const char* evaluatorName();
static void* creator();
void reset();
TargetMapHash fTransformTargetHash;
};
void constraintEvaluator::reset()
{
fTransformTargetHash.clear();
}
{
MObject thisNode = node->dependencyNode(&stat);
{
MString nodeName = depNodeFn.name();
if (depNodeFn.typeName() == "transform")
{
MPlug translateXPlug = depNodeFn.findPlug(
"translateX", &stat);
translateXPlug.
connectedTo(sources,
true ,
false , &stat);
if (sources.
length() != 1)
return false;
MPlug rotateXPlug = depNodeFn.findPlug(
"rotateX", &stat);
rotateXPlug.
connectedTo(sources,
true ,
false , &stat);
if (sources.
length() > 1)
return false;
MPlug pointConstraintTargetTranslatePlug = pointConstraintNode.findPlug(
"targetTranslate",
true, &stat);
MString targetNodeName = sourceNode.name();
MPlug orientConstraintTargetRotatePlug = orientConstraintNode.findPlug(
"targetRotate",
true, &stat);
stat = sourceNode.setObject(orientConstraintTargetRotatePlug.
source().
node());
if (targetNodeName != sourceNode.name()) return false;
return true;
}
else if ((depNodeFn.typeName() == "pointConstraint") || (depNodeFn.typeName() == "orientConstraint"))
{
return true;
}
}
return false;
}
{
}
{
MProfilingScope profilingScope(_profilerCategory, MProfiler::kColorD_L1,
"Evaluate constraint cluster");
{
while (!iterator.isDone())
{
iterator.next(&stat);
MObject currObject = currEvalNode.dependencyNode(&stat);
{
currEvalNode.datablock(&stat).isClean(worldMatrixForPreEval);
{
MProfilingScope profilingScopeParent(_profilerCategory, MProfiler::kColorD_L1,
"Get parent inverse matrix");
MArrayDataHandle parentInverseMatrixArrayHandle = currEvalNode.datablock(&stat).inputArrayValue(parentInverseMatrixPlug.attribute());
MObject parentInverseMatrixData = elementHandle.
data();
piMatrix = fnMatrixData.
matrix(&stat);
}
{
MProfilingScope profilingScopeTarget(_profilerCategory, MProfiler::kColorD_L1,
"Get target matrix");
MObject targetWorldMatrixData = targetWorldMatrixHandle.
data();
MFnMatrixData fnTargetWorldMatrixData(targetWorldMatrixData, &stat);
wMatrix = fnTargetWorldMatrixData.
matrix(&stat);
}
{
MProfilingScope profilingScopeDestination(_profilerCategory, MProfiler::kColorD_L1,
"Compute and write TR");
MMatrix currLocalMatrix = wMatrix * piMatrix;
double rotateVals[3];
stat = transformer.getRotation(rotateVals, ro);
MDataBlock currDataBlock = currEvalNode.datablock(&stat);
currTranslateHandle.
set3Double(translate.
x, translate.
y, translate.
z);
currRotateHandle.
set3Double(rotateVals[0], rotateVals[1], rotateVals[2]);
tPlug.setValue(currTranslateHandle);
rPlug.setValue(currRotateHandle);
}
{
MProfilingScope profilingScopeUpdate(_profilerCategory, MProfiler::kColorD_L1,
"Finalize evaluation and render update");
}
}
}
}
{
MProfilingScope profilingScopeFallback(_profilerCategory, MProfiler::kColorD_L2,
"Fall back to native");
}
}
{
reset();
}
void* constraintEvaluator::creator()
{
constraintEvaluator* newEval = new constraintEvaluator();
newEval->reset();
return ((void*) newEval);
}
const char* constraintEvaluator::evaluatorName()
{
return "ConstraintEvaluator";
}
constraintEvaluator::~constraintEvaluator()
{
}
{
MFnPlugin plugin( obj, PLUGIN_COMPANY,
"3.0",
"Any");
status = plugin.registerEvaluator("ConstraintEvaluator", 42, constraintEvaluator::creator);
if (!status)
{
status.
perror(
"registerEvaluator");
return status;
}
return status;
}
{
status = plugin.deregisterEvaluator( "ConstraintEvaluator" );
if (!status)
{
status.
perror(
"deRegisterEvaluator");
return status;
}
return status;
}