convertVerticesToEdgesCmd.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/MItMeshEdge.h>
class convertVerticesToContainedEdgesCommand : public MPxCommand
{
public:
convertVerticesToContainedEdgesCommand();
virtual ~convertVerticesToContainedEdgesCommand();
static void* creator();
bool isUndoable() const;
MStatus doIt(const MArgList&);
MStatus redoIt();
MStatus undoIt();
private:
MSelectionList previousSelectionList;
};
convertVerticesToContainedEdgesCommand::convertVerticesToContainedEdgesCommand()
{
}
convertVerticesToContainedEdgesCommand::~convertVerticesToContainedEdgesCommand()
{
previousSelectionList.clear();
}
void* convertVerticesToContainedEdgesCommand::creator()
{
return new convertVerticesToContainedEdgesCommand;
}
bool convertVerticesToContainedEdgesCommand::isUndoable() const
{
return true;
}
MStatus convertVerticesToContainedEdgesCommand::doIt(const MArgList& args)
{
MGlobal::getActiveSelectionList(previousSelectionList);
return redoIt();
}
MStatus convertVerticesToContainedEdgesCommand::redoIt()
{
MSelectionList finalEdgesSelection;
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 connectedEdgesIndices;
vertexIter.getConnectedEdges(connectedEdgesIndices);
MItMeshEdge edgeIter(meshDagPath);
for (unsigned i=0; i<connectedEdgesIndices.length(); i++)
{
edgeIter.setIndex(connectedEdgesIndices[i], dummyIndex);
MSelectionList singleVertexList;
MString vertexName = meshName;
vertexName += ".vtx[";
vertexName += edgeIter.index(0);
vertexName += "]";
singleVertexList.add(vertexName);
singleVertexList.getDagPath(0, meshDagPath, singleVertexComponent);
if (!singleVertexComponent.isNull() && previousSelectionList.hasItem(meshDagPath, singleVertexComponent))
{
singleVertexList.clear();
vertexName = meshName;
vertexName += ".vtx[";
vertexName += edgeIter.index(1);
vertexName += "]";
singleVertexList.add(vertexName);
singleVertexList.getDagPath(0, meshDagPath, singleVertexComponent);
if (!singleVertexComponent.isNull() && previousSelectionList.hasItem(meshDagPath, singleVertexComponent))
{
MString edgeName = meshName;
edgeName += ".e[";
edgeName += connectedEdgesIndices[i];
edgeName += "]";
finalEdgesSelection.add(edgeName);
}
}
}
}
}
}
MGlobal::setActiveSelectionList(finalEdgesSelection, MGlobal::kReplaceList);
MStringArray containedEdgesArray;
finalEdgesSelection.getSelectionStrings(containedEdgesArray);
MPxCommand::setResult(containedEdgesArray);
return MS::kSuccess;
}
MStatus convertVerticesToContainedEdgesCommand::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("convertVerticesToEdges", convertVerticesToContainedEdgesCommand::creator);
if (!status)
{
status.perror("registerCommand");
return status;
}
return status;
}
MStatus uninitializePlugin(MObject obj)
{
MStatus status;
MFnPlugin plugin(obj);
status = plugin.deregisterCommand("convertVerticesToEdges");
if (!status)
{
status.perror("deregisterCommand");
return status;
}
return status;
}