1

我正在尝试使用 C++ API 编写一个 Maya 插件,该插件将识别选择中的网格,(计算一些东西),删除它们,然后在场景中插入一个新的网格。

我通过与objExporter插件示例相同的方式收集选定的网格。我正在使用 aMDagModifier删除网格节点并尝试撤消这些更改。但undoIt它不起作用......

这是我的简化代码:

#include <maya/MArgList.h>
#include <maya/MObject.h>
#include <maya/MGlobal.h>
#include <maya/MPxCommand.h>
#include <maya/MDagModifier.h>

class LibiglBoolean : public MPxCommand
{
  private:
    MDagModifier dgModifier;
  public:
    LibiglBoolean() {};
    virtual MStatus doIt(const MArgList& argList);
    virtual MStatus redoIt();
    virtual MStatus undoIt();
    virtual bool isUndoable() const;
    static void* creator();
};

#include "LibiglBoolean.h"
#include <maya/MDagPath.h>
#include <maya/MFnDagNode.h>
#include <maya/MFnPlugin.h>
#include <maya/MItDag.h>
#include <maya/MItSelectionList.h>
#include <maya/MSelectionList.h>

void* LibiglBoolean::creator() { return new LibiglBoolean; }

MStatus LibiglBoolean::doIt(const MArgList& /*argList*/) 
{
  return redoIt();
}

MStatus LibiglBoolean::redoIt()
{
  MSelectionList slist;
  MGlobal::getActiveSelectionList(slist);
  MItSelectionList iter(slist);
  for(MItDag dagIterator(MItDag::kDepthFirst, MFn::kInvalid);
    !iter.isDone(); 
    iter.next())
  {
    MDagPath objectPath;
    iter.getDagPath( objectPath );
    dagIterator.reset(objectPath.node());

    // DAG iteration beginning at at selected node
    for ( ; !dagIterator.isDone(); dagIterator.next() )
    {
      MDagPath dagPath;
      dagIterator.getPath(dagPath);
      // skip over intermediate objects
      MFnDagNode dagNode(dagPath);
      if(dagNode.isIntermediateObject()) 
      {
        continue;
      }else if((dagPath.hasFn(MFn::kMesh)) && (dagPath.hasFn(MFn::kTransform)))
      {
        continue;
      }else if( dagPath.hasFn(MFn::kMesh))
      {
        dgModifier.deleteNode(dagPath.node());
      }
    }
  }
  dgModifier.doIt();
  return MS::kSuccess;
}

MStatus LibiglBoolean::undoIt()
{
  return dgModifier.undoIt();
}

bool LibiglBoolean::isUndoable() const
{
  return true;
}

MStatus initializePlugin(MObject obj)
{
  MFnPlugin plugin(obj, "Name", "1.0", "Any");
  return plugin.registerCommand("LibiglBoolean", LibiglBoolean::creator);
}

MStatus uninitializePlugin(MObject obj) 
{
  MFnPlugin plugin(obj);
  return plugin.deregisterCommand("LibiglBoolean");
}

(我想坚持使用 C++ API 并避免使用 MEL/Python 接口)。

如何deleteNode正确撤消操作?

4

0 回答 0