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

For documentation on the groups APIs, see Groups.

The example below shows how to create groups on an input asset and query their membership. It will create a cube, marshal it in, and create a point group on the input geometry. It goes on to create a transform SOP, connect the two assets together, and sets the parameters on the transform SOP such that it affects only the points in the group we created. The scene at this point is dumped, so that we can see the result in Houdini.

Finally, the transform SOP asset is cooked, we then query its geometry for group information and show via output that the group is intact and display the group membership.

#include <HAPI/HAPI.h>
#include <string>
bool cookAsset( HAPI_AssetId assetId )
{
int status;
HAPI_CookAsset( nullptr, assetId, nullptr );
do
{
HAPI_GetStatus( nullptr, HAPI_STATUS_COOK_STATE, &status );
}
while ( status > HAPI_STATE_MAX_READY_STATE );
return status == HAPI_STATE_READY;
}
static std::string get_string( HAPI_StringHandle 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;
HAPI_GetStringBufLength( nullptr, string_handle, &buffer_length );
char * buf = new char[ buffer_length ];
HAPI_GetString( nullptr, string_handle, buf, buffer_length );
std::string result( buf );
delete[] buf;
return result;
}
bool setXformParameters( HAPI_AssetId xformAssetId )
{
HAPI_AssetInfo asset_info;
HAPI_GetAssetInfo( nullptr, xformAssetId, &asset_info );
HAPI_NodeInfo node_info;
HAPI_GetNodeInfo( nullptr, asset_info.nodeId, &node_info );
// Get information about all the parameters.
HAPI_ParmInfo * parm_infos = new HAPI_ParmInfo[ node_info.parmCount ];
nullptr, node_info.id, parm_infos, 0, node_info.parmCount );
// Print out all the parameters and their values. Also look for the
// parms named "group" and "t".
int group_parm_index = -1;
int t_parm_index = -1;
for ( int ii = 0; ii < node_info.parmCount; ++ii )
{
std::string parm_name = get_string( parm_infos[ ii ].nameSH );
if ( parm_name == "group" )
group_parm_index = ii;
if ( parm_name == "t" )
t_parm_index = ii;
}
if( t_parm_index < 0 || group_parm_index < 0 )
{
printf( "Error: couldn't find required parameters group or t\n" );
return false;
}
float t_parm_value[ 3 ] = { 0.0f, 1.0f, 0.0f };
const int length = 3;
nullptr,
asset_info.nodeId, t_parm_value,
parm_infos[ t_parm_index ].floatValuesIndex, length );
nullptr,
asset_info.nodeId, "example_point_group", group_parm_index, 0 );
return true;
}
int main( int argc, char* argv[] )
{
HAPI_CookOptions_Init( &cook_options );
cook_options.maxVerticesPerPrimitive = 4;
nullptr, // session
&cook_options,
true, // use_cooking_thread
-1, // cooking_thread_stack_size
nullptr, // otl_search_path
nullptr, // dso_search_path
nullptr, // image_dso_search_path
nullptr // audio_dso_search_path
);
if ( result != HAPI_RESULT_SUCCESS )
{
printf( "Error: couldn't initialize HAPI\n" );
return -1;
}
HAPI_AssetId geoCreatorId;
HAPI_CreateInputAsset( nullptr, &geoCreatorId, NULL );
HAPI_CookAsset( nullptr, geoCreatorId, NULL );
newPart.vertexCount = 24;
newPart.pointCount = 8;
newPart.faceCount = 6;
HAPI_SetPartInfo( nullptr, geoCreatorId, 0, 0, &newPart );
pointInfo.count = 8; // 8 points
pointInfo.tupleSize = 3; // 3 floats per point (x, y, z)
pointInfo.exists = true;
HAPI_AddAttribute( nullptr, geoCreatorId, 0, 0, "P", &pointInfo );
float positions[ 24 ] = {
0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 1.0f,
1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 1.0f
};
nullptr, geoCreatorId, 0, 0, "P", &pointInfo, positions, 0, 8 );
int vertices[ 24 ] = {
0, 2, 6, 4,
2, 3, 7, 6,
2, 0, 1, 3,
1, 5, 7, 3,
5, 4, 6, 7,
0, 4, 5, 1,
};
HAPI_SetVertexList( nullptr, geoCreatorId, 0, 0, vertices, 0, 24 );
int face_counts[ 6 ] = { 4, 4, 4, 4, 4, 4 }; // 4 verts for each face (quads)
HAPI_SetFaceCounts( nullptr, geoCreatorId, 0, 0, face_counts, 0, 6 );
nullptr, geoCreatorId, 0, 0, HAPI_GROUPTYPE_POINT,
"example_point_group" );
&newPart, HAPI_GROUPTYPE_POINT );
int * pointMembership = new int[ groupElementCount ];
for ( int ii = 0; ii < groupElementCount; ii++ )
{
if ( ii % 2 )
pointMembership[ ii ] = 1;
else
pointMembership[ ii ] = 0;
}
nullptr,
geoCreatorId, 0, 0, HAPI_GROUPTYPE_POINT, "example_point_group",
pointMembership, 0, groupElementCount );
HAPI_CommitGeo( nullptr, geoCreatorId, 0, 0 );
HAPI_AssetId xformAssetId = -1;
HAPI_InstantiateAsset( nullptr, "Sop/xform", true, &xformAssetId );
HAPI_ConnectAssetGeometry( nullptr, geoCreatorId, 0, xformAssetId, 0 );
setXformParameters( xformAssetId );
HAPI_SaveHIPFile( nullptr, "C:\\test\\testoutput.hip", false );
if( !cookAsset( xformAssetId ) )
{
printf( "Error: couldn't cook xform asset\n" );
return -1;
}
HAPI_GeoInfo geoInfo;
HAPI_GetGeoInfo( nullptr, xformAssetId, 0, 0, &geoInfo );
&geoInfo, HAPI_GROUPTYPE_POINT );
printf( "num point groups on xform: %d\n", numGroups );
HAPI_PartInfo partInfo;
HAPI_GetPartInfo( nullptr, xformAssetId, 0, 0, 0, &partInfo );
int numElementsInGroup = HAPI_PartInfo_GetElementCountByGroupType(
&partInfo,HAPI_GROUPTYPE_POINT );
printf( "%d points in group example_point_group\n", numElementsInGroup );
int * membership = new int[ numElementsInGroup ];
nullptr,
xformAssetId, 0, 0, 0, HAPI_GROUPTYPE_POINT, "example_point_group",
membership,0, numElementsInGroup);
for ( int ii = 0; ii < numElementsInGroup; ii++ )
{
if ( membership[ ii ] )
printf( "Point %d is in example_point_group\n", ii );
}
delete[] membership;
return 0;
}

The output from a run of the application is as follows:

num point groups on xform: 1
8 points in group example_point_group
Point 1 is in example_point_group
Point 3 is in example_point_group
Point 5 is in example_point_group
Point 7 is in example_point_group

The HIP file that was saved as part of the program can also be seen here. Note that only points 1,3,5,7 have been moved up by the transform SOP:

HAPI_Groups_OutputOfMarhalGroupsIn.png