Blender V5.3
editmesh_select.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2004 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10#include <optional>
11
12#include "MEM_guardedalloc.h"
13
14#include "BLI_array.hh"
15#include "BLI_function_ref.hh"
16#include "BLI_heap.hh"
17#include "BLI_listbase.hh"
18#include "BLI_math_bits.hh"
19#include "BLI_math_geom_c.hh"
20#include "BLI_math_matrix_c.hh"
22#include "BLI_math_vector_c.hh"
23#include "BLI_rand_c.hh"
25#include "BLI_vector.hh"
26
27#include "BKE_attribute.h"
28#include "BKE_attribute.hh"
29#include "BKE_context.hh"
30#include "BKE_customdata.hh"
31#include "BKE_deform.hh"
32#include "BKE_editmesh.hh"
33#include "BKE_layer.hh"
34#include "BKE_mesh.hh"
35#include "BKE_mesh_wrapper.hh"
36#include "BKE_object.hh"
37#include "BKE_report.hh"
38
39#include "WM_api.hh"
40#include "WM_types.hh"
41
42#include "RNA_access.hh"
43#include "RNA_define.hh"
44#include "RNA_enum_types.hh"
45
46#include "ED_mesh.hh"
47#include "ED_object.hh"
48#include "ED_screen.hh"
49#include "ED_select_utils.hh"
50#include "ED_transform.hh"
51#include "ED_uvedit.hh"
52#include "ED_view3d.hh"
53
54#include "BLT_translation.hh"
55
56#include "DNA_mesh_types.h"
57#include "DNA_meshdata_types.h"
58#include "DNA_object_types.h"
59
60#include "bmesh_tools.hh"
61
62#include "DEG_depsgraph.hh"
64
65#include "DRW_select_buffer.hh"
66
67#include "mesh_intern.hh" /* Own include. */
68
69namespace blender {
70
72#define BMO_ELE_TAG 1
73
74/* -------------------------------------------------------------------- */
77
79{
80 Object *obedit = CTX_data_edit_object(C);
81 if (obedit && obedit->type == OB_MESH) {
82 const BMEditMesh *em = BKE_editmesh_from_object(obedit);
83 if (em) {
85 return true;
86 }
87 }
88 }
89
90 CTX_wm_operator_poll_msg_set(C, "An edit-mesh with vertex or edge selection mode is required");
91
92 return false;
93}
94
96
97/* -------------------------------------------------------------------- */
100
107
109 {ELEM_COUNT_LESS, "LESS", false, "Less Than", ""},
110 {ELEM_COUNT_EQUAL, "EQUAL", false, "Equal To", ""},
111 {ELEM_COUNT_GREATER, "GREATER", false, "Greater Than", ""},
112 {ELEM_COUNT_NOT_EQUAL, "NOTEQUAL", false, "Not Equal To", ""},
113 {0, nullptr, 0, nullptr, nullptr},
114};
115
116static inline bool is_count_a_match(const eElemCountType type,
117 const int value_test,
118 const int value_reference)
119{
120 switch (type) {
121 case ELEM_COUNT_LESS:
122 return (value_test < value_reference);
123 case ELEM_COUNT_EQUAL:
124 return (value_test == value_reference);
126 return (value_test > value_reference);
128 return (value_test != value_reference);
129 default:
130 BLI_assert_unreachable(); /* Bad value of selection `type`. */
131 return false;
132 }
133}
134
136
137/* -------------------------------------------------------------------- */
140
142 BMEditMesh *em, Mesh *mesh, const int axis, const bool extend, int *r_totmirr, int *r_totfail)
143{
145 BMIter iter;
146 int totmirr = 0;
147 int totfail = 0;
148 bool use_topology = mesh->editflag & ME_EDIT_MIRROR_TOPO;
149
150 *r_totmirr = *r_totfail = 0;
151
152 /* Flush (select -> tag). */
154 BMVert *v;
155 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
157 }
158 }
159 else if (em->selectmode & SCE_SELECT_EDGE) {
160 BMEdge *e;
161 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
163 }
164 }
165 else {
166 BMFace *f;
167 BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
169 }
170 }
171
172 EDBM_verts_mirror_cache_begin(em, bm, axis, true, true, false, use_topology);
173
174 if (!extend) {
176 }
177
179 BMVert *v;
180 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
182 continue;
183 }
184
186 BMVert *v_mirr = EDBM_verts_mirror_get(em, bm, v);
187 if (v_mirr && !BM_elem_flag_test(v_mirr, BM_ELEM_HIDDEN)) {
188 BM_vert_select_set(bm, v_mirr, true);
189 totmirr++;
190 }
191 else {
192 totfail++;
193 }
194 }
195 }
196 }
197 else if (em->selectmode & SCE_SELECT_EDGE) {
198 BMEdge *e;
199 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
201 continue;
202 }
203
205 BMEdge *e_mirr = EDBM_verts_mirror_get_edge(em, bm, e);
206 if (e_mirr && !BM_elem_flag_test(e_mirr, BM_ELEM_HIDDEN)) {
207 BM_edge_select_set(bm, e_mirr, true);
208 totmirr++;
209 }
210 else {
211 totfail++;
212 }
213 }
214 }
215 }
216 else {
217 BMFace *f;
218 BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
220 continue;
221 }
222
224 BMFace *f_mirr = EDBM_verts_mirror_get_face(em, bm, f);
225 if (f_mirr && !BM_elem_flag_test(f_mirr, BM_ELEM_HIDDEN)) {
226 BM_face_select_set(bm, f_mirr, true);
227 totmirr++;
228 }
229 else {
230 totfail++;
231 }
232 }
233 }
234 }
235
237
238 *r_totmirr = totmirr;
239 *r_totfail = totfail;
240}
241
242static bool UNUSED_FUNCTION(EDBM_select_mirrored_extend_all)(Object *obedit, BMEditMesh *em)
243{
245 int selectmode = em->selectmode;
246 bool changed = false;
247
248 if (bm->totfacesel == 0) {
249 selectmode &= ~SCE_SELECT_FACE;
250 }
251 if (bm->totedgesel == 0) {
252 selectmode &= ~SCE_SELECT_EDGE;
253 }
254 if (bm->totvertsel == 0) {
255 selectmode &= ~SCE_SELECT_VERTEX;
256 }
257
258 if (selectmode == 0) {
259 return changed;
260 }
261
262 char symmetry_htype = 0;
263 if (selectmode & SCE_SELECT_FACE) {
264 symmetry_htype |= BM_FACE;
265 }
266 if (selectmode & SCE_SELECT_EDGE) {
267 symmetry_htype |= BM_EDGE;
268 }
269 if (selectmode & SCE_SELECT_VERTEX) {
270 symmetry_htype |= BM_VERT;
271 }
272 if (std::optional<EditMeshSymmetryHelper> symmetry_helper =
273 EditMeshSymmetryHelper::create_if_needed(obedit, symmetry_htype))
274 {
275 const char hflag = BM_ELEM_SELECT;
276 BMIter iter;
277
278 if (selectmode & SCE_SELECT_FACE) {
279 Vector<BMFace *> source_faces;
280 source_faces.reserve(bm->totfacesel);
281 BMFace *f;
282 BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
283 if (BM_elem_flag_test(f, hflag)) {
284 source_faces.append(f);
285 }
286 }
287 const int totfacesel_prev = bm->totfacesel;
288 for (BMFace *f_orig : source_faces) {
289 symmetry_helper->set_hflag_on_mirror_faces(f_orig, hflag, true);
290 }
291 if (bm->totfacesel != totfacesel_prev) {
292 changed = true;
293 }
294 }
295 if (selectmode & SCE_SELECT_EDGE) {
296 Vector<BMEdge *> source_edges;
297 source_edges.reserve(bm->totedgesel);
298 BMEdge *e;
299 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
300 if (BM_elem_flag_test(e, hflag)) {
301 source_edges.append(e);
302 }
303 }
304 const int totedgesel_prev = bm->totedgesel;
305 for (BMEdge *e_orig : source_edges) {
306 symmetry_helper->set_hflag_on_mirror_edges(e_orig, hflag, true);
307 }
308 if (bm->totedgesel != totedgesel_prev) {
309 changed = true;
310 }
311 }
312 if (selectmode & SCE_SELECT_VERTEX) {
313 Vector<BMVert *> source_verts;
314 source_verts.reserve(bm->totvertsel);
315 BMVert *v;
316 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
317 if (BM_elem_flag_test(v, hflag)) {
318 source_verts.append(v);
319 }
320 }
321 const int totvertsel_prev = bm->totvertsel;
322 for (BMVert *v_orig : source_verts) {
323 symmetry_helper->set_hflag_on_mirror_verts(v_orig, hflag, true);
324 }
325 if (bm->totvertsel != totvertsel_prev) {
326 changed = true;
327 }
328 }
329 if (changed) {
330 EDBM_selectmode_flush(bm, selectmode);
331 }
332 }
333
334 return changed;
335}
336
338
339/* -------------------------------------------------------------------- */
342
344 const uint sel_id,
345 uint &r_base_index)
346{
347 uint elem_id;
348 char elem_type = 0;
349 bool success = DRW_select_buffer_elem_get(sel_id, elem_id, r_base_index, elem_type);
350
351 if (success) {
352 Object *obedit = bases[r_base_index]->object;
354
355 switch (elem_type) {
356 case SCE_SELECT_FACE:
357 return reinterpret_cast<BMElem *>(BM_face_at_index_find_or_table(bm, elem_id));
358 case SCE_SELECT_EDGE:
359 return reinterpret_cast<BMElem *>(BM_edge_at_index_find_or_table(bm, elem_id));
361 return reinterpret_cast<BMElem *>(BM_vert_at_index_find_or_table(bm, elem_id));
362 default:
363 BLI_assert(0);
364 return nullptr;
365 }
366 }
367
368 return nullptr;
369}
370
372
373/* -------------------------------------------------------------------- */
383
384#define FIND_NEAR_SELECT_BIAS 5
385#define FIND_NEAR_CYCLE_THRESHOLD_MIN 3
386
393
403
404static void findnearestvert__doClosest(void *user_data,
405 BMVert *eve,
406 const float screen_co[2],
407 int index)
408{
409 NearestVertUserData *data = static_cast<NearestVertUserData *>(user_data);
410 float dist_test, dist_test_bias;
411
412 dist_test = dist_test_bias = len_manhattan_v2v2(data->mval_fl, screen_co);
413
414 if (data->use_select_bias && BM_elem_flag_test(eve, BM_ELEM_SELECT)) {
415 dist_test_bias += FIND_NEAR_SELECT_BIAS;
416 }
417
418 if (dist_test_bias < data->hit.dist_bias) {
419 data->hit.dist_bias = dist_test_bias;
420 data->hit.dist = dist_test;
421 data->hit.index = index;
422 data->hit.vert = eve;
423 }
424
425 if (data->use_cycle) {
426 if ((data->hit_cycle.vert == nullptr) && (index > data->cycle_index_prev) &&
427 (dist_test_bias < FIND_NEAR_CYCLE_THRESHOLD_MIN))
428 {
429 data->hit_cycle.dist_bias = dist_test_bias;
430 data->hit_cycle.dist = dist_test;
431 data->hit_cycle.index = index;
432 data->hit_cycle.vert = eve;
433 }
434 }
435}
436
438 float *dist_px_manhattan_p,
439 const bool use_select_bias,
440 bool use_cycle,
441 const Span<Base *> bases,
442 uint *r_base_index)
443{
444 uint base_index = 0;
445
446 if (!XRAY_FLAG_ENABLED(vc->v3d)) {
447 uint dist_px_manhattan_test = uint(
448 ED_view3d_backbuf_sample_size_clamp(vc->region, *dist_px_manhattan_p));
449 uint index;
450 BMVert *eve;
451
452 /* No after-queue (yet), so we check it now, otherwise the bm_xxxofs indices are bad. */
453 {
455
457 vc->depsgraph, vc->region, vc->v3d, vc->mval, 1, UINT_MAX, &dist_px_manhattan_test);
458
459 if (index) {
460 eve = reinterpret_cast<BMVert *>(edbm_select_id_bm_elem_get(bases, index, base_index));
461 }
462 else {
463 eve = nullptr;
464 }
465 }
466
467 if (eve) {
468 if (dist_px_manhattan_test < *dist_px_manhattan_p) {
469 if (r_base_index) {
470 *r_base_index = base_index;
471 }
472 *dist_px_manhattan_p = dist_px_manhattan_test;
473 return eve;
474 }
475 }
476 return nullptr;
477 }
478
480 const NearestVertUserData_Hit *hit = nullptr;
481 const eV3DProjTest clip_flag = RV3D_CLIPPING_ENABLED(vc->v3d, vc->rv3d) ?
484 BMesh *prev_select_bm = nullptr;
485
486 static struct {
487 int index;
488 const BMVert *elem;
489 const BMesh *bm;
490 } prev_select = {0};
491
492 data.mval_fl[0] = vc->mval[0];
493 data.mval_fl[1] = vc->mval[1];
494 data.use_select_bias = use_select_bias;
495 data.use_cycle = use_cycle;
496
497 for (; base_index < bases.size(); base_index++) {
498 Base *base_iter = bases[base_index];
501 if (use_cycle && prev_select.bm == bm &&
502 prev_select.elem == BM_vert_at_index_find_or_table(bm, prev_select.index))
503 {
504 data.cycle_index_prev = prev_select.index;
505 /* No need to compare in the rest of the loop. */
506 use_cycle = false;
507 }
508 else {
509 data.cycle_index_prev = 0;
510 }
511
512 data.hit.dist = data.hit_cycle.dist = data.hit.dist_bias = data.hit_cycle.dist_bias =
513 *dist_px_manhattan_p;
514
517
518 hit = (data.use_cycle && data.hit_cycle.vert) ? &data.hit_cycle : &data.hit;
519
520 if (hit->dist < *dist_px_manhattan_p) {
521 if (r_base_index) {
522 *r_base_index = base_index;
523 }
524 *dist_px_manhattan_p = hit->dist;
525 prev_select_bm = bm;
526 }
527 }
528
529 if (hit == nullptr) {
530 return nullptr;
531 }
532
533 prev_select.index = hit->index;
534 prev_select.elem = hit->vert;
535 prev_select.bm = prev_select_bm;
536
537 return hit->vert;
538}
539
540BMVert *EDBM_vert_find_nearest(ViewContext *vc, float *dist_px_manhattan_p)
541{
544 return EDBM_vert_find_nearest_ex(vc, dist_px_manhattan_p, false, false, {base}, nullptr);
545}
546
549 float mval_fl[2];
550 float dist;
552};
553
554static void find_nearest_edge_center__doZBuf(void *user_data,
555 BMEdge *eed,
556 const float screen_co_a[2],
557 const float screen_co_b[2],
558 int /*index*/)
559{
560 NearestEdgeUserData_ZBuf *data = static_cast<NearestEdgeUserData_ZBuf *>(user_data);
561
562 if (eed == data->edge_test) {
563 float dist_test;
564 float screen_co_mid[2];
565
566 mid_v2_v2v2(screen_co_mid, screen_co_a, screen_co_b);
567 dist_test = len_manhattan_v2v2(data->mval_fl, screen_co_mid);
568
569 data->dist = std::min(dist_test, data->dist);
570 }
571}
572
585
596
597/* NOTE: uses v3d, so needs active 3d window. */
598static void find_nearest_edge__doClosest(void *user_data,
599 BMEdge *eed,
600 const float screen_co_a[2],
601 const float screen_co_b[2],
602 int index)
603{
604 NearestEdgeUserData *data = static_cast<NearestEdgeUserData *>(user_data);
605 float dist_test, dist_test_bias;
606
607 float fac = line_point_factor_v2(data->mval_fl, screen_co_a, screen_co_b);
608 float screen_co[2];
609
610 if (fac <= 0.0f) {
611 fac = 0.0f;
612 copy_v2_v2(screen_co, screen_co_a);
613 }
614 else if (fac >= 1.0f) {
615 fac = 1.0f;
616 copy_v2_v2(screen_co, screen_co_b);
617 }
618 else {
619 interp_v2_v2v2(screen_co, screen_co_a, screen_co_b, fac);
620 }
621
622 dist_test = dist_test_bias = len_manhattan_v2v2(data->mval_fl, screen_co);
623
624 if (data->use_select_bias && BM_elem_flag_test(eed, BM_ELEM_SELECT)) {
625 dist_test_bias += FIND_NEAR_SELECT_BIAS;
626 }
627
628 if ((data->vc.rv3d->rflag & RV3D_CLIPPING) && (data->vc.rv3d->clipbb != nullptr)) {
629 float vec[3];
630
631 interp_v3_v3v3(vec, eed->v1->co, eed->v2->co, fac);
632 if (ED_view3d_clipping_test(data->vc.rv3d, vec, true)) {
633 return;
634 }
635 }
636
637 if (dist_test_bias < data->hit.dist_bias) {
638 float screen_co_mid[2];
639
640 data->hit.dist_bias = dist_test_bias;
641 data->hit.dist = dist_test;
642 data->hit.index = index;
643 data->hit.edge = eed;
644
645 mid_v2_v2v2(screen_co_mid, screen_co_a, screen_co_b);
646 data->hit.dist_center_px_manhattan = len_manhattan_v2v2(data->mval_fl, screen_co_mid);
647 }
648
649 if (data->use_cycle) {
650 if ((data->hit_cycle.edge == nullptr) && (index > data->cycle_index_prev) &&
651 (dist_test_bias < FIND_NEAR_CYCLE_THRESHOLD_MIN))
652 {
653 float screen_co_mid[2];
654
655 data->hit_cycle.dist_bias = dist_test_bias;
656 data->hit_cycle.dist = dist_test;
657 data->hit_cycle.index = index;
658 data->hit_cycle.edge = eed;
659
660 mid_v2_v2v2(screen_co_mid, screen_co_a, screen_co_b);
661 data->hit_cycle.dist_center_px_manhattan = len_manhattan_v2v2(data->mval_fl, screen_co_mid);
662 }
663 }
664}
665
667 float *dist_px_manhattan_p,
668 float *r_dist_center_px_manhattan,
669 const bool use_select_bias,
670 bool use_cycle,
671 BMEdge **r_eed_zbuf,
672 const Span<Base *> bases,
673 uint *r_base_index)
674{
675 uint base_index = 0;
676
677 if (!XRAY_FLAG_ENABLED(vc->v3d)) {
678 uint dist_px_manhattan_test = uint(
679 ED_view3d_backbuf_sample_size_clamp(vc->region, *dist_px_manhattan_p));
680 uint index;
681 BMEdge *eed;
682
683 /* No after-queue (yet), so we check it now, otherwise the bm_xxxofs indices are bad. */
684 {
686
688 vc->depsgraph, vc->region, vc->v3d, vc->mval, 1, UINT_MAX, &dist_px_manhattan_test);
689
690 if (index) {
691 eed = reinterpret_cast<BMEdge *>(edbm_select_id_bm_elem_get(bases, index, base_index));
692 }
693 else {
694 eed = nullptr;
695 }
696 }
697
698 if (r_eed_zbuf) {
699 *r_eed_zbuf = eed;
700 }
701
702 /* Exception for faces (verts don't need this). */
703 if (r_dist_center_px_manhattan && eed) {
705
706 data.mval_fl[0] = vc->mval[0];
707 data.mval_fl[1] = vc->mval[1];
708 data.dist = FLT_MAX;
709 data.edge_test = eed;
710
712
715 &data,
717
718 *r_dist_center_px_manhattan = data.dist;
719 }
720 /* End exception. */
721
722 if (eed) {
723 if (dist_px_manhattan_test < *dist_px_manhattan_p) {
724 if (r_base_index) {
725 *r_base_index = base_index;
726 }
727 *dist_px_manhattan_p = dist_px_manhattan_test;
728 return eed;
729 }
730 }
731 return nullptr;
732 }
733
734 NearestEdgeUserData data = {{nullptr}};
735 const NearestEdgeUserData_Hit *hit = nullptr;
736 /* Interpolate along the edge before doing a clipping plane test. */
738 BMesh *prev_select_bm = nullptr;
739
740 static struct {
741 int index;
742 const BMEdge *elem;
743 const BMesh *bm;
744 } prev_select = {0};
745
746 data.vc = *vc;
747 data.mval_fl[0] = vc->mval[0];
748 data.mval_fl[1] = vc->mval[1];
749 data.use_select_bias = use_select_bias;
750 data.use_cycle = use_cycle;
751
752 for (; base_index < bases.size(); base_index++) {
753 Base *base_iter = bases[base_index];
756 if (use_cycle && prev_select.bm == bm &&
757 prev_select.elem == BM_edge_at_index_find_or_table(bm, prev_select.index))
758 {
759 data.cycle_index_prev = prev_select.index;
760 /* No need to compare in the rest of the loop. */
761 use_cycle = false;
762 }
763 else {
764 data.cycle_index_prev = 0;
765 }
766
767 data.hit.dist = data.hit_cycle.dist = data.hit.dist_bias = data.hit_cycle.dist_bias =
768 *dist_px_manhattan_p;
769
773
774 hit = (data.use_cycle && data.hit_cycle.edge) ? &data.hit_cycle : &data.hit;
775
776 if (hit->dist < *dist_px_manhattan_p) {
777 if (r_base_index) {
778 *r_base_index = base_index;
779 }
780 *dist_px_manhattan_p = hit->dist;
781 prev_select_bm = bm;
782 }
783 }
784
785 if (hit == nullptr) {
786 return nullptr;
787 }
788
789 if (r_dist_center_px_manhattan) {
790 *r_dist_center_px_manhattan = hit->dist_center_px_manhattan;
791 }
792
793 prev_select.index = hit->index;
794 prev_select.elem = hit->edge;
795 prev_select.bm = prev_select_bm;
796
797 return hit->edge;
798}
799
800BMEdge *EDBM_edge_find_nearest(ViewContext *vc, float *dist_px_manhattan_p)
801{
805 vc, dist_px_manhattan_p, nullptr, false, false, nullptr, {base}, nullptr);
806}
807
814
815static void find_nearest_face_center__doZBuf(void *user_data,
816 BMFace *efa,
817 const float screen_co[2],
818 int /*index*/)
819{
820 NearestFaceUserData_ZBuf *data = static_cast<NearestFaceUserData_ZBuf *>(user_data);
821
822 if (efa == data->face_test) {
823 const float dist_test = len_manhattan_v2v2(data->mval_fl, screen_co);
824
825 data->dist_px_manhattan = std::min(dist_test, data->dist_px_manhattan);
826 }
827}
828
835
845
846static void findnearestface__doClosest(void *user_data,
847 BMFace *efa,
848 const float screen_co[2],
849 int index)
850{
851 NearestFaceUserData *data = static_cast<NearestFaceUserData *>(user_data);
852 float dist_test, dist_test_bias;
853
854 dist_test = dist_test_bias = len_manhattan_v2v2(data->mval_fl, screen_co);
855
856 if (data->use_select_bias && BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
857 dist_test_bias += FIND_NEAR_SELECT_BIAS;
858 }
859
860 if (dist_test_bias < data->hit.dist_bias) {
861 data->hit.dist_bias = dist_test_bias;
862 data->hit.dist = dist_test;
863 data->hit.index = index;
864 data->hit.face = efa;
865 }
866
867 if (data->use_cycle) {
868 if ((data->hit_cycle.face == nullptr) && (index > data->cycle_index_prev) &&
869 (dist_test_bias < FIND_NEAR_CYCLE_THRESHOLD_MIN))
870 {
871 data->hit_cycle.dist_bias = dist_test_bias;
872 data->hit_cycle.dist = dist_test;
873 data->hit_cycle.index = index;
874 data->hit_cycle.face = efa;
875 }
876 }
877}
878
880 float *dist_px_manhattan_p,
881 float *r_dist_center,
882 const bool use_zbuf_single_px,
883 const bool use_select_bias,
884 bool use_cycle,
885 BMFace **r_efa_zbuf,
886 const Span<Base *> bases,
887 uint *r_base_index)
888{
889 uint base_index = 0;
890
891 if (!XRAY_FLAG_ENABLED(vc->v3d)) {
892 float dist_test;
893 uint index;
894 BMFace *efa;
895
896 {
897 uint dist_px_manhattan_test = 0;
898 if (*dist_px_manhattan_p != 0.0f && (use_zbuf_single_px == false)) {
899 dist_px_manhattan_test = uint(
900 ED_view3d_backbuf_sample_size_clamp(vc->region, *dist_px_manhattan_p));
901 }
902
904
905 if (dist_px_manhattan_test == 0) {
907 dist_test = 0.0f;
908 }
909 else {
911 vc->depsgraph, vc->region, vc->v3d, vc->mval, 1, UINT_MAX, &dist_px_manhattan_test);
912 dist_test = dist_px_manhattan_test;
913 }
914
915 if (index) {
916 efa = reinterpret_cast<BMFace *>(edbm_select_id_bm_elem_get(bases, index, base_index));
917 }
918 else {
919 efa = nullptr;
920 }
921 }
922
923 if (r_efa_zbuf) {
924 *r_efa_zbuf = efa;
925 }
926
927 /* Exception for faces (verts don't need this). */
928 if (r_dist_center && efa) {
930
931 data.mval_fl[0] = vc->mval[0];
932 data.mval_fl[1] = vc->mval[1];
933 data.dist_px_manhattan = FLT_MAX;
934 data.face_test = efa;
935
937
940
941 *r_dist_center = data.dist_px_manhattan;
942 }
943 /* End exception. */
944
945 if (efa) {
946 if (dist_test < *dist_px_manhattan_p) {
947 if (r_base_index) {
948 *r_base_index = base_index;
949 }
950 *dist_px_manhattan_p = dist_test;
951 return efa;
952 }
953 }
954 return nullptr;
955 }
956
958 const NearestFaceUserData_Hit *hit = nullptr;
960 BMesh *prev_select_bm = nullptr;
961
962 static struct {
963 int index;
964 const BMFace *elem;
965 const BMesh *bm;
966 } prev_select = {0};
967
968 data.mval_fl[0] = vc->mval[0];
969 data.mval_fl[1] = vc->mval[1];
970 data.use_select_bias = use_select_bias;
971 data.use_cycle = use_cycle;
972
973 for (; base_index < bases.size(); base_index++) {
974 Base *base_iter = bases[base_index];
977 if (use_cycle && prev_select.bm == bm &&
978 prev_select.elem == BM_face_at_index_find_or_table(bm, prev_select.index))
979 {
980 data.cycle_index_prev = prev_select.index;
981 /* No need to compare in the rest of the loop. */
982 use_cycle = false;
983 }
984 else {
985 data.cycle_index_prev = 0;
986 }
987
988 data.hit.dist = data.hit_cycle.dist = data.hit.dist_bias = data.hit_cycle.dist_bias =
989 *dist_px_manhattan_p;
990
993
994 hit = (data.use_cycle && data.hit_cycle.face) ? &data.hit_cycle : &data.hit;
995
996 if (hit->dist < *dist_px_manhattan_p) {
997 if (r_base_index) {
998 *r_base_index = base_index;
999 }
1000 *dist_px_manhattan_p = hit->dist;
1001 prev_select_bm = bm;
1002 }
1003 }
1004
1005 if (hit == nullptr) {
1006 return nullptr;
1007 }
1008
1009 if (r_dist_center) {
1010 *r_dist_center = hit->dist;
1011 }
1012
1013 prev_select.index = hit->index;
1014 prev_select.elem = hit->face;
1015 prev_select.bm = prev_select_bm;
1016
1017 return hit->face;
1018}
1019
1020BMFace *EDBM_face_find_nearest(ViewContext *vc, float *dist_px_manhattan_p)
1021{
1025 vc, dist_px_manhattan_p, nullptr, false, false, false, nullptr, {base}, nullptr);
1026}
1027
1028#undef FIND_NEAR_SELECT_BIAS
1029#undef FIND_NEAR_CYCLE_THRESHOLD_MIN
1030
1038 const Span<Base *> bases,
1039 int *r_base_index,
1040 BMVert **r_eve,
1041 BMEdge **r_eed,
1042 BMFace **r_efa)
1043{
1044 BMEditMesh *em = vc->em;
1045
1046 const bool use_cycle = !WM_cursor_test_motion_and_update(vc->mval);
1047 const float dist_init = ED_view3d_select_dist_px();
1048 /* Since edges select lines, we give dots advantage of ~20 pix. */
1049 const float dist_margin = (dist_init / 2);
1050 float dist = dist_init;
1051
1052 struct {
1053 struct {
1054 BMVert *ele;
1055 int base_index;
1056 } v;
1057 struct {
1058 BMEdge *ele;
1059 int base_index;
1060 } e, e_zbuf;
1061 struct {
1062 BMFace *ele;
1063 int base_index;
1064 } f, f_zbuf;
1065 } hit = {{nullptr}};
1066
1067 /* No after-queue (yet), so we check it now, otherwise the em_xxxofs indices are bad. */
1068
1069 if ((dist > 0.0f) && (em->selectmode & SCE_SELECT_FACE)) {
1070 float dist_center = 0.0f;
1071 float *dist_center_p = (em->selectmode & (SCE_SELECT_EDGE | SCE_SELECT_VERTEX)) ?
1072 &dist_center :
1073 nullptr;
1074
1075 uint base_index = 0;
1076 BMFace *efa_zbuf = nullptr;
1078 vc, &dist, dist_center_p, true, true, use_cycle, &efa_zbuf, bases, &base_index);
1079
1080 if (efa_test && dist_center_p) {
1081 dist = min_ff(dist_margin, dist_center);
1082 }
1083 if (efa_test) {
1084 hit.f.base_index = base_index;
1085 hit.f.ele = efa_test;
1086 }
1087 if (efa_zbuf) {
1088 hit.f_zbuf.base_index = base_index;
1089 hit.f_zbuf.ele = efa_zbuf;
1090 }
1091 }
1092
1093 if ((dist > 0.0f) && (em->selectmode & SCE_SELECT_EDGE)) {
1094 float dist_center = 0.0f;
1095 float *dist_center_p = (em->selectmode & SCE_SELECT_VERTEX) ? &dist_center : nullptr;
1096
1097 uint base_index = 0;
1098 BMEdge *eed_zbuf = nullptr;
1100 vc, &dist, dist_center_p, true, use_cycle, &eed_zbuf, bases, &base_index);
1101
1102 if (eed_test && dist_center_p) {
1103 dist = min_ff(dist_margin, dist_center);
1104 }
1105 if (eed_test) {
1106 hit.e.base_index = base_index;
1107 hit.e.ele = eed_test;
1108 }
1109 if (eed_zbuf) {
1110 hit.e_zbuf.base_index = base_index;
1111 hit.e_zbuf.ele = eed_zbuf;
1112 }
1113 }
1114
1115 if ((dist > 0.0f) && (em->selectmode & SCE_SELECT_VERTEX)) {
1116 uint base_index = 0;
1117 BMVert *eve_test = EDBM_vert_find_nearest_ex(vc, &dist, true, use_cycle, bases, &base_index);
1118
1119 if (eve_test) {
1120 hit.v.base_index = base_index;
1121 hit.v.ele = eve_test;
1122 }
1123 }
1124
1125 /* Return only one of 3 pointers, for front-buffer redraws. */
1126 if (hit.v.ele) {
1127 hit.f.ele = nullptr;
1128 hit.e.ele = nullptr;
1129 }
1130 else if (hit.e.ele) {
1131 hit.f.ele = nullptr;
1132 }
1133
1134 /* There may be a face under the cursor, who's center if too far away
1135 * use this if all else fails, it makes sense to select this. */
1136 if ((hit.v.ele || hit.e.ele || hit.f.ele) == 0) {
1137 if (hit.e_zbuf.ele) {
1138 hit.e.base_index = hit.e_zbuf.base_index;
1139 hit.e.ele = hit.e_zbuf.ele;
1140 }
1141 else if (hit.f_zbuf.ele) {
1142 hit.f.base_index = hit.f_zbuf.base_index;
1143 hit.f.ele = hit.f_zbuf.ele;
1144 }
1145 }
1146
1147 /* Only one element type will be non-null. */
1148 BLI_assert(((hit.v.ele != nullptr) + (hit.e.ele != nullptr) + (hit.f.ele != nullptr)) <= 1);
1149
1150 if (hit.v.ele) {
1151 *r_base_index = hit.v.base_index;
1152 }
1153 if (hit.e.ele) {
1154 *r_base_index = hit.e.base_index;
1155 }
1156 if (hit.f.ele) {
1157 *r_base_index = hit.f.base_index;
1158 }
1159
1160 *r_eve = hit.v.ele;
1161 *r_eed = hit.e.ele;
1162 *r_efa = hit.f.ele;
1163
1164 return (hit.v.ele || hit.e.ele || hit.f.ele);
1165}
1166
1167#undef FAKE_SELECT_MODE_BEGIN
1168#undef FAKE_SELECT_MODE_END
1169
1171 const Span<Base *> bases,
1172 int *r_base_index,
1173 BMVert **r_eve,
1174 BMEdge **r_eed,
1175 BMFace **r_efa)
1176{
1177 return unified_findnearest(vc, bases, r_base_index, r_eve, r_eed, r_efa);
1178}
1179
1181
1182/* -------------------------------------------------------------------- */
1188
1190 const Span<Base *> bases,
1191 bool use_boundary_vertices,
1192 bool use_boundary_edges,
1193 int *r_base_index_vert,
1194 int *r_base_index_edge,
1195 int *r_base_index_face,
1196 BMVert **r_eve,
1197 BMEdge **r_eed,
1198 BMFace **r_efa)
1199{
1200 const float mval_fl[2] = {float(vc->mval[0]), float(vc->mval[1])};
1201 float ray_origin[3], ray_direction[3];
1202
1203 struct {
1204 uint base_index;
1205 BMElem *ele;
1206 } best = {0, nullptr};
1207 /* Currently unused, keep since we may want to pick the best. */
1208 UNUSED_VARS(best);
1209
1210 struct {
1211 uint base_index;
1212 BMElem *ele;
1213 } best_vert = {0, nullptr};
1214
1215 struct {
1216 uint base_index;
1217 BMElem *ele;
1218 } best_edge = {0, nullptr};
1219
1220 struct {
1221 uint base_index;
1222 BMElem *ele;
1223 } best_face = {0, nullptr};
1224
1226 vc->depsgraph, vc->region, vc->v3d, mval_fl, ray_origin, ray_direction, true))
1227 {
1228 float dist_sq_best = FLT_MAX;
1229 float dist_sq_best_vert = FLT_MAX;
1230 float dist_sq_best_edge = FLT_MAX;
1231 float dist_sq_best_face = FLT_MAX;
1232
1233 const bool use_vert = (r_eve != nullptr);
1234 const bool use_edge = (r_eed != nullptr);
1235 const bool use_face = (r_efa != nullptr);
1236
1237 for (const int base_index : bases.index_range()) {
1238 Base *base_iter = bases[base_index];
1239 Object *obedit = base_iter->object;
1240
1242 float imat3[3][3];
1243
1245 copy_m3_m4(imat3, obedit->object_to_world().ptr());
1246 invert_m3(imat3);
1247
1248 Span<float3> vert_positions;
1249 {
1250 const Object *obedit_eval = DEG_get_evaluated(vc->depsgraph, obedit);
1251 const Mesh *mesh_eval = BKE_object_get_editmesh_eval_cage(obedit_eval);
1252 if (BKE_mesh_wrapper_vert_len(mesh_eval) == bm->totvert) {
1253 vert_positions = BKE_mesh_wrapper_vert_coords(mesh_eval);
1254 }
1255 }
1256
1257 if (!vert_positions.is_empty()) {
1259 }
1260
1261 if ((use_boundary_vertices || use_boundary_edges) && (use_vert || use_edge)) {
1262 BMEdge *e;
1263 BMIter eiter;
1264 BM_ITER_MESH (e, &eiter, bm, BM_EDGES_OF_MESH) {
1266 continue;
1267 }
1268
1269 if (BM_edge_is_boundary(e)) {
1270 if (use_vert && use_boundary_vertices) {
1271 for (uint j = 0; j < 2; j++) {
1272 BMVert *v = *((&e->v1) + j);
1273 float point[3];
1274 mul_v3_m4v3(point,
1275 obedit->object_to_world().ptr(),
1276 !vert_positions.is_empty() ? vert_positions[BM_elem_index_get(v)] :
1277 v->co);
1278 const float dist_sq_test = dist_squared_to_ray_v3_normalized(
1279 ray_origin, ray_direction, point);
1280 if (dist_sq_test < dist_sq_best_vert) {
1281 dist_sq_best_vert = dist_sq_test;
1282 best_vert.base_index = base_index;
1283 best_vert.ele = reinterpret_cast<BMElem *>(v);
1284 }
1285 if (dist_sq_test < dist_sq_best) {
1286 dist_sq_best = dist_sq_test;
1287 best.base_index = base_index;
1288 best.ele = reinterpret_cast<BMElem *>(v);
1289 }
1290 }
1291 }
1292
1293 if (use_edge && use_boundary_edges) {
1294 float point[3];
1295#if 0
1296 const float dist_sq_test = dist_squared_ray_to_seg_v3(
1297 ray_origin, ray_direction, e->v1->co, e->v2->co, point, &depth);
1298#else
1299 if (!vert_positions.is_empty()) {
1300 mid_v3_v3v3(point,
1301 vert_positions[BM_elem_index_get(e->v1)],
1302 vert_positions[BM_elem_index_get(e->v2)]);
1303 }
1304 else {
1305 mid_v3_v3v3(point, e->v1->co, e->v2->co);
1306 }
1307 mul_m4_v3(obedit->object_to_world().ptr(), point);
1308 const float dist_sq_test = dist_squared_to_ray_v3_normalized(
1309 ray_origin, ray_direction, point);
1310 if (dist_sq_test < dist_sq_best_edge) {
1311 dist_sq_best_edge = dist_sq_test;
1312 best_edge.base_index = base_index;
1313 best_edge.ele = reinterpret_cast<BMElem *>(e);
1314 }
1315 if (dist_sq_test < dist_sq_best) {
1316 dist_sq_best = dist_sq_test;
1317 best.base_index = base_index;
1318 best.ele = reinterpret_cast<BMElem *>(e);
1319 }
1320#endif
1321 }
1322 }
1323 }
1324 }
1325 /* Non boundary case. */
1326 if (use_vert && !use_boundary_vertices) {
1327 BMVert *v;
1328 BMIter viter;
1329 BM_ITER_MESH (v, &viter, bm, BM_VERTS_OF_MESH) {
1331 continue;
1332 }
1333
1334 float point[3];
1335 mul_v3_m4v3(point,
1336 obedit->object_to_world().ptr(),
1337 !vert_positions.is_empty() ? vert_positions[BM_elem_index_get(v)] : v->co);
1338 const float dist_sq_test = dist_squared_to_ray_v3_normalized(
1339 ray_origin, ray_direction, point);
1340 if (dist_sq_test < dist_sq_best_vert) {
1341 dist_sq_best_vert = dist_sq_test;
1342 best_vert.base_index = base_index;
1343 best_vert.ele = reinterpret_cast<BMElem *>(v);
1344 }
1345 if (dist_sq_test < dist_sq_best) {
1346 dist_sq_best = dist_sq_test;
1347 best.base_index = base_index;
1348 best.ele = reinterpret_cast<BMElem *>(v);
1349 }
1350 }
1351 }
1352
1353 if (use_edge && !use_boundary_edges) {
1354 BMEdge *e;
1355 BMIter eiter;
1356 BM_ITER_MESH (e, &eiter, bm, BM_EDGES_OF_MESH) {
1358 continue;
1359 }
1360
1361 float point[3];
1362 if (!vert_positions.is_empty()) {
1363 mid_v3_v3v3(point,
1364 vert_positions[BM_elem_index_get(e->v1)],
1365 vert_positions[BM_elem_index_get(e->v2)]);
1366 }
1367 else {
1368 mid_v3_v3v3(point, e->v1->co, e->v2->co);
1369 }
1370 mul_m4_v3(obedit->object_to_world().ptr(), point);
1371 const float dist_sq_test = dist_squared_to_ray_v3_normalized(
1372 ray_origin, ray_direction, point);
1373 if (dist_sq_test < dist_sq_best_edge) {
1374 dist_sq_best_edge = dist_sq_test;
1375 best_edge.base_index = base_index;
1376 best_edge.ele = reinterpret_cast<BMElem *>(e);
1377 }
1378 if (dist_sq_test < dist_sq_best) {
1379 dist_sq_best = dist_sq_test;
1380 best.base_index = base_index;
1381 best.ele = reinterpret_cast<BMElem *>(e);
1382 }
1383 }
1384 }
1385
1386 if (use_face) {
1387 BMFace *f;
1388 BMIter fiter;
1389 BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
1391 continue;
1392 }
1393
1394 float point[3];
1395 if (!vert_positions.is_empty()) {
1396 BM_face_calc_center_median_vcos(bm, f, point, vert_positions);
1397 }
1398 else {
1400 }
1401 mul_m4_v3(obedit->object_to_world().ptr(), point);
1402 const float dist_sq_test = dist_squared_to_ray_v3_normalized(
1403 ray_origin, ray_direction, point);
1404 if (dist_sq_test < dist_sq_best_face) {
1405 dist_sq_best_face = dist_sq_test;
1406 best_face.base_index = base_index;
1407 best_face.ele = reinterpret_cast<BMElem *>(f);
1408 }
1409 if (dist_sq_test < dist_sq_best) {
1410 dist_sq_best = dist_sq_test;
1411 best.base_index = base_index;
1412 best.ele = reinterpret_cast<BMElem *>(f);
1413 }
1414 }
1415 }
1416 }
1417 }
1418
1419 *r_base_index_vert = best_vert.base_index;
1420 *r_base_index_edge = best_edge.base_index;
1421 *r_base_index_face = best_face.base_index;
1422
1423 if (r_eve) {
1424 *r_eve = nullptr;
1425 }
1426 if (r_eed) {
1427 *r_eed = nullptr;
1428 }
1429 if (r_efa) {
1430 *r_efa = nullptr;
1431 }
1432
1433 if (best_vert.ele) {
1434 *r_eve = reinterpret_cast<BMVert *>(best_vert.ele);
1435 }
1436 if (best_edge.ele) {
1437 *r_eed = reinterpret_cast<BMEdge *>(best_edge.ele);
1438 }
1439 if (best_face.ele) {
1440 *r_efa = reinterpret_cast<BMFace *>(best_face.ele);
1441 }
1442
1443 return (best_vert.ele != nullptr || best_edge.ele != nullptr || best_face.ele != nullptr);
1444}
1445
1447
1448/* -------------------------------------------------------------------- */
1451
1453{
1454 Object *obedit = CTX_data_edit_object(C);
1456 bool changed = false;
1457
1458 /* Group variables. */
1459 int (*group_index)[2];
1460 int group_tot;
1461 int i;
1462
1463 if (bm->totfacesel < 2) {
1464 BKE_report(op->reports, RPT_ERROR, "No face regions selected");
1465 return OPERATOR_CANCELLED;
1466 }
1467
1468 int *groups_array = MEM_new_array_uninitialized<int>(bm->totfacesel, __func__);
1469 group_tot = BM_mesh_calc_face_groups(
1470 bm, groups_array, &group_index, nullptr, nullptr, nullptr, BM_ELEM_SELECT, BM_VERT);
1471
1473
1474 for (i = 0; i < group_tot; i++) {
1475 ListBaseT<LinkData> faces_regions;
1476 int tot;
1477
1478 const int fg_sta = group_index[i][0];
1479 const int fg_len = group_index[i][1];
1480 int j;
1481 BMFace **fg = MEM_new_array_uninitialized<BMFace *>(fg_len, __func__);
1482
1483 for (j = 0; j < fg_len; j++) {
1484 fg[j] = BM_face_at_index(bm, groups_array[fg_sta + j]);
1485 }
1486
1487 tot = BM_mesh_region_match(bm, fg, fg_len, &faces_regions);
1488
1489 MEM_delete(fg);
1490
1491 if (tot) {
1492 while (LinkData *link = static_cast<LinkData *>(BLI_pophead(&faces_regions))) {
1493 BMFace **faces = static_cast<BMFace **>(link->data);
1494 while (BMFace *f = *(faces++)) {
1495 BM_face_select_set(bm, f, true);
1496 }
1497 MEM_delete_void(link->data);
1498 MEM_delete(link);
1499
1500 changed = true;
1501 }
1502 }
1503 }
1504
1505 MEM_delete(groups_array);
1506 MEM_delete(group_index);
1507
1508 if (changed) {
1512 }
1513 else {
1514 BKE_report(op->reports, RPT_WARNING, "No matching face regions found");
1515 }
1516
1517 return OPERATOR_FINISHED;
1518}
1519
1521{
1522 /* Identifiers. */
1523 ot->name = "Select Similar Regions";
1524 ot->idname = "MESH_OT_select_similar_region";
1525 ot->description = "Select similar face regions to the current selection";
1526
1527 /* API callbacks. */
1530
1531 /* Flags. */
1533}
1534
1536
1537/* -------------------------------------------------------------------- */
1540
1542{
1543 const int type = RNA_enum_get(op->ptr, "type");
1544 const int action = RNA_enum_get(op->ptr, "action");
1545 const bool use_extend = RNA_boolean_get(op->ptr, "use_extend");
1546 const bool use_expand = RNA_boolean_get(op->ptr, "use_expand");
1547
1548 if (EDBM_selectmode_toggle_multi(C, type, action, use_extend, use_expand)) {
1549 return OPERATOR_FINISHED;
1550 }
1551 return OPERATOR_CANCELLED;
1552}
1553
1555{
1556 /* Bypass when in UV non sync-select mode, fall through to keymap that edits. */
1557 if (CTX_wm_space_image(C)) {
1559 if ((ts->uv_flag & UV_FLAG_SELECT_SYNC) == 0) {
1560 return OPERATOR_PASS_THROUGH;
1561 }
1562 /* Bypass when no action is needed. */
1563 if (!RNA_struct_property_is_set(op->ptr, "type")) {
1564 return OPERATOR_CANCELLED;
1565 }
1566 }
1567
1568 /* Detecting these options based on shift/control here is weak, but it's done
1569 * to make this work when clicking buttons or menus. */
1570 if (!RNA_struct_property_is_set(op->ptr, "use_extend")) {
1571 RNA_boolean_set(op->ptr, "use_extend", event->modifier & KM_SHIFT);
1572 }
1573 if (!RNA_struct_property_is_set(op->ptr, "use_expand")) {
1574 RNA_boolean_set(op->ptr, "use_expand", event->modifier & KM_CTRL);
1575 }
1576
1577 return edbm_select_mode_exec(C, op);
1578}
1579
1581 wmOperatorType * /*ot*/,
1582 PointerRNA *ptr)
1583{
1584 const int type = RNA_enum_get(ptr, "type");
1585
1586 /* Because the special behavior for shift and ctrl click depend on user input, they may be
1587 * incorrect if the operator is used from a script or from a special button. So only return the
1588 * specialized descriptions if only the "type" is set, which conveys that the operator is meant
1589 * to be used with the logic in the `invoke` method. */
1590 if (RNA_struct_property_is_set(ptr, "type") && !RNA_struct_property_is_set(ptr, "use_extend") &&
1591 !RNA_struct_property_is_set(ptr, "use_expand") && !RNA_struct_property_is_set(ptr, "action"))
1592 {
1593 switch (type) {
1594 case SCE_SELECT_VERTEX:
1595 return TIP_(
1596 "Vertex select - Shift-Click for multiple modes, Ctrl-Click contracts selection");
1597 case SCE_SELECT_EDGE:
1598 return TIP_(
1599 "Edge select - Shift-Click for multiple modes, "
1600 "Ctrl-Click expands/contracts selection depending on the current mode");
1601 case SCE_SELECT_FACE:
1602 return TIP_("Face select - Shift-Click for multiple modes, Ctrl-Click expands selection");
1603 }
1604 }
1605
1606 return "";
1607}
1608
1610{
1611 PropertyRNA *prop;
1612
1613 static const EnumPropertyItem actions_items[] = {
1614 {0, "DISABLE", false, "Disable", "Disable the selection mode"},
1615 {1, "ENABLE", false, "Enable", "Enable the selection mode"},
1616 {2, "TOGGLE", false, "Toggle", "Toggle the selection mode"},
1617 {0, nullptr, 0, nullptr, nullptr},
1618 };
1619
1620 /* Identifiers. */
1621 ot->name = "Select Mode";
1622 ot->idname = "MESH_OT_select_mode";
1623 ot->description = "Change selection mode";
1624
1625 /* API callbacks. */
1630
1631 /* Flags. */
1633
1634 /* Properties. */
1635 /* Hide all, not to show redo panel. */
1636 prop = RNA_def_boolean(ot->srna, "use_extend", false, "Extend", "");
1638 prop = RNA_def_boolean(ot->srna, "use_expand", false, "Expand", "");
1640 ot->prop = prop = RNA_def_enum(ot->srna, "type", rna_enum_mesh_select_mode_items, 0, "Type", "");
1642
1643 prop = RNA_def_enum(
1644 ot->srna, "action", actions_items, 2, "Action", "Selection action to execute");
1646}
1647
1649
1650/* -------------------------------------------------------------------- */
1653
1655 int walkercode,
1656 void *start,
1657 int r_count_by_select[2],
1658 BMWFlag flags,
1659 BMWDelimitFlag delimit)
1660{
1661 BMElem *ele;
1662 BMWalker walker;
1663
1664 r_count_by_select[0] = r_count_by_select[1] = 0;
1665
1666 BMW_init(&walker,
1667 bm,
1668 walkercode,
1672 flags,
1674 delimit);
1675
1676 for (ele = static_cast<BMElem *>(BMW_begin(&walker, start)); ele;
1677 ele = static_cast<BMElem *>(BMW_step(&walker)))
1678 {
1679 r_count_by_select[BM_elem_flag_test(ele, BM_ELEM_SELECT) ? 1 : 0] += 1;
1680
1681 /* Early exit when mixed (could be optional if needed). */
1682 if (r_count_by_select[0] && r_count_by_select[1]) {
1683 r_count_by_select[0] = r_count_by_select[1] = -1;
1684 break;
1685 }
1686 }
1687
1688 BMW_end(&walker);
1689}
1690
1692 int walkercode,
1693 void *start,
1694 const bool select,
1695 BMWFlag flags,
1696 BMWDelimitFlag delimit,
1697 FunctionRef<void(BMElem *)> foreach_elem_fn)
1698{
1699 BMElem *ele;
1700 BMWalker walker;
1701 bool changed = false;
1702
1703 BMW_init(&walker,
1704 bm,
1705 walkercode,
1709 flags,
1711 delimit);
1712
1713 for (ele = static_cast<BMElem *>(BMW_begin(&walker, start)); ele;
1714 ele = static_cast<BMElem *>(BMW_step(&walker)))
1715 {
1716 if (!select) {
1718 }
1720 if (foreach_elem_fn) {
1721 foreach_elem_fn(ele);
1722 }
1723 changed = true;
1724 }
1725 BMW_end(&walker);
1726 return changed;
1727}
1728
1730{
1731 const BMWDelimitFlag delimit = BMWDelimitFlag(RNA_enum_get(op->ptr, "delimit_edge_loop"));
1732 const Main *bmain = CTX_data_main(C);
1733 const Scene *scene = CTX_data_scene(C);
1734 ViewLayer *view_layer = CTX_data_view_layer(C);
1736 *bmain, scene, view_layer, CTX_wm_view3d(C));
1737
1738 for (Object *obedit : objects) {
1741
1742 if (bm->totedgesel == 0) {
1743 continue;
1744 }
1745
1746 BMEdge *eed;
1747 int edindex;
1748 BMIter iter;
1749 int totedgesel = 0;
1750
1751 BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
1753 totedgesel++;
1754 }
1755 }
1756
1757 BMEdge **edarray = MEM_new_array_uninitialized<BMEdge *>(totedgesel, "edge array");
1758 edindex = 0;
1759
1760 BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
1762 edarray[edindex] = eed;
1763 edindex++;
1764 }
1765 }
1766
1767 bool changed = false;
1768 for (edindex = 0; edindex < totedgesel; edindex += 1) {
1769 eed = edarray[edindex];
1770 const bool non_manifold = BM_edge_face_count_is_over(eed, 2);
1771 if (non_manifold) {
1772 changed |= walker_select(bm,
1774 eed,
1775 true,
1777 /* No support for delimiters, see #BMW_init. */
1779 nullptr);
1780 }
1781 else {
1782 changed |= walker_select(
1783 bm, BMW_EDGELOOP, eed, true, BMW_FLAG_TEST_HIDDEN, delimit, nullptr);
1784 }
1785 }
1786 if (changed) {
1789 }
1790
1791 MEM_delete(edarray);
1792
1793 if (changed) {
1794 DEG_id_tag_update(obedit->data, ID_RECALC_SELECT);
1795 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
1796 }
1797 }
1798
1799 return OPERATOR_FINISHED;
1800}
1801
1803{
1804 const BMWDelimitFlag delimit = BMWDelimitFlag(RNA_enum_get(op->ptr, "delimit_edge_ring"));
1805 const Main *bmain = CTX_data_main(C);
1806 const Scene *scene = CTX_data_scene(C);
1807 ViewLayer *view_layer = CTX_data_view_layer(C);
1809 *bmain, scene, view_layer, CTX_wm_view3d(C));
1810
1811 for (Object *obedit : objects) {
1814 if (bm->totedgesel == 0) {
1815 continue;
1816 }
1817
1818 BMEdge *eed;
1819 int edindex;
1820 BMIter iter;
1821 int totedgesel = 0;
1822
1823 BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
1825 totedgesel++;
1826 }
1827 }
1828
1829 BMEdge **edarray = MEM_new_array_uninitialized<BMEdge *>(totedgesel, "edge array");
1830 edindex = 0;
1831
1832 BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
1834 edarray[edindex] = eed;
1835 edindex++;
1836 }
1837 }
1838
1839 bool changed = false;
1840 for (edindex = 0; edindex < totedgesel; edindex += 1) {
1841 eed = edarray[edindex];
1842 changed |= walker_select(
1843 bm, BMW_EDGERING, eed, true, BMW_FLAG_TEST_HIDDEN, delimit, nullptr);
1844 }
1845 if (changed) {
1848 }
1849
1850 MEM_delete(edarray);
1851
1852 if (changed) {
1853 DEG_id_tag_update(obedit->data, ID_RECALC_SELECT);
1854 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
1855 }
1856 }
1857
1858 return OPERATOR_FINISHED;
1859}
1860
1862{
1863 const Main *bmain = CTX_data_main(C);
1864 const Scene *scene = CTX_data_scene(C);
1865 ViewLayer *view_layer = CTX_data_view_layer(C);
1867 *bmain, scene, view_layer, CTX_wm_view3d(C));
1868
1869 bool has_selected_boundary_multi = false;
1870
1871 Array<Vector<BMEdge *>> object_edges(objects.size());
1872
1873 for (const int ob_index : objects.index_range()) {
1874 Object *obedit = objects[ob_index];
1876
1877 if (bm->totedgesel > 0) {
1878 BMEdge *eed;
1879 BMIter iter;
1880 BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
1881 /* Use the TAG to avoid unnecessary work when an entire boundary is already selected,
1882 * there is no need for the walker to walk the boundary for *every* edge.
1883 * Use tags so its only walked once. */
1885
1886 if (!BM_elem_flag_test(eed, BM_ELEM_SELECT)) {
1887 continue;
1888 }
1889 if (!BM_edge_is_boundary(eed)) {
1890 continue;
1891 }
1892 object_edges[ob_index].append(eed);
1893 has_selected_boundary_multi = true;
1894 }
1895 }
1896 }
1897
1898 if (!has_selected_boundary_multi) {
1899 BKE_report(op->reports,
1900 RPT_ERROR,
1901 "At least one boundary edge is needed to make a boundary loop selection");
1902 return OPERATOR_CANCELLED;
1903 }
1904
1905 const bool extend = RNA_boolean_get(op->ptr, "extend");
1906 const BMWDelimitFlag delimit = BMWDelimitFlag(RNA_enum_get(op->ptr, "delimit_edge_loop"));
1907 bool changed_multi = false;
1908
1909 for (const int ob_index : objects.index_range()) {
1910 Object *obedit = objects[ob_index];
1913
1914 bool changed = false;
1915 bool changed_boundary = false;
1916 if (extend == false) {
1918 changed = true;
1919 }
1920
1921 for (BMEdge *e : object_edges[ob_index]) {
1923 /* Already selected, don't handle again. */
1924 continue;
1925 }
1926 changed_boundary |= walker_select(
1927 bm, BMW_EDGELOOP, e, true, BMW_FLAG_TEST_HIDDEN, delimit, [](BMElem *ele) {
1929 });
1930 changed |= changed_boundary;
1931 }
1932 if (changed) {
1933 /* Only flush modes if a boundary selection was made,
1934 * otherwise it's a simple de-select all. */
1935 if (changed_boundary) {
1937 }
1941 changed_multi = true;
1942 }
1943 }
1944
1945 /* If there are any boundary edges selected,
1946 * always return finished so the user can modify the delimiter property in the "redo" panel. */
1947 if (!changed_multi) {
1948 BKE_report(op->reports,
1949 RPT_INFO,
1950 "The selection has not changed. The full delimited loop was already selected");
1951 }
1952 return OPERATOR_FINISHED;
1953}
1954
1956{
1957 /* Identifiers. */
1958 ot->name = "Multi Select Edge Loops";
1959 ot->idname = "MESH_OT_select_edge_loop_multi";
1960 ot->description = "Select loops of connected edges from each selected edge";
1961
1962 /* API callbacks. */
1965
1966 /* Flags. */
1968
1969 /* Properties. */
1971 "delimit_edge_loop",
1974 "Delimit",
1975 "Delimit edge loop selection");
1976}
1977
1979{
1980 /* Identifiers. */
1981 ot->name = "Multi Select Edge Rings";
1982 ot->idname = "MESH_OT_select_edge_ring_multi";
1983 ot->description = "Select rings of connected edges from each selected edge";
1984
1985 /* API callbacks. */
1988
1989 /* Flags. */
1991
1992 /* Properties. */
1994 "delimit_edge_ring",
1997 "Edge Ring Delimit",
1998 "Delimit edge ring selection");
1999}
2000
2002{
2003 /* Identifiers. */
2004 ot->name = "Multi Select Boundary Loops";
2005 ot->idname = "MESH_OT_select_boundary_loop_multi";
2006 ot->description = "Select entire boundary loop of each selected boundary edge";
2007
2008 /* API callbacks. */
2011
2012 /* Flags. */
2014
2015 /* Properties. */
2016 RNA_def_boolean(ot->srna, "extend", true, "Extend", "Extend the selection");
2018 "delimit_edge_loop",
2021 "Delimit",
2022 "Delimit edge loop selection");
2023}
2024
2026
2027/* -------------------------------------------------------------------- */
2030
2032 BMesh *bm, BMEdge *eed, bool select, bool select_clear, BMWDelimitFlag delimit)
2033{
2034 if (select_clear) {
2036 }
2037 walker_select(bm, BMW_FACELOOP, eed, select, BMW_FLAG_TEST_HIDDEN, delimit, nullptr);
2038}
2039
2041 BMesh *bm, BMEdge *eed, bool select, bool select_clear, BMWDelimitFlag delimit)
2042{
2043 bool full_loop = false;
2044
2045 /* Cycle between using delimits and skipping them. */
2046 if ((delimit & BMW_DELIMIT_EDGE_MARK_SEAM) || (delimit & BMW_DELIMIT_EDGE_MARK_SHARP)) {
2047 int count_by_select[2];
2048
2049 /* If up to the delimits is selected toggle the whole loop. */
2050 walker_select_count(bm, BMW_EDGERING, eed, count_by_select, BMW_FLAG_TEST_HIDDEN, delimit);
2051 if (count_by_select[!select] == 0) {
2052 full_loop = true;
2053
2054 /* If the whole loop is selected, toggle back to delimits. */
2056 bm, BMW_EDGERING, eed, count_by_select, BMW_FLAG_TEST_HIDDEN, BMW_DELIMIT_NONE);
2057 if (count_by_select[!select] == 0) {
2058 full_loop = false;
2059 }
2060 }
2061 }
2062 if (select_clear) {
2064 }
2065 if (full_loop) {
2067 }
2068 else {
2069 walker_select(bm, BMW_EDGERING, eed, select, BMW_FLAG_TEST_HIDDEN, delimit, nullptr);
2070 }
2071}
2072
2074 BMEdge *eed,
2075 bool select,
2076 bool select_clear,
2077 bool select_cycle,
2078 BMWDelimitFlag delimit)
2079{
2080 bool full_boundary = false;
2081 bool full_loop = false;
2082 bool non_manifold = BM_edge_face_count_is_over(eed, 2);
2083
2084 /* Cycle between BMW_EDGELOOP / BMW_EDGEBOUNDARY. */
2085 if (select_cycle && BM_edge_is_boundary(eed)) {
2086 int count_by_select[2];
2087
2088 /* If the loops selected toggle the boundaries. */
2089 walker_select_count(bm, BMW_EDGELOOP, eed, count_by_select, BMW_FLAG_TEST_HIDDEN, delimit);
2090 if (count_by_select[!select] == 0) {
2091 full_boundary = true;
2092
2093 /* If the boundaries selected, toggle back to the loop. */
2095 bm, BMW_EDGEBOUNDARY, eed, count_by_select, BMW_FLAG_TEST_HIDDEN, BMW_DELIMIT_NONE);
2096 if (count_by_select[!select] == 0) {
2097 full_boundary = false;
2098 }
2099 }
2100 }
2101 /* Cycle between using delimits and skipping them. */
2102 else if (!non_manifold &&
2103 ((delimit & BMW_DELIMIT_EDGE_MARK_SEAM) || (delimit & BMW_DELIMIT_EDGE_MARK_SHARP)))
2104 {
2105 int count_by_select[2];
2106
2107 /* If up to the delimits is selected toggle the whole loop. */
2108 walker_select_count(bm, BMW_EDGELOOP, eed, count_by_select, BMW_FLAG_TEST_HIDDEN, delimit);
2109 if (count_by_select[!select] == 0) {
2110 full_loop = true;
2111
2112 /* If the whole loop is selected, toggle back to delimits. */
2114 bm, BMW_EDGELOOP, eed, count_by_select, BMW_FLAG_TEST_HIDDEN, BMW_DELIMIT_NONE);
2115 if (count_by_select[!select] == 0) {
2116 full_loop = false;
2117 }
2118 }
2119 }
2120
2121 if (select_clear) {
2123 }
2124
2125 if (full_boundary) {
2128 }
2129 else if (non_manifold) {
2132 eed,
2133 select,
2136 nullptr);
2137 }
2138 else if (full_loop) {
2140 }
2141 else {
2142 walker_select(bm, BMW_EDGELOOP, eed, select, BMW_FLAG_TEST_HIDDEN, delimit, nullptr);
2143 }
2144}
2145
2150 Object *obedit,
2151 BMEdge *eed,
2152 bool select,
2153 bool select_clear,
2154 bool select_cycle,
2155 bool ring,
2156 BMWDelimitFlag delimit)
2157{
2159 if (em->selectmode & SCE_SELECT_FACE) {
2160 mouse_mesh_loop_face(bm, eed, select, select_clear, delimit);
2161 }
2162 else {
2163 if (ring) {
2164 mouse_mesh_loop_edge_ring(bm, eed, select, select_clear, delimit);
2165 }
2166 else {
2167 mouse_mesh_loop_edge(bm, eed, select, select_clear, select_cycle, delimit);
2168 }
2169 }
2170
2173
2174 /* Sets as active, useful for other tools. */
2175 if (select) {
2176 if (em->selectmode & SCE_SELECT_EDGE) {
2178 }
2179 }
2180
2182}
2183
2189 const int mval[2],
2190 Object **r_obedit,
2191 BMEdge **r_eed)
2192{
2193 *r_obedit = nullptr;
2194 *r_eed = nullptr;
2195
2197 vc.mval[0] = mval[0];
2198 vc.mval[1] = mval[1];
2199
2200 BMEditMesh *em_original = vc.em;
2201 const short selectmode = em_original->selectmode;
2202 em_original->selectmode = SCE_SELECT_EDGE;
2203
2205 *vc.bmain, vc.scene, vc.view_layer, vc.v3d);
2206
2207 BMVert *eve = nullptr;
2208 BMEdge *eed = nullptr;
2209 BMFace *efa = nullptr;
2210 int base_index = -1;
2211
2212 if (EDBM_unified_findnearest(&vc, bases, &base_index, &eve, &eed, &efa)) {
2213 *r_obedit = bases[base_index]->object;
2214 *r_eed = eed;
2215 }
2216
2217 em_original->selectmode = selectmode;
2218
2219 return eed != nullptr;
2220}
2221
2223
2224/* -------------------------------------------------------------------- */
2227
2229{
2230 PropertyRNA *prop;
2231
2232 /* Selection properties could be exposed in the UI however they are mainly set via shortcuts,
2233 * so they aren't all that useful to expose. */
2234 prop = RNA_def_boolean(ot->srna, "extend", false, "Extend Select", "Extend the selection");
2236 prop = RNA_def_boolean(ot->srna, "deselect", false, "Deselect", "Remove from the selection");
2238 prop = RNA_def_boolean(ot->srna, "toggle", false, "Toggle Select", "Toggle the selection");
2240
2241 /* Use for redo. */
2242 prop = RNA_def_int(ot->srna, "object_index", -1, -1, INT_MAX, "", "", 0, INT_MAX);
2244 prop = RNA_def_int(ot->srna, "edge_index", -1, -1, INT_MAX, "", "", 0, INT_MAX);
2246
2247 /* For setting active element during redo. */
2248 prop = RNA_def_int(ot->srna, "vert_index", -1, -1, INT_MAX, "", "", 0, INT_MAX);
2250 prop = RNA_def_int(ot->srna, "face_index", -1, -1, INT_MAX, "", "", 0, INT_MAX);
2252}
2253
2255 wmOperator *op,
2256 bool ring,
2257 BMWDelimitFlag delimit)
2258{
2259 const Main *bmain = CTX_data_main(C);
2260 const Scene *scene = CTX_data_scene(C);
2261 ViewLayer *view_layer = CTX_data_view_layer(C);
2262
2263 /* Intentionally wrap negative values so the lookup fails. */
2264 const uint object_index = uint(RNA_int_get(op->ptr, "object_index"));
2265 const uint edge_index = uint(RNA_int_get(op->ptr, "edge_index"));
2266
2268 *bmain, scene, view_layer, nullptr);
2269 if (object_index >= bases.size()) {
2270 return OPERATOR_CANCELLED;
2271 }
2272
2273 Object *obedit = bases[object_index]->object;
2276
2277 if (edge_index >= uint(bm->totedge)) {
2278 return OPERATOR_CANCELLED;
2279 }
2280
2281 BMEdge *eed = BM_edge_at_index_find_or_table(bm, int(edge_index));
2282
2284 return OPERATOR_CANCELLED;
2285 }
2286
2287 const bool extend = RNA_boolean_get(op->ptr, "extend");
2288 const bool deselect = RNA_boolean_get(op->ptr, "deselect");
2289 const bool toggle = RNA_boolean_get(op->ptr, "toggle");
2290
2291 bool select = true;
2292 bool select_clear = false;
2293 bool select_cycle = true;
2294
2295 if (extend == false && deselect == false && toggle == false) {
2296 select_clear = true;
2297 }
2298
2299 if (extend) {
2300 select = true;
2301 }
2302 else if (deselect) {
2303 select = false;
2304 }
2305 else if (select_clear || (BM_elem_flag_test(eed, BM_ELEM_SELECT) == 0)) {
2306 select = true;
2307 }
2308 else if (toggle) {
2309 select = false;
2310 select_cycle = false;
2311 }
2312
2313 /* Clear selection in other objects when not extending.
2314 *
2315 * NOTE(@ideasman42): the current object must *not* be cleared here because the loop selection
2316 * logic checks if edges are already selected to cycle between loop and boundary selection.
2317 * Clearing before that check prevents cycling from triggering, see #153473. */
2318 if (select_clear) {
2319 for (Base *base_iter : bases) {
2320 Object *obedit_iter = base_iter->object;
2321 if (obedit_iter == obedit) {
2322 continue;
2323 }
2324 BMesh *bm_iter = BKE_editmesh_bmesh_get_for_write(obedit_iter);
2325 if (bm_iter->totvertsel == 0) {
2326 continue;
2327 }
2330 }
2331 }
2332
2334 em, obedit, eed, select, select_clear, select_cycle, ring, delimit);
2335
2336 /* Set active element from stored indices (for redo support).
2337 * These indices should only be set when "select" is true,
2338 * add extra check in case script call with confusing arguments. */
2339 if (select) {
2340 const uint vert_index = uint(RNA_int_get(op->ptr, "vert_index"));
2341 const uint face_index = uint(RNA_int_get(op->ptr, "face_index"));
2342
2343 if (vert_index < uint(bm->totvert)) {
2344 BMVert *v = BM_vert_at_index_find_or_table(bm, vert_index);
2347 }
2348 }
2349 else if (face_index < uint(bm->totface)) {
2350 BMFace *f = BM_face_at_index_find_or_table(bm, face_index);
2354 }
2355 }
2356 }
2357
2359
2360 return OPERATOR_FINISHED;
2361}
2362
2364 wmOperator *op,
2365 const wmEvent *event)
2366{
2367 PropertyRNA *prop_object_index = RNA_struct_find_property(op->ptr, "object_index");
2368 PropertyRNA *prop_vert_index = RNA_struct_find_property(op->ptr, "vert_index");
2369 PropertyRNA *prop_edge_index = RNA_struct_find_property(op->ptr, "edge_index");
2370 PropertyRNA *prop_face_index = RNA_struct_find_property(op->ptr, "face_index");
2371
2372 if (RNA_property_is_set(op->ptr, prop_object_index) &&
2373 RNA_property_is_set(op->ptr, prop_edge_index))
2374 {
2375 /* Pass, everything is set. */
2376 }
2377 else {
2379
2380 BMEdge *eed = nullptr;
2381 Object *obedit = nullptr;
2382
2383 if (!edbm_select_loop_or_ring_pick(C, event->mval, &obedit, &eed)) {
2384 return OPERATOR_CANCELLED;
2385 }
2386
2387 BLI_assert(eed != nullptr && obedit != nullptr);
2390
2391 /* Store indices for redo. */
2393
2394 const Main *bmain = CTX_data_main(C);
2395 const Scene *scene = CTX_data_scene(C);
2396 ViewLayer *view_layer = CTX_data_view_layer(C);
2398 *bmain, scene, view_layer, nullptr);
2399 int object_index = -1;
2400 for (const int i : bases.index_range()) {
2401 if (bases[i]->object == obedit) {
2402 object_index = i;
2403 break;
2404 }
2405 }
2406 BLI_assert(object_index >= 0);
2407 RNA_property_int_set(op->ptr, prop_object_index, object_index);
2408 RNA_property_int_set(op->ptr, prop_edge_index, BM_elem_index_get(eed));
2409
2410 /* Never use stale values. */
2411 RNA_property_unset(op->ptr, prop_vert_index);
2412 RNA_property_unset(op->ptr, prop_face_index);
2413
2414 /* Compute active element index for redo support (view-dependent). */
2415 const bool extend = RNA_boolean_get(op->ptr, "extend");
2416 const bool deselect = RNA_boolean_get(op->ptr, "deselect");
2417 const bool toggle = RNA_boolean_get(op->ptr, "toggle");
2418 const bool select = extend || (!deselect && !toggle);
2419
2420 if (select) {
2421 /* NOTE(@ideasman42): This view-context setup may be redundant if `op->type->exec` fails.
2422 * Accepted because this only runs during interactive picking (not performance critical),
2423 * and avoiding this would add complexity without tangible benefit. */
2426 ED_view3d_init_mats_rv3d(obedit, vc.rv3d);
2427 float mvalf[2] = {float(event->mval[0]), float(event->mval[1])};
2428
2429 if (em->selectmode & SCE_SELECT_VERTEX) {
2430 /* Find nearest vert from mouse. */
2431 float v1_co[2], v2_co[2];
2432 float length_1 = FLT_MAX;
2433 float length_2 = FLT_MAX;
2434
2436 vc.region, eed->v1->co, v1_co, V3D_PROJ_TEST_CLIP_NEAR) == V3D_PROJ_RET_OK)
2437 {
2438 length_1 = len_squared_v2v2(mvalf, v1_co);
2439 }
2441 vc.region, eed->v2->co, v2_co, V3D_PROJ_TEST_CLIP_NEAR) == V3D_PROJ_RET_OK)
2442 {
2443 length_2 = len_squared_v2v2(mvalf, v2_co);
2444 }
2445
2447 BMVert *v = (length_1 < length_2) ? eed->v1 : eed->v2;
2448 RNA_property_int_set(op->ptr, prop_vert_index, BM_elem_index_get(v));
2449 }
2450 else if (em->selectmode & SCE_SELECT_FACE) {
2451 /* Find the face of eed which is the nearest of mouse. */
2452 BMFace *f, *efa = nullptr;
2453 BMIter iterf;
2454 float best_dist = FLT_MAX;
2455
2456 BM_ITER_ELEM (f, &iterf, eed, BM_FACES_OF_EDGE) {
2457 float cent[3];
2458 float co[2], tdist;
2459
2463 {
2464 tdist = len_squared_v2v2(mvalf, co);
2465 if (tdist < best_dist) {
2466 best_dist = tdist;
2467 efa = f;
2468 }
2469 }
2470 }
2471 if (efa) {
2473 RNA_property_int_set(op->ptr, prop_face_index, BM_elem_index_get(efa));
2474 }
2475 }
2476 }
2477 }
2478 /* Do the selection via exec. */
2479 return op->type->exec(C, op);
2480}
2481
2483
2484/* -------------------------------------------------------------------- */
2487
2489{
2490 Object *obedit = CTX_data_edit_object(C);
2492 const char *delimit_prop = (em->selectmode & SCE_SELECT_FACE) ? "delimit_face_loop" :
2493 "delimit_edge_loop";
2495 C, op, false, BMWDelimitFlag(RNA_enum_get(op->ptr, delimit_prop)));
2496}
2497
2499{
2500 return edbm_select_loop_or_ring_invoke_impl(C, op, event);
2501}
2502
2504{
2505 Object *obedit = CTX_data_edit_object(C);
2507 const char *delimit_prop = (em->selectmode & SCE_SELECT_FACE) ? "delimit_face_loop" :
2508 "delimit_edge_ring";
2510 C, op, true, BMWDelimitFlag(RNA_enum_get(op->ptr, delimit_prop)));
2511}
2512
2514{
2515 return edbm_select_loop_or_ring_invoke_impl(C, op, event);
2516}
2517
2519 wmOperator * /*op*/,
2520 const PropertyRNA *prop)
2521{
2522 Object *obedit = CTX_data_edit_object(C);
2524 const char *prop_id = RNA_property_identifier(prop);
2525
2526 if (em->selectmode & SCE_SELECT_FACE) {
2527 if (STREQ(prop_id, "delimit_edge_loop")) {
2528 return false;
2529 }
2530 }
2531 else {
2532 if (STREQ(prop_id, "delimit_face_loop")) {
2533 return false;
2534 }
2535 }
2536 return true;
2537}
2538
2540 wmOperator * /*op*/,
2541 const PropertyRNA *prop)
2542{
2543 Object *obedit = CTX_data_edit_object(C);
2545 const char *prop_id = RNA_property_identifier(prop);
2546
2547 if (em->selectmode & SCE_SELECT_FACE) {
2548 if (STREQ(prop_id, "delimit_edge_ring")) {
2549 return false;
2550 }
2551 }
2552 else {
2553 if (STREQ(prop_id, "delimit_face_loop")) {
2554 return false;
2555 }
2556 }
2557 return true;
2558}
2559
2561{
2562 /* Identifiers. */
2563 ot->name = "Loop Select";
2564 ot->idname = "MESH_OT_loop_select";
2565 ot->description = "Select a loop of connected edges";
2566
2567 /* API callbacks. */
2572
2573 /* Flags. */
2575
2576 /* Properties. */
2577
2578 /* Only shown when face-select mode is disabled. */
2580 "delimit_edge_loop",
2583 "Delimit",
2584 "Delimit edge loop selection");
2585 /* Only shown when face-select mode is enabled. */
2587 "delimit_face_loop",
2589 0,
2590 "Face Loop Delimit",
2591 "Delimit face loop selection");
2592
2594}
2595
2597{
2598 /* Identifiers. */
2599 ot->name = "Edge Ring Select";
2600 ot->idname = "MESH_OT_edgering_select";
2601 ot->description = "Select an edge ring";
2602
2603 /* API callbacks. */
2608
2609 /* Flags. */
2611
2612 /* Properties. */
2614 "delimit_edge_ring",
2617 "Edge Ring Delimit",
2618 "Delimit edge ring selection");
2619 /* Only shown when face-select mode is enabled. */
2621 "delimit_face_loop",
2623 0,
2624 "Face Loop Delimit",
2625 "Delimit face loop selection");
2626
2628}
2629
2631
2632/* -------------------------------------------------------------------- */
2635
2637{
2638 const Main *bmain = CTX_data_main(C);
2639 const Scene *scene = CTX_data_scene(C);
2640 ViewLayer *view_layer = CTX_data_view_layer(C);
2641 int action = RNA_enum_get(op->ptr, "action");
2642
2644 *bmain, scene, view_layer, CTX_wm_view3d(C));
2645
2646 if (action == SEL_TOGGLE) {
2647 action = SEL_SELECT;
2648 for (Object *obedit : objects) {
2650 if (bm->totvertsel || bm->totedgesel || bm->totfacesel) {
2651 action = SEL_DESELECT;
2652 break;
2653 }
2654 }
2655 }
2656
2657 for (Object *obedit : objects) {
2660 switch (action) {
2661 case SEL_SELECT:
2663 break;
2664 case SEL_DESELECT:
2666 break;
2667 case SEL_INVERT:
2668 EDBM_select_swap(em, bm);
2670 break;
2671 }
2672
2673 DEG_id_tag_update(obedit->data, ID_RECALC_SELECT);
2674 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
2675 }
2676
2677 return OPERATOR_FINISHED;
2678}
2679
2681{
2682 /* Identifiers. */
2683 ot->name = "(De)select All";
2684 ot->idname = "MESH_OT_select_all";
2685 ot->description = "(De)select all vertices, edges or faces";
2686
2687 /* API callbacks. */
2690
2691 /* Flags. */
2693
2695}
2696
2698
2699/* -------------------------------------------------------------------- */
2702
2704{
2705 const Main *bmain = CTX_data_main(C);
2706 const Scene *scene = CTX_data_scene(C);
2707 ViewLayer *view_layer = CTX_data_view_layer(C);
2709 *bmain, scene, view_layer, CTX_wm_view3d(C));
2710
2711 for (Object *obedit : objects) {
2714 continue;
2715 }
2716
2717 DEG_id_tag_update(obedit->data, ID_RECALC_SELECT);
2718 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
2719 }
2720
2721 return OPERATOR_FINISHED;
2722}
2723
2725{
2726 /* Identifiers. */
2727 ot->name = "Select Interior Faces";
2728 ot->idname = "MESH_OT_select_interior_faces";
2729 ot->description = "Select faces where all edges have more than 2 face users";
2730
2731 /* API callbacks. */
2734
2735 /* Flags. */
2737}
2738
2740
2741/* -------------------------------------------------------------------- */
2747
2748bool EDBM_select_pick(bContext *C, const int mval[2], const SelectPick_Params &params)
2749{
2750 int base_index_active = -1;
2751 BMVert *eve = nullptr;
2752 BMEdge *eed = nullptr;
2753 BMFace *efa = nullptr;
2754
2755 /* Setup view context for argument to callbacks. */
2757 vc.mval[0] = mval[0];
2758 vc.mval[1] = mval[1];
2759
2761 *vc.bmain, vc.scene, vc.view_layer, vc.v3d);
2762
2763 bool changed = false;
2764 bool found = unified_findnearest(&vc, bases, &base_index_active, &eve, &eed, &efa);
2765
2766 if (params.sel_op == SEL_OP_SET) {
2767 BMElem *ele = efa ? reinterpret_cast<BMElem *>(efa) :
2768 (eed ? reinterpret_cast<BMElem *>(eed) : reinterpret_cast<BMElem *>(eve));
2769 if ((found && params.select_passthrough) && BM_elem_flag_test(ele, BM_ELEM_SELECT)) {
2770 found = false;
2771 }
2772 else if (found || params.deselect_all) {
2773 /* Deselect everything. */
2774 for (Base *base_iter : bases) {
2775 Object *ob_iter = base_iter->object;
2780 }
2781 changed = true;
2782 }
2783 }
2784
2785 if (found) {
2786 Base *basact = bases[base_index_active];
2788 Object *obedit = vc.obedit;
2789 BMEditMesh *em = vc.em;
2791
2792 const int cd_loop_uv_offset = CustomData_get_offset(&bm->ldata, CD_PROP_FLOAT2);
2793 const BMUVSelectPickParams uv_pick_params = {
2794 /*cd_loop_uv_offset*/ cd_loop_uv_offset,
2796 };
2797
2798 if (efa) {
2799 switch (params.sel_op) {
2800 case SEL_OP_ADD: {
2802
2803 /* Work-around: deselect first, so we can guarantee it will
2804 * be active even if it was already selected. */
2806 BM_face_select_set(bm, efa, false);
2808 BM_face_select_set(bm, efa, true);
2809 if (bm->uv_select_sync_valid) {
2810 BM_face_uvselect_set_pick(bm, efa, true, uv_pick_params);
2811 }
2812 break;
2813 }
2814 case SEL_OP_SUB: {
2816 BM_face_select_set(bm, efa, false);
2817 if (bm->uv_select_sync_valid) {
2818 BM_face_uvselect_set_pick(bm, efa, false, uv_pick_params);
2819 }
2820 break;
2821 }
2822 case SEL_OP_XOR: {
2824 if (!BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
2826 BM_face_select_set(bm, efa, true);
2827 if (bm->uv_select_sync_valid) {
2828 BM_face_uvselect_set_pick(bm, efa, true, uv_pick_params);
2829 }
2830 }
2831 else {
2833 BM_face_select_set(bm, efa, false);
2834 if (bm->uv_select_sync_valid) {
2835 BM_face_uvselect_set_pick(bm, efa, false, uv_pick_params);
2836 }
2837 }
2838 break;
2839 }
2840 case SEL_OP_SET: {
2842 if (!BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
2844 BM_face_select_set(bm, efa, true);
2845 }
2846 /* UV select will have been cleared. */
2847 break;
2848 }
2849 case SEL_OP_AND: {
2850 BLI_assert_unreachable(); /* Doesn't make sense for picking. */
2851 break;
2852 }
2853 }
2854 }
2855 else if (eed) {
2856
2857 switch (params.sel_op) {
2858 case SEL_OP_ADD: {
2859 /* Work-around: deselect first, so we can guarantee it will
2860 * be active even if it was already selected. */
2862 BM_edge_select_set(bm, eed, false);
2864 BM_edge_select_set(bm, eed, true);
2865 if (bm->uv_select_sync_valid) {
2866 BM_edge_uvselect_set_pick(bm, eed, true, uv_pick_params);
2867 }
2868 break;
2869 }
2870 case SEL_OP_SUB: {
2872 BM_edge_select_set(bm, eed, false);
2873 if (bm->uv_select_sync_valid) {
2874 BM_edge_uvselect_set_pick(bm, eed, false, uv_pick_params);
2875 }
2876 break;
2877 }
2878 case SEL_OP_XOR: {
2879 if (!BM_elem_flag_test(eed, BM_ELEM_SELECT)) {
2881 BM_edge_select_set(bm, eed, true);
2882 if (bm->uv_select_sync_valid) {
2883 BM_edge_uvselect_set_pick(bm, eed, true, uv_pick_params);
2884 }
2885 }
2886 else {
2888 BM_edge_select_set(bm, eed, false);
2889 if (bm->uv_select_sync_valid) {
2890 BM_edge_uvselect_set_pick(bm, eed, false, uv_pick_params);
2891 }
2892 }
2893 break;
2894 }
2895 case SEL_OP_SET: {
2896 if (!BM_elem_flag_test(eed, BM_ELEM_SELECT)) {
2898 BM_edge_select_set(bm, eed, true);
2899 }
2900 break;
2901 }
2902 case SEL_OP_AND: {
2903 BLI_assert_unreachable(); /* Doesn't make sense for picking. */
2904 break;
2905 }
2906 }
2907 }
2908 else if (eve) {
2909 switch (params.sel_op) {
2910 case SEL_OP_ADD: {
2911 /* Work-around: deselect first, so we can guarantee it will
2912 * be active even if it was already selected. */
2914 BM_vert_select_set(bm, eve, false);
2916 BM_vert_select_set(bm, eve, true);
2917 if (bm->uv_select_sync_valid) {
2918 BM_vert_uvselect_set_pick(bm, eve, true, uv_pick_params);
2919 }
2920 break;
2921 }
2922 case SEL_OP_SUB: {
2924 BM_vert_select_set(bm, eve, false);
2925 if (bm->uv_select_sync_valid) {
2926 BM_vert_uvselect_set_pick(bm, eve, false, uv_pick_params);
2927 }
2928 break;
2929 }
2930 case SEL_OP_XOR: {
2931 if (!BM_elem_flag_test(eve, BM_ELEM_SELECT)) {
2933 BM_vert_select_set(bm, eve, true);
2934 if (bm->uv_select_sync_valid) {
2935 BM_vert_uvselect_set_pick(bm, eve, true, uv_pick_params);
2936 }
2937 }
2938 else {
2940 BM_vert_select_set(bm, eve, false);
2941 if (bm->uv_select_sync_valid) {
2942 BM_vert_uvselect_set_pick(bm, eve, false, uv_pick_params);
2943 }
2944 }
2945 break;
2946 }
2947 case SEL_OP_SET: {
2948 if (!BM_elem_flag_test(eve, BM_ELEM_SELECT)) {
2950 BM_vert_select_set(bm, eve, true);
2951 }
2952 break;
2953 }
2954 case SEL_OP_AND: {
2955 BLI_assert_unreachable(); /* Doesn't make sense for picking. */
2956 break;
2957 }
2958 }
2959 }
2960
2962 if (bm->uv_select_sync_valid) {
2964 }
2965
2966 if (efa) {
2968 em->mat_nr = efa->mat_nr;
2969 }
2970
2971 /* Changing active object is handy since it allows us to
2972 * switch UV layers, vgroups for eg. */
2974 if (BKE_view_layer_active_base_get(vc.view_layer) != basact) {
2976 }
2977
2980
2981 changed = true;
2982 }
2983
2984 return changed;
2985}
2986
2988
2989/* -------------------------------------------------------------------- */
2992
2993static void edbm_strip_selections(BMesh *bm, const short selectmode)
2994{
2995 BMEditSelection *ese, *nextese;
2996
2997 if (!(selectmode & SCE_SELECT_VERTEX)) {
2998 ese = bm->selected.first();
2999 while (ese) {
3000 nextese = ese->next;
3001 if (ese->htype == BM_VERT) {
3002 BLI_freelinkN(&(bm->selected), ese);
3003 }
3004 ese = nextese;
3005 }
3006 }
3007 if (!(selectmode & SCE_SELECT_EDGE)) {
3008 ese = bm->selected.first();
3009 while (ese) {
3010 nextese = ese->next;
3011 if (ese->htype == BM_EDGE) {
3012 BLI_freelinkN(&(bm->selected), ese);
3013 }
3014 ese = nextese;
3015 }
3016 }
3017 if (!(selectmode & SCE_SELECT_FACE)) {
3018 ese = bm->selected.first();
3019 while (ese) {
3020 nextese = ese->next;
3021 if (ese->htype == BM_FACE) {
3022 BLI_freelinkN(&(bm->selected), ese);
3023 }
3024 ese = nextese;
3025 }
3026 }
3027}
3028
3029void EDBM_selectmode_set(BMEditMesh *em, BMesh *bm, const short selectmode)
3030{
3031 BMVert *eve;
3032 BMEdge *eed;
3033 BMFace *efa;
3034 BMIter iter;
3035
3036 const short selectmode_prev = em->selectmode;
3037 em->selectmode = selectmode;
3038 bm->selectmode = selectmode;
3039
3040 /* Strip stored selection isn't relevant to the new mode. */
3041 edbm_strip_selections(bm, selectmode);
3042
3043 if (bm->totvertsel == 0 && bm->totedgesel == 0 && bm->totfacesel == 0) {
3044 return;
3045 }
3046
3047 if (em->selectmode & SCE_SELECT_VERTEX) {
3048 if (bm->totvertsel) {
3050 }
3051 }
3052 else if (em->selectmode & SCE_SELECT_EDGE) {
3053 /* Deselect vertices, and select again based on edge select. */
3054 BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
3055 BM_vert_select_set(bm, eve, false);
3056 }
3057
3058 if (bm->totedgesel) {
3059 BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
3061 BM_edge_select_set(bm, eed, true);
3062 }
3063 }
3064
3065 /* Selects faces based on edge status. */
3067 }
3068 }
3069 else if (em->selectmode & SCE_SELECT_FACE) {
3070 /* Deselect edges, and select again based on face select. */
3071 BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
3072 BM_edge_select_set(bm, eed, false);
3073 }
3074
3075 if (bm->totfacesel) {
3076 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
3078 BM_face_select_set(bm, efa, true);
3079 }
3080 }
3081 }
3082 }
3083
3084 if (bm->uv_select_sync_valid) {
3086 /* NOTE(@ideasman42): this could/should use the "sticky" tool setting.
3087 * Although in practice it's OK to assume "connected" sticky in this case. */
3088 const int cd_loop_uv_offset = CustomData_get_offset(&bm->ldata, CD_PROP_FLOAT2);
3089 BM_mesh_uvselect_mode_flush_update(bm, selectmode_prev, selectmode, cd_loop_uv_offset);
3090 }
3091 else {
3092 /* Always clear for face-only selection, because any discrepancy
3093 * between UV/Viewport selection caused by de-selecting verts/edges
3094 * can't be represented in face-only selection mode. */
3096 }
3097 }
3098}
3099
3100void EDBM_selectmode_convert(BMesh *bm, const short selectmode_old, const short selectmode_new)
3101{
3102 /* NOTE: it's important only the selection modes passed in a re used,
3103 * not the meshes current selection mode because this is called when the
3104 * selection mode is being manipulated (see: #EDBM_selectmode_toggle_multi). */
3105
3106 BMVert *eve;
3107 BMEdge *eed;
3108 BMFace *efa;
3109 BMIter iter;
3110
3111 /* First tag-to-select, then select.
3112 * This avoids a feedback loop. */
3113
3114 /* Have to find out what the selection-mode was previously. */
3115 if (selectmode_old == SCE_SELECT_VERTEX) {
3116 if (bm->totvertsel == 0) {
3117 /* Pass. */
3118 }
3119 else if (selectmode_new == SCE_SELECT_EDGE) {
3120 /* Flush up (vert -> edge). */
3121
3122 /* Select all edges associated with every selected vert. */
3123 BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
3125 }
3126
3127 BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
3128 if (BM_elem_flag_test(eed, BM_ELEM_TAG)) {
3129 BM_edge_select_set(bm, eed, true);
3130 }
3131 }
3132 }
3133 else if (selectmode_new == SCE_SELECT_FACE) {
3134 /* Flush up (vert -> face). */
3135
3136 /* Select all faces associated with every selected vert. */
3137 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
3139 }
3140
3141 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
3142 if (BM_elem_flag_test(efa, BM_ELEM_TAG)) {
3143 BM_face_select_set(bm, efa, true);
3144 }
3145 }
3146 }
3147 }
3148 else if (selectmode_old == SCE_SELECT_EDGE) {
3149 if (bm->totedgesel == 0) {
3150 /* Pass. */
3151 }
3152 else if (selectmode_new == SCE_SELECT_FACE) {
3153 /* Flush up (edge -> face). */
3154
3155 /* Select all faces associated with every selected edge. */
3156 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
3158 }
3159
3160 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
3161 if (BM_elem_flag_test(efa, BM_ELEM_TAG)) {
3162 BM_face_select_set(bm, efa, true);
3163 }
3164 }
3165 }
3166 else if (selectmode_new == SCE_SELECT_VERTEX) {
3167 /* Flush down (edge -> vert). */
3168
3169 BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
3171 BM_vert_select_set(bm, eve, false);
3172 }
3173 }
3174 /* Deselect edges without both verts selected. */
3176 }
3177 }
3178 else if (selectmode_old == SCE_SELECT_FACE) {
3179 if (bm->totfacesel == 0) {
3180 /* Pass. */
3181 }
3182 else if (selectmode_new == SCE_SELECT_EDGE) {
3183 /* Flush down (face -> edge). */
3184
3185 BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
3187 BM_edge_select_set(bm, eed, false);
3188 }
3189 }
3190 /* Deselect faces without edges selected. */
3192 }
3193 else if (selectmode_new == SCE_SELECT_VERTEX) {
3194 /* Flush down (face -> vert). */
3195
3196 BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
3198 BM_vert_select_set(bm, eve, false);
3199 }
3200 }
3201 /* Deselect faces without verts selected. */
3203 }
3204 }
3205}
3206
3208 const short selectmode_toggle,
3209 const int action,
3210 const bool use_extend,
3211 const bool use_expand)
3212{
3214 const Main *bmain = CTX_data_main(C);
3215 Scene *scene = CTX_data_scene(C);
3216 ViewLayer *view_layer = CTX_data_view_layer(C);
3218 bool ret = false;
3219
3220 short selectmode_new;
3221 /* Avoid mixing up the active/iterable edit-mesh by limiting its scope. */
3222 {
3223 Object *obedit = CTX_data_edit_object(C);
3224 BMEditMesh *em = nullptr;
3225
3226 if (obedit && obedit->type == OB_MESH) {
3227 em = BKE_editmesh_from_object(obedit);
3228 }
3229
3230 if (em == nullptr) {
3231 return ret;
3232 }
3233
3234 selectmode_new = em->selectmode;
3235 }
3236 /* Assign before the new value is modified. */
3237 const short selectmode_old = selectmode_new;
3238
3239 bool only_update = false;
3240 switch (action) {
3241 case -1:
3242 /* Already set. */
3243 break;
3244 case 0: /* Disable. */
3245 /* Check we have something to do. */
3246 if ((selectmode_old & selectmode_toggle) == 0) {
3247 only_update = true;
3248 break;
3249 }
3250 /* Can't disable this flag if its the only one set. */
3251 if (selectmode_old == selectmode_toggle) {
3252 only_update = true;
3253 break;
3254 }
3255 selectmode_new &= ~selectmode_toggle;
3256 break;
3257 case 1: /* Enable. */
3258 /* Check we have something to do. */
3259 if ((selectmode_old & selectmode_toggle) != 0) {
3260 only_update = true;
3261 break;
3262 }
3263 selectmode_new |= selectmode_toggle;
3264 break;
3265 case 2: /* Toggle. */
3266 /* Can't disable this flag if its the only one set. */
3267 if (selectmode_old == selectmode_toggle) {
3268 only_update = true;
3269 break;
3270 }
3271 selectmode_new ^= selectmode_toggle;
3272 break;
3273 default:
3274 BLI_assert(0);
3275 break;
3276 }
3277 BLI_assert(selectmode_new != 0);
3278
3280 *bmain, scene, view_layer, CTX_wm_view3d(C));
3281
3282 if (only_update) {
3283 for (Object *ob_iter : objects) {
3284 BMEditMesh *em_iter = BKE_editmesh_from_object(ob_iter);
3285 em_iter->selectmode = selectmode_new;
3286 }
3287
3288 return false;
3289 }
3290
3291 /* WARNING: unfortunately failing to ensure this causes problems in *some* cases.
3292 * Adding UV data has negative performance impacts, but failing to do this means
3293 * switching to the UV editor *might* should strange selection.
3294 * Since we can't know if users will proceed to do UV editing after switching modes,
3295 * ensure the UV data.
3296 *
3297 * Even though the data is added, it's only added if it's needed,
3298 * so selecting all/none or when there are no UV's.
3299 *
3300 * Failing to do this means switching from face to vertex selection modes
3301 * will leave vertices on adjacent islands selected - which seems like a bug. */
3302 bool use_uv_select_ensure = false;
3303
3304 /* Only do this when sync-select is enabled so users can have better
3305 * performance when editing high poly meshes. */
3306 if (ts->uv_flag & UV_FLAG_SELECT_SYNC) {
3307 /* Only when flushing down. */
3308 if ((bitscan_forward_i(selectmode_new) < bitscan_forward_i(selectmode_old))) {
3309 use_uv_select_ensure = true;
3310 }
3311 }
3312
3313 if (use_extend == false || selectmode_new == 0) {
3314 if (use_expand) {
3315 const short selectmode_max = highest_order_bit_s(selectmode_old);
3316 for (Object *ob_iter : objects) {
3317 BMesh *bm_iter = BKE_editmesh_bmesh_get_for_write(ob_iter);
3318 EDBM_selectmode_convert(bm_iter, selectmode_max, selectmode_toggle);
3319 /* NOTE: This could be supported, but converting UV's too is reasonably complicated.
3320 * This can be considered a low priority TODO. */
3321 EDBM_uvselect_clear(bm_iter);
3322 }
3323 use_uv_select_ensure = false;
3324 }
3325 }
3326
3327 switch (selectmode_toggle) {
3328 case SCE_SELECT_VERTEX:
3329 if (use_extend == false || selectmode_new == 0) {
3330 selectmode_new = SCE_SELECT_VERTEX;
3331 }
3332 ret = true;
3333 break;
3334 case SCE_SELECT_EDGE:
3335 if (use_extend == false || selectmode_new == 0) {
3336 selectmode_new = SCE_SELECT_EDGE;
3337 }
3338 ret = true;
3339 break;
3340 case SCE_SELECT_FACE:
3341 if (use_extend == false || selectmode_new == 0) {
3342 selectmode_new = SCE_SELECT_FACE;
3343 }
3344 ret = true;
3345 break;
3346 default:
3347 BLI_assert(0);
3348 break;
3349 }
3350
3351 if (ret == true) {
3352 BLI_assert(selectmode_new != 0);
3353 for (Object *ob_iter : objects) {
3354 BMEditMesh *em_iter = BKE_editmesh_from_object(ob_iter);
3355 BMesh *bm_iter = BKE_editmesh_bmesh_get_for_write(ob_iter);
3356
3357 if (use_uv_select_ensure) {
3358 if (BM_mesh_select_is_mixed(bm_iter)) {
3360 }
3361 else {
3362 EDBM_uvselect_clear(bm_iter);
3363 }
3364 }
3365
3366 EDBM_selectmode_set(em_iter, bm_iter, selectmode_new);
3368 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, ob_iter->data);
3369 }
3370
3371 ts->selectmode = selectmode_new;
3374 }
3375
3376 return ret;
3377}
3378
3379bool EDBM_selectmode_set_multi_ex(Scene *scene, Span<Object *> objects, const short selectmode)
3380{
3381 ToolSettings *ts = scene->toolsettings;
3382 bool changed = false;
3383 bool changed_toolsettings = false;
3384
3385 if (ts->selectmode != selectmode) {
3386 ts->selectmode = selectmode;
3387 changed_toolsettings = true;
3388 }
3389
3390 for (Object *ob_iter : objects) {
3391 BMEditMesh *em_iter = BKE_editmesh_from_object(ob_iter);
3392 BMesh *bm_iter = BKE_editmesh_bmesh_get_for_write(ob_iter);
3393 if (em_iter->selectmode == selectmode) {
3394 continue;
3395 }
3396 EDBM_selectmode_set(em_iter, bm_iter, selectmode);
3398 WM_main_add_notifier(NC_GEOM | ND_SELECT, ob_iter->data);
3399 changed = true;
3400 }
3401
3402 if (changed_toolsettings) {
3405 }
3406
3407 return changed || changed_toolsettings;
3408}
3409
3410bool EDBM_selectmode_set_multi(bContext *C, const short selectmode)
3411{
3412 BLI_assert(selectmode != 0);
3413 const Main *bmain = CTX_data_main(C);
3414 Scene *scene = CTX_data_scene(C);
3415 ViewLayer *view_layer = CTX_data_view_layer(C);
3416 BKE_view_layer_synced_ensure(*bmain, scene, view_layer);
3417 Object *obact = BKE_view_layer_active_object_get(view_layer);
3418 if (!(obact && (obact->type == OB_MESH) && (obact->mode & OB_MODE_EDIT) &&
3419 (BKE_editmesh_from_object(obact) != nullptr)))
3420 {
3421 return false;
3422 }
3423
3425 *bmain, scene, view_layer, CTX_wm_view3d(C));
3426
3427 return EDBM_selectmode_set_multi_ex(scene, objects, selectmode);
3428}
3429
3443{
3444 if (objects.size() <= 1) {
3445 return false;
3446 }
3447
3448 bool changed = false;
3449 BMEditMesh *em_active = BKE_editmesh_from_object(objects[0]);
3450 for (Object *obedit : objects) {
3453 if (em_active->selectmode == em->selectmode) {
3454 continue;
3455 }
3456 EDBM_selectmode_set(em, bm, em_active->selectmode);
3457 changed = true;
3458
3460 WM_main_add_notifier(NC_GEOM | ND_SELECT, obedit->data);
3461 }
3462
3463 return changed;
3464}
3465
3467 BMEditMesh *em,
3468 BMesh *bm,
3469 const short selectmode_disable,
3470 const short selectmode_fallback)
3471{
3472 /* Not essential, but switch out of vertex mode since the
3473 * selected regions won't be nicely isolated after flushing. */
3474 if (em->selectmode & selectmode_disable) {
3475 const short selectmode = (em->selectmode == selectmode_disable) ?
3476 selectmode_fallback :
3477 (em->selectmode & ~selectmode_disable);
3478 scene->toolsettings->selectmode = selectmode;
3479 EDBM_selectmode_set(em, bm, selectmode);
3480
3482
3483 return true;
3484 }
3485 return false;
3486}
3487
3489
3490/* -------------------------------------------------------------------- */
3493
3494bool EDBM_deselect_by_material(BMesh *bm, const short index, const bool select)
3495{
3496 BMIter iter;
3497 BMFace *efa;
3498 bool changed = false;
3499
3500 /* Face only mode can re-create the UV selection from the mesh. */
3501 const bool use_uv_select = bm->uv_select_sync_valid &&
3503 /* Only needed to update the UV selection. */
3504 Vector<BMFace *> faces;
3505
3506 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
3508 continue;
3509 }
3510 if (efa->mat_nr == index) {
3511 changed = true;
3513 if (use_uv_select) {
3514 faces.append(efa);
3515 }
3516 }
3517 }
3518
3519 if (changed) {
3521
3522 const int cd_loop_uv_offset = use_uv_select ?
3524 -1;
3525 if (cd_loop_uv_offset == -1) {
3527 }
3528 else {
3529 /* The sticky mode isn't available here, assume shared UV's are selected. */
3530 const BMUVSelectPickParams uv_pick_params = {
3531 /*cd_loop_uv_offset*/ cd_loop_uv_offset,
3532 /*shared*/ true,
3533 };
3535 bm, select, uv_pick_params, Span<BMVert *>(), Span<BMEdge *>(), faces.as_span());
3536 }
3537 }
3538 BLI_assert(use_uv_select || faces.is_empty());
3539 return changed;
3540}
3541
3542void EDBM_select_toggle_all(BMesh *bm) /* Exported for UV. */
3543{
3544 if (bm->totvertsel || bm->totedgesel || bm->totfacesel) {
3546 }
3547 else {
3549 }
3550}
3551
3552void EDBM_select_swap(BMEditMesh *em, BMesh *bm) /* Exported for UV. */
3553{
3554 BMIter iter;
3555 BMVert *eve;
3556 BMEdge *eed;
3557 BMFace *efa;
3558
3560 BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
3562 continue;
3563 }
3565 }
3566 }
3567 else if (em->selectmode & SCE_SELECT_EDGE) {
3568 BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
3570 continue;
3571 }
3573 }
3574 }
3575 else {
3576 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
3578 continue;
3579 }
3581 }
3582 }
3583
3584 /* Don't attempt to handle UV selection while inverting from the 3D viewport.
3585 * The inverted selection of a partially selected UV causes the connected vertex to remain
3586 * selected, which you won't want unless inverting from the UV editor, see #152801. */
3588}
3589
3591{
3592 bool changed_multi = false;
3593 for (Base *base_iter : bases) {
3594 Object *ob_iter = base_iter->object;
3595 BMesh *bm_iter = BKE_editmesh_bmesh_get_for_write(ob_iter);
3596
3597 if (bm_iter->totvertsel == 0) {
3598 continue;
3599 }
3600
3603 changed_multi = true;
3604 }
3605 return changed_multi;
3606}
3607
3616
3618 const Span<Base *> bases,
3619 const short selectmode_disable,
3620 const short selectmode_fallback)
3621{
3622 bool changed_multi = false;
3623 for (Base *base_iter : bases) {
3624 Object *ob_iter = base_iter->object;
3625 BMEditMesh *em_iter = BKE_editmesh_from_object(ob_iter);
3627 if (EDBM_selectmode_disable(scene, em_iter, bm, selectmode_disable, selectmode_fallback)) {
3628 changed_multi = true;
3629 }
3630 }
3631 return changed_multi;
3632}
3633
3635 const short selectmode_disable,
3636 const short selectmode_fallback)
3637{
3638 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
3639 Scene *scene = CTX_data_scene(C);
3640 ViewContext vc = ED_view3d_viewcontext_init(C, depsgraph);
3642 *vc.bmain, vc.scene, vc.view_layer, nullptr);
3643 return EDBM_selectmode_disable_multi_ex(scene, bases, selectmode_disable, selectmode_fallback);
3644}
3645
3647
3648/* -------------------------------------------------------------------- */
3659
3665
3666static bool bm_interior_loop_filter_fn(const BMLoop *l, void * /*user_data*/)
3667{
3669 return false;
3670 }
3671 return true;
3672}
3674 int face_index,
3675 BMLoop *r_l_pair[2])
3676{
3677
3678 BMLoop *l_iter = e->l;
3679 int loop_index = 0;
3680 do {
3681 BMFace *f = l_iter->f;
3682 int i = BM_elem_index_get(f);
3683 if (!ELEM(i, -1, face_index)) {
3684 if (loop_index == 2) {
3685 return false;
3686 }
3687 r_l_pair[loop_index++] = l_iter;
3688 }
3689 } while ((l_iter = l_iter->radial_next) != e->l);
3690 return (loop_index == 2);
3691}
3692
3697static float bm_interior_face_group_calc_cost(ListBaseT<BMFaceLink> *ls, const float *edge_lengths)
3698{
3699 /* Dividing by the area is important so larger face groups (which will become the outer shell)
3700 * aren't detected as having a high cost. */
3701 float area = 0.0f;
3702 float cost = 0.0f;
3703 bool found = false;
3704 for (BMFaceLink &f_link : *ls) {
3705 BMFace *f = f_link.face;
3706 area += f_link.area;
3707 int i = BM_elem_index_get(f);
3708 BLI_assert(i != -1);
3709 BMLoop *l_iter, *l_first;
3710 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
3711 do {
3712 if (BM_elem_flag_test(l_iter->e, BM_ELEM_TAG)) {
3713 float cost_test = 0.0f;
3714 int cost_count = 0;
3715 /* All other faces. */
3716 BMLoop *l_radial_iter = l_iter;
3717 do {
3718 int i_other = BM_elem_index_get(l_radial_iter->f);
3719 if (!ELEM(i_other, -1, i)) {
3720 float angle = angle_normalized_v3v3(f->no, l_radial_iter->f->no);
3721 /* Ignore face direction since in the case on non-manifold faces connecting edges,
3722 * the face flipping may not be meaningful. */
3723 if (angle > DEG2RADF(90)) {
3724 angle = DEG2RADF(180) - angle;
3725 }
3726 /* Avoid calculating it inline, pass in pre-calculated edge lengths. */
3727#if 0
3728 cost_test += BM_edge_calc_length(l_iter->e) * angle;
3729#else
3730 BLI_assert(edge_lengths[BM_elem_index_get(l_iter->e)] != -1.0f);
3731 cost_test += edge_lengths[BM_elem_index_get(l_iter->e)] * angle;
3732#endif
3733 cost_count += 1;
3734 }
3735 } while ((l_radial_iter = l_radial_iter->radial_next) != l_iter);
3736
3737 if (cost_count >= 2) {
3738 cost += cost_test;
3739 found = true;
3740 }
3741 }
3742 } while ((l_iter = l_iter->next) != l_first);
3743 }
3744 return found ? cost / area : FLT_MAX;
3745}
3746
3748{
3749 BMIter iter;
3750 bool changed = false;
3751
3752 float *edge_lengths = MEM_new_array_uninitialized<float>(bm->totedge, __func__);
3753
3754 {
3755 bool has_nonmanifold = false;
3756 BMEdge *e;
3757 int i;
3758 BM_ITER_MESH_INDEX (e, &iter, bm, BM_EDGES_OF_MESH, i) {
3759 const bool is_over = BM_edge_face_count_is_over(e, 2);
3760 if (is_over) {
3762 has_nonmanifold = true;
3763 edge_lengths[i] = BM_edge_calc_length(e);
3764 }
3765 else {
3767 edge_lengths[i] = -1.0;
3768 }
3769
3770 BM_elem_index_set(e, i); /* set_inline */
3771 }
3773
3774 if (has_nonmanifold == false) {
3775 MEM_delete(edge_lengths);
3776 return false;
3777 }
3778 }
3779
3780 /* Group variables. */
3781 int (*fgroup_index)[2];
3782 int fgroup_len;
3783
3784 int *fgroup_array = MEM_new_array_uninitialized<int>(bm->totface, __func__);
3785 fgroup_len = BM_mesh_calc_face_groups(
3786 bm, fgroup_array, &fgroup_index, bm_interior_loop_filter_fn, nullptr, nullptr, 0, BM_EDGE);
3787
3788 int *fgroup_recalc_stack = MEM_new_array_uninitialized<int>(fgroup_len, __func__);
3789 STACK_DECLARE(fgroup_recalc_stack);
3790 STACK_INIT(fgroup_recalc_stack, fgroup_len);
3791
3793
3794 {
3795 BMFace *f;
3796 BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
3797 BM_elem_index_set(f, -1); /* set_dirty! */
3798 }
3799 }
3801
3802 ListBaseT<BMFaceLink> *fgroup_listbase = MEM_new_array_zeroed<ListBaseT<BMFaceLink>>(fgroup_len,
3803 __func__);
3804 BMFaceLink *f_link_array = MEM_new_array_zeroed<BMFaceLink>(bm->totface, __func__);
3805
3806 for (int i = 0; i < fgroup_len; i++) {
3807 const int fg_sta = fgroup_index[i][0];
3808 const int fg_len = fgroup_index[i][1];
3809 for (int j = 0; j < fg_len; j++) {
3810 const int face_index = fgroup_array[fg_sta + j];
3811 BMFace *f = BM_face_at_index(bm, face_index);
3812 BM_elem_index_set(f, i);
3813
3814 BMFaceLink *f_link = &f_link_array[face_index];
3815 f_link->face = f;
3816 f_link->area = BM_face_calc_area(f);
3817 BLI_addtail(&fgroup_listbase[i], f_link);
3818 }
3819 }
3820
3821 MEM_delete(fgroup_array);
3822 MEM_delete(fgroup_index);
3823
3824 Heap *fgroup_heap = BLI_heap_new_ex(fgroup_len);
3825 HeapNode **fgroup_table = MEM_new_array_uninitialized<HeapNode *>(fgroup_len, __func__);
3826 bool *fgroup_dirty = MEM_new_array_zeroed<bool>(fgroup_len, __func__);
3827
3828 for (int i = 0; i < fgroup_len; i++) {
3829 const float cost = bm_interior_face_group_calc_cost(&fgroup_listbase[i], edge_lengths);
3830 if (cost != FLT_MAX) {
3831 fgroup_table[i] = BLI_heap_insert(fgroup_heap, -cost, POINTER_FROM_INT(i));
3832 }
3833 else {
3834 fgroup_table[i] = nullptr;
3835 }
3836 }
3837
3838 /* Avoid re-running cost calculations for large face-groups which will end up forming the
3839 * outer shell and not be considered interior.
3840 * As these face groups become increasingly bigger - their chance of being considered
3841 * interior reduces as does the time to calculate their cost.
3842 *
3843 * This delays recalculating them until they are considered can dates to remove
3844 * which becomes less and less likely as they increase in area. */
3845
3846#define USE_DELAY_FACE_GROUP_COST_CALC
3847
3848 while (true) {
3849
3850#if defined(USE_DELAY_FACE_GROUP_COST_CALC)
3851 while (!BLI_heap_is_empty(fgroup_heap)) {
3852 HeapNode *node_min = BLI_heap_top(fgroup_heap);
3853 const int i = POINTER_AS_INT(BLI_heap_node_ptr(node_min));
3854 if (fgroup_dirty[i]) {
3855 const float cost = bm_interior_face_group_calc_cost(&fgroup_listbase[i], edge_lengths);
3856 if (cost != FLT_MAX) {
3857 /* The cost may have improves (we may be able to skip this),
3858 * however the cost should _never_ make this a choice. */
3859 BLI_assert(-BLI_heap_node_value(node_min) >= cost);
3860 BLI_heap_node_value_update(fgroup_heap, fgroup_table[i], -cost);
3861 }
3862 else {
3863 BLI_heap_remove(fgroup_heap, fgroup_table[i]);
3864 fgroup_table[i] = nullptr;
3865 }
3866 fgroup_dirty[i] = false;
3867 }
3868 else {
3869 break;
3870 }
3871 }
3872#endif
3873
3874 if (BLI_heap_is_empty(fgroup_heap)) {
3875 break;
3876 }
3877
3878 const int i_min = POINTER_AS_INT(BLI_heap_pop_min(fgroup_heap));
3879 BLI_assert(fgroup_table[i_min] != nullptr);
3880 BLI_assert(fgroup_dirty[i_min] == false);
3881 fgroup_table[i_min] = nullptr;
3882 changed = true;
3883
3884 while (BMFaceLink *f_link = static_cast<BMFaceLink *>(BLI_pophead(&fgroup_listbase[i_min]))) {
3885 BMFace *f = f_link->face;
3886 BM_face_select_set(bm, f, true);
3887 BM_elem_index_set(f, -1); /* set_dirty */
3888
3889 BMLoop *l_iter, *l_first;
3890
3891 /* Loop over edges face edges, merging groups which are no longer separated
3892 * by non-manifold edges (when manifold check ignores faces from this group). */
3893 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
3894 do {
3895 BMLoop *l_pair[2];
3896 if (bm_interior_edge_is_manifold_except_face_index(l_iter->e, i_min, l_pair)) {
3898
3899 int i_a = BM_elem_index_get(l_pair[0]->f);
3900 int i_b = BM_elem_index_get(l_pair[1]->f);
3901 if (i_a != i_b) {
3902 /* Only for predictable results that don't depend on the order of radial loops,
3903 * not essential. */
3904 if (i_a > i_b) {
3905 std::swap(i_a, i_b);
3906 }
3907
3908 /* Merge the groups. */
3909 for (BMFaceLink &n : fgroup_listbase[i_b]) {
3910 BMFace *f_iter = n.face;
3911 BM_elem_index_set(f_iter, i_a);
3912 }
3913 BLI_movelisttolist(&fgroup_listbase[i_a], &fgroup_listbase[i_b]);
3914
3915 /* This may have been added to 'fgroup_recalc_stack', instead of removing it,
3916 * just check the heap node isn't nullptr before recalculating. */
3917 BLI_heap_remove(fgroup_heap, fgroup_table[i_b]);
3918 fgroup_table[i_b] = nullptr;
3919 /* Keep the dirty flag as-is for 'i_b', because it may be in the 'fgroup_recalc_stack'
3920 * and we don't want to add it again.
3921 * Instead rely on the 'fgroup_table[i_b]' being nullptr as a secondary check. */
3922
3923 if (fgroup_dirty[i_a] == false) {
3924 BLI_assert(fgroup_table[i_a] != nullptr);
3925 STACK_PUSH(fgroup_recalc_stack, i_a);
3926 fgroup_dirty[i_a] = true;
3927 }
3928 }
3929 }
3930
3931 /* Mark all connected groups for re-calculation. */
3932 BMLoop *l_radial_iter = l_iter->radial_next;
3933 if (l_radial_iter != l_iter) {
3934 do {
3935 int i_other = BM_elem_index_get(l_radial_iter->f);
3936 if (!ELEM(i_other, -1, i_min)) {
3937 if ((fgroup_table[i_other] != nullptr) && (fgroup_dirty[i_other] == false)) {
3938#if !defined(USE_DELAY_FACE_GROUP_COST_CALC)
3939 STACK_PUSH(fgroup_recalc_stack, i_other);
3940#endif
3941 fgroup_dirty[i_other] = true;
3942 }
3943 }
3944 } while ((l_radial_iter = l_radial_iter->radial_next) != l_iter);
3945 }
3946
3947 } while ((l_iter = l_iter->next) != l_first);
3948 }
3949
3950 for (int index = 0; index < STACK_SIZE(fgroup_recalc_stack); index++) {
3951 const int i = fgroup_recalc_stack[index];
3952 if (fgroup_table[i] != nullptr && fgroup_dirty[i] == true) {
3953 /* First update edge tags. */
3954 const float cost = bm_interior_face_group_calc_cost(&fgroup_listbase[i], edge_lengths);
3955 if (cost != FLT_MAX) {
3956 BLI_heap_node_value_update(fgroup_heap, fgroup_table[i], -cost);
3957 }
3958 else {
3959 BLI_heap_remove(fgroup_heap, fgroup_table[i]);
3960 fgroup_table[i] = nullptr;
3961 }
3962 }
3963 fgroup_dirty[i] = false;
3964 }
3965 STACK_CLEAR(fgroup_recalc_stack);
3966 }
3967
3968 MEM_delete(edge_lengths);
3969 MEM_delete(f_link_array);
3970 MEM_delete(fgroup_listbase);
3971 MEM_delete(fgroup_recalc_stack);
3972 MEM_delete(fgroup_table);
3973 MEM_delete(fgroup_dirty);
3974
3975 BLI_heap_free(fgroup_heap, nullptr);
3976
3977 return changed;
3978}
3979
3981
3982/* -------------------------------------------------------------------- */
3987
3989#define USE_LINKED_SELECT_DEFAULT_HACK
3990
3995
3996static bool select_linked_delimit_test(BMEdge *e, int delimit, const DelimitData *delimit_data)
3997{
3998 BLI_assert(delimit);
3999
4000 if (delimit & BMO_DELIM_SEAM) {
4002 return true;
4003 }
4004 }
4005
4006 if (delimit & BMO_DELIM_SHARP) {
4007 if (BM_elem_flag_test(e, BM_ELEM_SMOOTH) == 0) {
4008 return true;
4009 }
4010 }
4011
4012 if (delimit & BMO_DELIM_NORMAL) {
4013 if (!BM_edge_is_contiguous(e)) {
4014 return true;
4015 }
4016 }
4017
4018 if (delimit & BMO_DELIM_MATERIAL) {
4019 if (e->l && e->l->radial_next != e->l) {
4020 const short mat_nr = e->l->f->mat_nr;
4021 BMLoop *l_iter = e->l->radial_next;
4022 do {
4023 if (l_iter->f->mat_nr != mat_nr) {
4024 return true;
4025 }
4026 } while ((l_iter = l_iter->radial_next) != e->l);
4027 }
4028 }
4029
4030 if (delimit & BMO_DELIM_UV) {
4032 e, delimit_data->cd_loop_type, delimit_data->cd_loop_offset) == 0)
4033 {
4034 return true;
4035 }
4036 }
4037
4038 return false;
4039}
4040
4041#ifdef USE_LINKED_SELECT_DEFAULT_HACK
4046static int select_linked_delimit_default_from_op(wmOperator *op, const int select_mode)
4047{
4048 static char delimit_last_store[2] = {0, BMO_DELIM_SEAM};
4049 int delimit_last_index = (select_mode & (SCE_SELECT_VERTEX | SCE_SELECT_EDGE)) == 0;
4050 char *delimit_last = &delimit_last_store[delimit_last_index];
4051 PropertyRNA *prop_delimit = RNA_struct_find_property(op->ptr, "delimit");
4052 int delimit;
4053
4054 if (RNA_property_is_set(op->ptr, prop_delimit)) {
4055 delimit = RNA_property_enum_get(op->ptr, prop_delimit);
4056 *delimit_last = delimit;
4057 }
4058 else {
4059 delimit = *delimit_last;
4060 RNA_property_enum_set(op->ptr, prop_delimit, delimit);
4061 }
4062 return delimit;
4063}
4064#endif
4065
4066static void select_linked_delimit_validate(BMesh *bm, int *delimit)
4067{
4068 if ((*delimit) & BMO_DELIM_UV) {
4070 (*delimit) &= ~BMO_DELIM_UV;
4071 }
4072 }
4073}
4074
4075static void select_linked_delimit_begin(BMesh *bm, int delimit)
4076{
4077 DelimitData delimit_data{};
4078
4079 if (delimit & BMO_DELIM_UV) {
4080 delimit_data.cd_loop_type = CD_PROP_FLOAT2;
4081 delimit_data.cd_loop_offset = CustomData_get_offset(&bm->ldata, delimit_data.cd_loop_type);
4082 if (delimit_data.cd_loop_offset == -1) {
4083 delimit &= ~BMO_DELIM_UV;
4084 }
4085 }
4086
4087 /* Shouldn't need to allocated BMO flags here (sigh). */
4089
4090 {
4091 BMIter iter;
4092 BMEdge *e;
4093
4094 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
4095 const bool is_walk_ok = (select_linked_delimit_test(e, delimit, &delimit_data) == false);
4096
4097 BMO_edge_flag_set(bm, e, BMO_ELE_TAG, is_walk_ok);
4098 }
4099 }
4100}
4101
4106
4108{
4109 const Main *bmain = CTX_data_main(C);
4110 Scene *scene = CTX_data_scene(C);
4111 ViewLayer *view_layer = CTX_data_view_layer(C);
4112
4113#ifdef USE_LINKED_SELECT_DEFAULT_HACK
4114 const int delimit_init = select_linked_delimit_default_from_op(op,
4115 scene->toolsettings->selectmode);
4116#else
4117 const int delimit_init = RNA_enum_get(op->ptr, "delimit");
4118#endif
4119
4121 *bmain, scene, view_layer, CTX_wm_view3d(C));
4122
4123 for (Object *obedit : objects) {
4126 BMIter iter;
4127 BMWalker walker;
4128
4129 int delimit = delimit_init;
4130
4132
4133 if (delimit) {
4135 }
4136
4137 if (em->selectmode & SCE_SELECT_VERTEX) {
4138 BMVert *v;
4139
4140 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
4142 }
4143
4144 /* Exclude all delimited verts. */
4145 if (delimit) {
4146 BMEdge *e;
4147 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
4149 /* Check the edge for selected faces,
4150 * this supports stepping off isolated vertices which would otherwise be ignored. */
4154 }
4155 }
4156 }
4157 }
4158
4159 BMW_init(&walker,
4160 bm,
4163 delimit ? BMO_ELE_TAG : BMW_MASK_NOP,
4168
4169 if (delimit) {
4170 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
4172 BMElem *ele_walk;
4173 BMW_ITER (ele_walk, &walker, v) {
4174 if (ele_walk->head.htype == BM_LOOP) {
4175 BMVert *v_step = (reinterpret_cast<BMLoop *>(ele_walk))->v;
4176 BM_vert_select_set(bm, v_step, true);
4178 }
4179 else {
4180 BMEdge *e_step = reinterpret_cast<BMEdge *>(ele_walk);
4181 BLI_assert(ele_walk->head.htype == BM_EDGE);
4182 BM_edge_select_set(bm, e_step, true);
4185 }
4186 }
4187 }
4188 }
4189 }
4190 else {
4191 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
4193 BMEdge *e_walk;
4194 BMW_ITER (e_walk, &walker, v) {
4195 BM_edge_select_set(bm, e_walk, true);
4197 }
4198 }
4199 }
4200 }
4201
4202 BMW_end(&walker);
4203
4205 }
4206 else if (em->selectmode & SCE_SELECT_EDGE) {
4207 BMEdge *e;
4208
4209 if (delimit) {
4210 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
4211 /* Check the edge for selected faces,
4212 * this supports stepping off isolated edges which would otherwise be ignored. */
4218 }
4219 }
4220 else {
4221 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
4223 }
4224 }
4225
4226 BMW_init(&walker,
4227 bm,
4230 delimit ? BMO_ELE_TAG : BMW_MASK_NOP,
4235
4236 if (delimit) {
4237 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
4239 BMElem *ele_walk;
4240 BMW_ITER (ele_walk, &walker, e) {
4241 if (ele_walk->head.htype == BM_LOOP) {
4242 BMLoop *l_step = reinterpret_cast<BMLoop *>(ele_walk);
4243 BM_edge_select_set(bm, l_step->e, true);
4244 BM_edge_select_set(bm, l_step->prev->e, true);
4246 }
4247 else {
4248 BMEdge *e_step = reinterpret_cast<BMEdge *>(ele_walk);
4249 BLI_assert(ele_walk->head.htype == BM_EDGE);
4250 BM_edge_select_set(bm, e_step, true);
4252 }
4253 }
4254 }
4255 }
4256 }
4257 else {
4258 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
4260 BMEdge *e_walk;
4261 BMW_ITER (e_walk, &walker, e) {
4262 BM_edge_select_set(bm, e_walk, true);
4264 }
4265 }
4266 }
4267 }
4268
4269 BMW_end(&walker);
4270
4272 }
4273 else {
4274 BMFace *f;
4275
4276 BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
4278 }
4279
4280 BMW_init(&walker,
4281 bm,
4282 BMW_ISLAND,
4284 delimit ? BMO_ELE_TAG : BMW_MASK_NOP,
4289
4290 BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
4292 BMFace *f_walk;
4293 BMW_ITER (f_walk, &walker, f) {
4294 BM_face_select_set(bm, f_walk, true);
4296 }
4297 }
4298 }
4299
4300 BMW_end(&walker);
4301 }
4302
4303 if (delimit) {
4305 }
4306
4308
4309 DEG_id_tag_update(obedit->data, ID_RECALC_SELECT);
4310 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
4311 }
4312
4313 return OPERATOR_FINISHED;
4314}
4315
4317{
4318 PropertyRNA *prop;
4319
4320 /* Identifiers. */
4321 ot->name = "Select Linked All";
4322 ot->idname = "MESH_OT_select_linked";
4323 ot->description = "Select all vertices connected to the current selection";
4324
4325 /* API callbacks. */
4328
4329 /* Flags. */
4331
4332 prop = RNA_def_enum_flag(ot->srna,
4333 "delimit",
4336 "Delimit",
4337 "Delimit selected region");
4338#ifdef USE_LINKED_SELECT_DEFAULT_HACK
4340#else
4341 UNUSED_VARS(prop);
4342#endif
4343}
4344
4346
4347/* -------------------------------------------------------------------- */
4350
4351static wmOperatorStatus edbm_select_linked_pick_exec(bContext *C, wmOperator *op);
4352
4354 BMesh *bm, const short selectmode, BMElem *ele, bool sel, int delimit)
4355{
4356 BMWalker walker;
4357
4359
4360 if (delimit) {
4362 }
4363
4364 /* NOTE: logic closely matches #edbm_select_linked_exec, keep in sync. */
4365
4366 if (ele->head.htype == BM_VERT) {
4367 BMVert *eve = reinterpret_cast<BMVert *>(ele);
4368
4369 BMW_init(&walker,
4370 bm,
4373 delimit ? BMO_ELE_TAG : BMW_MASK_NOP,
4378
4379 if (delimit) {
4380 BMElem *ele_walk;
4381 BMW_ITER (ele_walk, &walker, eve) {
4382 if (ele_walk->head.htype == BM_LOOP) {
4383 BMVert *v_step = (reinterpret_cast<BMLoop *>(ele_walk))->v;
4384 BM_vert_select_set(bm, v_step, sel);
4385 }
4386 else {
4387 BMEdge *e_step = reinterpret_cast<BMEdge *>(ele_walk);
4388 BLI_assert(ele_walk->head.htype == BM_EDGE);
4389 BM_edge_select_set(bm, e_step, sel);
4390 }
4391 }
4392 }
4393 else {
4394 BMEdge *e_walk;
4395 BMW_ITER (e_walk, &walker, eve) {
4396 BM_edge_select_set(bm, e_walk, sel);
4397 }
4398 }
4399
4400 BMW_end(&walker);
4401
4402 EDBM_selectmode_flush(bm, selectmode);
4403 }
4404 else if (ele->head.htype == BM_EDGE) {
4405 BMEdge *eed = reinterpret_cast<BMEdge *>(ele);
4406
4407 BMW_init(&walker,
4408 bm,
4411 delimit ? BMO_ELE_TAG : BMW_MASK_NOP,
4416
4417 if (delimit) {
4418 BMElem *ele_walk;
4419 BMW_ITER (ele_walk, &walker, eed) {
4420 if (ele_walk->head.htype == BM_LOOP) {
4421 BMEdge *e_step = (reinterpret_cast<BMLoop *>(ele_walk))->e;
4422 BM_edge_select_set(bm, e_step, sel);
4423 }
4424 else {
4425 BMEdge *e_step = reinterpret_cast<BMEdge *>(ele_walk);
4426 BLI_assert(ele_walk->head.htype == BM_EDGE);
4427 BM_edge_select_set(bm, e_step, sel);
4428 }
4429 }
4430 }
4431 else {
4432 BMEdge *e_walk;
4433 BMW_ITER (e_walk, &walker, eed) {
4434 BM_edge_select_set(bm, e_walk, sel);
4435 }
4436 }
4437
4438 BMW_end(&walker);
4439
4440 EDBM_selectmode_flush(bm, selectmode);
4441 }
4442 else if (ele->head.htype == BM_FACE) {
4443 BMFace *efa = reinterpret_cast<BMFace *>(ele);
4444
4445 BMW_init(&walker,
4446 bm,
4447 BMW_ISLAND,
4449 delimit ? BMO_ELE_TAG : BMW_MASK_NOP,
4454
4455 {
4456 BMFace *f_walk;
4457 BMW_ITER (f_walk, &walker, efa) {
4458 BM_face_select_set(bm, f_walk, sel);
4460 }
4461 }
4462
4463 BMW_end(&walker);
4464 }
4465
4467
4468 if (delimit) {
4470 }
4471}
4472
4474 wmOperator *op,
4475 const wmEvent *event)
4476{
4477 Base *basact = nullptr;
4478 BMVert *eve;
4479 BMEdge *eed;
4480 BMFace *efa;
4481 const bool sel = !RNA_boolean_get(op->ptr, "deselect");
4482 int index;
4483
4484 if (RNA_struct_property_is_set(op->ptr, "index")) {
4485 return edbm_select_linked_pick_exec(C, op);
4486 }
4487
4488 /* #unified_findnearest needs OpenGL. */
4490
4491 /* Setup view context for argument to callbacks. */
4493
4495 *vc.bmain, vc.scene, vc.view_layer, vc.v3d);
4496
4497 {
4498 bool has_edges = false;
4499 for (Base *base : bases) {
4500 Object *ob_iter = base->object;
4502 const BMesh *bm = BKE_editmesh_bmesh_get(ob_iter);
4503 if (bm->totedge) {
4504 has_edges = true;
4505 }
4506 }
4507 if (has_edges == false) {
4508 return OPERATOR_CANCELLED;
4509 }
4510 }
4511
4512 vc.mval[0] = event->mval[0];
4513 vc.mval[1] = event->mval[1];
4514
4515 const short active_selectmode = vc.em->selectmode;
4516
4517 /* Return warning. */
4518 {
4519 int base_index = -1;
4520 const bool ok = unified_findnearest(&vc, bases, &base_index, &eve, &eed, &efa);
4521 if (!ok) {
4522 return OPERATOR_CANCELLED;
4523 }
4524 basact = bases[base_index];
4525 }
4526
4528 BMEditMesh *em = vc.em;
4530 /* It's possible other objects are not in the same mode
4531 * (although it shouldn't occur in typical workflows).
4532 * The modes must match, otherwise an element may be found & `ele` set to null, see: #166007 */
4533 if (em->selectmode != active_selectmode) [[unlikely]] {
4534 EDBM_selectmode_set(em, bm, active_selectmode);
4535 }
4536
4537#ifdef USE_LINKED_SELECT_DEFAULT_HACK
4539#else
4540 int delimit = RNA_enum_get(op->ptr, "delimit");
4541#endif
4542
4543 BMElem *ele = EDBM_elem_from_selectmode(em, eve, eed, efa);
4544
4545 edbm_select_linked_pick_ex(bm, em->selectmode, ele, sel, delimit);
4546
4547 /* To support redo. */
4548 {
4549 /* Note that the `base_index` can't be used as the index depends on the 3D Viewport
4550 * which might not be available on redo. */
4552 int object_index;
4554 *vc.bmain, vc.scene, vc.view_layer, em, ele, &object_index);
4555 BLI_assert(object_index >= 0);
4556 RNA_int_set(op->ptr, "object_index", object_index);
4557 RNA_int_set(op->ptr, "index", index);
4558 }
4559
4562
4563 return OPERATOR_FINISHED;
4564}
4565
4567{
4568 Object *obedit = nullptr;
4569 BMElem *ele;
4570
4571 {
4572 const Main *bmain = CTX_data_main(C);
4573 const Scene *scene = CTX_data_scene(C);
4574 ViewLayer *view_layer = CTX_data_view_layer(C);
4575 /* Intentionally wrap negative values so the lookup fails. */
4576 const uint object_index = uint(RNA_int_get(op->ptr, "object_index"));
4577 const uint index = uint(RNA_int_get(op->ptr, "index"));
4578 ele = EDBM_elem_from_index_any_multi(*bmain, scene, view_layer, object_index, index, &obedit);
4579 }
4580
4581 if (ele == nullptr) {
4582 return OPERATOR_CANCELLED;
4583 }
4584
4587 const bool sel = !RNA_boolean_get(op->ptr, "deselect");
4588
4589#ifdef USE_LINKED_SELECT_DEFAULT_HACK
4591#else
4592 int delimit = RNA_enum_get(op->ptr, "delimit");
4593#endif
4594
4595 edbm_select_linked_pick_ex(bm, em->selectmode, ele, sel, delimit);
4596
4599
4600 return OPERATOR_FINISHED;
4601}
4602
4604{
4605 PropertyRNA *prop;
4606
4607 /* Identifiers. */
4608 ot->name = "Select Linked";
4609 ot->idname = "MESH_OT_select_linked_pick";
4610 ot->description = "(De)select all vertices linked to the edge under the mouse cursor";
4611
4612 /* API callbacks. */
4616
4617 /* Flags. */
4619
4620 RNA_def_boolean(ot->srna, "deselect", false, "Deselect", "");
4621 prop = RNA_def_enum_flag(ot->srna,
4622 "delimit",
4625 "Delimit",
4626 "Delimit selected region");
4627#ifdef USE_LINKED_SELECT_DEFAULT_HACK
4629#endif
4630
4631 /* Use for redo. */
4632 prop = RNA_def_int(ot->srna, "object_index", -1, -1, INT_MAX, "", "", 0, INT_MAX);
4634 prop = RNA_def_int(ot->srna, "index", -1, -1, INT_MAX, "", "", 0, INT_MAX);
4636}
4637
4639
4640/* -------------------------------------------------------------------- */
4643
4645{
4646 const Main *bmain = CTX_data_main(C);
4647 const Scene *scene = CTX_data_scene(C);
4648 ViewLayer *view_layer = CTX_data_view_layer(C);
4649 const bool extend = RNA_boolean_get(op->ptr, "extend");
4650 const bool exclude_nonmanifold = RNA_boolean_get(op->ptr, "exclude_nonmanifold");
4651 const int pole_count = RNA_int_get(op->ptr, "pole_count");
4652 const eElemCountType type = eElemCountType(RNA_enum_get(op->ptr, "type"));
4654 *bmain, scene, view_layer, CTX_wm_view3d(C));
4655
4656 for (Object *obedit : objects) {
4659 bool changed = false;
4660
4661 BMIter iter;
4662 BMVert *v;
4663
4664 if (!extend) {
4666 changed = true;
4667 }
4668
4669 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
4671 continue;
4672 }
4673
4674 const int v_edge_count = BM_vert_edge_count_at_most(v, pole_count + 1);
4675 if (!is_count_a_match(type, v_edge_count, pole_count)) {
4676 continue;
4677 }
4678
4679 if (exclude_nonmanifold) {
4680 /* Exclude non-manifold vertices (no edges). */
4681 if (BM_vert_is_manifold(v) == false) {
4682 continue;
4683 }
4684
4685 /* Exclude vertices connected to non-manifold edges. */
4686 BMIter eiter;
4687 BMEdge *e;
4688 bool all_edges_manifold = true;
4689 BM_ITER_ELEM (e, &eiter, v, BM_EDGES_OF_VERT) {
4690 if (BM_edge_is_manifold(e) == false) {
4691 all_edges_manifold = false;
4692 break;
4693 }
4694 }
4695
4696 if (all_edges_manifold == false) {
4697 continue;
4698 }
4699 }
4700
4701 /* All tests passed, perform the selection. */
4702
4703 /* Multiple selection modes may be active.
4704 * Select elements per the finest-grained choice. */
4705 changed = true;
4706
4707 if (em->selectmode & SCE_SELECT_VERTEX) {
4708 BM_vert_select_set(bm, v, true);
4709 }
4710 else if (em->selectmode & SCE_SELECT_EDGE) {
4711 BMIter eiter;
4712 BMEdge *e;
4713 BM_ITER_ELEM (e, &eiter, v, BM_EDGES_OF_VERT) {
4714 BM_edge_select_set(bm, e, true);
4715 }
4716 }
4717 else if (em->selectmode & SCE_SELECT_FACE) {
4718 BMIter fiter;
4719 BMFace *f;
4720 BM_ITER_ELEM (f, &fiter, v, BM_FACES_OF_VERT) {
4721 BM_face_select_set(bm, f, true);
4722 }
4723 }
4724 else {
4726 }
4727 }
4728
4729 if (changed) {
4732
4733 DEG_id_tag_update(obedit->data, ID_RECALC_SELECT);
4734 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
4735 }
4736 }
4737
4738 return OPERATOR_FINISHED;
4739}
4740
4742{
4743 /* Identifiers. */
4744 ot->name = "Select By Pole Count";
4745 ot->description =
4746 "Select vertices at poles by the number of connected edges. "
4747 "In edge and face mode the geometry connected to the vertices is selected";
4748 ot->idname = "MESH_OT_select_by_pole_count";
4749
4750 /* API callbacks. */
4753
4754 /* Flags. */
4756
4757 /* Properties. */
4758 RNA_def_int(ot->srna, "pole_count", 4, 0, INT_MAX, "Pole Count", "", 0, INT_MAX);
4759 RNA_def_enum(ot->srna,
4760 "type",
4763 "Type",
4764 "Type of comparison to make");
4765 RNA_def_boolean(ot->srna, "extend", false, "Extend", "Extend the selection");
4767 ot->srna, "exclude_nonmanifold", true, "Exclude Non Manifold", "Exclude non-manifold poles");
4768}
4769
4771
4772/* -------------------------------------------------------------------- */
4775
4777{
4778 const Main *bmain = CTX_data_main(C);
4779 const Scene *scene = CTX_data_scene(C);
4780 ViewLayer *view_layer = CTX_data_view_layer(C);
4781 const bool extend = RNA_boolean_get(op->ptr, "extend");
4782 const int numverts = RNA_int_get(op->ptr, "number");
4783 const eElemCountType type = eElemCountType(RNA_enum_get(op->ptr, "type"));
4785 *bmain, scene, view_layer, CTX_wm_view3d(C));
4786
4787 for (Object *obedit : objects) {
4790 bool changed = false;
4791
4792 BMFace *efa;
4793 BMIter iter;
4794
4795 if (!extend) {
4797 changed = true;
4798 }
4799
4800 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
4802 continue;
4803 }
4804
4805 if (is_count_a_match(type, efa->len, numverts)) {
4806 changed = true;
4807 BM_face_select_set(bm, efa, true);
4808 }
4809 }
4810
4811 if (changed) {
4814
4815 DEG_id_tag_update(obedit->data, ID_RECALC_SELECT);
4816 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
4817 }
4818 }
4819
4820 return OPERATOR_FINISHED;
4821}
4822
4824{
4825
4826 /* Identifiers. */
4827 ot->name = "Select Faces by Sides";
4828 ot->description = "Select vertices or faces by the number of face sides";
4829 ot->idname = "MESH_OT_select_face_by_sides";
4830
4831 /* API callbacks. */
4834
4835 /* Flags. */
4837
4838 /* Properties. */
4839 RNA_def_int(ot->srna, "number", 4, 3, INT_MAX, "Number of Vertices", "", 3, INT_MAX);
4840 RNA_def_enum(ot->srna,
4841 "type",
4844 "Type",
4845 "Type of comparison to make");
4846 RNA_def_boolean(ot->srna, "extend", true, "Extend", "Extend the selection");
4847}
4848
4850
4851/* -------------------------------------------------------------------- */
4854
4856{
4857 const Main *bmain = CTX_data_main(C);
4858 const Scene *scene = CTX_data_scene(C);
4859 ViewLayer *view_layer = CTX_data_view_layer(C);
4860 const bool extend = RNA_boolean_get(op->ptr, "extend");
4861
4863 *bmain, scene, view_layer, CTX_wm_view3d(C));
4864
4865 for (Object *obedit : objects) {
4868 BMIter iter;
4869
4870 bool changed = false;
4871
4872 if (!extend) {
4874 changed = true;
4875 }
4876
4877 if (em->selectmode & SCE_SELECT_VERTEX) {
4878 BMVert *eve;
4879 BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
4881 continue;
4882 }
4883 if (!eve->e) {
4884 BM_vert_select_set(bm, eve, true);
4885 changed = true;
4886 }
4887 }
4888 }
4889
4890 if (em->selectmode & SCE_SELECT_EDGE) {
4891 BMEdge *eed;
4892 BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
4894 continue;
4895 }
4896 if (BM_edge_is_wire(eed)) {
4897 BM_edge_select_set(bm, eed, true);
4898 changed = true;
4899 }
4900 }
4901 }
4902
4903 if (em->selectmode & SCE_SELECT_FACE) {
4904 BMFace *efa;
4905 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
4907 continue;
4908 }
4909 BMIter liter;
4910 BMLoop *l;
4911 bool is_loose = true;
4912 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
4913 if (!BM_edge_is_boundary(l->e)) {
4914 is_loose = false;
4915 break;
4916 }
4917 }
4918 if (is_loose) {
4919 BM_face_select_set(bm, efa, true);
4920 changed = true;
4921 }
4922 }
4923 }
4924
4925 if (changed) {
4928
4929 DEG_id_tag_update(obedit->data, ID_RECALC_SELECT);
4930 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
4931 }
4932 }
4933
4934 return OPERATOR_FINISHED;
4935}
4936
4938{
4939 /* Identifiers. */
4940 ot->name = "Select Loose Geometry";
4941 ot->description = "Select loose geometry based on the selection mode";
4942 ot->idname = "MESH_OT_select_loose";
4943
4944 /* API callbacks. */
4947
4948 /* Flags. */
4950
4951 /* Props. */
4952 RNA_def_boolean(ot->srna, "extend", false, "Extend", "Extend the selection");
4953}
4954
4956
4957/* -------------------------------------------------------------------- */
4960
4962{
4963 const Main *bmain = CTX_data_main(C);
4964 const Scene *scene = CTX_data_scene(C);
4965 ViewLayer *view_layer = CTX_data_view_layer(C);
4966 const int axis_flag = RNA_enum_get(op->ptr, "axis");
4967 const bool extend = RNA_boolean_get(op->ptr, "extend");
4968 Object *obedit_active = CTX_data_edit_object(C);
4969 BMesh *bm_active = BKE_editmesh_bmesh_get_for_write(obedit_active);
4970 const int select_mode = bm_active->selectmode;
4971 int tot_mirr = 0, tot_fail = 0;
4972
4974 *bmain, scene, view_layer, CTX_wm_view3d(C));
4975
4976 for (Object *obedit : objects) {
4979 if (bm->totvertsel == 0) {
4980 continue;
4981 }
4982
4983 int tot_mirr_iter = 0, tot_fail_iter = 0;
4984
4985 for (int axis = 0; axis < 3; axis++) {
4986 if ((1 << axis) & axis_flag) {
4988 em, id_cast<Mesh *>(obedit->data), axis, extend, &tot_mirr_iter, &tot_fail_iter);
4989 }
4990 }
4991
4992 if (tot_mirr_iter) {
4995
4996 DEG_id_tag_update(obedit->data, ID_RECALC_SELECT);
4997 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
4998 }
4999
5000 tot_fail += tot_fail_iter;
5001 tot_mirr += tot_mirr_iter;
5002 }
5003
5004 if (tot_mirr || tot_fail) {
5005 ED_mesh_report_mirror_ex(*op->reports, tot_mirr, tot_fail, select_mode);
5006 }
5007 return OPERATOR_FINISHED;
5008}
5009
5011{
5012 /* Identifiers. */
5013 ot->name = "Select Mirror";
5014 ot->description = "Select mesh items at mirrored locations";
5015 ot->idname = "MESH_OT_select_mirror";
5016
5017 /* API callbacks. */
5020
5021 /* Flags. */
5023
5024 /* Props. */
5025 RNA_def_enum_flag(ot->srna, "axis", rna_enum_axis_flag_xyz_items, (1 << 0), "Axis", "");
5026
5027 RNA_def_boolean(ot->srna, "extend", false, "Extend", "Extend the existing selection");
5028}
5029
5031
5032/* -------------------------------------------------------------------- */
5035
5037{
5038 const Main *bmain = CTX_data_main(C);
5039 const Scene *scene = CTX_data_scene(C);
5040 ViewLayer *view_layer = CTX_data_view_layer(C);
5041 const bool use_face_step = RNA_boolean_get(op->ptr, "use_face_step");
5042
5044 *bmain, scene, view_layer, CTX_wm_view3d(C));
5045 for (Object *obedit : objects) {
5048 if ((bm->totvertsel == 0) && (bm->totedgesel == 0) && (bm->totfacesel == 0)) {
5049 continue;
5050 }
5051
5052 EDBM_select_more(bm, em->selectmode, use_face_step);
5053 DEG_id_tag_update(obedit->data, ID_RECALC_SELECT);
5054 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
5055 }
5056
5057 return OPERATOR_FINISHED;
5058}
5059
5061{
5062 /* Identifiers. */
5063 ot->name = "Select More";
5064 ot->idname = "MESH_OT_select_more";
5065 ot->description = "Select more vertices, edges or faces connected to initial selection";
5066
5067 /* API callbacks */
5070
5071 /* Flags. */
5073
5075 ot->srna, "use_face_step", true, "Face Step", "Connected faces (instead of edges)");
5076}
5077
5079
5080/* -------------------------------------------------------------------- */
5083
5085{
5086 const Main *bmain = CTX_data_main(C);
5087 const Scene *scene = CTX_data_scene(C);
5088 ViewLayer *view_layer = CTX_data_view_layer(C);
5089 const bool use_face_step = RNA_boolean_get(op->ptr, "use_face_step");
5090
5092 *bmain, scene, view_layer, CTX_wm_view3d(C));
5093 for (Object *obedit : objects) {
5096 if ((bm->totvertsel == 0) && (bm->totedgesel == 0) && (bm->totfacesel == 0)) {
5097 continue;
5098 }
5099
5100 EDBM_select_less(bm, em->selectmode, use_face_step);
5101 DEG_id_tag_update(obedit->data, ID_RECALC_SELECT);
5102 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
5103 }
5104
5105 return OPERATOR_FINISHED;
5106}
5107
5109{
5110 /* Identifiers. */
5111 ot->name = "Select Less";
5112 ot->idname = "MESH_OT_select_less";
5113 ot->description = "Deselect vertices, edges or faces at the boundary of each selection region";
5114
5115 /* API callbacks */
5118
5119 /* Flags. */
5121
5123 ot->srna, "use_face_step", true, "Face Step", "Connected faces (instead of edges)");
5124}
5125
5127
5128/* -------------------------------------------------------------------- */
5131
5136{
5137 BMIter viter;
5138 BMVert *v;
5139
5140 BM_ITER_ELEM (v, &viter, e, BM_VERTS_OF_EDGE) {
5141 BMIter eiter;
5142 BMEdge *e_other;
5143
5144 BM_ITER_ELEM (e_other, &eiter, v, BM_EDGES_OF_VERT) {
5145 if ((e_other != e) && BM_elem_flag_test(e_other, BM_ELEM_SELECT)) {
5146 return false;
5147 }
5148 }
5149 }
5150 return true;
5151}
5152
5154{
5155 BMIter eiter;
5156 BMEdge *e_other, *e_next = nullptr;
5157 int count = 0;
5158 const int count_expected = 1;
5159
5160 BM_ITER_ELEM (e_other, &eiter, v, BM_EDGES_OF_VERT) {
5161 if (e_other == e_curr || !BM_elem_flag_test(e_other, BM_ELEM_SELECT)) {
5162 continue;
5163 }
5164 if (++count > count_expected) {
5165 return nullptr;
5166 }
5167 e_next = e_other;
5168 }
5169 return (count == count_expected) ? e_next : nullptr;
5170}
5171
5173{
5174 BMIter eiter;
5175 BMEdge *e;
5176 BMVert *v_next = nullptr;
5177 int count = 0;
5178 const int count_expected = v_prev ? 1 : 2;
5179
5180 BM_ITER_ELEM (e, &eiter, v_curr, BM_EDGES_OF_VERT) {
5181 BMVert *v_other = BM_edge_other_vert(e, v_curr);
5182 if (v_other == v_prev || !BM_elem_flag_test(v_other, BM_ELEM_SELECT)) {
5183 continue;
5184 }
5185 if (++count > count_expected) {
5186 return nullptr;
5187 }
5188 v_next = v_other;
5189 }
5190 return (count == count_expected) ? v_next : nullptr;
5191}
5192
5194 BMFace *f_prev)
5195{
5196 BMIter liter;
5197 BMLoop *l;
5198 BMFace *f_next = nullptr;
5199 int count = 0;
5200 const int count_expected = f_prev ? 1 : 2;
5201
5202 BM_ITER_ELEM (l, &liter, f_curr, BM_LOOPS_OF_FACE) {
5203 BMIter fiter;
5204 BMFace *f_other;
5205 BM_ITER_ELEM (f_other, &fiter, l->e, BM_FACES_OF_EDGE) {
5206 if (ELEM(f_other, f_curr, f_prev) || !BM_elem_flag_test(f_other, BM_ELEM_SELECT)) {
5207 continue;
5208 }
5209 if (++count > count_expected) {
5210 return nullptr;
5211 }
5212 f_next = f_other;
5213 }
5214 }
5215 return (count == count_expected) ? f_next : nullptr;
5216}
5217
5222{
5223 BMVert *v_prev = nullptr, *v_curr = v_start;
5224
5225 do {
5226 int selected_neighbor_count = 0;
5227 BMIter eiter;
5228 BMEdge *e;
5229 BM_ITER_ELEM (e, &eiter, v_curr, BM_EDGES_OF_VERT) {
5230 BMVert *v_other = BM_edge_other_vert(e, v_curr);
5231 if (BM_elem_flag_test(v_other, BM_ELEM_SELECT)) {
5232 if (++selected_neighbor_count > 2) {
5233 return false;
5234 }
5235 }
5236 }
5237 if (selected_neighbor_count != 2) {
5238 return false;
5239 }
5240
5241 BMVert *v_next = bm_step_to_next_selected_vert_in_chain(v_curr, v_prev);
5242 if (v_next == nullptr) {
5243 return false;
5244 }
5245 v_prev = v_curr;
5246 v_curr = v_next;
5247 } while (v_curr != v_start);
5248
5249 return true;
5250}
5251
5256{
5257 BMEdge *e_curr = e_start;
5258 BMVert *v_through = e_start->v1;
5259
5260 do {
5261 BMEdge *e_next = bm_step_over_vert_to_next_selected_edge_in_chain(e_curr, v_through);
5262 if (e_next == nullptr) {
5263 return false;
5264 }
5265 v_through = BM_edge_other_vert(e_next, v_through);
5266 e_curr = e_next;
5267
5268 } while (e_curr != e_start);
5269
5270 return true;
5271}
5272
5277{
5278 BMFace *f_prev = nullptr;
5279 BMFace *f_curr = f_start;
5280
5281 do {
5282 int selected_neighbor_count = 0;
5283 BMIter liter;
5284 BMLoop *l;
5285
5286 BM_ITER_ELEM (l, &liter, f_curr, BM_LOOPS_OF_FACE) {
5287 BMIter fiter;
5288 BMFace *f_other;
5289 BM_ITER_ELEM (f_other, &fiter, l->e, BM_FACES_OF_EDGE) {
5290 if (f_other != f_curr && BM_elem_flag_test(f_other, BM_ELEM_SELECT)) {
5291 selected_neighbor_count++;
5292 }
5293 }
5294 }
5295 if (selected_neighbor_count != 2) {
5296 return false;
5297 }
5298
5300 if (f_next == nullptr) {
5301 return false;
5302 }
5303
5304 f_prev = f_curr;
5305 f_curr = f_next;
5306 } while (f_curr != f_start);
5307
5308 return true;
5309}
5310
5312 const CheckerIntervalParams *op_params,
5313 BMVert *v_start)
5314{
5315 BMVert *v_prev = nullptr;
5316 BMVert *v_curr = v_start;
5317 int index = 0;
5318
5319 /* Mark all vertices as unvisited. */
5321
5322 while (v_curr && !BM_elem_flag_test(v_curr, BM_ELEM_TAG)) {
5323 /* Mark as visited. */
5325
5326 /* Apply checker pattern based on position in loop. */
5328 BM_elem_select_set(bm, reinterpret_cast<BMElem *>(v_curr), false);
5329 }
5330
5331 /* Find next vertex in the loop. */
5332 BMVert *v_next = bm_step_to_next_selected_vert_in_chain(v_curr, v_prev);
5333 if (ELEM(v_next, nullptr, v_start)) {
5334 break;
5335 }
5336
5337 v_prev = v_curr;
5338 v_curr = v_next;
5339 index++;
5340 }
5341}
5342
5344 const CheckerIntervalParams *op_params,
5345 BMEdge *e_start)
5346{
5347 BMEdge *e_curr = e_start;
5348 BMVert *v_through = e_start->v1;
5349 int index = 0;
5350
5351 /* Mark all edges as unvisited. */
5353
5354 while (e_curr && !BM_elem_flag_test(e_curr, BM_ELEM_TAG)) {
5355 /* Mark as visited. */
5357
5358 /* Apply checker pattern based on position in loop. */
5360 BM_elem_select_set(bm, reinterpret_cast<BMElem *>(e_curr), false);
5361 }
5362
5363 /* Find next edge in the loop. */
5364 BMEdge *e_next = bm_step_over_vert_to_next_selected_edge_in_chain(e_curr, v_through);
5365 if (ELEM(e_next, nullptr, e_start)) {
5366 break;
5367 }
5368
5369 v_through = BM_edge_other_vert(e_next, v_through);
5370 e_curr = e_next;
5371 index++;
5372 }
5373}
5374
5376 const CheckerIntervalParams *op_params,
5377 BMFace *f_start)
5378{
5379 BMFace *f_prev = nullptr;
5380 BMFace *f_curr = f_start;
5381 int index = 0;
5382
5383 /* Mark all faces as unvisited. */
5385
5386 while (f_curr && !BM_elem_flag_test(f_curr, BM_ELEM_TAG)) {
5388
5390
5391 /* Apply checker pattern to current face. */
5393 BM_elem_select_set(bm, reinterpret_cast<BMElem *>(f_curr), false);
5394 }
5395
5396 if (ELEM(f_next, nullptr, f_start)) {
5397 break;
5398 }
5399
5400 f_prev = f_curr;
5401 f_curr = f_next;
5402 index++;
5403 }
5404}
5405
5406/* Walk all reachable elements of the same type as h_act in breadth-first
5407 * order, starting from h_act. Deselects elements if the depth when they
5408 * are reached is not a multiple of "nth". */
5409static void walker_deselect_nth(BMesh *bm, const CheckerIntervalParams *op_params, BMHeader *h_act)
5410{
5411 BMElem *ele;
5412 BMWalker walker;
5413 BMIter iter;
5414 int walktype = 0, itertype = 0, flushtype = 0;
5415 short mask_vert = 0, mask_edge = 0, mask_face = 0;
5416
5417 /* No active element from which to start - nothing to do. */
5418 if (h_act == nullptr) {
5419 return;
5420 }
5421
5422 /* Note on cyclic-chain handling here:
5423 *
5424 * The use of a breadth first search to determine element order
5425 * causes problems with cyclic topology.
5426 *
5427 * The walker ordered vertices by their graph depth from the active element.
5428 * This approach was failing on loops like a circle because the breadth first
5429 * search expanded in two directions simultaneously, creating a symmetrical
5430 * but non sequential depth map, see: #126909. */
5431 if (h_act->htype == BM_VERT) {
5432 BMVert *v_start = reinterpret_cast<BMVert *>(h_act);
5433 if (bm_verts_form_cyclic_chain(v_start)) {
5434 walker_deselect_nth_vertex_chain(bm, op_params, v_start);
5436 return;
5437 }
5438 }
5439 else if (h_act->htype == BM_EDGE) {
5440 BMEdge *e_start = reinterpret_cast<BMEdge *>(h_act);
5441 if (bm_edges_form_cyclic_chain(e_start)) {
5442 walker_deselect_nth_edge_chain(bm, op_params, e_start);
5444 return;
5445 }
5446 }
5447 else if (h_act->htype == BM_FACE) {
5448 BMFace *f_start = reinterpret_cast<BMFace *>(h_act);
5449 if (bm_faces_form_cyclic_chain(f_start)) {
5450 walker_deselect_nth_face_chain(bm, op_params, f_start);
5452 return;
5453 }
5454 }
5455
5456 /* Determine which type of iterator, walker, and select flush to use
5457 * based on type of the elements being deselected. */
5458 switch (h_act->htype) {
5459 case BM_VERT:
5460 itertype = BM_VERTS_OF_MESH;
5461 walktype = BMW_CONNECTED_VERTEX;
5462 flushtype = SCE_SELECT_VERTEX;
5463 mask_vert = BMO_ELE_TAG;
5464 break;
5465 case BM_EDGE:
5466 /* When an edge has no connected-selected edges,
5467 * use face-stepping (supports edge-rings). */
5468 itertype = BM_EDGES_OF_MESH;
5469 walktype = bm_edge_is_select_isolated(reinterpret_cast<BMEdge *>(h_act)) ? BMW_FACE_SHELL :
5471 flushtype = SCE_SELECT_EDGE;
5472 mask_edge = BMO_ELE_TAG;
5473 break;
5474 case BM_FACE:
5475 itertype = BM_FACES_OF_MESH;
5476 walktype = BMW_ISLAND;
5477 flushtype = SCE_SELECT_FACE;
5478 mask_face = BMO_ELE_TAG;
5479 break;
5480 }
5481
5482 /* Shouldn't need to allocate BMO flags here (sigh). */
5484
5485 /* Walker restrictions uses BMO flags, not header flags,
5486 * so transfer BM_ELEM_SELECT from HFlags onto a BMO flag layer. */
5487 BMO_push(bm, nullptr);
5488 BM_ITER_MESH (ele, &iter, bm, itertype) {
5491 }
5492 }
5493
5494 /* Walk over selected elements starting at active. */
5495 BMW_init(&walker,
5496 bm,
5497 walktype,
5498 mask_vert,
5499 mask_edge,
5500 mask_face,
5501 BMW_FLAG_NOP, /* Don't use #BMW_FLAG_TEST_HIDDEN here since we want to deselect all. */
5504
5505 /* Use tag to avoid touching the same elems twice. */
5506 BM_ITER_MESH (ele, &iter, bm, itertype) {
5508 }
5509
5511 for (ele = static_cast<BMElem *>(BMW_begin(&walker, h_act)); ele != nullptr;
5512 ele = static_cast<BMElem *>(BMW_step(&walker)))
5513 {
5514 if (!BM_elem_flag_test(ele, BM_ELEM_TAG)) {
5515 /* Deselect elements that aren't at "nth" depth from active. */
5516 const int depth = BMW_current_depth(&walker) - 1;
5517 if (!WM_operator_properties_checker_interval_test(op_params, depth)) {
5518 BM_elem_select_set(bm, ele, false);
5519 }
5521 }
5522 }
5523 BMW_end(&walker);
5524
5525 BMO_pop(bm);
5526
5527 /* Flush selection up. */
5528 EDBM_selectmode_flush_ex(bm, flushtype);
5529}
5530
5532 BMesh *bm, const short selectmode, BMVert **r_eve, BMEdge **r_eed, BMFace **r_efa)
5533{
5534 BMIter iter;
5535 BMElem *ele;
5536
5537 *r_eve = nullptr;
5538 *r_eed = nullptr;
5539 *r_efa = nullptr;
5540
5541 EDBM_selectmode_flush(bm, selectmode);
5543
5544 if (ele && BM_elem_flag_test(ele, BM_ELEM_SELECT)) {
5545 switch (ele->head.htype) {
5546 case BM_VERT:
5547 *r_eve = reinterpret_cast<BMVert *>(ele);
5548 return;
5549 case BM_EDGE:
5550 *r_eed = reinterpret_cast<BMEdge *>(ele);
5551 return;
5552 case BM_FACE:
5553 *r_efa = reinterpret_cast<BMFace *>(ele);
5554 return;
5555 }
5556 }
5557
5558 if (selectmode & SCE_SELECT_VERTEX) {
5559 BMVert *v;
5560 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
5562 *r_eve = v;
5563 return;
5564 }
5565 }
5566 }
5567 else if (selectmode & SCE_SELECT_EDGE) {
5568 BMEdge *e;
5569 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
5571 *r_eed = e;
5572 return;
5573 }
5574 }
5575 }
5576 else if (selectmode & SCE_SELECT_FACE) {
5577 BMFace *f = BM_mesh_active_face_get(bm, true, false);
5578 if (f && BM_elem_flag_test(f, BM_ELEM_SELECT)) {
5579 *r_efa = f;
5580 return;
5581 }
5582 }
5583}
5584
5586 const short selectmode,
5587 const CheckerIntervalParams *op_params)
5588{
5589 BMVert *v;
5590 BMEdge *e;
5591 BMFace *f;
5592
5593 deselect_nth_active(bm, selectmode, &v, &e, &f);
5594
5595 if (v) {
5596 walker_deselect_nth(bm, op_params, &v->head);
5597 return true;
5598 }
5599 if (e) {
5600 walker_deselect_nth(bm, op_params, &e->head);
5601 return true;
5602 }
5603 if (f) {
5604 walker_deselect_nth(bm, op_params, &f->head);
5605 return true;
5606 }
5607
5608 return false;
5609}
5610
5612{
5613 const Main *bmain = CTX_data_main(C);
5614 const Scene *scene = CTX_data_scene(C);
5615 ViewLayer *view_layer = CTX_data_view_layer(C);
5616 CheckerIntervalParams op_params;
5618 bool found_active_elt = false;
5619
5621 *bmain, scene, view_layer, CTX_wm_view3d(C));
5622
5623 for (Object *obedit : objects) {
5626 if ((bm->totvertsel == 0) && (bm->totedgesel == 0) && (bm->totfacesel == 0)) {
5627 continue;
5628 }
5629
5630 if (edbm_deselect_nth(bm, em->selectmode, &op_params) == true) {
5632
5633 found_active_elt = true;
5634 EDBMUpdate_Params params{};
5635 params.calc_looptris = false;
5636 params.calc_normals = false;
5637 params.is_destructive = false;
5638 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
5639 }
5640 }
5641
5642 if (!found_active_elt) {
5643 BKE_report(op->reports, RPT_ERROR, "Mesh object(s) have no active vertex/edge/face");
5644 return OPERATOR_CANCELLED;
5645 }
5646
5647 return OPERATOR_FINISHED;
5648}
5649
5651{
5652 /* Identifiers. */
5653 ot->name = "Checker Deselect";
5654 ot->idname = "MESH_OT_select_nth";
5655 ot->description = "Deselect every Nth element starting from the active vertex, edge or face";
5656
5657 /* API callbacks. */
5660
5661 /* Flags. */
5663
5665}
5666
5668{
5669 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
5670 ViewContext vc = ED_view3d_viewcontext_init(C, depsgraph);
5671
5672 if (vc.obedit) {
5674 }
5675 return vc;
5676}
5677
5679
5680/* -------------------------------------------------------------------- */
5683
5685{
5686 /* Find edges that have exactly two neighboring faces,
5687 * check the angle between those faces, and if angle is
5688 * small enough, select the edge. */
5689 const float angle_limit_cos = cosf(RNA_float_get(op->ptr, "sharpness"));
5690
5691 const Main *bmain = CTX_data_main(C);
5692 const Scene *scene = CTX_data_scene(C);
5693 ViewLayer *view_layer = CTX_data_view_layer(C);
5695 *bmain, scene, view_layer, CTX_wm_view3d(C));
5696
5697 for (Object *obedit : objects) {
5700 BMIter iter;
5701 BMEdge *e;
5702
5703 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
5705 continue;
5706 }
5707
5708 BMLoop *l_a, *l_b;
5709 if (BM_edge_loop_pair(e, &l_a, &l_b)) {
5710 /* Edge has exactly two neighboring faces, check angle. */
5711 const float angle_cos = dot_v3v3(l_a->f->no, l_b->f->no);
5712
5713 if (angle_cos < angle_limit_cos) {
5714 BM_edge_select_set(bm, e, true);
5715 }
5716 }
5717 }
5718
5719 if ((bm->selectmode & (SCE_SELECT_VERTEX | SCE_SELECT_EDGE)) == 0) {
5720 /* Since we can't select individual edges, select faces connected to them. */
5722 }
5723 else {
5725 }
5727
5728 DEG_id_tag_update(obedit->data, ID_RECALC_SELECT);
5729 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
5730 }
5731
5732 return OPERATOR_FINISHED;
5733}
5734
5736{
5737 PropertyRNA *prop;
5738
5739 /* Identifiers. */
5740 ot->name = "Select Sharp Edges";
5741 ot->description = "Select all sharp enough edges";
5742 ot->idname = "MESH_OT_edges_select_sharp";
5743
5744 /* API callbacks. */
5747
5748 /* Flags. */
5750
5751 /* Props. */
5752 prop = RNA_def_float_rotation(ot->srna,
5753 "sharpness",
5754 0,
5755 nullptr,
5756 DEG2RADF(0.01f),
5757 DEG2RADF(180.0f),
5758 "Sharpness",
5759 "",
5760 DEG2RADF(1.0f),
5761 DEG2RADF(180.0f));
5763}
5764
5766
5767/* -------------------------------------------------------------------- */
5770
5772{
5773 const Main *bmain = CTX_data_main(C);
5774 const Scene *scene = CTX_data_scene(C);
5775 ViewLayer *view_layer = CTX_data_view_layer(C);
5777 *bmain, scene, view_layer, CTX_wm_view3d(C));
5778 const float angle_limit_cos = cosf(RNA_float_get(op->ptr, "sharpness"));
5779
5780 for (Object *obedit : objects) {
5782 if (bm->totfacesel == 0) {
5783 continue;
5784 }
5785
5786 Vector<BMFace *> stack;
5787
5788 BMIter iter, liter, liter2;
5789 BMFace *f;
5790 BMLoop *l, *l2;
5791
5793
5794 BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
5795 if ((BM_elem_flag_test(f, BM_ELEM_HIDDEN) != 0) ||
5797 {
5798 continue;
5799 }
5800
5801 BLI_assert(stack.is_empty());
5802
5803 do {
5804 BM_face_select_set(bm, f, true);
5805
5807
5808 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
5809 BM_ITER_ELEM (l2, &liter2, l, BM_LOOPS_OF_LOOP) {
5810 float angle_cos;
5811
5813 {
5814 continue;
5815 }
5816
5817 angle_cos = dot_v3v3(f->no, l2->f->no);
5818
5819 if (angle_cos > angle_limit_cos) {
5820 stack.append(l2->f);
5821 }
5822 }
5823 }
5824 } while (!stack.is_empty() && (f = stack.pop_last()));
5825 }
5826
5828 DEG_id_tag_update(obedit->data, ID_RECALC_SELECT);
5829 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
5830 }
5831
5832 return OPERATOR_FINISHED;
5833}
5834
5836{
5837 PropertyRNA *prop;
5838
5839 /* Identifiers. */
5840 ot->name = "Select Linked Flat Faces";
5841 ot->description = "Select linked faces by angle";
5842 ot->idname = "MESH_OT_faces_select_linked_flat";
5843
5844 /* API callbacks. */
5847
5848 /* Flags. */
5850
5851 /* Props. */
5852 prop = RNA_def_float_rotation(ot->srna,
5853 "sharpness",
5854 0,
5855 nullptr,
5856 DEG2RADF(0.01f),
5857 DEG2RADF(180.0f),
5858 "Sharpness",
5859 "",
5860 DEG2RADF(1.0f),
5861 DEG2RADF(180.0f));
5863}
5864
5866
5867/* -------------------------------------------------------------------- */
5870
5872{
5873 const bool use_extend = RNA_boolean_get(op->ptr, "extend");
5874 const bool use_wire = RNA_boolean_get(op->ptr, "use_wire");
5875 const bool use_boundary = RNA_boolean_get(op->ptr, "use_boundary");
5876 const bool use_multi_face = RNA_boolean_get(op->ptr, "use_multi_face");
5877 const bool use_non_contiguous = RNA_boolean_get(op->ptr, "use_non_contiguous");
5878 const bool use_verts = RNA_boolean_get(op->ptr, "use_verts");
5879
5880 const Main *bmain = CTX_data_main(C);
5881 const Scene *scene = CTX_data_scene(C);
5882 ViewLayer *view_layer = CTX_data_view_layer(C);
5884 *bmain, scene, view_layer, CTX_wm_view3d(C));
5885
5887
5888 for (Object *obedit : objects) {
5891 BMVert *v;
5892 BMEdge *e;
5893 BMIter iter;
5894
5895 bool changed = false;
5896
5897 if (!use_extend) {
5899 changed = true;
5900 }
5901
5902 /* Selects isolated verts, and edges that do not have 2 neighboring faces. */
5903 if (use_verts) {
5904 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
5906 continue;
5907 }
5908
5909 if (!BM_vert_is_manifold(v)) {
5910 BM_vert_select_set(bm, v, true);
5911 changed = true;
5912 }
5913 }
5914 }
5915
5916 if (use_wire || use_boundary || use_multi_face || use_non_contiguous) {
5917 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
5919 continue;
5920 }
5921 if ((use_wire && BM_edge_is_wire(e)) || (use_boundary && BM_edge_is_boundary(e)) ||
5922 (use_non_contiguous && (BM_edge_is_manifold(e) && !BM_edge_is_contiguous(e))) ||
5923 (use_multi_face && BM_edge_face_count_is_over(e, 2)))
5924 {
5925 /* Check we never select perfect edge (in test above). */
5927
5928 BM_edge_select_set(bm, e, true);
5929 changed = true;
5930 }
5931 }
5932 }
5933
5934 if (changed) {
5935 DEG_id_tag_update(obedit->data, ID_RECALC_SELECT);
5936 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
5937
5940 }
5941 }
5942
5943 return OPERATOR_FINISHED;
5944}
5945
5947{
5948 /* Identifiers. */
5949 ot->name = "Select Non-Manifold";
5950 ot->description = "Select all non-manifold vertices or edges";
5951 ot->idname = "MESH_OT_select_non_manifold";
5952
5953 /* API callbacks */
5956
5957 /* Flags. */
5959
5960 /* Props. */
5961 RNA_def_boolean(ot->srna, "extend", true, "Extend", "Extend the selection");
5962 /* Edges. */
5963 RNA_def_boolean(ot->srna, "use_wire", true, "Wire", "Wire edges");
5964 RNA_def_boolean(ot->srna, "use_boundary", true, "Boundaries", "Boundary edges");
5966 ot->srna, "use_multi_face", true, "Multiple Faces", "Edges shared by more than two faces");
5968 "use_non_contiguous",
5969 true,
5970 "Non Contiguous",
5971 "Edges between faces pointing in alternate directions");
5972 /* Verts. */
5974 ot->srna, "use_verts", true, "Vertices", "Vertices connecting multiple face regions");
5975}
5976
5978
5979/* -------------------------------------------------------------------- */
5982
5984{
5985 const bool select = (RNA_enum_get(op->ptr, "action") == SEL_SELECT);
5986 const float randfac = RNA_float_get(op->ptr, "ratio");
5988
5989 const Main *bmain = CTX_data_main(C);
5990 const Scene *scene = CTX_data_scene(C);
5991 ViewLayer *view_layer = CTX_data_view_layer(C);
5992
5994 *bmain, scene, view_layer, CTX_wm_view3d(C));
5995 for (const int ob_index : objects.index_range()) {
5996 Object *obedit = objects[ob_index];
5999 BMIter iter;
6000 int seed_iter = seed;
6001
6002 /* This gives a consistent result regardless of object order. */
6003 if (ob_index) {
6004 seed_iter += BLI_ghashutil_strhash_p(obedit->id.name);
6005 }
6006
6007 if (em->selectmode & SCE_SELECT_VERTEX) {
6008 int elem_map_len = 0;
6009 BMVert **elem_map = MEM_new_array_uninitialized<BMVert *>(bm->totvert, __func__);
6010 BMVert *eve;
6011 BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
6012 if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN)) {
6013 elem_map[elem_map_len++] = eve;
6014 }
6015 }
6016
6017 BLI_array_randomize(elem_map, sizeof(*elem_map), elem_map_len, seed_iter);
6018 const int count_select = elem_map_len * randfac;
6019 for (int i = 0; i < count_select; i++) {
6020 BM_vert_select_set(bm, elem_map[i], select);
6021 }
6022 MEM_delete(elem_map);
6023 }
6024 else if (em->selectmode & SCE_SELECT_EDGE) {
6025 int elem_map_len = 0;
6026 BMEdge **elem_map = MEM_new_array_uninitialized<BMEdge *>(bm->totedge, __func__);
6027 BMEdge *eed;
6028 BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
6029 if (!BM_elem_flag_test(eed, BM_ELEM_HIDDEN)) {
6030 elem_map[elem_map_len++] = eed;
6031 }
6032 }
6033 BLI_array_randomize(elem_map, sizeof(*elem_map), elem_map_len, seed_iter);
6034 const int count_select = elem_map_len * randfac;
6035 for (int i = 0; i < count_select; i++) {
6036 BM_edge_select_set(bm, elem_map[i], select);
6037 }
6038 MEM_delete(elem_map);
6039 }
6040 else {
6041 int elem_map_len = 0;
6042 BMFace **elem_map = MEM_new_array_uninitialized<BMFace *>(bm->totface, __func__);
6043 BMFace *efa;
6044 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
6045 if (!BM_elem_flag_test(efa, BM_ELEM_HIDDEN)) {
6046 elem_map[elem_map_len++] = efa;
6047 }
6048 }
6049 BLI_array_randomize(elem_map, sizeof(*elem_map), elem_map_len, seed_iter);
6050 const int count_select = elem_map_len * randfac;
6051 for (int i = 0; i < count_select; i++) {
6052 BM_face_select_set(bm, elem_map[i], select);
6053 }
6054 MEM_delete(elem_map);
6055 }
6056
6057 if (select) {
6058 /* Was #EDBM_select_flush_from_verts, but it over selects in edge/face mode. */
6060 }
6061 else {
6063 }
6065
6068 }
6069
6070 return OPERATOR_FINISHED;
6071}
6072
6074{
6075 /* Identifiers. */
6076 ot->name = "Select Random";
6077 ot->description = "Randomly select vertices";
6078 ot->idname = "MESH_OT_select_random";
6079
6080 /* API callbacks */
6083
6084 /* Flags. */
6086
6087 /* Props. */
6089}
6090
6092
6093/* -------------------------------------------------------------------- */
6096
6098{
6099 if (ED_operator_editmesh(C)) {
6100 Object *obedit = CTX_data_edit_object(C);
6102 const BMesh *bm = BKE_editmesh_bmesh_get(obedit);
6103 const int cd_dvert_offset = CustomData_get_offset(&bm->vdata, CD_MDEFORMVERT);
6104
6105 const ListBaseT<bDeformGroup> *defbase = BKE_object_defgroup_list(obedit);
6106 if ((em->selectmode & SCE_SELECT_VERTEX) == 0) {
6107 CTX_wm_operator_poll_msg_set(C, "Must be in vertex selection mode");
6108 }
6109 else if (defbase->is_empty() || cd_dvert_offset == -1) {
6110 CTX_wm_operator_poll_msg_set(C, "No weights/vertex groups on object");
6111 }
6112 else {
6113 return true;
6114 }
6115 }
6116 return false;
6117}
6118
6120{
6121 const bool extend = RNA_boolean_get(op->ptr, "extend");
6122 const Main *bmain = CTX_data_main(C);
6123 const Scene *scene = CTX_data_scene(C);
6124 ViewLayer *view_layer = CTX_data_view_layer(C);
6125
6127 *bmain, scene, view_layer, CTX_wm_view3d(C));
6128
6129 for (Object *obedit : objects) {
6132 const int cd_dvert_offset = CustomData_get_offset(&bm->vdata, CD_MDEFORMVERT);
6133
6134 if (cd_dvert_offset == -1) {
6135 continue;
6136 }
6137
6138 BMVert *eve;
6139 BMIter iter;
6140
6141 bool changed = false;
6142
6143 if (!extend) {
6144 if (bm->totvertsel) {
6146 changed = true;
6147 }
6148 }
6149
6150 BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
6151 if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN)) {
6152 MDeformVert *dv = static_cast<MDeformVert *>(BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset));
6153 /* Skip `dv` or `dv` set with zero weight. */
6154 if (ELEM(nullptr, dv, dv->dw)) {
6155 BM_vert_select_set(bm, eve, true);
6156 changed = true;
6157 }
6158 }
6159 }
6160
6161 if (changed) {
6164
6165 DEG_id_tag_update(obedit->data, ID_RECALC_SELECT);
6166 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
6167 }
6168 }
6169 return OPERATOR_FINISHED;
6170}
6171
6173{
6174 /* Identifiers. */
6175 ot->name = "Select Ungrouped";
6176 ot->idname = "MESH_OT_select_ungrouped";
6177 ot->description = "Select vertices without a group";
6178
6179 /* API callbacks. */
6182
6183 /* Flags. */
6185
6186 RNA_def_boolean(ot->srna, "extend", false, "Extend", "Extend the selection");
6187}
6188
6190
6191/* -------------------------------------------------------------------- */
6194
6195enum {
6199};
6200
6202{
6203 const Main *bmain = CTX_data_main(C);
6204 Scene *scene = CTX_data_scene(C);
6205 ViewLayer *view_layer = CTX_data_view_layer(C);
6206 Object *obedit = CTX_data_edit_object(C);
6207 BMesh *bm_act = BKE_editmesh_bmesh_get_for_write(obedit);
6208 BMVert *v_act = BM_mesh_active_vert_get(bm_act);
6209 const int orientation = RNA_enum_get(op->ptr, "orientation");
6210 const int axis = RNA_enum_get(op->ptr, "axis");
6211 const int sign = RNA_enum_get(op->ptr, "sign");
6212
6213 if (v_act == nullptr) {
6214 BKE_report(
6215 op->reports, RPT_WARNING, "This operator requires an active vertex (last selected)");
6216 return OPERATOR_CANCELLED;
6217 }
6218
6219 const float limit = RNA_float_get(op->ptr, "threshold");
6220
6221 float value;
6222 float axis_mat[3][3];
6223
6224 /* 3D view variables may be nullptr, (no need to check in poll function). */
6226 scene,
6227 view_layer,
6230 obedit,
6231 obedit,
6232 orientation,
6234 axis_mat);
6235
6236 const float *axis_vector = axis_mat[axis];
6237
6238 {
6239 float vertex_world[3];
6240 mul_v3_m4v3(vertex_world, obedit->object_to_world().ptr(), v_act->co);
6241 value = dot_v3v3(axis_vector, vertex_world);
6242 }
6243
6244 if (sign == SELECT_AXIS_NEG) {
6245 value += limit;
6246 }
6247 else if (sign == SELECT_AXIS_POS) {
6248 value -= limit;
6249 }
6250
6252 *bmain, scene, view_layer, CTX_wm_view3d(C));
6253 for (Object *obedit_iter : objects) {
6254 BMEditMesh *em_iter = BKE_editmesh_from_object(obedit_iter);
6256
6257 if (bm->totvert == bm->totvertsel) {
6258 continue;
6259 }
6260
6261 BMIter iter;
6262 BMVert *v;
6263 bool changed = false;
6264
6265 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
6267 float v_iter_world[3];
6268 mul_v3_m4v3(v_iter_world, obedit_iter->object_to_world().ptr(), v->co);
6269 const float value_iter = dot_v3v3(axis_vector, v_iter_world);
6270 switch (sign) {
6271 case SELECT_AXIS_ALIGN:
6272 if (fabsf(value_iter - value) < limit) {
6273 BM_vert_select_set(bm, v, true);
6274 changed = true;
6275 }
6276 break;
6277 case SELECT_AXIS_NEG:
6278 if (value_iter < value) {
6279 BM_vert_select_set(bm, v, true);
6280 changed = true;
6281 }
6282 break;
6283 case SELECT_AXIS_POS:
6284 if (value_iter > value) {
6285 BM_vert_select_set(bm, v, true);
6286 changed = true;
6287 }
6288 break;
6289 }
6290 }
6291 }
6292 if (changed) {
6295
6296 WM_event_add_notifier(C, NC_GEOM | ND_DATA, obedit_iter->data);
6297 DEG_id_tag_update(obedit_iter->data, ID_RECALC_SELECT);
6298 }
6299 }
6300 return OPERATOR_FINISHED;
6301}
6302
6304{
6305 static const EnumPropertyItem axis_sign_items[] = {
6306 {SELECT_AXIS_POS, "POS", false, "Positive Axis", ""},
6307 {SELECT_AXIS_NEG, "NEG", false, "Negative Axis", ""},
6308 {SELECT_AXIS_ALIGN, "ALIGN", false, "Aligned Axis", ""},
6309 {0, nullptr, 0, nullptr, nullptr},
6310 };
6311
6312 /* Identifiers. */
6313 ot->name = "Select Axis";
6314 ot->description = "Select all data in the mesh on a single axis";
6315 ot->idname = "MESH_OT_select_axis";
6316
6317 /* API callbacks. */
6320
6321 /* Flags. */
6323
6324 /* Properties. */
6325 RNA_def_enum(ot->srna,
6326 "orientation",
6329 "Axis Mode",
6330 "Axis orientation");
6331 RNA_def_enum(ot->srna, "sign", axis_sign_items, SELECT_AXIS_POS, "Axis Sign", "Side to select");
6332 RNA_def_enum(ot->srna,
6333 "axis",
6335 0,
6336 "Axis",
6337 "Select the axis to compare each vertex on");
6339 ot->srna, "threshold", 0.0001f, 0.000001f, 50.0f, "Threshold", "", 0.00001f, 10.0f);
6340}
6341
6343
6344/* -------------------------------------------------------------------- */
6347
6349{
6350 const Main *bmain = CTX_data_main(C);
6351 const Scene *scene = CTX_data_scene(C);
6352 ViewLayer *view_layer = CTX_data_view_layer(C);
6354 *bmain, scene, view_layer, CTX_wm_view3d(C));
6355 bool changed = false;
6356 for (Object *obedit : objects) {
6358 if (bm->totfacesel == 0) {
6359 continue;
6360 }
6361 BMFace *f;
6362 BMEdge *e;
6363 BMIter iter;
6364
6366
6367 BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
6368 BMLoop *l1, *l2;
6369 BMIter liter1, liter2;
6370
6371 BM_ITER_ELEM (l1, &liter1, f, BM_LOOPS_OF_FACE) {
6372 int tot = 0, totsel = 0;
6373
6374 BM_ITER_ELEM (l2, &liter2, l1->e, BM_LOOPS_OF_EDGE) {
6375 tot++;
6376 totsel += BM_elem_flag_test(l2->f, BM_ELEM_SELECT) != 0;
6377 }
6378
6379 if ((tot != totsel && totsel > 0) || (totsel == 1 && tot == 1)) {
6381 }
6382 }
6383 }
6384
6386
6387 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
6389 BM_edge_select_set(bm, e, true);
6390 changed = true;
6391 }
6392 }
6393
6395 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
6396 }
6397
6398 if (changed) {
6399 /* If in face-only select mode, switch to edge select mode so that
6400 * an edge-only selection is not inconsistent state. Do this for all meshes in multi-object
6401 * editmode so their selectmode is in sync for following operators. */
6403 }
6404
6405 return OPERATOR_FINISHED;
6406}
6407
6409{
6410 /* Identifiers. */
6411 ot->name = "Select Boundary of Selected";
6412 ot->idname = "MESH_OT_region_to_loop";
6413 ot->description = "Select boundary edges around the selected faces";
6414
6415 /* API callbacks. */
6418
6419 /* Flags. */
6421}
6422
6424
6425/* -------------------------------------------------------------------- */
6428
6430 int flag,
6431 Set<BMFace *> &visit_face_set,
6432 BMFace ***region_out)
6433{
6434 Vector<BMFace *> stack;
6435 Vector<BMFace *> region;
6436
6437 stack.append(l->f);
6438 visit_face_set.add(l->f);
6439
6440 while (!stack.is_empty()) {
6441 BMIter liter1, liter2;
6442 BMLoop *l1, *l2;
6443
6444 BMFace *f = stack.pop_last();
6445 region.append(f);
6446
6447 BM_ITER_ELEM (l1, &liter1, f, BM_LOOPS_OF_FACE) {
6448 if (BM_elem_flag_test(l1->e, flag)) {
6449 continue;
6450 }
6451
6452 BM_ITER_ELEM (l2, &liter2, l1->e, BM_LOOPS_OF_EDGE) {
6453 /* Avoids finding same region twice
6454 * (otherwise) the logic works fine without. */
6455 if (BM_elem_flag_test(l2->f, BM_ELEM_TAG)) {
6456 continue;
6457 }
6458
6459 if (visit_face_set.add(l2->f)) {
6460 stack.append(l2->f);
6461 }
6462 }
6463 }
6464 }
6465
6466 BMFace **region_alloc = MEM_new_array_uninitialized<BMFace *>(region.size(), __func__);
6467 memcpy(region_alloc, region.data(), region.as_span().size_in_bytes());
6468 *region_out = region_alloc;
6469 return region.size();
6470}
6471
6472static int verg_radial(const void *va, const void *vb)
6473{
6474 const BMEdge *e_a = *(static_cast<const BMEdge **>(const_cast<void *>(va)));
6475 const BMEdge *e_b = *(static_cast<const BMEdge **>(const_cast<void *>(vb)));
6476
6477 const int a = BM_edge_face_count(e_a);
6478 const int b = BM_edge_face_count(e_b);
6479
6480 if (a > b) {
6481 return -1;
6482 }
6483 if (a < b) {
6484 return 1;
6485 }
6486 return 0;
6487}
6488
6495static int loop_find_regions(BMesh *bm, const bool selbigger)
6496{
6497 BMIter iter;
6498 const int edges_len = bm->totedgesel;
6499 BMEdge *e;
6500 int count = 0, i;
6501
6502 Set<BMFace *> visit_face_set;
6503 BMEdge **edges = MEM_new_array_uninitialized<BMEdge *>(edges_len, __func__);
6504
6505 i = 0;
6506 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
6508 edges[i++] = e;
6510 }
6511 else {
6513 }
6514 }
6515
6516 /* Sort edges by radial cycle length. */
6517 qsort(edges, edges_len, sizeof(*edges), verg_radial);
6518
6519 for (i = 0; i < edges_len; i++) {
6520 BMIter liter;
6521 BMLoop *l;
6522 BMFace **region = nullptr, **region_out;
6523 int c, tot = 0;
6524
6525 e = edges[i];
6526
6528 continue;
6529 }
6530
6531 BM_ITER_ELEM (l, &liter, e, BM_LOOPS_OF_EDGE) {
6532 if (visit_face_set.contains(l->f)) {
6533 continue;
6534 }
6535
6536 c = loop_find_region(l, BM_ELEM_SELECT, visit_face_set, &region_out);
6537
6538 if (!region || (selbigger ? c >= tot : c < tot)) {
6539 /* This region is the best seen so far. */
6540 tot = c;
6541 if (region) {
6542 /* Free the previous best. */
6543 MEM_delete(region);
6544 }
6545 /* Track the current region as the new best. */
6546 region = region_out;
6547 }
6548 else {
6549 /* This region is not as good as best so far, just free it. */
6550 MEM_delete(region_out);
6551 }
6552 }
6553
6554 if (region) {
6555 int j;
6556
6557 for (j = 0; j < tot; j++) {
6558 BM_elem_flag_enable(region[j], BM_ELEM_TAG);
6559 BM_ITER_ELEM (l, &liter, region[j], BM_LOOPS_OF_FACE) {
6561 }
6562 }
6563
6564 count += tot;
6565
6566 MEM_delete(region);
6567 }
6568 }
6569
6570 MEM_delete(edges);
6571
6572 return count;
6573}
6574
6576{
6577 const bool select_bigger = RNA_boolean_get(op->ptr, "select_bigger");
6578
6579 const Main *bmain = CTX_data_main(C);
6580 const Scene *scene = CTX_data_scene(C);
6581 ViewLayer *view_layer = CTX_data_view_layer(C);
6583 *bmain, scene, view_layer, CTX_wm_view3d(C));
6584 for (Object *obedit : objects) {
6587 if (bm->totedgesel == 0) {
6588 continue;
6589 }
6590
6591 BMIter iter;
6592 BMFace *f;
6593
6594 /* Find the set of regions with smallest number of total faces. */
6596 const int a = loop_find_regions(bm, select_bigger);
6597 const int b = loop_find_regions(bm, !select_bigger);
6598
6600 loop_find_regions(bm, ((a <= b) != select_bigger) ? select_bigger : !select_bigger);
6601
6602 /* Unlike most operators, always de-select all. */
6603 bool changed = true;
6605
6606 BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
6608 continue;
6609 }
6611 BM_face_select_set(bm, f, true);
6612 }
6613 }
6614
6615 if (changed) {
6618
6619 DEG_id_tag_update(obedit->data, ID_RECALC_SELECT);
6620 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
6621 }
6622 }
6623
6624 return OPERATOR_FINISHED;
6625}
6626
6628{
6629 /* Identifiers. */
6630 ot->name = "Select Loop Inner-Region";
6631 ot->idname = "MESH_OT_loop_to_region";
6632 ot->description = "Select region of faces inside of a selected loop of edges";
6633
6634 /* API callbacks. */
6637
6638 /* Flags. */
6640
6642 "select_bigger",
6643 false,
6644 "Select Bigger",
6645 "Select bigger regions instead of smaller ones");
6646}
6647
6649{
6650 if (!ED_operator_editmesh(C)) {
6651 return false;
6652 }
6653 Object *obedit = CTX_data_edit_object(C);
6654 const Mesh *mesh = id_cast<const Mesh *>(obedit->data);
6655 AttributeOwner owner = AttributeOwner::from_id(&const_cast<ID &>(mesh->id));
6656 const std::optional<StringRef> name = BKE_attributes_active_name_get(owner);
6657 if (!name) {
6658 CTX_wm_operator_poll_msg_set(C, "There must be an active attribute");
6659 return false;
6660 }
6662 const BMDataLayerLookup attr = BM_data_layer_lookup(*bm, *name);
6663 if (attr.type != bke::AttrType::Bool) {
6664 CTX_wm_operator_poll_msg_set(C, "The active attribute must have a boolean type");
6665 return false;
6666 }
6667 if (attr.domain == bke::AttrDomain::Corner) {
6669 C, "The active attribute must be on the vertex, edge, or face domain");
6670 return false;
6671 }
6672 return true;
6673}
6674
6675static std::optional<BMIterType> domain_to_iter_type(const bke::AttrDomain domain)
6676{
6677 switch (domain) {
6679 return BM_VERTS_OF_MESH;
6681 return BM_EDGES_OF_MESH;
6683 return BM_FACES_OF_MESH;
6684 default:
6685 return std::nullopt;
6686 }
6687}
6688
6690{
6691 const Main *bmain = CTX_data_main(C);
6692 const Scene *scene = CTX_data_scene(C);
6693 ViewLayer *view_layer = CTX_data_view_layer(C);
6695 *bmain, scene, view_layer, CTX_wm_view3d(C));
6696 for (Object *obedit : objects) {
6697 Mesh *mesh = id_cast<Mesh *>(obedit->data);
6701 const std::optional<StringRef> name = BKE_attributes_active_name_get(owner);
6702 if (!name) {
6703 continue;
6704 }
6705 const BMDataLayerLookup attr = BM_data_layer_lookup(*bm, *name);
6706 if (!attr) {
6707 continue;
6708 }
6709 if (attr.type != bke::AttrType::Bool) {
6710 continue;
6711 }
6712 if (attr.domain == bke::AttrDomain::Corner) {
6713 continue;
6714 }
6715 const std::optional<BMIterType> iter_type = domain_to_iter_type(attr.domain);
6716 if (!iter_type) {
6717 continue;
6718 }
6719
6720 bool changed = false;
6721 BMElem *elem;
6722 BMIter iter;
6723 BM_ITER_MESH (elem, &iter, bm, *iter_type) {
6725 continue;
6726 }
6727 if (BM_ELEM_CD_GET_BOOL(elem, attr.offset)) {
6728 BM_elem_select_set(bm, elem, true);
6729 changed = true;
6730 }
6731 }
6732
6733 if (changed) {
6736
6737 DEG_id_tag_update(obedit->data, ID_RECALC_SELECT);
6738 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
6739 }
6740 }
6741
6742 return OPERATOR_FINISHED;
6743}
6744
6746{
6747 ot->name = "Select by Attribute";
6748 ot->idname = "MESH_OT_select_by_attribute";
6749 ot->description = "Select elements based on the active boolean attribute";
6750
6753
6755}
6756
6758
6759} // namespace blender
Generic geometry attributes built on CustomData.
CustomData interface, see also DNA_customdata_types.h.
support for deformation groups and hooks.
General operations, lookup, etc. for blender objects.
A min-heap / priority queue ADT.
#define DEG2RADF(_deg)
Random number functions.
#define STACK_CLEAR(stack)
#define STACK_PUSH(stack, val)
#define STACK_DECLARE(stack)
#define STACK_SIZE(stack)
#define STACK_INIT(stack, stack_num)
#define TIP_(msgid)
Object is a sort of wrapper for general info.
#define RV3D_CLIPPING_ENABLED(v3d, rv3d)
#define V3D_PROJ_TEST_CLIP_CONTENT_DEFAULT
Definition ED_view3d.hh:331
#define V3D_PROJ_TEST_CLIP_DEFAULT
Definition ED_view3d.hh:325
#define XRAY_FLAG_ENABLED(v3d)
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DATA
Definition WM_types.hh:552
#define NC_SCENE
Definition WM_types.hh:391
#define ND_TOOLSETTINGS
Definition WM_types.hh:468
#define ND_SELECT
Definition WM_types.hh:551
#define UNUSED_FUNCTION(x)
#define BLI_assert_unreachable()
#define UNUSED_VARS(...)
#define POINTER_FROM_INT(i)
#define POINTER_AS_INT(i)
#define ELEM(...)
#define STREQ(a, b)
#define BM_ELEM_CD_GET_BOOL(ele, offset)
#define BM_FACE_FIRST_LOOP(p)
#define BM_ELEM_CD_GET_VOID_P(ele, offset)
#define BM_elem_index_get(ele)
#define BM_elem_flag_disable(ele, hflag)
#define BM_elem_flag_set(ele, hflag, val)
#define BM_elem_index_set(ele, index)
#define BM_elem_flag_test(ele, hflag)
#define BM_elem_flag_enable(ele, hflag)
#define BM_ITER_ELEM(ele, iter, data, itype)
#define BM_ITER_MESH(ele, iter, bm, itype)
#define BM_ITER_MESH_INDEX(ele, iter, bm, itype, indexvar)
#define BM_select_history_store(bm, ele)
#define BM_select_history_remove(bm, ele)
#define BMO_edge_flag_test(bm, e, oflag)
#define BMO_edge_flag_set(bm, e, oflag, val)
#define BMO_elem_flag_enable(bm, ele, oflag)
#define BM_edge_face_count_is_over(e, n)
#define BMW_NIL_LAY
#define BMW_MASK_NOP
#define BMW_ITER(ele, walker, data)
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
static AttributeOwner from_id(ID *id)
static std::optional< EditMeshSymmetryHelper > create_if_needed(Object *ob, uchar htype)
bool contains(const Key &key) const
Definition BLI_set.hh:310
bool add(const Key &key)
Definition BLI_set.hh:248
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
constexpr bool is_empty() const
Definition BLI_span.hh:264
int64_t size() const
void append(const T &value)
bool is_empty() const
IndexRange index_range() const
void reserve(const int64_t min_capacity)
Span< T > as_span() const
#define BMO_ELE_TAG
#define FIND_NEAR_SELECT_BIAS
#define FIND_NEAR_CYCLE_THRESHOLD_MIN
#define FLT_MAX
#define INT_MAX
constexpr T sign(T) RET
#define UINT_MAX
Definition hash_md5.cc:46
void base_activate(bContext *C, Base *base)
bool material_active_index_set(Object *ob, int index)
short calc_orientation_from_type_ex(const Main &bmain, const Scene *scene, ViewLayer *view_layer, const View3D *v3d, const RegionView3D *rv3d, Object *ob, Object *obedit, short orientation_index, int pivot_point, float r_mat[3][3])
BMesh Iterators.
MINLINE float min_ff(float a, float b)
void WM_main_add_notifier(uint type, void *reference)
void EDBM_flag_disable_all(BMesh *bm, char hflag)
std::optional< StringRefNull > BKE_attributes_active_name_get(AttributeOwner &owner)
void BM_edge_uvselect_set_pick(BMesh *bm, BMEdge *e, bool select, const BMUVSelectPickParams &params)
void BM_face_calc_center_median(const BMFace *f, float r_cent[3])
int CustomData_get_offset(const CustomData *data, eCustomDataType type)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
BMElem * EDBM_elem_from_selectmode(BMEditMesh *em, BMVert *eve, BMEdge *eed, BMFace *efa)
void void bool BLI_heap_is_empty(const Heap *heap) ATTR_NONNULL(1)
Definition BLI_heap.cc:271
@ V3D_PROJ_RET_OK
Definition ED_view3d.hh:273
void ED_uvedit_sync_uvselect_ensure_if_needed(const ToolSettings *ts, BMesh *bm)
bool EDBM_deselect_by_material(BMesh *bm, short index, bool select)
static wmOperatorStatus edbm_select_face_by_sides_exec(bContext *C, wmOperator *op)
const Mesh * BKE_object_get_editmesh_eval_cage(const Object *object)
Definition object.cc:4492
float angle_normalized_v3v3(const float v1[3], const float v2[3]) ATTR_WARN_UNUSED_RESULT
bool EDBM_mesh_deselect_all_multi_ex(Span< Base * > bases)
static wmOperatorStatus edbm_select_linked_pick_exec(bContext *C, wmOperator *op)
int BM_edge_face_count(const BMEdge *e)
void BM_mesh_elem_index_ensure(BMesh *bm, const char htype)
const EnumPropertyItem rna_enum_mesh_select_mode_items[]
Definition rna_scene.cc:135
bool BM_face_is_any_vert_flag_test(const BMFace *f, const char hflag)
Heap * BLI_heap_new_ex(unsigned int reserve_num) ATTR_WARN_UNUSED_RESULT
Definition BLI_heap.cc:173
BLI_INLINE bool BM_edge_is_wire(const BMEdge *e) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
int RNA_enum_get(PointerRNA *ptr, const char *name)
uint DRW_select_buffer_find_nearest_to_point(Depsgraph *depsgraph, ARegion *region, View3D *v3d, const int center[2], uint id_min, uint id_max, uint *dist)
void BM_mesh_elem_toolflags_ensure(BMesh *bm)
Definition bmesh_mesh.cc:78
bool EDBM_select_pick(bContext *C, const int mval[2], const SelectPick_Params &params)
static wmOperatorStatus edbm_select_loop_or_ring_exec_impl(bContext *C, wmOperator *op, bool ring, BMWDelimitFlag delimit)
static void mouse_mesh_loop_face(BMesh *bm, BMEdge *eed, bool select, bool select_clear, BMWDelimitFlag delimit)
static wmOperatorStatus edbm_select_mode_exec(bContext *C, wmOperator *op)
static void edbm_strip_selections(BMesh *bm, const short selectmode)
Object * BKE_view_layer_active_object_get(const ViewLayer *view_layer)
@ BMW_FLAG_TEST_HIDDEN
@ RPT_WARNING
Definition BKE_report.hh:40
bool BM_mesh_uvselect_clear(BMesh *bm)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
void EDBM_selectmode_flush(BMesh *bm, short selectmode)
void EDBM_select_more(BMesh *bm, short selectmode, bool use_face_step)
static wmOperatorStatus edbm_select_ring_invoke(bContext *C, wmOperator *op, const wmEvent *event)
const ListBaseT< bDeformGroup > * BKE_object_defgroup_list(const Object *ob)
Definition deform.cc:591
static wmOperatorStatus edbm_faces_select_interior_exec(bContext *C, wmOperator *)
BMesh * BKE_editmesh_bmesh_get_for_write(Mesh *mesh)
Definition editmesh.cc:79
void WM_operator_properties_select_random(wmOperatorType *ot)
static BMEdge * bm_step_over_vert_to_next_selected_edge_in_chain(const BMEdge *e_curr, BMVert *v)
ViewContext ED_view3d_viewcontext_init(bContext *C, Depsgraph *depsgraph)
HeapNode * BLI_heap_top(const Heap *heap) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition BLI_heap.cc:281
unsigned int uint
Definition BKE_text.h:55
static bool edbm_select_ring_poll_property(const bContext *C, wmOperator *, const PropertyRNA *prop)
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
int BM_mesh_region_match(BMesh *bm, BMFace **faces_region, uint faces_region_len, ListBaseT< LinkData > *r_face_regions)
static void select_linked_delimit_begin(BMesh *bm, int delimit)
const EnumPropertyItem rna_enum_mesh_walk_delimit_face_loop_items[]
Definition rna_mesh.cc:79
void copy_m3_m4(float m1[3][3], const float m2[4][4])
void MESH_OT_select_by_pole_count(wmOperatorType *ot)
ATTR_WARN_UNUSED_RESULT const BMLoop * l
static bool edbm_selectmode_sync_multi_ex(Span< Object * > objects)
void BM_mesh_elem_table_ensure(BMesh *bm, const char htype)
BMVert * EDBM_verts_mirror_get(BMEditMesh *em, BMesh *bm, BMVert *v)
void * BMW_begin(BMWalker *walker, void *start)
void void BLI_heap_remove(Heap *heap, HeapNode *node) ATTR_NONNULL(1
Definition BLI_heap.cc:309
bool CustomData_has_layer(const CustomData *data, eCustomDataType type)
@ BMW_CONNECTED_VERTEX
@ BMW_LOOP_SHELL_WIRE
@ BMW_EDGELOOP_NONMANIFOLD
void BM_mesh_uvselect_mode_flush_only_select(BMesh *bm)
eV3DProjStatus ED_view3d_project_float_object(const ARegion *region, const float co[3], float r_co[2], eV3DProjTest flag)
static wmOperatorStatus edbm_select_loose_exec(bContext *C, wmOperator *op)
static void walker_deselect_nth_face_chain(BMesh *bm, const CheckerIntervalParams *op_params, BMFace *f_start)
static void select_linked_delimit_end(BMesh *bm)
void MESH_OT_select_more(wmOperatorType *ot)
static std::string edbm_select_mode_get_description(bContext *, wmOperatorType *, PointerRNA *ptr)
const char * RNA_property_identifier(const PropertyRNA *prop)
void mul_v3_m4v3(float r[3], const float mat[4][4], const float vec[3])
bool EDBM_selectmode_disable(Scene *scene, BMEditMesh *em, BMesh *bm, short selectmode_disable, short selectmode_fallback)
MINLINE void copy_v2_v2(float r[2], const float a[2])
void MESH_OT_select_less(wmOperatorType *ot)
static bool unified_findnearest(ViewContext *vc, const Span< Base * > bases, int *r_base_index, BMVert **r_eve, BMEdge **r_eed, BMFace **r_efa)
BMEdge * EDBM_verts_mirror_get_edge(BMEditMesh *em, BMesh *bm, BMEdge *e)
int WM_operator_properties_select_random_seed_increment_get(wmOperator *op)
void BMW_init(BMWalker *walker, BMesh *bm, int type, short mask_vert, short mask_edge, short mask_face, BMWFlag flag, int layer, BMWDelimitFlag delimit)
Initialize Walker.
void MESH_OT_loop_select(wmOperatorType *ot)
BMVert * EDBM_vert_find_nearest_ex(ViewContext *vc, float *dist_px_manhattan_p, bool use_select_bias, bool use_cycle, Span< Base * > bases, uint *r_base_index)
BMVert * BM_vert_at_index_find_or_table(BMesh *bm, const int index)
void EDBM_update(Mesh *mesh, const EDBMUpdate_Params *params)
int RNA_property_enum_get(PointerRNA *ptr, PropertyRNA *prop)
static wmOperatorStatus edbm_loop_to_region_exec(bContext *C, wmOperator *op)
void RNA_int_set(PointerRNA *ptr, const char *name, int value)
bool DRW_select_buffer_elem_get(uint sel_id, uint &r_elem, uint &r_base_index, char &r_elem_type)
static void find_nearest_edge__doClosest(void *user_data, BMEdge *eed, const float screen_co_a[2], const float screen_co_b[2], int index)
const EnumPropertyItem rna_enum_axis_flag_xyz_items[]
@ BMW_BREADTH_FIRST
bool BM_edge_is_any_face_flag_test(const BMEdge *e, const char hflag)
void MESH_OT_select_edge_ring_multi(wmOperatorType *ot)
void EDBM_select_mirrored(BMEditMesh *em, Mesh *mesh, int axis, bool extend, int *r_totmirr, int *r_totfail)
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
void MESH_OT_select_ungrouped(wmOperatorType *ot)
static void edbm_select_linked_pick_ex(BMesh *bm, const short selectmode, BMElem *ele, bool sel, int delimit)
void MESH_OT_select_axis(wmOperatorType *ot)
void BLI_heap_free(Heap *heap, HeapFreeFP ptrfreefp) ATTR_NONNULL(1)
Definition BLI_heap.cc:193
bool BM_face_is_any_edge_flag_test(const BMFace *f, const char hflag)
void void void BLI_movelisttolist(ListBase *dst, ListBase *src) ATTR_NONNULL(1
Definition listbase.cc:32
int RNA_int_get(PointerRNA *ptr, const char *name)
static void find_nearest_face_center__doZBuf(void *user_data, BMFace *efa, const float screen_co[2], int)
void view3d_operator_needs_gpu(const bContext *C)
void BM_mesh_elem_hflag_disable_all(BMesh *bm, const char htype, const char hflag, const bool respecthide)
static wmOperatorStatus edbm_select_ring_exec(bContext *C, wmOperator *op)
Base * BKE_view_layer_active_base_get(ViewLayer *view_layer)
bool BM_vert_is_all_face_flag_test(const BMVert *v, const char hflag, const bool respect_hide)
static void select_linked_delimit_validate(BMesh *bm, int *delimit)
void EDBM_selectmode_convert(BMesh *bm, short selectmode_old, short selectmode_new)
static bool bm_faces_form_cyclic_chain(BMFace *f_start)
ATTR_WARN_UNUSED_RESULT const BMVert * v
static void edbm_select_loop_or_ring_properties(wmOperatorType *ot)
BLI_INLINE bool BM_edge_is_manifold(const BMEdge *e) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void WM_operator_properties_checker_interval(wmOperatorType *ot, bool nth_can_disable)
void MESH_OT_select_interior_faces(wmOperatorType *ot)
void EDBM_verts_mirror_cache_begin(BMEditMesh *em, BMesh *bm, int axis, bool use_self, bool use_select, bool respecthide, bool use_topology)
BMVert * EDBM_vert_find_nearest(ViewContext *vc, float *dist_px_manhattan_p)
bool EDBM_selectmode_toggle_multi(bContext *C, short selectmode_toggle, int action, bool use_extend, bool use_expand)
static const EnumPropertyItem elem_count_compare_items[]
BMElem * EDBM_elem_from_index_any_multi(const Main &bmain, const Scene *scene, ViewLayer *view_layer, uint object_index, uint elem_index, Object **r_obedit)
float BM_edge_calc_length(const BMEdge *e)
int BM_vert_edge_count_at_most(const BMVert *v, const int count_max)
static bool bm_interior_loop_filter_fn(const BMLoop *l, void *)
void MEM_delete_void(void *vmemh)
BLI_INLINE BMVert * BM_edge_other_vert(BMEdge *e, const BMVert *v) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
uint DRW_select_buffer_sample_point(Depsgraph *depsgraph, ARegion *region, View3D *v3d, const int center[2])
static wmOperatorStatus edbm_select_loop_invoke(bContext *C, wmOperator *op, const wmEvent *event)
void BM_vert_uvselect_set_pick(BMesh *bm, BMVert *v, bool select, const BMUVSelectPickParams &params)
void * BLI_heap_pop_min(Heap *heap) ATTR_NONNULL(1)
Definition BLI_heap.cc:293
static bool bm_edge_is_select_isolated(BMEdge *e)
ViewLayer * CTX_data_view_layer(const bContext *C)
static void deselect_nth_active(BMesh *bm, const short selectmode, BMVert **r_eve, BMEdge **r_eed, BMFace **r_efa)
void WM_operator_properties_select_all(wmOperatorType *ot)
bool EDBM_selectmode_set_multi(bContext *C, short selectmode)
static int select_linked_delimit_default_from_op(wmOperator *op, const int select_mode)
void BLI_array_randomize(void *data, unsigned int elem_size, unsigned int elem_num, unsigned int seed)
Definition rand.cc:158
const EnumPropertyItem rna_enum_mesh_walk_delimit_edge_ring_items[]
Definition rna_mesh.cc:59
void MESH_OT_select_linked_pick(wmOperatorType *ot)
static wmOperatorStatus edbm_select_loop_or_ring_invoke_impl(bContext *C, wmOperator *op, const wmEvent *event)
MINLINE int bitscan_forward_i(int a)
static bool bm_interior_edge_is_manifold_except_face_index(BMEdge *e, int face_index, BMLoop *r_l_pair[2])
void RNA_def_property_float_default(PropertyRNA *prop, float value)
void * BLI_pophead(ListBase *listbase) ATTR_NONNULL(1)
Definition listbase.cc:259
void BMO_push(BMesh *bm, BMOperator *op)
BMESH OPSTACK PUSH.
static bool edbm_vert_or_edge_select_mode_poll(bContext *C)
void RNA_property_int_set(PointerRNA *ptr, PropertyRNA *prop, int value)
void ED_view3d_viewcontext_init_object(ViewContext *vc, Object *obact)
void BM_face_calc_center_median_vcos(const BMesh *bm, const BMFace *f, float r_cent[3], const Span< float3 > vert_positions)
static wmOperatorStatus edbm_select_linked_pick_invoke(bContext *C, wmOperator *op, const wmEvent *event)
void mul_m4_v3(const float M[4][4], float r[3])
static wmOperatorStatus edbm_select_by_attribute_exec(bContext *C, wmOperator *)
bool EDBM_selectmode_disable_multi(bContext *C, short selectmode_disable, short selectmode_fallback)
bool ED_operator_editmesh(bContext *C)
static void walker_deselect_nth_vertex_chain(BMesh *bm, const CheckerIntervalParams *op_params, BMVert *v_start)
void CTX_wm_operator_poll_msg_set(bContext *C, const char *msg)
void BM_mesh_uvselect_mode_flush_update(BMesh *bm, const short selectmode_old, const short selectmode_new, const int cd_loop_uv_offset)
static wmOperatorStatus edbm_select_axis_exec(bContext *C, wmOperator *op)
void MESH_OT_select_mirror(wmOperatorType *ot)
void DRW_select_buffer_context_create(Depsgraph *depsgraph, Span< Base * > bases, short select_mode)
float BM_face_calc_area(const BMFace *f)
bool EDBM_unified_findnearest(ViewContext *vc, Span< Base * > bases, int *r_base_index, BMVert **r_eve, BMEdge **r_eed, BMFace **r_efa)
@ ME_EDIT_MIRROR_TOPO
void BMW_end(BMWalker *walker)
End Walker.
static void walker_select_count(BMesh *bm, int walkercode, void *start, int r_count_by_select[2], BMWFlag flags, BMWDelimitFlag delimit)
void DEG_id_tag_update(ID *id, unsigned int flags)
void BM_mesh_active_face_set(BMesh *bm, BMFace *f)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
BMesh const char void * data
static int loop_find_regions(BMesh *bm, const bool selbigger)
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
float line_point_factor_v2(const float p[2], const float l1[2], const float l2[2])
const EnumPropertyItem rna_enum_mesh_walk_delimit_edge_loop_items[]
Definition rna_mesh.cc:37
void MESH_OT_loop_to_region(wmOperatorType *ot)
BLI_INLINE bool BM_edge_is_contiguous(const BMEdge *e) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
static wmOperatorStatus edbm_select_nth_exec(bContext *C, wmOperator *op)
BMFace * EDBM_verts_mirror_get_face(BMEditMesh *em, BMesh *bm, BMFace *f)
bool WM_operator_properties_checker_interval_test(const CheckerIntervalParams *op_params, int depth)
Object * CTX_data_edit_object(const bContext *C)
static void findnearestface__doClosest(void *user_data, BMFace *efa, const float screen_co[2], int index)
void BM_elem_select_set(BMesh *bm, BMElem *ele, const bool select)
HeapNode * BLI_heap_insert(Heap *heap, float value, void *ptr) ATTR_NONNULL(1)
Definition BLI_heap.cc:236
void MESH_OT_select_face_by_sides(wmOperatorType *ot)
int EDBM_elem_to_index_any_multi(const Main &bmain, const Scene *scene, ViewLayer *view_layer, BMEditMesh *em, BMElem *ele, int *r_object_index)
BMFace * BM_face_at_index_find_or_table(BMesh *bm, const int index)
bool EDBM_select_interior_faces(BMesh *bm)
void BM_face_select_set(BMesh *bm, BMFace *f, const bool select)
Select Face.
static bool edbm_select_loop_poll_property(const bContext *C, wmOperator *, const PropertyRNA *prop)
ATTR_WARN_UNUSED_RESULT const BMLoop * l_b
bool BM_vert_is_all_edge_flag_test(const BMVert *v, const char hflag, const bool respect_hide)
void RNA_property_unset(PointerRNA *ptr, PropertyRNA *prop)
void MESH_OT_select_by_attribute(wmOperatorType *ot)
static bool edbm_deselect_nth(BMesh *bm, const short selectmode, const CheckerIntervalParams *op_params)
static bool bm_verts_form_cyclic_chain(BMVert *v_start)
void BLI_freelinkN(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:277
bool BKE_view_layer_synced_ensure(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
const EnumPropertyItem rna_enum_transform_orientation_items[]
Definition rna_scene.cc:617
const EnumPropertyItem rna_enum_mesh_delimit_mode_items[]
Definition rna_mesh.cc:28
void MESH_OT_edges_select_sharp(wmOperatorType *ot)
bool RNA_struct_property_is_set(PointerRNA *ptr, const char *identifier)
int BMW_current_depth(BMWalker *walker)
Walker Current Depth.
static void findnearestvert__doClosest(void *user_data, BMVert *eve, const float screen_co[2], int index)
BLI_INLINE bool BM_edge_is_boundary(const BMEdge *e) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void interp_v3_v3v3(float r[3], const float a[3], const float b[3], float t)
void MESH_OT_select_boundary_loop_multi(wmOperatorType *ot)
BMDataLayerLookup BM_data_layer_lookup(const BMesh &bm, const StringRef name)
RegionView3D * CTX_wm_region_view3d(const bContext *C)
ToolSettings * CTX_data_tool_settings(const bContext *C)
MINLINE unsigned short highest_order_bit_s(unsigned short n)
static bool edbm_select_by_attribute_poll(bContext *C)
int BKE_mesh_wrapper_vert_len(const Mesh *mesh)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
void ED_view3d_init_mats_rv3d(const Object *ob, RegionView3D *rv3d)
static wmOperatorStatus edbm_select_mirror_exec(bContext *C, wmOperator *op)
void MESH_OT_edgering_select(wmOperatorType *ot)
void ED_mesh_report_mirror_ex(ReportList &reports, int totmirr, int totfail, char selectmode)
static void walker_deselect_nth_edge_chain(BMesh *bm, const CheckerIntervalParams *op_params, BMEdge *e_start)
static int loop_find_region(BMLoop *l, int flag, Set< BMFace * > &visit_face_set, BMFace ***region_out)
static wmOperatorStatus edbm_select_by_pole_count_exec(bContext *C, wmOperator *op)
static void edbm_select_loop_or_ring_by_edge(BMEditMesh *em, Object *obedit, BMEdge *eed, bool select, bool select_clear, bool select_cycle, bool ring, BMWDelimitFlag delimit)
BMEdge * EDBM_edge_find_nearest(ViewContext *vc, float *dist_px_manhattan_p)
void MESH_OT_select_loose(wmOperatorType *ot)
static std::optional< BMIterType > domain_to_iter_type(const bke::AttrDomain domain)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void RNA_boolean_set(PointerRNA *ptr, const char *name, bool value)
void BM_vert_select_set(BMesh *bm, BMVert *v, const bool select)
Select Vert.
BMFace * EDBM_face_find_nearest(ViewContext *vc, float *dist_px_manhattan_p)
void MESH_OT_select_non_manifold(wmOperatorType *ot)
static wmOperatorStatus edbm_boundary_loop_multiselect_exec(bContext *C, wmOperator *op)
static void mouse_mesh_loop_edge(BMesh *bm, BMEdge *eed, bool select, bool select_clear, bool select_cycle, BMWDelimitFlag delimit)
void EDBM_flag_enable_all(BMesh *bm, char hflag)
void mesh_foreachScreenVert(const ViewContext *vc, void(*func)(void *user_data, BMVert *eve, const float screen_co[2], int index), void *user_data, eV3DProjTest clip_flag)
void MESH_OT_select_edge_loop_multi(wmOperatorType *ot)
BMEditMesh * BKE_editmesh_from_object(Object *ob)
Return the BMEditMesh for a given object.
Definition editmesh.cc:59
bool EDBM_selectmode_set_multi_ex(Scene *scene, Span< Object * > objects, const short selectmode)
bool invert_m3(float mat[3][3])
static wmOperatorStatus edbm_select_linked_exec(bContext *C, wmOperator *op)
static wmOperatorStatus edbm_select_linked_flat_faces_exec(bContext *C, wmOperator *op)
void BM_face_uvselect_set_pick(BMesh *bm, BMFace *f, bool select, const BMUVSelectPickParams &params)
static wmOperatorStatus edbm_select_ungrouped_exec(bContext *C, wmOperator *op)
SpaceImage * CTX_wm_space_image(const bContext *C)
void mesh_foreachScreenEdge(const ViewContext *vc, void(*func)(void *user_data, BMEdge *eed, const float screen_co_a[2], const float screen_co_b[2], int index), void *user_data, eV3DProjTest clip_flag)
ViewContext em_setup_viewcontext(bContext *C)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
bool BM_edge_loop_pair(BMEdge *e, BMLoop **r_la, BMLoop **r_lb)
MINLINE float len_manhattan_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
static void mouse_mesh_loop_edge_ring(BMesh *bm, BMEdge *eed, bool select, bool select_clear, BMWDelimitFlag delimit)
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, const int default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
void mid_v3_v3v3(float r[3], const float a[3], const float b[3])
void EDBM_verts_mirror_cache_end(BMEditMesh *em)
bool ED_view3d_clipping_test(const RegionView3D *rv3d, const float co[3], bool is_local)
void EDBM_selectmode_flush_ex(BMesh *bm, short selectmode)
void EDBM_select_swap(BMEditMesh *em, BMesh *bm)
bool BM_edge_is_all_face_flag_test(const BMEdge *e, const char hflag, const bool respect_hide)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
Main * CTX_data_main(const bContext *C)
Vector< Object * > BKE_view_layer_array_from_objects_in_edit_mode(const Main &bmain, const Scene *scene, ViewLayer *view_layer, const View3D *v3d)
BMFace * BM_mesh_active_face_get(BMesh *bm, const bool is_sloppy, const bool is_selected)
void BLI_heap_node_value_update(Heap *heap, HeapNode *node, float value) ATTR_NONNULL(1
Definition BLI_heap.cc:324
void EDBM_select_toggle_all(BMesh *bm)
static BMElem * edbm_select_id_bm_elem_get(const Span< Base * > bases, const uint sel_id, uint &r_base_index)
BMElem * BM_mesh_active_elem_get(BMesh *bm)
PropertyRNA * RNA_def_float_rotation(StructOrFunctionRNA *cont_, const char *identifier, const int len, const float *default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
float dist_squared_to_ray_v3_normalized(const float ray_origin[3], const float ray_direction[3], const float co[3])
Definition math_geom.cc:613
void interp_v2_v2v2(float r[2], const float a[2], const float b[2], float t)
void MESH_OT_select_linked(wmOperatorType *ot)
bool ED_view3d_win_to_ray_clipped(Depsgraph *depsgraph, const ARegion *region, const View3D *v3d, const float mval[2], float r_ray_start[3], float r_ray_normal[3], bool do_clip_planes)
int BM_mesh_calc_face_groups(BMesh *bm, int *r_groups_array, int(**r_group_index)[2], BMLoopFilterFunc filter_fn, BMLoopPairFilterFunc filter_pair_fn, void *user_data, const char hflag_test, const char htype_step)
int ED_view3d_backbuf_sample_size_clamp(ARegion *region, float dist)
BMEdge * EDBM_edge_find_nearest_ex(ViewContext *vc, float *dist_px_manhattan, float *r_dist_center, bool use_select_bias, bool use_cycle, BMEdge **r_eed_zbuf, Span< Base * > bases, uint *r_base_index)
static wmOperatorStatus edbm_select_all_exec(bContext *C, wmOperator *op)
void mid_v2_v2v2(float r[2], const float a[2], const float b[2])
@ ID_RECALC_SELECT
Definition DNA_ID.h:1101
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
static float bm_interior_face_group_calc_cost(ListBaseT< BMFaceLink > *ls, const float *edge_lengths)
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
@ PROP_HIDDEN
Definition RNA_types.hh:408
static void walker_deselect_nth(BMesh *bm, const CheckerIntervalParams *op_params, BMHeader *h_act)
BMFace * EDBM_face_find_nearest_ex(ViewContext *vc, float *dist_px_manhattan, float *r_dist_center, bool use_zbuf_single_px, bool use_select_bias, bool use_cycle, BMFace **r_efa_zbuf, Span< Base * > bases, uint *r_base_index)
static wmOperatorStatus edbm_select_more_exec(bContext *C, wmOperator *op)
@ V3D_PROJ_TEST_CLIP_NEAR
Definition ED_view3d.hh:298
@ V3D_PROJ_TEST_CLIP_BB
Definition ED_view3d.hh:296
static bool edbm_select_ungrouped_poll(bContext *C)
Vector< Object * > BKE_view_layer_array_from_objects_in_edit_mode_unique_data(const Main &bmain, const Scene *scene, ViewLayer *view_layer, const View3D *v3d)
Base * BKE_view_layer_base_find(ViewLayer *view_layer, Object *ob)
static wmOperatorStatus edbm_select_less_exec(bContext *C, wmOperator *op)
Span< float3 > BKE_mesh_wrapper_vert_coords(const Mesh *mesh)
void RNA_property_enum_set(PointerRNA *ptr, PropertyRNA *prop, int value)
void * BMW_step(BMWalker *walker)
Step Walker.
bool ED_operator_editmesh_region_view3d(bContext *C)
const BMesh * BKE_editmesh_bmesh_get(const Object *ob)
Definition editmesh.cc:65
PropertyRNA * RNA_def_enum_flag(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
Vector< Base * > BKE_view_layer_array_from_bases_in_edit_mode_unique_data(const Main &bmain, const Scene *scene, ViewLayer *view_layer, const View3D *v3d)
static wmOperatorStatus edbm_select_sharp_edges_exec(bContext *C, wmOperator *op)
unsigned int BLI_ghashutil_strhash_p(const void *ptr)
static wmOperatorStatus edbm_select_random_exec(bContext *C, wmOperator *op)
bool EDBM_selectmode_disable_multi_ex(Scene *scene, Span< Base * > bases, short selectmode_disable, short selectmode_fallback)
bool EDBM_uvselect_clear(BMesh *bm)
static wmOperatorStatus edbm_select_loop_exec(bContext *C, wmOperator *op)
void mesh_foreachScreenFace(const ViewContext *vc, void(*func)(void *user_data, BMFace *efa, const float screen_co[2], int index), void *user_data, eV3DProjTest clip_flag)
static wmOperatorStatus edbm_select_non_manifold_exec(bContext *C, wmOperator *op)
Scene * CTX_data_scene(const bContext *C)
bool WM_cursor_test_motion_and_update(const int mval[2])
static wmOperatorStatus edbm_region_to_loop_exec(bContext *C, wmOperator *)
float dist_squared_ray_to_seg_v3(const float ray_origin[3], const float ray_direction[3], const float v0[3], const float v1[3], float r_point[3], float *r_depth)
Definition math_geom.cc:629
static BMVert * bm_step_to_next_selected_vert_in_chain(BMVert *v_curr, BMVert *v_prev)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
@ OPTYPE_DEPENDS_ON_CURSOR
Definition WM_types.hh:228
static int verg_radial(const void *va, const void *vb)
bool EDBM_mesh_deselect_all_multi(bContext *C)
void BM_mesh_select_flush_from_verts(BMesh *bm, const bool select)
void MESH_OT_select_nth(wmOperatorType *ot)
static bool select_linked_delimit_test(BMEdge *e, int delimit, const DelimitData *delimit_data)
BLI_INLINE BMFace * BM_face_at_index(BMesh *bm, const int index)
void MESH_OT_faces_select_linked_flat(wmOperatorType *ot)
static BMFace * bm_step_over_shared_edge_to_next_selected_face_in_chain(BMFace *f_curr, BMFace *f_prev)
bool BM_vert_is_manifold(const BMVert *v)
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
static bool walker_select(BMesh *bm, int walkercode, void *start, const bool select, BMWFlag flags, BMWDelimitFlag delimit, FunctionRef< void(BMElem *)> foreach_elem_fn)
static wmOperatorStatus edbm_edge_loop_multiselect_exec(bContext *C, wmOperator *op)
float ED_view3d_select_dist_px()
MINLINE float len_squared_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
Vector< Base * > BKE_view_layer_array_from_bases_in_edit_mode(const Main &bmain, const Scene *scene, ViewLayer *view_layer, const View3D *v3d)
void EDBM_select_flush_from_verts(BMesh *bm, short selectmode, bool select)
void BM_mesh_uvselect_set_elem_from_mesh(BMesh *bm, const bool select, const BMUVSelectPickParams &params, const VectorList< BMVert * > &verts, const VectorList< BMEdge * > &edges, const VectorList< BMFace * > &faces)
static wmOperatorStatus edbm_edge_ring_multiselect_exec(bContext *C, wmOperator *op)
void MESH_OT_select_all(wmOperatorType *ot)
void BMO_pop(BMesh *bm)
BMESH OPSTACK POP.
static bool is_count_a_match(const eElemCountType type, const int value_test, const int value_reference)
static bool bm_edges_form_cyclic_chain(BMEdge *e_start)
PropertyRNA * RNA_def_float(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
bool EDBM_unified_findnearest_from_raycast(ViewContext *vc, Span< Base * > bases, bool use_boundary_vertices, bool use_boundary_edges, int *r_base_index_vert, int *r_base_index_edge, int *r_base_index_face, BMVert **r_eve, BMEdge **r_eed, BMFace **r_efa)
static wmOperatorStatus edbm_select_similar_region_exec(bContext *C, wmOperator *op)
void BM_mesh_elem_toolflags_clear(BMesh *bm)
void MESH_OT_select_mode(wmOperatorType *ot)
void MESH_OT_region_to_loop(wmOperatorType *ot)
void WM_operator_properties_checker_interval_from_op(wmOperator *op, CheckerIntervalParams *op_params)
void MESH_OT_select_random(wmOperatorType *ot)
static void find_nearest_edge_center__doZBuf(void *user_data, BMEdge *eed, const float screen_co_a[2], const float screen_co_b[2], int)
float RNA_float_get(PointerRNA *ptr, const char *name)
bool BM_edge_is_any_vert_flag_test(const BMEdge *e, const char hflag)
static wmOperatorStatus edbm_select_mode_invoke(bContext *C, wmOperator *op, const wmEvent *event)
bool RNA_property_is_set(PointerRNA *ptr, PropertyRNA *prop)
void EDBM_selectmode_set(BMEditMesh *em, BMesh *bm, short selectmode)
BMVert * BM_mesh_active_vert_get(BMesh *bm)
View3D * CTX_wm_view3d(const bContext *C)
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
@ BMW_DELIMIT_EDGE_LOOP_OUTER_CORNERS
@ BMW_DELIMIT_EDGE_MARK_SHARP
@ BMW_DELIMIT_EDGE_LOOP_NGONS
@ BMW_DELIMIT_EDGE_MARK_SEAM
@ BMW_DELIMIT_EDGE_RING_NGONS
BMEdge * BM_edge_at_index_find_or_table(BMesh *bm, const int index)
bool BM_edge_is_contiguous_loop_cd(const BMEdge *e, const int cd_loop_type, const int cd_loop_offset)
void MESH_OT_select_similar_region(wmOperatorType *ot)
const EnumPropertyItem rna_enum_axis_xyz_items[]
bool BM_mesh_select_is_mixed(const BMesh *bm)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void * BLI_heap_node_ptr(const HeapNode *heap) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition BLI_heap.cc:354
void EDBM_select_less(BMesh *bm, short selectmode, bool use_face_step)
void void float BLI_heap_node_value(const HeapNode *heap) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition BLI_heap.cc:349
static bool edbm_select_loop_or_ring_pick(bContext *C, const int mval[2], Object **r_obedit, BMEdge **r_eed)
void BM_edge_select_set(BMesh *bm, BMEdge *e, const bool select)
Select Edge.
#define fabsf
#define cosf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
struct BMEditSelection * next
struct BMLoop * next
struct BMLoop * prev
struct BMEdge * e
struct BMLoop * radial_next
struct BMFace * f
struct BMEdge * e
struct Object * object
eCustomDataType cd_loop_type
char name[258]
Definition DNA_ID.h:433
struct MDeformWeight * dw
NearestEdgeUserData_Hit hit
NearestEdgeUserData_Hit hit_cycle
NearestFaceUserData_Hit hit
NearestFaceUserData_Hit hit_cycle
NearestVertUserData_Hit hit_cycle
NearestVertUserData_Hit hit
struct ToolSettings * toolsettings
ViewLayer * view_layer
Definition ED_view3d.hh:80
RegionView3D * rv3d
Definition ED_view3d.hh:86
BMEditMesh * em
Definition ED_view3d.hh:87
Depsgraph * depsgraph
Definition ED_view3d.hh:78
ListBaseT< BMEditSelection > selected
wmEventModifierFlag modifier
Definition WM_types.hh:834
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
bool(*) poll_property(const bContext *C, wmOperator *op, const PropertyRNA *prop) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1171
const char * description
Definition WM_types.hh:1115
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
std::string(*) get_description(bContext *C, wmOperatorType *ot, PointerRNA *ptr)
Definition WM_types.hh:1197
struct ReportList * reports
struct wmOperatorType * type