[Bf-blender-cvs] [7317074eb37] functions-experimental-refactor: bring back some more math operations

Jacques Lucke noreply at git.blender.org
Tue Nov 5 15:55:10 CET 2019


Commit: 7317074eb376a88f78e04af2eabfd0232da50fcd
Author: Jacques Lucke
Date:   Tue Nov 5 14:36:45 2019 +0100
Branches: functions-experimental-refactor
https://developer.blender.org/rB7317074eb376a88f78e04af2eabfd0232da50fcd

bring back some more math operations

===================================================================

M	release/scripts/startup/nodes/function_nodes/float_math.py
M	source/blender/functions2/intern/vtree_multi_function_network/mappings_nodes.cc

===================================================================

diff --git a/release/scripts/startup/nodes/function_nodes/float_math.py b/release/scripts/startup/nodes/function_nodes/float_math.py
index d0aeb716957..f08d3edebce 100644
--- a/release/scripts/startup/nodes/function_nodes/float_math.py
+++ b/release/scripts/startup/nodes/function_nodes/float_math.py
@@ -4,9 +4,6 @@ from .. base import FunctionNode
 from .. node_builder import NodeBuilder
 
 def create_variadic_math_node(data_type, idname, label):
-    '''
-    Should only be used for operations with associative property.
-    '''
 
     class MathNode(bpy.types.Node, FunctionNode):
         bl_idname = idname
@@ -24,6 +21,22 @@ def create_variadic_math_node(data_type, idname, label):
 
     return MathNode
 
+def create_two_inputs_math_node(data_type, idname, label):
+
+    class MathNode(bpy.types.Node, FunctionNode):
+        bl_idname = idname
+        bl_label = label
+
+        use_list__a: NodeBuilder.VectorizedProperty()
+        use_list__b: NodeBuilder.VectorizedProperty()
+
+        def declaration(self, builder: NodeBuilder):
+            builder.vectorized_input("a", "use_list__a", "A", "A", data_type)
+            builder.vectorized_input("b", "use_list__b", "B", "B", data_type)
+            builder.vectorized_output("result", ["use_list__a", "use_list__b"], "Result", "Result", data_type)
+
+    return MathNode
+
 def create_single_input_math_node(data_type, idname, label):
 
     class MathNode(bpy.types.Node, FunctionNode):
