-
Notifications
You must be signed in to change notification settings - Fork 60
/
OutputGeometryPart.C
2402 lines (2076 loc) · 84.6 KB
/
OutputGeometryPart.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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <maya/MArrayDataBuilder.h>
#include <maya/MEulerRotation.h>
#include <maya/MFnArrayAttrsData.h>
#include <maya/MFnDoubleArrayData.h>
#include <maya/MFnIntArrayData.h>
#include <maya/MFnMesh.h>
#include <maya/MFnMeshData.h>
#include <maya/MFnNurbsCurve.h>
#include <maya/MFnNurbsCurveData.h>
#include <maya/MFnStringArrayData.h>
#include <maya/MGlobal.h>
#include <maya/MItMeshPolygon.h>
#include <maya/MMatrix.h>
#include <maya/MPointArray.h>
#include <maya/MQuaternion.h>
#include <maya/MTime.h>
#if MAYA_API_VERSION >= 201400
#include <maya/MFnFloatArrayData.h>
#endif
#include <maya/MFnVectorArrayData.h>
#include <algorithm>
#include <limits>
#include <map>
#include <string>
#include <vector>
#include "Asset.h"
#include "AssetNode.h"
#include "OutputGeometryPart.h"
#include "hapiutil.h"
#include "types.h"
#include "util.h"
OutputGeometryPart::OutputGeometryPart(HAPI_NodeId nodeId, HAPI_PartId partId)
: myNodeId(nodeId),
myPartId(partId),
myLastOutputGeometryGroups(true),
myLastOutputCustomAttributes(true)
{
update();
}
OutputGeometryPart::~OutputGeometryPart() {}
#if MAYA_API_VERSION >= 201400
void
OutputGeometryPart::computeVolumeTransform(const MTime &time,
MDataHandle &volumeTransformHandle,
const bool preserveScale)
{
HAPI_Transform transform = myVolumeInfo.transform;
MDataHandle translateHandle =
volumeTransformHandle.child(AssetNode::outputPartVolumeTranslate);
MDataHandle rotateHandle =
volumeTransformHandle.child(AssetNode::outputPartVolumeRotate);
MDataHandle scaleHandle =
volumeTransformHandle.child(AssetNode::outputPartVolumeScale);
MEulerRotation r = MQuaternion(transform.rotationQuaternion[0],
transform.rotationQuaternion[1],
transform.rotationQuaternion[2],
transform.rotationQuaternion[3])
.asEulerRotation();
const double rot[3] = {r[0], r[1], r[2]};
const double scale[3] = {
transform.scale[0], transform.scale[1], transform.scale[2]};
if (preserveScale)
{
transform.position[0] *= 100.0;
transform.position[1] *= 100.0;
transform.position[2] *= 100.0;
}
MTransformationMatrix matrix;
matrix.addScale(scale, MSpace::kTransform);
matrix.addRotation(rot, MTransformationMatrix::kXYZ, MSpace::kTransform);
matrix.addTranslation(MVector(transform.position[0], transform.position[1],
transform.position[2]),
MSpace::kTransform);
double xoffset = myVolumeInfo.xLength / 2.0 + myVolumeInfo.minX;
double yoffset = myVolumeInfo.yLength / 2.0 + myVolumeInfo.minY;
double zoffset = myVolumeInfo.zLength / 2.0 + myVolumeInfo.minZ;
const double scale2[3] = {2, 2, 2};
matrix.addScale(scale2, MSpace::kPreTransform);
if (preserveScale)
matrix.addTranslation(
MVector(-50.0, -50.0, -50.0), MSpace::kPreTransform);
else
matrix.addTranslation(MVector(-0.5, -0.5, -0.5), MSpace::kPreTransform);
if (preserveScale)
{
matrix.addTranslation(
MVector(xoffset * 100.0, yoffset * 100.0, zoffset * 100.0),
MSpace::kPreTransform);
}
else
{
matrix.addTranslation(
MVector(xoffset, yoffset, zoffset), MSpace::kPreTransform);
}
double scale3[3] = {static_cast<double>(myVolumeInfo.xLength),
static_cast<double>(myVolumeInfo.yLength),
static_cast<double>(myVolumeInfo.zLength)};
if (preserveScale)
{
scale3[0] *= 100.0;
scale3[1] *= 100.0;
scale3[2] *= 100.0;
}
matrix.addScale(scale3, MSpace::kPreTransform);
double final_scale[3];
double final_rotate[3];
MTransformationMatrix::RotationOrder order = MTransformationMatrix::kXYZ;
matrix.getScale(final_scale, MSpace::kTransform);
matrix.getRotation(final_rotate, order);
translateHandle.set(matrix.getTranslation(MSpace::kTransform));
rotateHandle.set3Double(final_rotate[0], final_rotate[1], final_rotate[2]);
scaleHandle.set3Double(final_scale[0], final_scale[1], final_scale[2]);
}
#endif
void
OutputGeometryPart::update()
{
CHECK_HAPI(
HoudiniApi::GetGeoInfo(Util::theHAPISession.get(), myNodeId, &myGeoInfo));
CHECK_HAPI(HoudiniApi::GetPartInfo(
Util::theHAPISession.get(), myNodeId, myPartId, &myPartInfo));
if (myPartInfo.type == HAPI_PARTTYPE_VOLUME)
{
CHECK_HAPI(HoudiniApi::GetVolumeInfo(
Util::theHAPISession.get(), myNodeId, myPartId, &myVolumeInfo));
}
if (myPartInfo.type == HAPI_PARTTYPE_CURVE)
{
CHECK_HAPI(HoudiniApi::GetCurveInfo(
Util::theHAPISession.get(), myNodeId, myPartId, &myCurveInfo));
}
}
bool
OutputGeometryPart::needCompute(
AssetNodeOptions::AccessorDataBlock &options) const
{
return myLastOutputGeometryGroups != options.outputGeometryGroups() ||
myLastOutputCustomAttributes != options.outputCustomAttributes();
}
MStatus
OutputGeometryPart::compute(const MTime &time,
const MPlug &partPlug,
MDataBlock &data,
MDataHandle &partHandle,
AssetNodeOptions::AccessorDataBlock &options,
bool &needToSyncOutputs)
{
data.setClean(partPlug);
update();
// compute geometry
{
clearAttributesUsed();
// Name
MDataHandle nameHandle = partHandle.child(AssetNode::outputPartName);
MString partName;
if (myPartInfo.nameSH != 0)
{
partName = Util::HAPIString(myPartInfo.nameSH);
}
nameHandle.set(partName);
// Mesh
MDataHandle hasMeshHandle =
partHandle.child(AssetNode::outputPartHasMesh);
MDataHandle meshHandle = partHandle.child(AssetNode::outputPartMesh);
computeMesh(time, partPlug.child(AssetNode::outputPartHasMesh),
partPlug.child(AssetNode::outputPartMesh), data,
hasMeshHandle, meshHandle, options);
// Particle
MDataHandle hasParticlesHandle =
partHandle.child(AssetNode::outputPartHasParticles);
MDataHandle particleHandle =
partHandle.child(AssetNode::outputPartParticle);
computeParticle(time, partPlug.child(AssetNode::outputPartHasParticles),
partPlug.child(AssetNode::outputPartParticle), data,
hasParticlesHandle, particleHandle, options);
#if MAYA_API_VERSION >= 201400
// Volume
MDataHandle volumeHandle =
partHandle.child(AssetNode::outputPartVolume);
if (myPartInfo.type == HAPI_PARTTYPE_VOLUME)
{
computeVolume(time, partPlug.child(AssetNode::outputPartVolume),
data, volumeHandle, options.preserveScale());
}
#endif
// Curve
MDataHandle curvesHandle =
partHandle.child(AssetNode::outputPartCurves);
MDataHandle curvesIsBezierHandle =
partHandle.child(AssetNode::outputPartCurvesIsBezier);
computeCurves(time, partPlug.child(AssetNode::outputPartCurves),
partPlug.child(AssetNode::outputPartCurvesIsBezier), data,
curvesHandle, curvesIsBezierHandle, options);
// Instancer
MDataHandle hasInstancerHandle =
partHandle.child(AssetNode::outputPartHasInstancer);
MDataHandle instanceHandle =
partHandle.child(AssetNode::outputPartInstancer);
computeInstancer(
time, partPlug.child(AssetNode::outputPartHasInstancer),
partPlug.child(AssetNode::outputPartInstancer), data,
hasInstancerHandle, instanceHandle, options.preserveScale());
// Groups
MDataHandle groupsHandle =
partHandle.child(AssetNode::outputPartGroups);
computeGroups(time, partPlug.child(AssetNode::outputPartGroups), data,
groupsHandle, options, needToSyncOutputs);
// compute material
MDataHandle materialHandle =
partHandle.child(AssetNode::outputPartMaterialIds);
computeMaterial(time, partPlug.child(AssetNode::outputPartMaterialIds),
data, materialHandle);
// Extra attributes
MDataHandle extraAttributesHandle =
partHandle.child(AssetNode::outputPartExtraAttributes);
computeExtraAttributes(
time, partPlug.child(AssetNode::outputPartExtraAttributes), data,
extraAttributesHandle, options, needToSyncOutputs);
}
return MS::kSuccess;
}
void
OutputGeometryPart::computeCurves(const MTime &time,
const MPlug &curvesPlug,
const MPlug &curvesIsBezierPlug,
MDataBlock &data,
MDataHandle &curvesHandle,
MDataHandle &curvesIsBezierHandle,
AssetNodeOptions::AccessorDataBlock &options)
{
MStatus status;
MArrayDataHandle curvesArrayHandle(curvesHandle);
int curveCount = 0;
if (myPartInfo.type == HAPI_PARTTYPE_CURVE)
curveCount = myCurveInfo.curveCount;
if (curvesArrayHandle.elementCount() != (unsigned int)curveCount)
Util::resizeArrayDataHandle(curvesArrayHandle, curveCount);
if (curveCount)
{
HAPI_AttributeInfo attrInfo;
std::vector<float> pArray, pwArray;
hapiGetPointAttribute(myNodeId, myPartId, "P", attrInfo, pArray);
hapiGetPointAttribute(myNodeId, myPartId, "Pw", attrInfo, pwArray);
markAttributeUsed("P");
markAttributeUsed("Pw");
int vertexOffset = 0;
int knotOffset = 0;
for (int iCurve = 0; iCurve < myCurveInfo.curveCount; iCurve++)
{
CHECK_MSTATUS(curvesArrayHandle.jumpToArrayElement(iCurve));
MDataHandle curve = curvesArrayHandle.outputValue();
MObject curveDataObj = curve.data();
MFnNurbsCurveData curveDataFn(curveDataObj);
if (curve.data().isNull())
{
// set the MDataHandle
curveDataObj = curveDataFn.create();
curve.setMObject(curveDataObj);
// then get the copy from MDataHandle
curveDataObj = curve.data();
curveDataFn.setObject(curveDataObj);
}
// Number of CVs
int numVertices = 0;
HoudiniApi::GetCurveCounts(Util::theHAPISession.get(), myNodeId, myPartId,
&numVertices, iCurve, 1);
const int nextVertexOffset = vertexOffset + numVertices;
if (nextVertexOffset > static_cast<int>(pArray.size()) * 3 ||
(!pwArray.empty() &&
nextVertexOffset > static_cast<int>(pwArray.size())))
{
MGlobal::displayError("Not enough points to create a curve");
break;
}
// Order of this particular curve
int order;
if (myCurveInfo.order != HAPI_CURVE_ORDER_VARYING &&
myCurveInfo.order != HAPI_CURVE_ORDER_INVALID)
{
order = myCurveInfo.order;
}
else
{
HoudiniApi::GetCurveOrders(Util::theHAPISession.get(), myNodeId,
myPartId, &order, iCurve, 1);
}
// If there's not enough vertices, then don't try to create the
// curve.
if (numVertices < order)
{
// Need to make sure we clear out the curve that was created
// previously.
curve.setMObject(curveDataFn.create());
// The curve at i will have numVertices vertices, and may have
// some knots. The knot count will be numVertices + order for
// nurbs curves
vertexOffset = nextVertexOffset;
knotOffset += numVertices + order;
continue;
}
MPointArray controlVertices(numVertices);
for (int iDst = 0, iSrc = vertexOffset; iDst < numVertices;
++iDst, ++iSrc)
{
controlVertices[iDst] = MPoint(
pArray[iSrc * 3], pArray[iSrc * 3 + 1],
pArray[iSrc * 3 + 2],
pwArray.empty() ? 1.0f : pwArray[iSrc]);
}
if (options.preserveScale())
{
for (unsigned int i = 0; i < controlVertices.length(); i++)
{
controlVertices[i].x *= 100.0;
controlVertices[i].y *= 100.0;
controlVertices[i].z *= 100.0;
controlVertices[i].w *= 100.0;
}
}
MDoubleArray knotSequences;
if (myCurveInfo.hasKnots)
{
std::vector<float> knots;
knots.resize(numVertices + order);
// The Maya knot vector has two fewer knots;
// the first and last houdini knot are excluded
knotSequences.setLength(numVertices + order - 2);
HoudiniApi::GetCurveKnots(Util::theHAPISession.get(), myNodeId,
myPartId, &knots.front(), knotOffset,
numVertices + order);
// Maya doesn't need the first and last knots
for (int j = 0; j < numVertices + order - 2; j++)
knotSequences[j] = knots[j + 1];
}
else if (myCurveInfo.curveType == HAPI_CURVETYPE_BEZIER)
{
// Bezier knot vector needs to still be passed in
knotSequences.setLength(numVertices + order - 2);
for (int j = 0; j < numVertices + order - 2; j++)
knotSequences[j] = j / (order - 1);
}
else
{
knotSequences.setLength(numVertices + order - 2);
int j = 0;
for (; j < order - 1; j++)
knotSequences[j] = 0.0;
for (int k = 1; j < numVertices - 1; k++, j++)
knotSequences[j] = (double)k / (numVertices - order + 1);
for (; j < numVertices + order - 2; j++)
knotSequences[j] = 1.0;
}
// NOTE: Periodicity is always constant, so periodic and
// non-periodic curve meshes will have different parts.
MFnNurbsCurve curveFn;
MObject nurbsCurve = curveFn.create(
controlVertices, knotSequences, order - 1,
myCurveInfo.isPeriodic ? MFnNurbsCurve::kPeriodic :
MFnNurbsCurve::kOpen,
false /* 2d? */, myCurveInfo.isRational /* rational? */,
curveDataObj, &status);
CHECK_MSTATUS(status);
// The curve at i will have numVertices vertices, and may have
// some knots. The knot count will be numVertices + order for
// nurbs curves
vertexOffset = nextVertexOffset;
knotOffset += numVertices + order;
}
curvesIsBezierHandle.setBool(myCurveInfo.curveType ==
HAPI_CURVETYPE_BEZIER);
}
curvesArrayHandle.setAllClean();
}
template <typename T>
bool
OutputGeometryPart::convertParticleAttribute(T particleArray,
const char *houdiniName,
bool preserveScale)
{
typedef ARRAYTRAIT(T) Trait;
typedef ELEMENTTRAIT(T) ElementTrait;
HAPI_AttributeInfo attrInfo;
std::vector<typename ElementTrait::ComponentType> dataArray;
if (!HAPI_FAIL(hapiGetPointAttribute(
myNodeId, myPartId, houdiniName, attrInfo, dataArray)))
{
particleArray = Util::reshapeArray<T>(dataArray);
if (preserveScale)
{
for (unsigned int i = 0; i < particleArray.length(); i++)
particleArray[i] *= 100.0;
}
return true;
}
else
{
Trait::resize(particleArray, myPartInfo.pointCount);
Util::zeroArray(particleArray);
return false;
}
}
bool
OutputGeometryPart::computeExtraAttribute(const MPlug &extraAttributePlug,
MDataBlock &data,
MDataHandle &extraAttributeHandle,
HAPI_AttributeOwner attributeOwner,
const char *attributeName)
{
static const MString attributeOwnersString[] = {
"vertex",
"point",
"primitive",
"detail",
};
static const MString dataTypesString[] = {
"int", "long", "float", "double", "string",
};
MDataHandle nameHandle =
extraAttributeHandle.child(AssetNode::outputPartExtraAttributeName);
MDataHandle ownerHandle =
extraAttributeHandle.child(AssetNode::outputPartExtraAttributeOwner);
MDataHandle dataTypeHandle =
extraAttributeHandle.child(AssetNode::outputPartExtraAttributeDataType);
MDataHandle tupleHandle =
extraAttributeHandle.child(AssetNode::outputPartExtraAttributeTuple);
MDataHandle dataHandle =
extraAttributeHandle.child(AssetNode::outputPartExtraAttributeData);
HAPI_AttributeInfo attributeInfo;
HAPI_FAIL(HoudiniApi::GetAttributeInfo(Util::theHAPISession.get(), myNodeId,
myPartId, attributeName, attributeOwner,
&attributeInfo));
if (!attributeInfo.exists)
{
// HAPI might not be able to handle certain attributes (e.g.
// tuple size is 0).
return false;
}
HAPI_StorageType storage = attributeInfo.storage;
// Particle requires special treatment
bool hasParticles = myPartInfo.pointCount != 0 &&
myPartInfo.vertexCount == 0 &&
myPartInfo.faceCount == 0;
if (hasParticles)
{
if (attributeOwner == HAPI_ATTROWNER_POINT)
{
// Maya Particle only supports double or double vector per-particle
// attributes. For int and float attributes, always use the double
// code path to cast to double.
if (storage == HAPI_STORAGETYPE_INT ||
storage == HAPI_STORAGETYPE_INT64 ||
storage == HAPI_STORAGETYPE_FLOAT ||
storage == HAPI_STORAGETYPE_FLOAT64)
{
storage = HAPI_STORAGETYPE_FLOAT64;
}
}
}
if (storage == HAPI_STORAGETYPE_FLOAT)
{
MFloatArray floatArray;
hapiGetAttribute(myNodeId, myPartId, attributeOwner, attributeName,
attributeInfo, floatArray);
if (attributeOwner == HAPI_ATTROWNER_DETAIL &&
attributeInfo.tupleSize == 1)
{
dataHandle.setGenericFloat(floatArray[0], true);
}
else if (attributeOwner == HAPI_ATTROWNER_DETAIL &&
attributeInfo.tupleSize == 2)
{
MFnNumericData numericData;
MObject dataObject = numericData.create(MFnNumericData::k2Float);
numericData.setData2Float(floatArray[0], floatArray[1]);
dataHandle.setMObject(dataObject);
}
else if (attributeOwner == HAPI_ATTROWNER_DETAIL &&
attributeInfo.tupleSize == 3)
{
MFnNumericData numericData;
MObject dataObject = numericData.create(MFnNumericData::k3Float);
numericData.setData3Float(
floatArray[0], floatArray[1], floatArray[2]);
dataHandle.setMObject(dataObject);
}
else if (attributeInfo.tupleSize == 3)
{
// Since MFnFloatVectorArrayData doesn't exist, use
// MFnVectorArrayData instead.
MFnVectorArrayData vectorArrayData;
MObject dataObject = vectorArrayData.create();
MVectorArray outputVectorArray = vectorArrayData.array();
outputVectorArray = Util::reshapeArray<MVectorArray>(floatArray);
dataHandle.setMObject(dataObject);
}
else
{
MFnFloatArrayData floatArrayData;
MObject dataObject = floatArrayData.create();
MFloatArray outputFloatArray = floatArrayData.array();
outputFloatArray = floatArray;
dataHandle.setMObject(dataObject);
}
}
else if (storage == HAPI_STORAGETYPE_FLOAT64)
{
MDoubleArray doubleArray;
hapiGetAttribute(myNodeId, myPartId, attributeOwner, attributeName,
attributeInfo, doubleArray);
if (attributeOwner == HAPI_ATTROWNER_DETAIL &&
attributeInfo.tupleSize == 1)
{
dataHandle.setGenericDouble(doubleArray[0], true);
}
else if (attributeOwner == HAPI_ATTROWNER_DETAIL &&
attributeInfo.tupleSize == 2)
{
MFnNumericData numericData;
MObject dataObject = numericData.create(MFnNumericData::k2Double);
numericData.setData2Double(doubleArray[0], doubleArray[1]);
dataHandle.setMObject(dataObject);
}
else if (attributeOwner == HAPI_ATTROWNER_DETAIL &&
attributeInfo.tupleSize == 3)
{
MFnNumericData numericData;
MObject dataObject = numericData.create(MFnNumericData::k3Double);
numericData.setData3Double(
doubleArray[0], doubleArray[1], doubleArray[2]);
dataHandle.setMObject(dataObject);
}
else if (attributeOwner == HAPI_ATTROWNER_DETAIL &&
attributeInfo.tupleSize == 4)
{
MFnNumericData numericData;
MObject dataObject = numericData.create(MFnNumericData::k4Double);
numericData.setData4Double(
doubleArray[0], doubleArray[1], doubleArray[2], doubleArray[3]);
dataHandle.setMObject(dataObject);
}
else if (attributeInfo.tupleSize == 3)
{
MFnVectorArrayData vectorArrayData;
MObject dataObject = vectorArrayData.create();
MVectorArray outputVectorArray = vectorArrayData.array();
outputVectorArray = Util::reshapeArray<MVectorArray>(doubleArray);
dataHandle.setMObject(dataObject);
}
else
{
MFnDoubleArrayData doubleArrayData;
MObject dataObject = doubleArrayData.create();
MDoubleArray outputDoubleArray = doubleArrayData.array();
outputDoubleArray = doubleArray;
dataHandle.setMObject(dataObject);
}
}
else if (storage == HAPI_STORAGETYPE_INT ||
storage == HAPI_STORAGETYPE_INT64)
{
MIntArray intArray;
hapiGetAttribute(myNodeId, myPartId, attributeOwner, attributeName,
attributeInfo, intArray);
if (attributeInfo.owner == HAPI_ATTROWNER_DETAIL &&
attributeInfo.tupleSize == 1)
{
dataHandle.setGenericInt(intArray[0], true);
}
else if (attributeInfo.owner == HAPI_ATTROWNER_DETAIL &&
attributeInfo.tupleSize == 2)
{
MFnNumericData numericData;
MObject dataObject = numericData.create(MFnNumericData::k2Int);
numericData.setData2Int(intArray[0], intArray[1]);
dataHandle.setMObject(dataObject);
}
else if (attributeInfo.owner == HAPI_ATTROWNER_DETAIL &&
attributeInfo.tupleSize == 3)
{
MFnNumericData numericData;
MObject dataObject = numericData.create(MFnNumericData::k3Int);
numericData.setData3Int(intArray[0], intArray[1], intArray[2]);
dataHandle.setMObject(dataObject);
}
else
{
MFnIntArrayData intArrayData;
MObject dataObject = intArrayData.create();
MIntArray outputIntArray = intArrayData.array();
outputIntArray = intArray;
dataHandle.setMObject(dataObject);
}
}
else if (storage == HAPI_STORAGETYPE_STRING)
{
MStringArray stringArray;
hapiGetAttribute(myNodeId, myPartId, attributeOwner, attributeName,
attributeInfo, stringArray);
if (attributeInfo.owner == HAPI_ATTROWNER_DETAIL &&
attributeInfo.tupleSize == 1)
{
dataHandle.setString(stringArray[0]);
}
else
{
MFnStringArrayData stringArrayData;
MObject dataObject = stringArrayData.create();
MStringArray outputStringArray = stringArrayData.array();
outputStringArray = stringArray;
dataHandle.setMObject(dataObject);
}
}
else
{
return false;
}
const MString &ownerString = attributeOwnersString[attributeOwner];
const MString &dataTypeString = dataTypesString[storage];
nameHandle.setString(attributeName);
ownerHandle.setString(ownerString);
dataTypeHandle.setString(dataTypeString);
tupleHandle.setInt(attributeInfo.tupleSize);
return true;
}
void
OutputGeometryPart::computeParticle(
const MTime &time,
const MPlug &hasParticlePlug,
const MPlug &particlePlug,
MDataBlock &data,
MDataHandle &hasParticlesHandle,
MDataHandle &particleHandle,
AssetNodeOptions::AccessorDataBlock &options)
{
data.setClean(hasParticlePlug);
data.setClean(particlePlug);
bool hasParticles = myPartInfo.pointCount != 0 &&
myPartInfo.vertexCount == 0 &&
myPartInfo.faceCount == 0;
if (!hasParticlesHandle.asBool() && !hasParticles)
return;
hasParticlesHandle.setBool(hasParticles);
MDataHandle currentTimeHandle =
particleHandle.child(AssetNode::outputPartParticleCurrentTime);
MDataHandle positionsHandle =
particleHandle.child(AssetNode::outputPartParticlePositions);
MDataHandle arrayDataHandle =
particleHandle.child(AssetNode::outputPartParticleArrayData);
std::vector<float> floatArray;
std::vector<int> intArray;
int particleCount = myPartInfo.pointCount;
// currentTime
currentTimeHandle.setMTime(time);
// array data
MObject arrayDataObj = arrayDataHandle.data();
MFnArrayAttrsData arrayDataFn(arrayDataObj);
if (arrayDataObj.isNull())
{
arrayDataObj = arrayDataFn.create();
arrayDataHandle.setMObject(arrayDataObj);
arrayDataObj = arrayDataHandle.data();
arrayDataFn.setObject(arrayDataObj);
}
// count
MDoubleArray countArray = arrayDataFn.doubleArray("count");
countArray.setLength(1);
countArray[0] = particleCount;
markAttributeUsed("count");
// position
convertParticleAttribute(
arrayDataFn.vectorArray("position"), "P", options.preserveScale());
{
MObject positionsObj = positionsHandle.data();
MFnVectorArrayData positionDataFn(positionsObj);
if (positionsObj.isNull())
{
positionsObj = positionDataFn.create();
positionsHandle.setMObject(positionsObj);
}
MVectorArray positions = positionDataFn.array();
positions = arrayDataFn.vectorArray("position");
}
markAttributeUsed("P");
markAttributeUsed("position");
if (!hasParticles)
return;
// id
{
HAPI_AttributeInfo attrInfo;
MDoubleArray idArray = arrayDataFn.doubleArray("id");
if (HAPI_FAIL(hapiGetPointAttribute(
myNodeId, myPartId, "id", attrInfo, idArray)))
{
idArray.setLength(particleCount);
for (unsigned int i = 0; i < idArray.length(); i++)
{
idArray[i] = i;
}
}
}
markAttributeUsed("id");
// velocity
convertParticleAttribute(
arrayDataFn.vectorArray("velocity"), "v", options.preserveScale());
markAttributeUsed("v");
markAttributeUsed("velocity");
// acceleration
convertParticleAttribute(arrayDataFn.vectorArray("acceleration"), "force",
options.preserveScale());
markAttributeUsed("acceleration");
markAttributeUsed("force");
// worldPosition
arrayDataFn.vectorArray("worldPosition")
.copy(arrayDataFn.vectorArray("position"));
markAttributeUsed("worldPosition");
// worldVelocity
arrayDataFn.vectorArray("worldVelocity")
.copy(arrayDataFn.vectorArray("velocity"));
markAttributeUsed("worldVelocity");
// worldVelocityInObjectSpace
arrayDataFn.vectorArray("worldVelocityInObjectSpace")
.copy(arrayDataFn.vectorArray("velocity"));
markAttributeUsed("worldVelocityInObjectSpace");
// mass
convertParticleAttribute(arrayDataFn.doubleArray("mass"), "mass", true);
markAttributeUsed("mass");
// birthTime
bool birthTimeDefined = convertParticleAttribute(
arrayDataFn.doubleArray("birthTime"), "birthTime", false);
markAttributeUsed("birthTime");
// age
bool ageDefined = convertParticleAttribute(
arrayDataFn.doubleArray("age"), "age", false);
markAttributeUsed("age");
// in Maya, age is an output computed from birthTime
// so if the asset has explicitly defined birthTime, we use it
// if there is age but no birthTime, we compute it from age
// if there is neither age nor birthTime, we leave birthTime zeroed
if (ageDefined && !birthTimeDefined)
{
double timeInSec = time.as(MTime::Unit::kSeconds);
MDoubleArray birthTimeArray = arrayDataFn.doubleArray("birthTime");
MDoubleArray ageArray = arrayDataFn.doubleArray("age");
for (int i = 0; i < particleCount; i++)
{
birthTimeArray[i] = timeInSec - ageArray[i];
}
}
// lifespanPP
convertParticleAttribute(
arrayDataFn.doubleArray("lifespanPP"), "life", false);
// finalLifespanPP
convertParticleAttribute(
arrayDataFn.doubleArray("finalLifespanPP"), "life", false);
markAttributeUsed("life");
// other attributes
std::vector<HAPI_StringHandle> attributeNames(
myPartInfo.attributeCounts[HAPI_ATTROWNER_POINT]);
HoudiniApi::GetAttributeNames(Util::theHAPISession.get(), myNodeId, myPartId,
HAPI_ATTROWNER_POINT,
attributeNames.empty() ? NULL : &attributeNames[0],
myPartInfo.attributeCounts[HAPI_ATTROWNER_POINT]);
for (unsigned int i = 0; i < attributeNames.size(); i++)
{
MString attributeName = Util::HAPIString(attributeNames[i]);
// skip attributes that were done above already
if (isAttributeUsed(attributeName.asChar()))
{
continue;
}
// translate certain attributes into Maya names
MString translatedAttributeName;
if (attributeName == "Cd")
{
translatedAttributeName = "rgbPP";
}
else if (attributeName == "Alpha")
{
translatedAttributeName = "opacityPP";
}
else if (attributeName == "pscale")
{
translatedAttributeName = "radiusPP";
}
else if (attributeName == "life")
{
translatedAttributeName = "finalLifespanPP";
}
else
{
translatedAttributeName = attributeName;
}
HAPI_AttributeInfo attributeInfo;
HoudiniApi::GetAttributeInfo(Util::theHAPISession.get(), myNodeId, myPartId,
attributeName.asChar(), HAPI_ATTROWNER_POINT,
&attributeInfo);
HAPI_StorageType storage = attributeInfo.storage;
if (storage == HAPI_STORAGETYPE_INT ||
storage == HAPI_STORAGETYPE_INT64 ||
storage == HAPI_STORAGETYPE_FLOAT ||
storage == HAPI_STORAGETYPE_FLOAT64)
{
storage = HAPI_STORAGETYPE_FLOAT64;
}
// put the data into MFnArrayAttrsData
if (storage == HAPI_STORAGETYPE_FLOAT64 && attributeInfo.tupleSize == 3)
{
convertParticleAttribute(
arrayDataFn.vectorArray(translatedAttributeName),
attributeName.asChar(), false);
}
else if (storage == HAPI_STORAGETYPE_FLOAT64 &&
attributeInfo.tupleSize == 1)
{
convertParticleAttribute(
arrayDataFn.doubleArray(translatedAttributeName),
attributeName.asChar(), false);
}
}
}
#if MAYA_API_VERSION >= 201400
void
OutputGeometryPart::computeVolume(const MTime &time,
const MPlug &volumePlug,
MDataBlock &data,
MDataHandle &volumeHandle,
const bool preserveScale)
{
MDataHandle gridHandle =
volumeHandle.child(AssetNode::outputPartVolumeGrid);
MDataHandle transformHandle =
volumeHandle.child(AssetNode::outputPartVolumeTransform);
MDataHandle resHandle = volumeHandle.child(AssetNode::outputPartVolumeRes);
MDataHandle nameHandle =
volumeHandle.child(AssetNode::outputPartVolumeName);
// grid
{
MObject gridDataObj = gridHandle.data();
MFnFloatArrayData gridDataFn(gridDataObj);
if (gridDataObj.isNull())
{
gridDataObj = gridDataFn.create();
gridHandle.setMObject(gridDataObj);
}
MFloatArray grid = gridDataFn.array();
int xres = myVolumeInfo.xLength;
int yres = myVolumeInfo.yLength;
int zres = myVolumeInfo.zLength;
int tileSize = myVolumeInfo.tileSize;
grid.setLength(xres * yres * zres);
for (unsigned int i = 0; i < grid.length(); i++)
grid[i] = 0.0f;
std::vector<float> tile;
tile.resize(tileSize * tileSize * tileSize);
HAPI_VolumeTileInfo tileInfo;
HoudiniApi::GetFirstVolumeTile(
Util::theHAPISession.get(), myNodeId, myPartId, &tileInfo);
#ifdef max
#undef max
#endif
while (tileInfo.minX != std::numeric_limits<int>::max() &&
tileInfo.minY != std::numeric_limits<int>::max() &&
tileInfo.minZ != std::numeric_limits<int>::max())
{
HoudiniApi::GetVolumeTileFloatData(Util::theHAPISession.get(), myNodeId,
myPartId, 0.0f, &tileInfo,
&tile.front(), (int)tile.size());