All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
undoHelperCommand.h
Go to the documentation of this file.
1 //
2 // Copyright 2018 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXRUSDMAYA_UNDO_HELPER_CMD_H
25 #define PXRUSDMAYA_UNDO_HELPER_CMD_H
26 
28 
29 #include "pxr/pxr.h"
30 #include "pxr/base/tf/token.h"
31 #include "pxr/base/vt/value.h"
32 
33 #include "usdMaya/api.h"
34 #include "usdMaya/adaptor.h"
35 
36 #include <maya/MDGModifier.h>
37 #include <maya/MGlobal.h>
38 #include <maya/MPxCommand.h>
39 
40 PXR_NAMESPACE_OPEN_SCOPE
41 
47 class UsdMayaUndoHelperCommand : public MPxCommand
48 {
49 public:
50  template <typename T>
51  using UndoableResultFunction = std::function<T (MDGModifier&)>;
52  using UndoableFunction = UndoableResultFunction<void>;
53 
54  PXRUSDMAYA_API
56  PXRUSDMAYA_API
57  ~UsdMayaUndoHelperCommand() override;
58 
59  PXRUSDMAYA_API
60  MStatus doIt(const MArgList& args) override;
61  PXRUSDMAYA_API
62  MStatus redoIt() override;
63  PXRUSDMAYA_API
64  MStatus undoIt() override;
65  PXRUSDMAYA_API
66  bool isUndoable() const override;
67 
68  PXRUSDMAYA_API
69  static MSyntax createSyntax();
70  PXRUSDMAYA_API
71  static void* creator();
72 
78  PXRUSDMAYA_API
79  static void ExecuteWithUndo(const UndoableFunction& func);
80 
83  template <typename T>
84  static T ExecuteWithUndo(const UndoableResultFunction<T>& func)
85  {
86  T result;
87  ExecuteWithUndo([&result, &func](MDGModifier& modifier) {
88  result = func(modifier);
89  });
90  return result;
91  }
92 
93 private:
94  MDGModifier _modifier;
95  bool _undoable;
96 
97  static const UndoableFunction* _dgModifierFunc;
98 };
99 
100 PXR_NAMESPACE_CLOSE_SCOPE
101 
102 #endif
This is an internal helper command to provide undo support for operations performed through the OpenM...
static PXRUSDMAYA_API void ExecuteWithUndo(const UndoableFunction &func)
Calls func with an MDGModifier, saving the modifier for future undo and redo operations.
static T ExecuteWithUndo(const UndoableResultFunction< T > &func)
This overload of ExecuteWithUndo() supports a func that returns a value of type T.