[Bf-blender-cvs] [a001417d641] simulation-tree: initial automatic rebuild when the node tree changes

Jacques Lucke noreply at git.blender.org
Mon Feb 17 18:16:39 CET 2020


Commit: a001417d6414d851e850433af09b9e0533a9293a
Author: Jacques Lucke
Date:   Mon Feb 17 16:49:26 2020 +0100
Branches: simulation-tree
https://developer.blender.org/rBa001417d6414d851e850433af09b9e0533a9293a

initial automatic rebuild when the node tree changes

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

M	source/blender/simulations/nodes/SIM_node_tree.h
M	source/blender/simulations/nodes/my_test_node.cc
M	source/blender/simulations/nodes/simulation_node_tree.cc

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

diff --git a/source/blender/simulations/nodes/SIM_node_tree.h b/source/blender/simulations/nodes/SIM_node_tree.h
index f55c40aa256..5b08a2dfbc3 100644
--- a/source/blender/simulations/nodes/SIM_node_tree.h
+++ b/source/blender/simulations/nodes/SIM_node_tree.h
@@ -11,6 +11,7 @@ void register_node_tree_type_sim(void);
 void register_node_type_my_test_node(void);
 void init_socket_data_types(void);
 void free_socket_data_types(void);
+void update_sim_node_tree(struct bNodeTree *tree);
 
 #ifdef __cplusplus
 }
diff --git a/source/blender/simulations/nodes/my_test_node.cc b/source/blender/simulations/nodes/my_test_node.cc
index 0228602b982..9316fdc7fdd 100644
--- a/source/blender/simulations/nodes/my_test_node.cc
+++ b/source/blender/simulations/nodes/my_test_node.cc
@@ -9,9 +9,13 @@
 #include "BLI_linear_allocator.h"
 #include "BLI_color.h"
 #include "BLI_string.h"
+#include "BLI_array_cxx.h"
+#include "PIL_time.h"
 
 #include "UI_interface.h"
 
+using BLI::Array;
+using BLI::ArrayRef;
 using BLI::LinearAllocator;
 using BLI::rgba_f;
 using BLI::Set;
@@ -92,15 +96,30 @@ class SocketDecl {
  protected:
   bNodeTree &m_ntree;
   bNode &m_node;
+  uint m_amount;
 
  public:
-  SocketDecl(bNodeTree &ntree, bNode &node) : m_ntree(ntree), m_node(node)
+  SocketDecl(bNodeTree &ntree, bNode &node, uint amount)
+      : m_ntree(ntree), m_node(node), m_amount(amount)
   {
   }
 
+  virtual ~SocketDecl();
+
+  uint amount() const
+  {
+    return m_amount;
+  }
+
+  virtual bool sockets_are_correct(ArrayRef<bNodeSocket *> sockets) const = 0;
+
   virtual void build() const = 0;
 };
 
