forked from code-google-com/stretchmesh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stretchMeshDeformer.cpp
2530 lines (2209 loc) · 102 KB
/
stretchMeshDeformer.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
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 "stretchMeshDeformer.h"
#include <string.h>
#include <vector>
#include <maya/MIOStream.h>
#include <math.h>
#include <maya/MItGeometry.h>
#include <maya/MGlobal.h>
#include <maya/MTypeId.h>
#include <maya/MDagPath.h>
#include <maya/MPlug.h>
#include <maya/MDataBlock.h>
#include <maya/MDataHandle.h>
#include <maya/MFnNumericAttribute.h>
#include <maya/MFnMessageAttribute.h>
#include <maya/MFnMatrixAttribute.h>
#include <maya/MFnCompoundAttribute.h>
#include <maya/MFnUnitAttribute.h>
#include <maya/MFnTypedAttribute.h>
#include <maya/MFnMesh.h>
#include <maya/MFnNurbsCurve.h>
#include <maya/MFnNurbsSurface.h>
#include <maya/MDistance.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MFnDoubleArrayData.h>
#include <maya/MMeshIntersector.h>
#include <maya/MTimer.h>
#include <maya/MPoint.h>
#include <maya/MPointArray.h>
#include <maya/MFloatPoint.h>
#include <maya/MFloatPointArray.h>
#include <maya/MMatrix.h>
#include <maya/MMatrixArray.h>
#include <maya/MItMeshVertex.h>
#include <maya/MPxLocatorNode.h>
#include "CRSpline.h"
#include "ksUtils.h"
#include "IncludeMFnPluginClass.h"
MTypeId stretchMeshDeformer::id( 0x00113000 );
////////////////////////
// stretchMeshDeformer attributes //
////////////////////////
MObject stretchMeshDeformer::stretchMeshVersion;
MObject stretchMeshDeformer::collisionStep;
MObject stretchMeshDeformer::iterations;
MObject stretchMeshDeformer::collisions;
MObject stretchMeshDeformer::meanWeightsList;
MObject stretchMeshDeformer::meanWeights;
MObject stretchMeshDeformer::connVrtIdList;
MObject stretchMeshDeformer::connVrtId;
MObject stretchMeshDeformer::connVrtIdNrmlOrderList;
MObject stretchMeshDeformer::connVrtIdNrmlOrder;
MObject stretchMeshDeformer::enableScaleSafe;
MObject stretchMeshDeformer::b;
MObject stretchMeshDeformer::bScalableList;
MObject stretchMeshDeformer::bScalable;
MObject stretchMeshDeformer::stiffnessList;
MObject stretchMeshDeformer::stiffness;
MObject stretchMeshDeformer::attrWorldMatrixList;
MObject stretchMeshDeformer::attrctrStrength;
MObject stretchMeshDeformer::attrctrVrtMultList;
MObject stretchMeshDeformer::attrctrVrtMult;
MObject stretchMeshDeformer::attrPaintWeights;
MObject stretchMeshDeformer::attrPaintTrans;
MObject stretchMeshDeformer::attrPaintArrDirty;
// curve attractors
MObject stretchMeshDeformer::crvAttractorCurve;
MObject stretchMeshDeformer::crvAttractorStrength;
MObject stretchMeshDeformer::crvAttractorVrtMultList;
MObject stretchMeshDeformer::crvAttractorVrtMult;
MObject stretchMeshDeformer::crvAttractorAttachUVList;
MObject stretchMeshDeformer::crvAttractorAttachUV;
// end curve attractors
MObject stretchMeshDeformer::mshCollider;
MObject stretchMeshDeformer::mshColliderPad;
MObject stretchMeshDeformer::mshColliderInflated;
MObject stretchMeshDeformer::mshColliderMult;
MObject stretchMeshDeformer::mshColliderVrtMultList;
MObject stretchMeshDeformer::mshColliderVrtMult;
MObject stretchMeshDeformer::nrbsCollider;
MObject stretchMeshDeformer::primSphrColliderList;
MObject stretchMeshDeformer::primSphrColliderMult;
MObject stretchMeshDeformer::primSphrColliderVrtMultList;
MObject stretchMeshDeformer::primSphrColliderVrtMult;
MObject stretchMeshDeformer::primCrvColliderList;
MObject stretchMeshDeformer::primCrvColliderMult;
MObject stretchMeshDeformer::crvColliderVrtMultList;
MObject stretchMeshDeformer::crvColliderVrtMult;
MObject stretchMeshDeformer::crvColliderRadiusList;
MObject stretchMeshDeformer::crvColliderRadius;
bool stretchMeshDeformer::Registered = false;
bool stretchMeshDeformer::Licensed = false;
using namespace std;
struct smConnectedVerts {
vector<int> connectedVerts;
};
struct smMeanWeights {
vector<double> meanWeights;
};
struct smBScalable {
vector<double> bScalable;
};
struct smAttractorMults {
vector<double> attractorMults;
};
struct smCrvColliderRadius {
vector<double> colliderRadius;
};
struct smCrvAttractorUV {
vector<double> attractorUV;
};
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// mWeightListCallbackId is necessary for per-vertex mult painting
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Description:
// constructor
//
stretchMeshDeformer::stretchMeshDeformer() : mWeightListCallbackId(0)
{
}
stretchMeshDeformer::~stretchMeshDeformer()
//
// Description:
// destructor
//
{}
void* stretchMeshDeformer::creator()
//
// Description:
// create the stretchMeshDeformer
//
{
return new stretchMeshDeformer();
}
MStatus stretchMeshDeformer::initialize()
//
// Description:
// initialize the attributes
//
{
// local attribute initialization
//
MFnNumericAttribute nAttr;
MFnMatrixAttribute mAttr;
MFnCompoundAttribute cmpAttr;
MFnUnitAttribute unitFn;
MFnTypedAttribute typedAttr;
MFnNumericAttribute stfAttr;
stretchMeshVersion=nAttr.create( "stretchMeshVersion", "smv", MFnNumericData::kDouble );
nAttr.setDefault(1.0);
nAttr.setKeyable(false);
nAttr.setReadable(false);
nAttr.setStorable(true);
nAttr.setHidden(true);
addAttribute( stretchMeshVersion );
collisionStep=nAttr.create( "collisionStep", "collS", MFnNumericData::kInt );
nAttr.setDefault(1);
nAttr.setMin(1);
nAttr.setKeyable(true);
addAttribute( collisionStep);
iterations=nAttr.create( "iterations", "itr", MFnNumericData::kInt );
// iterations is defaulted to 0 so that the deformer effectively doesn't run during the setup process. The stretchMeshDeformer()
// python script will set the iterations attribute after the initialization of all the relevant deformer attributes (mean_weights,
// conn_vrt_ids and b).
nAttr.setDefault(0);
nAttr.setMin(0);
nAttr.setKeyable(true);
addAttribute( iterations);
collisions = nAttr.create( "collisions", "coll", MFnNumericData::kBoolean, true );
nAttr.setKeyable(true);
addAttribute(collisions);
meanWeights=nAttr.create( "meanWeights", "mw", MFnNumericData::kDouble );
nAttr.setDefault(-1.0);
nAttr.setHidden(true);
nAttr.setKeyable(false);
nAttr.setArray(true);
nAttr.setReadable(true);
nAttr.setUsesArrayDataBuilder(true);
nAttr.setStorable(true);
addAttribute( meanWeights );
meanWeightsList=cmpAttr.create( "meanWeightsList", "mwl" );
cmpAttr.addChild(meanWeights);
cmpAttr.setHidden(true);
cmpAttr.setArray(true);
cmpAttr.setUsesArrayDataBuilder(true);
addAttribute( meanWeightsList );
connVrtId=nAttr.create( "connVrtId", "cvid", MFnNumericData::kInt );
nAttr.setDefault(-1);
nAttr.setKeyable(false);
nAttr.setArray(true);
nAttr.setReadable(true);
//nAttr.setUsesArrayDataBuilder(true);
nAttr.setStorable(true);
nAttr.setHidden(true);
addAttribute( connVrtId );
connVrtIdList=cmpAttr.create( "connVrtIdList", "cvidl" );
cmpAttr.addChild(connVrtId);
cmpAttr.setHidden(true);
cmpAttr.setArray(true);
//cmpAttr.setUsesArrayDataBuilder(true);
cmpAttr.setStorable(true);
addAttribute( connVrtIdList );
connVrtIdNrmlOrder=nAttr.create( "connVrtIdNrmlOrder", "cvidN", MFnNumericData::kInt );
nAttr.setDefault(-1);
nAttr.setKeyable(false);
nAttr.setArray(true);
nAttr.setReadable(true);
//nAttr.setUsesArrayDataBuilder(true);
nAttr.setStorable(true);
nAttr.setHidden(true);
addAttribute( connVrtIdNrmlOrder );
connVrtIdNrmlOrderList=cmpAttr.create( "connVrtIdNrmlOrderList", "cvidnl" );
cmpAttr.addChild(connVrtIdNrmlOrder);
cmpAttr.setHidden(true);
cmpAttr.setArray(true);
//cmpAttr.setUsesArrayDataBuilder(true);
cmpAttr.setStorable(true);
addAttribute( connVrtIdNrmlOrderList );
b=nAttr.create( "b", "b", MFnNumericData::kDouble );
nAttr.setDefault(-1.0);
nAttr.setKeyable(false);
nAttr.setArray(true);
nAttr.setReadable(true);
nAttr.setUsesArrayDataBuilder(true);
nAttr.setStorable(true);
nAttr.setHidden(true);
addAttribute( b );
enableScaleSafe = nAttr.create( "enableScaleSafe", "ess", MFnNumericData::kBoolean, false );
nAttr.setKeyable(true);
// nAttr.setDefault(0);
addAttribute(enableScaleSafe);
bScalable=nAttr.create( "bScalable", "bs", MFnNumericData::kDouble );
nAttr.setDefault(-1.0);
nAttr.setHidden(true);
nAttr.setKeyable(false);
nAttr.setArray(true);
nAttr.setReadable(true);
nAttr.setUsesArrayDataBuilder(true);
nAttr.setStorable(true);
addAttribute( bScalable );
bScalableList=cmpAttr.create( "bScalableList", "bsl" );
cmpAttr.addChild(bScalable);
cmpAttr.setHidden(true);
cmpAttr.setArray(true);
cmpAttr.setUsesArrayDataBuilder(true);
addAttribute( bScalableList );
stiffness=stfAttr.create( "stiffness", "stf", MFnNumericData::kFloat, 0.0);
stfAttr.setHidden(true);
stfAttr.setStorable(true);
stfAttr.setArray(true);
stfAttr.setUsesArrayDataBuilder(true);
addAttribute( stiffness );
stiffnessList=cmpAttr.create( "stiffnessList", "stfl" );
cmpAttr.addChild(stiffness);
cmpAttr.setHidden(true);
cmpAttr.setArray(true);
cmpAttr.setUsesArrayDataBuilder(true);
addAttribute( stiffnessList );
attrWorldMatrixList=mAttr.create("matrix", "ma");
mAttr.setArray(true);
mAttr.setReadable(false);
mAttr.setKeyable(false);
mAttr.setConnectable(true);
mAttr.setHidden(true);
addAttribute( attrWorldMatrixList );
attrctrStrength=nAttr.create( "attrctrStrength", "aStr", MFnNumericData::kDouble, 0.0);
nAttr.setArray(true);
nAttr.setReadable(false);
nAttr.setKeyable(false);
nAttr.setConnectable(true);
nAttr.setHidden(true);
addAttribute( attrctrStrength );
attrctrVrtMult=nAttr.create( "attrctrVrtMult", "aVMlt", MFnNumericData::kDouble, 0.0);
nAttr.setArray(true);
nAttr.setUsesArrayDataBuilder(true);
nAttr.setHidden(true);
addAttribute( attrctrVrtMult );
attrctrVrtMultList=cmpAttr.create( "attrctrVrtMultList", "aVMltL" );
cmpAttr.addChild(attrctrVrtMult);
cmpAttr.setHidden(true);
cmpAttr.setArray(true);
cmpAttr.setUsesArrayDataBuilder(true);
addAttribute( attrctrVrtMultList );
// Curve attractors
crvAttractorCurve=typedAttr.create("crvAttrctrCurve", "cACrv", MFnData::kNurbsCurve);
typedAttr.setArray(true);
typedAttr.setReadable(true);
typedAttr.setHidden(true);
addAttribute( crvAttractorCurve );
crvAttractorStrength=nAttr.create( "crvAttrctrStrength", "cAStr", MFnNumericData::kDouble, 0.0);
nAttr.setArray(true);
nAttr.setReadable(false);
nAttr.setKeyable(false);
nAttr.setConnectable(true);
addAttribute( crvAttractorStrength );
crvAttractorVrtMult=nAttr.create( "crvAttrctrVrtMult", "cAVM", MFnNumericData::kDouble, 0.0);
nAttr.setArray(true);
nAttr.setHidden(true);
nAttr.setUsesArrayDataBuilder(true);
addAttribute( crvAttractorVrtMult );
crvAttractorVrtMultList=cmpAttr.create( "crvAttrctrVrtMultList", "aVML" );
cmpAttr.addChild(crvAttractorVrtMult);
cmpAttr.setHidden(true);
cmpAttr.setArray(true);
cmpAttr.setUsesArrayDataBuilder(true);
addAttribute( crvAttractorVrtMultList );
crvAttractorAttachUV=nAttr.create( "crvAttrctrAttchUV", "cAUV", MFnNumericData::kDouble, 0.0);
nAttr.setArray(true);
nAttr.setUsesArrayDataBuilder(true);
nAttr.setHidden(true);
addAttribute( crvAttractorAttachUV );
crvAttractorAttachUVList=cmpAttr.create( "crvAttrctrAttchUVList", "cAUVL" );
cmpAttr.addChild(crvAttractorAttachUV);
cmpAttr.setHidden(true);
cmpAttr.setArray(true);
cmpAttr.setUsesArrayDataBuilder(true);
addAttribute( crvAttractorAttachUVList );
// End curve attractors
// Make sure the <kDoubleArray> attribute has a default value. Behaves weird otherwise.
MDoubleArray defaultDoubleArray;
MFnDoubleArrayData defaultDoubleArrayData;
MObject defaultDoubleArrayAttr;
defaultDoubleArrayAttr = defaultDoubleArrayData.create(defaultDoubleArray);
attrPaintWeights = typedAttr.create("paintWeights", "ptw", MFnDoubleArrayData::kDoubleArray);
typedAttr.setReadable(false);
typedAttr.setKeyable(false);
typedAttr.setConnectable(true);
typedAttr.setDefault(defaultDoubleArrayAttr);
addAttribute(attrPaintWeights);
MFnMessageAttribute msgAttr;
attrPaintTrans = msgAttr.create("paintTrans", "ptt");
msgAttr.setReadable(false);
addAttribute(attrPaintTrans);
attrPaintArrDirty = msgAttr.create("paintArrDirty", "pad");
msgAttr.setReadable(false);
addAttribute(attrPaintArrDirty);
mshCollider = typedAttr.create( "mshCollider", "mcldr", MFnData::kMesh );
typedAttr.setArray(true);
typedAttr.setReadable(true);
typedAttr.setCached(true);
addAttribute( mshCollider );
mshColliderPad = nAttr.create( "mshColliderPad", "mcldrp", MFnNumericData::kDouble );
nAttr.setDefault(0);
nAttr.setKeyable(true);
nAttr.setArray(true);
addAttribute( mshColliderPad );
mshColliderInflated = typedAttr.create( "mshColliderInflated", "mcldri", MFnData::kMesh );
typedAttr.setArray(true);
typedAttr.setReadable(true);
typedAttr.setCached(true);
typedAttr.setUsesArrayDataBuilder(true);
typedAttr.setHidden(true);
addAttribute( mshColliderInflated );
mshColliderMult=nAttr.create( "mshColliderMult", "mcm", MFnNumericData::kDouble, 0.0);
nAttr.setArray(true);
nAttr.setReadable(false);
nAttr.setKeyable(false);
nAttr.setConnectable(true);
nAttr.setHidden(true);
addAttribute( mshColliderMult );
mshColliderVrtMult=nAttr.create( "mshColliderVrtMult", "mCVM", MFnNumericData::kDouble, 0.0);
nAttr.setArray(true);
nAttr.setUsesArrayDataBuilder(true);
nAttr.setHidden(true);
addAttribute( mshColliderVrtMult );
mshColliderVrtMultList=cmpAttr.create( "mshColliderVrtMultList", "mCVML" );
cmpAttr.addChild(mshColliderVrtMult);
cmpAttr.setHidden(true);
cmpAttr.setArray(true);
cmpAttr.setUsesArrayDataBuilder(true);
addAttribute( mshColliderVrtMultList );
nrbsCollider = typedAttr.create( "nrbsCollider", "ncldr", MFnData::kNurbsSurface );
typedAttr.setArray(true);
typedAttr.setReadable(true);
typedAttr.setCached(true);
typedAttr.setHidden(true);
addAttribute( nrbsCollider );
primSphrColliderList = mAttr.create("primSphrCollider", "psc");
mAttr.setArray(true);
mAttr.setReadable(false);
mAttr.setKeyable(false);
mAttr.setConnectable(true);
mAttr.setHidden(true);
addAttribute( primSphrColliderList );
primSphrColliderMult=nAttr.create( "primSphrColliderMult", "scm", MFnNumericData::kDouble, 0.0);
nAttr.setArray(true);
nAttr.setReadable(false);
nAttr.setKeyable(false);
nAttr.setConnectable(true);
nAttr.setHidden(true);
addAttribute( primSphrColliderMult );
primSphrColliderVrtMult=nAttr.create( "primSphrColliderVrtMult", "sCVM", MFnNumericData::kDouble, 0.0);
nAttr.setArray(true);
nAttr.setUsesArrayDataBuilder(true);
nAttr.setHidden(true);
addAttribute( primSphrColliderVrtMult );
primSphrColliderVrtMultList=cmpAttr.create( "primSphrColliderVrtMultList", "sCVML" );
cmpAttr.addChild(primSphrColliderVrtMult);
cmpAttr.setHidden(true);
cmpAttr.setArray(true);
cmpAttr.setUsesArrayDataBuilder(true);
addAttribute( primSphrColliderVrtMultList );
primCrvColliderList = typedAttr.create("primCrvCollider", "pcc", MFnData::kNurbsCurve);
typedAttr.setArray(true);
typedAttr.setReadable(true);
typedAttr.setHidden(true);
addAttribute( primCrvColliderList );
primCrvColliderMult=nAttr.create( "crvColliderMult", "ccm", MFnNumericData::kDouble, 0.0);
nAttr.setArray(true);
nAttr.setReadable(false);
nAttr.setKeyable(false);
nAttr.setConnectable(true);
nAttr.setHidden(true);
addAttribute( primCrvColliderMult );
crvColliderVrtMult=nAttr.create( "crvColliderVrtMult", "cCVM", MFnNumericData::kDouble, 0.0);
nAttr.setArray(true);
nAttr.setHidden(true);
nAttr.setUsesArrayDataBuilder(true);
addAttribute( crvColliderVrtMult );
crvColliderVrtMultList=cmpAttr.create( "crvColliderVrtMultList", "cCVML" );
cmpAttr.addChild(crvColliderVrtMult);
cmpAttr.setHidden(true);
cmpAttr.setArray(true);
cmpAttr.setUsesArrayDataBuilder(true);
addAttribute( crvColliderVrtMultList );
crvColliderRadius=nAttr.create( "crvColliderRadius", "cCR", MFnNumericData::kDouble, 0.0);
nAttr.setArray(true);
nAttr.setHidden(true);
nAttr.setUsesArrayDataBuilder(true);
addAttribute( crvColliderRadius );
crvColliderRadiusList=cmpAttr.create( "crvColliderRadiusList", "cCRL" );
cmpAttr.addChild(crvColliderRadius);
cmpAttr.setHidden(true);
cmpAttr.setArray(true);
cmpAttr.setUsesArrayDataBuilder(true);
addAttribute( crvColliderRadiusList );
// affects
//
attributeAffects( stretchMeshDeformer::collisionStep, stretchMeshDeformer::outputGeom );
attributeAffects( stretchMeshDeformer::iterations, stretchMeshDeformer::outputGeom );
attributeAffects( stretchMeshDeformer::collisions, stretchMeshDeformer::outputGeom );
attributeAffects( stretchMeshDeformer::meanWeights, stretchMeshDeformer::outputGeom );
attributeAffects( stretchMeshDeformer::connVrtId, stretchMeshDeformer::outputGeom );
attributeAffects( stretchMeshDeformer::connVrtIdNrmlOrder, stretchMeshDeformer::outputGeom );
attributeAffects( stretchMeshDeformer::b, stretchMeshDeformer::outputGeom );
attributeAffects( stretchMeshDeformer::enableScaleSafe, stretchMeshDeformer::outputGeom );
attributeAffects( stretchMeshDeformer::bScalable, stretchMeshDeformer::outputGeom );
attributeAffects( stretchMeshDeformer::stiffness, stretchMeshDeformer::outputGeom );
attributeAffects( stretchMeshDeformer::attrWorldMatrixList, stretchMeshDeformer::outputGeom );
attributeAffects( stretchMeshDeformer::attrctrStrength, stretchMeshDeformer::outputGeom );
attributeAffects( stretchMeshDeformer::attrctrVrtMult, stretchMeshDeformer::outputGeom );
attributeAffects( stretchMeshDeformer::attrPaintWeights, stretchMeshDeformer::outputGeom);
attributeAffects( stretchMeshDeformer::attrPaintTrans, stretchMeshDeformer::outputGeom);
attributeAffects( stretchMeshDeformer::attrPaintArrDirty, stretchMeshDeformer::outputGeom);
attributeAffects( stretchMeshDeformer::crvAttractorCurve, stretchMeshDeformer::outputGeom);
attributeAffects( stretchMeshDeformer::crvAttractorStrength, stretchMeshDeformer::outputGeom);
attributeAffects( stretchMeshDeformer::crvAttractorVrtMult, stretchMeshDeformer::outputGeom);
attributeAffects( stretchMeshDeformer::mshCollider, stretchMeshDeformer::outputGeom );
attributeAffects( stretchMeshDeformer::mshColliderPad, stretchMeshDeformer::outputGeom );
attributeAffects( stretchMeshDeformer::mshColliderInflated, stretchMeshDeformer::outputGeom );
attributeAffects( stretchMeshDeformer::mshColliderMult, stretchMeshDeformer::outputGeom );
attributeAffects( stretchMeshDeformer::mshColliderVrtMult, stretchMeshDeformer::outputGeom );
attributeAffects( stretchMeshDeformer::mshColliderVrtMultList, stretchMeshDeformer::outputGeom);
attributeAffects( stretchMeshDeformer::nrbsCollider, stretchMeshDeformer::outputGeom );
attributeAffects( stretchMeshDeformer::primSphrColliderList, stretchMeshDeformer::outputGeom );
attributeAffects( stretchMeshDeformer::primSphrColliderMult, stretchMeshDeformer::outputGeom );
attributeAffects( stretchMeshDeformer::primSphrColliderVrtMult, stretchMeshDeformer::outputGeom );
attributeAffects( stretchMeshDeformer::primSphrColliderVrtMultList, stretchMeshDeformer::outputGeom);
attributeAffects( stretchMeshDeformer::primCrvColliderList, stretchMeshDeformer::outputGeom );
attributeAffects( stretchMeshDeformer::primCrvColliderMult, stretchMeshDeformer::outputGeom );
attributeAffects( stretchMeshDeformer::crvColliderVrtMult, stretchMeshDeformer::outputGeom);
attributeAffects( stretchMeshDeformer::crvColliderRadius, stretchMeshDeformer::outputGeom);
return MS::kSuccess;
}
void stretchMeshDeformer::postConstructor()
{
MPxDeformerNode::postConstructor();
MObject this_mobj = thisMObject();
mWeightListCallbackId = MNodeMessage::addAttributeChangedCallback(this_mobj, sAttrChangedCallback, this);
}
void stretchMeshDeformer::RemoveCallbacks()
{
if (mWeightListCallbackId!=0)
{
MNodeMessage::removeCallback(mWeightListCallbackId);
mWeightListCallbackId = 0;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
MStatus
stretchMeshDeformer::deform( MDataBlock& block,
MItGeometry& iter,
const MMatrix& inMatrix,
unsigned int multiIndex)
//
// Method: deform
//
// Description: Deform the point with a stretchMeshDeformer algorithm
//
// Arguments:
// block : the datablock of the node
// iter : an iterator for the geometry to be deformed
// m : matrix to transform the point into world space
// multiIndex : the index of the geometry that we are deforming
//
//
{
// If not licensed, pass through
if ( !Licensed )
return MStatus::kSuccess;
// do this if we are using an OpenMP implementation that is not the same as Maya's.
// Even if it is the same, it does no harm to make this call. testing svn on ubuntu
// MThreadUtils::syncNumOpenMPThreads();
MStatus status = MS::kSuccess;
MObject thisNode = thisMObject();
// determine the stretch factor
//
MDataHandle stretchMeshVersionData = block.inputValue(stretchMeshVersion,&status);
McheckErr(status, "Error getting stretchMeshVersion data handle\n");
double stretchMeshVers = stretchMeshVersionData.asDouble();
MDataHandle collisionStepData = block.inputValue(collisionStep,&status);
McheckErr(status, "Error getting collisionStep data handle\n");
int collisionStep = collisionStepData.asInt();
MDataHandle iterationsData = block.inputValue(iterations,&status);
McheckErr(status, "Error getting iterations data handle\n");
int iterations = iterationsData.asInt();
MDataHandle collisionsData;
collisionsData = block.inputValue(collisions );
bool doCollisions = collisionsData.asBool();
MDataHandle enableScaleSafeHndl;
enableScaleSafeHndl = block.inputValue(enableScaleSafe );
bool doScaleSafe = enableScaleSafeHndl.asBool();
// Get the output geometry as a polygon mesh.
// Currently, only poly meshes are supported.
MStatus outGeomStatus;
MArrayDataHandle outputData = block.outputArrayValue( outputGeom, &outGeomStatus );
MDataHandle outMesh = outputData.outputValue( &outGeomStatus );
MObject meshObj;
meshObj = outMesh.asMesh();
// Create a vertex iterator from the output mesh
MStatus stat;
MItMeshVertex vertIter(meshObj, &status);
if( !status ){
status.perror( "Stretch mesh couldn't initialize vertex iterator for output poly mesh: " );
MGlobal::displayError( "Stretch mesh couldn't initialize vertex iterator for output poly mesh: " );
return status;
}
MArrayDataHandle weightListHndl = block.inputArrayValue(meanWeightsList, &stat);
if(!stat){stat.perror("weights array handle construction failed\n");}
MArrayDataHandle connVrtListHndl = block.inputArrayValue(connVrtIdList, &stat);
if(!stat){stat.perror("connVrtIdList array handle construction failed\n");}
MArrayDataHandle connVrtNrmlOrderListHndl = block.inputArrayValue(connVrtIdNrmlOrderList, &stat);
if(!stat){stat.perror("connVrtIdList array handle construction failed\n");}
MArrayDataHandle b_hndl = block.inputArrayValue(b, &stat);
if(!stat){stat.perror("b array handle construction failed\n");}
MArrayDataHandle bScalableListHndl = block.inputArrayValue(bScalableList, &stat);
if(!stat){stat.perror("bScalable array handle construction failed\n");}
MArrayDataHandle stfListHndl = block.inputArrayValue(stiffnessList, &stat);
if(!stat){stat.perror("stiffnessList array handle construction failed\n");}
MArrayDataHandle attrctrXformsHndl = block.inputValue(attrWorldMatrixList, &stat);
if(!stat){stat.perror("attrctrXformsHndl handle construction failed\n");}
MArrayDataHandle attrctrStrengthsHndl = block.inputValue(attrctrStrength, &stat);
if(!stat){stat.perror("attrctrStrengthsHndl handle construction failed\n");}
MArrayDataHandle aVrtMultListHndl = block.inputArrayValue(attrctrVrtMultList, &stat);
if(!stat){stat.perror("attrctrVrtMultList handle construction failed\n");}
MArrayDataHandle attrctrCrvsHndl = block.inputValue(crvAttractorCurve, &stat);
if(!stat){stat.perror("attrctrCrvsHndl handle construction failed\n");}
MArrayDataHandle crvAttrctrStrengthsHndl = block.inputValue(crvAttractorStrength, &stat);
if(!stat){stat.perror("crvAttrctrStrengthsHndl handle construction failed\n");}
MArrayDataHandle cAVrtMultListHndl = block.inputArrayValue(crvAttractorVrtMultList, &stat);
if(!stat){stat.perror("attrctrVrtMultList handle construction failed\n");}
MArrayDataHandle cAttachUVListHndl = block.inputArrayValue(crvAttractorAttachUVList, &stat);
if(!stat){stat.perror("crvAttractorAttachUV handle construction failed\n");}
MArrayDataHandle mColliderArrayHandle = block.inputArrayValue(mshCollider, &stat);
if(!stat){stat.perror("Mesh collider array handle construction failed\n");}
MArrayDataHandle mColliderPadArrayHandle = block.inputArrayValue(mshColliderPad, &stat);
if(!stat){stat.perror("Mesh collider pad array handle construction failed\n");}
MArrayDataHandle mColliderInflatedArrayHandle = block.inputArrayValue(mshColliderInflated, &stat);
if(!stat){stat.perror("Mesh colliderInflated array handle construction failed\n");}
MArrayDataHandle mColliderMultHandle = block.inputValue(mshColliderMult, &stat);
if(!stat){stat.perror("mColliderMultHandle handle construction failed\n");}
MArrayDataHandle mCVrtMultListHndl = block.inputArrayValue(mshColliderVrtMultList, &stat);
if(!stat){stat.perror("mCVrtMultListHndl handle construction failed\n");}
MArrayDataHandle nColliderArrayHandle = block.inputArrayValue(nrbsCollider, &stat);
if(!stat){stat.perror("Nurbs collider array handle construction failed\n");}
MArrayDataHandle pSColliderArrayHandle = block.inputArrayValue(primSphrColliderList, &stat);
if(!stat){stat.perror("Primitive sphere collider array handle construction failed\n");}
MArrayDataHandle pSColliderMultHandle = block.inputValue(primSphrColliderMult, &stat);
if(!stat){stat.perror("pSColliderMultHandle handle construction failed\n");}
MArrayDataHandle pSVrtMultListHndl = block.inputArrayValue(primSphrColliderVrtMultList, &stat);
if(!stat){stat.perror("pSVrtMultListHndl handle construction failed\n");}
MArrayDataHandle pCColliderArrayHandle = block.inputArrayValue(primCrvColliderList, &stat);
if(!stat){stat.perror("Primitive curve collider array handle construction failed\n");}
MArrayDataHandle pCColliderMultHandle = block.inputValue(primCrvColliderMult, &stat);
if(!stat){stat.perror("pCColliderMultHandle handle construction failed\n");}
MArrayDataHandle cCVrtMultListHndl = block.inputArrayValue(crvColliderVrtMultList, &stat);
if(!stat){stat.perror("crvColliderVrtMultList handle construction failed\n");}
MArrayDataHandle cCRadiusListHndl = block.inputArrayValue(crvColliderRadiusList, &stat);
if(!stat){stat.perror("crvColliderRadiusList handle construction failed\n");}
// determine the envelope (this is a global scale factor)
//
MDataHandle envData = block.inputValue(envelope,&status);
McheckErr(status, "Error getting envelope data handle\n");
double envelope = envData.asFloat();
if(envelope == 0.0){return MS::kSuccess;}
MPlug colliderDestPlugArray(thisNode, mshCollider);
MPlug attractorStrengthPlugArray(thisNode, attrctrStrength);
MPlug crvAttractorStrengthPlugArray(thisNode, crvAttractorStrength);
MDataHandle cAttractorHandle;
MObject cAttractorObj;
MObjectArray cAttractorObjArray;
MDataHandle mColliderInflatedHandle;
MObject mClldrInfltdObj;
MPointArray mClldrPts;
MPointArray clldrMinPts;
MPointArray clldrMaxPts;
MVector mClldrNrml;
MBoundingBox colliderBoundBox;
MDataHandle nColliderHandle;
MObject nColliderObj;
MFnNurbsSurface nColliderFn;
MPlug clldrDestPlug;
MPlugArray clldrSrcPlgArray;
MObject clldrSrcObj;
MFnDagNode clldrDagNode;
MDagPath clldrDagPath;
MMatrix clldrXform;
MDataHandle pColliderHandle;
// curve colliders
MDataHandle cColliderHandle;
MObject cColliderObj;
MPlug crvColliderMultPlugArray(thisNode, primCrvColliderMult);
// mesh colliders
MDataHandle mColliderHandle;
MObject mColliderObj;
MFnMesh mColliderFn;
MPlug mshColliderMultPlugArray(thisNode, mshColliderMult);
// SPhere colliders
MPlug sphereColliderDestPlugArray(thisNode, primSphrColliderList );
MPlug sphereColliderMultPlugArray(thisNode, primSphrColliderMult);
MBoundingBox inMeshBoundBox;
MVector searchVector;
MIntArray connected_vrts;
MVector nrml;
MPoint pt;
MPoint conn_pt;
MPoint conn1;
MPoint conn2;
double searchRadius;
MFloatPoint hitPoint;
MFloatPointArray hitPoints;
MPointArray nHitPoints;
MDoubleArray intersectUArray;
MDoubleArray intersectVArray;
MDoubleArray intersectDistances;
double intersectU;
double intersectV;
bool wasExactHit;
unsigned int numHits;
MPoint closestPoint;
MFloatPoint raySource;
MVector rayDirVec;
MFloatVector rayDir;
MMeshIsectAccelParams intersectAccel;
MVector conn_pt_t[MAX_NUM_CONN_VRTS];
MVectorArray vrt_snapshot;
MVector currVertSnapshot;
MVector curr_pt_t;
MVector conn_pt_avg;
MVector inputToResult;
MVector vrt_prj_sum;
MVector prj_edge;
MIntArray conn_vrts;
unsigned int num_conn_vrts;
MPointArray verts;
MPlug crvColliderRadiusListPlug(thisNode, crvColliderRadiusList);
int conn_vrt_id;
int prev_vrt_id;
MPointArray inputPts; // The point array that represents the initial position of the points. It is used to interpolate
// with the resultPts array depending on env/weight values.
MPointArray resultPts; // The point array used throughout the deform method.
// It's the working copy of the output points ultimately used to set the point positions
inputPts.setLength(iter.count());
resultPts.setLength(iter.count());
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// INITIALIZATION //
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// The following for loop initializes several arrays that represent the array attributes of the SM node (stiffness,
// mean value weights, connected verts, etc). This is an optimization, creating MArrayDataHandle objects is
// expensive, so we do it just once, here
// Fill arrays representing the attractor strengths and xforms... optimization so we're not checking it later
int count = attrctrStrengthsHndl.elementCount();
// fill an array representing the indices of the connected attractors... optimization
vector<int> connectedAttractorIds;
connectedAttractorIds.clear();
for(int attrctrItr = 0; attrctrItr < count; attrctrItr++){
// If this attractor's xform attribute is unconnected, continue to the next attractor
MPlug attractorStrengthPlug = attractorStrengthPlugArray.elementByPhysicalIndex(attrctrItr);
if(attractorStrengthPlug.isConnected()){
connectedAttractorIds.push_back(attractorStrengthPlug.logicalIndex());
}
}
int lastElementId = -1;
if(connectedAttractorIds.size() != 0){ lastElementId = connectedAttractorIds.back(); }
vector<double> attractorStrengthArray;
attractorStrengthArray.clear();
MMatrixArray attractorXfmArray;
attractorXfmArray.clear();
for (int attrctrItr = 0; attrctrItr <= lastElementId; attrctrItr++){
status = attrctrStrengthsHndl.jumpToElement(attrctrItr);
if(status){
attractorStrengthArray.push_back(attrctrStrengthsHndl.inputValue().asDouble());
}else{
attractorStrengthArray.push_back(0.0);
}
status = attrctrXformsHndl.jumpToElement(attrctrItr);
if(status){
attractorXfmArray.append( attrctrXformsHndl.inputValue().asMatrix());
}else{
MMatrix identity;
attractorXfmArray.append(identity);
}
}
// CURVE ATTRACTOR INITIALIZATION
count = crvAttrctrStrengthsHndl.elementCount();
// fill an array representing the indices of the connected attractors... optimization
vector<int> connectedCrvAttractorIds;
connectedCrvAttractorIds.clear();
for(int attrctrItr = 0; attrctrItr < count; attrctrItr++){
// If this attractor's xform attribute is unconnected, continue to the next attractor
MPlug crvAttractorStrengthPlug = crvAttractorStrengthPlugArray.elementByPhysicalIndex(attrctrItr);
if(crvAttractorStrengthPlug.isConnected()){
connectedCrvAttractorIds.push_back(crvAttractorStrengthPlug.logicalIndex());
}
}
int lastCrvAttrctrId = -1;
if(connectedCrvAttractorIds.size() != 0){ lastCrvAttrctrId = connectedCrvAttractorIds.back(); }
vector<double> crvAttractorStrengthArray;
crvAttractorStrengthArray.clear();
for (int attrctrItr = 0; attrctrItr <= lastCrvAttrctrId; attrctrItr++){
status = crvAttrctrStrengthsHndl.jumpToElement(attrctrItr);
if(status){
crvAttractorStrengthArray.push_back(crvAttrctrStrengthsHndl.inputValue().asDouble());
}else{
crvAttractorStrengthArray.push_back(0.0);
}
}
cAttractorObjArray.clear();
for(int attrctrItr = 0; attrctrItr < connectedCrvAttractorIds.size(); attrctrItr++){
int attractorId = connectedCrvAttractorIds[attrctrItr];
attrctrCrvsHndl.jumpToArrayElement(attractorId);
cAttractorObjArray.append(attrctrCrvsHndl.inputValue().asNurbsCurveTransformed());
}
// CURVE COLLIDER INITIALIZATION
count = pCColliderArrayHandle.elementCount();
// fill an array representing the indices of the connected attractors... optimization
vector<int> connectedCrvColliderIds;
connectedCrvColliderIds.clear();
for(int colliderItr = 0; colliderItr < count; colliderItr++){
// If this attractor's mshCollider attribute is unconnected, continue to the next attractor
MPlug crvColliderPlug = crvColliderMultPlugArray.elementByPhysicalIndex(colliderItr);
if(crvColliderPlug.isConnected()){
connectedCrvColliderIds.push_back(crvColliderPlug.logicalIndex());
}
}
int lastCrvColliderId = -1;
if(connectedCrvColliderIds.size() != 0){ lastCrvColliderId = connectedCrvColliderIds.back(); }
vector<double> crvColliderMultArray;
crvColliderMultArray.clear();
for (int colliderItr = 0; colliderItr <= lastCrvColliderId; colliderItr++){
status = pCColliderMultHandle.jumpToElement(colliderItr);
if(status){
crvColliderMultArray.push_back(pCColliderMultHandle.inputValue().asDouble());
}else{
crvColliderMultArray.push_back(1.0);
}
}
// CURVE COLLIDER RADIUS INITIALIZATION
MPointArray crvColliderRadiusPts;
MPoint crvColliderRadiusPt;
crvColliderRadiusPts.clear();
vector<MPointArray> crvColliderRadiusPtsArray;
crvColliderRadiusPts.clear();
int colliderCount = cCRadiusListHndl.elementCount();
for(int colliderItr = 0; colliderItr < colliderCount; colliderItr++){
status = cCRadiusListHndl.jumpToElement(colliderItr);
if(!status){
MGlobal::displayError(MString("Could not access curve collider array element"));
continue;
}
MArrayDataHandle cCRadiusHndl = cCRadiusListHndl.inputValue(&status).child(crvColliderRadius);
int radiusCount = cCRadiusHndl.elementCount();
// iterate through the radius list, storing an array of the connected radius IDs
MPlug crvColliderRadiusPlug = crvColliderRadiusListPlug.elementByLogicalIndex(colliderItr);
crvColliderRadiusPlug = crvColliderRadiusPlug.child(0);
vector<int> connectedRadiusIds;
for(int radiusPlugItr = 0; radiusPlugItr < crvColliderRadiusPlug.numElements(); radiusPlugItr++){
if(crvColliderRadiusPlug[radiusPlugItr].isConnected()){
connectedRadiusIds.push_back(radiusPlugItr);
}
}
crvColliderRadiusPts.clear();
crvColliderRadiusPts.setLength(connectedRadiusIds.size());
for (int radiusItr = 0; radiusItr < connectedRadiusIds.size(); radiusItr++){
status = cCRadiusHndl.jumpToElement(connectedRadiusIds[radiusItr]);
if(!status){
MGlobal::displayError(MString("Could not access curve collider radius array element"));
continue;
}
crvColliderRadiusPt.x = cCRadiusHndl.inputValue().asDouble(); crvColliderRadiusPt.y = 0.0; crvColliderRadiusPt.z = 0.0;
crvColliderRadiusPts[radiusItr] = crvColliderRadiusPt;
}
crvColliderRadiusPtsArray.push_back(crvColliderRadiusPts);
}
// MESH COLLIDER INITIALIZATION (for per-vert mult)
count = mColliderArrayHandle.elementCount();
// fill an array representing the indices of the connected colliders... optimization
vector<int> connectedMshColliderIds;
connectedMshColliderIds.clear();
for(int colliderItr = 0; colliderItr < count; colliderItr++){
// If this colliders's xform attribute is unconnected, continue to the next attractor
MPlug mshColliderPlug = colliderDestPlugArray.elementByPhysicalIndex(colliderItr);
if(mshColliderPlug.isConnected()){
connectedMshColliderIds.push_back(mshColliderPlug.logicalIndex());
}
}
int lastMshColliderId = -1;
if(connectedMshColliderIds.size() != 0){ lastMshColliderId = connectedMshColliderIds.back(); }
vector<double> mshColliderMultArray;
mshColliderMultArray.clear();
for (int colliderItr = 0; colliderItr <= lastMshColliderId; colliderItr++){
status = mColliderMultHandle.jumpToElement(colliderItr);
if(status){
mshColliderMultArray.push_back(mColliderMultHandle.inputValue().asDouble());
}else{
mshColliderMultArray.push_back(1.0);
}
}
// SHERE COLLIDER INITIALIZATION (for per-vert mult)
count = pSColliderArrayHandle.elementCount();
// fill an array representing the indices of the connected sphere colliders... optimization
vector<int> connectedSphereColliderIds;
connectedSphereColliderIds.clear();
for(int colliderItr = 0; colliderItr < count; colliderItr++){
// If this colliders's xform attribute is unconnected, continue to the next attractor
MPlug sphereColliderPlug = sphereColliderDestPlugArray.elementByPhysicalIndex(colliderItr);
if(sphereColliderPlug.isConnected()){
connectedSphereColliderIds.push_back(sphereColliderPlug.logicalIndex());
}
}
int lastSphereColliderId = -1;
if(connectedSphereColliderIds.size() != 0){ lastSphereColliderId = connectedSphereColliderIds.back(); }
vector<double> sphereColliderMultArray;
sphereColliderMultArray.clear();
for (int colliderItr = 0; colliderItr <= lastSphereColliderId; colliderItr++){
status = pSColliderMultHandle.jumpToElement(colliderItr);
if(status){
sphereColliderMultArray.push_back(pSColliderMultHandle.inputValue().asDouble());
}else{
sphereColliderMultArray.push_back(1.0);
}
}
// Initialize variables containing the connected sphere primitive collider matrices
// int spherePrimCount = pSColliderArrayHandle.elementCount();
vector<smConnectedVerts> connectedVertArray;
connectedVertArray.clear();
smConnectedVerts currVert;
vector<smConnectedVerts> connectedVertArrayNrmlOrder;
connectedVertArrayNrmlOrder.clear();
smConnectedVerts currVertNrmlOrder;
vector<float> stiffnessArray;
stiffnessArray.clear();