Skip to content

Commit

Permalink
Implement MDGModifier::addAttribute binding
Browse files Browse the repository at this point in the history
  • Loading branch information
yantor3d committed May 31, 2021
1 parent 67fce72 commit d13fac8
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 7 deletions.
22 changes: 21 additions & 1 deletion src/MDGModifier.inl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,27 @@ py::class_<MDGModifier>(m, "DGModifier")
.def(py::init<>())

.def("addAttribute", [](MDGModifier & self, MObject node, MObject attribute) {
throw std::logic_error{"Function not yet implemented."};
if (node.isNull())
{
throw std::invalid_argument("Cannot add attribute to a null object.");
} else if (!node.hasFn(MFn::kDependencyNode)) {
MString error_msg("Cannot add attribute - must specify a 'node' object, not a '^1s' object.");
error_msg.format(error_msg, node.apiTypeStr());
throw pybind11::type_error(error_msg.asChar());
}

if (attribute.isNull())
{
throw std::invalid_argument("Cannot add null attribute to an object.");
} else if (!node.hasFn(MFn::kAttribute)) {
MString error_msg("Cannot add attribute - must specify an 'attribute' object, not a(n) '^1s' object.");
error_msg.format(error_msg, attribute.apiTypeStr());
throw pybind11::type_error(error_msg.asChar());
}

MStatus status = self.addAttribute(node, attribute);

CHECK_STATUS(status)
},
R"pbdoc(Adds an operation to the modifier to add a new dynamic attribute to the given dependency node.
If the attribute is a compound its children will ae added as well, so only the parent needs to be added using this method.)pbdoc")
Expand Down
17 changes: 17 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,28 @@
import maya.standalone
from maya import cmds

import cmdc


def setup():
maya.standalone.initialize()


def new_scene():
cmds.file(new=True, force=True)


def teardown():
maya.standalone.uninitialize()


def as_obj(arg):
"""Return the Maya Object for the given node."""

return cmdc.SelectionList().add(arg).getDependNode(0)


def as_plug(arg):
"""Return the Maya Plug for the given plug."""

return cmdc.SelectionList().add(arg).getPlug(0)
36 changes: 30 additions & 6 deletions tests/test_MDGModifier.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,42 @@
import cmdc
import nose

from nose.plugins.skip import SkipTest

from maya import cmds
from maya.api import OpenMaya

from . import new_scene
from . import as_obj, as_plug, new_scene


def _node_type_id():
node = cmds.createNode('network')
node = OpenMaya.MSelectionList().add(node).getDependNode(0)
type_id = OpenMaya.MFnDependencyNode(node).typeId.id()
def test_addAttribute_pass():
raise SkipTest("Cannot test DGModifier.addAttribute until MFnAttribute classes are implemented.")


def test_addAttribute_fail():
node = as_obj(cmds.createNode('network'))
null = cmdc.Object()

attr = as_plug('persp.message').attribute()

for exc, doc, node, attr in (
[ValueError, 'a null object', null, null],
[ValueError, 'a null attribute', node, null],
[TypeError, 'a non-node object', attr, null],
[TypeError, 'a non-attribute object', node, node],
):
test_addAttribute_fail.__doc__ = """Test MDGModifier::addAttribute errors if passed {}.""".format(doc)

yield _addAttribute_fail, exc, node, attr


def _addAttribute_fail(exception, node, attr):
nose.tools.assert_raises(
exception,
cmdc.DGModifier().addAttribute,
node, attr
)

return cmdc.TypeId(type_id)

@nose.with_setup(teardown=new_scene)
def test_createNode_pass():
Expand Down

0 comments on commit d13fac8

Please sign in to comment.