forked from sideeffects/HoudiniEngineForMaya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.h
757 lines (668 loc) · 20.7 KB
/
util.h
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
#ifndef __util_h__
#define __util_h__
#include <maya/MComputation.h>
#include <maya/MDGModifier.h>
#include <maya/MFnDagNode.h>
#include <maya/MGlobal.h>
#include <maya/MObject.h>
#include <maya/MPlug.h>
#include <maya/MPlugArray.h>
#include <maya/MString.h>
#include <maya/MTimer.h>
#include <maya/MIntArray.h>
#include <cassert>
#include <vector>
#include <memory>
#include <string>
#include <iosfwd>
#include <stdio.h>
#include <errno.h>
#include <memory>
#ifdef _WIN32
#include <direct.h>
#endif
#include <HAPI/HAPI.h>
#include "traits.h"
#include "types.h"
#define DISPLAY_MSG(displayMethod, ...) \
{ \
MString msg; \
msg.format(__VA_ARGS__); \
/* Workaround MString::format() bug when there are no positional
arguments at all */ \
if(!msg.length()) \
{ \
msg.format("^1s", __VA_ARGS__); \
} \
MGlobal::displayMethod(msg); \
}
#define DISPLAY_ERROR(...) \
DISPLAY_MSG(displayError, __VA_ARGS__)
#define DISPLAY_WARNING(...) \
DISPLAY_MSG(displayWarning, __VA_ARGS__)
#define DISPLAY_INFO(...) \
DISPLAY_MSG(displayInfo, __VA_ARGS__)
#define HAPI_FAIL(r) \
((r) != HAPI_RESULT_SUCCESS)
#define GET_HAPI_STATUS_TYPE(status_type, verbosity) \
std::vector<char> _hapiStatusBuffer; \
{ \
int bufferLength; \
HAPI_GetStatusStringBufLength( \
Util::theHAPISession.get(), \
(status_type), (verbosity), &bufferLength \
); \
_hapiStatusBuffer.resize(bufferLength); \
HAPI_GetStatusString( \
Util::theHAPISession.get(), \
(status_type), \
&_hapiStatusBuffer.front(), \
bufferLength \
); \
} \
const char * hapiStatus = &_hapiStatusBuffer.front();
#define GET_HAPI_STATUS_CALL() \
GET_HAPI_STATUS_TYPE(HAPI_STATUS_CALL_RESULT, HAPI_STATUSVERBOSITY_ERRORS)
#define DISPLAY_ERROR_HAPI_STATUS_CALL() \
DISPLAY_HAPI_STATUS_CALL(displayError)
#define DISPLAY_WARNING_HAPI_STATUS_CALL() \
DISPLAY_HAPI_STATUS_CALL(displayWarning)
#define DISPLAY_INFO_HAPI_STATUS_CALL() \
DISPLAY_HAPI_STATUS_CALL(displayInfo)
#define DISPLAY_HAPI_STATUS_CALL(displayMethod) \
{ \
GET_HAPI_STATUS_CALL(); \
DISPLAY_MSG(displayMethod, hapiStatus); \
}
#define GET_HAPI_STATUS_COOK() \
GET_HAPI_STATUS_TYPE(HAPI_STATUS_COOK_RESULT, HAPI_STATUSVERBOSITY_MESSAGES)
#define CHECK_HAPI_AND_RETURN(r, returnValue) \
CHECK_HAPI_AND(r, return returnValue;)
#define CHECK_HAPI(r) \
CHECK_HAPI_AND(r, )
#define CHECK_HAPI_AND(r, footer) \
{ \
HAPI_Result _hapi_result = (r); \
if(HAPI_FAIL(_hapi_result)) \
{ \
std::cerr << "HAPI error in " __FILE__ " at line " << __LINE__ << std::endl; \
\
GET_HAPI_STATUS_CALL(); \
\
std::cerr << hapiStatus << std::endl; \
\
footer \
} \
}
#ifdef _WIN32
#define PATH_SEPARATOR "\\"
#else
#define PATH_SEPARATOR "/"
#endif
namespace Util
{
class HAPISession : public HAPI_Session
{
public:
HAPISession()
{
type = HAPI_SESSION_MAX;
id = 0;
}
~HAPISession()
{
if ( type != HAPI_SESSION_MAX )
{
HAPI_CloseSession( this );
}
}
};
extern std::unique_ptr<HAPISession> theHAPISession;
#ifdef _WIN32
bool mkpath(const std::string &path);
#else
bool mkpath(const std::string &path, mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
#endif
extern const char* pathSeparator;
std::string getTempDir();
void displayInfoForNode(
const MString &typeName,
const MString &message
);
void displayWarningForNode(
const MString &typeName,
const MString &message
);
void displayErrorForNode(
const MString &typeName,
const MString &message
);
class HAPIString
{
public:
HAPIString(int handle) :
myHandle(handle)
{
int bufLen;
HAPI_GetStringBufLength(
theHAPISession.get(),
myHandle,
&bufLen
);
if(bufLen == 0)
{
return;
}
myString.resize(bufLen - 1);
HAPI_GetString(
theHAPISession.get(),
myHandle,
&myString[0],
myString.size() + 1
);
}
operator std::string() const
{
return myString;
}
operator MString() const
{
MString mayaString;
mayaString.setUTF8(myString.c_str());
return mayaString;
}
template<typename T>
bool operator ==(const T o) const
{
return static_cast<T>(*this) == o;
}
template<typename T>
bool operator !=(const T o) const
{
return !(*this == o);
}
private:
int myHandle;
std::string myString;
};
template<>
inline bool HAPIString::operator ==<const char*>(const char * const o) const
{
return static_cast<std::string>(*this) == o;
}
MString getAttrNameFromParm(const HAPI_ParmInfo &parm);
MString getAttrNameFromParm(
const HAPI_ParmInfo &parm,
const HAPI_ParmInfo *parentParm
);
MString getParmAttrPrefix();
bool hasHAPICallFailed(HAPI_Result stat);
inline MString
getAttrLayerName(const char *name, int layer)
{
MString layerName = name;
if(layer > 0)
{
layerName += layer + 1;
}
return layerName;
}
bool startsWith(const MString &str, const MString &begin);
bool endsWith(const MString &str, const MString &end);
MString escapeString(const MString &str);
class PythonInterpreterLock
{
public:
PythonInterpreterLock();
~PythonInterpreterLock();
};
class ProgressBar
{
public:
ProgressBar(double waitTimeBeforeShowing = 1.0);
virtual ~ProgressBar();
void beginProgress();
void updateProgress(
int progress,
int maxProgress,
const MString &status
);
void endProgress();
bool isInterrupted();
protected:
bool isShowing() const;
double elapsedTime();
MString elapsedTimeString();
virtual void showProgress();
virtual void displayProgress(
int progress,
int maxProgress,
const MString &status
);
virtual void hideProgress();
virtual bool checkInterrupted();
private:
double myWaitTimeBeforeShowing;
bool myIsShowing;
MTimer myTimer;
};
class MainProgressBar : public ProgressBar
{
public:
MainProgressBar(double waitTimeBeforeShowing = 1.0);
virtual ~MainProgressBar();
protected:
virtual void showProgress();
virtual void displayProgress(
int progress,
int maxProgress,
const MString &status
);
virtual void hideProgress();
virtual bool checkInterrupted();
};
class LogProgressBar : public ProgressBar
{
public:
LogProgressBar(
double timeBetweenLog = 2.0,
double waitTimeBeforeShowing = 1.0
);
virtual ~LogProgressBar();
protected:
virtual void showProgress();
virtual void displayProgress(
int progress,
int maxProgress,
const MString &status
);
virtual void hideProgress();
virtual bool checkInterrupted();
private:
double myTimeBetweenLog;
double myLastPrintedTime;
MComputation myComputation;
};
bool statusCheckLoop(bool wantMainProgressBar = true);
MString getNodeName(const MObject &nodeObj);
MObject findNodeByName(const MString &name, MFn::Type expectedFn = MFn::kInvalid);
MObject findDagChild(const MFnDagNode &dag, const MString &name);
MStatus createNodeByModifierCommand(
MDGModifier &dgModifier,
const MString &command,
MObject &object,
unsigned int index = 0
);
MString replaceString(const MString &str, const MString &searchStr, const MString &newChar);
MString sanitizeStringForNodeName(const MString &str);
// Returns true if the parm was found.
int findParm(std::vector<HAPI_ParmInfo>& parms, MString name, int instanceNum = -1);
class WalkParmOperation
{
public:
WalkParmOperation();
virtual ~WalkParmOperation();
virtual void pushFolder(const HAPI_ParmInfo &parmInfo);
virtual void popFolder();
virtual void pushMultiparm(const HAPI_ParmInfo &parmInfo);
virtual void nextMultiparm();
virtual void popMultiparm();
virtual void leaf(const HAPI_ParmInfo &parmInfo);
private:
// This class is not copyable so these are not implemented
WalkParmOperation(const WalkParmOperation &);
WalkParmOperation& operator=(const WalkParmOperation &);
};
void walkParm(const std::vector<HAPI_ParmInfo> &parmInfos, WalkParmOperation &operation);
template<typename T, typename U>
T convert(const U &src)
{
return src;
}
// std::string owns the returned char*
template<>
inline const char*
convert(const std::string &src)
{
return src.c_str();
}
// MString owns the returned char*
template<>
inline const char*
convert(const MString &src)
{
return src.asChar();
}
// Convert HAPI string
template<>
inline std::string
convert(const int &src)
{
return HAPIString(src);
}
// Convert HAPI string
template<>
inline MString
convert(const int &src)
{
return HAPIString(src);
}
template<typename T, typename U>
void convertArray(T &dstArray, const U &srcArray)
{
typedef ARRAYTRAIT(T) DstTrait;
typedef ELEMENTTYPE(T) DstElementType;
typedef ARRAYTRAIT(U) SrcTrait;
DstTrait::resize(dstArray, SrcTrait::size(srcArray));
for(size_t i = 0; i < DstTrait::size(dstArray); i++)
{
DstTrait::getElement(dstArray, i)
= convert<DstElementType>(SrcTrait::getElement(srcArray, i));
}
}
template<typename T, typename U>
T convertArray(const U &srcArray)
{
T dstArray;
convertArray(dstArray, srcArray);
return dstArray;
}
template<typename T>
void zeroArray(T &array)
{
typedef ELEMENTTYPE(T) ElementType;
std::fill(
arrayBegin<T>(array),
arrayEnd<T>(array),
ElementType()
);
}
template<
size_t NumComponents,
size_t DstStartComponent,
size_t DstStride,
size_t SrcStartComponent,
size_t SrcStride,
typename DstType,
typename SrcType
>
DstType reshapeArray(const SrcType &srcArray)
{
DstType dstArray;
typedef ARRAYTRAIT(DstType) DstTrait;
typedef ELEMENTTRAIT(DstType) DstElementTrait;
typedef ARRAYTRAIT(SrcType) SrcTrait;
typedef ELEMENTTRAIT(SrcType) SrcElementTrait;
DstTrait::resize(
dstArray,
SrcTrait::size(srcArray)
* SrcElementTrait::numComponents
* DstStride
/ SrcStride
/ DstElementTrait::numComponents
);
std::copy(
componentBegin<
SrcStartComponent,
NumComponents,
SrcStride
>(srcArray),
componentEnd<
SrcStartComponent,
NumComponents,
SrcStride
>(srcArray),
componentBegin<
DstStartComponent,
NumComponents,
DstStride
>(dstArray)
);
return dstArray;
}
template<size_t NumComponents, typename DstType, typename SrcType>
DstType reshapeArray(const SrcType &srcArray)
{
return reshapeArray<
NumComponents,
0,
NumComponents,
0,
NumComponents,
DstType
>(srcArray);
}
template<typename DstType, typename SrcType>
DstType reshapeArray(const SrcType &srcArray)
{
typedef ELEMENTTRAIT(DstType) DstElementTrait;
return reshapeArray<DstElementTrait::numComponents, DstType>(srcArray);
}
template <typename Type, typename FaceCountsType>
void
reverseWindingOrder(Type &arrayData, const FaceCountsType &faceCounts)
{
typedef ARRAYTRAIT(Type) Trait;
typedef ARRAYTRAIT(FaceCountsType) FaceCountsTrait;
unsigned int current_index = 0;
for(unsigned int i = 0; i < FaceCountsTrait::size(faceCounts); i++)
{
const unsigned int faceCount
= FaceCountsTrait::getElement(faceCounts, i);
for(unsigned int a = current_index,
b = current_index + faceCount - 1;
a < current_index + faceCount / 2;
a++, b--)
{
std::swap(
Trait::getElement(arrayData, a),
Trait::getElement(arrayData, b)
);
}
current_index += faceCount;
}
}
template <
unsigned int NumComponents,
unsigned int DstStartComponent,
unsigned int SrcStartComponent,
typename DstType,
typename SrcType,
typename FaceCountsType,
typename FaceConnectsType
>
void
promoteAttributeData(
HAPI_AttributeOwner dstOwner,
DstType &dstArray,
HAPI_AttributeOwner srcOwner,
SrcType &srcArray,
unsigned int pointCount,
const FaceCountsType* polygonCounts = NULL,
const FaceConnectsType* polygonConnects = NULL
)
{
typedef ARRAYTRAIT(DstType) DstTrait;
//typedef ARRAYTRAIT(SrcType) SrcTrait;
typedef ARRAYTRAIT(FaceCountsType) FaceCountsTrait;
typedef ARRAYTRAIT(FaceConnectsType) FaceConnectsTrait;
switch(srcOwner)
{
case HAPI_ATTROWNER_VERTEX:
switch(dstOwner)
{
case HAPI_ATTROWNER_VERTEX:
{
assert(polygonCounts);
assert(polygonConnects);
DstTrait::resize(
dstArray,
FaceConnectsTrait::size(*polygonConnects)
);
unsigned int current_index = 0;
for(unsigned int i = 0;
i < FaceCountsTrait::size(*polygonCounts);
++i)
{
const unsigned int polygonCount
= FaceCountsTrait::getElement(
*polygonCounts,
i
);
for(unsigned int a = current_index,
b = current_index + polygonCount - 1;
a < current_index + polygonCount;
a++, b--)
{
ComponentWrapper<
DstType,
DstStartComponent, NumComponents
> (dstArray, a, DstStartComponent)
= ComponentWrapper<
SrcType,
SrcStartComponent, NumComponents
> (srcArray, b, SrcStartComponent);
}
current_index += polygonCount;
}
}
break;
default:
assert(false);
break;
}
break;
case HAPI_ATTROWNER_POINT:
switch(dstOwner)
{
case HAPI_ATTROWNER_VERTEX:
assert(polygonConnects);
DstTrait::resize(
dstArray,
FaceConnectsTrait::size(*polygonConnects)
);
for(unsigned int i = 0;
i < FaceCountsTrait::size(*polygonConnects);
++i)
{
unsigned int point
= FaceConnectsTrait::getElement(
*polygonConnects,
i
);
ComponentWrapper<
DstType,
DstStartComponent, NumComponents
>(dstArray, i, DstStartComponent)
= ComponentWrapper<
SrcType,
SrcStartComponent, NumComponents
>(srcArray, point, SrcStartComponent);
}
break;
case HAPI_ATTROWNER_POINT:
DstTrait::resize(dstArray, pointCount);
for(unsigned int i = 0; i < pointCount; ++i)
{
ComponentWrapper<
DstType,
DstStartComponent, NumComponents
>(dstArray, i, DstStartComponent)
= ComponentWrapper<
SrcType,
SrcStartComponent, NumComponents
>(srcArray, i, SrcStartComponent);
}
break;
default:
assert(false);
break;
}
break;
case HAPI_ATTROWNER_PRIM:
switch(dstOwner)
{
case HAPI_ATTROWNER_VERTEX:
assert(polygonCounts);
assert(polygonConnects);
DstTrait::resize(
dstArray,
FaceConnectsTrait::size(*polygonConnects)
);
for(unsigned int i = 0, j = 0;
i < FaceCountsTrait::size(*polygonCounts);
++i)
{
const unsigned int polygonCount
= FaceCountsTrait::getElement(*polygonCounts, i);
for(unsigned int k = 0; k < polygonCount; ++j, ++k)
{
ComponentWrapper<
DstType,
DstStartComponent, NumComponents
>(dstArray, j, DstStartComponent)
= ComponentWrapper<
SrcType,
SrcStartComponent, NumComponents
>(srcArray, i, SrcStartComponent);
}
}
break;
case HAPI_ATTROWNER_POINT:
// Don't convert the prim attributes to point
// attributes, because that would lose information.
// Convert everything to vertex attributs instead.
assert(false);
break;
default:
assert(false);
break;
}
break;
case HAPI_ATTROWNER_DETAIL:
{
unsigned int count = 0;
switch(dstOwner)
{
case HAPI_ATTROWNER_VERTEX:
assert(polygonConnects);
count = FaceConnectsTrait::size(*polygonConnects);
break;
case HAPI_ATTROWNER_POINT:
count = pointCount;
break;
case HAPI_ATTROWNER_PRIM:
assert(polygonCounts);
count = FaceCountsTrait::size(*polygonCounts);
break;
default:
assert(false);
break;
}
DstTrait::resize(dstArray, count);
for(unsigned int i = 0; i < count; ++i)
{
ComponentWrapper<
DstType,
DstStartComponent, NumComponents
>(dstArray, i, DstStartComponent)
= ComponentWrapper<
SrcType,
SrcStartComponent, NumComponents
>(srcArray, 0, SrcStartComponent);
}
}
break;
default:
assert(false);
break;
}
}
MPlug
plugSource(const MPlug &plug);
MPlugArray
plugDestination(const MPlug &plug);
bool
assetLockingEnabled();
}
#endif