forked from sideeffects/HoudiniEngineForMaya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSyncOutputGeometryPart.C
648 lines (550 loc) · 19.1 KB
/
SyncOutputGeometryPart.C
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
#include "SyncOutputGeometryPart.h"
#include <maya/MDagPath.h>
#include <maya/MGlobal.h>
#include <maya/MSelectionList.h>
#include <maya/MFnDagNode.h>
#include <maya/MFnGenericAttribute.h>
#include <maya/MFnNumericAttribute.h>
#include <maya/MFnNurbsCurve.h>
#include <maya/MFnTypedAttribute.h>
#include "AssetNode.h"
#include "util.h"
static MObject
createAttributeFromDataHandle(
const MString &attributeName,
const MDataHandle &dataHandle
)
{
MObject attribute;
bool isNumeric = false;
bool isNull = false;
dataHandle.isGeneric(isNumeric, isNull);
if(isNumeric)
{
// This is a singleton generic type. It is not MFnNumericData, and
// needs to be handled differently. This is stored internally as a
// double, and there's no way to determine the original type.
MFnNumericAttribute numericAttribute;
attribute = numericAttribute.create(
attributeName,
attributeName,
MFnNumericData::kDouble
);
}
else if(dataHandle.numericType() != MFnNumericData::kInvalid)
{
MFnNumericAttribute numericAttribute;
attribute = numericAttribute.create(
attributeName,
attributeName,
dataHandle.numericType()
);
}
else if(dataHandle.type() != MFnData::kInvalid)
{
MFnTypedAttribute typedAttribute;
attribute = typedAttribute.create(
attributeName,
attributeName,
dataHandle.type()
);
}
return attribute;
}
SyncOutputGeometryPart::SyncOutputGeometryPart(
const MPlug &outputPlug,
const MObject &objectTransform
) :
myOutputPlug(outputPlug),
myObjectTransform(objectTransform)
{
}
SyncOutputGeometryPart::~SyncOutputGeometryPart()
{
}
MStatus
SyncOutputGeometryPart::doIt()
{
MStatus status;
MString partName = myOutputPlug.child(AssetNode::outputPartName).asString();
if(partName.length())
{
partName = Util::sanitizeStringForNodeName(partName);
}
else
{
partName = "emptyPart";
}
MFnDagNode objectTransformFn(myObjectTransform);
// create part
MObject partTransform = Util::findDagChild(objectTransformFn, partName);
if(partTransform.isNull())
{
status = createOutputPart(myObjectTransform, partName, partTransform);
}
// create material
MPlug materialPlug = myOutputPlug.child(AssetNode::outputPartMaterial);
MPlug materialExistsPlug = materialPlug.child(AssetNode::outputPartMaterialExists);
if(materialExistsPlug.asBool())
{
//TODO: check if material already exists
status = createOutputMaterial(materialPlug, partTransform);
CHECK_MSTATUS_AND_RETURN_IT(status);
}
else
{
MFnDagNode partTransformFn(partTransform);
status = myDagModifier.commandToExecute("assignSG \"lambert1\" \"" + partTransformFn.fullPathName() + "\";");
CHECK_MSTATUS_AND_RETURN_IT(status);
}
return redoIt();
}
MStatus
SyncOutputGeometryPart::undoIt()
{
MStatus status;
status = myDagModifier.undoIt();
CHECK_MSTATUS_AND_RETURN_IT(status);
return MStatus::kSuccess;
}
MStatus
SyncOutputGeometryPart::redoIt()
{
MStatus status;
status = myDagModifier.doIt();
CHECK_MSTATUS_AND_RETURN_IT(status);
return MStatus::kSuccess;
}
bool
SyncOutputGeometryPart::isUndoable() const
{
return true;
}
MStatus
SyncOutputGeometryPart::createOutputPart(
const MObject &objectTransform,
const MString &partName,
MObject &partTransform
)
{
MStatus status;
// create partTransform
partTransform = myDagModifier.createNode("transform", objectTransform, &status);
CHECK_MSTATUS_AND_RETURN_IT(status);
// rename partTransform
status = myDagModifier.renameNode(partTransform, partName);
CHECK_MSTATUS_AND_RETURN_IT(status);
// create mesh
MPlug hasMeshPlug = myOutputPlug.child(AssetNode::outputPartHasMesh);
if(hasMeshPlug.asBool())
{
status = createOutputMesh(
partTransform,
partName,
myOutputPlug.child(AssetNode::outputPartMesh)
);
}
// create particle
MPlug particleExistsPlug = myOutputPlug.child(AssetNode::outputPartHasParticles);
if(particleExistsPlug.asBool())
{
status = createOutputParticle(
partTransform,
partName,
myOutputPlug.child(AssetNode::outputPartParticle)
);
CHECK_MSTATUS_AND_RETURN_IT(status);
}
// create curves
MPlug curveIsBezier = myOutputPlug.child(AssetNode::outputPartCurvesIsBezier);
createOutputCurves(myOutputPlug.child(AssetNode::outputPartCurves),
partTransform,
partName,
curveIsBezier.asBool());
// doIt
// Need to do it here right away because otherwise the top level
// transform won't be shared.
status = myDagModifier.doIt();
CHECK_MSTATUS_AND_RETURN_IT(status);
return MStatus::kSuccess;
}
MStatus
SyncOutputGeometryPart::createOutputMesh(
const MObject &partTransform,
const MString &partName,
const MPlug &meshPlug
)
{
MStatus status;
// create mesh
MObject meshShape = myDagModifier.createNode("mesh", partTransform, &status);
CHECK_MSTATUS_AND_RETURN_IT(status);
// rename mesh
status = myDagModifier.renameNode(meshShape, partName + "Shape");
CHECK_MSTATUS_AND_RETURN_IT(status);
MFnDependencyNode partMeshFn(meshShape, &status);
CHECK_MSTATUS_AND_RETURN_IT(status);
// set mesh.displayColors
myDagModifier.newPlugValueBool(
partMeshFn.findPlug("displayColors"),
true
);
// connect mesh attributes
{
MPlug srcPlug;
MPlug dstPlug;
srcPlug = myOutputPlug.child(AssetNode::outputPartMesh);
dstPlug = partMeshFn.findPlug("inMesh");
status = myDagModifier.connect(srcPlug, dstPlug);
CHECK_MSTATUS_AND_RETURN_IT(status);
}
createOutputExtraAttributes(meshShape);
return MStatus::kSuccess;
}
MStatus
SyncOutputGeometryPart::createOutputCurves(
MPlug curvesPlug,
const MObject &partTransform,
const MString &partName,
bool isBezier
)
{
MStatus status;
int numCurves = curvesPlug.evaluateNumElements();
for(int i=0; i<numCurves; i++)
{
MPlug curvePlug = curvesPlug[i];
MString curveTransformName;
MString curveShapeName;
curveTransformName.format("curve^1s", MString() + (i + 1));
curveShapeName.format("curveShape^1s", MString() + (i + 1));
// create curve transform
MObject curveTransform = myDagModifier.createNode(
"transform",
partTransform,
&status
);
CHECK_MSTATUS_AND_RETURN_IT(status);
// rename curve transform
status = myDagModifier.renameNode(curveTransform, curveTransformName);
CHECK_MSTATUS_AND_RETURN_IT(status);
// create curve shape
MObject curveShape = myDagModifier.createNode(
isBezier ? "bezierCurve" : "nurbsCurve",
curveTransform,
&status
);
CHECK_MSTATUS(status);
// rename curve shape
status = myDagModifier.renameNode(curveShape, curveShapeName);
CHECK_MSTATUS_AND_RETURN_IT(status);
MFnDependencyNode curveShapeFn(curveShape, &status);
MPlug dstPlug = curveShapeFn.findPlug("create");
CHECK_MSTATUS(status);
myDagModifier.connect(curvePlug, dstPlug);
}
return MStatus::kSuccess;
}
MStatus
SyncOutputGeometryPart::createOutputMaterial(
const MPlug &materialPlug,
const MObject &partTransform
)
{
MStatus status;
MFnDagNode partTransformFn(partTransform, &status);
// create shader
MFnDependencyNode shaderFn;
{
MObject shader;
status = Util::createNodeByModifierCommand(
myDagModifier,
"shadingNode -asShader phong",
shader
);
CHECK_MSTATUS_AND_RETURN_IT(status);
status = shaderFn.setObject(shader);
CHECK_MSTATUS_AND_RETURN_IT(status);
}
// rename the shader
MPlug namePlug = materialPlug.child(AssetNode::outputPartMaterialName);
myDagModifier.renameNode(shaderFn.object(), namePlug.asString());
CHECK_MSTATUS_AND_RETURN_IT(myDagModifier.doIt());
// assign shader
status = myDagModifier.commandToExecute(
"assignSG " + shaderFn.name() + " " + partTransformFn.fullPathName()
);
CHECK_MSTATUS_AND_RETURN_IT(status);
// create file node if texture exists
MPlug texturePathPlug = materialPlug.child(AssetNode::outputPartTexturePath);
MString texturePath = texturePathPlug.asString();
MFnDependencyNode textureFileFn;
if(texturePath.length())
{
MObject textureFile;
status = Util::createNodeByModifierCommand(
myDagModifier,
"shadingNode -asTexture file",
textureFile
);
CHECK_MSTATUS_AND_RETURN_IT(status);
status = textureFileFn.setObject(textureFile);
CHECK_MSTATUS_AND_RETURN_IT(status);
}
// connect shader attributse
{
MPlug srcPlug;
MPlug dstPlug;
// color
if(textureFileFn.object().isNull())
{
srcPlug = materialPlug.child(AssetNode::outputPartDiffuseColor);
dstPlug = shaderFn.findPlug("color");
status = myDagModifier.connect(srcPlug, dstPlug);
CHECK_MSTATUS_AND_RETURN_IT(status);
}
else
{
dstPlug = textureFileFn.findPlug("fileTextureName");
status = myDagModifier.connect(texturePathPlug, dstPlug);
CHECK_MSTATUS_AND_RETURN_IT(status);
srcPlug = textureFileFn.findPlug("outColor");
dstPlug = shaderFn.findPlug("color");
status = myDagModifier.connect(srcPlug, dstPlug);
CHECK_MSTATUS_AND_RETURN_IT(status);
}
// specularColor
srcPlug = materialPlug.child(AssetNode::outputPartSpecularColor);
dstPlug = shaderFn.findPlug("specularColor");
status = myDagModifier.connect(srcPlug, dstPlug);
CHECK_MSTATUS_AND_RETURN_IT(status);
// ambientColor
srcPlug = materialPlug.child(AssetNode::outputPartAmbientColor);
dstPlug = shaderFn.findPlug("ambientColor");
status = myDagModifier.connect(srcPlug, dstPlug);
CHECK_MSTATUS_AND_RETURN_IT(status);
// transparency
srcPlug = materialPlug.child(AssetNode::outputPartAlphaColor);
dstPlug = shaderFn.findPlug("transparency");
status = myDagModifier.connect(srcPlug, dstPlug);
CHECK_MSTATUS_AND_RETURN_IT(status);
}
// doIt
status = myDagModifier.doIt();
CHECK_MSTATUS_AND_RETURN_IT(status);
return MStatus::kSuccess;
}
MStatus
SyncOutputGeometryPart::createOutputParticle(
const MObject &partTransform,
const MString &partName,
const MPlug &particlePlug
)
{
MStatus status;
// create nParticle
MObject particleShapeObj = myDagModifier.createNode(
"nParticle",
partTransform,
&status
);
CHECK_MSTATUS_AND_RETURN_IT(status);
// rename particle
status = myDagModifier.renameNode(particleShapeObj, partName + "Shape");
CHECK_MSTATUS_AND_RETURN_IT(status);
status = myDagModifier.doIt();
CHECK_MSTATUS_AND_RETURN_IT(status);
myDagModifier.commandToExecute(
"setupNParticleConnections " +
MFnDagNode(partTransform).fullPathName()
);
MPlug srcPlug;
MPlug dstPlug;
MFnDependencyNode particleShapeFn(particleShapeObj);
// connect nParticleShape attributes
status = myDagModifier.connect(srcPlug, dstPlug);
srcPlug = particlePlug.child(AssetNode::outputPartParticleCurrentTime);
dstPlug = particleShapeFn.findPlug("currentTime");
status = myDagModifier.connect(srcPlug, dstPlug);
CHECK_MSTATUS_AND_RETURN_IT(status);
srcPlug = particlePlug.child(AssetNode::outputPartParticlePositions);
dstPlug = particleShapeFn.findPlug("positions");
status = myDagModifier.connect(srcPlug, dstPlug);
CHECK_MSTATUS_AND_RETURN_IT(status);
srcPlug = particlePlug.child(AssetNode::outputPartParticleArrayData);
dstPlug = particleShapeFn.findPlug("cacheArrayData");
status = myDagModifier.connect(srcPlug, dstPlug);
CHECK_MSTATUS_AND_RETURN_IT(status);
// set particleRenderType to points
status = myDagModifier.newPlugValueInt(
particleShapeFn.findPlug("particleRenderType"),
3);
CHECK_MSTATUS_AND_RETURN_IT(status);
// set playFromCache to true
status = myDagModifier.newPlugValueBool(
particleShapeFn.findPlug("playFromCache"),
true);
CHECK_MSTATUS_AND_RETURN_IT(status);
createOutputExtraAttributes(particleShapeObj);
return MStatus::kSuccess;
}
MStatus
SyncOutputGeometryPart::createOutputExtraAttributes(
const MObject &dstNode
)
{
MStatus status;
MFnDependencyNode dstNodeFn(dstNode);
MPlug extraAttributesPlug = myOutputPlug.child(
AssetNode::outputPartExtraAttributes
);
bool isParticle = dstNode.hasFn(MFn::kParticle);
int numExtraAttributes = extraAttributesPlug.numElements();
for(int i = 0; i < numExtraAttributes; i++)
{
MPlug extraAttributePlug = extraAttributesPlug[i];
MPlug extraAttributeNamePlug = extraAttributePlug.child(
AssetNode::outputPartExtraAttributeName
);
MPlug extraAttributeOwnerPlug = extraAttributePlug.child(
AssetNode::outputPartExtraAttributeOwner
);
MPlug extraAttributeDataTypePlug = extraAttributePlug.child(
AssetNode::outputPartExtraAttributeDataType
);
MPlug extraAttributeTuplePlug = extraAttributePlug.child(
AssetNode::outputPartExtraAttributeTuple
);
MPlug extraAttributeDataPlug = extraAttributePlug.child(
AssetNode::outputPartExtraAttributeData
);
MString dstAttributeName = extraAttributeNamePlug.asString();
MString owner = extraAttributeOwnerPlug.asString();
MString dataType = extraAttributeDataTypePlug.asString();
int tuple = extraAttributeTuplePlug.asInt();
bool isPerParticleAttribute = false;
if(isParticle)
{
// Not every attribute type can be supported on a particle node.
bool canSupportAttribute = false;
if(owner == "detail")
{
// Make sure the data is not array type.
if((dataType == "float"
|| dataType == "int")
&& tuple <= 3)
{
canSupportAttribute = true;
}
else if(dataType == "string"
&& tuple == 1)
{
canSupportAttribute = true;
}
}
else if(owner == "primitive")
{
// Shouldn't be possible.
}
else if(owner == "point")
{
// Particles don't support int and string attributes. Also,
// make sure the one particle maps to one element of the array.
if((dataType == "float" && tuple == 1)
|| (dataType == "float" && tuple == 3))
{
isPerParticleAttribute = true;
canSupportAttribute = true;
}
}
else if(owner == "vertex")
{
// Shouldn't be possible.
}
if(!canSupportAttribute)
{
DISPLAY_WARNING(
"The particle node:\n"
" ^1s\n"
"cannot support the attribute:\n"
" owner: ^2s, name: ^3s, type: ^4s, tuple: ^5s\n"
"from the plug:"
" ^6s\n",
dstNodeFn.name(),
owner, dstAttributeName, dataType, MString() + tuple,
extraAttributeDataPlug.name()
);
continue;
}
}
// Some special cases to remap certain attributes.
if(owner != "detail" && dstAttributeName == "v")
{
// "v" means velocity in Houdini. However, in Maya, "v" happens to
// be the short name of the "visibility" attribute. Instead of
// connecting velocity to visibility, which would be very confusing
// for the user, we rename "v" to "velocity".
dstAttributeName = "velocity";
}
// Use existing attribute if it exists.
MObject dstAttribute = dstNodeFn.attribute(dstAttributeName);
// If it doesn't exist, create it.
if(dstAttribute.isNull())
{
MDataHandle dataHandle = extraAttributeDataPlug.asMDataHandle();
dstAttribute = createAttributeFromDataHandle(
dstAttributeName,
dataHandle
);
if(!dstAttribute.isNull())
{
CHECK_MSTATUS(myDagModifier.addAttribute(
dstNode,
dstAttribute
));
CHECK_MSTATUS(myDagModifier.doIt());
}
}
// If there's still no attribute, skip it.
if(dstAttribute.isNull())
{
DISPLAY_WARNING(
"Cannot create attribute on the node:\n"
" ^1s\n"
"for the attribute:\n"
" owner: ^2s, name: ^3s, type: ^4s, tuple: ^5s\n"
"from the plug:\n"
" ^6s\n",
dstNodeFn.name(),
owner, dstAttributeName, dataType, MString() + tuple,
extraAttributeDataPlug.name()
);
continue;
}
// Per-particle attributes does not need to be, and cannot be,
// connected.
if(isParticle && isPerParticleAttribute)
{
continue;
}
MPlug dstPlug(dstNode, dstAttribute);
CHECK_MSTATUS(myDagModifier.connect(
extraAttributeDataPlug,
dstPlug
));
status = (myDagModifier.doIt());
if(!status)
{
DISPLAY_WARNING(
"Failed to connect:\n"
" ^1s\n"
" to:\n"
" ^2s\n",
extraAttributeDataPlug.name(),
dstPlug.name()
);
CHECK_MSTATUS(status);
}
}
return status;
}