This example demonstrates the implementation and usage of custom exporters to be used in conjunction with the Iray SDK API. In this example the new exporter is defined in and used by the main application for simplicity. Note that it is possible to provide custom exporters via plugins, as for any other user defined class, see Plugins.
A simple exporter called "Vanilla exporter" will be used in this example to demonstrate the basic steps. This exporter is an illustrative skeleton that implements all interfaces but does not actually write elements out, it just writes their types and names.
#include <string>
#include <list>
#include <set>
#include <map>
#include <utility>
#include <sstream>
{
switch( severity) {
default: return "unknown" ;
}
}
class Vanilla_export_state
{
public:
: m_line_number( 1),
m_uri( uri ? uri : ""),
const char* get_uri() const { return m_uri.empty() ? 0 : m_uri.c_str(); }
mi::Uint32 get_line_number()
const {
return m_line_number; }
void set_line_number(
mi::Uint32 number) { m_line_number = number; }
void incr_line_number() { ++m_line_number; }
{
if( m_parent_state)
return m_parent_state.get();
}
private:
std::string m_uri;
};
class Vanilla_exporter
{
public:
{
return new Vanilla_export_state( uri, parent_state);
}
const char* get_supported_extensions(
mi::Uint32 i)
const
{
switch( i) {
case 0: return ".vnl";
case 1: return ".van";
default: return 0;
}
}
{
}
const char* get_name() const { return "NVIDIA example vanilla (v1) exporter"; }
const char* get_author() const { return "NVIDIA Corporation, Berlin, Germany"; }
{
return uuid;
}
mi::Uint32 get_major_version()
const {
return 1; }
mi::Uint32 get_minor_version()
const {
return 0; }
bool test_file_type( const char* extension) const;
const char* extension,
const char* rootgroup,
const char* caminst,
const char* options,
const char* extension,
private:
std::string message,
{
std::ostringstream s;
const char* uri = export_state->
get_uri();
s << (uri ? uri : "(no URI)")
<< "Vanilla exporter message " << message_number << ", "
<< "severity " << enum_to_str( message_severity) << ": "
<< message;
while( parent_state.is_valid_interface()) {
s << "\n included from: " << parent_state->get_uri()
<< ":" << parent_state->get_line_number();
parent_state = parent_state->get_parent_state();
}
return result;
}
static void write_name(
{
if( prefix.size() > 0 && 0 == name.compare( 0, prefix.size(), prefix))
writer->
writeline( name.c_str() + prefix.size());
else
}
{
if( group.is_valid_interface())
return "Group";
if( instance.is_valid_interface())
return "Instance";
return "Unknown";
}
};
bool Vanilla_exporter::test_file_type( const char* extension) const
{
return (len > 3)
&& (( 0 == strcmp( extension + len - 4, ".vnl"))
|| ( 0 == strcmp( extension + len - 4, ".van")));
}
bool Vanilla_exporter::test_file_type(
{
return test_file_type( extension);
}
const char* ,
const char* rootgroup,
const char* caminst,
const char* options,
{
if( !result)
return 0;
std::string strip_prefix;
if( exporter_options && exporter_options->
has_key(
"strip_prefix")) {
if( !option.is_valid_interface())
return report_message(
"The option 'strip_prefix' has an invalid type.", export_state);
strip_prefix = option->get_c_str();
}
std::list< std::pair< std::string, bool> > elements;
std::set< std::string> elements_exported;
if( options && options[0] != '\0')
elements.push_back( std::make_pair( std::string( options), false));
if( caminst && caminst[0] != '\0')
elements.push_back( std::make_pair( std::string( caminst), false));
elements.push_back( std::make_pair( std::string( rootgroup), false));
if( elements.front().second) {
std::string name = elements.front().first;
elements.pop_front();
if( elements_exported.find( name) == elements_exported.end()) {
elements_exported.insert( name);
transaction->
access( name.c_str()));
if( !element.is_valid_interface()) {
std::string message( "Element '");
message += name + "' does not exist in database, export failed.";
message, export_state);
}
writer->
writeline( get_element_type( element.get()).c_str());
write_name( name, strip_prefix, writer);
}
} else {
elements.front().second = true;
std::string name = elements.front().first;
transaction->
access( name.c_str()));
if( !element.is_valid_interface()) {
std::string message( "Element '");
message += name + "' does not exist in database, export failed.";
message, export_state);
}
std::string element_type = get_element_type( element.get());
if( element_type == "Group") {
const char* element_name = group->get_element( group_size - i -1);
if( elements_exported.find( element_name) == elements_exported.end())
elements.push_front( std::make_pair( std::string( element_name), false));
}
} else if( element_type == "Instance") {
const char* element_name = instance->get_item();
if( elements_exported.find( element_name) == elements_exported.end())
elements.push_front( std::make_pair( std::string( element_name), false));
}
}
}
return report_message(
result,
export_state);
return result;
}
const char* ,
{
if( !result)
return 0;
std::string strip_prefix;
if( exporter_options && exporter_options->
has_key(
"strip_prefix")) {
if( !option.is_valid_interface())
return report_message(
"The option 'strip_prefix' has an invalid type.", export_state);
strip_prefix = option->get_c_str();
}
if( !name.is_valid_interface()) {
"element array contains an invalid object", export_state);
}
const char* element_name = name->get_c_str();
if( !element.is_valid_interface()) {
std::string message( "Element '");
message += element_name;
message += "' does not exist in database, export failed.";
message, export_state);
}
writer->
writeline( get_element_type( element.get()).c_str());
write_name( element_name, strip_prefix, writer);
}
return result;
}
This interface represents static arrays, i.e., arrays with a fixed number of elements.
Definition: iarray.h:37
virtual const base::IInterface * get_element(Size index) const =0
Returns the index -th element of the array.
virtual Size get_length() const =0
Returns the size of the array.
virtual const base::IInterface * get_value(const char *key) const =0
Returns the value for key key.
virtual bool has_key(const char *key) const =0
Indicates whether the key key exists or not.
This interface represents maps, i.e., a key-value based data structure.
Definition: imap.h:41
A simple string class.
Definition: istring.h:22
Handle class template for interfaces, automatizing the lifetime control via reference counting.
Definition: handle.h:113
The basic extensible interface.
Definition: iinterface.h:103
Mixin class template for deriving interface implementations.
Definition: interface_implement.h:41
This interface represents the result of an export operation.
Definition: iexport_result.h:107
This interface represents the result of an export operation.
Definition: iexport_result.h:44
A group is a container for other scene elements.
Definition: igroup.h:39
This interface represents states that are passed to recursive calls of importers and exporters.
Definition: iimpexp_state.h:36
An instance is a scene element that adds a transformation and attributes to another scene element.
Definition: iinstance.h:117
virtual Sint32 get_error_number() const =0
Returns the error number of the last error that happened in this reader or writer,...
virtual const char * get_error_message() const =0
Returns the error message of the last error that happened in this reader or writer.
A transaction provides a consistent view on the database.
Definition: itransaction.h:81
virtual const base::IInterface * access(const char *name)=0
Retrieves an element from the database.
virtual base::IInterface * create(const char *type_name, Uint32 argc=0, const base::IInterface *argv[]=0)=0
Creates an object of the type type_name.
A writer supports binary block writes and string-oriented line writes that accept a zero-terminated s...
Definition: iwriter.h:27
virtual bool writeline(const char *str)=0
Writes a zero-terminated string to the stream.
virtual const IInterface * get_interface(const Uuid &interface_id) const =0
Acquires a const interface from another.
virtual Uint32 retain() const =0
Increments the reference count.
static const Dup_interface DUP_INTERFACE
Symbolic constant to trigger a special constructor in the Handle class.
Definition: handle.h:37
Uint32 m_id1
First value.
Definition: uuid.h:27
Uint32 m_id2
Second value.
Definition: uuid.h:28
Uint32 m_id3
Third value.
Definition: uuid.h:29
Uint32 m_id4
Fourth value.
Definition: uuid.h:30
Message_severity
Constants for possible message severities.
Definition: enums.h:31
@ MESSAGE_SEVERITY_FATAL
A fatal error has occurred.
Definition: enums.h:33
@ MESSAGE_SEVERITY_DEBUG
This is debug message.
Definition: enums.h:43
@ MESSAGE_SEVERITY_WARNING
A warning has occurred.
Definition: enums.h:37
@ MESSAGE_SEVERITY_INFO
This is a normal operational message.
Definition: enums.h:39
@ MESSAGE_SEVERITY_VERBOSE
This is a more verbose message.
Definition: enums.h:41
@ MESSAGE_SEVERITY_ERROR
An error has occurred.
Definition: enums.h:35
unsigned int Uint32
32-bit unsigned integer.
Definition: types.h:49
Uint64 Size
Unsigned integral type that is large enough to hold the size of all types.
Definition: types.h:112
virtual const IImpexp_state * get_parent_state() const =0
Returns the state of the parent importer or exporter.
virtual const char * get_uri() const =0
Returns the URI for this file.
virtual Sint32 message_push_back(Uint32 number, base::Message_severity severity, const char *message)=0
Appends a message number, severity, and message to the array of recorded message numbers,...
Impexp_priority
Confidence in capabilities of an importer or exporter.
Definition: iimpexp_base.h:31
virtual Uint32 get_line_number() const =0
Returns the line number after the last read or write operation.
@ IMPEXP_PRIORITY_WELL_DEFINED
The highest possible priority for importers or exporters in plugins provided in the Iray SDK.
Definition: iimpexp_base.h:37
Common namespace for APIs of NVIDIA Advanced Rendering Center GmbH.
Definition: neuraylib.h:179
A 128 bit representation of a universally unique identifier (UUID or GUID).
Definition: uuid.h:26
#include <iostream>
#include "example_shared.h"
#include "vanilla_exporter.h"
{
check_success( rendering_configuration->add_mdl_path( mdl_path) == 0);
check_success( rendering_configuration->add_mdl_path( ".") == 0);
check_success( extension_api.is_valid_interface());
exporter = new Vanilla_exporter;
check_success( extension_api->register_exporter( exporter.
get()) == 0);
check_success( plugin_configuration->load_plugin_library(
}
{
check_success( database.is_valid_interface());
database->get_global_scope());
scope->create_transaction());
check_success( transaction.is_valid_interface());
check_success( import_api.is_valid_interface());
import_api->import_elements( transaction.get(), uri->get_c_str()));
check_success( import_result->get_error_number() == 0);
const char* root_group = import_result->get_rootgroup();
check_success( export_api.is_valid_interface());
export_api->export_scene( transaction.get(), "file:test3.vnl", root_group));
check_success( export_result.is_valid_interface());
for(
mi::Size i = 0; i < export_result->get_messages_length(); ++i)
std::cout << export_result->get_message( i) << std::endl;
check_success( export_result->get_error_number() == 0);
transaction->commit();
}
int main( int argc, char* argv[])
{
if( argc != 3) {
std::cerr << "Usage: example_exporter <scene_file> <mdl_path>" << std::endl;
keep_console_open();
return EXIT_FAILURE;
}
const char* scene_file = argv[1];
const char* mdl_path = argv[2];
check_success( neuray.is_valid_interface());
configuration( neuray.get(), mdl_path);
check_start_success( result);
test_exporter( neuray.get(), scene_file);
check_success( neuray->
shutdown() == 0);
check_success( extension_api->unregister_exporter( exporter.
get()) == 0);
exporter = 0;
extension_api = 0;
neuray = 0;
check_success( unload());
keep_console_open();
return EXIT_SUCCESS;
}
This interface is used to interact with the distributed database.
Definition: idatabase.h:293
This interface is used to export files.
Definition: iexport_api.h:38
This interface is used to extent the Iray SDK API.
Definition: iextension_api.h:32
This interface is used to import files.
Definition: iimport_api.h:100
This is an object representing the Iray library.
Definition: ineuray.h:44
virtual Sint32 shutdown(bool blocking=true)=0
Shuts down the library.
virtual base::IInterface * get_api_component(const base::Uuid &uuid) const =0
Returns an API component from the Iray SDK API.
virtual Sint32 start(bool blocking=true)=0
Starts the operation of the Iray library.
This interface is used to load plugins and to query information about loaded plugins.
Definition: iplugin_configuration.h:24
This interface is used to query and change the rendering configuration.
Definition: irendering_configuration.h:109
#define MI_BASE_DLL_FILE_EXT
The operating system specific default filename extension for shared libraries (DLLs)
Definition: config.h:340
Interface * get() const
Access to the interface. Returns 0 for an invalid interface.
Definition: handle.h:294
signed int Sint32
32-bit signed integer.
Definition: types.h:46