Fix extrude glitch where undo state was visible
[blender.git] / source / blender / editors / mesh / editmesh_extrude.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_extrude.c
29  *  \ingroup edmesh
30  */
31
32 #include "DNA_modifier_types.h"
33 #include "DNA_object_types.h"
34
35 #include "BLI_math.h"
36 #include "BLI_listbase.h"
37
38 #include "BKE_layer.h"
39 #include "BKE_context.h"
40 #include "BKE_report.h"
41 #include "BKE_editmesh.h"
42
43 #include "RNA_define.h"
44 #include "RNA_access.h"
45
46 #include "WM_api.h"
47 #include "WM_types.h"
48
49 #include "ED_mesh.h"
50 #include "ED_screen.h"
51 #include "ED_transform.h"
52 #include "ED_view3d.h"
53 #include "ED_manipulator_library.h"
54
55 #include "UI_resources.h"
56
57 #include "MEM_guardedalloc.h"
58
59 #include "mesh_intern.h"  /* own include */
60
61 #define USE_MANIPULATOR
62
63 /* -------------------------------------------------------------------- */
64 /** \name Extrude Internal Utilities
65  * \{ */
66
67 static void edbm_extrude_edge_exclude_mirror(
68         Object *obedit, BMEditMesh *em,
69         const char hflag,
70         BMOperator *op, BMOpSlot *slot_edges_exclude)
71 {
72         BMesh *bm = em->bm;
73         ModifierData *md;
74
75         /* If a mirror modifier with clipping is on, we need to adjust some
76          * of the cases above to handle edges on the line of symmetry.
77          */
78         for (md = obedit->modifiers.first; md; md = md->next) {
79                 if ((md->type == eModifierType_Mirror) && (md->mode & eModifierMode_Realtime)) {
80                         MirrorModifierData *mmd = (MirrorModifierData *) md;
81
82                         if (mmd->flag & MOD_MIR_CLIPPING) {
83                                 BMIter iter;
84                                 BMEdge *edge;
85
86                                 float mtx[4][4];
87                                 if (mmd->mirror_ob) {
88                                         float imtx[4][4];
89                                         invert_m4_m4(imtx, mmd->mirror_ob->obmat);
90                                         mul_m4_m4m4(mtx, imtx, obedit->obmat);
91                                 }
92
93                                 BM_ITER_MESH (edge, &iter, bm, BM_EDGES_OF_MESH) {
94                                         if (BM_elem_flag_test(edge, hflag) &&
95                                             BM_edge_is_boundary(edge) &&
96                                             BM_elem_flag_test(edge->l->f, hflag))
97                                         {
98                                                 float co1[3], co2[3];
99
100                                                 copy_v3_v3(co1, edge->v1->co);
101                                                 copy_v3_v3(co2, edge->v2->co);
102
103                                                 if (mmd->mirror_ob) {
104                                                         mul_v3_m4v3(co1, mtx, co1);
105                                                         mul_v3_m4v3(co2, mtx, co2);
106                                                 }
107
108                                                 if (mmd->flag & MOD_MIR_AXIS_X) {
109                                                         if ((fabsf(co1[0]) < mmd->tolerance) &&
110                                                             (fabsf(co2[0]) < mmd->tolerance))
111                                                         {
112                                                                 BMO_slot_map_empty_insert(op, slot_edges_exclude, edge);
113                                                         }
114                                                 }
115                                                 if (mmd->flag & MOD_MIR_AXIS_Y) {
116                                                         if ((fabsf(co1[1]) < mmd->tolerance) &&
117                                                             (fabsf(co2[1]) < mmd->tolerance))
118                                                         {
119                                                                 BMO_slot_map_empty_insert(op, slot_edges_exclude, edge);
120                                                         }
121                                                 }
122                                                 if (mmd->flag & MOD_MIR_AXIS_Z) {
123                                                         if ((fabsf(co1[2]) < mmd->tolerance) &&
124                                                             (fabsf(co2[2]) < mmd->tolerance))
125                                                         {
126                                                                 BMO_slot_map_empty_insert(op, slot_edges_exclude, edge);
127                                                         }
128                                                 }
129                                         }
130                                 }
131                         }
132                 }
133         }
134 }
135
136 /* individual face extrude */
137 /* will use vertex normals for extrusion directions, so *nor is unaffected */
138 static bool edbm_extrude_discrete_faces(BMEditMesh *em, wmOperator *op, const char hflag)
139 {
140         BMOIter siter;
141         BMIter liter;
142         BMFace *f;
143         BMLoop *l;
144         BMOperator bmop;
145
146         EDBM_op_init(
147                 em, &bmop, op,
148                 "extrude_discrete_faces faces=%hf use_select_history=%b",
149                 hflag, true);
150
151         /* deselect original verts */
152         EDBM_flag_disable_all(em, BM_ELEM_SELECT);
153
154         BMO_op_exec(em->bm, &bmop);
155
156         BMO_ITER (f, &siter, bmop.slots_out, "faces.out", BM_FACE) {
157                 BM_face_select_set(em->bm, f, true);
158
159                 /* set face vertex normals to face normal */
160                 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
161                         copy_v3_v3(l->v->no, f->no);
162                 }
163         }
164
165         if (!EDBM_op_finish(em, &bmop, op, true)) {
166                 return false;
167         }
168
169         return true;
170 }
171
172 /* extrudes individual edges */
173 static bool edbm_extrude_edges_indiv(BMEditMesh *em, wmOperator *op, const char hflag)
174 {
175         BMesh *bm = em->bm;
176         BMOperator bmop;
177
178         EDBM_op_init(
179                 em, &bmop, op,
180                 "extrude_edge_only edges=%he use_select_history=%b",
181                 hflag, true);
182
183         /* deselect original verts */
184         BM_SELECT_HISTORY_BACKUP(bm);
185         EDBM_flag_disable_all(em, BM_ELEM_SELECT);
186         BM_SELECT_HISTORY_RESTORE(bm);
187
188         BMO_op_exec(em->bm, &bmop);
189         BMO_slot_buffer_hflag_enable(em->bm, bmop.slots_out, "geom.out", BM_VERT | BM_EDGE, BM_ELEM_SELECT, true);
190
191         if (!EDBM_op_finish(em, &bmop, op, true)) {
192                 return false;
193         }
194
195         return true;
196 }
197
198 /* extrudes individual vertices */
199 static bool edbm_extrude_verts_indiv(BMEditMesh *em, wmOperator *op, const char hflag)
200 {
201         BMOperator bmop;
202
203         EDBM_op_init(
204                 em, &bmop, op,
205                 "extrude_vert_indiv verts=%hv use_select_history=%b",
206                 hflag, true);
207
208         /* deselect original verts */
209         BMO_slot_buffer_hflag_disable(em->bm, bmop.slots_in, "verts", BM_VERT, BM_ELEM_SELECT, true);
210
211         BMO_op_exec(em->bm, &bmop);
212         BMO_slot_buffer_hflag_enable(em->bm, bmop.slots_out, "verts.out", BM_VERT, BM_ELEM_SELECT, true);
213
214         if (!EDBM_op_finish(em, &bmop, op, true)) {
215                 return false;
216         }
217
218         return true;
219 }
220
221 static char edbm_extrude_htype_from_em_select(BMEditMesh *em)
222 {
223         char htype = BM_ALL_NOLOOP;
224
225         if (em->selectmode & SCE_SELECT_VERTEX) {
226                 /* pass */
227         }
228         else if (em->selectmode & SCE_SELECT_EDGE) {
229                 htype &= ~BM_VERT;
230         }
231         else {
232                 htype &= ~(BM_VERT | BM_EDGE);
233         }
234
235         if (em->bm->totedgesel == 0) {
236                 htype &= ~(BM_EDGE | BM_FACE);
237         }
238         else if (em->bm->totfacesel == 0) {
239                 htype &= ~BM_FACE;
240         }
241
242         return htype;
243 }
244
245 static bool edbm_extrude_ex(
246         Object *obedit, BMEditMesh *em,
247         char htype, const char hflag,
248         const bool use_mirror,
249         const bool use_select_history)
250 {
251         BMesh *bm = em->bm;
252         BMOIter siter;
253         BMOperator extop;
254         BMElem *ele;
255
256         /* needed to remove the faces left behind */
257         if (htype & BM_FACE) {
258                 htype |= BM_EDGE;
259         }
260
261         BMO_op_init(bm, &extop, BMO_FLAG_DEFAULTS, "extrude_face_region");
262         BMO_slot_bool_set(extop.slots_in, "use_select_history", use_select_history);
263         BMO_slot_buffer_from_enabled_hflag(bm, &extop, extop.slots_in, "geom", htype, hflag);
264
265         if (use_mirror) {
266                 BMOpSlot *slot_edges_exclude;
267                 slot_edges_exclude = BMO_slot_get(extop.slots_in, "edges_exclude");
268
269                 edbm_extrude_edge_exclude_mirror(obedit, em, hflag, &extop, slot_edges_exclude);
270         }
271
272         BM_SELECT_HISTORY_BACKUP(bm);
273         EDBM_flag_disable_all(em, BM_ELEM_SELECT);
274         BM_SELECT_HISTORY_RESTORE(bm);
275
276         BMO_op_exec(bm, &extop);
277
278         BMO_ITER (ele, &siter, extop.slots_out, "geom.out", BM_ALL_NOLOOP) {
279                 BM_elem_select_set(bm, ele, true);
280         }
281
282         BMO_op_finish(bm, &extop);
283
284         return true;
285 }
286
287 /** \} */
288
289 /* -------------------------------------------------------------------- */
290 /** \name Extrude Repeat Operator
291  * \{ */
292
293 static int edbm_extrude_repeat_exec(bContext *C, wmOperator *op)
294 {
295         Object *obedit = CTX_data_edit_object(C);
296         BMEditMesh *em = BKE_editmesh_from_object(obedit);
297         RegionView3D *rv3d = CTX_wm_region_view3d(C);
298
299         const int steps = RNA_int_get(op->ptr, "steps");
300
301         const float offs = RNA_float_get(op->ptr, "offset");
302         float dvec[3], tmat[3][3], bmat[3][3];
303         short a;
304
305         /* dvec */
306         normalize_v3_v3_length(dvec, rv3d->persinv[2], offs);
307
308         /* base correction */
309         copy_m3_m4(bmat, obedit->obmat);
310         invert_m3_m3(tmat, bmat);
311         mul_m3_v3(tmat, dvec);
312
313         for (a = 0; a < steps; a++) {
314                 edbm_extrude_ex(obedit, em, BM_ALL_NOLOOP, BM_ELEM_SELECT, false, false);
315
316                 BMO_op_callf(
317                         em->bm, BMO_FLAG_DEFAULTS,
318                         "translate vec=%v verts=%hv",
319                         dvec, BM_ELEM_SELECT);
320         }
321
322         EDBM_mesh_normals_update(em);
323
324         EDBM_update_generic(em, true, true);
325
326         return OPERATOR_FINISHED;
327 }
328
329 void MESH_OT_extrude_repeat(wmOperatorType *ot)
330 {
331         /* identifiers */
332         ot->name = "Extrude Repeat Mesh";
333         ot->description = "Extrude selected vertices, edges or faces repeatedly";
334         ot->idname = "MESH_OT_extrude_repeat";
335
336         /* api callbacks */
337         ot->exec = edbm_extrude_repeat_exec;
338         ot->poll = ED_operator_editmesh_view3d;
339
340         /* flags */
341         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
342
343         /* props */
344         RNA_def_float_distance(ot->srna, "offset", 2.0f, 0.0f, 1e12f, "Offset", "", 0.0f, 100.0f);
345         RNA_def_int(ot->srna, "steps", 10, 0, 1000000, "Steps", "", 0, 180);
346 }
347
348 /** \} */
349
350
351 /* -------------------------------------------------------------------- */
352 /** \name Extrude Manipulator
353  * \{ */
354
355 #ifdef USE_MANIPULATOR
356
357 typedef struct ManipulatorExtrudeGroup {
358         /* Extrude . */
359         struct wmManipulator *axis_arrow;
360         /* Redo Z-axis translation. */
361         struct wmManipulator *axis_redo;
362 } ManipulatorExtrudeGroup;
363
364 static bool manipulator_mesh_extrude_poll(const bContext *C, wmManipulatorGroupType *wgt)
365 {
366         WorkSpace *workspace = CTX_wm_workspace(C);
367         if (!STREQ(workspace->tool.manipulator_group, "MESH_WGT_extrude")) {
368                 WM_manipulator_group_type_unlink_delayed_ptr(wgt);
369                 return false;
370         }
371         return true;
372 }
373
374 static void manipulator_mesh_extrude_setup(const bContext *UNUSED(C), wmManipulatorGroup *mgroup)
375 {
376         const int constraint_z[3] = {0, 0, 1};
377
378         struct ManipulatorExtrudeGroup *man = MEM_callocN(sizeof(ManipulatorExtrudeGroup), __func__);
379         mgroup->customdata = man;
380
381         const wmManipulatorType *wt_arrow = WM_manipulatortype_find("MANIPULATOR_WT_arrow_3d", true);
382         const wmManipulatorType *wt_grab = WM_manipulatortype_find("MANIPULATOR_WT_grab_3d", true);
383
384         man->axis_arrow = WM_manipulator_new_ptr(wt_arrow, mgroup, NULL);
385         man->axis_redo = WM_manipulator_new_ptr(wt_grab, mgroup, NULL);
386
387         UI_GetThemeColor3fv(TH_MANIPULATOR_PRIMARY, man->axis_arrow->color);
388         UI_GetThemeColor3fv(TH_MANIPULATOR_SECONDARY, man->axis_redo->color);
389
390         WM_manipulator_set_scale(man->axis_arrow, 2.0);
391         WM_manipulator_set_scale(man->axis_redo, 2.0);
392
393         RNA_enum_set(man->axis_arrow->ptr, "draw_style", ED_MANIPULATOR_ARROW_STYLE_NORMAL);
394         RNA_enum_set(man->axis_redo->ptr, "draw_style", ED_MANIPULATOR_GRAB_STYLE_RING_2D);
395
396         WM_manipulator_set_flag(man->axis_redo, WM_MANIPULATOR_DRAW_VALUE, true);
397
398         /* New extrude. */
399         {
400                 wmOperatorType *ot = WM_operatortype_find("MESH_OT_extrude_region_move", true);
401                 PointerRNA *ptr = WM_manipulator_operator_set(man->axis_arrow, 0, ot, NULL);
402                 {
403                         PointerRNA macroptr = RNA_pointer_get(ptr, "TRANSFORM_OT_translate");
404                         RNA_boolean_set(&macroptr, "release_confirm", true);
405                         RNA_boolean_set_array(&macroptr, "constraint_axis", constraint_z);
406                 }
407         }
408         /* Adjust extrude. */
409         {
410                 wmOperatorType *ot = WM_operatortype_find("MESH_OT_extrude_region_move", true);
411                 PointerRNA *ptr = WM_manipulator_operator_set(man->axis_redo, 0, ot, NULL);
412                 {
413                         PointerRNA macroptr = RNA_pointer_get(ptr, "TRANSFORM_OT_translate");
414                         RNA_boolean_set(&macroptr, "release_confirm", true);
415                         RNA_boolean_set_array(&macroptr, "constraint_axis", constraint_z);
416                 }
417                 wmManipulatorOpElem *mpop = WM_manipulator_operator_get(man->axis_redo, 0);
418                 mpop->is_redo = true;
419         }
420 }
421
422 static void manipulator_mesh_extrude_refresh(const bContext *C, wmManipulatorGroup *mgroup)
423 {
424         ManipulatorExtrudeGroup *man = mgroup->customdata;
425
426         WM_manipulator_set_flag(man->axis_arrow, WM_MANIPULATOR_HIDDEN, true);
427         WM_manipulator_set_flag(man->axis_redo, WM_MANIPULATOR_HIDDEN, true);
428
429         struct TransformBounds tbounds;
430         if (!ED_transform_calc_manipulator_stats(C, false, &tbounds)) {
431                 return;
432         }
433
434         /* Needed for normal orientation. */
435         copy_m4_m3(man->axis_arrow->matrix_basis, tbounds.axis);
436         copy_m4_m3(man->axis_redo->matrix_basis, tbounds.axis);
437
438         WM_manipulator_set_matrix_location(man->axis_arrow, tbounds.center);
439         WM_manipulator_set_matrix_location(man->axis_redo, tbounds.center);
440
441         wmOperator *op = WM_operator_last_redo(C);
442         bool has_redo = (op && STREQ(op->type->idname, "MESH_OT_extrude_region_move"));
443
444         WM_manipulator_set_flag(man->axis_arrow, WM_MANIPULATOR_HIDDEN, false);
445         WM_manipulator_set_flag(man->axis_redo, WM_MANIPULATOR_HIDDEN, !has_redo);
446
447         /* Redo with current settings. */
448         if (has_redo) {
449                 /* We could also access this from 'ot->last_properties' */
450
451                 wmOperator *op_transform = op->macro.last;
452                 wmManipulatorOpElem *mpop = WM_manipulator_operator_get(man->axis_redo, 0);
453
454                 PointerRNA macroptr = RNA_pointer_get(&mpop->ptr, "TRANSFORM_OT_translate");
455
456                 float value[4];
457                 RNA_float_get_array(op_transform->ptr, "value", value);
458                 RNA_float_set_array(&macroptr, "value", value);
459
460                 /* Currently has glitch in re-applying. */
461 #if 0
462                 int constraint_axis[3];
463                 RNA_boolean_get_array(op_transform->ptr, "constraint_axis", constraint_axis);
464                 RNA_boolean_set_array(&macroptr, "constraint_axis", constraint_axis);
465 #endif
466         }
467 }
468
469 static void MESH_WGT_extrude(struct wmManipulatorGroupType *wgt)
470 {
471         wgt->name = "Mesh Extrude";
472         wgt->idname = "MESH_WGT_extrude";
473
474         wgt->flag = WM_MANIPULATORGROUPTYPE_3D;
475
476         wgt->mmap_params.spaceid = SPACE_VIEW3D;
477         wgt->mmap_params.regionid = RGN_TYPE_WINDOW;
478
479         wgt->poll = manipulator_mesh_extrude_poll;
480         wgt->setup = manipulator_mesh_extrude_setup;
481         wgt->refresh = manipulator_mesh_extrude_refresh;
482 }
483
484 #endif  /* USE_MANIPULATOR */
485
486 /** \} */
487
488 /* -------------------------------------------------------------------- */
489 /** \name Extrude Operator
490  * \{ */
491
492 /* generic extern called extruder */
493 static bool edbm_extrude_mesh(Object *obedit, BMEditMesh *em, wmOperator *op)
494 {
495         bool changed = false;
496         const char htype = edbm_extrude_htype_from_em_select(em);
497         enum {NONE = 0, ELEM_FLAG, VERT_ONLY, EDGE_ONLY} nr;
498
499         if (em->selectmode & SCE_SELECT_VERTEX) {
500                 if      (em->bm->totvertsel == 0) nr = NONE;
501                 else if (em->bm->totvertsel == 1) nr = VERT_ONLY;
502                 else if (em->bm->totedgesel == 0) nr = VERT_ONLY;
503                 else                              nr = ELEM_FLAG;
504         }
505         else if (em->selectmode & SCE_SELECT_EDGE) {
506                 if      (em->bm->totedgesel == 0) nr = NONE;
507                 else if (em->bm->totfacesel == 0) nr = EDGE_ONLY;
508                 else                              nr = ELEM_FLAG;
509         }
510         else {
511                 if      (em->bm->totfacesel == 0) nr = NONE;
512                 else                              nr = ELEM_FLAG;
513         }
514
515         switch (nr) {
516                 case NONE:
517                         return false;
518                 case ELEM_FLAG:
519                         changed = edbm_extrude_ex(obedit, em, htype, BM_ELEM_SELECT, true, true);
520                         break;
521                 case VERT_ONLY:
522                         changed = edbm_extrude_verts_indiv(em, op, BM_ELEM_SELECT);
523                         break;
524                 case EDGE_ONLY:
525                         changed = edbm_extrude_edges_indiv(em, op, BM_ELEM_SELECT);
526                         break;
527         }
528
529         if (changed) {
530                 return true;
531         }
532         else {
533                 BKE_report(op->reports, RPT_ERROR, "Not a valid selection for extrude");
534                 return false;
535         }
536 }
537
538 /* extrude without transform */
539 static int edbm_extrude_region_exec(bContext *C, wmOperator *op)
540 {
541         ViewLayer *view_layer = CTX_data_view_layer(C);
542         uint objects_len = 0;
543         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
544
545         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
546                 Object *obedit = objects[ob_index];
547                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
548                 if (em->bm->totvertsel == 0) {
549                         continue;
550                 }
551
552                 edbm_extrude_mesh(obedit, em, op);
553                 /* This normally happens when pushing undo but modal operators
554                  * like this one don't push undo data until after modal mode is
555                  * done.*/
556                 EDBM_mesh_normals_update(em);
557
558                 EDBM_update_generic(em, true, true);
559         }
560         MEM_freeN(objects);
561         return OPERATOR_FINISHED;
562 }
563
564 void MESH_OT_extrude_region(wmOperatorType *ot)
565 {
566         /* identifiers */
567         ot->name = "Extrude Region";
568         ot->idname = "MESH_OT_extrude_region";
569         ot->description = "Extrude region of faces";
570
571         /* api callbacks */
572         //ot->invoke = mesh_extrude_region_invoke;
573         ot->exec = edbm_extrude_region_exec;
574         ot->poll = ED_operator_editmesh;
575
576         /* flags */
577         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
578
579         Transform_Properties(ot, P_NO_DEFAULTS | P_MIRROR_DUMMY);
580
581 #ifdef USE_MANIPULATOR
582         WM_manipulatorgrouptype_append(MESH_WGT_extrude);
583 #endif
584 }
585
586 /** \} */
587
588 /* -------------------------------------------------------------------- */
589 /** \name Extrude Verts Operator
590  * \{ */
591
592 static int edbm_extrude_verts_exec(bContext *C, wmOperator *op)
593 {
594         ViewLayer *view_layer = CTX_data_view_layer(C);
595         uint objects_len = 0;
596         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
597
598         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
599                 Object *obedit = objects[ob_index];
600                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
601                 if (em->bm->totvertsel == 0) {
602                         continue;
603                 }
604
605                 edbm_extrude_verts_indiv(em, op, BM_ELEM_SELECT);
606
607                 EDBM_update_generic(em, true, true);
608         }
609         MEM_freeN(objects);
610
611         return OPERATOR_FINISHED;
612 }
613
614 void MESH_OT_extrude_verts_indiv(wmOperatorType *ot)
615 {
616         /* identifiers */
617         ot->name = "Extrude Only Vertices";
618         ot->idname = "MESH_OT_extrude_verts_indiv";
619         ot->description = "Extrude individual vertices only";
620
621         /* api callbacks */
622         ot->exec = edbm_extrude_verts_exec;
623         ot->poll = ED_operator_editmesh;
624
625         /* flags */
626         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
627
628         /* to give to transform */
629         Transform_Properties(ot, P_NO_DEFAULTS | P_MIRROR_DUMMY);
630 }
631
632 /** \} */
633
634 /* -------------------------------------------------------------------- */
635 /** \name Extrude Edges Operator
636  * \{ */
637
638 static int edbm_extrude_edges_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
644         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
645                 Object *obedit = objects[ob_index];
646                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
647                 if (em->bm->totedgesel == 0) {
648                         continue;
649                 }
650
651                 edbm_extrude_edges_indiv(em, op, BM_ELEM_SELECT);
652
653                 EDBM_update_generic(em, true, true);
654         }
655         MEM_freeN(objects);
656
657         return OPERATOR_FINISHED;
658 }
659
660 void MESH_OT_extrude_edges_indiv(wmOperatorType *ot)
661 {
662         /* identifiers */
663         ot->name = "Extrude Only Edges";
664         ot->idname = "MESH_OT_extrude_edges_indiv";
665         ot->description = "Extrude individual edges only";
666
667         /* api callbacks */
668         ot->exec = edbm_extrude_edges_exec;
669         ot->poll = ED_operator_editmesh;
670
671         /* flags */
672         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
673
674         /* to give to transform */
675         Transform_Properties(ot, P_NO_DEFAULTS | P_MIRROR_DUMMY);
676 }
677
678 /** \} */
679
680 /* -------------------------------------------------------------------- */
681 /** \name Extrude Faces Operator
682  * \{ */
683
684 static int edbm_extrude_faces_exec(bContext *C, wmOperator *op)
685 {
686         ViewLayer *view_layer = CTX_data_view_layer(C);
687         uint objects_len = 0;
688         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
689
690         for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
691                 Object *obedit = objects[ob_index];
692                 BMEditMesh *em = BKE_editmesh_from_object(obedit);
693                 if (em->bm->totfacesel == 0) {
694                         continue;
695                 }
696
697                 edbm_extrude_discrete_faces(em, op, BM_ELEM_SELECT);
698
699                 EDBM_update_generic(em, true, true);
700         }
701         MEM_freeN(objects);
702
703         return OPERATOR_FINISHED;
704 }
705
706 void MESH_OT_extrude_faces_indiv(wmOperatorType *ot)
707 {
708         /* identifiers */
709         ot->name = "Extrude Individual Faces";
710         ot->idname = "MESH_OT_extrude_faces_indiv";
711         ot->description = "Extrude individual faces only";
712
713         /* api callbacks */
714         ot->exec = edbm_extrude_faces_exec;
715         ot->poll = ED_operator_editmesh;
716
717         /* flags */
718         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
719
720         Transform_Properties(ot, P_NO_DEFAULTS | P_MIRROR_DUMMY);
721 }
722
723 /** \} */
724
725 /* -------------------------------------------------------------------- */
726 /** \name Dupli-Extrude Operator
727  *
728  * Add-click-mesh (extrude) operator.
729  * \{ */
730
731 static int edbm_dupli_extrude_cursor_invoke(bContext *C, wmOperator *op, const wmEvent *event)
732 {
733         ViewContext vc;
734         BMVert *v1;
735         BMIter iter;
736         float center[3];
737         uint verts_len;
738         bool use_proj;
739
740         em_setup_viewcontext(C, &vc);
741
742         invert_m4_m4(vc.obedit->imat, vc.obedit->obmat);
743
744         ED_view3d_init_mats_rv3d(vc.obedit, vc.rv3d);
745
746         use_proj = ((vc.scene->toolsettings->snap_flag & SCE_SNAP) &&
747                     (vc.scene->toolsettings->snap_mode == SCE_SNAP_MODE_FACE));
748
749         zero_v3(center);
750         verts_len = 0;
751
752         BM_ITER_MESH (v1, &iter, vc.em->bm, BM_VERTS_OF_MESH) {
753                 if (BM_elem_flag_test(v1, BM_ELEM_SELECT)) {
754                         add_v3_v3(center, v1->co);
755                         verts_len += 1;
756                 }
757         }
758
759         /* call extrude? */
760         if (verts_len != 0) {
761                 const char extrude_htype = edbm_extrude_htype_from_em_select(vc.em);
762                 const bool rot_src = RNA_boolean_get(op->ptr, "rotate_source");
763                 BMEdge *eed;
764                 float mat[3][3];
765                 float vec[3], ofs[3];
766                 float nor[3] = {0.0, 0.0, 0.0};
767
768                 /* 2D normal calc */
769                 const float mval_f[2] = {(float)event->mval[0],
770                                          (float)event->mval[1]};
771
772                 mul_v3_fl(center, 1.0f / (float)verts_len);
773
774                 /* check for edges that are half selected, use for rotation */
775                 bool done = false;
776                 BM_ITER_MESH (eed, &iter, vc.em->bm, BM_EDGES_OF_MESH) {
777                         if (BM_elem_flag_test(eed, BM_ELEM_SELECT)) {
778                                 float co1[2], co2[2];
779
780                                 if ((ED_view3d_project_float_object(vc.ar, eed->v1->co, co1, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK) &&
781                                     (ED_view3d_project_float_object(vc.ar, eed->v2->co, co2, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK))
782                                 {
783                                         /* 2D rotate by 90d while adding.
784                                          *  (x, y) = (y, -x)
785                                          *
786                                          * accumulate the screenspace normal in 2D,
787                                          * with screenspace edge length weighting the result. */
788                                         if (line_point_side_v2(co1, co2, mval_f) >= 0.0f) {
789                                                 nor[0] +=  (co1[1] - co2[1]);
790                                                 nor[1] += -(co1[0] - co2[0]);
791                                         }
792                                         else {
793                                                 nor[0] +=  (co2[1] - co1[1]);
794                                                 nor[1] += -(co2[0] - co1[0]);
795                                         }
796                                         done = true;
797                                 }
798                         }
799                 }
800
801                 if (done) {
802                         float view_vec[3], cross[3];
803
804                         /* convert the 2D nomal into 3D */
805                         mul_mat3_m4_v3(vc.rv3d->viewinv, nor); /* worldspace */
806                         mul_mat3_m4_v3(vc.obedit->imat, nor); /* local space */
807
808                         /* correct the normal to be aligned on the view plane */
809                         mul_v3_mat3_m4v3(view_vec, vc.obedit->imat, vc.rv3d->viewinv[2]);
810                         cross_v3_v3v3(cross, nor, view_vec);
811                         cross_v3_v3v3(nor, view_vec, cross);
812                         normalize_v3(nor);
813                 }
814
815                 /* center */
816                 copy_v3_v3(ofs, center);
817
818                 mul_m4_v3(vc.obedit->obmat, ofs);  /* view space */
819                 ED_view3d_win_to_3d_int(vc.v3d, vc.ar, ofs, event->mval, ofs);
820                 mul_m4_v3(vc.obedit->imat, ofs); // back in object space
821
822                 sub_v3_v3(ofs, center);
823
824                 /* calculate rotation */
825                 unit_m3(mat);
826                 if (done) {
827                         float angle;
828
829                         normalize_v3_v3(vec, ofs);
830
831                         angle = angle_normalized_v3v3(vec, nor);
832
833                         if (angle != 0.0f) {
834                                 float axis[3];
835
836                                 cross_v3_v3v3(axis, nor, vec);
837
838                                 /* halve the rotation if its applied twice */
839                                 if (rot_src) {
840                                         angle *= 0.5f;
841                                 }
842
843                                 axis_angle_to_mat3(mat, axis, angle);
844                         }
845                 }
846
847                 if (rot_src) {
848                         EDBM_op_callf(vc.em, op, "rotate verts=%hv cent=%v matrix=%m3",
849                                       BM_ELEM_SELECT, center, mat);
850
851                         /* also project the source, for retopo workflow */
852                         if (use_proj)
853                                 EMBM_project_snap_verts(C, vc.ar, vc.em);
854                 }
855
856                 edbm_extrude_ex(vc.obedit, vc.em, extrude_htype, BM_ELEM_SELECT, true, true);
857                 EDBM_op_callf(vc.em, op, "rotate verts=%hv cent=%v matrix=%m3",
858                               BM_ELEM_SELECT, center, mat);
859                 EDBM_op_callf(vc.em, op, "translate verts=%hv vec=%v",
860                               BM_ELEM_SELECT, ofs);
861         }
862         else {
863                 const float *cursor = ED_view3d_cursor3d_get(vc.scene, vc.v3d);
864                 BMOperator bmop;
865                 BMOIter oiter;
866
867                 copy_v3_v3(center, cursor);
868                 ED_view3d_win_to_3d_int(vc.v3d, vc.ar, center, event->mval, center);
869
870                 mul_m4_v3(vc.obedit->imat, center); // back in object space
871
872                 EDBM_op_init(vc.em, &bmop, op, "create_vert co=%v", center);
873                 BMO_op_exec(vc.em->bm, &bmop);
874
875                 BMO_ITER (v1, &oiter, bmop.slots_out, "vert.out", BM_VERT) {
876                         BM_vert_select_set(vc.em->bm, v1, true);
877                 }
878
879                 if (!EDBM_op_finish(vc.em, &bmop, op, true)) {
880                         return OPERATOR_CANCELLED;
881                 }
882         }
883
884         if (use_proj)
885                 EMBM_project_snap_verts(C, vc.ar, vc.em);
886
887         /* This normally happens when pushing undo but modal operators
888          * like this one don't push undo data until after modal mode is
889          * done. */
890         EDBM_mesh_normals_update(vc.em);
891
892         EDBM_update_generic(vc.em, true, true);
893
894         return OPERATOR_FINISHED;
895 }
896
897 void MESH_OT_dupli_extrude_cursor(wmOperatorType *ot)
898 {
899         /* identifiers */
900         ot->name = "Duplicate or Extrude to Cursor";
901         ot->idname = "MESH_OT_dupli_extrude_cursor";
902         ot->description = "Duplicate and extrude selected vertices, edges or faces towards the mouse cursor";
903
904         /* api callbacks */
905         ot->invoke = edbm_dupli_extrude_cursor_invoke;
906         ot->poll = ED_operator_editmesh_region_view3d;
907
908         /* flags */
909         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
910
911         RNA_def_boolean(ot->srna, "rotate_source", true, "Rotate Source", "Rotate initial selection giving better shape");
912 }
913
914 /** \} */