[Bf-blender-cvs] [ced689a43ac] functions: new Random Floats node

Jacques Lucke noreply at git.blender.org
Tue Dec 17 14:51:57 CET 2019


Commit: ced689a43ac31d9030aa576668d7b8204f659538
Author: Jacques Lucke
Date:   Tue Dec 17 14:48:05 2019 +0100
Branches: functions
https://developer.blender.org/rBced689a43ac31d9030aa576668d7b8204f659538

new Random Floats node

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

M	release/scripts/startup/nodes/base.py
M	release/scripts/startup/nodes/function_nodes/noise.py
M	source/blender/functions/intern/inlined_tree_multi_function_network/mappings_nodes.cc
M	source/blender/functions/intern/multi_functions/mixed.cc
M	source/blender/functions/intern/multi_functions/mixed.h

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

diff --git a/release/scripts/startup/nodes/base.py b/release/scripts/startup/nodes/base.py
index d7d1fb053b0..00d72db5654 100644
--- a/release/scripts/startup/nodes/base.py
+++ b/release/scripts/startup/nodes/base.py
@@ -217,6 +217,10 @@ class BaseNode:
 
     def copy(self, src_node):
         self.identifier = get_new_node_identifier()
+        self.duplicate(src_node)
+
+    def duplicate(self, src_node):
+        pass
 
 
 class BaseSocket:
diff --git a/release/scripts/startup/nodes/function_nodes/noise.py b/release/scripts/startup/nodes/function_nodes/noise.py
index 10b70e0ae3c..46f3e4b827c 100644
--- a/release/scripts/startup/nodes/function_nodes/noise.py
+++ b/release/scripts/startup/nodes/function_nodes/noise.py
@@ -1,4 +1,5 @@
 import bpy
+import random
 from bpy.props import *
 from .. base import FunctionNode
 from .. node_builder import NodeBuilder
@@ -20,7 +21,29 @@ class RandomFloatNode(bpy.types.Node, FunctionNode):
     bl_label = "Random Float"
 
     def declaration(self, builder: NodeBuilder):
-        builder.fixed_input("seed", "Seed", "Integer")
         builder.fixed_input("min", "Min", "Float", default=0)
         builder.fixed_input("max", "Max", "Float", default=1)
+        builder.fixed_input("seed", "Seed", "Integer")
         builder.fixed_output("value", "Value", "Float")
+
+
+class RandomFloatsNode(bpy.types.Node, FunctionNode):
+    bl_idname = "fn_RandomFloatsNode"
+    bl_label = "Random Floats"
+
+    node_seed: IntProperty(
+        name="Node Seed",
+    )
+
+    def declaration(self, builder: NodeBuilder):
+        builder.fixed_input("amount", "Amount", "Integer", default=10)
+        builder.fixed_input("min", "Min", "Float")
+        builder.fixed_input("max", "Max", "Float", default=1)
+        builder.fixed_input("seed", "Seed", "Integer")
+        builder.fixed_output("values", "Values", "Float List")
+
+    def draw_advanced(self, layout):
+        layout.prop(self, "node_seed")
+
+    def duplicate(self, src_node):
+        self.node_seed = random.randint(0, 10000)
diff --git a/source/blender/functions/intern/inlined_tree_multi_function_network/mappings_nodes.cc b/source/blender/functions/intern/inlined_tree_multi_function_network/mappings_nodes.cc
index 7118508f3cf..84c70cc9da7 100644
--- a/source/blender/functions/intern/inlined_tree_multi_function_network/mappings_nodes.cc
+++ b/source/blender/functions/intern/inlined_tree_multi_function_network/mappings_nodes.cc
@@ -470,6 +470,12 @@ static void INSERT_random_float(VNodeMFNetworkBuilder &builder)
   builder.set_constructed_matching_fn<MF_RandomFloat>();
 }
 
