[Bf-blender-cvs] [8369adabc0e] master: Particles: initial object socket and emitter node support

Jacques Lucke noreply at git.blender.org
Tue Jul 21 17:37:13 CEST 2020


Commit: 8369adabc0ec7a1fce248b688bf20860ae0434bb
Author: Jacques Lucke
Date:   Tue Jul 21 17:20:05 2020 +0200
Branches: master
https://developer.blender.org/rB8369adabc0ec7a1fce248b688bf20860ae0434bb

Particles: initial object socket and emitter node support

Object sockets work now, but only the new Object Transforms and the
Particle Mesh Emitter node use it. The emitter does not actually
use the mesh surface yet. Instead, new particles are just emitted around
the origin of the object.

Internally, handles to object data blocks are passed around in the network,
instead of raw object pointers. Using handles has a couple of benefits:
* The caller of the function has control over which handles can be resolved
  and therefore limit access to specific data. The set of data blocks that
  is accessed by a node tree should be known statically. This is necessary
  for a proper integration with the dependency graph.
* When the pointer to an object changes (e.g. after restarting Blender),
  all handles are still valid.
* When an object is deleted, the handle is invalidated without causing crashes.
* The handle is just an integer that can be stored per particle and can be cached easily.

The mapping between handles and their corresponding data blocks is
stored in the Simulation data block.

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

M	release/scripts/startup/nodeitems_builtins.py
M	source/blender/blenkernel/BKE_node.h
A	source/blender/blenkernel/BKE_persistent_data_handle.hh
M	source/blender/blenkernel/CMakeLists.txt
M	source/blender/blenkernel/intern/node.c
M	source/blender/blenkernel/intern/simulation.cc
M	source/blender/blenloader/intern/readfile.c
M	source/blender/blenloader/intern/writefile.c
M	source/blender/functions/FN_multi_function.hh
M	source/blender/functions/FN_multi_function_context.hh
M	source/blender/functions/FN_multi_function_network.hh
M	source/blender/functions/FN_multi_function_params.hh
M	source/blender/functions/FN_multi_function_signature.hh
M	source/blender/functions/intern/multi_function_network_evaluation.cc
M	source/blender/functions/intern/multi_function_network_optimization.cc
M	source/blender/makesdna/DNA_simulation_types.h
M	source/blender/nodes/CMakeLists.txt
M	source/blender/nodes/NOD_function.h
M	source/blender/nodes/NOD_node_tree_multi_function.hh
M	source/blender/nodes/NOD_static_types.h
A	source/blender/nodes/function/nodes/node_fn_object_transforms.cc
M	source/blender/nodes/intern/node_socket.cc
M	source/blender/simulation/intern/particle_function.cc
M	source/blender/simulation/intern/particle_function.hh
M	source/blender/simulation/intern/simulation_collect_influences.cc
M	source/blender/simulation/intern/simulation_solver.cc
M	source/blender/simulation/intern/simulation_solver.hh

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

