[Bf-blender-cvs] [e95c0078a29] functions: initial custom function emitter

Jacques Lucke noreply at git.blender.org
Mon Jul 15 18:13:03 CEST 2019


Commit: e95c0078a299ab2f3d3027cef2f0adb72b81195c
Author: Jacques Lucke
Date:   Mon Jul 15 17:56:02 2019 +0200
Branches: functions
https://developer.blender.org/rBe95c0078a299ab2f3d3027cef2f0adb72b81195c

initial custom function emitter

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

A	release/scripts/startup/nodes/bparticle_nodes/custom_emitter.py
M	source/blender/functions/types/lists.hpp
M	source/blender/simulations/bparticles/emitters.cpp
M	source/blender/simulations/bparticles/emitters.hpp
M	source/blender/simulations/bparticles/inserters.cpp

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

diff --git a/release/scripts/startup/nodes/bparticle_nodes/custom_emitter.py b/release/scripts/startup/nodes/bparticle_nodes/custom_emitter.py
new file mode 100644
index 00000000000..f0f02720950
--- /dev/null
+++ b/release/scripts/startup/nodes/bparticle_nodes/custom_emitter.py
@@ -0,0 +1,20 @@
+import bpy
+from bpy.props import *
+from .. base import FunctionNode
+from .. socket_builder import SocketBuilder
+
+class CustomEmitterNode(bpy.types.Node, FunctionNode):
+    bl_idname = "bp_CustomEmitterNode"
+    bl_label = "Custom Emitter"
+
+    function_tree: PointerProperty(
+        name="Function Tree",
+        type=bpy.types.NodeTree,
+        update=FunctionNode.refresh,
+    )
+
+    def declaration(self, builder: SocketBuilder):
+        builder.emitter_output("emitter", "Emitter")
+
+    def draw(self, layout):
+        layout.prop(self, "function_tree", text="")
diff --git a/source/blender/functions/types/lists.hpp b/source/blender/functions/types/lists.hpp
index 93fa8d86fb7..b04dd4f9901 100644
--- a/source/blender/functions/types/lists.hpp
+++ b/source/blender/functions/types/lists.hpp
@@ -35,6 +35,11 @@ template<typename T> class List : public BLI::SharedImmutable {
     m_data = SmallVector<T>(size);
   }
 
+  operator ArrayRef<T>() const
+  {
+    return m_data;
+  }
+
   void append(T value)
   {
     this->assert_mutable();
diff --git a/source/blender/simulations/bparticles/emitters.cpp b/source/blender/simulations/bparticles/emitters.cpp
index 28a2b02c1c0..ef0b0aa17c8 100644
--- a/source/blender/simulations/bparticles/emitters.cpp
+++ b/source/blender/simulations/bparticles/emitters.cpp
@@ -8,6 +8,8 @@
 
 #include "BLI_math_geom.h"
 
+#include "FN_types.hpp"
+
 #include "emitters.hpp"
 
 namespace BParticles {
@@ -118,4 +120,24 @@ void SurfaceEmitter::emit(EmitterInterface &interface)
   ActionInterface::RunFromEmitter(m_action, target, interface);
 }
 
+void CustomFunctionEmitter::emit(EmitterInterface &interface)
+{
+  TupleCallBody *body = m_function->body<TupleCallBody>();
+  BLI_assert(body);
+
+  if (m_function->input_amount() > 0) {
+    return;
+  }
+
+  FN_TUPLE_CALL_ALLOC_TUPLES(body, fn_in, fn_out);
+  body->call__setup_execution_context(fn_in, fn_out);
+
+  auto new_positions = fn_out.relocate_out<FN::Types::SharedFloat3List>(0);
+
+  auto target = interface.particle_allocator().request(m_particle_type_name,
+                                                       new_positions->size());
+  target.set_float3("Position", *new_positions.ptr());
+  target.fill_float("Birth Time", interface.time_span().end());
+}
+
 }  // namespace BParticles
diff --git a/source/blender/simulations/bparticles/emitters.hpp b/source/blender/simulations/bparticles/emitters.hpp
index 323e796e4c6..db962de0d78 100644
--- a/source/blender/simulations/bparticles/emitters.hpp
+++ b/source/blender/simulations/bparticles/emitters.hpp
@@ -46,7 +46,7 @@ class SurfaceEmitter : public Emitter {
   void emit(EmitterInterface &interface) override;
 };
 
