meshReorderCmd.cpp
#include "meshReorderCmd.h"
#include "meshMapUtils.h"
#include <maya/MItSelectionList.h>
#include <maya/MItMeshVertex.h>
#include <maya/MArgList.h>
#include <maya/MFloatPointArray.h>
#include <maya/MFnMesh.h>
#include <maya/MFnMeshData.h>
#include <maya/MDagPathArray.h>
meshReorderCommand::meshReorderCommand()
{
}
meshReorderCommand::~meshReorderCommand()
{
}
void* meshReorderCommand::creator()
{
return new meshReorderCommand;
}
MStatus meshReorderCommand::parseArgs(const MArgList& args)
{
MStatus stat;
MString err;
if( args.length() != 3 )
{
displayError("3 vertices must be specified");
return MS::kFailure;
}
MObjectArray selectedComponent(3);
MDagPathArray selectedPath;
selectedPath.setLength(3);
int argIdx = 0;
for (unsigned int j = 0; j < 3; j++)
{
MString arg;
if( ( stat = args.get( argIdx, arg )) != MStatus::kSuccess )
{
displayError( "Can't parse arg");
return stat;
}
MSelectionList list;
if (! list.add( arg ) )
{
err = arg + ": no such component";
displayError(err);
return MS::kFailure;
}
MItSelectionList selectionIt (list, MFn::kComponent);
if (selectionIt.isDone ())
{
err = arg + ": not a component";
displayError (err);
return MS::kFailure;
}
if( selectionIt.getDagPath (selectedPath[j], selectedComponent[j]) != MStatus::kSuccess )
{
displayError( "Can't get path for");
return stat;
}
if (!selectedPath[j].node().hasFn(MFn::kMesh) && !(selectedPath[j].node().hasFn(MFn::kTransform) && selectedPath[j].hasFn(MFn::kMesh)))
{
err = arg + ": Invalid type! Only a mesh or its transform can be specified!";
displayError (err);
return MStatus::kFailure;
}
argIdx++;
}
if( ( stat = meshMapUtils::validateFaceSelection( selectedPath, selectedComponent, &fFaceIdxSrc, &fFaceVtxSrc ) ) != MStatus::kSuccess )
{
displayError("Selected vertices don't define a unique face on source mesh");
return stat;
}
fDagPathSrc = selectedPath[0];
return stat;
}
MStatus meshReorderCommand::doIt(const MArgList& args)
{
MStatus stat = MStatus::kSuccess;
if ( ( stat = parseArgs( args ) ) != MStatus::kSuccess )
{
displayError ("Error parsing arguments");
return stat;
}
return redoIt();
}
MStatus meshReorderCommand::redoIt()
{
MStatus stat = MStatus::kSuccess;
MIntArray newPolygonCounts;
MIntArray newPolygonConnects;
MFloatPointArray origVertices;
MFloatPointArray newVertices;
MFnMesh theMesh( fDagPathSrc, &stat );
if( stat != MStatus::kSuccess )
{
displayError(" MFnMesh creation");
return stat;
}
stat = theMesh.getPoints (origVertices, MSpace::kWorld );
if( stat != MStatus::kSuccess )
{
displayError(" MFnMesh getPoints");
return stat;
}
MIntArray faceTraversal( theMesh.numPolygons(), false );
MIntArray cvMapping( theMesh.numVertices(), -1 );
MIntArray cvMappingInverse( theMesh.numVertices(), -1 );
stat = meshMapUtils::traverseFace( fDagPathSrc, fFaceIdxSrc, fFaceVtxSrc[0], fFaceVtxSrc[1], faceTraversal,
cvMapping, cvMappingInverse,
newPolygonCounts, newPolygonConnects,
origVertices, newVertices );
if ( stat != MStatus::kSuccess )
{
displayError(" could not process all the mesh faces.");
return stat;
}
theMesh.createInPlace( newVertices.length(), newPolygonCounts.length(), newVertices, newPolygonCounts, newPolygonConnects );
return stat;
}
MStatus meshReorderCommand::setSeedInfo( MDagPath& path, int face, int vtx0, int vtx1 )
{
fFaceIdxSrc = face;
fFaceVtxSrc.setLength(2);
fFaceVtxSrc[0] = vtx0;
fFaceVtxSrc[1] = vtx1;
fDagPathSrc = path;
return MStatus::kSuccess;
}