/*---------------------------------------------------------------------- Maya Programming - Lab6 Daniel Loran (2007) ------------------------------------------------------------------------*/ #include // needed to compile on Windows (if you work with Unix remove this include) #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; class Math{ public: /*---------------------------------------------------------------------- Return int random number in a given range (min, max) ------------------------------------------------------------------------*/ static int random(int min, int max){ int result = min + rand() % (max - min); return result; } /*---------------------------------------------------------------------- Return double random number in a given range (min, max) ------------------------------------------------------------------------*/ static double random(double min, double max){ double result = min + rand() * (max - min) / RAND_MAX; return result; } }; class Lab6 : public MPxCommand{ public: virtual MStatus doIt ( const MArgList& ); virtual MStatus undoIt(); virtual MStatus redoIt(); virtual bool isUndoable() const { return true; } static void *creator() { return new Lab6; } static MSyntax newSyntax(); private: MDGModifier dgMod; }; const char *randomFlag = "-r", *randomLongFlag = "-random"; const char *numberFlag = "-nr", *numberLongFlag = "-number"; const char *heightFlag = "-he", *heightLongFlag = "-height"; const char *helpFlag = "-h", *helpLongFlag = "-help"; const char *helpText = "\nThe mplab6 command duplicates any object called myObject1 along the selected curve/s." "\nTo see the list of accepted parameters type: help mplab6"; MSyntax Lab6::newSyntax(){ MSyntax syntax; syntax.addFlag( randomFlag, randomLongFlag ); syntax.addFlag( numberFlag, numberLongFlag, MSyntax::kLong ); syntax.addFlag( heightFlag, heightLongFlag, MSyntax::kDouble ); syntax.addFlag( helpFlag, helpLongFlag ); return syntax; } MStatus Lab6::doIt ( const MArgList &args ){ static int sceneObjectIndex = 1; bool random = false; int nPosts = 20; MString sourceObject = "myObject1"; double height = 1.0; MArgDatabase argData( syntax(), args ); if( argData.isFlagSet( randomFlag ) ) random = true; if( argData.isFlagSet( numberFlag ) ) argData.getFlagArgument( numberFlag, 0, nPosts ); if( argData.isFlagSet( heightFlag ) ) argData.getFlagArgument( heightFlag, 0, height ); if( argData.isFlagSet( helpFlag ) ){ setResult( helpText ); return MS::kSuccess; } // Get a list of currently selected objects MSelectionList selection; MGlobal::getActiveSelectionList( selection ); MDagPath dagPath; MFnNurbsCurve curveFn; // Iterate over the nurbs curves MItSelectionList iter( selection, MFn::kNurbsCurve ); for ( ; !iter.isDone(); iter.next() ){ // Get the curve and attach it to the function set iter.getDagPath( dagPath ); curveFn.setObject( dagPath ); // Get the domain of the curve, i.e. its start and end parametric values double tStart, tEnd; curveFn.getKnotDomain( tStart, tEnd ); MPoint pt; int i; double t; double tIncr = (tEnd - tStart) / (nPosts - 1); for( i=0, t=tStart; i < nPosts; i++, t += tIncr ){ // Get point along curve at parametric position t curveFn.getPointAtParam( t, pt, MSpace::kWorld ); if(random) height = Math::random(0.5, 3.0); // modify the height randomly MString objName = sourceObject + sceneObjectIndex; //cout << "sourceObject = " << sourceObject << endl; //cout << "objName = " << objName << endl; dgMod.commandToExecute( MString( "select -replace ") + sourceObject ); dgMod.commandToExecute( MString( "duplicate -returnRootsOnly -name ") + "\"" + objName + "\"" ); dgMod.commandToExecute( MString("move -x ") + pt.x + " -y " + pt.y + " -z " + pt.z + " " + objName ); dgMod.commandToExecute( MString("setAttr ") + objName + ".scaleZ " + height ); sceneObjectIndex += 1; } } return redoIt(); } MStatus Lab6::undoIt(){ return dgMod.undoIt(); } MStatus Lab6::redoIt(){ return dgMod.doIt(); } MStatus initializePlugin( MObject obj ){ MFnPlugin plugin( obj, "Daniel Loran", "1.0" ); MStatus stat; stat = plugin.registerCommand( "mplab6", Lab6::creator, Lab6::newSyntax ); if ( !stat ) stat.perror( "registerCommand failed"); return stat; } MStatus uninitializePlugin( MObject obj ){ MFnPlugin plugin( obj ); MStatus stat; stat = plugin.deregisterCommand( "mplab6" ); if ( !stat ) stat.perror( "deregisterCommand failed" ); return stat; }