Blender V5.3
editmesh_tools.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 <cstddef>
10#include <fmt/format.h>
11
12#include "MEM_guardedalloc.h"
13
14#include "DNA_key_types.h"
15#include "DNA_material_types.h"
16#include "DNA_mesh_types.h"
17#include "DNA_meshdata_types.h"
18#include "DNA_modifier_types.h"
19#include "DNA_object_types.h"
20#include "DNA_scene_types.h"
21
22#include "BLI_array.hh"
23#include "BLI_bitmap.hh"
24#include "BLI_heap_simple.hh"
25#include "BLI_linklist.hh"
26#include "BLI_linklist_stack.hh"
27#include "BLI_listbase.hh"
28#include "BLI_math_bits.hh"
29#include "BLI_math_geom_c.hh"
30#include "BLI_math_matrix_c.hh"
32#include "BLI_math_vector_c.hh"
33#include "BLI_rand_c.hh"
34#include "BLI_sort_utils.hh"
35
36#include "BKE_attribute.h"
37#include "BKE_context.hh"
38#include "BKE_customdata.hh"
39#include "BKE_deform.hh"
40#include "BKE_editmesh.hh"
41#include "BKE_key.hh"
42#include "BKE_layer.hh"
43#include "BKE_lib_id.hh"
44#include "BKE_material.hh"
45#include "BKE_mesh.hh"
46#include "BKE_mesh_types.hh"
47#include "BKE_object.hh"
48#include "BKE_object_types.hh"
49#include "BKE_paint.hh"
50#include "BKE_report.hh"
51
52#include "DEG_depsgraph.hh"
54
55#include "BLT_translation.hh"
56
57#include "RNA_access.hh"
58#include "RNA_define.hh"
59#include "RNA_enum_types.hh"
60#include "RNA_prototypes.hh"
61
62#include "WM_api.hh"
63#include "WM_types.hh"
64
65#include "ED_mesh.hh"
66#include "ED_object.hh"
67#include "ED_outliner.hh"
68#include "ED_screen.hh"
69#include "ED_select_utils.hh"
70#include "ED_transform.hh"
71#include "ED_uvedit.hh"
72#include "ED_view3d.hh"
73
74#include "UI_interface.hh"
76#include "UI_resources.hh"
77
78#include "mesh_intern.hh" /* own include */
79
80#include "bmesh_tools.hh"
81
82namespace blender {
83
84#define USE_FACE_CREATE_SEL_EXTEND
85
86/* -------------------------------------------------------------------- */
89
91{
92 const int cuts = RNA_int_get(op->ptr, "number_cuts");
93 const float smooth = RNA_float_get(op->ptr, "smoothness");
94 const float fractal = RNA_float_get(op->ptr, "fractal") / 2.5f;
95 const float along_normal = RNA_float_get(op->ptr, "fractal_along_normal");
96 const bool use_quad_tri = !RNA_boolean_get(op->ptr, "ngon");
97
98 if (use_quad_tri && RNA_enum_get(op->ptr, "quadcorner") == SUBD_CORNER_STRAIGHT_CUT) {
99 RNA_enum_set(op->ptr, "quadcorner", SUBD_CORNER_INNERVERT);
100 }
101 const int quad_corner_type = RNA_enum_get(op->ptr, "quadcorner");
102 const int seed = RNA_int_get(op->ptr, "seed");
103
104 const Main *bmain = CTX_data_main(C);
105 const Scene *scene = CTX_data_scene(C);
106 ViewLayer *view_layer = CTX_data_view_layer(C);
108 *bmain, scene, view_layer, CTX_wm_view3d(C));
109
110 for (Object *obedit : objects) {
112 if (!(bm->totedgesel || bm->totfacesel)) {
113 continue;
114 }
115
118 smooth,
120 false,
121 fractal,
122 along_normal,
123 cuts,
125 quad_corner_type,
126 use_quad_tri,
127 true,
128 false,
129 seed);
130
131 EDBMUpdate_Params params{};
132 params.calc_looptris = true;
133 params.calc_normals = false;
134 params.is_destructive = true;
135 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
136 }
137
138 return OPERATOR_FINISHED;
139}
140
141/* NOTE: these values must match delete_mesh() event values. */
143 {SUBD_CORNER_INNERVERT, "INNERVERT", 0, "Inner Vert", ""},
144 {SUBD_CORNER_PATH, "PATH", 0, "Path", ""},
145 {SUBD_CORNER_STRAIGHT_CUT, "STRAIGHT_CUT", 0, "Straight Cut", ""},
146 {SUBD_CORNER_FAN, "FAN", 0, "Fan", ""},
147 {0, nullptr, 0, nullptr, nullptr},
148};
149
151{
152 PropertyRNA *prop;
153
154 /* identifiers */
155 ot->name = "Subdivide";
156 ot->description = "Subdivide selected edges";
157 ot->idname = "MESH_OT_subdivide";
158
159 /* API callbacks. */
162
163 /* flags */
165
166 /* properties */
167 prop = RNA_def_int(ot->srna, "number_cuts", 1, 1, 100, "Number of Cuts", "", 1, 10);
168 /* avoid re-using last var because it can cause
169 * _very_ high poly meshes and annoy users (or worse crash) */
171
173 ot->srna, "smoothness", 0.0f, 0.0f, 1e3f, "Smoothness", "Smoothness factor", 0.0f, 1.0f);
174
176
178 "ngon",
179 true,
180 "Create N-Gons",
181 "When disabled, newly created faces are limited to 3 and 4 sided faces");
183 ot->srna,
184 "quadcorner",
187 "Quad Corner Type",
188 "How to subdivide quad corners (anything other than Straight Cut will prevent n-gons)");
189
191 "fractal",
192 0.0f,
193 0.0f,
194 1e6f,
195 "Fractal",
196 "Fractal randomness factor",
197 0.0f,
198 1000.0f);
200 "fractal_along_normal",
201 0.0f,
202 0.0f,
203 1.0f,
204 "Along Normal",
205 "Apply fractal displacement along normal only",
206 0.0f,
207 1.0f);
208 RNA_def_int(ot->srna,
209 "seed",
210 0,
211 0,
212 INT_MAX,
213 "Random Seed",
214 "Seed for the random number generator",
215 0,
216 255);
217}
218
220
221/* -------------------------------------------------------------------- */
227
236
238 const int cuts_min,
239 const int cuts_default)
240{
241 /* NOTE: these values must match delete_mesh() event values. */
242 static const EnumPropertyItem prop_subd_edgering_types[] = {
243 {SUBD_RING_INTERP_LINEAR, "LINEAR", 0, "Linear", ""},
244 {SUBD_RING_INTERP_PATH, "PATH", 0, "Blend Path", ""},
245 {SUBD_RING_INTERP_SURF, "SURFACE", 0, "Blend Surface", ""},
246 {0, nullptr, 0, nullptr, nullptr},
247 };
248
249 PropertyRNA *prop;
250
251 prop = RNA_def_int(
252 ot->srna, "number_cuts", cuts_default, 0, 1000, "Number of Cuts", "", cuts_min, 64);
254
255 RNA_def_enum(ot->srna,
256 "interpolation",
257 prop_subd_edgering_types,
259 "Interpolation",
260 "Interpolation method");
261
263 ot->srna, "smoothness", 1.0f, 0.0f, 1e3f, "Smoothness", "Smoothness factor", 0.0f, 2.0f);
264
265 /* profile-shape */
267 "profile_shape_factor",
268 0.0f,
269 -1e3f,
270 1e3f,
271 "Profile Factor",
272 "How much intermediary new edges are shrunk/expanded",
273 -2.0f,
274 2.0f);
275
276 prop = RNA_def_property(ot->srna, "profile_shape", PROP_ENUM, PROP_NONE);
279 RNA_def_property_ui_text(prop, "Profile Shape", "Shape of the profile");
281 BLT_I18NCONTEXT_ID_CURVE_LEGACY); /* Abusing id_curve :/ */
282}
283
285{
286 op_props->interp_mode = RNA_enum_get(op->ptr, "interpolation");
287 op_props->cuts = RNA_int_get(op->ptr, "number_cuts");
288 op_props->smooth = RNA_float_get(op->ptr, "smoothness");
289
290 op_props->profile_shape = RNA_enum_get(op->ptr, "profile_shape");
291 op_props->profile_shape_factor = RNA_float_get(op->ptr, "profile_shape_factor");
292}
293
295{
296 const Main *bmain = CTX_data_main(C);
297 const Scene *scene = CTX_data_scene(C);
298 ViewLayer *view_layer = CTX_data_view_layer(C);
300 *bmain, scene, view_layer, CTX_wm_view3d(C));
301 EdgeRingOpSubdProps op_props;
302
304
305 for (Object *obedit : objects) {
307
308 if (bm->totedgesel == 0) {
309 continue;
310 }
311
312 if (!EDBM_op_callf(bm,
313 op,
314 "subdivide_edgering edges=%he interp_mode=%i cuts=%i smooth=%f "
315 "profile_shape=%i profile_shape_factor=%f",
317 op_props.interp_mode,
318 op_props.cuts,
319 op_props.smooth,
320 op_props.profile_shape,
321 op_props.profile_shape_factor))
322 {
323 continue;
324 }
325
326 EDBMUpdate_Params params{};
327 params.calc_looptris = true;
328 params.calc_normals = false;
329 params.is_destructive = true;
330 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
331 }
332
333 return OPERATOR_FINISHED;
334}
335
337{
338 /* identifiers */
339 ot->name = "Subdivide Edge-Ring";
340 ot->description = "Subdivide perpendicular edges to the selected edge-ring";
341 ot->idname = "MESH_OT_subdivide_edgering";
342
343 /* API callbacks. */
346
347 /* flags */
349
350 /* properties */
352}
353
355
356/* -------------------------------------------------------------------- */
359
361{
362 const int iterations = RNA_int_get(op->ptr, "iterations");
363 const Main *bmain = CTX_data_main(C);
364 const Scene *scene = CTX_data_scene(C);
365 ViewLayer *view_layer = CTX_data_view_layer(C);
367 *bmain, scene, view_layer, CTX_wm_view3d(C));
368 for (Object *obedit : objects) {
371
372 if ((bm->totvertsel == 0) && (bm->totedgesel == 0) && (bm->totfacesel == 0)) {
373 continue;
374 }
375
376 BMOperator bmop;
377 EDBM_op_init(bm, &bmop, op, "unsubdivide verts=%hv iterations=%i", BM_ELEM_SELECT, iterations);
378
379 BMO_op_exec(bm, &bmop);
380
381 if (!EDBM_op_finish(bm, &bmop, op, true)) {
382 continue;
383 }
384
385 if ((em->selectmode & SCE_SELECT_VERTEX) == 0) {
386 EDBM_selectmode_flush_ex(bm, SCE_SELECT_VERTEX); /* need to flush vert->face first */
387 }
389
390 EDBMUpdate_Params params{};
391 params.calc_looptris = true;
392 params.calc_normals = false;
393 params.is_destructive = true;
394 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
395 }
396
397 return OPERATOR_FINISHED;
398}
399
401{
402 /* identifiers */
403 ot->name = "Un-Subdivide";
404 ot->description = "Un-subdivide selected edges and faces";
405 ot->idname = "MESH_OT_unsubdivide";
406
407 /* API callbacks. */
410
411 /* flags */
413
414 /* props */
416 ot->srna, "iterations", 2, 1, 1000, "Iterations", "Number of times to un-subdivide", 1, 100);
417}
418
420
421/* -------------------------------------------------------------------- */
424
425/* NOTE: these values must match delete_mesh() event values. */
426enum {
432};
433
435 const int totelem_old[3],
436 const int totelem_new[3])
437{
438 BKE_reportf(reports,
439 RPT_INFO,
440 "Removed: %d vertices, %d edges, %d faces",
441 totelem_old[0] - totelem_new[0],
442 totelem_old[1] - totelem_new[1],
443 totelem_old[2] - totelem_new[2]);
444}
445
447{
448 const Main *bmain = CTX_data_main(C);
449 const Scene *scene = CTX_data_scene(C);
450 ViewLayer *view_layer = CTX_data_view_layer(C);
451
453 *bmain, scene, view_layer, CTX_wm_view3d(C));
454 bool changed_multi = false;
455
456 for (Object *obedit : objects) {
458 const int type = RNA_enum_get(op->ptr, "type");
459
460 switch (type) {
461 case MESH_DELETE_VERT: /* Erase Vertices */
462 if (bm->totvertsel == 0) {
463 continue;
464 }
466 if (!EDBM_op_callf(bm, op, "delete geom=%hv context=%i", BM_ELEM_SELECT, DEL_VERTS)) {
467 continue;
468 }
469 break;
470 case MESH_DELETE_EDGE: /* Erase Edges */
471 if (bm->totedgesel == 0) {
472 continue;
473 }
475 if (!EDBM_op_callf(bm, op, "delete geom=%he context=%i", BM_ELEM_SELECT, DEL_EDGES)) {
476 continue;
477 }
478 break;
479 case MESH_DELETE_FACE: /* Erase Faces */
480 if (bm->totfacesel == 0) {
481 continue;
482 }
484 if (!EDBM_op_callf(bm, op, "delete geom=%hf context=%i", BM_ELEM_SELECT, DEL_FACES)) {
485 continue;
486 }
487 break;
488 case MESH_DELETE_EDGE_FACE: /* Edges and Faces */
489 if ((bm->totedgesel == 0) && (bm->totfacesel == 0)) {
490 continue;
491 }
493 if (!EDBM_op_callf(bm, op, "delete geom=%hef context=%i", BM_ELEM_SELECT, DEL_EDGESFACES))
494 {
495 continue;
496 }
497 break;
498 case MESH_DELETE_ONLY_FACE: /* Only faces. */
499 if (bm->totfacesel == 0) {
500 continue;
501 }
503 if (!EDBM_op_callf(bm, op, "delete geom=%hf context=%i", BM_ELEM_SELECT, DEL_ONLYFACES)) {
504 continue;
505 }
506 break;
507 default:
508 BLI_assert(0);
509 break;
510 }
511
512 changed_multi = true;
513
515
517
518 EDBMUpdate_Params params{};
519 params.calc_looptris = true;
520 params.calc_normals = false;
521 params.is_destructive = true;
522 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
523
524 DEG_id_tag_update(obedit->data, ID_RECALC_SELECT);
525 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
526 }
527
528 return changed_multi ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
529}
530
532{
533 static const EnumPropertyItem prop_mesh_delete_types[] = {
534 {MESH_DELETE_VERT, "VERT", 0, "Vertices", ""},
535 {MESH_DELETE_EDGE, "EDGE", 0, "Edges", ""},
536 {MESH_DELETE_FACE, "FACE", 0, "Faces", ""},
537 {MESH_DELETE_EDGE_FACE, "EDGE_FACE", 0, "Only Edges & Faces", ""},
538 {MESH_DELETE_ONLY_FACE, "ONLY_FACE", 0, "Only Faces", ""},
539 {0, nullptr, 0, nullptr, nullptr},
540 };
541
542 /* identifiers */
543 ot->name = "Delete";
544 ot->description = "Delete selected vertices, edges or faces";
545 ot->idname = "MESH_OT_delete";
546
547 /* API callbacks. */
550
552
553 /* flags */
555
556 /* props */
557 ot->prop = RNA_def_enum(ot->srna,
558 "type",
559 prop_mesh_delete_types,
561 "Type",
562 "Method used for deleting mesh data");
564}
565
567
568/* -------------------------------------------------------------------- */
571
573{
574 BMLoop *l_iter, *l_first;
575
576 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
577 do {
578 if (!BM_edge_is_boundary(l_iter->e)) {
579 return false;
580 }
581 } while ((l_iter = l_iter->next) != l_first);
582
583 return true;
584}
585
587{
588 const Main *bmain = CTX_data_main(C);
589 const Scene *scene = CTX_data_scene(C);
590 ViewLayer *view_layer = CTX_data_view_layer(C);
591 int totelem_old_sel[3];
592 int totelem_old[3];
593
595 *bmain, scene, view_layer, CTX_wm_view3d(C));
596
597 EDBM_mesh_stats_multi(objects, totelem_old, totelem_old_sel);
598
599 const bool use_verts = (RNA_boolean_get(op->ptr, "use_verts") && totelem_old_sel[0]);
600 const bool use_edges = (RNA_boolean_get(op->ptr, "use_edges") && totelem_old_sel[1]);
601 const bool use_faces = (RNA_boolean_get(op->ptr, "use_faces") && totelem_old_sel[2]);
602
603 for (Object *obedit : objects) {
605 BMIter iter;
606
608
609 if (use_faces) {
610 BMFace *f;
611
612 BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
615 }
616 }
617
619 }
620
621 if (use_edges) {
622 BMEdge *e;
623
624 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
627 }
628 }
629
631 }
632
633 if (use_verts) {
634 BMVert *v;
635
636 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
638 BM_elem_flag_set(v, BM_ELEM_TAG, (v->e == nullptr));
639 }
640 }
641
643 }
644
646
647 EDBMUpdate_Params params{};
648 params.calc_looptris = true;
649 params.calc_normals = false;
650 params.is_destructive = true;
651 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
652 }
653
654 int totelem_new[3];
655 EDBM_mesh_stats_multi(objects, totelem_new, nullptr);
656
657 edbm_report_delete_info(op->reports, totelem_old, totelem_new);
658
659 return OPERATOR_FINISHED;
660}
661
663{
664 /* identifiers */
665 ot->name = "Delete Loose";
666 ot->description = "Delete loose vertices, edges or faces";
667 ot->idname = "MESH_OT_delete_loose";
668
669 /* API callbacks. */
671
673
674 /* flags */
676
677 /* props */
678 RNA_def_boolean(ot->srna, "use_verts", true, "Vertices", "Remove loose vertices");
679 RNA_def_boolean(ot->srna, "use_edges", true, "Edges", "Remove loose edges");
680 RNA_def_boolean(ot->srna, "use_faces", false, "Faces", "Remove loose faces");
681}
682
684
685/* -------------------------------------------------------------------- */
688
690{
691 const Main *bmain = CTX_data_main(C);
692 const Scene *scene = CTX_data_scene(C);
693 ViewLayer *view_layer = CTX_data_view_layer(C);
695 *bmain, scene, view_layer, CTX_wm_view3d(C));
696 for (Object *obedit : objects) {
698
699 if (bm->totedgesel == 0) {
700 continue;
701 }
702
703 if (!EDBM_op_callf(bm, op, "collapse edges=%he uvs=%b", BM_ELEM_SELECT, true)) {
704 continue;
705 }
706
707 EDBMUpdate_Params params{};
708 params.calc_looptris = true;
709 params.calc_normals = false;
710 params.is_destructive = true;
711 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
712 }
713
714 return OPERATOR_FINISHED;
715}
716
718{
719 /* identifiers */
720 ot->name = "Collapse Edges & Faces";
721 ot->description =
722 "Collapse isolated edge and face regions, merging data such as UVs and color attributes. "
723 "This can collapse edge-rings as well as regions of connected faces into vertices";
724 ot->idname = "MESH_OT_edge_collapse";
725
726 /* API callbacks. */
729
730 /* flags */
732}
733
735
736/* -------------------------------------------------------------------- */
739
741{
742 BMEdge *e;
743 BMIter iter;
744
745 uint vote_on_smooth[2] = {0, 0};
746
747 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
748 if (BM_elem_flag_test(e, BM_ELEM_SELECT) && e->l) {
749 vote_on_smooth[BM_elem_flag_test_bool(e->l->f, BM_ELEM_SMOOTH)]++;
750 }
751 }
752
753 return (vote_on_smooth[0] < vote_on_smooth[1]);
754}
755
756#ifdef USE_FACE_CREATE_SEL_EXTEND
762 BMVert *v, BMEdge *e_used, BMEdge **e_arr, const int e_arr_len, bool (*func)(const BMEdge *))
763{
764 BMIter iter;
765 BMEdge *e_iter;
766 int i = 0;
767 BM_ITER_ELEM (e_iter, &iter, v, BM_EDGES_OF_VERT) {
768 if (BM_elem_flag_test(e_iter, BM_ELEM_HIDDEN) == false) {
769 if ((e_used == nullptr) || (e_used != e_iter)) {
770 if (func(e_iter)) {
771 e_arr[i++] = e_iter;
772 if (i >= e_arr_len) {
773 break;
774 }
775 }
776 }
777 }
778 }
779 return i;
780}
781
783{
784 BMIter iter;
785 bool found = false;
786
787 if (bm->totvertsel == 1 && bm->totedgesel == 0 && bm->totfacesel == 0) {
788 /* first look for 2 boundary edges */
789 BMVert *v;
790
791 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
793 found = true;
794 break;
795 }
796 }
797
798 if (found) {
799 BMEdge *ed_pair[3];
800 if (((edbm_add_edge_face_exec__vert_edge_lookup(v, nullptr, ed_pair, 3, BM_edge_is_wire) ==
801 2) &&
802 (BM_edge_share_face_check(ed_pair[0], ed_pair[1]) == false)) ||
803
805 v, nullptr, ed_pair, 3, BM_edge_is_boundary) == 2) &&
806 (BM_edge_share_face_check(ed_pair[0], ed_pair[1]) == false)))
807 {
808 BMEdge *e_other = BM_edge_exists(BM_edge_other_vert(ed_pair[0], v),
809 BM_edge_other_vert(ed_pair[1], v));
810 BM_edge_select_set(bm, ed_pair[0], true);
811 BM_edge_select_set(bm, ed_pair[1], true);
812 if (e_other) {
813 BM_edge_select_set(bm, e_other, true);
814 }
815 return reinterpret_cast<BMElem *>(v);
816 }
817 }
818 }
819 else if (bm->totvertsel == 2 && bm->totedgesel == 1 && bm->totfacesel == 0) {
820 /* first look for 2 boundary edges */
821 BMEdge *e;
822
823 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
825 found = true;
826 break;
827 }
828 }
829 if (found) {
830 BMEdge *ed_pair_v1[2];
831 BMEdge *ed_pair_v2[2];
832 if (((edbm_add_edge_face_exec__vert_edge_lookup(e->v1, e, ed_pair_v1, 2, BM_edge_is_wire) ==
833 1) &&
835 1) &&
836 (BM_edge_share_face_check(e, ed_pair_v1[0]) == false) &&
837 (BM_edge_share_face_check(e, ed_pair_v2[0]) == false)) ||
838
839# if 1 /* better support mixed cases #37203. */
841 1) &&
843 e->v2, e, ed_pair_v2, 2, BM_edge_is_boundary) == 1) &&
844 (BM_edge_share_face_check(e, ed_pair_v1[0]) == false) &&
845 (BM_edge_share_face_check(e, ed_pair_v2[0]) == false)) ||
846
848 e->v1, e, ed_pair_v1, 2, BM_edge_is_boundary) == 1) &&
850 1) &&
851 (BM_edge_share_face_check(e, ed_pair_v1[0]) == false) &&
852 (BM_edge_share_face_check(e, ed_pair_v2[0]) == false)) ||
853# endif
854
856 e->v1, e, ed_pair_v1, 2, BM_edge_is_boundary) == 1) &&
858 e->v2, e, ed_pair_v2, 2, BM_edge_is_boundary) == 1) &&
859 (BM_edge_share_face_check(e, ed_pair_v1[0]) == false) &&
860 (BM_edge_share_face_check(e, ed_pair_v2[0]) == false)))
861 {
862 BMVert *v1_other = BM_edge_other_vert(ed_pair_v1[0], e->v1);
863 BMVert *v2_other = BM_edge_other_vert(ed_pair_v2[0], e->v2);
864 BMEdge *e_other = (v1_other != v2_other) ? BM_edge_exists(v1_other, v2_other) : nullptr;
865 BM_edge_select_set(bm, ed_pair_v1[0], true);
866 BM_edge_select_set(bm, ed_pair_v2[0], true);
867 if (e_other) {
868 BM_edge_select_set(bm, e_other, true);
869 }
870 return reinterpret_cast<BMElem *>(e);
871 }
872 }
873 }
874
875 return nullptr;
876}
878{
879 /* Now we need to find the edge that isn't connected to this element. */
881
882 /* Notes on hidden geometry:
883 * - Un-hide the face since its possible hidden was copied when copying
884 * surrounding face attributes.
885 * - Un-hide before adding to select history
886 * since we may extend into an existing, hidden vert/edge.
887 */
888
890 BM_face_select_set(bm, f, false);
891
892 if (ele_desel->head.htype == BM_VERT) {
893 BMLoop *l = BM_face_vert_share_loop(f, reinterpret_cast<BMVert *>(ele_desel));
894 BLI_assert(f->len == 3);
895 BM_vert_select_set(bm, reinterpret_cast<BMVert *>(ele_desel), false);
896 BM_edge_select_set(bm, l->next->e, true);
898 }
899 else {
900 BMLoop *l = BM_face_edge_share_loop(f, reinterpret_cast<BMEdge *>(ele_desel));
901 BLI_assert(ELEM(f->len, 4, 3));
902
903 BM_edge_select_set(bm, reinterpret_cast<BMEdge *>(ele_desel), false);
904 if (f->len == 4) {
905 BMEdge *e_active = l->next->next->e;
907 BM_edge_select_set(bm, e_active, true);
908 BM_select_history_store(bm, e_active);
909 }
910 else {
911 BMVert *v_active = l->next->next->v;
913 BM_vert_select_set(bm, v_active, true);
914 BM_select_history_store(bm, v_active);
915 }
916 }
917}
918#endif /* USE_FACE_CREATE_SEL_EXTEND */
919
921{
922 /* When this is used to dissolve we could avoid this, but checking isn't too slow. */
923 bool changed_multi = false;
924 const Main *bmain = CTX_data_main(C);
925 const Scene *scene = CTX_data_scene(C);
926 ViewLayer *view_layer = CTX_data_view_layer(C);
928 *bmain, scene, view_layer, CTX_wm_view3d(C));
929 for (Object *obedit : objects) {
932
933 if ((bm->totvertsel == 0) && (bm->totedgesel == 0) && (bm->totfacesel == 0)) {
934 continue;
935 }
936
937 bool use_smooth = edbm_add_edge_face__smooth_get(bm);
938 int totedge_orig = bm->totedge;
939 int totface_orig = bm->totface;
940
941 BMOperator bmop;
942#ifdef USE_FACE_CREATE_SEL_EXTEND
943 BMElem *ele_desel;
944 BMFace *ele_desel_face;
945
946 /* be extra clever, figure out if a partial selection should be extended so we can create
947 * geometry with single vert or single edge selection. */
949#endif
950 if (!EDBM_op_init(bm,
951 &bmop,
952 op,
953 "contextual_create geom=%hfev mat_nr=%i use_smooth=%b",
955 em->mat_nr,
956 use_smooth))
957 {
958 continue;
959 }
960
961 BMO_op_exec(bm, &bmop);
962
963 /* cancel if nothing was done */
964 if ((totedge_orig == bm->totedge) && (totface_orig == bm->totface)) {
965 EDBM_op_finish(bm, &bmop, op, true);
966 continue;
967 }
968#ifdef USE_FACE_CREATE_SEL_EXTEND
969 /* normally we would want to leave the new geometry selected,
970 * but being able to press F many times to add geometry is too useful! */
971 if (ele_desel && (BMO_slot_buffer_len(bmop.slots_out, "faces.out") == 1) &&
972 (ele_desel_face = static_cast<BMFace *>(
973 BMO_slot_buffer_get_first(bmop.slots_out, "faces.out"))))
974 {
975 edbm_add_edge_face_exec__tricky_finalize_sel(bm, ele_desel, ele_desel_face);
976 }
977 else
978#endif
979 {
980 /* Newly created faces may include existing hidden edges,
981 * copying face data from surrounding, may have copied hidden face flag too.
982 *
983 * Important that faces use flushing since 'edges.out'
984 * won't include hidden edges that already existed.
985 */
987 bm, bmop.slots_out, "faces.out", BM_FACE, BM_ELEM_HIDDEN, true);
989 bm, bmop.slots_out, "edges.out", BM_EDGE, BM_ELEM_HIDDEN, false);
990
993 }
994
995 if (!EDBM_op_finish(bm, &bmop, op, true)) {
996 continue;
997 }
998
999 EDBMUpdate_Params params{};
1000 params.calc_looptris = true;
1001 params.calc_normals = false;
1002 params.is_destructive = true;
1003 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
1004 changed_multi = true;
1005 }
1006
1007 if (!changed_multi) {
1008 return OPERATOR_CANCELLED;
1009 }
1010
1011 return OPERATOR_FINISHED;
1012}
1013
1015{
1016 /* identifiers */
1017 ot->name = "Make Edge/Face";
1018 ot->description = "Add an edge or face to selected";
1019 ot->idname = "MESH_OT_edge_face_add";
1020
1021 /* API callbacks. */
1024
1025 /* flags */
1027}
1028
1030
1031/* -------------------------------------------------------------------- */
1034
1036{
1037 const Main *bmain = CTX_data_main(C);
1038 Scene *scene = CTX_data_scene(C);
1039 ViewLayer *view_layer = CTX_data_view_layer(C);
1040 BMEdge *eed;
1041 BMIter iter;
1042 const bool clear = RNA_boolean_get(op->ptr, "clear");
1043
1045 *bmain, scene, view_layer, CTX_wm_view3d(C));
1046 for (Object *obedit : objects) {
1048
1049 if (bm->totedgesel == 0) {
1050 continue;
1051 }
1052
1053 if (clear) {
1054 BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
1056 continue;
1057 }
1058
1060 }
1061 }
1062 else {
1063 BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
1065 continue;
1066 }
1068 }
1069 }
1070 }
1071
1072 ED_uvedit_live_unwrap(scene, objects);
1073
1074 for (Object *obedit : objects) {
1075 EDBMUpdate_Params params{};
1076 params.calc_looptris = true;
1077 params.calc_normals = false;
1078 params.is_destructive = false;
1079 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
1080 }
1081
1082 return OPERATOR_FINISHED;
1083}
1084
1086{
1087 PropertyRNA *prop;
1088
1089 /* identifiers */
1090 ot->name = "Mark Seam";
1091 ot->idname = "MESH_OT_mark_seam";
1092 ot->description = "(Un)mark selected edges as a seam";
1093
1094 /* API callbacks. */
1097
1098 /* flags */
1100
1101 prop = RNA_def_boolean(ot->srna, "clear", false, "Clear", "");
1103
1105}
1106
1108
1109/* -------------------------------------------------------------------- */
1112
1114{
1115 BMEdge *eed;
1116 BMIter iter;
1117 const bool clear = RNA_boolean_get(op->ptr, "clear");
1118 const bool use_verts = RNA_boolean_get(op->ptr, "use_verts");
1119 const Main *bmain = CTX_data_main(C);
1120 const Scene *scene = CTX_data_scene(C);
1121 ViewLayer *view_layer = CTX_data_view_layer(C);
1122
1124 *bmain, scene, view_layer, CTX_wm_view3d(C));
1125 for (Object *obedit : objects) {
1127
1128 if ((use_verts && bm->totvertsel == 0) || (!use_verts && bm->totedgesel == 0)) {
1129 continue;
1130 }
1131
1132 BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
1133 if (use_verts) {
1134 if (!(BM_elem_flag_test(eed->v1, BM_ELEM_SELECT) ||
1136 {
1137 continue;
1138 }
1139 }
1140 else if (!BM_elem_flag_test(eed, BM_ELEM_SELECT)) {
1141 continue;
1142 }
1143
1144 BM_elem_flag_set(eed, BM_ELEM_SMOOTH, clear);
1145 }
1146
1147 EDBMUpdate_Params params{};
1148 params.calc_looptris = true;
1149 params.calc_normals = false;
1150 params.is_destructive = false;
1151 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
1152 }
1153
1154 return OPERATOR_FINISHED;
1155}
1156
1158{
1159 PropertyRNA *prop;
1160
1161 /* identifiers */
1162 ot->name = "Mark Sharp";
1163 ot->idname = "MESH_OT_mark_sharp";
1164 ot->description = "(Un)mark selected edges as sharp";
1165
1166 /* API callbacks. */
1169
1170 /* flags */
1172
1173 prop = RNA_def_boolean(ot->srna, "clear", false, "Clear", "");
1175 prop = RNA_def_boolean(
1176 ot->srna,
1177 "use_verts",
1178 false,
1179 "Vertices",
1180 "Consider vertices instead of edges to select which edges to (un)tag as sharp");
1182}
1183
1185
1186/* -------------------------------------------------------------------- */
1189
1191{
1193 BMOperator bmop;
1194 const int verts_len = bm->totvertsel;
1195 bool is_pair = (verts_len == 2);
1196 int len = 0;
1197 bool check_degenerate = true;
1198
1199 bool checks_succeded = true;
1200
1201 /* sanity check */
1202 if (verts_len < 2) {
1203 return false;
1204 }
1205
1206 BMVert **verts = MEM_new_array_uninitialized<BMVert *>(verts_len, __func__);
1207 {
1208 BMIter iter;
1209 BMVert *v;
1210 int i = 0;
1211
1212 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
1214 verts[i++] = v;
1215 }
1216 }
1217
1220 {
1221 check_degenerate = false;
1222 is_pair = false;
1223 }
1224 }
1225
1226 if (is_pair) {
1227 if (!EDBM_op_init(bm,
1228 &bmop,
1229 op,
1230 "connect_vert_pair verts=%eb verts_exclude=%hv faces_exclude=%hf",
1231 verts,
1232 verts_len,
1235 {
1236 checks_succeded = false;
1237 }
1238 }
1239 else {
1240 if (!EDBM_op_init(bm,
1241 &bmop,
1242 op,
1243 "connect_verts verts=%eb faces_exclude=%hf check_degenerate=%b",
1244 verts,
1245 verts_len,
1247 check_degenerate))
1248 {
1249 checks_succeded = false;
1250 }
1251 }
1252 if (checks_succeded) {
1253 BMBackup em_backup = EDBM_redo_state_store(bm);
1254
1256
1257 BMO_op_exec(bm, &bmop);
1258 const bool failure = BMO_error_occurred_at_level(bm, BMO_ERROR_FATAL);
1259 len = BMO_slot_get(bmop.slots_out, "edges.out")->len;
1260
1261 if (len && is_pair) {
1262 /* new verts have been added, we have to select the edges, not just flush */
1264 }
1265
1266 bool em_backup_free = true;
1267 if (!EDBM_op_finish(bm, &bmop, op, false)) {
1268 len = 0;
1269 }
1270 else if (failure) {
1271 len = 0;
1272 EDBM_redo_state_restore_and_free(&em_backup, em, bm, true);
1273 em_backup_free = false;
1274 }
1275 else {
1276 /* so newly created edges get the selection state from the vertex */
1278
1280
1281 EDBMUpdate_Params params{};
1282 params.calc_looptris = true;
1283 params.calc_normals = false;
1284 params.is_destructive = true;
1285 EDBM_update(mesh, &params);
1286 }
1287
1288 if (em_backup_free) {
1289 EDBM_redo_state_free(&em_backup);
1290 }
1291 }
1292 MEM_delete(verts);
1293
1294 return len;
1295}
1296
1298{
1299 const Main *bmain = CTX_data_main(C);
1300 const Scene *scene = CTX_data_scene(C);
1301 ViewLayer *view_layer = CTX_data_view_layer(C);
1302 uint failed_objects_len = 0;
1304 *bmain, scene, view_layer, CTX_wm_view3d(C));
1305
1306 for (Object *obedit : objects) {
1308
1309 if (!edbm_connect_vert_pair(em, id_cast<Mesh *>(obedit->data), op)) {
1310 failed_objects_len++;
1311 }
1312 }
1313 return failed_objects_len == objects.size() ? OPERATOR_CANCELLED : OPERATOR_FINISHED;
1314}
1315
1317{
1318 /* identifiers */
1319 ot->name = "Vertex Connect";
1320 ot->idname = "MESH_OT_vert_connect";
1321 ot->description = "Connect selected vertices of faces, splitting the face";
1322
1323 /* API callbacks. */
1326
1327 /* flags */
1329}
1330
1332
1333/* -------------------------------------------------------------------- */
1336
1341{
1342 BMEditSelection *ele_a = bm->selected.first();
1343 BMEditSelection *ele_b = bm->selected.last();
1344 if ((ele_a->htype == BM_VERT) && (ele_b->htype == BM_VERT)) {
1346 BM_EDGES_OF_VERT, reinterpret_cast<BMVert *>(ele_a->ele), BM_ELEM_SELECT, true) ==
1347 1) &&
1349 BM_EDGES_OF_VERT, reinterpret_cast<BMVert *>(ele_b->ele), BM_ELEM_SELECT, true) == 1))
1350 {
1351 return true;
1352 }
1353 }
1354
1355 return false;
1356}
1357
1359 BMVert *v_a,
1360 BMVert *v_b,
1361 const bool skip_normals = false)
1362{
1363 BMOperator bmop;
1364 BMVert **verts;
1365 const int totedge_orig = bm->totedge;
1366
1367 BMO_op_init(bm, &bmop, BMO_FLAG_DEFAULTS, "connect_vert_pair");
1368
1369 verts = static_cast<BMVert **>(BMO_slot_buffer_alloc(&bmop, bmop.slots_in, "verts", 2));
1370 verts[0] = v_a;
1371 verts[1] = v_b;
1372
1373 /* Note that normals may be overridden when connecting more than 2 vertices. */
1374 if (!skip_normals) {
1375 BM_vert_normal_update(verts[0]);
1376 BM_vert_normal_update(verts[1]);
1377 }
1378
1379 BMO_op_exec(bm, &bmop);
1381 BMO_op_finish(bm, &bmop);
1382 return (bm->totedge != totedge_orig);
1383}
1384
1386{
1387 /* Logic is as follows:
1388 *
1389 * - If there are any isolated/wire verts - connect as edges.
1390 * - Otherwise connect faces.
1391 * - If all edges have been created already, closed the loop.
1392 */
1393 if (BLI_listbase_count_at_most(&bm->selected, 2) == 2 && (bm->totvertsel > 2)) {
1394 BMEditSelection *ese;
1395 int tot = 0;
1396 bool changed = false;
1397 bool has_wire = false;
1398 // bool all_verts;
1399
1400 /* ensure all verts have history */
1401 for (ese = bm->selected.first(); ese; ese = ese->next, tot++) {
1402 BMVert *v;
1403 if (ese->htype != BM_VERT) {
1404 break;
1405 }
1406 v = reinterpret_cast<BMVert *>(ese->ele);
1407 if ((has_wire == false) && ((v->e == nullptr) || BM_vert_is_wire(v))) {
1408 has_wire = true;
1409 }
1410 }
1411 // all_verts = (ese == nullptr);
1412
1413 if (has_wire == false) {
1414 /* all verts have faces , connect verts via faces! */
1415 if (tot == bm->totvertsel) {
1416 BMEditSelection *ese_last;
1417 Map<BMVert *, float3> orig_normals;
1418
1419 /* Connecting more than 2 vertices can change the mesh normal state, which can break
1420 * symmetry in cases where it is expected so we store the original normals to restore
1421 * later before connecting. See #154197 */
1422 const bool is_multi_cut = (bm->totvertsel > 2);
1423 if (is_multi_cut) {
1424 for (ese_last = bm->selected.first(); ese_last; ese_last = ese_last->next) {
1425 BMVert *v = reinterpret_cast<BMVert *>(ese_last->ele);
1427 orig_normals.add(v, v->no);
1428 }
1429 }
1430 ese_last = bm->selected.first();
1431 ese = ese_last->next;
1432
1433 do {
1434
1435 if (BM_edge_exists(reinterpret_cast<BMVert *>(ese_last->ele),
1436 reinterpret_cast<BMVert *>(ese->ele)))
1437 {
1438 /* pass, edge exists (and will be selected) */
1439 }
1440 else {
1441 BMVert *v_last = reinterpret_cast<BMVert *>(ese_last->ele);
1442 BMVert *v_curr = reinterpret_cast<BMVert *>(ese->ele);
1443
1444 if (is_multi_cut) {
1445 BLI_assert(orig_normals.contains(v_last));
1446 BLI_assert(orig_normals.contains(v_curr));
1447 copy_v3_v3(v_last->no, orig_normals.lookup(v_last));
1448 copy_v3_v3(v_curr->no, orig_normals.lookup(v_curr));
1449 }
1450 changed |= bm_vert_connect_pair(bm, v_last, v_curr, is_multi_cut);
1451 }
1452 } while ((void)(ese_last = ese), (ese = ese->next));
1453
1454 if (changed) {
1455 return true;
1456 }
1457 }
1458
1459 if (changed == false) {
1460 /* existing loops: close the selection */
1462 changed |= bm_vert_connect_pair(bm,
1463 reinterpret_cast<BMVert *>(bm->selected.first()->ele),
1464 reinterpret_cast<BMVert *>(bm->selected.last()->ele));
1465
1466 if (changed) {
1467 return true;
1468 }
1469 }
1470 }
1471 }
1472
1473 else {
1474 /* no faces, simply connect the verts by edges */
1475 BMEditSelection *ese_prev;
1476 ese_prev = bm->selected.first();
1477 ese = ese_prev->next;
1478
1479 do {
1480 if (BM_edge_exists(reinterpret_cast<BMVert *>(ese_prev->ele),
1481 reinterpret_cast<BMVert *>(ese->ele)))
1482 {
1483 /* pass, edge exists (and will be selected) */
1484 }
1485 else {
1486 BMEdge *e;
1488 reinterpret_cast<BMVert *>(ese_prev->ele),
1489 reinterpret_cast<BMVert *>(ese->ele),
1490 nullptr,
1491 eBMCreateFlag(0));
1492 BM_edge_select_set(bm, e, true);
1493 changed = true;
1494 }
1495 } while ((void)(ese_prev = ese), (ese = ese->next));
1496
1497 if (changed == false) {
1498 /* existing loops: close the selection */
1500 BMEdge *e;
1501 ese_prev = bm->selected.first();
1502 ese = bm->selected.last();
1504 reinterpret_cast<BMVert *>(ese_prev->ele),
1505 reinterpret_cast<BMVert *>(ese->ele),
1506 nullptr,
1507 eBMCreateFlag(0));
1508 BM_edge_select_set(bm, e, true);
1509 }
1510 }
1511
1512 return true;
1513 }
1514 }
1515
1516 return false;
1517}
1518
1524 BMesh *bm, ListBaseT<BMEditSelection> *r_selected)
1525{
1526 ListBaseT<BMEditSelection> selected_orig = {nullptr, nullptr};
1527 int edges_len = 0;
1528 bool side = false;
1529
1530 /* first check all edges are OK */
1531 for (BMEditSelection &ese : bm->selected) {
1532 if (ese.htype == BM_EDGE) {
1533 edges_len += 1;
1534 }
1535 else {
1536 return false;
1537 }
1538 }
1539 /* if this is a mixed selection, bail out! */
1540 if (bm->totedgesel != edges_len) {
1541 return false;
1542 }
1543
1544 std::swap(bm->selected, selected_orig);
1545
1546 /* convert edge selection into 2 ordered loops (where the first edge ends up in the middle) */
1547 for (BMEditSelection &ese : selected_orig) {
1548 BMEdge *e_curr = reinterpret_cast<BMEdge *>(ese.ele);
1549 BMEdge *e_prev = ese.prev ? reinterpret_cast<BMEdge *>(ese.prev->ele) : nullptr;
1550 BMLoop *l_curr;
1551 BMLoop *l_prev;
1552 BMVert *v;
1553
1554 if (e_prev) {
1555 BMFace *f = BM_edge_pair_share_face_by_len(e_curr, e_prev, &l_curr, &l_prev, true);
1556 if (f) {
1557 if ((e_curr->v1 != l_curr->v) == (e_prev->v1 != l_prev->v)) {
1558 side = !side;
1559 }
1560 }
1561 else if (is_quad_flip_v3(e_curr->v1->co, e_curr->v2->co, e_prev->v2->co, e_prev->v1->co)) {
1562 side = !side;
1563 }
1564 }
1565
1566 v = (&e_curr->v1)[side];
1567 if (!bm->selected.last() || reinterpret_cast<BMVert *>(bm->selected.last()->ele) != v) {
1569 }
1570
1571 v = (&e_curr->v1)[!side];
1572 if (!bm->selected.first() || reinterpret_cast<BMVert *>(bm->selected.first()->ele) != v) {
1574 }
1575
1576 e_prev = e_curr;
1577 }
1578
1579 *r_selected = bm->selected;
1580 bm->selected = selected_orig;
1581
1582 return true;
1583}
1584
1586{
1587 const Main *bmain = CTX_data_main(C);
1588 const Scene *scene = CTX_data_scene(C);
1589 ViewLayer *view_layer = CTX_data_view_layer(C);
1590 uint failed_selection_order_len = 0;
1591 uint failed_connect_len = 0;
1592 bool has_select_history_mixed = false;
1593 bool has_select_history_face = false;
1595 *bmain, scene, view_layer, CTX_wm_view3d(C));
1596
1597 for (Object *obedit : objects) {
1600 const bool is_pair = (bm->totvertsel == 2);
1601 ListBaseT<BMEditSelection> selected_orig = {nullptr, nullptr};
1602
1603 if (bm->totvertsel == 0) {
1604 continue;
1605 }
1606
1607 /* when there is only 2 vertices, we can ignore selection order */
1608 if (is_pair) {
1609 if (!edbm_connect_vert_pair(em, id_cast<Mesh *>(obedit->data), op)) {
1610 failed_connect_len++;
1611 }
1612 continue;
1613 }
1614
1615 /* Skip mixed selections since path handling only supports uniform types, see #147150. */
1616 const char htype_selected = BM_select_history_htype_all(bm);
1617 if (count_bits_i(htype_selected) > 1) {
1618 has_select_history_mixed = true;
1619 failed_selection_order_len++;
1620 continue;
1621 }
1622 /* Faces are not supported, this check is only done to show a more useful error. */
1623 if (htype_selected & BM_FACE) {
1624 has_select_history_face = true;
1625 failed_selection_order_len++;
1626 continue;
1627 }
1628
1629 if (bm->selected.first()) {
1630 BMEditSelection *ese = bm->selected.first();
1631 if (ese->htype == BM_EDGE) {
1633 std::swap(bm->selected, selected_orig);
1634 }
1635 }
1636 }
1637
1639
1642
1644
1645 EDBMUpdate_Params params{};
1646 params.calc_looptris = true;
1647 params.calc_normals = false;
1648 params.is_destructive = true;
1649 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
1650 }
1651 else {
1652 failed_selection_order_len++;
1653 }
1654
1655 if (!selected_orig.is_empty()) {
1657 bm->selected = selected_orig;
1658 }
1659 }
1660
1661 if (failed_selection_order_len == objects.size()) {
1662 if (has_select_history_mixed) {
1663 BKE_report(op->reports, RPT_ERROR, "Could not connect mixed selection types");
1664 }
1665 else if (has_select_history_face) {
1666 BKE_report(op->reports, RPT_ERROR, "Could not connect a face selection");
1667 }
1668 else {
1669 BKE_report(op->reports, RPT_ERROR, "Invalid selection order");
1670 }
1671 return OPERATOR_CANCELLED;
1672 }
1673 if (failed_connect_len == objects.size()) {
1674 BKE_report(op->reports, RPT_ERROR, "Could not connect vertices");
1675 return OPERATOR_CANCELLED;
1676 }
1677
1678 return OPERATOR_FINISHED;
1679}
1680
1682{
1683 /* identifiers */
1684 ot->name = "Vertex Connect Path";
1685 ot->idname = "MESH_OT_vert_connect_path";
1686 ot->description = "Connect vertices by their selection order, creating edges, splitting faces";
1687
1688 /* API callbacks. */
1691
1692 /* flags */
1694}
1695
1697
1698/* -------------------------------------------------------------------- */
1701
1703{
1704 const Main *bmain = CTX_data_main(C);
1705 const Scene *scene = CTX_data_scene(C);
1706 ViewLayer *view_layer = CTX_data_view_layer(C);
1708 *bmain, scene, view_layer, CTX_wm_view3d(C));
1709 for (Object *obedit : objects) {
1711
1712 if (bm->totfacesel == 0) {
1713 continue;
1714 }
1715
1717 bm, op, "faces.out", true, "connect_verts_concave faces=%hf", BM_ELEM_SELECT))
1718 {
1719 continue;
1720 }
1721 EDBMUpdate_Params params{};
1722 params.calc_looptris = true;
1723 params.calc_normals = false;
1724 params.is_destructive = true;
1725 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
1726 }
1727
1728 return OPERATOR_FINISHED;
1729}
1730
1732{
1733 /* identifiers */
1734 ot->name = "Split Concave Faces";
1735 ot->idname = "MESH_OT_vert_connect_concave";
1736 ot->description = "Split concave faces by connecting vertices to make them convex";
1737
1738 /* API callbacks. */
1741
1742 /* flags */
1744}
1745
1747
1748/* -------------------------------------------------------------------- */
1751
1753{
1754 const Main *bmain = CTX_data_main(C);
1755 const Scene *scene = CTX_data_scene(C);
1756 ViewLayer *view_layer = CTX_data_view_layer(C);
1757 const float angle_limit = RNA_float_get(op->ptr, "angle_limit");
1759 *bmain, scene, view_layer, CTX_wm_view3d(C));
1760
1761 for (Object *obedit : objects) {
1763
1764 if (bm->totfacesel == 0) {
1765 continue;
1766 }
1767
1769 op,
1770 "faces.out",
1771 true,
1772 "connect_verts_nonplanar faces=%hf angle_limit=%f",
1774 angle_limit))
1775 {
1776 continue;
1777 }
1778
1779 EDBMUpdate_Params params{};
1780 params.calc_looptris = true;
1781 params.calc_normals = false;
1782 params.is_destructive = true;
1783 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
1784 }
1785
1786 return OPERATOR_FINISHED;
1787}
1788
1790{
1791 PropertyRNA *prop;
1792
1793 /* identifiers */
1794 ot->name = "Split Non-Planar Faces";
1795 ot->idname = "MESH_OT_vert_connect_nonplanar";
1796 ot->description = "Split non-planar faces that exceed the angle threshold";
1797
1798 /* API callbacks. */
1801
1802 /* flags */
1804
1805 /* props */
1806 prop = RNA_def_float_rotation(ot->srna,
1807 "angle_limit",
1808 0,
1809 nullptr,
1810 0.0f,
1811 DEG2RADF(180.0f),
1812 "Max Angle",
1813 "Angle limit",
1814 0.0f,
1815 DEG2RADF(180.0f));
1817}
1818
1820
1821/* -------------------------------------------------------------------- */
1824
1826{
1827 const Main *bmain = CTX_data_main(C);
1828 const Scene *scene = CTX_data_scene(C);
1829 ViewLayer *view_layer = CTX_data_view_layer(C);
1831 *bmain, scene, view_layer, CTX_wm_view3d(C));
1832
1833 const int repeat = RNA_int_get(op->ptr, "repeat");
1834 const float fac = RNA_float_get(op->ptr, "factor");
1835
1836 int totobjects = 0;
1837
1838 for (Object *obedit : objects) {
1840
1841 if (bm->totfacesel == 0) {
1842 continue;
1843 }
1844
1846 continue;
1847 }
1848
1849 totobjects++;
1850
1851 if (!EDBM_op_callf(
1852 bm, op, "planar_faces faces=%hf iterations=%i factor=%f", BM_ELEM_SELECT, repeat, fac))
1853 {
1854 continue;
1855 }
1856
1857 EDBMUpdate_Params params{};
1858 params.calc_looptris = true;
1859 params.calc_normals = true;
1860 params.is_destructive = true;
1861 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
1862 }
1863
1864 return totobjects ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
1865}
1866
1868{
1869 /* identifiers */
1870 ot->name = "Make Planar Faces";
1871 ot->idname = "MESH_OT_face_make_planar";
1872 ot->description = "Flatten selected faces";
1873
1874 /* API callbacks. */
1877
1878 /* flags */
1880
1881 /* props */
1882 RNA_def_float(ot->srna, "factor", 1.0f, -10.0f, 10.0f, "Factor", "", 0.0f, 1.0f);
1883 RNA_def_int(ot->srna, "repeat", 1, 1, 10000, "Iterations", "", 1, 200);
1884}
1885
1887
1888/* -------------------------------------------------------------------- */
1891
1893{
1895 if (bm->totedgesel == 0) {
1896 return false;
1897 }
1898
1900
1902 bm, op, "edges.out", false, "split_edges edges=%he", BM_ELEM_SELECT))
1903 {
1904 return false;
1905 }
1906
1908
1910 EDBMUpdate_Params params{};
1911 params.calc_looptris = true;
1912 params.calc_normals = false;
1913 params.is_destructive = true;
1914 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
1915
1916 return true;
1917}
1918
1920{
1922
1923 /* Note that tracking vertices through the 'split_edges' operator is complicated.
1924 * Instead, tag loops for selection. */
1925 if (bm->totvertsel == 0) {
1926 return false;
1927 }
1928
1930
1931 /* Flush from vertices to edges. */
1932 BMIter iter;
1933 BMEdge *eed;
1934 BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
1936 if (eed->l != nullptr) {
1939 {
1941 }
1942 /* Store selection in loop tags. */
1943 BMLoop *l_iter = eed->l;
1944 do {
1946 } while ((l_iter = l_iter->radial_next) != eed->l);
1947 }
1948 }
1949
1950 if (!EDBM_op_callf(bm,
1951 op,
1952 "split_edges edges=%he verts=%hv use_verts=%b",
1955 true))
1956 {
1957 return false;
1958 }
1959
1960 BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
1961 if (eed->l != nullptr) {
1962 BMLoop *l_iter = eed->l;
1963 do {
1964 if (BM_elem_flag_test(l_iter, BM_ELEM_TAG)) {
1965 BM_vert_select_set(bm, l_iter->v, true);
1966 }
1967 } while ((l_iter = l_iter->radial_next) != eed->l);
1968 }
1969 else {
1970 /* Split out wire. */
1971 for (int i = 0; i < 2; i++) {
1972 BMVert *v = *(&eed->v1 + i);
1974 if (eed != BM_DISK_EDGE_NEXT(eed, v)) {
1975 BM_vert_separate(bm, v, &eed, 1, true, nullptr, nullptr);
1976 }
1977 }
1978 }
1979 }
1980 }
1981
1983
1985 EDBMUpdate_Params params{};
1986 params.calc_looptris = true;
1987 params.calc_normals = false;
1988 params.is_destructive = true;
1989 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
1990
1991 return true;
1992}
1993
1995{
1996 const int type = RNA_enum_get(op->ptr, "type");
1997
1998 const Main *bmain = CTX_data_main(C);
1999 const Scene *scene = CTX_data_scene(C);
2000 ViewLayer *view_layer = CTX_data_view_layer(C);
2002 *bmain, scene, view_layer, CTX_wm_view3d(C));
2003 for (Object *obedit : objects) {
2005
2006 switch (type) {
2007 case BM_VERT:
2008 if (!edbm_edge_split_selected_verts(op, obedit, em)) {
2009 continue;
2010 }
2011 break;
2012 case BM_EDGE:
2013 if (!edbm_edge_split_selected_edges(op, obedit, em)) {
2014 continue;
2015 }
2016 break;
2017 default:
2018 BLI_assert(0);
2019 }
2020 }
2021
2022 return OPERATOR_FINISHED;
2023}
2024
2026{
2027 /* identifiers */
2028 ot->name = "Edge Split";
2029 ot->idname = "MESH_OT_edge_split";
2030 ot->description = "Split selected edges so that each neighbor face gets its own copy";
2031
2032 /* API callbacks. */
2035
2036 /* flags */
2038
2039 /* properties */
2040 static const EnumPropertyItem split_type_items[] = {
2041 {BM_EDGE, "EDGE", 0, "Faces by Edges", "Split faces along selected edges"},
2042 {BM_VERT,
2043 "VERT",
2044 0,
2045 "Faces & Edges by Vertices",
2046 "Split faces and edges connected to selected vertices"},
2047 {0, nullptr, 0, nullptr, nullptr},
2048 };
2049
2050 ot->prop = RNA_def_enum(
2051 ot->srna, "type", split_type_items, BM_EDGE, "Type", "Method to use for splitting");
2052}
2053
2055
2056/* -------------------------------------------------------------------- */
2059
2061{
2062 const Main *bmain = CTX_data_main(C);
2063 const Scene *scene = CTX_data_scene(C);
2064 ViewLayer *view_layer = CTX_data_view_layer(C);
2066 *bmain, scene, view_layer, CTX_wm_view3d(C));
2067 bool changed = false;
2068
2069 for (Object *obedit : objects) {
2071 if (bm->totvertsel == 0) {
2072 continue;
2073 }
2074
2075 BMOperator bmop;
2076 changed = true;
2077
2079 &bmop,
2080 op,
2081 "duplicate geom=%hvef use_select_history=%b use_edge_flip_from_face=%b",
2083 true,
2084 true);
2085
2086 BMO_op_exec(bm, &bmop);
2087
2088 /* de-select all would clear otherwise */
2090
2092
2094 bm, bmop.slots_out, "geom.out", BM_ALL_NOLOOP, BM_ELEM_SELECT, true);
2095
2096 /* Rebuild edit-selection. */
2098
2099 if (!EDBM_op_finish(bm, &bmop, op, true)) {
2100 continue;
2101 }
2102 EDBMUpdate_Params params{};
2103 params.calc_looptris = true;
2104 params.calc_normals = false;
2105 params.is_destructive = true;
2106 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
2107 }
2108
2109 return (changed) ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
2110}
2111
2113 wmOperator *op,
2114 const wmEvent * /*event*/)
2115{
2116 WM_cursor_wait(true);
2117 const wmOperatorStatus retval = edbm_duplicate_exec(C, op);
2118 WM_cursor_wait(false);
2119
2120 return retval;
2121}
2122
2124{
2125 /* identifiers */
2126 ot->name = "Duplicate";
2127 ot->description = "Duplicate selected vertices, edges or faces";
2128 ot->idname = "MESH_OT_duplicate";
2129
2130 /* API callbacks. */
2133
2135
2136 /* to give to transform */
2137 PropertyRNA *prop = RNA_def_int(
2138 ot->srna, "mode", ed::transform::TFM_TRANSLATION, 0, INT_MAX, "Mode", "", 0, INT_MAX);
2140}
2141
2143{
2144 BMLoopNorEditDataArray *lnors_ed_arr = nullptr;
2145 if (CustomData_has_layer_named(&bm->ldata, CD_PROP_INT16_2D, "custom_normal")) {
2146 /* The mesh has custom normal data, update these too.
2147 * Otherwise they will be left in a mangled state.
2148 */
2151 bm,
2152 true,
2153 /* Force #BM_FACE because the loop below operates on all selected faces. */
2154 BM_FACE);
2155 }
2156
2157 return lnors_ed_arr;
2158}
2159
2161{
2162 if (!lnors_ed_arr) {
2163 return false;
2164 }
2165
2166 if (lnors_ed_arr->totloop == 0) {
2167 /* No loops normals to flip, exit early! */
2168 return false;
2169 }
2170
2173
2174 /* We need to recreate the custom normal array because the clnors_data will
2175 * be mangled because we swapped the loops around when we flipped the faces. */
2177 bm,
2178 true,
2179 /* Force #BM_FACE because the loop below operates on all selected faces. */
2180 BM_FACE);
2181
2182 {
2183 /* We need to recalculate all loop normals in the affected area. Even the ones that are not
2184 * going to be flipped because the clnors data is mangled. */
2185
2186 BMLoopNorEditData *lnor_ed_new_full = lnors_ed_arr_new_full->lnor_editdata;
2187 for (int i = 0; i < lnors_ed_arr_new_full->totloop; i++, lnor_ed_new_full++) {
2188
2189 BMLoopNorEditData *lnor_ed =
2190 lnors_ed_arr->lidx_to_lnor_editdata[lnor_ed_new_full->loop_index];
2191
2192 BLI_assert(lnor_ed != nullptr);
2193
2195 bm->lnor_spacearr->lspacearr[lnor_ed_new_full->loop_index],
2196 lnor_ed->nloc,
2197 lnor_ed_new_full->clnors_data);
2198 }
2199 }
2200
2201 BMFace *f;
2202 BMLoop *l, *l_start;
2203 BMIter iter_f;
2204 BM_ITER_MESH (f, &iter_f, bm, BM_FACES_OF_MESH) {
2205 /* Flip all the custom loop normals on the selected faces. */
2207 continue;
2208 }
2209
2210 /* Because the winding has changed, we need to go the reverse way around the face to get the
2211 * correct placement of the normals. However we need to derive the old loop index to get the
2212 * correct data. Note that the first loop index is the same though. So the loop starts and ends
2213 * in the same place as before the flip.
2214 */
2215
2216 l_start = l = BM_FACE_FIRST_LOOP(f);
2217 int old_index = BM_elem_index_get(l);
2218 do {
2219 int loop_index = BM_elem_index_get(l);
2220
2221 BMLoopNorEditData *lnor_ed = lnors_ed_arr->lidx_to_lnor_editdata[old_index];
2222 BMLoopNorEditData *lnor_ed_new = lnors_ed_arr_new_full->lidx_to_lnor_editdata[loop_index];
2223 BLI_assert(lnor_ed != nullptr && lnor_ed_new != nullptr);
2224
2225 negate_v3(lnor_ed->nloc);
2226
2228 bm->lnor_spacearr->lspacearr[loop_index], lnor_ed->nloc, lnor_ed_new->clnors_data);
2229
2230 old_index++;
2231 l = l->prev;
2232 } while (l != l_start);
2233 }
2234 BM_loop_normal_editdata_array_free(lnors_ed_arr_new_full);
2235 return true;
2236}
2237
2239
2240/* -------------------------------------------------------------------- */
2243
2245{
2246 if (!CustomData_has_layer_named(&bm->ldata, CD_PROP_INT16_2D, "custom_normal")) {
2247 return;
2248 }
2249
2250 /* The mesh has custom normal data, flip them. */
2251
2254 BMLoopNorEditData *lnor_ed = lnors_ed_arr->lnor_editdata;
2255
2256 for (int i = 0; i < lnors_ed_arr->totloop; i++, lnor_ed++) {
2257 negate_v3(lnor_ed->nloc);
2258
2260 bm->lnor_spacearr->lspacearr[lnor_ed->loop_index], lnor_ed->nloc, lnor_ed->clnors_data);
2261 }
2263 EDBMUpdate_Params params{};
2264 params.calc_looptris = true;
2265 params.calc_normals = false;
2266 params.is_destructive = false;
2267 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
2268}
2269
2271{
2272 if (EDBM_op_callf(bm, op, "flip_quad_tessellation faces=%hf", BM_ELEM_SELECT)) {
2273 EDBMUpdate_Params params{};
2274 params.calc_looptris = true;
2275 params.calc_normals = false;
2276 params.is_destructive = false;
2277 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
2278 }
2279}
2280
2282{
2283 bool has_flipped_faces = false;
2284
2285 /* See if we have any custom normals to flip. */
2287
2288 if (EDBM_op_callf(bm, op, "reverse_faces faces=%hf flip_multires=%b", BM_ELEM_SELECT, true)) {
2289 has_flipped_faces = true;
2290 }
2291
2292 if (flip_custom_normals(bm, lnors_ed_arr) || has_flipped_faces) {
2293 EDBMUpdate_Params params{};
2294 params.calc_looptris = true;
2295 params.calc_normals = false;
2296 params.is_destructive = false;
2297 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
2298 }
2299
2300 if (lnors_ed_arr != nullptr) {
2302 }
2303}
2304
2306{
2307 const Main *bmain = CTX_data_main(C);
2308 const Scene *scene = CTX_data_scene(C);
2309 ViewLayer *view_layer = CTX_data_view_layer(C);
2311 *bmain, scene, view_layer, CTX_wm_view3d(C));
2312
2313 for (Object *obedit : objects) {
2315 if (bm->totfacesel == 0) {
2316 continue;
2317 }
2318 edbm_flip_quad_tessellation(op, obedit, bm);
2319 }
2320
2321 return OPERATOR_FINISHED;
2322}
2323
2325{
2326 const bool only_clnors = RNA_boolean_get(op->ptr, "only_clnors");
2327
2328 const Main *bmain = CTX_data_main(C);
2329 const Scene *scene = CTX_data_scene(C);
2330 ViewLayer *view_layer = CTX_data_view_layer(C);
2332 *bmain, scene, view_layer, CTX_wm_view3d(C));
2333
2334 for (Object *obedit : objects) {
2336
2337 if (only_clnors) {
2338 if ((bm->totvertsel == 0) && (bm->totedgesel == 0) && (bm->totfacesel == 0)) {
2339 continue;
2340 }
2342 }
2343 else {
2344 if (bm->totfacesel == 0) {
2345 continue;
2346 }
2348 }
2349 }
2350
2351 return OPERATOR_FINISHED;
2352}
2353
2355{
2356 /* identifiers */
2357 ot->name = "Flip Normals";
2358 ot->description = "Flip the direction of selected faces' normals (and of their vertices)";
2359 ot->idname = "MESH_OT_flip_normals";
2360
2361 /* API callbacks. */
2364
2365 /* flags */
2367
2369 "only_clnors",
2370 false,
2371 "Custom Normals Only",
2372 "Only flip the custom loop normals of the selected elements");
2373}
2374
2376
2377/* -------------------------------------------------------------------- */
2380
2385{
2386 BMEdge *eed;
2387 BMIter iter;
2388 const bool use_ccw = RNA_boolean_get(op->ptr, "use_ccw");
2389
2390 int tot_failed_all = 0;
2391 bool no_selected_edges = true, invalid_selected_edges = true;
2392
2393 const Main *bmain = CTX_data_main(C);
2394 const Scene *scene = CTX_data_scene(C);
2395 ViewLayer *view_layer = CTX_data_view_layer(C);
2397 *bmain, scene, view_layer, CTX_wm_view3d(C));
2398 for (Object *obedit : objects) {
2401 int tot = 0;
2402
2403 if (bm->totedgesel == 0) {
2404 continue;
2405 }
2406 no_selected_edges = false;
2407
2408 /* first see if we have two adjacent faces */
2409 BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
2412 BMFace *fa, *fb;
2413 if (BM_edge_face_pair(eed, &fa, &fb)) {
2414 /* if both faces are selected we rotate between them,
2415 * otherwise - rotate between 2 unselected - but not mixed */
2418 tot++;
2419 }
2420 }
2421 }
2422 }
2423
2424 /* OK, we don't have two adjacent faces, but we do have two selected ones.
2425 * that's an error condition. */
2426 if (tot == 0) {
2427 continue;
2428 }
2429 invalid_selected_edges = false;
2430
2431 BMOperator bmop;
2432 EDBM_op_init(bm, &bmop, op, "rotate_edges edges=%he use_ccw=%b", BM_ELEM_TAG, use_ccw);
2433
2434 /* avoids leaving old verts selected which can be a problem running multiple times,
2435 * since this means the edges become selected around the face
2436 * which then attempt to rotate */
2438
2439 BMO_op_exec(bm, &bmop);
2440 /* edges may rotate into hidden vertices, if this does _not_ run we get an illogical state */
2443
2444 const int tot_rotate = BMO_slot_buffer_len(bmop.slots_out, "edges.out");
2445 const int tot_failed = tot - tot_rotate;
2446
2447 tot_failed_all += tot_failed;
2448
2449 if (tot_failed != 0) {
2450 /* If some edges fail to rotate, we need to re-select them,
2451 * otherwise we can end up with invalid selection
2452 * (unselected edge between 2 selected faces). */
2454 }
2455
2457
2458 if (!EDBM_op_finish(bm, &bmop, op, true)) {
2459 continue;
2460 }
2461
2462 EDBMUpdate_Params params{};
2463 params.calc_looptris = true;
2464 params.calc_normals = false;
2465 params.is_destructive = true;
2466 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
2467 }
2468
2469 if (no_selected_edges) {
2470 BKE_report(
2471 op->reports, RPT_ERROR, "Select edges or face pairs for edge loops to rotate about");
2472 return OPERATOR_CANCELLED;
2473 }
2474
2475 /* Ok, we don't have two adjacent faces, but we do have two selected ones.
2476 * that's an error condition. */
2477 if (invalid_selected_edges) {
2478 BKE_report(op->reports, RPT_ERROR, "Could not find any selected edges that can be rotated");
2479 return OPERATOR_CANCELLED;
2480 }
2481
2482 if (tot_failed_all != 0) {
2483 BKE_reportf(op->reports, RPT_WARNING, "Unable to rotate %d edge(s)", tot_failed_all);
2484 }
2485
2486 return OPERATOR_FINISHED;
2487}
2488
2490{
2491 /* identifiers */
2492 ot->name = "Rotate Selected Edge";
2493 ot->description = "Rotate selected edge or adjoining faces";
2494 ot->idname = "MESH_OT_edge_rotate";
2495
2496 /* API callbacks. */
2499
2500 /* flags */
2502
2503 /* props */
2504 RNA_def_boolean(ot->srna, "use_ccw", false, "Counter Clockwise", "");
2505}
2506
2508
2509/* -------------------------------------------------------------------- */
2512
2514{
2515 const bool unselected = RNA_boolean_get(op->ptr, "unselected");
2516 const Main *bmain = CTX_data_main(C);
2517 const Scene *scene = CTX_data_scene(C);
2518 ViewLayer *view_layer = CTX_data_view_layer(C);
2519 bool changed = false;
2520
2522 *bmain, scene, view_layer, CTX_wm_view3d(C));
2523 for (Object *obedit : objects) {
2526
2527 if (unselected) {
2528 if (em->selectmode & SCE_SELECT_VERTEX) {
2529 if (bm->totvertsel == bm->totvert) {
2530 continue;
2531 }
2532 }
2533 else if (em->selectmode & SCE_SELECT_EDGE) {
2534 if (bm->totedgesel == bm->totedge) {
2535 continue;
2536 }
2537 }
2538 else if (em->selectmode & SCE_SELECT_FACE) {
2539 if (bm->totfacesel == bm->totface) {
2540 continue;
2541 }
2542 }
2543 }
2544 else {
2545 if (bm->totvertsel == 0) {
2546 continue;
2547 }
2548 }
2549
2550 /* Disable as this causes the following problem:
2551 * Selecting an area of interest (on one side of the mesh), "Invert" then "Hide"
2552 * will hide the area of interest too. */
2553#if 0
2554 /* Only if symmetry is enabled. */
2555 EDBM_select_mirrored_extend_all(obedit, em);
2556#endif
2557
2558 if (EDBM_mesh_hide(bm, em->selectmode, unselected)) {
2559 EDBMUpdate_Params params{};
2560 params.calc_looptris = true;
2561 params.calc_normals = false;
2562 params.is_destructive = false;
2563 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
2564 changed = true;
2565 }
2566 }
2567
2568 if (!changed) {
2569 return OPERATOR_CANCELLED;
2570 }
2571
2572 return OPERATOR_FINISHED;
2573}
2574
2576{
2577 /* identifiers */
2578 ot->name = "Hide Selected";
2579 ot->idname = "MESH_OT_hide";
2580 ot->description = "Hide (un)selected vertices, edges or faces";
2581
2582 /* API callbacks. */
2583 ot->exec = edbm_hide_exec;
2585
2586 /* flags */
2588
2589 /* props */
2591 ot->srna, "unselected", false, "Unselected", "Hide unselected rather than selected");
2592}
2593
2595
2596/* -------------------------------------------------------------------- */
2599
2601{
2602 const bool select = RNA_boolean_get(op->ptr, "select");
2603 const Main *bmain = CTX_data_main(C);
2604 const Scene *scene = CTX_data_scene(C);
2605 ViewLayer *view_layer = CTX_data_view_layer(C);
2606
2608 *bmain, scene, view_layer, CTX_wm_view3d(C));
2609 for (Object *obedit : objects) {
2612 if (EDBM_mesh_reveal(bm, em->selectmode, select)) {
2613 EDBMUpdate_Params params{};
2614 params.calc_looptris = true;
2615 params.calc_normals = false;
2616 params.is_destructive = false;
2617 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
2618 }
2619 }
2620
2621 return OPERATOR_FINISHED;
2622}
2623
2625{
2626 /* identifiers */
2627 ot->name = "Reveal Hidden";
2628 ot->idname = "MESH_OT_reveal";
2629 ot->description = "Reveal all hidden vertices, edges and faces";
2630
2631 /* API callbacks. */
2632 ot->exec = edbm_reveal_exec;
2634
2635 /* flags */
2637
2638 RNA_def_boolean(ot->srna, "select", true, "Select", "");
2639}
2640
2642
2643/* -------------------------------------------------------------------- */
2646
2648{
2649 const Main *bmain = CTX_data_main(C);
2650 const Scene *scene = CTX_data_scene(C);
2651 ViewLayer *view_layer = CTX_data_view_layer(C);
2652 const bool inside = RNA_boolean_get(op->ptr, "inside");
2653
2655 *bmain, scene, view_layer, CTX_wm_view3d(C));
2656 for (Object *obedit : objects) {
2658
2659 if (bm->totfacesel == 0) {
2660 continue;
2661 }
2662
2663 BMLoopNorEditDataArray *lnors_ed_arr = nullptr;
2664
2665 if (inside) {
2666 /* Save custom normal data for later so we can flip them correctly. */
2667 lnors_ed_arr = flip_custom_normals_init_data(bm);
2668 }
2669
2670 if (!EDBM_op_callf(bm, op, "recalc_face_normals faces=%hf", BM_ELEM_SELECT)) {
2671 continue;
2672 }
2673
2674 if (inside) {
2675 EDBM_op_callf(bm, op, "reverse_faces faces=%hf flip_multires=%b", BM_ELEM_SELECT, true);
2676 flip_custom_normals(bm, lnors_ed_arr);
2677 if (lnors_ed_arr != nullptr) {
2679 }
2680 }
2681
2682 EDBMUpdate_Params params{};
2683 params.calc_looptris = true;
2684 params.calc_normals = false;
2685 params.is_destructive = false;
2686 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
2687 }
2688
2689 return OPERATOR_FINISHED;
2690}
2691
2693{
2694 /* identifiers */
2695 ot->name = "Recalculate Normals";
2696 ot->description = "Make face and vertex normals point either outside or inside the mesh";
2697 ot->idname = "MESH_OT_normals_make_consistent";
2698
2699 /* API callbacks. */
2702
2703 /* flags */
2705
2706 RNA_def_boolean(ot->srna, "inside", false, "Inside", "");
2707}
2708
2710
2711/* -------------------------------------------------------------------- */
2714
2716{
2717 const float fac = RNA_float_get(op->ptr, "factor");
2718
2719 const bool xaxis = RNA_boolean_get(op->ptr, "xaxis");
2720 const bool yaxis = RNA_boolean_get(op->ptr, "yaxis");
2721 const bool zaxis = RNA_boolean_get(op->ptr, "zaxis");
2722 int repeat = RNA_int_get(op->ptr, "repeat");
2723
2724 if (!repeat) {
2725 repeat = 1;
2726 }
2727
2728 const Main *bmain = CTX_data_main(C);
2729 const Scene *scene = CTX_data_scene(C);
2730 ViewLayer *view_layer = CTX_data_view_layer(C);
2731 int tot_selected = 0, tot_locked = 0;
2733 *bmain, scene, view_layer, CTX_wm_view3d(C));
2734 for (Object *obedit : objects) {
2735 Mesh *mesh = id_cast<Mesh *>(obedit->data);
2738 bool mirrx = false, mirry = false, mirrz = false;
2739 float clip_dist = 0.0f;
2740 const bool use_topology = (mesh->editflag & ME_EDIT_MIRROR_TOPO) != 0;
2741
2742 if (bm->totvertsel == 0) {
2743 continue;
2744 }
2745
2747 tot_locked++;
2748 continue;
2749 }
2750
2751 tot_selected++;
2752
2753 /* mirror before smooth */
2754 if ((id_cast<Mesh *>(obedit->data))->symmetry & ME_SYMMETRY_X) {
2755 EDBM_verts_mirror_cache_begin(em, bm, 0, false, true, false, use_topology);
2756 }
2757
2758 /* if there is a mirror modifier with clipping, flag the verts that
2759 * are within tolerance of the plane(s) of reflection
2760 */
2761 for (ModifierData &md : obedit->modifiers) {
2763 MirrorModifierData *mmd = reinterpret_cast<MirrorModifierData *>(&md);
2764
2765 if (mmd->flag & MOD_MIR_CLIPPING) {
2766 if (mmd->flag & MOD_MIR_AXIS_X) {
2767 mirrx = true;
2768 }
2769 if (mmd->flag & MOD_MIR_AXIS_Y) {
2770 mirry = true;
2771 }
2772 if (mmd->flag & MOD_MIR_AXIS_Z) {
2773 mirrz = true;
2774 }
2775
2776 clip_dist = mmd->tolerance;
2777 }
2778 }
2779 }
2780
2781 for (int i = 0; i < repeat; i++) {
2782 if (!EDBM_op_callf(
2783 bm,
2784 op,
2785 "smooth_vert verts=%hv factor=%f mirror_clip_x=%b mirror_clip_y=%b mirror_clip_z=%b "
2786 "clip_dist=%f use_axis_x=%b use_axis_y=%b use_axis_z=%b",
2788 fac,
2789 mirrx,
2790 mirry,
2791 mirrz,
2792 clip_dist,
2793 xaxis,
2794 yaxis,
2795 zaxis))
2796 {
2797 continue;
2798 }
2799 }
2800
2801 /* NOTE: redundant calculation could be avoided if the EDBM API could skip calculation. */
2802 bool calc_normals = false;
2803
2804 /* apply mirror */
2805 if ((id_cast<Mesh *>(obedit->data))->symmetry & ME_SYMMETRY_X) {
2808 calc_normals = true;
2809 }
2810
2811 EDBMUpdate_Params params{};
2812 params.calc_looptris = true;
2813 params.calc_normals = calc_normals;
2814 params.is_destructive = false;
2815 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
2816 }
2817
2818 if (tot_selected == 0 && !tot_locked) {
2819 BKE_report(op->reports, RPT_WARNING, "No selected vertex");
2820 }
2821
2822 return tot_selected ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
2823}
2824
2826{
2827 /* identifiers */
2828 ot->name = "Smooth Vertices";
2829 ot->description = "Flatten angles of selected vertices";
2830 ot->idname = "MESH_OT_vertices_smooth";
2831
2832 /* API callbacks. */
2835
2836 /* flags */
2838
2840 ot->srna, "factor", 0.0f, -10.0f, 10.0f, "Smoothing", "Smoothing factor", 0.0f, 1.0f);
2842 ot->srna, "repeat", 1, 1, 1000, "Repeat", "Number of times to smooth the mesh", 1, 100);
2843
2845
2846 RNA_def_boolean(ot->srna, "xaxis", true, "X-Axis", "Smooth along the X axis");
2847 RNA_def_boolean(ot->srna, "yaxis", true, "Y-Axis", "Smooth along the Y axis");
2848 RNA_def_boolean(ot->srna, "zaxis", true, "Z-Axis", "Smooth along the Z axis");
2849
2850 /* Set generic modal callbacks. */
2852}
2853
2855
2856/* -------------------------------------------------------------------- */
2859
2861{
2862 int tot_selected = 0, tot_locked = 0;
2863 const Main *bmain = CTX_data_main(C);
2864 const Scene *scene = CTX_data_scene(C);
2865 ViewLayer *view_layer = CTX_data_view_layer(C);
2866
2867 const float lambda_factor = RNA_float_get(op->ptr, "lambda_factor");
2868 const float lambda_border = RNA_float_get(op->ptr, "lambda_border");
2869 const bool usex = RNA_boolean_get(op->ptr, "use_x");
2870 const bool usey = RNA_boolean_get(op->ptr, "use_y");
2871 const bool usez = RNA_boolean_get(op->ptr, "use_z");
2872 const bool preserve_volume = RNA_boolean_get(op->ptr, "preserve_volume");
2873 int repeat = RNA_int_get(op->ptr, "repeat");
2874
2875 if (!repeat) {
2876 repeat = 1;
2877 }
2878
2880 *bmain, scene, view_layer, CTX_wm_view3d(C));
2881 for (Object *obedit : objects) {
2883 Mesh *mesh = id_cast<Mesh *>(obedit->data);
2885 bool use_topology = (mesh->editflag & ME_EDIT_MIRROR_TOPO) != 0;
2886
2887 if (bm->totvertsel == 0) {
2888 continue;
2889 }
2890
2892 tot_locked++;
2893 continue;
2894 }
2895
2896 tot_selected++;
2897
2898 /* Mirror before smooth. */
2899 if ((id_cast<Mesh *>(obedit->data))->symmetry & ME_SYMMETRY_X) {
2900 EDBM_verts_mirror_cache_begin(em, bm, 0, false, true, false, use_topology);
2901 }
2902
2903 bool failed_repeat_loop = false;
2904 for (int i = 0; i < repeat; i++) {
2905 if (!EDBM_op_callf(bm,
2906 op,
2907 "smooth_laplacian_vert verts=%hv lambda_factor=%f lambda_border=%f "
2908 "use_x=%b use_y=%b use_z=%b preserve_volume=%b",
2910 lambda_factor,
2911 lambda_border,
2912 usex,
2913 usey,
2914 usez,
2915 preserve_volume))
2916 {
2917 failed_repeat_loop = true;
2918 break;
2919 }
2920 }
2921 if (failed_repeat_loop) {
2922 continue;
2923 }
2924
2925 /* NOTE: redundant calculation could be avoided if the EDBM API could skip calculation. */
2926 bool calc_normals = false;
2927
2928 /* Apply mirror. */
2929 if ((id_cast<Mesh *>(obedit->data))->symmetry & ME_SYMMETRY_X) {
2932 calc_normals = true;
2933 }
2934
2935 EDBMUpdate_Params params{};
2936 params.calc_looptris = true;
2937 params.calc_normals = calc_normals;
2938 params.is_destructive = false;
2939 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
2940 }
2941
2942 if (tot_selected == 0 && !tot_locked) {
2943 BKE_report(op->reports, RPT_WARNING, "No selected vertex");
2944 }
2945
2946 return tot_selected ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
2947}
2948
2950{
2951 /* identifiers */
2952 ot->name = "Laplacian Smooth Vertices";
2953 ot->description = "Laplacian smooth of selected vertices";
2954 ot->idname = "MESH_OT_vertices_smooth_laplacian";
2955
2956 /* API callbacks. */
2959
2960 /* flags */
2962
2964 ot->srna, "repeat", 1, 1, 1000, "Number of iterations to smooth the mesh", "", 1, 200);
2966 ot->srna, "lambda_factor", 1.0f, 1e-7f, 1000.0f, "Lambda factor", "", 1e-7f, 1000.0f);
2967 RNA_def_float(ot->srna,
2968 "lambda_border",
2969 5e-5f,
2970 1e-7f,
2971 1000.0f,
2972 "Lambda factor in border",
2973 "",
2974 1e-7f,
2975 1000.0f);
2976
2978
2979 RNA_def_boolean(ot->srna, "use_x", true, "Smooth X Axis", "Smooth object along X axis");
2980 RNA_def_boolean(ot->srna, "use_y", true, "Smooth Y Axis", "Smooth object along Y axis");
2981 RNA_def_boolean(ot->srna, "use_z", true, "Smooth Z Axis", "Smooth object along Z axis");
2983 "preserve_volume",
2984 true,
2985 "Preserve Volume",
2986 "Apply volume preservation after smooth");
2987}
2988
2990
2991/* -------------------------------------------------------------------- */
2994
2996{
2997 BMIter iter;
2998 BMFace *efa;
2999
3000 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
3003 }
3004 }
3005}
3006
3008{
3009 const Main *bmain = CTX_data_main(C);
3010 const Scene *scene = CTX_data_scene(C);
3011 ViewLayer *view_layer = CTX_data_view_layer(C);
3013 *bmain, scene, view_layer, CTX_wm_view3d(C));
3014 for (Object *obedit : objects) {
3016 if (bm->totfacesel == 0) {
3017 continue;
3018 }
3019
3021 EDBMUpdate_Params params{};
3022 params.calc_looptris = false;
3023 params.calc_normals = false;
3024 params.is_destructive = false;
3025 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
3026 }
3027
3028 return OPERATOR_FINISHED;
3029}
3030
3032{
3033 /* identifiers */
3034 ot->name = "Shade Smooth";
3035 ot->description = "Display faces smooth (using vertex normals)";
3036 ot->idname = "MESH_OT_faces_shade_smooth";
3037
3038 /* API callbacks. */
3041
3042 /* flags */
3044}
3045
3047
3048/* -------------------------------------------------------------------- */
3051
3053{
3054 const Main *bmain = CTX_data_main(C);
3055 const Scene *scene = CTX_data_scene(C);
3056 ViewLayer *view_layer = CTX_data_view_layer(C);
3058 *bmain, scene, view_layer, CTX_wm_view3d(C));
3059 for (Object *obedit : objects) {
3061 if (bm->totfacesel == 0) {
3062 continue;
3063 }
3064
3066 EDBMUpdate_Params params{};
3067 params.calc_looptris = false;
3068 params.calc_normals = false;
3069 params.is_destructive = false;
3070 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
3071 }
3072
3073 return OPERATOR_FINISHED;
3074}
3075
3077{
3078 /* identifiers */
3079 ot->name = "Shade Flat";
3080 ot->description = "Display faces flat";
3081 ot->idname = "MESH_OT_faces_shade_flat";
3082
3083 /* API callbacks. */
3086
3087 /* flags */
3089}
3090
3092
3093/* -------------------------------------------------------------------- */
3096
3098{
3099 /* get the direction from RNA */
3100 const bool use_ccw = RNA_boolean_get(op->ptr, "use_ccw");
3101
3102 const Main *bmain = CTX_data_main(C);
3103 const Scene *scene = CTX_data_scene(C);
3104 ViewLayer *view_layer = CTX_data_view_layer(C);
3106 *bmain, scene, view_layer, CTX_wm_view3d(C));
3107 for (Object *obedit : objects) {
3109 if (bm->totfacesel == 0) {
3110 continue;
3111 }
3112
3113 BMOperator bmop;
3114
3115 EDBM_op_init(bm, &bmop, op, "rotate_uvs faces=%hf use_ccw=%b", BM_ELEM_SELECT, use_ccw);
3116
3117 BMO_op_exec(bm, &bmop);
3118
3119 if (!EDBM_op_finish(bm, &bmop, op, true)) {
3120 continue;
3121 }
3122
3123 EDBMUpdate_Params params{};
3124 params.calc_looptris = false;
3125 params.calc_normals = false;
3126 params.is_destructive = false;
3127 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
3128 }
3129
3130 return OPERATOR_FINISHED;
3131}
3132
3134{
3135 const Main *bmain = CTX_data_main(C);
3136 const Scene *scene = CTX_data_scene(C);
3137 ViewLayer *view_layer = CTX_data_view_layer(C);
3139 *bmain, scene, view_layer, CTX_wm_view3d(C));
3140 for (Object *obedit : objects) {
3142
3143 if (bm->totfacesel == 0) {
3144 continue;
3145 }
3146
3147 BMOperator bmop;
3148
3149 EDBM_op_init(bm, &bmop, op, "reverse_uvs faces=%hf", BM_ELEM_SELECT);
3150
3151 BMO_op_exec(bm, &bmop);
3152
3153 if (!EDBM_op_finish(bm, &bmop, op, true)) {
3154 continue;
3155 }
3156 EDBMUpdate_Params params{};
3157 params.calc_looptris = false;
3158 params.calc_normals = false;
3159 params.is_destructive = false;
3160 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
3161 }
3162
3163 return OPERATOR_FINISHED;
3164}
3165
3167{
3168 /* get the direction from RNA */
3169 const bool use_ccw = RNA_boolean_get(op->ptr, "use_ccw");
3170
3171 const Main *bmain = CTX_data_main(C);
3172 const Scene *scene = CTX_data_scene(C);
3173 ViewLayer *view_layer = CTX_data_view_layer(C);
3175 *bmain, scene, view_layer, CTX_wm_view3d(C));
3176
3177 for (uint ob_index = 0; ob_index < objects.size(); ob_index++) {
3178 Object *ob = objects[ob_index];
3180 if (bm->totfacesel == 0) {
3181 continue;
3182 }
3183
3184 BMOperator bmop;
3185
3188
3189 int color_index = BKE_attribute_to_index(
3190 owner, mesh->active_color_attribute, ATTR_DOMAIN_MASK_CORNER, CD_MASK_COLOR_ALL);
3191 if (color_index == -1) {
3192 continue;
3193 }
3195 &bmop,
3196 op,
3197 "rotate_colors faces=%hf use_ccw=%b color_index=%i",
3199 use_ccw,
3200 color_index);
3201
3202 BMO_op_exec(bm, &bmop);
3203
3204 if (!EDBM_op_finish(bm, &bmop, op, true)) {
3205 continue;
3206 }
3207
3208 /* dependencies graph and notification stuff */
3209 EDBMUpdate_Params params{};
3210 params.calc_looptris = false;
3211 params.calc_normals = false;
3212 params.is_destructive = false;
3213 EDBM_update(id_cast<Mesh *>(ob->data), &params);
3214 }
3215
3216 return OPERATOR_FINISHED;
3217}
3218
3220{
3221 const Main *bmain = CTX_data_main(C);
3222 const Scene *scene = CTX_data_scene(C);
3223 ViewLayer *view_layer = CTX_data_view_layer(C);
3225 *bmain, scene, view_layer, CTX_wm_view3d(C));
3226
3227 for (Object *obedit : objects) {
3229
3230 if (bm->totfacesel == 0) {
3231 continue;
3232 }
3233
3236
3237 BMOperator bmop;
3238
3239 int color_index = BKE_attribute_to_index(
3240 owner, mesh->active_color_attribute, ATTR_DOMAIN_MASK_CORNER, CD_MASK_COLOR_ALL);
3241 if (color_index == -1) {
3242 continue;
3243 }
3245 bm, &bmop, op, "reverse_colors faces=%hf color_index=%i", BM_ELEM_SELECT, color_index);
3246
3247 BMO_op_exec(bm, &bmop);
3248
3249 if (!EDBM_op_finish(bm, &bmop, op, true)) {
3250 continue;
3251 }
3252
3253 EDBMUpdate_Params params{};
3254 params.calc_looptris = false;
3255 params.calc_normals = false;
3256 params.is_destructive = false;
3257 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
3258 }
3259
3260 return OPERATOR_FINISHED;
3261}
3262
3264{
3265 /* identifiers */
3266 ot->name = "Rotate UVs";
3267 ot->idname = "MESH_OT_uvs_rotate";
3268 ot->description = "Rotate UV coordinates inside faces";
3269
3270 /* API callbacks. */
3273
3274 /* flags */
3276
3277 /* props */
3278 RNA_def_boolean(ot->srna, "use_ccw", false, "Counter Clockwise", "");
3279}
3280
3282{
3283 /* identifiers */
3284 ot->name = "Reverse UVs";
3285 ot->idname = "MESH_OT_uvs_reverse";
3286 ot->description = "Flip direction of UV coordinates inside faces";
3287
3288 /* API callbacks. */
3291
3292 /* flags */
3294
3295 /* props */
3296 // RNA_def_enum(ot->srna, "axis", axis_items, DIRECTION_CW, "Axis", "Axis to mirror UVs around");
3297}
3298
3300{
3301 /* identifiers */
3302 ot->name = "Rotate Colors";
3303 ot->idname = "MESH_OT_colors_rotate";
3304 ot->description = "Rotate face corner color attribute inside faces";
3305
3306 /* API callbacks. */
3309
3310 /* flags */
3312
3313 /* props */
3314 RNA_def_boolean(ot->srna, "use_ccw", false, "Counter Clockwise", "");
3315}
3316
3318{
3319 /* identifiers */
3320 ot->name = "Reverse Colors";
3321 ot->idname = "MESH_OT_colors_reverse";
3322 ot->description = "Flip direction of face corner color attribute inside faces";
3323
3324 /* API callbacks. */
3327
3328 /* flags */
3330
3331 /* props */
3332#if 0
3333 RNA_def_enum(ot->srna, "axis", axis_items, DIRECTION_CW, "Axis", "Axis to mirror colors around");
3334#endif
3335}
3336
3338
3339/* -------------------------------------------------------------------- */
3342
3343enum {
3349};
3350
3352 const bool use_first,
3353 const bool use_uvmerge,
3354 wmOperator *wmop)
3355{
3356 BMVert *mergevert;
3357 BMEditSelection *ese;
3358
3359 /* operator could be called directly from shortcut or python,
3360 * so do extra check for data here
3361 */
3362
3363 /* While #merge_type_itemf does a sanity check, this operation runs on all edit-mode objects.
3364 * Some of them may not have the expected selection state. */
3365 if (use_first == false) {
3366 if (!bm->selected.last() || (bm->selected.last())->htype != BM_VERT) {
3367 return false;
3368 }
3369
3370 ese = bm->selected.last();
3371 mergevert = reinterpret_cast<BMVert *>(ese->ele);
3372 }
3373 else {
3374 if (!bm->selected.first() || (bm->selected.first())->htype != BM_VERT) {
3375 return false;
3376 }
3377
3378 ese = bm->selected.first();
3379 mergevert = reinterpret_cast<BMVert *>(ese->ele);
3380 }
3381
3382 if (!BM_elem_flag_test(mergevert, BM_ELEM_SELECT)) {
3383 return false;
3384 }
3385
3386 if (use_uvmerge) {
3387 if (!EDBM_op_callf(
3388 bm, wmop, "pointmerge_facedata verts=%hv vert_target=%e", BM_ELEM_SELECT, mergevert))
3389 {
3390 return false;
3391 }
3392 }
3393
3394 if (!EDBM_op_callf(bm,
3395 wmop,
3396 "pointmerge verts=%hv merge_co=%v vert_target=%e",
3398 mergevert->co,
3399 mergevert))
3400 {
3401 return false;
3402 }
3403
3404 return true;
3405}
3406
3407static bool merge_target(BMesh *bm,
3408 Scene *scene,
3409 Object *ob,
3410 const bool use_cursor,
3411 const bool use_uvmerge,
3412 wmOperator *wmop)
3413{
3414 BMIter iter;
3415 BMVert *v;
3416 float co[3], cent[3] = {0.0f, 0.0f, 0.0f};
3417 const float *vco = nullptr;
3418
3419 if (use_cursor) {
3420 vco = scene->cursor.location;
3421 copy_v3_v3(co, vco);
3422 invert_m4_m4(ob->runtime->world_to_object.ptr(), ob->object_to_world().ptr());
3423 mul_m4_v3(ob->world_to_object().ptr(), co);
3424 }
3425 else {
3426 float fac;
3427 int i = 0;
3428 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
3430 continue;
3431 }
3432 add_v3_v3(cent, v->co);
3433 i++;
3434 }
3435
3436 if (!i) {
3437 return false;
3438 }
3439
3440 fac = 1.0f / float(i);
3441 mul_v3_fl(cent, fac);
3442 copy_v3_v3(co, cent);
3443 vco = co;
3444 }
3445
3446 if (!vco) {
3447 return false;
3448 }
3449
3450 if (use_uvmerge) {
3451 if (!EDBM_op_callf(bm, wmop, "average_vert_facedata verts=%hv", BM_ELEM_SELECT)) {
3452 return false;
3453 }
3454 }
3455
3456 if (!EDBM_op_callf(bm, wmop, "pointmerge verts=%hv merge_co=%v", BM_ELEM_SELECT, co)) {
3457 return false;
3458 }
3459
3460 return true;
3461}
3462
3464{
3465 const Main *bmain = CTX_data_main(C);
3466 Scene *scene = CTX_data_scene(C);
3467 ViewLayer *view_layer = CTX_data_view_layer(C);
3469 *bmain, scene, view_layer, CTX_wm_view3d(C));
3470 const int type = RNA_enum_get(op->ptr, "type");
3471 const bool uvs = RNA_boolean_get(op->ptr, "uvs");
3472
3473 for (Object *obedit : objects) {
3476
3477 if (bm->totvertsel == 0) {
3478 continue;
3479 }
3480
3482
3483 bool ok = false;
3484 switch (type) {
3485 case MESH_MERGE_CENTER:
3486 ok = merge_target(bm, scene, obedit, false, uvs, op);
3487 break;
3488 case MESH_MERGE_CURSOR:
3489 ok = merge_target(bm, scene, obedit, true, uvs, op);
3490 break;
3491 case MESH_MERGE_LAST:
3492 ok = merge_firstlast(bm, false, uvs, op);
3493 break;
3494 case MESH_MERGE_FIRST:
3495 ok = merge_firstlast(bm, true, uvs, op);
3496 break;
3498 ok = EDBM_op_callf(bm, op, "collapse edges=%he uvs=%b", BM_ELEM_SELECT, uvs);
3499 break;
3500 default:
3501 BLI_assert(0);
3502 break;
3503 }
3504
3505 if (!ok) {
3506 continue;
3507 }
3508
3510
3511 EDBMUpdate_Params params{};
3512 params.calc_looptris = true;
3513 params.calc_normals = false;
3514 params.is_destructive = true;
3515 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
3516
3517 /* once collapsed, we can't have edge/face selection */
3518 if ((em->selectmode & SCE_SELECT_VERTEX) == 0) {
3520 }
3521 /* Only active object supported, see comment below. */
3523 break;
3524 }
3525 }
3526
3527 return OPERATOR_FINISHED;
3528}
3529
3531 {MESH_MERGE_CENTER, "CENTER", 0, "At Center", ""},
3532 {MESH_MERGE_CURSOR, "CURSOR", 0, "At Cursor", ""},
3533 {MESH_MERGE_COLLAPSE, "COLLAPSE", 0, "Collapse", ""},
3534 {MESH_MERGE_FIRST, "FIRST", 0, "At First", ""},
3535 {MESH_MERGE_LAST, "LAST", 0, "At Last", ""},
3536 {0, nullptr, 0, nullptr, nullptr},
3537};
3538
3540 PointerRNA * /*ptr*/,
3541 PropertyRNA * /*prop*/,
3542 bool *r_free)
3543{
3544 if (!C) { /* needed for docs */
3545 return merge_type_items;
3546 }
3547
3548 Object *obedit = CTX_data_edit_object(C);
3549 if (obedit && obedit->type == OB_MESH) {
3550 EnumPropertyItem *item = nullptr;
3551 int totitem = 0;
3554
3555 /* Keep these first so that their automatic shortcuts don't change. */
3559
3560 /* Only active object supported:
3561 * In practice it doesn't make sense to run this operation on non-active meshes
3562 * since selecting will activate - we could have a separate code-path for these but it's a
3563 * hassle for now just apply to the active (first) object. */
3564 if (em->selectmode & SCE_SELECT_VERTEX) {
3565 if (bm->selected.first() && bm->selected.last() &&
3566 (bm->selected.first())->htype == BM_VERT && (bm->selected.last())->htype == BM_VERT)
3567 {
3570 }
3571 else if (bm->selected.first() && (bm->selected.first())->htype == BM_VERT) {
3573 }
3574 else if (bm->selected.last() && (bm->selected.last())->htype == BM_VERT) {
3576 }
3577 }
3578
3579 RNA_enum_item_end(&item, &totitem);
3580
3581 *r_free = true;
3582
3583 return item;
3584 }
3585
3586 /* Get all items e.g. when creating keymap item. */
3587 return merge_type_items;
3588}
3589
3591{
3592 /* identifiers */
3593 ot->name = "Merge";
3594 ot->description = "Merge selected vertices";
3595 ot->idname = "MESH_OT_merge";
3596
3597 /* API callbacks. */
3598 ot->exec = edbm_merge_exec;
3599 ot->invoke = WM_menu_invoke;
3601
3602 /* flags */
3604
3605 /* properties */
3606 ot->prop = RNA_def_enum(
3607 ot->srna, "type", merge_type_items, MESH_MERGE_CENTER, "Type", "Merge method to use");
3610
3612
3613 RNA_def_boolean(ot->srna, "uvs", false, "UVs", "Move UVs according to merge");
3614}
3615
3617
3618/* -------------------------------------------------------------------- */
3621
3623{
3624 const float threshold = RNA_float_get(op->ptr, "threshold");
3625 const bool use_unselected = RNA_boolean_get(op->ptr, "use_unselected");
3626 const bool use_sharp_edge_from_normals = RNA_boolean_get(op->ptr, "use_sharp_edge_from_normals");
3627
3628 int count_multi = 0;
3629
3630 const Main *bmain = CTX_data_main(C);
3631 const Scene *scene = CTX_data_scene(C);
3632 ViewLayer *view_layer = CTX_data_view_layer(C);
3634 *bmain, scene, view_layer, CTX_wm_view3d(C));
3635
3636 for (Object *obedit : objects) {
3639
3640 /* Selection used as target with 'use_unselected'. */
3641 if (bm->totvertsel == 0) {
3642 continue;
3643 }
3644
3645 BMOperator bmop;
3646 const int totvert_orig = bm->totvert;
3647
3648 /* avoid losing selection state (select -> tags) */
3649 char htype_select;
3650 if (em->selectmode & SCE_SELECT_VERTEX) {
3651 htype_select = BM_VERT;
3652 }
3653 else if (em->selectmode & SCE_SELECT_EDGE) {
3654 htype_select = BM_EDGE;
3655 }
3656 else {
3657 htype_select = BM_FACE;
3658 }
3659
3661
3662 /* store selection as tags */
3663 BM_mesh_elem_hflag_enable_test(bm, htype_select, BM_ELEM_TAG, true, true, BM_ELEM_SELECT);
3664
3665 const bool use_centroid = RNA_boolean_get(op->ptr, "use_centroid");
3666
3667 if (use_unselected) {
3668 EDBM_automerge(obedit, false, BM_ELEM_SELECT, threshold, use_centroid);
3669 }
3670 else {
3671 EDBM_op_init(bm, &bmop, op, "find_doubles verts=%hv dist=%f", BM_ELEM_SELECT, threshold);
3672
3673 BMO_op_exec(bm, &bmop);
3674
3675 if (!EDBM_op_callf(bm,
3676 op,
3677 "weld_verts targetmap=%S use_centroid=%b",
3678 &bmop,
3679 "targetmap.out",
3680 use_centroid))
3681 {
3682 BMO_op_finish(bm, &bmop);
3683 continue;
3684 }
3685
3686 if (!EDBM_op_finish(bm, &bmop, op, true)) {
3687 continue;
3688 }
3689 }
3690
3691 const int count = (totvert_orig - bm->totvert);
3692
3693 /* restore selection from tags */
3694 BM_mesh_elem_hflag_enable_test(bm, htype_select, BM_ELEM_SELECT, true, true, BM_ELEM_TAG);
3696
3697 BM_custom_loop_normals_from_vector_layer(bm, use_sharp_edge_from_normals);
3698
3699 if (count) {
3700 count_multi += count;
3701 EDBMUpdate_Params params{};
3702 params.calc_looptris = true;
3703 params.calc_normals = false;
3704 params.is_destructive = true;
3705 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
3706 }
3707 }
3708
3709 BKE_reportf(op->reports,
3710 RPT_INFO,
3711 count_multi == 1 ? RPT_("Removed %d vertex") : RPT_("Removed %d vertices"),
3712 count_multi);
3713
3714 return OPERATOR_FINISHED;
3715}
3716
3718{
3719 /* identifiers */
3720 ot->name = "Merge by Distance";
3721 ot->description = "Merge vertices based on their proximity";
3722 ot->idname = "MESH_OT_remove_doubles";
3723
3724 /* API callbacks. */
3727
3728 /* flags */
3730
3732 "threshold",
3733 1e-4f,
3734 1e-6f,
3735 50.0f,
3736 "Merge Distance",
3737 "Maximum distance between elements to merge",
3738 1e-5f,
3739 10.0f);
3741 "use_centroid",
3742 true,
3743 "Centroid Merge",
3744 "Move vertices to the centroid of the duplicate cluster, "
3745 "otherwise the vertex closest to the centroid is used.");
3746
3748 "use_unselected",
3749 false,
3750 "Unselected",
3751 "Merge selected to other unselected vertices");
3752
3754 "use_sharp_edge_from_normals",
3755 false,
3756 "Sharp Edges",
3757 "Calculate sharp edges using custom normal data (when available)");
3758}
3759
3761
3762/* -------------------------------------------------------------------- */
3765
3766/* BMESH_TODO this should be properly encapsulated in a bmop. but later. */
3767static bool shape_propagate(BMEditMesh *em, BMesh *bm, bool use_symmetry)
3768{
3769 BMIter iter;
3770 BMVert *eve = nullptr;
3771 float *co;
3773
3775 return false;
3776 }
3777
3778 BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
3780 BMVert *mirr = use_symmetry ? EDBM_verts_mirror_get(em, bm, eve) : nullptr;
3781
3782 if (!mirr || !BM_elem_flag_test(mirr, BM_ELEM_SELECT) ||
3784 {
3785 continue;
3786 }
3787 }
3788
3789 for (int i = 0; i < totshape; i++) {
3790 co = static_cast<float *>(
3792 copy_v3_v3(co, eve->co);
3793 }
3794 }
3795 return true;
3796}
3797
3799{
3800 const Main *bmain = CTX_data_main(C);
3801 const Scene *scene = CTX_data_scene(C);
3802 ViewLayer *view_layer = CTX_data_view_layer(C);
3803 int tot_shapekeys = 0;
3804 int tot_selected_verts_objects = 0;
3805 int tot_locked = 0;
3806
3808 *bmain, scene, view_layer, CTX_wm_view3d(C));
3809 for (Object *obedit : objects) {
3810 Mesh *mesh = id_cast<Mesh *>(obedit->data);
3811 BMEditMesh *em = mesh->runtime->edit_mesh.get();
3813
3814 if (bm->totvertsel == 0) {
3815 continue;
3816 }
3817
3818 /* Check for locked shape keys. */
3820 tot_locked++;
3821 continue;
3822 }
3823
3824 tot_selected_verts_objects++;
3825
3826 const bool use_symmetry = (mesh->symmetry & ME_SYMMETRY_X) != 0;
3827
3828 if (use_symmetry) {
3829 const bool use_topology = (mesh->editflag & ME_EDIT_MIRROR_TOPO) != 0;
3830
3831 EDBM_verts_mirror_cache_begin(em, bm, 0, false, false, false, use_topology);
3832 }
3833
3834 if (shape_propagate(em, bm, use_symmetry)) {
3835 tot_shapekeys++;
3836 }
3837
3838 if (use_symmetry) {
3840 }
3841
3842 EDBMUpdate_Params params{};
3843 params.calc_looptris = false;
3844 params.calc_normals = false;
3845 params.is_destructive = false;
3846 EDBM_update(mesh, &params);
3847 }
3848
3849 if (tot_selected_verts_objects == 0) {
3850 if (!tot_locked) {
3851 BKE_report(op->reports, RPT_ERROR, "No selected vertex");
3852 }
3853 return OPERATOR_CANCELLED;
3854 }
3855 if (tot_shapekeys == 0) {
3856 BKE_report(op->reports, RPT_ERROR, "Mesh(es) do not have shape keys");
3857 return OPERATOR_CANCELLED;
3858 }
3859
3860 return OPERATOR_FINISHED;
3861}
3862
3864{
3865 /* identifiers */
3866 ot->name = "Shape Propagate";
3867 ot->description = "Apply selected vertex locations to all other shape keys";
3868 ot->idname = "MESH_OT_shape_propagate_to_all";
3869
3870 /* API callbacks. */
3873
3874 /* flags */
3876}
3877
3879
3880/* -------------------------------------------------------------------- */
3883
3884/* BMESH_TODO this should be properly encapsulated in a bmop. but later. */
3886{
3887 Object *obedit_ref = CTX_data_edit_object(C);
3888 Mesh *me_ref = id_cast<Mesh *>(obedit_ref->data);
3889 Key *key_ref = me_ref->key;
3890 KeyBlock *kb_ref = nullptr;
3891 BMVert *eve;
3892 BMIter iter;
3893 const Main *bmain = CTX_data_main(C);
3894 const Scene *scene = CTX_data_scene(C);
3895 ViewLayer *view_layer = CTX_data_view_layer(C);
3896 float co[3], *sco;
3897 int totshape_ref = 0;
3898
3899 const float blend = RNA_float_get(op->ptr, "blend");
3900 int shape_ref = RNA_enum_get(op->ptr, "shape");
3901 const bool use_add = RNA_boolean_get(op->ptr, "add");
3902
3903 /* Sanity check. */
3904 totshape_ref = CustomData_number_of_layers(&BKE_editmesh_bmesh_get(obedit_ref)->vdata,
3905 CD_SHAPEKEY);
3906
3907 if (totshape_ref == 0 || shape_ref < 0) {
3908 BKE_report(op->reports, RPT_ERROR, "Active mesh does not have shape keys");
3909 return OPERATOR_CANCELLED;
3910 }
3911 if (shape_ref >= totshape_ref) {
3912 /* This case occurs if operator was used before on object with more keys than current one. */
3913 shape_ref = 0; /* default to basis */
3914 }
3915
3916 /* Get shape key - needed for finding reference shape (for add mode only). */
3917 if (key_ref) {
3918 kb_ref = static_cast<KeyBlock *>(BLI_findlink(&key_ref->block, shape_ref));
3919 }
3920
3921 int tot_selected_verts_objects = 0, tot_locked = 0;
3923 *bmain, scene, view_layer, CTX_wm_view3d(C));
3924 for (Object *obedit : objects) {
3925 Mesh *mesh = id_cast<Mesh *>(obedit->data);
3926 Key *key = mesh->key;
3927 KeyBlock *kb = nullptr;
3928 BMEditMesh *em = mesh->runtime->edit_mesh.get();
3930 int shape;
3931
3932 if (bm->totvertsel == 0) {
3933 continue;
3934 }
3935
3937 tot_locked++;
3938 continue;
3939 }
3940
3941 tot_selected_verts_objects++;
3942
3943 if (!key) {
3944 continue;
3945 }
3946 kb = BKE_keyblock_find_name(key, kb_ref->name);
3947 shape = BLI_findindex(&key->block, kb);
3948
3949 if (kb) {
3950 const bool use_symmetry = (mesh->symmetry & ME_SYMMETRY_X) != 0;
3951
3952 if (use_symmetry) {
3953 const bool use_topology = (mesh->editflag & ME_EDIT_MIRROR_TOPO) != 0;
3954
3955 EDBM_verts_mirror_cache_begin(em, bm, 0, false, true, false, use_topology);
3956 }
3957
3958 /* Perform blending on selected vertices. */
3959 BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
3961 continue;
3962 }
3963
3964 /* Get coordinates of shapekey we're blending from. */
3965 sco = static_cast<float *>(
3967 copy_v3_v3(co, sco);
3968
3969 if (use_add) {
3970 /* In add mode, we add relative shape key offset. */
3971 const float *rco = static_cast<const float *>(
3973 sub_v3_v3v3(co, co, rco);
3974
3975 madd_v3_v3fl(eve->co, co, blend);
3976 }
3977 else {
3978 /* In blend mode, we interpolate to the shape key. */
3979 interp_v3_v3v3(eve->co, eve->co, co, blend);
3980 }
3981 }
3982
3983 if (use_symmetry) {
3986 }
3987
3988 EDBMUpdate_Params params{};
3989 params.calc_looptris = true;
3990 params.calc_normals = true;
3991 params.is_destructive = false;
3992 EDBM_update(mesh, &params);
3993 }
3994 }
3995
3996 if (tot_selected_verts_objects == 0 && !tot_locked) {
3997 BKE_report(op->reports, RPT_ERROR, "No selected vertex");
3998 }
3999
4000 return tot_selected_verts_objects ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
4001}
4002
4004 PointerRNA * /*ptr*/,
4005 PropertyRNA * /*prop*/,
4006 bool *r_free)
4007{
4008 Object *obedit = (C) ? CTX_data_edit_object(C) : nullptr;
4009 const BMesh *bm;
4010 EnumPropertyItem *item = nullptr;
4011 int totitem = 0;
4012
4013 if ((obedit && obedit->type == OB_MESH) && (bm = BKE_editmesh_bmesh_get(obedit)) &&
4015 {
4016 EnumPropertyItem tmp = {0, "", 0, "", ""};
4017 int a;
4018
4019 for (a = 0; a < bm->vdata.totlayer; a++) {
4020 if (bm->vdata.layers[a].type != CD_SHAPEKEY) {
4021 continue;
4022 }
4023
4024 tmp.value = totitem;
4025 tmp.identifier = bm->vdata.layers[a].name;
4026 tmp.name = bm->vdata.layers[a].name;
4027 /* RNA_enum_item_add sets totitem itself! */
4028 RNA_enum_item_add(&item, &totitem, &tmp);
4029 }
4030 }
4031
4032 RNA_enum_item_end(&item, &totitem);
4033 *r_free = true;
4034
4035 return item;
4036}
4037
4039{
4040 ui::Layout &layout = *op->layout;
4041 Object *obedit = CTX_data_edit_object(C);
4042 Mesh *mesh = id_cast<Mesh *>(obedit->data);
4043
4044 PointerRNA ptr_key = RNA_id_pointer_create(id_cast<ID *>(mesh->key));
4045
4046 layout.use_property_split_set(true);
4047 layout.use_property_decorate_set(false);
4048
4049 layout.prop_search(op->ptr, "shape", &ptr_key, "key_blocks", std::nullopt, ICON_SHAPEKEY_DATA);
4050 layout.prop(op->ptr, "blend", UI_ITEM_NONE, std::nullopt, ICON_NONE);
4051 layout.prop(op->ptr, "add", UI_ITEM_NONE, std::nullopt, ICON_NONE);
4052}
4053
4055{
4056 PropertyRNA *prop;
4057
4058 /* identifiers */
4059 ot->name = "Blend from Shape";
4060 ot->description = "Blend in shape from a shape key";
4061 ot->idname = "MESH_OT_blend_from_shape";
4062
4063 /* API callbacks. */
4065 /* disable because search popup closes too easily */
4066 // ot->invoke = WM_operator_props_popup_call;
4069
4070 /* flags */
4072
4073 /* properties */
4074 prop = RNA_def_enum(
4075 ot->srna, "shape", rna_enum_dummy_NULL_items, 0, "Shape", "Shape key to use for blending");
4078 RNA_def_float(ot->srna, "blend", 1.0f, -1e3f, 1e3f, "Blend", "Blending factor", -2.0f, 2.0f);
4079 RNA_def_boolean(ot->srna, "add", true, "Add", "Add rather than blend between shapes");
4080}
4081
4083
4084/* -------------------------------------------------------------------- */
4087
4089{
4090 const float thickness = RNA_float_get(op->ptr, "thickness");
4091
4092 const Main *bmain = CTX_data_main(C);
4093 const Scene *scene = CTX_data_scene(C);
4094 ViewLayer *view_layer = CTX_data_view_layer(C);
4096 *bmain, scene, view_layer, CTX_wm_view3d(C));
4097 for (Object *obedit : objects) {
4099
4100 if (bm->totfacesel == 0) {
4101 continue;
4102 }
4103
4104 BMOperator bmop;
4105
4106 if (!EDBM_op_init(bm, &bmop, op, "solidify geom=%hf thickness=%f", BM_ELEM_SELECT, thickness))
4107 {
4108 continue;
4109 }
4110
4111 /* deselect only the faces in the region to be solidified (leave wire
4112 * edges and loose verts selected, as there will be no corresponding
4113 * geometry selected below) */
4115
4116 /* run the solidify operator */
4117 BMO_op_exec(bm, &bmop);
4118
4119 /* select the newly generated faces */
4121
4122 /* No need to flush the selection, any selection history is no longer valid. */
4124
4125 if (!EDBM_op_finish(bm, &bmop, op, true)) {
4126 continue;
4127 }
4128
4129 EDBMUpdate_Params params{};
4130 params.calc_looptris = true;
4131 params.calc_normals = false;
4132 params.is_destructive = true;
4133 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
4134 }
4135
4136 return OPERATOR_FINISHED;
4137}
4138
4140{
4141 PropertyRNA *prop;
4142 /* identifiers */
4143 ot->name = "Solidify";
4144 ot->description = "Create a solid skin by extruding, compensating for sharp angles";
4145 ot->idname = "MESH_OT_solidify";
4146
4147 /* API callbacks. */
4150
4151 /* flags */
4153
4155 ot->srna, "thickness", 0.01f, -1e4f, 1e4f, "Thickness", "", -10.0f, 10.0f);
4156 RNA_def_property_ui_range(prop, -10.0, 10.0, 0.1, 4);
4157}
4158
4160
4161/* -------------------------------------------------------------------- */
4164
4165enum {
4169};
4170
4173 Main *bmain, Scene *scene, ViewLayer *view_layer, Base *base_old, BMesh *bm_old)
4174{
4175 Object *obedit = base_old->object;
4176 BMeshCreateParams create_params{};
4177 create_params.use_toolflags = true;
4178 BMesh *bm_new = BM_mesh_create(&bm_mesh_allocsize_default, &create_params);
4179 BM_mesh_elem_toolflags_ensure(bm_new); /* Needed for 'duplicate' BMO. */
4180
4181 const bool use_custom_normals = (bm_old->lnor_spacearr != nullptr);
4182 if (use_custom_normals) {
4183 /* Needed so the temporary normal layer is copied too. */
4185 }
4186 else {
4188 }
4189
4190 /* Take into account user preferences for duplicating actions. */
4191 const eDupli_ID_Flags dupflag = eDupli_ID_Flags(USER_DUP_MESH | (U.dupflag & USER_DUP_ACT));
4192 Base *base_new = ed::object::add_duplicate(bmain, scene, view_layer, base_old, dupflag);
4193
4194 /* normally would call directly after but in this case delay recalc */
4195 // DAG_relations_tag_update(bmain);
4196
4197 /* new in 2.5 */
4199 base_new->object,
4202 false);
4203
4205
4206 BMO_op_callf(bm_old,
4208 "duplicate geom=%hvef dest=%p",
4210 bm_new);
4211 BMO_op_callf(bm_old,
4213 "delete geom=%hvef context=%i",
4215 DEL_FACES);
4216
4217 /* deselect loose data - this used to get deleted,
4218 * we could de-select edges and verts only, but this turns out to be less complicated
4219 * since de-selecting all skips selection flushing logic */
4221
4222 if (use_custom_normals) {
4224 }
4225 else {
4226 BM_mesh_normals_update(bm_new);
4227 }
4228
4229 BMeshToMeshParams to_mesh_params{};
4230 BM_mesh_bm_to_me(bmain, bm_new, id_cast<Mesh *>(base_new->object->data), &to_mesh_params);
4231
4232 BM_mesh_free(bm_new);
4233 (id_cast<Mesh *>(base_new->object->data))->runtime->edit_mesh = nullptr;
4234
4235 return base_new;
4236}
4237
4239 Scene *scene,
4240 ViewLayer *view_layer,
4241 Base *base_old,
4242 BMesh *bm_old,
4243 BMVert **verts,
4244 uint verts_len,
4245 BMEdge **edges,
4246 uint edges_len,
4247 BMFace **faces,
4248 uint faces_len)
4249{
4250 BMAllocTemplate bm_new_allocsize{};
4251 bm_new_allocsize.totvert = verts_len;
4252 bm_new_allocsize.totedge = edges_len;
4253 bm_new_allocsize.totloop = faces_len * 3;
4254 bm_new_allocsize.totface = faces_len;
4255
4256 const bool use_custom_normals = (bm_old->lnor_spacearr != nullptr);
4257
4258 Object *obedit = base_old->object;
4259
4260 BMeshCreateParams create_params{};
4261 BMesh *bm_new = BM_mesh_create(&bm_new_allocsize, &create_params);
4262
4263 if (use_custom_normals) {
4264 /* Needed so the temporary normal layer is copied too. */
4265 BM_mesh_copy_init_customdata_all_layers(bm_new, bm_old, BM_ALL, &bm_new_allocsize);
4266 }
4267 else {
4268 BM_mesh_copy_init_customdata(bm_new, bm_old, &bm_new_allocsize);
4269 }
4270
4271 /* Take into account user preferences for duplicating actions. */
4272 const eDupli_ID_Flags dupflag = eDupli_ID_Flags(USER_DUP_MESH | (U.dupflag & USER_DUP_ACT));
4273 Base *base_new = ed::object::add_duplicate(bmain, scene, view_layer, base_old, dupflag);
4274
4275 /* normally would call directly after but in this case delay recalc */
4276 // DAG_relations_tag_update(bmain);
4277
4278 /* new in 2.5 */
4280 base_new->object,
4283 false);
4284
4286
4287 BM_mesh_copy_arrays(bm_old, bm_new, verts, verts_len, edges, edges_len, faces, faces_len);
4288
4289 if (use_custom_normals) {
4291 }
4292
4293 for (uint i = 0; i < verts_len; i++) {
4294 BM_vert_kill(bm_old, verts[i]);
4295 }
4296 BMeshToMeshParams to_mesh_params{};
4297 BM_mesh_bm_to_me(bmain, bm_new, id_cast<Mesh *>(base_new->object->data), &to_mesh_params);
4298
4299 BM_mesh_free(bm_new);
4300 (id_cast<Mesh *>(base_new->object->data))->runtime->edit_mesh = nullptr;
4301
4302 return base_new;
4303}
4304
4306 Main *bmain, Scene *scene, ViewLayer *view_layer, Base *base_old, BMesh *bm_old)
4307{
4309
4310 /* we may have tags from previous operators */
4312
4313 /* sel -> tag */
4315 bm_old, BM_FACE | BM_EDGE | BM_VERT, BM_ELEM_TAG, true, false, BM_ELEM_SELECT);
4316
4317 Base *base_new = mesh_separate_tagged(bmain, scene, view_layer, base_old, bm_old);
4318
4320
4321 return (base_new != nullptr);
4322}
4323
4330static void mesh_separate_material_assign_mat_nr(Main *bmain, Object *ob, const short mat_nr)
4331{
4332 ID *obdata = ob->data;
4333
4334 const short *totcolp = BKE_id_material_len_p(obdata);
4335 Material ***matarar = BKE_id_material_array_p(obdata);
4336
4337 if ((totcolp && matarar) == 0) {
4338 BLI_assert(0);
4339 return;
4340 }
4341
4342 if (*totcolp) {
4343 Material *ma_ob;
4344 Material *ma_obdata;
4345 char matbit;
4346
4347 if (mat_nr < ob->totcol) {
4348 ma_ob = ob->mat[mat_nr];
4349 matbit = ob->matbits[mat_nr];
4350 }
4351 else {
4352 ma_ob = nullptr;
4353 matbit = 0;
4354 }
4355
4356 if (mat_nr < *totcolp) {
4357 ma_obdata = (*matarar)[mat_nr];
4358 }
4359 else {
4360 ma_obdata = nullptr;
4361 }
4362
4363 BKE_id_material_clear(bmain, obdata);
4364 BKE_id_material_resize(bmain, obdata, 1, true);
4366
4367 ob->mat[0] = ma_ob;
4368 id_us_plus(id_cast<ID *>(ma_ob));
4369 ob->matbits[0] = matbit;
4370 (*matarar)[0] = ma_obdata;
4371 id_us_plus(id_cast<ID *>(ma_obdata));
4372 }
4373 else {
4374 BKE_id_material_clear(bmain, obdata);
4375 }
4376}
4377
4379 Main *bmain, Scene *scene, ViewLayer *view_layer, Base *base_old, BMesh *bm_old)
4380{
4381 BMFace *f_cmp, *f;
4382 BMIter iter;
4383 bool result = false;
4384
4386
4387 while ((f_cmp = static_cast<BMFace *>(BM_iter_at_index(bm_old, BM_FACES_OF_MESH, nullptr, 0)))) {
4388 Base *base_new;
4389 const short mat_nr = f_cmp->mat_nr;
4390 int tot = 0;
4391
4393
4394 BM_ITER_MESH (f, &iter, bm_old, BM_FACES_OF_MESH) {
4395 if (f->mat_nr == mat_nr) {
4396 BMLoop *l_iter;
4397 BMLoop *l_first;
4398
4400 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
4401 do {
4404 } while ((l_iter = l_iter->next) != l_first);
4405
4406 tot++;
4407 }
4408 }
4409
4410 /* leave the current object with some materials */
4411 if (tot == bm_old->totface) {
4412 mesh_separate_material_assign_mat_nr(bmain, base_old->object, mat_nr);
4413
4414 /* since we're in editmode, must set faces here */
4415 BM_ITER_MESH (f, &iter, bm_old, BM_FACES_OF_MESH) {
4416 f->mat_nr = 0;
4417 }
4418 break;
4419 }
4420
4421 /* Move selection into a separate object */
4422 base_new = mesh_separate_tagged(bmain, scene, view_layer, base_old, bm_old);
4423 if (base_new) {
4424 mesh_separate_material_assign_mat_nr(bmain, base_new->object, mat_nr);
4425 }
4426
4427 result |= (base_new != nullptr);
4428 }
4429
4431
4432 return result;
4433}
4434
4436 Main *bmain, Scene *scene, ViewLayer *view_layer, Base *base_old, BMesh *bm_old)
4437{
4438 /* Without this, we duplicate the object mode mesh for each loose part.
4439 * This can get very slow especially for large meshes with many parts
4440 * which would duplicate the mesh on entering edit-mode. */
4441 const bool clear_object_data = true;
4442
4443 bool result = false;
4444
4445 Array<BMVert *> vert_groups(bm_old->totvert);
4446 Array<BMEdge *> edge_groups(bm_old->totedge);
4447 Array<BMFace *> face_groups(bm_old->totface);
4448
4449 int (*groups)[3] = nullptr;
4450 int groups_len = BM_mesh_calc_edge_groups_as_arrays(
4451 bm_old, vert_groups.data(), edge_groups.data(), face_groups.data(), &groups);
4452 if (groups_len <= 1) {
4453 MEM_SAFE_DELETE(groups);
4454 return false;
4455 }
4456
4457 if (clear_object_data) {
4458 ED_mesh_geometry_clear(id_cast<Mesh *>(base_old->object->data));
4459 }
4460
4462
4463 /* Separate out all groups except the first. */
4464 uint group_ofs[3] = {uint(groups[0][0]), uint(groups[0][1]), uint(groups[0][2])};
4465 for (int i = 1; i < groups_len; i++) {
4466 Base *base_new = mesh_separate_arrays(bmain,
4467 scene,
4468 view_layer,
4469 base_old,
4470 bm_old,
4471 vert_groups.data() + group_ofs[0],
4472 groups[i][0],
4473 edge_groups.data() + group_ofs[1],
4474 groups[i][1],
4475 face_groups.data() + group_ofs[2],
4476 groups[i][2]);
4477 result |= (base_new != nullptr);
4478
4479 group_ofs[0] += groups[i][0];
4480 group_ofs[1] += groups[i][1];
4481 group_ofs[2] += groups[i][2];
4482 }
4483
4484 Mesh *me_old = id_cast<Mesh *>(base_old->object->data);
4486
4487 if (clear_object_data) {
4488 BMeshToMeshParams to_mesh_params{};
4489 to_mesh_params.update_shapekey_indices = true;
4490 BM_mesh_bm_to_me(nullptr, bm_old, me_old, &to_mesh_params);
4491 }
4492
4493 MEM_delete(groups);
4494 return result;
4495}
4496
4498{
4499 Main *bmain = CTX_data_main(C);
4500 Scene *scene = CTX_data_scene(C);
4501 ViewLayer *view_layer = CTX_data_view_layer(C);
4502 const int type = RNA_enum_get(op->ptr, "type");
4503 bool changed_multi = false;
4504
4505 if (ED_operator_editmesh(C)) {
4506 uint empty_selection_len = 0;
4508 *bmain, scene, view_layer, CTX_wm_view3d(C));
4509 for (const int base_index : bases.index_range()) {
4510 Base *base = bases[base_index];
4512
4513 if (type == 0) {
4514 if ((bm->totvertsel == 0) && (bm->totedgesel == 0) && (bm->totfacesel == 0)) {
4515 /* when all objects has no selection */
4516 if (++empty_selection_len == bases.size()) {
4517 BKE_report(op->reports, RPT_ERROR, "Nothing selected");
4518 }
4519 continue;
4520 }
4521 }
4522
4523 /* editmode separate */
4524 bool changed = false;
4525 switch (type) {
4527 changed = mesh_separate_selected(bmain, scene, view_layer, base, bm);
4528 break;
4530 changed = mesh_separate_material(bmain, scene, view_layer, base, bm);
4531 break;
4533 changed = mesh_separate_loose(bmain, scene, view_layer, base, bm);
4534 break;
4535 default:
4536 BLI_assert(0);
4537 break;
4538 }
4539
4540 if (changed) {
4541 EDBMUpdate_Params params{};
4542 params.calc_looptris = true;
4543 params.calc_normals = false;
4544 params.is_destructive = true;
4545 EDBM_update(id_cast<Mesh *>(base->object->data), &params);
4546 }
4547 changed_multi |= changed;
4548 }
4549 }
4550 else {
4551 if (type == MESH_SEPARATE_SELECTED) {
4552 BKE_report(op->reports, RPT_ERROR, "Selection not supported in object mode");
4553 return OPERATOR_CANCELLED;
4554 }
4555
4556 /* object mode separate */
4557 CTX_DATA_BEGIN (C, Base *, base_iter, selected_editable_bases) {
4558 Object *ob = base_iter->object;
4559 if (ob->type != OB_MESH) {
4560 continue;
4561 }
4562 Mesh *mesh = id_cast<Mesh *>(ob->data);
4563 if (!BKE_id_is_editable(bmain, &mesh->id)) {
4564 continue;
4565 }
4566
4567 BMeshCreateParams create_params{};
4568 create_params.use_toolflags = true;
4569 BMesh *bm_old = BM_mesh_create(&bm_mesh_allocsize_default, &create_params);
4570
4571 BMeshFromMeshParams from_mesh_params{};
4572 from_mesh_params.calc_face_normal = true;
4573 from_mesh_params.calc_vert_normal = true;
4574 BM_mesh_bm_from_me(bm_old, mesh, &from_mesh_params);
4575
4576 bool changed = false;
4577 switch (type) {
4579 changed = mesh_separate_material(bmain, scene, view_layer, base_iter, bm_old);
4580 break;
4582 changed = mesh_separate_loose(bmain, scene, view_layer, base_iter, bm_old);
4583 break;
4584 default:
4585 BLI_assert(0);
4586 break;
4587 }
4588
4589 if (changed) {
4590 BMeshToMeshParams to_mesh_params{};
4591 to_mesh_params.calc_object_remap = true;
4592 BM_mesh_bm_to_me(bmain, bm_old, mesh, &to_mesh_params);
4596 }
4597
4598 BM_mesh_free(bm_old);
4599
4600 changed_multi |= changed;
4601 }
4603 }
4604
4605 if (changed_multi) {
4606 /* delay depsgraph recalc until all objects are duplicated */
4610
4611 return OPERATOR_FINISHED;
4612 }
4613
4614 return OPERATOR_CANCELLED;
4615}
4616
4618{
4619 static const EnumPropertyItem prop_separate_types[] = {
4620 {MESH_SEPARATE_SELECTED, "SELECTED", 0, "Selection", ""},
4621 {MESH_SEPARATE_MATERIAL, "MATERIAL", 0, "By Material", ""},
4622 {MESH_SEPARATE_LOOSE, "LOOSE", 0, "By Loose Parts", ""},
4623 {0, nullptr, 0, nullptr, nullptr},
4624 };
4625
4626 /* identifiers */
4627 ot->name = "Separate";
4628 ot->description = "Separate selected geometry into a new mesh";
4629 ot->idname = "MESH_OT_separate";
4630
4631 /* API callbacks. */
4632 ot->invoke = WM_menu_invoke;
4634 ot->poll = ED_operator_scene_editable; /* object and editmode */
4635
4636 /* flags */
4637 ot->flag = OPTYPE_UNDO;
4638
4639 ot->prop = RNA_def_enum(
4640 ot->srna, "type", prop_separate_types, MESH_SEPARATE_SELECTED, "Type", "");
4641}
4642
4644
4645/* -------------------------------------------------------------------- */
4648
4650{
4651 const bool use_beauty = RNA_boolean_get(op->ptr, "use_beauty");
4652
4653 bool has_selected_edges = false, has_faces_filled = false;
4654
4655 const Main *bmain = CTX_data_main(C);
4656 const Scene *scene = CTX_data_scene(C);
4657 ViewLayer *view_layer = CTX_data_view_layer(C);
4659 *bmain, scene, view_layer, CTX_wm_view3d(C));
4660 for (Object *obedit : objects) {
4662
4663 const int totface_orig = bm->totface;
4664
4665 if (bm->totedgesel == 0) {
4666 continue;
4667 }
4668 has_selected_edges = true;
4669
4670 BMOperator bmop;
4671 if (!EDBM_op_init(
4672 bm, &bmop, op, "triangle_fill edges=%he use_beauty=%b", BM_ELEM_SELECT, use_beauty))
4673 {
4674 continue;
4675 }
4676
4677 BMO_op_exec(bm, &bmop);
4678
4679 /* cancel if nothing was done */
4680 if (totface_orig == bm->totface) {
4681 EDBM_op_finish(bm, &bmop, op, true);
4682 continue;
4683 }
4684 has_faces_filled = true;
4685
4686 /* select new geometry */
4688 bm, bmop.slots_out, "geom.out", BM_FACE | BM_EDGE, BM_ELEM_SELECT, true);
4689
4690 if (!EDBM_op_finish(bm, &bmop, op, true)) {
4691 continue;
4692 }
4693
4694 EDBMUpdate_Params params{};
4695 params.calc_looptris = true;
4696 params.calc_normals = false;
4697 params.is_destructive = true;
4698 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
4699 }
4700
4701 if (!has_selected_edges) {
4702 BKE_report(op->reports, RPT_ERROR, "No edges selected");
4703 return OPERATOR_CANCELLED;
4704 }
4705
4706 if (!has_faces_filled) {
4707 BKE_report(op->reports, RPT_WARNING, "No faces filled");
4708 return OPERATOR_CANCELLED;
4709 }
4710
4711 return OPERATOR_FINISHED;
4712}
4713
4715{
4716 /* identifiers */
4717 ot->name = "Fill";
4718 ot->idname = "MESH_OT_fill";
4719 ot->description = "Fill a selected edge loop with faces";
4721
4722 /* API callbacks. */
4723 ot->exec = edbm_fill_exec;
4725
4726 /* flags */
4728
4729 RNA_def_boolean(ot->srna, "use_beauty", true, "Beauty", "Use best triangulation division");
4730}
4731
4733
4734/* -------------------------------------------------------------------- */
4737
4739{
4740 BMIter iter;
4741 BMEdge *e_iter;
4742 BMVert *v_pair[2];
4743 int i = 0;
4744 BM_ITER_ELEM (e_iter, &iter, v, BM_EDGES_OF_VERT) {
4745 if (BM_elem_flag_test(e_iter, BM_ELEM_TAG)) {
4746 v_pair[i++] = BM_edge_other_vert(e_iter, v);
4747 }
4748 }
4749 BLI_assert(i == 2);
4750
4751 return fabsf(float(M_PI) - angle_v3v3v3(v_pair[0]->co, v->co, v_pair[1]->co));
4752}
4753
4757static bool edbm_fill_grid_prepare(BMesh *bm, int offset, int *span_p, const bool span_calc)
4758{
4759 /* angle differences below this value are considered 'even'
4760 * in that they shouldn't be used to calculate corners used for the 'span' */
4761 const float eps_even = 1e-3f;
4762 BMEdge *e;
4763 BMIter iter;
4764 int count;
4765 int span = *span_p;
4766
4767 ListBaseT<BMEdgeLoopStore> eloops = {nullptr};
4768 BMEdgeLoopStore *el_store;
4769 // LinkData *el_store;
4770
4771 count = BM_mesh_edgeloops_find(
4772 bm, &eloops, [](BMEdge *e) { return BM_elem_flag_test_bool(e, BM_ELEM_SELECT); });
4773 el_store = eloops.first();
4774
4775 if (count != 1) {
4776 /* Let the operator use the selection flags,
4777 * most likely failing with an error in this case. */
4778 BM_mesh_edgeloops_free(&eloops);
4779 return false;
4780 }
4781
4782 /* Only tag edges that are part of a loop. */
4783 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
4785 }
4786 const int verts_len = BM_edgeloop_length_get(el_store);
4787 const int edges_len = verts_len - (BM_edgeloop_is_closed(el_store) ? 0 : 1);
4788 BMEdge **edges = MEM_new_array_uninitialized<BMEdge *>(edges_len, __func__);
4789 BM_edgeloop_edges_get(el_store, edges);
4790 for (int i = 0; i < edges_len; i++) {
4792 }
4793
4794 if (span_calc) {
4795 span = verts_len / 4;
4796 }
4797 else {
4798 span = min_ii(span, (verts_len / 2) - 1);
4799 }
4800 offset = mod_i(offset, verts_len);
4801
4802 if ((count == 1) && ((verts_len & 1) == 0) && (verts_len == edges_len)) {
4803
4804 /* be clever! detect 2 edge loops from one closed edge loop */
4805 ListBaseT<LinkData> *verts = BM_edgeloop_verts_get(el_store);
4807 LinkData *v_act_link;
4808 int i;
4809
4810 if (v_act && (v_act_link = static_cast<LinkData *>(
4811 BLI_findptr(verts, v_act, offsetof(LinkData, data)))))
4812 {
4813 /* pass */
4814 }
4815 else {
4816 /* find the vertex with the best angle (a corner vertex) */
4817 LinkData *v_link_best = nullptr;
4818 float angle_best = -1.0f;
4819 for (LinkData &v_link : *verts) {
4820 const float angle = edbm_fill_grid_vert_tag_angle(static_cast<BMVert *>(v_link.data));
4821 if ((angle > angle_best) || (v_link_best == nullptr)) {
4822 angle_best = angle;
4823 v_link_best = &v_link;
4824 }
4825 }
4826
4827 v_act_link = v_link_best;
4828 v_act = static_cast<BMVert *>(v_act_link->data);
4829 }
4830
4831 /* set this vertex first */
4832 BLI_listbase_rotate_first(verts, v_act_link);
4833
4834 if (offset != 0) {
4835 v_act_link = static_cast<LinkData *>(BLI_findlink(verts, offset));
4836 v_act = static_cast<BMVert *>(v_act_link->data);
4837 BLI_listbase_rotate_first(verts, v_act_link);
4838 }
4839
4840 /* Run again to update the edge order from the rotated vertex list. */
4841 BM_edgeloop_edges_get(el_store, edges);
4842
4843 if (span_calc) {
4844 /* calculate the span by finding the next corner in 'verts'
4845 * we don't know what defines a corner exactly so find the 4 verts
4846 * in the loop with the greatest angle.
4847 * Tag them and use the first tagged vertex to calculate the span.
4848 *
4849 * NOTE: we may have already checked 'edbm_fill_grid_vert_tag_angle()' on each
4850 * vert, but advantage of de-duplicating is minimal. */
4851 SortPtrByFloat *ele_sort = MEM_new_array_uninitialized<SortPtrByFloat>(verts_len, __func__);
4852 LinkData *v_link;
4853 for (v_link = verts->first(), i = 0; v_link; v_link = v_link->next, i++) {
4854 BMVert *v = static_cast<BMVert *>(v_link->data);
4855 const float angle = edbm_fill_grid_vert_tag_angle(v);
4856 ele_sort[i].sort_value = angle;
4857 ele_sort[i].data = v_link;
4858
4859 /* Do not allow the best corner or the diagonally opposite corner to be detected. */
4860 if (ELEM(i, 0, verts_len / 2)) {
4861 ele_sort[i].sort_value = 0;
4862 }
4863 }
4864
4865 qsort(ele_sort, verts_len, sizeof(*ele_sort), BLI_sortutil_cmp_float_reverse);
4866
4867 /* Check that we have at least 3 corners.
4868 * The excluded corners are the last and second from last elements (both reset to 0).
4869 * The best remaining corner is `ele_sort[0]`
4870 * if the angle on the best remaining corner is roughly the same as the third-last,
4871 * then we can't calculate 3+ corners - fallback to the even span. */
4872 if ((ele_sort[0].sort_value - ele_sort[verts_len - 3].sort_value) > eps_even) {
4873 span = BLI_findindex(verts, ele_sort[0].data);
4874 }
4875 MEM_delete(ele_sort);
4876 }
4877 /* end span calc */
4878 int start = 0;
4879
4880 /* The algorithm needs to iterate the shorter distance, between the best and second best vert.
4881 * If the second best vert is near the beginning of the loop, it starts at 0 and walks forward.
4882 * If, instead, the second best vert is near the end of the loop, then it starts at the second
4883 * best vertex and walks to the end of the loop. */
4884 if (span > verts_len / 2) {
4885 span = (verts_len)-span;
4886 start = (verts_len / 2) - span;
4887 }
4888
4889 /* un-flag 'rails' */
4890 for (i = start; i < start + span; i++) {
4892 BM_elem_flag_disable(edges[(verts_len / 2) + i], BM_ELEM_TAG);
4893 }
4894 }
4895 /* else let the bmesh-operator handle it */
4896
4897 BM_mesh_edgeloops_free(&eloops);
4898 MEM_delete(edges);
4899
4900 *span_p = span;
4901
4902 return true;
4903}
4904
4909
4921{
4923 BMIter iter;
4924 BMFace *f;
4925 BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
4928 BMIter liter;
4929 BMLoop *l;
4930 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
4933 }
4934 }
4935 else {
4937 }
4938 }
4939
4941 blender::Array<uint8_t> vert_boundary_edge_counts(bm->totvert, 0);
4942 int boundary_edges_count = 0;
4943 BMEdge *e;
4944 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
4945 int faces_tagged = 0;
4946 BMIter fiter;
4947 BMFace *f;
4948 BM_ITER_ELEM (f, &fiter, e, BM_FACES_OF_EDGE) {
4950 if (++faces_tagged == 2) {
4951 break;
4952 }
4953 }
4954 }
4955 if (faces_tagged == 1) {
4956 boundary_edges_count++;
4957 for (int i = 0; i < 2; i++) {
4958 const BMVert *v = *((&e->v1) + i);
4959 /* There is no need to check for a count of 1 since we only evaluate
4960 * edges from face-boundaries the final count will never be one. */
4961 if (++vert_boundary_edge_counts[BM_elem_index_get(v)] == 3) {
4962 return false;
4963 }
4964 }
4965 }
4966 }
4967
4968 /* No boundary means the faces form a closed region with nothing to grid fill. */
4969 return boundary_edges_count != 0;
4970}
4971
4985{
4986 /* Convert selection to tags, so `split` doesn't run on disconnected geometry.
4987 * The only user of the tag is the call to #BMO_slot_buffer_from_enabled_hflag below. */
4989 return nullptr;
4990 }
4991
4993
4994 /* Split the tagged faces into an island. */
4995 BMOperator split_op;
4996 BMO_op_init(bm, &split_op, 0, "split");
4998 bm, &split_op, split_op.slots_in, "geom", BM_FACE | BM_EDGE | BM_VERT, BM_ELEM_TAG);
4999 BMO_op_exec(bm, &split_op);
5000
5001 /* Setup the weld op that will undo the split.
5002 * Switch the selection to the corresponding edges on the island instead of the edges around the
5003 * hole, so fill_grid will interpolate using the face and loop data from the island.
5004 * Also create a new map for the weld, which maps pairs of verts instead of pairs of edges. */
5005 BMO_op_init(bm, &split_join->weld_op, 0, "weld_verts");
5006 BMOpSlot *weld_target_map = BMO_slot_get(split_join->weld_op.slots_in, "targetmap");
5007 BMOIter siter;
5008 BMEdge *e;
5009 BMO_ITER (e, &siter, split_op.slots_out, "boundary_map.out", 0) {
5010 BMEdge *e_dst = static_cast<BMEdge *>(BMO_iter_map_value_ptr(&siter));
5011
5012 BLI_assert(e_dst);
5013
5014 /* For edges, flip the selection from the edge of the hole to the edge of the island. */
5016
5017 /* When these match, the source edge has been deleted. */
5018 if (e != e_dst) {
5020
5021 /* For verts, flip the selection from the edge of the hole to the edge of the island.
5022 * Also add it to the weld map. But check selection first. Don't try to add the same vert to
5023 * the map more than once. If the selection was changed false, it's already been processed.
5024 */
5028 BMO_slot_map_elem_insert(&split_join->weld_op, weld_target_map, e->v1, e_dst->v1);
5029 }
5033 BMO_slot_map_elem_insert(&split_join->weld_op, weld_target_map, e->v2, e_dst->v2);
5034 }
5035 }
5036 }
5037
5038 /* Store the island for removal once it has been replaced by new fill_grid geometry. */
5039 BMO_op_init(bm, &split_join->delete_op, 0, "delete");
5040 BMO_slot_int_set(split_join->delete_op.slots_in, "context", DEL_FACES);
5041 BMO_slot_buffer_hflag_enable(bm, split_op.slots_out, "geom.out", BM_FACE, BM_ELEM_SELECT, false);
5043 bm, &split_join->delete_op, split_join->delete_op.slots_in, "geom", BM_FACE, BM_ELEM_SELECT);
5044
5045 /* Clean up the split operator. */
5046 BMO_op_finish(bm, &split_op);
5047
5048 return split_join;
5049}
5050
5060 wmOperator *op,
5061 FillGridSplitJoin *split_join,
5062 bool changed)
5063{
5064
5065 /* If fill_grid worked, delete the replaced faces. Otherwise, restore original selection. */
5066 if (changed) {
5067 BMO_op_exec(bm, &split_join->delete_op);
5068 }
5069 else {
5071 bm, split_join->delete_op.slots_in, "geom", BM_ALL_NOLOOP, BM_ELEM_SELECT, true);
5072 }
5073 BMO_op_finish(bm, &split_join->delete_op);
5074
5075 /* If fill_grid created geometry from faces after those faces had been split
5076 * from the rest of the mesh, the geometry it generated will be inward-facing.
5077 * (using the fill_grid on an island instead of a hole is 'inside out'.) Fix it.
5078 * This is the same as #edbm_flip_normals_face_winding without the #EDBM_update
5079 * since that will happen later. */
5080 if (changed) {
5082 EDBM_op_callf(bm, op, "reverse_faces faces=%hf flip_multires=%b", BM_ELEM_SELECT, true);
5083 if (lnors_ed_arr) {
5084 flip_custom_normals(bm, lnors_ed_arr);
5086 }
5087 }
5088
5089 /* Put the mesh back together. */
5090 BMO_op_exec(bm, &split_join->weld_op);
5091 BMO_op_finish(bm, &split_join->weld_op);
5092
5093 MEM_delete(split_join);
5094}
5095
5097{
5098 const bool use_interp_simple = RNA_boolean_get(op->ptr, "use_interp_simple");
5099
5100 const Main *bmain = CTX_data_main(C);
5101 const Scene *scene = CTX_data_scene(C);
5102 ViewLayer *view_layer = CTX_data_view_layer(C);
5104 *bmain, scene, view_layer, CTX_wm_view3d(C));
5105 for (uint ob_index = 0; ob_index < objects.size(); ob_index++) {
5106
5107 Object *obedit = objects[ob_index];
5110 if (bm->totedgesel == 0) {
5111 continue;
5112 }
5113
5114 bool use_prepare = true;
5115 const bool use_smooth = edbm_add_edge_face__smooth_get(bm);
5116
5117 FillGridSplitJoin *split_join = nullptr;
5118 if (bm->totfacesel != 0) {
5119 split_join = edbm_fill_grid_split_join_init(bm);
5120 if (split_join == nullptr) {
5121 /* The selected faces don't have a clean boundary to grid fill from, skip this object
5122 * rather than falling through to a grid fill that would run over the existing faces. */
5123 continue;
5124 }
5125 }
5126
5127 const int totedge_orig = bm->totedge;
5128 const int totface_orig = bm->totface;
5129
5130 if (use_prepare) {
5131 /* use when we have a single loop selected */
5132 PropertyRNA *prop_span = RNA_struct_find_property(op->ptr, "span");
5133 PropertyRNA *prop_offset = RNA_struct_find_property(op->ptr, "offset");
5134 bool calc_span;
5135
5136 int span;
5137 int offset;
5138
5139 /* Only reuse on redo because these settings need to match the current selection.
5140 * We never want to use them on other geometry, repeat last for eg, see: #60777. */
5141 if (((op->flag & OP_IS_REPEAT_LAST) == 0) && RNA_property_is_set(op->ptr, prop_span)) {
5142 span = RNA_property_int_get(op->ptr, prop_span);
5143 calc_span = false;
5144 }
5145 else {
5146 /* Will be overwritten if possible. */
5147 span = 0;
5148 calc_span = true;
5149 }
5150
5151 offset = RNA_property_int_get(op->ptr, prop_offset);
5152
5153 /* in simple cases, move selection for tags, but also support more advanced cases */
5154 use_prepare = edbm_fill_grid_prepare(bm, offset, &span, calc_span);
5155
5156 RNA_property_int_set(op->ptr, prop_span, span);
5157 }
5158 /* end tricky prepare code */
5159
5160 bool changed = EDBM_op_call_and_selectf(
5161 bm,
5162 op,
5163 "faces.out",
5164 true,
5165 "grid_fill edges=%he mat_nr=%i use_smooth=%b use_interp_simple=%b",
5166 use_prepare ? BM_ELEM_TAG : BM_ELEM_SELECT,
5167 em->mat_nr,
5168 use_smooth,
5169 use_interp_simple);
5170
5171 /* Check that the results match the return value. */
5172 const bool has_geometry_changed = totedge_orig != bm->totedge || totface_orig != bm->totface;
5173 BLI_assert(changed == has_geometry_changed);
5174 UNUSED_VARS_NDEBUG(has_geometry_changed);
5175
5176 /* If a split/join in progress, finish it. */
5177 if (split_join) {
5178 edbm_fill_grid_split_join_finish(bm, op, split_join, changed);
5179 }
5180
5181 /* Update the object. */
5182 if (changed || split_join) {
5183 EDBMUpdate_Params params{};
5184 params.calc_looptris = true;
5185 params.calc_normals = false;
5186 params.is_destructive = true;
5187 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
5188 }
5189 }
5190
5191 return OPERATOR_FINISHED;
5192}
5193
5195{
5196 PropertyRNA *prop;
5197
5198 /* identifiers */
5199 ot->name = "Grid Fill";
5200 ot->description = "Fill grid from two loops";
5201 ot->idname = "MESH_OT_fill_grid";
5202
5203 /* API callbacks. */
5206
5207 /* flags */
5209
5210 /* properties */
5211 prop = RNA_def_int(ot->srna, "span", 1, 1, 1000, "Span", "Number of grid columns", 1, 100);
5213 prop = RNA_def_int(ot->srna,
5214 "offset",
5215 0,
5216 -1000,
5217 1000,
5218 "Offset",
5219 "Vertex that is the corner of the grid",
5220 -100,
5221 100);
5224 "use_interp_simple",
5225 false,
5226 "Simple Blending",
5227 "Use simple interpolation of grid vertices");
5228}
5229
5231
5232/* -------------------------------------------------------------------- */
5235
5237{
5238 const int sides = RNA_int_get(op->ptr, "sides");
5239
5240 const Main *bmain = CTX_data_main(C);
5241 const Scene *scene = CTX_data_scene(C);
5242 ViewLayer *view_layer = CTX_data_view_layer(C);
5244 *bmain, scene, view_layer, CTX_wm_view3d(C));
5245
5246 for (Object *obedit : objects) {
5248
5249 if (bm->totedgesel == 0) {
5250 continue;
5251 }
5252
5254 bm, op, "faces.out", true, "holes_fill edges=%he sides=%i", BM_ELEM_SELECT, sides))
5255 {
5256 continue;
5257 }
5258
5259 EDBMUpdate_Params params{};
5260 params.calc_looptris = true;
5261 params.calc_normals = false;
5262 params.is_destructive = true;
5263 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
5264 }
5265
5266 return OPERATOR_FINISHED;
5267}
5268
5270{
5271 /* identifiers */
5272 ot->name = "Fill Holes";
5273 ot->idname = "MESH_OT_fill_holes";
5274 ot->description = "Fill in holes (boundary edge loops)";
5275
5276 /* API callbacks. */
5279
5280 /* flags */
5282
5283 RNA_def_int(ot->srna,
5284 "sides",
5285 4,
5286 0,
5287 1000,
5288 "Sides",
5289 "Number of sides in hole required to fill (zero fills all holes)",
5290 0,
5291 100);
5292}
5293
5295
5296/* -------------------------------------------------------------------- */
5299
5301{
5302 const Main *bmain = CTX_data_main(C);
5303 const Scene *scene = CTX_data_scene(C);
5304 ViewLayer *view_layer = CTX_data_view_layer(C);
5306 *bmain, scene, view_layer, CTX_wm_view3d(C));
5307
5308 const float angle_max = M_PI;
5309 const float angle_limit = RNA_float_get(op->ptr, "angle_limit");
5310 char hflag;
5311
5312 for (Object *obedit : objects) {
5314
5315 if (bm->totfacesel == 0) {
5316 continue;
5317 }
5318
5319 if (angle_limit >= angle_max) {
5320 hflag = BM_ELEM_SELECT;
5321 }
5322 else {
5323 BMIter iter;
5324 BMEdge *e;
5325
5326 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
5330 BM_edge_calc_face_angle_ex(e, angle_max) < angle_limit));
5331 }
5332 hflag = BM_ELEM_TAG;
5333 }
5334
5336 bm, op, "geom.out", true, "beautify_fill faces=%hf edges=%he", BM_ELEM_SELECT, hflag))
5337 {
5338 continue;
5339 }
5340
5341 EDBMUpdate_Params params{};
5342 params.calc_looptris = true;
5343 params.calc_normals = false;
5344 params.is_destructive = true;
5345 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
5346 }
5347
5348 return OPERATOR_FINISHED;
5349}
5350
5352{
5353 PropertyRNA *prop;
5354
5355 /* identifiers */
5356 ot->name = "Beautify Faces";
5357 ot->idname = "MESH_OT_beautify_fill";
5358 ot->description = "Rearrange some faces to try to get less degenerated geometry";
5359
5360 /* API callbacks. */
5363
5364 /* flags */
5366
5367 /* props */
5368 prop = RNA_def_float_rotation(ot->srna,
5369 "angle_limit",
5370 0,
5371 nullptr,
5372 0.0f,
5373 DEG2RADF(180.0f),
5374 "Max Angle",
5375 "Angle limit",
5376 0.0f,
5377 DEG2RADF(180.0f));
5379}
5380
5382
5383/* -------------------------------------------------------------------- */
5386
5388{
5389 const float offset = RNA_float_get(op->ptr, "offset");
5390 const bool use_relative_offset = RNA_boolean_get(op->ptr, "use_relative_offset");
5391 const int center_mode = RNA_enum_get(op->ptr, "center_mode");
5392
5393 const Main *bmain = CTX_data_main(C);
5394 const Scene *scene = CTX_data_scene(C);
5395 ViewLayer *view_layer = CTX_data_view_layer(C);
5397 *bmain, scene, view_layer, CTX_wm_view3d(C));
5398 for (Object *obedit : objects) {
5400
5401 if (bm->totfacesel == 0) {
5402 continue;
5403 }
5404
5405 BMOperator bmop;
5407 &bmop,
5408 op,
5409 "poke faces=%hf offset=%f use_relative_offset=%b center_mode=%i",
5411 offset,
5412 use_relative_offset,
5413 center_mode);
5414 BMO_op_exec(bm, &bmop);
5415
5417
5420
5421 if (!EDBM_op_finish(bm, &bmop, op, true)) {
5422 continue;
5423 }
5424
5425 EDBMUpdate_Params params{};
5426 params.calc_looptris = true;
5427 params.calc_normals = true;
5428 params.is_destructive = true;
5429 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
5430 }
5431
5432 return OPERATOR_FINISHED;
5433}
5434
5436{
5437 static const EnumPropertyItem poke_center_modes[] = {
5439 "MEDIAN_WEIGHTED",
5440 0,
5441 "Weighted Median",
5442 "Weighted median face center"},
5443 {BMOP_POKE_MEDIAN, "MEDIAN", 0, "Median", "Median face center"},
5444 {BMOP_POKE_BOUNDS, "BOUNDS", 0, "Bounds", "Face bounds center"},
5445 {0, nullptr, 0, nullptr, nullptr},
5446 };
5447
5448 /* identifiers */
5449 ot->name = "Poke Faces";
5450 ot->idname = "MESH_OT_poke";
5451 ot->description = "Split a face into a fan";
5452
5453 /* API callbacks. */
5456
5457 /* flags */
5459
5461 ot->srna, "offset", 0.0f, -1e3f, 1e3f, "Poke Offset", "Poke Offset", -10.0f, 10.0f);
5463 "use_relative_offset",
5464 false,
5465 "Offset Relative",
5466 "Scale the offset by surrounding geometry");
5467 RNA_def_enum(ot->srna,
5468 "center_mode",
5469 poke_center_modes,
5471 "Poke Center",
5472 "Poke face center calculation");
5473}
5474
5476
5477/* -------------------------------------------------------------------- */
5480
5482{
5483 const int quad_method = RNA_enum_get(op->ptr, "quad_method");
5484 const int ngon_method = RNA_enum_get(op->ptr, "ngon_method");
5485 const Main *bmain = CTX_data_main(C);
5486 const Scene *scene = CTX_data_scene(C);
5487 ViewLayer *view_layer = CTX_data_view_layer(C);
5488
5490 *bmain, scene, view_layer, CTX_wm_view3d(C));
5491 for (Object *obedit : objects) {
5494
5495 if (bm->totfacesel == 0) {
5496 continue;
5497 }
5498
5499 BMOperator bmop;
5500 BMOIter oiter;
5501 BMFace *f;
5502
5504
5506 &bmop,
5507 op,
5508 "triangulate faces=%hf quad_method=%i ngon_method=%i",
5510 quad_method,
5511 ngon_method);
5512 BMO_op_exec(bm, &bmop);
5513
5514 /* select the output */
5516
5517 /* remove the doubles */
5518 BMO_ITER (f, &oiter, bmop.slots_out, "face_map_double.out", BM_FACE) {
5519 BM_face_kill(bm, f);
5520 }
5521
5523
5524 if (!EDBM_op_finish(bm, &bmop, op, true)) {
5525 continue;
5526 }
5527
5529
5530 EDBMUpdate_Params params{};
5531 params.calc_looptris = true;
5532 params.calc_normals = false;
5533 params.is_destructive = true;
5534 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
5535 }
5536
5537 return OPERATOR_FINISHED;
5538}
5539
5541{
5542 /* identifiers */
5543 ot->name = "Triangulate Faces";
5544 ot->idname = "MESH_OT_quads_convert_to_tris";
5545 ot->description = "Triangulate selected faces";
5546
5547 /* API callbacks. */
5550
5551 /* flags */
5553
5554 RNA_def_enum(ot->srna,
5555 "quad_method",
5558 "Quad Method",
5559 "Method for splitting the quads into triangles");
5560 RNA_def_enum(ot->srna,
5561 "ngon_method",
5564 "N-gon Method",
5565 "Method for splitting the n-gons into triangles");
5566}
5567
5569
5570/* -------------------------------------------------------------------- */
5573
5574#if 0 /* See comments at top of bmo_join_triangles.cc */
5575# define USE_JOIN_TRIANGLE_TESTING_API
5576#endif
5577
5579{
5580 const Main *bmain = CTX_data_main(C);
5581 const Scene *scene = CTX_data_scene(C);
5582 ViewLayer *view_layer = CTX_data_view_layer(C);
5583
5585 *bmain, scene, view_layer, CTX_wm_view3d(C));
5586
5587 const bool do_seam = RNA_boolean_get(op->ptr, "seam");
5588 const bool do_sharp = RNA_boolean_get(op->ptr, "sharp");
5589 const bool do_uvs = RNA_boolean_get(op->ptr, "uvs");
5590 const bool do_vcols = RNA_boolean_get(op->ptr, "vcols");
5591 const bool do_materials = RNA_boolean_get(op->ptr, "materials");
5592
5593#ifdef USE_JOIN_TRIANGLE_TESTING_API
5594 int merge_limit = RNA_int_get(op->ptr, "merge_limit");
5595 int neighbor_debug = RNA_int_get(op->ptr, "neighbor_debug");
5596#endif
5597
5598 const float topology_influence = RNA_float_get(op->ptr, "topology_influence");
5599 const bool deselect_joined = RNA_boolean_get(op->ptr, "deselect_joined");
5600
5601 float angle_face_threshold, angle_shape_threshold;
5602 bool is_face_pair;
5603 {
5604 int totelem_sel[3];
5605 EDBM_mesh_stats_multi(objects, nullptr, totelem_sel);
5606 is_face_pair = (totelem_sel[2] == 2);
5607 }
5608
5609 /* When joining exactly 2 faces, no limit.
5610 * this is useful for one off joins while editing. */
5611 {
5612 PropertyRNA *prop;
5613 prop = RNA_struct_find_property(op->ptr, "face_threshold");
5614 if (is_face_pair && (RNA_property_is_set(op->ptr, prop) == false)) {
5615 angle_face_threshold = DEG2RADF(180.0f);
5616 }
5617 else {
5618 angle_face_threshold = RNA_property_float_get(op->ptr, prop);
5619 }
5620
5621 prop = RNA_struct_find_property(op->ptr, "shape_threshold");
5622 if (is_face_pair && (RNA_property_is_set(op->ptr, prop) == false)) {
5623 angle_shape_threshold = DEG2RADF(180.0f);
5624 }
5625 else {
5626 angle_shape_threshold = RNA_property_float_get(op->ptr, prop);
5627 }
5628 }
5629
5630 for (Object *obedit : objects) {
5633 if (bm->totfacesel == 0) {
5634 continue;
5635 }
5636
5638
5639 bool extend_selection = (deselect_joined == false);
5640
5641#ifdef USE_JOIN_TRIANGLE_TESTING_API
5642 if (merge_limit != -1) {
5643 extend_selection = false;
5644 }
5645#endif
5646
5648 bm,
5649 op,
5650 "faces.out",
5651 extend_selection,
5652 "join_triangles faces=%hf angle_face_threshold=%f angle_shape_threshold=%f "
5653 "cmp_seam=%b cmp_sharp=%b cmp_uvs=%b cmp_vcols=%b cmp_materials=%b "
5654#ifdef USE_JOIN_TRIANGLE_TESTING_API
5655 "merge_limit=%i neighbor_debug=%i "
5656#endif
5657 "topology_influence=%f deselect_joined=%b",
5659 angle_face_threshold,
5660 angle_shape_threshold,
5661 do_seam,
5662 do_sharp,
5663 do_uvs,
5664 do_vcols,
5665 do_materials,
5666#ifdef USE_JOIN_TRIANGLE_TESTING_API
5667 merge_limit + 1,
5668 neighbor_debug,
5669#endif
5670 topology_influence,
5671 deselect_joined))
5672 {
5673 continue;
5674 }
5675
5676 if (deselect_joined) {
5677 /* When de-selecting faces outside of face mode:
5678 * failing to flush would leave an invalid selection. */
5681 }
5682 }
5684
5686
5687 EDBMUpdate_Params params{};
5688 params.calc_looptris = true;
5689 params.calc_normals = false;
5690 params.is_destructive = true;
5691 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
5692 }
5693
5694 return OPERATOR_FINISHED;
5695}
5696
5698{
5699 PropertyRNA *prop;
5700
5701#ifdef USE_JOIN_TRIANGLE_TESTING_API
5702 prop = RNA_def_int(ot->srna,
5703 "merge_limit",
5704 0,
5705 -1,
5706 INT_MAX,
5707 "Merge Limit",
5708 "Maximum number of merges",
5709 -1,
5710 INT_MAX);
5712
5713 prop = RNA_def_int(ot->srna,
5714 "neighbor_debug",
5715 0,
5716 0,
5717 INT_MAX,
5718 "Neighbor Debug",
5719 "Neighbor to highlight",
5720 0,
5721 INT_MAX);
5723#endif
5724
5725 prop = RNA_def_float_rotation(ot->srna,
5726 "face_threshold",
5727 0,
5728 nullptr,
5729 0.0f,
5730 DEG2RADF(180.0f),
5731 "Max Face Angle",
5732 "Face angle limit",
5733 0.0f,
5734 DEG2RADF(180.0f));
5736
5737 prop = RNA_def_float_rotation(ot->srna,
5738 "shape_threshold",
5739 0,
5740 nullptr,
5741 0.0f,
5742 DEG2RADF(180.0f),
5743 "Max Shape Angle",
5744 "Shape angle limit",
5745 0.0f,
5746 DEG2RADF(180.0f));
5748
5749 prop = RNA_def_float_factor(ot->srna,
5750 "topology_influence",
5751 0.0f,
5752 0.0f,
5753 2.0f,
5754 "Topology Influence",
5755 "How much to prioritize regular grids of quads as well as "
5756 "quads that touch existing quads",
5757 0.0f,
5758 2.0f);
5759
5760 RNA_def_boolean(ot->srna, "uvs", false, "Compare UVs", "");
5761 RNA_def_boolean(ot->srna, "vcols", false, "Compare Color Attributes", "");
5762 RNA_def_boolean(ot->srna, "seam", false, "Compare Seam", "");
5763 RNA_def_boolean(ot->srna, "sharp", false, "Compare Sharp", "");
5764 RNA_def_boolean(ot->srna, "materials", false, "Compare Materials", "");
5765
5767 "deselect_joined",
5768 false,
5769 "Deselect Joined",
5770 "Only select remaining triangles that were not merged");
5771}
5772
5774{
5775 /* identifiers */
5776 ot->name = "Triangles to Quads";
5777 ot->idname = "MESH_OT_tris_convert_to_quads";
5778 ot->description = "Merge triangles into four sided polygons where possible";
5779
5780 /* API callbacks. */
5783
5784 /* flags */
5786
5788}
5789
5791
5792/* -------------------------------------------------------------------- */
5800
5802{
5803 const float ratio = RNA_float_get(op->ptr, "ratio");
5804 bool use_vertex_group = RNA_boolean_get(op->ptr, "use_vertex_group");
5805 const float vertex_group_factor = RNA_float_get(op->ptr, "vertex_group_factor");
5806 const bool invert_vertex_group = RNA_boolean_get(op->ptr, "invert_vertex_group");
5807 const bool use_symmetry = RNA_boolean_get(op->ptr, "use_symmetry");
5808 const float symmetry_eps = 0.00002f;
5809 const int symmetry_axis = use_symmetry ? RNA_enum_get(op->ptr, "symmetry_axis") : -1;
5810
5811 /* nop */
5812 if (ratio == 1.0f) {
5813 return OPERATOR_FINISHED;
5814 }
5815
5816 const Main *bmain = CTX_data_main(C);
5817 const Scene *scene = CTX_data_scene(C);
5818 ViewLayer *view_layer = CTX_data_view_layer(C);
5820 *bmain, scene, view_layer, CTX_wm_view3d(C));
5821
5822 for (Object *obedit : objects) {
5825 if (bm->totedgesel == 0) {
5826 continue;
5827 }
5828
5829 float *vweights = MEM_new_array_uninitialized<float>(bm->totvert, __func__);
5830 {
5831 const int cd_dvert_offset = CustomData_get_offset(&bm->vdata, CD_MDEFORMVERT);
5832 const int defbase_act = BKE_object_defgroup_active_index_get(obedit) - 1;
5833
5834 if (use_vertex_group && (cd_dvert_offset == -1)) {
5835 BKE_report(op->reports, RPT_WARNING, "No active vertex group");
5836 use_vertex_group = false;
5837 }
5838
5839 BMIter iter;
5840 BMVert *v;
5841 int i;
5842 BM_ITER_MESH_INDEX (v, &iter, bm, BM_VERTS_OF_MESH, i) {
5843 float weight = 0.0f;
5845 if (use_vertex_group) {
5846 const MDeformVert *dv = static_cast<const MDeformVert *>(
5847 BM_ELEM_CD_GET_VOID_P(v, cd_dvert_offset));
5848 weight = BKE_defvert_find_weight(dv, defbase_act);
5849 if (invert_vertex_group) {
5850 weight = 1.0f - weight;
5851 }
5852 }
5853 else {
5854 weight = 1.0f;
5855 }
5856 }
5857
5858 vweights[i] = weight;
5859 BM_elem_index_set(v, i); /* set_inline */
5860 }
5862 }
5863
5864 float ratio_adjust;
5865
5866 if ((bm->totface == bm->totfacesel) || (ratio == 0.0f)) {
5867 ratio_adjust = ratio;
5868 }
5869 else {
5877
5878 int totface_basis = 0;
5879 int totface_adjacent = 0;
5880 BMIter iter;
5881 BMFace *f;
5882 BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
5883 /* count faces during decimation, ngons are triangulated */
5884 const int f_len = f->len > 4 ? (f->len - 2) : 1;
5885 totface_basis += f_len;
5886
5887 BMLoop *l_iter, *l_first;
5888 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
5889 do {
5890 if (vweights[BM_elem_index_get(l_iter->v)] != 0.0f) {
5891 totface_adjacent += f_len;
5892 break;
5893 }
5894 } while ((l_iter = l_iter->next) != l_first);
5895 }
5896
5897 ratio_adjust = ratio;
5898 ratio_adjust = 1.0f - ratio_adjust;
5899 ratio_adjust *= float(totface_adjacent) / float(totface_basis);
5900 ratio_adjust = 1.0f - ratio_adjust;
5901 }
5902
5904 bm, ratio_adjust, vweights, vertex_group_factor, false, symmetry_axis, symmetry_eps);
5905
5906 MEM_delete(vweights);
5907
5908 {
5909 short selectmode = em->selectmode;
5910 if ((selectmode & (SCE_SELECT_VERTEX | SCE_SELECT_EDGE)) == 0) {
5911 /* ensure we flush edges -> faces */
5912 selectmode |= SCE_SELECT_EDGE;
5913 }
5914 EDBM_selectmode_flush_ex(bm, selectmode);
5916 }
5917 EDBMUpdate_Params params{};
5918 params.calc_looptris = true;
5919 params.calc_normals = true;
5920 params.is_destructive = true;
5921 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
5922 }
5923
5924 return OPERATOR_FINISHED;
5925}
5926
5927static bool edbm_decimate_check(bContext * /*C*/, wmOperator * /*op*/)
5928{
5929 return true;
5930}
5931
5932static void edbm_decimate_ui(bContext * /*C*/, wmOperator *op)
5933{
5934 ui::Layout &layout = *op->layout;
5935
5936 layout.use_property_split_set(true);
5937
5938 layout.prop(op->ptr, "ratio", UI_ITEM_NONE, std::nullopt, ICON_NONE);
5939
5940 layout.prop(op->ptr, "use_vertex_group", UI_ITEM_NONE, std::nullopt, ICON_NONE);
5941 ui::Layout &col = layout.column(false);
5942 col.active_set(RNA_boolean_get(op->ptr, "use_vertex_group"));
5943 col.prop(op->ptr, "vertex_group_factor", UI_ITEM_NONE, std::nullopt, ICON_NONE);
5944 col.prop(op->ptr, "invert_vertex_group", UI_ITEM_NONE, std::nullopt, ICON_NONE);
5945
5946 ui::Layout &row = layout.row(true, IFACE_("Symmetry"));
5947 row.prop(op->ptr, "use_symmetry", UI_ITEM_NONE, "", ICON_NONE);
5948 ui::Layout &sub = row.row(true);
5949 sub.active_set(RNA_boolean_get(op->ptr, "use_symmetry"));
5950 sub.prop(op->ptr, "symmetry_axis", ui::ITEM_R_EXPAND, std::nullopt, ICON_NONE);
5951}
5952
5954{
5955 /* identifiers */
5956 ot->name = "Decimate Geometry";
5957 ot->idname = "MESH_OT_decimate";
5958 ot->description = "Simplify geometry by collapsing edges";
5959
5960 /* API callbacks. */
5963 ot->ui = edbm_decimate_ui;
5965
5966 /* flags */
5968
5969 /* NOTE: keep in sync with 'rna_def_modifier_decimate'. */
5970 RNA_def_float(ot->srna, "ratio", 1.0f, 0.0f, 1.0f, "Ratio", "", 0.0f, 1.0f);
5971
5973 "use_vertex_group",
5974 false,
5975 "Vertex Group",
5976 "Use active vertex group as an influence");
5977 RNA_def_float(ot->srna,
5978 "vertex_group_factor",
5979 1.0f,
5980 0.0f,
5981 1000.0f,
5982 "Weight",
5983 "Vertex group strength",
5984 0.0f,
5985 10.0f);
5987 ot->srna, "invert_vertex_group", false, "Invert", "Invert vertex group influence");
5988
5989 RNA_def_boolean(ot->srna, "use_symmetry", false, "Symmetry", "Maintain symmetry on an axis");
5990
5991 RNA_def_enum(ot->srna, "symmetry_axis", rna_enum_axis_xyz_items, 1, "Axis", "Axis of symmetry");
5992}
5993
5995
5996/* -------------------------------------------------------------------- */
5999
6000static void edbm_dissolve_prop__use_verts(wmOperatorType *ot, bool value, int flag)
6001{
6002 PropertyRNA *prop;
6003
6004 prop = RNA_def_boolean(ot->srna,
6005 "use_verts",
6006 value,
6007 "Dissolve Vertices",
6008 "Dissolve remaining vertices which connect to only two edges");
6009
6010 if (flag) {
6012 }
6013}
6015{
6017 "use_face_split",
6018 false,
6019 "Face Split",
6020 "Split off face corners to maintain surrounding geometry");
6021}
6023{
6025 "use_boundary_tear",
6026 false,
6027 "Tear Boundary",
6028 "Split off face corners instead of merging faces");
6029}
6031{
6033 ot->srna,
6034 "angle_threshold",
6035 0,
6036 nullptr,
6037 0.0f,
6038 DEG2RADF(180.0f),
6039 "Angle Threshold",
6040 "Remaining vertices which separate edge pairs are preserved if their edge angle exceeds "
6041 "this threshold.",
6042 0.0f,
6043 DEG2RADF(180.0f));
6045 if (flag) {
6047 }
6048}
6049static void edbm_dissolve_prop__use_preserve_quads(wmOperatorType *ot, bool value, int flag)
6050{
6052 ot->srna,
6053 "use_preserve_quads",
6054 value,
6055 "Preserve Quads",
6056 "When dissolving the edge between two triangles, don't dissolve vertices");
6057 if (flag) {
6059 }
6060}
6061
6063{
6064 const bool use_face_split = RNA_boolean_get(op->ptr, "use_face_split");
6065 const bool use_boundary_tear = RNA_boolean_get(op->ptr, "use_boundary_tear");
6066
6067 const Main *bmain = CTX_data_main(C);
6068 const Scene *scene = CTX_data_scene(C);
6069 ViewLayer *view_layer = CTX_data_view_layer(C);
6071 *bmain, scene, view_layer, CTX_wm_view3d(C));
6072
6073 for (Object *obedit : objects) {
6075 if (bm->totvertsel == 0) {
6076 continue;
6077 }
6078
6080
6081 if (!EDBM_op_callf(bm,
6082 op,
6083 "dissolve_verts verts=%hv use_face_split=%b use_boundary_tear=%b",
6085 use_face_split,
6086 use_boundary_tear))
6087 {
6088 continue;
6089 }
6090
6092
6093 EDBMUpdate_Params params{};
6094 params.calc_looptris = true;
6095 params.calc_normals = false;
6096 params.is_destructive = true;
6097 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
6098 }
6099
6100 return OPERATOR_FINISHED;
6101}
6102
6104{
6105 /* identifiers */
6106 ot->name = "Dissolve Vertices";
6107 ot->description = "Dissolve vertices, merge edges and faces";
6108 ot->idname = "MESH_OT_dissolve_verts";
6109
6110 /* API callbacks. */
6113
6114 /* flags */
6116
6119}
6120
6122
6123/* -------------------------------------------------------------------- */
6126
6128{
6129 const bool use_verts = RNA_boolean_get(op->ptr, "use_verts");
6130 const bool use_face_split = RNA_boolean_get(op->ptr, "use_face_split");
6131 const float angle_threshold = RNA_float_get(op->ptr, "angle_threshold");
6132 const bool use_preserve_quads = RNA_boolean_get(op->ptr, "use_preserve_quads");
6133
6134 const Main *bmain = CTX_data_main(C);
6135 const Scene *scene = CTX_data_scene(C);
6136 ViewLayer *view_layer = CTX_data_view_layer(C);
6138 *bmain, scene, view_layer, CTX_wm_view3d(C));
6139 bool changed_multi = false;
6140
6141 for (Object *obedit : objects) {
6143 const int totvert_orig = bm->totvert;
6144 const int totedge_orig = bm->totedge;
6145 const int totface_orig = bm->totface;
6146
6147 if (bm->totedgesel == 0) {
6148 continue;
6149 }
6150
6152
6153 if (!EDBM_op_callf(bm,
6154 op,
6155 "dissolve_edges edges=%he use_verts=%b use_face_split=%b "
6156 "angle_threshold=%f use_preserve_quads=%b",
6158 use_verts,
6159 use_face_split,
6160 angle_threshold,
6161 use_preserve_quads))
6162 {
6163 continue;
6164 }
6165
6166 if (totvert_orig == bm->totvert && totedge_orig == bm->totedge && totface_orig == bm->totface)
6167 {
6168 continue;
6169 }
6170
6171 changed_multi = true;
6172
6174
6175 EDBMUpdate_Params params{};
6176 params.calc_looptris = true;
6177 params.calc_normals = false;
6178 params.is_destructive = true;
6179 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
6180 }
6181
6182 if (!changed_multi) {
6183 BKE_report(op->reports, RPT_WARNING, "No edges dissolved");
6184 }
6185
6186 return OPERATOR_FINISHED;
6187}
6188
6190{
6191 /* identifiers */
6192 ot->name = "Dissolve Edges";
6193 ot->description = "Dissolve edges, merging faces";
6194 ot->idname = "MESH_OT_dissolve_edges";
6195
6196 /* API callbacks. */
6199
6200 /* flags */
6202
6203 edbm_dissolve_prop__use_verts(ot, true, 0);
6207}
6208
6210
6211/* -------------------------------------------------------------------- */
6214
6216{
6217 const bool use_verts = RNA_boolean_get(op->ptr, "use_verts");
6218 const Main *bmain = CTX_data_main(C);
6219 const Scene *scene = CTX_data_scene(C);
6220 ViewLayer *view_layer = CTX_data_view_layer(C);
6222 *bmain, scene, view_layer, CTX_wm_view3d(C));
6223 for (Object *obedit : objects) {
6225 if (bm->totfacesel == 0) {
6226 continue;
6227 }
6228
6230
6232 op,
6233 "region.out",
6234 true,
6235 "dissolve_faces faces=%hf use_verts=%b",
6237 use_verts))
6238 {
6239 continue;
6240 }
6241
6243
6244 EDBMUpdate_Params params{};
6245 params.calc_looptris = true;
6246 params.calc_normals = false;
6247 params.is_destructive = true;
6248 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
6249 }
6250
6251 return OPERATOR_FINISHED;
6252}
6253
6255{
6256 /* identifiers */
6257 ot->name = "Dissolve Faces";
6258 ot->description = "Dissolve faces";
6259 ot->idname = "MESH_OT_dissolve_faces";
6260
6261 /* API callbacks. */
6264
6265 /* flags */
6267
6268 edbm_dissolve_prop__use_verts(ot, false, 0);
6269}
6270
6272
6273/* -------------------------------------------------------------------- */
6276
6278{
6279 Object *obedit = CTX_data_edit_object(C);
6281 PropertyRNA *prop;
6282
6283 prop = RNA_struct_find_property(op->ptr, "use_verts");
6284 if (!RNA_property_is_set(op->ptr, prop)) {
6285 /* always enable in edge-mode */
6286 if ((em->selectmode & SCE_SELECT_FACE) == 0) {
6287 RNA_property_boolean_set(op->ptr, prop, true);
6288 }
6289 }
6290
6291 if (em->selectmode & SCE_SELECT_VERTEX) {
6292 return edbm_dissolve_verts_exec(C, op);
6293 }
6294 if (em->selectmode & SCE_SELECT_EDGE) {
6295 return edbm_dissolve_edges_exec(C, op);
6296 }
6297 return edbm_dissolve_faces_exec(C, op);
6298}
6299
6300static bool dissolve_mode_poll_property(const bContext *C, wmOperator *op, const PropertyRNA *prop)
6301{
6302 UNUSED_VARS(op);
6303
6304 const char *prop_id = RNA_property_identifier(prop);
6305
6306 Object *obedit = CTX_data_edit_object(C);
6307 const BMEditMesh *em = BKE_editmesh_from_object(obedit);
6308 bool is_edge_select_mode = false;
6309
6310 if (em->selectmode & SCE_SELECT_VERTEX) {
6311 /* Pass. */
6312 }
6313 if (em->selectmode & SCE_SELECT_EDGE) {
6314 is_edge_select_mode = true;
6315 }
6316
6317 if (!is_edge_select_mode) {
6318 /* Angle Threshold is only used in edge select mode. */
6319 if (STREQ(prop_id, "angle_threshold")) {
6320 return false;
6321 }
6322 /* Preserve Quads is only used in edge select mode. */
6323 if (STREQ(prop_id, "use_preserve_quads")) {
6324 return false;
6325 }
6326 }
6327 return true;
6328}
6329
6331{
6332 /* identifiers */
6333 ot->name = "Dissolve Selection";
6334 ot->description = "Dissolve geometry based on the selection mode";
6335 ot->idname = "MESH_OT_dissolve_mode";
6336
6337 /* API callbacks. */
6341
6342 /* flags */
6344
6350}
6351
6353
6354/* -------------------------------------------------------------------- */
6357
6359{
6360 const float angle_limit = RNA_float_get(op->ptr, "angle_limit");
6361 const bool use_dissolve_boundaries = RNA_boolean_get(op->ptr, "use_dissolve_boundaries");
6362 const int delimit = RNA_enum_get(op->ptr, "delimit");
6363 char dissolve_flag;
6364
6365 const Main *bmain = CTX_data_main(C);
6366 const Scene *scene = CTX_data_scene(C);
6367 ViewLayer *view_layer = CTX_data_view_layer(C);
6369 *bmain, scene, view_layer, CTX_wm_view3d(C));
6370 for (Object *obedit : objects) {
6373
6374 if ((bm->totvertsel == 0) && (bm->totedgesel == 0) && (bm->totfacesel == 0)) {
6375 continue;
6376 }
6377
6379
6380 if (em->selectmode == SCE_SELECT_FACE) {
6381 /* flush selection to tags and untag edges/verts with partially selected faces */
6382 BMIter iter;
6383 BMIter liter;
6384
6385 BMElem *ele;
6386 BMFace *f;
6387 BMLoop *l;
6388
6389 BM_ITER_MESH (ele, &iter, bm, BM_VERTS_OF_MESH) {
6391 }
6392 BM_ITER_MESH (ele, &iter, bm, BM_EDGES_OF_MESH) {
6394 }
6395
6396 BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
6398 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
6401 }
6402 }
6403 }
6404
6405 dissolve_flag = BM_ELEM_TAG;
6406 }
6407 else {
6408 dissolve_flag = BM_ELEM_SELECT;
6409 }
6410
6412 bm,
6413 op,
6414 "region.out",
6415 true,
6416 "dissolve_limit edges=%he verts=%hv angle_limit=%f use_dissolve_boundaries=%b delimit=%i",
6417 dissolve_flag,
6418 dissolve_flag,
6419 angle_limit,
6420 use_dissolve_boundaries,
6421 delimit);
6422
6424
6425 EDBMUpdate_Params params{};
6426 params.calc_looptris = true;
6427 params.calc_normals = false;
6428 params.is_destructive = true;
6429 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
6430 }
6431
6432 return OPERATOR_FINISHED;
6433}
6434
6436{
6437 PropertyRNA *prop;
6438
6439 /* identifiers */
6440 ot->name = "Limited Dissolve";
6441 ot->idname = "MESH_OT_dissolve_limited";
6442 ot->description =
6443 "Dissolve selected edges and vertices, limited by the angle of surrounding geometry";
6444
6445 /* API callbacks. */
6448
6449 /* flags */
6451
6452 prop = RNA_def_float_rotation(ot->srna,
6453 "angle_limit",
6454 0,
6455 nullptr,
6456 0.0f,
6457 DEG2RADF(180.0f),
6458 "Max Angle",
6459 "Angle limit",
6460 0.0f,
6461 DEG2RADF(180.0f));
6464 "use_dissolve_boundaries",
6465 false,
6466 "All Boundaries",
6467 "Dissolve all vertices in between face boundaries");
6469 "delimit",
6472 "Delimit",
6473 "Delimit dissolve operation");
6474}
6475
6477
6478/* -------------------------------------------------------------------- */
6481
6483{
6484 const Main *bmain = CTX_data_main(C);
6485 const Scene *scene = CTX_data_scene(C);
6486 ViewLayer *view_layer = CTX_data_view_layer(C);
6487 int totelem_old[3] = {0, 0, 0};
6488 int totelem_new[3] = {0, 0, 0};
6489 bool changed_multi = false;
6490
6492 *bmain, scene, view_layer, CTX_wm_view3d(C));
6493
6494 const float thresh = RNA_float_get(op->ptr, "threshold");
6495
6496 for (Object *obedit : objects) {
6499 const int totvert_orig = bm->totvert;
6500 const int totedge_orig = bm->totedge;
6501 const int totface_orig = bm->totface;
6502
6503 if (!EDBM_op_callf(bm, op, "dissolve_degenerate edges=%he dist=%f", BM_ELEM_SELECT, thresh)) {
6504 continue;
6505 }
6506 if (totvert_orig == bm->totvert && totedge_orig == bm->totedge && totface_orig == bm->totface)
6507 {
6508 continue;
6509 }
6510
6511 /* tricky to maintain correct selection here, so just flush up from verts */
6513
6514 changed_multi = true;
6515 EDBMUpdate_Params params{};
6516 params.calc_looptris = true;
6517 params.calc_normals = false;
6518 params.is_destructive = true;
6519 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
6520
6521 totelem_old[0] += totvert_orig;
6522 totelem_old[1] += totedge_orig;
6523 totelem_old[2] += totface_orig;
6524
6525 totelem_new[0] += bm->totvert;
6526 totelem_new[1] += bm->totedge;
6527 totelem_new[2] += bm->totface;
6528 }
6529 if (!changed_multi) {
6530 BKE_report(op->reports, RPT_INFO, "No degenerate geometry found");
6531 }
6532 else {
6533 edbm_report_delete_info(op->reports, totelem_old, totelem_new);
6534 }
6535
6536 return OPERATOR_FINISHED;
6537}
6538
6540{
6541 /* identifiers */
6542 ot->name = "Degenerate Dissolve";
6543 ot->idname = "MESH_OT_dissolve_degenerate";
6544 ot->description = "Dissolve zero area faces and zero length edges";
6545
6546 /* API callbacks. */
6549
6550 /* flags */
6552
6554 "threshold",
6555 1e-4f,
6556 1e-6f,
6557 50.0f,
6558 "Merge Distance",
6559 "Maximum distance between elements to merge",
6560 1e-5f,
6561 10.0f);
6562}
6563
6565
6566/* -------------------------------------------------------------------- */
6569
6570/* internally uses dissolve */
6572{
6573 const bool use_face_split = RNA_boolean_get(op->ptr, "use_face_split");
6574 const Main *bmain = CTX_data_main(C);
6575 const Scene *scene = CTX_data_scene(C);
6576 ViewLayer *view_layer = CTX_data_view_layer(C);
6577
6579 *bmain, scene, view_layer, CTX_wm_view3d(C));
6580 for (Object *obedit : objects) {
6582 if (bm->totedgesel == 0) {
6583 continue;
6584 }
6585
6586 /* deal with selection */
6587 {
6588 BMEdge *e;
6589 BMIter iter;
6590
6592
6593 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
6594 if (BM_elem_flag_test(e, BM_ELEM_SELECT) && e->l) {
6595 BMLoop *l_iter = e->l;
6596 do {
6598 } while ((l_iter = l_iter->radial_next) != e->l);
6599 }
6600 }
6601 }
6602
6603 if (!EDBM_op_callf(
6604 bm,
6605 op,
6606 "dissolve_edges edges=%he use_verts=%b use_face_split=%b angle_threshold=%f",
6608 true,
6609 use_face_split,
6610 M_PI))
6611 {
6612 continue;
6613 }
6614
6616
6619
6620 EDBMUpdate_Params params{};
6621 params.calc_looptris = true;
6622 params.calc_normals = false;
6623 params.is_destructive = true;
6624 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
6625 }
6626
6627 return OPERATOR_FINISHED;
6628}
6629
6631{
6632 /* identifiers */
6633 ot->name = "Delete Edge Loop";
6634 ot->description = "Delete an edge loop by merging the faces on each side";
6635 ot->idname = "MESH_OT_delete_edgeloop";
6636
6637 /* API callbacks. */
6640
6641 /* flags */
6643
6645 "use_face_split",
6646 true,
6647 "Face Split",
6648 "Split off face corners to maintain surrounding geometry");
6649}
6650
6652
6653/* -------------------------------------------------------------------- */
6656
6658{
6659 const Main *bmain = CTX_data_main(C);
6660 const Scene *scene = CTX_data_scene(C);
6661 ViewLayer *view_layer = CTX_data_view_layer(C);
6663 *bmain, scene, view_layer, CTX_wm_view3d(C));
6664 for (Object *obedit : objects) {
6666 if ((bm->totvertsel == 0) && (bm->totedgesel == 0) && (bm->totfacesel == 0)) {
6667 continue;
6668 }
6670
6671 BMOperator bmop;
6672 EDBM_op_init(bm, &bmop, op, "split geom=%hvef use_only_faces=%b", BM_ELEM_SELECT, false);
6673 BMO_op_exec(bm, &bmop);
6676 bm, bmop.slots_out, "geom.out", BM_ALL_NOLOOP, BM_ELEM_SELECT, true);
6677
6679
6680 if (!EDBM_op_finish(bm, &bmop, op, true)) {
6681 continue;
6682 }
6683
6684 /* Geometry has changed, need to recalculate normals and tessellation. */
6685 EDBMUpdate_Params params{};
6686 params.calc_looptris = true;
6687 params.calc_normals = true;
6688 params.is_destructive = true;
6689 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
6690 }
6691
6692 return OPERATOR_FINISHED;
6693}
6694
6696{
6697 /* identifiers */
6698 ot->name = "Split";
6699 ot->idname = "MESH_OT_split";
6700 ot->description = "Split off selected geometry from connected unselected geometry";
6701
6702 /* API callbacks. */
6703 ot->exec = edbm_split_exec;
6705
6706 /* flags */
6708}
6709
6711
6712/* -------------------------------------------------------------------- */
6718
6719enum {
6735};
6736
6739 float srt;
6742};
6743
6744static int bmelemsort_comp(const void *v1, const void *v2)
6745{
6746 const BMElemSort *x1 = static_cast<const BMElemSort *>(v1);
6747 const BMElemSort *x2 = static_cast<const BMElemSort *>(v2);
6748
6749 return (x1->srt > x2->srt) - (x1->srt < x2->srt);
6750}
6751
6752/* Reorders vertices/edges/faces using a given methods. Loops are not supported. */
6754 Scene *scene,
6755 Object *ob,
6756 RegionView3D *rv3d,
6757 const int types,
6758 const int flag,
6759 const int action,
6760 const int reverse,
6761 const uint seed)
6762{
6764
6765 BMVert *ve;
6766 BMEdge *ed;
6767 BMFace *fa;
6768 BMIter iter;
6769
6770 /* In all five elements below, 0 = vertices, 1 = edges, 2 = faces. */
6771 /* Just to mark protected elements. */
6772 char *pblock[3] = {nullptr, nullptr, nullptr}, *pb;
6773 BMElemSort *sblock[3] = {nullptr, nullptr, nullptr}, *sb;
6774 uint *map[3] = {nullptr, nullptr, nullptr}, *mp;
6775 int totelem[3] = {0, 0, 0};
6776 int affected[3] = {0, 0, 0};
6777 int i, j;
6778
6779 if (!(types && flag && action)) {
6780 return;
6781 }
6782
6783 if (types & BM_VERT) {
6784 totelem[0] = bm->totvert;
6785 }
6786 if (types & BM_EDGE) {
6787 totelem[1] = bm->totedge;
6788 }
6789 if (types & BM_FACE) {
6790 totelem[2] = bm->totface;
6791 }
6792
6793 if (ELEM(action, SRT_VIEW_ZAXIS, SRT_VIEW_XAXIS)) {
6794 float mat[4][4];
6795 float fact = reverse ? -1.0 : 1.0;
6796 int coidx = (action == SRT_VIEW_ZAXIS) ? 2 : 0;
6797
6798 /* Apply the view matrix to the object matrix. */
6799 mul_m4_m4m4(mat, rv3d->viewmat, ob->object_to_world().ptr());
6800
6801 if (totelem[0]) {
6802 pb = pblock[0] = MEM_new_array_zeroed<char>(totelem[0], __func__);
6803 sb = sblock[0] = MEM_new_array_zeroed<BMElemSort>(totelem[0], __func__);
6804
6805 BM_ITER_MESH_INDEX (ve, &iter, bm, BM_VERTS_OF_MESH, i) {
6806 if (BM_elem_flag_test(ve, flag)) {
6807 float co[3];
6808 mul_v3_m4v3(co, mat, ve->co);
6809
6810 pb[i] = false;
6811 sb[affected[0]].org_idx = i;
6812 sb[affected[0]++].srt = co[coidx] * fact;
6813 }
6814 else {
6815 pb[i] = true;
6816 }
6817 }
6818 }
6819
6820 if (totelem[1]) {
6821 pb = pblock[1] = MEM_new_array_zeroed<char>(totelem[1], __func__);
6822 sb = sblock[1] = MEM_new_array_zeroed<BMElemSort>(totelem[1], __func__);
6823
6825 if (BM_elem_flag_test(ed, flag)) {
6826 float co[3];
6827 mid_v3_v3v3(co, ed->v1->co, ed->v2->co);
6828 mul_m4_v3(mat, co);
6829
6830 pb[i] = false;
6831 sb[affected[1]].org_idx = i;
6832 sb[affected[1]++].srt = co[coidx] * fact;
6833 }
6834 else {
6835 pb[i] = true;
6836 }
6837 }
6838 }
6839
6840 if (totelem[2]) {
6841 pb = pblock[2] = MEM_new_array_zeroed<char>(totelem[2], __func__);
6842 sb = sblock[2] = MEM_new_array_zeroed<BMElemSort>(totelem[2], __func__);
6843
6844 BM_ITER_MESH_INDEX (fa, &iter, bm, BM_FACES_OF_MESH, i) {
6845 if (BM_elem_flag_test(fa, flag)) {
6846 float co[3];
6848 mul_m4_v3(mat, co);
6849
6850 pb[i] = false;
6851 sb[affected[2]].org_idx = i;
6852 sb[affected[2]++].srt = co[coidx] * fact;
6853 }
6854 else {
6855 pb[i] = true;
6856 }
6857 }
6858 }
6859 }
6860
6861 else if (action == SRT_CURSOR_DISTANCE) {
6862 float cur[3];
6863 float mat[4][4];
6864 float fact = reverse ? -1.0 : 1.0;
6865
6866 copy_v3_v3(cur, scene->cursor.location);
6867
6868 invert_m4_m4(mat, ob->object_to_world().ptr());
6869 mul_m4_v3(mat, cur);
6870
6871 if (totelem[0]) {
6872 pb = pblock[0] = MEM_new_array_zeroed<char>(totelem[0], __func__);
6873 sb = sblock[0] = MEM_new_array_zeroed<BMElemSort>(totelem[0], __func__);
6874
6875 BM_ITER_MESH_INDEX (ve, &iter, bm, BM_VERTS_OF_MESH, i) {
6876 if (BM_elem_flag_test(ve, flag)) {
6877 pb[i] = false;
6878 sb[affected[0]].org_idx = i;
6879 sb[affected[0]++].srt = len_squared_v3v3(cur, ve->co) * fact;
6880 }
6881 else {
6882 pb[i] = true;
6883 }
6884 }
6885 }
6886
6887 if (totelem[1]) {
6888 pb = pblock[1] = MEM_new_array_zeroed<char>(totelem[1], __func__);
6889 sb = sblock[1] = MEM_new_array_zeroed<BMElemSort>(totelem[1], __func__);
6890
6892 if (BM_elem_flag_test(ed, flag)) {
6893 float co[3];
6894 mid_v3_v3v3(co, ed->v1->co, ed->v2->co);
6895
6896 pb[i] = false;
6897 sb[affected[1]].org_idx = i;
6898 sb[affected[1]++].srt = len_squared_v3v3(cur, co) * fact;
6899 }
6900 else {
6901 pb[i] = true;
6902 }
6903 }
6904 }
6905
6906 if (totelem[2]) {
6907 pb = pblock[2] = MEM_new_array_zeroed<char>(totelem[2], __func__);
6908 sb = sblock[2] = MEM_new_array_zeroed<BMElemSort>(totelem[2], __func__);
6909
6910 BM_ITER_MESH_INDEX (fa, &iter, bm, BM_FACES_OF_MESH, i) {
6911 if (BM_elem_flag_test(fa, flag)) {
6912 float co[3];
6914
6915 pb[i] = false;
6916 sb[affected[2]].org_idx = i;
6917 sb[affected[2]++].srt = len_squared_v3v3(cur, co) * fact;
6918 }
6919 else {
6920 pb[i] = true;
6921 }
6922 }
6923 }
6924 }
6925
6926 /* Faces only! */
6927 else if (action == SRT_MATERIAL && totelem[2]) {
6928 pb = pblock[2] = MEM_new_array_zeroed<char>(totelem[2], __func__);
6929 sb = sblock[2] = MEM_new_array_zeroed<BMElemSort>(totelem[2], __func__);
6930
6931 BM_ITER_MESH_INDEX (fa, &iter, bm, BM_FACES_OF_MESH, i) {
6932 if (BM_elem_flag_test(fa, flag)) {
6933 /* Reverse materials' order, not order of faces inside each mat! */
6934 /* NOTE: cannot use totcol, as mat_nr may sometimes be greater... */
6935 float srt = reverse ? float(MAXMAT - fa->mat_nr) : float(fa->mat_nr);
6936 pb[i] = false;
6937 sb[affected[2]].org_idx = i;
6938 /* Multiplying with totface and adding i ensures us
6939 * we keep current order for all faces of same mat. */
6940 sb[affected[2]++].srt = srt * float(totelem[2]) + float(i);
6941 // printf("e: %d; srt: %f; final: %f\n", i, srt, srt * float(totface) + float(i));
6942 }
6943 else {
6944 pb[i] = true;
6945 }
6946 }
6947 }
6948
6949 else if (action == SRT_SELECTED) {
6950 uint *tbuf[3] = {nullptr, nullptr, nullptr}, *tb;
6951
6952 if (totelem[0]) {
6953 tb = tbuf[0] = MEM_new_array_zeroed<uint>(totelem[0], __func__);
6954 mp = map[0] = MEM_new_array_zeroed<uint>(totelem[0], __func__);
6955
6956 BM_ITER_MESH_INDEX (ve, &iter, bm, BM_VERTS_OF_MESH, i) {
6957 if (BM_elem_flag_test(ve, flag)) {
6958 mp[affected[0]++] = i;
6959 }
6960 else {
6961 *tb = i;
6962 tb++;
6963 }
6964 }
6965 }
6966
6967 if (totelem[1]) {
6968 tb = tbuf[1] = MEM_new_array_zeroed<uint>(totelem[1], __func__);
6969 mp = map[1] = MEM_new_array_zeroed<uint>(totelem[1], __func__);
6970
6972 if (BM_elem_flag_test(ed, flag)) {
6973 mp[affected[1]++] = i;
6974 }
6975 else {
6976 *tb = i;
6977 tb++;
6978 }
6979 }
6980 }
6981
6982 if (totelem[2]) {
6983 tb = tbuf[2] = MEM_new_array_zeroed<uint>(totelem[2], __func__);
6984 mp = map[2] = MEM_new_array_zeroed<uint>(totelem[2], __func__);
6985
6986 BM_ITER_MESH_INDEX (fa, &iter, bm, BM_FACES_OF_MESH, i) {
6987 if (BM_elem_flag_test(fa, flag)) {
6988 mp[affected[2]++] = i;
6989 }
6990 else {
6991 *tb = i;
6992 tb++;
6993 }
6994 }
6995 }
6996
6997 for (j = 3; j--;) {
6998 int tot = totelem[j];
6999 int aff = affected[j];
7000 tb = tbuf[j];
7001 mp = map[j];
7002 if (!(tb && mp)) {
7003 continue;
7004 }
7005 if (ELEM(aff, 0, tot)) {
7006 MEM_delete(tb);
7007 MEM_delete(mp);
7008 map[j] = nullptr;
7009 continue;
7010 }
7011 if (reverse) {
7012 memcpy(tb + (tot - aff), mp, aff * sizeof(int));
7013 }
7014 else {
7015 memcpy(mp + aff, tb, (tot - aff) * sizeof(int));
7016 tb = mp;
7017 mp = map[j] = tbuf[j];
7018 tbuf[j] = tb;
7019 }
7020
7021 /* Reverse mapping, we want an org2new one! */
7022 for (i = tot, tb = tbuf[j] + tot - 1; i--; tb--) {
7023 mp[*tb] = i;
7024 }
7025 MEM_delete(tbuf[j]);
7026 }
7027 }
7028
7029 else if (action == SRT_RANDOMIZE) {
7030 if (totelem[0]) {
7031 /* Re-init random generator for each element type, to get consistent random when
7032 * enabling/disabling an element type. */
7033 RNG *rng = BLI_rng_new_srandom(seed);
7034 pb = pblock[0] = MEM_new_array_zeroed<char>(totelem[0], __func__);
7035 sb = sblock[0] = MEM_new_array_zeroed<BMElemSort>(totelem[0], __func__);
7036
7037 BM_ITER_MESH_INDEX (ve, &iter, bm, BM_VERTS_OF_MESH, i) {
7038 if (BM_elem_flag_test(ve, flag)) {
7039 pb[i] = false;
7040 sb[affected[0]].org_idx = i;
7041 sb[affected[0]++].srt = BLI_rng_get_float(rng);
7042 }
7043 else {
7044 pb[i] = true;
7045 }
7046 }
7047
7048 BLI_rng_free(rng);
7049 }
7050
7051 if (totelem[1]) {
7052 RNG *rng = BLI_rng_new_srandom(seed);
7053 pb = pblock[1] = MEM_new_array_zeroed<char>(totelem[1], __func__);
7054 sb = sblock[1] = MEM_new_array_zeroed<BMElemSort>(totelem[1], __func__);
7055
7057 if (BM_elem_flag_test(ed, flag)) {
7058 pb[i] = false;
7059 sb[affected[1]].org_idx = i;
7060 sb[affected[1]++].srt = BLI_rng_get_float(rng);
7061 }
7062 else {
7063 pb[i] = true;
7064 }
7065 }
7066
7067 BLI_rng_free(rng);
7068 }
7069
7070 if (totelem[2]) {
7071 RNG *rng = BLI_rng_new_srandom(seed);
7072 pb = pblock[2] = MEM_new_array_zeroed<char>(totelem[2], __func__);
7073 sb = sblock[2] = MEM_new_array_zeroed<BMElemSort>(totelem[2], __func__);
7074
7075 BM_ITER_MESH_INDEX (fa, &iter, bm, BM_FACES_OF_MESH, i) {
7076 if (BM_elem_flag_test(fa, flag)) {
7077 pb[i] = false;
7078 sb[affected[2]].org_idx = i;
7079 sb[affected[2]++].srt = BLI_rng_get_float(rng);
7080 }
7081 else {
7082 pb[i] = true;
7083 }
7084 }
7085
7086 BLI_rng_free(rng);
7087 }
7088 }
7089
7090 else if (action == SRT_REVERSE) {
7091 if (totelem[0]) {
7092 pb = pblock[0] = MEM_new_array_zeroed<char>(totelem[0], __func__);
7093 sb = sblock[0] = MEM_new_array_zeroed<BMElemSort>(totelem[0], __func__);
7094
7095 BM_ITER_MESH_INDEX (ve, &iter, bm, BM_VERTS_OF_MESH, i) {
7096 if (BM_elem_flag_test(ve, flag)) {
7097 pb[i] = false;
7098 sb[affected[0]].org_idx = i;
7099 sb[affected[0]++].srt = float(-i);
7100 }
7101 else {
7102 pb[i] = true;
7103 }
7104 }
7105 }
7106
7107 if (totelem[1]) {
7108 pb = pblock[1] = MEM_new_array_zeroed<char>(totelem[1], __func__);
7109 sb = sblock[1] = MEM_new_array_zeroed<BMElemSort>(totelem[1], __func__);
7110
7112 if (BM_elem_flag_test(ed, flag)) {
7113 pb[i] = false;
7114 sb[affected[1]].org_idx = i;
7115 sb[affected[1]++].srt = float(-i);
7116 }
7117 else {
7118 pb[i] = true;
7119 }
7120 }
7121 }
7122
7123 if (totelem[2]) {
7124 pb = pblock[2] = MEM_new_array_zeroed<char>(totelem[2], __func__);
7125 sb = sblock[2] = MEM_new_array_zeroed<BMElemSort>(totelem[2], __func__);
7126
7127 BM_ITER_MESH_INDEX (fa, &iter, bm, BM_FACES_OF_MESH, i) {
7128 if (BM_elem_flag_test(fa, flag)) {
7129 pb[i] = false;
7130 sb[affected[2]].org_idx = i;
7131 sb[affected[2]++].srt = float(-i);
7132 }
7133 else {
7134 pb[i] = true;
7135 }
7136 }
7137 }
7138 }
7139
7140 // printf("%d vertices: %d to be affected...\n", totelem[0], affected[0]);
7141 // printf("%d edges: %d to be affected...\n", totelem[1], affected[1]);
7142 // printf("%d faces: %d to be affected...\n", totelem[2], affected[2]);
7143 if (affected[0] == 0 && affected[1] == 0 && affected[2] == 0) {
7144 for (j = 3; j--;) {
7145 if (pblock[j]) {
7146 MEM_delete(pblock[j]);
7147 }
7148 if (sblock[j]) {
7149 MEM_delete(sblock[j]);
7150 }
7151 if (map[j]) {
7152 MEM_delete(map[j]);
7153 }
7154 }
7155 return;
7156 }
7157
7158 /* Sort affected elements, and populate mapping arrays, if needed. */
7159 for (j = 3; j--;) {
7160 pb = pblock[j];
7161 sb = sblock[j];
7162 if (pb && sb && !map[j]) {
7163 const char *p_blk;
7164 BMElemSort *s_blk;
7165 int tot = totelem[j];
7166 int aff = affected[j];
7167
7168 qsort(sb, aff, sizeof(BMElemSort), bmelemsort_comp);
7169
7170 mp = map[j] = MEM_new_array_uninitialized<uint>(tot, __func__);
7171 p_blk = pb + tot - 1;
7172 s_blk = sb + aff - 1;
7173 for (i = tot; i--; p_blk--) {
7174 if (*p_blk) { /* Protected! */
7175 mp[i] = i;
7176 }
7177 else {
7178 mp[s_blk->org_idx] = i;
7179 s_blk--;
7180 }
7181 }
7182 }
7183 if (pb) {
7184 MEM_delete(pb);
7185 }
7186 if (sb) {
7187 MEM_delete(sb);
7188 }
7189 }
7190
7191 BM_mesh_remap(bm, map[0], map[1], map[2]);
7192
7193 EDBMUpdate_Params params{};
7194 params.calc_looptris = (totelem[2] != 0);
7195 params.calc_normals = false;
7196 params.is_destructive = true;
7197 EDBM_update(id_cast<Mesh *>(ob->data), &params);
7198
7201
7202 for (j = 3; j--;) {
7203 if (map[j]) {
7204 MEM_delete(map[j]);
7205 }
7206 }
7207}
7208
7210{
7211 const Main *bmain = CTX_data_main(C);
7212 Scene *scene = CTX_data_scene(C);
7213 ViewLayer *view_layer = CTX_data_view_layer(C);
7214 Object *ob_active = CTX_data_edit_object(C);
7215
7216 /* may be nullptr */
7218
7219 const int action = RNA_enum_get(op->ptr, "type");
7220 PropertyRNA *prop_elem_types = RNA_struct_find_property(op->ptr, "elements");
7221 const bool use_reverse = RNA_boolean_get(op->ptr, "reverse");
7222 uint seed = RNA_int_get(op->ptr, "seed");
7223 int elem_types = 0;
7224
7225 if (ELEM(action, SRT_VIEW_ZAXIS, SRT_VIEW_XAXIS)) {
7226 if (rv3d == nullptr) {
7227 BKE_report(op->reports, RPT_ERROR, "View not found, cannot sort by view axis");
7228 return OPERATOR_CANCELLED;
7229 }
7230 }
7231
7232 /* If no elem_types set, use current selection mode to set it! */
7233 if (RNA_property_is_set(op->ptr, prop_elem_types)) {
7234 elem_types = RNA_property_enum_get(op->ptr, prop_elem_types);
7235 }
7236 else {
7237 BMEditMesh *em = BKE_editmesh_from_object(ob_active);
7238 if (em->selectmode & SCE_SELECT_VERTEX) {
7239 elem_types |= BM_VERT;
7240 }
7241 if (em->selectmode & SCE_SELECT_EDGE) {
7242 elem_types |= BM_EDGE;
7243 }
7244 if (em->selectmode & SCE_SELECT_FACE) {
7245 elem_types |= BM_FACE;
7246 }
7247 RNA_enum_set(op->ptr, "elements", elem_types);
7248 }
7249
7251 *bmain, scene, view_layer, CTX_wm_view3d(C));
7252
7253 for (uint ob_index = 0; ob_index < objects.size(); ob_index++) {
7254 Object *ob = objects[ob_index];
7256
7257 if (!((elem_types & BM_VERT && bm->totvertsel > 0) ||
7258 (elem_types & BM_EDGE && bm->totedgesel > 0) ||
7259 (elem_types & BM_FACE && bm->totfacesel > 0)))
7260 {
7261 continue;
7262 }
7263
7264 int seed_iter = seed;
7265
7266 /* This gives a consistent result regardless of object order */
7267 if (ob_index) {
7268 seed_iter += BLI_ghashutil_strhash_p(ob->id.name);
7269 }
7270
7272 C, scene, ob, rv3d, elem_types, BM_ELEM_SELECT, action, use_reverse, seed_iter);
7273 }
7274 return OPERATOR_FINISHED;
7275}
7276
7278 wmOperator *op,
7279 const PropertyRNA *prop)
7280{
7281 const char *prop_id = RNA_property_identifier(prop);
7282 const int action = RNA_enum_get(op->ptr, "type");
7283
7284 /* Only show seed for randomize action! */
7285 if (STREQ(prop_id, "seed")) {
7286 if (action == SRT_RANDOMIZE) {
7287 return true;
7288 }
7289 return false;
7290 }
7291
7292 /* Hide seed for reverse and randomize actions! */
7293 if (STREQ(prop_id, "reverse")) {
7294 if (ELEM(action, SRT_RANDOMIZE, SRT_REVERSE)) {
7295 return false;
7296 }
7297 return true;
7298 }
7299
7300 return true;
7301}
7302
7304{
7305 static const EnumPropertyItem type_items[] = {
7307 "VIEW_ZAXIS",
7308 0,
7309 "View Z Axis",
7310 "Sort selected elements from farthest to nearest one in current view"},
7312 "VIEW_XAXIS",
7313 0,
7314 "View X Axis",
7315 "Sort selected elements from left to right one in current view"},
7317 "CURSOR_DISTANCE",
7318 0,
7319 "Cursor Distance",
7320 "Sort selected elements from nearest to farthest from 3D cursor"},
7321 {SRT_MATERIAL,
7322 "MATERIAL",
7323 0,
7324 "Material",
7325 "Sort selected faces from smallest to greatest material index"},
7326 {SRT_SELECTED,
7327 "SELECTED",
7328 0,
7329 "Selected",
7330 "Move all selected elements in first places, preserving their relative order.\n"
7331 "Warning: This will affect unselected elements' indices as well"},
7332 {SRT_RANDOMIZE, "RANDOMIZE", 0, "Randomize", "Randomize order of selected elements"},
7333 {SRT_REVERSE, "REVERSE", 0, "Reverse", "Reverse current order of selected elements"},
7334 {0, nullptr, 0, nullptr, nullptr},
7335 };
7336
7337 static const EnumPropertyItem elem_items[] = {
7338 {BM_VERT, "VERT", 0, "Vertices", ""},
7339 {BM_EDGE, "EDGE", 0, "Edges", ""},
7340 {BM_FACE, "FACE", 0, "Faces", ""},
7341 {0, nullptr, 0, nullptr, nullptr},
7342 };
7343
7344 /* identifiers */
7345 ot->name = "Sort Mesh Elements";
7346 ot->description =
7347 "The order of selected vertices/edges/faces is modified, based on a given method";
7348 ot->idname = "MESH_OT_sort_elements";
7349
7350 /* API callbacks. */
7351 ot->invoke = WM_menu_invoke;
7355
7356 /* flags */
7358
7359 /* properties */
7360 ot->prop = RNA_def_enum(ot->srna,
7361 "type",
7362 type_items,
7364 "Type",
7365 "Type of reordering operation to apply");
7367 "elements",
7368 elem_items,
7369 BM_VERT,
7370 "Elements",
7371 "Which elements to affect (vertices, edges and/or faces)");
7372 RNA_def_boolean(ot->srna, "reverse", false, "Reverse", "Reverse the sorting effect");
7373 RNA_def_int(ot->srna, "seed", 0, 0, INT_MAX, "Seed", "Seed for random-based operations", 0, 255);
7374}
7375
7377
7378/* -------------------------------------------------------------------- */
7381
7382enum {
7386};
7387
7389{
7390 /* tags boundary edges from a face selection */
7391 BMIter iter;
7392 BMFace *f;
7393 BMEdge *e;
7394 int totface_del = 0;
7395
7397
7398 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
7402 }
7403 else {
7404 BMIter fiter;
7405 bool is_all_sel = true;
7406 /* check if its only used by selected faces */
7407 BM_ITER_ELEM (f, &fiter, e, BM_FACES_OF_EDGE) {
7409 /* Tag face for removal. */
7410 if (!BM_elem_flag_test(f, BM_ELEM_TAG)) {
7412 totface_del++;
7413 }
7414 }
7415 else {
7416 is_all_sel = false;
7417 }
7418 }
7419
7420 if (is_all_sel == false) {
7422 }
7423 }
7424 }
7425 }
7426
7427 return totface_del;
7428}
7429
7431 Mesh *mesh,
7432 const bool use_pairs,
7433 const bool use_cyclic,
7434 const bool use_merge,
7435 const float merge_factor,
7436 const int twist_offset)
7437{
7439 BMOperator bmop;
7440 char edge_hflag;
7441 int totface_del = 0;
7442 BMFace **totface_del_arr = nullptr;
7443 const bool use_faces = (bm->totfacesel != 0);
7444 bool changed = false;
7445
7446 if (use_faces) {
7447 /* NOTE: When all faces are selected, all faces will be deleted with no edge-loops remaining.
7448 * In this case bridge will fail with a waning and delete all faces.
7449 * Ideally it's possible to detect cases when deleting faces leaves remaining edge-loops.
7450 * While this can be done in trivial cases - by checking the number of selected faces matches
7451 * the number of faces, that won't work for more involved cases involving hidden faces
7452 * and wire edges. One option could be to copy & restore the edit-mesh however
7453 * this is quite an expensive operation - to properly handle clearly invalid input.
7454 * Accept this limitation, the user must undo to restore the previous state, see: #123405. */
7455
7456 BMIter iter;
7457 BMFace *f;
7458 int i;
7459
7460 totface_del = edbm_bridge_tag_boundary_edges(bm);
7461 totface_del_arr = MEM_new_array_uninitialized<BMFace *>(totface_del, __func__);
7462
7463 i = 0;
7464 BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
7466 totface_del_arr[i++] = f;
7467 }
7468 }
7469 edge_hflag = BM_ELEM_TAG;
7470 }
7471 else {
7472 edge_hflag = BM_ELEM_SELECT;
7473 }
7474
7476 &bmop,
7477 op,
7478 "bridge_loops edges=%he use_pairs=%b use_cyclic=%b use_merge=%b merge_factor=%f "
7479 "twist_offset=%i",
7480 edge_hflag,
7481 use_pairs,
7482 use_cyclic,
7483 use_merge,
7484 merge_factor,
7485 twist_offset);
7486
7487 if (use_faces && totface_del) {
7488 int i;
7490 for (i = 0; i < totface_del; i++) {
7491 BM_elem_flag_enable(totface_del_arr[i], BM_ELEM_TAG);
7492 }
7494 bm, BMO_FLAG_DEFAULTS, "delete geom=%hf context=%i", BM_ELEM_TAG, DEL_FACES_KEEP_BOUNDARY);
7495 changed = true;
7496 }
7497
7498 BMO_op_exec(bm, &bmop);
7499
7501 /* when merge is used the edges are joined and remain selected */
7502 if (use_merge == false) {
7505
7506 changed = true;
7507 }
7508
7509 if (use_merge == false) {
7510 EdgeRingOpSubdProps op_props;
7511 mesh_operator_edgering_props_get(op, &op_props);
7512
7513 if (op_props.cuts) {
7514 BMOperator bmop_subd;
7515 /* we only need face normals updated */
7517
7519 &bmop_subd,
7520 0,
7521 "subdivide_edgering edges=%S interp_mode=%i cuts=%i smooth=%f "
7522 "profile_shape=%i profile_shape_factor=%f",
7523 &bmop,
7524 "edges.out",
7525 op_props.interp_mode,
7526 op_props.cuts,
7527 op_props.smooth,
7528 op_props.profile_shape,
7529 op_props.profile_shape_factor);
7530 BMO_op_exec(bm, &bmop_subd);
7532 bm, bmop_subd.slots_out, "faces.out", BM_FACE, BM_ELEM_SELECT, true);
7533 BMO_op_finish(bm, &bmop_subd);
7534
7535 changed = true;
7536 }
7537 }
7538 }
7539
7540 if (totface_del_arr) {
7541 MEM_delete(totface_del_arr);
7542 }
7543
7544 if (EDBM_op_finish(bm, &bmop, op, true)) {
7545 changed = true;
7546 }
7547
7548 if (changed) {
7549 EDBMUpdate_Params params{};
7550 params.calc_looptris = true;
7551 params.calc_normals = false;
7552 params.is_destructive = true;
7553 EDBM_update(mesh, &params);
7554 }
7555
7556 /* Always return finished so the user can select different options. */
7557 return OPERATOR_FINISHED;
7558}
7559
7561{
7562 const int type = RNA_enum_get(op->ptr, "type");
7563 const bool use_pairs = (type == MESH_BRIDGELOOP_PAIRS);
7564 const bool use_cyclic = (type == MESH_BRIDGELOOP_CLOSED);
7565 const bool use_merge = RNA_boolean_get(op->ptr, "use_merge");
7566 const float merge_factor = RNA_float_get(op->ptr, "merge_factor");
7567 const int twist_offset = RNA_int_get(op->ptr, "twist_offset");
7568 const Main *bmain = CTX_data_main(C);
7569 const Scene *scene = CTX_data_scene(C);
7570 ViewLayer *view_layer = CTX_data_view_layer(C);
7571
7573 *bmain, scene, view_layer, CTX_wm_view3d(C));
7574 for (Object *obedit : objects) {
7576 if (bm->totvertsel == 0) {
7577 continue;
7578 }
7579
7581 id_cast<Mesh *>(obedit->data),
7582 use_pairs,
7583 use_cyclic,
7584 use_merge,
7585 merge_factor,
7586 twist_offset);
7587 }
7588 return OPERATOR_FINISHED;
7589}
7590
7592{
7593 static const EnumPropertyItem type_items[] = {
7594 {MESH_BRIDGELOOP_SINGLE, "SINGLE", 0, "Open Loop", ""},
7595 {MESH_BRIDGELOOP_CLOSED, "CLOSED", 0, "Closed Loop", ""},
7596 {MESH_BRIDGELOOP_PAIRS, "PAIRS", 0, "Loop Pairs", ""},
7597 {0, nullptr, 0, nullptr, nullptr},
7598 };
7599
7600 /* identifiers */
7601 ot->name = "Bridge Edge Loops";
7602 ot->description = "Create a bridge of faces between two or more selected edge loops";
7603 ot->idname = "MESH_OT_bridge_edge_loops";
7604
7605 /* API callbacks. */
7608
7609 /* flags */
7611
7612 ot->prop = RNA_def_enum(ot->srna,
7613 "type",
7614 type_items,
7616 "Connect Loops",
7617 "Method of bridging multiple loops");
7618
7619 RNA_def_boolean(ot->srna, "use_merge", false, "Merge", "Merge rather than creating faces");
7620 RNA_def_float(ot->srna, "merge_factor", 0.5f, 0.0f, 1.0f, "Merge Factor", "", 0.0f, 1.0f);
7621 RNA_def_int(ot->srna,
7622 "twist_offset",
7623 0,
7624 -1000,
7625 1000,
7626 "Twist",
7627 "Twist offset for closed loops",
7628 -1000,
7629 1000);
7630
7632}
7633
7635
7636/* -------------------------------------------------------------------- */
7639
7641{
7642 const bool use_boundary = RNA_boolean_get(op->ptr, "use_boundary");
7643 const bool use_even_offset = RNA_boolean_get(op->ptr, "use_even_offset");
7644 const bool use_replace = RNA_boolean_get(op->ptr, "use_replace");
7645 const bool use_relative_offset = RNA_boolean_get(op->ptr, "use_relative_offset");
7646 const bool use_crease = RNA_boolean_get(op->ptr, "use_crease");
7647 const float crease_weight = RNA_float_get(op->ptr, "crease_weight");
7648 const float thickness = RNA_float_get(op->ptr, "thickness");
7649 const float offset = RNA_float_get(op->ptr, "offset");
7650
7651 const Main *bmain = CTX_data_main(C);
7652 const Scene *scene = CTX_data_scene(C);
7653 ViewLayer *view_layer = CTX_data_view_layer(C);
7655 *bmain, scene, view_layer, CTX_wm_view3d(C));
7656 for (Object *obedit : objects) {
7658
7659 if (bm->totfacesel == 0) {
7660 continue;
7661 }
7662
7663 BMOperator bmop;
7664
7666 &bmop,
7667 op,
7668 "wireframe faces=%hf use_replace=%b use_boundary=%b use_even_offset=%b "
7669 "use_relative_offset=%b "
7670 "use_crease=%b crease_weight=%f thickness=%f offset=%f",
7672 use_replace,
7673 use_boundary,
7674 use_even_offset,
7675 use_relative_offset,
7676 use_crease,
7677 crease_weight,
7678 thickness,
7679 offset);
7680
7681 BMO_op_exec(bm, &bmop);
7682
7685
7686 if (!EDBM_op_finish(bm, &bmop, op, true)) {
7687 continue;
7688 }
7689
7690 EDBMUpdate_Params params{};
7691 params.calc_looptris = true;
7692 params.calc_normals = false;
7693 params.is_destructive = true;
7694 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
7695 }
7696
7697 return OPERATOR_FINISHED;
7698}
7699
7701{
7702 PropertyRNA *prop;
7703
7704 /* identifiers */
7705 ot->name = "Wireframe";
7706 ot->idname = "MESH_OT_wireframe";
7707 ot->description = "Create a solid wireframe from faces";
7708
7709 /* API callbacks. */
7712
7713 /* flags */
7715
7716 /* properties */
7717 RNA_def_boolean(ot->srna, "use_boundary", true, "Boundary", "Inset face boundaries");
7719 "use_even_offset",
7720 true,
7721 "Offset Even",
7722 "Scale the offset to give more even thickness");
7724 "use_relative_offset",
7725 false,
7726 "Offset Relative",
7727 "Scale the offset by surrounding geometry");
7728 RNA_def_boolean(ot->srna, "use_replace", true, "Replace", "Remove original faces");
7730 ot->srna, "thickness", 0.01f, 0.0f, 1e4f, "Thickness", "", 0.0f, 10.0f);
7731 /* use 1 rather than 10 for max else dragging the button moves too far */
7732 RNA_def_property_ui_range(prop, 0.0, 1.0, 0.01, 4);
7733 RNA_def_float_distance(ot->srna, "offset", 0.01f, 0.0f, 1e4f, "Offset", "", 0.0f, 10.0f);
7735 "use_crease",
7736 false,
7737 "Crease",
7738 "Crease hub edges for an improved subdivision surface");
7739 prop = RNA_def_float(
7740 ot->srna, "crease_weight", 0.01f, 0.0f, 1e3f, "Crease Weight", "", 0.0f, 1.0f);
7741 RNA_def_property_ui_range(prop, 0.0, 1.0, 0.1, 2);
7742}
7743
7745
7746/* -------------------------------------------------------------------- */
7749
7751{
7752 const bool use_cap_endpoint = RNA_boolean_get(op->ptr, "use_cap_endpoint");
7753 bool changed_multi = false;
7754 const Main *bmain = CTX_data_main(C);
7755 Scene *scene = CTX_data_scene(C);
7756 ViewLayer *view_layer = CTX_data_view_layer(C);
7758 *bmain, scene, view_layer, CTX_wm_view3d(C));
7759 for (Base *base : bases) {
7760 Object *obedit = base->object;
7762 if (bm->totedgesel == 0) {
7763 continue;
7764 }
7765
7766 BMOperator bmop;
7768 &bmop,
7769 op,
7770 "offset_edgeloops edges=%he use_cap_endpoint=%b",
7772 use_cap_endpoint);
7773
7774 BMO_op_exec(bm, &bmop);
7775
7777
7779
7780 if (EDBM_op_finish(bm, &bmop, op, true)) {
7781 EDBMUpdate_Params params{};
7782 params.calc_looptris = true;
7783 params.calc_normals = false;
7784 params.is_destructive = true;
7785 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
7786 changed_multi = true;
7787 }
7788 }
7789
7790 if (changed_multi) {
7796 if (scene->toolsettings->selectmode == SCE_SELECT_FACE) {
7798 }
7799 }
7800
7801 return changed_multi ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
7802}
7803
7805{
7806 /* identifiers */
7807 ot->name = "Offset Edge Loop";
7808 ot->idname = "MESH_OT_offset_edge_loops";
7809 ot->description = "Create offset edge loop from the current selection";
7810
7811 /* API callbacks. */
7814
7815 /* Keep internal, since this is only meant to be accessed via
7816 * `MESH_OT_offset_edge_loops_slide`. */
7817
7818 /* flags */
7820
7822 ot->srna, "use_cap_endpoint", false, "Cap Endpoint", "Extend loop around end-points");
7823}
7824
7826
7827/* -------------------------------------------------------------------- */
7830
7831#ifdef WITH_BULLET
7832static wmOperatorStatus edbm_convex_hull_exec(bContext *C, wmOperator *op)
7833{
7834 const bool use_existing_faces = RNA_boolean_get(op->ptr, "use_existing_faces");
7835 const bool delete_unused = RNA_boolean_get(op->ptr, "delete_unused");
7836 const bool make_holes = RNA_boolean_get(op->ptr, "make_holes");
7837 const bool join_triangles = RNA_boolean_get(op->ptr, "join_triangles");
7838
7839 float angle_face_threshold = RNA_float_get(op->ptr, "face_threshold");
7840 float angle_shape_threshold = RNA_float_get(op->ptr, "shape_threshold");
7841
7842 const Main *bmain = CTX_data_main(C);
7843 const Scene *scene = CTX_data_scene(C);
7844 ViewLayer *view_layer = CTX_data_view_layer(C);
7845 const Vector<Object *> objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(
7846 *bmain, scene, view_layer, CTX_wm_view3d(C));
7847 for (Object *obedit : objects) {
7848 BMEditMesh *em = BKE_editmesh_from_object(obedit);
7849 BMesh *bm = BKE_editmesh_bmesh_get_for_write(obedit);
7850
7851 if (bm->totvertsel == 0) {
7852 continue;
7853 }
7854
7855 BMOperator bmop;
7856
7858 &bmop,
7859 op,
7860 "convex_hull input=%hvef "
7861 "use_existing_faces=%b",
7863 use_existing_faces);
7864 BMO_op_exec(bm, &bmop);
7865
7866 /* Hull fails if input is coplanar */
7868 EDBM_op_finish(bm, &bmop, op, true);
7869 continue;
7870 }
7871
7872 BMO_slot_buffer_hflag_enable(bm, bmop.slots_out, "geom.out", BM_FACE, BM_ELEM_SELECT, true);
7873
7874 /* Delete unused vertices, edges, and faces */
7875 if (delete_unused) {
7876 if (!EDBM_op_callf(
7877 bm, op, "delete geom=%S context=%i", &bmop, "geom_unused.out", DEL_ONLYTAGGED))
7878 {
7879 EDBM_op_finish(bm, &bmop, op, true);
7880 continue;
7881 }
7882 }
7883
7884 /* Delete hole edges/faces */
7885 if (make_holes) {
7886 if (!EDBM_op_callf(
7887 bm, op, "delete geom=%S context=%i", &bmop, "geom_holes.out", DEL_ONLYTAGGED))
7888 {
7889 EDBM_op_finish(bm, &bmop, op, true);
7890 continue;
7891 }
7892 }
7893
7894 /* Merge adjacent triangles */
7895 if (join_triangles) {
7897 op,
7898 "faces.out",
7899 true,
7900 "join_triangles faces=%S "
7901 "angle_face_threshold=%f angle_shape_threshold=%f",
7902 &bmop,
7903 "geom.out",
7904 angle_face_threshold,
7905 angle_shape_threshold))
7906 {
7907 EDBM_op_finish(bm, &bmop, op, true);
7908 continue;
7909 }
7910 }
7911
7912 if (!EDBM_op_finish(bm, &bmop, op, true)) {
7913 continue;
7914 }
7915
7916 EDBMUpdate_Params params{};
7917 params.calc_looptris = true;
7918 params.calc_normals = false;
7919 params.is_destructive = true;
7920 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
7921
7924 }
7925
7926 return OPERATOR_FINISHED;
7927}
7928
7930{
7931 /* identifiers */
7932 ot->name = "Convex Hull";
7933 ot->description = "Enclose selected vertices in a convex polyhedron";
7934 ot->idname = "MESH_OT_convex_hull";
7935
7936 /* API callbacks. */
7937 ot->exec = edbm_convex_hull_exec;
7938 ot->poll = ED_operator_editmesh;
7939
7940 /* flags */
7941 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
7942
7943 /* props */
7944 RNA_def_boolean(ot->srna,
7945 "delete_unused",
7946 true,
7947 "Delete Unused",
7948 "Delete selected elements that are not used by the hull");
7949
7950 RNA_def_boolean(ot->srna,
7951 "use_existing_faces",
7952 true,
7953 "Use Existing Faces",
7954 "Skip hull triangles that are covered by a pre-existing face");
7955
7956 RNA_def_boolean(ot->srna,
7957 "make_holes",
7958 false,
7959 "Make Holes",
7960 "Delete selected faces that are used by the hull");
7961
7963 ot->srna, "join_triangles", true, "Join Triangles", "Merge adjacent triangles into quads");
7964
7966}
7967#endif /* WITH_BULLET */
7968
7970
7971/* -------------------------------------------------------------------- */
7974
7976{
7977 const float thresh = RNA_float_get(op->ptr, "threshold");
7978 const Main *bmain = CTX_data_main(C);
7979 const Scene *scene = CTX_data_scene(C);
7980 ViewLayer *view_layer = CTX_data_view_layer(C);
7982 *bmain, scene, view_layer, CTX_wm_view3d(C));
7983
7984 for (Object *obedit : objects) {
7987
7988 if (bm->totvertsel == 0) {
7989 continue;
7990 }
7991
7992 BMOperator bmop;
7994 &bmop,
7995 op,
7996 "symmetrize input=%hvef direction=%i dist=%f",
7998 RNA_enum_get(op->ptr, "direction"),
7999 thresh);
8000 BMO_op_exec(bm, &bmop);
8001
8003
8005 bm, bmop.slots_out, "geom.out", BM_ALL_NOLOOP, BM_ELEM_SELECT, true);
8006
8007 if (!EDBM_op_finish(bm, &bmop, op, true)) {
8008 continue;
8009 }
8010
8011 bool calc_normals = false;
8012 if (scene->toolsettings->automerge & AUTO_MERGE) {
8013 calc_normals = EDBM_automerge_connected(
8014 obedit, false, BM_ELEM_SELECT, scene->toolsettings->doublimit);
8015 }
8016
8017 EDBMUpdate_Params params{};
8018 params.calc_looptris = true;
8019 params.calc_normals = calc_normals;
8020 params.is_destructive = true;
8021 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
8022
8025 }
8026
8027 return OPERATOR_FINISHED;
8028}
8029
8031{
8032 /* identifiers */
8033 ot->name = "Symmetrize";
8034 ot->description = "Enforce symmetry (both form and topological) across an axis";
8035 ot->idname = "MESH_OT_symmetrize";
8036
8037 /* API callbacks. */
8040
8041 /* flags */
8043
8044 ot->prop = RNA_def_enum(ot->srna,
8045 "direction",
8048 "Direction",
8049 "Which sides to copy from and to");
8051 "threshold",
8052 1e-4f,
8053 0.0f,
8054 10.0f,
8055 "Threshold",
8056 "Limit for snap middle vertices to the axis center",
8057 1e-5f,
8058 0.1f);
8059}
8060
8062
8063/* -------------------------------------------------------------------- */
8066
8068{
8069 const float eps = 0.00001f;
8070 const float eps_sq = eps * eps;
8071 const float thresh = RNA_float_get(op->ptr, "threshold");
8072 const float fac = RNA_float_get(op->ptr, "factor");
8073 const bool use_center = RNA_boolean_get(op->ptr, "use_center");
8074 const bool use_topology = RNA_boolean_get(op->ptr, "use_topology");
8075 const int axis_dir = RNA_enum_get(op->ptr, "direction");
8076
8077 /* Vertices stats (total over all selected objects). */
8078 int totvertfound = 0, totvertmirr = 0, totvertfail = 0, totobjects = 0;
8079
8080 /* Axis. */
8081 int axis = axis_dir % 3;
8082 bool axis_sign = axis != axis_dir;
8083
8084 const Main *bmain = CTX_data_main(C);
8085 const Scene *scene = CTX_data_scene(C);
8086 ViewLayer *view_layer = CTX_data_view_layer(C);
8088 *bmain, scene, view_layer, CTX_wm_view3d(C));
8089
8090 for (Object *obedit : objects) {
8093
8094 if (bm->totvertsel == 0) {
8095 continue;
8096 }
8097
8099 continue;
8100 }
8101
8102 totobjects++;
8103
8104 /* Only allocate memory after checking whether to skip object. */
8106
8107 /* Vertex iter. */
8108 BMIter iter;
8109 BMVert *v;
8110 int i;
8111
8112 EDBM_verts_mirror_cache_begin_ex(em, bm, axis, true, true, false, use_topology, thresh, index);
8113
8115
8117
8118 BM_ITER_MESH_INDEX (v, &iter, bm, BM_VERTS_OF_MESH, i) {
8119 if ((BM_elem_flag_test(v, BM_ELEM_SELECT) != false) &&
8120 (BM_elem_flag_test(v, BM_ELEM_TAG) == false))
8121 {
8122 int i_mirr = index[i];
8123 if (i_mirr != -1) {
8124
8125 BMVert *v_mirr = BM_vert_at_index(bm, index[i]);
8126
8127 if (v != v_mirr) {
8128 float co[3], co_mirr[3];
8129
8130 if ((v->co[axis] > v_mirr->co[axis]) == axis_sign) {
8131 std::swap(v, v_mirr);
8132 }
8133
8134 copy_v3_v3(co_mirr, v_mirr->co);
8135 co_mirr[axis] *= -1.0f;
8136
8137 if (len_squared_v3v3(v->co, co_mirr) > eps_sq) {
8138 totvertmirr++;
8139 }
8140
8141 interp_v3_v3v3(co, v->co, co_mirr, fac);
8142
8143 copy_v3_v3(v->co, co);
8144
8145 co[axis] *= -1.0f;
8146 copy_v3_v3(v_mirr->co, co);
8147
8150 totvertfound++;
8151 }
8152 else {
8153 if (use_center) {
8154
8155 if (fabsf(v->co[axis]) > eps) {
8156 totvertmirr++;
8157 }
8158
8159 v->co[axis] = 0.0f;
8160 }
8162 totvertfound++;
8163 }
8164 }
8165 else {
8166 totvertfail++;
8167 }
8168 }
8169 }
8170 EDBMUpdate_Params params{};
8171 params.calc_looptris = false;
8172 params.calc_normals = true;
8173 params.is_destructive = false;
8174 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
8175
8176 /* No need to end cache, just free the array. */
8177 MEM_delete(index);
8178 }
8179
8180 if (totvertfail) {
8181 BKE_reportf(op->reports,
8183 "%d already symmetrical, %d pairs mirrored, %d failed",
8184 totvertfound - totvertmirr,
8185 totvertmirr,
8186 totvertfail);
8187 }
8188 else if (totobjects) {
8189 BKE_reportf(op->reports,
8190 RPT_INFO,
8191 "%d already symmetrical, %d pairs mirrored",
8192 totvertfound - totvertmirr,
8193 totvertmirr);
8194 }
8195
8196 return totobjects ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
8197}
8198
8200{
8201 /* identifiers */
8202 ot->name = "Snap to Symmetry";
8203 ot->description = "Snap vertex pairs to their mirrored locations";
8204 ot->idname = "MESH_OT_symmetry_snap";
8205
8206 /* API callbacks. */
8209
8210 /* flags */
8212
8213 ot->prop = RNA_def_enum(ot->srna,
8214 "direction",
8217 "Direction",
8218 "Which sides to copy from and to");
8220 "threshold",
8221 0.05f,
8222 0.0f,
8223 10.0f,
8224 "Threshold",
8225 "Distance within which matching vertices are searched",
8226 1e-4f,
8227 1.0f);
8228 RNA_def_float(ot->srna,
8229 "factor",
8230 0.5f,
8231 0.0f,
8232 1.0f,
8233 "Factor",
8234 "Mix factor of the locations of the vertices",
8235 0.0f,
8236 1.0f);
8238 ot->srna, "use_center", true, "Center", "Snap middle vertices to the axis center");
8240 "use_topology",
8241 false,
8242 "Topology Mirror",
8243 "Use topology to find mirrored vertices instead of spatial proximity");
8244}
8245
8247
8248#if defined(WITH_FREESTYLE)
8249
8250/* -------------------------------------------------------------------- */
8253
8254static wmOperatorStatus edbm_mark_freestyle_edge_exec(bContext *C, wmOperator *op)
8255{
8256 BMEdge *eed;
8257 BMIter iter;
8258 const bool clear = RNA_boolean_get(op->ptr, "clear");
8259 const Main *bmain = CTX_data_main(C);
8260 const Scene *scene = CTX_data_scene(C);
8261 ViewLayer *view_layer = CTX_data_view_layer(C);
8262
8263 const Vector<Object *> objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(
8264 *bmain, scene, view_layer, CTX_wm_view3d(C));
8265 for (Object *obedit : objects) {
8266 BMesh *bm = BKE_editmesh_bmesh_get_for_write(obedit);
8267
8268 if (bm->totedgesel == 0) {
8269 continue;
8270 }
8271
8272 BM_data_layer_ensure_named(bm, &bm->edata, CD_PROP_BOOL, "freestyle_edge");
8273 const int offset = CustomData_get_offset_named(&bm->edata, CD_PROP_BOOL, "freestyle_edge");
8274 if (offset == -1) {
8275 continue;
8276 }
8277
8278 if (clear) {
8279 BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
8281 BM_ELEM_CD_SET_BOOL(eed, offset, false);
8282 }
8283 }
8284 }
8285 else {
8286 BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
8288 BM_ELEM_CD_SET_BOOL(eed, offset, true);
8289 }
8290 }
8291 }
8292
8294 WM_event_add_notifier(C, NC_GEOM | ND_DATA, obedit->data);
8295 }
8296
8297 return OPERATOR_FINISHED;
8298}
8299
8300void MESH_OT_mark_freestyle_edge(wmOperatorType *ot)
8301{
8302 PropertyRNA *prop;
8303
8304 /* identifiers */
8305 ot->name = "Mark Freestyle Edge";
8306 ot->description = "(Un)mark selected edges as Freestyle feature edges";
8307 ot->idname = "MESH_OT_mark_freestyle_edge";
8308
8309 /* API callbacks. */
8310 ot->exec = edbm_mark_freestyle_edge_exec;
8311 ot->poll = ED_operator_editmesh;
8312
8313 /* flags */
8314 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
8315
8316 prop = RNA_def_boolean(ot->srna, "clear", false, "Clear", "");
8318}
8319
8321
8322/* -------------------------------------------------------------------- */
8325
8326static wmOperatorStatus edbm_mark_freestyle_face_exec(bContext *C, wmOperator *op)
8327{
8328 BMFace *efa;
8329 BMIter iter;
8330 const bool clear = RNA_boolean_get(op->ptr, "clear");
8331 const Main *bmain = CTX_data_main(C);
8332 const Scene *scene = CTX_data_scene(C);
8333 ViewLayer *view_layer = CTX_data_view_layer(C);
8334
8336 *bmain, scene, view_layer, CTX_wm_view3d(C));
8337 for (Object *obedit : objects) {
8339 if (bm->totfacesel == 0) {
8340 continue;
8341 }
8342
8343 BM_data_layer_ensure_named(bm, &bm->pdata, CD_PROP_BOOL, "freestyle_face");
8344 const int offset = CustomData_get_offset_named(&bm->pdata, CD_PROP_BOOL, "freestyle_face");
8345 if (offset == -1) {
8346 continue;
8347 }
8348
8349 if (clear) {
8350 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
8352 BM_ELEM_CD_SET_BOOL(efa, offset, false);
8353 }
8354 }
8355 }
8356 else {
8357 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
8359 BM_ELEM_CD_SET_BOOL(efa, offset, true);
8360 }
8361 }
8362 }
8363
8365 WM_event_add_notifier(C, NC_GEOM | ND_DATA, obedit->data);
8366 }
8367
8368 return OPERATOR_FINISHED;
8369}
8370
8371void MESH_OT_mark_freestyle_face(wmOperatorType *ot)
8372{
8373 PropertyRNA *prop;
8374
8375 /* identifiers */
8376 ot->name = "Mark Freestyle Face";
8377 ot->description = "(Un)mark selected faces for exclusion from Freestyle feature edge detection";
8378 ot->idname = "MESH_OT_mark_freestyle_face";
8379
8380 /* API callbacks. */
8381 ot->exec = edbm_mark_freestyle_face_exec;
8382 ot->poll = ED_operator_editmesh;
8383
8384 /* flags */
8385 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
8386
8387 prop = RNA_def_boolean(ot->srna, "clear", false, "Clear", "");
8389}
8390
8392
8393#endif /* WITH_FREESTYLE */
8394
8395/* -------------------------------------------------------------------- */
8398
8399/* NOTE: these defines are saved in keymap files, do not change values but just add new ones */
8400/* NOTE: We could add more here, like e.g. a switch between local or global coordinates of target,
8401 * use number-input to type in explicit vector values. */
8402enum {
8403 /* Generic commands. */
8406
8407 /* Point To operator. */
8412
8418};
8419
8421{
8422 static const EnumPropertyItem modal_items[] = {
8423 {EDBM_CLNOR_MODAL_CANCEL, "CANCEL", 0, "Cancel", ""},
8424 {EDBM_CLNOR_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
8425
8426 /* Point To operator. */
8427 {EDBM_CLNOR_MODAL_POINTTO_RESET, "RESET", 0, "Reset", "Reset normals to initial ones"},
8429 "INVERT",
8430 0,
8431 "Invert",
8432 "Toggle inversion of affected normals"},
8434 "SPHERIZE",
8435 0,
8436 "Spherize",
8437 "Interpolate between new and original normals"},
8438 {EDBM_CLNOR_MODAL_POINTTO_ALIGN, "ALIGN", 0, "Align", "Make all affected normals parallel"},
8439
8441 "USE_MOUSE",
8442 0,
8443 "Use Mouse",
8444 "Follow mouse cursor position"},
8446 "USE_PIVOT",
8447 0,
8448 "Use Pivot",
8449 "Use current rotation/scaling pivot point coordinates"},
8451 "USE_OBJECT",
8452 0,
8453 "Use Object",
8454 "Use current edited object's location"},
8456 "SET_USE_3DCURSOR",
8457 0,
8458 "Set and Use 3D Cursor",
8459 "Set new 3D cursor position and use it"},
8461 "SET_USE_SELECTED",
8462 0,
8463 "Select and Use Mesh Item",
8464 "Select new active mesh element and use its location"},
8465 {0, nullptr, 0, nullptr, nullptr},
8466 };
8467 static const char *keymap_name = "Custom Normals Modal Map";
8468
8469 wmKeyMap *keymap = WM_modalkeymap_find(keyconf, keymap_name);
8470
8471 /* We only need to add map once */
8472 if (keymap && keymap->modal_items) {
8473 return nullptr;
8474 }
8475
8476 keymap = WM_modalkeymap_ensure(keyconf, keymap_name, modal_items);
8477
8478 WM_modalkeymap_assign(keymap, "MESH_OT_point_normals");
8479
8480 return keymap;
8481}
8482
8483#define CLNORS_VALID_VEC_LEN (1e-4f)
8484
8486
8487/* -------------------------------------------------------------------- */
8490
8491enum {
8494};
8495
8498 "COORDINATES",
8499 0,
8500 "Coordinates",
8501 "Use static coordinates (defined by various means)"},
8502 {EDBM_CLNOR_POINTTO_MODE_MOUSE, "MOUSE", 0, "Mouse", "Follow mouse cursor"},
8503 {0, nullptr, 0, nullptr, nullptr},
8504};
8505
8506/* Initialize loop normal data */
8508{
8509 Object *obedit = CTX_data_edit_object(C);
8513
8514 op->customdata = lnors_ed_arr;
8515
8516 return (lnors_ed_arr->totloop != 0);
8517}
8518
8520{
8521 if (op->customdata != nullptr) {
8522 return true;
8523 }
8524 return point_normals_init(C, op);
8525}
8526
8528{
8529 if (op->customdata != nullptr) {
8530 BMLoopNorEditDataArray *lnors_ed_arr = static_cast<BMLoopNorEditDataArray *>(op->customdata);
8532 op->customdata = nullptr;
8533 }
8534}
8535
8537{
8539 ED_workspace_status_text(C, nullptr);
8540}
8541
8543{
8544 WorkspaceStatus status(C);
8545
8546 status.opmodal(IFACE_("Confirm"), op->type, EDBM_CLNOR_MODAL_CONFIRM);
8547 status.opmodal(IFACE_("Cancel"), op->type, EDBM_CLNOR_MODAL_CANCEL);
8548 status.opmodal(IFACE_("Reset"), op->type, EDBM_CLNOR_MODAL_POINTTO_RESET);
8549
8550 status.opmodal(IFACE_("Invert"),
8551 op->type,
8553 RNA_boolean_get(op->ptr, "invert"));
8554 status.opmodal(IFACE_("Spherize"),
8555 op->type,
8557 RNA_boolean_get(op->ptr, "spherize"));
8558 status.opmodal(IFACE_("Align"),
8559 op->type,
8561 RNA_boolean_get(op->ptr, "align"));
8562
8563 status.opmodal(IFACE_("Use mouse"),
8564 op->type,
8567
8568 status.opmodal(IFACE_("Use Pivot"), op->type, EDBM_CLNOR_MODAL_POINTTO_USE_PIVOT);
8569 status.opmodal(IFACE_("Use Object"), op->type, EDBM_CLNOR_MODAL_POINTTO_USE_OBJECT);
8570 status.opmodal(
8571 IFACE_("Set and use 3D cursor"), op->type, EDBM_CLNOR_MODAL_POINTTO_SET_USE_3DCURSOR);
8572 status.opmodal(
8573 IFACE_("Select and use mesh item"), op->type, EDBM_CLNOR_MODAL_POINTTO_SET_USE_SELECTED);
8574}
8575
8576/* TODO: move that to generic function in BMesh? */
8577static void bmesh_selected_verts_center_calc(BMesh *bm, float *r_center)
8578{
8579 BMVert *v;
8580 BMIter viter;
8581 int i = 0;
8582
8583 zero_v3(r_center);
8584 BM_ITER_MESH (v, &viter, bm, BM_VERTS_OF_MESH) {
8586 add_v3_v3(r_center, v->co);
8587 i++;
8588 }
8589 }
8590 mul_v3_fl(r_center, 1.0f / float(i));
8591}
8592
8593static void point_normals_apply(bContext *C, wmOperator *op, float target[3], const bool do_reset)
8594{
8595 Object *obedit = CTX_data_edit_object(C);
8597 BMLoopNorEditDataArray *lnors_ed_arr = static_cast<BMLoopNorEditDataArray *>(op->customdata);
8598
8599 const bool do_invert = RNA_boolean_get(op->ptr, "invert");
8600 const bool do_spherize = RNA_boolean_get(op->ptr, "spherize");
8601 const bool do_align = RNA_boolean_get(op->ptr, "align");
8602 float center[3];
8603
8604 if (do_align && !do_reset) {
8606 }
8607
8608 sub_v3_v3(target, obedit->loc); /* Move target to local coordinates. */
8609
8610 BMLoopNorEditData *lnor_ed = lnors_ed_arr->lnor_editdata;
8611 for (int i = 0; i < lnors_ed_arr->totloop; i++, lnor_ed++) {
8612 if (do_reset) {
8613 copy_v3_v3(lnor_ed->nloc, lnor_ed->niloc);
8614 }
8615 else if (do_spherize) {
8616 /* Note that this is *not* real spherical interpolation.
8617 * Probably good enough in this case though? */
8618 const float strength = RNA_float_get(op->ptr, "spherize_strength");
8619 float spherized_normal[3];
8620
8621 sub_v3_v3v3(spherized_normal, target, lnor_ed->loc);
8622
8623 /* otherwise, multiplication by strength is meaningless... */
8624 normalize_v3(spherized_normal);
8625
8626 mul_v3_fl(spherized_normal, strength);
8627 mul_v3_v3fl(lnor_ed->nloc, lnor_ed->niloc, 1.0f - strength);
8628 add_v3_v3(lnor_ed->nloc, spherized_normal);
8629 }
8630 else if (do_align) {
8631 sub_v3_v3v3(lnor_ed->nloc, target, center);
8632 }
8633 else {
8634 sub_v3_v3v3(lnor_ed->nloc, target, lnor_ed->loc);
8635 }
8636
8637 if (do_invert && !do_reset) {
8638 negate_v3(lnor_ed->nloc);
8639 }
8640 if (normalize_v3(lnor_ed->nloc) >= CLNORS_VALID_VEC_LEN) {
8642 bm->lnor_spacearr->lspacearr[lnor_ed->loop_index], lnor_ed->nloc, lnor_ed->clnors_data);
8643 }
8644 }
8645}
8646
8648{
8649 /* As this operator passes events through, we can't be sure the user didn't exit edit-mode.
8650 * or performed some other operation. */
8651 if (!WM_operator_poll(C, op->type)) {
8653 return OPERATOR_CANCELLED;
8654 }
8655
8656 View3D *v3d = CTX_wm_view3d(C);
8657 Scene *scene = CTX_data_scene(C);
8658 Object *obedit = CTX_data_edit_object(C);
8660
8661 float target[3];
8662
8664 int mode = RNA_enum_get(op->ptr, "mode");
8665 int new_mode = mode;
8666 bool force_mousemove = false;
8667 bool do_reset = false;
8668
8669 PropertyRNA *prop_target = RNA_struct_find_property(op->ptr, "target_location");
8670
8671 if (event->type == EVT_MODAL_MAP) {
8672 switch (event->val) {
8674 RNA_property_float_get_array(op->ptr, prop_target, target);
8676 break;
8677
8679 do_reset = true;
8681 break;
8682
8684 do_reset = true;
8686 break;
8687
8689 PropertyRNA *prop_invert = RNA_struct_find_property(op->ptr, "invert");
8691 op->ptr, prop_invert, !RNA_property_boolean_get(op->ptr, prop_invert));
8692 RNA_property_float_get_array(op->ptr, prop_target, target);
8694 break;
8695 }
8696
8698 PropertyRNA *prop_spherize = RNA_struct_find_property(op->ptr, "spherize");
8700 op->ptr, prop_spherize, !RNA_property_boolean_get(op->ptr, prop_spherize));
8701 RNA_property_float_get_array(op->ptr, prop_target, target);
8703 break;
8704 }
8705
8707 PropertyRNA *prop_align = RNA_struct_find_property(op->ptr, "align");
8709 op->ptr, prop_align, !RNA_property_boolean_get(op->ptr, prop_align));
8710 RNA_property_float_get_array(op->ptr, prop_target, target);
8712 break;
8713 }
8714
8717 /* We want to immediately update to mouse cursor position... */
8718 force_mousemove = true;
8720 break;
8721
8724 copy_v3_v3(target, obedit->loc);
8726 break;
8727
8731 copy_v3_v3(target, scene->cursor.location);
8733 break;
8734
8738 SelectPick_Params params{};
8739 params.sel_op = SEL_OP_SET;
8740 if (EDBM_select_pick(C, event->mval, params)) {
8741 /* Point to newly selected active. */
8742 ed::object::calc_active_center_for_editmode(obedit, false, target);
8743
8744 add_v3_v3(target, obedit->loc);
8746 }
8747 break;
8748 }
8751 switch (scene->toolsettings->transform_pivot_point) {
8752 case V3D_AROUND_CENTER_BOUNDS: /* calculateCenterBound */
8753 {
8754 BMVert *v;
8755 BMIter viter;
8756 float min[3], max[3];
8757 int i = 0;
8758
8759 BM_ITER_MESH (v, &viter, bm, BM_VERTS_OF_MESH) {
8761 if (i) {
8763 }
8764 else {
8765 copy_v3_v3(min, v->co);
8766 copy_v3_v3(max, v->co);
8767 }
8768 i++;
8769 }
8770 }
8771 mid_v3_v3v3(target, min, max);
8772 add_v3_v3(target, obedit->loc);
8773 break;
8774 }
8775
8778 add_v3_v3(target, obedit->loc);
8779 break;
8780 }
8781
8782 case V3D_AROUND_CURSOR:
8783 copy_v3_v3(target, scene->cursor.location);
8784 break;
8785
8786 case V3D_AROUND_ACTIVE:
8787 if (!ed::object::calc_active_center_for_editmode(obedit, false, target)) {
8788 zero_v3(target);
8789 }
8790 add_v3_v3(target, obedit->loc);
8791 break;
8792
8793 default:
8794 BKE_report(op->reports, RPT_WARNING, "Does not support Individual Origins as pivot");
8795 copy_v3_v3(target, obedit->loc);
8796 }
8798 break;
8799 }
8800 default:
8801 break;
8802 }
8803 }
8804
8805 if (new_mode != mode) {
8806 mode = new_mode;
8807 RNA_enum_set(op->ptr, "mode", mode);
8808 }
8809
8810 /* Only handle mouse-move event in case we are in mouse mode. */
8811 if (event->type == MOUSEMOVE || force_mousemove) {
8812 if (mode == EDBM_CLNOR_POINTTO_MODE_MOUSE) {
8813 ARegion *region = CTX_wm_region(C);
8814 float center[3];
8815
8817
8818 ED_view3d_win_to_3d_int(v3d, region, center, event->mval, target);
8819
8821 }
8822 }
8823
8824 if (ret != OPERATOR_PASS_THROUGH) {
8826 RNA_property_float_set_array(op->ptr, prop_target, target);
8827 }
8828
8829 if (point_normals_ensure(C, op)) {
8830 point_normals_apply(C, op, target, do_reset);
8831 EDBMUpdate_Params params{};
8832 params.calc_looptris = true;
8833 params.calc_normals = false;
8834 params.is_destructive = false;
8835 /* Recheck booleans. */
8836 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
8837
8839 }
8840 else {
8842 }
8843 }
8844
8847 }
8848
8849 /* Keep the normal data active while the operator is running,
8850 * and only free it when the operation ends or is canceled. */
8853 }
8854 return ret;
8855}
8856
8858 wmOperator *op,
8859 const wmEvent * /*event*/)
8860{
8861 if (!point_normals_init(C, op)) {
8863 return OPERATOR_CANCELLED;
8864 }
8865
8867
8869
8872}
8873
8874/* TODO: make this work on multiple objects at once */
8876{
8877 Object *obedit = CTX_data_edit_object(C);
8878
8879 if (!point_normals_init(C, op)) {
8881 return OPERATOR_CANCELLED;
8882 }
8883
8884 /* Note that 'mode' is ignored in exec case,
8885 * we directly use vector stored in target_location, whatever that is. */
8886
8887 float target[3];
8888 RNA_float_get_array(op->ptr, "target_location", target);
8889
8890 point_normals_apply(C, op, target, false);
8891
8892 EDBMUpdate_Params params{};
8893 params.calc_looptris = true;
8894 params.calc_normals = false;
8895 params.is_destructive = false;
8896 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
8898
8899 return OPERATOR_FINISHED;
8900}
8901
8902static bool point_normals_draw_check_prop(PointerRNA *ptr, PropertyRNA *prop, void * /*user_data*/)
8903{
8904 const char *prop_id = RNA_property_identifier(prop);
8905
8906 /* Only show strength option if spherize is enabled. */
8907 if (STREQ(prop_id, "spherize_strength")) {
8908 return RNA_boolean_get(ptr, "spherize");
8909 }
8910
8911 /* Else, show it! */
8912 return true;
8913}
8914
8916{
8917 ui::Layout &layout = *op->layout;
8919
8921
8922 layout.use_property_split_set(true);
8923
8924 /* Main auto-draw call */
8925 uiDefAutoButsRNA(&layout,
8926 &ptr,
8928 nullptr,
8929 nullptr,
8931 false);
8932}
8933
8935{
8936 /* identifiers */
8937 ot->name = "Point Normals to Target";
8938 ot->description = "Point selected custom normals to specified Target";
8939 ot->idname = "MESH_OT_point_normals";
8940
8941 /* API callbacks. */
8948
8949 /* flags */
8951
8952 ot->prop = RNA_def_enum(ot->srna,
8953 "mode",
8956 "Mode",
8957 "How to define coordinates to point custom normals to");
8959
8960 RNA_def_boolean(ot->srna, "invert", false, "Invert", "Invert affected normals");
8961
8962 RNA_def_boolean(ot->srna, "align", false, "Align", "Make all affected normals parallel");
8963
8965 "target_location",
8966 3,
8967 nullptr,
8968 -FLT_MAX,
8969 FLT_MAX,
8970 "Target",
8971 "Target location to which normals will point",
8972 -1000.0f,
8973 1000.0f);
8974
8976 ot->srna, "spherize", false, "Spherize", "Interpolate between original and new normals");
8977
8978 RNA_def_float(ot->srna,
8979 "spherize_strength",
8980 0.1,
8981 0.0f,
8982 1.0f,
8983 "Spherize Strength",
8984 "Ratio of spherized normal to original normal",
8985 0.0f,
8986 1.0f);
8987}
8988
8990
8991/* -------------------------------------------------------------------- */
8994
8995static void normals_merge(BMesh *bm, BMLoopNorEditDataArray *lnors_ed_arr)
8996{
8997 BMLoopNorEditData *lnor_ed = lnors_ed_arr->lnor_editdata;
8998
8999 BLI_SMALLSTACK_DECLARE(clnors, short *);
9000
9002
9004
9005 for (int i = 0; i < lnors_ed_arr->totloop; i++, lnor_ed++) {
9007
9008 if (BM_elem_flag_test(lnor_ed->loop, BM_ELEM_TAG)) {
9009 continue;
9010 }
9011
9012 MLoopNorSpace *lnor_space = bm->lnor_spacearr->lspacearr[lnor_ed->loop_index];
9013
9014 if ((lnor_space->flags & MLNOR_SPACE_IS_SINGLE) == 0) {
9015 LinkNode *loops = lnor_space->loops;
9016 float avg_normal[3] = {0.0f, 0.0f, 0.0f};
9017 short *clnors_data;
9018
9019 for (; loops; loops = loops->next) {
9020 BMLoop *l = static_cast<BMLoop *>(loops->link);
9021 const int loop_index = BM_elem_index_get(l);
9022
9023 BMLoopNorEditData *lnor_ed_tmp = lnors_ed_arr->lidx_to_lnor_editdata[loop_index];
9024 BLI_assert(lnor_ed_tmp->loop_index == loop_index && lnor_ed_tmp->loop == l);
9025 add_v3_v3(avg_normal, lnor_ed_tmp->nloc);
9026 BLI_SMALLSTACK_PUSH(clnors, lnor_ed_tmp->clnors_data);
9028 }
9029 if (normalize_v3(avg_normal) < CLNORS_VALID_VEC_LEN) {
9030 /* If avg normal is nearly 0, set clnor to default value. */
9031 zero_v3(avg_normal);
9032 }
9033 while ((clnors_data = static_cast<short *>(BLI_SMALLSTACK_POP(clnors)))) {
9034 BKE_lnor_space_custom_normal_to_data(lnor_space, avg_normal, clnors_data);
9035 }
9036 }
9037 }
9038}
9039
9041{
9042 BMFace *f;
9043 BMLoop *l, *l_curr, *l_first;
9044 BMIter fiter;
9045
9047
9049
9050 BLI_SMALLSTACK_DECLARE(loop_stack, BMLoop *);
9051
9053 const int cd_clnors_offset = CustomData_get_offset_named(
9054 &bm->ldata, CD_PROP_INT16_2D, "custom_normal");
9055 BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
9057
9058 l_curr = l_first = BM_FACE_FIRST_LOOP(f);
9059 do {
9060 if (BM_elem_flag_test(l_curr->v, BM_ELEM_SELECT) &&
9061 (!BM_elem_flag_test(l_curr->e, BM_ELEM_TAG) ||
9063 {
9064 if (!BM_elem_flag_test(l_curr->e, BM_ELEM_TAG) &&
9065 !BM_elem_flag_test(l_curr->prev->e, BM_ELEM_TAG))
9066 {
9067 const int loop_index = BM_elem_index_get(l_curr);
9068 short *clnors = static_cast<short *>(BM_ELEM_CD_GET_VOID_P(l_curr, cd_clnors_offset));
9070 bm->lnor_spacearr->lspacearr[loop_index], f->no, clnors);
9071 }
9072 else {
9073 BMVert *v_pivot = l_curr->v;
9074 UNUSED_VARS_NDEBUG(v_pivot);
9075 BMEdge *e_next;
9076 const BMEdge *e_org = l_curr->e;
9077 BMLoop *lfan_pivot, *lfan_pivot_next;
9078
9079 lfan_pivot = l_curr;
9080 e_next = lfan_pivot->e;
9081 float avg_normal[3] = {0.0f};
9082
9083 while (true) {
9084 lfan_pivot_next = BM_vert_step_fan_loop(lfan_pivot, &e_next);
9085 if (lfan_pivot_next) {
9086 BLI_assert(lfan_pivot_next->v == v_pivot);
9087 }
9088 else {
9089 e_next = (lfan_pivot->e == e_next) ? lfan_pivot->prev->e : lfan_pivot->e;
9090 }
9091
9092 BLI_SMALLSTACK_PUSH(loop_stack, lfan_pivot);
9093 add_v3_v3(avg_normal, lfan_pivot->f->no);
9094
9095 if (!BM_elem_flag_test(e_next, BM_ELEM_TAG) || (e_next == e_org)) {
9096 break;
9097 }
9098 lfan_pivot = lfan_pivot_next;
9099 }
9100 if (normalize_v3(avg_normal) < CLNORS_VALID_VEC_LEN) {
9101 /* If avg normal is nearly 0, set clnor to default value. */
9102 zero_v3(avg_normal);
9103 }
9104 while ((l = static_cast<BMLoop *>(BLI_SMALLSTACK_POP(loop_stack)))) {
9105 const int l_index = BM_elem_index_get(l);
9106 short *clnors = static_cast<short *>(BM_ELEM_CD_GET_VOID_P(l, cd_clnors_offset));
9108 bm->lnor_spacearr->lspacearr[l_index], avg_normal, clnors);
9109 }
9110 }
9111 }
9112 } while ((l_curr = l_curr->next) != l_first);
9113 }
9114}
9115
9116static wmOperatorStatus normals_split_merge(bContext *C, const bool do_merge)
9117{
9118 const Main *bmain = CTX_data_main(C);
9119 const Scene *scene = CTX_data_scene(C);
9120 ViewLayer *view_layer = CTX_data_view_layer(C);
9122 *bmain, scene, view_layer, CTX_wm_view3d(C));
9123
9124 for (Object *obedit : objects) {
9126 BMEdge *e;
9127 BMIter eiter;
9128
9130
9131 /* Note that we need temp lnor editing data for all loops of all affected vertices, since by
9132 * setting some faces/edges as smooth we are going to change clnors spaces... See also #65809.
9133 */
9134 BMLoopNorEditDataArray *lnors_ed_arr = do_merge ?
9136 nullptr;
9137
9138 mesh_set_smooth_faces(bm, do_merge);
9139
9140 BM_ITER_MESH (e, &eiter, bm, BM_EDGES_OF_MESH) {
9142 BM_elem_flag_set(e, BM_ELEM_SMOOTH, do_merge);
9143 }
9144 }
9145
9148
9149 if (do_merge) {
9150 normals_merge(bm, lnors_ed_arr);
9151 }
9152 else {
9154 }
9155
9156 if (lnors_ed_arr) {
9158 }
9159
9160 EDBMUpdate_Params params{};
9161 params.calc_looptris = true;
9162 params.calc_normals = false;
9163 params.is_destructive = false;
9164 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
9165 }
9166
9167 return OPERATOR_FINISHED;
9168}
9169
9171{
9172 return normals_split_merge(C, true);
9173}
9174
9176{
9177 /* identifiers */
9178 ot->name = "Merge Normals";
9179 ot->description = "Merge custom normals of selected vertices";
9180 ot->idname = "MESH_OT_merge_normals";
9181
9182 /* API callbacks. */
9185
9186 /* flags */
9188}
9189
9191{
9192 return normals_split_merge(C, false);
9193}
9194
9196{
9197 /* identifiers */
9198 ot->name = "Split Normals";
9199 ot->description = "Split custom normals of selected vertices";
9200 ot->idname = "MESH_OT_split_normals";
9201
9202 /* API callbacks. */
9205
9206 /* flags */
9208}
9209
9211
9212/* -------------------------------------------------------------------- */
9215
9216enum {
9220};
9221
9224 "CUSTOM_NORMAL",
9225 0,
9226 "Custom Normal",
9227 "Take average of vertex normals"},
9229 "FACE_AREA",
9230 0,
9231 "Face Area",
9232 "Set all vertex normals by face area"},
9234 "CORNER_ANGLE",
9235 0,
9236 "Corner Angle",
9237 "Set all vertex normals by corner angle"},
9238 {0, nullptr, 0, nullptr, nullptr},
9239};
9240
9242{
9243 const Main *bmain = CTX_data_main(C);
9244 const Scene *scene = CTX_data_scene(C);
9245 ViewLayer *view_layer = CTX_data_view_layer(C);
9247 *bmain, scene, view_layer, CTX_wm_view3d(C));
9248 const int average_type = RNA_enum_get(op->ptr, "average_type");
9249 const float absweight = float(RNA_int_get(op->ptr, "weight"));
9250 const float threshold = RNA_float_get(op->ptr, "threshold");
9251
9252 HeapSimple *loop_weight = BLI_heapsimple_new();
9253 BLI_SMALLSTACK_DECLARE(loop_stack, BMLoop *);
9254
9255 for (uint ob_index = 0; ob_index < objects.size(); ob_index++) {
9257 BLI_assert(BLI_heapsimple_is_empty(loop_weight));
9258
9259 Object *obedit = objects[ob_index];
9261 BMFace *f;
9262 BMLoop *l, *l_curr, *l_first;
9263 BMIter fiter;
9264
9267
9269 const int cd_clnors_offset = CustomData_get_offset_named(
9270 &bm->ldata, CD_PROP_INT16_2D, "custom_normal");
9271
9272 float weight = absweight / 50.0f;
9273 if (absweight == 100.0f) {
9274 weight = float(SHRT_MAX);
9275 }
9276 else if (absweight == 1.0f) {
9277 weight = 1 / float(SHRT_MAX);
9278 }
9279 else if ((weight - 1) * 25 > 1) {
9280 weight = (weight - 1) * 25;
9281 }
9282
9284
9285 BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
9286 l_curr = l_first = BM_FACE_FIRST_LOOP(f);
9287 do {
9288 if (BM_elem_flag_test(l_curr->v, BM_ELEM_SELECT) &&
9289 (!BM_elem_flag_test(l_curr->e, BM_ELEM_TAG) ||
9291 {
9292 if (!BM_elem_flag_test(l_curr->e, BM_ELEM_TAG) &&
9293 !BM_elem_flag_test(l_curr->prev->e, BM_ELEM_TAG))
9294 {
9295 const int loop_index = BM_elem_index_get(l_curr);
9296 short *clnors = static_cast<short *>(BM_ELEM_CD_GET_VOID_P(l_curr, cd_clnors_offset));
9298 bm->lnor_spacearr->lspacearr[loop_index], f->no, clnors);
9299 }
9300 else {
9301 BMVert *v_pivot = l_curr->v;
9302 UNUSED_VARS_NDEBUG(v_pivot);
9303 BMEdge *e_next;
9304 const BMEdge *e_org = l_curr->e;
9305 BMLoop *lfan_pivot, *lfan_pivot_next;
9306
9307 lfan_pivot = l_curr;
9308 e_next = lfan_pivot->e;
9309
9310 while (true) {
9311 lfan_pivot_next = BM_vert_step_fan_loop(lfan_pivot, &e_next);
9312 if (lfan_pivot_next) {
9313 BLI_assert(lfan_pivot_next->v == v_pivot);
9314 }
9315 else {
9316 e_next = (lfan_pivot->e == e_next) ? lfan_pivot->prev->e : lfan_pivot->e;
9317 }
9318
9319 float val = 1.0f;
9320 if (average_type == EDBM_CLNOR_AVERAGE_FACE_AREA) {
9321 val = 1.0f / BM_face_calc_area(lfan_pivot->f);
9322 }
9323 else if (average_type == EDBM_CLNOR_AVERAGE_ANGLE) {
9324 val = 1.0f / BM_loop_calc_face_angle(lfan_pivot);
9325 }
9326
9327 BLI_heapsimple_insert(loop_weight, val, lfan_pivot);
9328
9329 if (!BM_elem_flag_test(e_next, BM_ELEM_TAG) || (e_next == e_org)) {
9330 break;
9331 }
9332 lfan_pivot = lfan_pivot_next;
9333 }
9334
9335 float wnor[3], avg_normal[3] = {0.0f}, count = 0;
9336 float val = BLI_heapsimple_top_value(loop_weight);
9337
9338 while (!BLI_heapsimple_is_empty(loop_weight)) {
9339 const float cur_val = BLI_heapsimple_top_value(loop_weight);
9340 if (!compare_ff(val, cur_val, threshold)) {
9341 count++;
9342 val = cur_val;
9343 }
9344 l = static_cast<BMLoop *>(BLI_heapsimple_pop_min(loop_weight));
9345 BLI_SMALLSTACK_PUSH(loop_stack, l);
9346
9347 const float n_weight = pow(weight, count);
9348
9349 if (average_type == EDBM_CLNOR_AVERAGE_LOOP) {
9350 const int l_index = BM_elem_index_get(l);
9351 short *clnors = static_cast<short *>(BM_ELEM_CD_GET_VOID_P(l, cd_clnors_offset));
9353 bm->lnor_spacearr->lspacearr[l_index], clnors, wnor);
9354 }
9355 else {
9356 copy_v3_v3(wnor, l->f->no);
9357 }
9358 mul_v3_fl(wnor, (1.0f / cur_val) * (1.0f / n_weight));
9359 add_v3_v3(avg_normal, wnor);
9360 }
9361
9362 if (normalize_v3(avg_normal) < CLNORS_VALID_VEC_LEN) {
9363 /* If avg normal is nearly 0, set clnor to default value. */
9364 zero_v3(avg_normal);
9365 }
9366 while ((l = static_cast<BMLoop *>(BLI_SMALLSTACK_POP(loop_stack)))) {
9367 const int l_index = BM_elem_index_get(l);
9368 short *clnors = static_cast<short *>(BM_ELEM_CD_GET_VOID_P(l, cd_clnors_offset));
9370 bm->lnor_spacearr->lspacearr[l_index], avg_normal, clnors);
9371 }
9372 }
9373 }
9374 } while ((l_curr = l_curr->next) != l_first);
9375 }
9376
9377 EDBMUpdate_Params params{};
9378 params.calc_looptris = true;
9379 params.calc_normals = false;
9380 params.is_destructive = false;
9381 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
9382 }
9383
9384 BLI_heapsimple_free(loop_weight, nullptr);
9385
9386 return OPERATOR_FINISHED;
9387}
9388
9390 PropertyRNA *prop,
9391 void * /*user_data*/)
9392{
9393 const char *prop_id = RNA_property_identifier(prop);
9394 const int average_type = RNA_enum_get(ptr, "average_type");
9395
9396 /* Only show weight/threshold options when not in loop average type. */
9397 const bool is_clor_average_loop = average_type == EDBM_CLNOR_AVERAGE_LOOP;
9398 if (STREQ(prop_id, "weight")) {
9399 return !is_clor_average_loop;
9400 }
9401 if (STREQ(prop_id, "threshold")) {
9402 return !is_clor_average_loop;
9403 }
9404
9405 /* Else, show it! */
9406 return true;
9407}
9408
9410{
9411 ui::Layout &layout = *op->layout;
9413
9415
9416 layout.use_property_split_set(true);
9417
9418 /* Main auto-draw call */
9419 uiDefAutoButsRNA(&layout,
9420 &ptr,
9422 nullptr,
9423 nullptr,
9425 false);
9426}
9427
9429{
9430 /* identifiers */
9431 ot->name = "Average Normals";
9432 ot->description = "Average custom normals of selected vertices";
9433 ot->idname = "MESH_OT_average_normals";
9434
9435 /* API callbacks. */
9439
9440 /* flags */
9442
9443 ot->prop = RNA_def_enum(ot->srna,
9444 "average_type",
9447 "Type",
9448 "Averaging method");
9449
9450 RNA_def_int(ot->srna, "weight", 50, 1, 100, "Weight", "Weight applied per face", 1, 100);
9451
9452 RNA_def_float(ot->srna,
9453 "threshold",
9454 0.01f,
9455 0,
9456 10,
9457 "Threshold",
9458 "Threshold value for different weights to be considered equal",
9459 0,
9460 5);
9461}
9462
9464
9465/* -------------------------------------------------------------------- */
9468
9469enum {
9475};
9476
9478 {EDBM_CLNOR_TOOLS_COPY, "COPY", 0, "Copy Normal", "Copy normal to the internal clipboard"},
9480 "PASTE",
9481 0,
9482 "Paste Normal",
9483 "Paste normal from the internal clipboard"},
9484 {EDBM_CLNOR_TOOLS_ADD, "ADD", 0, "Add Normal", "Add normal vector with selection"},
9486 "MULTIPLY",
9487 0,
9488 "Multiply Normal",
9489 "Multiply normal vector with selection"},
9491 "RESET",
9492 0,
9493 "Reset Normal",
9494 "Reset the internal clipboard and/or normal of selected element"},
9495 {0, nullptr, 0, nullptr, nullptr},
9496};
9497
9499{
9500 const Main *bmain = CTX_data_main(C);
9501 Scene *scene = CTX_data_scene(C);
9502 ViewLayer *view_layer = CTX_data_view_layer(C);
9504 *bmain, scene, view_layer, CTX_wm_view3d(C));
9505 const int mode = RNA_enum_get(op->ptr, "mode");
9506 const bool absolute = RNA_boolean_get(op->ptr, "absolute");
9507 float *normal_vector = scene->toolsettings->normal_vector;
9508 bool done_copy = false;
9509
9510 for (Object *obedit : objects) {
9512 if (bm->totloop == 0) {
9513 continue;
9514 }
9515
9518 BMLoopNorEditData *lnor_ed = lnors_ed_arr->lnor_editdata;
9519
9520 switch (mode) {
9522 if (bm->totfacesel == 0 && bm->totvertsel == 0) {
9524 continue;
9525 }
9526
9527 if (done_copy ||
9528 (bm->totfacesel != 1 && lnors_ed_arr->totloop != 1 && bm->totvertsel != 1))
9529 {
9530 BKE_report(op->reports,
9531 RPT_ERROR,
9532 "Can only copy one custom normal, vertex normal or face normal");
9534 continue;
9535 }
9536 if (lnors_ed_arr->totloop == 1) {
9537 copy_v3_v3(scene->toolsettings->normal_vector, lnors_ed_arr->lnor_editdata->nloc);
9538 done_copy = true;
9539 }
9540 else if (bm->totfacesel == 1) {
9541 BMFace *f;
9542 BMIter fiter;
9543 BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
9546 }
9547 }
9548 done_copy = true;
9549 }
9550 else if (lnors_ed_arr->totloop != 0) {
9551 /* 'Vertex' normal, i.e. common set of loop normals on the same vertex,
9552 * only if they are all the same. */
9553 bool are_same_lnors = true;
9554 for (int i = 0; i < lnors_ed_arr->totloop; i++, lnor_ed++) {
9555 if (!compare_v3v3(lnors_ed_arr->lnor_editdata->nloc, lnor_ed->nloc, 1e-4f)) {
9556 are_same_lnors = false;
9557 }
9558 }
9559 if (are_same_lnors) {
9560 copy_v3_v3(scene->toolsettings->normal_vector, lnors_ed_arr->lnor_editdata->nloc);
9561 done_copy = true;
9562 }
9563 }
9564 break;
9565
9567 if (!absolute) {
9568 if (normalize_v3(normal_vector) < CLNORS_VALID_VEC_LEN) {
9569 /* If normal is nearly 0, do nothing. */
9570 break;
9571 }
9572 }
9573 for (int i = 0; i < lnors_ed_arr->totloop; i++, lnor_ed++) {
9574 if (absolute) {
9575 float abs_normal[3];
9576 copy_v3_v3(abs_normal, lnor_ed->loc);
9577 negate_v3(abs_normal);
9578 add_v3_v3(abs_normal, normal_vector);
9579
9580 if (normalize_v3(abs_normal) < CLNORS_VALID_VEC_LEN) {
9581 /* If abs normal is nearly 0, set clnor to initial value. */
9582 copy_v3_v3(abs_normal, lnor_ed->niloc);
9583 }
9585 abs_normal,
9586 lnor_ed->clnors_data);
9587 }
9588 else {
9590 normal_vector,
9591 lnor_ed->clnors_data);
9592 }
9593 }
9594 break;
9595
9597 for (int i = 0; i < lnors_ed_arr->totloop; i++, lnor_ed++) {
9598 mul_v3_v3(lnor_ed->nloc, normal_vector);
9599
9600 if (normalize_v3(lnor_ed->nloc) < CLNORS_VALID_VEC_LEN) {
9601 /* If abs normal is nearly 0, set clnor to initial value. */
9602 copy_v3_v3(lnor_ed->nloc, lnor_ed->niloc);
9603 }
9605 lnor_ed->nloc,
9606 lnor_ed->clnors_data);
9607 }
9608 break;
9609
9611 for (int i = 0; i < lnors_ed_arr->totloop; i++, lnor_ed++) {
9612 add_v3_v3(lnor_ed->nloc, normal_vector);
9613
9614 if (normalize_v3(lnor_ed->nloc) < CLNORS_VALID_VEC_LEN) {
9615 /* If abs normal is nearly 0, set clnor to initial value. */
9616 copy_v3_v3(lnor_ed->nloc, lnor_ed->niloc);
9617 }
9619 lnor_ed->nloc,
9620 lnor_ed->clnors_data);
9621 }
9622 break;
9623
9625 zero_v3(normal_vector);
9626 for (int i = 0; i < lnors_ed_arr->totloop; i++, lnor_ed++) {
9628 normal_vector,
9629 lnor_ed->clnors_data);
9630 }
9631 break;
9632
9633 default:
9634 BLI_assert(0);
9635 break;
9636 }
9637
9639
9640 EDBMUpdate_Params params{};
9641 params.calc_looptris = true;
9642 params.calc_normals = false;
9643 params.is_destructive = false;
9644 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
9645 }
9646
9647 return OPERATOR_FINISHED;
9648}
9649
9650static bool normals_tools_draw_check_prop(PointerRNA *ptr, PropertyRNA *prop, void * /*user_data*/)
9651{
9652 const char *prop_id = RNA_property_identifier(prop);
9653 const int mode = RNA_enum_get(ptr, "mode");
9654
9655 /* Only show absolute option in paste mode. */
9656 if (STREQ(prop_id, "absolute")) {
9657 return (mode == EDBM_CLNOR_TOOLS_PASTE);
9658 }
9659
9660 /* Else, show it! */
9661 return true;
9662}
9663
9665{
9666 ui::Layout &layout = *op->layout;
9668
9670
9671 /* Main auto-draw call */
9672 uiDefAutoButsRNA(&layout,
9673 &ptr,
9675 nullptr,
9676 nullptr,
9678 false);
9679}
9680
9682{
9683 const int mode = RNA_enum_get(ptr, "mode");
9684 return mode == EDBM_CLNOR_TOOLS_PASTE;
9685}
9686
9688{
9689 /* identifiers */
9690 ot->name = "Normals Vector Tools";
9691 ot->description = "Custom normals tools using Normal Vector of UI";
9692 ot->idname = "MESH_OT_normals_tools";
9693
9694 /* API callbacks. */
9699
9700 /* flags */
9702
9703 ot->prop = RNA_def_enum(ot->srna,
9704 "mode",
9707 "Mode",
9708 "Mode of tools taking input from interface");
9710
9712 "absolute",
9713 false,
9714 "Absolute Coordinates",
9715 "Copy Absolute coordinates of Normal vector");
9716}
9717
9719
9720/* -------------------------------------------------------------------- */
9723
9725{
9726 const Main *bmain = CTX_data_main(C);
9727 const Scene *scene = CTX_data_scene(C);
9728 ViewLayer *view_layer = CTX_data_view_layer(C);
9730 *bmain, scene, view_layer, CTX_wm_view3d(C));
9731
9732 for (Object *obedit : objects) {
9734 if (bm->totfacesel == 0) {
9735 continue;
9736 }
9737
9738 BMFace *f;
9739 BMVert *v;
9740 BMEdge *e;
9741 BMLoop *l;
9742 BMIter fiter, viter, eiter, liter;
9743
9744 const bool keep_sharp = RNA_boolean_get(op->ptr, "keep_sharp");
9745
9747
9748 float (*vert_normals)[3] = MEM_new_array_uninitialized<float[3]>(bm->totvert, __func__);
9749 {
9750 int v_index;
9751 BM_ITER_MESH_INDEX (v, &viter, bm, BM_VERTS_OF_MESH, v_index) {
9752 BM_elem_index_set(v, v_index); /* set_inline */
9753 BM_vert_calc_normal_ex(v, BM_ELEM_SELECT, vert_normals[v_index]);
9754 }
9756 }
9757
9760 BLI_bitmap *loop_set = BLI_BITMAP_NEW(bm->totloop, __func__);
9761 const int cd_clnors_offset = CustomData_get_offset_named(
9762 &bm->ldata, CD_PROP_INT16_2D, "custom_normal");
9763
9764 BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
9765 BM_ITER_ELEM (e, &eiter, f, BM_EDGES_OF_FACE) {
9766 if (!keep_sharp ||
9768 {
9769 BM_ITER_ELEM (v, &viter, e, BM_VERTS_OF_EDGE) {
9771 const int l_index = BM_elem_index_get(l);
9772 const int v_index = BM_elem_index_get(l->v);
9773
9774 if (!is_zero_v3(vert_normals[v_index])) {
9775 short *clnors = static_cast<short *>(BM_ELEM_CD_GET_VOID_P(l, cd_clnors_offset));
9777 bm->lnor_spacearr->lspacearr[l_index], vert_normals[v_index], clnors);
9778
9780 BLI_BITMAP_ENABLE(loop_set, l_index);
9781 }
9782 else {
9783 LinkNode *loops = bm->lnor_spacearr->lspacearr[l_index]->loops;
9784 for (; loops; loops = loops->next) {
9785 BLI_BITMAP_ENABLE(loop_set, BM_elem_index_get((BMLoop *)loops->link));
9786 }
9787 }
9788 }
9789 }
9790 }
9791 }
9792 }
9793
9794 int v_index;
9795 BM_ITER_MESH_INDEX (v, &viter, bm, BM_VERTS_OF_MESH, v_index) {
9796 BM_ITER_ELEM (l, &liter, v, BM_LOOPS_OF_VERT) {
9797 if (BLI_BITMAP_TEST(loop_set, BM_elem_index_get(l))) {
9798 const int loop_index = BM_elem_index_get(l);
9799 short *clnors = static_cast<short *>(BM_ELEM_CD_GET_VOID_P(l, cd_clnors_offset));
9801 bm->lnor_spacearr->lspacearr[loop_index], vert_normals[v_index], clnors);
9802 }
9803 }
9804 }
9805
9806 MEM_delete(loop_set);
9807 MEM_delete(vert_normals);
9808 EDBMUpdate_Params params{};
9809 params.calc_looptris = true;
9810 params.calc_normals = false;
9811 params.is_destructive = false;
9812 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
9813 }
9814
9815 return OPERATOR_FINISHED;
9816}
9817
9819{
9820 /* identifiers */
9821 ot->name = "Set Normals from Faces";
9822 ot->description = "Set the custom normals from the selected faces ones";
9823 ot->idname = "MESH_OT_set_normals_from_faces";
9824
9825 /* API callbacks. */
9828
9829 /* flags */
9831
9833 ot->srna, "keep_sharp", false, "Keep Sharp Edges", "Do not set sharp edges to face");
9834}
9835
9837
9838/* -------------------------------------------------------------------- */
9841
9843{
9844 const Main *bmain = CTX_data_main(C);
9845 const Scene *scene = CTX_data_scene(C);
9846 ViewLayer *view_layer = CTX_data_view_layer(C);
9848 *bmain, scene, view_layer, CTX_wm_view3d(C));
9849
9850 for (Object *obedit : objects) {
9852 BMFace *f;
9853 BMLoop *l;
9854 BMIter fiter, liter;
9855
9858
9859 float (*smooth_normal)[3] = MEM_new_array_zeroed<float[3]>(lnors_ed_arr->totloop, __func__);
9860
9861 /* NOTE(@mont29): This is weird choice of operation, taking all loops of faces of current
9862 * vertex. Could lead to some rather far away loops weighting as much as very close ones
9863 * (topologically speaking), with complex polygons.
9864 * Using topological distance here (rather than geometrical one)
9865 * makes sense IMHO, but would rather go with a more consistent and flexible code,
9866 * we could even add max topological distance to take into account, and a weighting curve.
9867 * Would do that later though, think for now we can live with that choice. */
9868 BMLoopNorEditData *lnor_ed = lnors_ed_arr->lnor_editdata;
9869 for (int i = 0; i < lnors_ed_arr->totloop; i++, lnor_ed++) {
9870 l = lnor_ed->loop;
9871 float loop_normal[3];
9872
9873 BM_ITER_ELEM (f, &fiter, l->v, BM_FACES_OF_VERT) {
9874 BMLoop *l_other;
9875 BM_ITER_ELEM (l_other, &liter, f, BM_LOOPS_OF_FACE) {
9876 const int l_index_other = BM_elem_index_get(l_other);
9877 short *clnors = static_cast<short *>(
9878 BM_ELEM_CD_GET_VOID_P(l_other, lnors_ed_arr->cd_custom_normal_offset));
9880 bm->lnor_spacearr->lspacearr[l_index_other], clnors, loop_normal);
9881 add_v3_v3(smooth_normal[i], loop_normal);
9882 }
9883 }
9884 }
9885
9886 const float factor = RNA_float_get(op->ptr, "factor");
9887
9888 lnor_ed = lnors_ed_arr->lnor_editdata;
9889 for (int i = 0; i < lnors_ed_arr->totloop; i++, lnor_ed++) {
9890 float current_normal[3];
9891
9892 if (normalize_v3(smooth_normal[i]) < CLNORS_VALID_VEC_LEN) {
9893 /* Skip in case the smooth normal is invalid. */
9894 continue;
9895 }
9896
9898 bm->lnor_spacearr->lspacearr[lnor_ed->loop_index], lnor_ed->clnors_data, current_normal);
9899
9900 /* NOTE: again, this is not true spherical interpolation that normals would need...
9901 * But it's probably good enough for now. */
9902 mul_v3_fl(current_normal, 1.0f - factor);
9903 mul_v3_fl(smooth_normal[i], factor);
9904 add_v3_v3(current_normal, smooth_normal[i]);
9905
9906 if (normalize_v3(current_normal) < CLNORS_VALID_VEC_LEN) {
9907 /* Skip in case the smoothed normal is invalid. */
9908 continue;
9909 }
9910
9912 bm->lnor_spacearr->lspacearr[lnor_ed->loop_index], current_normal, lnor_ed->clnors_data);
9913 }
9914
9916 MEM_delete(smooth_normal);
9917
9918 EDBMUpdate_Params params{};
9919 params.calc_looptris = true;
9920 params.calc_normals = false;
9921 params.is_destructive = false;
9922 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
9923 }
9924
9925 return OPERATOR_FINISHED;
9926}
9927
9929{
9930 /* identifiers */
9931 ot->name = "Smooth Normals Vectors";
9932 ot->description = "Smooth custom normals based on adjacent vertex normals";
9933 ot->idname = "MESH_OT_smooth_normals";
9934
9935 /* API callbacks. */
9938
9939 /* flags */
9941
9942 RNA_def_float(ot->srna,
9943 "factor",
9944 0.5f,
9945 0.0f,
9946 1.0f,
9947 "Factor",
9948 "Specifies weight of smooth vs original normal",
9949 0.0f,
9950 1.0f);
9951}
9952
9954
9955/* -------------------------------------------------------------------- */
9958
9960{
9961 const Main *bmain = CTX_data_main(C);
9962 const Scene *scene = CTX_data_scene(C);
9963 ViewLayer *view_layer = CTX_data_view_layer(C);
9965 *bmain, scene, view_layer, CTX_wm_view3d(C));
9966
9967 for (Object *obedit : objects) {
9969 BMFace *f;
9970 BMIter fiter;
9971 const int face_strength = RNA_enum_get(op->ptr, "face_strength");
9972 const bool set = RNA_boolean_get(op->ptr, "set");
9973
9975
9976 const char *layer_id = MOD_WEIGHTEDNORMALS_FACEWEIGHT_CDLAYER_ID;
9977 int cd_prop_int_index = CustomData_get_named_layer_index(&bm->pdata, CD_PROP_INT32, layer_id);
9978 if (cd_prop_int_index == -1) {
9980 cd_prop_int_index = CustomData_get_named_layer_index(&bm->pdata, CD_PROP_INT32, layer_id);
9981 }
9982 cd_prop_int_index -= CustomData_get_layer_index(&bm->pdata, CD_PROP_INT32);
9983 const int cd_prop_int_offset = CustomData_get_n_offset(
9984 &bm->pdata, CD_PROP_INT32, cd_prop_int_index);
9985
9987
9988 if (set) {
9989 BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
9991 int *strength = static_cast<int *>(BM_ELEM_CD_GET_VOID_P(f, cd_prop_int_offset));
9992 *strength = face_strength;
9993 }
9994 }
9995 }
9996 else {
9997 BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
9999 continue;
10000 }
10001 const int *strength = static_cast<int *>(BM_ELEM_CD_GET_VOID_P(f, cd_prop_int_offset));
10002 BM_face_select_set(bm, f, *strength == face_strength);
10003 }
10004 }
10005
10006 EDBMUpdate_Params params{};
10007 params.calc_looptris = false;
10008 params.calc_normals = false;
10009 params.is_destructive = false;
10010 EDBM_update(id_cast<Mesh *>(obedit->data), &params);
10011 }
10012
10013 return OPERATOR_FINISHED;
10014}
10015
10017 {FACE_STRENGTH_WEAK, "WEAK", 0, "Weak", ""},
10018 {FACE_STRENGTH_MEDIUM, "MEDIUM", 0, "Medium", ""},
10019 {FACE_STRENGTH_STRONG, "STRONG", 0, "Strong", ""},
10020 {0, nullptr, 0, nullptr, nullptr},
10021};
10022
10024{
10025 /* identifiers */
10026 ot->name = "Face Normals Strength";
10027 ot->description = "Set/Get strength of face (used in Weighted Normal modifier)";
10028 ot->idname = "MESH_OT_mod_weighted_strength";
10029
10030 /* API callbacks. */
10033
10034 /* flags */
10036
10037 ot->prop = RNA_def_boolean(ot->srna, "set", false, "Set Value", "Set value of faces");
10038
10039 ot->prop = RNA_def_enum(
10040 ot->srna,
10041 "face_strength",
10044 "Face Strength",
10045 "Strength to use for assigning or selecting face influence for weighted normal modifier");
10046}
10047
10049{
10050 /* identifiers */
10051 ot->name = "Flip Quad Tessellation";
10052 ot->description = "Flips the tessellation of selected quads";
10053 ot->idname = "MESH_OT_flip_quad_tessellation";
10054
10057
10058 /* flags */
10060}
10061
10063
10064} // namespace blender
Generic geometry attributes built on CustomData.
#define CTX_DATA_BEGIN(C, Type, instance, member)
#define CTX_DATA_END
CustomData interface, see also DNA_customdata_types.h.
support for deformation groups and hooks.
General operations, lookup, etc. for materials.
General operations, lookup, etc. for blender objects.
#define BLI_BITMAP_NEW(_num, _alloc_string)
Definition BLI_bitmap.hh:39
#define BLI_BITMAP_TEST(_bitmap, _index)
Definition BLI_bitmap.hh:63
#define BLI_BITMAP_ENABLE(_bitmap, _index)
Definition BLI_bitmap.hh:80
A min-heap / priority queue ADT.
#define DEG2RADF(_deg)
#define M_PI
Random number functions.
#define RPT_(msgid)
#define BLT_I18NCONTEXT_ID_CURVE_LEGACY
#define IFACE_(msgid)
#define BLT_I18NCONTEXT_ID_MESH
#define MAXMAT
#define MOD_WEIGHTEDNORMALS_FACEWEIGHT_CDLAYER_ID
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DRAW
Definition WM_types.hh:502
#define ND_DATA
Definition WM_types.hh:552
#define ND_SELECT
Definition WM_types.hh:551
#define NC_OBJECT
Definition WM_types.hh:392
#define UNUSED_VARS(...)
#define UNUSED_VARS_NDEBUG(...)
#define ELEM(...)
#define STREQ(a, b)
#define BM_elem_cb_check_hflag_disabled_simple(type, hflag_n)
#define BM_DISK_EDGE_NEXT(e, v)
#define BM_ALL_NOLOOP
#define BM_ALL
#define BM_FACE_FIRST_LOOP(p)
#define BM_ELEM_CD_SET_BOOL(ele, offset, f)
#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_test_bool(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_notest(bm, ele)
#define BM_select_history_store_head_notest(bm, ele)
#define BM_select_history_store(bm, ele)
#define BM_SELECT_HISTORY_BACKUP(bm)
#define BM_SELECT_HISTORY_RESTORE(bm)
#define BMO_ITER(ele, iter, slot_args, slot_name, restrict_flag)
#define BMO_FLAG_DEFAULTS
#define CD_MASK_COLOR_ALL
static void clear(T &value)
btScalar max
btMatrix3x3 absolute() const
Return the matrix with all values non negative.
Definition btVector3.h:364
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
Definition scene.h:133
const T * data() const
Definition BLI_array.hh:323
static AttributeOwner from_id(ID *id)
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:295
const Value & lookup(const Key &key) const
Definition BLI_map.hh:545
bool contains(const Key &key) const
Definition BLI_map.hh:353
int64_t size() const
IndexRange index_range() const
void opmodal(std::string text, const wmOperatorType *ot, int propvalue, bool inverted=false)
Definition area.cc:1107
#define offsetof(t, d)
#define CLNORS_VALID_VEC_LEN
#define SHRT_MAX
#define FLT_MAX
#define INT_MAX
#define pow
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
bool shape_key_report_if_locked(const Object *obedit, ReportList *reports)
bool shape_key_report_if_any_locked(Object *ob, ReportList *reports)
void base_select(Base *base, eObjectSelect_Mode mode)
Base * add_duplicate(Main *bmain, Scene *scene, ViewLayer *view_layer, Base *base, eDupli_ID_Flags dupflag)
bool calc_active_center_for_editmode(Object *obedit, bool select_only, float r_center[3])
BMesh Iterators.
void EDBM_flag_disable_all(BMesh *bm, char hflag)
void BM_lnorspace_update(BMesh *bm)
void * BLI_findptr(const ListBase *listbase, const void *ptr, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:710
UserDef U
Definition blender.cc:58
void BM_mesh_delete_hflag_context(BMesh *bm, const char hflag, const int type)
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)
static wmOperatorStatus edbm_duplicate_invoke(bContext *C, wmOperator *op, const wmEvent *)
bool BMO_error_occurred_at_level(BMesh *bm, eBMOpErrorLevel level)
MINLINE float len_squared_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
ListBaseT< LinkData > * BM_edgeloop_verts_get(BMEdgeLoopStore *el_store)
void RNA_def_enum_funcs(PropertyRNA *prop, EnumPropertyItemFunc itemfunc)
void MESH_OT_merge_normals(wmOperatorType *ot)
void MESH_OT_symmetrize(wmOperatorType *ot)
int BM_mesh_edgeloops_find(BMesh *bm, ListBaseT< BMEdgeLoopStore > *r_eloops, FunctionRef< bool(BMEdge *)> test_fn, const BMEdgeLoopFind_Params *params)
void MESH_OT_dissolve_faces(wmOperatorType *ot)
static wmOperatorStatus edbm_fill_grid_exec(bContext *C, wmOperator *op)
bool EDBM_automerge(Object *obedit, bool update, char hflag, float dist, bool use_centroid)
bool ED_operator_scene_editable(bContext *C)
void BM_mesh_copy_init_customdata(BMesh *bm_dst, BMesh *bm_src, const BMAllocTemplate *allocsize)
wmOperatorStatus WM_menu_invoke(bContext *C, wmOperator *op, const wmEvent *)
void BM_mesh_elem_index_ensure(BMesh *bm, const char htype)
void RNA_enum_item_add(EnumPropertyItem **items, int *totitem, const EnumPropertyItem *item)
MINLINE int mod_i(int i, int n)
static wmOperatorStatus edbm_duplicate_exec(bContext *C, wmOperator *op)
void RNA_def_property_ui_text(PropertyRNA *prop, const char *name, const char *description)
BLI_INLINE bool BM_edge_is_wire(const BMEdge *e) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void ED_mesh_geometry_clear(Mesh *mesh)
static wmOperatorStatus edbm_shape_propagate_to_all_exec(bContext *C, wmOperator *op)
int RNA_enum_get(PointerRNA *ptr, const char *name)
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_point_normals_invoke(bContext *C, wmOperator *op, const wmEvent *)
@ BM_SPACEARR_DIRTY_ALL
int BMO_slot_buffer_len(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
void RNA_property_float_get_array(PointerRNA *ptr, PropertyRNA *prop, float *values)
@ RPT_WARNING
Definition BKE_report.hh:40
void RNA_def_property_enum_default(PropertyRNA *prop, int value)
void MESH_OT_faces_shade_smooth(wmOperatorType *ot)
static bool point_normals_init(bContext *C, wmOperator *op)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
void EDBM_selectmode_flush(BMesh *bm, short selectmode)
static void mesh_set_smooth_faces(BMesh *bm, short smooth)
static int bmelemsort_comp(const void *v1, const void *v2)
int CustomData_number_of_layers(const CustomData *data, eCustomDataType type)
void BM_mesh_free(BMesh *bm)
BMesh Free Mesh.
void void void EDBM_redo_state_free(BMBackup *backup) ATTR_NONNULL(1)
static bool mesh_separate_material(Main *bmain, Scene *scene, ViewLayer *view_layer, Base *base_old, BMesh *bm_old)
wmKeyMap * WM_modalkeymap_find(wmKeyConfig *keyconf, const char *idname)
Definition wm_keymap.cc:999
BMesh * BKE_editmesh_bmesh_get_for_write(Mesh *mesh)
Definition editmesh.cc:79
static wmOperatorStatus edbm_normals_make_consistent_exec(bContext *C, wmOperator *op)
static wmOperatorStatus edbm_dissolve_limited_exec(bContext *C, wmOperator *op)
static wmOperatorStatus edbm_mark_seam_exec(bContext *C, wmOperator *op)
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
void MESH_OT_vert_connect_concave(wmOperatorType *ot)
void MESH_OT_dissolve_edges(wmOperatorType *ot)
int BM_iter_elem_count_flag(const char itype, void *data, const char hflag, const bool value)
Elem Iter Flag Count.
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
static wmOperatorStatus edbm_average_normals_exec(bContext *C, wmOperator *op)
static void edbm_flip_normals_custom_loop_normals(Object *obedit, BMesh *bm)
bool BM_edge_share_face_check(BMEdge *e1, BMEdge *e2)
short * BKE_object_material_len_p(Object *ob)
static wmOperatorStatus edbm_reverse_uvs_exec(bContext *C, wmOperator *op)
void BMO_slot_buffer_hflag_enable(BMesh *bm, BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, char htype, char hflag, bool do_flush)
BMO_FLAG_BUFFER.
static bool mesh_separate_selected(Main *bmain, Scene *scene, ViewLayer *view_layer, Base *base_old, BMesh *bm_old)
void minmax_v3v3_v3(float min[3], float max[3], const float vec[3])
static wmOperatorStatus edbm_delete_exec(bContext *C, wmOperator *op)
bool EDBM_op_init(BMesh *bm, BMOperator *bmop, wmOperator *op, const char *fmt,...)
void MESH_OT_mod_weighted_strength(wmOperatorType *ot)
Material *** BKE_object_material_array_p(Object *ob)
ATTR_WARN_UNUSED_RESULT const BMLoop * l
static bool edbm_edge_split_selected_edges(wmOperator *op, Object *obedit, BMEditMesh *em)
void MESH_OT_beautify_fill(wmOperatorType *ot)
char BM_select_history_htype_all(const BMesh *bm)
void MESH_OT_subdivide(wmOperatorType *ot)
static bool edbm_connect_vert_pair(BMEditMesh *em, Mesh *mesh, wmOperator *op)
void id_us_plus(ID *id)
Definition lib_id.cc:433
static wmOperatorStatus edbm_vert_connect_exec(bContext *C, wmOperator *op)
void BM_mesh_elem_table_ensure(BMesh *bm, const char htype)
void BM_vert_kill(BMesh *bm, BMVert *v)
static void edbm_average_normals_ui(bContext *C, wmOperator *op)
BMVert * EDBM_verts_mirror_get(BMEditMesh *em, BMesh *bm, BMVert *v)
static bool edbm_add_edge_face__smooth_get(BMesh *bm)
PointerRNA RNA_id_pointer_create(ID *id)
static void edbm_blend_from_shape_ui(bContext *C, wmOperator *op)
bool CustomData_has_layer(const CustomData *data, eCustomDataType type)
void BM_mesh_edgeloops_free(ListBaseT< BMEdgeLoopStore > *eloops)
static wmOperatorStatus edbm_do_smooth_vertex_exec(bContext *C, wmOperator *op)
BLI_INLINE float fb(float length, float L)
void MESH_OT_convex_hull(wmOperatorType *ot)
@ EDBM_CLNOR_POINTTO_MODE_MOUSE
@ EDBM_CLNOR_POINTTO_MODE_COORDINATES
bool BM_vert_is_wire(const BMVert *v)
static void point_normals_update_statusbar(bContext *C, wmOperator *op)
const char * RNA_property_identifier(const PropertyRNA *prop)
void mul_v3_m4v3(float r[3], const float mat[4][4], const float vec[3])
void MESH_OT_duplicate(wmOperatorType *ot)
BMBackup EDBM_redo_state_store(BMesh *bm)
void MESH_OT_delete_edgeloop(wmOperatorType *ot)
struct RNG * BLI_rng_new_srandom(unsigned int seed)
Definition rand.cc:48
float angle_v3v3v3(const float a[3], const float b[3], const float c[3]) ATTR_WARN_UNUSED_RESULT
MINLINE int count_bits_i(unsigned int n)
static bool bm_faces_select_to_tag_with_manifold_bounds_check(BMesh *bm)
void MESH_OT_dissolve_verts(wmOperatorType *ot)
void EDBM_update(Mesh *mesh, const EDBMUpdate_Params *params)
int RNA_property_enum_get(PointerRNA *ptr, PropertyRNA *prop)
int CustomData_get_n_offset(const CustomData *data, eCustomDataType type, int n)
void MESH_OT_decimate(wmOperatorType *ot)
short * BKE_id_material_len_p(ID *id)
wmKeyMap * point_normals_modal_keymap(wmKeyConfig *keyconf)
void MESH_OT_vertices_smooth_laplacian(wmOperatorType *ot)
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
bool EDBM_op_callf(BMesh *bm, wmOperator *op, const char *fmt,...)
void MESH_OT_vert_connect_nonplanar(wmOperatorType *ot)
static bool dissolve_mode_poll_property(const bContext *C, wmOperator *op, const PropertyRNA *prop)
@ V3D_AROUND_CENTER_BOUNDS
@ V3D_AROUND_CENTER_MEDIAN
const EnumPropertyItem rna_enum_symmetrize_direction_items[]
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
BMLoop * BM_vert_step_fan_loop(BMLoop *l, BMEdge **e_step)
void BM_custom_loop_normals_from_vector_layer(BMesh *bm, bool add_sharp_edges)
bool BMO_op_initf(BMesh *bm, BMOperator *op, int flag, const char *fmt,...)
static int edbm_add_edge_face_exec__vert_edge_lookup(BMVert *v, BMEdge *e_used, BMEdge **e_arr, const int e_arr_len, bool(*func)(const BMEdge *))
static Base * mesh_separate_tagged(Main *bmain, Scene *scene, ViewLayer *view_layer, Base *base_old, BMesh *bm_old)
static void point_normals_cancel(bContext *C, wmOperator *op)
BMLoop * BM_face_edge_share_loop(BMFace *f, BMEdge *e)
Return the Loop Shared by Face and Edge.
static wmOperatorStatus edbm_sort_elements_exec(bContext *C, wmOperator *op)
void MESH_OT_wireframe(wmOperatorType *ot)
void MESH_OT_shape_propagate_to_all(wmOperatorType *ot)
Material *** BKE_id_material_array_p(ID *id)
static wmOperatorStatus edbm_dissolve_degenerate_exec(bContext *C, wmOperator *op)
static wmOperatorStatus edbm_separate_exec(bContext *C, wmOperator *op)
int RNA_int_get(PointerRNA *ptr, const char *name)
void * BMO_iter_map_value_ptr(BMOIter *iter)
KeyBlock * BKE_keyblock_find_name(Key *key, const char name[])
Definition key.cc:1434
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
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)
void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
void MESH_OT_colors_rotate(wmOperatorType *ot)
void RNA_property_float_set_array(PointerRNA *ptr, PropertyRNA *prop, const float *values)
static wmOperatorStatus edbm_quads_convert_to_tris_exec(bContext *C, wmOperator *op)
static wmOperatorStatus edbm_point_normals_exec(bContext *C, wmOperator *op)
static bool shape_propagate(BMEditMesh *em, BMesh *bm, bool use_symmetry)
int CustomData_get_layer_index(const CustomData *data, eCustomDataType type)
void MESH_OT_poke(wmOperatorType *ot)
ATTR_WARN_UNUSED_RESULT const BMVert * v
static EnumPropertyItem normal_vector_tool_items[]
static void edbm_normals_tools_ui(bContext *C, wmOperator *op)
static void mesh_operator_edgering_props(wmOperatorType *ot, const int cuts_min, const int cuts_default)
void MESH_OT_vertices_smooth(wmOperatorType *ot)
void MESH_OT_split_normals(wmOperatorType *ot)
static bool edbm_sort_elements_poll_property(const bContext *, wmOperator *op, const PropertyRNA *prop)
RegionView3D * ED_view3d_context_rv3d(bContext *C)
static wmOperatorStatus edbm_remove_doubles_exec(bContext *C, wmOperator *op)
void EDBM_verts_mirror_cache_begin(BMEditMesh *em, BMesh *bm, int axis, bool use_self, bool use_select, bool respecthide, bool use_topology)
void MESH_OT_vert_connect_path(wmOperatorType *ot)
void BMO_op_exec(BMesh *bm, BMOperator *op)
BMESH OPSTACK EXEC OP.
static void edbm_dissolve_prop__use_preserve_quads(wmOperatorType *ot, bool value, int flag)
void MESH_OT_blend_from_shape(wmOperatorType *ot)
void MESH_OT_subdivide_edgering(wmOperatorType *ot)
static bool bm_vert_connect_select_history(BMesh *bm)
void MESH_OT_split(wmOperatorType *ot)
static wmOperatorStatus edbm_flip_normals_exec(bContext *C, wmOperator *op)
void BKE_sculptsession_free_pbvh(Object &object)
static wmOperatorStatus edbm_flip_quad_tessellation_exec(bContext *C, wmOperator *op)
void MESH_OT_merge(wmOperatorType *ot)
static wmOperatorStatus edbm_dissolve_mode_exec(bContext *C, wmOperator *op)
BLI_INLINE BMVert * BM_edge_other_vert(BMEdge *e, const BMVert *v) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void MESH_OT_symmetry_snap(wmOperatorType *ot)
bool EDBM_automerge_connected(Object *obedit, bool update, char hflag, float dist)
static wmOperatorStatus edbm_vert_connect_path_exec(bContext *C, wmOperator *op)
ViewLayer * CTX_data_view_layer(const bContext *C)
void ED_outliner_select_sync_from_object_tag(bContext *C)
void MESH_OT_delete_loose(wmOperatorType *ot)
static void edbm_decimate_ui(bContext *, wmOperator *op)
MINLINE void mul_v3_fl(float r[3], float f)
void RNA_def_property_ui_range(PropertyRNA *prop, double min, double max, double step, int precision)
bool BM_edgeloop_is_closed(BMEdgeLoopStore *el_store)
static void edbm_flip_quad_tessellation(wmOperator *op, Object *obedit, BMesh *bm)
static void edbm_dissolve_prop__use_angle_threshold(wmOperatorType *ot, int flag)
static wmOperatorStatus edbm_set_normals_from_faces_exec(bContext *C, wmOperator *op)
static void normals_split(BMesh *bm)
void RNA_def_property_float_default(PropertyRNA *prop, float value)
static wmOperatorStatus edbm_faces_shade_smooth_exec(bContext *C, wmOperator *)
static void edbm_flip_normals_face_winding(wmOperator *op, Object *obedit, BMesh *bm)
void BM_vert_normal_update(BMVert *v)
static wmOperatorStatus edbm_smooth_normals_exec(bContext *C, wmOperator *op)
bool RNA_property_boolean_get(PointerRNA *ptr, PropertyRNA *prop)
void * BM_iter_at_index(BMesh *bm, const char itype, void *data, int index)
MINLINE void sub_v3_v3(float r[3], const float a[3])
static bool mesh_separate_loose(Main *bmain, Scene *scene, ViewLayer *view_layer, Base *base_old, BMesh *bm_old)
void RNA_property_int_set(PointerRNA *ptr, PropertyRNA *prop, int value)
@ SUBD_CORNER_STRAIGHT_CUT
@ SUBD_CORNER_INNERVERT
bool BM_loop_check_cyclic_smooth_fan(BMLoop *l_curr)
static wmOperatorStatus edbm_edge_split_exec(bContext *C, wmOperator *op)
int BM_edgeloop_length_get(BMEdgeLoopStore *el_store)
void BM_mesh_bm_from_me(BMesh *bm, const Mesh *mesh, const BMeshFromMeshParams *params)
Mesh -> BMesh.
void BM_mesh_esubdivide(BMesh *bm, char edge_hflag, float smooth, short smooth_falloff, bool use_smooth_even, float fractal, float along_normal, int numcuts, int seltype, int cornertype, short use_single_edge, short use_grid_fill, short use_only_quads, int seed)
void BM_vert_separate(BMesh *bm, BMVert *v, BMEdge **e_in, int e_in_len, const bool copy_select, BMVert ***r_vout, int *r_vout_len)
static bool merge_firstlast(BMesh *bm, const bool use_first, const bool use_uvmerge, wmOperator *wmop)
PropertyRNA * RNA_def_float_factor(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)
void mul_m4_v3(const float M[4][4], float r[3])
static wmOperatorStatus mesh_symmetry_snap_exec(bContext *C, wmOperator *op)
PropertyRNA * RNA_def_property(StructOrFunctionRNA *cont_, const char *identifier_c_str, int type, int subtype)
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
float BKE_defvert_find_weight(const MDeformVert *dvert, int defgroup)
Definition deform.cc:779
void MESH_OT_solidify(wmOperatorType *ot)
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:541
void EDBM_verts_mirror_cache_begin_ex(BMEditMesh *em, BMesh *bm, int axis, bool use_self, bool use_select, bool respecthide, bool use_topology, float maxdist, int *r_index)
void ED_uvedit_live_unwrap(const Scene *scene, Span< Object * > objects)
static Base * mesh_separate_arrays(Main *bmain, Scene *scene, ViewLayer *view_layer, Base *base_old, BMesh *bm_old, BMVert **verts, uint verts_len, BMEdge **edges, uint edges_len, BMFace **faces, uint faces_len)
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:593
bool ED_operator_editmesh(bContext *C)
void MESH_OT_uvs_reverse(wmOperatorType *ot)
static bool bm_vert_connect_pair(BMesh *bm, BMVert *v_a, BMVert *v_b, const bool skip_normals=false)
static FillGridSplitJoin * edbm_fill_grid_split_join_init(BMesh *bm)
static wmOperatorStatus edbm_reveal_exec(bContext *C, wmOperator *op)
void BM_mesh_copy_arrays(BMesh *bm_src, BMesh *bm_dst, BMVert **verts_src, uint verts_src_len, BMEdge **edges_src, uint edges_src_len, BMFace **faces_src, uint faces_src_len)
@ EDBM_CLNOR_AVERAGE_ANGLE
@ EDBM_CLNOR_AVERAGE_FACE_AREA
@ EDBM_CLNOR_AVERAGE_LOOP
bool EDBM_op_finish(BMesh *bm, BMOperator *bmop, wmOperator *op, const bool do_report)
static wmOperatorStatus edbm_solidify_exec(bContext *C, wmOperator *op)
void MESH_OT_face_make_planar(wmOperatorType *ot)
bool EDBM_op_call_and_selectf(BMesh *bm, wmOperator *op, const char *select_slot_out, const bool select_extend, const char *fmt,...)
void * BLI_heapsimple_pop_min(HeapSimple *heap) ATTR_NONNULL(1)
void RNA_def_property_enum_items(PropertyRNA *prop, const EnumPropertyItem *item)
void BM_edgeloop_edges_get(BMEdgeLoopStore *el_store, BMEdge **e_arr)
void MESH_OT_edge_face_add(wmOperatorType *ot)
static wmOperatorStatus edbm_fill_exec(bContext *C, wmOperator *op)
static void edbm_add_edge_face_exec__tricky_finalize_sel(BMesh *bm, BMElem *ele_desel, BMFace *f)
MINLINE void mul_v3_v3(float r[3], const float a[3])
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
bool BM_vert_calc_normal_ex(const BMVert *v, const char hflag, float r_no[3])
float BM_face_calc_area(const BMFace *f)
void MESH_OT_flip_normals(wmOperatorType *ot)
bool EDBM_mesh_hide(BMesh *bm, const short selectmode, bool swap)
@ ME_EDIT_MIRROR_TOPO
static bool edbm_normals_tools_ui_poll(wmOperatorType *, PointerRNA *ptr)
void MESH_OT_separate(wmOperatorType *ot)
static wmOperatorStatus edbm_add_edge_face_exec(bContext *C, wmOperator *op)
static wmOperatorStatus edbm_delete_loose_exec(bContext *C, wmOperator *op)
void DEG_id_tag_update(ID *id, unsigned int flags)
static BMElem * edbm_add_edge_face_exec__tricky_extend_sel(BMesh *bm)
void MESH_OT_dissolve_degenerate(wmOperatorType *ot)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
void MESH_OT_smooth_normals(wmOperatorType *ot)
BMesh const char void * data
static bool bm_face_is_loose(BMFace *f)
void BM_mesh_elem_hflag_enable_test(BMesh *bm, const char htype, const char hflag, const bool respecthide, const bool overwrite, const char hflag_test)
static bool edbm_decimate_check(bContext *, wmOperator *)
const EnumPropertyItem rna_enum_modifier_triangulate_quad_method_items[]
static wmOperatorStatus edbm_tris_convert_to_quads_exec(bContext *C, wmOperator *op)
static const EnumPropertyItem * shape_itemf(bContext *C, PointerRNA *, PropertyRNA *, bool *r_free)
bool EDBM_mesh_reveal(BMesh *bm, const short selectmode, bool select)
void BKE_id_material_resize(Main *bmain, ID *id, short totcol, bool do_id_user)
wmWindowManager * CTX_wm_manager(const bContext *C)
void MESH_OT_offset_edge_loops(wmOperatorType *ot)
static void mesh_separate_material_assign_mat_nr(Main *bmain, Object *ob, const short mat_nr)
Object * CTX_data_edit_object(const bContext *C)
static wmOperatorStatus edbm_normals_tools_exec(bContext *C, wmOperator *op)
int CustomData_get_offset_named(const CustomData *data, eCustomDataType type, StringRef name)
float BM_edge_calc_face_angle_ex(const BMEdge *e, const float fallback)
BMESH EDGE/FACE ANGLE.
static bool normals_tools_draw_check_prop(PointerRNA *ptr, PropertyRNA *prop, void *)
void BM_face_select_set(BMesh *bm, BMFace *f, const bool select)
Select Face.
int BLI_sortutil_cmp_float_reverse(const void *a_, const void *b_)
Definition sort_utils.cc:41
static wmOperatorStatus edbm_mark_sharp_exec(bContext *C, wmOperator *op)
void MESH_OT_mark_seam(wmOperatorType *ot)
void BLI_listbase_rotate_first(ListBase *lb, void *vlink) ATTR_NONNULL(1
Definition listbase.cc:869
void MESH_OT_vert_connect(wmOperatorType *ot)
static EnumPropertyItem average_method_items[]
void ED_view3d_cursor3d_update(bContext *C, const int mval[2], bool use_depth, enum eV3DCursorOrient orientation)
static void edbm_fill_grid_split_join_finish(BMesh *bm, wmOperator *op, FillGridSplitJoin *split_join, bool changed)
void BM_loop_normal_editdata_array_free(BMLoopNorEditDataArray *lnors_ed_arr)
static bool point_normals_ensure(bContext *C, wmOperator *op)
static void join_triangle_props(wmOperatorType *ot)
MINLINE void negate_v3(float r[3])
static wmOperatorStatus mesh_symmetrize_exec(bContext *C, wmOperator *op)
const EnumPropertyItem rna_enum_proportional_falloff_curve_only_items[]
Definition rna_scene.cc:119
@ MESH_DELETE_ONLY_FACE
@ MESH_DELETE_EDGE_FACE
void BM_normals_loops_edges_tag(BMesh *bm, const bool do_edges)
const EnumPropertyItem rna_enum_mesh_delimit_mode_items[]
Definition rna_mesh.cc:28
static void sort_bmelem_flag(bContext *C, Scene *scene, Object *ob, RegionView3D *rv3d, const int types, const int flag, const int action, const int reverse, const uint seed)
const BMAllocTemplate bm_mesh_allocsize_default
Definition bmesh_mesh.cc:26
static wmOperatorStatus edbm_unsubdivide_exec(bContext *C, wmOperator *op)
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
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)
bool BMO_op_callf(BMesh *bm, int flag, const char *fmt,...)
void MESH_OT_faces_shade_flat(wmOperatorType *ot)
void RNA_enum_items_add_value(EnumPropertyItem **items, int *totitem, const EnumPropertyItem *item, int value)
void RNA_enum_item_end(EnumPropertyItem **items, int *totitem)
void BLI_heapsimple_free(HeapSimple *heap, HeapSimpleFreeFP ptrfreefp) ATTR_NONNULL(1)
static BMLoopNorEditDataArray * flip_custom_normals_init_data(BMesh *bm)
bool BM_edge_face_pair(BMEdge *e, BMFace **r_fa, BMFace **r_fb)
void BLI_rng_free(struct RNG *rng) ATTR_NONNULL(1)
Definition rand.cc:55
void BKE_lnor_space_custom_normal_to_data(const MLoopNorSpace *lnor_space, const float custom_lnor[3], short r_clnor_data[2])
static void edbm_dissolve_prop__use_face_split(wmOperatorType *ot)
void MESH_OT_average_normals(wmOperatorType *ot)
void MESH_OT_tris_convert_to_quads(wmOperatorType *ot)
static wmOperatorStatus edbm_do_smooth_laplacian_vertex_exec(bContext *C, wmOperator *op)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
void BKE_objects_materials_sync_length_all(Main *bmain, ID *id)
void MESH_OT_set_normals_from_faces(wmOperatorType *ot)
static EnumPropertyItem clnors_pointto_mode_items[]
void BMO_slot_int_set(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, int i)
const EnumPropertyItem rna_enum_dummy_NULL_items[]
Definition rna_rna.cc:28
static wmOperatorStatus edbm_rotate_uvs_exec(bContext *C, wmOperator *op)
MINLINE void zero_v3(float r[3])
void MESH_OT_mark_sharp(wmOperatorType *ot)
void MESH_OT_fill_holes(wmOperatorType *ot)
BLI_INLINE BMVert * BM_vert_at_index(BMesh *bm, const int index)
void MESH_OT_normals_tools(wmOperatorType *ot)
void MESH_OT_dissolve_mode(wmOperatorType *ot)
int BLI_listbase_count_at_most(const ListBase *listbase, int count_max) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:518
static wmOperatorStatus edbm_dissolve_faces_exec(bContext *C, wmOperator *op)
void BKE_object_material_array_assign(Main *bmain, Object *ob, Material ***matar, int totcol, bool to_object_only)
static wmOperatorStatus edbm_offset_edgeloop_exec(bContext *C, wmOperator *op)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void BKE_id_material_clear(Main *bmain, ID *id)
static wmOperatorStatus edbm_hide_exec(bContext *C, wmOperator *op)
void BM_vert_select_set(BMesh *bm, BMVert *v, const bool select)
Select Vert.
static wmOperatorStatus edbm_poke_face_exec(bContext *C, wmOperator *op)
static void point_normals_apply(bContext *C, wmOperator *op, float target[3], const bool do_reset)
HeapSimple * BLI_heapsimple_new() ATTR_WARN_UNUSED_RESULT
static wmOperatorStatus edbm_subdivide_edge_ring_exec(bContext *C, wmOperator *op)
void MESH_OT_normals_make_consistent(wmOperatorType *ot)
static bool bm_vert_is_select_history_open(BMesh *bm)
int CustomData_get_named_layer_index(const CustomData *data, eCustomDataType type, StringRef name)
static wmOperatorStatus edbm_face_make_planar_exec(bContext *C, wmOperator *op)
float BLI_rng_get_float(struct RNG *rng) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition rand.cc:90
BMFace * BM_edge_pair_share_face_by_len(BMEdge *e_a, BMEdge *e_b, BMLoop **r_l_a, BMLoop **r_l_b, const bool allow_adjacent)
static bool merge_target(BMesh *bm, Scene *scene, Object *ob, const bool use_cursor, const bool use_uvmerge, wmOperator *wmop)
@ EDBM_CLNOR_MODAL_CANCEL
@ EDBM_CLNOR_MODAL_CONFIRM
@ EDBM_CLNOR_MODAL_POINTTO_INVERT
@ EDBM_CLNOR_MODAL_POINTTO_USE_PIVOT
@ EDBM_CLNOR_MODAL_POINTTO_SET_USE_SELECTED
@ EDBM_CLNOR_MODAL_POINTTO_USE_MOUSE
@ EDBM_CLNOR_MODAL_POINTTO_SPHERIZE
@ EDBM_CLNOR_MODAL_POINTTO_ALIGN
@ EDBM_CLNOR_MODAL_POINTTO_SET_USE_3DCURSOR
@ EDBM_CLNOR_MODAL_POINTTO_RESET
@ EDBM_CLNOR_MODAL_POINTTO_USE_OBJECT
@ V3D_CURSOR_ORIENT_NONE
Definition ED_view3d.hh:101
float BM_loop_calc_face_angle(const BMLoop *l)
static wmOperatorStatus edbm_point_normals_modal(bContext *C, wmOperator *op, const wmEvent *event)
void MESH_OT_fill_grid(wmOperatorType *ot)
BMOpSlot * BMO_slot_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *identifier)
BMESH OPSTACK GET SLOT.
void RNA_def_property_translation_context(PropertyRNA *prop, const char *context)
static void edbm_report_delete_info(ReportList *reports, const int totelem_old[3], const int totelem_new[3])
BMEditMesh * BKE_editmesh_from_object(Object *ob)
Return the BMEditMesh for a given object.
Definition editmesh.cc:59
void MESH_OT_uvs_rotate(wmOperatorType *ot)
static wmOperatorStatus edbm_dissolve_edges_exec(bContext *C, wmOperator *op)
MINLINE void mul_v3_v3fl(float r[3], const float a[3], float f)
float BLI_heapsimple_top_value(const HeapSimple *heap) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
float RNA_property_float_get(PointerRNA *ptr, PropertyRNA *prop)
static void edbm_point_normals_ui(bContext *C, wmOperator *op)
BMEdge * BM_edge_exists(BMVert *v_a, BMVert *v_b)
MINLINE float normalize_v3(float n[3])
void MESH_OT_delete(wmOperatorType *ot)
static void bmesh_selected_verts_center_calc(BMesh *bm, float *r_center)
static void mesh_operator_edgering_props_get(wmOperator *op, EdgeRingOpSubdProps *op_props)
MINLINE void add_v3_v3(float r[3], const float a[3])
wmEventHandler_Op * WM_event_add_modal_handler(bContext *C, wmOperator *op)
@ MLNOR_SPACE_IS_SINGLE
Definition BKE_mesh.h:288
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
static wmOperatorStatus edbm_split_exec(bContext *C, wmOperator *op)
void BKE_editmesh_lnorspace_update(BMesh *bm)
Definition editmesh.cc:253
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)
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
void mid_v3_v3v3(float r[3], const float a[3], const float b[3])
void EDBM_mesh_stats_multi(Span< Object * > objects, int totelem[3], int totelem_sel[3])
static wmOperatorStatus edbm_merge_normals_exec(bContext *C, wmOperator *)
@ MLNOR_SPACEARR_BMLOOP_PTR
Definition BKE_mesh.h:310
void EDBM_verts_mirror_cache_end(BMEditMesh *em)
MINLINE bool compare_v3v3(const float v1[3], const float v2[3], float limit) ATTR_WARN_UNUSED_RESULT
PropertyRNA * RNA_def_float_vector_xyz(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)
void BM_data_layer_ensure_named(BMesh *bm, CustomData *data, int type, const StringRef name)
void BLI_heapsimple_insert(HeapSimple *heap, float value, void *ptr) ATTR_NONNULL(1)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
void EDBM_selectmode_flush_ex(BMesh *bm, short selectmode)
unsigned int BLI_bitmap
Definition BLI_bitmap.hh:15
@ ATTR_DOMAIN_MASK_CORNER
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)
@ SUBD_RING_INTERP_SURF
@ SUBD_RING_INTERP_PATH
@ SUBD_RING_INTERP_LINEAR
ATTR_WARN_UNUSED_RESULT const BMVert * v2
void MESH_OT_colors_reverse(wmOperatorType *ot)
void BM_mesh_decimate_collapse(BMesh *bm, float factor, float *vweights, float vweight_factor, bool do_triangulate, int symmetry_axis, float symmetry_eps)
BM_mesh_decimate.
void BM_mesh_copy_init_customdata_all_layers(BMesh *bm_dst, BMesh *bm_src, const char htype, const BMAllocTemplate *allocsize)
void BM_face_kill(BMesh *bm, BMFace *f)
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)
static const EnumPropertyItem prop_mesh_cornervert_types[]
int BKE_attribute_to_index(const AttributeOwner &owner, const StringRef name, AttrDomainMask domain_mask, eCustomDataMask layer_mask, bool include_anonymous=true)
void * BMO_slot_buffer_alloc(BMOperator *op, BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, int len)
void EDBM_mesh_normals_update(BMesh *bm)
static bool edbm_edge_split_selected_verts(wmOperator *op, Object *obedit, BMEditMesh *em)
bool BLI_heapsimple_is_empty(const HeapSimple *heap) ATTR_NONNULL(1)
void WM_operatortype_props_advanced_begin(wmOperatorType *ot)
static wmOperatorStatus edbm_bridge_edge_loops_exec(bContext *C, wmOperator *op)
@ ID_RECALC_SELECT
Definition DNA_ID.h:1101
@ ID_RECALC_GEOMETRY_ALL_MODES
Definition DNA_ID.h:1183
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
@ PROP_ENUM_NO_TRANSLATE
Definition RNA_types.hh:502
@ PROP_NEVER_UNLINK
Definition RNA_types.hh:454
@ PROP_HIDDEN
Definition RNA_types.hh:408
void EDBM_verts_mirror_apply(BMEditMesh *em, BMesh *bm, int sel_from, int sel_to)
BMLoopNorEditDataArray * BM_loop_normal_editdata_array_init(BMesh *bm, const bool do_all_loops_of_vert)
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)
static wmOperatorStatus normals_split_merge(bContext *C, const bool do_merge)
PropertyRNA * RNA_def_float_distance(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)
static bool bm_vert_connect_select_history_edge_to_vert_path(BMesh *bm, ListBaseT< BMEditSelection > *r_selected)
const BMesh * BKE_editmesh_bmesh_get(const Object *ob)
Definition editmesh.cc:65
BMLoopNorEditDataArray * BM_loop_normal_editdata_array_init_with_htype(BMesh *bm, const bool do_all_loops_of_vert, const char htype_override)
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)
void BKE_lnor_space_custom_data_to_normal(const MLoopNorSpace *lnor_space, const short clnor_data[2], float r_custom_lnor[3])
unsigned int BLI_ghashutil_strhash_p(const void *ptr)
void WM_modalkeymap_assign(wmKeyMap *km, const char *opname)
void ED_view3d_win_to_3d_int(const View3D *v3d, const ARegion *region, const float depth_pt[3], const int mval[2], float r_out[3])
void BM_select_history_clear(BMesh *bm)
static const EnumPropertyItem merge_type_items[]
bool EDBM_selectmode_disable_multi_ex(Scene *scene, Span< Base * > bases, short selectmode_disable, short selectmode_fallback)
bool EDBM_uvselect_clear(BMesh *bm)
void DEG_relations_tag_update(Main *bmain)
BMLoop * BM_face_vert_share_loop(BMFace *f, BMVert *v)
Return the Loop Shared by Face and Vertex.
void BM_data_layer_add_named(BMesh *bm, CustomData *data, int type, const StringRef name)
static void edbm_dissolve_prop__use_verts(wmOperatorType *ot, bool value, int flag)
void RNA_property_boolean_set(PointerRNA *ptr, PropertyRNA *prop, bool value)
static wmOperatorStatus edbm_split_normals_exec(bContext *C, wmOperator *)
Scene * CTX_data_scene(const bContext *C)
void BMO_op_init(BMesh *bm, BMOperator *op, int flag, const char *opname)
BMESH OPSTACK INIT OP.
static wmOperatorStatus edbm_merge_exec(bContext *C, wmOperator *op)
void MESH_OT_reveal(wmOperatorType *ot)
void MESH_OT_fill(wmOperatorType *ot)
static bool average_normals_draw_check_prop(PointerRNA *ptr, PropertyRNA *prop, void *)
void WM_operator_type_modal_from_exec_for_object_edit_coords(wmOperatorType *ot)
void MESH_OT_point_normals(wmOperatorType *ot)
wmKeyMap * WM_modalkeymap_ensure(wmKeyConfig *keyconf, const char *idname, const EnumPropertyItem *items)
Definition wm_keymap.cc:972
Mesh * BKE_object_get_original_mesh(const Object *object)
Definition object.cc:4461
static wmOperatorStatus edbm_delete_edgeloop_exec(bContext *C, wmOperator *op)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
@ OPTYPE_BLOCKING
Definition WM_types.hh:192
@ OPTYPE_INTERNAL
Definition WM_types.hh:210
void void EDBM_redo_state_restore_and_free(BMBackup *backup, BMEditMesh *em, BMesh *bm, bool recalc_looptris) ATTR_NONNULL(1
void BM_mesh_bm_to_me(Main *bmain, BMesh *bm, Mesh *mesh, const BMeshToMeshParams *params)
static wmOperatorStatus edbm_dissolve_verts_exec(bContext *C, wmOperator *op)
static int edbm_bridge_tag_boundary_edges(BMesh *bm)
static void edbm_dissolve_prop__use_boundary_tear(wmOperatorType *ot)
int is_quad_flip_v3(const float v1[3], const float v2[3], const float v3[3], const float v4[3])
static wmOperatorStatus edbm_vert_connect_nonplaner_exec(bContext *C, wmOperator *op)
static wmOperatorStatus edbm_mod_weighted_strength_exec(bContext *C, wmOperator *op)
void MESH_OT_quads_convert_to_tris(wmOperatorType *ot)
void BMO_op_finish(BMesh *bm, BMOperator *op)
BMESH OPSTACK FINISH OP.
const EnumPropertyItem rna_enum_modifier_triangulate_ngon_method_items[]
static float edbm_fill_grid_vert_tag_angle(BMVert *v)
void MESH_OT_hide(wmOperatorType *ot)
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
void MESH_OT_unsubdivide(wmOperatorType *ot)
static wmOperatorStatus edbm_reverse_colors_exec(bContext *C, wmOperator *op)
static wmOperatorStatus edbm_subdivide_exec(bContext *C, wmOperator *op)
BLI_INLINE void BMO_slot_map_elem_insert(BMOperator *op, BMOpSlot *slot, const void *element, void *val)
static wmOperatorStatus edbm_vert_connect_concave_exec(bContext *C, wmOperator *op)
bool CustomData_has_layer_named(const CustomData *data, eCustomDataType type, StringRef name)
void BMO_slot_buffer_hflag_disable(BMesh *bm, BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, char htype, char hflag, bool do_flush)
BMO_FLAG_BUFFER.
void RNA_def_property_int_default(PropertyRNA *prop, int value)
int RNA_property_int_get(PointerRNA *ptr, PropertyRNA *prop)
void MESH_OT_sort_elements(wmOperatorType *ot)
void EDBM_select_flush_from_verts(BMesh *bm, short selectmode, bool select)
bool BKE_id_is_editable(const Main *bmain, const ID *id)
Definition lib_id.cc:2782
bool WM_operator_poll(bContext *C, wmOperatorType *ot)
static bool edbm_fill_grid_prepare(BMesh *bm, int offset, int *span_p, const bool span_calc)
@ BMO_ERROR_FATAL
@ BMO_ERROR_CANCEL
void MESH_OT_dissolve_limited(wmOperatorType *ot)
void WM_cursor_wait(bool val)
static const EnumPropertyItem prop_mesh_face_strength_types[]
MINLINE int min_ii(int a, int b)
static wmOperatorStatus edbm_edge_rotate_selected_exec(bContext *C, wmOperator *op)
void MESH_OT_edge_collapse(wmOperatorType *ot)
static wmOperatorStatus edbm_faces_shade_flat_exec(bContext *C, wmOperator *)
BMesh * BM_mesh_create(const BMAllocTemplate *allocsize, const BMeshCreateParams *params)
BMesh Make Mesh.
void BMO_slot_buffer_from_enabled_hflag(BMesh *bm, BMOperator *op, BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, char htype, char hflag)
static wmOperatorStatus edbm_decimate_exec(bContext *C, wmOperator *op)
static int edbm_bridge_edge_loops_for_single_editmesh(wmOperator *op, Mesh *mesh, const bool use_pairs, const bool use_cyclic, const bool use_merge, const float merge_factor, const int twist_offset)
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)
bool BM_custom_loop_normals_to_vector_layer(BMesh *bm)
static wmOperatorStatus edbm_wireframe_exec(bContext *C, wmOperator *op)
ARegion * CTX_wm_region(const bContext *C)
void ED_workspace_status_text(bContext *C, const char *str)
Definition area.cc:1153
static bool point_normals_draw_check_prop(PointerRNA *ptr, PropertyRNA *prop, void *)
BMEdge * BM_edge_create(BMesh *bm, BMVert *v1, BMVert *v2, const BMEdge *e_example, const eBMCreateFlag create_flag)
Main function for creating a new edge.
float RNA_float_get(PointerRNA *ptr, const char *name)
MINLINE bool is_zero_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
bool BM_vert_pair_share_face_check_cb(BMVert *v_a, BMVert *v_b, bool(*test_fn)(BMFace *, void *user_data), void *user_data)
static void normals_merge(BMesh *bm, BMLoopNorEditDataArray *lnors_ed_arr)
static void point_normals_free(wmOperator *op)
bool RNA_property_is_set(PointerRNA *ptr, PropertyRNA *prop)
BMVert * BM_mesh_active_vert_get(BMesh *bm)
View3D * CTX_wm_view3d(const bContext *C)
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
void BM_mesh_remap(BMesh *bm, const uint *vert_idx, const uint *edge_idx, const uint *face_idx)
MINLINE int compare_ff(float a, float b, float max_diff)
static wmOperatorStatus edbm_collapse_edge_exec(bContext *C, wmOperator *op)
void MESH_OT_edge_split(wmOperatorType *ot)
static const EnumPropertyItem * merge_type_itemf(bContext *C, PointerRNA *, PropertyRNA *, bool *r_free)
void BM_mesh_normals_update(BMesh *bm)
void MESH_OT_remove_doubles(wmOperatorType *ot)
int BM_mesh_calc_edge_groups_as_arrays(BMesh *bm, BMVert **verts, BMEdge **edges, BMFace **faces, int(**r_groups)[3])
void MESH_OT_bridge_edge_loops(wmOperatorType *ot)
void * BMO_slot_buffer_get_first(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
void RNA_float_get_array(PointerRNA *ptr, const char *name, float *values)
static bool flip_custom_normals(BMesh *bm, BMLoopNorEditDataArray *lnors_ed_arr)
void MESH_OT_flip_quad_tessellation(wmOperatorType *ot)
@ EDBM_CLNOR_TOOLS_MULTIPLY
static wmOperatorStatus edbm_beautify_fill_exec(bContext *C, wmOperator *op)
void MESH_OT_edge_rotate(wmOperatorType *ot)
const EnumPropertyItem rna_enum_axis_xyz_items[]
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void * CustomData_bmesh_get_n(const CustomData *data, void *block, eCustomDataType type, int n)
static wmOperatorStatus edbm_rotate_colors_exec(bContext *C, wmOperator *op)
static wmOperatorStatus edbm_fill_holes_exec(bContext *C, wmOperator *op)
int BKE_object_defgroup_active_index_get(const Object *ob)
Definition deform.cc:613
@ BMOP_POKE_MEDIAN_WEIGHTED
static wmOperatorStatus edbm_blend_from_shape_exec(bContext *C, wmOperator *op)
void BM_edge_select_set(BMesh *bm, BMEdge *e, const bool select)
Select Edge.
const btScalar eps
Definition poly34.cpp:11
#define fabsf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
struct BMLoop * l
struct BMEditSelection * next
struct BMEditSelection * prev
BMLoopNorEditData * lnor_editdata
BMLoopNorEditData ** lidx_to_lnor_editdata
struct BMLoop * next
struct BMLoop * prev
struct BMEdge * e
struct BMVert * v
struct BMLoop * radial_next
struct BMFace * f
struct BMOpSlot slots_in[BMO_OP_MAX_SLOTS]
struct BMOpSlot slots_out[BMO_OP_MAX_SLOTS]
struct MLoopNorSpaceArray * lnor_spacearr
struct Object * object
char name[258]
Definition DNA_ID.h:433
ListBaseT< KeyBlock > block
struct LinkData * next
struct LinkNode * next
MLoopNorSpace ** lspacearr
Definition BKE_mesh.h:296
struct LinkNode * loops
Definition BKE_mesh.h:281
const c_style_mat & ptr() const
struct Key * key
bke::ObjectRuntime * runtime
struct Material ** mat
View3DCursor cursor
struct ToolSettings * toolsettings
struct MLoopNorSpaceArray * lnor_spacearr
ListBaseT< BMEditSelection > selected
void prop_search(PointerRNA *ptr, PropertyRNA *prop, PointerRNA *searchptr, PropertyRNA *searchprop, PropertyRNA *item_searchpropname, std::optional< StringRefNull > name, int icon, bool results_are_suggestions)
void use_property_decorate_set(bool is_decorate)
void use_property_split_set(bool is_sep)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)
void active_set(bool active)
Layout & column(bool align)
Layout & row(bool align)
wmEventType type
Definition WM_types.hh:817
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
bool(*) check(bContext *C, wmOperator *op)
Definition WM_types.hh:1133
const char * description
Definition WM_types.hh:1115
void(*) cancel(bContext *C, wmOperator *op)
Definition WM_types.hh:1149
const char * translation_context
Definition WM_types.hh:1113
bool(*) ui_poll(wmOperatorType *ot, PointerRNA *ptr)
Definition WM_types.hh:1181
void(*) ui(bContext *C, wmOperator *op)
Definition WM_types.hh:1176
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
wmOperatorStatus(*) modal(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1157
struct ReportList * reports
struct wmOperatorType * type