Maya API How-To #11 | ||
| ||
How do I use MDGModifier so I can get reliable Undo/Redo?I've had several logistical problems with MDGModifiers, exclusively with its ::undo() and ::redo() mechanism. After much trial and error on the topic I've come to the following conclusions: There are times when you may have to perform a ::doIt() in the middle of your MDGModifier modification so the scene state is valid for further operations. For example, if you use MDGModifier::create(), you will need to ::doIt() so this node exists if you wish to perform any edits using a class other than MDGModifier (finding an MPlug on your new node, for instance). Do not continue to use a MDGModifier object after you have called its ::doIt() method! My "Golden Rule" for MDGModifier is now the following: "Execute ::doIt() as soon as I need to update the state of the scene, then immediately allocate another MDGModifier for further edits. Continue, rinse, repeat." All allocated MDGModifier nodes go onto a stack. The stack in undone in FILO order, and redone in FIFO order.
class MyCommand : public MPxNode
{
private:
typedef std::vector<MDGModifier*>::const_iterator redoIter;
typedef std::vector<MDGModifier*>::const_reverse_iterator undoIter;
std::vector<MDGModifier*> mUndo;
};
MStatus MyCommand::doIt()
{
MStatus status = MS::kSuccess;
MDGModifier* modifier = NULL;
// Allocate a new modifier and add to undo
modifier = new MDGModifier;
mUndo.push_back( modifier );
// populate modifier here...
status = modifier.doIt();
modifier = NULL; // now don't touch!
// Allocate another new modifier and add to undo
modifier = new MDGModifier;
mUndo.push_back( modifier );
// populate modifier here...
status = modifier.doIt();
modifier = NULL; // now don't touch!
return status;
}
MStatus MyCommand::redoIt()
{
MStatus status = MS::kSuccess;
redoIter d;
for ( d = mUndo.begin(); ( status == MS::kSuccess ) && ( d != mUndo.end() ); d++ )
{
status = (*d)->doIt();
}
return status;
}
MStatus MyCommand::undoIt()
{
MStatus status = MS::kSuccess;
undoIter u;
for ( u = mUndo.rbegin(); ( status == MS::kSuccess ) && ( u != mUndo.rend() ); u++ )
{
status = (*u)->undoIt();
}
return status;
}
Related How-To's02 February 2003 | ||
| Copyright ©2005 by Bryan Ewert, maya@ewertb.com Maya is a Registered Trademark of Alias |