@@ -43,91 +56,11 @@ MultiplyFloatsNode = create_variadic_math_node("Float", "fn_MultiplyFloatsNode",
 MinimumFloatsNode = create_variadic_math_node("Float", "fn_MinimumFloatsNode", "Minimum Floats")
 MaximumFloatsNode = create_variadic_math_node("Float", "fn_MaximumFloatsNode", "Maximum Floats")
 
+SubtractFloatsNode = create_two_inputs_math_node("Float", "fn_SubtractFloatsNode", "Subtract Floats")
+DivideFloatsNode = create_two_inputs_math_node("Float", "fn_DivideFloatsNode", "Divide Floats")
+PowerFloatsNode = create_two_inputs_math_node("Float", "fn_PowerFloatsNode", "Power Floats")
+
 SqrtFloatNode = create_single_input_math_node("Float", "fn_SqrtFloatNode", "Sqrt Float")
 AbsFloatNode = create_single_input_math_node("Float", "fn_AbsoluteFloatNode", "Absolute Float")
 SineNode = create_single_input_math_node("Float", "fn_SineNode", "Sine")
 CosineNode = create_single_input_math_node("Float", "fn_CosineNode", "Cosine")
-
-
-operation_items = [
-    ("ADD",     "Add",          "", "", 1),
-    ("SUB",     "Subtract",     "", "", 2),
-    ("MULTIPLY", "Multiply",    "", "", 3),
-    ("DIVIDE",  "Divide",       "", "", 4),
-    None,
-    ("POWER",   "Power",        "", "", 5),
-    ("LOG",     "Logarithm",    "", "", 6),
-    ("SQRT",    "Square Root",  "", "", 7),
-    None,
-    ("ABS",     "Absolute",     "", "", 8),
-    ("MIN",     "Minimum",      "", "", 9),
-    ("MAX",     "Maximum",      "", "", 10),
-    ("MOD",     "Modulo",       "", "", 18),
-    None,
-    ("SIN",     "Sine",         "", "", 11),
-    ("COS",     "Cosine",       "", "", 12),
-    ("TAN",     "Tangent",      "", "", 13),
-    ("ASIN",    "Arcsine",      "", "", 14),
-    ("ACOS",    "Arccosine",    "", "", 15),
-    ("ATAN",    "Arctangent",   "", "", 16),
-    ("ATAN2",   "Arctan2",      "", "", 17),
-    None,
-    ("FRACT",   "Fract",        "", "", 19),
-    ("CEIL",    "Ceil",         "", "", 20),
-    ("FLOOR",   "Floor",        "", "", 21),
-    ("ROUND",   "Round",        "", "", 22),
-    ("SNAP",    "Snap",         "", "", 23),
-]
-
-single_value_operations = {
-    "SQRT",
-    "ABS",
-    "SIN",
-    "COS",
-    "TAN",
-    "ASIN",
-    "ACOS",
-    "ATAN",
-    "FRACT",
-    "CEIL",
-    "FLOOR",
-    "ROUND"
-}
-
-class FloatMathNode(bpy.types.Node, FunctionNode):
-    bl_idname = "fn_FloatMathNode"
-    bl_label = "Float Math"
-
-    search_terms = (
-        ("Subtract Floats", {"operation" : "SUB"}),
-        ("Divide Floats", {"operation" : "DIVIDE"}),
-    )
-
-    operation: EnumProperty(
-        name="Operation",
-        items=operation_items,
-        update=FunctionNode.sync_tree,
-    )
-
-    use_list__a: NodeBuilder.VectorizedProperty()
-    use_list__b: NodeBuilder.VectorizedProperty()
-
-    def declaration(self, builder: NodeBuilder):
-        builder.vectorized_input(
-            "a", "use_list__a",
-            "A", "A", "Float")
-        prop_names = ["use_list__a"]
-
-        if self.operation not in single_value_operations:
-            builder.vectorized_input(
-                "b", "use_list__b",
-                "B", "B", "Float")
-            prop_names.append("use_list__b")
-
-
-        builder.vectorized_output(
-            "result", prop_names,
-            "Result", "Result", "Float")
-
-    def draw(self, layout):
-        layout.prop(self, "operation", text="")
diff --git a/source/blender/functions2/intern/vtree_multi_function_network/mappings_nodes.cc b/source/blender/functions2/intern/vtree_multi_function_network/mappings_nodes.cc
index ea352610365..7c721ad2824 100644
--- a/source/blender/functions2/intern/vtree_multi_function_network/mappings_nodes.cc
+++ b/source/blender/functions2/intern/vtree_multi_function_network/mappings_nodes.cc
@@ -229,6 +229,46 @@ static void INSERT_maximum_floats(VTreeMFNetworkBuilder &builder, const VNode &v
   insert_simple_math_function<float, max_func_cb<float>>(builder, vnode, 0.0f);
 }
 
+template<typename T> T subtract_func_cb(T a, T b)
+{
+  return a - b;
+}
+
+template<typename T> T safe_divide_func_cb(T a, T b)
+{
+  return (b != 0) ? a / b : 0.0f;
+}
+
+template<typename T> T safe_power_func_cb(T a, T b)
+{
+  return (a >= 0) ? (T)std::pow(a, b) : (T)0;
+}
+
+template<typename T, T (*Compute)(T, T)>
+void insert_two_inputs_math_function(VTreeMFNetworkBuilder &builder, const VNode &vnode)
+{
+  const MultiFunction &base_fn = builder.allocate_function<MF_SimpleMath<T, Compute>>(vnode.name(),
+                                                                                      2);
+  const MultiFunction &fn = get_vectorized_function(
+      builder, base_fn, vnode.rna(), {"use_list__a", "use_list__b"});
+  builder.add_function(fn, {0, 1}, {2}, vnode);
+}
+
+static void INSERT_subtract_floats(VTreeMFNetworkBuilder &builder, const VNode &vnode)
+{
+  insert_two_inputs_math_function<float, subtract_func_cb<float>>(builder, vnode);
+}
+
+static void INSERT_divide_floats(VTreeMFNetworkBuilder &builder, const VNode &vnode)
+{
+  insert_two_inputs_math_function<float, safe_divide_func_cb<float>>(builder, vnode);
+}
+
+static void INSERT_power_floats(VTreeMFNetworkBuilder &builder, const VNode &vnode)
+{
+  insert_two_inputs_math_function<float, safe_power_func_cb<float>>(builder, vnode);
+}
+
 template<typename T, T (*Compute)(const T &)>
 static void insert_single_input_math_function(VTreeMFNetworkBuilder &builder, const VNode &vnode)
 {
@@ -298,6 +338,10 @@ void add_vtree_node_mapping_info(VTreeMultiFunctionMappings &mappings)
   mappings.vnode_inserters.add_new("fn_MinimumFloatsNode", INSERT_minimum_floats);
   mappings.vnode_inserters.add_new("fn_MaximumFloatsNode", INSERT_maximum_floats);
 
+  mappings.vnode_inserters.add_new("fn_SubtractFloatsNode", INSERT_subtract_floats);
+  mappings.vnode_inserters.add_new("fn_DivideFloatsNode", INSERT_divide_floats);
+  mappings.vnode_inserters.add_new("fn_PowerFloatsNode", INSERT_power_floats);
+
   mappings.vnode_inserters.add_new("fn_SqrtFloatNode", INSERT_sqrt_float);
   mappings.vnode_inserters.add_new("fn_AbsoluteFloatNode", INSERT_abs_float);
   mappings.vnode_inserters.add_new("fn_SineNode", INSERT_sine);



More information about the Bf-blender-cvs mailing list