[Bf-blender-cvs] [9644f7c8c7f] functions: New boolean math node with And, Or and Not operation

Eitan noreply at git.blender.org
Thu Aug 22 11:48:11 CEST 2019


Commit: 9644f7c8c7f175dce134dcaeeb2c2d46c849317a
Author: Eitan
Date:   Thu Aug 22 11:47:30 2019 +0200
Branches: functions
https://developer.blender.org/rB9644f7c8c7f175dce134dcaeeb2c2d46c849317a

New boolean math node with And, Or and Not operation

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

A	release/scripts/startup/nodes/function_nodes/boolean.py
M	source/blender/functions/CMakeLists.txt
M	source/blender/functions/FN_functions.hpp
M	source/blender/functions/frontends/data_flow_nodes/mappings/node_inserters.cpp
A	source/blender/functions/functions/boolean.cpp
A	source/blender/functions/functions/boolean.hpp

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

diff --git a/release/scripts/startup/nodes/function_nodes/boolean.py b/release/scripts/startup/nodes/function_nodes/boolean.py
new file mode 100644
index 00000000000..1e67bc52388
--- /dev/null
+++ b/release/scripts/startup/nodes/function_nodes/boolean.py
@@ -0,0 +1,51 @@
+import bpy
+from bpy.props import *
+from .. base import FunctionNode
+from .. node_builder import NodeBuilder
+
+operation_items = [
+    ("AND", "And", "", "", 1),
+    ("OR", "Or", "", "", 2),
+    ("NOT", "Not", "", "", 3),
+]
+single_value_operations = {
+    "NOT"
+}
+class BooleanMathNode(bpy.types.Node, FunctionNode):
+    bl_idname = "fn_BooleanMathNode"
+    bl_label = "Boolean Math"
+
+    search_terms = (
+        ("And", {"operation" : "AND"}),
+        ("Or", {"operation" : "OR"}),
+        ("Not", {"operation" : "NOT"}),
+    )
+
+    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", "Boolean")
+        prop_names = ["use_list__a"]
+
+        if self.operation not in single_value_operations:
+            builder.vectorized_input(
+                "b", "use_list__b",
+                "B", "B", "Boolean")
+            prop_names.append("use_list__b")
+
+
+        builder.vectorized_output(
+            "result", prop_names,
+            "Result", "Result", "Boolean")
+
+    def draw(self, layout):
+        layout.prop(self, "operation", text="")
diff --git a/source/blender/functions/CMakeLists.txt b/source/blender/functions/CMakeLists.txt
index 1142db625d1..3b5115b4036 100644
--- a/source/blender/functions/CMakeLists.txt
+++ b/source/blender/functions/CMakeLists.txt
@@ -141,6 +141,8 @@ set(SRC
   functions/constants.cpp
   functions/color.hpp
   functions/color.cpp
+  functions/boolean.hpp
+  functions/boolean.cpp
 
   frontends/data_flow_nodes/vtree_data_graph_builder.hpp
   frontends/data_flow_nodes/vtree_data_graph_builder.cpp
diff --git a/source/blender/functions/FN_functions.hpp b/source/blender/functions/FN_functions.hpp
index d0e55721d89..b9498cdf370 100644
--- a/source/blender/functions/FN_functions.hpp
+++ b/source/blender/functions/FN_functions.hpp
@@ -13,3 +13,4 @@
 #include "functions/constants.hpp"
 #include "functions/color.hpp"
 #include "functions/array_execution.hpp"
+#include "functions/boolean.hpp"
diff --git a/source/blender/functions/frontends/data_flow_nodes/mappings/node_inserters.cpp b/source/blender/functions/frontends/data_flow_nodes/mappings/node_inserters.cpp
index 212ca7b4815..761911d0d23 100644
--- a/source/blender/functions/frontends/data_flow_nodes/mappings/node_inserters.cpp
+++ b/source/blender/functions/frontends/data_flow_nodes/mappings/node_inserters.cpp
@@ -351,7 +351,42 @@ static void INSERT_compare(VTreeDataGraphBuilder &builder, VirtualNode *vnode)
        {"use_list__b", Functions::GET_FN_output_float_0()}});
   builder.insert_matching_function(fn, vnode);
 }
+static SharedFunction &get_boolean_function(int operation)
+{
+  switch (operation) {
+    case 1:
+      return Functions::GET_FN_and();
+    case 2:
+      return Functions::GET_FN_or();
+    case 3:
+      return Functions::GET_FN_not();
+    default:
+      BLI_assert(false);
+      return *(SharedFunction *)nullptr;
+  }
+}
 
