#include <maya/MFnPlugin.h>
#include <maya/MDagPath.h>
#include <maya/MFnMesh.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/MFnSpotLight.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>
#ifndef INTERSECTCMD
#define INTERSECTCMD
#define MERR_CHK(stat,msg) if ( !stat ) { MGlobal::displayError(msg); } // cerr << msg << endl; }
class intersectCmd : public MPxCommand
{
public:
intersectCmd();
virtual ~intersectCmd();
static void* creator();
bool isUndoable() const;
MStatus doIt(const MArgList&);
MStatus undoIt();
};
#endif
intersectCmd::intersectCmd()
{
}
intersectCmd::~intersectCmd()
{
}
void* intersectCmd::creator()
{
return new intersectCmd;
}
bool intersectCmd::isUndoable() const
{
return false;
}
MStatus intersectCmd::doIt(const MArgList& args)
{
MStatus stat = 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;
stat = args.get(i, strCurrSelection);
if (MStatus::kSuccess == stat) activeList.add(strCurrSelection);
}
MItSelectionList iter(activeList);
MFnSpotLight fnLight;
MFnMesh fnMesh;
MFnDagNode dagNod;
MFnDependencyNode fnDN;
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, &stat);
}
if (tempObjectChild.apiType() == MFn::kSpotLight)
{
MDagPath pathToLight;
MERR_CHK(MDagPath::getAPathTo(tempObjectParent, pathToLight), "Couldn't get a path to the spotlight");
MERR_CHK(fnLight.setObject(pathToLight), "Failure on assigning light");
stat = fnDN.setObject(tempObjectParent);
MPlug pTempPlug = fnDN.findPlug("translateX", &stat);
if (MStatus::kSuccess == stat)
{
pTempPlug.getValue(fX);
}
pTempPlug = fnDN.findPlug("translateY", &stat);
if (MStatus::kSuccess == stat)
{
pTempPlug.getValue(fY);
}
pTempPlug = fnDN.findPlug("translateZ", &stat);
if (MStatus::kSuccess == stat)
{
pTempPlug.getValue(fZ);
}
}
else if (tempObjectChild.apiType() == MFn::kMesh)
{
MDagPath pathToMesh;
MERR_CHK(MDagPath::getAPathTo(tempObjectChild, pathToMesh), "Couldn't get a path to the spotlight");
MERR_CHK(fnMesh.setObject(pathToMesh), "Failure on assigning light");
}
else
{
MGlobal::displayError("Need a spotlight and a mesh");
return MStatus::kFailure;
}
}
MFloatPoint fpSource(fX, fY, fZ);
MFloatVector fvRayDir = fnLight.lightDirection(0, MSpace::kWorld, &stat);
MFloatPoint hitPoint;
MMeshIsectAccelParams mmAccelParams = fnMesh.autoUniformGridParams();
float fHitRayParam, fHitBary1, fHitBary2;
int nHitFace, nHitTriangle;
bool bAnyIntersection = fnMesh.anyIntersection(fpSource, fvRayDir, NULL, NULL, false, MSpace::kWorld, (float)9999, false, &mmAccelParams, hitPoint, &fHitRayParam, &nHitFace, &nHitTriangle, &fHitBary1, &fHitBary2, (float)1e-6, &stat);
if (! bAnyIntersection)
{
MGlobal::displayInfo("There were no intersection points detected");
return stat;
}
MFloatPointArray hitPoints;
MFloatArray faHitRayParams;
MIntArray iaHitFaces;
MIntArray iaHitTriangles;
MFloatArray faHitBary1;
MFloatArray faHitBary2;
bool bAllIntersections = fnMesh.allIntersections(fpSource, fvRayDir, NULL, NULL, false, MSpace::kWorld, 9999, false, NULL, false, hitPoints, &faHitRayParams, &iaHitFaces, &iaHitTriangles, &faHitBary1, &faHitBary2, 0.000001f, &stat);
if (! bAllIntersections)
{
MGlobal::displayInfo("Error getting all intersections");
return stat;
}
unsigned int nNumberHitPoints = hitPoints.length();
if (! nNumberHitPoints)
{
MGlobal::displayInfo("No hit points detected");
return MStatus::kSuccess;
}
MString strCommandString = "string $strBall[] = `polySphere -r 0.5`;";
strCommandString += "$strBallName = $strBall[0];";
float x = 0;
float y = 0;
float z = 0;
for (i = 0; i < (int)nNumberHitPoints; i++)
{
x = hitPoints[i][0];
y = hitPoints[i][1];
z = hitPoints[i][2];
strCommandString += "setAttr ($strBallName + \".tx\") ";
strCommandString += x;
strCommandString += ";";
strCommandString += "setAttr ($strBallName + \".ty\") ";
strCommandString += y;
strCommandString += ";";
strCommandString += "setAttr ($strBallName + \".tz\") ";
strCommandString += z;
strCommandString += ";";
MGlobal::executeCommand(strCommandString);
}
return stat;
}
MStatus intersectCmd::undoIt()
{
MStatus status;
return status;
}
MStatus initializePlugin(MObject obj)
{
MStatus status;
MFnPlugin plugin(obj, PLUGIN_COMPANY, "6.5", "Any");
status = plugin.registerCommand("intersectCmd", intersectCmd::creator);
return status;
}
MStatus uninitializePlugin(MObject obj)
{
MStatus status;
MFnPlugin plugin(obj);
plugin.deregisterCommand("intersectCmd");
return status;
}