+static void INSERT_random_floats(VNodeMFNetworkBuilder &builder)
+{
+  uint node_seed = (uint)RNA_int_get(builder.rna(), "node_seed");
+  builder.set_constructed_matching_fn<MF_RandomFloats>(node_seed);
+}
+
 static void INSERT_value(VNodeMFNetworkBuilder &builder)
 {
   const XOutputSocket &xsocket = builder.xnode().output(0);
@@ -534,6 +540,7 @@ void add_inlined_tree_node_mapping_info(VTreeMultiFunctionMappings &mappings)
   mappings.xnode_inserters.add_new("fn_MapRangeNode", INSERT_map_range);
   mappings.xnode_inserters.add_new("fn_FloatClampNode", INSERT_clamp_float);
   mappings.xnode_inserters.add_new("fn_RandomFloatNode", INSERT_random_float);
+  mappings.xnode_inserters.add_new("fn_RandomFloatsNode", INSERT_random_floats);
   mappings.xnode_inserters.add_new("fn_ValueNode", INSERT_value);
   mappings.xnode_inserters.add_new("fn_EmitterTimeInfoNode", INSERT_emitter_time_info);
   mappings.xnode_inserters.add_new("fn_SampleObjectSurfaceNode", INSERT_sample_object_surface);
diff --git a/source/blender/functions/intern/multi_functions/mixed.cc b/source/blender/functions/intern/multi_functions/mixed.cc
index 27e0aa84ce4..64d59a2cd82 100644
--- a/source/blender/functions/intern/multi_functions/mixed.cc
+++ b/source/blender/functions/intern/multi_functions/mixed.cc
@@ -545,17 +545,17 @@ void MF_Clamp::call(MFMask mask, MFParams params, MFContext UNUSED(context)) con
 MF_RandomFloat::MF_RandomFloat()
 {
   MFSignatureBuilder signature = this->get_builder("Random Float");
-  signature.single_input<int>("Seed");
   signature.single_input<float>("Min");
   signature.single_input<float>("Max");
+  signature.single_input<int>("Seed");
   signature.single_output<float>("Value");
 }
 
 void MF_RandomFloat::call(MFMask mask, MFParams params, MFContext UNUSED(context)) const
 {
-  VirtualListRef<int> seeds = params.readonly_single_input<int>(0, "Seed");
-  VirtualListRef<float> min_values = params.readonly_single_input<float>(1, "Min");
-  VirtualListRef<float> max_values = params.readonly_single_input<float>(2, "Max");
+  VirtualListRef<float> min_values = params.readonly_single_input<float>(0, "Min");
+  VirtualListRef<float> max_values = params.readonly_single_input<float>(1, "Max");
+  VirtualListRef<int> seeds = params.readonly_single_input<int>(2, "Seed");
   MutableArrayRef<float> r_values = params.uninitialized_single_output<float>(3, "Value");
 
   for (uint i : mask.indices()) {
@@ -564,6 +564,41 @@ void MF_RandomFloat::call(MFMask mask, MFParams params, MFContext UNUSED(context
   }
 }
 
+MF_RandomFloats::MF_RandomFloats(uint seed) : m_seed(seed * 2354567)
+{
+  MFSignatureBuilder signature = this->get_builder("Random Floats");
+  signature.single_input<int>("Amount");
+  signature.single_input<float>("Min");
+  signature.single_input<float>("Max");
+  signature.single_input<int>("Seed");
+  signature.vector_output<float>("Values");
+}
+
+void MF_RandomFloats::call(MFMask mask, MFParams params, MFContext UNUSED(context)) const
+{
+  VirtualListRef<int> amounts = params.readonly_single_input<int>(0, "Amount");
+  VirtualListRef<float> min_values = params.readonly_single_input<float>(1, "Min");
+  VirtualListRef<float> max_values = params.readonly_single_input<float>(2, "Max");
+  VirtualListRef<int> seeds = params.readonly_single_input<int>(3, "Seed");
+  GenericVectorArray::MutableTypedRef<float> r_values = params.vector_output<float>(4, "Values");
+
+  RNG *rng = BLI_rng_new(0);
+
+  for (uint i : mask.indices()) {
+    MutableArrayRef<float> r_array = r_values.allocate(i, amounts[i]);
+    BLI_rng_srandom(rng, seeds[i] + m_seed);
+
+    float range = max_values[i] - min_values[i];
+    float offset = min_values[i];
+
+    for (float &r_value : r_array) {
+      r_value = BLI_rng_get_float(rng) * range + offset;
+    }
+  }
+
+  BLI_rng_free(rng);
+}
+
 MF_FindNonClosePoints::MF_FindNonClosePoints()
 {
   MFSignatureBuilder signature = this->get_builder("Remove Close Points");
diff --git a/source/blender/functions/intern/multi_functions/mixed.h b/source/blender/functions/intern/multi_functions/mixed.h
index 458103a80a6..927a8957a0d 100644
--- a/source/blender/functions/intern/multi_functions/mixed.h
+++ b/source/blender/functions/intern/multi_functions/mixed.h
@@ -66,6 +66,15 @@ class MF_RandomFloat final : public MultiFunction {
   void call(MFMask mask, MFParams parms, MFContext context) const override;
 };
 
+class MF_RandomFloats final : public MultiFunction {
+ private:
+  uint m_seed;
+
+ public:
+  MF_RandomFloats(uint seed);
+  void call(MFMask mask, MFParams parms, MFContext context) const override;
+};
+
 class MF_ContextVertexPosition final : public MultiFunction {
  public:
   MF_ContextVertexPosition();



More information about the Bf-blender-cvs mailing list