forked from sideeffects/HoudiniEngineForMaya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsset.C
1585 lines (1375 loc) · 48.8 KB
/
Asset.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/MFnCompoundAttribute.h>
#include <maya/MFnNumericAttribute.h>
#include <maya/MFnTypedAttribute.h>
#include <maya/MFnGenericAttribute.h>
#include <maya/MFnEnumAttribute.h>
#include <maya/MFnMesh.h>
#include <maya/MFnIntArrayData.h>
#include <maya/MItMeshPolygon.h>
#include <maya/MArrayDataBuilder.h>
#include <maya/MDataHandle.h>
#include <maya/MTime.h>
#include <maya/MGlobal.h>
#include "Asset.h"
#include "Input.h"
#include "AssetNode.h"
#include "OutputGeometryObject.h"
#include "OutputInstancerObject.h"
#include "util.h"
#include <cassert>
class AttrOperation : public Util::WalkParmOperation
{
public:
enum Mode{
Get,
Set
};
AttrOperation(
MDataBlock &dataBlock,
const Mode &mode,
const MFnDependencyNode &nodeFn,
const HAPI_NodeInfo &nodeInfo,
const std::vector<MObject>* attrs
);
~AttrOperation();
virtual void pushFolder(const HAPI_ParmInfo &parmInfo);
virtual void popFolder();
virtual void pushMultiparm(const HAPI_ParmInfo &parmInfo);
virtual void nextMultiparm();
virtual void popMultiparm();
bool containsParm(
const HAPI_ParmInfo &parm,
bool multiSize = false
) const;
protected:
std::vector<MDataHandle> myDataHandles;
std::vector<MPlug> myPlugs;
std::vector<bool> myExists;
std::vector<bool> myIsMulti;
std::vector<MDataHandle> myMultiSizeDataHandles;
std::vector<MPlug> myMultiSizePlugs;
std::vector<bool> myHasMultiAttr;
std::vector<MArrayDataHandle> myMultiDataHandles;
std::vector<MPlug> myMultiPlugs;
std::vector<int> myMultiLogicalIndices;
MDataBlock &myDataBlock;
const Mode myMode;
const MFnDependencyNode &myNodeFn;
const HAPI_NodeInfo &myNodeInfo;
const std::vector<MObject>* myAttrs;
};
AttrOperation::AttrOperation(
MDataBlock &dataBlock,
const Mode &mode,
const MFnDependencyNode &nodeFn,
const HAPI_NodeInfo &nodeInfo,
const std::vector<MObject>* attrs
) :
myDataBlock(dataBlock),
myMode(mode),
myNodeFn(nodeFn),
myNodeInfo(nodeInfo),
myAttrs(attrs)
{
MDataHandle dataHandle;
MPlug plug;
plug = myNodeFn.findPlug(Util::getParmAttrPrefix());
if(myMode == AttrOperation::Get)
{
dataHandle = myDataBlock.outputValue(plug);
}
else if(myMode == AttrOperation::Set)
{
dataHandle = myDataBlock.inputValue(plug);
}
myDataHandles.push_back(dataHandle);
myPlugs.push_back(plug);
myExists.push_back(true);
}
AttrOperation::~AttrOperation()
{
myDataHandles.pop_back();
myPlugs.pop_back();
myExists.pop_back();
}
void
AttrOperation::pushFolder(const HAPI_ParmInfo &parmInfo)
{
MDataHandle dataHandle;
MPlug plug;
bool exists = false;
MDataHandle &parentDataHandle = myDataHandles.back();
MPlug &parentPlug = myPlugs.back();
bool parentExists = myExists.back();
if(parentExists)
{
MString folderAttrName = Util::getAttrNameFromParm(parmInfo);
MObject folderAttrObj = myNodeFn.attribute(folderAttrName);
if(!folderAttrObj.isNull())
{
exists = true;
plug = parentPlug.child(folderAttrObj);
// When we got the parentDataHandle, Maya doesn't actually evaluate
// the child compound attributes. So if we get the child dataHandle
// with child(), it could still be dirty. Workaround it by calling
// MDataBlock's inputValue()/outputValue(). Unfortunately, this
// still doesn't work for the case where attributes are under a
// multi attribute.
if(myIsMulti.size() == 0)
{
if(myMode == AttrOperation::Get)
{
dataHandle = myDataBlock.outputValue(plug);
}
else if(myMode == AttrOperation::Set)
{
dataHandle = myDataBlock.inputValue(plug);
}
}
else
{
dataHandle = parentDataHandle.child(folderAttrObj);
}
}
}
myDataHandles.push_back(dataHandle);
myPlugs.push_back(plug);
myExists.push_back(exists);
}
void
AttrOperation::popFolder()
{
//MDataHandle &dataHandle = myDataHandles.back();
//MPlug &plug = myPlugs.back();
//bool exists = myExists.back();
myDataHandles.pop_back();
myPlugs.pop_back();
myExists.pop_back();
}
void
AttrOperation::pushMultiparm(const HAPI_ParmInfo &parmInfo)
{
MStatus status;
// In a multiparm context, these variables are used to store the element
// attribute.
MDataHandle dataHandle;
MPlug plug;
bool exists = false;
// These parameters are pushed only for multiparms, and not created for the
// other parameters (e.g. folders and leaf).
// Since MArrayDataHandle doesn't have a default constructor, we construct
// an invalid MArrayDataHandle this way.
bool isMulti = false;
MDataHandle multiSizeDataHandle;
MPlug multiSizePlug;
bool hasMultiAttr = false;
MArrayDataHandle multiDataHandle(dataHandle);
MPlug multiPlug;
int multiLogicalIndex = -1;
MDataHandle &parentDataHandle = myDataHandles.back();
MPlug &parentPlug = myPlugs.back();
bool parentExists = myExists.back();
if(parentExists)
{
MString multiAttrName = Util::getAttrNameFromParm(parmInfo);
MObject multiSizeAttrObj = myNodeFn.attribute(multiAttrName + "__multiSize");
MObject multiAttrObj = myNodeFn.attribute(multiAttrName);
if(!multiSizeAttrObj.isNull())
{
multiSizeDataHandle = parentDataHandle.child(multiSizeAttrObj);
multiSizePlug = parentPlug.child(multiSizeAttrObj);
isMulti = true;
}
// multiAttrObj might not exist if the current instanceCount is 0
if(isMulti && !multiAttrObj.isNull())
{
multiDataHandle = MArrayDataHandle(parentDataHandle.child(multiAttrObj));
multiPlug = parentPlug.child(multiAttrObj);
hasMultiAttr = true;
}
}
myIsMulti.push_back(isMulti);
myMultiSizeDataHandles.push_back(multiSizeDataHandle);
myMultiSizePlugs.push_back(multiSizePlug);
myHasMultiAttr.push_back(hasMultiAttr);
myMultiDataHandles.push_back(multiDataHandle);
myMultiPlugs.push_back(multiPlug);
myMultiLogicalIndices.push_back(multiLogicalIndex);
myDataHandles.push_back(dataHandle);
myPlugs.push_back(plug);
myExists.push_back(exists);
// initializes the first element
nextMultiparm();
}
void
AttrOperation::nextMultiparm()
{
MStatus status;
MDataHandle &dataHandle = myDataHandles.back();
MPlug &plug = myPlugs.back();
bool exists = myExists.back();
//bool isMulti = myIsMulti.back();
//MDataHandle &multiSizeDataHandle = myMultiSizeDataHandles.back();
//MPlug &multiSizePlug = myMultiSizePlugs.back();
bool hasMultiAttr = myHasMultiAttr.back();
MArrayDataHandle &multiDataHandle = myMultiDataHandles.back();
MPlug &multiPlug = myMultiPlugs.back();
int &multiLogicalIndex = myMultiLogicalIndices.back();
if(hasMultiAttr)
{
multiLogicalIndex++;
status = multiDataHandle.jumpToElement(multiLogicalIndex);
exists = status;
if(exists)
{
if(myMode == AttrOperation::Get)
{
dataHandle = multiDataHandle.outputValue();
}
else if(myMode == AttrOperation::Set)
{
dataHandle = multiDataHandle.inputValue();
}
plug = multiPlug.elementByLogicalIndex(multiLogicalIndex);
}
}
myExists.back() = exists;
}
void
AttrOperation::popMultiparm()
{
//MDataHandle &dataHandle = myDataHandles.back();
//MPlug &plug = myPlugs.back();
//bool exists = myExists.back();
//bool isMulti = myIsMulti.back();
//MDataHandle &multiSizeDataHandle = myMultiSizeDataHandles.back();
//MPlug &multiSizePlug = myMultiSizePlugs.back();
//bool hasMultiAttr = myHasMultiAttr.back();
//MArrayDataHandle &multiDataHandle = myMultiDataHandles.back();
//MPlug &multiPlug = myMultiPlugs.back();
//int &multiLogicalIndex = myMultiLogicalIndices.back();
myIsMulti.pop_back();
myMultiSizeDataHandles.pop_back();
myMultiSizePlugs.pop_back();
myHasMultiAttr.pop_back();
myMultiDataHandles.pop_back();
myMultiPlugs.pop_back();
myMultiLogicalIndices.pop_back();
myDataHandles.pop_back();
myPlugs.pop_back();
myExists.pop_back();
}
bool
AttrOperation::containsParm(
const HAPI_ParmInfo &parm,
bool multiSize
) const
{
if(!myAttrs)
{
return true;
}
MString attrName = Util::getAttrNameFromParm(parm);
if(multiSize)
{
attrName += "__multiSize";
}
MPlug parmPlug = myNodeFn.findPlug(attrName);
for(std::vector<MObject>::const_iterator iter = myAttrs->begin();
iter != myAttrs->end();
iter++)
{
MPlug plug = myNodeFn.findPlug(*iter);
if(parmPlug == plug)
{
return true;
}
// If the parm is a tuple, then we also need to check the parent plug.
// We need to check if it's int, float, or string, because non-values
// like folders also use parm.size.
if((HAPI_ParmInfo_IsInt(&parm) || HAPI_ParmInfo_IsFloat(&parm) || HAPI_ParmInfo_IsString(&parm))
&& parm.size > 1
&& plug.isChild() && parmPlug == plug.parent())
{
return true;
}
}
return false;
}
Asset::Asset(
const MString &otlFilePath,
const MString &assetName,
const MObject &node
) :
// initialize values here because instantiating the asset could error out
myNode(node),
myAssetInputs(NULL)
{
HAPI_Result hapiResult = HAPI_RESULT_SUCCESS;
HAPI_AssetInfo_Init(&myAssetInfo);
// load the otl
int libraryId = -1;
hapiResult = HAPI_LoadAssetLibraryFromFile(otlFilePath.asChar(), true, &libraryId);
if(HAPI_FAIL(hapiResult))
{
DISPLAY_WARNING("Could not load OTL file: ^1s\n"
"Attempting to instantiate asset anyway.",
otlFilePath);
DISPLAY_WARNING_HAPI_STATUS_CALL();
}
// get the list of assets in the otl
std::vector<HAPI_StringHandle> assetNamesSH;
if(libraryId >= 0)
{
int assetCount = 0;
hapiResult = HAPI_GetAvailableAssetCount(libraryId, &assetCount);
CHECK_HAPI(hapiResult);
assetNamesSH.resize(assetCount);
hapiResult = HAPI_GetAvailableAssets(libraryId, &assetNamesSH.front(), assetCount);
CHECK_HAPI(hapiResult);
}
// find the asset in the otl
if(assetNamesSH.size())
{
bool foundAsset = false;
for(unsigned int i = 0; i < assetNamesSH.size(); i++)
{
if(Util::getString(assetNamesSH[i]) == assetName)
{
foundAsset = true;
}
}
if(!foundAsset)
{
DISPLAY_WARNING("Could not find asset: ^1s\n"
"in OTL file: ^2s\n"
"Attempting to instantiate asset anyway.",
assetName,
otlFilePath);
}
}
// instantiate the asset
int assetId = -1;
{
Util::PythonInterpreterLock pythonInterpreterLock;
hapiResult = HAPI_InstantiateAsset(
assetName.asChar(),
false,
&assetId
);
if(HAPI_FAIL(hapiResult))
{
DISPLAY_ERROR("Could not instantiate asset: ^1s\n"
"in OTL file: ^2s\n",
assetName,
otlFilePath);
GET_HAPI_STATUS_CALL();
DISPLAY_ERROR(hapiStatus);
return;
}
if(!Util::statusCheckLoop())
{
DISPLAY_ERROR("Could not instantiate asset: ^1s\n"
"in OTL file: ^2s\n",
assetName,
otlFilePath);
GET_HAPI_STATUS_COOK();
DISPLAY_ERROR(hapiStatus);
// Do nothing else if the asset is invalid.
return;
}
}
hapiResult = HAPI_GetAssetInfo(assetId, &myAssetInfo);
CHECK_HAPI(hapiResult);
hapiResult = HAPI_GetNodeInfo(myAssetInfo.nodeId, & myNodeInfo);
CHECK_HAPI(hapiResult);
// Warn the user if the OTL path is not what was originally requested.
if(Util::getString(myAssetInfo.filePathSH) != otlFilePath)
{
DISPLAY_WARNING("The asset: ^1s\n"
"was instantiated from: ^2s\n"
"but the expected path was: ^3s",
assetName,
Util::getString(myAssetInfo.filePathSH),
otlFilePath
);
}
myAssetInputs = new Inputs(myAssetInfo.id);
myAssetInputs->setNumInputs(myAssetInfo.geoInputCount);
}
Asset::~Asset()
{
HAPI_Result hstat = HAPI_RESULT_SUCCESS;
for(OutputObjects::const_iterator iter = myObjects.begin();
iter != myObjects.end(); iter++)
{
delete *iter;
}
myObjects.clear();
myObjectInfos.clear();
delete myAssetInputs;
if(myAssetInfo.id >= 0)
{
hstat = HAPI_DestroyAsset(myAssetInfo.id);
Util::checkHAPIStatus(hstat);
}
}
bool
Asset::isValid() const
{
return myAssetInfo.id >= 0;
}
MString
Asset::getOTLFilePath() const
{
if(!isValid())
{
return MString();
}
return Util::getString(myAssetInfo.filePathSH);
}
MString
Asset::getAssetName() const
{
if(!isValid())
{
return MString();
}
return Util::getString(myAssetInfo.fullOpNameSH);
}
OutputObject*
Asset::findObjectByName(MString name)
{
assert(myAssetInfo.id >= 0);
for(int i=0; i< myAssetInfo.objectCount; i++)
{
if(myObjects[i]->getName() == name)
return myObjects[i];
}
return NULL;
}
OutputObject*
Asset::findObjectById(int id)
{
return myObjects[id];
}
// Getters for infos
HAPI_ObjectInfo
Asset::getObjectInfo(int id) { return myObjectInfos[id]; }
void
Asset::resetSimulation()
{
assert(myAssetInfo.id >= 0);
HAPI_ResetSimulation(myAssetInfo.id);
}
MString
Asset::getCookMessages()
{
Util::PythonInterpreterLock pythonInterpreterLock;
// Trigger a cook so that the asset will become the "last cooked asset",
// because HAPI_STATUS_COOK_RESULT only consider the "last cooked asset".
// In most cases, this shouldn't do any actual cooking.
HAPI_CookAsset(myAssetInfo.id, NULL);
GET_HAPI_STATUS_COOK();
return hapiStatus;
}
void
Asset::update()
{
assert(myAssetInfo.id >= 0);
// update object infos
myObjectInfos.resize(myAssetInfo.objectCount);
HAPI_GetObjects(
myAssetInfo.id,
&myObjectInfos.front(),
0, myAssetInfo.objectCount
);
// Create the OutputObjects
if((int) myObjects.size() != myAssetInfo.objectCount)
{
for(OutputObjects::const_iterator iter = myObjects.begin();
iter != myObjects.end(); iter++)
{
delete *iter;
}
myObjects.resize(myAssetInfo.objectCount);
for(unsigned int i = 0; i < myObjects.size(); i++)
{
myObjects[i] = OutputObject::createObject(
myAssetInfo.id,
i,
myObjectInfos[i]
);
}
}
// Pass the ObjectInfo to the OutputObjects
for(unsigned int i = 0; i < myObjects.size(); i++)
{
myObjects[i]->setObjectInfo(myObjectInfos[i]);
}
}
void
Asset::computeInstancerObjects(
const MPlug& plug,
MDataBlock& data,
bool &needToSyncOutputs
)
{
MStatus stat;
MPlug instancersPlug = plug.child(AssetNode::outputInstancers);
int instancerIndex = 0;
MArrayDataHandle instancersHandle = data.outputArrayValue(instancersPlug);
MArrayDataBuilder instancersBuilder = instancersHandle.builder();
MIntArray instancedObjIds;
for(OutputObjects::const_iterator iter = myObjects.begin();
iter != myObjects.end(); iter++)
{
OutputObject* obj = *iter;
//MPlug instancerElemPlug = instancersPlug.elementByLogicalIndex(instancerIndex);
if(obj->type() == OutputObject::OBJECT_TYPE_INSTANCER)
{
MDataHandle instancerElemHandle = instancersBuilder.addElement(instancerIndex);
stat = obj->compute(
myTime,
instancerElemHandle,
needToSyncOutputs
);
if(MS::kSuccess == stat)
{
instancerIndex++;
// get all the object ids that are instanced
MIntArray instIds = dynamic_cast< OutputInstancerObject* >(obj)->getInstancedObjIds();
MStringArray instNames = dynamic_cast< OutputInstancerObject* >(obj)->getUniqueInstObjNames();
for(unsigned int j = 0; j < instNames.length(); ++j)
{
OutputObject* o = findObjectByName(instNames[j]);
if(o != NULL)
instancedObjIds.append(o->getId());
}
for(unsigned int j = 0; j < instIds.length(); ++j)
{
instancedObjIds.append(instIds[j]);
}
}
}
}
// clean up extra elements
int instBuilderSizeCheck = instancersBuilder.elementCount();
if(instBuilderSizeCheck > instancerIndex)
{
for(int i=instancerIndex; i<instBuilderSizeCheck; i++)
{
instancersBuilder.removeElement(i);
}
}
instancersHandle.set(instancersBuilder);
// mark instanced objects
for(unsigned int i = 0; i < instancedObjIds.length(); ++i)
{
OutputObject* obj = myObjects[instancedObjIds[i]];
obj->myIsInstanced = true;
}
instancersHandle.setAllClean();
data.setClean(instancersPlug);
}
void
Asset::computeGeometryObjects(
const MPlug& plug,
MDataBlock& data,
bool &needToSyncOutputs
)
{
MStatus stat;
MPlug objectsPlug = plug.child(AssetNode::outputObjects);
MArrayDataHandle objectsHandle = data.outputArrayValue(objectsPlug);
MArrayDataBuilder objectsBuilder = objectsHandle.builder();
if(objectsBuilder.elementCount() != myObjects.size())
{
needToSyncOutputs = true;
}
for(unsigned int i = 0; i < myObjects.size(); i++)
{
OutputObject * obj = myObjects[i];
MDataHandle objectHandle = objectsBuilder.addElement(i);
if(obj->type() == OutputObject::OBJECT_TYPE_GEOMETRY)
{
obj->compute(
myTime,
objectHandle,
needToSyncOutputs
);
}
}
// clean up extra elements
// in case the number of objects shrinks
unsigned int objBuilderSizeCheck = objectsBuilder.elementCount();
if(objBuilderSizeCheck > myObjects.size())
{
for(unsigned int i = myObjects.size(); i < objBuilderSizeCheck; i++)
{
stat = objectsBuilder.removeElement(i);
CHECK_MSTATUS(stat);
}
}
objectsHandle.set(objectsBuilder);
objectsHandle.setAllClean();
data.setClean(objectsPlug);
}
MTime
Asset::getTime() const
{
return myTime;
}
void
Asset::setTime(const MTime &mayaTime)
{
myTime = mayaTime;
// Houdini's "frame 1" is "0 seconds", but Maya's "frame 0" is "0 seconds".
// So we need to offset the time by 1.
MTime hapiTime = myTime - MTime(1, MTime::uiUnit());
float hapiTimeSeconds = (float)hapiTime.as(MTime::kSeconds);
HAPI_SetTime(hapiTimeSeconds);
}
void
Asset::setInputs(const MPlug& plug, MDataBlock& data)
{
assert(myAssetInfo.id >= 0);
MStatus status;
MPlug inputsPlug(myNode, AssetNode::input);
myAssetInputs->compute(data);
for(int i=0; i< myAssetInfo.geoInputCount; i++)
{
MPlug inputPlug = inputsPlug.elementByLogicalIndex(i, &status);
CHECK_MSTATUS(status);
myAssetInputs->setInput(i, data, inputPlug);
}
}
MStatus
Asset::compute(
const MPlug& plug,
MDataBlock& data,
bool splitGeosByGroup,
bool cookTemplatedGeos,
bool &needToSyncOutputs
)
{
assert(myAssetInfo.id >= 0);
MStatus stat(MS::kSuccess);
{
Util::PythonInterpreterLock pythonInterpreterLock;
HAPI_CookOptions cookOptions;
HAPI_CookOptions_Init(&cookOptions);
cookOptions.splitGeosByGroup = splitGeosByGroup;
cookOptions.cookTemplatedGeos = cookTemplatedGeos;
HAPI_CookAsset(myAssetInfo.id, &cookOptions);
if(!Util::statusCheckLoop())
{
GET_HAPI_STATUS_COOK();
DISPLAY_MSG(displayError, hapiStatus);
return MStatus::kFailure;
}
}
update();
// first pass - instancers
// There is a reason that instancers are computed first.
// computeInstancerObjects will mark instanced geometry objects as
// instanced. In computeGeometryObjects, each object will check
// if it is instanced or not, and will compute an output or not
// depending on whether it is instanced and whether it is visible
computeInstancerObjects(plug, data, needToSyncOutputs);
// second pass - geometry objects
computeGeometryObjects(plug, data, needToSyncOutputs);
return stat;
}
class GetMultiparmLengthOperation : public AttrOperation
{
public:
GetMultiparmLengthOperation(
MDataBlock &dataBlock,
const MFnDependencyNode &nodeFn,
const HAPI_NodeInfo &nodeInfo,
const std::vector<MObject>* attrs
);
virtual void pushMultiparm(const HAPI_ParmInfo &parmInfo);
};
GetMultiparmLengthOperation::GetMultiparmLengthOperation(
MDataBlock &dataBlock,
const MFnDependencyNode &nodeFn,
const HAPI_NodeInfo &nodeInfo,
const std::vector<MObject>* attrs
) :
AttrOperation(
dataBlock,
AttrOperation::Get,
nodeFn,
nodeInfo,
attrs
)
{
}
void
GetMultiparmLengthOperation::pushMultiparm(const HAPI_ParmInfo &parmInfo)
{
MStatus status;
AttrOperation::pushMultiparm(parmInfo);
//MDataHandle &dataHandle = myDataHandles.back();
//MPlug &plug = myPlugs.back();
//bool exists = myExists.back();
bool isMulti = myIsMulti.back();
//MDataHandle &multiSizeDataHandle = myMultiSizeDataHandles.back();
//MPlug &multiSizePlug = myMultiSizePlugs.back();
bool hasMultiAttr = myHasMultiAttr.back();
MArrayDataHandle &multiDataHandle = myMultiDataHandles.back();
//MPlug &multiPlug = myMultiPlugs.back();
//int &multiLogicalIndex = myMultiLogicalIndices.back();
if(isMulti && containsParm(parmInfo, true))
{
int multiSize = parmInfo.instanceCount;
if(hasMultiAttr)
{
MArrayDataBuilder builder = multiDataHandle.builder(&status);
CHECK_MSTATUS(status);
const int builderCount = builder.elementCount();
// If the builder has less elements than the multiparm, then we need to
// add to the builder.
for(int i = builderCount; i < multiSize; ++i)
{
builder.addElement(i);
}
// If the builder has more elements than the multiparm, then we need to
// remove from the builder.
for(int i = builderCount; i-- > multiSize;)
{
builder.removeElement(i);
}
multiDataHandle.set(builder);
}
}
}
class GetAttrOperation : public AttrOperation
{
public:
GetAttrOperation(
MDataBlock &dataBlock,
const MFnDependencyNode &nodeFn,
const HAPI_NodeInfo &nodeInfo,
const std::vector<MObject>* attrs
);
virtual void pushMultiparm(const HAPI_ParmInfo &parmInfo);
virtual void leaf(const HAPI_ParmInfo &parmInfo);
};
GetAttrOperation::GetAttrOperation(
MDataBlock &dataBlock,
const MFnDependencyNode &nodeFn,
const HAPI_NodeInfo &nodeInfo,
const std::vector<MObject>* attrs
) :
AttrOperation(
dataBlock,
AttrOperation::Get,
nodeFn,
nodeInfo,
attrs
)
{
}
void
GetAttrOperation::pushMultiparm(const HAPI_ParmInfo &parmInfo)
{
MStatus status;
AttrOperation::pushMultiparm(parmInfo);
//MDataHandle &dataHandle = myDataHandles.back();
//MPlug &plug = myPlugs.back();
//bool exists = myExists.back();
bool isMulti = myIsMulti.back();
MDataHandle &multiSizeDataHandle = myMultiSizeDataHandles.back();
//MPlug &multiSizePlug = myMultiSizePlugs.back();
//bool hasMultiAttr = myHasMultiAttr.back();
//MArrayDataHandle &multiDataHandle = myMultiDataHandles.back();
//MPlug &multiPlug = myMultiPlugs.back();
//int &multiLogicalIndex = myMultiLogicalIndices.back();
if(isMulti && containsParm(parmInfo, true))
{
int multiSize = parmInfo.instanceCount;
multiSizeDataHandle.setInt(multiSize);
}
}
void
GetAttrOperation::leaf(const HAPI_ParmInfo &parmInfo)
{
MDataHandle dataHandle;
MPlug plug;
bool exists = false;
MDataHandle &parentDataHandle = myDataHandles.back();
MPlug &parentPlug = myPlugs.back();
bool parentExists = myExists.back();
if(parentExists && containsParm(parmInfo))
{
MString attrName = Util::getAttrNameFromParm(parmInfo);
MObject attrObj = myNodeFn.attribute(attrName);
if(!attrObj.isNull())
{
exists = true;
}
if(exists)
{
dataHandle = parentDataHandle.child(attrObj);
plug = parentPlug.child(attrObj);
if((parmInfo.type == HAPI_PARMTYPE_INT
|| parmInfo.type == HAPI_PARMTYPE_BUTTON
|| parmInfo.type == HAPI_PARMTYPE_STRING)
&& parmInfo.choiceCount > 0)
{
int enumIndex = 0;
if(parmInfo.type == HAPI_PARMTYPE_BUTTON)
{
// The value of button menu items is irrelevant. We need it
// to always stay at the first dummy field regardless of
// what the actual value is.
enumIndex = 0;
}
else if(parmInfo.type == HAPI_PARMTYPE_STRING)
{
int value;
HAPI_GetParmStringValues(
myNodeInfo.id,
false,
&value,
parmInfo.stringValuesIndex, parmInfo.size
);
MString valueString = Util::getString(value);
HAPI_ParmChoiceInfo * choiceInfos =
new HAPI_ParmChoiceInfo[parmInfo.choiceCount];
HAPI_GetParmChoiceLists(