Sunday, March 31, 2013

Rotate Vertex Maya Api Plugin -- Part 2

Hi,
I made some additions to code. There are still the same issues as before no undo ..
and it is messing up rotating one side.  But hope this is helpful.

cheers,
Nate




/**@file rotatePolyCmd.cpp -- select some polygons and type in mel //rotatePolyCmd;  which will rotate in x all vertices by 45 degrees
@brief v1.0.0 given selected polyShape it rotates all the vertices by 90 degrees
@author Nathaniel O. Anozie (ogbonnawork at gmail dot com)
@bug all functions no assert checks
@note date created: 02/19/2013
@note this has some useful info for
@note inspired by Foley & Van Dam Chapter 5, Geometrical Transformations
@note inspired by Rick Parent (cse dot ohio-state dot edu) accessVertices.cpp
@note inspired by Autdodesk Api tutorial examples surfaceTwistCmd.cpp, translateCmd.cpp learning about MMatrix, and MItMeshVertex
*/

#include < maya/MSimple.h>
#include < maya/MGlobal.h>
//im guessing these are needed almost always
//printing stuff, selecting stuff
#include < maya/MItSelectionList.h>

//need iterating for polygon vertex
#include < maya/MItMeshVertex.h>
//#include < maya/MFnMesh.h>
#include < maya/MPointArray.h>

//need finding outliner polys
#include < maya/MDagpath.h>

//need for matrix stuff
#include < math.h>
#include < maya/MPoint.h>
#include < maya/MVector.h>
#include < maya/MMatrix.h>


//the name used here is the command that will be typed in Maya like rotatePolyCmd;
DeclareSimpleCommand( rotatePolyCmd, "Autodesk", "8.0");


/**the names here needs to match that used in DeclareSimpleCommand
@note notice no dollar signs, also we
@note need to do search manual to figure out the kind of thing that will help us find keyframes
@note the ampersand is alot different from MEL cause in MEL
@note i think only arrays are passed by reference
@note here a boolean type thing can be edited somewhere else and we can see what we got for error checking           
*/
MStatus rotatePolyCmd::doIt( const MArgList& )
{
    //i'm not sure if this is required in some cases
    MStatus stat = MS::kSuccess;
   
    MSelectionList selList;
    MGlobal::getActiveSelectionList(selList);
   
    //not yet any restrictions
    MItSelectionList polyIt(selList, MFn::kMesh, &stat);//instead of setFilter
    //if used MFn::kInvalid i think more items possible. for any kind poly,nurbs, ..
   
    if(MS::kSuccess == stat){
       
       
        MDagPath meshDag;
        MObject mComponent; //will hold current vtx
       
        //---------- constants    
        //rotate in x all polys of all selected polys about origin
        double rotAmt = 1.571; //in radians about 90 degrees
       
        MPoint mid;
        MVector midC(0.0, -mid.x, -mid.y); //this is crucial something like were rotating in x so need a zero there
       
        //loop polys, were not looping vertices yet
        for( ; !polyIt.isDone(); polyIt.next() )
        {
            polyIt.getDagPath(meshDag, mComponent); //saves how to find this poly from outliner
            
            MItMeshVertex vtxIter( meshDag, mComponent, &stat);
            if( MS::kSuccess == stat ){
               
                //rotate vertices by loop over vertices
                for( ; !vtxIter.isDone(); vtxIter.next() ){
                   
                    MPoint pnt = vtxIter.position( MSpace::kWorld);
                    //MSpace::kWorld
                    //rotate computation
                    //--------
                    pnt = pnt + midC;
                    double rotation = pnt.x * rotAmt;
                    MMatrix rMatrix;
                    rMatrix(1,1) = cos( rotation );
                    rMatrix(1,2) = sin( rotation );
                    rMatrix(2,1) = -sin( rotation );
                    rMatrix(2,2) = cos( rotation );
                    pnt = ( pnt * rMatrix ) - midC;
                   
                    stat = vtxIter.setPosition( pnt, MSpace::kWorld);
                    if( MS::kSuccess != stat ){
                        cerr << "Error setting Vertex\n -- Exiting";
                        break;
                    }
                }//end vertex loop
            }
           
        }//end poly loop
       
    }
    return MS::kSuccess;
   
}