Skip to content

Instantly share code, notes, and snippets.

@nathanielanozie
Last active August 29, 2015 14:07
Show Gist options
  • Save nathanielanozie/497a44cee78a9b1daf46 to your computer and use it in GitHub Desktop.
Save nathanielanozie/497a44cee78a9b1daf46 to your computer and use it in GitHub Desktop.
make all the selected animation curves' keys stepped or smoothed (maya command plugin)
/**@file naTangentTypeCmd.cpp
@brief v1.0.0 naTangentTypeCmd -step; //make all the selected animation curves' keys stepped or smoothed -smooth
@author Nathaniel O. Anozie (ogbonnawork at gmail dot com)
@note modify at your own risk
*/
//Flags:
//
// -step <string>: whether to use step tangent
//
// -smooth <string>: whether to use smooth tangent
//
//
#include <maya/MPxCommand.h>
#include <maya/MFnPlugin.h>
#include <maya/MGlobal.h>
//for parameter stuff we need this one
#include <maya/MArgList.h>
#include <maya/MItSelectionList.h>
//look in docs what library needed to link to
#include <maya/MItKeyframe.h>
#define na_Cmd "naTangentTypeCmd"
#define na_Author "Nathaniel Anozie"
class naTangentTypeCmd : public MPxCommand
{
public:
naTangentTypeCmd();
virtual ~naTangentTypeCmd();
virtual MStatus doIt ( const MArgList& args );
static void* creator();
private:
int getTangentTypeIndexFromArg(const MArgList& args, MStatus& stat );
};
void * naTangentTypeCmd::creator() { return new naTangentTypeCmd(); }
naTangentTypeCmd::~naTangentTypeCmd() {}
naTangentTypeCmd::naTangentTypeCmd() {}
//also need
//initializePlugin
//uninitializePlugin
MStatus initializePlugin( MObject obj )
{
MFnPlugin plugin( obj, na_Author, "8.0", "Any");
MStatus status = plugin.registerCommand(na_Cmd,
naTangentTypeCmd::creator );
if (!status) status.perror("registerCommand");
return status;
}
MStatus uninitializePlugin( MObject obj )
{
MFnPlugin plugin( obj );
MStatus status = plugin.deregisterCommand(na_Cmd);
if (!status) status.perror("deregisterCommand");
return status;
}
///////////////////////////////////
/**get the tangent type index to use given user arguments, basically I want rest of script
to know what the user selection was while keeping it separate from doIt
@param reference to user parameter (const MargList& arg)
@param reference to status code (MStatus& stat)
@result 1 -- step, 2 -- smooth
*/
int naTangentTypeCmd::getTangentTypeIndexFromArg( const MArgList& arg, MStatus& stat)
{
CHECK_MSTATUS(stat);
int result = 1;
stat = MS::kSuccess;
//like MEL string
MString parameterMessage = "Required single argument: -smooth ; - step; ";
//like MEL's size using length
if(arg.length() != 1){
MGlobal::displayInfo( parameterMessage );
stat.perror(parameterMessage);
stat = MS::kFailure; }
else{
//like MEL, param = array[0]
//
MString param;
param = arg.asString(0,&stat);
MGlobal::displayInfo( param );
//like MEL's strcmp but we'll use ==
//dash needed
if(param == "-smooth"){ result = 2;}
else if(param == "-step"){ result = 1; }
else{
//tell callee that something went wrong
//by looking in docs for MStatus could find out these
stat = MS::kFailure;
stat.perror(parameterMessage);
}
}
//neat were returning something similar to MEL's usual
//int
return result;
}
MStatus naTangentTypeCmd::doIt( const MArgList& arg)
{
MStatus stat = MS::kSuccess;
int tangentTypeChoice = getTangentTypeIndexFromArg( arg, stat );
CHECK_MSTATUS(stat);
MSelectionList selList;
MGlobal::getActiveSelectionList(selList);
MItSelectionList curveIt(selList,MFn::kAnimCurve);
while( !curveIt.isDone() )
{
MObject myCurve;
curveIt.getDependNode(myCurve);
if(!myCurve.isNull())
{
//going from animator curve like animCurveTA to its keyframe
MItKeyframe itKey(myCurve,&stat);
CHECK_MSTATUS(stat);
if(stat == MS::kSuccess)
{
while( !itKey.isDone() )
{
if( tangentTypeChoice == 1 ){
stat = itKey.setInTangentType(MItKeyframe::kTangentStep);
stat = itKey.setOutTangentType(MItKeyframe::kTangentStep);
}
else if( tangentTypeChoice == 2 ){
stat = itKey.setInTangentType(MItKeyframe::kTangentSmooth);
stat = itKey.setOutTangentType(MItKeyframe::kTangentSmooth);
stat = itKey.setTangentsLocked(false);
}
else
{
stat.perror("Skipping");
}
CHECK_MSTATUS(stat);
itKey.next();
}
//end is keyframe check
}
}
//increment selected
curveIt.next();
}
return MS::kSuccess;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment