Merge branch 'master' into blender2.8
[blender.git] / source / blender / editors / mesh / editmesh_tools.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2004 by Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Joseph Eagar
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/mesh/editmesh_tools.c
29  *  \ingroup edmesh
30  */
31
32 #include <stddef.h>
33
34 #include "MEM_guardedalloc.h"
35
36 #include "DNA_key_types.h"
37 #include "DNA_material_types.h"
38 #include "DNA_mesh_types.h"
39 #include "DNA_meshdata_types.h"
40 #include "DNA_modifier_types.h"
41 #include "DNA_object_types.h"
42 #include "DNA_scene_types.h"
43
44 #include "BLI_listbase.h"
45 #include "BLI_noise.h"
46 #include "BLI_math.h"
47 #include "BLI_rand.h"
48 #include "BLI_sort_utils.h"
49
50 #include "BKE_layer.h"
51 #include "BKE_material.h"
52 #include "BKE_context.h"
53 #include "BKE_deform.h"
54 #include "BKE_report.h"
55 #include "BKE_texture.h"
56 #include "BKE_main.h"
57 #include "BKE_editmesh.h"
58
59 #include "DEG_depsgraph.h"
60 #include "DEG_depsgraph_build.h"
61
62 #include "BLT_translation.h"
63
64 #include "RNA_define.h"
65 #include "RNA_access.h"
66 #include "RNA_enum_types.h"
67
68 #include "WM_api.h"
69 #include "WM_types.h"
70
71 #include "ED_mesh.h"
72 #include "ED_object.h"
73 #include "ED_screen.h"
74 #include "ED_transform.h"
75 #include "ED_transform_snap_object_context.h"
76 #include "ED_uvedit.h"
77 #include "ED_view3d.h"
78
79 #include "RE_render_ext.h"
80
81 #include "UI_interface.h"
82 #include "UI_resources.h"
83
84 #include "mesh_intern.h"  /* own include */
85
86 #include "bmesh_tools.h"
87
88 #define USE_FACE_CREATE_SEL_EXTEND
89
90 /* -------------------------------------------------------------------- */
91 /** \name Subdivide Operator
92  * \{ */
93
94 static int edbm_subdivide_exec(bContext *C, wmOperator *op)
95 {
96         const int cuts = RNA_int_get(op->ptr, "number_cuts");
97         const float smooth = RNA_float_get(op->ptr, "smoothness");
98         const float fractal = RNA_float_get(op->ptr, "fractal") / 2.5f;
99         const float along_normal = RNA_float_get(op->ptr, "fractal_along_normal");
100
101         if (RNA_boolean_get(op->ptr, "quadtri") &&
102             RNA_enum_get(op->ptr, "quadcorner") == SUBD_CORNER_STRAIGHT_CUT)
103         {
104                 RNA_enum_set(op->ptr, "quadcorner", SUBD_CORNER_INNERVERT);
105         }
106         const int quad_corner_type = RNA_enum_get(op->ptr, "quadcorner");
107         const bool use_quad_tri = RNA_boolean_get(op->ptr, "quadtri");
108         const int seed = RNA_int_get(op->ptr, "seed");
109
110         ViewLayer *view_layer = CTX_data_view_layer(C);
111         uint objects_len = 0;
112         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
113
114         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
115                 Object *obedit = objects[ob_index];
116                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
117
118                 if (!(em->bm->totedgesel || em->bm->totfacesel)) {
119                         continue;
120                 }
121
122                 BM_mesh_esubdivide(
123                         em->bm, BM_ELEM_SELECT,
124                         smooth, SUBD_FALLOFF_LIN, false,
125                         fractal, along_normal,
126                         cuts,
127                         SUBDIV_SELECT_ORIG, quad_corner_type,
128                         use_quad_tri, true, false,
129                         seed);
130
131                 EDBM_update_generic(em, true, true);
132         }
133
134         MEM_SAFE_FREE(objects);
135
136         return OPERATOR_FINISHED;
137 }
138
139 /* Note, these values must match delete_mesh() event values */
140 static const EnumPropertyItem prop_mesh_cornervert_types[] = {
141         {SUBD_CORNER_INNERVERT,     "INNERVERT", 0,      "Inner Vert", ""},
142         {SUBD_CORNER_PATH,          "PATH", 0,           "Path", ""},
143         {SUBD_CORNER_STRAIGHT_CUT,  "STRAIGHT_CUT", 0,   "Straight Cut", ""},
144         {SUBD_CORNER_FAN,           "FAN", 0,            "Fan", ""},
145         {0, NULL, 0, NULL, NULL}
146 };
147
148 void MESH_OT_subdivide(wmOperatorType *ot)
149 {
150         PropertyRNA *prop;
151
152         /* identifiers */
153         ot->name = "Subdivide";
154         ot->description = "Subdivide selected edges";
155         ot->idname = "MESH_OT_subdivide";
156
157         /* api callbacks */
158         ot->exec = edbm_subdivide_exec;
159         ot->poll = ED_operator_editmesh;
160
161         /* flags */
162         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
163
164         /* properties */
165         prop = RNA_def_int(ot->srna, "number_cuts", 1, 1, 100, "Number of Cuts", "", 1, 10);
166         /* avoid re-using last var because it can cause _very_ high poly meshes and annoy users (or worse crash) */
167         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
168
169         RNA_def_float(ot->srna, "smoothness", 0.0f, 0.0f, 1e3f, "Smoothness", "Smoothness factor", 0.0f, 1.0f);
170
171         WM_operatortype_props_advanced_begin(ot);
172
173         RNA_def_boolean(ot->srna, "quadtri", 0, "Quad/Tri Mode", "Tries to prevent ngons");
174         RNA_def_enum(ot->srna, "quadcorner", prop_mesh_cornervert_types, SUBD_CORNER_STRAIGHT_CUT,
175                      "Quad Corner Type", "How to subdivide quad corners (anything other than Straight Cut will prevent ngons)");
176
177         RNA_def_float(ot->srna, "fractal", 0.0f, 0.0f, 1e6f, "Fractal", "Fractal randomness factor", 0.0f, 1000.0f);
178         RNA_def_float(ot->srna, "fractal_along_normal", 0.0f, 0.0f, 1.0f,
179                       "Along Normal", "Apply fractal displacement along normal only", 0.0f, 1.0f);
180         RNA_def_int(ot->srna, "seed", 0, 0, INT_MAX, "Random Seed", "Seed for the random number generator", 0, 255);
181 }
182
183 /** \} */
184
185 /* -------------------------------------------------------------------- */
186 /** \name Edge Ring Subdivide Operator
187  *
188  * Bridge code shares props.
189  *
190  * \{ */
191
192 struct EdgeRingOpSubdProps {
193         int interp_mode;
194         int cuts;
195         float smooth;
196
197         int profile_shape;
198         float profile_shape_factor;
199 };
200
201
202 static void mesh_operator_edgering_props(wmOperatorType *ot, const int cuts_min, const int cuts_default)
203 {
204         /* Note, these values must match delete_mesh() event values */
205         static const EnumPropertyItem prop_subd_edgering_types[] = {
206                 {SUBD_RING_INTERP_LINEAR, "LINEAR", 0, "Linear", ""},
207                 {SUBD_RING_INTERP_PATH, "PATH", 0, "Blend Path", ""},
208                 {SUBD_RING_INTERP_SURF, "SURFACE", 0, "Blend Surface", ""},
209                 {0, NULL, 0, NULL, NULL}
210         };
211
212         PropertyRNA *prop;
213
214         prop = RNA_def_int(ot->srna, "number_cuts", cuts_default, 0, 1000, "Number of Cuts", "", cuts_min, 64);
215         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
216
217         RNA_def_enum(ot->srna, "interpolation", prop_subd_edgering_types, SUBD_RING_INTERP_PATH,
218                      "Interpolation", "Interpolation method");
219
220         RNA_def_float(ot->srna, "smoothness", 1.0f, 0.0f, 1e3f,
221                       "Smoothness", "Smoothness factor", 0.0f, 2.0f);
222
223         /* profile-shape */
224         RNA_def_float(ot->srna, "profile_shape_factor", 0.0f, -1e3f, 1e3f,
225                       "Profile Factor", "How much intermediary new edges are shrunk/expanded", -2.0f, 2.0f);
226
227         prop = RNA_def_property(ot->srna, "profile_shape", PROP_ENUM, PROP_NONE);
228         RNA_def_property_enum_items(prop, rna_enum_proportional_falloff_curve_only_items);
229         RNA_def_property_enum_default(prop, PROP_SMOOTH);
230         RNA_def_property_ui_text(prop, "Profile Shape", "Shape of the profile");
231         RNA_def_property_translation_context(prop, BLT_I18NCONTEXT_ID_CURVE); /* Abusing id_curve :/ */
232 }
233
234 static void mesh_operator_edgering_props_get(wmOperator *op, struct EdgeRingOpSubdProps *op_props)
235 {
236         op_props->interp_mode = RNA_enum_get(op->ptr, "interpolation");
237         op_props->cuts = RNA_int_get(op->ptr, "number_cuts");
238         op_props->smooth = RNA_float_get(op->ptr, "smoothness");
239
240         op_props->profile_shape = RNA_enum_get(op->ptr, "profile_shape");
241         op_props->profile_shape_factor = RNA_float_get(op->ptr, "profile_shape_factor");
242 }
243
244 static int edbm_subdivide_edge_ring_exec(bContext *C, wmOperator *op)
245 {
246
247         ViewLayer *view_layer = CTX_data_view_layer(C);
248         uint objects_len = 0;
249         Object * *objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
250         struct EdgeRingOpSubdProps op_props;
251
252         mesh_operator_edgering_props_get(op, &op_props);
253
254         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
255                 Object * obedit = objects[ob_index];
256                 BMEditMesh * em = BKE_editmesh_from_object(obedit);
257
258                 if (em->bm->totedgesel == 0) {
259                         continue;
260                 }
261
262                 if (!EDBM_op_callf(
263                         em, op,
264                         "subdivide_edgering edges=%he interp_mode=%i cuts=%i smooth=%f "
265                         "profile_shape=%i profile_shape_factor=%f",
266                         BM_ELEM_SELECT, op_props.interp_mode, op_props.cuts, op_props.smooth,
267                         op_props.profile_shape, op_props.profile_shape_factor))
268                 {
269                         continue;
270                 }
271
272                 EDBM_update_generic(em, true, true);
273         }
274
275         MEM_freeN(objects);
276         return OPERATOR_FINISHED;
277 }
278
279 void MESH_OT_subdivide_edgering(wmOperatorType *ot)
280 {
281         /* identifiers */
282         ot->name = "Subdivide Edge-Ring";
283         ot->description = "";
284         ot->idname = "MESH_OT_subdivide_edgering";
285
286         /* api callbacks */
287         ot->exec = edbm_subdivide_edge_ring_exec;
288         ot->poll = ED_operator_editmesh;
289
290         /* flags */
291         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
292
293         /* properties */
294         mesh_operator_edgering_props(ot, 1, 10);
295 }
296
297 /** \} */
298
299 /* -------------------------------------------------------------------- */
300 /** \name Un-Subdivide Operator
301  * \{ */
302
303 static int edbm_unsubdivide_exec(bContext *C, wmOperator *op)
304 {
305         const int iterations = RNA_int_get(op->ptr, "iterations");
306         ViewLayer *view_layer = CTX_data_view_layer(C);
307         uint objects_len = 0;
308         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode(view_layer, &objects_len);
309         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
310                 Object *obedit = objects[ob_index];
311                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
312
313                 if ((em->bm->totvertsel == 0) &&
314                     (em->bm->totedgesel == 0) &&
315                     (em->bm->totfacesel == 0))
316                 {
317                         continue;
318                 }
319
320                 BMOperator bmop;
321                 EDBM_op_init(em, &bmop, op,
322                        "unsubdivide verts=%hv iterations=%i", BM_ELEM_SELECT, iterations);
323
324                 BMO_op_exec(em->bm, &bmop);
325
326                 if (!EDBM_op_finish(em, &bmop, op, true)) {
327                         continue;
328                 }
329
330                 if ((em->selectmode & SCE_SELECT_VERTEX) == 0) {
331                         EDBM_selectmode_flush_ex(em, SCE_SELECT_VERTEX);  /* need to flush vert->face first */
332                 }
333                 EDBM_selectmode_flush(em);
334
335                 EDBM_update_generic(em, true, true);
336         }
337         MEM_freeN(objects);
338
339         return OPERATOR_FINISHED;
340 }
341
342 void MESH_OT_unsubdivide(wmOperatorType *ot)
343 {
344         /* identifiers */
345         ot->name = "Un-Subdivide";
346         ot->description = "UnSubdivide selected edges & faces";
347         ot->idname = "MESH_OT_unsubdivide";
348
349         /* api callbacks */
350         ot->exec = edbm_unsubdivide_exec;
351         ot->poll = ED_operator_editmesh;
352
353         /* flags */
354         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
355
356         /* props */
357         RNA_def_int(ot->srna, "iterations", 2, 1, 1000, "Iterations", "Number of times to unsubdivide", 1, 100);
358 }
359
360 void EMBM_project_snap_verts(bContext *C, ARegion *ar, BMEditMesh *em)
361 {
362         Object *obedit = em->ob;
363         BMIter iter;
364         BMVert *eve;
365
366         ED_view3d_init_mats_rv3d(obedit, ar->regiondata);
367
368         struct SnapObjectContext *snap_context = ED_transform_snap_object_context_create_view3d(
369                 CTX_data_main(C), CTX_data_scene(C), CTX_data_depsgraph(C), 0,
370                 ar, CTX_wm_view3d(C));
371
372         BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
373                 if (BM_elem_flag_test(eve, BM_ELEM_SELECT)) {
374                         float mval[2], co_proj[3];
375                         if (ED_view3d_project_float_object(ar, eve->co, mval, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK) {
376                                 if (ED_transform_snap_object_project_view3d_mixed(
377                                         snap_context,
378                                         SCE_SELECT_FACE,
379                                         &(const struct SnapObjectParams){
380                                             .snap_select = SNAP_NOT_ACTIVE,
381                                             .use_object_edit_cage = false,
382                                         },
383                                         mval, NULL, true,
384                                         co_proj, NULL))
385                                 {
386                                         mul_v3_m4v3(eve->co, obedit->imat, co_proj);
387                                 }
388                         }
389                 }
390         }
391
392         ED_transform_snap_object_context_destroy(snap_context);
393 }
394
395 /** \} */
396
397 /* -------------------------------------------------------------------- */
398 /** \name Delete Operator
399  * \{ */
400
401 /* Note, these values must match delete_mesh() event values */
402 enum {
403         MESH_DELETE_VERT      = 0,
404         MESH_DELETE_EDGE      = 1,
405         MESH_DELETE_FACE      = 2,
406         MESH_DELETE_EDGE_FACE = 3,
407         MESH_DELETE_ONLY_FACE = 4,
408 };
409
410 static void edbm_report_delete_info(ReportList *reports, const int totelem_old[3], const int totelem_new[3])
411 {
412         BKE_reportf(reports, RPT_INFO,
413                     "Removed: %d vertices, %d edges, %d faces",
414                     totelem_old[0] - totelem_new[0], totelem_old[1] - totelem_new[1], totelem_old[2] - totelem_new[2]);
415 }
416
417 static int edbm_delete_exec(bContext *C, wmOperator *op)
418 {
419         ViewLayer *view_layer = CTX_data_view_layer(C);
420
421         uint objects_len = 0;
422         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
423         bool changed_multi = false;
424
425         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
426                 Object *obedit = objects[ob_index];
427                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
428                 const int type = RNA_enum_get(op->ptr, "type");
429
430                 switch (type) {
431                         case MESH_DELETE_VERT: /* Erase Vertices */
432                                 if (!(em->bm->totvertsel &&
433                                       EDBM_op_callf(em, op, "delete geom=%hv context=%i", BM_ELEM_SELECT, DEL_VERTS)))
434                                 {
435                                         continue;
436                                 }
437                                 break;
438                         case MESH_DELETE_EDGE: /* Erase Edges */
439                                 if (!(em->bm->totedgesel &&
440                                       EDBM_op_callf(em, op, "delete geom=%he context=%i", BM_ELEM_SELECT, DEL_FACES)))
441                                 {
442                                         continue;
443                                 }
444                                 break;
445                         case MESH_DELETE_FACE: /* Erase Faces */
446                                 if (!(em->bm->totfacesel &&
447                                       EDBM_op_callf(em, op, "delete geom=%hf context=%i", BM_ELEM_SELECT, DEL_FACES)))
448                                 {
449                                         continue;
450                                 }
451                                 break;
452                         case MESH_DELETE_EDGE_FACE:
453                                 /* Edges and Faces */
454                                 if (!((em->bm->totedgesel || em->bm->totfacesel) &&
455                                       EDBM_op_callf(em, op, "delete geom=%hef context=%i", BM_ELEM_SELECT, DEL_EDGESFACES)))
456                                 {
457                                         continue;
458                                 }
459                                 break;
460                         case MESH_DELETE_ONLY_FACE:
461                                 /* Only faces. */
462                                 if (!(em->bm->totfacesel &&
463                                       EDBM_op_callf(em, op, "delete geom=%hf context=%i", BM_ELEM_SELECT, DEL_ONLYFACES)))
464                                 {
465                                         continue;
466                                 }
467                                 break;
468                         default:
469                                 BLI_assert(0);
470                                 break;
471                 }
472
473                 changed_multi = true;
474
475                 EDBM_flag_disable_all(em, BM_ELEM_SELECT);
476
477                 EDBM_update_generic(em, true, true);
478         }
479
480         MEM_SAFE_FREE(objects);
481
482         return changed_multi ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
483 }
484
485 void MESH_OT_delete(wmOperatorType *ot)
486 {
487         static const EnumPropertyItem prop_mesh_delete_types[] = {
488                 {MESH_DELETE_VERT,      "VERT",      0, "Vertices", ""},
489                 {MESH_DELETE_EDGE,      "EDGE",      0, "Edges", ""},
490                 {MESH_DELETE_FACE,      "FACE",      0, "Faces", ""},
491                 {MESH_DELETE_EDGE_FACE, "EDGE_FACE", 0, "Only Edges & Faces", ""},
492                 {MESH_DELETE_ONLY_FACE, "ONLY_FACE", 0, "Only Faces", ""},
493                 {0, NULL, 0, NULL, NULL}
494         };
495
496         /* identifiers */
497         ot->name = "Delete";
498         ot->description = "Delete selected vertices, edges or faces";
499         ot->idname = "MESH_OT_delete";
500
501         /* api callbacks */
502         ot->invoke = WM_menu_invoke;
503         ot->exec = edbm_delete_exec;
504
505         ot->poll = ED_operator_editmesh;
506
507         /* flags */
508         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
509
510         /* props */
511         ot->prop = RNA_def_enum(ot->srna, "type", prop_mesh_delete_types, MESH_DELETE_VERT,
512                                 "Type", "Method used for deleting mesh data");
513 }
514
515 /** \} */
516
517 /* -------------------------------------------------------------------- */
518 /** \name Delete Loose Operator
519  * \{ */
520
521 static bool bm_face_is_loose(BMFace *f)
522 {
523         BMLoop *l_iter, *l_first;
524
525         l_iter = l_first = BM_FACE_FIRST_LOOP(f);
526         do {
527                 if (!BM_edge_is_boundary(l_iter->e)) {
528                         return false;
529                 }
530         } while ((l_iter = l_iter->next) != l_first);
531
532         return true;
533 }
534
535 static int edbm_delete_loose_exec(bContext *C, wmOperator *op)
536 {
537         ViewLayer *view_layer = CTX_data_view_layer(C);
538         int totelem_old_sel[3];
539         int totelem_old[3];
540
541         uint objects_len = 0;
542         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
543
544         EDBM_mesh_stats_multi(objects, objects_len, totelem_old, totelem_old_sel);
545
546         const bool use_verts = (RNA_boolean_get(op->ptr, "use_verts") && totelem_old_sel[0]);
547         const bool use_edges = (RNA_boolean_get(op->ptr, "use_edges") && totelem_old_sel[1]);
548         const bool use_faces = (RNA_boolean_get(op->ptr, "use_faces") && totelem_old_sel[2]);
549
550         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
551                 Object *obedit = objects[ob_index];
552
553                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
554                 BMesh *bm = em->bm;
555                 BMIter iter;
556
557                 BM_mesh_elem_hflag_disable_all(bm, BM_VERT | BM_EDGE | BM_FACE, BM_ELEM_TAG, false);
558
559                 if (use_faces) {
560                         BMFace *f;
561
562                         BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
563                                 if (BM_elem_flag_test(f, BM_ELEM_SELECT)) {
564                                         BM_elem_flag_set(f, BM_ELEM_TAG, bm_face_is_loose(f));
565                                 }
566                         }
567
568                         BM_mesh_delete_hflag_context(bm, BM_ELEM_TAG, DEL_FACES);
569                 }
570
571                 if (use_edges) {
572                         BMEdge *e;
573
574                         BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
575                                 if (BM_elem_flag_test(e, BM_ELEM_SELECT)) {
576                                         BM_elem_flag_set(e, BM_ELEM_TAG, BM_edge_is_wire(e));
577                                 }
578                         }
579
580                         BM_mesh_delete_hflag_context(bm, BM_ELEM_TAG, DEL_EDGES);
581                 }
582
583                 if (use_verts) {
584                         BMVert *v;
585
586                         BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
587                                 if (BM_elem_flag_test(v, BM_ELEM_SELECT)) {
588                                         BM_elem_flag_set(v, BM_ELEM_TAG, (v->e == NULL));
589                                 }
590                         }
591
592                         BM_mesh_delete_hflag_context(bm, BM_ELEM_TAG, DEL_VERTS);
593                 }
594
595                 EDBM_flag_disable_all(em, BM_ELEM_SELECT);
596
597                 EDBM_update_generic(em, true, true);
598         }
599
600         int totelem_new[3];
601         EDBM_mesh_stats_multi(objects, objects_len, totelem_new, NULL);
602
603         edbm_report_delete_info(op->reports, totelem_old, totelem_new);
604
605         MEM_SAFE_FREE(objects);
606
607         return OPERATOR_FINISHED;
608 }
609
610
611 void MESH_OT_delete_loose(wmOperatorType *ot)
612 {
613         /* identifiers */
614         ot->name = "Delete Loose";
615         ot->description = "Delete loose vertices, edges or faces";
616         ot->idname = "MESH_OT_delete_loose";
617
618         /* api callbacks */
619         ot->exec = edbm_delete_loose_exec;
620
621         ot->poll = ED_operator_editmesh;
622
623         /* flags */
624         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
625
626         /* props */
627         RNA_def_boolean(ot->srna, "use_verts", true, "Vertices", "Remove loose vertices");
628         RNA_def_boolean(ot->srna, "use_edges", true, "Edges", "Remove loose edges");
629         RNA_def_boolean(ot->srna, "use_faces", false, "Faces", "Remove loose faces");
630 }
631
632 /** \} */
633
634 /* -------------------------------------------------------------------- */
635 /** \name Collapse Edge Operator
636  * \{ */
637
638 static int edbm_collapse_edge_exec(bContext *C, wmOperator *op)
639 {
640         ViewLayer *view_layer = CTX_data_view_layer(C);
641         uint objects_len = 0;
642         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
643         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
644                 Object *obedit = objects[ob_index];
645                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
646
647                 if (em->bm->totedgesel == 0) {
648                         continue;
649                 }
650
651                 if (!EDBM_op_callf(em, op, "collapse edges=%he uvs=%b", BM_ELEM_SELECT, true)) {
652                         continue;
653                 }
654
655                 EDBM_update_generic(em, true, true);
656         }
657         MEM_freeN(objects);
658
659         return OPERATOR_FINISHED;
660 }
661
662 void MESH_OT_edge_collapse(wmOperatorType *ot)
663 {
664         /* identifiers */
665         ot->name = "Edge Collapse";
666         ot->description = "Collapse selected edges";
667         ot->idname = "MESH_OT_edge_collapse";
668
669         /* api callbacks */
670         ot->exec = edbm_collapse_edge_exec;
671         ot->poll = ED_operator_editmesh;
672
673         /* flags */
674         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
675 }
676
677 /** \} */
678
679 /* -------------------------------------------------------------------- */
680 /** \name Create Edge/Face Operator
681  * \{ */
682
683 static bool edbm_add_edge_face__smooth_get(BMesh *bm)
684 {
685         BMEdge *e;
686         BMIter iter;
687
688         unsigned int vote_on_smooth[2] = {0, 0};
689
690         BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
691                 if (BM_elem_flag_test(e, BM_ELEM_SELECT) && e->l) {
692                         vote_on_smooth[BM_elem_flag_test_bool(e->l->f, BM_ELEM_SMOOTH)]++;
693                 }
694         }
695
696         return (vote_on_smooth[0] < vote_on_smooth[1]);
697 }
698
699 #ifdef USE_FACE_CREATE_SEL_EXTEND
700 /**
701  * Function used to get a fixed number of edges linked to a vertex that passes a test function.
702  * This is used so we can request all boundary edges connected to a vertex for eg.
703  */
704 static int edbm_add_edge_face_exec__vert_edge_lookup(
705         BMVert *v, BMEdge *e_used, BMEdge **e_arr, const int e_arr_len,
706         bool (* func)(const BMEdge *))
707 {
708         BMIter iter;
709         BMEdge *e_iter;
710         int i = 0;
711         BM_ITER_ELEM (e_iter, &iter, v, BM_EDGES_OF_VERT) {
712                 if (BM_elem_flag_test(e_iter, BM_ELEM_HIDDEN) == false) {
713                         if ((e_used == NULL) || (e_used != e_iter)) {
714                                 if (func(e_iter)) {
715                                         e_arr[i++] = e_iter;
716                                         if (i >= e_arr_len) {
717                                                 break;
718                                         }
719                                 }
720                         }
721                 }
722         }
723         return i;
724 }
725
726 static BMElem *edbm_add_edge_face_exec__tricky_extend_sel(BMesh *bm)
727 {
728         BMIter iter;
729         bool found = false;
730
731         if (bm->totvertsel == 1 && bm->totedgesel == 0 && bm->totfacesel == 0) {
732                 /* first look for 2 boundary edges */
733                 BMVert *v;
734
735                 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
736                         if (BM_elem_flag_test(v, BM_ELEM_SELECT)) {
737                                 found = true;
738                                 break;
739                         }
740                 }
741
742                 if (found) {
743                         BMEdge *ed_pair[3];
744                         if (
745                             ((edbm_add_edge_face_exec__vert_edge_lookup(v, NULL, ed_pair, 3, BM_edge_is_wire) == 2) &&
746                              (BM_edge_share_face_check(ed_pair[0], ed_pair[1]) == false)) ||
747
748                             ((edbm_add_edge_face_exec__vert_edge_lookup(v, NULL, ed_pair, 3, BM_edge_is_boundary) == 2) &&
749                              (BM_edge_share_face_check(ed_pair[0], ed_pair[1]) == false))
750                             )
751                         {
752                                 BMEdge *e_other = BM_edge_exists(
753                                         BM_edge_other_vert(ed_pair[0], v),
754                                         BM_edge_other_vert(ed_pair[1], v));
755                                 BM_edge_select_set(bm, ed_pair[0], true);
756                                 BM_edge_select_set(bm, ed_pair[1], true);
757                                 if (e_other) {
758                                         BM_edge_select_set(bm, e_other, true);
759                                 }
760                                 return (BMElem *)v;
761                         }
762                 }
763         }
764         else if (bm->totvertsel == 2 && bm->totedgesel == 1 && bm->totfacesel == 0) {
765                 /* first look for 2 boundary edges */
766                 BMEdge *e;
767
768                 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
769                         if (BM_elem_flag_test(e, BM_ELEM_SELECT)) {
770                                 found = true;
771                                 break;
772                         }
773                 }
774                 if (found) {
775                         BMEdge *ed_pair_v1[2];
776                         BMEdge *ed_pair_v2[2];
777                         if (
778                             ((edbm_add_edge_face_exec__vert_edge_lookup(e->v1, e, ed_pair_v1, 2, BM_edge_is_wire) == 1) &&
779                              (edbm_add_edge_face_exec__vert_edge_lookup(e->v2, e, ed_pair_v2, 2, BM_edge_is_wire) == 1) &&
780                              (BM_edge_share_face_check(e, ed_pair_v1[0]) == false) &&
781                              (BM_edge_share_face_check(e, ed_pair_v2[0]) == false)) ||
782
783 #if 1  /* better support mixed cases [#37203] */
784                             ((edbm_add_edge_face_exec__vert_edge_lookup(e->v1, e, ed_pair_v1, 2, BM_edge_is_wire)     == 1) &&
785                              (edbm_add_edge_face_exec__vert_edge_lookup(e->v2, e, ed_pair_v2, 2, BM_edge_is_boundary) == 1) &&
786                              (BM_edge_share_face_check(e, ed_pair_v1[0]) == false) &&
787                              (BM_edge_share_face_check(e, ed_pair_v2[0]) == false)) ||
788
789                             ((edbm_add_edge_face_exec__vert_edge_lookup(e->v1, e, ed_pair_v1, 2, BM_edge_is_boundary) == 1) &&
790                              (edbm_add_edge_face_exec__vert_edge_lookup(e->v2, e, ed_pair_v2, 2, BM_edge_is_wire)     == 1) &&
791                              (BM_edge_share_face_check(e, ed_pair_v1[0]) == false) &&
792                              (BM_edge_share_face_check(e, ed_pair_v2[0]) == false)) ||
793 #endif
794
795                             ((edbm_add_edge_face_exec__vert_edge_lookup(e->v1, e, ed_pair_v1, 2, BM_edge_is_boundary) == 1) &&
796                              (edbm_add_edge_face_exec__vert_edge_lookup(e->v2, e, ed_pair_v2, 2, BM_edge_is_boundary) == 1) &&
797                              (BM_edge_share_face_check(e, ed_pair_v1[0]) == false) &&
798                              (BM_edge_share_face_check(e, ed_pair_v2[0]) == false))
799                             )
800                         {
801                                 BMVert *v1_other = BM_edge_other_vert(ed_pair_v1[0], e->v1);
802                                 BMVert *v2_other = BM_edge_other_vert(ed_pair_v2[0], e->v2);
803                                 BMEdge *e_other = (v1_other != v2_other) ? BM_edge_exists(v1_other, v2_other) : NULL;
804                                 BM_edge_select_set(bm, ed_pair_v1[0], true);
805                                 BM_edge_select_set(bm, ed_pair_v2[0], true);
806                                 if (e_other) {
807                                         BM_edge_select_set(bm, e_other, true);
808                                 }
809                                 return (BMElem *)e;
810                         }
811                 }
812         }
813
814         return NULL;
815 }
816 static void edbm_add_edge_face_exec__tricky_finalize_sel(BMesh *bm, BMElem *ele_desel, BMFace *f)
817 {
818         /* now we need to find the edge that isnt connected to this element */
819         BM_select_history_clear(bm);
820
821         /* Notes on hidden geometry:
822          * - un-hide the face since its possible hidden was copied when copying surrounding face attributes.
823          * - un-hide before adding to select history
824          *   since we may extend into an existing, hidden vert/edge.
825          */
826
827         BM_elem_flag_disable(f, BM_ELEM_HIDDEN);
828         BM_face_select_set(bm, f, false);
829
830         if (ele_desel->head.htype == BM_VERT) {
831                 BMLoop *l = BM_face_vert_share_loop(f, (BMVert *)ele_desel);
832                 BLI_assert(f->len == 3);
833                 BM_vert_select_set(bm, (BMVert *)ele_desel, false);
834                 BM_edge_select_set(bm, l->next->e, true);
835                 BM_select_history_store(bm, l->next->e);
836         }
837         else {
838                 BMLoop *l = BM_face_edge_share_loop(f, (BMEdge *)ele_desel);
839                 BLI_assert(f->len == 4 || f->len == 3);
840
841                 BM_edge_select_set(bm, (BMEdge *)ele_desel, false);
842                 if (f->len == 4) {
843                         BMEdge *e_active = l->next->next->e;
844                         BM_elem_flag_disable(e_active, BM_ELEM_HIDDEN);
845                         BM_edge_select_set(bm, e_active, true);
846                         BM_select_history_store(bm, e_active);
847                 }
848                 else {
849                         BMVert *v_active = l->next->next->v;
850                         BM_elem_flag_disable(v_active, BM_ELEM_HIDDEN);
851                         BM_vert_select_set(bm, v_active, true);
852                         BM_select_history_store(bm, v_active);
853                 }
854         }
855 }
856 #endif  /* USE_FACE_CREATE_SEL_EXTEND */
857
858 static int edbm_add_edge_face_exec(bContext *C, wmOperator *op)
859 {
860         BMOperator bmop;
861         Object *obedit = CTX_data_edit_object(C);
862         BMEditMesh *em = BKE_editmesh_from_object(obedit);
863         const bool use_smooth = edbm_add_edge_face__smooth_get(em->bm);
864         const int totedge_orig = em->bm->totedge;
865         const int totface_orig = em->bm->totface;
866         /* when this is used to dissolve we could avoid this, but checking isnt too slow */
867
868 #ifdef USE_FACE_CREATE_SEL_EXTEND
869         BMElem *ele_desel;
870         BMFace *ele_desel_face;
871
872         /* be extra clever, figure out if a partial selection should be extended so we can create geometry
873          * with single vert or single edge selection */
874         ele_desel = edbm_add_edge_face_exec__tricky_extend_sel(em->bm);
875 #endif
876
877         if (!EDBM_op_init(
878                     em, &bmop, op,
879                     "contextual_create geom=%hfev mat_nr=%i use_smooth=%b",
880                     BM_ELEM_SELECT, em->mat_nr, use_smooth))
881         {
882                 return OPERATOR_CANCELLED;
883         }
884
885         BMO_op_exec(em->bm, &bmop);
886
887         /* cancel if nothing was done */
888         if ((totedge_orig == em->bm->totedge) &&
889             (totface_orig == em->bm->totface))
890         {
891                 EDBM_op_finish(em, &bmop, op, true);
892                 return OPERATOR_CANCELLED;
893         }
894
895 #ifdef USE_FACE_CREATE_SEL_EXTEND
896         /* normally we would want to leave the new geometry selected,
897          * but being able to press F many times to add geometry is too useful! */
898         if (ele_desel &&
899             (BMO_slot_buffer_count(bmop.slots_out, "faces.out") == 1) &&
900             (ele_desel_face = BMO_slot_buffer_get_first(bmop.slots_out, "faces.out")))
901         {
902                 edbm_add_edge_face_exec__tricky_finalize_sel(em->bm, ele_desel, ele_desel_face);
903         }
904         else
905 #endif
906         {
907                 /* Newly created faces may include existing hidden edges,
908                  * copying face data from surrounding, may have copied hidden face flag too.
909                  *
910                  * Important that faces use flushing since 'edges.out' wont include hidden edges that already existed.
911                  */
912                 BMO_slot_buffer_hflag_disable(em->bm, bmop.slots_out, "faces.out", BM_FACE, BM_ELEM_HIDDEN, true);
913                 BMO_slot_buffer_hflag_disable(em->bm, bmop.slots_out, "edges.out", BM_EDGE, BM_ELEM_HIDDEN, false);
914
915                 BMO_slot_buffer_hflag_enable(em->bm, bmop.slots_out, "faces.out", BM_FACE, BM_ELEM_SELECT, true);
916                 BMO_slot_buffer_hflag_enable(em->bm, bmop.slots_out, "edges.out", BM_EDGE, BM_ELEM_SELECT, true);
917         }
918
919         if (!EDBM_op_finish(em, &bmop, op, true)) {
920                 return OPERATOR_CANCELLED;
921         }
922
923         EDBM_update_generic(em, true, true);
924
925         return OPERATOR_FINISHED;
926 }
927
928 void MESH_OT_edge_face_add(wmOperatorType *ot)
929 {
930         /* identifiers */
931         ot->name = "Make Edge/Face";
932         ot->description = "Add an edge or face to selected";
933         ot->idname = "MESH_OT_edge_face_add";
934
935         /* api callbacks */
936         ot->exec = edbm_add_edge_face_exec;
937         ot->poll = ED_operator_editmesh;
938
939         /* flags */
940         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
941 }
942
943 /** \} */
944
945 /* -------------------------------------------------------------------- */
946 /** \name Mark Edge (Seam) Operator
947  * \{ */
948
949 static int edbm_mark_seam_exec(bContext *C, wmOperator *op)
950 {
951         Scene *scene = CTX_data_scene(C);
952         ViewLayer *view_layer = CTX_data_view_layer(C);
953         BMEdge *eed;
954         BMIter iter;
955         const bool clear = RNA_boolean_get(op->ptr, "clear");
956
957         uint objects_len = 0;
958         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
959         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
960                 Object *obedit = objects[ob_index];
961                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
962                 BMesh *bm = em->bm;
963
964                 if (bm->totedgesel == 0) {
965                         continue;
966                 }
967
968                 Mesh *me = ((Mesh *)obedit->data);
969
970                 /* auto-enable seams drawing */
971                 if (clear == 0) {
972                         me->drawflag |= ME_DRAWSEAMS;
973                 }
974
975                 if (clear) {
976                         BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
977                                 if (!BM_elem_flag_test(eed, BM_ELEM_SELECT) || BM_elem_flag_test(eed, BM_ELEM_HIDDEN)) {
978                                         continue;
979                                 }
980
981                                 BM_elem_flag_disable(eed, BM_ELEM_SEAM);
982                         }
983                 }
984                 else {
985                         BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
986                                 if (!BM_elem_flag_test(eed, BM_ELEM_SELECT) || BM_elem_flag_test(eed, BM_ELEM_HIDDEN)) {
987                                         continue;
988                                 }
989                                 BM_elem_flag_enable(eed, BM_ELEM_SEAM);
990                         }
991                 }
992
993                 ED_uvedit_live_unwrap(scene, obedit);
994                 EDBM_update_generic(em, true, false);
995         }
996         MEM_freeN(objects);
997
998         return OPERATOR_FINISHED;
999 }
1000
1001 void MESH_OT_mark_seam(wmOperatorType *ot)
1002 {
1003         PropertyRNA *prop;
1004
1005         /* identifiers */
1006         ot->name = "Mark Seam";
1007         ot->idname = "MESH_OT_mark_seam";
1008         ot->description = "(Un)mark selected edges as a seam";
1009
1010         /* api callbacks */
1011         ot->exec = edbm_mark_seam_exec;
1012         ot->poll = ED_operator_editmesh;
1013
1014         /* flags */
1015         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1016
1017         prop = RNA_def_boolean(ot->srna, "clear", 0, "Clear", "");
1018         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1019
1020         WM_operatortype_props_advanced_begin(ot);
1021 }
1022
1023 /** \} */
1024
1025 /* -------------------------------------------------------------------- */
1026 /** \name Mark Edge (Sharp) Operator
1027  * \{ */
1028
1029 static int edbm_mark_sharp_exec(bContext *C, wmOperator *op)
1030 {
1031         BMEdge *eed;
1032         BMIter iter;
1033         const bool clear = RNA_boolean_get(op->ptr, "clear");
1034         const bool use_verts = RNA_boolean_get(op->ptr, "use_verts");
1035         ViewLayer *view_layer = CTX_data_view_layer(C);
1036
1037         uint objects_len = 0;
1038         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
1039         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
1040                 Object *obedit = objects[ob_index];
1041                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
1042                 BMesh *bm = em->bm;
1043                 Mesh *me = ((Mesh *)obedit->data);
1044
1045                 if (bm->totedgesel == 0) {
1046                         continue;
1047                 }
1048
1049                 /* auto-enable sharp edge drawing */
1050                 if (clear == 0) {
1051                         me->drawflag |= ME_DRAWSHARP;
1052                 }
1053
1054                 BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
1055                         if (use_verts) {
1056                                 if (!(BM_elem_flag_test(eed->v1, BM_ELEM_SELECT) || BM_elem_flag_test(eed->v2, BM_ELEM_SELECT))) {
1057                                         continue;
1058                                 }
1059                         }
1060                         else if (!BM_elem_flag_test(eed, BM_ELEM_SELECT)) {
1061                                 continue;
1062                         }
1063
1064                         BM_elem_flag_set(eed, BM_ELEM_SMOOTH, clear);
1065                 }
1066
1067                 EDBM_update_generic(em, true, false);
1068         }
1069         MEM_freeN(objects);
1070
1071         return OPERATOR_FINISHED;
1072 }
1073
1074 void MESH_OT_mark_sharp(wmOperatorType *ot)
1075 {
1076         PropertyRNA *prop;
1077
1078         /* identifiers */
1079         ot->name = "Mark Sharp";
1080         ot->idname = "MESH_OT_mark_sharp";
1081         ot->description = "(Un)mark selected edges as sharp";
1082
1083         /* api callbacks */
1084         ot->exec = edbm_mark_sharp_exec;
1085         ot->poll = ED_operator_editmesh;
1086
1087         /* flags */
1088         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1089
1090         prop = RNA_def_boolean(ot->srna, "clear", false, "Clear", "");
1091         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1092         prop = RNA_def_boolean(ot->srna, "use_verts", false, "Vertices",
1093                                "Consider vertices instead of edges to select which edges to (un)tag as sharp");
1094         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
1095 }
1096
1097 static int edbm_vert_connect_exec(bContext *C, wmOperator *op)
1098 {
1099         ViewLayer *view_layer = CTX_data_view_layer(C);
1100         uint objects_len = 0;
1101         uint failed_objects_len = 0;
1102         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
1103         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
1104                 Object *obedit = objects[ob_index];
1105                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
1106                 BMesh *bm = em->bm;
1107                 BMOperator bmop;
1108                 bool is_pair = (bm->totvertsel == 2);
1109                 int len = 0;
1110                 bool check_degenerate = true;
1111                 const int verts_len = bm->totvertsel;
1112                 BMVert **verts;
1113                 bool checks_succeded = true;
1114
1115                 if (!is_pair) {
1116                         continue;
1117                 }
1118
1119                 verts = MEM_mallocN(sizeof(*verts) * verts_len, __func__);
1120                 {
1121                         BMIter iter;
1122                         BMVert *v;
1123                         int i = 0;
1124
1125                         BM_ITER_MESH(v, &iter, bm, BM_VERTS_OF_MESH) {
1126                                 if (BM_elem_flag_test(v, BM_ELEM_SELECT)) {
1127                                         verts[i++] = v;
1128                                 }
1129                         }
1130
1131                         if (is_pair) {
1132                                 if (BM_vert_pair_share_face_check_cb(
1133                                         verts[0], verts[1],
1134                                         BM_elem_cb_check_hflag_disabled_simple(BMFace *, BM_ELEM_HIDDEN)))
1135                                 {
1136                                         check_degenerate = false;
1137                                         is_pair = false;
1138                                 }
1139                         }
1140                 }
1141                 
1142                 if (is_pair) {
1143                         if (!EDBM_op_init(
1144                                 em, &bmop, op,
1145                                 "connect_vert_pair verts=%eb verts_exclude=%hv faces_exclude=%hf",
1146                                 verts, verts_len, BM_ELEM_HIDDEN, BM_ELEM_HIDDEN))
1147                         {
1148                                 checks_succeded = false;
1149                         }
1150                 }
1151                 else {
1152                         if (!EDBM_op_init(
1153                                 em, &bmop, op,
1154                                 "connect_verts verts=%eb faces_exclude=%hf check_degenerate=%b",
1155                                 verts, verts_len, BM_ELEM_HIDDEN, check_degenerate))
1156                         {
1157                                 checks_succeded = false;
1158                         }
1159                 }
1160                 if (checks_succeded)
1161                 {
1162                         BMO_op_exec(bm, &bmop);
1163                         len = BMO_slot_get(bmop.slots_out, "edges.out")->len;
1164
1165                         if (len) {
1166                                 if (is_pair) {
1167                                         /* new verts have been added, we have to select the edges, not just flush */
1168                                         BMO_slot_buffer_hflag_enable(em->bm, bmop.slots_out, "edges.out", BM_EDGE, BM_ELEM_SELECT, true);
1169                                 }
1170                         }
1171
1172                         if (!EDBM_op_finish(em, &bmop, op, true)) {
1173                                 len = 0;
1174                         }
1175                         else {
1176                                 EDBM_selectmode_flush(em);  /* so newly created edges get the selection state from the vertex */
1177
1178                                 EDBM_update_generic(em, true, true);
1179                         }
1180                 }
1181                 MEM_freeN(verts);
1182                 if (len == 0)
1183                 {
1184                         failed_objects_len++;
1185                 }
1186         }
1187         MEM_freeN(objects);
1188         return failed_objects_len == objects_len ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
1189 }
1190
1191 void MESH_OT_vert_connect(wmOperatorType *ot)
1192 {
1193         /* identifiers */
1194         ot->name = "Vertex Connect";
1195         ot->idname = "MESH_OT_vert_connect";
1196         ot->description = "Connect selected vertices of faces, splitting the face";
1197
1198         /* api callbacks */
1199         ot->exec = edbm_vert_connect_exec;
1200         ot->poll = ED_operator_editmesh;
1201
1202         /* flags */
1203         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1204 }
1205
1206 /** \} */
1207
1208 /* -------------------------------------------------------------------- */
1209 /** \name Split Concave Faces Operator
1210  * \{ */
1211
1212 /**
1213  * check that endpoints are verts and only have a single selected edge connected.
1214  */
1215 static bool bm_vert_is_select_history_open(BMesh *bm)
1216 {
1217         BMEditSelection *ele_a = bm->selected.first;
1218         BMEditSelection *ele_b = bm->selected.last;
1219         if ((ele_a->htype == BM_VERT) &&
1220             (ele_b->htype == BM_VERT))
1221         {
1222                 if ((BM_iter_elem_count_flag(BM_EDGES_OF_VERT, (BMVert *)ele_a->ele, BM_ELEM_SELECT, true) == 1) &&
1223                     (BM_iter_elem_count_flag(BM_EDGES_OF_VERT, (BMVert *)ele_b->ele, BM_ELEM_SELECT, true) == 1))
1224                 {
1225                         return true;
1226                 }
1227         }
1228
1229         return false;
1230 }
1231
1232 static bool bm_vert_connect_pair(BMesh *bm, BMVert *v_a, BMVert *v_b)
1233 {
1234         BMOperator bmop;
1235         BMVert **verts;
1236         const int totedge_orig = bm->totedge;
1237
1238         BMO_op_init(bm, &bmop, BMO_FLAG_DEFAULTS, "connect_vert_pair");
1239
1240         verts = BMO_slot_buffer_alloc(&bmop, bmop.slots_in, "verts", 2);
1241         verts[0] = v_a;
1242         verts[1] = v_b;
1243
1244         BM_vert_normal_update(verts[0]);
1245         BM_vert_normal_update(verts[1]);
1246
1247         BMO_op_exec(bm, &bmop);
1248         BMO_slot_buffer_hflag_enable(bm, bmop.slots_out, "edges.out", BM_EDGE, BM_ELEM_SELECT, true);
1249         BMO_op_finish(bm, &bmop);
1250         return (bm->totedge != totedge_orig);
1251 }
1252
1253 static bool bm_vert_connect_select_history(BMesh *bm)
1254 {
1255         /* Logic is as follows:
1256          *
1257          * - If there are any isolated/wire verts - connect as edges.
1258          * - Otherwise connect faces.
1259          * - If all edges have been created already, closed the loop.
1260          */
1261         if (BLI_listbase_count_at_most(&bm->selected, 2) == 2 && (bm->totvertsel > 2)) {
1262                 BMEditSelection *ese;
1263                 int tot = 0;
1264                 bool changed = false;
1265                 bool has_wire = false;
1266                 // bool all_verts;
1267
1268                 /* ensure all verts have history */
1269                 for (ese = bm->selected.first; ese; ese = ese->next, tot++) {
1270                         BMVert *v;
1271                         if (ese->htype != BM_VERT) {
1272                                 break;
1273                         }
1274                         v = (BMVert *)ese->ele;
1275                         if ((has_wire == false) && ((v->e == NULL) || BM_vert_is_wire(v))) {
1276                                 has_wire = true;
1277                         }
1278                 }
1279                 // all_verts = (ese == NULL);
1280
1281                 if (has_wire == false) {
1282                         /* all verts have faces , connect verts via faces! */
1283                         if (tot == bm->totvertsel) {
1284                                 BMEditSelection *ese_last;
1285                                 ese_last = bm->selected.first;
1286                                 ese = ese_last->next;
1287
1288                                 do {
1289
1290                                         if (BM_edge_exists((BMVert *)ese_last->ele, (BMVert *)ese->ele)) {
1291                                                 /* pass, edge exists (and will be selected) */
1292                                         }
1293                                         else {
1294                                                 changed |= bm_vert_connect_pair(bm, (BMVert *)ese_last->ele, (BMVert *)ese->ele);
1295                                         }
1296                                 } while ((void)
1297                                          (ese_last = ese),
1298                                          (ese = ese->next));
1299
1300                                 if (changed) {
1301                                         return true;
1302                                 }
1303                         }
1304
1305                         if (changed == false) {
1306                                 /* existing loops: close the selection */
1307                                 if (bm_vert_is_select_history_open(bm)) {
1308                                         changed |= bm_vert_connect_pair(
1309                                                 bm,
1310                                                 (BMVert *)((BMEditSelection *)bm->selected.first)->ele,
1311                                                 (BMVert *)((BMEditSelection *)bm->selected.last)->ele);
1312
1313                                         if (changed) {
1314                                                 return true;
1315                                         }
1316                                 }
1317                         }
1318                 }
1319
1320                 else {
1321                         /* no faces, simply connect the verts by edges */
1322                         BMEditSelection *ese_prev;
1323                         ese_prev = bm->selected.first;
1324                         ese = ese_prev->next;
1325
1326
1327                         do {
1328                                 if (BM_edge_exists((BMVert *)ese_prev->ele, (BMVert *)ese->ele)) {
1329                                         /* pass, edge exists (and will be selected) */
1330                                 }
1331                                 else {
1332                                         BMEdge *e;
1333                                         e = BM_edge_create(bm, (BMVert *)ese_prev->ele, (BMVert *)ese->ele, NULL, 0);
1334                                         BM_edge_select_set(bm, e, true);
1335                                         changed = true;
1336                                 }
1337                         } while ((void)
1338                                  (ese_prev = ese),
1339                                  (ese = ese->next));
1340
1341                         if (changed == false) {
1342                                 /* existing loops: close the selection */
1343                                 if (bm_vert_is_select_history_open(bm)) {
1344                                         BMEdge *e;
1345                                         ese_prev = bm->selected.first;
1346                                         ese = bm->selected.last;
1347                                         e = BM_edge_create(bm, (BMVert *)ese_prev->ele, (BMVert *)ese->ele, NULL, 0);
1348                                         BM_edge_select_set(bm, e, true);
1349                                 }
1350                         }
1351
1352                         return true;
1353                 }
1354         }
1355
1356         return false;
1357 }
1358
1359 /**
1360  * Convert an edge selection to a temp vertex selection
1361  * (which must be cleared after use as a path to connect).
1362  */
1363 static bool bm_vert_connect_select_history_edge_to_vert_path(BMesh *bm, ListBase *r_selected)
1364 {
1365         ListBase selected_orig = {NULL, NULL};
1366         BMEditSelection *ese;
1367         int edges_len = 0;
1368         bool side = false;
1369
1370         /* first check all edges are OK */
1371         for (ese = bm->selected.first; ese; ese = ese->next) {
1372                 if (ese->htype == BM_EDGE) {
1373                         edges_len += 1;
1374                 }
1375                 else {
1376                         return false;
1377                 }
1378         }
1379         /* if this is a mixed selection, bail out! */
1380         if (bm->totedgesel != edges_len) {
1381                 return false;
1382         }
1383
1384         SWAP(ListBase, bm->selected, selected_orig);
1385
1386         /* convert edge selection into 2 ordered loops (where the first edge ends up in the middle) */
1387         for (ese = selected_orig.first; ese; ese = ese->next) {
1388                 BMEdge *e_curr = (BMEdge *)ese->ele;
1389                 BMEdge *e_prev = ese->prev ? (BMEdge *)ese->prev->ele : NULL;
1390                 BMLoop *l_curr;
1391                 BMLoop *l_prev;
1392                 BMVert *v;
1393
1394                 if (e_prev) {
1395                         BMFace *f = BM_edge_pair_share_face_by_len(e_curr, e_prev, &l_curr, &l_prev, true);
1396                         if (f) {
1397                                 if ((e_curr->v1 != l_curr->v) == (e_prev->v1 != l_prev->v)) {
1398                                         side = !side;
1399                                 }
1400                         }
1401                         else if (is_quad_flip_v3(e_curr->v1->co, e_curr->v2->co, e_prev->v2->co, e_prev->v1->co)) {
1402                                 side = !side;
1403                         }
1404                 }
1405
1406                 v = (&e_curr->v1)[side];
1407                 if (!bm->selected.last || (BMVert *)((BMEditSelection *)bm->selected.last)->ele != v) {
1408                         BM_select_history_store_notest(bm, v);
1409                 }
1410
1411                 v = (&e_curr->v1)[!side];
1412                 if (!bm->selected.first || (BMVert *)((BMEditSelection *)bm->selected.first)->ele != v) {
1413                         BM_select_history_store_head_notest(bm, v);
1414                 }
1415
1416                 e_prev = e_curr;
1417         }
1418
1419         *r_selected = bm->selected;
1420         bm->selected = selected_orig;
1421
1422         return true;
1423 }
1424
1425 static int edbm_vert_connect_path_exec(bContext *C, wmOperator *op)
1426 {
1427         Object *obedit = CTX_data_edit_object(C);
1428         BMEditMesh *em = BKE_editmesh_from_object(obedit);
1429         BMesh *bm = em->bm;
1430         bool is_pair = (em->bm->totvertsel == 2);
1431         ListBase selected_orig = {NULL, NULL};
1432         int retval;
1433
1434         /* when there is only 2 vertices, we can ignore selection order */
1435         if (is_pair) {
1436                 return edbm_vert_connect_exec(C, op);
1437         }
1438
1439         if (bm->selected.first) {
1440                 BMEditSelection *ese = bm->selected.first;
1441                 if (ese->htype == BM_EDGE) {
1442                         if (bm_vert_connect_select_history_edge_to_vert_path(bm, &selected_orig)) {
1443                                 SWAP(ListBase, bm->selected, selected_orig);
1444                         }
1445                 }
1446         }
1447
1448         if (bm_vert_connect_select_history(bm)) {
1449                 EDBM_selectmode_flush(em);
1450                 EDBM_update_generic(em, true, true);
1451                 retval = OPERATOR_FINISHED;
1452         }
1453         else {
1454                 BKE_report(op->reports, RPT_ERROR, "Invalid selection order");
1455                 retval = OPERATOR_CANCELLED;
1456         }
1457
1458         if (!BLI_listbase_is_empty(&selected_orig)) {
1459                 BM_select_history_clear(bm);
1460                 bm->selected = selected_orig;
1461         }
1462
1463         return retval;
1464 }
1465
1466 void MESH_OT_vert_connect_path(wmOperatorType *ot)
1467 {
1468         /* identifiers */
1469         ot->name = "Vertex Connect Path";
1470         ot->idname = "MESH_OT_vert_connect_path";
1471         ot->description = "Connect vertices by their selection order, creating edges, splitting faces";
1472
1473         /* api callbacks */
1474         ot->exec = edbm_vert_connect_path_exec;
1475         ot->poll = ED_operator_editmesh;
1476
1477         /* flags */
1478         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1479 }
1480
1481 static int edbm_vert_connect_concave_exec(bContext *C, wmOperator *op)
1482 {
1483         Object *obedit = CTX_data_edit_object(C);
1484         BMEditMesh *em = BKE_editmesh_from_object(obedit);
1485
1486         if (!EDBM_op_call_and_selectf(
1487                      em, op,
1488                      "faces.out", true,
1489                      "connect_verts_concave faces=%hf",
1490                      BM_ELEM_SELECT))
1491         {
1492                 return OPERATOR_CANCELLED;
1493         }
1494
1495
1496         EDBM_update_generic(em, true, true);
1497         return OPERATOR_FINISHED;
1498 }
1499
1500 void MESH_OT_vert_connect_concave(wmOperatorType *ot)
1501 {
1502         /* identifiers */
1503         ot->name = "Split Concave Faces";
1504         ot->idname = "MESH_OT_vert_connect_concave";
1505         ot->description = "Make all faces convex";
1506
1507         /* api callbacks */
1508         ot->exec = edbm_vert_connect_concave_exec;
1509         ot->poll = ED_operator_editmesh;
1510
1511         /* flags */
1512         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1513 }
1514
1515 /** \} */
1516
1517 /* -------------------------------------------------------------------- */
1518 /** \name Split Non-Planar Faces Operator
1519  * \{ */
1520
1521 static int edbm_vert_connect_nonplaner_exec(bContext *C, wmOperator *op)
1522 {
1523         Object *obedit = CTX_data_edit_object(C);
1524         BMEditMesh *em = BKE_editmesh_from_object(obedit);
1525
1526         const float angle_limit = RNA_float_get(op->ptr, "angle_limit");
1527
1528         if (!EDBM_op_call_and_selectf(
1529                      em, op,
1530                      "faces.out", true,
1531                      "connect_verts_nonplanar faces=%hf angle_limit=%f",
1532                      BM_ELEM_SELECT, angle_limit))
1533         {
1534                 return OPERATOR_CANCELLED;
1535         }
1536
1537
1538         EDBM_update_generic(em, true, true);
1539         return OPERATOR_FINISHED;
1540 }
1541
1542 void MESH_OT_vert_connect_nonplanar(wmOperatorType *ot)
1543 {
1544         PropertyRNA *prop;
1545
1546         /* identifiers */
1547         ot->name = "Split Non-Planar Faces";
1548         ot->idname = "MESH_OT_vert_connect_nonplanar";
1549         ot->description = "Split non-planar faces that exceed the angle threshold";
1550
1551         /* api callbacks */
1552         ot->exec = edbm_vert_connect_nonplaner_exec;
1553         ot->poll = ED_operator_editmesh;
1554
1555         /* flags */
1556         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1557
1558         /* props */
1559         prop = RNA_def_float_rotation(ot->srna, "angle_limit", 0, NULL, 0.0f, DEG2RADF(180.0f),
1560                                       "Max Angle", "Angle limit", 0.0f, DEG2RADF(180.0f));
1561         RNA_def_property_float_default(prop, DEG2RADF(5.0f));
1562 }
1563
1564 /** \} */
1565
1566 /* -------------------------------------------------------------------- */
1567 /** \name Make Planar Faces Operator
1568  * \{ */
1569
1570 static int edbm_face_make_planar_exec(bContext *C, wmOperator *op)
1571 {
1572         ViewLayer *view_layer = CTX_data_view_layer(C);
1573         uint objects_len = 0;
1574         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
1575
1576         const int repeat = RNA_int_get(op->ptr, "repeat");
1577         const float fac = RNA_float_get(op->ptr, "factor");
1578
1579         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
1580                 Object *obedit = objects[ob_index];
1581                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
1582                 if (em->bm->totfacesel == 0) {
1583                         continue;
1584                 }
1585
1586                 if (!EDBM_op_callf(
1587                             em, op, "planar_faces faces=%hf iterations=%i factor=%f",
1588                             BM_ELEM_SELECT, repeat, fac))
1589                 {
1590                         continue;
1591                 }
1592
1593                 EDBM_update_generic(em, true, true);
1594         }
1595         MEM_freeN(objects);
1596
1597         return OPERATOR_FINISHED;
1598 }
1599
1600 void MESH_OT_face_make_planar(wmOperatorType *ot)
1601 {
1602         /* identifiers */
1603         ot->name = "Make Planar Faces";
1604         ot->idname = "MESH_OT_face_make_planar";
1605         ot->description = "Flatten selected faces";
1606
1607         /* api callbacks */
1608         ot->exec = edbm_face_make_planar_exec;
1609         ot->poll = ED_operator_editmesh;
1610
1611         /* flags */
1612         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1613
1614         /* props */
1615         RNA_def_float(ot->srna, "factor", 1.0f, -10.0f, 10.0f, "Factor", "", 0.0f, 1.0f);
1616         RNA_def_int(ot->srna, "repeat", 1, 1, 10000, "Iterations", "", 1, 200);
1617 }
1618
1619 /** \} */
1620
1621 /* -------------------------------------------------------------------- */
1622 /** \name Split Edge Operator
1623  * \{ */
1624
1625 static int edbm_edge_split_exec(bContext *C, wmOperator *op)
1626 {
1627         ViewLayer *view_layer = CTX_data_view_layer(C);
1628         uint objects_len = 0;
1629         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
1630         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
1631                 Object *obedit = objects[ob_index];
1632                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
1633                 if (em->bm->totedgesel == 0) {
1634                         continue;
1635                 }
1636
1637                 if (!EDBM_op_call_and_selectf(
1638                             em, op,
1639                             "edges.out", false,
1640                             "split_edges edges=%he",
1641                             BM_ELEM_SELECT))
1642                 {
1643                         continue;
1644                 }
1645
1646                 if (em->selectmode == SCE_SELECT_FACE) {
1647                         EDBM_select_flush(em);
1648                 }
1649
1650                 EDBM_update_generic(em, true, true);
1651         }
1652         MEM_freeN(objects);
1653
1654         return OPERATOR_FINISHED;
1655 }
1656
1657 void MESH_OT_edge_split(wmOperatorType *ot)
1658 {
1659         /* identifiers */
1660         ot->name = "Edge Split";
1661         ot->idname = "MESH_OT_edge_split";
1662         ot->description = "Split selected edges so that each neighbor face gets its own copy";
1663
1664         /* api callbacks */
1665         ot->exec = edbm_edge_split_exec;
1666         ot->poll = ED_operator_editmesh;
1667
1668         /* flags */
1669         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1670 }
1671
1672 /** \} */
1673
1674 /* -------------------------------------------------------------------- */
1675 /** \name Duplicate Operator
1676  * \{ */
1677
1678 static int edbm_duplicate_exec(bContext *C, wmOperator *op)
1679 {
1680         ViewLayer *view_layer = CTX_data_view_layer(C);
1681         uint objects_len = 0;
1682         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
1683
1684         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
1685                 Object *obedit = objects[ob_index];
1686                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
1687                 if (em->bm->totvertsel == 0) {
1688                         continue;
1689                 }
1690
1691                 BMOperator bmop;
1692                 BMesh *bm = em->bm;
1693
1694                 EDBM_op_init(
1695                         em, &bmop, op,
1696                         "duplicate geom=%hvef use_select_history=%b",
1697                         BM_ELEM_SELECT, true);
1698
1699                 BMO_op_exec(bm, &bmop);
1700
1701                 /* de-select all would clear otherwise */
1702                 BM_SELECT_HISTORY_BACKUP(bm);
1703
1704                 EDBM_flag_disable_all(em, BM_ELEM_SELECT);
1705
1706                 BMO_slot_buffer_hflag_enable(bm, bmop.slots_out, "geom.out", BM_ALL_NOLOOP, BM_ELEM_SELECT, true);
1707
1708                 /* rebuild editselection */
1709                 BM_SELECT_HISTORY_RESTORE(bm);
1710
1711                 if (!EDBM_op_finish(em, &bmop, op, true)) {
1712                         continue;
1713                 }
1714                 EDBM_update_generic(em, true, true);
1715         }
1716         MEM_freeN(objects);
1717
1718         return OPERATOR_FINISHED;
1719 }
1720
1721 static int edbm_duplicate_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
1722 {
1723         WM_cursor_wait(1);
1724         edbm_duplicate_exec(C, op);
1725         WM_cursor_wait(0);
1726
1727         return OPERATOR_FINISHED;
1728 }
1729
1730 void MESH_OT_duplicate(wmOperatorType *ot)
1731 {
1732         /* identifiers */
1733         ot->name = "Duplicate";
1734         ot->description = "Duplicate selected vertices, edges or faces";
1735         ot->idname = "MESH_OT_duplicate";
1736
1737         /* api callbacks */
1738         ot->invoke = edbm_duplicate_invoke;
1739         ot->exec = edbm_duplicate_exec;
1740
1741         ot->poll = ED_operator_editmesh;
1742
1743         /* to give to transform */
1744         RNA_def_int(ot->srna, "mode", TFM_TRANSLATION, 0, INT_MAX, "Mode", "", 0, INT_MAX);
1745 }
1746
1747 /** \} */
1748
1749 /* -------------------------------------------------------------------- */
1750 /** \name Flip Normals Operator
1751  * \{ */
1752 static int edbm_flip_normals_exec(bContext *C, wmOperator *op)
1753 {
1754         ViewLayer *view_layer = CTX_data_view_layer(C);
1755         uint objects_len = 0;
1756         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
1757
1758         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
1759                 Object *obedit = objects[ob_index];
1760                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
1761
1762                 if (em->bm->totfacesel == 0) {
1763                         continue;
1764                 }
1765
1766                 if (!EDBM_op_callf(
1767                         em, op, "reverse_faces faces=%hf flip_multires=%b",
1768                         BM_ELEM_SELECT, true))
1769                 {
1770                         continue;
1771                 }
1772
1773                 EDBM_update_generic(em, true, false);
1774         }
1775
1776         MEM_freeN(objects);
1777         return OPERATOR_FINISHED;
1778 }
1779
1780 void MESH_OT_flip_normals(wmOperatorType *ot)
1781 {
1782         /* identifiers */
1783         ot->name = "Flip Normals";
1784         ot->description = "Flip the direction of selected faces' normals (and of their vertices)";
1785         ot->idname = "MESH_OT_flip_normals";
1786
1787         /* api callbacks */
1788         ot->exec = edbm_flip_normals_exec;
1789         ot->poll = ED_operator_editmesh;
1790
1791         /* flags */
1792         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1793 }
1794
1795 /** \} */
1796
1797 /* -------------------------------------------------------------------- */
1798 /** \name Rotate Edge Operator
1799  * \{ */
1800
1801 /**
1802  * Rotate the edges between selected faces, otherwise rotate the selected edges.
1803  */
1804 static int edbm_edge_rotate_selected_exec(bContext *C, wmOperator *op)
1805 {
1806         BMEdge *eed;
1807         BMIter iter;
1808         const bool use_ccw = RNA_boolean_get(op->ptr, "use_ccw");
1809
1810         int tot_rotate_all = 0, tot_failed_all = 0;
1811         bool no_selected_edges = true, invalid_selected_edges = true;
1812
1813         ViewLayer *view_layer = CTX_data_view_layer(C);
1814         uint objects_len = 0;
1815         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
1816         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
1817                 Object *obedit = objects[ob_index];
1818                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
1819                 int tot = 0;
1820
1821                 if (em->bm->totedgesel == 0) {
1822                         continue;
1823                 }
1824                 no_selected_edges = false;
1825
1826                 /* first see if we have two adjacent faces */
1827                 BM_ITER_MESH (eed, &iter, em->bm, BM_EDGES_OF_MESH) {
1828                         BM_elem_flag_disable(eed, BM_ELEM_TAG);
1829                         if (BM_elem_flag_test(eed, BM_ELEM_SELECT)) {
1830                                 BMFace *fa, *fb;
1831                                 if (BM_edge_face_pair(eed, &fa, &fb)) {
1832                                         /* if both faces are selected we rotate between them,
1833                                          * otherwise - rotate between 2 unselected - but not mixed */
1834                                         if (BM_elem_flag_test(fa, BM_ELEM_SELECT) == BM_elem_flag_test(fb, BM_ELEM_SELECT)) {
1835                                                 BM_elem_flag_enable(eed, BM_ELEM_TAG);
1836                                                 tot++;
1837                                         }
1838                                 }
1839                         }
1840                 }
1841
1842                 /* ok, we don't have two adjacent faces, but we do have two selected ones.
1843                  * that's an error condition.*/
1844                 if (tot == 0) {
1845                         continue;
1846                 }
1847                 invalid_selected_edges = false;
1848
1849                 BMOperator bmop;
1850                 EDBM_op_init(em, &bmop, op, "rotate_edges edges=%he use_ccw=%b", BM_ELEM_TAG, use_ccw);
1851
1852                 /* avoids leaving old verts selected which can be a problem running multiple times,
1853                  * since this means the edges become selected around the face which then attempt to rotate */
1854                 BMO_slot_buffer_hflag_disable(em->bm, bmop.slots_in, "edges", BM_EDGE, BM_ELEM_SELECT, true);
1855
1856                 BMO_op_exec(em->bm, &bmop);
1857                 /* edges may rotate into hidden vertices, if this does _not_ run we get an ilogical state */
1858                 BMO_slot_buffer_hflag_disable(em->bm, bmop.slots_out, "edges.out", BM_EDGE, BM_ELEM_HIDDEN, true);
1859                 BMO_slot_buffer_hflag_enable(em->bm, bmop.slots_out, "edges.out", BM_EDGE, BM_ELEM_SELECT, true);
1860
1861                 const int tot_rotate = BMO_slot_buffer_count(bmop.slots_out, "edges.out");
1862                 const int tot_failed = tot - tot_rotate;
1863
1864                 tot_rotate_all += tot_rotate;
1865                 tot_failed_all += tot_failed;
1866
1867                 if (tot_failed != 0) {
1868                         /* If some edges fail to rotate, we need to re-select them,
1869                          * otherwise we can end up with invalid selection
1870                          * (unselected edge between 2 selected faces). */
1871                         BM_mesh_elem_hflag_enable_test(em->bm, BM_EDGE, BM_ELEM_SELECT, true, false, BM_ELEM_TAG);
1872                 }
1873
1874                 EDBM_selectmode_flush(em);
1875
1876                 if (!EDBM_op_finish(em, &bmop, op, true)) {
1877                         continue;
1878                 }
1879
1880                 EDBM_update_generic(em, true, true);
1881         }
1882         MEM_freeN(objects);
1883
1884         if (no_selected_edges) {
1885                 BKE_report(op->reports, RPT_ERROR, "Select edges or face pairs for edge loops to rotate about");
1886                 return OPERATOR_CANCELLED;
1887         }
1888
1889         /* Ok, we don't have two adjacent faces, but we do have two selected ones.
1890          * that's an error condition. */
1891         if (invalid_selected_edges) {
1892                 BKE_report(op->reports, RPT_ERROR, "Could not find any selected edges that can be rotated");
1893                 return OPERATOR_CANCELLED;
1894         }
1895
1896         if (tot_failed_all != 0) {
1897                 BKE_reportf(op->reports, RPT_WARNING, "Unable to rotate %d edge(s)", tot_failed_all);
1898         }
1899
1900         return OPERATOR_FINISHED;
1901 }
1902
1903 void MESH_OT_edge_rotate(wmOperatorType *ot)
1904 {
1905         /* identifiers */
1906         ot->name = "Rotate Selected Edge";
1907         ot->description = "Rotate selected edge or adjoining faces";
1908         ot->idname = "MESH_OT_edge_rotate";
1909
1910         /* api callbacks */
1911         ot->exec = edbm_edge_rotate_selected_exec;
1912         ot->poll = ED_operator_editmesh;
1913
1914         /* flags */
1915         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1916
1917         /* props */
1918         RNA_def_boolean(ot->srna, "use_ccw", false, "Counter Clockwise", "");
1919 }
1920
1921 /** \} */
1922
1923 /* -------------------------------------------------------------------- */
1924 /** \name Hide Operator
1925  * \{ */
1926
1927 static int edbm_hide_exec(bContext *C, wmOperator *op)
1928 {
1929         const bool unselected = RNA_boolean_get(op->ptr, "unselected");
1930         ViewLayer *view_layer = CTX_data_view_layer(C);
1931
1932         uint objects_len = 0;
1933         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
1934         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
1935                 Object *obedit = objects[ob_index];
1936                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
1937                 BMesh *bm = em->bm;
1938
1939                 if ((bm->totvertsel == 0) &&
1940                     (bm->totedgesel == 0) &&
1941                     (bm->totfacesel == 0))
1942                 {
1943                         continue;
1944                 }
1945
1946                 EDBM_mesh_hide(em, unselected);
1947                 EDBM_update_generic(em, true, false);
1948         }
1949
1950         MEM_freeN(objects);
1951         return OPERATOR_FINISHED;
1952 }
1953
1954 void MESH_OT_hide(wmOperatorType *ot)
1955 {
1956         /* identifiers */
1957         ot->name = "Hide Selection";
1958         ot->idname = "MESH_OT_hide";
1959         ot->description = "Hide (un)selected vertices, edges or faces";
1960
1961         /* api callbacks */
1962         ot->exec = edbm_hide_exec;
1963         ot->poll = ED_operator_editmesh;
1964
1965         /* flags */
1966         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1967
1968         /* props */
1969         RNA_def_boolean(ot->srna, "unselected", false, "Unselected", "Hide unselected rather than selected");
1970 }
1971
1972 /** \} */
1973
1974 /* -------------------------------------------------------------------- */
1975 /** \name Reveal Operator
1976  * \{ */
1977
1978 static int edbm_reveal_exec(bContext *C, wmOperator *op)
1979 {
1980         const bool select = RNA_boolean_get(op->ptr, "select");
1981         ViewLayer *view_layer = CTX_data_view_layer(C);
1982
1983         uint objects_len = 0;
1984         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
1985         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
1986                 Object *obedit = objects[ob_index];
1987                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
1988
1989                 EDBM_mesh_reveal(em, select);
1990                 EDBM_update_generic(em, true, false);
1991         }
1992         MEM_freeN(objects);
1993
1994         return OPERATOR_FINISHED;
1995 }
1996
1997 void MESH_OT_reveal(wmOperatorType *ot)
1998 {
1999         /* identifiers */
2000         ot->name = "Reveal Hidden";
2001         ot->idname = "MESH_OT_reveal";
2002         ot->description = "Reveal all hidden vertices, edges and faces";
2003
2004         /* api callbacks */
2005         ot->exec = edbm_reveal_exec;
2006         ot->poll = ED_operator_editmesh;
2007
2008         /* flags */
2009         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2010
2011         RNA_def_boolean(ot->srna, "select", true, "Select", "");
2012 }
2013
2014 /** \} */
2015
2016 /* -------------------------------------------------------------------- */
2017 /** \name Recalculate Normals Operator
2018  * \{ */
2019
2020 static int edbm_normals_make_consistent_exec(bContext *C, wmOperator *op)
2021 {
2022         Object *obedit = CTX_data_edit_object(C);
2023         BMEditMesh *em = BKE_editmesh_from_object(obedit);
2024
2025         /* doflip has to do with bmesh_rationalize_normals, it's an internal
2026          * thing */
2027         if (!EDBM_op_callf(em, op, "recalc_face_normals faces=%hf", BM_ELEM_SELECT))
2028                 return OPERATOR_CANCELLED;
2029
2030         if (RNA_boolean_get(op->ptr, "inside")) {
2031                 EDBM_op_callf(em, op, "reverse_faces faces=%hf flip_multires=%b", BM_ELEM_SELECT, true);
2032         }
2033
2034         EDBM_update_generic(em, true, false);
2035
2036         return OPERATOR_FINISHED;
2037 }
2038
2039 void MESH_OT_normals_make_consistent(wmOperatorType *ot)
2040 {
2041         /* identifiers */
2042         ot->name = "Make Normals Consistent";
2043         ot->description = "Make face and vertex normals point either outside or inside the mesh";
2044         ot->idname = "MESH_OT_normals_make_consistent";
2045
2046         /* api callbacks */
2047         ot->exec = edbm_normals_make_consistent_exec;
2048         ot->poll = ED_operator_editmesh;
2049
2050         /* flags */
2051         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2052
2053         RNA_def_boolean(ot->srna, "inside", false, "Inside", "");
2054 }
2055
2056 /** \} */
2057
2058 /* -------------------------------------------------------------------- */
2059 /** \name Smooth Vertex Operator
2060  * \{ */
2061
2062 static int edbm_do_smooth_vertex_exec(bContext *C, wmOperator *op)
2063 {
2064         const float fac = RNA_float_get(op->ptr, "factor");
2065
2066         const bool xaxis = RNA_boolean_get(op->ptr, "xaxis");
2067         const bool yaxis = RNA_boolean_get(op->ptr, "yaxis");
2068         const bool zaxis = RNA_boolean_get(op->ptr, "zaxis");
2069         int repeat = RNA_int_get(op->ptr, "repeat");
2070
2071         if (!repeat) {
2072                 repeat = 1;
2073         }
2074
2075         ViewLayer *view_layer = CTX_data_view_layer(C);
2076         uint objects_len = 0;
2077         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
2078         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
2079                 Object *obedit = objects[ob_index];
2080                 Mesh *me = obedit->data;
2081                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
2082                 ModifierData *md;
2083                 bool mirrx = false, mirry = false, mirrz = false;
2084                 int i;
2085                 float clip_dist = 0.0f;
2086                 const bool use_topology = (me->editflag & ME_EDIT_MIRROR_TOPO) != 0;
2087
2088                 if (em->bm->totvertsel == 0) {
2089                         continue;
2090                 }
2091
2092                 /* mirror before smooth */
2093                 if (((Mesh *)obedit->data)->editflag & ME_EDIT_MIRROR_X) {
2094                         EDBM_verts_mirror_cache_begin(em, 0, false, true, use_topology);
2095                 }
2096
2097                 /* if there is a mirror modifier with clipping, flag the verts that
2098                  * are within tolerance of the plane(s) of reflection
2099                  */
2100                 for (md = obedit->modifiers.first; md; md = md->next) {
2101                         if (md->type == eModifierType_Mirror && (md->mode & eModifierMode_Realtime)) {
2102                                 MirrorModifierData *mmd = (MirrorModifierData *)md;
2103
2104                                 if (mmd->flag & MOD_MIR_CLIPPING) {
2105                                         if (mmd->flag & MOD_MIR_AXIS_X)
2106                                                 mirrx = true;
2107                                         if (mmd->flag & MOD_MIR_AXIS_Y)
2108                                                 mirry = true;
2109                                         if (mmd->flag & MOD_MIR_AXIS_Z)
2110                                                 mirrz = true;
2111
2112                                         clip_dist = mmd->tolerance;
2113                                 }
2114                         }
2115                 }
2116
2117                 for (i = 0; i < repeat; i++) {
2118                         if (!EDBM_op_callf(
2119                                 em, op,
2120                                 "smooth_vert verts=%hv factor=%f mirror_clip_x=%b mirror_clip_y=%b mirror_clip_z=%b "
2121                                 "clip_dist=%f use_axis_x=%b use_axis_y=%b use_axis_z=%b",
2122                                 BM_ELEM_SELECT, fac, mirrx, mirry, mirrz, clip_dist, xaxis, yaxis, zaxis))
2123                         {
2124                                 continue;
2125                         }
2126                 }
2127
2128                 /* apply mirror */
2129                 if (((Mesh *)obedit->data)->editflag & ME_EDIT_MIRROR_X) {
2130                         EDBM_verts_mirror_apply(em, BM_ELEM_SELECT, 0);
2131                         EDBM_verts_mirror_cache_end(em);
2132                 }
2133
2134                 EDBM_update_generic(em, true, false);
2135         }
2136         MEM_freeN(objects);
2137
2138         return OPERATOR_FINISHED;
2139 }
2140
2141 void MESH_OT_vertices_smooth(wmOperatorType *ot)
2142 {
2143         /* identifiers */
2144         ot->name = "Smooth Vertex";
2145         ot->description = "Flatten angles of selected vertices";
2146         ot->idname = "MESH_OT_vertices_smooth";
2147
2148         /* api callbacks */
2149         ot->exec = edbm_do_smooth_vertex_exec;
2150         ot->poll = ED_operator_editmesh;
2151
2152         /* flags */
2153         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2154
2155         RNA_def_float(ot->srna, "factor", 0.5f, -10.0f, 10.0f, "Smoothing", "Smoothing factor", 0.0f, 1.0f);
2156         RNA_def_int(ot->srna, "repeat", 1, 1, 1000, "Repeat", "Number of times to smooth the mesh", 1, 100);
2157
2158         WM_operatortype_props_advanced_begin(ot);
2159
2160         RNA_def_boolean(ot->srna, "xaxis", true, "X-Axis", "Smooth along the X axis");
2161         RNA_def_boolean(ot->srna, "yaxis", true, "Y-Axis", "Smooth along the Y axis");
2162         RNA_def_boolean(ot->srna, "zaxis", true, "Z-Axis", "Smooth along the Z axis");
2163 }
2164
2165 /** \} */
2166
2167 /* -------------------------------------------------------------------- */
2168 /** \name Laplacian Vertex Smooth Operator
2169  * \{ */
2170
2171 static int edbm_do_smooth_laplacian_vertex_exec(bContext *C, wmOperator *op)
2172 {
2173         Object *obedit = CTX_data_edit_object(C);
2174         BMEditMesh *em = BKE_editmesh_from_object(obedit);
2175         Mesh *me = obedit->data;
2176         bool use_topology = (me->editflag & ME_EDIT_MIRROR_TOPO) != 0;
2177         bool usex = true, usey = true, usez = true, preserve_volume = true;
2178         int i, repeat;
2179         float lambda_factor;
2180         float lambda_border;
2181         BMIter fiter;
2182         BMFace *f;
2183
2184         /* Check if select faces are triangles */
2185         BM_ITER_MESH (f, &fiter, em->bm, BM_FACES_OF_MESH) {
2186                 if (BM_elem_flag_test(f, BM_ELEM_SELECT)) {
2187                         if (f->len > 4) {
2188                                 BKE_report(op->reports, RPT_WARNING, "Selected faces must be triangles or quads");
2189                                 return OPERATOR_CANCELLED;
2190                         }
2191                 }
2192         }
2193
2194         /* mirror before smooth */
2195         if (((Mesh *)obedit->data)->editflag & ME_EDIT_MIRROR_X) {
2196                 EDBM_verts_mirror_cache_begin(em, 0, false, true, use_topology);
2197         }
2198
2199         repeat = RNA_int_get(op->ptr, "repeat");
2200         lambda_factor = RNA_float_get(op->ptr, "lambda_factor");
2201         lambda_border = RNA_float_get(op->ptr, "lambda_border");
2202         usex = RNA_boolean_get(op->ptr, "use_x");
2203         usey = RNA_boolean_get(op->ptr, "use_y");
2204         usez = RNA_boolean_get(op->ptr, "use_z");
2205         preserve_volume = RNA_boolean_get(op->ptr, "preserve_volume");
2206         if (!repeat)
2207                 repeat = 1;
2208
2209         for (i = 0; i < repeat; i++) {
2210                 if (!EDBM_op_callf(
2211                             em, op,
2212                             "smooth_laplacian_vert verts=%hv lambda_factor=%f lambda_border=%f use_x=%b use_y=%b use_z=%b preserve_volume=%b",
2213                             BM_ELEM_SELECT, lambda_factor, lambda_border, usex, usey, usez, preserve_volume))
2214                 {
2215                         return OPERATOR_CANCELLED;
2216                 }
2217         }
2218
2219         /* apply mirror */
2220         if (((Mesh *)obedit->data)->editflag & ME_EDIT_MIRROR_X) {
2221                 EDBM_verts_mirror_apply(em, BM_ELEM_SELECT, 0);
2222                 EDBM_verts_mirror_cache_end(em);
2223         }
2224
2225         EDBM_update_generic(em, true, false);
2226
2227         return OPERATOR_FINISHED;
2228 }
2229
2230 void MESH_OT_vertices_smooth_laplacian(wmOperatorType *ot)
2231 {
2232         /* identifiers */
2233         ot->name = "Laplacian Smooth Vertex";
2234         ot->description = "Laplacian smooth of selected vertices";
2235         ot->idname = "MESH_OT_vertices_smooth_laplacian";
2236
2237         /* api callbacks */
2238         ot->exec = edbm_do_smooth_laplacian_vertex_exec;
2239         ot->poll = ED_operator_editmesh;
2240
2241         /* flags */
2242         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2243
2244         RNA_def_int(ot->srna, "repeat", 1, 1, 1000,
2245                     "Number of iterations to smooth the mesh", "", 1, 200);
2246         RNA_def_float(ot->srna, "lambda_factor", 5e-5f, 1e-7f, 1000.0f,
2247                       "Lambda factor", "", 1e-7f, 1000.0f);
2248         RNA_def_float(ot->srna, "lambda_border", 5e-5f, 1e-7f, 1000.0f,
2249                       "Lambda factor in border", "", 1e-7f, 1000.0f);
2250
2251         WM_operatortype_props_advanced_begin(ot);
2252
2253         RNA_def_boolean(ot->srna, "use_x", true, "Smooth X Axis", "Smooth object along X axis");
2254         RNA_def_boolean(ot->srna, "use_y", true, "Smooth Y Axis", "Smooth object along Y axis");
2255         RNA_def_boolean(ot->srna, "use_z", true, "Smooth Z Axis", "Smooth object along Z axis");
2256         RNA_def_boolean(ot->srna, "preserve_volume", true, "Preserve Volume", "Apply volume preservation after smooth");
2257 }
2258
2259 /** \} */
2260
2261 /* -------------------------------------------------------------------- */
2262 /** \name Set Faces Smooth Shading Operator
2263  * \{ */
2264
2265 static void mesh_set_smooth_faces(BMEditMesh *em, short smooth)
2266 {
2267         BMIter iter;
2268         BMFace *efa;
2269
2270         if (em == NULL) return;
2271
2272         BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
2273                 if (BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
2274                         BM_elem_flag_set(efa, BM_ELEM_SMOOTH, smooth);
2275                 }
2276         }
2277 }
2278
2279 static int edbm_faces_shade_smooth_exec(bContext *C, wmOperator *UNUSED(op))
2280 {
2281         ViewLayer * view_layer = CTX_data_view_layer(C);
2282         uint objects_len = 0;
2283         Object * *objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
2284         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
2285                 Object * obedit = objects[ob_index];
2286                 BMEditMesh * em = BKE_editmesh_from_object(obedit);
2287
2288                 if (em->bm->totfacesel == 0) {
2289                         continue;
2290                 }
2291
2292                 mesh_set_smooth_faces(em, 1);
2293                 EDBM_update_generic(em, false, false);
2294         }
2295         MEM_freeN(objects);
2296
2297         return OPERATOR_FINISHED;
2298 }
2299
2300 void MESH_OT_faces_shade_smooth(wmOperatorType *ot)
2301 {
2302         /* identifiers */
2303         ot->name = "Shade Smooth";
2304         ot->description = "Display faces smooth (using vertex normals)";
2305         ot->idname = "MESH_OT_faces_shade_smooth";
2306
2307         /* api callbacks */
2308         ot->exec = edbm_faces_shade_smooth_exec;
2309         ot->poll = ED_operator_editmesh;
2310
2311         /* flags */
2312         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2313 }
2314
2315 /** \} */
2316
2317 /* -------------------------------------------------------------------- */
2318 /** \name Set Faces Flat Shading Operator
2319  * \{ */
2320
2321 static int edbm_faces_shade_flat_exec(bContext *C, wmOperator *UNUSED(op))
2322 {
2323         ViewLayer *view_layer = CTX_data_view_layer(C);
2324         uint objects_len = 0;
2325         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
2326         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
2327                 Object *obedit = objects[ob_index];
2328                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
2329
2330                 if (em->bm->totfacesel == 0) {
2331                         continue;
2332                 }
2333
2334                 mesh_set_smooth_faces(em, 0);
2335                 EDBM_update_generic(em, false, false);
2336         }
2337         MEM_freeN(objects);
2338
2339         return OPERATOR_FINISHED;
2340 }
2341
2342 void MESH_OT_faces_shade_flat(wmOperatorType *ot)
2343 {
2344         /* identifiers */
2345         ot->name = "Shade Flat";
2346         ot->description = "Display faces flat";
2347         ot->idname = "MESH_OT_faces_shade_flat";
2348
2349         /* api callbacks */
2350         ot->exec = edbm_faces_shade_flat_exec;
2351         ot->poll = ED_operator_editmesh;
2352
2353         /* flags */
2354         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2355 }
2356
2357 /** \} */
2358
2359 /* -------------------------------------------------------------------- */
2360 /** \name UV/Color Rotate/Reverse Operator
2361  * \{ */
2362
2363 static int edbm_rotate_uvs_exec(bContext *C, wmOperator *op)
2364 {
2365         /* get the direction from RNA */
2366         const bool use_ccw = RNA_boolean_get(op->ptr, "use_ccw");
2367
2368         ViewLayer *view_layer = CTX_data_view_layer(C);
2369         uint objects_len = 0;
2370         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
2371         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
2372                 Object *obedit = objects[ob_index];
2373                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
2374
2375                 if (em->bm->totfacesel == 0) {
2376                         continue;
2377                 }
2378
2379                 BMOperator bmop;
2380
2381                 /* initialize the bmop using EDBM api, which does various ui error reporting and other stuff */
2382                 EDBM_op_init(em, &bmop, op, "rotate_uvs faces=%hf use_ccw=%b", BM_ELEM_SELECT, use_ccw);
2383
2384                 /* execute the operator */
2385                 BMO_op_exec(em->bm, &bmop);
2386
2387                 if (!EDBM_op_finish(em, &bmop, op, true)) {
2388                         continue;
2389                 }
2390
2391                 EDBM_update_generic(em, false, false);
2392         }
2393
2394         MEM_freeN(objects);
2395         return OPERATOR_FINISHED;
2396 }
2397
2398 static int edbm_reverse_uvs_exec(bContext *C, wmOperator *op)
2399 {
2400         ViewLayer *view_layer = CTX_data_view_layer(C);
2401         uint objects_len = 0;
2402         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
2403         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
2404                 Object *obedit = objects[ob_index];
2405                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
2406
2407                 if (em->bm->totfacesel == 0) {
2408                         continue;
2409                 }
2410
2411                 BMOperator bmop;
2412
2413                 /* initialize the bmop using EDBM api, which does various ui error reporting and other stuff */
2414                 EDBM_op_init(em, &bmop, op, "reverse_uvs faces=%hf", BM_ELEM_SELECT);
2415
2416                 /* execute the operator */
2417                 BMO_op_exec(em->bm, &bmop);
2418
2419                 /* finish the operator */
2420                 if (!EDBM_op_finish(em, &bmop, op, true)) {
2421                         continue;
2422                 }
2423                 EDBM_update_generic(em, false, false);
2424         }
2425
2426         MEM_freeN(objects);
2427         return OPERATOR_FINISHED;
2428 }
2429
2430 static int edbm_rotate_colors_exec(bContext *C, wmOperator *op)
2431 {
2432                 /* get the direction from RNA */
2433         const bool use_ccw = RNA_boolean_get(op->ptr, "use_ccw");
2434
2435         ViewLayer *view_layer = CTX_data_view_layer(C);
2436         uint objects_len = 0;
2437         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
2438
2439         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
2440                 Object *ob = objects[ob_index];
2441                 BMEditMesh *em = BKE_editmesh_from_object(ob);
2442                 if (em->bm->totfacesel == 0) {
2443                         continue;
2444                 }
2445
2446                 BMOperator bmop;
2447
2448                 /* initialize the bmop using EDBM api, which does various ui error reporting and other stuff */
2449                 EDBM_op_init(em, &bmop, op, "rotate_colors faces=%hf use_ccw=%b", BM_ELEM_SELECT, use_ccw);
2450
2451                 /* execute the operator */
2452                 BMO_op_exec(em->bm, &bmop);
2453
2454                 /* finish the operator */
2455                 if (!EDBM_op_finish(em, &bmop, op, true)) {
2456                         continue;
2457                 }
2458
2459                 /* dependencies graph and notification stuff */
2460                 EDBM_update_generic(em, false, false);
2461         }
2462
2463         MEM_freeN(objects);
2464
2465         return OPERATOR_FINISHED;
2466 }
2467
2468
2469 static int edbm_reverse_colors_exec(bContext *C, wmOperator *op)
2470 {
2471         Object *ob = CTX_data_edit_object(C);
2472         BMEditMesh *em = BKE_editmesh_from_object(ob);
2473         BMOperator bmop;
2474
2475         /* initialize the bmop using EDBM api, which does various ui error reporting and other stuff */
2476         EDBM_op_init(em, &bmop, op, "reverse_colors faces=%hf", BM_ELEM_SELECT);
2477
2478         /* execute the operator */
2479         BMO_op_exec(em->bm, &bmop);
2480
2481         /* finish the operator */
2482         if (!EDBM_op_finish(em, &bmop, op, true)) {
2483                 return OPERATOR_CANCELLED;
2484         }
2485
2486         EDBM_update_generic(em, false, false);
2487
2488         return OPERATOR_FINISHED;
2489 }
2490
2491 void MESH_OT_uvs_rotate(wmOperatorType *ot)
2492 {
2493         /* identifiers */
2494         ot->name = "Rotate UVs";
2495         ot->idname = "MESH_OT_uvs_rotate";
2496         ot->description = "Rotate UV coordinates inside faces";
2497
2498         /* api callbacks */
2499         ot->exec = edbm_rotate_uvs_exec;
2500         ot->poll = ED_operator_editmesh;
2501
2502         /* flags */
2503         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2504
2505         /* props */
2506         RNA_def_boolean(ot->srna, "use_ccw", false, "Counter Clockwise", "");
2507 }
2508
2509 void MESH_OT_uvs_reverse(wmOperatorType *ot)
2510 {
2511         /* identifiers */
2512         ot->name = "Reverse UVs";
2513         ot->idname = "MESH_OT_uvs_reverse";
2514         ot->description = "Flip direction of UV coordinates inside faces";
2515
2516         /* api callbacks */
2517         ot->exec = edbm_reverse_uvs_exec;
2518         ot->poll = ED_operator_editmesh;
2519
2520         /* flags */
2521         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2522
2523         /* props */
2524         //RNA_def_enum(ot->srna, "axis", axis_items, DIRECTION_CW, "Axis", "Axis to mirror UVs around");
2525 }
2526
2527 void MESH_OT_colors_rotate(wmOperatorType *ot)
2528 {
2529         /* identifiers */
2530         ot->name = "Rotate Colors";
2531         ot->idname = "MESH_OT_colors_rotate";
2532         ot->description = "Rotate vertex colors inside faces";
2533
2534         /* api callbacks */
2535         ot->exec = edbm_rotate_colors_exec;
2536         ot->poll = ED_operator_editmesh;
2537
2538         /* flags */
2539         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2540
2541         /* props */
2542         RNA_def_boolean(ot->srna, "use_ccw", false, "Counter Clockwise", "");
2543 }
2544
2545 void MESH_OT_colors_reverse(wmOperatorType *ot)
2546 {
2547         /* identifiers */
2548         ot->name = "Reverse Colors";
2549         ot->idname = "MESH_OT_colors_reverse";
2550         ot->description = "Flip direction of vertex colors inside faces";
2551
2552         /* api callbacks */
2553         ot->exec = edbm_reverse_colors_exec;
2554         ot->poll = ED_operator_editmesh;
2555
2556         /* flags */
2557         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2558
2559         /* props */
2560         //RNA_def_enum(ot->srna, "axis", axis_items, DIRECTION_CW, "Axis", "Axis to mirror colors around");
2561 }
2562
2563 /** \} */
2564
2565 /* -------------------------------------------------------------------- */
2566 /** \name Merge Vertices Operator
2567  * \{ */
2568
2569 enum {
2570         MESH_MERGE_LAST     = 1,
2571         MESH_MERGE_CENTER   = 3,
2572         MESH_MERGE_CURSOR   = 4,
2573         MESH_MERGE_COLLAPSE = 5,
2574         MESH_MERGE_FIRST    = 6,
2575 };
2576
2577 static bool merge_firstlast(BMEditMesh *em, const bool use_first, const bool use_uvmerge, wmOperator *wmop)
2578 {
2579         BMVert *mergevert;
2580         BMEditSelection *ese;
2581
2582         /* operator could be called directly from shortcut or python,
2583          * so do extra check for data here
2584          */
2585
2586         /* do sanity check in mergemenu in edit.c ?*/
2587         if (use_first == false) {
2588                 if (!em->bm->selected.last || ((BMEditSelection *)em->bm->selected.last)->htype != BM_VERT)
2589                         return false;
2590
2591                 ese = em->bm->selected.last;
2592                 mergevert = (BMVert *)ese->ele;
2593         }
2594         else {
2595                 if (!em->bm->selected.first || ((BMEditSelection *)em->bm->selected.first)->htype != BM_VERT)
2596                         return false;
2597
2598                 ese = em->bm->selected.first;
2599                 mergevert = (BMVert *)ese->ele;
2600         }
2601
2602         if (!BM_elem_flag_test(mergevert, BM_ELEM_SELECT))
2603                 return false;
2604
2605         if (use_uvmerge) {
2606                 if (!EDBM_op_callf(em, wmop, "pointmerge_facedata verts=%hv vert_snap=%e", BM_ELEM_SELECT, mergevert))
2607                         return false;
2608         }
2609
2610         if (!EDBM_op_callf(em, wmop, "pointmerge verts=%hv merge_co=%v", BM_ELEM_SELECT, mergevert->co))
2611                 return false;
2612
2613         return true;
2614 }
2615
2616 static bool merge_target(
2617         BMEditMesh *em, Scene *scene, View3D *v3d, Object *ob,
2618         const bool use_cursor, const bool use_uvmerge, wmOperator *wmop)
2619 {
2620         BMIter iter;
2621         BMVert *v;
2622         float co[3], cent[3] = {0.0f, 0.0f, 0.0f};
2623         const float *vco = NULL;
2624
2625         if (use_cursor) {
2626                 vco = ED_view3d_cursor3d_get(scene, v3d)->location;
2627                 copy_v3_v3(co, vco);
2628                 invert_m4_m4(ob->imat, ob->obmat);
2629                 mul_m4_v3(ob->imat, co);
2630         }
2631         else {
2632                 float fac;
2633                 int i = 0;
2634                 BM_ITER_MESH (v, &iter, em->bm, BM_VERTS_OF_MESH) {
2635                         if (!BM_elem_flag_test(v, BM_ELEM_SELECT))
2636                                 continue;
2637                         add_v3_v3(cent, v->co);
2638                         i++;
2639                 }
2640
2641                 if (!i)
2642                         return false;
2643
2644                 fac = 1.0f / (float)i;
2645                 mul_v3_fl(cent, fac);
2646                 copy_v3_v3(co, cent);
2647                 vco = co;
2648         }
2649
2650         if (!vco)
2651                 return false;
2652
2653         if (use_uvmerge) {
2654                 if (!EDBM_op_callf(em, wmop, "average_vert_facedata verts=%hv", BM_ELEM_SELECT))
2655                         return false;
2656         }
2657
2658         if (!EDBM_op_callf(em, wmop, "pointmerge verts=%hv merge_co=%v", BM_ELEM_SELECT, co))
2659                 return false;
2660
2661         return true;
2662 }
2663
2664 static int edbm_merge_exec(bContext *C, wmOperator *op)
2665 {
2666         Scene *scene = CTX_data_scene(C);
2667         View3D *v3d = CTX_wm_view3d(C);
2668         ViewLayer *view_layer = CTX_data_view_layer(C);
2669         uint objects_len = 0;
2670         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
2671         const int type = RNA_enum_get(op->ptr, "type");
2672         const bool uvs = RNA_boolean_get(op->ptr, "uvs");
2673
2674         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
2675                 Object *obedit = objects[ob_index];
2676                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
2677
2678                 if (em->bm->totvertsel == 0) {
2679                         continue;
2680                 }
2681
2682                 bool ok = false;
2683                 switch (type) {
2684                         case MESH_MERGE_CENTER:
2685                                 ok = merge_target(em, scene, v3d, obedit, false, uvs, op);
2686                                 break;
2687                         case MESH_MERGE_CURSOR:
2688                                 ok = merge_target(em, scene, v3d, obedit, true, uvs, op);
2689                                 break;
2690                         case MESH_MERGE_LAST:
2691                                 ok = merge_firstlast(em, false, uvs, op);
2692                                 break;
2693                         case MESH_MERGE_FIRST:
2694                                 ok = merge_firstlast(em, true, uvs, op);
2695                                 break;
2696                         case MESH_MERGE_COLLAPSE:
2697                                 ok = EDBM_op_callf(em, op, "collapse edges=%he uvs=%b", BM_ELEM_SELECT, uvs);
2698                                 break;
2699                         default:
2700                                 BLI_assert(0);
2701                                 break;
2702                 }
2703
2704                 if (!ok) {
2705                         continue;
2706                 }
2707
2708                 EDBM_update_generic(em, true, true);
2709
2710                 /* once collapsed, we can't have edge/face selection */
2711                 if ((em->selectmode & SCE_SELECT_VERTEX) == 0) {
2712                         EDBM_flag_disable_all(em, BM_ELEM_SELECT);
2713                 }
2714                 /* Only active object supported, see comment below. */
2715                 if (ELEM(type, MESH_MERGE_FIRST, MESH_MERGE_LAST)) {
2716                         break;
2717                 }
2718         }
2719
2720         MEM_freeN(objects);
2721
2722         return OPERATOR_FINISHED;
2723 }
2724
2725 static const EnumPropertyItem merge_type_items[] = {
2726         {MESH_MERGE_FIRST, "FIRST", 0, "At First", ""},
2727         {MESH_MERGE_LAST, "LAST", 0, "At Last", ""},
2728         {MESH_MERGE_CENTER, "CENTER", 0, "At Center", ""},
2729         {MESH_MERGE_CURSOR, "CURSOR", 0, "At Cursor", ""},
2730         {MESH_MERGE_COLLAPSE, "COLLAPSE", 0, "Collapse", ""},
2731         {0, NULL, 0, NULL, NULL}
2732 };
2733
2734 static const EnumPropertyItem *merge_type_itemf(bContext *C, PointerRNA *UNUSED(ptr),  PropertyRNA *UNUSED(prop), bool *r_free)
2735 {
2736         Object *obedit;
2737         EnumPropertyItem *item = NULL;
2738         int totitem = 0;
2739
2740         if (!C) /* needed for docs */
2741                 return merge_type_items;
2742
2743         obedit = CTX_data_edit_object(C);
2744         if (obedit && obedit->type == OB_MESH) {
2745                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
2746
2747                 /* Only active object supported:
2748                  * In practice it doesn't make sense to run this operation on non-active meshes
2749                  * since selecting will activate - we could have own code-path for these but it's a hassle
2750                  * for now just apply to the active (first) object. */
2751                 if (em->selectmode & SCE_SELECT_VERTEX) {
2752                         if (em->bm->selected.first && em->bm->selected.last &&
2753                             ((BMEditSelection *)em->bm->selected.first)->htype == BM_VERT &&
2754                             ((BMEditSelection *)em->bm->selected.last)->htype == BM_VERT)
2755                         {
2756                                 RNA_enum_items_add_value(&item, &totitem, merge_type_items, MESH_MERGE_FIRST);
2757                                 RNA_enum_items_add_value(&item, &totitem, merge_type_items, MESH_MERGE_LAST);
2758                         }
2759                         else if (em->bm->selected.first && ((BMEditSelection *)em->bm->selected.first)->htype == BM_VERT) {
2760                                 RNA_enum_items_add_value(&item, &totitem, merge_type_items, MESH_MERGE_FIRST);
2761                         }
2762                         else if (em->bm->selected.last && ((BMEditSelection *)em->bm->selected.last)->htype == BM_VERT) {
2763                                 RNA_enum_items_add_value(&item, &totitem, merge_type_items, MESH_MERGE_LAST);
2764                         }
2765                 }
2766
2767                 RNA_enum_items_add_value(&item, &totitem, merge_type_items, MESH_MERGE_CENTER);
2768                 RNA_enum_items_add_value(&item, &totitem, merge_type_items, MESH_MERGE_CURSOR);
2769                 RNA_enum_items_add_value(&item, &totitem, merge_type_items, MESH_MERGE_COLLAPSE);
2770                 RNA_enum_item_end(&item, &totitem);
2771
2772                 *r_free = true;
2773
2774                 return item;
2775         }
2776
2777         return NULL;
2778 }
2779
2780 void MESH_OT_merge(wmOperatorType *ot)
2781 {
2782         /* identifiers */
2783         ot->name = "Merge";
2784         ot->description = "Merge selected vertices";
2785         ot->idname = "MESH_OT_merge";
2786
2787         /* api callbacks */
2788         ot->exec = edbm_merge_exec;
2789         ot->invoke = WM_menu_invoke;
2790         ot->poll = ED_operator_editmesh;
2791
2792         /* flags */
2793         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2794
2795         /* properties */
2796         ot->prop = RNA_def_enum(ot->srna, "type", merge_type_items, MESH_MERGE_CENTER, "Type", "Merge method to use");
2797         RNA_def_enum_funcs(ot->prop, merge_type_itemf);
2798
2799         WM_operatortype_props_advanced_begin(ot);
2800
2801         RNA_def_boolean(ot->srna, "uvs", false, "UVs", "Move UVs according to merge");
2802 }
2803
2804 /** \} */
2805
2806 /* -------------------------------------------------------------------- */
2807 /** \name Remove Doubles Operator
2808  * \{ */
2809
2810 static int edbm_remove_doubles_exec(bContext *C, wmOperator *op)
2811 {
2812         const float threshold = RNA_float_get(op->ptr, "threshold");
2813         const bool use_unselected = RNA_boolean_get(op->ptr, "use_unselected");
2814         int count_multi = 0;
2815
2816         ViewLayer *view_layer = CTX_data_view_layer(C);
2817         uint objects_len = 0;
2818         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
2819
2820         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
2821                 Object *obedit = objects[ob_index];
2822                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
2823
2824                 /* Selection used as target with 'use_unselected'. */
2825                 if (em->bm->totvertsel == 0) {
2826                         continue;
2827                 }
2828
2829                 BMOperator bmop;
2830                 const int totvert_orig = em->bm->totvert;
2831
2832                 /* avoid loosing selection state (select -> tags) */
2833                 char htype_select;
2834                 if      (em->selectmode & SCE_SELECT_VERTEX) htype_select = BM_VERT;
2835                 else if (em->selectmode & SCE_SELECT_EDGE)   htype_select = BM_EDGE;
2836                 else                                         htype_select = BM_FACE;
2837
2838                 /* store selection as tags */
2839                 BM_mesh_elem_hflag_enable_test(em->bm, htype_select, BM_ELEM_TAG, true, true, BM_ELEM_SELECT);
2840
2841
2842                 if (use_unselected) {
2843                         EDBM_op_init(
2844                                 em, &bmop, op,
2845                                 "automerge verts=%hv dist=%f",
2846                                 BM_ELEM_SELECT, threshold);
2847                         BMO_op_exec(em->bm, &bmop);
2848
2849                         if (!EDBM_op_finish(em, &bmop, op, true)) {
2850                                 continue;
2851                         }
2852                 }
2853                 else {
2854                         EDBM_op_init(
2855                                 em, &bmop, op,
2856                                 "find_doubles verts=%hv dist=%f",
2857                                 BM_ELEM_SELECT, threshold);
2858
2859                         BMO_op_exec(em->bm, &bmop);
2860
2861                         if (!EDBM_op_callf(em, op, "weld_verts targetmap=%S", &bmop, "targetmap.out")) {
2862                                 BMO_op_finish(em->bm, &bmop);
2863                                 continue;
2864                         }
2865
2866                         if (!EDBM_op_finish(em, &bmop, op, true)) {
2867                                 continue;
2868                         }
2869                 }
2870
2871                 const int count = (totvert_orig - em->bm->totvert);
2872
2873                 /* restore selection from tags */
2874                 BM_mesh_elem_hflag_enable_test(em->bm, htype_select, BM_ELEM_SELECT, true, true, BM_ELEM_TAG);
2875                 EDBM_selectmode_flush(em);
2876
2877                 if (count) {
2878                         count_multi += count;
2879                         EDBM_update_generic(em, true, true);
2880                 }
2881         }
2882         MEM_freeN(objects);
2883
2884         BKE_reportf(op->reports, RPT_INFO, "Removed %d vertices", count_multi);
2885
2886         return OPERATOR_FINISHED;
2887 }
2888
2889 void MESH_OT_remove_doubles(wmOperatorType *ot)
2890 {
2891         /* identifiers */
2892         ot->name = "Remove Doubles";
2893         ot->description = "Remove duplicate vertices";
2894         ot->idname = "MESH_OT_remove_doubles";
2895
2896         /* api callbacks */
2897         ot->exec = edbm_remove_doubles_exec;
2898         ot->poll = ED_operator_editmesh;
2899
2900         /* flags */
2901         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2902
2903         RNA_def_float_distance(ot->srna, "threshold", 1e-4f, 1e-6f, 50.0f, "Merge Distance",
2904                                "Minimum distance between elements to merge", 1e-5f, 10.0f);
2905         RNA_def_boolean(ot->srna, "use_unselected", false, "Unselected", "Merge selected to other unselected vertices");
2906 }
2907
2908 /** \} */
2909
2910 /* -------------------------------------------------------------------- */
2911 /** \name Shape Key Propagate Operator
2912  * \{ */
2913
2914 /* BMESH_TODO this should be properly encapsulated in a bmop.  but later.*/
2915 static void shape_propagate(BMEditMesh *em, wmOperator *op)
2916 {
2917         BMIter iter;
2918         BMVert *eve = NULL;
2919         float *co;
2920         int i, totshape = CustomData_number_of_layers(&em->bm->vdata, CD_SHAPEKEY);
2921
2922         if (!CustomData_has_layer(&em->bm->vdata, CD_SHAPEKEY)) {
2923                 BKE_report(op->reports, RPT_ERROR, "Mesh does not have shape keys");
2924                 return;
2925         }
2926
2927         BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
2928                 if (!BM_elem_flag_test(eve, BM_ELEM_SELECT) || BM_elem_flag_test(eve, BM_ELEM_HIDDEN))
2929                         continue;
2930
2931                 for (i = 0; i < totshape; i++) {
2932                         co = CustomData_bmesh_get_n(&em->bm->vdata, eve->head.data, CD_SHAPEKEY, i);
2933                         copy_v3_v3(co, eve->co);
2934                 }
2935         }
2936
2937 #if 0
2938         //TAG Mesh Objects that share this data
2939         for (base = scene->base.first; base; base = base->next) {
2940                 if (base->object && base->object->data == me) {
2941                         DEG_id_tag_update(&base->object->id, OB_RECALC_DATA);
2942                 }
2943         }
2944 #endif
2945 }
2946
2947
2948 static int edbm_shape_propagate_to_all_exec(bContext *C, wmOperator *op)
2949 {
2950         Object *obedit = CTX_data_edit_object(C);
2951         Mesh *me = obedit->data;
2952         BMEditMesh *em = me->edit_btmesh;
2953
2954         shape_propagate(em, op);
2955
2956         EDBM_update_generic(em, false, false);
2957
2958         return OPERATOR_FINISHED;
2959 }
2960
2961
2962 void MESH_OT_shape_propagate_to_all(wmOperatorType *ot)
2963 {
2964         /* identifiers */
2965         ot->name = "Shape Propagate";
2966         ot->description = "Apply selected vertex locations to all other shape keys";
2967         ot->idname = "MESH_OT_shape_propagate_to_all";
2968
2969         /* api callbacks */
2970         ot->exec = edbm_shape_propagate_to_all_exec;
2971         ot->poll = ED_operator_editmesh;
2972
2973         /* flags */
2974         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2975 }
2976
2977 /** \} */
2978
2979 /* -------------------------------------------------------------------- */
2980 /** \name Blend from Shape Operator
2981  * \{ */
2982
2983 /* BMESH_TODO this should be properly encapsulated in a bmop.  but later.*/
2984 static int edbm_blend_from_shape_exec(bContext *C, wmOperator *op)
2985 {
2986         Object *obedit = CTX_data_edit_object(C);
2987         Mesh *me = obedit->data;
2988         Key *key = me->key;
2989         KeyBlock *kb = NULL;
2990         BMEditMesh *em = me->edit_btmesh;
2991         BMVert *eve;
2992         BMIter iter;
2993         float co[3], *sco;
2994         int totshape;
2995
2996         const float blend = RNA_float_get(op->ptr, "blend");
2997         const int shape = RNA_enum_get(op->ptr, "shape");
2998         const bool use_add = RNA_boolean_get(op->ptr, "add");
2999
3000         /* sanity check */
3001         totshape = CustomData_number_of_layers(&em->bm->vdata, CD_SHAPEKEY);
3002         if (totshape == 0 || shape < 0 || shape >= totshape)
3003                 return OPERATOR_CANCELLED;
3004
3005         /* get shape key - needed for finding reference shape (for add mode only) */
3006         if (key) {
3007                 kb = BLI_findlink(&key->block, shape);
3008         }
3009
3010         /* perform blending on selected vertices*/
3011         BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
3012                 if (!BM_elem_flag_test(eve, BM_ELEM_SELECT) || BM_elem_flag_test(eve, BM_ELEM_HIDDEN))
3013                         continue;
3014
3015                 /* get coordinates of shapekey we're blending from */
3016                 sco = CustomData_bmesh_get_n(&em->bm->vdata, eve->head.data, CD_SHAPEKEY, shape);
3017                 copy_v3_v3(co, sco);
3018
3019                 if (use_add) {
3020                         /* in add mode, we add relative shape key offset */
3021                         if (kb) {
3022                                 const float *rco = CustomData_bmesh_get_n(&em->bm->vdata, eve->head.data, CD_SHAPEKEY, kb->relative);
3023                                 sub_v3_v3v3(co, co, rco);
3024                         }
3025
3026                         madd_v3_v3fl(eve->co, co, blend);
3027                 }
3028                 else {
3029                         /* in blend mode, we interpolate to the shape key */
3030                         interp_v3_v3v3(eve->co, eve->co, co, blend);
3031                 }
3032         }
3033
3034         EDBM_update_generic(em, true, false);
3035
3036         return OPERATOR_FINISHED;
3037 }
3038
3039 static const EnumPropertyItem *shape_itemf(bContext *C, PointerRNA *UNUSED(ptr),  PropertyRNA *UNUSED(prop), bool *r_free)
3040 {
3041         Object *obedit = CTX_data_edit_object(C);
3042         BMEditMesh *em;
3043         EnumPropertyItem *item = NULL;
3044         int totitem = 0;
3045
3046         if ((obedit && obedit->type == OB_MESH) &&
3047             (em = BKE_editmesh_from_object(obedit)) &&
3048             CustomData_has_layer(&em->bm->vdata, CD_SHAPEKEY))
3049         {
3050                 EnumPropertyItem tmp = {0, "", 0, "", ""};
3051                 int a;
3052
3053                 for (a = 0; a < em->bm->vdata.totlayer; a++) {
3054                         if (em->bm->vdata.layers[a].type != CD_SHAPEKEY)
3055                                 continue;
3056
3057                         tmp.value = totitem;
3058                         tmp.identifier = em->bm->vdata.layers[a].name;
3059                         tmp.name = em->bm->vdata.layers[a].name;
3060                         /* RNA_enum_item_add sets totitem itself! */
3061                         RNA_enum_item_add(&item, &totitem, &tmp);
3062                 }
3063         }
3064
3065         RNA_enum_item_end(&item, &totitem);
3066         *r_free = true;
3067
3068         return item;
3069 }
3070
3071 static void edbm_blend_from_shape_ui(bContext *C, wmOperator *op)
3072 {
3073         uiLayout *layout = op->layout;
3074         PointerRNA ptr;
3075         Object *obedit = CTX_data_edit_object(C);
3076         Mesh *me = obedit->data;
3077         PointerRNA ptr_key;
3078
3079         RNA_pointer_create(NULL, op->type->srna, op->properties, &ptr);
3080         RNA_id_pointer_create((ID *)me->key, &ptr_key);
3081
3082         uiItemPointerR(layout, &ptr, "shape", &ptr_key, "key_blocks", "", ICON_SHAPEKEY_DATA);
3083         uiItemR(layout, &ptr, "blend", 0, NULL, ICON_NONE);
3084         uiItemR(layout, &ptr, "add", 0, NULL, ICON_NONE);
3085 }
3086
3087 void MESH_OT_blend_from_shape(wmOperatorType *ot)
3088 {
3089         PropertyRNA *prop;
3090
3091         /* identifiers */
3092         ot->name = "Blend From Shape";
3093         ot->description = "Blend in shape from a shape key";
3094         ot->idname = "MESH_OT_blend_from_shape";
3095
3096         /* api callbacks */
3097         ot->exec = edbm_blend_from_shape_exec;
3098 //      ot->invoke = WM_operator_props_popup_call;  /* disable because search popup closes too easily */
3099         ot->ui = edbm_blend_from_shape_ui;
3100         ot->poll = ED_operator_editmesh;
3101
3102         /* flags */
3103         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
3104
3105         /* properties */
3106         prop = RNA_def_enum(ot->srna, "shape", DummyRNA_NULL_items, 0, "Shape", "Shape key to use for blending");
3107         RNA_def_enum_funcs(prop, shape_itemf);
3108         RNA_def_property_flag(prop, PROP_ENUM_NO_TRANSLATE | PROP_NEVER_UNLINK);
3109         RNA_def_float(ot->srna, "blend", 1.0f, -1e3f, 1e3f, "Blend", "Blending factor", -2.0f, 2.0f);
3110         RNA_def_boolean(ot->srna, "add", true, "Add", "Add rather than blend between shapes");
3111 }
3112
3113 /** \} */
3114
3115 /* -------------------------------------------------------------------- */
3116 /** \name Solidify Mesh Operator
3117  * \{ */
3118
3119 static int edbm_solidify_exec(bContext *C, wmOperator *op)
3120 {
3121         const float thickness = RNA_float_get(op->ptr, "thickness");
3122
3123         ViewLayer *view_layer = CTX_data_view_layer(C);
3124         uint objects_len = 0;
3125         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
3126         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
3127                 Object *obedit = objects[ob_index];
3128                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
3129                 BMesh *bm = em->bm;
3130
3131                 if (em->bm->totfacesel == 0) {
3132                         continue;
3133                 }
3134
3135                 BMOperator bmop;
3136
3137                 if (!EDBM_op_init(em, &bmop, op, "solidify geom=%hf thickness=%f", BM_ELEM_SELECT, thickness)) {
3138                         continue;
3139                 }
3140
3141                 /* deselect only the faces in the region to be solidified (leave wire
3142                  * edges and loose verts selected, as there will be no corresponding
3143                  * geometry selected below) */
3144                 BMO_slot_buffer_hflag_disable(bm, bmop.slots_in, "geom", BM_FACE, BM_ELEM_SELECT, true);
3145
3146                 /* run the solidify operator */
3147                 BMO_op_exec(bm, &bmop);
3148
3149                 /* select the newly generated faces */
3150                 BMO_slot_buffer_hflag_enable(bm, bmop.slots_out, "geom.out", BM_FACE, BM_ELEM_SELECT, true);
3151
3152                 if (!EDBM_op_finish(em, &bmop, op, true)) {
3153                         continue;
3154                 }
3155
3156                 EDBM_update_generic(em, true, true);
3157         }
3158
3159         MEM_freeN(objects);
3160         return OPERATOR_FINISHED;
3161 }
3162
3163 void MESH_OT_solidify(wmOperatorType *ot)
3164 {
3165         PropertyRNA *prop;
3166         /* identifiers */
3167         ot->name = "Solidify";
3168         ot->description = "Create a solid skin by extruding, compensating for sharp angles";
3169         ot->idname = "MESH_OT_solidify";
3170
3171         /* api callbacks */
3172         ot->exec = edbm_solidify_exec;
3173         ot->poll = ED_operator_editmesh;
3174
3175         /* flags */
3176         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
3177
3178         prop = RNA_def_float_distance(ot->srna, "thickness", 0.01f, -1e4f, 1e4f, "Thickness", "", -10.0f, 10.0f);
3179         RNA_def_property_ui_range(prop, -10.0, 10.0, 0.1, 4);
3180 }
3181
3182 /** \} */
3183
3184 /* -------------------------------------------------------------------- */
3185 /** \name Knife Subdivide Operator
3186  * \{ */
3187
3188 /* ******************************************************************** */
3189 /* Knife Subdivide Tool.  Subdivides edges intersected by a mouse trail
3190  * drawn by user.
3191  *
3192  * Currently mapped to KKey when in MeshEdit mode.
3193  * Usage:
3194  * - Hit Shift K, Select Centers or Exact
3195  * - Hold LMB down to draw path, hit RETKEY.
3196  * - ESC cancels as expected.
3197  *
3198  * Contributed by Robert Wenzlaff (Det. Thorn).
3199  *
3200  * 2.5 Revamp:
3201  *  - non modal (no menu before cutting)
3202  *  - exit on mouse release
3203  *  - polygon/segment drawing can become handled by WM cb later
3204  *
3205  * bmesh port version
3206  */
3207
3208 #define KNIFE_EXACT     1
3209 #define KNIFE_MIDPOINT  2
3210 #define KNIFE_MULTICUT  3
3211
3212 static const EnumPropertyItem knife_items[] = {
3213         {KNIFE_EXACT, "EXACT", 0, "Exact", ""},
3214         {KNIFE_MIDPOINT, "MIDPOINTS", 0, "Midpoints", ""},
3215         {KNIFE_MULTICUT, "MULTICUT", 0, "Multicut", ""},
3216         {0, NULL, 0, NULL, NULL}
3217 };
3218
3219 /* bm_edge_seg_isect() Determines if and where a mouse trail intersects an BMEdge */
3220
3221 static float bm_edge_seg_isect(
3222         const float sco_a[2], const float sco_b[2],
3223         float (*mouse_path)[2], int len, char mode, int *isected)
3224 {
3225 #define MAXSLOPE 100000
3226         float x11, y11, x12 = 0, y12 = 0, x2max, x2min, y2max;
3227         float y2min, dist, lastdist = 0, xdiff2, xdiff1;
3228         float m1, b1, m2, b2, x21, x22, y21, y22, xi;
3229         float yi, x1min, x1max, y1max, y1min, perc = 0;
3230         float threshold = 0.0;
3231         int i;
3232
3233         //threshold = 0.000001; /* tolerance for vertex intersection */
3234         // XXX threshold = scene->toolsettings->select_thresh / 100;
3235
3236         /* Get screen coords of verts */
3237         x21 = sco_a[0];
3238         y21 = sco_a[1];
3239
3240         x22 = sco_b[0];
3241         y22 = sco_b[1];
3242
3243         xdiff2 = (x22 - x21);
3244         if (xdiff2) {
3245                 m2 = (y22 - y21) / xdiff2;
3246                 b2 = ((x22 * y21) - (x21 * y22)) / xdiff2;
3247         }
3248         else {
3249                 m2 = MAXSLOPE;  /* Verticle slope  */
3250                 b2 = x22;
3251         }
3252
3253         *isected = 0;
3254
3255         /* check for _exact_ vertex intersection first */
3256         if (mode != KNIFE_MULTICUT) {
3257                 for (i = 0; i < len; i++) {
3258                         if (i > 0) {
3259                                 x11 = x12;
3260                                 y11 = y12;
3261                         }
3262                         else {
3263                                 x11 = mouse_path[i][0];
3264                                 y11 = mouse_path[i][1];
3265                         }
3266                         x12 = mouse_path[i][0];
3267                         y12 = mouse_path[i][1];
3268
3269                         /* test e->v1 */
3270