The example below gives an illustration of extracting both the diffuse texture map from the spaceship asset (available from www.orbolt.com), as well as extracting the Mantra rendered image planes of C (diffuse color), A (alpha), and N (normal) in the UV space of the asset.
#include <stdlib.h>
#include <iostream>
#include <string>
#define ENSURE_SUCCESS( result ) \
if ( (result) != HAPI_RESULT_SUCCESS ) \
{ \
std::cout << "failure at " << __FILE__ << ":" \
<< __LINE__ << std::endl; \
std::cout << get_last_error() << std::endl; \
exit( 1 ); \
}
#define ENSURE_COOK_SUCCESS( result ) \
if ( (result) != HAPI_STATE_READY ) \
{ \
std::cout << "failure at " << __FILE__ << ":" \
<< __LINE__ << std::endl; \
std::cout << get_last_cook_error() << std::endl; \
exit( 1 ); \
}
static void dump_asset_materials(
static void dump_part_diffuse_texturemap(
int asset_id, int object_id, int geo_id, int part_id );
static void dump_part_mantra_render(
int asset_id, int object_id, int geo_id, int part_id );
static void extract_image_planes(
int asset_id,
const char * base_file_name );
static std::string get_last_error();
static std::string get_last_cook_error();
static void wait_for_cook();
int main( int argc, char **argv )
{
const char *otl_file = argc == 2 ? argv[ 1 ] : "test_asset.otl";
nullptr,
&cook_options,
true,
-1,
nullptr,
nullptr,
nullptr,
nullptr
) );
int library_id;
int asset_id;
nullptr,
otl_file,
true,
{
std::cout << "Could not load " << otl_file << std::endl;
exit( 1 );
}
nullptr, library_id, &asset_name_sh, 1 ) );
std::string asset_name = get_string( asset_name_sh );
nullptr,
asset_name.c_str(),
true,
{
std::cout << "Could not instantiate asset " << asset_name << std::endl;
exit( 1 );
}
wait_for_cook();
dump_asset_materials( asset_id, asset_info );
return 0;
}
static void wait_for_cook()
{
int status;
do
{
}
ENSURE_COOK_SUCCESS( status );
}
static void dump_asset_materials(
{
nullptr,
for (
int object_index = 0; object_index < asset_info.
objectCount;
++object_index )
{
for (
int geo_index = 0; geo_index < object_info.
geoCount;
++geo_index )
{
nullptr,
asset_id, object_info.
id, geo_index, &geo_info ) );
for (
int part_index = 0; part_index < geo_info.
partCount;
++part_index )
{
dump_part_diffuse_texturemap(
asset_id, object_info.
id, geo_info.
id, part_index );
}
}
}
delete [] object_infos;
}
static int find_parm_index(
const char * parm_name_in )
{
int parm_index = -1;
for (
int i = 0; i < node_info.
parmCount; ++i )
{
std::string parm_name = get_string( parm_infos[ i ].nameSH );
if ( parm_name == parm_name_in )
{
parm_index = i;
break;
}
}
return parm_index;
}
static void extract_image_planes(
int asset_id,
{
nullptr, asset_id, material_info.
id, & image_info ) );
std::string image_format_name =
std::cout <<
"Image width=" << image_info.
xRes <<
" height="
<< image_info.
yRes <<
" Format:"
<< image_format_name << std::endl;
int num_image_planes;
nullptr, asset_id, material_info.
id, &num_image_planes ) );
std::cout << "Number of image planes" << num_image_planes << std::endl;
int * image_planes = new int[ num_image_planes ];
nullptr, asset_id, material_info.
id,
image_planes, num_image_planes ) );
for ( int ii = 0; ii < num_image_planes; ii++ )
{
std::string image_plane_name = get_string( image_planes[ ii ] );
std::cout
<< "Imange plane [" << ii << "] = "
<< image_plane_name << std::endl;
int destination_file_path;
nullptr,
asset_id, material_info.
id,
image_plane_name.c_str(),
"C:/test",
nullptr,
&destination_file_path );
std::string destination_file_path_str =
get_string( destination_file_path );
std::cout
<< "File written to "
<< destination_file_path_str << std::endl;
}
delete[] image_planes;
}
static void dump_part_diffuse_texturemap(
int asset_id, int object_id, int geo_id, int part_id )
{
std::cout
<< "object " << object_id << ", geo " << geo_id
<< ", part " << part_id << std::endl;
nullptr,
asset_id, object_id, geo_id, part_id, &part_info ) );
nullptr,
asset_id, object_id, geo_id, part_id, NULL, &material_id, 0, 1 ) );
nullptr, asset_id, material_id, &material_info ) );
{
std::cout
<< "No material found:" << "object " << object_id
<< ", geo " << geo_id
<< ", part " << part_id << std::endl;
return;
}
nullptr, material_info.
nodeId, &node_info ) );
nullptr,
int base_color_map_parm_index = find_parm_index(
node_info, parm_infos, "baseColorMap" );
if ( base_color_map_parm_index < 0 )
{
std::cout
<< "Could not find parameter for diffuse texture map."
<< std::endl;
return;
}
nullptr,
asset_id, material_info.
id,
parm_infos[ base_color_map_parm_index ].
id ) );
extract_image_planes( asset_id, material_info );
}
{
if ( string_handle == 0 )
return "";
int buffer_length;
nullptr, string_handle, &buffer_length ) );
char * buf = new char[ buffer_length ];
nullptr, string_handle, buf, buffer_length ) );
std::string result( buf );
delete[] buf;
return result;
}
static std::string get_last_error()
{
int buffer_length;
nullptr,
char * buf = new char[ buffer_length ];
std::string result( buf );
delete[] buf;
return result;
}
static std::string get_last_cook_error()
{
int buffer_length;
nullptr,
char * buf = new char[ buffer_length ];
std::string result( buf );
delete[] buf;
return result;
}