+static void INSERT_boolean(VTreeDataGraphBuilder &builder, VirtualNode *vnode)
+{
+  PointerRNA rna = vnode->rna();
+  int operation = RNA_enum_get(&rna, "operation");
+  SharedFunction &original_fn = get_boolean_function(operation);
+  uint input_amount = original_fn->input_amount();
+  if (input_amount == 1) {
+    SharedFunction fn = get_vectorized_function(
+        original_fn, rna, {{"use_list__a", Functions::GET_FN_output_false()}});
+    builder.insert_matching_function(fn, vnode);
+  }
+  else {
+    BLI_assert(input_amount == 2);
+    SharedFunction fn = get_vectorized_function(
+        original_fn,
+        rna,
+        {{"use_list__a", Functions::GET_FN_output_false()},
+         {"use_list__b", Functions::GET_FN_output_true()}});
+    builder.insert_matching_function(fn, vnode);
+  }
+}
 void REGISTER_node_inserters(std::unique_ptr<NodeInserters> &inserters)
 {
 #define REGISTER_FUNCTION(idname, fn) inserters->register_function(idname, Functions::GET_FN_##fn)
@@ -377,6 +412,7 @@ void REGISTER_node_inserters(std::unique_ptr<NodeInserters> &inserters)
   REGISTER_INSERTER("fn_SeparateVectorNode", INSERT_separate_vector);
   REGISTER_INSERTER("fn_SwitchNode", INSERT_switch);
   REGISTER_INSERTER("fn_VectorMathNode", INSERT_vector_math);
+  REGISTER_INSERTER("fn_BooleanMathNode", INSERT_boolean);
 
 #undef REGISTER_INSERTER
 #undef REGISTER_FUNCTION
diff --git a/source/blender/functions/functions/boolean.cpp b/source/blender/functions/functions/boolean.cpp
new file mode 100644
index 00000000000..546eb044f14
--- /dev/null
+++ b/source/blender/functions/functions/boolean.cpp
@@ -0,0 +1,81 @@
+#include "BLI_lazy_init.hpp"
+
+#include "FN_tuple_call.hpp"
+#include "FN_types.hpp"
+
+#include "boolean.hpp"
+
+namespace FN {
+namespace Functions {
+
+using namespace Types;
+
+static SharedFunction get_boolean_function__one_input(StringRef name)
+{
+  FunctionBuilder builder;
+  builder.add_input("Value", TYPE_bool);
+  builder.add_output("Result", TYPE_bool);
+  return builder.build(name);
+}
+
+static SharedFunction get_boolean_function__two_inputs(StringRef name)
+{
+  FunctionBuilder builder;
+  builder.add_input("A", TYPE_bool);
+  builder.add_input("B", TYPE_bool);
+  builder.add_output("Result", TYPE_bool);
+  return builder.build(name);
+}
+
+class AndBoolean : public TupleCallBody {
+  void call(Tuple &fn_in, Tuple &fn_out, ExecutionContext &UNUSED(ctx)) const
+  {
+    bool a = fn_in.get<bool>(0);
+    bool b = fn_in.get<bool>(1);
+    bool result = a && b;
+    fn_out.set<bool>(0, result);
+  }
+};
+
+BLI_LAZY_INIT(SharedFunction, GET_FN_and)
+{
+  auto fn = get_boolean_function__two_inputs("And");
+  fn->add_body<AndBoolean>();
+  return fn;
+}
+
+class OrBoolean : public TupleCallBody {
+  void call(Tuple &fn_in, Tuple &fn_out, ExecutionContext &UNUSED(ctx)) const
+  {
+    bool a = fn_in.get<bool>(0);
+    bool b = fn_in.get<bool>(1);
+    bool result = a || b;
+    fn_out.set<bool>(0, result);
+  }
+};
+
+BLI_LAZY_INIT(SharedFunction, GET_FN_or)
+{
+  auto fn = get_boolean_function__two_inputs("Or");
+  fn->add_body<OrBoolean>();
+  return fn;
+}
+
+class NotBoolean : public TupleCallBody {
+  void call(Tuple &fn_in, Tuple &fn_out, ExecutionContext &UNUSED(ctx)) const
+  {
+    bool value = fn_in.get<bool>(0);
+    bool result = !value;
+    fn_out.set<bool>(0, result);
+  }
+};
+
+BLI_LAZY_INIT(SharedFunction, GET_FN_not)
+{
+  auto fn = get_boolean_function__one_input("Not");
+  fn->add_body<NotBoolean>();
+  return fn;
+}
+
+}  // namespace Functions
+}  // namespace FN
diff --git a/source/blender/functions/functions/boolean.hpp b/source/blender/functions/functions/boolean.hpp
new file mode 100644
index 00000000000..c75d189ebf2
--- /dev/null
+++ b/source/blender/functions/functions/boolean.hpp
@@ -0,0 +1,13 @@
+#pragma once
+
+#include "FN_core.hpp"
+
+namespace FN {
+namespace Functions {
+
+SharedFunction &GET_FN_and();
+SharedFunction &GET_FN_or();
+SharedFunction &GET_FN_not();
+
+}  // namespace Functions
+}  // namespace FN



More information about the Bf-blender-cvs mailing list