This example creates material instances and function calls by instantiating the corresponding material and function definitions. It also shows how to change arguments of material instances and function calls.
#include <iostream>
#include <string>
#include "example_shared.h"
const char* material_definition_name
= "mdl::nvidia::sdk_examples::tutorials::example_material(color,float)";
const char* material_instance_name = "instance of example_material";
const char* function_definition_name
= "mdl::nvidia::sdk_examples::tutorials::example_function(color,float)";
const char* function_call_name = "call of example_function";
template <class T>
void dump_instance(
{
mi::Size count = material->get_parameter_count();
for(
mi::Size index = 0; index < count; index++) {
arguments->get_expression( index));
std::string name = material->get_parameter_name( index);
expression_factory->
dump( argument.get(), name.c_str(), 1));
s << " argument " << argument_text->get_c_str() << std::endl;
}
s << std::endl;
}
void instantiate_definitions(
{
mdl_factory->create_value_factory( transaction));
mdl_factory->create_expression_factory( transaction));
mdl_factory->create_execution_context());
check_success( mdl_impexp_api->load_module(
transaction, "::nvidia::sdk_examples::tutorials", context.get()) >= 0);
print_messages( context.get());
check_success( module.is_valid_interface());
{
material_definition_name));
material_definition->create_function_call( 0, &result));
check_success( result == 0);
std::cout << "Dumping material instance \"" << material_instance_name << "\":" << std::endl;
dump_instance( expression_factory.get(), material_instance.get(), std::cout);
transaction->
store( material_instance.get(), material_instance_name);
}
{
function_definition_name));
value_factory->create_color( 1.0f, 0.0f, 0.0f));
arguments->add_expression( "tint", tint_expr.get());
value_factory->create_float( 2.0f));
arguments->add_expression( "distance", distance_expr.get());
function_definition->create_function_call( arguments.get(), &result));
check_success( result == 0);
std::cout << "Dumping function call \"" << function_call_name << "\":" << std::endl;
dump_instance( expression_factory.get(), function_call.get(), std::cout);
transaction->
store( function_call.get(), function_call_name);
}
}
{
mdl_factory->create_value_factory( transaction));
mdl_factory->create_expression_factory( transaction));
{
check_success( material_instance.is_valid_interface());
material_instance->get_arguments());
arguments->get_expression( "tint"));
expression_factory->
clone( argument.get()));
check_success( material_instance->set_argument( "tint", new_argument.get()) == 0);
std::cout << "Dumping modified material instance \"" << material_instance_name << "\":"
<< std::endl;
dump_instance( expression_factory.get(), material_instance.get(), std::cout);
}
{
{
transaction, function_call_name, mdl_factory.get());
check_success( argument_editor.set_value( "tint", blue) == 0);
}
check_success( function_call.is_valid_interface());
std::cout << "Dumping modified function call \"" << function_call_name << "\":"
<< std::endl;
dump_instance( expression_factory.get(), function_call.get(), std::cout);
}
}
void print_annotations(
{
std::cout << "There are "
<< annotations.get_annotation_count() << " annotation(s).\n";
for(
mi::Size a = 0; a < annotations.get_annotation_count(); ++a)
{
std::string name = anno_def->get_mdl_simple_name();
std::cout << " \"" << name << "\" with "
<< annotations.get_annotation_param_count( a) << " parameter(s):\n";
for(
mi::Size p = 0; p < annotations.get_annotation_param_count( a); ++p)
{
annotations.get_annotation_param_type( a, p));
std::cout << " \"" << annotations.get_annotation_param_name( a, p)
<< "\" of type \""
<< type_text->get_c_str() << "\" = ";
switch( type_handle->get_kind())
{
{
const char* string_value = nullptr;
annotations.get_annotation_param_value<const char*>( a, p, string_value);
std::cout << "\"" << string_value << "\"\n";
break;
}
{
annotations.get_annotation_param_value<
mi::Float32>( a, p, float_value);
std::cout << float_value << "\n";
break;
}
default:
{
annotations.get_annotation_param_value( a, p)).
get() <<
"\n";
break;
}
}
}
}
std::cout << "\n";
std::cout << "Index of \"hard_range\": " << annotations.get_annotation_index(
"::anno::hard_range(float,float)") << "\n";
std::cout <<
"Index of \"foo\": " <<
static_cast<mi::Sint32>( annotations.get_annotation_index(
"::anno::foo(int)")) << " (which is not present)\n";
const char* descValue = nullptr;
mi::Sint32 res = annotations.get_annotation_param_value_by_name<
const char*>(
"::anno::description(string)", 0, descValue);
std::cout << R"(Value of "description": ")" << ( res == 0 ? descValue : "nullptr") << "\"\n";
res = annotations.get_annotation_param_value_by_name<
mi::Sint32>(
"::anno::foo(int)", 0, fooValue);
if ( res != 0)
std::cout << "Value of \"foo\" not found (annotation is not present)\n";
std::cout << std::endl;
}
{
mdl_factory->create_type_factory( transaction));
mdl_factory->create_value_factory( transaction));
mdl_factory->create_expression_factory( transaction));
material_instance->get_arguments());
expression_factory->
clone( arguments.get()));
value_factory->create_string(
"a variant of ::nvidia::sdk_examples::tutorials::example_material"
" with different defaults"));
anno_args->add_expression( "description", anno_arg_expression.get());
expression_factory->
create_annotation(
"::anno::description(string)", anno_args.get()));
range_args->add_expression( "min", range_min_expression.get());
range_args->add_expression( "max", range_max_expression.get());
"::anno::hard_range(float,float)", range_args.get()));
print_annotations( type_factory.get(), anno_block.get());
mdl_factory->create_execution_context());
mdl_factory->create_module_builder(
transaction,
"mdl::variants",
context.get()));
check_success( module_builder);
check_success( module_builder->add_variant(
"green_example_material",
material_definition_name,
defaults.get(),
anno_block.get(),
nullptr,
true,
true,
context.get()) == 0);
"mdl::variants::green_example_material(color,float)"));
material_instance = material_definition->create_function_call( 0, &result);
check_success( result == 0);
std::cout << "Dumping material instance with defaults of material definition "
"\"mdl::variants::green_example_material\":" << std::endl;
dump_instance( expression_factory.get(), material_instance.get(), std::cout);
check_success( mdl_impexp_api->export_module( transaction, "mdl::variants", "variants.mdl") == 0);
}
int MAIN_UTF8( int , char* [])
{
if (!neuray.is_valid_interface())
exit_failure("Failed to load the SDK.");
if (!mi::examples::mdl::configure(neuray.get()))
exit_failure("Failed to initialize the SDK.");
if (ret != 0)
exit_failure("Failed to initialize the SDK. Result code: %d", ret);
{
{
instantiate_definitions( neuray.get(), transaction.get());
change_arguments( neuray.get(), transaction.get());
create_variant( neuray.get(), transaction.get());
}
}
exit_failure("Failed to shutdown the SDK.");
neuray = nullptr;
if (!mi::examples::mdl::unload())
exit_failure("Failed to unload the SDK.");
exit_success();
}
COMMANDLINE_TO_UTF8
Handle class template for interfaces, automatizing the lifetime control via reference counting.
Definition: handle.h:113
Standard RGBA color class with floating point elements and operations.
Definition: color.h:81
A wrapper around the interfaces for MDL annotations.
Definition: annotation_wrapper.h:37
A wrapper around the interface for MDL material instances and function calls.
Definition: argument_editor.h:57
An annotation block is an array of annotations.
Definition: iexpression.h:575
virtual Sint32 add_annotation(IAnnotation *annotation)=0
Adds an annotation at the end of the annotation block.
virtual const IAnnotation * get_annotation(Size index) const =0
Returns the annotation for index, or NULL if index is out of bounds.
This interface is used to interact with the distributed database.
Definition: idatabase.h:289
A constant expression.
Definition: iexpression.h:96
The interface for creating expressions.
Definition: iexpression.h:650
virtual const IString * dump(const IExpression *expr, const char *name, Size depth=0) const =0
Returns a textual representation of an expression.
virtual IExpression_list * create_expression_list() const =0
Creates a new expression list.
virtual IExpression * clone(const IExpression *expr) const =0
Clones the given expression.
virtual IAnnotation * create_annotation(const char *name, const IExpression_list *arguments) const =0
Creates a new annotation.
virtual IAnnotation_block * create_annotation_block() const =0
Creates a new annotation block.
virtual IExpression_constant * create_constant(IValue *value) const =0
Creates a constant (mutable).
This interface represents a function call.
Definition: ifunction_call.h:52
This interface represents a function definition.
Definition: ifunction_definition.h:44
Factory for various MDL interfaces and functions.
Definition: imdl_factory.h:53
API component for MDL related import and export operations.
Definition: imdl_impexp_api.h:43
This interface represents an MDL module.
Definition: imodule.h:634
This is an object representing the MDL SDK 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 MDL SDK API.
virtual Sint32 start(bool blocking=true)=0
Starts the operation of the MDL SDK library.
A transaction provides a consistent view on the database.
Definition: itransaction.h:82
virtual const base::IInterface * access(const char *name)=0
Retrieves an element from the database.
virtual base::IInterface * edit(const char *name)=0
Retrieves an element from the database and returns it ready for editing.
virtual Sint32 commit()=0
Commits the transaction.
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.
The interface for creating types.
Definition: itype.h:716
virtual const IString * dump(const IStruct_category *struct_category, Size depth=0) const =0
Returns a textual representation of a struct category.
@ TK_STRING
The string type. See mi::neuraylib::IType_string.
Definition: itype.h:168
@ TK_FLOAT
The float type. See mi::neuraylib::IType_float.
Definition: itype.h:164
A value of type color.
Definition: ivalue.h:374
Interface * get() const
Access to the interface. Returns 0 for an invalid interface.
Definition: handle.h:294
Uint64 Size
Unsigned integral type that is large enough to hold the size of all types.
Definition: types.h:112
float Float32
32-bit float.
Definition: types.h:51
signed int Sint32
32-bit signed integer.
Definition: types.h:46
mi::Sint32 set_value(mi::neuraylib::IValue *value, const T &v)
Simplifies setting the value of mi::neuraylib::IValue from the corresponding classes from the base an...
Definition: ivalue.h:925
@ MDL_VERSION_1_0
MDL version 1.0.
Definition: iexpression.h:29
@ MDL_VERSION_LATEST
Latest MDL version.
Definition: iexpression.h:40