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_material() const;
130
132 const IType* get_return_type() const;
133
136
141 const char* get_parameter_name( Size index) const;
142
147 Size get_parameter_index( const char* name) const;
148
150 const IType_list* get_parameter_types() const;
151
158 bool is_parameter_enabled( Size index, IMdl_evaluator_api* evaluator) const;
159
161 const IExpression_list* get_arguments() const;
162
168 IExpression::Kind get_argument_kind( Size parameter_index) const;
169
175 IExpression::Kind get_argument_kind( const char* parameter_name) const;
176
178
180
196 Sint32 reset_argument( Size index);
197
214 Sint32 reset_argument( const char* name);
215
217
219
248 template<class T>
249 Sint32 get_value( Size parameter_index, T& value) const;
250
276 template <class T>
277 Sint32 get_value( const char* parameter_name, T& value) const;
278
280
282
299 template<class T>
300 Sint32 get_value( Size parameter_index, T* value, Size n) const;
301
315 template <class T>
316 Sint32 get_value( const char* parameter_name, T* value, Size n) const;
317
319
321
338 template<class T>
339 Sint32 get_value( Size parameter_index, Size component_index, T& value) const;
340
354 template<class T>
355 Sint32 get_value( const char* parameter_name, Size component_index, T& value) const;
356
373 template<class T>
374 Sint32 get_value( Size parameter_index, const char* field_name, T& value) const;
375
389 template <class T>
390 Sint32 get_value( const char* parameter_name, const char* field_name, T& value) const;
391
393
395
426 template<class T>
427 Sint32 set_value( Size parameter_index, const T& value);
428
456 template <class T>
457 Sint32 set_value( const char* parameter_name, const T& value);
458
460
462
484 template<class T>
485 Sint32 set_value( Size parameter_index, const T* value, Size n);
486
504 template <class T>
505 Sint32 set_value( const char* parameter_name, const T* value, Size n);
506
508
510
532 template<class T>
533 Sint32 set_value( Size parameter_index, Size component_index, const T& value);
534
553 template <class T>
554 Sint32 set_value( const char* parameter_name, Size component_index, const T& value);
555
574 template<class T>
575 Sint32 set_value( Size parameter_index, const char* field_name, const T& value);
576
592 template <class T>
593 Sint32 set_value( const char* parameter_name, const char* field_name, const T& value);
594
596
598
612 Sint32 get_array_length( Uint32 parameter_index, Size& size) const;
613
624 Sint32 get_array_length( const char* parameter_name, Size& size) const;
625
641 Sint32 set_array_size( Uint32 parameter_index, Size size);
642
655 Sint32 set_array_size( const char* parameter_name, Size size);
656
658
660
669 const char* get_call( Size parameter_index) const;
670
676 const char* get_call( const char* parameter_name) const;
677
695 Sint32 set_call( Size parameter_index, const char* call_name);
696
711 Sint32 set_call( const char* parameter_name, const char* call_name);
712
714
716
719
722
725
728
730 const IFunction_call* get_scene_element() const;
731
734
737
739 const std::string& get_name() const;
740
742
743private:
744 void promote_to_edit_if_needed();
745
746 base::Handle<ITransaction> m_transaction;
747 base::Handle<IMdl_factory> m_mdl_factory;
748 base::Handle<IValue_factory> m_value_factory;
749 base::Handle<IExpression_factory> m_expression_factory;
753 Element_type m_type;
754 std::string m_name;
755};
756 // end group mi_neuray_mdl_elements
758
760 ITransaction* transaction, const char* name, IMdl_factory* mdl_factory, bool intent_to_edit)
761{
762 mi_neuray_assert( transaction);
763 mi_neuray_assert( name);
764
765 m_transaction = make_handle_dup( transaction);
766 m_name = name;
767 m_mdl_factory = make_handle_dup( mdl_factory);
768 m_value_factory
769 = m_mdl_factory ? m_mdl_factory->create_value_factory( m_transaction.get()) : 0;
770 m_expression_factory
771 = m_mdl_factory ? m_mdl_factory->create_expression_factory( m_transaction.get()) : 0;
772
773 if( intent_to_edit) {
774 m_edit = transaction->edit<IFunction_call>( name);
775 m_access = m_edit;
776 m_old_access = m_edit;
777 m_type = m_access ? m_access->get_element_type() : static_cast<Element_type>( 0);
778 } else {
779 m_access = transaction->access<IFunction_call>( name);
780 m_type = m_access ? m_access->get_element_type() : static_cast<Element_type>( 0);
781 }
782}
783
784inline bool Argument_editor::is_valid() const
785{
786 return m_access.is_valid_interface();
787}
788
790{
791 if( !is_valid())
792 return false;
793
794 return m_access->is_valid( context);
795}
796
798{
799 if( !is_valid())
800 return false;
801
802 promote_to_edit_if_needed();
803
804 return m_edit->repair( flags, context);
805}
806
808{
809 return m_type;
810}
811
812inline const char* Argument_editor::get_definition() const
813{
814 if( !is_valid())
815 return 0;
816
817 return m_access->get_function_definition();
818}
819
820inline const char* Argument_editor::get_mdl_definition() const
821{
822 if( !is_valid())
823 return 0;
824
825 return m_access->get_mdl_function_definition();
826}
827
829{
830 if( !is_valid())
831 return false;
832
833 return m_access->is_array_constructor();
834}
835
837{
838 if( !is_valid())
839 return false;
840
841 return m_access->is_material();
842}
843
845{
846 if( !is_valid())
847 return 0;
848
849 return m_access->get_parameter_count();
850}
851
852inline const char* Argument_editor::get_parameter_name( Size parameter_index) const
853{
854 if( !is_valid())
855 return 0;
856
857 return m_access->get_parameter_name( parameter_index);
858}
859
860inline Size Argument_editor::get_parameter_index( const char* name) const
861{
862 if( !is_valid())
863 return 0;
864
865 return m_access->get_parameter_index( name);
866}
867
869{
870 if( !is_valid())
871 return 0;
872
873 return m_access->get_return_type();
874}
875
877{
878 if( !is_valid())
879 return 0;
880
881 return m_access->get_parameter_types();
882}
883
885{
886 if( !evaluator)
887 return true;
888
889 if( !is_valid())
890 return true;
891
893 m_transaction.get(), m_value_factory.get(), m_access.get(), index, /*error*/ 0));
894 if( !b)
895 return true;
896 return b->get_value();
897}
898
900{
901 if( !is_valid())
902 return -1;
903
904 promote_to_edit_if_needed();
905
906 return m_edit->reset_argument( index);
907}
908
910{
911 if( !is_valid())
912 return -1;
913
914 promote_to_edit_if_needed();
915
916 return m_edit->reset_argument( name);
917}
918
919
921{
922 if( !is_valid())
923 return 0;
924
925 return m_access->get_arguments();
926}
927
929{
930 if( !is_valid())
931 return static_cast<IExpression::Kind>( 0);
932
933 base::Handle<const IExpression_list> arguments( m_access->get_arguments());
934 base::Handle<const IExpression> argument( arguments->get_expression( parameter_index));
935 return argument ? argument->get_kind() : static_cast<IExpression::Kind>( 0);
936}
937
938inline IExpression::Kind Argument_editor::get_argument_kind( const char* parameter_name) const
939{
940 if( !is_valid())
941 return static_cast<IExpression::Kind>( 0);
942
943 base::Handle<const IExpression_list> arguments( m_access->get_arguments());
944 base::Handle<const IExpression> argument( arguments->get_expression( parameter_name));
945 return argument ? argument->get_kind() : static_cast<IExpression::Kind>( 0);
946}
947
948template <class T>
949Sint32 Argument_editor::get_value( Size parameter_index, T& value) const
950{
951 if( !is_valid())
952 return -1;
953
954 base::Handle<const IExpression_list> arguments( m_access->get_arguments());
955 base::Handle<const IExpression> argument( arguments->get_expression( parameter_index));
956 if( !argument)
957 return -2;
960 if( !argument_constant)
961 return -4;
962 base::Handle<const IValue> argument_value( argument_constant->get_value());
963 Sint32 result = neuraylib::get_value( argument_value.get(), value);
964 return result == 0 ? 0 : -5;
965}
966
967template <class T>
968Sint32 Argument_editor::get_value( const char* name, T& value) const
969{
970 if( !is_valid())
971 return -1;
972
973 base::Handle<const IExpression_list> arguments( m_access->get_arguments());
974 base::Handle<const IExpression> argument_( arguments->get_expression( name));
975 if( !argument_)
976 return -2;
978 argument_->get_interface<IExpression_constant>());
979 if( !argument_constant)
980 return -4;
981 base::Handle<const IValue> argument_value( argument_constant->get_value());
982 Sint32 result = neuraylib::get_value( argument_value.get(), value);
983 return result == 0 ? 0 : -5;
984}
985
986template <class T>
987Sint32 Argument_editor::get_value( Size parameter_index, T* value, Size n) const
988{
989 if( !is_valid())
990 return -1;
991
992 base::Handle<const IExpression_list> arguments( m_access->get_arguments());
993 base::Handle<const IExpression> argument( arguments->get_expression( parameter_index));
994 if( !argument)
995 return -2;
998 if( !argument_constant)
999 return -4;
1000 base::Handle<const IValue> argument_value( argument_constant->get_value());
1001 Sint32 result = neuraylib::get_value( argument_value.get(), value, n);
1002 return result == 0 ? 0 : -5;
1003}
1004
1005template <class T>
1006Sint32 Argument_editor::get_value( const char* name, T* value, Size n) const
1007{
1008 if( !is_valid())
1009 return -1;
1010
1011 base::Handle<const IExpression_list> arguments( m_access->get_arguments());
1012 base::Handle<const IExpression> argument_( arguments->get_expression( name));
1013 if( !argument_)
1014 return -2;
1016 argument_->get_interface<IExpression_constant>());
1017 if( !argument_constant)
1018 return -4;
1019 base::Handle<const IValue> argument_value( argument_constant->get_value());
1020 Sint32 result = neuraylib::get_value( argument_value.get(), value, n);
1021 return result == 0 ? 0 : -5;
1022}
1023
1024template <class T>
1025Sint32 Argument_editor::get_value( Size parameter_index, Size component_index, T& value) const
1026{
1027 if( !is_valid())
1028 return -1;
1029
1030 base::Handle<const IExpression_list> arguments( m_access->get_arguments());
1031 base::Handle<const IExpression> argument( arguments->get_expression( parameter_index));
1032 if( !argument)
1033 return -2;
1035 argument->get_interface<IExpression_constant>());
1036 if( !argument_constant)
1037 return -4;
1038 base::Handle<const IValue> argument_value( argument_constant->get_value());
1039 Sint32 result = neuraylib::get_value( argument_value.get(), component_index, value);
1040 return result == 0 ? 0 : (result == -3 ? -3 : -5);
1041}
1042
1043template <class T>
1044Sint32 Argument_editor::get_value( const char* parameter_name, Size component_index, T& value) const
1045{
1046 if( !is_valid())
1047 return -1;
1048
1049 base::Handle<const IExpression_list> arguments( m_access->get_arguments());
1050 base::Handle<const IExpression> argument( arguments->get_expression( parameter_name));
1051 if( !argument)
1052 return -2;
1054 argument->get_interface<IExpression_constant>());
1055 if( !argument_constant)
1056 return -4;
1057 base::Handle<const IValue> argument_value( argument_constant->get_value());
1058 Sint32 result = neuraylib::get_value( argument_value.get(), component_index, value);
1059 return result == 0 ? 0 : (result == -3 ? -3 : -5);
1060}
1061
1062template <class T>
1064 Size parameter_index, const char* field_name, T& value) const
1065{
1066 if( !is_valid())
1067 return -1;
1068
1069 base::Handle<const IExpression_list> arguments( m_access->get_arguments());
1070 base::Handle<const IExpression> argument( arguments->get_expression( parameter_index));
1071 if( !argument)
1072 return -2;
1074 argument->get_interface<IExpression_constant>());
1075 if( !argument_constant)
1076 return -4;
1077 base::Handle<const IValue> argument_value( argument_constant->get_value());
1078 Sint32 result = neuraylib::get_value( argument_value.get(), field_name, value);
1079 return result == 0 ? 0 : (result == -3 ? -3 : -5);
1080}
1081
1082template <class T>
1084 const char* parameter_name, const char* field_name, T& value) const
1085{
1086 if( !is_valid())
1087 return -1;
1088
1089 base::Handle<const IExpression_list> arguments( m_access->get_arguments());
1090 base::Handle<const IExpression> argument( arguments->get_expression( parameter_name));
1091 if( !argument)
1092 return -2;
1094 argument->get_interface<IExpression_constant>());
1095 if( !argument_constant)
1096 return -4;
1097 base::Handle<const IValue> argument_value( argument_constant->get_value());
1098 Sint32 result = neuraylib::get_value( argument_value.get(), field_name, value);
1099 return result == 0 ? 0 : (result == -3 ? -3 : -5);
1100}
1101
1102template <class T>
1103Sint32 Argument_editor::set_value( Size parameter_index, const T& value)
1104{
1105 if( !is_valid())
1106 return -1;
1107
1108 promote_to_edit_if_needed();
1109
1110 if( m_edit->is_default())
1111 return -4;
1112 base::Handle<const IExpression_list> arguments( m_edit->get_arguments());
1113 base::Handle<const IExpression> argument( arguments->get_expression( parameter_index));
1114 if( !argument)
1115 return -2;
1116 base::Handle<const IType> type( argument->get_type());
1117 base::Handle<IValue> new_value( m_value_factory->create( type.get()));
1118 Sint32 result = neuraylib::set_value( new_value.get(), value);
1119 if( result != 0)
1120 return -5;
1121 base::Handle<IExpression> new_expression(
1122 m_expression_factory->create_constant( new_value.get()));
1123 result = m_edit->set_argument( parameter_index, new_expression.get());
1124 mi_neuray_assert( result == 0);
1125 return result;
1126}
1127
1128template <class T>
1129Sint32 Argument_editor::set_value( const char* parameter_name, const T& value)
1130{
1131 if( !is_valid())
1132 return -1;
1133
1134 promote_to_edit_if_needed();
1135
1136 if( m_edit->is_default())
1137 return -4;
1138 base::Handle<const IExpression_list> arguments( m_edit->get_arguments());
1139 base::Handle<const IExpression> argument( arguments->get_expression( parameter_name));
1140 if( !argument)
1141 return -2;
1142 base::Handle<const IType> type( argument->get_type());
1143 base::Handle<IValue> new_value( m_value_factory->create( type.get()));
1144 Sint32 result = neuraylib::set_value( new_value.get(), value);
1145 if( result != 0)
1146 return -5;
1147 base::Handle<IExpression> new_expression(
1148 m_expression_factory->create_constant( new_value.get()));
1149 result = m_edit->set_argument( parameter_name, new_expression.get());
1150 mi_neuray_assert( result == 0);
1151 return result;
1152}
1153
1154template <class T>
1155Sint32 Argument_editor::set_value( Size parameter_index, const T* value, Size n)
1156{
1157 if( !is_valid())
1158 return -1;
1159
1160 promote_to_edit_if_needed();
1161
1162 if( m_edit->is_default())
1163 return -4;
1164 base::Handle<const IExpression_list> arguments( m_edit->get_arguments());
1165 base::Handle<const IExpression> argument( arguments->get_expression( parameter_index));
1166 if( !argument)
1167 return -2;
1168 base::Handle<const IType_array> type( argument->get_type<IType_array>());
1169 base::Handle<IValue_array> new_value( m_value_factory->create_array( type.get()));
1170 if( !new_value)
1171 return -5;
1172 if( !type->is_immediate_sized())
1173 new_value->set_size( n);
1174 Sint32 result = neuraylib::set_value( new_value.get(), value, n);
1175 if( result != 0)
1176 return -5;
1177 base::Handle<IExpression> new_expression(
1178 m_expression_factory->create_constant( new_value.get()));
1179 result = m_edit->set_argument( parameter_index, new_expression.get());
1180 mi_neuray_assert( result == 0);
1181 return result;
1182}
1183
1184template <class T>
1185Sint32 Argument_editor::set_value( const char* parameter_name, const T* value, Size n)
1186{
1187 if( !is_valid())
1188 return -1;
1189
1190 promote_to_edit_if_needed();
1191
1192 if( m_edit->is_default())
1193 return -4;
1194 base::Handle<const IExpression_list> arguments( m_edit->get_arguments());
1195 base::Handle<const IExpression> argument( arguments->get_expression( parameter_name));
1196 if( !argument)
1197 return -2;
1198 base::Handle<const IType_array> type( argument->get_type<IType_array>());
1199 base::Handle<IValue_array> new_value( m_value_factory->create_array( type.get()));
1200 if( !new_value)
1201 return -5;
1202 if( !type->is_immediate_sized())
1203 new_value->set_size( n);
1204 Sint32 result = neuraylib::set_value( new_value.get(), value, n);
1205 if( result != 0)
1206 return -5;
1207 base::Handle<IExpression> new_expression(
1208 m_expression_factory->create_constant( new_value.get()));
1209 result = m_edit->set_argument( parameter_name, new_expression.get());
1210 mi_neuray_assert( result == 0);
1211 return result;
1212}
1213
1214template <class T>
1215Sint32 Argument_editor::set_value( Size parameter_index, Size component_index, const T& value)
1216{
1217 if( !is_valid())
1218 return -1;
1219
1220 promote_to_edit_if_needed();
1221
1222 if( m_edit->is_default())
1223 return -4;
1224 base::Handle<const IExpression_list> arguments( m_edit->get_arguments());
1225 base::Handle<const IExpression> argument( arguments->get_expression( parameter_index));
1226 if( !argument)
1227 return -2;
1228 if( argument->get_kind() == IExpression::EK_CONSTANT) {
1229 // reuse existing constant expression
1230 base::Handle<IExpression> new_argument( m_expression_factory->clone( argument.get()));
1231 base::Handle<IExpression_constant> new_argument_constant(
1232 new_argument->get_interface<IExpression_constant>());
1233 base::Handle<IValue> new_value( new_argument_constant->get_value());
1234 Sint32 result = neuraylib::set_value( new_value.get(), component_index, value);
1235 if( result != 0)
1236 return result == -3 ? -3 : -5;
1237 result = m_edit->set_argument( parameter_index, new_argument.get());
1238 mi_neuray_assert( result == 0);
1239 return result;
1240 } else {
1241 // create new constant expression
1242 base::Handle<const IType> type( argument->get_type());
1243 base::Handle<IValue> new_value( m_value_factory->create( type.get()));
1244 Sint32 result = neuraylib::set_value( new_value.get(), component_index, value);
1245 if( result != 0)
1246 return result == -3 ? -3 : -5;
1247 base::Handle<IExpression> new_expression(
1248 m_expression_factory->create_constant( new_value.get()));
1249 result = m_edit->set_argument( parameter_index, new_expression.get());
1250 mi_neuray_assert( result == 0);
1251 return result;
1252 }
1253}
1254
1255template <class T>
1256Sint32 Argument_editor::set_value( const char* parameter_name, Size component_index, const T& value)
1257{
1258 if( !is_valid())
1259 return -1;
1260
1261 promote_to_edit_if_needed();
1262
1263 if( m_edit->is_default())
1264 return -4;
1265 base::Handle<const IExpression_list> arguments( m_edit->get_arguments());
1266 base::Handle<const IExpression> argument( arguments->get_expression( parameter_name));
1267 if( !argument)
1268 return -2;
1269 if( argument->get_kind() == IExpression::EK_CONSTANT) {
1270 // reuse existing constant expression
1271 base::Handle<IExpression> new_argument( m_expression_factory->clone( argument.get()));
1272 base::Handle<IExpression_constant> new_argument_constant(
1273 new_argument->get_interface<IExpression_constant>());
1274 base::Handle<IValue> new_value( new_argument_constant->get_value());
1275 Sint32 result = neuraylib::set_value( new_value.get(), component_index, value);
1276 if( result != 0)
1277 return result == -3 ? -3 : -5;
1278 result = m_edit->set_argument( parameter_name, new_argument.get());
1279 mi_neuray_assert( result == 0);
1280 return result;
1281 } else {
1282 // create new constant expression
1283 base::Handle<const IType> type( argument->get_type());
1284 base::Handle<IValue> new_value( m_value_factory->create( type.get()));
1285 Sint32 result = neuraylib::set_value( new_value.get(), component_index, value);
1286 if( result != 0)
1287 return result == -3 ? -3 : -5;
1288 base::Handle<IExpression> new_expression(
1289 m_expression_factory->create_constant( new_value.get()));
1290 result = m_edit->set_argument( parameter_name, new_expression.get());
1291 mi_neuray_assert( result == 0);
1292 return result;
1293 }
1294}
1295
1296template <class T>
1298 Size parameter_index, const char* field_name, const T& value)
1299{
1300 if( !is_valid())
1301 return -1;
1302
1303 promote_to_edit_if_needed();
1304
1305 if( m_edit->is_default())
1306 return -4;
1307 base::Handle<const IExpression_list> arguments( m_edit->get_arguments());
1308 base::Handle<const IExpression> argument( arguments->get_expression( parameter_index));
1309 if( !argument)
1310 return -2;
1311 if( argument->get_kind() == IExpression::EK_CONSTANT) {
1312 // reuse existing constant expression
1313 base::Handle<IExpression> new_argument( m_expression_factory->clone( argument.get()));
1314 base::Handle<IExpression_constant> new_argument_constant(
1315 new_argument->get_interface<IExpression_constant>());
1316 base::Handle<IValue> new_value( new_argument_constant->get_value());
1317 Sint32 result = neuraylib::set_value( new_value.get(), field_name, value);
1318 if( result != 0)
1319 return result == -3 ? -3 : -5;
1320 result = m_edit->set_argument( parameter_index, new_argument.get());
1321 mi_neuray_assert( result == 0);
1322 return result;
1323 } else {
1324 // create new constant expression
1325 base::Handle<const IType> type( argument->get_type());
1326 base::Handle<IValue> new_value( m_value_factory->create( type.get()));
1327 Sint32 result = neuraylib::set_value( new_value.get(), field_name, value);
1328 if( result != 0)
1329 return result == -3 ? -3 : -5;
1330 base::Handle<IExpression> new_expression(
1331 m_expression_factory->create_constant( new_value.get()));
1332 result = m_edit->set_argument( parameter_index, new_expression.get());
1333 mi_neuray_assert( result == 0);
1334 return result;
1335 }
1336}
1337
1338template <class T>
1340 const char* parameter_name, const char* field_name, const T& value)
1341{
1342 if( !is_valid())
1343 return -1;
1344
1345 promote_to_edit_if_needed();
1346
1347 if( m_edit->is_default())
1348 return -4;
1349 base::Handle<const IExpression_list> arguments( m_edit->get_arguments());
1350 base::Handle<const IExpression> argument( arguments->get_expression( parameter_name));
1351 if( !argument)
1352 return -2;
1353 if( argument->get_kind() == IExpression::EK_CONSTANT) {
1354 // reuse existing constant expression
1355 base::Handle<IExpression> new_argument( m_expression_factory->clone( argument.get()));
1356 base::Handle<IExpression_constant> new_argument_constant(
1357 new_argument->get_interface<IExpression_constant>());
1358 base::Handle<IValue> new_value( new_argument_constant->get_value());
1359 Sint32 result = neuraylib::set_value( new_value.get(), field_name, value);
1360 if( result != 0)
1361 return result == -3 ? -3 : -5;
1362 result = m_edit->set_argument( parameter_name, new_argument.get());
1363 mi_neuray_assert( result == 0);
1364 return result;
1365 } else {
1366 // create new constant expression
1367 base::Handle<const IType> type( argument->get_type());
1368 base::Handle<IValue> new_value( m_value_factory->create( type.get()));
1369 Sint32 result = neuraylib::set_value( new_value.get(), field_name, value);
1370 if( result != 0)
1371 return result == -3 ? -3 : -5;
1372 base::Handle<IExpression> new_expression(
1373 m_expression_factory->create_constant( new_value.get()));
1374 result = m_edit->set_argument( parameter_name, new_expression.get());
1375 mi_neuray_assert( result == 0);
1376 return result;
1377 }
1378}
1379
1380inline Sint32 Argument_editor::get_array_length( Uint32 parameter_index, Size& size) const
1381{
1382 if( !is_valid())
1383 return -1;
1384
1385 base::Handle<const IExpression_list> arguments( m_access->get_arguments());
1386 base::Handle<const IExpression> argument( arguments->get_expression( parameter_index));
1387 if( !argument)
1388 return -2;
1390 argument->get_interface<IExpression_constant>());
1391 if( !argument_constant)
1392 return -4;
1393 base::Handle<const IValue_array> value( argument_constant->get_value<IValue_array>());
1394 if( !value)
1395 return -5;
1396 size = value->get_size();
1397 return 0;
1398}
1399
1400inline Sint32 Argument_editor::get_array_length( const char* parameter_name, Size& size) const
1401{
1402 if( !is_valid())
1403 return -1;
1404
1405 base::Handle<const IExpression_list> arguments( m_access->get_arguments());
1406 base::Handle<const IExpression> argument( arguments->get_expression( parameter_name));
1407 if( !argument)
1408 return -2;
1410 argument->get_interface<IExpression_constant>());
1411 if( !argument_constant)
1412 return -4;
1413 base::Handle<const IValue_array> value( argument_constant->get_value<IValue_array>());
1414 if( !value)
1415 return -5;
1416 size = value->get_size();
1417 return 0;
1418}
1419
1421{
1422 if( !is_valid())
1423 return -1;
1424
1425 promote_to_edit_if_needed();
1426
1427 if( m_edit->is_default())
1428 return -4;
1429 base::Handle<const IExpression_list> arguments( m_edit->get_arguments());
1430 base::Handle<const IExpression> argument( arguments->get_expression( parameter_index));
1431 if( !argument)
1432 return -2;
1433 if( argument->get_kind() == IExpression::EK_CONSTANT) {
1434 // reuse existing constant expression
1435 base::Handle<IExpression> new_argument( m_expression_factory->clone( argument.get()));
1436 base::Handle<IExpression_constant> new_argument_constant(
1437 new_argument->get_interface<IExpression_constant>());
1439 new_argument_constant->get_value<IValue_array>());
1440 if( !new_value)
1441 return -5;
1442 Sint32 result = new_value->set_size( size);
1443 if( result != 0)
1444 return -5;
1445 result = m_edit->set_argument( parameter_index, new_argument.get());
1446 mi_neuray_assert( result == 0);
1447 return result;
1448 } else {
1449 // create new constant expression
1450 base::Handle<const IType> type( argument->get_type());
1452 m_value_factory->create<IValue_array>( type.get()));
1453 if( !new_value)
1454 return -5;
1455 Sint32 result = new_value->set_size( size);
1456 if( result != 0)
1457 return -5;
1458 base::Handle<IExpression> new_expression(
1459 m_expression_factory->create_constant( new_value.get()));
1460 result = m_edit->set_argument( parameter_index, new_expression.get());
1461 mi_neuray_assert( result == 0);
1462 return result;
1463 }
1464}
1465
1466inline Sint32 Argument_editor::set_array_size( const char* parameter_name, Size size)
1467{
1468 if( !is_valid())
1469 return -1;
1470
1471 promote_to_edit_if_needed();
1472
1473 if( m_edit->is_default())
1474 return -4;
1475 base::Handle<const IExpression_list> arguments( m_edit->get_arguments());
1476 base::Handle<const IExpression> argument( arguments->get_expression( parameter_name));
1477 if( !argument)
1478 return -2;
1479 if( argument->get_kind() == IExpression::EK_CONSTANT) {
1480 // reuse existing constant expression
1481 base::Handle<IExpression> new_argument( m_expression_factory->clone( argument.get()));
1482 base::Handle<IExpression_constant> new_argument_constant(
1483 new_argument->get_interface<IExpression_constant>());
1485 new_argument_constant->get_value<IValue_array>());
1486 if( !new_value)
1487 return -5;
1488 Sint32 result = new_value->set_size( size);
1489 if( result != 0)
1490 return -5;
1491 result = m_edit->set_argument( parameter_name, new_argument.get());
1492 mi_neuray_assert( result == 0);
1493 return result;
1494 } else {
1495 // create new constant expression
1496 base::Handle<const IType> type( argument->get_type());
1498 m_value_factory->create<IValue_array>( type.get()));
1499 if( !new_value)
1500 return -5;
1501 Sint32 result = new_value->set_size( size);
1502 if( result != 0)
1503 return -5;
1504 base::Handle<IExpression> new_expression(
1505 m_expression_factory->create_constant( new_value.get()));
1506 result = m_edit->set_argument( parameter_name, new_expression.get());
1507 mi_neuray_assert( result == 0);
1508 return result;
1509 }
1510}
1511
1512inline const char* Argument_editor::get_call( Size parameter_index) const
1513{
1514 if( !is_valid())
1515 return 0;
1516
1517 base::Handle<const IExpression_list> arguments( m_access->get_arguments());
1519 arguments->get_expression<IExpression_call>( parameter_index));
1520 if( !argument)
1521 return 0;
1522 return argument->get_call();
1523}
1524
1525inline const char* Argument_editor::get_call( const char* parameter_name) const
1526{
1527 if( !is_valid())
1528 return 0;
1529
1530 base::Handle<const IExpression_list> arguments( m_access->get_arguments());
1532 arguments->get_expression<IExpression_call>( parameter_name));
1533 if( !argument)
1534 return 0;
1535 return argument->get_call();
1536}
1537
1538inline Sint32 Argument_editor::set_call( Size parameter_index, const char* call_name)
1539{
1540 if( !is_valid())
1541 return -1;
1542
1543 promote_to_edit_if_needed();
1544
1545 base::Handle<IExpression_call> new_argument( m_expression_factory->create_call( call_name));
1546 if( !new_argument)
1547 return -6;
1548 return m_edit->set_argument( parameter_index, new_argument.get());
1549}
1550
1551inline Sint32 Argument_editor::set_call( const char* parameter_name, const char* call_name)
1552{
1553 if( !is_valid())
1554 return -1;
1555
1556 promote_to_edit_if_needed();
1557
1558 base::Handle<IExpression_call> new_argument( m_expression_factory->create_call( call_name));
1559 if( !new_argument)
1560 return -6;
1561 return m_edit->set_argument( parameter_name, new_argument.get());
1562}
1563
1565{
1566 m_transaction->retain();
1567 return m_transaction.get();
1568}
1569
1571{
1572 m_mdl_factory->retain();
1573 return m_mdl_factory.get();
1574}
1575
1577{
1578 m_value_factory->retain();
1579 return m_value_factory.get();
1580}
1581
1583{
1584 m_expression_factory->retain();
1585 return m_expression_factory.get();
1586}
1587
1589{
1590 m_access->retain();
1591 return m_access.get();
1592}
1593
1595{
1596 promote_to_edit_if_needed();
1597
1598 m_edit->retain();
1599 return m_edit.get();
1600}
1601
1603{
1604 return m_type;
1605}
1606
1607inline const std::string& Argument_editor::get_name() const
1608{
1609 return m_name;
1610}
1611
1612inline void Argument_editor::promote_to_edit_if_needed()
1613{
1614 if( m_edit)
1615 return;
1616
1617 m_edit = m_transaction->edit<IFunction_call>( m_name.c_str());
1618 mi_neuray_assert( m_edit);
1619 m_old_access = m_access;
1620 m_access = m_edit;
1621}
1622
1623} // namespace neuraylib
1624
1625} // namespace mi
1626
1627#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:784
IExpression::Kind get_argument_kind(Size parameter_index) const
Returns the expression kind of an argument.
Definition: argument_editor.h:928
IValue_factory * get_value_factory() const
Get the value factory.
Definition: argument_editor.h:1576
Sint32 set_value(Size parameter_index, const T &value)
Sets an argument.
Definition: argument_editor.h:1103
Sint32 set_array_size(Uint32 parameter_index, Size size)
Sets the length of a deferred-sized array argument.
Definition: argument_editor.h:1420
const std::string & get_name() const
Get the DB name of the MDL function call or material instance.
Definition: argument_editor.h:1607
bool is_material() const
Indicates whether the argument editor acts on a material instance.
Definition: argument_editor.h:836
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:828
Sint32 get_value(Size parameter_index, T &value) const
Returns an argument (values of constants only, no calls).
Definition: argument_editor.h:949
const char * get_definition() const
Returns the DB name of the corresponding material or function definition.
Definition: argument_editor.h:812
IExpression_factory * get_expression_factory() const
Get the expression factory.
Definition: argument_editor.h:1582
const char * get_parameter_name(Size index) const
Returns the name of the parameter at index.
Definition: argument_editor.h:852
Element_type get_element_type() const
Get the element type.
Definition: argument_editor.h:1602
Size get_parameter_count() const
Returns the number of parameters.
Definition: argument_editor.h:844
Sint32 set_call(Size parameter_index, const char *call_name)
Sets an argument (call expressions only).
Definition: argument_editor.h:1538
const IFunction_call * get_scene_element() const
Get the MDL function call or material instance.
Definition: argument_editor.h:1588
Size get_parameter_index(const char *name) const
Returns the index position of a parameter.
Definition: argument_editor.h:860
bool is_parameter_enabled(Size index, IMdl_evaluator_api *evaluator) const
Checks the enable_if condition of the given parameter.
Definition: argument_editor.h:884
ITransaction * get_transaction() const
Get the transaction.
Definition: argument_editor.h:1564
Sint32 reset_argument(Size index)
Resets the argument at index.
Definition: argument_editor.h:899
const IExpression_list * get_arguments() const
Returns all arguments.
Definition: argument_editor.h:920
const IType * get_return_type() const
Returns the return type.
Definition: argument_editor.h:868
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:789
Element_type get_type() const
Indicates whether the argument editor acts on a material instance or on a function call.
Definition: argument_editor.h:807
const char * get_call(Size parameter_index) const
Returns an argument (call expressions only).
Definition: argument_editor.h:1512
const char * get_mdl_definition() const
Returns the MDL name of the corresponding material or function definition.
Definition: argument_editor.h:820
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:759
mi::Sint32 repair(mi::Uint32 flags, IMdl_execution_context *context)
Attempts to repair an invalid material instance or function call.
Definition: argument_editor.h:797
IMdl_factory * get_mdl_factory() const
Get the MDL factory.
Definition: argument_editor.h:1570
Sint32 get_array_length(Uint32 parameter_index, Size &size) const
Returns the length of an array argument.
Definition: argument_editor.h:1380
const IType_list * get_parameter_types() const
Returns the types of all parameters.
Definition: argument_editor.h:876
An indirect call expression.
Definition: iexpression.h:173
A constant expression.
Definition: iexpression.h:94
The interface for creating expressions.
Definition: iexpression.h:648
An ordered collection of expressions identified by name or index.
Definition: iexpression.h:315
Kind
The possible kinds of expressions.
Definition: iexpression.h:51
@ EK_CONSTANT
A constant expression. See mi::neuraylib::IExpression_constant.
Definition: iexpression.h:53
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 *error) 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:83
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:349
An ordered collection of types identified by name or index.
Definition: itype.h:540
The interface to MDL types.
Definition: itype.h:51
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.