-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSeamsEasyCmd.cpp
400 lines (331 loc) · 12.6 KB
/
SeamsEasyCmd.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#include "SeamsEasyCmd.h"
#include "StitchEasyNode.h"
#include <maya/MSyntax.h>
#include <maya/MSelectionList.h>
#include <maya/MItSelectionList.h>
#include <maya/MDagPath.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MFnDagNode.h>
#include <maya/MGlobal.h>
#include <maya/MFnComponentListData.h>
#include <maya/MFnSet.h>
#include <maya/MFnTransform.h>
#include <maya/MMatrix.h>
#include <maya/MItDependencyNodes.h>
#include <maya/MArgList.h>
#include <maya/MAngle.h>
SeamsEasyCmd::SeamsEasyCmd()
{
}
SeamsEasyCmd::~SeamsEasyCmd()
{
}
void *SeamsEasyCmd::creator()
{
return new SeamsEasyCmd;
}
MSyntax SeamsEasyCmd::newSyntax()
{
MSyntax syntax;
syntax.addFlag(THICKNESS_FLAG, THICKNESS_FLAG_LONG, MSyntax::kDistance);
syntax.addFlag(DIVISIONS_FLAG, DIVISIONS_FLAG_LONG, MSyntax::kLong);
syntax.addFlag(EXTRUDEALL_FLAG, EXTRUDEALL_FLAG_LONG, MSyntax::kBoolean);
syntax.addFlag(GAP_FLAG, GAP_FLAG_LONG, MSyntax::kDistance);
syntax.addFlag(MODE_FLAG, MODE_FLAG_LONG, MSyntax::kLong);
syntax.addFlag(WIDTH_FLAG, WIDTH_FLAG_LONG, MSyntax::kDistance);
syntax.addFlag(DEPTH_FLAG, DEPTH_FLAG_LONG, MSyntax::kDistance);
syntax.addFlag(SUBDIVS_FLAG, SUBDIVS_FLAG_LONG, MSyntax::kLong);
syntax.addFlag(ADDLOOP_FLAG, ADDLOOP_FLAG_LONG, MSyntax::kDistance, MSyntax::kDistance, MSyntax::kBoolean);
syntax.makeFlagMultiUse(ADDLOOP_FLAG);
syntax.addFlag(SORT_FLAG, SORT_FLAG_LONG, MSyntax::kString, MSyntax::kString);
syntax.addFlag(DISTANCEMULTI_FLAG, DISTANCEMULTI_FLAG_LONG, MSyntax::kDouble);
syntax.addFlag(DEPTHMULTI_FLAG, DEPTHMULTI_FLAG_LONG, MSyntax::kDouble);
syntax.addFlag(HARDEDGEANGLE_FLAG, HARDEDGEANGLE_FLAG_LONG, MSyntax::kAngle);
syntax.addArg(MSyntax::kString);
syntax.useSelectionAsDefault(true);
syntax.setObjectType(MSyntax::kSelectionList, 1);
syntax.enableEdit(true);
syntax.enableQuery(true);
return syntax;
}
MStatus SeamsEasyCmd::doIt(const MArgList& args)
{
MStatus status;
MArgDatabase argData(syntax(), args, &status);
CHECK_MSTATUS_AND_RETURN_IT(status);
bool isEdit = argData.isEdit();
bool isQuery = argData.isQuery();
m_isCreation = (isQuery || isEdit) ? false : true;
MString name = argData.commandArgumentString(0);
// Creation mode //////////////////////////////////////////////////////////////////////////////
if (m_isCreation) {
// Get input mesh node and component
MSelectionList selection;
status = argData.getObjects(selection);
if (status != MS::kSuccess) {
displayError("Nothing selected");
setResult(false);
return status;
}
MObject component;
selection.getDagPath(0, m_path, component);
if (component.apiType() != MFn::kMeshEdgeComponent) {
displayError("Invalid selection. Select mesh edges and retry");
setResult(false);
return status;
}
MFnDagNode
fnMesh(m_path);
MPlug
pOutputMesh;
MPlugArray incomingConnections;
bool connected = fnMesh.findPlug("inMesh", false).connectedTo(incomingConnections, true, false);
if(connected)
m_dgMod.disconnect(incomingConnections[0], fnMesh.findPlug("inMesh", false));
MIntArray tweakOutPoints;
int numTweaks = fnMesh.findPlug("pnts", false).getExistingArrayAttributeIndices(tweakOutPoints);
MObject tweakNode;
if (0<numTweaks) {
tweakNode = m_dgMod.createNode("polyTweak", &status);
CHECK_MSTATUS_AND_RETURN_IT(status);
MFnDependencyNode fnTweak(tweakNode, &status);
CHECK_MSTATUS_AND_RETURN_IT(status);
for (int t = 0; t < numTweaks; t++) {
MPlug
pTweakOutPoint = fnMesh.findPlug("pnts", false).elementByLogicalIndex(tweakOutPoints[t]),
pTweakInPoint = fnTweak.findPlug("tweak", false).elementByLogicalIndex(tweakOutPoints[t]);
for (unsigned int c = 0; c < pTweakOutPoint.numChildren(); c++) {
status = m_dgMod.newPlugValueFloat(pTweakInPoint.child(c), pTweakOutPoint.child(c).asFloat());
CHECK_MSTATUS_AND_RETURN_IT(status);
}
status = m_dgMod.removeMultiInstance(pTweakOutPoint, true);
CHECK_MSTATUS_AND_RETURN_IT(status);
}
pOutputMesh = fnTweak.findPlug("output", false);
if(connected)
m_dgMod.connect(incomingConnections[0], fnTweak.findPlug("inputPolymesh", false));
}
else if(connected)
pOutputMesh = incomingConnections[0];
m_node = m_dgMod.createNode(SeamsEasyNode::id, &status);
status = m_dgMod.doIt();
if (!connected) {
MFnMesh fnIntermediate;
m_intermediate = fnIntermediate.copy(m_path.node(), m_path.transform());
fnIntermediate.setIntermediateObject(true);
if (!tweakNode.isNull()) {
MFnDependencyNode fnTweak(tweakNode);
m_dagMod.connect(fnIntermediate.findPlug("outMesh", false), fnTweak.findPlug("inputPolymesh", false));
}
else
pOutputMesh = fnIntermediate.findPlug("outMesh", false);
}
// Create plugin node and make connections ////////////////////////////////////////////////
MFnDependencyNode
fnNode(m_node);
fnNode.setName((name == "") ? "seamsEasy" : name);
status = m_dagMod.connect(pOutputMesh, fnNode.findPlug(SeamsEasyNode::aInMesh, false));
CHECK_MSTATUS_AND_RETURN_IT(status);
status = m_dagMod.connect(fnNode.findPlug(SeamsEasyNode::aOutMesh, false), fnMesh.findPlug("inMesh", false));
CHECK_MSTATUS_AND_RETURN_IT(status);
MFnComponentListData compData;
MObject compList = compData.create();
compData.add(component);
status = m_dagMod.newPlugValue(fnNode.findPlug(SeamsEasyNode::aSelectedEdges, false), compList);
CHECK_MSTATUS_AND_RETURN_IT(status);
}
if (isQuery || isEdit) {
status = SNode::getPluginNode(name, SeamsEasyNode::id, m_node);
if (status != MS::kSuccess) {
displayError("invalid object");
setResult(false);
return status;
}
}
MFnDependencyNode fnNode(m_node, &status);
if (isQuery) {
for (auto &flag : m_attrFlags)
if (argData.isFlagSet(flag.first)) {
MPlug fs = fnNode.findPlug(flag.second, false);
queryAttrValue(fs);
break;
}
}
else if (isEdit || m_isCreation) {
for (auto &flag : m_attrFlags){
if (argData.isFlagSet(flag.first)){
MPlug fs = fnNode.findPlug(flag.second, false);
setFlagAttr(argData, flag.first, fs);
}
}
if (argData.isFlagSet(ADDLOOP_FLAG)) {
MPlug offset = fnNode.findPlug(SeamsEasyNode::aOffset, false);
MIntArray indices;
int lowestAvId = (offset.getExistingArrayAttributeIndices(indices) == 0) ? 0 : indices[indices.length() - 1] + 1;
for (unsigned int i = 0; i < argData.numberOfFlagUses(ADDLOOP_FLAG); i++) {
MArgList argList;
argData.getFlagArgumentList(ADDLOOP_FLAG, i, argList);
MPlug pDistance = offset.elementByLogicalIndex(lowestAvId).child(SeamsEasyNode::aOffsetDistance);
m_dgMod.newPlugValueMDistance(pDistance, argList.asDistance(0));
MPlug pDepth = offset.elementByLogicalIndex(lowestAvId).child(SeamsEasyNode::aOffsetDepth);
m_dgMod.newPlugValueMDistance(pDepth, argList.asDistance(1));
MPlug pStitch = offset.elementByLogicalIndex(lowestAvId).child(SeamsEasyNode::aOffsetStitch);
m_dgMod.newPlugValueBool(pDepth, argList.asBool(2));
lowestAvId++;
}
}
if (argData.isFlagSet(SORT_FLAG)) {
MString value = argData.flagArgumentString(SORT_FLAG, 0);
MString order = argData.flagArgumentString(SORT_FLAG, 1);
if (value == "distance")
OffsetParams::compare = OffsetParams::kDistance;
else if (value == "depth")
OffsetParams::compare = OffsetParams::kDepth;
else {
displayError("invalid value");
setResult(false);
return status;
}
std::set <OffsetParams> offsetParams;
MPlug pOffset = fnNode.findPlug(SeamsEasyNode::aOffset, false);
MIntArray offsetIndices;
for (unsigned int i = 0; i < pOffset.getExistingArrayAttributeIndices(offsetIndices); i++) {
MPlug pOffsetElement = pOffset.elementByLogicalIndex(offsetIndices[i]);
OffsetParams offsetParam(
pOffsetElement.child(SeamsEasyNode::aOffsetDistance).asFloat(),
pOffsetElement.child(SeamsEasyNode::aOffsetDepth).asFloat(),
pOffsetElement.child(SeamsEasyNode::aOffsetStitch).asBool(),
offsetIndices[i]
);
offsetParams.insert(offsetParam);
}
if (order == "asc" || order == "desc") {
auto param = offsetParams.begin();
auto rparam = offsetParams.rbegin();
for (unsigned int i = 0; i < offsetIndices.length(); i++) {
MPlug pOffsetElement = pOffset.elementByLogicalIndex(offsetIndices[i]);
m_dagMod.newPlugValueFloat(pOffsetElement.child(SeamsEasyNode::aOffsetDistance), (order == "asc") ? param->distance : rparam->distance);
m_dagMod.newPlugValueFloat(pOffsetElement.child(SeamsEasyNode::aOffsetDepth), (order == "asc") ? param->depth : rparam->depth);
m_dagMod.newPlugValueBool(pOffsetElement.child(SeamsEasyNode::aOffsetStitch), (order == "asc") ? param->stitch : rparam->stitch);
param++; rparam++;
}
}
else {
displayError("invalid value");
setResult(false);
return status;
}
}
}
m_isDoIt = true;
return redoIt();
}
MStatus SeamsEasyCmd::redoIt()
{
MStatus status;
if (m_isDoIt && !m_intermediate.isNull()) {
MFnDagNode
fnShape(m_path),
fnIntermediate(m_intermediate);
MGlobal::executeCommand("if(`isConnected \"" + fnIntermediate.name() + ".outMesh\" \"" + fnShape.name() + ".inMesh\"`) disconnectAttr \"" + fnIntermediate.name() + ".outMesh\" \"" + fnShape.name() + ".inMesh\";", false, false);
}
status = m_dgMod.doIt();
CHECK_MSTATUS_AND_RETURN_IT(status);
status = m_dagMod.doIt();
CHECK_MSTATUS_AND_RETURN_IT(status);
if (m_isCreation) {
MFnDependencyNode fnNode(m_node);
setResult(fnNode.name());
MGlobal::select(m_node, MGlobal::kReplaceList);
}
return MS::kSuccess;
}
MStatus SeamsEasyCmd::undoIt()
{
MStatus status;
status = m_dagMod.undoIt();
CHECK_MSTATUS_AND_RETURN_IT(status);
status = m_dgMod.undoIt();
CHECK_MSTATUS_AND_RETURN_IT(status);
if (!m_intermediate.isNull()) {
MFnDagNode
fnShape(m_path),
fnIntermediate(m_intermediate);
MGlobal::executeCommand("connectAttr \"" + fnIntermediate.name() + ".outMesh\" \"" + fnShape.name() + ".inMesh\";", false, false);
}
return MS::kSuccess;
}
MStatus SeamsEasyCmd::queryAttrValue(MPlug& attrPlug) {
MStatus status;
MDataHandle hAttr = attrPlug.asMDataHandle();
MFnNumericData::Type type = hAttr.numericType();
if (attrPlug == SeamsEasyNode::aProfileMode)
appendToResult(attrPlug.asShort());
else if (attrPlug == SeamsEasyNode::aHardEdgeAngle)
appendToResult(attrPlug.asMAngle().as(MAngle::uiUnit()));
else {
switch (type) {
case MFnNumericData::kBoolean:
appendToResult(attrPlug.asBool());
break;
case MFnNumericData::kInt:
appendToResult(attrPlug.asInt());
break;
case MFnNumericData::kShort:
appendToResult(attrPlug.asShort());
break;
case MFnNumericData::kFloat:
appendToResult(attrPlug.asFloat());
break;
case MFnNumericData::kDouble:
appendToResult(attrPlug.asDouble());
break;
default:
appendToResult(attrPlug.asMDistance().as(MDistance::uiUnit()));
break;
}
}
return MS::kSuccess;
}
MStatus SeamsEasyCmd::setFlagAttr(MArgDatabase& argData, char *flag, MPlug& attrPlug) {
MStatus status;
MDataHandle hAttr = attrPlug.asMDataHandle();
MFnNumericData::Type type = hAttr.numericType();
if (attrPlug == SeamsEasyNode::aProfileMode) {
int value = argData.flagArgumentBool(flag, 0, &status);
status = m_dagMod.newPlugValueInt(attrPlug, value);
}
else if (attrPlug == SeamsEasyNode::aHardEdgeAngle) {
MAngle value = argData.flagArgumentMAngle(flag, 0, &status);
status = m_dagMod.newPlugValueMAngle(attrPlug, value);
}
else {
switch (type) {
case MFnNumericData::kBoolean: {
bool value = argData.flagArgumentBool(flag, 0, &status);
status = m_dagMod.newPlugValueBool(attrPlug, value);
break; }
case MFnNumericData::kInt: {
int value = argData.flagArgumentInt(flag, 0, &status);
status = m_dagMod.newPlugValueInt(attrPlug, value);
break; }
case MFnNumericData::kShort: {
short value = (short)argData.flagArgumentInt(flag, 0, &status);
status = m_dagMod.newPlugValueShort(attrPlug, value);
break; }
case MFnNumericData::kFloat: {
float value = (float)argData.flagArgumentDouble(flag, 0, &status);
status = m_dagMod.newPlugValueFloat(attrPlug, value);
break; }
case MFnNumericData::kDouble: {
double value = argData.flagArgumentDouble(flag, 0, &status);
status = m_dagMod.newPlugValueDouble(attrPlug, value);
break; }
default: {
MDistance value = argData.flagArgumentMDistance(flag, 0, &status);
status = m_dagMod.newPlugValueMDistance(attrPlug, value);
break; }
}
}
return MS::kSuccess;
}