-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHAPI_CPP.h
697 lines (576 loc) · 17.8 KB
/
HAPI_CPP.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
/******************************************************************************
Houdini Engine Module for Omegalib
Authors:
Darren Lee [email protected]
Copyright 2015-2016, Data Arena, University of Technology Sydney
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and authors, and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the Data Arena Project.
-------------------------------------------------------------------------------
Based on example file from SideFX
******************************************************************************/
#ifndef __HAPI_CPP_h__
#define __HAPI_CPP_h__
#include <HAPI/HAPI.h>
#include <string>
#include <vector>
#include <map>
#include <stdexcept>
namespace hapi
{
//----------------------------------------------------------------------------
// Common error handling:
// Instances of this class are thrown whenever an underlying HAPI function
// call fails.
class Failure
{
public:
Failure(HAPI_Result result)
: result(result)
{}
// Retrieve details about the last non-successful HAPI function call.
// You would typically call this method after catching a Failure exception,
// but it can be called as a static method after calling a C HAPI function.
static std::string lastErrorMessage(HAPI_Session* session)
{
int buffer_length;
HAPI_GetStatusStringBufLength(
session,
HAPI_STATUS_CALL_RESULT, HAPI_STATUSVERBOSITY_ERRORS, &buffer_length);
char * buf = new char[ buffer_length ];
HAPI_GetStatusString(session, HAPI_STATUS_CALL_RESULT, buf, buffer_length);
std::string result(buf);
return result;
}
// Retrieve details about the last non-successful HAPI_CookAsset() or
// HAPI_InstantiateAsset() function call.
static std::string lastCookErrorMessage(HAPI_Session* session)
{
int buffer_length;
HAPI_GetStatusStringBufLength(
session,
HAPI_STATUS_COOK_RESULT, HAPI_STATUSVERBOSITY_ERRORS, &buffer_length);
char * buf = new char[ buffer_length ];
HAPI_GetStatusString(session, HAPI_STATUS_CALL_RESULT, buf, buffer_length);
std::string result(buf);
return result;
}
HAPI_Result result;
};
// This helper function is used internally by the classes below.
static void throwOnFailure(HAPI_Result result)
{
if (result != HAPI_RESULT_SUCCESS)
throw Failure(result);
}
//----------------------------------------------------------------------------
// Utility functions:
// Return a std::string corresponding to a string handle.
static std::string getString(HAPI_Session* session, int string_handle)
{
// A string handle of 0 means an invalid string handle -- similar to
// a null pointer. Since we can't return NULL, though, return an empty
// string.
if (string_handle == 0)
return "";
int buffer_length;
throwOnFailure(HAPI_GetStringBufLength(session, string_handle, &buffer_length));
char * buf = new char[ buffer_length ];
throwOnFailure(HAPI_GetString(session, string_handle, buf, buffer_length));
std::string result(buf);
return result;
}
//----------------------------------------------------------------------------
// Classes:
class Object;
class Parm;
class Asset
{
public:
Asset(int id, HAPI_Session* mySession)
: id(id), _info(NULL), _nodeInfo(NULL), session(mySession)
{}
Asset(const Asset &asset)
: id(asset.id)
, _info(asset._info ? new HAPI_AssetInfo(*asset._info) : NULL)
, _nodeInfo(asset._nodeInfo ? new HAPI_NodeInfo(*asset._nodeInfo) : NULL)
, session(asset.session)
{}
~Asset()
{
delete this->_info;
delete this->_nodeInfo;
}
Asset &operator=(const Asset &asset)
{
if (this != &asset)
{
delete this->_info;
delete this->_nodeInfo;
this->id = asset.id;
this->_info = asset._info ? new HAPI_AssetInfo(*asset._info) : NULL;
this->_nodeInfo = asset._nodeInfo
? new HAPI_NodeInfo(*asset._nodeInfo) : NULL;
}
return *this;
}
const HAPI_AssetInfo &info() const
{
if (!this->_info)
{
this->_info = new HAPI_AssetInfo();
throwOnFailure(HAPI_GetAssetInfo(session, this->id, this->_info));
}
return *this->_info;
}
const HAPI_NodeInfo &nodeInfo() const
{
if (!this->_nodeInfo)
{
this->_nodeInfo = new HAPI_NodeInfo();
throwOnFailure(HAPI_GetNodeInfo(
session,
this->info().nodeId, this->_nodeInfo));
}
return *this->_nodeInfo;
}
std::vector<Object> objects() const;
std::vector<Parm> parms() const;
std::map<std::string, Parm> parmMap() const;
bool isValid() const
{
int is_valid = 0;
try
{
// Note that calling info() might fail if the info isn't cached
// and the asest id is invalid.
throwOnFailure(HAPI_IsAssetValid(
session,
this->id, this->info().validationId, &is_valid));
return is_valid;
}
catch (Failure &failure)
{
return false;
}
}
std::string name() const
{ return getString(session, info().nameSH); }
std::string label() const
{ return getString(session, info().labelSH); }
std::string filePath() const
{ return getString(session, info().filePathSH); }
void destroyAsset() const
{ throwOnFailure(HAPI_DestroyAsset(session, this->id)); }
void cook() const
{ throwOnFailure(HAPI_CookAsset(session, this->id, NULL)); }
HAPI_TransformEuler getTransform(
HAPI_RSTOrder rst_order, HAPI_XYZOrder rot_order) const
{
HAPI_TransformEuler result;
throwOnFailure(HAPI_GetAssetTransform(
session,
this->id, rst_order, rot_order, &result));
return result;
}
void getTransformAsMatrix(float result_matrix[16]) const
{
HAPI_TransformEuler transform =
this->getTransform( HAPI_SRT, HAPI_XYZ );
throwOnFailure(HAPI_ConvertTransformEulerToMatrix(
session,
&transform, result_matrix ) );
}
int id;
HAPI_Session* session;
private:
mutable HAPI_AssetInfo *_info;
mutable HAPI_NodeInfo *_nodeInfo;
};
class Geo;
class Object
{
public:
Object(int asset_id, int object_id, HAPI_Session* mySession)
: asset(asset_id, mySession), id(object_id), _info(NULL), session(mySession)
{}
Object(const Asset &asset, int id)
: asset(asset), id(id), _info(NULL), session(asset.session)
{}
Object(const Object &object)
: asset(object.asset)
, id(object.id)
, _info(object._info ? new HAPI_ObjectInfo(*object._info) : NULL)
, session(object.session)
{}
~Object()
{ delete this->_info; }
Object &operator=(const Object &object)
{
if (this != &object)
{
delete this->_info;
asset = object.asset;
this->id = object.id;
this->_info = object._info
? new HAPI_ObjectInfo(*object._info) : NULL;
}
return *this;
}
const HAPI_ObjectInfo &info() const
{
if (!this->_info)
{
this->_info = new HAPI_ObjectInfo();
throwOnFailure(HAPI_GetObjects(
session,
this->asset.id, this->_info, this->id, /*length=*/1));
}
return *this->_info;
}
std::vector<Geo> geos() const;
std::string name() const
{ return getString(session, info().nameSH); }
std::string objectInstancePath() const
{ return getString(session, info().objectInstancePathSH); }
Asset asset;
int id;
HAPI_Session* session;
private:
mutable HAPI_ObjectInfo *_info;
};
class Part;
class Geo
{
public:
Geo(const Object &object, int id)
: object(object), id(id), _info(NULL), session(object.session)
{}
Geo(int asset_id, int object_id, int geo_id, HAPI_Session* mySession)
: object(asset_id, object_id, mySession), id(geo_id), _info(NULL), session(mySession)
{}
Geo(const Geo &geo)
: object(geo.object)
, id(geo.id)
, _info(geo._info ? new HAPI_GeoInfo(*geo._info) : NULL)
, session(geo.session)
{}
~Geo()
{ delete _info; }
Geo &operator=(const Geo &geo)
{
if (this != &geo)
{
delete this->_info;
this->object = geo.object;
this->id = geo.id;
this->_info = geo._info ? new HAPI_GeoInfo(*geo._info) : NULL;
}
return *this;
}
const HAPI_GeoInfo &info() const
{
if (!this->_info)
{
this->_info = new HAPI_GeoInfo();
throwOnFailure(HAPI_GetGeoInfo(
session,
this->object.asset.id, this->object.id, this->id, this->_info));
}
return *this->_info;
}
std::string name() const
{ return getString(session, info().nameSH); }
std::vector<Part> parts() const;
Object object;
int id;
HAPI_Session* session;
private:
mutable HAPI_GeoInfo *_info;
};
class Part
{
public:
Part(const Geo &geo, int id)
: geo(geo), id(id), _info(NULL), session(geo.session)
{}
Part(int asset_id, int object_id, int geo_id, int part_id, HAPI_Session* mySession)
: geo(asset_id, object_id, geo_id, mySession), id(part_id), _info(NULL), session(mySession)
{}
Part(const Part &part)
: geo(part.geo)
, id(part.id)
, _info(part._info ? new HAPI_PartInfo(*part._info) : NULL)
, session(part.session)
{}
~Part()
{ delete _info; }
Part &operator=(const Part &part)
{
if (this != &part)
{
delete this->_info;
this->geo = part.geo;
this->id = part.id;
this->_info = part._info ? new HAPI_PartInfo(*part._info) : NULL;
}
return *this;
}
const HAPI_PartInfo &info() const
{
if (!this->_info)
{
this->_info = new HAPI_PartInfo();
throwOnFailure(HAPI_GetPartInfo(
session,
this->geo.object.asset.id, this->geo.object.id, this->geo.id,
this->id, this->_info));
}
return *this->_info;
}
std::string name() const
{ return getString(session, info().nameSH); }
int numAttribs(HAPI_AttributeOwner attrib_owner) const
{
switch (attrib_owner)
{
case HAPI_ATTROWNER_VERTEX:
return this->info().vertexAttributeCount;
case HAPI_ATTROWNER_POINT:
return this->info().pointAttributeCount;
case HAPI_ATTROWNER_PRIM:
return this->info().faceAttributeCount;
case HAPI_ATTROWNER_DETAIL:
return this->info().detailAttributeCount;
case HAPI_ATTROWNER_MAX:
case HAPI_ATTROWNER_INVALID:
break;
}
return 0;
}
std::vector<std::string> attribNames(HAPI_AttributeOwner attrib_owner) const
{
int num_attribs = numAttribs(attrib_owner);
std::vector<std::string> result;
// HAPI_GetAttributeNames() will fail if num_attribs given is 0, even if
// that is the number of attributes. Return empty string vector if that is the case.
if (num_attribs == 0) {
return result;
}
std::vector<int> attrib_names_sh(num_attribs);
throwOnFailure(HAPI_GetAttributeNames(
session,
this->geo.object.asset.id, this->geo.object.id, this->geo.id,
this->id, attrib_owner, &attrib_names_sh[0], num_attribs));
for (int attrib_index=0; attrib_index < int(attrib_names_sh.size());
++attrib_index)
result.push_back(getString(session, attrib_names_sh[attrib_index]));
return result;
}
HAPI_AttributeInfo attribInfo(
HAPI_AttributeOwner attrib_owner, const char *attrib_name) const
{
HAPI_AttributeInfo result;
throwOnFailure(HAPI_GetAttributeInfo(
session,
this->geo.object.asset.id, this->geo.object.id, this->geo.id,
this->id, attrib_name, attrib_owner, &result));
return result;
}
float *getNewFloatAttribData(
HAPI_AttributeInfo &attrib_info, const char *attrib_name,
int start=0, int length=-1) const
{
if (length < 0)
length = attrib_info.count - start;
float *result = new float[attrib_info.count * attrib_info.tupleSize];
throwOnFailure(HAPI_GetAttributeFloatData(
session,
this->geo.object.asset.id, this->geo.object.id, this->geo.id,
this->id, attrib_name, &attrib_info, result,
/*start=*/0, attrib_info.count));
return result;
}
Geo geo;
int id;
HAPI_Session* session;
private:
mutable HAPI_PartInfo *_info;
};
class ParmChoice;
class Parm
{
public:
// This constructor is required only for std::map::operator[] for the case
// where a Parm object does not exist in the map.
Parm()
{ throw std::out_of_range("Invalid parameter name"); }
Parm(int node_id, const HAPI_ParmInfo &info,
HAPI_ParmChoiceInfo *all_choice_infos, HAPI_Session* mySession);
const HAPI_ParmInfo &info() const
{ return _info; }
std::string name() const
{ return getString(session, _info.nameSH); }
std::string label() const
{ return getString(session, _info.labelSH); }
int getIntValue(int sub_index) const
{
int result;
throwOnFailure(HAPI_GetParmIntValues(
session,
this->node_id, &result, this->_info.intValuesIndex + sub_index,
/*length=*/1));
return result;
}
float getFloatValue(int sub_index) const
{
float result;
throwOnFailure(HAPI_GetParmFloatValues(
session,
this->node_id, &result, this->_info.floatValuesIndex + sub_index,
/*length=*/1));
return result;
}
std::string getStringValue(int sub_index) const
{
int string_handle;
throwOnFailure(HAPI_GetParmStringValues(
session,
this->node_id, true, &string_handle,
this->_info.stringValuesIndex + sub_index, /*length=*/1));
return getString(session, string_handle);
}
void setIntValue(int sub_index, int value)
{
throwOnFailure(HAPI_SetParmIntValues(
session,
this->node_id, &value, this->_info.intValuesIndex + sub_index,
/*length=*/1));
}
void setFloatValue(int sub_index, float value)
{
throwOnFailure(HAPI_SetParmFloatValues(
session,
this->node_id, &value, this->_info.floatValuesIndex + sub_index,
/*length=*/1));
}
void setStringValue(int sub_index, const char *value)
{
throwOnFailure(HAPI_SetParmStringValue(
session,
this->node_id, value, this->_info.id, sub_index));
}
void insertMultiparmInstance(int instance_position)
{
throwOnFailure(HAPI_InsertMultiparmInstance(
session,
this->node_id, this->_info.id, instance_position));
}
void removeMultiparmInstance(int instance_position)
{
throwOnFailure(HAPI_RemoveMultiparmInstance(
session,
this->node_id, this->_info.id, instance_position));
}
int node_id;
std::vector<ParmChoice> choices;
HAPI_Session* session;
private:
HAPI_ParmInfo _info;
};
class ParmChoice
{
public:
ParmChoice(HAPI_ParmChoiceInfo &info, HAPI_Session* mySession)
: _info(info), session(mySession)
{}
const HAPI_ParmChoiceInfo &info() const
{ return _info; }
std::string label() const
{ return getString(session, _info.labelSH); }
std::string value() const
{ return getString(session, _info.valueSH); }
HAPI_Session* session;
private:
HAPI_ParmChoiceInfo _info;
};
// Methods that could not be declared inside the classes:
inline Parm::Parm(int node_id, const HAPI_ParmInfo &info,
HAPI_ParmChoiceInfo *all_choice_infos, HAPI_Session* mySession)
: node_id(node_id), _info(info), session(mySession)
{
for (int i=0; i < info.choiceCount; ++i)
this->choices.push_back(ParmChoice(
all_choice_infos[info.choiceIndex + i], session));
}
inline std::vector<Object> Asset::objects() const
{
std::vector<Object> result;
for (int object_id=0; object_id < info().objectCount; ++object_id)
result.push_back(Object(*this, object_id));
return result;
}
inline std::vector<Parm> Asset::parms() const
{
// Get all the parm infos.
int num_parms = nodeInfo().parmCount;
std::vector<HAPI_ParmInfo> parm_infos(num_parms);
throwOnFailure(HAPI_GetParameters(
this->session,
this->info().nodeId, &parm_infos[0], /*start=*/0, num_parms));
// Get all the parm choice infos.
std::vector<HAPI_ParmChoiceInfo> parm_choice_infos(
this->nodeInfo().parmChoiceCount);
throwOnFailure(HAPI_GetParmChoiceLists(
session,
this->info().nodeId, &parm_choice_infos[0], /*start=*/0,
this->nodeInfo().parmChoiceCount));
// Build and return a vector of Parm objects.
std::vector<Parm> result;
for (int i=0; i < num_parms; ++i)
result.push_back(Parm(
this->info().nodeId, parm_infos[i], &parm_choice_infos[0], this->session));
return result;
}
inline std::map<std::string, Parm> Asset::parmMap() const
{
std::vector<Parm> parms = this->parms();
std::map<std::string, Parm> result;
for (int i=0; i < int(parms.size()); ++i)
result.insert(std::make_pair(parms[i].name(), parms[i]));
return result;
}
inline std::vector<Geo> Object::geos() const
{
std::vector<Geo> result;
for (int geo_id=0; geo_id < info().geoCount; ++geo_id)
result.push_back(Geo(*this, geo_id));
return result;
}
inline std::vector<Part> Geo::parts() const
{
std::vector<Part> result;
for (int part_id=0; part_id < info().partCount; ++part_id)
result.push_back(Part(*this, part_id));
return result;
}
};
#endif