+SocketDecl::~SocketDecl()
+{
+}
+
 class FixedTypeSocketDecl : public SocketDecl {
   eNodeSocketInOut m_in_out;
   SocketDataType &m_type;
@@ -114,7 +133,7 @@ class FixedTypeSocketDecl : public SocketDecl {
                       SocketDataType &type,
                       StringRefNull ui_name,
                       StringRefNull identifier)
-      : SocketDecl(ntree, node),
+      : SocketDecl(ntree, node, 1),
         m_in_out(in_out),
         m_type(type),
         m_ui_name(ui_name),
@@ -122,6 +141,25 @@ class FixedTypeSocketDecl : public SocketDecl {
   {
   }
 
+  bool sockets_are_correct(ArrayRef<bNodeSocket *> sockets) const override
+  {
+    if (sockets.size() != 1) {
+      return false;
+    }
+
+    bNodeSocket *socket = sockets[0];
+    if (socket->typeinfo != m_type.m_socket_type) {
+      return false;
+    }
+    if (socket->name != m_ui_name) {
+      return false;
+    }
+    if (socket->identifier != m_identifier) {
+      return false;
+    }
+    return true;
+  }
+
   void build() const override
   {
     m_type.build(m_ntree, m_node, m_in_out, m_identifier, m_ui_name);
@@ -148,6 +186,43 @@ class NodeDecl {
       decl->build();
     }
   }
+
+  bool sockets_are_correct() const
+  {
+    if (!this->sockets_are_correct(m_node.inputs, m_inputs)) {
+      return false;
+    }
+    if (!this->sockets_are_correct(m_node.outputs, m_outputs)) {
+      return false;
+    }
+    return true;
+  }
+
+ private:
+  bool sockets_are_correct(ListBase &sockets_list, ArrayRef<SocketDecl *> decls) const
+  {
+    Vector<bNodeSocket *, 10> sockets;
+    LISTBASE_FOREACH (bNodeSocket *, socket, &sockets_list) {
+      sockets.append(socket);
+    }
+
+    uint offset = 0;
+    for (SocketDecl *decl : decls) {
+      uint amount = decl->amount();
+      if (offset + amount > sockets.size()) {
+        return false;
+      }
+      ArrayRef<bNodeSocket *> sockets_for_decl = sockets.as_ref().slice(offset, amount);
+      if (!decl->sockets_are_correct(sockets_for_decl)) {
+        return false;
+      }
+      offset += amount;
+    }
+    if (offset != sockets.size()) {
+      return false;
+    }
+    return true;
+  }
 };
 
 class NodeBuilder {
@@ -186,18 +261,24 @@ class NodeBuilder {
   }
 };
 
+using DeclareNodeFunc = void (*)(NodeBuilder &builder);
+
+static void declare_test_node(NodeBuilder &builder)
+{
+  builder.fixed_input("id1", "ID 1", *data_socket_float);
+  builder.fixed_input("id2", "ID 2", *data_socket_int);
+  builder.fixed_input("id4", "ID 4", *data_socket_int_list);
+  builder.fixed_output("id3", "ID 3", *data_socket_float);
+  builder.fixed_output(
+      "id5", "Hello " + std::to_string(PIL_check_seconds_timer_i() / 10), *data_socket_float_list);
+}
+
 static void init_node(bNodeTree *ntree, bNode *node)
 {
   LinearAllocator<> allocator;
   NodeDecl node_decl{*ntree, *node};
   NodeBuilder node_builder{allocator, node_decl};
-
-  node_builder.fixed_input("id1", "ID 1", *data_socket_float);
-  node_builder.fixed_input("id2", "ID 2", *data_socket_int);
-  node_builder.fixed_input("id4", "ID 4", *data_socket_int_list);
-  node_builder.fixed_output("id3", "ID 3", *data_socket_float);
-  node_builder.fixed_output("id5", "ID 5", *data_socket_float_list);
-
+  declare_test_node(node_builder);
   node_decl.build();
 }
 
@@ -218,6 +299,7 @@ void register_node_type_my_test_node()
 
   ntype.initfunc = init_node;
   ntype.poll = [](bNodeType *UNUSED(ntype), bNodeTree *UNUSED(ntree)) { return true; };
+  ntype.userdata = (void *)declare_test_node;
 
   nodeRegisterType(&ntype);
 }
@@ -279,3 +361,29 @@ void free_socket_data_types()
   delete data_socket_float_list;
   delete data_socket_int_list;
 }
+
+void update_sim_node_tree(bNodeTree *ntree)
+{
+  Vector<bNode *> nodes;
+  for (bNode *node : BLI::IntrusiveListBaseWrapper<bNode>(ntree->nodes)) {
+    nodes.append(node);
+  }
+
+  LinearAllocator<> allocator;
+
+  for (bNode *node : nodes) {
+    NodeDecl node_decl{*ntree, *node};
+    NodeBuilder builder{allocator, node_decl};
+    DeclareNodeFunc fn = (DeclareNodeFunc)node->typeinfo->userdata;
+    fn(builder);
+
+    if (!node_decl.sockets_are_correct()) {
+      std::cout << "Rebuild\n";
+      nodeRemoveAllSockets(ntree, node);
+      node_decl.build();
+    }
+    else {
+      std::cout << "Don't rebuild\n";
+    }
+  }
+}
diff --git a/source/blender/simulations/nodes/simulation_node_tree.cc b/source/blender/simulations/nodes/simulation_node_tree.cc
index 919ccfa7b5c..e3a03219b5f 100644
--- a/source/blender/simulations/nodes/simulation_node_tree.cc
+++ b/source/blender/simulations/nodes/simulation_node_tree.cc
@@ -18,6 +18,7 @@ void register_node_tree_type_sim()
   strcpy(tt->ui_description, N_("Simulation nodes"));
   tt->ui_icon = 0; /* Defined in drawnode.c */
   tt->ext.srna = &RNA_SimulationNodeTree;
+  tt->update = update_sim_node_tree;
 
   ntreeTypeAdd(tt);



More information about the Bf-blender-cvs mailing list