[Bf-blender-cvs] [b52c3b3fc7e] functions: Initial string socket type

Jacques Lucke noreply at git.blender.org
Tue Aug 27 17:26:13 CEST 2019


Commit: b52c3b3fc7e50083d00765344d32e9293e15b5d5
Author: Jacques Lucke
Date:   Tue Aug 27 17:22:47 2019 +0200
Branches: functions
https://developer.blender.org/rBb52c3b3fc7e50083d00765344d32e9293e15b5d5

Initial string socket type

The internal implementation is working, but not very efficient yet (every string
is allocated separately without any cache). I'm still thinking about a better
solution, but for now having any working text socket is more important.

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

A	release/scripts/startup/nodes/function_nodes/text.py
M	release/scripts/startup/nodes/sockets.py
M	release/scripts/startup/nodes/types.py
M	source/blender/blenlib/BLI_string_ref.hpp
M	source/blender/functions/CMakeLists.txt
M	source/blender/functions/FN_functions.hpp
M	source/blender/functions/FN_types.hpp
M	source/blender/functions/frontends/data_flow_nodes/mappings/node_inserters.cpp
M	source/blender/functions/frontends/data_flow_nodes/mappings/socket_loaders.cpp
M	source/blender/functions/frontends/data_flow_nodes/mappings/type_mappings.cpp
M	source/blender/functions/functions/lists.cpp
A	source/blender/functions/functions/string.cpp
A	source/blender/functions/functions/string.hpp
M	source/blender/functions/types/initialization.cpp
A	source/blender/functions/types/string_type.cpp
A	source/blender/functions/types/string_type.hpp

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

diff --git a/release/scripts/startup/nodes/function_nodes/text.py b/release/scripts/startup/nodes/function_nodes/text.py
new file mode 100644
index 00000000000..894d6cdb1d4
--- /dev/null
+++ b/release/scripts/startup/nodes/function_nodes/text.py
@@ -0,0 +1,11 @@
+import bpy
+from .. node_builder import NodeBuilder
+from .. base import FunctionNode
+
+class TextLengthNode(bpy.types.Node, FunctionNode):
+    bl_idname = "fn_TextLengthNode"
+    bl_label = "Text Length"
+
+    def declaration(self, builder: NodeBuilder):
+        builder.fixed_input("text", "Text", "Text")
+        builder.fixed_output("length", "Length", "Integer")
diff --git a/release/scripts/startup/nodes/sockets.py b/release/scripts/startup/nodes/sockets.py
index 2a1a272dae3..e10c5538833 100644
--- a/release/scripts/startup/nodes/sockets.py
+++ b/release/scripts/startup/nodes/sockets.py
@@ -134,6 +134,26 @@ class ColorSocket(bpy.types.NodeSocket, DataSocket):
     def restore_state(self, state):
         self.value = state
 
