latticeNoiseCmd.cpp
#include <maya/MIOStream.h>
#include "latticeNoise.h"
#include <maya/MObject.h>
#include <maya/MFnLatticeDeformer.h>
#include <maya/MSelectionList.h>
#include <maya/MString.h>
#include <maya/MArgList.h>
#include <maya/MGlobal.h>
#include <maya/MItSelectionList.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MFnLattice.h>
#include <maya/MDGModifier.h>
#define McheckErr(stat,msg) \
if ( MS::kSuccess != stat ) { \
cerr << msg << endl; \
return MS::kFailure; \
}
void* latticeNoiseCmd::creator()
{
return new latticeNoiseCmd();
}
MStatus latticeNoiseCmd::doIt( const MArgList& args )
{
MStatus res = MS::kSuccess;
MSelectionList list;
if ( args.length() > 0 ) {
MString argStr;
unsigned last = args.length();
for ( unsigned i = 0; i < last; i++ ) {
res = args.get( i, argStr );
McheckErr( res, "Invalid argument type" );
res = list.add( argStr );
McheckErr( res, "Invalid object" );
}
} else {
MGlobal::getActiveSelectionList( list );
}
MFnLatticeDeformer defFn;
MObject deformNode = defFn.create( 2, 5, 2, &res );
McheckErr( res, "Deformer creation failed" );
MObject geomObj;
for ( MItSelectionList iter( list, MFn::kGeometric );
!iter.isDone(); iter.next() ) {
iter.getDependNode( geomObj );
defFn.addGeometry( geomObj );
}
defFn.resetLattice( true );
MFnDependencyNode depNodeFn;
MDGModifier modifier;
MString attrName;
MString nodeType("latticeNoise" );
MObject noiseNode = depNodeFn.create( nodeType, &res );
McheckErr( res, "Lattice noise node creation failed" );
attrName.set( "input" );
MObject inputAttr = depNodeFn.attribute( attrName );
attrName.set( "output" );
MObject outputAttr = depNodeFn.attribute( attrName );
attrName.set( "time" );
MObject timeAttr = depNodeFn.attribute( attrName );
attrName.set( "deformedLatticePoints" );
MObject destLattAttr = defFn.attribute( attrName );
MObject deformedLattice = defFn.deformLattice( &res );
McheckErr( res, "Could not get the deformed lattice node" );
MFnLattice latticeShapeFn( deformedLattice, &res );
attrName.set( "latticeOutput" );
MObject sourceLattAttr = latticeShapeFn.attribute( attrName );
res = modifier.disconnect( deformedLattice, sourceLattAttr,
deformNode, destLattAttr );
McheckErr( res, "Could not disconnect nodes" );
modifier.connect( deformedLattice, sourceLattAttr,
noiseNode, inputAttr );
modifier.connect( noiseNode, outputAttr,
deformNode, destLattAttr );
list.clear();
list.add( "time1" );
MObject timeNode;
list.getDependNode( 0, timeNode );
MFnDependencyNode timeFn( timeNode, &res );
McheckErr( res, "Could not get the time node" );
attrName.set( "outTime" );
MObject timeOutAttr = timeFn.attribute( attrName );
modifier.connect( timeNode, timeOutAttr,
noiseNode, timeAttr );
res = modifier.doIt();
McheckErr( res, "Error changing deformer graph connections" );
MString name = latticeShapeFn.name();
MGlobal::selectByName( name, MGlobal::kReplaceList );
return res;
}