Houdini Engine 2.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Packed Primitives Sample

For documentation on the packed primitives APIs, see Packed Primitives.

Simple example that goes through the 3 different packed primitive instancing modes and prints the resulting part layout for each.

#include <HAPI.h>
#include <vector>
#include <string>
#include <iostream>
#include <assert.h>
using std::cout;
using std::cin;
using std::endl;
#define CHECK( hapi_call ) \
do { \
HAPI_Result result = ( hapi_call ); \
process_call_result( result ); \
assert( result == HAPI_RESULT_SUCCESS ); \
} while( false )
void process_call_result( HAPI_Result result )
{
if ( result == HAPI_RESULT_SUCCESS )
return;
cout << "Error Code: " << result << endl;
int buf_length = 0;
char * buf = new char[ buf_length ];
HAPI_GetStatusString( nullptr, HAPI_STATUS_CALL_RESULT, buf, buf_length );
cout << "Error Message: " << buf << endl;
delete buf;
}
void print_part_info( HAPI_AssetId asset_id, HAPI_PartId part_id, std::string indent )
{
// Defines some known constants for this asset.
const HAPI_ObjectId object_id = 0;
const HAPI_GeoId geo_id = 0;
// Get the part info.
HAPI_PartInfo part_info;
nullptr, asset_id, object_id, geo_id, part_id, &part_info ) );
// If the part is being instanced by another part, don't print it.
// It will be printed by its instancer parent part.
if ( part_info.isInstanced && indent == "" )
return;
if ( part_info.type == HAPI_PARTTYPE_MESH )
{
cout << indent << "Part " << part_id << ":" << endl;
cout << indent << " Type = Mesh" << endl;
cout << indent << " Point Count = " << part_info.pointCount << endl;
}
else if ( part_info.type == HAPI_PARTTYPE_CURVE )
{
cout << indent << "Part " << part_id << ":" << endl;
cout << indent << " Type = Curve" << endl;
cout << indent << " Point Count = " << part_info.pointCount << endl;
}
else if ( part_info.type == HAPI_PARTTYPE_INSTANCER )
{
cout << indent << "Part " << part_id << ":" << endl;
cout << indent << " Type = Instancer" << endl;
cout << indent << " Point Count = " << part_info.pointCount << endl;
cout << indent << " Instance Count = " << part_info.instanceCount << endl;
cout << indent << " Instanced Part Count = " << part_info.instancedPartCount << endl;
// Get the transforms for each instance.
std::vector< HAPI_Transform > instance_transforms( part_info.instanceCount );
nullptr, asset_id, object_id, geo_id, part_id,
HAPI_RSTORDER_DEFAULT, instance_transforms.data(),
0, part_info.instanceCount ) );
// Print the instance transforms:
cout << indent << " Instance Transforms:" << endl;
for ( auto instance_transform : instance_transforms )
{
float * p = &instance_transform.position[ 0 ];
cout << indent << " " << p[ 0 ] << ", " << p[ 1 ] << ", " << p[ 2 ] << endl;
}
// Get the part ids of the parts being instanced.
std::vector< HAPI_PartId > instanced_part_ids( part_info.instancedPartCount );
nullptr, asset_id, object_id, geo_id, part_id, instanced_part_ids.data(),
0, part_info.instancedPartCount ) );
// Print the part infos of all the instanced parts.
cout << indent << " Instanced Parts:" << endl;
for ( auto instanced_part_id : instanced_part_ids )
print_part_info( asset_id, instanced_part_id, indent + " " );
}
}
void cook_and_print_asset( HAPI_AssetId asset_id, HAPI_PackedPrimInstancingMode instancing_mode )
{
switch ( instancing_mode )
{
cout << "Using: HAPI_PACKEDPRIM_INSTANCING_MODE_DISABLED" << endl; break;
cout << "Using: HAPI_PACKEDPRIM_INSTANCING_MODE_HIERARCHY" << endl; break;
cout << "Using: HAPI_PACKEDPRIM_INSTANCING_MODE_FLAT" << endl; break;
}
// Set the packed prim mode and cook.
cook_options.packedPrimInstancingMode = instancing_mode;
CHECK( HAPI_CookAsset( nullptr, asset_id, &cook_options ) );
// Defines some known constants for this asset.
const HAPI_ObjectId object_id = 0;
const HAPI_GeoId geo_id = 0;
// Get the geo info.
HAPI_GeoInfo geo_info;
CHECK( HAPI_GetGeoInfo( nullptr, asset_id, object_id, geo_id, &geo_info ) );
cout << "Total Part Count: " << geo_info.partCount << endl;
// Loop through the parts and print their details.
for ( int part_id = 0; part_id < geo_info.partCount; ++part_id )
print_part_info( asset_id, part_id, "" );
// Just add a newline for neatness.
cout << endl;
}
int main()
{
// Initialize HAPI.
nullptr, &cook_options, true, -1, nullptr, nullptr, nullptr, nullptr ) );
// Load the library from file.
HAPI_AssetLibraryId library_id = -1;
nullptr, "HAPI_Test_Instancing_Pack_Simple.otl",
false, &library_id ) );
// Instantiate the asset.
HAPI_AssetId asset_id = -1;
nullptr, "Object/HAPI_Test_Instancing_Pack_Simple",
false, &asset_id ) );
// Print and cook
cook_and_print_asset( asset_id, HAPI_PACKEDPRIM_INSTANCING_MODE_DISABLED );
cook_and_print_asset( asset_id, HAPI_PACKEDPRIM_INSTANCING_MODE_HIERARCHY );
cook_and_print_asset( asset_id, HAPI_PACKEDPRIM_INSTANCING_MODE_FLAT );
// Cleanup.
CHECK( HAPI_Cleanup( nullptr ) );
// Wait to terminate.
char in;
cout << "Press any key to exit." << endl;
cin >> in;
}