2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2009 Blender Foundation.
19 * All rights reserved.
22 * Contributor(s): Blender Foundation
24 * ***** END GPL LICENSE BLOCK *****
27 /** \file blender/editors/mesh/mesh_data.c
31 #include "MEM_guardedalloc.h"
33 #include "DNA_mesh_types.h"
34 #include "DNA_meshdata_types.h"
35 #include "DNA_object_types.h"
36 #include "DNA_scene_types.h"
37 #include "DNA_view3d_types.h"
39 #include "BLI_utildefines.h"
40 #include "BLI_alloca.h"
41 #include "BLI_path_util.h"
44 #include "BKE_context.h"
45 #include "BKE_depsgraph.h"
46 #include "BKE_library.h"
49 #include "BKE_paint.h"
50 #include "BKE_report.h"
51 #include "BKE_editmesh.h"
53 #include "RNA_define.h"
59 #include "ED_object.h"
60 #include "ED_screen.h"
61 #include "ED_uvedit.h"
62 #include "ED_view3d.h"
64 #include "mesh_intern.h" /* own include */
67 static CustomData *mesh_customdata_get_type(Mesh *me, const char htype, int *r_tot)
70 BMesh *bm = (me->edit_btmesh) ? me->edit_btmesh->bm : NULL;
126 #define GET_CD_DATA(me, data) (me->edit_btmesh ? &me->edit_btmesh->bm->data : &me->data)
127 static void delete_customdata_layer(Mesh *me, CustomDataLayer *layer)
129 const int type = layer->type;
131 int layer_index, tot, n;
133 data = mesh_customdata_get_type(me, (ELEM(type, CD_MLOOPUV, CD_MLOOPCOL)) ? BM_LOOP : BM_FACE, &tot);
134 layer_index = CustomData_get_layer_index(data, type);
135 n = (layer - &data->layers[layer_index]);
136 BLI_assert(n >= 0 && (n + layer_index) < data->totlayer);
138 if (me->edit_btmesh) {
139 BM_data_layer_free_n(me->edit_btmesh->bm, data, type, n);
142 CustomData_free_layer(data, type, tot, layer_index + n);
143 BKE_mesh_update_customdata_pointers(me, true);
147 static void mesh_uv_reset_array(float **fuv, const int len)
171 /*make sure we ignore 2-sided faces*/
174 float fac = 0.0f, dfac = 1.0f / (float)len;
177 dfac *= (float)M_PI * 2.0f;
179 for (i = 0; i < len; i++) {
180 fuv[i][0] = 0.5f * sinf(fac) + 0.5f;
181 fuv[i][1] = 0.5f * cosf(fac) + 0.5f;
188 static void mesh_uv_reset_bmface(BMFace *f, const int cd_loop_uv_offset)
190 float **fuv = BLI_array_alloca(fuv, f->len);
195 BM_ITER_ELEM_INDEX (l, &liter, f, BM_LOOPS_OF_FACE, i) {
196 fuv[i] = ((MLoopUV *)BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset))->uv;
199 mesh_uv_reset_array(fuv, f->len);
202 static void mesh_uv_reset_mface(MPoly *mp, MLoopUV *mloopuv)
204 float **fuv = BLI_array_alloca(fuv, mp->totloop);
207 for (i = 0; i < mp->totloop; i++) {
208 fuv[i] = mloopuv[mp->loopstart + i].uv;
211 mesh_uv_reset_array(fuv, mp->totloop);
214 /* without bContext, called in uvedit */
215 void ED_mesh_uv_loop_reset_ex(struct Mesh *me, const int layernum)
217 BMEditMesh *em = me->edit_btmesh;
220 /* Collect BMesh UVs */
221 const int cd_loop_uv_offset = CustomData_get_n_offset(&em->bm->ldata, CD_MLOOPUV, layernum);
226 BLI_assert(cd_loop_uv_offset != -1);
228 BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
229 if (!BM_elem_flag_test(efa, BM_ELEM_SELECT))
232 mesh_uv_reset_bmface(efa, cd_loop_uv_offset);
236 /* Collect Mesh UVs */
240 BLI_assert(CustomData_has_layer(&me->ldata, CD_MLOOPUV));
241 mloopuv = CustomData_get_layer_n(&me->ldata, CD_MLOOPUV, layernum);
243 for (i = 0; i < me->totpoly; i++) {
244 mesh_uv_reset_mface(&me->mpoly[i], mloopuv);
248 DAG_id_tag_update(&me->id, 0);
251 void ED_mesh_uv_loop_reset(struct bContext *C, struct Mesh *me)
253 /* could be ldata or pdata */
254 CustomData *ldata = GET_CD_DATA(me, ldata);
255 const int layernum = CustomData_get_active_layer_index(ldata, CD_MLOOPUV);
256 ED_mesh_uv_loop_reset_ex(me, layernum);
258 WM_event_add_notifier(C, NC_GEOM | ND_DATA, me);
261 /* note: keep in sync with ED_mesh_color_add */
262 int ED_mesh_uv_texture_add(Mesh *me, const char *name, const bool active_set)
267 bool is_init = false;
269 if (me->edit_btmesh) {
270 em = me->edit_btmesh;
272 layernum_dst = CustomData_number_of_layers(&em->bm->ldata, CD_MLOOPUV);
273 if (layernum_dst >= MAX_MTFACE)
277 BM_data_layer_add_named(em->bm, &em->bm->ldata, CD_MLOOPUV, name);
278 /* copy data from active UV */
280 const int layernum_src = CustomData_get_active_layer(&em->bm->ldata, CD_MLOOPUV);
281 BM_data_layer_copy(em->bm, &em->bm->ldata, CD_MLOOPUV, layernum_src, layernum_dst);
285 if (active_set || layernum_dst == 0) {
286 CustomData_set_layer_active(&em->bm->ldata, CD_MLOOPUV, layernum_dst);
290 layernum_dst = CustomData_number_of_layers(&me->ldata, CD_MLOOPUV);
291 if (layernum_dst >= MAX_MTFACE)
295 CustomData_add_layer_named(&me->ldata, CD_MLOOPUV, CD_DUPLICATE, me->mloopuv, me->totloop, name);
296 CustomData_add_layer_named(&me->fdata, CD_MTFACE, CD_DUPLICATE, me->mtface, me->totface, name);
300 CustomData_add_layer_named(&me->ldata, CD_MLOOPUV, CD_DEFAULT, NULL, me->totloop, name);
301 CustomData_add_layer_named(&me->fdata, CD_MTFACE, CD_DEFAULT, NULL, me->totface, name);
304 if (active_set || layernum_dst == 0) {
305 CustomData_set_layer_active(&me->ldata, CD_MLOOPUV, layernum_dst);
306 CustomData_set_layer_active(&me->fdata, CD_MTFACE, layernum_dst);
309 BKE_mesh_update_customdata_pointers(me, true);
312 /* don't overwrite our copied coords */
313 if (is_init == false) {
314 ED_mesh_uv_loop_reset_ex(me, layernum_dst);
317 DAG_id_tag_update(&me->id, 0);
318 WM_main_add_notifier(NC_GEOM | ND_DATA, me);
323 void ED_mesh_uv_texture_ensure(struct Mesh *me, const char *name)
328 if (me->edit_btmesh) {
329 em = me->edit_btmesh;
331 layernum_dst = CustomData_number_of_layers(&em->bm->ldata, CD_MLOOPUV);
332 if (layernum_dst == 0)
333 ED_mesh_uv_texture_add(me, name, true);
336 layernum_dst = CustomData_number_of_layers(&me->ldata, CD_MLOOPUV);
337 if (layernum_dst == 0)
338 ED_mesh_uv_texture_add(me, name, true);
343 bool ED_mesh_uv_texture_remove_index(Mesh *me, const int n)
345 CustomData *ldata = GET_CD_DATA(me, ldata);
346 CustomDataLayer *cdlu;
349 index = CustomData_get_layer_index_n(ldata, CD_MLOOPUV, n);
350 cdlu = (index == -1) ? NULL : &ldata->layers[index];
355 delete_customdata_layer(me, cdlu);
357 DAG_id_tag_update(&me->id, 0);
358 WM_main_add_notifier(NC_GEOM | ND_DATA, me);
362 bool ED_mesh_uv_texture_remove_active(Mesh *me)
364 /* texpoly/uv are assumed to be in sync */
365 CustomData *ldata = GET_CD_DATA(me, ldata);
366 const int n = CustomData_get_active_layer(ldata, CD_MLOOPUV);
369 return ED_mesh_uv_texture_remove_index(me, n);
375 bool ED_mesh_uv_texture_remove_named(Mesh *me, const char *name)
377 /* texpoly/uv are assumed to be in sync */
378 CustomData *ldata = GET_CD_DATA(me, ldata);
379 const int n = CustomData_get_named_layer(ldata, CD_MLOOPUV, name);
381 return ED_mesh_uv_texture_remove_index(me, n);
388 /* note: keep in sync with ED_mesh_uv_texture_add */
389 int ED_mesh_color_add(Mesh *me, const char *name, const bool active_set)
394 if (me->edit_btmesh) {
395 em = me->edit_btmesh;
397 layernum = CustomData_number_of_layers(&em->bm->ldata, CD_MLOOPCOL);
398 if (layernum >= MAX_MCOL) {
403 BM_data_layer_add_named(em->bm, &em->bm->ldata, CD_MLOOPCOL, name);
404 /* copy data from active vertex color layer */
406 const int layernum_dst = CustomData_get_active_layer(&em->bm->ldata, CD_MLOOPCOL);
407 BM_data_layer_copy(em->bm, &em->bm->ldata, CD_MLOOPCOL, layernum_dst, layernum);
409 if (active_set || layernum == 0) {
410 CustomData_set_layer_active(&em->bm->ldata, CD_MLOOPCOL, layernum);
414 layernum = CustomData_number_of_layers(&me->ldata, CD_MLOOPCOL);
415 if (layernum >= MAX_MCOL) {
420 CustomData_add_layer_named(&me->ldata, CD_MLOOPCOL, CD_DUPLICATE, me->mloopcol, me->totloop, name);
421 CustomData_add_layer_named(&me->fdata, CD_MCOL, CD_DUPLICATE, me->mcol, me->totface, name);
424 CustomData_add_layer_named(&me->ldata, CD_MLOOPCOL, CD_DEFAULT, NULL, me->totloop, name);
425 CustomData_add_layer_named(&me->fdata, CD_MCOL, CD_DEFAULT, NULL, me->totface, name);
428 if (active_set || layernum == 0) {
429 CustomData_set_layer_active(&me->ldata, CD_MLOOPCOL, layernum);
430 CustomData_set_layer_active(&me->fdata, CD_MCOL, layernum);
433 BKE_mesh_update_customdata_pointers(me, true);
436 DAG_id_tag_update(&me->id, 0);
437 WM_main_add_notifier(NC_GEOM | ND_DATA, me);
442 bool ED_mesh_color_remove_index(Mesh *me, const int n)
444 CustomData *ldata = GET_CD_DATA(me, ldata);
445 CustomDataLayer *cdl;
448 index = CustomData_get_layer_index_n(ldata, CD_MLOOPCOL, n);
449 cdl = (index == -1) ? NULL : &ldata->layers[index];
454 delete_customdata_layer(me, cdl);
455 DAG_id_tag_update(&me->id, 0);
456 WM_main_add_notifier(NC_GEOM | ND_DATA, me);
460 bool ED_mesh_color_remove_active(Mesh *me)
462 CustomData *ldata = GET_CD_DATA(me, ldata);
463 const int n = CustomData_get_active_layer(ldata, CD_MLOOPCOL);
465 return ED_mesh_color_remove_index(me, n);
471 bool ED_mesh_color_remove_named(Mesh *me, const char *name)
473 CustomData *ldata = GET_CD_DATA(me, ldata);
474 const int n = CustomData_get_named_layer(ldata, CD_MLOOPCOL, name);
476 return ED_mesh_color_remove_index(me, n);
483 /*********************** UV texture operators ************************/
485 static int layers_poll(bContext *C)
487 Object *ob = ED_object_context(C);
488 ID *data = (ob) ? ob->data : NULL;
489 return (ob && !ID_IS_LINKED_DATABLOCK(ob) && ob->type == OB_MESH && data && !ID_IS_LINKED_DATABLOCK(data));
492 static int mesh_uv_texture_add_exec(bContext *C, wmOperator *UNUSED(op))
494 Object *ob = ED_object_context(C);
497 if (ED_mesh_uv_texture_add(me, NULL, true) == -1)
498 return OPERATOR_CANCELLED;
500 if (ob->mode & OB_MODE_TEXTURE_PAINT) {
501 Scene *scene = CTX_data_scene(C);
502 BKE_paint_proj_mesh_data_check(scene, ob, NULL, NULL, NULL, NULL);
503 WM_event_add_notifier(C, NC_SCENE | ND_TOOLSETTINGS, NULL);
506 return OPERATOR_FINISHED;
509 void MESH_OT_uv_texture_add(wmOperatorType *ot)
512 ot->name = "Add UV Map";
513 ot->description = "Add UV Map";
514 ot->idname = "MESH_OT_uv_texture_add";
517 ot->poll = layers_poll;
518 ot->exec = mesh_uv_texture_add_exec;
521 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
524 static int drop_named_image_invoke(bContext *C, wmOperator *op, const wmEvent *event)
526 Main *bmain = CTX_data_main(C);
527 Scene *scene = CTX_data_scene(C);
528 View3D *v3d = CTX_wm_view3d(C);
536 BKE_report(op->reports, RPT_ERROR, "No 3D View Available");
537 return OPERATOR_CANCELLED;
540 base = ED_view3d_give_base_under_cursor(C, event->mval);
543 if (base == NULL || base->object->type != OB_MESH) {
544 BKE_report(op->reports, RPT_ERROR, "Not an object or mesh");
545 return OPERATOR_CANCELLED;
548 ima = (Image *)WM_operator_drop_load_path(C, op, ID_IM);
550 return OPERATOR_CANCELLED;
553 id_us_min((ID *)ima);
555 /* put mesh in editmode */
557 obedit = base->object;
559 if (me->edit_btmesh == NULL) {
560 EDBM_mesh_make(scene->toolsettings, obedit, false);
563 if (me->edit_btmesh == NULL)
564 return OPERATOR_CANCELLED;
566 ED_uvedit_assign_image(bmain, scene, obedit, ima, NULL);
569 EDBM_mesh_load(obedit);
570 EDBM_mesh_free(me->edit_btmesh);
571 MEM_freeN(me->edit_btmesh);
572 me->edit_btmesh = NULL;
574 /* load_editMesh free's pointers used by CustomData layers which might be used by DerivedMesh too,
575 * so signal to re-create DerivedMesh here (sergey) */
576 DAG_id_tag_update(&me->id, 0);
579 /* dummie drop support; ensure view shows a result :) */
581 v3d->flag2 |= V3D_SOLID_TEX;
583 WM_event_add_notifier(C, NC_GEOM | ND_DATA, obedit->data);
585 return OPERATOR_FINISHED;
588 void MESH_OT_drop_named_image(wmOperatorType *ot)
591 ot->name = "Drop Image to Mesh UV Map";
592 ot->description = "Assign Image to active UV Map, or create an UV Map";
593 ot->idname = "MESH_OT_drop_named_image";
596 ot->poll = layers_poll;
597 ot->invoke = drop_named_image_invoke;
600 ot->flag = OPTYPE_UNDO | OPTYPE_INTERNAL;
603 RNA_def_string(ot->srna, "name", "Image", MAX_ID_NAME - 2, "Name", "Image name to assign");
604 RNA_def_string(ot->srna, "filepath", "Path", FILE_MAX, "Filepath", "Path to image file");
605 RNA_def_boolean(ot->srna, "relative_path", true, "Relative Path", "Select the file relative to the blend file");
608 static int mesh_uv_texture_remove_exec(bContext *C, wmOperator *UNUSED(op))
610 Object *ob = ED_object_context(C);
613 if (!ED_mesh_uv_texture_remove_active(me))
614 return OPERATOR_CANCELLED;
616 if (ob->mode & OB_MODE_TEXTURE_PAINT) {
617 Scene *scene = CTX_data_scene(C);
618 BKE_paint_proj_mesh_data_check(scene, ob, NULL, NULL, NULL, NULL);
619 WM_event_add_notifier(C, NC_SCENE | ND_TOOLSETTINGS, NULL);
622 return OPERATOR_FINISHED;
625 void MESH_OT_uv_texture_remove(wmOperatorType *ot)
628 ot->name = "Remove UV Map";
629 ot->description = "Remove UV Map";
630 ot->idname = "MESH_OT_uv_texture_remove";
633 ot->poll = layers_poll;
634 ot->exec = mesh_uv_texture_remove_exec;
637 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
640 /*********************** vertex color operators ************************/
642 static int mesh_vertex_color_add_exec(bContext *C, wmOperator *UNUSED(op))
644 Object *ob = ED_object_context(C);
647 if (ED_mesh_color_add(me, NULL, true) == -1)
648 return OPERATOR_CANCELLED;
650 return OPERATOR_FINISHED;
653 void MESH_OT_vertex_color_add(wmOperatorType *ot)
656 ot->name = "Add Vertex Color";
657 ot->description = "Add vertex color layer";
658 ot->idname = "MESH_OT_vertex_color_add";
661 ot->poll = layers_poll;
662 ot->exec = mesh_vertex_color_add_exec;
665 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
668 static int mesh_vertex_color_remove_exec(bContext *C, wmOperator *UNUSED(op))
670 Object *ob = ED_object_context(C);
673 if (!ED_mesh_color_remove_active(me))
674 return OPERATOR_CANCELLED;
676 return OPERATOR_FINISHED;
679 void MESH_OT_vertex_color_remove(wmOperatorType *ot)
682 ot->name = "Remove Vertex Color";
683 ot->description = "Remove vertex color layer";
684 ot->idname = "MESH_OT_vertex_color_remove";
687 ot->exec = mesh_vertex_color_remove_exec;
688 ot->poll = layers_poll;
691 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
694 /* *** CustomData clear functions, we need an operator for each *** */
696 static int mesh_customdata_clear_exec__internal(bContext *C,
697 char htype, int type)
699 Object *obedit = ED_object_context(C);
700 Mesh *me = obedit->data;
703 CustomData *data = mesh_customdata_get_type(me, htype, &tot);
705 BLI_assert(CustomData_layertype_is_singleton(type) == true);
707 if (CustomData_has_layer(data, type)) {
708 if (me->edit_btmesh) {
709 BM_data_layer_free(me->edit_btmesh->bm, data, type);
712 CustomData_free_layers(data, type, tot);
715 DAG_id_tag_update(&me->id, 0);
716 WM_event_add_notifier(C, NC_GEOM | ND_DATA, me);
718 return OPERATOR_FINISHED;
721 return OPERATOR_CANCELLED;
726 static int mesh_customdata_mask_clear_poll(bContext *C)
728 Object *ob = ED_object_context(C);
729 if (ob && ob->type == OB_MESH) {
732 /* special case - can't run this if we're in sculpt mode */
733 if (ob->mode & OB_MODE_SCULPT) {
737 if (!ID_IS_LINKED_DATABLOCK(me)) {
738 CustomData *data = GET_CD_DATA(me, vdata);
739 if (CustomData_has_layer(data, CD_PAINT_MASK)) {
742 data = GET_CD_DATA(me, ldata);
743 if (CustomData_has_layer(data, CD_GRID_PAINT_MASK)) {
750 static int mesh_customdata_mask_clear_exec(bContext *C, wmOperator *UNUSED(op))
752 int ret_a = mesh_customdata_clear_exec__internal(C, BM_VERT, CD_PAINT_MASK);
753 int ret_b = mesh_customdata_clear_exec__internal(C, BM_LOOP, CD_GRID_PAINT_MASK);
755 if (ret_a == OPERATOR_FINISHED ||
756 ret_b == OPERATOR_FINISHED)
758 return OPERATOR_FINISHED;
761 return OPERATOR_CANCELLED;
765 void MESH_OT_customdata_mask_clear(wmOperatorType *ot)
769 ot->name = "Clear Sculpt-Mask Data";
770 ot->idname = "MESH_OT_customdata_mask_clear";
771 ot->description = "Clear vertex sculpt masking data from the mesh";
774 ot->exec = mesh_customdata_mask_clear_exec;
775 ot->poll = mesh_customdata_mask_clear_poll;
778 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
783 * \return -1 invalid state, 0 no skin, 1 has skin.
785 static int mesh_customdata_skin_state(bContext *C)
787 Object *ob = ED_object_context(C);
789 if (ob && ob->type == OB_MESH) {
791 if (!ID_IS_LINKED_DATABLOCK(me)) {
792 CustomData *data = GET_CD_DATA(me, vdata);
793 return CustomData_has_layer(data, CD_MVERT_SKIN);
799 static int mesh_customdata_skin_add_poll(bContext *C)
801 return (mesh_customdata_skin_state(C) == 0);
804 static int mesh_customdata_skin_add_exec(bContext *C, wmOperator *UNUSED(op))
806 Object *ob = ED_object_context(C);
809 BKE_mesh_ensure_skin_customdata(me);
811 DAG_id_tag_update(&me->id, 0);
812 WM_event_add_notifier(C, NC_GEOM | ND_DATA, me);
814 return OPERATOR_FINISHED;
817 void MESH_OT_customdata_skin_add(wmOperatorType *ot)
820 ot->name = "Add Skin Data";
821 ot->idname = "MESH_OT_customdata_skin_add";
822 ot->description = "Add a vertex skin layer";
825 ot->exec = mesh_customdata_skin_add_exec;
826 ot->poll = mesh_customdata_skin_add_poll;
829 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
832 static int mesh_customdata_skin_clear_poll(bContext *C)
834 return (mesh_customdata_skin_state(C) == 1);
837 static int mesh_customdata_skin_clear_exec(bContext *C, wmOperator *UNUSED(op))
839 return mesh_customdata_clear_exec__internal(C, BM_VERT, CD_MVERT_SKIN);
842 void MESH_OT_customdata_skin_clear(wmOperatorType *ot)
845 ot->name = "Clear Skin Data";
846 ot->idname = "MESH_OT_customdata_skin_clear";
847 ot->description = "Clear vertex skin layer";
850 ot->exec = mesh_customdata_skin_clear_exec;
851 ot->poll = mesh_customdata_skin_clear_poll;
854 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
857 /* Clear custom loop normals */
858 static int mesh_customdata_custom_splitnormals_add_exec(bContext *C, wmOperator *UNUSED(op))
860 Object *ob = ED_object_context(C);
863 if (!BKE_mesh_has_custom_loop_normals(me)) {
864 CustomData *data = GET_CD_DATA(me, ldata);
866 if (me->edit_btmesh) {
867 BM_data_layer_add(me->edit_btmesh->bm, data, CD_CUSTOMLOOPNORMAL);
870 CustomData_add_layer(data, CD_CUSTOMLOOPNORMAL, CD_DEFAULT, NULL, me->totloop);
873 DAG_id_tag_update(&me->id, 0);
874 WM_event_add_notifier(C, NC_GEOM | ND_DATA, me);
876 return OPERATOR_FINISHED;
878 return OPERATOR_CANCELLED;
881 void MESH_OT_customdata_custom_splitnormals_add(wmOperatorType *ot)
884 ot->name = "Add Custom Split Normals Data";
885 ot->idname = "MESH_OT_customdata_custom_splitnormals_add";
886 ot->description = "Add a custom split normals layer, if none exists yet";
889 ot->exec = mesh_customdata_custom_splitnormals_add_exec;
890 ot->poll = ED_operator_object_active_editable_mesh;
893 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
896 static int mesh_customdata_custom_splitnormals_clear_exec(bContext *C, wmOperator *UNUSED(op))
898 Object *ob = ED_object_context(C);
901 if (BKE_mesh_has_custom_loop_normals(me)) {
902 return mesh_customdata_clear_exec__internal(C, BM_LOOP, CD_CUSTOMLOOPNORMAL);
904 return OPERATOR_CANCELLED;
907 void MESH_OT_customdata_custom_splitnormals_clear(wmOperatorType *ot)
910 ot->name = "Clear Custom Split Normals Data";
911 ot->idname = "MESH_OT_customdata_custom_splitnormals_clear";
912 ot->description = "Remove the custom split normals layer, if it exists";
915 ot->exec = mesh_customdata_custom_splitnormals_clear_exec;
916 ot->poll = ED_operator_object_active_editable_mesh;
919 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
922 /************************** Add Geometry Layers *************************/
924 void ED_mesh_update(Mesh *mesh, bContext *C, int calc_edges, int calc_tessface)
926 bool tessface_input = false;
928 if (mesh->totface > 0 && mesh->totpoly == 0) {
929 BKE_mesh_convert_mfaces_to_mpolys(mesh);
931 /* would only be converting back again, don't bother */
932 tessface_input = true;
935 if (calc_edges || ((mesh->totpoly || mesh->totface) && mesh->totedge == 0))
936 BKE_mesh_calc_edges(mesh, calc_edges, true);
939 if (tessface_input == false) {
940 BKE_mesh_tessface_calc(mesh);
944 /* default state is not to have tessface's so make sure this is the case */
945 BKE_mesh_tessface_clear(mesh);
948 BKE_mesh_calc_normals(mesh);
950 DAG_id_tag_update(&mesh->id, 0);
951 WM_event_add_notifier(C, NC_GEOM | ND_DATA, mesh);
954 static void mesh_add_verts(Mesh *mesh, int len)
963 totvert = mesh->totvert + len;
964 CustomData_copy(&mesh->vdata, &vdata, CD_MASK_MESH, CD_DEFAULT, totvert);
965 CustomData_copy_data(&mesh->vdata, &vdata, 0, 0, mesh->totvert);
967 if (!CustomData_has_layer(&vdata, CD_MVERT))
968 CustomData_add_layer(&vdata, CD_MVERT, CD_CALLOC, NULL, totvert);
970 CustomData_free(&mesh->vdata, mesh->totvert);
972 BKE_mesh_update_customdata_pointers(mesh, false);
974 /* scan the input list and insert the new vertices */
976 /* set default flags */
977 mvert = &mesh->mvert[mesh->totvert];
978 for (i = 0; i < len; i++, mvert++)
979 mvert->flag |= SELECT;
981 /* set final vertex list size */
982 mesh->totvert = totvert;
985 static void mesh_add_edges(Mesh *mesh, int len)
994 totedge = mesh->totedge + len;
996 /* update customdata */
997 CustomData_copy(&mesh->edata, &edata, CD_MASK_MESH, CD_DEFAULT, totedge);
998 CustomData_copy_data(&mesh->edata, &edata, 0, 0, mesh->totedge);
1000 if (!CustomData_has_layer(&edata, CD_MEDGE))
1001 CustomData_add_layer(&edata, CD_MEDGE, CD_CALLOC, NULL, totedge);
1003 CustomData_free(&mesh->edata, mesh->totedge);
1004 mesh->edata = edata;
1005 BKE_mesh_update_customdata_pointers(mesh, false); /* new edges don't change tessellation */
1007 /* set default flags */
1008 medge = &mesh->medge[mesh->totedge];
1009 for (i = 0; i < len; i++, medge++)
1010 medge->flag = ME_EDGEDRAW | ME_EDGERENDER | SELECT;
1012 mesh->totedge = totedge;
1015 static void mesh_add_tessfaces(Mesh *mesh, int len)
1024 totface = mesh->totface + len; /* new face count */
1026 /* update customdata */
1027 CustomData_copy(&mesh->fdata, &fdata, CD_MASK_MESH, CD_DEFAULT, totface);
1028 CustomData_copy_data(&mesh->fdata, &fdata, 0, 0, mesh->totface);
1030 if (!CustomData_has_layer(&fdata, CD_MFACE))
1031 CustomData_add_layer(&fdata, CD_MFACE, CD_CALLOC, NULL, totface);
1033 CustomData_free(&mesh->fdata, mesh->totface);
1034 mesh->fdata = fdata;
1035 BKE_mesh_update_customdata_pointers(mesh, true);
1037 /* set default flags */
1038 mface = &mesh->mface[mesh->totface];
1039 for (i = 0; i < len; i++, mface++)
1040 mface->flag = ME_FACE_SEL;
1042 mesh->totface = totface;
1045 static void mesh_add_loops(Mesh *mesh, int len)
1053 totloop = mesh->totloop + len; /* new face count */
1055 /* update customdata */
1056 CustomData_copy(&mesh->ldata, &ldata, CD_MASK_MESH, CD_DEFAULT, totloop);
1057 CustomData_copy_data(&mesh->ldata, &ldata, 0, 0, mesh->totloop);
1059 if (!CustomData_has_layer(&ldata, CD_MLOOP))
1060 CustomData_add_layer(&ldata, CD_MLOOP, CD_CALLOC, NULL, totloop);
1062 CustomData_free(&mesh->ldata, mesh->totloop);
1063 mesh->ldata = ldata;
1064 BKE_mesh_update_customdata_pointers(mesh, true);
1066 mesh->totloop = totloop;
1069 static void mesh_add_polys(Mesh *mesh, int len)
1078 totpoly = mesh->totpoly + len; /* new face count */
1080 /* update customdata */
1081 CustomData_copy(&mesh->pdata, &pdata, CD_MASK_MESH, CD_DEFAULT, totpoly);
1082 CustomData_copy_data(&mesh->pdata, &pdata, 0, 0, mesh->totpoly);
1084 if (!CustomData_has_layer(&pdata, CD_MPOLY))
1085 CustomData_add_layer(&pdata, CD_MPOLY, CD_CALLOC, NULL, totpoly);
1087 CustomData_free(&mesh->pdata, mesh->totpoly);
1088 mesh->pdata = pdata;
1089 BKE_mesh_update_customdata_pointers(mesh, true);
1091 /* set default flags */
1092 mpoly = &mesh->mpoly[mesh->totpoly];
1093 for (i = 0; i < len; i++, mpoly++)
1094 mpoly->flag = ME_FACE_SEL;
1096 mesh->totpoly = totpoly;
1099 static void mesh_remove_verts(Mesh *mesh, int len)
1106 totvert = mesh->totvert - len;
1107 CustomData_free_elem(&mesh->vdata, totvert, len);
1109 /* set final vertex list size */
1110 mesh->totvert = totvert;
1113 static void mesh_remove_edges(Mesh *mesh, int len)
1120 totedge = mesh->totedge - len;
1121 CustomData_free_elem(&mesh->edata, totedge, len);
1123 mesh->totedge = totedge;
1126 static void mesh_remove_faces(Mesh *mesh, int len)
1133 totface = mesh->totface - len; /* new face count */
1134 CustomData_free_elem(&mesh->fdata, totface, len);
1136 mesh->totface = totface;
1140 void ED_mesh_geometry_add(Mesh *mesh, ReportList *reports, int verts, int edges, int faces)
1142 if (mesh->edit_btmesh) {
1143 BKE_report(reports, RPT_ERROR, "Cannot add geometry in edit mode");
1148 mesh_add_verts(mesh, verts);
1150 mesh_add_edges(mesh, edges);
1152 mesh_add_faces(mesh, faces);
1156 void ED_mesh_tessfaces_add(Mesh *mesh, ReportList *reports, int count)
1158 if (mesh->edit_btmesh) {
1159 BKE_report(reports, RPT_ERROR, "Cannot add tessfaces in edit mode");
1164 BKE_report(reports, RPT_ERROR, "Cannot add tessfaces to a mesh that already has polygons");
1168 mesh_add_tessfaces(mesh, count);
1171 void ED_mesh_edges_add(Mesh *mesh, ReportList *reports, int count)
1173 if (mesh->edit_btmesh) {
1174 BKE_report(reports, RPT_ERROR, "Cannot add edges in edit mode");
1178 mesh_add_edges(mesh, count);
1181 void ED_mesh_vertices_add(Mesh *mesh, ReportList *reports, int count)
1183 if (mesh->edit_btmesh) {
1184 BKE_report(reports, RPT_ERROR, "Cannot add vertices in edit mode");
1188 mesh_add_verts(mesh, count);
1191 void ED_mesh_faces_remove(Mesh *mesh, ReportList *reports, int count)
1193 if (mesh->edit_btmesh) {
1194 BKE_report(reports, RPT_ERROR, "Cannot remove faces in edit mode");
1197 else if (count > mesh->totface) {
1198 BKE_report(reports, RPT_ERROR, "Cannot remove more faces than the mesh contains");
1202 mesh_remove_faces(mesh, count);
1205 void ED_mesh_edges_remove(Mesh *mesh, ReportList *reports, int count)
1207 if (mesh->edit_btmesh) {
1208 BKE_report(reports, RPT_ERROR, "Cannot remove edges in edit mode");
1211 else if (count > mesh->totedge) {
1212 BKE_report(reports, RPT_ERROR, "Cannot remove more edges than the mesh contains");
1216 mesh_remove_edges(mesh, count);
1219 void ED_mesh_vertices_remove(Mesh *mesh, ReportList *reports, int count)
1221 if (mesh->edit_btmesh) {
1222 BKE_report(reports, RPT_ERROR, "Cannot remove vertices in edit mode");
1225 else if (count > mesh->totvert) {
1226 BKE_report(reports, RPT_ERROR, "Cannot remove more vertices than the mesh contains");
1230 mesh_remove_verts(mesh, count);
1233 void ED_mesh_loops_add(Mesh *mesh, ReportList *reports, int count)
1235 if (mesh->edit_btmesh) {
1236 BKE_report(reports, RPT_ERROR, "Cannot add loops in edit mode");
1240 mesh_add_loops(mesh, count);
1243 void ED_mesh_polys_add(Mesh *mesh, ReportList *reports, int count)
1245 if (mesh->edit_btmesh) {
1246 BKE_report(reports, RPT_ERROR, "Cannot add polygons in edit mode");
1250 mesh_add_polys(mesh, count);
1253 void ED_mesh_calc_tessface(Mesh *mesh, bool free_mpoly)
1255 if (mesh->edit_btmesh) {
1256 BKE_editmesh_tessface_calc(mesh->edit_btmesh);
1259 BKE_mesh_tessface_calc(mesh);
1262 CustomData_free(&mesh->ldata, mesh->totloop);
1263 CustomData_free(&mesh->pdata, mesh->totpoly);
1267 mesh->mloopcol = NULL;
1268 mesh->mloopuv = NULL;
1273 void ED_mesh_report_mirror_ex(wmOperator *op, int totmirr, int totfail,
1276 const char *elem_type;
1278 if (selectmode & SCE_SELECT_VERTEX) {
1279 elem_type = "vertices";
1281 else if (selectmode & SCE_SELECT_EDGE) {
1282 elem_type = "edges";
1285 elem_type = "faces";
1289 BKE_reportf(op->reports, RPT_WARNING, "%d %s mirrored, %d failed", totmirr, elem_type, totfail);
1292 BKE_reportf(op->reports, RPT_INFO, "%d %s mirrored", totmirr, elem_type);
1296 void ED_mesh_report_mirror(wmOperator *op, int totmirr, int totfail)
1298 ED_mesh_report_mirror_ex(op, totmirr, totfail, SCE_SELECT_VERTEX);