Merge branch 'master' into blender2.8
[blender.git] / source / blender / editors / metaball / mball_edit.c
index fa48462..65bf258 100644 (file)
 #include "RNA_define.h"
 #include "RNA_access.h"
 
-#include "BKE_depsgraph.h"
 #include "BKE_context.h"
 #include "BKE_mball.h"
+#include "BKE_layer.h"
+
+#include "DEG_depsgraph.h"
 
 #include "ED_mball.h"
 #include "ED_screen.h"
@@ -366,32 +368,47 @@ void MBALL_OT_select_similar(wmOperatorType *ot)
 /* Random metaball selection */
 static int select_random_metaelems_exec(bContext *C, wmOperator *op)
 {
-       Object *obedit = CTX_data_edit_object(C);
-       MetaBall *mb = (MetaBall *)obedit->data;
-       MetaElem *ml;
        const bool select = (RNA_enum_get(op->ptr, "action") == SEL_SELECT);
        const float randfac = RNA_float_get(op->ptr, "percent") / 100.0f;
        const int seed = WM_operator_properties_select_random_seed_increment_get(op);
 
-       RNG *rng = BLI_rng_new_srandom(seed);
+       ViewLayer *view_layer = CTX_data_view_layer(C);
+       uint objects_len = 0;
+       Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
+       for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
+               Object *obedit = objects[ob_index];
+               MetaBall *mb = (MetaBall *)obedit->data;
+               if (!BKE_mball_is_any_unselected(mb)) {
+                       continue;
+               }
+               int seed_iter = seed;
 
-       for (ml = mb->editelems->first; ml; ml = ml->next) {
-               if (BLI_rng_get_float(rng) < randfac) {
-                       if (select)
-                               ml->flag |= SELECT;
-                       else
-                               ml->flag &= ~SELECT;
+               /* This gives a consistent result regardless of object order. */
+               if (ob_index) {
+                       seed_iter += BLI_ghashutil_strhash_p(obedit->id.name);
                }
-       }
 
-       BLI_rng_free(rng);
+               RNG *rng = BLI_rng_new_srandom(seed_iter);
 
-       WM_event_add_notifier(C, NC_GEOM | ND_SELECT, mb);
+               for (MetaElem *ml = mb->editelems->first; ml; ml = ml->next) {
+                       if (BLI_rng_get_float(rng) < randfac) {
+                               if (select) {
+                                       ml->flag |= SELECT;
+                               }
+                               else {
+                                       ml->flag &= ~SELECT;
+                               }
+                       }
+               }
 
+               BLI_rng_free(rng);
+
+               WM_event_add_notifier(C, NC_GEOM | ND_SELECT, mb);
+       }
+       MEM_freeN(objects);
        return OPERATOR_FINISHED;
 }
 
-
 void MBALL_OT_select_random_metaelems(struct wmOperatorType *ot)
 {
        /* identifiers */
@@ -431,7 +448,7 @@ static int duplicate_metaelems_exec(bContext *C, wmOperator *UNUSED(op))
                        ml = ml->prev;
                }
                WM_event_add_notifier(C, NC_GEOM | ND_DATA, mb);
-               DAG_id_tag_update(obedit->data, 0);
+               DEG_id_tag_update(obedit->data, 0);
        }
 
        return OPERATOR_FINISHED;
@@ -473,7 +490,7 @@ static int delete_metaelems_exec(bContext *C, wmOperator *UNUSED(op))
                        ml = next;
                }
                WM_event_add_notifier(C, NC_GEOM | ND_DATA, mb);
-               DAG_id_tag_update(obedit->data, 0);
+               DEG_id_tag_update(obedit->data, 0);
        }
 
        return OPERATOR_FINISHED;
@@ -513,7 +530,7 @@ static int hide_metaelems_exec(bContext *C, wmOperator *op)
                        ml = ml->next;
                }
                WM_event_add_notifier(C, NC_GEOM | ND_DATA, mb);
-               DAG_id_tag_update(obedit->data, 0);
+               DEG_id_tag_update(obedit->data, 0);
        }
 
        return OPERATOR_FINISHED;
@@ -556,7 +573,7 @@ static int reveal_metaelems_exec(bContext *C, wmOperator *op)
        }
        if (changed) {
                WM_event_add_notifier(C, NC_GEOM | ND_DATA, mb);
-               DAG_id_tag_update(obedit->data, 0);
+               DEG_id_tag_update(obedit->data, 0);
        }
 
        return OPERATOR_FINISHED;