#include <math.h>
#include <maya/MIOStream.h>
#include <maya/MPxCommand.h>
#include <maya/MStatus.h>
#include <maya/MObject.h>
#include <maya/MFnPlugin.h>
#include <maya/MString.h>
#include <maya/MArgList.h>
#include <maya/MGlobal.h>
#include <maya/MPoint.h>
#include <maya/MVector.h>
#include <maya/MMatrix.h>
#include <maya/MDagPath.h>
#include <maya/MSelectionList.h>
#include <maya/MItSelectionList.h>
#include <maya/MItSurfaceCV.h>
#include <maya/MItMeshVertex.h>
class surfaceTwist : public MPxCommand {
public:
surfaceTwist();
virtual ~surfaceTwist();
virtual MStatus doIt( const MArgList& args );
static void* creator();
};
#define NUM_SPANS 30
#define WIDTH 10.0
#define VERTICAL_SCALING 4.0
surfaceTwist::~surfaceTwist() {}
surfaceTwist::surfaceTwist() {}
void* surfaceTwist::creator()
{
return new surfaceTwist;
}
static MStatus twistNurbsSurface(MDagPath& objectPath, MObject& component)
{
MStatus status;
MPoint center;
MVector toCenter( -center.x, 0.0, -center.y );
double rotFactor = 0.5;
MItSurfaceCV cvIter( objectPath, component, true, &status );
if ( MS::kSuccess == status ) {
for ( ; !cvIter.isDone(); cvIter.nextRow() ) {
for ( ; !cvIter.isRowDone(); cvIter.next() ) {
MPoint pnt = cvIter.position( MSpace::kWorld );
pnt = pnt + toCenter;
double rotation = pnt.y * rotFactor;
MMatrix rotMatrix;
rotMatrix(0,0) = cos( rotation );
rotMatrix(0,2) = sin( rotation );
rotMatrix(2,0) = -sin( rotation );
rotMatrix(2,2) = cos( rotation );
pnt = ( pnt * rotMatrix ) - toCenter;
status = cvIter.setPosition( pnt, MSpace::kWorld );
if ( MS::kSuccess != status )
break;
}
}
cvIter.updateSurface();
return MS::kSuccess;
} else
return MS::kFailure;
}
static MStatus twistPolygon(MDagPath& objectPath, MObject& component)
{
MStatus status;
MPoint center;
MVector toCenter( -center.x, 0.0, -center.y );
double rotFactor = 0.5;
MItMeshVertex vertIter( objectPath, component, &status );
if ( MS::kSuccess == status ) {
for ( ; !vertIter.isDone(); vertIter.next() ) {
MPoint pnt = vertIter.position( MSpace::kWorld );
pnt = pnt + toCenter;
double rotation = pnt.y * rotFactor;
MMatrix rotMatrix;
rotMatrix(0,0) = cos( rotation );
rotMatrix(0,2) = sin( rotation );
rotMatrix(2,0) = -sin( rotation );
rotMatrix(2,2) = cos( rotation );
pnt = ( pnt * rotMatrix ) - toCenter;
status = vertIter.setPosition( pnt, MSpace::kWorld );
if ( MS::kSuccess != status )
break;
}
vertIter.updateSurface();
return MS::kSuccess;
} else
return MS::kFailure;
}
MStatus surfaceTwist::doIt( const MArgList& )
{
MStatus status;
MSelectionList slist;
MGlobal::getActiveSelectionList( slist );
MItSelectionList iter( slist );
if (iter.isDone()) {
cerr << "Nothing selected\n";
return MS::kFailure;
}
MDagPath objectPath;
MObject component;
for ( ; !iter.isDone(); iter.next() ) {
status = iter.getDagPath( objectPath, component );
if (objectPath.hasFn(MFn::kNurbsSurface))
status = twistNurbsSurface(objectPath, component);
else if (objectPath.hasFn(MFn::kMesh))
status = twistPolygon(objectPath, component);
else {
cerr << "Selected object is not a NURBS surface or a polygon\n";
return MS::kFailure;
}
}
return status;
}
MStatus initializePlugin( MObject obj )
{
MStatus status;
MFnPlugin plugin( obj, PLUGIN_COMPANY, "3.0", "Any");
status = plugin.registerCommand( "surfaceTwist", surfaceTwist::creator );
if (!status) {
status.perror("registerCommand");
return status;
}
return status;
}
MStatus uninitializePlugin( MObject obj )
{
MStatus status;
MFnPlugin plugin( obj );
status = plugin.deregisterCommand( "surfaceTwist" );
if (!status) {
status.perror("deregisterCommand");
return status;
}
return status;
}