createClipCmd.cpp
#include <math.h>
#include <maya/MPxCommand.h>
#include <maya/MStatus.h>
#include <maya/MArgList.h>
#include <maya/MFnPlugin.h>
#include <maya/MObject.h>
#include <maya/MGlobal.h>
#include <maya/MSelectionList.h>
#include <maya/MItSelectionList.h>
#include <maya/MObjectArray.h>
#include <maya/MPlugArray.h>
#include <maya/MFnCharacter.h>
#include <maya/MFnClip.h>
#include <maya/MFnAnimCurve.h>
#include <maya/MDGModifier.h>
#include <maya/MIOStream.h>
#define CheckError(stat,msg) \
if ( MS::kSuccess != stat ) { \
displayError(msg); \
continue; \
}
class createClip : public MPxCommand
{
public:
createClip();
virtual ~createClip();
MStatus parseArgs( const MArgList& args );
MStatus doIt ( const MArgList& args );
MStatus redoIt ();
MStatus undoIt ();
bool isUndoable() const;
static void* creator();
private:
MObject fCharacter;
MDGModifier fMod;
};
createClip::createClip()
{
}
createClip::~createClip() {}
void* createClip::creator()
{
return new createClip;
}
bool createClip::isUndoable() const
{
return true;
}
MStatus createClip::undoIt()
{
MStatus status = fMod.undoIt();
return status;
}
MStatus createClip::parseArgs( const MArgList& args )
{
MStatus stat = MS::kSuccess;
MString arg;
MSelectionList list;
bool charNameUsed = 0;
MString charName;
const MString charFlag ("-c");
const MString charFlagLong ("-char");
for ( unsigned int i = 0; i < args.length(); i++ ) {
arg = args.asString( i, &stat );
if (!stat)
continue;
if ( arg == charFlag || arg == charFlagLong ) {
if (i == args.length()-1) {
arg += ": must specify a character name";
displayError(arg);
return MS::kFailure;
}
i++;
args.get(i, charName);
list.add(charName);
charNameUsed = 1;
}
else {
arg += ": unknown argument";
displayError(arg);
return MS::kFailure;
}
}
if (charNameUsed) {
MItSelectionList iter (list);
for ( ; !iter.isDone(); iter.next() ) {
MObject node;
iter.getDependNode(node);
if (node.apiType() == MFn::kCharacter) {
fCharacter = node;
break;
}
}
if (fCharacter.isNull()) {
MString errMsg("Character flag must specify a character node.");
displayError(errMsg);
return MS::kFailure;
}
}
return stat;
}
MStatus createClip::doIt( const MArgList& args )
{
MStatus stat = parseArgs(args);
if (stat != MS::kSuccess) {
return stat;
}
unsigned int count = 0;
MFnCharacter fnCharacter;
if (fCharacter.isNull()) {
MSelectionList activeList;
MGlobal::getActiveSelectionList (activeList);
if (0 == activeList.length()) {
MString errMsg("No character was specified, and no objects were selected.");
displayError(errMsg);
return MS::kFailure;
}
fCharacter = fnCharacter.create(activeList,MFnSet::kNone,&stat);
if (stat != MS::kSuccess) {
MString errMsg("Failed to create character using the selection.");
displayError(errMsg);
return MS::kFailure;
}
} else {
fnCharacter.setObject(fCharacter);
}
MPlugArray plugs;
fnCharacter.getMemberPlugs(plugs);
MTime start(0.0);
MTime duration(10.0);
MObjectArray clipCurves;
for (count = 0; count < plugs.length(); ++count) {
MFnAnimCurve fnCurve;
MObject curve = fnCurve.create(MFnAnimCurve::kAnimCurveTL);
fnCurve.addKeyframe(start,5.0);
fnCurve.addKeyframe(duration,15.0);
clipCurves.append(curve);
}
MFnClip fnClipCreate;
MObject sourceClip = fnClipCreate.createSourceClip(start,duration,fMod,&stat);
fnCharacter.attachSourceToCharacter(sourceClip,fMod);
for (count = 0; count < plugs.length(); ++count) {
MPlug animPlug = plugs[count];
fnCharacter.addCurveToClip(clipCurves[count],sourceClip,animPlug,fMod);
}
MTime schedStart(15.0);
MObject instancedClip = fnClipCreate.createInstancedClip(sourceClip,
schedStart,
fMod,
&stat);
fnCharacter.attachInstanceToCharacter(instancedClip,fMod);
schedStart.setValue(30.0);
MObject instancedClip2 = fnClipCreate.createInstancedClip(sourceClip,
schedStart,
fMod,
&stat);
fnCharacter.attachInstanceToCharacter(instancedClip2,fMod);
return stat;
}
MStatus createClip::redoIt()
{
MStatus status = fMod.doIt();
return status;
}
MStatus initializePlugin( MObject obj )
{
MStatus status;
MFnPlugin plugin( obj, PLUGIN_COMPANY, "4.0", "Any");
status = plugin.registerCommand( "createClip", createClip::creator );
if (!status) {
status.perror("registerCommand");
return status;
}
return status;
}
MStatus uninitializePlugin( MObject obj )
{
MStatus status;
MFnPlugin plugin( obj );
status = plugin.deregisterCommand( "createClip" );
if (!status) {
status.perror("deregisterCommand");
}
return status;
}