pointOnMeshCmd.cpp
#include "pointOnMeshCmd.h"
#include "getPointAndNormal.h"
pointOnMeshCommand::pointOnMeshCommand()
{
}
pointOnMeshCommand::~pointOnMeshCommand()
{
}
void* pointOnMeshCommand::creator()
{
return new pointOnMeshCommand;
}
bool pointOnMeshCommand::isUndoable() const
{
return true;
}
MStatus pointOnMeshCommand::doIt(const MArgList& args)
{
nodeCreated = positionSpecified = normalSpecified = faceIndexSpecified = relativeSpecified = parameterUSpecified = parameterVSpecified = false;
meshNodeName = pointOnMeshInfoName = "";
for (unsigned i=0; i<args.length(); i++)
{
if ((MString("-name")==args.asString(i)) || (MString("-na")==args.asString(i)))
pointOnMeshInfoName = args.asString(++i);
else if ((MString("-position")==args.asString(i)) || (MString("-p")==args.asString(i)))
positionSpecified = true;
else if ((MString("-normal")==args.asString(i)) || (MString("-nr")==args.asString(i)))
normalSpecified = true;
else if ((MString("-faceIndex")==args.asString(i)) || (MString("-f")==args.asString(i)))
{
faceIndexSpecified = true;
int temp = args.asInt(++i);
if (temp<0)
{
displayError("Invalid faceIndex!");
return MS::kFailure;
}
faceIndex = temp;
}
else if ((MString("-relative")==args.asString(i)) || (MString("-r")==args.asString(i)))
{
relativeSpecified = true;
relative = args.asBool(++i);
}
else if ((MString("-parameterU")==args.asString(i)) || (MString("-u")==args.asString(i)))
{
parameterUSpecified = true;
double temp = args.asDouble(++i);
if ((temp<0) || (temp>1))
{
displayError("Invalid parameterU!");
return MS::kFailure;
}
parameterU = temp;
}
else if ((MString("-parameterV")==args.asString(i)) || (MString("-v")==args.asString(i)))
{
parameterVSpecified = true;
double temp = args.asDouble(++i);
if ((temp<0) || (temp>1))
{
displayError("Invalid parameterV!");
return MS::kFailure;
}
parameterV = temp;
}
else if (i==(args.length()-1))
meshNodeName = args.asString(i);
else
{
MString errorMessage = "Invalid flag: ";
errorMessage += args.asString(i);
displayError(errorMessage);
return MS::kFailure;
}
}
if (!faceIndexSpecified)
faceIndex = 0;
if (!relativeSpecified)
relative = true;
if (!parameterUSpecified)
parameterU = 0.5;
if (!parameterVSpecified)
parameterV = 0.5;
return redoIt();
}
MStatus pointOnMeshCommand::redoIt()
{
MSelectionList sList;
if (meshNodeName=="")
{
MGlobal::getActiveSelectionList(sList);
if (sList.length()==0)
{
displayError("No mesh or mesh transform specified!");
return MS::kFailure;
}
}
else if (sList.add(meshNodeName) == MS::kInvalidParameter)
{
displayError("Specified mesh does not exist!");
return MS::kFailure;
}
MDagPath meshDagPath;
sList.getDagPath(0, meshDagPath);
MPoint point;
MVector normal;
if (meshDagPath.node().hasFn(MFn::kMesh))
{
if (!positionSpecified && !normalSpecified)
{
nodeCreated = true;
MFnDependencyNode depNodeFn;
if (pointOnMeshInfoName == "")
depNodeFn.create("pointOnMeshInfo");
else
depNodeFn.create("pointOnMeshInfo", pointOnMeshInfoName);
pointOnMeshInfoName = depNodeFn.name();
if (faceIndexSpecified)
{
MPlug faceIndexPlug = depNodeFn.findPlug("faceIndex");
faceIndexPlug.setValue(faceIndex);
}
if (relativeSpecified)
{
MPlug relativePlug = depNodeFn.findPlug("relative");
relativePlug.setValue(relative);
}
if (parameterUSpecified)
{
MPlug parameterUPlug = depNodeFn.findPlug("parameterU");
parameterUPlug.setValue(parameterU);
}
if (parameterVSpecified)
{
MPlug parameterVPlug = depNodeFn.findPlug("parameterV");
parameterVPlug.setValue(parameterV);
}
MPlug worldMeshPlug, inMeshPlug;
inMeshPlug = depNodeFn.findPlug("inMesh");
depNodeFn.setObject(meshDagPath.node());
worldMeshPlug = depNodeFn.findPlug("worldMesh");
worldMeshPlug = worldMeshPlug.elementByLogicalIndex(0);
MDGModifier dgModifier;
dgModifier.connect(worldMeshPlug, inMeshPlug);
dgModifier.doIt();
setResult(pointOnMeshInfoName);
return MStatus::kSuccess;
}
else
getPointAndNormal(meshDagPath, faceIndex, relative, parameterU, parameterV, point, normal);
}
else if (meshDagPath.node().hasFn(MFn::kTransform) && meshDagPath.hasFn(MFn::kMesh))
{
if (!positionSpecified && !normalSpecified)
{
nodeCreated = true;
meshDagPath.extendToShape();
MFnDependencyNode depNodeFn;
if (pointOnMeshInfoName == "")
depNodeFn.create("pointOnMeshInfo");
else
depNodeFn.create("pointOnMeshInfo", pointOnMeshInfoName);
pointOnMeshInfoName = depNodeFn.name();
if (faceIndexSpecified)
{
MPlug faceIndexPlug = depNodeFn.findPlug("faceIndex");
faceIndexPlug.setValue(faceIndex);
}
if (relativeSpecified)
{
MPlug relativePlug = depNodeFn.findPlug("relative");
relativePlug.setValue(relative);
}
if (parameterUSpecified)
{
MPlug parameterUPlug = depNodeFn.findPlug("parameterU");
parameterUPlug.setValue(parameterU);
}
if (parameterVSpecified)
{
MPlug parameterVPlug = depNodeFn.findPlug("parameterV");
parameterVPlug.setValue(parameterV);
}
MPlug worldMeshPlug, inMeshPlug;
inMeshPlug = depNodeFn.findPlug("inMesh");
depNodeFn.setObject(meshDagPath.node());
worldMeshPlug = depNodeFn.findPlug("worldMesh");
worldMeshPlug = worldMeshPlug.elementByLogicalIndex(meshDagPath.instanceNumber());
MDGModifier dgModifier;
dgModifier.connect(worldMeshPlug, inMeshPlug);
dgModifier.doIt();
setResult(pointOnMeshInfoName);
return MStatus::kSuccess;
}
else
getPointAndNormal(meshDagPath, faceIndex, relative, parameterU, parameterV, point, normal);
}
else
{
displayError("Invalid type! Only a mesh or its transform can be specified!");
return MStatus::kFailure;
}
MDoubleArray result;
if (positionSpecified)
{
result.append(point.x);
result.append(point.y);
result.append(point.z);
}
if (normalSpecified)
{
result.append(normal.x);
result.append(normal.y);
result.append(normal.z);
}
setResult(result);
return MStatus::kSuccess;
}
MStatus pointOnMeshCommand::undoIt()
{
if (nodeCreated)
{
MString deleteCmd = "delete ";
deleteCmd += pointOnMeshInfoName;
MGlobal::executeCommand(deleteCmd);
}
return MStatus::kSuccess;
}