2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software Foundation,
14 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * The Original Code is Copyright (C) 2009 Blender Foundation.
17 * All rights reserved.
24 #include "MEM_guardedalloc.h"
26 #include "DNA_mesh_types.h"
27 #include "DNA_meshdata_types.h"
28 #include "DNA_object_types.h"
29 #include "DNA_scene_types.h"
30 #include "DNA_view3d_types.h"
32 #include "BLI_alloca.h"
34 #include "BLI_utildefines.h"
36 #include "BKE_context.h"
37 #include "BKE_editmesh.h"
39 #include "BKE_report.h"
41 #include "DEG_depsgraph.h"
43 #include "RNA_access.h"
44 #include "RNA_define.h"
50 #include "ED_object.h"
52 #include "ED_screen.h"
53 #include "ED_uvedit.h"
54 #include "ED_view3d.h"
56 #include "mesh_intern.h" /* own include */
58 static CustomData *mesh_customdata_get_type(Mesh *me, const char htype, int *r_tot)
61 BMesh *bm = (me->edit_mesh) ? me->edit_mesh->bm : NULL;
117 #define GET_CD_DATA(me, data) ((me)->edit_mesh ? &(me)->edit_mesh->bm->data : &(me)->data)
118 static void delete_customdata_layer(Mesh *me, CustomDataLayer *layer)
120 const int type = layer->type;
122 int layer_index, tot, n;
124 char htype = BM_FACE;
125 if (ELEM(type, CD_MLOOPCOL, CD_MLOOPUV)) {
128 else if (ELEM(type, CD_PROP_COLOR)) {
132 data = mesh_customdata_get_type(me, htype, &tot);
133 layer_index = CustomData_get_layer_index(data, type);
134 n = (layer - &data->layers[layer_index]);
135 BLI_assert(n >= 0 && (n + layer_index) < data->totlayer);
138 BM_data_layer_free_n(me->edit_mesh->bm, data, type, n);
141 CustomData_free_layer(data, type, tot, layer_index + n);
142 BKE_mesh_update_customdata_pointers(me, true);
146 static void mesh_uv_reset_array(float **fuv, const int len)
170 /*make sure we ignore 2-sided faces*/
173 float fac = 0.0f, dfac = 1.0f / (float)len;
175 dfac *= (float)M_PI * 2.0f;
177 for (int i = 0; i < len; i++) {
178 fuv[i][0] = 0.5f * sinf(fac) + 0.5f;
179 fuv[i][1] = 0.5f * cosf(fac) + 0.5f;
186 static void mesh_uv_reset_bmface(BMFace *f, const int cd_loop_uv_offset)
188 float **fuv = BLI_array_alloca(fuv, f->len);
193 BM_ITER_ELEM_INDEX (l, &liter, f, BM_LOOPS_OF_FACE, i) {
194 fuv[i] = ((MLoopUV *)BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset))->uv;
197 mesh_uv_reset_array(fuv, f->len);
200 static void mesh_uv_reset_mface(MPoly *mp, MLoopUV *mloopuv)
202 float **fuv = BLI_array_alloca(fuv, mp->totloop);
204 for (int i = 0; i < mp->totloop; i++) {
205 fuv[i] = mloopuv[mp->loopstart + i].uv;
208 mesh_uv_reset_array(fuv, mp->totloop);
211 /* without bContext, called in uvedit */
212 void ED_mesh_uv_loop_reset_ex(struct Mesh *me, const int layernum)
214 BMEditMesh *em = me->edit_mesh;
217 /* Collect BMesh UVs */
218 const int cd_loop_uv_offset = CustomData_get_n_offset(&em->bm->ldata, CD_MLOOPUV, layernum);
223 BLI_assert(cd_loop_uv_offset != -1);
225 BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
226 if (!BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
230 mesh_uv_reset_bmface(efa, cd_loop_uv_offset);
234 /* Collect Mesh UVs */
235 BLI_assert(CustomData_has_layer(&me->ldata, CD_MLOOPUV));
236 MLoopUV *mloopuv = CustomData_get_layer_n(&me->ldata, CD_MLOOPUV, layernum);
238 for (int i = 0; i < me->totpoly; i++) {
239 mesh_uv_reset_mface(&me->mpoly[i], mloopuv);
243 DEG_id_tag_update(&me->id, 0);
246 void ED_mesh_uv_loop_reset(struct bContext *C, struct Mesh *me)
248 /* could be ldata or pdata */
249 CustomData *ldata = GET_CD_DATA(me, ldata);
250 const int layernum = CustomData_get_active_layer(ldata, CD_MLOOPUV);
251 ED_mesh_uv_loop_reset_ex(me, layernum);
253 WM_event_add_notifier(C, NC_GEOM | ND_DATA, me);
256 /* note: keep in sync with ED_mesh_color_add */
257 int ED_mesh_uv_texture_add(Mesh *me, const char *name, const bool active_set, const bool do_init)
262 bool is_init = false;
267 layernum_dst = CustomData_number_of_layers(&em->bm->ldata, CD_MLOOPUV);
268 if (layernum_dst >= MAX_MTFACE) {
273 BM_data_layer_add_named(em->bm, &em->bm->ldata, CD_MLOOPUV, name);
274 /* copy data from active UV */
275 if (layernum_dst && do_init) {
276 const int layernum_src = CustomData_get_active_layer(&em->bm->ldata, CD_MLOOPUV);
277 BM_data_layer_copy(em->bm, &em->bm->ldata, CD_MLOOPUV, layernum_src, layernum_dst);
281 if (active_set || layernum_dst == 0) {
282 CustomData_set_layer_active(&em->bm->ldata, CD_MLOOPUV, layernum_dst);
286 layernum_dst = CustomData_number_of_layers(&me->ldata, CD_MLOOPUV);
287 if (layernum_dst >= MAX_MTFACE) {
291 if (me->mloopuv && do_init) {
292 CustomData_add_layer_named(
293 &me->ldata, CD_MLOOPUV, CD_DUPLICATE, me->mloopuv, me->totloop, name);
297 CustomData_add_layer_named(&me->ldata, CD_MLOOPUV, CD_DEFAULT, NULL, me->totloop, name);
300 if (active_set || layernum_dst == 0) {
301 CustomData_set_layer_active(&me->ldata, CD_MLOOPUV, layernum_dst);
304 BKE_mesh_update_customdata_pointers(me, true);
307 /* don't overwrite our copied coords */
308 if (!is_init && do_init) {
309 ED_mesh_uv_loop_reset_ex(me, layernum_dst);
312 DEG_id_tag_update(&me->id, 0);
313 WM_main_add_notifier(NC_GEOM | ND_DATA, me);
318 void ED_mesh_uv_texture_ensure(struct Mesh *me, const char *name)
326 layernum_dst = CustomData_number_of_layers(&em->bm->ldata, CD_MLOOPUV);
327 if (layernum_dst == 0) {
328 ED_mesh_uv_texture_add(me, name, true, true);
332 layernum_dst = CustomData_number_of_layers(&me->ldata, CD_MLOOPUV);
333 if (layernum_dst == 0) {
334 ED_mesh_uv_texture_add(me, name, true, true);
339 bool ED_mesh_uv_texture_remove_index(Mesh *me, const int n)
341 CustomData *ldata = GET_CD_DATA(me, ldata);
342 CustomDataLayer *cdlu;
345 index = CustomData_get_layer_index_n(ldata, CD_MLOOPUV, n);
346 cdlu = (index == -1) ? NULL : &ldata->layers[index];
352 delete_customdata_layer(me, cdlu);
354 DEG_id_tag_update(&me->id, 0);
355 WM_main_add_notifier(NC_GEOM | ND_DATA, me);
359 bool ED_mesh_uv_texture_remove_active(Mesh *me)
361 /* texpoly/uv are assumed to be in sync */
362 CustomData *ldata = GET_CD_DATA(me, ldata);
363 const int n = CustomData_get_active_layer(ldata, CD_MLOOPUV);
366 return ED_mesh_uv_texture_remove_index(me, n);
370 bool ED_mesh_uv_texture_remove_named(Mesh *me, const char *name)
372 /* texpoly/uv are assumed to be in sync */
373 CustomData *ldata = GET_CD_DATA(me, ldata);
374 const int n = CustomData_get_named_layer(ldata, CD_MLOOPUV, name);
376 return ED_mesh_uv_texture_remove_index(me, n);
381 /* note: keep in sync with ED_mesh_uv_texture_add */
382 int ED_mesh_color_add(Mesh *me, const char *name, const bool active_set, const bool do_init)
390 layernum = CustomData_number_of_layers(&em->bm->ldata, CD_MLOOPCOL);
391 if (layernum >= MAX_MCOL) {
396 BM_data_layer_add_named(em->bm, &em->bm->ldata, CD_MLOOPCOL, name);
397 /* copy data from active vertex color layer */
398 if (layernum && do_init) {
399 const int layernum_dst = CustomData_get_active_layer(&em->bm->ldata, CD_MLOOPCOL);
400 BM_data_layer_copy(em->bm, &em->bm->ldata, CD_MLOOPCOL, layernum_dst, layernum);
402 if (active_set || layernum == 0) {
403 CustomData_set_layer_active(&em->bm->ldata, CD_MLOOPCOL, layernum);
407 layernum = CustomData_number_of_layers(&me->ldata, CD_MLOOPCOL);
408 if (layernum >= MAX_MCOL) {
412 if (me->mloopcol && do_init) {
413 CustomData_add_layer_named(
414 &me->ldata, CD_MLOOPCOL, CD_DUPLICATE, me->mloopcol, me->totloop, name);
417 CustomData_add_layer_named(&me->ldata, CD_MLOOPCOL, CD_DEFAULT, NULL, me->totloop, name);
420 if (active_set || layernum == 0) {
421 CustomData_set_layer_active(&me->ldata, CD_MLOOPCOL, layernum);
424 BKE_mesh_update_customdata_pointers(me, true);
427 DEG_id_tag_update(&me->id, 0);
428 WM_main_add_notifier(NC_GEOM | ND_DATA, me);
433 bool ED_mesh_color_ensure(struct Mesh *me, const char *name)
435 BLI_assert(me->edit_mesh == NULL);
437 if (!me->mloopcol && me->totloop) {
438 CustomData_add_layer_named(&me->ldata, CD_MLOOPCOL, CD_DEFAULT, NULL, me->totloop, name);
439 BKE_mesh_update_customdata_pointers(me, true);
442 DEG_id_tag_update(&me->id, 0);
444 return (me->mloopcol != NULL);
447 bool ED_mesh_color_remove_index(Mesh *me, const int n)
449 CustomData *ldata = GET_CD_DATA(me, ldata);
450 CustomDataLayer *cdl;
453 index = CustomData_get_layer_index_n(ldata, CD_MLOOPCOL, n);
454 cdl = (index == -1) ? NULL : &ldata->layers[index];
460 delete_customdata_layer(me, cdl);
461 DEG_id_tag_update(&me->id, 0);
462 WM_main_add_notifier(NC_GEOM | ND_DATA, me);
466 bool ED_mesh_color_remove_active(Mesh *me)
468 CustomData *ldata = GET_CD_DATA(me, ldata);
469 const int n = CustomData_get_active_layer(ldata, CD_MLOOPCOL);
471 return ED_mesh_color_remove_index(me, n);
475 bool ED_mesh_color_remove_named(Mesh *me, const char *name)
477 CustomData *ldata = GET_CD_DATA(me, ldata);
478 const int n = CustomData_get_named_layer(ldata, CD_MLOOPCOL, name);
480 return ED_mesh_color_remove_index(me, n);
485 /*********************** Sculpt Vertex colors operators ************************/
487 /* note: keep in sync with ED_mesh_uv_texture_add */
488 int ED_mesh_sculpt_color_add(Mesh *me, const char *name, const bool active_set, const bool do_init)
496 layernum = CustomData_number_of_layers(&em->bm->vdata, CD_PROP_COLOR);
497 if (layernum >= MAX_MCOL) {
502 BM_data_layer_add_named(em->bm, &em->bm->vdata, CD_PROP_COLOR, name);
503 /* copy data from active vertex color layer */
504 if (layernum && do_init) {
505 const int layernum_dst = CustomData_get_active_layer(&em->bm->vdata, CD_PROP_COLOR);
506 BM_data_layer_copy(em->bm, &em->bm->vdata, CD_PROP_COLOR, layernum_dst, layernum);
508 if (active_set || layernum == 0) {
509 CustomData_set_layer_active(&em->bm->vdata, CD_PROP_COLOR, layernum);
513 layernum = CustomData_number_of_layers(&me->vdata, CD_PROP_COLOR);
514 if (layernum >= MAX_MCOL) {
518 if (CustomData_has_layer(&me->vdata, CD_PROP_COLOR) && do_init) {
519 MPropCol *color_data = CustomData_get_layer(&me->vdata, CD_PROP_COLOR);
520 CustomData_add_layer_named(
521 &me->vdata, CD_PROP_COLOR, CD_DUPLICATE, color_data, me->totvert, name);
524 CustomData_add_layer_named(&me->vdata, CD_PROP_COLOR, CD_DEFAULT, NULL, me->totvert, name);
527 if (active_set || layernum == 0) {
528 CustomData_set_layer_active(&me->vdata, CD_PROP_COLOR, layernum);
531 BKE_mesh_update_customdata_pointers(me, true);
534 DEG_id_tag_update(&me->id, 0);
535 WM_main_add_notifier(NC_GEOM | ND_DATA, me);
540 bool ED_mesh_sculpt_color_ensure(struct Mesh *me, const char *name)
542 BLI_assert(me->edit_mesh == NULL);
544 if (me->totvert && !CustomData_has_layer(&me->vdata, CD_PROP_COLOR)) {
545 CustomData_add_layer_named(&me->vdata, CD_PROP_COLOR, CD_DEFAULT, NULL, me->totvert, name);
546 BKE_mesh_update_customdata_pointers(me, true);
549 DEG_id_tag_update(&me->id, 0);
551 return (me->mloopcol != NULL);
554 bool ED_mesh_sculpt_color_remove_index(Mesh *me, const int n)
556 CustomData *vdata = GET_CD_DATA(me, vdata);
557 CustomDataLayer *cdl;
560 index = CustomData_get_layer_index_n(vdata, CD_PROP_COLOR, n);
561 cdl = (index == -1) ? NULL : &vdata->layers[index];
567 delete_customdata_layer(me, cdl);
568 DEG_id_tag_update(&me->id, 0);
569 WM_main_add_notifier(NC_GEOM | ND_DATA, me);
573 bool ED_mesh_sculpt_color_remove_active(Mesh *me)
575 CustomData *vdata = GET_CD_DATA(me, vdata);
576 const int n = CustomData_get_active_layer(vdata, CD_PROP_COLOR);
578 return ED_mesh_sculpt_color_remove_index(me, n);
582 bool ED_mesh_sculpt_color_remove_named(Mesh *me, const char *name)
584 CustomData *vdata = GET_CD_DATA(me, vdata);
585 const int n = CustomData_get_named_layer(vdata, CD_PROP_COLOR, name);
587 return ED_mesh_sculpt_color_remove_index(me, n);
592 /*********************** UV texture operators ************************/
594 static bool layers_poll(bContext *C)
596 Object *ob = ED_object_context(C);
597 ID *data = (ob) ? ob->data : NULL;
598 return (ob && !ID_IS_LINKED(ob) && ob->type == OB_MESH && data && !ID_IS_LINKED(data));
601 static int mesh_uv_texture_add_exec(bContext *C, wmOperator *UNUSED(op))
603 Object *ob = ED_object_context(C);
606 if (ED_mesh_uv_texture_add(me, NULL, true, true) == -1) {
607 return OPERATOR_CANCELLED;
610 if (ob->mode & OB_MODE_TEXTURE_PAINT) {
611 Scene *scene = CTX_data_scene(C);
612 ED_paint_proj_mesh_data_check(scene, ob, NULL, NULL, NULL, NULL);
613 WM_event_add_notifier(C, NC_SCENE | ND_TOOLSETTINGS, NULL);
616 return OPERATOR_FINISHED;
619 void MESH_OT_uv_texture_add(wmOperatorType *ot)
622 ot->name = "Add UV Map";
623 ot->description = "Add UV map";
624 ot->idname = "MESH_OT_uv_texture_add";
627 ot->poll = layers_poll;
628 ot->exec = mesh_uv_texture_add_exec;
631 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
634 static int mesh_uv_texture_remove_exec(bContext *C, wmOperator *UNUSED(op))
636 Object *ob = ED_object_context(C);
639 if (!ED_mesh_uv_texture_remove_active(me)) {
640 return OPERATOR_CANCELLED;
643 if (ob->mode & OB_MODE_TEXTURE_PAINT) {
644 Scene *scene = CTX_data_scene(C);
645 ED_paint_proj_mesh_data_check(scene, ob, NULL, NULL, NULL, NULL);
646 WM_event_add_notifier(C, NC_SCENE | ND_TOOLSETTINGS, NULL);
649 return OPERATOR_FINISHED;
652 void MESH_OT_uv_texture_remove(wmOperatorType *ot)
655 ot->name = "Remove UV Map";
656 ot->description = "Remove UV map";
657 ot->idname = "MESH_OT_uv_texture_remove";
660 ot->poll = layers_poll;
661 ot->exec = mesh_uv_texture_remove_exec;
664 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
667 /*********************** vertex color operators ************************/
669 static int mesh_vertex_color_add_exec(bContext *C, wmOperator *UNUSED(op))
671 Object *ob = ED_object_context(C);
674 if (ED_mesh_color_add(me, NULL, true, true) == -1) {
675 return OPERATOR_CANCELLED;
678 return OPERATOR_FINISHED;
681 void MESH_OT_vertex_color_add(wmOperatorType *ot)
684 ot->name = "Add Vertex Color";
685 ot->description = "Add vertex color layer";
686 ot->idname = "MESH_OT_vertex_color_add";
689 ot->poll = layers_poll;
690 ot->exec = mesh_vertex_color_add_exec;
693 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
696 static int mesh_vertex_color_remove_exec(bContext *C, wmOperator *UNUSED(op))
698 Object *ob = ED_object_context(C);
701 if (!ED_mesh_color_remove_active(me)) {
702 return OPERATOR_CANCELLED;
705 return OPERATOR_FINISHED;
708 void MESH_OT_vertex_color_remove(wmOperatorType *ot)
711 ot->name = "Remove Vertex Color";
712 ot->description = "Remove vertex color layer";
713 ot->idname = "MESH_OT_vertex_color_remove";
716 ot->exec = mesh_vertex_color_remove_exec;
717 ot->poll = layers_poll;
720 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
723 /*********************** Sculpt Vertex Color Operators ************************/
725 static int mesh_sculpt_vertex_color_add_exec(bContext *C, wmOperator *UNUSED(op))
727 Object *ob = ED_object_context(C);
730 if (ED_mesh_sculpt_color_add(me, NULL, true, true) == -1) {
731 return OPERATOR_CANCELLED;
734 return OPERATOR_FINISHED;
737 void MESH_OT_sculpt_vertex_color_add(wmOperatorType *ot)
740 ot->name = "Add Sculpt Vertex Color";
741 ot->description = "Add vertex color layer";
742 ot->idname = "MESH_OT_sculpt_vertex_color_add";
745 ot->poll = layers_poll;
746 ot->exec = mesh_sculpt_vertex_color_add_exec;
749 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
752 static int mesh_sculpt_vertex_color_remove_exec(bContext *C, wmOperator *UNUSED(op))
754 Object *ob = ED_object_context(C);
757 if (!ED_mesh_sculpt_color_remove_active(me)) {
758 return OPERATOR_CANCELLED;
761 return OPERATOR_FINISHED;
764 void MESH_OT_sculpt_vertex_color_remove(wmOperatorType *ot)
767 ot->name = "Remove Sculpt Vertex Color";
768 ot->description = "Remove vertex color layer";
769 ot->idname = "MESH_OT_sculpt_vertex_color_remove";
772 ot->exec = mesh_sculpt_vertex_color_remove_exec;
773 ot->poll = layers_poll;
776 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
779 /* *** CustomData clear functions, we need an operator for each *** */
781 static int mesh_customdata_clear_exec__internal(bContext *C, char htype, int type)
783 Mesh *me = ED_mesh_context(C);
786 CustomData *data = mesh_customdata_get_type(me, htype, &tot);
788 BLI_assert(CustomData_layertype_is_singleton(type) == true);
790 if (CustomData_has_layer(data, type)) {
792 BM_data_layer_free(me->edit_mesh->bm, data, type);
795 CustomData_free_layers(data, type, tot);
798 DEG_id_tag_update(&me->id, 0);
799 WM_event_add_notifier(C, NC_GEOM | ND_DATA, me);
801 return OPERATOR_FINISHED;
803 return OPERATOR_CANCELLED;
807 static bool mesh_customdata_mask_clear_poll(bContext *C)
809 Object *ob = ED_object_context(C);
810 if (ob && ob->type == OB_MESH) {
813 /* special case - can't run this if we're in sculpt mode */
814 if (ob->mode & OB_MODE_SCULPT) {
818 if (!ID_IS_LINKED(me)) {
819 CustomData *data = GET_CD_DATA(me, vdata);
820 if (CustomData_has_layer(data, CD_PAINT_MASK)) {
823 data = GET_CD_DATA(me, ldata);
824 if (CustomData_has_layer(data, CD_GRID_PAINT_MASK)) {
831 static int mesh_customdata_mask_clear_exec(bContext *C, wmOperator *UNUSED(op))
833 int ret_a = mesh_customdata_clear_exec__internal(C, BM_VERT, CD_PAINT_MASK);
834 int ret_b = mesh_customdata_clear_exec__internal(C, BM_LOOP, CD_GRID_PAINT_MASK);
836 if (ret_a == OPERATOR_FINISHED || ret_b == OPERATOR_FINISHED) {
837 return OPERATOR_FINISHED;
839 return OPERATOR_CANCELLED;
842 void MESH_OT_customdata_mask_clear(wmOperatorType *ot)
846 ot->name = "Clear Sculpt Mask Data";
847 ot->idname = "MESH_OT_customdata_mask_clear";
848 ot->description = "Clear vertex sculpt masking data from the mesh";
851 ot->exec = mesh_customdata_mask_clear_exec;
852 ot->poll = mesh_customdata_mask_clear_poll;
855 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
860 * \return -1 invalid state, 0 no skin, 1 has skin.
862 static int mesh_customdata_skin_state(bContext *C)
864 Object *ob = ED_object_context(C);
866 if (ob && ob->type == OB_MESH) {
868 if (!ID_IS_LINKED(me)) {
869 CustomData *data = GET_CD_DATA(me, vdata);
870 return CustomData_has_layer(data, CD_MVERT_SKIN);
876 static bool mesh_customdata_skin_add_poll(bContext *C)
878 return (mesh_customdata_skin_state(C) == 0);
881 static int mesh_customdata_skin_add_exec(bContext *C, wmOperator *UNUSED(op))
883 Object *ob = ED_object_context(C);
886 BKE_mesh_ensure_skin_customdata(me);
888 DEG_id_tag_update(&me->id, 0);
889 WM_event_add_notifier(C, NC_GEOM | ND_DATA, me);
891 return OPERATOR_FINISHED;
894 void MESH_OT_customdata_skin_add(wmOperatorType *ot)
897 ot->name = "Add Skin Data";
898 ot->idname = "MESH_OT_customdata_skin_add";
899 ot->description = "Add a vertex skin layer";
902 ot->exec = mesh_customdata_skin_add_exec;
903 ot->poll = mesh_customdata_skin_add_poll;
906 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
909 static bool mesh_customdata_skin_clear_poll(bContext *C)
911 return (mesh_customdata_skin_state(C) == 1);
914 static int mesh_customdata_skin_clear_exec(bContext *C, wmOperator *UNUSED(op))
916 return mesh_customdata_clear_exec__internal(C, BM_VERT, CD_MVERT_SKIN);
919 void MESH_OT_customdata_skin_clear(wmOperatorType *ot)
922 ot->name = "Clear Skin Data";
923 ot->idname = "MESH_OT_customdata_skin_clear";
924 ot->description = "Clear vertex skin layer";
927 ot->exec = mesh_customdata_skin_clear_exec;
928 ot->poll = mesh_customdata_skin_clear_poll;
931 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
934 /* Clear custom loop normals */
935 static int mesh_customdata_custom_splitnormals_add_exec(bContext *C, wmOperator *UNUSED(op))
937 Mesh *me = ED_mesh_context(C);
939 if (!BKE_mesh_has_custom_loop_normals(me)) {
940 CustomData *data = GET_CD_DATA(me, ldata);
943 /* Tag edges as sharp according to smooth threshold if needed,
944 * to preserve autosmooth shading. */
945 if (me->flag & ME_AUTOSMOOTH) {
946 BM_edges_sharp_from_angle_set(me->edit_mesh->bm, me->smoothresh);
949 BM_data_layer_add(me->edit_mesh->bm, data, CD_CUSTOMLOOPNORMAL);
952 /* Tag edges as sharp according to smooth threshold if needed,
953 * to preserve autosmooth shading. */
954 if (me->flag & ME_AUTOSMOOTH) {
955 float(*polynors)[3] = MEM_mallocN(sizeof(*polynors) * (size_t)me->totpoly, __func__);
957 BKE_mesh_calc_normals_poly(me->mvert,
967 BKE_edges_sharp_from_angle_set(me->mvert,
981 CustomData_add_layer(data, CD_CUSTOMLOOPNORMAL, CD_DEFAULT, NULL, me->totloop);
984 DEG_id_tag_update(&me->id, 0);
985 WM_event_add_notifier(C, NC_GEOM | ND_DATA, me);
987 return OPERATOR_FINISHED;
989 return OPERATOR_CANCELLED;
992 void MESH_OT_customdata_custom_splitnormals_add(wmOperatorType *ot)
995 ot->name = "Add Custom Split Normals Data";
996 ot->idname = "MESH_OT_customdata_custom_splitnormals_add";
997 ot->description = "Add a custom split normals layer, if none exists yet";
1000 ot->exec = mesh_customdata_custom_splitnormals_add_exec;
1001 ot->poll = ED_operator_editable_mesh;
1004 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1007 static int mesh_customdata_custom_splitnormals_clear_exec(bContext *C, wmOperator *UNUSED(op))
1009 Mesh *me = ED_mesh_context(C);
1011 if (BKE_mesh_has_custom_loop_normals(me)) {
1012 BMEditMesh *em = me->edit_mesh;
1013 if (em != NULL && em->bm->lnor_spacearr != NULL) {
1014 BKE_lnor_spacearr_clear(em->bm->lnor_spacearr);
1016 return mesh_customdata_clear_exec__internal(C, BM_LOOP, CD_CUSTOMLOOPNORMAL);
1018 return OPERATOR_CANCELLED;
1021 void MESH_OT_customdata_custom_splitnormals_clear(wmOperatorType *ot)
1024 ot->name = "Clear Custom Split Normals Data";
1025 ot->idname = "MESH_OT_customdata_custom_splitnormals_clear";
1026 ot->description = "Remove the custom split normals layer, if it exists";
1029 ot->exec = mesh_customdata_custom_splitnormals_clear_exec;
1030 ot->poll = ED_operator_editable_mesh;
1033 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1036 /************************** Add Geometry Layers *************************/
1038 void ED_mesh_update(Mesh *mesh, bContext *C, bool calc_edges, bool calc_edges_loose)
1040 if (calc_edges || ((mesh->totpoly || mesh->totface) && mesh->totedge == 0)) {
1041 BKE_mesh_calc_edges(mesh, calc_edges, true);
1044 if (calc_edges_loose && mesh->totedge) {
1045 BKE_mesh_calc_edges_loose(mesh);
1048 /* Default state is not to have tessface's so make sure this is the case. */
1049 BKE_mesh_tessface_clear(mesh);
1051 BKE_mesh_calc_normals(mesh);
1053 DEG_id_tag_update(&mesh->id, 0);
1054 WM_event_add_notifier(C, NC_GEOM | ND_DATA, mesh);
1057 static void mesh_add_verts(Mesh *mesh, int len)
1063 int totvert = mesh->totvert + len;
1065 CustomData_copy(&mesh->vdata, &vdata, CD_MASK_MESH.vmask, CD_DEFAULT, totvert);
1066 CustomData_copy_data(&mesh->vdata, &vdata, 0, 0, mesh->totvert);
1068 if (!CustomData_has_layer(&vdata, CD_MVERT)) {
1069 CustomData_add_layer(&vdata, CD_MVERT, CD_CALLOC, NULL, totvert);
1072 CustomData_free(&mesh->vdata, mesh->totvert);
1073 mesh->vdata = vdata;
1074 BKE_mesh_update_customdata_pointers(mesh, false);
1076 /* scan the input list and insert the new vertices */
1078 /* set default flags */
1079 MVert *mvert = &mesh->mvert[mesh->totvert];
1080 for (int i = 0; i < len; i++, mvert++) {
1081 mvert->flag |= SELECT;
1084 /* set final vertex list size */
1085 mesh->totvert = totvert;
1088 static void mesh_add_edges(Mesh *mesh, int len)
1098 totedge = mesh->totedge + len;
1100 /* update customdata */
1101 CustomData_copy(&mesh->edata, &edata, CD_MASK_MESH.emask, CD_DEFAULT, totedge);
1102 CustomData_copy_data(&mesh->edata, &edata, 0, 0, mesh->totedge);
1104 if (!CustomData_has_layer(&edata, CD_MEDGE)) {
1105 CustomData_add_layer(&edata, CD_MEDGE, CD_CALLOC, NULL, totedge);
1108 CustomData_free(&mesh->edata, mesh->totedge);
1109 mesh->edata = edata;
1110 BKE_mesh_update_customdata_pointers(mesh, false); /* new edges don't change tessellation */
1112 /* set default flags */
1113 medge = &mesh->medge[mesh->totedge];
1114 for (i = 0; i < len; i++, medge++) {
1115 medge->flag = ME_EDGEDRAW | ME_EDGERENDER | SELECT;
1118 mesh->totedge = totedge;
1121 static void mesh_add_loops(Mesh *mesh, int len)
1130 totloop = mesh->totloop + len; /* new face count */
1132 /* update customdata */
1133 CustomData_copy(&mesh->ldata, &ldata, CD_MASK_MESH.lmask, CD_DEFAULT, totloop);
1134 CustomData_copy_data(&mesh->ldata, &ldata, 0, 0, mesh->totloop);
1136 if (!CustomData_has_layer(&ldata, CD_MLOOP)) {
1137 CustomData_add_layer(&ldata, CD_MLOOP, CD_CALLOC, NULL, totloop);
1140 CustomData_free(&mesh->ldata, mesh->totloop);
1141 mesh->ldata = ldata;
1142 BKE_mesh_update_customdata_pointers(mesh, true);
1144 mesh->totloop = totloop;
1147 static void mesh_add_polys(Mesh *mesh, int len)
1157 totpoly = mesh->totpoly + len; /* new face count */
1159 /* update customdata */
1160 CustomData_copy(&mesh->pdata, &pdata, CD_MASK_MESH.pmask, CD_DEFAULT, totpoly);
1161 CustomData_copy_data(&mesh->pdata, &pdata, 0, 0, mesh->totpoly);
1163 if (!CustomData_has_layer(&pdata, CD_MPOLY)) {
1164 CustomData_add_layer(&pdata, CD_MPOLY, CD_CALLOC, NULL, totpoly);
1167 CustomData_free(&mesh->pdata, mesh->totpoly);
1168 mesh->pdata = pdata;
1169 BKE_mesh_update_customdata_pointers(mesh, true);
1171 /* set default flags */
1172 mpoly = &mesh->mpoly[mesh->totpoly];
1173 for (i = 0; i < len; i++, mpoly++) {
1174 mpoly->flag = ME_FACE_SEL;
1177 mesh->totpoly = totpoly;
1180 /* -------------------------------------------------------------------- */
1181 /** \name Add Geometry
1184 void ED_mesh_verts_add(Mesh *mesh, ReportList *reports, int count)
1186 if (mesh->edit_mesh) {
1187 BKE_report(reports, RPT_ERROR, "Cannot add vertices in edit mode");
1190 mesh_add_verts(mesh, count);
1193 void ED_mesh_edges_add(Mesh *mesh, ReportList *reports, int count)
1195 if (mesh->edit_mesh) {
1196 BKE_report(reports, RPT_ERROR, "Cannot add edges in edit mode");
1199 mesh_add_edges(mesh, count);
1202 void ED_mesh_loops_add(Mesh *mesh, ReportList *reports, int count)
1204 if (mesh->edit_mesh) {
1205 BKE_report(reports, RPT_ERROR, "Cannot add loops in edit mode");
1208 mesh_add_loops(mesh, count);
1211 void ED_mesh_polys_add(Mesh *mesh, ReportList *reports, int count)
1213 if (mesh->edit_mesh) {
1214 BKE_report(reports, RPT_ERROR, "Cannot add polygons in edit mode");
1217 mesh_add_polys(mesh, count);
1222 /* -------------------------------------------------------------------- */
1223 /** \name Remove Geometry
1226 static void mesh_remove_verts(Mesh *mesh, int len)
1231 const int totvert = mesh->totvert - len;
1232 CustomData_free_elem(&mesh->vdata, totvert, len);
1233 mesh->totvert = totvert;
1236 static void mesh_remove_edges(Mesh *mesh, int len)
1241 const int totedge = mesh->totedge - len;
1242 CustomData_free_elem(&mesh->edata, totedge, len);
1243 mesh->totedge = totedge;
1246 static void mesh_remove_loops(Mesh *mesh, int len)
1251 const int totloop = mesh->totloop - len;
1252 CustomData_free_elem(&mesh->ldata, totloop, len);
1253 mesh->totloop = totloop;
1256 static void mesh_remove_polys(Mesh *mesh, int len)
1261 const int totpoly = mesh->totpoly - len;
1262 CustomData_free_elem(&mesh->pdata, totpoly, len);
1263 mesh->totpoly = totpoly;
1266 void ED_mesh_verts_remove(Mesh *mesh, ReportList *reports, int count)
1268 if (mesh->edit_mesh) {
1269 BKE_report(reports, RPT_ERROR, "Cannot remove vertices in edit mode");
1272 if (count > mesh->totvert) {
1273 BKE_report(reports, RPT_ERROR, "Cannot remove more vertices than the mesh contains");
1277 mesh_remove_verts(mesh, count);
1280 void ED_mesh_edges_remove(Mesh *mesh, ReportList *reports, int count)
1282 if (mesh->edit_mesh) {
1283 BKE_report(reports, RPT_ERROR, "Cannot remove edges in edit mode");
1286 if (count > mesh->totedge) {
1287 BKE_report(reports, RPT_ERROR, "Cannot remove more edges than the mesh contains");
1291 mesh_remove_edges(mesh, count);
1294 void ED_mesh_loops_remove(Mesh *mesh, ReportList *reports, int count)
1296 if (mesh->edit_mesh) {
1297 BKE_report(reports, RPT_ERROR, "Cannot remove loops in edit mode");
1300 if (count > mesh->totloop) {
1301 BKE_report(reports, RPT_ERROR, "Cannot remove more loops than the mesh contains");
1305 mesh_remove_loops(mesh, count);
1308 void ED_mesh_polys_remove(Mesh *mesh, ReportList *reports, int count)
1310 if (mesh->edit_mesh) {
1311 BKE_report(reports, RPT_ERROR, "Cannot remove polys in edit mode");
1314 if (count > mesh->totpoly) {
1315 BKE_report(reports, RPT_ERROR, "Cannot remove more polys than the mesh contains");
1319 mesh_remove_polys(mesh, count);
1322 void ED_mesh_geometry_clear(Mesh *mesh)
1324 mesh_remove_verts(mesh, mesh->totvert);
1325 mesh_remove_edges(mesh, mesh->totedge);
1326 mesh_remove_loops(mesh, mesh->totloop);
1327 mesh_remove_polys(mesh, mesh->totpoly);
1332 void ED_mesh_report_mirror_ex(wmOperator *op, int totmirr, int totfail, char selectmode)
1334 const char *elem_type;
1336 if (selectmode & SCE_SELECT_VERTEX) {
1337 elem_type = "vertices";
1339 else if (selectmode & SCE_SELECT_EDGE) {
1340 elem_type = "edges";
1343 elem_type = "faces";
1348 op->reports, RPT_WARNING, "%d %s mirrored, %d failed", totmirr, elem_type, totfail);
1351 BKE_reportf(op->reports, RPT_INFO, "%d %s mirrored", totmirr, elem_type);
1355 void ED_mesh_report_mirror(wmOperator *op, int totmirr, int totfail)
1357 ED_mesh_report_mirror_ex(op, totmirr, totfail, SCE_SELECT_VERTEX);
1360 Mesh *ED_mesh_context(struct bContext *C)
1362 Mesh *mesh = CTX_data_pointer_get_type(C, "mesh", &RNA_Mesh).data;
1367 Object *ob = ED_object_active_context(C);
1372 ID *data = (ID *)ob->data;
1373 if (data == NULL || GS(data->name) != ID_ME) {
1377 return (Mesh *)data;