+class TextSocket(bpy.types.NodeSocket, DataSocket):
+    bl_idname = "fn_TextSocket"
+    bl_label = "Text Socket"
+    data_type = "Text"
+    color = (0.8, 0.8, 0.8, 1)
+
+    value: StringProperty(
+        name="Value",
+        default="",
+    )
+
+    def draw_property(self, layout, node, text):
+        layout.prop(self, "value", text=text)
+
+    def get_state(self):
+        return self.value
+
+    def restore_state(self, state):
+        self.value = state
+
 def create_simple_data_socket(idname, data_type, color):
     return type(idname, (bpy.types.NodeSocket, DataSocket),
         {
@@ -155,6 +175,8 @@ ObjectListSocket = create_simple_data_socket(
     "fn_ObjectListSocket", "Object List", (0, 0, 0, 0.5))
 ColorListSocket = create_simple_data_socket(
     "fn_ColorListSocket", "Color List", (0.8, 0.8, 0.2, 0.5))
+TextListSocket = create_simple_data_socket(
+    "fn_TextListSocket", "Text List", (0.8, 0.8, 0.8, 0.5))
 
 class ExecuteSocket(bpy.types.NodeSocket, BaseSocket):
     bl_idname = "bp_ExecuteSocket"
diff --git a/release/scripts/startup/nodes/types.py b/release/scripts/startup/nodes/types.py
index 00046dbd598..6ad800d983f 100644
--- a/release/scripts/startup/nodes/types.py
+++ b/release/scripts/startup/nodes/types.py
@@ -9,5 +9,6 @@ type_infos.insert_data_type(s.IntegerSocket, s.IntegerListSocket)
 type_infos.insert_data_type(s.BooleanSocket, s.BooleanListSocket)
 type_infos.insert_data_type(s.ObjectSocket, s.ObjectListSocket)
 type_infos.insert_data_type(s.ColorSocket, s.ColorListSocket)
+type_infos.insert_data_type(s.TextSocket, s.TextListSocket)
 
 type_infos.insert_conversion_group(["Boolean", "Integer", "Float"])
diff --git a/source/blender/blenlib/BLI_string_ref.hpp b/source/blender/blenlib/BLI_string_ref.hpp
index 01edb004b55..678dccd9178 100644
--- a/source/blender/blenlib/BLI_string_ref.hpp
+++ b/source/blender/blenlib/BLI_string_ref.hpp
@@ -94,6 +94,12 @@ class StringRefBase {
     return m_data + m_size;
   }
 
+  void copy_to__with_null(char *dst) const
+  {
+    memcpy(dst, m_data, m_size);
+    dst[m_size] = '\0';
+  }
+
   /**
    * Returns true when the string begins with the given prefix. Otherwise false.
    */
diff --git a/source/blender/functions/CMakeLists.txt b/source/blender/functions/CMakeLists.txt
index 3b5115b4036..0658650b4e3 100644
--- a/source/blender/functions/CMakeLists.txt
+++ b/source/blender/functions/CMakeLists.txt
@@ -110,6 +110,8 @@ set(SRC
   types/boolean.cpp
   types/external.hpp
   types/external.cpp
+  types/string_type.hpp
+  types/string_type.cpp
   types/types-c.h
   types/types-c.cpp
   types/tuple_access-c.h
@@ -127,6 +129,8 @@ set(SRC
   functions/lists.cpp
   functions/simple_conversions.hpp
   functions/simple_conversions.cpp
+  functions/string.hpp
+  functions/string.cpp
   functions/switch.hpp
   functions/switch.cpp
   functions/auto_vectorization.hpp
diff --git a/source/blender/functions/FN_functions.hpp b/source/blender/functions/FN_functions.hpp
index b9498cdf370..17e0078f5de 100644
--- a/source/blender/functions/FN_functions.hpp
+++ b/source/blender/functions/FN_functions.hpp
@@ -14,3 +14,4 @@
 #include "functions/color.hpp"
 #include "functions/array_execution.hpp"
 #include "functions/boolean.hpp"
+#include "functions/string.hpp"
diff --git a/source/blender/functions/FN_types.hpp b/source/blender/functions/FN_types.hpp
index f9e5229c7b0..1f347997202 100644
--- a/source/blender/functions/FN_types.hpp
+++ b/source/blender/functions/FN_types.hpp
@@ -5,4 +5,5 @@
 #include "types/numeric.hpp"
 #include "types/boolean.hpp"
 #include "types/external.hpp"
+#include "types/string_type.hpp"
 #include "types/initialization.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 ade7bd7105c..41a1966efa4 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
@@ -397,6 +397,7 @@ void REGISTER_node_inserters(std::unique_ptr<NodeInserters> &inserters)
   REGISTER_FUNCTION("fn_ObjectMeshNode", object_mesh_vertices);
   REGISTER_FUNCTION("fn_RandomNumberNode", random_number);
   REGISTER_FUNCTION("fn_VectorDistanceNode", vector_distance);
+  REGISTER_FUNCTION("fn_TextLengthNode", string_length);
 
   REGISTER_INSERTER("fn_CallNode", INSERT_call);
   REGISTER_INSERTER("fn_ClampNode", INSERT_clamp);
diff --git a/source/blender/functions/frontends/data_flow_nodes/mappings/socket_loaders.cpp b/source/blender/functions/frontends/data_flow_nodes/mappings/socket_loaders.cpp
index f19a72c891f..95102e29310 100644
--- a/source/blender/functions/frontends/data_flow_nodes/mappings/socket_loaders.cpp
+++ b/source/blender/functions/frontends/data_flow_nodes/mappings/socket_loaders.cpp
@@ -50,6 +50,15 @@ static void LOAD_color(PointerRNA *rna, Tuple &tuple, uint index)
   tuple.set<rgba_f>(index, color);
 }
 
+static void LOAD_text(PointerRNA *rna, Tuple &tuple, uint index)
+{
+  int length = RNA_string_length(rna, "value");
+  char *stack_str = (char *)alloca(length + 1);
+  RNA_string_get(rna, "value", stack_str);
+  MyString str(stack_str);
+  tuple.move_in(index, str);
+}
+
 static SocketLoader GET_empty_list_loader(Type *type)
 {
   return [type](PointerRNA *UNUSED(rna), Tuple &tuple, uint index) {
@@ -70,6 +79,8 @@ void REGISTER_socket_loaders(std::unique_ptr<SocketLoaders> &loaders)
   loaders->register_loader("Integer", LOAD_integer);
   loaders->register_loader("Object List", GET_empty_list_loader(TYPE_object));
   loaders->register_loader("Object", LOAD_object);
+  loaders->register_loader("Text List", GET_empty_list_loader(TYPE_string));
+  loaders->register_loader("Text", LOAD_text);
   loaders->register_loader("Vector List", GET_empty_list_loader(TYPE_float3));
   loaders->register_loader("Vector", LOAD_vector);
 }
diff --git a/source/blender/functions/frontends/data_flow_nodes/mappings/type_mappings.cpp b/source/blender/functions/frontends/data_flow_nodes/mappings/type_mappings.cpp
index caa474448e0..8e0c176816a 100644
--- a/source/blender/functions/frontends/data_flow_nodes/mappings/type_mappings.cpp
+++ b/source/blender/functions/frontends/data_flow_nodes/mappings/type_mappings.cpp
@@ -21,6 +21,8 @@ void REGISTER_type_mappings(std::unique_ptr<TypeMappings> &type_mappings)
   ADD_TYPE("fn_IntegerSocket", "Integer", int32);
   ADD_TYPE("fn_ObjectListSocket", "Object List", object_list);
   ADD_TYPE("fn_ObjectSocket", "Object", object);
+  ADD_TYPE("fn_TextListSocket", "Text List", string_list);
+  ADD_TYPE("fn_TextSocket", "Text", string);
   ADD_TYPE("fn_VectorListSocket", "Vector List", float3_list);
   ADD_TYPE("fn_VectorSocket", "Vector", float3);
 
diff --git a/source/blender/functions/functions/lists.cpp b/source/blender/functions/functions/lists.cpp
index 2ca41fd0a0d..ae4e0c4da19 100644
--- a/source/blender/functions/functions/lists.cpp
+++ b/source/blender/functions/functions/lists.cpp
@@ -268,6 +268,7 @@ BLI_LAZY_INIT_STATIC(ListFunctions, get_list_functions)
   insert_list_functions_for_type(functions, TYPE_bool, TYPE_bool_list);
   insert_list_functions_for_type(functions, TYPE_object, TYPE_object_list);
   insert_list_functions_for_type(functions, TYPE_rgba_f, TYPE_rgba_f_list);
+  insert_list_functions_for_type(functions, TYPE_string, TYPE_string_list);
   return functions;
 }
 
diff --git a/source/blender/functions/functions/string.cpp b/source/blender/functions/functions/string.cpp
new file mode 100644
index 00000000000..a366b844ef4
--- /dev/null
+++ b/source/blender/functions/functions/string.cpp
@@ -0,0 +1,34 @@
+#include "FN_types.hpp"
+#include "FN_functions.hpp"
+#include "FN_tuple_call.hpp"
+#include "FN_llvm.hpp"
+#include "BLI_lazy_init.hpp"
+#include "BLI_math.h"
+
+namespace FN {
+namespace Functions {
+
+using namespace Types;
+
+class StringLength : public TupleCallBody {
+  void call(Tuple &fn_in, Tuple &fn_out, ExecutionContext &UNUSED(ctx)) const override
+  {
+    MyString str = fn_in.relocate_out<MyString>(0);
+    int length = str.size();
+    fn_out.set<int32_t>(0, length);
+  }
+};
+
+BLI_LAZY_INIT(SharedFunction, GET_FN_string_length)
+{
+  FunctionBuilder builder;
+  builder.add_input("String", TYPE_string);
+  builder.add_output("Length", TYPE_int32);
+
+  auto fn = builder.build("String Length");
+  fn->add_body<StringLength>();
+  return fn;
+}
+
+}  // namespace Functions
+}  // namespace FN
diff --git a/source/blender/functions/functions/string.hpp b/source/blender/functions/functions/string.hpp
new file mode 100644
index 00000000000..7bb58f0bb0b
--- /dev/null
+++ b/source/blender/functions/functions/string.hpp
@@ -0,0 +1,10 @@
+#pragma once
+
+#include "FN_core.hpp"
+
+namespace FN {
+namespace Functions {
+
+SharedFunction &GET_FN_string_length();
+}
+}  // namespace FN
diff --git a/source/blender/functions/types/initialization.cpp b/source/blender/functions/types/initialization.cpp
index 5ccd06c7dbe..ab09d64ee5e 100644
--- a/source/blender/functions/types/initialization.cpp
+++ b/source/blender/functions/types/initialization.cpp
@@ -10,6 +10,7 @@ void initialize_types(void)
   INIT_bool(types_to_free);


@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list