//- // ========================================================================== // Copyright (C) 1995 - 2006 Autodesk, Inc. and/or its licensors. All // rights reserved. // // The coded instructions, statements, computer programs, and/or related // material (collectively the "Data") in these files contain unpublished // information proprietary to Autodesk, Inc. ("Autodesk") and/or its // licensors, which is protected by U.S. and Canadian federal copyright // law and by international treaties. // // The Data is provided for use exclusively by You. You have the right // to use, modify, and incorporate this Data into other products for // purposes authorized by the Autodesk software license agreement, // without fee. // // The copyright notices in the Software and this entire statement, // including the above license grant, this restriction and the // following disclaimer, must be included in all copies of the // Software, in whole or in part, and all derivative works of // the Software, unless such copies or derivative works are solely // in the form of machine-executable object code generated by a // source language processor. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND. // AUTODESK DOES NOT MAKE AND HEREBY DISCLAIMS ANY EXPRESS OR IMPLIED // WARRANTIES INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF // NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR // PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE, OR // TRADE PRACTICE. IN NO EVENT WILL AUTODESK AND/OR ITS LICENSORS // BE LIABLE FOR ANY LOST REVENUES, DATA, OR PROFITS, OR SPECIAL, // DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES, EVEN IF AUTODESK // AND/OR ITS LICENSORS HAS BEEN ADVISED OF THE POSSIBILITY // OR PROBABILITY OF SUCH DAMAGES. // // ========================================================================== //+ // // Class: latticeNoiseCmd // // Description: // The latticeNoise command creates a new lattice (ffd) deformer. A // latticeNoise node is placed between the deformed lattice shape and the // actual deformer node. This causes the deformed object to wobble over time // as random continuous noise is applied to the pointes of the lattice. // // Once the deformer is created, the regular 'lattice' command can be // used on it to modify the standard lattice parameters. // // One thing to note is that the lattice geometry displayed on the // screen will not show the added noise. This is done this way // so that the lattice can be modified with the usual tools without // the noise node overriding all changes made to the lattice. // // Also note that the noise function is reasonably computationally // expensive, so dense lattices will be slow to update. // // eg // This causes the currently selected object to be deformed // // latticeNoise; // // This causes the specified geometry to be deformed // // latticeNoise sphereShape1; // #include #include "latticeNoise.h" #include #include #include #include #include #include #include #include #include #include #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++ ) { // Get the arg as a string. // res = args.get( i, argStr ); McheckErr( res, "Invalid argument type" ); // Attempt to find all of the objects matched // by the string and add them to the list // res = list.add( argStr ); McheckErr( res, "Invalid object" ); } } else { // Get the geometry list from what is currently selected in the // model // MGlobal::getActiveSelectionList( list ); } // Create the deformer // 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() ) { // Get the object and add it to the deformation. Note: we just // ignore objects that we don't understand. This is standard maya // command behavior // iter.getDependNode( geomObj ); defFn.addGeometry( geomObj ); } // Reset the lattice to bound it's geometry // defFn.resetLattice( true ); // Insert the latticeNoise node between the deformed lattice and the // actual lattice deformer node // MFnDependencyNode depNodeFn; MDGModifier modifier; MString attrName; // Make the noise node and get the attributes that we are going to // connect up // 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 ); // Get the lattice input attribute from the deformer node // attrName.set( "deformedLatticePoints" ); MObject destLattAttr = defFn.attribute( attrName ); // Get the lattice shape node so that we can find the output // attribute // 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 ); // Disconnect lattice from deformer // res = modifier.disconnect( deformedLattice, sourceLattAttr, deformNode, destLattAttr ); McheckErr( res, "Could not disconnect nodes" ); // Insert new noise node // modifier.connect( deformedLattice, sourceLattAttr, noiseNode, inputAttr ); modifier.connect( noiseNode, outputAttr, deformNode, destLattAttr ); // Find the time node and connect to it // 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 ); // Connect to time node // modifier.connect( timeNode, timeOutAttr, noiseNode, timeAttr ); // Perform all of the operations cued up in the modifier // res = modifier.doIt(); McheckErr( res, "Error changing deformer graph connections" ); // Lastly, make the new lattice that we have created active // This is standard Mel command behaviour. // MString name = latticeShapeFn.name(); MGlobal::selectByName( name, MGlobal::kReplaceList ); return res; }