-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapiMeshShape.cpp
More file actions
2620 lines (2340 loc) · 72.9 KB
/
apiMeshShape.cpp
File metadata and controls
2620 lines (2340 loc) · 72.9 KB
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
//-
// ==========================================================================
// Copyright 2015 Autodesk, Inc. All rights reserved.
//
// Use of this software is subject to the terms of the Autodesk
// license agreement provided at the time of installation or download,
// or which otherwise accompanies this software in either electronic
// or hard copy form.
// ==========================================================================
//+
///////////////////////////////////////////////////////////////////////////////
//
// This plug-in produces the shape node "apiMesh", dependency graph node "apiMeshCreator", and data type "apiMeshData".
//
// It demonstrates how to create a polygonal mesh shape with vertices that can be selected, moved, animated, and deformed.
// This shape also supports OpenGL display of materials.
//
// This plug-in also registers a new kind of geometry data called "apiMeshData", and demonstrates how to pass this data between nodes.
//
// The "apiMeshCreator" node can create two types of "apiMeshData": cube and sphere.
// The "shapeType" attribute is used to specify the type of shape to create.
// This node also takes normal mesh data as an input and converts it to "apiMeshData".
// If there is no input mesh, the output is based on the shapeType attribute.
//
// To create an "apiMesh" shape, you must first create the "apiMesh" node, then create an "apiMeshCreator" node, and connect the two nodes as follows:
//
// createNode apiMesh -n m1;
// createNode apiMeshCreator -n c1;
// connectAttr c1.outputSurface m1.inputSurface;
//
////////////////////////////////////////////////////////////////////////////////
#include <math.h>
#include <maya/MIOStream.h>
#include "apiMeshShape.h"
#include "apiMeshShapeUI.h"
#include "apiMeshGeometryOverride.h"
#include "apiMeshSubSceneOverride.h"
#include "apiMeshCreator.h"
#include "apiMeshData.h"
#include <api_macros.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MFnPlugin.h>
#include <maya/MFnPluginData.h>
#include <maya/MDrawRegistry.h>
#include <maya/MMatrix.h>
#include <maya/MAttributeSpecArray.h>
#include <maya/MAttributeSpec.h>
#include <maya/MAttributeIndex.h>
#include <maya/MObjectArray.h>
#include <maya/MFnSingleIndexedComponent.h>
#include <maya/MDagPath.h>
#include <maya/MFnAttribute.h>
#include <maya/MFnNumericAttribute.h>
#include <maya/MFnTypedAttribute.h>
#include <maya/MPointArray.h>
#include <maya/MViewport2Renderer.h>
#include <maya/MPlane.h>
#include <maya/MArrayDataBuilder.h>
namespace
{
bool debug = false;
const char* sPlugConnection = "->-";
// If there is any connection change to the shading group, we should mark the
// material dirty in order to tell the subscene override to bind the shader again.
void MaterialDirtyCb(MNodeMessage::AttributeMessage msg,
MPlug &plug,
MPlug &otherPlug,
void *clientData)
{
apiMesh *pMesh = static_cast<apiMesh *>(clientData);
if (pMesh && (msg & (MNodeMessage::kConnectionMade|MNodeMessage::kConnectionBroken)))
{
pMesh->setMaterialDirty(true);
}
}
}
////////////////////////////////////////////////////////////////////////////////
//
// Shape implementation
//
////////////////////////////////////////////////////////////////////////////////
MObject apiMesh::inputSurface;
MObject apiMesh::outputSurface;
MObject apiMesh::cachedSurface;
MObject apiMesh::worldSurface;
MObject apiMesh::bboxCorner1;
MObject apiMesh::bboxCorner2;
MObject apiMesh::useWeightedTransformUsingFunction;
MObject apiMesh::useWeightedTweakUsingFunction;
MTypeId apiMesh::id( 0x80099 );
apiMesh::apiMesh() {}
apiMesh::~apiMesh()
{
for(std::map<std::string, MCallbackId>::const_iterator i = fMaterialDirtyCbIds.begin();
i != fMaterialDirtyCbIds.end(); i++)
{
MMessage::removeCallback(i->second);
}
}
///////////////////////////////////////////////////////////////////////////////
//
// Overrides
//
///////////////////////////////////////////////////////////////////////////////
/* override */
void apiMesh::postConstructor()
//
// Description
//
// When instances of this node are created internally, the MObject associated
// with the instance is not created until after the constructor of this class
// is called. This means that no member functions of MPxSurfaceShape can
// be called in the constructor.
// The postConstructor solves this problem. Maya will call this function
// after the internal object has been created.
// As a general rule do all of your initialization in the postConstructor.
//
{
// This call allows the shape to have shading groups assigned
//
setRenderable( true );
// Is there input history to this node
//
fHasHistoryOnCreate = false;
// Used by VP2.0 sub-scene evaluator
//
fShapeDirty = true;
fMaterialDirty = true;
}
/* override */
MStatus apiMesh::compute( const MPlug& plug, MDataBlock& datablock )
//
// Description
//
// When input attributes are dirty this method will be called to
// recompute the output attributes.
//
// Arguments
//
// plug - the attribute that triggered the compute
// datablock - the nodes data
//
// Returns
//
// kSuccess - this method could compute the dirty attribute,
// kUnknownParameter - the dirty attribute can not be handled at this level
//
{
if (debug)
cerr << "apiMesh::compute : plug " << plug.info() << endl;
if ( plug == outputSurface ) {
return computeOutputSurface( plug, datablock );
}
else if ( plug == cachedSurface ) {
return computeOutputSurface( plug, datablock );
}
else if ( plug == worldSurface ) {
return computeWorldSurface( plug, datablock );
}
else {
return MS::kUnknownParameter;
}
}
/* override */
//
// Description
//
// Horribly abuse the purpose of this method to notify the Viewport 2.0
// renderer that something about this shape has changed and that it should
// be retranslated.
//
MStatus apiMesh::setDependentsDirty( const MPlug& plug, MPlugArray& plugArray)
{
// if the dirty attribute is the output mesh then we need to signal the
// the renderer that it needs to update the object
if ( plug == inputSurface ||
plug == mControlPoints ||
plug == mControlValueX ||
plug == mControlValueY ||
plug == mControlValueZ )
{
signalDirtyToViewport();
}
return MS::kSuccess;
}
/* override */
CDA3
//
// Description
//
// Handle internal attributes.
//
// Attributes that require special storage, bounds checking,
// or other non-standard behavior can be marked as "Internal" by
// using the "MFnAttribute::setInternal" method.
//
// The get/setInternalValue methods will get called for internal
// attributes whenever the attribute values are stored or retrieved
// using getAttr/setAttr or MPlug getValue/setValue.
//
// The inherited attribute mControlPoints is internal and we want
// its values to get stored only if there is input history. Otherwise
// any changes to the vertices are stored in the cachedMesh and outputMesh
// directly.
//
// If values are retrieved then we want the controlPoints value
// returned if there is history, this will be the offset or tweak.
// In the case of no history, the vertex position of the cached mesh
// is returned.
//
bool apiMesh::getInternalValue( const MPlug& plug, MDataHandle& result )
{
bool isOk = true;
if( (plug == mControlPoints) ||
(plug == mControlValueX) ||
(plug == mControlValueY) ||
(plug == mControlValueZ) )
{
// If there is input history then the control point value is
// directly returned. This is the tweak or offset that
// was applied to the vertex.
//
// If there is no input history then return the actual vertex
// position and ignore the controlPoints attribute.
//
if ( hasHistory() ) {
return MPxNode::getInternalValue( plug, result );
}
else {
double val = 0.0;
if ( (plug == mControlPoints) && !plug.isArray() ) {
MPoint pnt;
int index = plug.logicalIndex();
value( index, pnt );
result.set( pnt[0], pnt[1], pnt[2] );
}
else if ( plug == mControlValueX ) {
MPlug parentPlug = plug.parent();
int index = parentPlug.logicalIndex();
value( index, 0, val );
result.set( val );
}
else if ( plug == mControlValueY ) {
MPlug parentPlug = plug.parent();
int index = parentPlug.logicalIndex();
value( index, 1, val );
result.set( val );
}
else if ( plug == mControlValueZ ) {
MPlug parentPlug = plug.parent();
int index = parentPlug.logicalIndex();
value( index, 2, val );
result.set( val );
}
}
}
// This inherited attribute is used to specify whether or
// not this shape has history. During a file read, the shape
// is created before any input history can get connected.
// This attribute, also called "tweaks", provides a way to
// for the shape to determine if there is input history
// during file reads.
//
else if ( plug == mHasHistoryOnCreate ) {
result.set( fHasHistoryOnCreate );
}
else {
isOk = MPxSurfaceShape::getInternalValue( plug, result );
}
return isOk;
}
/* override */
//
// Description
//
// Handle internal attributes.
//
// Attributes that require special storage, bounds checking,
// or other non-standard behavior can be marked as "Internal" by
// using the "MFnAttribute::setInternal" method.
//
// The get/setInternalValue methods will get called for internal
// attributes whenever the attribute values are stored or retrieved
// using getAttr/setAttr or MPlug getValue/setValue.
//
// The inherited attribute mControlPoints is internal and we want
// its values to get stored only if there is input history. Otherwise
// any changes to the vertices are stored in the cachedMesh and outputMesh
// directly.
//
// If values are retrieved then we want the controlPoints value
// returned if there is history, this will be the offset or tweak.
// In the case of no history, the vertex position of the cached mesh
// is returned.
//
bool apiMesh::setInternalValue( const MPlug& plug, const MDataHandle& handle )
{
bool isOk = true;
if( (plug == mControlPoints) ||
(plug == mControlValueX) ||
(plug == mControlValueY) ||
(plug == mControlValueZ) )
{
// If there is input history then set the control points value
// using the normal mechanism. In this case we are setting
// the tweak or offset that will get applied to the input
// history.
//
// If there is no input history then ignore the controlPoints
// attribute and set the vertex position directly in the
// cachedMesh.
//
if ( hasHistory() ) {
verticesUpdated();
return MPxNode::setInternalValue( plug, handle );
}
else {
if( plug == mControlPoints && !plug.isArray()) {
int index = plug.logicalIndex();
MPoint point;
double3& ptData = handle.asDouble3();
point.x = ptData[0];
point.y = ptData[1];
point.z = ptData[2];
setValue( index, point );
}
else if( plug == mControlValueX ) {
MPlug parentPlug = plug.parent();
int index = parentPlug.logicalIndex();
setValue( index, 0, handle.asDouble() );
}
else if( plug == mControlValueY ) {
MPlug parentPlug = plug.parent();
int index = parentPlug.logicalIndex();
setValue( index, 1, handle.asDouble() );
}
else if( plug == mControlValueZ ) {
MPlug parentPlug = plug.parent();
int index = parentPlug.logicalIndex();
setValue( index, 2, handle.asDouble() );
}
}
}
// This inherited attribute is used to specify whether or
// not this shape has history. During a file read, the shape
// is created before any input history can get connected.
// This attribute, also called "tweaks", provides a way to
// for the shape to determine if there is input history
// during file reads.
//
else if ( plug == mHasHistoryOnCreate ) {
fHasHistoryOnCreate = handle.asBool();
}
else {
isOk = MPxSurfaceShape::setInternalValue( plug, handle );
}
return isOk;
}
/* override */
MStatus apiMesh::connectionMade( const MPlug& plug,
const MPlug& otherPlug,
bool asSrc )
//
// Description
//
// Whenever a connection is made to this node, this method
// will get called.
//
{
if ( plug == inputSurface )
{
MStatus stat;
MObject thisObj = thisMObject();
MPlug historyPlug( thisObj, mHasHistoryOnCreate );
stat = historyPlug.setValue( true );
MCHECKERROR( stat, "connectionMade: setValue(mHasHistoryOnCreate)" );
}
else if ( asSrc )
{
MObject otherNode = otherPlug.node();
if (otherNode.hasFn(MFn::kShadingEngine) &&
(plug.attribute() == instObjGroups || plug.attribute() == objectGroups) )
{
setMaterialDirty(true);
MStatus stat;
MCallbackId cbId = MNodeMessage::addAttributeChangedCallback(
otherNode, MaterialDirtyCb, this, &stat);
if (stat)
{
std::string k = plug.name().asChar();
k += sPlugConnection;
k += otherPlug.name().asChar();
#ifdef _DEBUG
cout << "apiMesh::connectionMade: " << k << endl;
#endif
fMaterialDirtyCbIds[k] = cbId;
}
}
}
return MPxNode::connectionMade( plug, otherPlug, asSrc );
}
/* override */
MStatus apiMesh::connectionBroken( const MPlug& plug,
const MPlug& otherPlug,
bool asSrc )
//
// Description
//
// Whenever a connection to this node is broken, this method
// will get called.
//
{
if ( plug == inputSurface )
{
MStatus stat;
MObject thisObj = thisMObject();
MPlug historyPlug( thisObj, mHasHistoryOnCreate );
stat = historyPlug.setValue( false );
MCHECKERROR( stat, "connectionBroken: setValue(mHasHistoryOnCreate)" );
}
else if (asSrc && otherPlug.node().hasFn(MFn::kShadingEngine) &&
(plug.attribute() == instObjGroups || plug.attribute() == objectGroups))
{
setMaterialDirty(true);
std::string k = plug.name().asChar();
k += sPlugConnection;
k += otherPlug.name().asChar();
#ifdef _DEBUG
cout << "apiMesh::connectionBroken: " << k << endl;
#endif
if (fMaterialDirtyCbIds.find(k) != fMaterialDirtyCbIds.end())
{
MMessage::removeCallback(fMaterialDirtyCbIds[k]);
fMaterialDirtyCbIds.erase(k);
}
}
return MPxNode::connectionBroken( plug, otherPlug, asSrc );
}
/* override */
MStatus apiMesh::shouldSave( const MPlug& plug, bool& result )
//
// Description
//
// During file save this method is called to determine which
// attributes of this node should get written. The default behavior
// is to only save attributes whose values differ from the default.
<
CDA3
/div>//
//
//
{
MStatus status = MS::kSuccess;
if( plug == mControlPoints || plug == mControlValueX ||
plug == mControlValueY || plug == mControlValueZ )
{
if( hasHistory() ) {
// Calling this will only write tweaks if they are
// different than the default value.
//
status = MPxNode::shouldSave( plug, result );
}
else {
result = false;
}
}
else if ( plug == cachedSurface ) {
if ( hasHistory() ) {
result = false;
}
else {
MObject data;
status = plug.getValue( data );
MCHECKERROR( status, "shouldSave: MPlug::getValue" );
result = ( ! data.isNull() );
}
}
else {
status = MPxNode::shouldSave( plug, result );
}
return status;
}
/* override */
void apiMesh::componentToPlugs( MObject & component,
MSelectionList & list ) const
//
// Description
//
// Converts the given component values into a selection list of plugs.
// This method is used to map components to attributes.
//
// Arguments
//
// component - the component to be translated to a plug/attribute
// list - a list of plugs representing the passed in component
//
{
if ( component.hasFn(MFn::kSingleIndexedComponent) ) {
apiMesh* nonConstPtr = (apiMesh*)this;
MObject vtxComp = nonConstPtr->convertToVertexComponent(component);
MFnSingleIndexedComponent fnVtxComp( vtxComp );
MObject thisNode = thisMObject();
MPlug plug( thisNode, mControlPoints );
// If this node is connected to a tweak node, reset the
// plug to point at the tweak node.
//
convertToTweakNodePlug(plug);
int len = fnVtxComp.elementCount();
for ( int i = 0; i < len; i++ )
{
plug.selectAncestorLogicalIndex(fnVtxComp.element(i),
plug.attribute());
list.add(plug);
}
}
}
/* override */
MPxSurfaceShape::MatchResult
apiMesh::matchComponent( const MSelectionList& item,
const MAttributeSpecArray& spec,
MSelectionList& list )
//
// Description:
//
// Component/attribute matching method.
// This method validates component names and indices which are
// specified as a string and adds the corresponding component
// to the passed in selection list.
//
// For instance, select commands such as "select shape1.vtx[0:7]"
// are validated with this method and the corresponding component
// is added to the selection list.
//
// Arguments
//
// item - DAG selection item for the object being matched
// spec - attribute specification object
// list - list to add components to
//
// Returns
//
// the result of the match
//
{
MPxSurfaceShape::MatchResult result = MPxSurfaceShape::kMatchOk;
MAttributeSpec attrSpec = spec[0];
int dim = attrSpec.dimensions();
// Look for attributes specifications of the form :
// vtx[ index ]
// vtx[ lower:upper ]
//
if ( (1 == spec.length()) && (dim > 0) && (attrSpec.name() == "vtx") ) {
int numVertices = meshGeom()->vertices.length();
MAttributeIndex attrIndex = attrSpec[0];
int upper = 0;
int lower = 0;
if ( attrIndex.hasLowerBound() ) {
attrIndex.getLower( lower );
}
if ( attrIndex.hasUpperBound() ) {
attrIndex.getUpper( upper );
}
// Check the attribute index range is valid
//
if ( (lower > upper) || (upper >= numVertices) ) {
result = MPxSurfaceShape::kMatchInvalidAttributeRange;
}
else {
MDagPath path;
item.getDagPath( 0, path );
MFnSingleIndexedComponent fnVtxComp;
MObject vtxComp = fnVtxComp.create( MFn::kMeshVertComponent );
for ( int i=lower; i<=upper; i++ )
{
fnVtxComp.addElement( i );
}
list.add( path, vtxComp );
}
}
else {
// Pass this to the parent class
return MPxSurfaceShape::matchComponent( item, spec, list );
}
return result;
}
/* override */
bool apiMesh::match( const MSelectionMask & mask,
const MObjectArray& componentList ) const
//
// Description:
//
// Check for matches between selection type / component list, and
// the type of this shape / or it's components
//
// This is used by sets and deformers to make sure that the selected
// components fall into the "vertex only" category.
//
// Arguments
//
// mask - selection type mask
// componentList - possible component list
//
// Returns
// true if matched any
//
{
bool result = false;
if( componentList.length() == 0 ) {
result = mask.intersects( MSelectionMask::kSelectMeshes );
}
else {
for ( int i=0; i<(int)componentList.length(); i++ ) {
if ( (componentList[i].apiType() == MFn::kMeshVertComponent) &&
(mask.intersects(MSelectionMask::kSelectMeshVerts))
) {
result = true;
break;
}
}
}
return result;
}
/* override */
MSelectionMask apiMesh::getShapeSelectionMask() const
//
// Description
// This method is overriden to support interactive object selection in Viewport 2.0
//
// Returns
//
// The selection mask of the shape
//
{
MSelectionMask::SelectionType selType = MSelectionMask::kSelectMeshes;
return MSelectionMask( selType );
}
/* override */
MSelectionMask apiMesh::getComponentSelectionMask() const
//
// Description
// This method is overriden to support interactive component selection in Viewport 2.0
//
// Returns
//
// The selection mask of the shape components
//
{
MSelectionMask retVal(MSelectionMask::kSelectMeshVerts);
retVal.addMask(MSelectionMask::kSelectMeshEdges);
retVal.addMask(MSelectionMask::kSelectMeshFaces);
return retVal;
}
/* override */
MObject apiMesh::createFullVertexGroup() const
//
// Description
// This method is used by maya when it needs to create a component
// containing every vertex (or control point) in the shape.
// This will get called if you apply some deformer to the whole
// shape, i.e. select the shape in object mode and add a deformer to it.
//
// Returns
//
// A "complete" component representing all vertices in the shape.
//
{
// Create a vertex component
//
MFnSingleIndexedComponent fnComponent;
MObject fullComponent = fnComponent.create( MFn::kMeshVertComponent );
// Set the component to be complete, i.e. the elements in
// the component will be [0:numVertices-1]
//
int numVertices = ((apiMesh*)this)->meshGeom()->vertices.length();
fnComponent.setCompleteData( numVertices );
return fullComponent;
}
/* override */
MObject apiMesh::localShapeInAttr() const
//
// Description
//
// Returns the input attribute of the shape. This is used by
// maya to establish input connections for deformers etc.
// This attribute must be data of type kGeometryData.
//
// Returns
//
// input attribute for the shape
//
{
return inputSurface;
}
/* override */
MObject apiMesh::localShapeOutAttr() const
//
// Description
//
// Returns the output attribute of the shape. This is used by
// maya to establish out connections for deformers etc.
// This attribute must be data of tye kGeometryData.
//
// Returns
//
// output attribute for the shape
CDA3
//
//
{
return outputSurface;
}
/* override */
MObject apiMesh::worldShapeOutAttr() const
//
// Description
//
// Returns the world space output "array" attribute of the shape.
// This is used by maya to establish out connections for deformers etc.
// This attribute must be an array attribute, each element representing
// a particular instance of the shape.
// This attribute must be data of type kGeometryData.
//
// Returns
//
// world space "array" attribute for the shape
//
{
return worldSurface;
}
/* override */
MObject apiMesh::cachedShapeAttr() const
//
// Description
//
// Returns the cached shape attribute of the shape.
// This attribute must be data of type kGeometryData.
//
// Returns
//
// cached shape attribute
//
{
return cachedSurface;
}
/* override */
MObject apiMesh::geometryData() const
//
// Description
//
// Returns the data object for the surface. This gets
// called internally for grouping (set) information.
//
{
apiMesh* nonConstThis = (apiMesh*)this;
MDataBlock datablock = nonConstThis->forceCache();
MDataHandle handle = datablock.inputValue( inputSurface );
return handle.data();
}
/*override */
void apiMesh:: closestPoint ( const MPoint & toThisPoint, \
MPoint & theClosestPoint, double tolerance ) const
//
// Description
//
// Returns the closest point to the given point in space.
// Used for rigid bind of skin. Currently returns wrong results;
// override it by implementing a closest point calculation.
{
// Iterate through the geometry to find the closest point within
// the given tolerance.
//
apiMeshGeom* geomPtr = ((apiMesh*)this)->meshGeom();
int numVertices = geomPtr->vertices.length();
for (int ii=0; ii<numVertices; ii++)
{
MPoint tryThisOne = geomPtr->vertices[ii];
}
// Set the output point to the result (hardcode for debug just now)
//
theClosestPoint = geomPtr->vertices[0];
}
/* override */
void apiMesh::transformUsing( const MMatrix & mat,
const MObjectArray & componentList )
//
// Description
//
// Transforms by the matrix the given components, or the entire shape
// if the componentList is empty. This method is used by the freezeTransforms command.
//
// Arguments
//
// mat - matrix to tranform the components by
// componentList - list of components to be transformed,
// or an empty list to indicate the whole surface
//
{
// Let the other version of transformUsing do the work for us.
//
transformUsing( mat,
componentList,
MPxSurfaceShape::kNoPointCaching,
NULL);
}
//
// Description
//
// Transforms the given components. This method is used by
// the move, rotate, and scale tools in component mode.
// The bounding box has to be updated here, so do the normals and
// any other attributes that depend on vertex positions.
//
// Arguments
// mat - matrix to tranform the components by
// componentList - list of components to be transformed,
// or an empty list to indicate the whole surface
// cachingMode - how to use the supplied pointCache (kSavePoints, kRestorePoints)
// pointCache - if non-null, save or restore points from this list base
// on the cachingMode
//
void apiMesh::transformUsing( const MMatrix & mat,
const MObjectArray & componentList,
MVertexCachingMode cachingMode,
MPointArray* pointCache)
{
MStatus stat;
apiMeshGeom* geomPtr = meshGeom();
// Create cachingMode boolean values for clearer reading of conditional code below
//
bool savePoints = (cachingMode == MPxSurfaceShape::kSavePoints);
bool restorePoints = (cachingMode == MPxSurfaceShape::kRestorePoints);
unsigned int i=0,j=0;
unsigned int len = componentList.length();
if ( restorePoints ) {
// restore the points based on the data provided in the pointCache attribute
//
unsigned int cacheLen = pointCache->length();
if (len > 0) {
// traverse the component list
//
for ( i = 0; i < len && j < cacheLen; i++ )
{
MObject comp = convertToVertexComponent(componentList[i]);
MFnSingleIndexedComponent fnComp( comp );
int elemCount = fnComp.elementCount();
for ( int idx=0; idx<elemCount && j < cacheLen; idx++, ++j ) {
int elemIndex = fnComp.element( idx );
geomPtr->vertices[elemIndex] = (*pointCache)[j];
}
}
} else {
// if the component list is of zero-length, it indicates that we
// should transform the entire surface
//
len = geomPtr->vertices.length();
for ( unsigned int idx = 0; idx < len && j < cacheLen; ++idx, ++j ) {
geomPtr->vertices[idx] = (*pointCache)[j];
}
}
} else {
// Transform the surface vertices with the matrix.
// If savePoints is true, save the points to the pointCache.
//
if (len > 0) {
// Traverse the componentList
//
for ( i=0; i<len; i++ )
{
MObject comp = convertToVertexComponent(componentList[i]);
MFnSingleIndexedComponent fnComp( comp );
int elemCount = fnComp.elementCount();
if (savePoints && 0 == i) {
pointCache->setSizeIncrement(elemCount);
}
for ( int idx=0; idx<elemCount; idx++ )
{
int elemIndex = fnComp.element( idx );
if (savePoints) {
pointCache->append(geomPtr->vertices[elemIndex]);
}
geomPtr->vertices[elemIndex] *= mat;
geomPtr->normals[idx] =
geomPtr->normals[idx].transformAsNormal( mat );
}
}
} else {
// If the component list is of zero-length, it indicates that we
// should transform the entire surface
//
len = geomPtr->vertices.length();
if (savePoints) {
pointCache->setSizeIncrement(len);
}
for ( unsigned int idx = 0; idx < len; ++idx ) {
if (savePoints) {
pointCache->append(geomPtr->vertices[idx]);
}
geomPtr->vertices[idx] *= mat;
geomPtr->normals[idx] =
geomPtr->normals[idx].transformAsNormal( mat );
}
}
}
// Update the surface
updateCachedSurface( geomPtr, componentList );
}
//
// Description
//
// Update the cached surface attribute, handle the tweak history as appropriate,
// and trigger a bounding box change calculation.
//
// Arguments
// geomPtr - the modified geometry to apply to the cached surface attribute
//
void apiMesh::updateCachedSurface( const apiMeshGeom* geomPtr, const MObjectArray & componentList )
{
MStatus stat;
unsigned int len = componentList.length();
// Retrieve the value of the cached surface attribute.
// We will set the new geometry data into the cached surface attribute
//
// Access the datablock directly. This code has to be efficient
// and so we bypass the compute mechanism completely.
// NOTE: In general we should always go though compute for getting
// and setting attributes.
//
MDataBlock datablock = forceCache();
You can’t perform that action at this time.
1032