Random Select Seed Option
authorCampbell Barton <ideasman42@gmail.com>
Sat, 10 Oct 2015 12:47:41 +0000 (23:47 +1100)
committerCampbell Barton <ideasman42@gmail.com>
Sat, 10 Oct 2015 12:52:11 +0000 (23:52 +1100)
Add 'Seed' option for all random select operators

D1508 by @mba105, w/ edits

source/blender/editors/curve/editcurve_select.c
source/blender/editors/mesh/editmesh_select.c
source/blender/editors/metaball/mball_edit.c
source/blender/editors/object/object_lattice.c
source/blender/editors/object/object_select.c
source/blender/editors/physics/particle_edit.c
source/blender/windowmanager/WM_api.h
source/blender/windowmanager/intern/wm_operators.c

index 0f3942b0c9020646b2beab1fea12dbe27e93dd42..34ac3b8bb97ea6bacb97cfc3add23066b929946f 100644 (file)
@@ -999,20 +999,22 @@ void CURVE_OT_select_less(wmOperatorType *ot)
 
 /********************** select random *********************/
 
-static void curve_select_random(ListBase *editnurb, float randfac, bool select)
+static void curve_select_random(ListBase *editnurb, float randfac, int seed, bool select)
 {
        Nurb *nu;
        BezTriple *bezt;
        BPoint *bp;
        int a;
 
+       RNG *rng = BLI_rng_new_srandom(seed);
+
        for (nu = editnurb->first; nu; nu = nu->next) {
                if (nu->type == CU_BEZIER) {
                        bezt = nu->bezt;
                        a = nu->pntsu;
                        while (a--) {
                                if (!bezt->hide) {
-                                       if (BLI_frand() < randfac) {
+                                       if (BLI_rng_get_float(rng) < randfac) {
                                                select_beztriple(bezt, select, SELECT, VISIBLE);
                                        }
                                }
@@ -1025,7 +1027,7 @@ static void curve_select_random(ListBase *editnurb, float randfac, bool select)
 
                        while (a--) {
                                if (!bp->hide) {
-                                       if (BLI_frand() < randfac) {
+                                       if (BLI_rng_get_float(rng) < randfac) {
                                                select_bpoint(bp, select, SELECT, VISIBLE);
                                        }
                                }
@@ -1033,6 +1035,8 @@ static void curve_select_random(ListBase *editnurb, float randfac, bool select)
                        }
                }
        }
+
+       BLI_rng_free(rng);
 }
 
 static int curve_select_random_exec(bContext *C, wmOperator *op)
@@ -1041,8 +1045,9 @@ static int curve_select_random_exec(bContext *C, wmOperator *op)
        ListBase *editnurb = object_editcurve_get(obedit);
        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 = RNA_int_get(op->ptr, "seed");
 
-       curve_select_random(editnurb, randfac, select);
+       curve_select_random(editnurb, randfac, seed, select);
        BKE_curve_nurb_vert_active_validate(obedit->data);
 
        WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
@@ -1065,9 +1070,7 @@ void CURVE_OT_select_random(wmOperatorType *ot)
        ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
 
        /* properties */
-       RNA_def_float_percentage(ot->srna, "percent", 50.f, 0.0f, 100.0f,
-                                "Percent", "Percentage of elements to select randomly", 0.0f, 100.0f);
-       WM_operator_properties_select_action_simple(ot, SEL_SELECT);
+       WM_operator_properties_select_random(ot);
 }
 
 /********************* every nth number of point *******************/
index 7f22ff7c2411907d9d405984427d3bb6504c4e32..1169c91bf662a6d5fd100541e42d2cbf04c16283 100644 (file)
@@ -3551,13 +3551,16 @@ static int edbm_select_random_exec(bContext *C, wmOperator *op)
        BMEditMesh *em = BKE_editmesh_from_object(obedit);
        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 = RNA_int_get(op->ptr, "seed");
 
        BMIter iter;
 
+       RNG *rng = BLI_rng_new_srandom(seed);
+
        if (em->selectmode & SCE_SELECT_VERTEX) {
                BMVert *eve;
                BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
-                       if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN) && BLI_frand() < randfac) {
+                       if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN) && BLI_rng_get_float(rng) < randfac) {
                                BM_vert_select_set(em->bm, eve, select);
                        }
                }
