[Bf-blender-cvs] [5743c0eab68] functions: new Custom Event node

Jacques Lucke noreply at git.blender.org
Wed Sep 11 11:24:54 CEST 2019


Commit: 5743c0eab681907f21ee1a1945bc803b5e6b39a6
Author: Jacques Lucke
Date:   Wed Sep 11 11:24:45 2019 +0200
Branches: functions
https://developer.blender.org/rB5743c0eab681907f21ee1a1945bc803b5e6b39a6

new Custom Event node

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

M	release/scripts/startup/nodes/bparticle_nodes/events.py
M	source/blender/simulations/bparticles/events.cpp
M	source/blender/simulations/bparticles/events.hpp
M	source/blender/simulations/bparticles/node_frontend.cpp
M	source/blender/simulations/bparticles/particle_function.hpp

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

diff --git a/release/scripts/startup/nodes/bparticle_nodes/events.py b/release/scripts/startup/nodes/bparticle_nodes/events.py
index a7a9e3e544a..288c815f128 100644
--- a/release/scripts/startup/nodes/bparticle_nodes/events.py
+++ b/release/scripts/startup/nodes/bparticle_nodes/events.py
@@ -29,3 +29,16 @@ class MeshCollisionEventNode(bpy.types.Node, BParticlesNode):
         builder.execute_input("execute_on_event", "Execute on Event", "execute_on_event__prop")
 
         builder.particle_effector_output("event", "Event")
+
+
+class CustomEventNode(bpy.types.Node, BParticlesNode):
+    bl_idname = "bp_CustomEventNode"
+    bl_label = "Custom Event"
+
+    execute_on_event__prop: NodeBuilder.ExecuteInputProperty()
+
+    def declaration(self, builder: NodeBuilder):
+        builder.fixed_input("condition", "Condition", "Boolean")
+        builder.execute_input("execute_on_event", "Execute on Event", "execute_on_event__prop")
+
+        builder.particle_effector_output("event", "Event")
diff --git a/source/blender/simulations/bparticles/events.cpp b/source/blender/simulations/bparticles/events.cpp
index b20c09c56ce..8a6e44592b0 100644
--- a/source/blender/simulations/bparticles/events.cpp
+++ b/source/blender/simulations/bparticles/events.cpp
@@ -69,6 +69,52 @@ void AgeReachedEvent::execute(EventExecuteInterface &interface)
   m_action->execute_from_event(interface);
 }
 
+/* Custom Event
+ ***********************************************/
+
+void CustomEvent::attributes(AttributesDeclaration &builder)
+{
+  builder.add<uint8_t>(m_identifier, 0);
+}
+
+void CustomEvent::filter(EventFilterInterface &interface)
+{
+  auto was_activated_before = interface.attributes().get<uint8_t>(m_identifier);
+
+  TemporaryVector<uint> pindices_to_check;
+  pindices_to_check.reserve(interface.pindices().size());
+
+  for (uint pindex : interface.pindices()) {
+    if (!was_activated_before[pindex]) {
+      pindices_to_check.append(pindex);
+    }
+  }
+
+  auto inputs = m_compute_inputs->compute(
+      pindices_to_check,
+      interface.attributes(),
+      ParticleTimes::FromDurationsAndEnd(interface.remaining_durations(),
+                                         interface.step_end_time()),
+      nullptr);
+
+  for (uint pindex : pindices_to_check) {
+    bool condition = inputs->get<bool>("Condition", 0, pindex);
+    if (condition) {
+      interface.trigger_particle(pindex, 0.0f);
+    }
+  }
+}
+
+void CustomEvent::execute(EventExecuteInterface &interface)
+{
+  auto was_activated_before = interface.attributes().get<uint8_t>(m_identifier);
+  for (uint pindex : interface.pindices()) {
+    was_activated_before[pindex] = true;
+  }
+
+  m_action->execute_from_event(interface);
+}
+
 /* Collision Event
  ***********************************************/
 
