-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathMDagModifier.cpp
168 lines (145 loc) · 6.83 KB
/
MDagModifier.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <pybind11/pybind11.h>
#include <pybind11/operators.h>
#include <maya/MDGModifier.h>
#include <maya/MDagModifier.h>
#include <maya/MObject.h>
#include <maya/MNodeClass.h>
#include <maya/MPlug.h>
#include <maya/MFnDagNode.h>
#include "util/obj.hpp"
#include "init.h"
namespace py = pybind11;
#define CHECK_STATUS(status) if (!status) { throw std::runtime_error(status.errorString().asChar());}
#define _doc_DagModifier_createNode \
"Adds an operation to the modifier to create a DAG node of the\n"\
"specified type.\n"\
"\n"\
"If a parent DAG node is provided the new node will be parented\n"\
"under it.\n"\
"If no parent is provided and the new DAG node is a transform type then\n"\
"it will be parented under the world.\n"\
"In both of these cases the method returns the new DAG node.\n"\
"\n"\
"If no parent is provided and the new DAG node is not a transform type\n"\
"then a transform node will be created and the child parented under that.\n"\
"The new transform will be parented under the world and it is\n"\
"the transform node which will be returned by the method, not the child.\n"\
"\n"\
"None of the newly created nodes will be added to the DAG until\n"\
"the modifier's doIt() method is called.\n"\
#define _doc_DagModifier_reparentNode \
"Adds an operation to the modifier to reparent a DAG node under\n"\
"a specified parent.\n"\
"\n"\
"If no parent is provided then the DAG node will be reparented under\n"\
"the world, so long as it is a transform type.\n"\
"If it is not a transform type then the doIt() will raise a RuntimeError."
template <>
void init_class(py::class_<MDagModifier, MDGModifier> &DagModifier) {
DagModifier
.def(py::init<>())
.def("createNode", [](MDagModifier & self, std::string type, MObject parent = MObject::kNullObj) -> MObject {
if (!parent.isNull())
{
validate::has_fn(
parent, MFn::kDagNode,
"Cannot createNode - 'parent' must be a 'kDagNode' object , not a '^1s' object."
);
}
MString type_name(type.c_str());
MStatus status;
MObject result = self.createNode(type_name, parent, &status);
if (status == MS::kInvalidParameter)
{
MString error_msg("Cannot create dependency node '^1s' - use DGModifier instead.");
error_msg.format(error_msg, type_name);
throw pybind11::type_error(error_msg.asChar());
} else if (result.isNull()) {
MString error_msg("Cannot create unknown node type '^1s'.");
error_msg.format(error_msg, type_name);
throw pybind11::type_error(error_msg.asChar());
}
CHECK_STATUS(status)
return result;
}, py::arg("type"),
py::arg_v("parent", MObject::kNullObj, "Object.kNullObj"),
_doc_DagModifier_createNode)
.def("createNode", [](MDagModifier & self, MTypeId typeId, MObject parent = MObject::kNullObj) -> MObject {
if (!parent.isNull())
{
validate::has_fn(
parent, MFn::kDagNode,
"Cannot createNode - 'parent' must be a 'kDagNode' object , not a '^1s' object."
);
}
MString type_id_str = MString() + typeId.id();
MStatus status;
MObject result = self.createNode(typeId, parent, &status);
if (status == MS::kInvalidParameter)
{
MString error_msg("Cannot create dependency node with type ID '^1s'' - use DGModifier instead.");
error_msg.format(error_msg, type_id_str);
throw pybind11::type_error(error_msg.asChar());
} else if (result.isNull()) {
MString error_msg("Cannot create unknown node with type ID '^1s'.");
error_msg.format(error_msg, type_id_str);
throw pybind11::type_error(error_msg.asChar());
}
CHECK_STATUS(status)
return result;
}, py::arg("typeId"),
py::arg_v("parent", MObject::kNullObj, "Object.kNullObj"),
_doc_DagModifier_createNode)
.def("reparentNode", [](MDagModifier & self, MObject node, MObject newParent = MObject::kNullObj) {
validate::is_not_null(node, "Cannot reparent a null object.");
if (!node.hasFn(MFn::kDagNode))
{
MString error_msg("Cannot parent '^1s' to '^2s' - must specify a 'kDagNode' object , not a '^3s' object.");
error_msg.format(
error_msg,
MFnDependencyNode(node).name(),
newParent.isNull() ? "the world" : MFnDependencyNode(newParent).name(),
node.apiTypeStr()
);
throw pybind11::type_error(error_msg.asChar());
}
if (!newParent.isNull())
{
if (!newParent.hasFn(MFn::kDagNode))
{
MString error_msg("Cannot parent '^1s' to '^2s' - must specify a 'kDagNode' object , not a '^3s' object.");
error_msg.format(
error_msg,
MFnDependencyNode(node).name(),
newParent.isNull() ? "the world" : MFnDependencyNode(newParent).name(),
newParent.apiTypeStr()
);
throw pybind11::type_error(error_msg.asChar());
}
MFnDagNode fn(newParent);
if (fn.isChildOf(node))
{
MString error_msg("Cannot parent '^1s' to one of its children, '^2s'.");
error_msg.format(
error_msg,
MFnDagNode(node).partialPathName(),
MFnDagNode(newParent).partialPathName()
);
throw std::invalid_argument(error_msg.asChar());
}
}
if (node == newParent)
{
MString error_msg("Cannot parent '^1s' to itself.");
error_msg.format(
error_msg,
MFnDagNode(node).partialPathName()
);
throw std::invalid_argument(error_msg.asChar());
}
MStatus status = self.reparentNode(node, newParent);
CHECK_STATUS(status)
}, py::arg("node"),
py::arg_v("newParent", MObject::kNullObj, "Object.kNullObj"),
_doc_DagModifier_reparentNode);
}