forked from sjirka/SeamsEasy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSeamsEasyNode.cpp
522 lines (418 loc) · 16.9 KB
/
SeamsEasyNode.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#include "SeamsEasyNode.h"
#include "StitchEasyNode.h"
#include "SNode.h"
#include "SPlane.h"
#include <maya\MFnNumericAttribute.h>
#include <maya\MFnTypedAttribute.h>
#include <maya\MFnCompoundAttribute.h>
#include <maya\MFnComponentListData.h>
#include <maya\MFnMesh.h>
#include <maya\MFnMeshData.h>
#include <maya\MFnSingleIndexedComponent.h>
#include <maya\MGlobal.h>
#include <maya\MPointArray.h>
#include <maya\MFloatPointArray.h>
#include <maya\MPlugArray.h>
#include <maya\MFnSet.h>
#include <maya\MFnEnumAttribute.h>
#include <maya\MFnUnitAttribute.h>
#include <maya\MRampAttribute.h>
#include <maya\MDistance.h>
#include <maya\MDGModifier.h>
#include <maya\MDagModifier.h>
#include <maya\MItDependencyGraph.h>
#include <maya\MAngle.h>
MTypeId SeamsEasyNode::id(0x00127891);
MObject SeamsEasyNode::aOutMesh;
MObject SeamsEasyNode::aOutStitchLines;
MObject SeamsEasyNode::aInMesh;
MObject SeamsEasyNode::aSelectedEdges;
MObject SeamsEasyNode::aExtrudeAllBoundaries;
MObject SeamsEasyNode::aExtrudeThickness;
MObject SeamsEasyNode::aExtrudeDivisions;
MObject SeamsEasyNode::aGap;
MObject SeamsEasyNode::aProfileMode;
MObject SeamsEasyNode::aOffset;
MObject SeamsEasyNode::aOffsetDistance;
MObject SeamsEasyNode::aOffsetDepth;
MObject SeamsEasyNode::aOffsetStitch;
MObject SeamsEasyNode::aDistanceMultiplier;
MObject SeamsEasyNode::aDepthMultiplier;
MObject SeamsEasyNode::aProfileWidth;
MObject SeamsEasyNode::aProfileDepth;
MObject SeamsEasyNode::aProfileSubdivs;
MObject SeamsEasyNode::aProfileCurve;
MObject SeamsEasyNode::aHardEdgeAngle;
OffsetParams::Compare OffsetParams::compare = OffsetParams::kDistance;
SeamsEasyNode::SeamsEasyNode(){
}
void SeamsEasyNode::postConstructor(){
callbackIds.append(MNodeMessage::addAttributeChangedCallback(thisMObject(), attrChanged, NULL));
// Set default profile curve shape
MRampAttribute rAttr(thisMObject(), aProfileCurve);
rAttr.setValueAtIndex(1, 0);
MFloatArray positions, values;
MIntArray interpolation;
positions.append(0.5f);
values.append(0.85f);
interpolation.append(MRampAttribute::kSpline);
positions.append(0.85f);
values.append(0.5f);
interpolation.append(MRampAttribute::kSpline);
positions.append(1.0f);
values.append(0.0f);
interpolation.append(MRampAttribute::kSpline);
rAttr.addEntries(positions, values, interpolation);
}
SeamsEasyNode::~SeamsEasyNode(){
MMessage::removeCallbacks(callbackIds);
}
void *SeamsEasyNode::creator()
{
return new SeamsEasyNode;
}
MStatus SeamsEasyNode::initialize()
{
MStatus status;
MFnNumericAttribute nAttr;
MFnTypedAttribute tAttr;
MFnCompoundAttribute cAttr;
MFnEnumAttribute eAttr;
MFnUnitAttribute uAttr;
MRampAttribute rAttr;
aOutMesh = tAttr.create("outMesh", "outMesh", MFnData::kMesh, &status);
tAttr.setWritable(false);
addAttribute(aOutMesh);
aOutStitchLines = tAttr.create("stitchLines", "stitchLines", MFnData::kComponentList, &status);
tAttr.setWritable(false);
addAttribute(aOutStitchLines);
aInMesh = tAttr.create("inMesh", "inMesh", MFnData::kMesh, &status);
addAttribute(aInMesh);
attributeAffects(aInMesh, aOutMesh);
attributeAffects(aInMesh, aOutStitchLines);
aSelectedEdges = tAttr.create("seamLines", "seamLines", MFnData::kComponentList, &status);
addAttribute(aSelectedEdges);
attributeAffects(aSelectedEdges, aOutMesh);
attributeAffects(aSelectedEdges, aOutStitchLines);
// Extrude attributes /////////////////////////////////////////////////////////////////////////
aExtrudeAllBoundaries = nAttr.create("extrudeAllBoundaries", "extrudeAllBoundaries", MFnNumericData::kBoolean, 0, &status);
addAttribute(aExtrudeAllBoundaries);
attributeAffects(aExtrudeAllBoundaries, aOutMesh);
aExtrudeThickness = uAttr.create("thickness", "thickness", MFnUnitAttribute::kDistance, 1.0, &status);
uAttr.setSoftMin(0);
uAttr.setSoftMax(10);
addAttribute(aExtrudeThickness);
attributeAffects(aExtrudeThickness, aOutMesh);
aExtrudeDivisions = nAttr.create("divisions", "divisions", MFnNumericData::kInt, 0, &status);
nAttr.setMin(0);
nAttr.setSoftMax(10);
addAttribute(aExtrudeDivisions);
attributeAffects(aExtrudeDivisions, aOutMesh);
aGap = uAttr.create("gap", "gap", MFnUnitAttribute::kDistance, 0.0, &status);
uAttr.setMin(0);
uAttr.setSoftMax(10);
addAttribute(aGap);
attributeAffects(aGap, aOutMesh);
// Profile attributes //////////////////////////////////////////////////////////////////////////
aProfileMode = eAttr.create("profileMode", "profileMode");
eAttr.addField("Manual", 0);
eAttr.addField("Curve", 1);
addAttribute(aProfileMode);
attributeAffects(aProfileMode, aOutMesh);
attributeAffects(aProfileMode, aOutStitchLines);
aProfileWidth = uAttr.create("profileWidth", "profileWidth", MFnUnitAttribute::kDistance, 1.0);
uAttr.setMin(0);
uAttr.setSoftMax(1);
addAttribute(aProfileWidth);
attributeAffects(aProfileWidth, aOutMesh);
aProfileDepth = uAttr.create("profileDepth", "profileDepth", MFnUnitAttribute::kDistance, -1.0);
uAttr.setSoftMin(-1);
uAttr.setSoftMax(1);
addAttribute(aProfileDepth);
attributeAffects(aProfileDepth, aOutMesh);
aProfileSubdivs = nAttr.create("profileSubdivisions", "profileSubdivs", MFnNumericData::kInt, 4);
nAttr.setMin(0);
nAttr.setSoftMax(10);
addAttribute(aProfileSubdivs);
attributeAffects(aProfileSubdivs, aOutMesh);
aProfileCurve = rAttr.createCurveRamp("profileCurve", "profileCurve");
addAttribute(aProfileCurve);
attributeAffects(aProfileCurve, aOutMesh);
// Offset attributes //////////////////////////////////////////////////////////////////////////
aOffset = cAttr.create("offset", "offset", &status);
cAttr.setArray(true);
aOffsetDistance = uAttr.create("distance", "distance", MFnUnitAttribute::kDistance, 0.0, &status);
uAttr.setMin(0);
uAttr.setSoftMax(10);
addAttribute(aOffsetDistance);
attributeAffects(aOffsetDistance, aOutMesh);
attributeAffects(aOffsetDistance, aOutStitchLines);
cAttr.addChild(aOffsetDistance);
aOffsetDepth = uAttr.create("depth", "depth", MFnUnitAttribute::kDistance, 0.0, &status);
uAttr.setSoftMin(-1);
uAttr.setSoftMax(1);
addAttribute(aOffsetDepth);
attributeAffects(aOffsetDepth, aOutMesh);
cAttr.addChild(aOffsetDepth);
aOffsetStitch = nAttr.create("stitch", "stitch", MFnNumericData::kBoolean, 0, &status);
addAttribute(aOffsetStitch);
attributeAffects(aOffsetStitch, aOutMesh);
attributeAffects(aOffsetStitch, aOutStitchLines);
cAttr.addChild(aOffsetStitch);
addAttribute(aOffset);
attributeAffects(aOffset, aOutMesh);
attributeAffects(aOffset, aOutStitchLines);
aDistanceMultiplier = nAttr.create("distanceMultiplier", "distanceMultiplier", MFnNumericData::kFloat, 1.0, &status);
nAttr.setMin(0);
nAttr.setSoftMax(10);
addAttribute(aDistanceMultiplier);
attributeAffects(aDistanceMultiplier, aOutMesh);
attributeAffects(aDistanceMultiplier, aOutStitchLines);
aDepthMultiplier = nAttr.create("depthMultiplier", "depthMultiplier", MFnNumericData::kFloat, 1.0, &status);
nAttr.setMin(0);
nAttr.setSoftMax(10);
addAttribute(aDepthMultiplier);
attributeAffects(aDepthMultiplier, aOutMesh);
attributeAffects(aDepthMultiplier, aOutStitchLines);
aHardEdgeAngle = uAttr.create("hardEdgeAngle", "hardEdgeAngle", MFnUnitAttribute::kAngle, M_PI/2, &status);
nAttr.setMin(0);
nAttr.setMax(180);
addAttribute(aHardEdgeAngle);
attributeAffects(aHardEdgeAngle, aOutMesh);
return MS::kSuccess;
}
MStatus SeamsEasyNode::setDependentsDirty(const MPlug &plug, MPlugArray &affected) {
if (aInMesh == plug)
dirtyMesh = true;
if (aSelectedEdges == plug)
dirtyComponent = true;
if (aExtrudeAllBoundaries == plug ||
aExtrudeThickness == plug ||
aExtrudeDivisions == plug)
dirtyExtrude = true;
if (aGap == plug ||
aProfileMode == plug ||
aProfileWidth == plug ||
aProfileDepth == plug ||
aProfileSubdivs == plug ||
aProfileCurve == plug.parent().array() ||
aOffset == plug ||
aOffsetDepth == plug ||
aOffsetDistance == plug ||
aOffsetStitch == plug ||
aDepthMultiplier == plug||
aDistanceMultiplier == plug)
dirtyProfile = true;
return MS::kSuccess;
}
MStatus SeamsEasyNode::compute(const MPlug &plug, MDataBlock &dataBlock) {
MStatus status;
if (plug != aOutMesh)
return MS::kUnknownParameter;
// Load attribs ///////////////////////////////////////////////////////////////////////////////
if (dirtyMesh || m_sourceMesh.isNull()) {
MObject sourceMesh = dataBlock.inputValue(aInMesh).asMesh();
if (m_sourceMesh.isNull() || !SMesh::isEquivalent(sourceMesh, m_sourceMesh))
dirtyBaseMesh = true;
m_sourceMesh = sourceMesh;
}
if (dirtyComponent || m_component.isNull()) {
dirtyBaseMesh = true;
dirtyComponent = false;
MObject oSeamsEasyData = dataBlock.inputValue(aSelectedEdges).data();
MFnComponentListData compListData(oSeamsEasyData, &status);
CHECK_MSTATUS_AND_RETURN_IT(status);
m_component = compListData[0];
}
if (m_component.apiType() != MFn::kMeshEdgeComponent || m_sourceMesh.isNull())
return MS::kInvalidParameter;
// Load numeric attribs ///////////////////////////////////////////////////////////////////////
bool extrudeAll = dataBlock.inputValue(aExtrudeAllBoundaries).asBool();
float thickness = (float)dataBlock.inputValue(aExtrudeThickness).asDistance().as(MDistance::internalUnit());
unsigned int divisions = dataBlock.inputValue(aExtrudeDivisions).asInt();
double hardEdgeAngle = dataBlock.inputValue(aHardEdgeAngle).asAngle().as(MAngle::internalUnit());
// Load profile settings //////////////////////////////////////////////////////////////////////
std::set <OffsetParams> offsetParams;
OffsetParams::compare = OffsetParams::kDistance;
dirtyProfile = true;
if (dirtyProfile) {
std::set <OffsetParams> offsetParamsTmp;
dirtyProfile = false;
dirtyProfileMesh = true;
int profileMode = dataBlock.inputValue(aProfileMode).asInt();
switch (profileMode)
{
case 1: { // profile curve mode
int subdivisions = dataBlock.inputValue(aProfileSubdivs).asInt();
float width = (float)dataBlock.inputValue(aProfileWidth).asDistance().as(MDistance::internalUnit());
float depth = (float)dataBlock.inputValue(aProfileDepth).asDistance().as(MDistance::internalUnit());
float span = 1 / (float)(subdivisions + 1);
MRampAttribute rampAttr(thisMObject(), aProfileCurve);
float firstValue;
float srcMin = -1, max = -1;
for (int s = 0; s < subdivisions + 2; s++) {
OffsetParams newParam;
float param = s*span;
newParam.index = s;
newParam.distance = width - (param*width);
rampAttr.getValueAtPosition(param, newParam.depth, &status);
CHECK_MSTATUS_AND_RETURN_IT(status);
if (s == 0)
firstValue = newParam.depth;
if (srcMin == -1 || newParam.depth < srcMin)
srcMin = newParam.depth;
if (max == -1 || newParam.depth > max)
max = newParam.depth;
offsetParamsTmp.insert(newParam);
}
float trgMin = depth, trgMax = 0;
float srcMax = (firstValue == srcMin) ? max : firstValue;
for (auto oldParam : offsetParamsTmp) {
OffsetParams newParam = oldParam;
newParam.depth = remap(srcMin, srcMax, trgMin, trgMax, newParam.depth);
offsetParams.insert(newParam);
}
break;
}
default: { // manual mode
float distanceMultiplier = dataBlock.inputValue(aDistanceMultiplier).asFloat();
float depthMultiplier = dataBlock.inputValue(aDepthMultiplier).asFloat();
MArrayDataHandle hOffsetArray = dataBlock.inputArrayValue(aOffset);
for (unsigned int i = 0; i < hOffsetArray.elementCount(); i++) {
OffsetParams newParam;
hOffsetArray.jumpToArrayElement(i);
MDataHandle hOffsetElement = hOffsetArray.inputValue();
newParam.index = i;
newParam.distance = (float)hOffsetElement.child(aOffsetDistance).asDistance().as(MDistance::internalUnit())*distanceMultiplier;
newParam.depth = (float)hOffsetElement.child(aOffsetDepth).asDistance().as(MDistance::internalUnit())*depthMultiplier;
newParam.stitch = hOffsetElement.child(aOffsetStitch).asBool();
offsetParams.insert(newParam);
}
break;
}
}
}
if (offsetParams.size() == 0 || offsetParams.begin()->distance != 0){
OffsetParams newParam(0, 0, 0, dataBlock.inputArrayValue(aOffset).elementCount());
offsetParams.insert(newParam);
}
/////////////////////////////////////////////////////////////////////////////////////////
// Update base mesh
if (dirtyMesh || dirtyBaseMesh) {
dirtyMesh = false;
dirtyProfileMesh = true;
if (!dirtyBaseMesh) {
status = m_baseMesh.updateMesh(m_sourceMesh);
CHECK_MSTATUS_AND_RETURN_IT(status);
}
else {
dirtyBaseMesh = false;
// Get splitline IDs
MFnSingleIndexedComponent fnComponent(m_component, &status);
CHECK_MSTATUS_AND_RETURN_IT(status);
MIntArray splitlineArray;
fnComponent.getElements(splitlineArray);
// Copy source mesh
MFnMeshData meshData;
MObject workMesh = meshData.create();
MFnMesh fnSourceMesh(m_sourceMesh);
fnSourceMesh.copy(m_sourceMesh, workMesh, &status);
CHECK_MSTATUS_AND_RETURN_IT(status);
// Create base mesh
m_baseMesh = SSeamMesh(workMesh);
status = m_baseMesh.detachEdges(splitlineArray);
CHECK_MSTATUS_AND_RETURN_IT(status);
status = m_baseMesh.transferEdges(m_sourceMesh, splitlineArray);
CHECK_MSTATUS_AND_RETURN_IT(status);
}
}
// Update profile mesh
if (dirtyProfileMesh) {
dirtyProfileMesh = false;
float gap = (float)dataBlock.inputValue(aGap).asDistance().as(MDistance::internalUnit());
m_profileMesh = SSeamMesh(m_baseMesh);
// Componend to be passed to stitch node
MFnComponentListData compListData;
MObject compList = compListData.create();
MFnSingleIndexedComponent stitchCompData;
MObject stitchComponent = stitchCompData.create(MFn::kMeshEdgeComponent);
// Insert new loops
m_loopEdges.clear();
for (auto param = offsetParams.rbegin(); param != offsetParams.rend(); param++) {
m_profileMesh.getActiveEdges(m_loopEdges[param->index]);
if (param->stitch)
stitchCompData.addElements(m_loopEdges[param->index]);
status = m_profileMesh.offsetEdgeloops(param->distance + gap / 2, (0 == param->distance && param==--offsetParams.rend()) ? false : true);
CHECK_MSTATUS_AND_RETURN_IT(status);
}
status = compListData.add(stitchComponent);
CHECK_MSTATUS_AND_RETURN_IT(status);
status = dataBlock.outputValue(aOutStitchLines).set(compList);
CHECK_MSTATUS_AND_RETURN_IT(status);
// Pull loop vertices
for (auto param : offsetParams){
MIntArray loopVertices;
status = m_profileMesh.getEdgeVertices(m_loopEdges[param.index], loopVertices);
CHECK_MSTATUS_AND_RETURN_IT(status);
status = m_profileMesh.pullVertices(loopVertices, param.depth);
CHECK_MSTATUS_AND_RETURN_IT(status);
}
}
SSeamMesh extrudeMesh(m_profileMesh);
if (thickness != 0) {
MIntArray edgesToExtrude;
if (extrudeAll)
extrudeMesh.getBoundaryEdges(edgesToExtrude);
else
extrudeMesh.getActiveEdges(edgesToExtrude);
status = extrudeMesh.extrudeEdges(edgesToExtrude, thickness, divisions);
CHECK_MSTATUS_AND_RETURN_IT(status);
}
for (auto &loop : m_loopEdges) {
status = extrudeMesh.setHardEdges(loop.second, hardEdgeAngle);
CHECK_MSTATUS_AND_RETURN_IT(status);
}
status = dataBlock.outputValue(aOutMesh).set(extrudeMesh.getObject());
CHECK_MSTATUS_AND_RETURN_IT(status);
dataBlock.setClean(aOutMesh);
return MS::kSuccess;
}
float SeamsEasyNode::remap(const float srcMin, const float srcMax, const float trgMin, const float trgMax, float value) {
float srcDomain = srcMax - srcMin;
float trgDomain = trgMax - trgMin;
if (srcDomain == 0)
return value;
return trgMin + (value - srcMin) * trgDomain / srcDomain;
}
void SeamsEasyNode::attrChanged(MNodeMessage::AttributeMessage msg, MPlug &plug, MPlug &otherPlug, void *data) {
if (plug != aOffsetStitch)
return;
MPlug pOffset = plug.parent().array();
bool shouldConnect = false;
for (unsigned int i = 0; i < pOffset.numElements(); i++)
if (pOffset.elementByPhysicalIndex(i).child(aOffsetStitch).asBool()) {
shouldConnect = true;
break;
}
MFnDependencyNode fnNode(plug.node());
MPlugArray targets;
bool isConnected = fnNode.findPlug(aOutStitchLines).connectedTo(targets, false, true);
MDGModifier dgMod;
MDagModifier dagMod;
if (!isConnected && shouldConnect)
dgMod.commandToExecute("stitchEasy -sn "+fnNode.name()+" "+fnNode.name()+"Stitch");
if (isConnected && !shouldConnect) {
for (unsigned int t = 0; t < targets.length(); t++) {
MFnDependencyNode fnStitchNode(targets[t].node());
if (fnStitchNode.typeId() != StitchEasyNode::id)
continue;
MPlugArray outMeshes;
fnStitchNode.findPlug(StitchEasyNode::aOutMesh).connectedTo(outMeshes, false, true);
for (unsigned int m = 0; m < outMeshes.length(); m++)
dagMod.deleteNode(outMeshes[m].node());
dgMod.deleteNode(targets[t].node());
}
}
dgMod.doIt();
dagMod.doIt();
}