diff --git a/release/scripts/startup/nodeitems_builtins.py b/release/scripts/startup/nodeitems_builtins.py
index 50c1a2e4e4e..60eb0194a9a 100644
--- a/release/scripts/startup/nodeitems_builtins.py
+++ b/release/scripts/startup/nodeitems_builtins.py
@@ -485,6 +485,7 @@ simulation_node_categories = [
         NodeItem("SimulationNodeParticleAttribute"),
         NodeItem("FunctionNodeGroupInstanceID"),
         NodeItem("ShaderNodeValue"),
+        NodeItem("FunctionNodeObjectTransforms"),
     ]),
     SimulationNodeCategory("SIM_EMITTERS", "Emitters", items=[
         NodeItem("SimulationNodeParticleMeshEmitter"),
diff --git a/source/blender/blenkernel/BKE_node.h b/source/blender/blenkernel/BKE_node.h
index 1d01436c7e4..4c55488ecd5 100644
--- a/source/blender/blenkernel/BKE_node.h
+++ b/source/blender/blenkernel/BKE_node.h
@@ -1343,6 +1343,7 @@ int ntreeTexExecTree(struct bNodeTree *ntree,
 #define FN_NODE_FLOAT_COMPARE 1202
 #define FN_NODE_GROUP_INSTANCE_ID 1203
 #define FN_NODE_COMBINE_STRINGS 1204
+#define FN_NODE_OBJECT_TRANSFORMS 1205
 
 /** \} */
 
diff --git a/source/blender/blenkernel/BKE_persistent_data_handle.hh b/source/blender/blenkernel/BKE_persistent_data_handle.hh
new file mode 100644
index 00000000000..721132560e3
--- /dev/null
+++ b/source/blender/blenkernel/BKE_persistent_data_handle.hh
@@ -0,0 +1,132 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef __BKE_PERSISTENT_DATA_HANDLE_H__
+#define __BKE_PERSISTENT_DATA_HANDLE_H__
+
+/** \file
+ * \ingroup bke
+ *
+ * A PersistentDataHandle is a weak reference to some data in a Blender file. The handle itself is
+ * just a number. A PersistentDataHandleMap is used to convert between handles and the actual data.
+ */
+
+#include "BLI_map.hh"
+
+#include "DNA_ID.h"
+
+struct Object;
+
+namespace blender::bke {
+
+class PersistentDataHandleMap;
+
+class PersistentDataHandle {
+ private:
+  /* Negative values indicate that the handle is "empty". */
+  int32_t handle_;
+
+  friend PersistentDataHandleMap;
+
+ protected:
+  PersistentDataHandle(int handle) : handle_(handle)
+  {
+  }
+
+ public:
+  PersistentDataHandle() : handle_(-1)
+  {
+  }
+
+  friend bool operator==(const PersistentDataHandle &a, const PersistentDataHandle &b)
+  {
+    return a.handle_ == b.handle_;
+  }
+
+  friend bool operator!=(const PersistentDataHandle &a, const PersistentDataHandle &b)
+  {
+    return !(a == b);
+  }
+
+  friend std::ostream &operator<<(std::ostream &stream, const PersistentDataHandle &a)
+  {
+    stream << a.handle_;
+    return stream;
+  }
+
+  uint64_t hash() const
+  {
+    return (uint64_t)handle_;
+  }
+};
+
+class PersistentIDHandle : public PersistentDataHandle {
+  friend PersistentDataHandleMap;
+  using PersistentDataHandle::PersistentDataHandle;
+};
+
+class PersistentObjectHandle : public PersistentIDHandle {
+  friend PersistentDataHandleMap;
+  using PersistentIDHandle::PersistentIDHandle;
+};
+
+class PersistentDataHandleMap {
+ private:
+  Map<int32_t, const ID *> id_by_handle_;
+  Map<const ID *, int32_t> handle_by_id_;
+
+ public:
+  void add(int32_t handle, const ID &id)
+  {
+    BLI_assert(handle >= 0);
+    handle_by_id_.add(&id, handle);
+    id_by_handle_.add(handle, &id);
+  }
+
+  PersistentIDHandle lookup(const ID *id) const
+  {
+    const int handle = handle_by_id_.lookup_default(id, -1);
+    return PersistentIDHandle(handle);
+  }
+
+  PersistentObjectHandle lookup(const Object *object) const
+  {
+    const int handle = handle_by_id_.lookup_default((const ID *)object, -1);
+    return PersistentObjectHandle(handle);
+  }
+
+  const ID *lookup(const PersistentIDHandle &handle) const
+  {
+    const ID *id = id_by_handle_.lookup_default(handle.handle_, nullptr);
+    return id;
+  }
+
+  const Object *lookup(const PersistentObjectHandle &handle) const
+  {
+    const ID *id = this->lookup((const PersistentIDHandle &)handle);
+    if (id == nullptr) {
+      return nullptr;
+    }
+    if (GS(id->name) != ID_OB) {
+      return nullptr;
+    }
+    return (const Object *)id;
+  }
+};
+
+}  // namespace blender::bke
+
+#endif /*  __BKE_PERSISTENT_DATA_HANDLE_H__ */
diff --git a/source/blender/blenkernel/CMakeLists.txt b/source/blender/blenkernel/CMakeLists.txt
index 800fa7d18ee..d702da55ea8 100644
--- a/source/blender/blenkernel/CMakeLists.txt
+++ b/source/blender/blenkernel/CMakeLists.txt
@@ -363,6 +363,7 @@ set(SRC
   BKE_packedFile.h
   BKE_paint.h
   BKE_particle.h
+  BKE_persistent_data_handle.hh
   BKE_pbvh.h
   BKE_pointcache.h
   BKE_pointcloud.h
diff --git a/source/blender/blenkernel/intern/node.c b/source/blender/blenkernel/intern/node.c
index 52f0d259058..20d65e52b09 100644
--- a/source/blender/blenkernel/intern/node.c
+++ b/source/blender/blenkernel/intern/node.c
@@ -4345,6 +4345,7 @@ static void registerFunctionNodes(void)
   register_node_type_fn_switch();
   register_node_type_fn_group_instance_id();
   register_node_type_fn_combine_strings();
+  register_node_type_fn_object_transforms();
 }
 
 void init_nodesystem(void)
diff --git a/source/blender/blenkernel/intern/simulation.cc b/source/blender/blenkernel/intern/simulation.cc
index 1ac987d130d..1f11869e21c 100644
--- a/source/blender/blenkernel/intern/simulation.cc
+++ b/source/blender/blenkernel/intern/simulation.cc
@@ -89,6 +89,9 @@ static void simulation_copy_data(Main *bmain, ID *id_dst, const ID *id_src, cons
   }
 
   BLI_listbase_clear(&simulation_dst->states);
+
+  BLI_duplicatelist(&simulation_dst->persistent_data_handles,
+                    &simulation_src->persistent_data_handles);
 }
 
 static void free_simulation_state_head(SimulationState *state)
@@ -169,6 +172,8 @@ static void simulation_free_data(ID *id)
   }
 
   BKE_simulation_state_remove_all(simulation);
+
+  BLI_freelistN(&simulation->persistent_data_handles);
 }
 
 static void simulation_foreach_id(ID *id, LibraryForeachIDData *data)
