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

For documentation on the nodes APIs, see Nodes.

Simple example that checks for editable node networks, queries child nodes, creates a node, renames it, connects it to existing nodes, and then deletes it.

#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;
}
std::vector< HAPI_NodeId >
print_network_child_nodes( HAPI_AssetId asset_id, HAPI_NodeId network_node_id )
{
// Get node network child count.
HAPI_NodeInfo editable_network_node_info;
CHECK( HAPI_GetNodeInfo( nullptr, network_node_id, &editable_network_node_info ) );
cout << "Editable Node Network Child Count: "
<< editable_network_node_info.childNodeCount << endl;
// Get the child node ids.
std::vector< HAPI_NodeId > child_node_ids(
editable_network_node_info.childNodeCount );
nullptr,
network_node_id,
child_node_ids.data(),
editable_network_node_info.childNodeCount ) );
// Print the child node ids.
cout << "Child Node Ids:" << endl;
for ( auto child_node_id : child_node_ids )
{
HAPI_NodeInfo child_node_info;
CHECK( HAPI_GetNodeInfo( nullptr, child_node_id, &child_node_info ) );
cout << " "
<< child_node_id
<< " - "
<< ( child_node_info.createdPostAssetLoad ? "NEW" : "EXISTING" )
<< endl;
}
// Print the asset's only geo part count.
HAPI_GeoInfo geo_info;
CHECK( HAPI_GetGeoInfo( nullptr, asset_id, 0, 0, &geo_info ) );
cout << "Display Geo Part Count: " << geo_info.partCount << endl;
// Just add a newline for neatness.
cout << endl;
return child_node_ids;
}
int main()
{
// Initialize HAPI.
nullptr, &cook_options, false, -1, nullptr, nullptr, nullptr, nullptr ) );
// Load the library from file.
HAPI_AssetLibraryId library_id = -1;
nullptr, "HAPI_Test_Nodes_EditableNodeNetwork.otl",
true, &library_id ) );
// Instantiate the asset.
HAPI_AssetId asset_id = -1;
nullptr, "Object/HAPI_Test_Nodes_EditableNodeNetwork",
true, &asset_id ) );
// Get the editable node network count.
HAPI_AssetInfo asset_info;
CHECK( HAPI_GetAssetInfo( nullptr, asset_id, &asset_info ) );
cout << "Editable Node Network Count: " << asset_info.editableNodeNetworkCount << endl;
// The the editable node network node id.
HAPI_NodeId editable_network_node_id;
CHECK( HAPI_GetEditableNodeNetworks( nullptr, asset_id, &editable_network_node_id, 1 ) );
cout << "Editable Node Network Node Id: " << editable_network_node_id << endl;
// Just add a newline for neatness.
cout << endl;
// Print and get the current set of node network child nodes.
cout << "AFTER FIRST COOK" << endl;
std::vector< HAPI_NodeId > child_node_ids =
print_network_child_nodes( asset_id, editable_network_node_id );
// Create a box SOP node inside the editable node network.
HAPI_NodeId box_node_id;
CHECK( HAPI_CreateNode( nullptr, editable_network_node_id, "box", &box_node_id ) );
// Rename the box SOP node to something cooler.
CHECK( HAPI_RenameNode( nullptr, box_node_id, "cool_cube" ) );
// Connect the box to our existing merge SOP inside the node network.
const HAPI_NodeId merge_node_id = child_node_ids[ 0 ];
CHECK( HAPI_ConnectNodeInput( nullptr, merge_node_id, 1, box_node_id ) );
// Cook the asset.
CHECK( HAPI_CookAsset( nullptr, asset_id, nullptr ) );
// Confirm the connection to the cool_cube node.
HAPI_NodeId connected_node_id;
CHECK( HAPI_QueryNodeInput( nullptr, merge_node_id, 1, &connected_node_id ) );
assert( connected_node_id == box_node_id );
// Reprint the node network's children.
cout << "AFTER CREATE NODE" << endl;
print_network_child_nodes( asset_id, editable_network_node_id );
// Save the HIP file.
CHECK( HAPI_SaveHIPFile( nullptr, "ChangedEditableNodeNetwork.hip", true ) );
// Now disconnect and delete the node we added.
cout << "AFTER DELETE NODE" << endl;
CHECK( HAPI_DisconnectNodeInput( nullptr, merge_node_id, 1 ) );
CHECK( HAPI_DeleteNode( nullptr, box_node_id ) );
// Cook the asset.
CHECK( HAPI_CookAsset( nullptr, asset_id, nullptr ) );
// Reprint the node network's children.
print_network_child_nodes( asset_id, editable_network_node_id );
// Cleanup.
CHECK( HAPI_Cleanup( nullptr ) );
// Wait to terminate.
char in;
cout << "Press any key to exit." << endl;
cin >> in;
}