MDL SDK API nvidia_logo_transpbg.gif Up
argument_editor.h
Go to the documentation of this file.
1/***************************************************************************************************
2 * Copyright 2024 NVIDIA Corporation. All rights reserved.
3 **************************************************************************************************/
6
7#ifndef MI_NEURAYLIB_ARGUMENT_EDITOR_H
8#define MI_NEURAYLIB_ARGUMENT_EDITOR_H
9
10#include <mi/base/handle.h>
11#include <mi/neuraylib/assert.h>
18#include <mi/neuraylib/itype.h>
19#include <mi/neuraylib/ivalue.h>
20
21#include <string>
22
23namespace mi {
24
25namespace neuraylib {
26
27class IMdl_execution_context;
28
57{
58public:
59
61
62
78 ITransaction* transaction,
79 const char* name,
80 IMdl_factory* mdl_factory,
81 bool intent_to_edit = false);
82
88 bool is_valid() const;
89
98 bool is_valid_instance( IMdl_execution_context* context) const;
99
109
114 Element_type get_type() const;
115
117 const char* get_definition() const;
118
120 const char* get_mdl_definition() const;
121
126 bool is_array_constructor() const;
127
129 bool is_declarative() const;
130
132 bool is_material() const;
133
135 const IType* get_return_type() const;
136
139
144 const char* get_parameter_name( Size index) const;
145
150 Size get_parameter_index( const char* name) const;
151
153 const IType_list* get_parameter_types() const;
154
161 bool is_parameter_enabled( Size index, IMdl_evaluator_api* evaluator) const;
162
164 const IExpression_list* get_arguments() const;
165
171 IExpression::Kind get_argument_kind( Size parameter_index) const;
172
178 IExpression::Kind get_argument_kind( const char* parameter_name) const;
179
181
183
199 Sint32 reset_argument( Size index);
200
217 Sint32 reset_argument( const char* name);
218
220
222
251 template<class T>
252 Sint32 get_value( Size parameter_index, T& value) const;
253
279 template <class T>
280 Sint32 get_value( const char* parameter_name, T& value) const;
281
283
285
302 template<class T>
303 Sint32 get_value( Size parameter_index, T* value, Size n) const;
304
318 template <class T>
319 Sint32 get_value( const char* parameter_name, T* value, Size n) const;
320
322
324
341 template<class T>
342 Sint32 get_value( Size parameter_index, Size component_index, T& value) const;
343
357 template<class T>
358 Sint32 get_value( const char* parameter_name, Size component_index, T& value) const;
359
376 template<class T>
377 Sint32 get_value( Size parameter_index, const char* field_name, T& value) const;
378
392 template <class T>
393 Sint32 get_value( const char* parameter_name, const char* field_name, T& value) const;
394
396
398
429 template<class T>
430 Sint32 set_value( Size parameter_index, const T& value);
431
459 template <class T>
460 Sint32 set_value( const char* parameter_name, const T& value);
461
463
465
487 template<class T>
488 Sint32 set_value( Size parameter_index, const T* value, Size n);
489
507 template <class T>
508 Sint32 set_value( const char* parameter_name, const T* value, Size n);
509
511
513
535 template<class T>
536 Sint32 set_value( Size parameter_index, Size component_index, const T& value);
537
556 template <class T>
557 Sint32 set_value( const char* parameter_name, Size component_index, const T& value);
558
577 template<class T>
578 Sint32 set_value( Size parameter_index, const char* field_name, const T& value);
579
595 template <class T>
596 Sint32 set_value( const char* parameter_name, const char* field_name, const T& value);
597
599
601
615 Sint32 get_array_length( Uint32 parameter_index, Size& size) const;
616
627 Sint32 get_array_length( const char* parameter_name, Size& size) const;
628
644 Sint32 set_array_size( Uint32 parameter_index, Size size);
645
658 Sint32 set_array_size( const char* parameter_name, Size size);
659
661
663
672 const char* get_call( Size parameter_index) const;
673
679 const char* get_call( const char* parameter_name) const;
680
704 Sint32 set_call( Size parameter_index, const char* call_name);
705
720 Sint32 set_call( const char* parameter_name, const char* call_name);
721
723
725
728
731
734
737
739 const IFunction_call* get_scene_element() const;
740
743
746
748 const std::string& get_name() const;
749
751
752private:
753 void promote_to_edit_if_needed();
754
755 base::Handle<ITransaction> m_transaction;
756 base::Handle<IMdl_factory> m_mdl_factory;
757 base::Handle<IValue_factory> m_value_factory;
758 base::Handle<IExpression_factory> m_expression_factory;
762 Element_type m_type;
763 std::string m_name;
764};
765 // end group mi_neuray_mdl_elements
767
769 ITransaction* transaction, const char* name, IMdl_factory* mdl_factory, bool intent_to_edit)
770{
771 mi_neuray_assert( transaction);
772 mi_neuray_assert( name);
773
774 m_transaction = make_handle_dup( transaction);
775 m_name = name;
776 m_mdl_factory = make_handle_dup( mdl_factory);
777 m_value_factory
778 = m_mdl_factory ? m_mdl_factory->create_value_factory( m_transaction.get()) : 0;
779 m_expression_factory
780 = m_mdl_factory ? m_mdl_factory->create_expression_factory( m_transaction.get()) : 0;
781
782 if( intent_to_edit) {
783 m_edit = transaction->edit<IFunction_call>( name);
784 m_access = m_edit;
785 m_old_access = m_edit;
786 m_type = m_access ? m_access->get_element_type() : static_cast<Element_type>( 0);
787 } else {
788 m_access = transaction->access<IFunction_call>( name);
789 m_type = m_access ? m_access->get_element_type() : static_cast<Element_type>( 0);
790 }
791}
792
793inline bool Argument_editor::is_valid() const
794{
795 return m_access.is_valid_interface();
796}
797
799{
800 if( !is_valid())
801 return false;
802
803 return m_access->is_valid( context);
804}
805
807{
808 if( !is_valid())
809 return false;
810
811 promote_to_edit_if_needed();
812
813 return m_edit->repair( flags, context);
814}
815
817{
818 return m_type;
819}
820
821inline const char* Argument_editor::get_definition() const
822{
823 if( !is_valid())
824 return 0;
825
826 return m_access->get_function_definition();
827}
828
829inline const char* Argument_editor::get_mdl_definition() const
830{
831 if( !is_valid())
832 return 0;
833
834 return m_access->get_mdl_function_definition();
835}
836
838{
839 if( !is_valid())
840 return false;
841
842 return m_access->is_array_constructor();
843}
844
846{
847 if( !is_valid())
848 return false;
849
850 return m_access->is_declarative();
851}
852
854{
855 if( !is_valid())
856 return false;
857
858 return m_access->is_material();
859}
860
862{
863 if( !is_valid())
864 return 0;
865
866 return m_access->get_parameter_count();
867}
868
869inline const char* Argument_editor::get_parameter_name( Size parameter_index) const
870{
871 if( !is_valid())
872 return 0;
873
874 return m_access->get_parameter_name( parameter_index);
875}
876
877inline Size Argument_editor::get_parameter_index( const char* name) const
878{
879 if( !is_valid())
880 return 0;
881
882 return m_access->get_parameter_index( name);
883}
884
886{
887 if( !is_valid())
888 return 0;
889
890 return m_access->get_return_type();
891}
892
894{
895 if( !is_valid())
896 return 0;
897
898 return m_access->get_parameter_types();
899}
900
902{
903 if( !evaluator)
904 return true;
905
906 if( !is_valid())
907 return true;
908
910 m_transaction.get(), m_value_factory.get(), m_access.get(), index, /*errors*/ 0));
911 if( !b)
912 return true;
913 return b->get_value();
914}
915
917{
918 if( !is_valid())
919 return -1;
920
921 promote_to_edit_if_needed();
922
923 return m_edit->reset_argument( index);
924}
925
927{
928 if( !is_valid())
929 return -1;
930
931 promote_to_edit_if_needed();
932
933 return m_edit->reset_argument( name);
934}
935
936
938{
939 if( !is_valid())
940 return 0;
941
942 return m_access->get_arguments();
943}
944
946{
947 if( !is_valid())
948 return static_cast<IExpression::Kind>( 0);
949
950 base::Handle<const IExpression_list> arguments( m_access->get_arguments());
951 base::Handle<const IExpression> argument( arguments->get_expression( parameter_index));
952 return argument ? argument->get_kind() : static_cast<IExpression::Kind>( 0);
953}
954
955inline IExpression::Kind Argument_editor::get_argument_kind( const char* parameter_name) const
956{
957 if( !is_valid())
958 return static_cast<IExpression::Kind>( 0);
959
960 base::Handle<const IExpression_list> arguments( m_access->get_arguments());
961 base::Handle<const IExpression> argument( arguments->get_expression( parameter_name));
962 return argument ? argument->get_kind() : static_cast<IExpression::Kind>( 0);
963}
964
965template <class T>
966Sint32 Argument_editor::get_value( Size parameter_index, T& value) const
967{
968 if( !is_valid())
969 return -1;
970
971 base::Handle<const IExpression_list> arguments( m_access->get_arguments());
972 base::Handle<const IExpression> argument( arguments->get_expression( parameter_index));
973 if( !argument)
974 return -2;
977 if( !argument_constant)
978 return -4;
979 base::Handle<const IValue> argument_value( argument_constant->get_value());
980 Sint32 result = neuraylib::get_value( argument_value.get(), value);
981 return result == 0 ? 0 : -5;
982}
983
984template <class T>
985Sint32 Argument_editor::get_value( const char* name, T& value) const
986{
987 if( !is_valid())
988 return -1;
989
990 base::Handle<const IExpression_list> arguments( m_access->get_arguments());
991 base::Handle<const IExpression> argument_( arguments->get_expression( name));
992 if( !argument_)
993 return -2;
995 argument_->get_interface<IExpression_constant>());
996 if( !argument_constant)
997 return -4;
998 base::Handle<const IValue> argument_value( argument_constant->get_value());
999 Sint32 result = neuraylib::get_value( argument_value.get(), value);
1000 return result == 0 ? 0 : -5;
1001}
1002
1003template <class T>
1004Sint32 Argument_editor::get_value( Size parameter_index, T* value, Size n) const
1005{
1006 if( !is_valid())
1007 return -1;
1008
1009 base::Handle<const IExpression_list> arguments( m_access->get_arguments());
1010 base::Handle<const IExpression> argument( arguments->get_expression( parameter_index));
1011 if( !argument)
1012 return -2;
1014 argument->get_interface<IExpression_constant>());
1015 if( !argument_constant)
1016 return -4;
1017 base::Handle<const IValue> argument_value( argument_constant->get_value());
1018 Sint32 result = neuraylib::get_value( argument_value.get(), value, n);
1019 return result == 0 ? 0 : -5;
1020}
1021
1022template <class T>
1023Sint32 Argument_editor::get_value( const char* name, T* value, Size n) const
1024{
1025 if( !is_valid())
1026 return -1;
1027
1028 base::Handle<const IExpression_list> arguments( m_access->get_arguments());
1029 base::Handle<const IExpression> argument_( arguments->get_expression( name));
1030 if( !argument_)
1031 return -2;
1033 argument_->get_interface<IExpression_constant>());
1034 if( !argument_constant)
1035 return -4;
1036 base::Handle<const IValue> argument_value( argument_constant->get_value());
1037 Sint32 result = neuraylib::get_value( argument_value.get(), value, n);
1038 return result == 0 ? 0 : -5;
1039}
1040
1041template <class T>
1042Sint32 Argument_editor::get_value( Size parameter_index, Size component_index, T& value) const
1043{
1044 if( !is_valid())
1045 return -1;
1046
1047 base::Handle<const IExpression_list> arguments( m_access->get_arguments());
1048 base::Handle<const IExpression> argument( arguments->get_expression( parameter_index));
1049 if( !argument)
1050 return -2;
1052 argument->get_interface<IExpression_constant>());
1053 if( !argument_constant)
1054 return -4;
1055 base::Handle<const IValue> argument_value( argument_constant->get_value());
1056 Sint32 result = neuraylib::get_value( argument_value.get(), component_index, value);
1057 return result == 0 ? 0 : (result == -3 ? -3 : -5);
1058}
1059
1060template <class T>
1061Sint32 Argument_editor::get_value( const char* parameter_name, Size component_index, T& value) const
1062{
1063 if( !is_valid())
1064 return -1;
1065
1066 base::Handle<const IExpression_list> arguments( m_access->get_arguments());
1067 base::Handle<const IExpression> argument( arguments->get_expression( parameter_name));
1068 if( !argument)
1069 return -2;
1071 argument->get_interface<IExpression_constant>());
1072 if( !argument_constant)
1073 return -4;
1074 base::Handle<const IValue> argument_value( argument_constant->get_value());
1075 Sint32 result = neuraylib::get_value( argument_value.get(), component_index, value);
1076 return result == 0 ? 0 : (result == -3 ? -3 : -5);
1077}
1078
1079template <class T>
1081 Size parameter_index, const char* field_name, T& value) const
1082{
1083 if( !is_valid())
1084 return -1;
1085
1086 base::Handle<const IExpression_list> arguments( m_access->get_arguments());
1087 base::Handle<const IExpression> argument( arguments->get_expression( parameter_index));
1088 if( !argument)
1089 return -2;
1091 argument->get_interface<IExpression_constant>());
1092 if( !argument_constant)
1093 return -4;
1094 base::Handle<const IValue> argument_value( argument_constant->get_value());
1095 Sint32 result = neuraylib::get_value( argument_value.get(), field_name, value);
1096 return result == 0 ? 0 : (result == -3 ? -3 : -5);
1097}
1098
1099template <class T>
1101 const char* parameter_name, const char* field_name, T& value) const
1102{
1103 if( !is_valid())
1104 return -1;
1105
1106 base::Handle<const IExpression_list> arguments( m_access->get_arguments());
1107 base::Handle<const IExpression> argument( arguments->get_expression( parameter_name));
1108 if( !argument)
1109 return -2;
1111 argument->get_interface<IExpression_constant>());
1112 if( !argument_constant)
1113 return -4;
1114 base::Handle<const IValue> argument_value( argument_constant->get_value());
1115 Sint32 result = neuraylib::get_value( argument_value.get(), field_name, value);
1116 return result == 0 ? 0 : (result == -3 ? -3 : -5);
1117}
1118
1119template <class T>
1120Sint32 Argument_editor::set_value( Size parameter_index, const T& value)
1121{
1122 if( !is_valid())
1123 return -1;
1124
1125 promote_to_edit_if_needed();
1126
1127 if( m_edit->is_default())
1128 return -4;
1129 base::Handle<const IExpression_list> arguments( m_edit->get_arguments());
1130 base::Handle<const IExpression> argument( arguments->get_expression( parameter_index));
1131 if( !argument)
1132 return -2;
1133 base::Handle<const IType> type( argument->get_type());
1134 base::Handle<IValue> new_value( m_value_factory->create( type.get()));
1135 Sint32 result = neuraylib::set_value( new_value.get(), value);
1136 if( result != 0)
1137 return -5;
1138 base::Handle<IExpression> new_expression(
1139 m_expression_factory->create_constant( new_value.get()));
1140 result = m_edit->set_argument( parameter_index, new_expression.get());
1141 mi_neuray_assert( result == 0);
1142 return result;
1143}
1144
1145template <class T>
1146Sint32 Argument_editor::set_value( const char* parameter_name, const T& value)
1147{
1148 if( !is_valid())
1149 return -1;
1150
1151 promote_to_edit_if_needed();
1152
1153 if( m_edit->is_default())
1154 return -4;
1155 base::Handle<const IExpression_list> arguments( m_edit->get_arguments());
1156 base::Handle<const IExpression> argument( arguments->get_expression( parameter_name));
1157 if( !argument)
1158 return -2;
1159 base::Handle<const IType> type( argument->get_type());
1160 base::Handle<IValue> new_value( m_value_factory->create( type.get()));
1161 Sint32 result = neuraylib::set_value( new_value.get(), value);
1162 if( result != 0)
1163 return -5;
1164 base::Handle<IExpression> new_expression(
1165 m_expression_factory->create_constant( new_value.get()));
1166 result = m_edit->set_argument( parameter_name, new_expression.get());
1167 mi_neuray_assert( result == 0);
1168 return result;
1169}
1170
1171template <class T>
1172Sint32 Argument_editor::set_value( Size parameter_index, const T* value, Size n)
1173{
1174 if( !is_valid())
1175 return -1;
1176
1177 promote_to_edit_if_needed();
1178
1179 if( m_edit->is_default())
1180 return -4;
1181 base::Handle<const IExpression_list> arguments( m_edit->get_arguments());
1182 base::Handle<const IExpression> argument( arguments->get_expression( parameter_index));
1183 if( !argument)
1184 return -2;
1185 base::Handle<const IType_array> type( argument->get_type<IType_array>());
1186 base::Handle<IValue_array> new_value( m_value_factory->create_array( type.get()));
1187 if( !new_value)
1188 return -5;
1189 if( !type->is_immediate_sized())
1190 new_value->set_size( n);
1191 Sint32 result = neuraylib::set_value( new_value.get(), value, n);
1192 if( result != 0)
1193 return -5;
1194 base::Handle<IExpression> new_expression(
1195 m_expression_factory->create_constant( new_value.get()));
1196 result = m_edit->set_argument( parameter_index, new_expression.get());
1197 mi_neuray_assert( result == 0);
1198 return result;
1199}
1200
1201template <class T>
1202Sint32 Argument_editor::set_value( const char* parameter_name, const T* value, Size n)
1203{
1204 if( !is_valid())
1205 return -1;
1206
1207 promote_to_edit_if_needed();
1208
1209 if( m_edit->is_default())
1210 return -4;
1211 base::Handle<const IExpression_list> arguments( m_edit->get_arguments());
1212 base::Handle<const IExpression> argument( arguments->get_expression( parameter_name));
1213 if( !argument)
1214 return -2;
1215 base::Handle<const IType_array> type( argument->get_type<IType_array>());
1216 base::Handle<IValue_array> new_value( m_value_factory->create_array( type.get()));
1217 if( !new_value)
1218 return -5;
1219 if( !type->is_immediate_sized())
1220 new_value->set_size( n);
1221 Sint32 result = neuraylib::set_value( new_value.get(), value, n);
1222 if( result != 0)
1223 return -5;
1224 base::Handle<IExpression> new_expression(
1225 m_expression_factory->create_constant( new_value.get()));
1226 result = m_edit->set_argument( parameter_name, new_expression.get());
1227 mi_neuray_assert( result == 0);
1228 return result;
1229}
1230
1231template <class T>
1232Sint32 Argument_editor::set_value( Size parameter_index, Size component_index, const T& value)
1233{
1234 if( !is_valid())
1235 return -1;
1236
1237 promote_to_edit_if_needed();
1238
1239 if( m_edit->is_default())
1240 return -4;
1241 base::Handle<const IExpression_list> arguments( m_edit->get_arguments());
1242 base::Handle<const IExpression> argument( arguments->get_expression( parameter_index));
1243 if( !argument)
1244 return -2;
1245 if( argument->get_kind() == IExpression::EK_CONSTANT) {
1246 // reuse existing constant expression
1247 base::Handle<IExpression> new_argument( m_expression_factory->clone( argument.get()));
1248 base::Handle<IExpression_constant> new_argument_constant(
1249 new_argument->get_interface<IExpression_constant>());
1250 base::Handle<IValue> new_value( new_argument_constant->get_value());
1251 Sint32 result = neuraylib::set_value( new_value.get(), component_index, value);
1252 if( result != 0)
1253 return result == -3 ? -3 : -5;
1254 result = m_edit->set_argument( parameter_index, new_argument.get());
1255 mi_neuray_assert( result == 0);
1256 return result;
1257 } else {
1258 // create new constant expression
1259 base::Handle<const IType> type( argument->get_type());
1260 base::Handle<IValue> new_value( m_value_factory->create( type.get()));
1261 Sint32 result = neuraylib::set_value( new_value.get(), component_index, value);
1262 if( result != 0)
1263 return result == -3 ? -3 : -5;
1264 base::Handle<IExpression> new_expression(
1265 m_expression_factory->create_constant( new_value.get()));
1266 result = m_edit->set_argument( parameter_index, new_expression.get());
1267 mi_neuray_assert( result == 0);
1268 return result;
1269 }
1270}
1271
1272template <class T>
1273Sint32 Argument_editor::set_value( const char* parameter_name, Size component_index, const T& value)
1274{
1275 if( !is_valid())
1276 return -1;
1277
1278 promote_to_edit_if_needed();
1279
1280 if( m_edit->is_default())
1281 return -4;
1282 base::Handle<const IExpression_list> arguments( m_edit->get_arguments());
1283 base::Handle<const IExpression> argument( arguments->get_expression( parameter_name));
1284 if( !argument)
1285 return -2;
1286 if( argument->get_kind() == IExpression::EK_CONSTANT) {
1287 // reuse existing constant expression
1288 base::Handle<IExpression> new_argument( m_expression_factory->clone( argument.get()));
1289 base::Handle<IExpression_constant> new_argument_constant(
1290 new_argument->get_interface<IExpression_constant>());
1291 base::Handle<IValue> new_value( new_argument_constant->get_value());
1292 Sint32 result = neuraylib::set_value( new_value.get(), component_index, value);
1293 if( result != 0)
1294 return result == -3 ? -3 : -5;
1295 result = m_edit->set_argument( parameter_name, new_argument.get());
1296 mi_neuray_assert( result == 0);
1297 return result;
1298 } else {
1299 // create new constant expression
1300 base::Handle<const IType> type( argument->get_type());
1301 base::Handle<IValue> new_value( m_value_factory->create( type.get()));
1302 Sint32 result = neuraylib::set_value( new_value.get(), component_index, value);
1303 if( result != 0)
1304 return result == -3 ? -3 : -5;
1305 base::Handle<IExpression> new_expression(
1306 m_expression_factory->create_constant( new_value.get()));
1307 result = m_edit->set_argument( parameter_name, new_expression.get());
1308 mi_neuray_assert( result == 0);
1309 return result;
1310 }
1311}
1312
1313template <class T>
1315 Size parameter_index, const char* field_name, const T& value)
1316{
1317 if( !is_valid())
1318 return -1;
1319
1320 promote_to_edit_if_needed();
1321
1322 if( m_edit->is_default())
1323 return -4;
1324 base::Handle<const IExpression_list> arguments( m_edit->get_arguments());
1325 base::Handle<const IExpression> argument( arguments->get_expression( parameter_index));
1326 if( !argument)
1327 return -2;
1328 if( argument->get_kind() == IExpression::EK_CONSTANT) {
1329 // reuse existing constant expression
1330 base::Handle<IExpression> new_argument( m_expression_factory->clone( argument.get()));
1331 base::Handle<IExpression_constant> new_argument_constant(
1332 new_argument->get_interface<IExpression_constant>());
1333 base::Handle<IValue> new_value( new_argument_constant->get_value());
1334 Sint32 result = neuraylib::set_value( new_value.get(), field_name, value);
1335 if( result != 0)
1336 return result == -3 ? -3 : -5;
1337 result = m_edit->set_argument( parameter_index, new_argument.get());
1338 mi_neuray_assert( result == 0);
1339 return result;
1340 } else {
1341 // create new constant expression
1342 base::Handle<const IType> type( argument->get_type());
1343 base::Handle<IValue> new_value( m_value_factory->create( type.get()));
1344 Sint32 result = neuraylib::set_value( new_value.get(), field_name, value);
1345 if( result != 0)
1346 return result == -3 ? -3 : -5;
1347 base::Handle<IExpression> new_expression(
1348 m_expression_factory->create_constant( new_value.get()));
1349 result = m_edit->set_argument( parameter_index, new_expression.get());
1350 mi_neuray_assert( result == 0);
1351 return result;
1352 }
1353}
1354
1355template <class T>
1357 const char* parameter_name, const char* field_name, const T& value)
1358{
1359 if( !is_valid())
1360 return -1;
1361
1362 promote_to_edit_if_needed();
1363
1364 if( m_edit->is_default())
1365 return -4;
1366 base::Handle<const IExpression_list> arguments( m_edit->get_arguments());
1367 base::Handle<const IExpression> argument( arguments->get_expression( parameter_name));
1368 if( !argument)
1369 return -2;
1370 if( argument->get_kind() == IExpression::EK_CONSTANT) {
1371 // reuse existing constant expression
1372 base::Handle<IExpression> new_argument( m_expression_factory->clone( argument.get()));
1373 base::Handle<IExpression_constant> new_argument_constant(
1374 new_argument->get_interface<IExpression_constant>());
1375 base::Handle<IValue> new_value( new_argument_constant->get_value());
1376 Sint32 result = neuraylib::set_value( new_value.get(), field_name, value);
1377 if( result != 0)
1378 return result == -3 ? -3 : -5;
1379 result = m_edit->set_argument( parameter_name, new_argument.get());
1380 mi_neuray_assert( result == 0);
1381 return result;
1382 } else {
1383 // create new constant expression
1384 base::Handle<const IType> type( argument->get_type());
1385 base::Handle<IValue> new_value( m_value_factory->create( type.get()));
1386 Sint32 result = neuraylib::set_value( new_value.get(), field_name, value);
1387 if( result != 0)
1388 return result == -3 ? -3 : -5;
1389 base::Handle<IExpression> new_expression(
1390 m_expression_factory->create_constant( new_value.get()));
1391 result = m_edit->set_argument( parameter_name, new_expression.get());
1392 mi_neuray_assert( result == 0);
1393 return result;
1394 }
1395}
1396
1397inline Sint32 Argument_editor::get_array_length( Uint32 parameter_index, Size& size) const
1398{
1399 if( !is_valid())
1400 return -1;
1401
1402 base::Handle<const IExpression_list> arguments( m_access->get_arguments());
1403 base::Handle<const IExpression> argument( arguments->get_expression( parameter_index));
1404 if( !argument)
1405 return -2;
1407 argument->get_interface<IExpression_constant>());
1408 if( !argument_constant)
1409 return -4;
1410 base::Handle<const IValue_array> value( argument_constant->get_value<IValue_array>());
1411 if( !value)
1412 return -5;
1413 size = value->get_size();
1414 return 0;
1415}
1416
1417inline Sint32 Argument_editor::get_array_length( const char* parameter_name, Size& size) const
1418{
1419 if( !is_valid())
1420 return -1;
1421
1422 base::Handle<const IExpression_list> arguments( m_access->get_arguments());
1423 base::Handle<const IExpression> argument( arguments->get_expression( parameter_name));
1424 if( !argument)
1425 return -2;
1427 argument->get_interface<IExpression_constant>());
1428 if( !argument_constant)
1429 return -4;
1430 base::Handle<const IValue_array> value( argument_constant->get_value<IValue_array>());
1431 if( !value)
1432 return -5;
1433 size = value->get_size();
1434 return 0;
1435}
1436
1438{
1439 if( !is_valid())
1440 return -1;
1441
1442 promote_to_edit_if_needed();
1443
1444 if( m_edit->is_default())
1445 return -4;
1446 base::Handle<const IExpression_list> arguments( m_edit->get_arguments());
1447 base::Handle<const IExpression> argument( arguments->get_expression( parameter_index));
1448 if( !argument)
1449 return -2;
1450 if( argument->get_kind() == IExpression::EK_CONSTANT) {
1451 // reuse existing constant expression
1452 base::Handle<IExpression> new_argument( m_expression_factory->clone( argument.get()));
1453 base::Handle<IExpression_constant> new_argument_constant(
1454 new_argument->get_interface<IExpression_constant>());
1456 new_argument_constant->get_value<IValue_array>());
1457 if( !new_value)
1458 return -5;
1459 Sint32 result = new_value->set_size( size);
1460 if( result != 0)
1461 return -5;
1462 result = m_edit->set_argument( parameter_index, new_argument.get());
1463 mi_neuray_assert( result == 0);
1464 return result;
1465 } else {
1466 // create new constant expression
1467 base::Handle<const IType> type( argument->get_type());
1469 m_value_factory->create<IValue_array>( type.get()));
1470 if( !new_value)
1471 return -5;
1472 Sint32 result = new_value->set_size( size);
1473 if( result != 0)
1474 return -5;
1475 base::Handle<IExpression> new_expression(
1476 m_expression_factory->create_constant( new_value.get()));
1477 result = m_edit->set_argument( parameter_index, new_expression.get());
1478 mi_neuray_assert( result == 0);
1479 return result;
1480 }
1481}
1482
1483inline Sint32 Argument_editor::set_array_size( const char* parameter_name, Size size)
1484{
1485 if( !is_valid())
1486 return -1;
1487
1488 promote_to_edit_if_needed();
1489
1490 if( m_edit->is_default())
1491 return -4;
1492 base::Handle<const IExpression_list> arguments( m_edit->get_arguments());
1493 base::Handle<const IExpression> argument( arguments->get_expression( parameter_name));
1494 if( !argument)
1495 return -2;
1496 if( argument->get_kind() == IExpression::EK_CONSTANT) {
1497 // reuse existing constant expression
1498 base::Handle<IExpression> new_argument( m_expression_factory->clone( argument.get()));
1499 base::Handle<IExpression_constant> new_argument_constant(
1500 new_argument->get_interface<IExpression_constant>());
1502 new_argument_constant->get_value<IValue_array>());
1503 if( !new_value)
1504 return -5;
1505 Sint32 result = new_value->set_size( size);
1506 if( result != 0)
1507 return -5;
1508 result = m_edit->set_argument( parameter_name, new_argument.get());
1509 mi_neuray_assert( result == 0);
1510 return result;
1511 } else {
1512 // create new constant expression
1513 base::Handle<const IType> type( argument->get_type());
1515 m_value_factory->create<IValue_array>( type.get()));
1516 if( !new_value)
1517 return -5;
1518 Sint32 result = new_value->set_size( size);
1519 if( result != 0)
1520 return -5;
1521 base::Handle<IExpression> new_expression(
1522 m_expression_factory->create_constant( new_value.get()));
1523 result = m_edit->set_argument( parameter_name, new_expression.get());
1524 mi_neuray_assert( result == 0);
1525 return result;
1526 }
1527}
1528
1529inline const char* Argument_editor::get_call( Size parameter_index) const
1530{
1531 if( !is_valid())
1532 return 0;
1533
1534 base::Handle<const IExpression_list> arguments( m_access->get_arguments());
1536 arguments->get_expression<IExpression_call>( parameter_index));
1537 if( !argument)
1538 return 0;
1539 return argument->get_call();
1540}
1541
1542inline const char* Argument_editor::get_call( const char* parameter_name) const
1543{
1544 if( !is_valid())
1545 return 0;
1546
1547 base::Handle<const IExpression_list> arguments( m_access->get_arguments());
1549 arguments->get_expression<IExpression_call>( parameter_name));
1550 if( !argument)
1551 return 0;
1552 return argument->get_call();
1553}
1554
1555inline Sint32 Argument_editor::set_call( Size parameter_index, const char* call_name)
1556{
1557 if( !is_valid())
1558 return -1;
1559
1560 promote_to_edit_if_needed();
1561
1562 base::Handle<IExpression_call> new_argument( m_expression_factory->create_call( call_name));
1563 if( !new_argument)
1564 return -6;
1565 return m_edit->set_argument( parameter_index, new_argument.get());
1566}
1567
1568inline Sint32 Argument_editor::set_call( const char* parameter_name, const char* call_name)
1569{
1570 if( !is_valid())
1571 return -1;
1572
1573 promote_to_edit_if_needed();
1574
1575 base::Handle<IExpression_call> new_argument( m_expression_factory->create_call( call_name));
1576 if( !new_argument)
1577 return -6;
1578 return m_edit->set_argument( parameter_name, new_argument.get());
1579}
1580
1582{
1583 m_transaction->retain();
1584 return m_transaction.get();
1585}
1586
1588{
1589 m_mdl_factory->retain();
1590 return m_mdl_factory.get();
1591}
1592
1594{
1595 m_value_factory->retain();
1596 return m_value_factory.get();
1597}
1598
1600{
1601 m_expression_factory->retain();
1602 return m_expression_factory.get();
1603}
1604
1606{
1607 m_access->retain();
1608 return m_access.get();
1609}
1610
1612{
1613 promote_to_edit_if_needed();
1614
1615 m_edit->retain();
1616 return m_edit.get();
1617}
1618
1620{
1621 return m_type;
1622}
1623
1624inline const std::string& Argument_editor::get_name() const
1625{
1626 return m_name;
1627}
1628
1629inline void Argument_editor::promote_to_edit_if_needed()
1630{
1631 if( m_edit)
1632 return;
1633
1634 m_edit = m_transaction->edit<IFunction_call>( m_name.c_str());
1635 mi_neuray_assert( m_edit);
1636 m_old_access = m_access;
1637 m_access = m_edit;
1638}
1639
1640} // namespace neuraylib
1641
1642} // namespace mi
1643
1644#endif // MI_NEURAYLIB_ARGUMENT_EDITOR_H
Handle class template for interfaces, automatizing the lifetime control via reference counting.
Definition: handle.h:113
A wrapper around the interface for MDL material instances and function calls.
Definition: argument_editor.h:57
bool is_valid() const
Indicates whether the argument editor is in a valid state.
Definition: argument_editor.h:793
bool is_declarative() const
Indicates whether the corresponding material or function definition is declarative.
Definition: argument_editor.h:845
IExpression::Kind get_argument_kind(Size parameter_index) const
Returns the expression kind of an argument.
Definition: argument_editor.h:945
IValue_factory * get_value_factory() const
Get the value factory.
Definition: argument_editor.h:1593
Sint32 set_value(Size parameter_index, const T &value)
Sets an argument.
Definition: argument_editor.h:1120
Sint32 set_array_size(Uint32 parameter_index, Size size)
Sets the length of a deferred-sized array argument.
Definition: argument_editor.h:1437
const std::string & get_name() const
Get the DB name of the MDL function call or material instance.
Definition: argument_editor.h:1624
bool is_material() const
Indicates whether the argument editor acts on a material instance.
Definition: argument_editor.h:853
bool is_array_constructor() const
Indicates whether the argument editor acts on a function call that is an instance of the array constr...
Definition: argument_editor.h:837
Sint32 get_value(Size parameter_index, T &value) const
Returns an argument (values of constants only, no calls).
Definition: argument_editor.h:966
const char * get_definition() const
Returns the DB name of the corresponding material or function definition.
Definition: argument_editor.h:821
IExpression_factory * get_expression_factory() const
Get the expression factory.
Definition: argument_editor.h:1599
const char * get_parameter_name(Size index) const
Returns the name of the parameter at index.
Definition: argument_editor.h:869
Element_type get_element_type() const
Get the element type.
Definition: argument_editor.h:1619
Size get_parameter_count() const
Returns the number of parameters.
Definition: argument_editor.h:861
Sint32 set_call(Size parameter_index, const char *call_name)
Sets an argument (call expressions only).
Definition: argument_editor.h:1555
const IFunction_call * get_scene_element() const
Get the MDL function call or material instance.
Definition: argument_editor.h:1605
Size get_parameter_index(const char *name) const
Returns the index position of a parameter.
Definition: argument_editor.h:877
bool is_parameter_enabled(Size index, IMdl_evaluator_api *evaluator) const
Checks the enable_if condition of the given parameter.
Definition: argument_editor.h:901
ITransaction * get_transaction() const
Get the transaction.
Definition: argument_editor.h:1581
Sint32 reset_argument(Size index)
Resets the argument at index.
Definition: argument_editor.h:916
const IExpression_list * get_arguments() const
Returns all arguments.
Definition: argument_editor.h:937
const IType * get_return_type() const
Returns the return type.
Definition: argument_editor.h:885
bool is_valid_instance(IMdl_execution_context *context) const
Indicates whether the material instance or function call referenced by this argument editor is valid.
Definition: argument_editor.h:798
Element_type get_type() const
Indicates whether the argument editor acts on a material instance or on a function call.
Definition: argument_editor.h:816
const char * get_call(Size parameter_index) const
Returns an argument (call expressions only).
Definition: argument_editor.h:1529
const char * get_mdl_definition() const
Returns the MDL name of the corresponding material or function definition.
Definition: argument_editor.h:829
Argument_editor(ITransaction *transaction, const char *name, IMdl_factory *mdl_factory, bool intent_to_edit=false)
Constructs an MDL argument editor for a fixed material instance or function call.
Definition: argument_editor.h:768
mi::Sint32 repair(mi::Uint32 flags, IMdl_execution_context *context)
Attempts to repair an invalid material instance or function call.
Definition: argument_editor.h:806
IMdl_factory * get_mdl_factory() const
Get the MDL factory.
Definition: argument_editor.h:1587
Sint32 get_array_length(Uint32 parameter_index, Size &size) const
Returns the length of an array argument.
Definition: argument_editor.h:1397
const IType_list * get_parameter_types() const
Returns the types of all parameters.
Definition: argument_editor.h:893
An indirect call expression.
Definition: iexpression.h:175
A constant expression.
Definition: iexpression.h:96
The interface for creating expressions.
Definition: iexpression.h:650
An ordered collection of expressions identified by name or index.
Definition: iexpression.h:317
Kind
The possible kinds of expressions.
Definition: iexpression.h:53
@ EK_CONSTANT
A constant expression. See mi::neuraylib::IExpression_constant.
Definition: iexpression.h:55
This interface represents a function call.
Definition: ifunction_call.h:52
Provides access to various functions for the evaluation of MDL expressions.
Definition: imdl_evaluator_api.h:27
virtual const IValue_bool * is_function_parameter_enabled(ITransaction *trans, IValue_factory *fact, const IFunction_call *call, Size index, Sint32 *errors) const =0
Evaluates if a function call parameter is enabled, i.e., the enable_if condition evaluates to true).
The execution context can be used to query status information like error and warning messages concern...
Definition: imdl_execution_context.h:131
Factory for various MDL interfaces and functions.
Definition: imdl_factory.h:53
virtual Element_type get_element_type() const =0
Indicates the actual scene element represented by interfaces derived from this interface.
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.
The type of kind array.
Definition: itype.h:452
An ordered collection of types identified by name or index.
Definition: itype.h:646
The interface to MDL types.
Definition: itype.h:151
A value of type array.
Definition: ivalue.h:403
The interface for creating values.
Definition: ivalue.h:660
Handle<Interface> make_handle_dup(Interface *iptr)
Converts passed-in interface pointer to a handle, without taking interface over.
Definition: handle.h:439
Handle<New_interface> get_interface() const
Returns a new handle for a possibly different interface type, similar to a dynamic cast,...
Definition: handle.h:353
Interface * get() const
Access to the interface. Returns 0 for an invalid interface.
Definition: handle.h:294
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
#define mi_neuray_assert(expr)
If expr evaluates to true this macro shall have no effect.
Definition: assert.h:67
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
mi::Sint32 get_value(const mi::neuraylib::IValue *value, T &v)
Simplifies reading the value of mi::neuraylib::IValue into the corresponding classes from the base an...
Definition: ivalue.h:1254
Element_type
Distinguishes scene elements.
Definition: iscene_element.h:30
Smart-pointer handle class for interfaces, const and non-const version.
Expressions of the MDL type system.
Scene element Function_call.
Scene element Material_instance.
API component that gives access to the MDL evaluator.
API component that gives access to some MDL functionality.
Database transactions.
Types of the MDL type system.
Values of the MDL type system.
Common namespace for APIs of NVIDIA Advanced Rendering Center GmbH.
Definition: example_derivatives.dox:5
Assertions and compile-time assertions.