Random Select Seed Option

Add 'Seed' option for all random select operators

D1508 by @mba105, w/ edits
This commit is contained in:
Campbell Barton 2015-10-10 23:47:41 +11:00
parent 240f356166
commit 05acf3d43a
Notes: blender-bot 2023-02-14 11:08:33 +01:00
Referenced by issue #44597, 'Seed' option for random select operators (completed)
8 changed files with 73 additions and 36 deletions

View 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 *******************/

View 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)

View 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 *****************************/

View 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);
}

View 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);
}

View 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");
}

View 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);

View 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);