diff --git a/source/blender/simulations/bparticles/events.hpp b/source/blender/simulations/bparticles/events.hpp
index 72efea5fd16..d3de7da5516 100644
--- a/source/blender/simulations/bparticles/events.hpp
+++ b/source/blender/simulations/bparticles/events.hpp
@@ -37,6 +37,27 @@ class AgeReachedEvent : public Event {
   void execute(EventExecuteInterface &interface) override;
 };
 
+class CustomEvent : public Event {
+ private:
+  std::string m_identifier;
+  std::unique_ptr<ParticleFunction> m_compute_inputs;
+  std::unique_ptr<Action> m_action;
+
+ public:
+  CustomEvent(StringRef identifier,
+              std::unique_ptr<ParticleFunction> compute_inputs,
+              std::unique_ptr<Action> action)
+      : m_identifier(identifier),
+        m_compute_inputs(std::move(compute_inputs)),
+        m_action(std::move(action))
+  {
+  }
+
+  void attributes(AttributesDeclaration &builder) override;
+  void filter(EventFilterInterface &interface) override;
+  void execute(EventExecuteInterface &interface) override;
+};
+
 class MeshCollisionEvent : public Event {
  private:
   std::string m_identifier;
diff --git a/source/blender/simulations/bparticles/node_frontend.cpp b/source/blender/simulations/bparticles/node_frontend.cpp
index 8c2cd4338f0..d4eca69f84a 100644
--- a/source/blender/simulations/bparticles/node_frontend.cpp
+++ b/source/blender/simulations/bparticles/node_frontend.cpp
@@ -604,6 +604,25 @@ static void PARSE_mesh_force(BehaviorCollector &collector,
   }
 }
 
+static void PARSE_custom_event(BehaviorCollector &collector,
+                               VTreeDataGraph &vtree_data_graph,
+                               WorldTransition &UNUSED(world_transition),
+                               VirtualNode *vnode)
+{
+  Vector<std::string> type_names = find_connected_particle_type_names(vnode->output(0, "Event"));
+  for (std::string &type_name : type_names) {
+    auto fn_or_error = create_particle_function(vnode, vtree_data_graph);
+    if (fn_or_error.is_error()) {
+      continue;
+    }
+    std::unique_ptr<ParticleFunction> compute_inputs = fn_or_error.extract_value();
+    auto action = build_action_list(vtree_data_graph, vnode, "Execute on Event");
+
+    Event *event = new CustomEvent(vnode->name(), std::move(compute_inputs), std::move(action));
+    collector.m_events.add(type_name, event);
+  }
+}
+
 BLI_LAZY_INIT_STATIC(StringMap<ParseNodeCallback>, get_node_parsers)
 {
   StringMap<ParseNodeCallback> map;
@@ -618,6 +637,7 @@ BLI_LAZY_INIT_STATIC(StringMap<ParseNodeCallback>, get_node_parsers)
   map.add_new("bp_SizeOverTimeNode", PARSE_size_over_time);
   map.add_new("bp_DragForceNode", PARSE_drag_force);
   map.add_new("bp_MeshForceNode", PARSE_mesh_force);
+  map.add_new("bp_CustomEventNode", PARSE_custom_event);
   return map;
 }
 
diff --git a/source/blender/simulations/bparticles/particle_function.hpp b/source/blender/simulations/bparticles/particle_function.hpp
index 0fb457b5d61..e5b18361cc1 100644
--- a/source/blender/simulations/bparticles/particle_function.hpp
+++ b/source/blender/simulations/bparticles/particle_function.hpp
@@ -259,12 +259,12 @@ class ParticleFunction {
   std::unique_ptr<ParticleFunctionResult> compute(ForceInterface &interface);
   std::unique_ptr<ParticleFunctionResult> compute(EventFilterInterface &interface);
 
- private:
   std::unique_ptr<ParticleFunctionResult> compute(ArrayRef<uint> pindices,
                                                   AttributesRef attributes,
                                                   ParticleTimes particle_times,
                                                   ActionContext *action_context);
 
+ private:
   void init_without_deps(ParticleFunctionResult *result);
 
   void init_with_deps(ParticleFunctionResult *result,



More information about the Bf-blender-cvs mailing list