You can use more than one instance of MDGModifier in your command. In your command's doIt, whenever a sub-command has a dependency on the name of a DG node created by an earlier sub-command, schedule the creation of the DG node on the first MDGModifier, call the first MDGModifier's doIt, then schedule the second sub-command on the second MDGModifier using the DG node's now known name, and call the second MDGModifier's doIt. Your command's redoIt simply calls doIt on both MDGModifiers in the same order, and your command's undoIt calls undoIt on both MDGModifiers, but in reverse order.
In the example that you give, your first sub-command is a polyCube, which creates multiple DG nodes, so scheduling the polyCube comamnd using MDGModifier::commandToExecute is the simplest way to achieve that in an undoable manner. But unlike MDGModifer::createNode or MDagModifier::createNode, it doesn't give you any MObject referring to the created node, and you need one to get the name. To get such a MObject, you need to add a callback using MDGMessage::addNodeAddedCallback before calling the MDGModfier's doIt, and remove it afterwards.
WARNING: NOT TESTED!
class MyCommand: public MPxCommand
{
...
MDGModifier m_modifier1;
MDGModifier m_modifier2;
static void NodeAdded( MObject& node, void* clientData )
{
if ( node.hasFn( MFn::kMesh ) )
{
*static_cast< MObject* >( clientData ) = node;
}
}
MStatus doIt( const MArgList& args )
{
MObject node;
MFnDependencyNode nodeFn;
MCallbackId nodeAddedId;
nodeAddedId = MDGMessage::addNodeAddedCallback( NodeAdded, &node );
m_modifier1.commandToExecute( "polyCube" );
m_modifier1.doIt();
MMessage::removeCallback( nodeAddedId );
nodeFn.setObject( node );
m_modifier2.commandToExecute( "polyTriangulate " + nodeFn.name() );
m_modifier2.doIt();
return MStatus::kSuccess;
}
MStatus redoIt()
{
m_modifier1.doIt();
m_modifier2.doIt();
return MStatus::kSuccess;
}
MStatus undoIt()
{
m_modifier2.undoIt();
m_modifier1.undoIt();
return MStatus::kSuccess;
}
...
};