#include <maya/MFnPlugin.h>
#include <maya/MDagPath.h>
#include <maya/MFnMesh.h>
#include <maya/MMeshIntersector.h>
#include <maya/MItSelectionList.h>
#include <maya/MItMeshPolygon.h>
#include <maya/MObject.h>
#include <maya/MSelectionList.h>
#include <maya/MArgList.h>
#include <maya/MFloatPoint.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MFnPointLight.h>
#include <maya/MFloatPointArray.h>
#include <maya/MPxCommand.h>
#include <maya/MDGModifier.h>
#include <maya/MFnDagNode.h>
#include <maya/MDagPath.h>
#include <maya/MGlobal.h>
#include <maya/MIOStream.h>
#define MERR_CHK(status,msg) if ( !status ) { MGlobal::displayError(msg); } // cerr << msg << endl; }
class closestPointCmd : public MPxCommand
{
public:
closestPointCmd();
virtual ~closestPointCmd();
static void* creator();
bool isUndoable() const;
MStatus doIt(const MArgList&);
MStatus undoIt();
void createDisplay( MPointOnMesh& info, MMatrix& matrix);
};
closestPointCmd::closestPointCmd()
{
}
closestPointCmd::~closestPointCmd()
{
}
void* closestPointCmd::creator()
{
return new closestPointCmd;
}
bool closestPointCmd::isUndoable() const
{
return false;
}
MStatus closestPointCmd::doIt(const MArgList& args)
{
MStatus status = MStatus::kSuccess;
if (args.length() != 2)
{
MGlobal::displayError("Need 2 items!");
return MStatus::kFailure;
}
MSelectionList activeList;
int i;
for ( i = 0; i < 2; i++)
{
MString strCurrSelection;
status = args.get(i, strCurrSelection);
if (MStatus::kSuccess == status) activeList.add(strCurrSelection);
}
MItSelectionList iter(activeList);
MFnPointLight fnLight;
MFnDagNode dagNod;
MFnDependencyNode fnDN;
MDagPath pathToMesh;
float fX = 0;
float fY = 0;
float fZ = 0;
for ( ; !iter.isDone(); iter.next() )
{
MObject tempObjectParent, tempObjectChild;
iter.getDependNode(tempObjectParent);
if (tempObjectParent.apiType() == MFn::kTransform)
{
dagNod.setObject(tempObjectParent);
tempObjectChild = dagNod.child(0, &status);
}
if (tempObjectChild.apiType() == MFn::kPointLight)
{
MDagPath pathToLight;
MERR_CHK(MDagPath::getAPathTo(tempObjectParent, pathToLight),
"Couldn't get a path to the pointlight");
MERR_CHK(fnLight.setObject(pathToLight), "Failure on assigning light");
status = fnDN.setObject(tempObjectParent);
MPlug pTempPlug = fnDN.findPlug("translateX", &status);
if (MStatus::kSuccess == status)
{
pTempPlug.getValue(fX);
}
pTempPlug = fnDN.findPlug("translateY", &status);
if (MStatus::kSuccess == status)
{
pTempPlug.getValue(fY);
}
pTempPlug = fnDN.findPlug("translateZ", &status);
if (MStatus::kSuccess == status)
{
pTempPlug.getValue(fZ);
}
}
else if (tempObjectChild.apiType() == MFn::kMesh)
{
MERR_CHK(MDagPath::getAPathTo(tempObjectChild, pathToMesh),
"Couldn't get a path to the pointlight");
}
else
{
MGlobal::displayError("Need a pointlight and a mesh");
return MStatus::kFailure;
}
}
MMeshIntersector intersector;
MMatrix matrix = pathToMesh.inclusiveMatrix();
MObject node = pathToMesh.node();
status = intersector.create( node, matrix );
if ( status )
{
MPoint point(fX,fY,fZ);
MPointOnMesh pointInfo;
cout << "Using point: " << point << endl;
status = intersector.getClosestPoint( point, pointInfo );
if ( status )
{
createDisplay( pointInfo, matrix );
}
else
{
MGlobal::displayError("Failed to get closest point");
}
}
else
{
MGlobal::displayError("Failed to create intersector");
}
return status;
}
MStatus closestPointCmd::undoIt()
{
MStatus status;
return status;
}
void closestPointCmd::createDisplay( MPointOnMesh& info, MMatrix& matrix )
{
MPoint worldPoint( info.getPoint() );
worldPoint = worldPoint * matrix;
MVector worldNormal( info.getNormal() );
worldNormal = worldNormal * matrix;
worldNormal.normalize();
MString strCommandString = "string $strBall[] = `polySphere -r 0.5`;";
strCommandString += "$strBallName = $strBall[0];";
strCommandString += "setAttr ($strBallName + \".tx\") ";
strCommandString += worldPoint.x;
strCommandString += ";";
strCommandString += "setAttr ($strBallName + \".ty\") ";
strCommandString += worldPoint.y;
strCommandString += ";";
strCommandString += "setAttr ($strBallName + \".tz\") ";
strCommandString += worldPoint.z;
strCommandString += ";";
MGlobal::executeCommand(strCommandString);
cout << "Normal: " << worldNormal << " face id: " << info.faceIndex() <<
" triangle id: " << info.triangleIndex() << endl;
}
MStatus initializePlugin(MObject obj)
{
MStatus status;
MFnPlugin plugin(obj, PLUGIN_COMPANY, "9.0", "Any");
status = plugin.registerCommand("closestPointCmd", closestPointCmd::creator);
return status;
}
MStatus uninitializePlugin(MObject obj)
{
MStatus status;
MFnPlugin plugin(obj);
plugin.deregisterCommand("closestPointCmd");
return status;
}