meshReorderTool.cpp
#define _MApiVersion
#include <maya/MIOStream.h>
#include <maya/MGlobal.h>
#include <maya/MCursor.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MItMeshVertex.h>
#include <meshReorderTool.h>
#include <meshMapUtils.h>
meshReorderTool::meshReorderTool()
{
setTitleString ( "Mesh Reorder Tool" );
setCursor( MCursor::editCursor );
reset();
}
meshReorderTool::~meshReorderTool() {}
void* meshReorderTool::creator()
{
return new meshReorderTool;
}
void meshReorderTool::toolOnSetup ( MEvent & )
{
reset();
}
void meshReorderTool::reset()
{
MEvent e;
fNumSelectedPoints = 0;
fSelectedPathSrc.clear();
fSelectedComponentSrc.clear();
fSelectVtxSrc.clear();
fSelectedFaceSrc = -1;
fCurrentHelpString = "Select 1st point on mesh.";
helpStateHasChanged( e );
}
MStatus meshReorderTool::doRelease( MEvent & event )
{
char buf[1024];
MStatus stat = MPxSelectionContext::doRelease(event);
MGlobal::getActiveSelectionList( fSelectionList );
if( fSelectionList.length() != 1 )
{
MGlobal::displayWarning( "Components must be selected one at a time" );
return MS::kSuccess;
}
MObject component;
MDagPath path;
MItSelectionList selectionIt (fSelectionList, MFn::kComponent);
MStringArray selections;
selectionIt.getStrings( selections );
if( selections.length() != 1 )
{
MGlobal::displayError( "Components must be selected one at a time" );
return MS::kSuccess;
}
if (selectionIt.isDone ())
{
MGlobal::displayError( "Selected Item not a component" );
return MS::kSuccess;
}
if( selectionIt.getDagPath (path, component) != MStatus::kSuccess )
{
MGlobal::displayError( "Can't get path for selection");
return MS::kSuccess;
}
if (!path.node().hasFn(MFn::kMesh) && !(path.node().hasFn(MFn::kTransform) && path.hasFn(MFn::kMesh)))
{
MGlobal::displayError( " Invalid type! Only a mesh or its transform can be specified." );
return MS::kSuccess;
}
MItMeshVertex fIt ( path, component, &stat );
if( stat != MStatus::kSuccess )
{
MGlobal::displayError( " MItMeshVertex failed");
return MStatus::kFailure;
}
if (fIt.count() != 1 )
{
sprintf(buf, " Invalid selection '%s'. Vertices must be picked one at a time.", selections[0].asChar() );
MGlobal::displayError( buf );
return MS::kSuccess;
}
else
{
sprintf(buf, "Accepting vertex '%s'", selections[0].asChar() );
MGlobal::displayInfo( buf );
}
fSelectedPathSrc.append( path );
fSelectedComponentSrc.append( component );
if( fNumSelectedPoints == 2 )
{
if( ( stat = meshMapUtils::validateFaceSelection( fSelectedPathSrc, fSelectedComponentSrc, &fSelectedFaceSrc, &fSelectVtxSrc ) ) != MStatus::kSuccess )
{
MGlobal::displayError("Selected vertices don't define a unique face on mesh");
reset();
return stat;
}
fCmd = new meshReorderCommand;
fCmd->setSeedInfo( fSelectedPathSrc[0], fSelectedFaceSrc, fSelectVtxSrc[0], fSelectVtxSrc[1] );
fCmd->redoIt();
MSelectionList empty;
MGlobal::setActiveSelectionList( empty );
MGlobal::displayInfo( "Mesh reordering complete" );
reset();
}
else
{
fNumSelectedPoints++;
}
helpStateHasChanged( event );
return stat;
}
MStatus meshReorderTool::helpStateHasChanged( MEvent &)
{
switch (fNumSelectedPoints)
{
case 0:
fCurrentHelpString = "Select 1st point on mesh.";
break;
case 1:
fCurrentHelpString = "Select 2nd point on mesh.";
break;
case 2:
fCurrentHelpString = "Select 3rd point on mesh.";
break;
default:
break;
}
setHelpString( fCurrentHelpString );
return MS::kSuccess;
}
MPxContext* meshReorderContextCmd::makeObj()
{
return new meshReorderTool;
}
void* meshReorderContextCmd::creator()
{
return new meshReorderContextCmd;
}