convertVerticesToFacesCmd.cpp
#include <maya/MFnPlugin.h>
#include <maya/MPxCommand.h>
#include <maya/MGlobal.h>
#include <maya/MSelectionList.h>
#include <maya/MItSelectionList.h>
#include <maya/MObject.h>
#include <maya/MDagPath.h>
#include <maya/MItMeshVertex.h>
#include <maya/MItMeshPolygon.h>
class convertVerticesToContainedFacesCommand : public MPxCommand
{
public:
convertVerticesToContainedFacesCommand();
virtual ~convertVerticesToContainedFacesCommand();
static void* creator();
bool isUndoable() const;
MStatus doIt(const MArgList&);
MStatus redoIt();
MStatus undoIt();
private:
MSelectionList previousSelectionList;
};
convertVerticesToContainedFacesCommand::convertVerticesToContainedFacesCommand()
{
}
convertVerticesToContainedFacesCommand::~convertVerticesToContainedFacesCommand()
{
previousSelectionList.clear();
}
void* convertVerticesToContainedFacesCommand::creator()
{
return new convertVerticesToContainedFacesCommand;
}
bool convertVerticesToContainedFacesCommand::isUndoable() const
{
return true;
}
MStatus convertVerticesToContainedFacesCommand::doIt(const MArgList& args)
{
MGlobal::getActiveSelectionList(previousSelectionList);
return redoIt();
}
MStatus convertVerticesToContainedFacesCommand::redoIt()
{
MSelectionList finalFacesSelection;
MDagPath meshDagPath;
MObject multiVertexComponent, singleVertexComponent;
int dummyIndex;
for (MItSelectionList vertexComponentIter(previousSelectionList, MFn::kMeshVertComponent); !vertexComponentIter.isDone(); vertexComponentIter.next())
{
vertexComponentIter.getDagPath(meshDagPath, multiVertexComponent);
MString meshName = meshDagPath.fullPathName();
if (!multiVertexComponent.isNull())
{
for (MItMeshVertex vertexIter(meshDagPath, multiVertexComponent); !vertexIter.isDone(); vertexIter.next())
{
MIntArray connectedFacesIndices;
vertexIter.getConnectedFaces(connectedFacesIndices);
MItMeshPolygon faceIter(meshDagPath);
for (unsigned i=0; i<connectedFacesIndices.length(); i++)
{
MIntArray faceVerticesIndices;
faceIter.setIndex(connectedFacesIndices[i], dummyIndex);
faceIter.getVertices(faceVerticesIndices);
bool faceIsContained=1;
for (unsigned j=0; j<faceVerticesIndices.length(); j++)
{
MSelectionList singleVertexList;
singleVertexList.clear();
MString vertexName = meshName;
vertexName += ".vtx[";
vertexName += faceVerticesIndices[j];
vertexName += "]";
singleVertexList.add(vertexName);
singleVertexList.getDagPath(0, meshDagPath, singleVertexComponent);
if (!previousSelectionList.hasItem(meshDagPath, singleVertexComponent))
{
faceIsContained = 0;
break;
}
}
if (faceIsContained)
{
MString faceName = meshName;
faceName += ".f[";
faceName += connectedFacesIndices[i];
faceName += "]";
finalFacesSelection.add(faceName);
}
}
}
}
}
MGlobal::setActiveSelectionList(finalFacesSelection, MGlobal::kReplaceList);
MStringArray containedFacesArray;
finalFacesSelection.getSelectionStrings(containedFacesArray);
MPxCommand::setResult(containedFacesArray);
return MS::kSuccess;
}
MStatus convertVerticesToContainedFacesCommand::undoIt()
{
MGlobal::setActiveSelectionList(previousSelectionList, MGlobal::kReplaceList);
return MS::kSuccess;
}
MStatus initializePlugin(MObject obj)
{
MStatus status;
MFnPlugin plugin(obj, PLUGIN_COMPANY, "4.0", "Any");
status = plugin.registerCommand("convertVerticesToFaces", convertVerticesToContainedFacesCommand::creator);
if (!status)
{
status.perror("registerCommand");
return status;
}
return status;
}
MStatus uninitializePlugin(MObject obj)
{
MStatus status;
MFnPlugin plugin(obj);
status = plugin.deregisterCommand("convertVerticesToFaces");
if (!status)
{
status.perror("deregisterCommand");
return status;
}
return status;
}