Blender V5.3
node_item.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "node_item.h"
6#include "node_graph.h"
7#include "node_parser.h"
8
9#include "BLI_assert.hh"
10#include "BLI_utildefines.hh"
11
13
14NodeItem::NodeItem(MaterialX::GraphElement *graph) : graph_(graph) {}
15
16NodeItem::Type NodeItem::type(const std::string &type_str)
17{
18 /* Converting only MaterialX supported types */
19 if (type_str == "multioutput") {
20 return Type::Multioutput;
21 }
22 if (type_str == "string") {
23 return Type::String;
24 }
25 if (type_str == "filename") {
26 return Type::Filename;
27 }
28 if (type_str == "boolean") {
29 return Type::Boolean;
30 }
31 if (type_str == "integer") {
32 return Type::Integer;
33 }
34 if (type_str == "float") {
35 return Type::Float;
36 }
37 if (type_str == "vector2") {
38 return Type::Vector2;
39 }
40 if (type_str == "vector3") {
41 return Type::Vector3;
42 }
43 if (type_str == "vector4") {
44 return Type::Vector4;
45 }
46 if (type_str == "color3") {
47 return Type::Color3;
48 }
49 if (type_str == "color4") {
50 return Type::Color4;
51 }
52 if (type_str == "BSDF") {
53 return Type::BSDF;
54 }
55 if (type_str == "EDF") {
56 return Type::EDF;
57 }
58 if (type_str == "displacementshader") {
60 }
61 if (type_str == "surfaceshader") {
63 }
64 if (type_str == "material") {
65 return Type::Material;
66 }
68 return Type::Empty;
69}
70
72{
73 switch (type) {
74 case Type::Any:
75 return "any";
77 return "multioutput";
78 case Type::String:
79 return "string";
80 case Type::Filename:
81 return "filename";
82 case Type::Boolean:
83 return "boolean";
84 case Type::Integer:
85 return "integer";
86 case Type::Float:
87 return "float";
88 case Type::Vector2:
89 return "vector2";
90 case Type::Vector3:
91 return "vector3";
92 case Type::Vector4:
93 return "vector4";
94 case Type::Color3:
95 return "color3";
96 case Type::Color4:
97 return "color4";
98 case Type::BSDF:
99 return "BSDF";
100 case Type::EDF:
101 return "EDF";
103 return "displacementshader";
105 return "surfaceshader";
106 case Type::Material:
107 return "material";
109 return "opacity";
110 default:
112 }
113 return "";
114}
115
120
121NodeItem::operator bool() const
122{
123 return value || node || input || output;
124}
125
127{
128 Type type = this->type();
129 if (ELEM(type, Type::BSDF, Type::EDF)) {
130 /* Special case: add BSDF/EDF shaders */
131 NodeItem res = empty();
132 if (other.type() == type) {
133 res = create_node("add", type, {{"in1", *this}, {"in2", other}});
134 }
135 else {
137 }
138 return res;
139 }
140
141 return arithmetic(other, "add", [](float a, float b) { return a + b; });
142}
143
145{
146 return arithmetic(other, "subtract", [](float a, float b) { return a - b; });
147}
148
150{
151 return val(0.0f) - *this;
152}
153
155{
156 Type type = this->type();
157 if (ELEM(type, Type::BSDF, Type::EDF)) {
158 /* Special case: multiple BSDF/EDF shader by Float or Color3 */
159 NodeItem res = empty();
160 Type other_type = other.type();
161 if (ELEM(other_type, Type::Float, Type::Color3)) {
162 res = create_node("multiply", type, {{"in1", *this}, {"in2", other}});
163 }
164 else {
166 }
167 return res;
168 }
169
170 return arithmetic(other, "multiply", [](float a, float b) { return a * b; });
171}
172
174{
175 return arithmetic(other, "divide", [](float a, float b) { return b == 0.0f ? 0.0f : a / b; });
176}
177
179{
180 return arithmetic(
181 other, "modulo", [](float a, float b) { return b == 0.0f ? 0.0f : std::fmod(a, b); });
182}
183
185{
186 return arithmetic(other, "power", [](float a, float b) { return std::pow(a, b); });
187}
188
190{
192
193 if (value) {
194 float v = 0.0f;
195 switch (type()) {
196 case Type::Boolean:
197 v = value->asA<bool>() ? 1.0f : 0.0f;
198 break;
199 case Type::Integer:
200 v = value->asA<int>();
201 break;
202 case Type::Float:
203 v = value->asA<float>();
204 break;
205 case Type::Vector2:
206 v = value->asA<MaterialX::Vector2>()[index];
207 break;
208 case Type::Vector3:
209 v = value->asA<MaterialX::Vector3>()[index];
210 break;
211 case Type::Vector4:
212 v = value->asA<MaterialX::Vector4>()[index];
213 break;
214 case Type::Color3:
215 v = value->asA<MaterialX::Color3>()[index];
216 break;
217 case Type::Color4:
218 v = value->asA<MaterialX::Color4>()[index];
219 break;
220 default:
222 }
223 return val(v);
224 }
225 return create_node("extract", Type::Float, {{"in", *this}, {"index", val(index)}});
226}
227
228bool NodeItem::operator==(const NodeItem &other) const
229{
230 if (!*this) {
231 return !other;
232 }
233 if (!other) {
234 return !*this;
235 }
236 if (node && node == other.node) {
237 return true;
238 }
239 if ((node && other.value) || (value && other.node)) {
240 return false;
241 }
242
243 NodeItem item1 = *this;
244 NodeItem item2 = other;
245 Type to_type = cast_types(item1, item2);
246 if (to_type == Type::Empty) {
247 return false;
248 }
249 return item1.value->getValueString() == item2.value->getValueString();
250}
251
252bool NodeItem::operator!=(const NodeItem &other) const
253{
254 return !(*this == other);
255}
256
258{
259 return arithmetic("absval", [](float a) { return std::abs(a); });
260}
261
263{
264 return arithmetic("floor", [](float a) { return std::floor(a); });
265}
266
268{
269 return arithmetic("ceil", [](float a) { return std::ceil(a); });
270}
271
273{
274 if (value) {
275 return dotproduct(*this).sqrt();
276 }
277 return create_node("magnitude", Type::Float, {{"in", to_vector()}});
278}
279
281{
282 if (value) {
283 return *this / length();
284 }
285 return create_node("normalize", Type::Vector3, {{"in", to_vector()}});
286}
287
288NodeItem NodeItem::min(const NodeItem &other) const
289{
290 return arithmetic(other, "min", [](float a, float b) { return std::min(a, b); });
291}
292
293NodeItem NodeItem::max(const NodeItem &other) const
294{
295 return arithmetic(other, "max", [](float a, float b) { return std::max(a, b); });
296}
297
299{
300 if (value && other.value) {
301 NodeItem d = *this * other;
302 float f = 0.0f;
303 switch (d.type()) {
304 case Type::Float: {
305 f = d.value->asA<float>();
306 break;
307 }
308 case Type::Vector2: {
309 auto v = d.value->asA<MaterialX::Vector2>();
310 f = v[0] + v[1];
311 break;
312 }
313 case Type::Vector3: {
314 auto v = d.value->asA<MaterialX::Vector3>();
315 f = v[0] + v[1] + v[2];
316 break;
317 }
318 case Type::Vector4: {
319 auto v = d.value->asA<MaterialX::Vector4>();
320 f = v[0] + v[1] + v[2] + v[3];
321 break;
322 }
323 case Type::Color3: {
324 auto v = d.value->asA<MaterialX::Color3>();
325 f = v[0] + v[1] + v[2];
326 break;
327 }
328 case Type::Color4: {
329 auto v = d.value->asA<MaterialX::Color4>();
330 f = v[0] + v[1] + v[2] + v[3];
331 break;
332 }
333 default:
335 }
336 return val(f);
337 }
338
339 NodeItem item1 = to_vector();
340 NodeItem item2 = other.to_vector();
341 cast_types(item1, item2);
342 return create_node("dotproduct", Type::Float, {{"in1", item1}, {"in2", item2}});
343}
344
345NodeItem NodeItem::mix(const NodeItem &val1, const NodeItem &val2) const
346{
347 if ((value && val1.value && val2.value) || type() != Type::Float) {
348 return (val(1.0f) - *this) * val1 + *this * val2;
349 }
350
351 Type type1 = val1.type();
352 if (ELEM(type1, Type::BSDF, Type::EDF)) {
353 BLI_assert(val2.type() == type1);
354
355 /* Special case: mix BSDF/EDF shaders */
356 return create_node("mix", type1, {{"bg", val1}, {"fg", val2}, {"mix", *this}});
357 };
358
359 NodeItem item1 = val1;
360 NodeItem item2 = val2;
361 Type to_type = cast_types(item1, item2);
362 return create_node("mix", to_type, {{"bg", item1}, {"fg", item2}, {"mix", *this}});
363}
364
365NodeItem NodeItem::clamp(const NodeItem &min_val, const NodeItem &max_val) const
366{
367 if (value && min_val.value && max_val.value) {
368 return min(max_val).max(min_val);
369 }
370
371 if (min_val.type() == Type::Float && max_val.type() == Type::Float) {
372 return create_node("clamp", type(), {{"in", *this}, {"low", min_val}, {"high", max_val}});
373 }
374
375 Type type = this->type();
376 return create_node(
377 "clamp",
378 type,
379 {{"in", *this}, {"low", min_val.convert(type)}, {"high", max_val.convert(type)}});
380}
381
382NodeItem NodeItem::clamp(float min_val, float max_val) const
383{
384 return clamp(val(min_val), val(max_val));
385}
386
388{
390 BLI_assert(angle.type() == Type::Float);
391 BLI_assert(axis.type() == Type::Vector3);
392
393 return create_node(
394 "rotate3d", NodeItem::Type::Vector3, {{"in", *this}, {"amount", angle}, {"axis", axis}});
395}
396
398{
399 NodeItem x = angle_xyz[0];
400 NodeItem y = angle_xyz[1];
401 NodeItem z = angle_xyz[2];
402
403 NodeItem x_axis = val(MaterialX::Vector3(1.0f, 0.0f, 0.0f));
404 NodeItem y_axis = val(MaterialX::Vector3(0.0f, 1.0f, 0.0f));
405 NodeItem z_axis = val(MaterialX::Vector3(0.0f, 0.0f, 1.0f));
406
407 if (invert) {
408 return rotate(z, z_axis).rotate(y, y_axis).rotate(x, x_axis);
409 }
410 return rotate(x, x_axis).rotate(y, y_axis).rotate(z, z_axis);
411}
412
414{
415 return to_vector().arithmetic("sin", [](float a) { return std::sin(a); });
416}
417
419{
420 return to_vector().arithmetic("cos", [](float a) { return std::cos(a); });
421}
422
424{
425 return to_vector().arithmetic("tan", [](float a) { return std::tan(a); });
426}
427
429{
430 return to_vector().arithmetic("asin", [](float a) { return std::asin(a); });
431}
432
434{
435 return to_vector().arithmetic("acos", [](float a) { return std::acos(a); });
436}
437
439{
440 return to_vector().arithmetic("atan", [](float a) { return std::atan(a); });
441}
442
444{
445 return to_vector().arithmetic(other, "atan2", [](float a, float b) { return std::atan2(a, b); });
446}
447
449{
450 NodeItem v = to_vector();
451 return (v.exp() - (-v).exp()) / val(2.0f);
452}
453
455{
456 NodeItem v = to_vector();
457 return (v.exp() + (-v).exp()) / val(2.0f);
458}
459
461{
462 NodeItem v = to_vector();
463 NodeItem a = v.exp();
464 NodeItem b = (-v).exp();
465 return (a - b) / (a + b);
466}
467
469{
470 return to_vector().arithmetic("ln", [](float a) { return std::log(a); });
471}
472
474{
475 return to_vector().arithmetic("sqrt", [](float a) { return std::sqrt(a); });
476}
477
479{
480 return arithmetic("sign", [](float a) { return a < 0.0f ? -1.0f : (a == 0.0f ? 0.0f : 1.0f); });
481}
482
484{
485 return to_vector().arithmetic("exp", [](float a) { return std::exp(a); });
486}
487
489{
490 switch (to_type) {
491 case Type::Any:
492 return true;
493 case Type::Empty:
495 return false;
496 case Type::String:
497 case Type::Filename:
498 return from_type == SOCK_STRING;
499 case Type::Boolean:
500 return from_type == SOCK_BOOLEAN;
501 case Type::Integer:
502 return from_type == SOCK_INT;
503 case Type::Float:
504 case Type::Vector2:
505 case Type::Vector3:
506 case Type::Color3:
507 case Type::Vector4:
508 case Type::Color4:
510 return ELEM(from_type, SOCK_FLOAT, SOCK_VECTOR, SOCK_RGBA);
511 case Type::EDF:
512 return ELEM(from_type, SOCK_FLOAT, SOCK_VECTOR, SOCK_RGBA, SOCK_SHADER);
513 case Type::BSDF:
515 case Type::Material:
517 return from_type == SOCK_SHADER;
518 }
519
520 return false;
521}
522
524{
525 Type from_type = type();
526 if (from_type == Type::Empty || from_type == to_type || to_type == Type::Any) {
527 return *this;
528 }
529
530 switch (to_type) {
531 /* Link arithmetic types to shader as EDF. */
532 case Type::EDF:
533 if (is_arithmetic(from_type)) {
534 return create_node("uniform_edf", NodeItem::Type::EDF, {{"color", convert(Type::Color3)}});
535 }
536 return empty();
537 /* Displacement shader from arithmetic types, when not using (Vector) Displacement node. */
539 if (is_arithmetic(from_type)) {
540 return create_node("displacement",
542 {{"displacement", convert(Type::Vector3)}});
543 }
544 return empty();
545 /* Surface opacity is just a float. */
548 if (from_type == to_type || to_type == Type::Any) {
549 return *this;
550 }
551 break;
552 /* Material output will evaluate graph multiple times for different components,
553 * when linking arithmetic types we want to leave those empty. */
554 case Type::BSDF:
556 case Type::Material:
557 return empty();
558 default:
559 break;
560 }
561
562 if (!is_arithmetic(from_type) || !is_arithmetic(to_type)) {
564 "Cannot convert: %s -> %s",
565 type(from_type).c_str(),
566 type(to_type).c_str());
567 return empty();
568 }
569
570 if (to_type == Type::Float) {
571 return (*this)[0];
572 }
573
574 /* Converting types which requires > 1 iteration */
575 switch (from_type) {
576 case Type::Boolean:
577 case Type::Integer:
578 switch (to_type) {
579 case Type::Vector2:
581 case Type::Vector3:
583 case Type::Vector4:
585 case Type::Color3:
587 case Type::Color4:
589 default:
590 break;
591 }
592 break;
593 case Type::Vector2:
594 switch (to_type) {
595 case Type::Vector4:
597 case Type::Color3:
599 case Type::Color4:
601 default:
602 break;
603 }
604 break;
605 case Type::Vector3:
606 switch (to_type) {
607 case Type::Color4:
609 default:
610 break;
611 }
612 break;
613 case Type::Vector4:
614 switch (to_type) {
615 case Type::Vector2:
617 case Type::Color3:
619 default:
620 break;
621 }
622 break;
623 case Type::Color3:
624 switch (to_type) {
625 case Type::Vector2:
627 case Type::Vector4:
629 default:
630 break;
631 }
632 break;
633 case Type::Color4:
634 switch (to_type) {
635 case Type::Vector2:
637 case Type::Vector3:
639 default:
640 break;
641 }
642 break;
643 default:
644 break;
645 }
646
647 /* Converting 1 iteration types */
648 NodeItem res = empty();
649 if (value) {
650 switch (from_type) {
651 case Type::Boolean: {
652 if (to_type == Type::Integer) {
653 int v = value->asA<bool>();
654 res.value = MaterialX::Value::createValue<int>(v);
655 }
656 break;
657 }
658 case Type::Float: {
659 float v = value->asA<float>();
660 switch (to_type) {
661 case Type::Vector2:
662 res.value = MaterialX::Value::createValue<MaterialX::Vector2>({v, v});
663 break;
664 case Type::Vector3:
665 res.value = MaterialX::Value::createValue<MaterialX::Vector3>({v, v, v});
666 break;
667 case Type::Vector4:
668 res.value = MaterialX::Value::createValue<MaterialX::Vector4>({v, v, v, 1.0f});
669 break;
670 case Type::Color3:
671 res.value = MaterialX::Value::createValue<MaterialX::Color3>({v, v, v});
672 break;
673 case Type::Color4:
674 res.value = MaterialX::Value::createValue<MaterialX::Color4>({v, v, v, 1.0f});
675 break;
676 default:
678 }
679 break;
680 }
681 case Type::Vector2: {
682 auto v = value->asA<MaterialX::Vector2>();
683 switch (to_type) {
684 case Type::Vector3:
685 res.value = MaterialX::Value::createValue<MaterialX::Vector3>({v[0], v[1], 0.0f});
686 break;
687 default:
689 }
690 break;
691 }
692 case Type::Vector3: {
693 auto v = value->asA<MaterialX::Vector3>();
694 switch (to_type) {
695 case Type::Vector2:
696 res.value = MaterialX::Value::createValue<MaterialX::Vector2>({v[0], v[1]});
697 break;
698 case Type::Vector4:
699 res.value = MaterialX::Value::createValue<MaterialX::Vector4>(
700 {v[0], v[1], v[2], 0.0f});
701 break;
702 case Type::Color3:
703 res.value = MaterialX::Value::createValue<MaterialX::Color3>({v[0], v[1], v[2]});
704 break;
705 default:
707 }
708 break;
709 }
710 case Type::Vector4: {
711 auto v = value->asA<MaterialX::Vector4>();
712 switch (to_type) {
713 case Type::Vector3:
714 res.value = MaterialX::Value::createValue<MaterialX::Vector3>({v[0], v[1], v[2]});
715 break;
716 case Type::Color4:
717 res.value = MaterialX::Value::createValue<MaterialX::Color4>({v[0], v[1], v[2], v[3]});
718 break;
719 default:
721 }
722 break;
723 }
724 case Type::Color3: {
725 auto v = value->asA<MaterialX::Color3>();
726 switch (to_type) {
727 case Type::Vector3:
728 res.value = MaterialX::Value::createValue<MaterialX::Vector3>({v[0], v[1], v[2]});
729 break;
730 case Type::Color4:
731 res.value = MaterialX::Value::createValue<MaterialX::Color4>({v[0], v[1], v[2], 1.0f});
732 break;
733 default:
735 }
736 break;
737 }
738 case Type::Color4: {
739 auto v = value->asA<MaterialX::Color4>();
740 switch (to_type) {
741 case Type::Vector4:
742 res.value = MaterialX::Value::createValue<MaterialX::Vector4>(
743 {v[0], v[1], v[2], v[3]});
744 break;
745 case Type::Color3:
746 res.value = MaterialX::Value::createValue<MaterialX::Color3>({v[0], v[1], v[2]});
747 break;
748 default:
750 }
751 break;
752 }
753 default:
755 }
756 }
757 else {
758 res = create_node("convert", to_type, {{"in", *this}});
759 }
760 return res;
761}
762
764{
765 switch (type()) {
766 case Type::Float:
767 case Type::Vector2:
768 case Type::Vector3:
769 case Type::Vector4:
770 return *this;
771
772 case Type::Color3:
773 return convert(Type::Vector3);
774
775 case Type::Color4:
776 return convert(Type::Vector4);
777
778 default:
780 }
781 return empty();
782}
783
785 const NodeItem &other,
786 const NodeItem &if_val,
787 const NodeItem &else_val) const
788{
789 switch (op) {
790 case CompareOp::Less:
791 return if_else(CompareOp::GreaterEq, other, else_val, if_val);
793 return if_else(CompareOp::Greater, other, else_val, if_val);
794 case CompareOp::NotEq:
795 return if_else(CompareOp::Eq, other, else_val, if_val);
796 default:
797 break;
798 }
799
800 NodeItem res = empty();
801 if (type() != Type::Float || other.type() != Type::Float) {
802 return res;
803 }
804
805 auto item1 = if_val;
806 auto item2 = else_val;
807 Type to_type = cast_types(item1, item2);
808 if (to_type == Type::Empty) {
809 return res;
810 }
811
812 std::function<bool(float, float)> func = nullptr;
813 std::string category;
814 switch (op) {
816 category = "ifgreater";
817 func = [](float a, float b) { return a > b; };
818 break;
820 category = "ifgreatereq";
821 func = [](float a, float b) { return a >= b; };
822 break;
823 case CompareOp::Eq:
824 category = "ifequal";
825 func = [](float a, float b) { return a == b; };
826 break;
827 default:
829 }
830
831 if (value && other.value) {
832 res = func(value->asA<float>(), other.value->asA<float>()) ? item1 : item2;
833 }
834 else {
835 res = create_node(
836 category, to_type, {{"value1", *this}, {"value2", other}, {"in1", item1}, {"in2", item2}});
837 }
838
839 return res;
840}
841
843{
844 return NodeItem(graph_);
845}
846
848{
849 if (value) {
850 return type(value->getTypeString());
851 }
852 if (node) {
853 return type(node->getType());
854 }
855 if (output) {
856 return type(output->getType());
857 }
858 return Type::Empty;
859}
860
861NodeItem NodeItem::create_node(const std::string &category, Type type) const
862{
863 const std::string name = NodeGraph::unique_anonymous_node_name(graph_);
864 const std::string type_str = NodeItem::type(type);
865 CLOG_DEBUG(LOG_IO_MATERIALX, "<%s type=%s>", category.c_str(), type_str.c_str());
866 NodeItem res = empty();
867 /* Surface-shader nodes and materials are added directly to the document,
868 * otherwise to the node-graph. */
870 res.node = graph_->getDocument()->addNode(category, name, type_str);
871 }
872 else {
873 res.node = graph_->addNode(category, name, type_str);
874 }
875 return res;
876}
877
878NodeItem NodeItem::create_node(const std::string &category, Type type, const Inputs &inputs) const
879{
880 NodeItem res = create_node(category, type);
881 for (const auto &it : inputs) {
882 if (it.second) {
883 res.set_input(it.first, it.second);
884 }
885 }
886 return res;
887}
888
889void NodeItem::set_input(const std::string &in_name, const NodeItem &item)
890{
891 if (item.value) {
892 Type item_type = item.type();
893 switch (item_type) {
894 case Type::String:
895 set_input(in_name, item.value->asA<std::string>(), item_type);
896 break;
897 case Type::Boolean:
898 set_input(in_name, item.value->asA<bool>(), item_type);
899 break;
900 case Type::Integer:
901 set_input(in_name, item.value->asA<int>(), item_type);
902 break;
903 case Type::Float:
904 set_input(in_name, item.value->asA<float>(), item_type);
905 break;
906 case Type::Vector2:
907 set_input(in_name, item.value->asA<MaterialX::Vector2>(), item_type);
908 break;
909 case Type::Vector3:
910 set_input(in_name, item.value->asA<MaterialX::Vector3>(), item_type);
911 break;
912 case Type::Vector4:
913 set_input(in_name, item.value->asA<MaterialX::Vector4>(), item_type);
914 break;
915 case Type::Color3:
916 set_input(in_name, item.value->asA<MaterialX::Color3>(), item_type);
917 break;
918 case Type::Color4:
919 set_input(in_name, item.value->asA<MaterialX::Color4>(), item_type);
920 break;
921 default:
923 }
924 }
925 else if (item.node) {
926 if (type() == Type::SurfaceShader) {
927 auto output_name = item.node->getName() + "_out";
928
929 auto output = graph_->getOutput(output_name);
930 if (!output) {
931 output = graph_->addOutput(output_name, item.node->getType());
932 }
933
934 output->setConnectedNode(item.node);
935 node->setConnectedOutput(in_name, output);
936 }
937 else {
938 node->setConnectedNode(in_name, item.node);
939 }
940 }
941 else if (item.input) {
942 node->setAttribute("interfacename", item.input->getName());
943 }
944 else if (item.output) {
945 node->setConnectedOutput(in_name, item.output);
946 }
947 else {
948 CLOG_WARN(LOG_IO_MATERIALX, "Empty item to input: %s", in_name.c_str());
949 }
950}
951
952NodeItem NodeItem::add_output(const std::string &out_name, Type out_type)
953{
954 NodeItem res = empty();
955 res.output = node->addOutput(out_name, type(out_type));
956 return res;
957}
958
959NodeItem NodeItem::create_input(const std::string &name, const NodeItem &item) const
960{
961 NodeItem res = empty();
962 res.input = graph_->addInput(name);
963
964 Type item_type = item.type();
965 if (item.node) {
966 res.input->setConnectedNode(item.node);
967 }
968 else {
970 }
971 res.input->setType(type(item_type));
972
973 return res;
974}
975
976NodeItem NodeItem::create_output(const std::string &name, const NodeItem &item) const
977{
978 NodeItem res = empty();
979 res.output = graph_->addOutput(name);
980
981 Type item_type = item.type();
982 if (item.node) {
983 res.output->setConnectedNode(item.node);
984 }
985 else if (item.input) {
986 res.output->setInterfaceName(item.input->getName());
987 }
988 else {
990 }
991 res.output->setType(type(item_type));
992
993 return res;
994}
995
996NodeItem::Type NodeItem::cast_types(NodeItem &item1, NodeItem &item2)
997{
998 Type t1 = item1.type();
999 Type t2 = item2.type();
1000 if (t1 == t2) {
1001 return t1;
1002 }
1003 if (!is_arithmetic(t1) || !is_arithmetic(t2)) {
1004 CLOG_WARN(
1005 LOG_IO_MATERIALX, "Can't adjust types: %s <-> %s", type(t1).c_str(), type(t2).c_str());
1006 return Type::Empty;
1007 }
1008 if (t1 < t2) {
1009 item1 = item1.convert(t2);
1010 return t2;
1011 }
1012 item2 = item2.convert(t1);
1013 return t1;
1014}
1015
1016bool NodeItem::is_arithmetic() const
1017{
1018 return is_arithmetic(type());
1019}
1020
1021NodeItem NodeItem::arithmetic(const std::string &category, std::function<float(float)> func) const
1022{
1023 NodeItem res = empty();
1024 Type type = this->type();
1026
1027 if (value) {
1028 switch (type) {
1029 case Type::Boolean: {
1030 float v = value->asA<bool>() ? 1.0f : 0.0f;
1031 res.value = MaterialX::Value::createValue<float>(func(v));
1032 break;
1033 }
1034 case Type::Integer: {
1035 float v = value->asA<int>();
1036 res.value = MaterialX::Value::createValue<float>(func(v));
1037 break;
1038 }
1039 case Type::Float: {
1040 float v = value->asA<float>();
1041 res.value = MaterialX::Value::createValue<float>(func(v));
1042 break;
1043 }
1044 case Type::Color3: {
1045 auto v = value->asA<MaterialX::Color3>();
1046 res.value = MaterialX::Value::createValue<MaterialX::Color3>(
1047 {func(v[0]), func(v[1]), func(v[2])});
1048 break;
1049 }
1050 case Type::Color4: {
1051 auto v = value->asA<MaterialX::Color4>();
1052 res.value = MaterialX::Value::createValue<MaterialX::Color4>(
1053 {func(v[0]), func(v[1]), func(v[2]), func(v[3])});
1054 break;
1055 }
1056 case Type::Vector2: {
1057 auto v = value->asA<MaterialX::Vector2>();
1058 res.value = MaterialX::Value::createValue<MaterialX::Vector2>({func(v[0]), func(v[1])});
1059 break;
1060 }
1061 case Type::Vector3: {
1062 auto v = value->asA<MaterialX::Vector3>();
1063 res.value = MaterialX::Value::createValue<MaterialX::Vector3>(
1064 {func(v[0]), func(v[1]), func(v[2])});
1065 break;
1066 }
1067 case Type::Vector4: {
1068 auto v = value->asA<MaterialX::Vector4>();
1069 res.value = MaterialX::Value::createValue<MaterialX::Vector4>(
1070 {func(v[0]), func(v[1]), func(v[2]), func(v[3])});
1071 break;
1072 }
1073 default:
1075 }
1076 }
1077 else {
1078 res = create_node(category, type, {{"in", *this}});
1079 }
1080 return res;
1081}
1082
1083NodeItem NodeItem::arithmetic(const NodeItem &other,
1084 const std::string &category,
1085 std::function<float(float, float)> func,
1086 Type to_type) const
1087{
1088 NodeItem res = empty();
1089 NodeItem item1 = *this;
1090 NodeItem item2 = other;
1091 to_type = (to_type == Type::Any) ? cast_types(item1, item2) : to_type;
1092 if (to_type == Type::Empty) {
1093 return res;
1094 }
1095
1096 if (value && other.value) {
1097 switch (to_type) {
1098 case Type::Boolean: {
1099 float v1 = item1.value->asA<bool>() ? 1.0f : 0.0f;
1100 float v2 = item2.value->asA<bool>() ? 1.0f : 0.0f;
1101 res.value = MaterialX::Value::createValue<float>(func(v1, v2));
1102 break;
1103 }
1104 case Type::Integer: {
1105 float v1 = item1.value->asA<int>();
1106 float v2 = item2.value->asA<int>();
1107 res.value = MaterialX::Value::createValue<float>(func(v1, v2));
1108 break;
1109 }
1110 case Type::Float: {
1111 float v1 = item1.value->asA<float>();
1112 float v2 = item2.value->asA<float>();
1113 res.value = MaterialX::Value::createValue<float>(func(v1, v2));
1114 break;
1115 }
1116 case Type::Color3: {
1117 auto v1 = item1.value->asA<MaterialX::Color3>();
1118 auto v2 = item2.value->asA<MaterialX::Color3>();
1119 res.value = MaterialX::Value::createValue<MaterialX::Color3>(
1120 {func(v1[0], v2[0]), func(v1[1], v2[1]), func(v1[2], v2[2])});
1121 break;
1122 }
1123 case Type::Color4: {
1124 auto v1 = item1.value->asA<MaterialX::Color4>();
1125 auto v2 = item2.value->asA<MaterialX::Color4>();
1126 res.value = MaterialX::Value::createValue<MaterialX::Color4>(
1127 {func(v1[0], v2[0]), func(v1[1], v2[1]), func(v1[2], v2[2]), func(v1[3], v2[3])});
1128 break;
1129 }
1130 case Type::Vector2: {
1131 auto v1 = item1.value->asA<MaterialX::Vector2>();
1132 auto v2 = item2.value->asA<MaterialX::Vector2>();
1133 res.value = MaterialX::Value::createValue<MaterialX::Vector2>(
1134 {func(v1[0], v2[0]), func(v1[1], v2[1])});
1135 break;
1136 }
1137 case Type::Vector3: {
1138 auto v1 = item1.value->asA<MaterialX::Vector3>();
1139 auto v2 = item2.value->asA<MaterialX::Vector3>();
1140 res.value = MaterialX::Value::createValue<MaterialX::Vector3>(
1141 {func(v1[0], v2[0]), func(v1[1], v2[1]), func(v1[2], v2[2])});
1142 break;
1143 }
1144 case Type::Vector4: {
1145 auto v1 = item1.value->asA<MaterialX::Vector4>();
1146 auto v2 = item2.value->asA<MaterialX::Vector4>();
1147 res.value = MaterialX::Value::createValue<MaterialX::Vector4>(
1148 {func(v1[0], v2[0]), func(v1[1], v2[1]), func(v1[2], v2[2]), func(v1[3], v2[3])});
1149 break;
1150 }
1151 default:
1153 }
1154 }
1155 else {
1156#if !(MATERIALX_MAJOR_VERSION <= 1 && MATERIALX_MINOR_VERSION <= 38)
1157 if (category == "atan2") {
1158 res = create_node(category, to_type, {{"iny", item1}, {"inx", item2}});
1159 }
1160 else
1161#endif
1162 {
1163 res = create_node(category, to_type, {{"in1", item1}, {"in2", item2}});
1164 }
1165 }
1166 return res;
1167}
1168
1169} // namespace blender::nodes::materialx
#define CLOG_DEBUG(clg_ref,...)
Definition CLG_log.h:191
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
#define BLI_assert_unreachable()
#define BLI_assert(a)
#define ELEM(...)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
NodeItem operator*(const NodeItem &other) const
Definition node_item.cc:154
NodeItem operator[](int index) const
Definition node_item.cc:189
NodeItem add_output(const std::string &out_name, Type out_type)
Definition node_item.cc:952
NodeItem clamp(const NodeItem &min_val, const NodeItem &max_val) const
Definition node_item.cc:365
NodeItem dotproduct(const NodeItem &other) const
Definition node_item.cc:298
NodeItem max(const NodeItem &other) const
Definition node_item.cc:293
NodeItem val(const T &data) const
Definition node_item.h:148
void set_input(const std::string &in_name, const T &value, Type in_type)
Definition node_item.h:156
bool operator!=(const NodeItem &other) const
Definition node_item.cc:252
NodeItem min(const NodeItem &other) const
Definition node_item.cc:288
bool operator==(const NodeItem &other) const
Definition node_item.cc:228
NodeItem atan2(const NodeItem &other) const
Definition node_item.cc:443
NodeItem create_input(const std::string &name, const NodeItem &item) const
Definition node_item.cc:959
MaterialX::OutputPtr output
Definition node_item.h:56
NodeItem operator+(const NodeItem &other) const
Definition node_item.cc:126
static bool is_arithmetic(Type type)
Definition node_item.cc:116
NodeItem if_else(CompareOp op, const NodeItem &other, const NodeItem &if_val, const NodeItem &else_val) const
Definition node_item.cc:784
NodeItem rotate(const NodeItem &angle, const NodeItem &axis)
Definition node_item.cc:387
NodeItem convert(Type to_type) const
Definition node_item.cc:523
NodeItem create_output(const std::string &name, const NodeItem &item) const
Definition node_item.cc:976
NodeItem operator^(const NodeItem &other) const
Definition node_item.cc:184
static bool is_convertible(eNodeSocketDatatype from_type, Type to_type)
Definition node_item.cc:488
NodeItem mix(const NodeItem &val1, const NodeItem &val2) const
Definition node_item.cc:345
static Type type(const std::string &type_str)
Definition node_item.cc:16
NodeItem operator/(const NodeItem &other) const
Definition node_item.cc:173
std::vector< std::pair< std::string, NodeItem > > Inputs
Definition node_item.h:20
NodeItem operator%(const NodeItem &other) const
Definition node_item.cc:178
NodeItem create_node(const std::string &category, Type type) const
Definition node_item.cc:861
float2x2 invert(float2x2 mat)
uint to_type(bool)
CLG_LogRef * LOG_IO_MATERIALX
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
ATTR_WARN_UNUSED_RESULT const BMVert * v
ATTR_WARN_UNUSED_RESULT const BMVert * v2
static std::string unique_anonymous_node_name(MaterialX::GraphElement *graph_element)