@@ -178,6 +183,10 @@ static void simulation_foreach_id(ID *id, LibraryForeachIDData *data)
     /* nodetree **are owned by IDs**, treat them as mere sub-data and not real ID! */
     BKE_library_foreach_ID_embedded(data, (ID **)&simulation->nodetree);
   }
+  LISTBASE_FOREACH (
+      PersistentDataHandleItem *, handle_item, &simulation->persistent_data_handles) {
+    BKE_LIB_FOREACHID_PROCESS_ID(data, handle_item->id, IDWALK_CB_USER);
+  }
 }
 
 IDTypeInfo IDType_ID_SIM = {
diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c
index 8bfab730313..f443859e62c 100644
--- a/source/blender/blenloader/intern/readfile.c
+++ b/source/blender/blenloader/intern/readfile.c
@@ -8675,8 +8675,12 @@ static void direct_link_volume(BlendDataReader *reader, Volume *volume)
 /** \name Read ID: Simulation
  * \{ */
 
-static void lib_link_simulation(BlendLibReader *UNUSED(reader), Simulation *UNUSED(simulation))
+static void lib_link_simulation(BlendLibReader *reader, Simulation *simulation)
 {
+  LISTBASE_FOREACH (
+      PersistentDataHandleItem *, handle_item, &simulation->persistent_data_handles) {
+    BLO_read_id_address(reader, simulation->id.lib, &handle_item->id);
+  }
 }
 
 static void direct_link_simulation(BlendDataReader *reader, Simulation *simulation)
@@ -8697,6 +8701,8 @@ static void direct_link_simulation(BlendDataReader *reader, Simulation *simulati
       };
     }
   }
+
+  BLO_read_list(reader, &simulation->persistent_data_handles);
 }
 
 /** \} */
@@ -11105,6 +11111,10 @@ static void expand_simulation(BlendExpander *expander, Simulation *simulation)
   if (simulation->adt) {
     expand_animdata(expander, simulation->adt);
   }
+  LISTBASE_FOREACH (
+      PersistentDataHandleItem *, handle_item, &simulation->persistent_data_handles) {
+    BLO_expand(expander, handle_item->id);
+  }
 }
 
 /**
diff --git a/source/blender/blenloader/intern/writefile.c b/source/blender/blenloader/intern/writefile.c
index 8b9de3eea24..d33d8032ed1 100644
--- a/source/blender/blenloader/intern/writefile.c
+++ b/source/blender/blenloader/intern/writefile.c
@@ -3859,6 +3859,8 @@ static void write_simulation(BlendWriter *writer, Simulation *simulation, const
         }
       }
     }
+
+    BLO_write_struct_list(writer, PersistentDataHandleItem, &simulation->persistent_data_handles);
   }
 }
 
diff --git a/source/blender/functions/FN_multi_function.hh b/source/blender/functions/FN_multi_function.hh
index 77ab2749377..eaddcee7964 100644
--- a/source/blender/functions/FN_multi_function.hh
+++ b/source/blender/functions/FN_multi_function.hh
@@ -98,6 +98,11 @@ class MultiFunction {
     return signature_.function_name;
   }
 
+  bool depends_on_context() const
+  {
+    return signature_.depends_on_context;
+  }
+
   const MFSignature &signature() const
   {
     return signature_;
diff --git a/source/blender/functions/FN_multi_function_context.hh b/source/blender/functions/FN_multi_function_context.hh
index 3a448cc2c6e..8492fd86742 100644
--- a/source/blender/functions/FN_multi_function_context.hh
+++ b/source/blender/functions/FN_multi_function_context.hh
@@ -29,15 +29,39 @@
 
 #include "BLI_utildefines.h"
 
+#include "BLI_map.hh"
+
 namespace blender::fn {
 
+class MFContext;
+
 class MFContextBuilder {
+ private:
+  Map<std::string, const void *> global_contexts_;
+
+  friend MFContext;
+
+ public:
+  template<typename T> void add_global_context(std::string name, const T *context)
+  {
+    global_contexts_.add_new(std::move(name), (const void *)context);
+  }
 };
 
 class MFContext {
+ private:
+  MFContextBuilder &builder_;
+
  public:
-  MFContext(MFContextBuilder &UNU

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list