Skip to content

Commit

Permalink
Make DagModifier a subclass of DGModifier
Browse files Browse the repository at this point in the history
If you Google "pybind11 subclass" you will find a couple of threads
on how to solve the problem, none of which worked in this scenario.

The issue on the pybind github about subclasses (below) cites a way
to subclass an existing binding that feels as robust as creating
a class on the fly using type("MyClass", bases, attrs)
 -> pybind/pybind11#1193

Another issue recommends using py:base, which has been deprecated.
 -> pybind/pybind11#17

...also it didn't work.

It seems to me that because we are defining each class as an .inl rather
than a .hpp or .cpp file, they are not aware of each other. To remedy
this, I've included the MDGModifier binding in the MDagModifier file,
with an pre-processor to prevent duplicate entries. Otherwise, the base
class would be omitted in the main.cpp.

I think this solution will work well when we get to other classes with
many subclasses, like the whole MFn* tree.
  • Loading branch information
yantor3d committed Jun 7, 2021
1 parent 64f18b0 commit 90d1aac
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/MDGModifier.inl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#ifndef DGMODIFIER_INL
#define DGMODIFIER_INL
py::class_<MDGModifier>(m, "DGModifier")
.def(py::init<>())

Expand Down Expand Up @@ -518,4 +520,5 @@ This requirement is used when the plugin is checked to see if it is in use, or i
For compound attributes only the topmost parent attribute may be passed in and all of its children will be unlinked, recursively.
Thus it's not possible to unlink a child attribute from a plugin by itself.
Note that the link is broken immediately and is not affected by the modifier's doIt() or undoIt() methods.)pbdoc");
Note that the link is broken immediately and is not affected by the modifier's doIt() or undoIt() methods.)pbdoc");
#endif // DGMODIFIER_INL
4 changes: 3 additions & 1 deletion src/MDagModifier.inl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
py::class_<MDagModifier>(m, "DagModifier", "DGModifier")
#include "MDGModifier.inl"

py::class_<MDagModifier, MDGModifier>(m, "DagModifier")
.def(py::init<>())

.def("createNode", [](MDagModifier & self, std::string type, MObject parent = MObject::kNullObj) -> MObject {
Expand Down
3 changes: 0 additions & 3 deletions tests/test_MDagModifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
from . import assert_equals, as_obj, as_plug, new_scene

def test_createNode():
raise SkipTest("Cannot test DAGModifier.createNode - not sure how to subclass from DGModifier.")


node = as_obj(cmds.createNode('transform'))
type_id = cmdc.FnDependencyNode(node).typeId()

Expand Down

0 comments on commit 90d1aac

Please sign in to comment.