-struct PointEmitter : public Emitter {
+class PointEmitter : public Emitter {
  private:
   std::string m_particle_type_name;
   InterpolatedFloat3 m_point;
@@ -61,4 +61,18 @@ struct PointEmitter : public Emitter {
   void emit(EmitterInterface &interface) override;
 };
 
+class CustomFunctionEmitter : public Emitter {
+ private:
+  std::string m_particle_type_name;
+  SharedFunction m_function;
+
+ public:
+  CustomFunctionEmitter(StringRef particle_type_name, SharedFunction &function)
+      : m_particle_type_name(particle_type_name.to_std_string()), m_function(function)
+  {
+  }
+
+  void emit(EmitterInterface &interface) override;
+};
+
 }  // namespace BParticles
diff --git a/source/blender/simulations/bparticles/inserters.cpp b/source/blender/simulations/bparticles/inserters.cpp
index b5887aa472a..719185b5b9b 100644
--- a/source/blender/simulations/bparticles/inserters.cpp
+++ b/source/blender/simulations/bparticles/inserters.cpp
@@ -207,7 +207,7 @@ static std::unique_ptr<Force> BUILD_FORCE_turbulence(BuildContext &ctx, bNode *b
   return std::unique_ptr<Force>(new TurbulenceForce(fn));
 }
 
-static std::unique_ptr<Event> Build_EVENT_mesh_collision(BuildContext &ctx, bNode *bnode)
+static std::unique_ptr<Event> BUILD_EVENT_mesh_collision(BuildContext &ctx, bNode *bnode)
 {
   PointerRNA rna = ctx.indexed_tree.get_rna(bnode);
   Object *object = (Object *)RNA_pointer_get(&rna, "object").id.data;
@@ -270,6 +270,24 @@ static std::unique_ptr<Emitter> BUILD_EMITTER_moving_point(BuildContext &ctx,
   return std::unique_ptr<PointEmitter>(new PointEmitter(particle_type_name, point, 10));
 }
 
+static std::unique_ptr<Emitter> BUILD_EMITTER_custom_function(BuildContext &ctx,
+                                                              bNode *bnode,
+                                                              StringRef particle_type_name)
+{
+  PointerRNA rna = ctx.indexed_tree.get_rna(bnode);
+  bNodeTree *btree = (bNodeTree *)RNA_pointer_get(&rna, "function_tree").id.data;
+  if (btree == nullptr) {
+    return {};
+  }
+
+  Optional<SharedFunction> fn = FN::DataFlowNodes::generate_function(btree);
+  if (!fn.has_value()) {
+    return {};
+  }
+
+  return std::unique_ptr<Emitter>(new CustomFunctionEmitter(particle_type_name, fn.value()));
+}
+
 BLI_LAZY_INIT(StringMap<ForceFromNodeCallback>, get_force_builders)
 {
   StringMap<ForceFromNodeCallback> map;
@@ -281,7 +299,7 @@ BLI_LAZY_INIT(StringMap<ForceFromNodeCallback>, get_force_builders)
 BLI_LAZY_INIT(StringMap<EventFromNodeCallback>, get_event_builders)
 {
   StringMap<EventFromNodeCallback> map;
-  map.add_new("bp_MeshCollisionEventNode", Build_EVENT_mesh_collision);
+  map.add_new("bp_MeshCollisionEventNode", BUILD_EVENT_mesh_collision);
   map.add_new("bp_AgeReachedEventNode", BUILD_EVENT_age_reached);
   return map;
 }
@@ -291,6 +309,7 @@ BLI_LAZY_INIT(StringMap<EmitterFromNodeCallback>, get_emitter_builders)
   StringMap<EmitterFromNodeCallback> map;
   map.add_new("bp_PointEmitterNode", BUILD_EMITTER_moving_point);
   map.add_new("bp_MeshEmitterNode", BUILD_EMITTER_mesh_surface);
+  map.add_new("bp_CustomEmitterNode", BUILD_EMITTER_custom_function);
   return map;
 }



More information about the Bf-blender-cvs mailing list