@@ -3565,7 +3568,7 @@ static int edbm_select_random_exec(bContext *C, wmOperator *op)
        else if (em->selectmode & SCE_SELECT_EDGE) {
                BMEdge *eed;
                BM_ITER_MESH (eed, &iter, em->bm, BM_EDGES_OF_MESH) {
-                       if (!BM_elem_flag_test(eed, BM_ELEM_HIDDEN) && BLI_frand() < randfac) {
+                       if (!BM_elem_flag_test(eed, BM_ELEM_HIDDEN) && BLI_rng_get_float(rng) < randfac) {
                                BM_edge_select_set(em->bm, eed, select);
                        }
                }
@@ -3573,12 +3576,14 @@ static int edbm_select_random_exec(bContext *C, wmOperator *op)
        else {
                BMFace *efa;
                BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
-                       if (!BM_elem_flag_test(efa, BM_ELEM_HIDDEN) && BLI_frand() < randfac) {
+                       if (!BM_elem_flag_test(efa, BM_ELEM_HIDDEN) && BLI_rng_get_float(rng) < randfac) {
                                BM_face_select_set(em->bm, efa, select);
                        }
                }
        }
 
+       BLI_rng_free(rng);
+
        if (select) {
                /* was EDBM_select_flush, but it over select in edge/face mode */
                EDBM_selectmode_flush(em);
@@ -3607,9 +3612,7 @@ void MESH_OT_select_random(wmOperatorType *ot)
        ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
        
        /* props */
-       RNA_def_float_percentage(ot->srna, "percent", 50.0f, 0.0f, 100.0f,
-                                "Percent", "Percentage of elements to select randomly", 0.0f, 100.0f);
-       WM_operator_properties_select_action_simple(ot, SEL_SELECT);
+       WM_operator_properties_select_random(ot);
 }
 
 static int edbm_select_ungrouped_poll(bContext *C)
index 42eda23527621584b8bb978b891a5bbaf88e1c53..de725d391f7507aced49641ae1776ca3d71717af 100644 (file)
@@ -372,17 +372,22 @@ static int select_random_metaelems_exec(bContext *C, wmOperator *op)
        MetaBall *mb = (MetaBall *)obedit->data;
        MetaElem *ml;
        const bool select = (RNA_enum_get(op->ptr, "action") == SEL_SELECT);
-       float percent = RNA_float_get(op->ptr, "percent") / 100.0f;
+       const float randfac = RNA_float_get(op->ptr, "percent") / 100.0f;
+       const int seed = RNA_int_get(op->ptr, "seed");
        
+       RNG *rng = BLI_rng_new_srandom(seed);
+
        for (ml = mb->editelems->first; ml; ml = ml->next) {
-               if (BLI_frand() < percent) {
+               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);
        
        return OPERATOR_FINISHED;
@@ -404,9 +409,7 @@ void MBALL_OT_select_random_metaelems(struct wmOperatorType *ot)
        ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
        
        /* properties */
-       RNA_def_float_percentage(ot->srna, "percent", 50.f, 0.0f, 100.0f,
-                                "Percent", "Percentage of elements to select randomly", 0.0f, 100.0f);
-       WM_operator_properties_select_action_simple(ot, SEL_SELECT);
+       WM_operator_properties_select_random(ot);
 }
 
 /***************************** Duplicate operator *****************************/
index 76d9facf701b5e6b657994b113ac807889253965..410b15581df974fb3f2bf21d86bbf2f1eccfcb36 100644 (file)
@@ -192,9 +192,13 @@ static int lattice_select_random_exec(bContext *C, wmOperator *op)
 {
        Object *obedit = CTX_data_edit_object(C);
        Lattice *lt = ((Lattice *)obedit->data)->editlatt->latt;
+
        const float randfac = RNA_float_get(op->ptr, "percent") / 100.0f;
+       const int seed = RNA_int_get(op->ptr, "seed");
        const bool select = (RNA_enum_get(op->ptr, "action") == SEL_SELECT);
 
+       RNG *rng = BLI_rng_new_srandom(seed);
+
        int tot;
        BPoint *bp;
 
@@ -202,7 +206,7 @@ static int lattice_select_random_exec(bContext *C, wmOperator *op)
        bp = lt->def;
        while (tot--) {
                if (!bp->hide) {
-                       if (BLI_frand() < randfac) {
+                       if (BLI_rng_get_float(rng) < randfac) {
                                bpoint_select_set(bp, select);
                        }
                }
@@ -213,6 +217,8 @@ static int lattice_select_random_exec(bContext *C, wmOperator *op)
                lt->actbp = LT_ACTBP_NONE;
        }
 
+       BLI_rng_free(rng);
+
        WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
 
        return OPERATOR_FINISHED;
@@ -233,9 +239,7 @@ void LATTICE_OT_select_random(wmOperatorType *ot)
        ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
 
        /* props */
-       RNA_def_float_percentage(ot->srna, "percent", 50.f, 0.0f, 100.0f,
-                                "Percent", "Percentage of elements to select randomly", 0.f, 100.0f);
-       WM_operator_properties_select_action_simple(ot, SEL_SELECT);
+       WM_operator_properties_select_random(ot);
 }
 
 
index f69f76cbcc33a01174944054c537ba3f17a6bf9d..2f2eece8018a29661bce2c47019ed6f6f3644a29 100644 (file)
@@ -1179,19 +1179,22 @@ void OBJECT_OT_select_mirror(wmOperatorType *ot)
 
 static int object_select_random_exec(bContext *C, wmOperator *op)
 {      
-       float percent;
+       const float randfac = RNA_float_get(op->ptr, "percent") / 100.0f;
+       const int seed = RNA_int_get(op->ptr, "seed");
        const bool select = (RNA_enum_get(op->ptr, "action") == SEL_SELECT);
 
-       percent = RNA_float_get(op->ptr, "percent") / 100.0f;
-               
+       RNG *rng = BLI_rng_new_srandom(seed);
+
        CTX_DATA_BEGIN (C, Base *, base, selectable_bases)
        {
-               if (BLI_frand() < percent) {
+               if (BLI_rng_get_float(rng) < randfac) {
                        ED_base_object_select(base, select);
                }
        }
        CTX_DATA_END;
-       
+
+       BLI_rng_free(rng);
+
        WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, CTX_data_scene(C));
        
        return OPERATOR_FINISHED;
@@ -1213,6 +1216,5 @@ void OBJECT_OT_select_random(wmOperatorType *ot)
        ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
        
        /* properties */
-       RNA_def_float_percentage(ot->srna, "percent", 50.f, 0.0f, 100.0f, "Percent", "Percentage of objects to select randomly", 0.f, 100.0f);
-       WM_operator_properties_select_action_simple(ot, SEL_SELECT);
+       WM_operator_properties_select_random(ot);
 }
index 892545a8efdccd1c3c6f5854bb0da12a861bf259..32cd1f7a1ce55a22df4dac0abfdd1ee6f11896c4 100644 (file)
@@ -1634,7 +1634,10 @@ static int select_random_exec(bContext *C, wmOperator *op)
        int p;
        int k;
 
-       const float randf = RNA_float_get (op->ptr, "percent") / 100.0f;
+       const float randfac = RNA_float_get (op->ptr, "percent") / 100.0f;
+       const int seed = RNA_int_get(op->ptr, "seed");
+       const bool select = (RNA_enum_get(op->ptr, "action") == SEL_SELECT);
+       RNG *rng;
 
        type = RNA_enum_get(op->ptr, "type");
 
@@ -1644,10 +1647,12 @@ static int select_random_exec(bContext *C, wmOperator *op)
        ob = CTX_data_active_object(C);
        edit = PE_get_current(scene, ob);
 
+       rng = BLI_rng_new_srandom(seed);
+
        switch (type) {
                case RAN_HAIR:
                        LOOP_VISIBLE_POINTS {
-                               int flag = (BLI_frand() < randf) ? SEL_SELECT : SEL_DESELECT;
+                               int flag = ((BLI_rng_get_float(rng) < randfac) == select) ? SEL_SELECT : SEL_DESELECT;
                                LOOP_KEYS {
                                        select_action_apply (point, key, flag);
                                }
@@ -1656,13 +1661,15 @@ static int select_random_exec(bContext *C, wmOperator *op)
                case RAN_POINTS:
                        LOOP_VISIBLE_POINTS {
                                LOOP_VISIBLE_KEYS {
-                                       int flag = (BLI_frand() < randf) ? SEL_SELECT : SEL_DESELECT;
+                                       int flag = ((BLI_rng_get_float(rng) < randfac) == select) ? SEL_SELECT : SEL_DESELECT;
                                        select_action_apply (point, key, flag);
                                }
                        }
                        break;
        }
 
+       BLI_rng_free(rng);
+
        PE_update_selection(data.scene, data.ob, 1);
        WM_event_add_notifier(C, NC_OBJECT|ND_PARTICLE|NA_SELECTED, data.ob);
 
@@ -1684,9 +1691,7 @@ void PARTICLE_OT_select_random(wmOperatorType *ot)
        ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
 
        /* properties */
-       RNA_def_float_percentage (ot->srna, "percent", 50.0f, 0.0f, 100.0f, "Percent",
-                                 "Percentage (mean) of elements in randomly selected set",
-                                 0.0f, 100.0f);
+       WM_operator_properties_select_random(ot);
        ot->prop = RNA_def_enum (ot->srna, "type", select_random_type_items, RAN_HAIR,
                                 "Type", "Select either hair or points");
 }
index 8aea5816b7f43b42d53a459368e4682e515c3f87..11407678e53ab0b886cf56055a55742507af6806 100644 (file)
@@ -290,6 +290,7 @@ void                WM_operator_properties_gesture_straightline(struct wmOperatorType *ot, int
 void           WM_operator_properties_select_all(struct wmOperatorType *ot);
 void           WM_operator_properties_select_action(struct wmOperatorType *ot, int default_action);
 void           WM_operator_properties_select_action_simple(struct wmOperatorType *ot, int default_action);
+void        WM_operator_properties_select_random(struct wmOperatorType *ot);
 
 bool        WM_operator_check_ui_enabled(const struct bContext *C, const char *idname);
 wmOperator *WM_operator_last_redo(const struct bContext *C);
index c9dd07208c178211fcf5474333443e645ebdf557..a288551aa083486583074eb25198eceadeebf0ad 100644 (file)
@@ -1320,6 +1320,22 @@ void WM_operator_properties_select_action_simple(wmOperatorType *ot, int default
        wm_operator_properties_select_action_ex(ot, default_action, select_actions);
 }
 
+/**
+ * Use for all select random operators.
+ * Adds properties: percent, seed, action.
+ */
+void WM_operator_properties_select_random(wmOperatorType *ot)
+{
+       RNA_def_float_percentage(
+               ot->srna, "percent", 50.f, 0.0f, 100.0f,
+               "Percent", "Percentage of objects to select randomly", 0.f, 100.0f);
+       RNA_def_int(
+               ot->srna, "seed", 0, 0, INT_MAX,
+               "Random Seed", "Seed for the random number generator", 0, 255);
+
+       WM_operator_properties_select_action_simple(ot, SEL_SELECT);
+}
+
 void WM_operator_properties_select_all(wmOperatorType *ot)
 {
        WM_operator_properties_select_action(ot, SEL_TOGGLE);