forked from sideeffects/HoudiniEngineForMaya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.h
451 lines (391 loc) · 14.9 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
#ifndef __util_h__
#define __util_h__
#include <maya/MComputation.h>
#include <maya/MGlobal.h>
#include <maya/MObject.h>
#include <maya/MString.h>
#include <maya/MTimer.h>
#include <maya/MIntArray.h>
#include <cassert>
#include <vector>
#include <HAPI/HAPI.h>
class MDGModifier;
class MFnDagNode;
class HAPIError: public std::exception
{
public:
HAPIError() throw();
HAPIError(const HAPIError & error) throw();
HAPIError(MString msg) throw();
virtual ~HAPIError() throw() {}
virtual const char* what() const throw();
protected:
mutable MString myBuffer;
MString myMessage;
};
#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( \
(status_type), (verbosity), &bufferLength); \
_hapiStatusBuffer.resize(bufferLength); \
HAPI_GetStatusString((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 \
} \
}
class Util {
public:
static void displayInfoForNode(
const MString &typeName,
const MString &message
);
static void displayWarningForNode(
const MString &typeName,
const MString &message
);
static void displayErrorForNode(
const MString &typeName,
const MString &message
);
static MString getString(int handle);
static MString getAttrNameFromParm(const HAPI_ParmInfo &parm);
static MStringArray getAttributeStringData(int assetId, int objectId,
int geoId, int partId,
HAPI_AttributeOwner owner,
const MString & name);
static MString getParmAttrPrefix();
static bool hasHAPICallFailed(HAPI_Result stat);
static MString escapeString(const MString &str);
// Throws an exception if an error occurred
static void checkHAPIStatus(HAPI_Result stat);
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;
};
static bool statusCheckLoop(bool wantMainProgressBar = true);
static MObject findNodeByName(const MString &name);
static MObject findDagChild(const MFnDagNode &dag, const MString &name);
static MStatus createNodeByModifierCommand(
MDGModifier &dgModifier,
const MString &command,
MObject &object,
unsigned int index = 0
);
static MString replaceString(const MString &str, const MString &searchStr, const MString &newChar);
static MString sanitizeStringForNodeName(const MString &str);
// Returns true if the parm was found.
static 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 &);
};
static void walkParm(const std::vector<HAPI_ParmInfo> &parmInfos, WalkParmOperation &operation);
// STL style containers
template <typename T, typename Alloc, template <typename, typename> class U>
static unsigned int getArrayLength(const U<T, Alloc> &array)
{
return array.size();
}
template <typename T, typename Alloc, template <typename, typename> class U>
static void setArrayLength(U<T, Alloc> &array, unsigned int n)
{
array.resize(n);
}
// Maya containers
template <typename T>
static unsigned int getArrayLength(const T &array)
{
return array.length();
}
template <typename T>
static void setArrayLength(T &array, unsigned int n)
{
array.setLength(n);
}
// Accessing components
template <unsigned int offset, typename A, typename T>
struct getComponent
{
A operator()(const T &t, unsigned int i)
{
return t[offset + i];
}
A &operator()(T &t, unsigned int i)
{
return t[offset + i];
}
};
template <unsigned int offset, typename T>
struct getComponent<offset, T, T>
{
T operator()(const T &t, unsigned int i)
{
return t;
}
T &operator()(T &t, unsigned int i)
{
return t;
}
};
template <unsigned int offset, unsigned int offset2,
unsigned int numComponents,
typename A,
typename T, typename U>
static void setComponents(T &a, const U &b)
{
for(unsigned int i = 0; i < numComponents; i++)
{
getComponent<offset, A, T>()(a, i) = getComponent<offset2, A, U>()(b, i);
}
}
template <typename T, typename U>
static void reverseWindingOrder(T &arrayData, const U &faceCounts)
{
unsigned int current_index = 0;
for(unsigned int i = 0; i < getArrayLength(faceCounts); i++)
{
for(unsigned int a = current_index, b = current_index + faceCounts[i] - 1;
a < current_index + faceCounts[i] / 2; a++, b--)
{
std::swap(arrayData[a], arrayData[b]);
}
current_index += faceCounts[i];
}
}
template <unsigned int offset, unsigned int offset2, unsigned int numComponents,
typename A,
typename T, typename U, typename V>
static void
promoteAttributeData(
HAPI_AttributeOwner toOwner,
T &toArray,
HAPI_AttributeOwner fromOwner,
const U &fromArray,
unsigned int pointCount,
const V* polygonCounts = NULL,
const V* polygonConnects = NULL
)
{
if(fromOwner == toOwner)
{
setArrayLength(toArray, getArrayLength(fromArray));
for(unsigned int i = 0; i < getArrayLength(fromArray); ++i)
{
setComponents<offset, offset2, numComponents, A>(toArray[i], fromArray[i]);
}
return;
}
switch(fromOwner)
{
case HAPI_ATTROWNER_POINT:
switch(toOwner)
{
case HAPI_ATTROWNER_VERTEX:
assert(polygonConnects);
assert(polygonConnects);
setArrayLength(toArray, getArrayLength(*polygonConnects));
for(unsigned int i = 0; i < getArrayLength(*polygonConnects); ++i)
{
unsigned int point = (*polygonConnects)[i];
setComponents<offset, offset2, numComponents, A>(toArray[i], fromArray[point]);
}
break;
default:
assert(false);
break;
}
break;
case HAPI_ATTROWNER_PRIM:
switch(toOwner)
{
case HAPI_ATTROWNER_VERTEX:
assert(polygonCounts);
assert(polygonConnects);
setArrayLength(toArray, getArrayLength(*polygonConnects));
for(unsigned int i = 0, j = 0; i < getArrayLength(*polygonCounts); ++i)
{
for(int k = 0; k < (*polygonCounts)[i]; ++j, ++k)
{
setComponents<offset, offset2, numComponents, A>(toArray[j], fromArray[i]);
}
}
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(toOwner)
{
case HAPI_ATTROWNER_VERTEX:
assert(polygonConnects);
count = getArrayLength(*polygonConnects);
break;
case HAPI_ATTROWNER_POINT:
count = pointCount;
break;
case HAPI_ATTROWNER_PRIM:
assert(polygonCounts);
count = getArrayLength(*polygonCounts);
break;
default:
assert(false);
break;
}
setArrayLength(toArray, count);
for(unsigned int i = 0; i < count; ++i)
{
setComponents<offset, offset2, numComponents, A>(toArray[i], fromArray[0]);
}
}
break;
default:
assert(false);
break;
}
}
};
#endif