This example demonstrates the implementation and usage of custom importers to be used in conjunction with the Iray SDK API. In this example the new importer is defined in and used by the main application for simplicity. Note that it is possible to provide custom importers via plugins, as for any other user defined class, see Plugins.
A simple importer called "Vanilla importer" will be used in this example to demonstrate the basic steps. This importer is an illustrative skeleton that implements all interfaces but does not actually parse the file content in a meaningful way.
#include <string>
#include <sstream>
{
switch( severity) {
default: return "unknown" ;
}
}
class Vanilla_import_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_importer
{
public:
{
return new Vanilla_import_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) importer"; }
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,
private:
std::string message,
{
std::ostringstream s;
<< "Vanilla importer 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;
}
};
bool Vanilla_importer::test_file_type( const char* extension) const
{
return (len > 3)
&& (( 0 == strcmp( extension + len - 4, ".vnl"))
|| ( 0 == strcmp( extension + len - 4, ".van")));
}
bool Vanilla_importer::test_file_type(
{
const char** buffer = 0;
return ( n >= 8) && (0 == std::strncmp( *buffer, "VANILLA", 7))
&& (((*buffer)[7] == '\n') || ((*buffer)[7] == '\r'));
}
return (len > 3)
&& (( 0 == strcmp( extension + len - 4, ".vnl"))
|| ( 0 == strcmp( extension + len - 4, ".van")));
}
const char* extension,
{
if( !result)
return 0;
std::string prefix;
if( importer_options && importer_options->
has_key(
"prefix")) {
prefix = option->get_c_str();
}
bool list_elements = false;
if( importer_options && importer_options->
has_key(
"list_elements")) {
list_elements = option->get_value<bool>();
}
std::string root_group_name = prefix + "Vanilla::root_group";
mi::Sint32 error_code = transaction->
store( rootgroup.get(), root_group_name.c_str());
if( error_code != 0)
"failed to create the root group", import_state);
if( list_elements)
char buffer[257];
while( reader->
readline( buffer, 257) && buffer[0] !=
'\0') {
break;
mi::Size ext_len = std::strlen( extension);
&& (ext_len > 3)
&& 0 == strcmp( extension + ext_len - 4, ".vnl")) {
if( !import_api.is_valid_interface())
"did not get a valid IImport_api object, import failed", import_state);
child_prefix->set_c_str( "Prefix_");
importer_options));
child_importer_options->erase( "prefix");
child_importer_options->insert( "prefix", child_prefix.get());
import_api->import_elements( transaction, "file:test2.van",
child_importer_options.get(), import_state));
if( !include_result.is_valid_interface())
"import was not able to create result object, import failed", import_state);
if( list_elements)
if( include_result->get_error_number() > 0) {
"including file 'test2.van' failed.", import_state);
} else {
if( 0 == include_result->get_rootgroup())
"include file 'test2.van' did not contain a rootgroup", import_state);
else
rootgroup->attach( include_result->get_rootgroup());
}
}
"test message in line 4", import_state);
"test message in line 4", import_state);
"test message in line 4", import_state);
"test message in line 4", import_state);
"test message in line 4", import_state);
std::string group_name = prefix + "Vanilla::Group1";
mi::Sint32 error_code = transaction->
store( group.get(), group_name.c_str());
if( error_code != 0)
"unexpected error in line 4", import_state);
else {
rootgroup->attach( group_name.c_str());
if( list_elements)
}
}
if( (len > 0) && ('\n' == buffer[len-1]))
}
return result;
}
return report_message(
result,
import_state);
}
This interface represents bool.
Definition: inumber.h:122
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
Mixin class template for deriving interface implementations.
Definition: interface_implement.h:41
This API component allows the creation, assignment, and cloning of instances of types.
Definition: ifactory.h:35
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
This interface is used to import files.
Definition: iimport_api.h:100
This interface represents the result of an import operation.
Definition: iimport_result.h:129
This interface represents the result of an import operation.
Definition: iimport_result.h:44
This abstract interface gives access to the Iray SDK API to plugins.
Definition: iplugin_api.h:27
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.
virtual bool eof() const =0
Returns true if the end of the file has been reached.
A reader supports binary block reads and string-oriented line reads that zero-terminate the result.
Definition: ireader.h:27
virtual bool supports_lookahead() const =0
Indicates whether lookahead is (in principle) supported.
virtual bool readline(char *buffer, Sint32 size)=0
Reads a line from the stream.
virtual Sint64 lookahead(Sint64 size, const char **buffer) const =0
Gives access to the lookahead data.
A transaction provides a consistent view on the database.
Definition: itransaction.h:81
virtual bool is_open() const =0
Indicates whether the transaction is open.
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.
virtual base::IInterface * edit(const char *name)=0
Retrieves an element from the database and returns it ready for editing.
virtual Sint32 store(base::IInterface *db_element, const char *name, Uint8 privacy=LOCAL_SCOPE)=0
Stores the element db_element in the database under the name name and with the privacy level privacy.
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
long long Sint64
64-bit signed integer.
Definition: types.h:61
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
signed int Sint32
32-bit signed integer.
Definition: types.h:46
virtual const IImpexp_state * get_parent_state() const =0
Returns the state of the parent importer or exporter.
virtual void incr_line_number()=0
Convenience function that increments the line number by one.
virtual Sint32 set_rootgroup(const char *group)=0
Sets the name of the root group.
virtual Sint32 append_messages(const IImport_result *import_result)=0
Appends all messages in import_result to this instance.
virtual const char * get_uri() const =0
Returns the URI for this file.
virtual Sint32 element_push_back(const char *element)=0
Appends an element to the array of recorded elements.
Impexp_priority
Confidence in capabilities of an importer or exporter.
Definition: iimpexp_base.h:31
virtual Sint32 append_elements(const IImport_result *import_result)=0
Appends all elements in import_result to this instance.
virtual Uint32 get_line_number() const =0
Returns the line number after the last read or write operation.
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_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_importer.h"
{
check_success( extension_api.is_valid_interface());
check_success( plugin_api.is_valid_interface());
importer = new Vanilla_importer( plugin_api.get());
check_success( extension_api->register_importer( importer.
get()) == 0);
}
{
check_success( database.is_valid_interface());
database->get_global_scope());
scope->create_transaction());
check_success( transaction.is_valid_interface());
list_elements->set_value( true);
importer_options->insert( "list_elements", list_elements.get());
check_success( import_api.is_valid_interface());
import_api->import_elements( transaction.get(), "file:test1.vnl", importer_options.get()));
check_success( import_result.is_valid_interface());
for(
mi::Size i = 0; i < import_result->get_messages_length(); ++i)
std::cout << import_result->get_message( i) << std::endl;
for(
mi::Size i = 0; i < import_result->get_elements_length(); ++i)
std::cout << import_result->get_element( i) << std::endl;
transaction->commit();
}
int main( int , char* [])
{
check_success( neuray.is_valid_interface());
configuration( neuray.get());
check_start_success( result);
test_importer( neuray.get());
check_success( neuray->
shutdown() == 0);
check_success( extension_api->unregister_importer( importer.
get()) == 0);
importer = 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 extent the Iray SDK API.
Definition: iextension_api.h:32
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.
Interface * get() const
Access to the interface. Returns 0 for an invalid interface.
Definition: handle.h:294