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
36 #include "MEM_guardedalloc.h"
38 #include "DNA_material_types.h"
39 #include "DNA_mesh_types.h"
40 #include "DNA_meshdata_types.h"
41 #include "DNA_object_types.h"
42 #include "DNA_scene_types.h"
43 #include "DNA_view3d_types.h"
45 #include "BLI_utildefines.h"
46 #include "BLI_array.h"
48 #include "BLI_edgehash.h"
49 #include "BLI_utildefines.h"
51 #include "BKE_context.h"
52 #include "BKE_depsgraph.h"
53 #include "BKE_displist.h"
54 #include "BKE_image.h"
55 #include "BKE_library.h"
57 #include "BKE_material.h"
59 #include "BKE_report.h"
60 #include "BKE_tessmesh.h"
62 #include "RNA_access.h"
63 #include "RNA_define.h"
69 #include "ED_object.h"
70 #include "ED_uvedit.h"
71 #include "ED_view3d.h"
73 #include "RE_render_ext.h"
75 #include "mesh_intern.h"
77 #define GET_CD_DATA(me, data) (me->edit_btmesh ? &me->edit_btmesh->bm->data : &me->data)
78 static void delete_customdata_layer(bContext *C, Object *ob, CustomDataLayer *layer)
82 void *actlayerdata, *rndlayerdata, *clonelayerdata, *stencillayerdata, *layerdata = layer->data;
83 int type = layer->type;
85 int i, actindex, rndindex, cloneindex, stencilindex, tot;
87 if (layer->type == CD_MLOOPCOL || layer->type == CD_MLOOPUV) {
88 if (me->edit_btmesh) {
89 data = &me->edit_btmesh->bm->ldata;
90 tot = me->edit_btmesh->bm->totloop;
98 if (me->edit_btmesh) {
99 data = &me->edit_btmesh->bm->pdata;
100 tot = me->edit_btmesh->bm->totface;
108 index = CustomData_get_layer_index(data, type);
110 /* ok, deleting a non-active layer needs to preserve the active layer indices.
111 * to do this, we store a pointer to the .data member of both layer and the active layer,
112 * (to detect if we're deleting the active layer or not), then use the active
113 * layer data pointer to find where the active layer has ended up.
115 * this is necessary because the deletion functions only support deleting the active
117 actlayerdata = data->layers[CustomData_get_active_layer_index(data, type)].data;
118 rndlayerdata = data->layers[CustomData_get_render_layer_index(data, type)].data;
119 clonelayerdata = data->layers[CustomData_get_clone_layer_index(data, type)].data;
120 stencillayerdata = data->layers[CustomData_get_stencil_layer_index(data, type)].data;
121 CustomData_set_layer_active(data, type, layer - &data->layers[index]);
123 if (me->edit_btmesh) {
124 BM_data_layer_free(me->edit_btmesh->bm, data, type);
127 CustomData_free_layer_active(data, type, tot);
128 mesh_update_customdata_pointers(me, TRUE);
131 if (!CustomData_has_layer(data, type) && (type == CD_MLOOPCOL && (ob->mode & OB_MODE_VERTEX_PAINT)))
132 ED_object_toggle_modes(C, OB_MODE_VERTEX_PAINT);
134 /* reconstruct active layer */
135 if (actlayerdata != layerdata) {
137 actindex = CustomData_get_layer_index(data, type);
138 for (i = actindex; i < data->totlayer; i++) {
139 if (data->layers[i].data == actlayerdata) {
140 actindex = i - actindex;
146 CustomData_set_layer_active(data, type, actindex);
149 if (rndlayerdata != layerdata) {
151 rndindex = CustomData_get_layer_index(data, type);
152 for (i = rndindex; i < data->totlayer; i++) {
153 if (data->layers[i].data == rndlayerdata) {
154 rndindex = i - rndindex;
160 CustomData_set_layer_render(data, type, rndindex);
163 if (clonelayerdata != layerdata) {
165 cloneindex = CustomData_get_layer_index(data, type);
166 for (i = cloneindex; i < data->totlayer; i++) {
167 if (data->layers[i].data == clonelayerdata) {
168 cloneindex = i - cloneindex;
174 CustomData_set_layer_clone(data, type, cloneindex);
177 if (stencillayerdata != layerdata) {
179 stencilindex = CustomData_get_layer_index(data, type);
180 for (i = stencilindex; i < data->totlayer; i++) {
181 if (data->layers[i].data == stencillayerdata) {
182 stencilindex = i - stencilindex;
188 CustomData_set_layer_stencil(data, type, stencilindex);
192 /* copies from active to 'index' */
193 static void editmesh_face_copy_customdata(BMEditMesh *em, int type, int index)
196 CustomData *pdata = &bm->pdata;
199 const int n = CustomData_get_active_layer(pdata, type);
201 /* ensure all current elements follow new customdata layout */
202 BM_ITER(efa, &iter, bm, BM_FACES_OF_MESH, NULL) {
203 void *data = CustomData_bmesh_get_n(pdata, efa->head.data, type, n);
204 CustomData_bmesh_set_n(pdata, efa->head.data, type, index, data);
208 /* copies from active to 'index' */
209 static void editmesh_loop_copy_customdata(BMEditMesh *em, int type, int index)
212 CustomData *ldata = &bm->ldata;
217 const int n = CustomData_get_active_layer(ldata, type);
219 /* ensure all current elements follow new customdata layout */
220 BM_ITER(efa, &iter, bm, BM_FACES_OF_MESH, NULL) {
221 BM_ITER(loop, &liter, bm, BM_LOOPS_OF_FACE, efa) {
222 void *data = CustomData_bmesh_get_n(ldata, loop->head.data, type, n);
223 CustomData_bmesh_set_n(ldata, loop->head.data, type, index, data);
228 int ED_mesh_uv_loop_reset_ex(struct bContext *C, struct Mesh *me, const int layernum)
230 BMEditMesh *em = me->edit_btmesh;
232 BLI_array_declare(polylengths);
233 int *polylengths = NULL;
234 BLI_array_declare(uvs);
240 /* Collect BMesh UVs */
246 BLI_assert(CustomData_has_layer(&em->bm->ldata, CD_MLOOPUV));
248 BM_ITER(efa, &iter, em->bm, BM_FACES_OF_MESH, NULL) {
249 if (!BM_elem_flag_test(efa, BM_ELEM_SELECT))
253 BM_ITER(l, &liter, em->bm, BM_LOOPS_OF_FACE, efa) {
254 luv = CustomData_bmesh_get_n(&em->bm->ldata, l->head.data, CD_MLOOPUV, layernum);
255 BLI_array_append(uvs, luv->uv);
259 BLI_array_append(polylengths, efa->len);
263 /* Collect Mesh UVs */
268 BLI_assert(CustomData_has_layer(&me->ldata, CD_MLOOPUV));
269 mloouv = CustomData_get_layer_n(&me->ldata, CD_MLOOPUV, layernum);
271 for (j = 0; j < me->totpoly; j++) {
274 for (i = 0; i < mp->totloop; i++) {
275 luv = &mloouv[mp->loopstart + i];
276 BLI_array_append(uvs, luv->uv);
279 BLI_array_append(polylengths, mp->totloop);
284 for (j = 0; j < BLI_array_count(polylengths); j++) {
285 int len = polylengths[j];
309 /*make sure we ignore 2-sided faces*/
312 float fac = 0.0f, dfac = 1.0f / (float)len;
316 for (i = 0; i < len; i++) {
317 fuvs[i][0] = 0.5f * sin(fac) + 0.5f;
318 fuvs[i][1] = 0.5f * cos(fac) + 0.5f;
328 BLI_array_free(polylengths);
330 DAG_id_tag_update(&me->id, 0);
331 WM_event_add_notifier(C, NC_GEOM | ND_DATA, me);
336 int ED_mesh_uv_loop_reset(struct bContext *C, struct Mesh *me)
338 /* could be ldata or pdata */
339 CustomData *pdata = GET_CD_DATA(me, pdata);
340 const int layernum = CustomData_get_active_layer_index(pdata, CD_MTEXPOLY);
341 return ED_mesh_uv_loop_reset_ex(C, me, layernum);
344 /* note: keep in sync with ED_mesh_color_add */
345 int ED_mesh_uv_texture_add(bContext *C, Mesh *me, const char *name, int active_set)
350 short is_init = FALSE;
352 if (me->edit_btmesh) {
353 em = me->edit_btmesh;
355 layernum = CustomData_number_of_layers(&em->bm->pdata, CD_MTEXPOLY);
356 if (layernum >= MAX_MTFACE)
360 BM_data_layer_add_named(em->bm, &em->bm->pdata, CD_MTEXPOLY, name);
361 /* copy data from active UV */
363 editmesh_face_copy_customdata(em, CD_MTEXPOLY, layernum);
365 if (active_set || layernum == 0) {
366 CustomData_set_layer_active(&em->bm->pdata, CD_MTEXPOLY, layernum);
370 BM_data_layer_add_named(em->bm, &em->bm->ldata, CD_MLOOPUV, name);
371 /* copy data from active UV */
373 editmesh_loop_copy_customdata(em, CD_MLOOPUV, layernum);
376 if (active_set || layernum == 0) {
377 CustomData_set_layer_active(&em->bm->ldata, CD_MLOOPUV, layernum);
381 layernum = CustomData_number_of_layers(&me->pdata, CD_MTEXPOLY);
382 if (layernum >= MAX_MTFACE)
386 CustomData_add_layer_named(&me->pdata, CD_MTEXPOLY, CD_DUPLICATE, me->mtpoly, me->totpoly, name);
387 CustomData_add_layer_named(&me->ldata, CD_MLOOPUV, CD_DUPLICATE, me->mloopuv, me->totloop, name);
388 CustomData_add_layer_named(&me->fdata, CD_MTFACE, CD_DUPLICATE, me->mtface, me->totface, name);
392 CustomData_add_layer_named(&me->pdata, CD_MTEXPOLY, CD_DEFAULT, NULL, me->totpoly, name);
393 CustomData_add_layer_named(&me->ldata, CD_MLOOPUV, CD_DEFAULT, NULL, me->totloop, name);
394 CustomData_add_layer_named(&me->fdata, CD_MTFACE, CD_DEFAULT, NULL, me->totface, name);
397 if (active_set || layernum == 0) {
398 CustomData_set_layer_active(&me->pdata, CD_MTEXPOLY, layernum);
399 CustomData_set_layer_active(&me->ldata, CD_MLOOPUV, layernum);
401 CustomData_set_layer_active(&me->fdata, CD_MTFACE, layernum);
404 mesh_update_customdata_pointers(me, TRUE);
407 /* don't overwrite our copied coords */
408 if (is_init == FALSE) {
409 ED_mesh_uv_loop_reset_ex(C, me, layernum);
412 DAG_id_tag_update(&me->id, 0);
413 WM_event_add_notifier(C, NC_GEOM | ND_DATA, me);
418 int ED_mesh_uv_texture_remove(bContext *C, Object *ob, Mesh *me)
420 CustomData *pdata = GET_CD_DATA(me, pdata), *ldata = GET_CD_DATA(me, ldata);
421 CustomDataLayer *cdlp, *cdlu;
424 index = CustomData_get_active_layer_index(pdata, CD_MTEXPOLY);
425 cdlp = (index == -1) ? NULL : &pdata->layers[index];
427 index = CustomData_get_active_layer_index(ldata, CD_MLOOPUV);
428 cdlu = (index == -1) ? NULL : &ldata->layers[index];
433 delete_customdata_layer(C, ob, cdlp);
434 delete_customdata_layer(C, ob, cdlu);
436 DAG_id_tag_update(&me->id, 0);
437 WM_event_add_notifier(C, NC_GEOM | ND_DATA, me);
442 /* note: keep in sync with ED_mesh_uv_texture_add */
443 int ED_mesh_color_add(bContext *C, Scene *UNUSED(scene), Object *UNUSED(ob), Mesh *me, const char *name, int active_set)
448 if (me->edit_btmesh) {
449 em = me->edit_btmesh;
451 layernum = CustomData_number_of_layers(&em->bm->ldata, CD_MLOOPCOL);
452 if (layernum >= MAX_MCOL) {
457 BM_data_layer_add_named(em->bm, &em->bm->ldata, CD_MLOOPCOL, name);
458 /* copy data from active vertex color layer */
460 editmesh_loop_copy_customdata(em, CD_MLOOPCOL, layernum);
462 if (active_set || layernum == 0) {
463 CustomData_set_layer_active(&em->bm->ldata, CD_MLOOPCOL, layernum);
467 layernum = CustomData_number_of_layers(&me->ldata, CD_MLOOPCOL);
468 if (layernum >= CD_MLOOPCOL) {
473 CustomData_add_layer_named(&me->ldata, CD_MLOOPCOL, CD_DUPLICATE, me->mloopcol, me->totloop, name);
474 CustomData_add_layer_named(&me->fdata, CD_MCOL, CD_DUPLICATE, me->mcol, me->totface, name);
477 CustomData_add_layer_named(&me->ldata, CD_MLOOPCOL, CD_DEFAULT, NULL, me->totloop, name);
478 CustomData_add_layer_named(&me->fdata, CD_MCOL, CD_DEFAULT, NULL, me->totface, name);
481 if (active_set || layernum == 0) {
482 CustomData_set_layer_active(&me->ldata, CD_MLOOPCOL, layernum);
483 CustomData_set_layer_active(&me->fdata, CD_MCOL, layernum);
486 mesh_update_customdata_pointers(me, TRUE);
489 DAG_id_tag_update(&me->id, 0);
490 WM_event_add_notifier(C, NC_GEOM | ND_DATA, me);
495 int ED_mesh_color_remove(bContext *C, Object *ob, Mesh *me)
497 CustomData *ldata = GET_CD_DATA(me, ldata);
498 CustomDataLayer *cdl;
501 index = CustomData_get_active_layer_index(ldata, CD_MLOOPCOL);
502 cdl = (index == -1) ? NULL : &ldata->layers[index];
507 delete_customdata_layer(C, ob, cdl);
508 DAG_id_tag_update(&me->id, 0);
509 WM_event_add_notifier(C, NC_GEOM | ND_DATA, me);
514 int ED_mesh_color_remove_named(bContext *C, Object *ob, Mesh *me, const char *name)
516 CustomData *ldata = GET_CD_DATA(me, ldata);
517 CustomDataLayer *cdl;
520 index = CustomData_get_named_layer_index(ldata, CD_MLOOPCOL, name);
521 cdl = (index == -1) ? NULL : &ldata->layers[index];
526 delete_customdata_layer(C, ob, cdl);
527 DAG_id_tag_update(&me->id, 0);
528 WM_event_add_notifier(C, NC_GEOM | ND_DATA, me);
533 /*********************** UV texture operators ************************/
535 static int layers_poll(bContext *C)
537 Object *ob = ED_object_context(C);
538 ID *data = (ob) ? ob->data : NULL;
539 return (ob && !ob->id.lib && ob->type == OB_MESH && data && !data->lib);
542 static int mesh_uv_texture_add_exec(bContext *C, wmOperator *UNUSED(op))
544 Object *ob = ED_object_context(C);
547 if (ED_mesh_uv_texture_add(C, me, NULL, TRUE) == -1)
548 return OPERATOR_CANCELLED;
550 return OPERATOR_FINISHED;
553 void MESH_OT_uv_texture_add(wmOperatorType *ot)
556 ot->name = "Add UV Map";
557 ot->description = "Add UV Map";
558 ot->idname = "MESH_OT_uv_texture_add";
561 ot->poll = layers_poll;
562 ot->exec = mesh_uv_texture_add_exec;
565 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
568 static int drop_named_image_invoke(bContext *C, wmOperator *op, wmEvent *event)
570 Main *bmain = CTX_data_main(C);
571 Scene *scene = CTX_data_scene(C);
572 View3D *v3d = CTX_wm_view3d(C);
573 Base *base = ED_view3d_give_base_under_cursor(C, event->mval);
578 char name[MAX_ID_NAME - 2];
581 if (base == NULL || base->object->type != OB_MESH) {
582 BKE_report(op->reports, RPT_ERROR, "Not an Object or Mesh");
583 return OPERATOR_CANCELLED;
586 /* check input variables */
587 if (RNA_struct_property_is_set(op->ptr, "filepath")) {
590 RNA_string_get(op->ptr, "filepath", path);
591 ima = BKE_add_image_file(path);
594 RNA_string_get(op->ptr, "name", name);
595 ima = (Image *)find_id("IM", name);
599 BKE_report(op->reports, RPT_ERROR, "Not an Image");
600 return OPERATOR_CANCELLED;
603 /* put mesh in editmode */
605 obedit = base->object;
607 if (me->edit_btmesh == NULL) {
608 EDBM_mesh_make(scene->toolsettings, scene, obedit);
611 if (me->edit_btmesh == NULL)
612 return OPERATOR_CANCELLED;
614 ED_uvedit_assign_image(bmain, scene, obedit, ima, NULL);
617 EDBM_mesh_load(obedit);
618 EDBM_mesh_free(me->edit_btmesh);
619 MEM_freeN(me->edit_btmesh);
620 me->edit_btmesh = NULL;
622 /* load_editMesh free's pointers used by CustomData layers which might be used by DerivedMesh too,
623 * so signal to re-create DerivedMesh here (sergey) */
624 DAG_id_tag_update(&me->id, 0);
627 /* dummie drop support; ensure view shows a result :) */
629 v3d->flag2 |= V3D_SOLID_TEX;
631 WM_event_add_notifier(C, NC_GEOM | ND_DATA, obedit->data);
633 return OPERATOR_FINISHED;
636 void MESH_OT_drop_named_image(wmOperatorType *ot)
639 ot->name = "Assign Image to UV Map";
640 ot->description = "Assign Image to active UV Map, or create an UV Map";
641 ot->idname = "MESH_OT_drop_named_image";
644 ot->poll = layers_poll;
645 ot->invoke = drop_named_image_invoke;
648 ot->flag = OPTYPE_UNDO;
651 RNA_def_string(ot->srna, "name", "Image", MAX_ID_NAME - 2, "Name", "Image name to assign");
652 RNA_def_string(ot->srna, "filepath", "Path", FILE_MAX, "Filepath", "Path to image file");
655 static int mesh_uv_texture_remove_exec(bContext *C, wmOperator *UNUSED(op))
657 Object *ob = ED_object_context(C);
660 if (!ED_mesh_uv_texture_remove(C, ob, me))
661 return OPERATOR_CANCELLED;
663 return OPERATOR_FINISHED;
666 void MESH_OT_uv_texture_remove(wmOperatorType *ot)
669 ot->name = "Remove UV Map";
670 ot->description = "Remove UV Map";
671 ot->idname = "MESH_OT_uv_texture_remove";
674 ot->poll = layers_poll;
675 ot->exec = mesh_uv_texture_remove_exec;
678 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
681 /*********************** vertex color operators ************************/
683 static int mesh_vertex_color_add_exec(bContext *C, wmOperator *UNUSED(op))
685 Scene *scene = CTX_data_scene(C);
686 Object *ob = ED_object_context(C);
689 if (ED_mesh_color_add(C, scene, ob, me, NULL, TRUE) == -1)
690 return OPERATOR_CANCELLED;
692 return OPERATOR_FINISHED;
695 void MESH_OT_vertex_color_add(wmOperatorType *ot)
698 ot->name = "Add Vertex Color";
699 ot->description = "Add vertex color layer";
700 ot->idname = "MESH_OT_vertex_color_add";
703 ot->poll = layers_poll;
704 ot->exec = mesh_vertex_color_add_exec;
707 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
710 static int mesh_vertex_color_remove_exec(bContext *C, wmOperator *UNUSED(op))
712 Object *ob = ED_object_context(C);
715 if (!ED_mesh_color_remove(C, ob, me))
716 return OPERATOR_CANCELLED;
718 return OPERATOR_FINISHED;
721 void MESH_OT_vertex_color_remove(wmOperatorType *ot)
724 ot->name = "Remove Vertex Color";
725 ot->description = "Remove vertex color layer";
726 ot->idname = "MESH_OT_vertex_color_remove";
729 ot->exec = mesh_vertex_color_remove_exec;
730 ot->poll = layers_poll;
733 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
736 /*********************** sticky operators ************************/
738 static int mesh_sticky_add_exec(bContext *C, wmOperator *UNUSED(op))
740 Scene *scene = CTX_data_scene(C);
741 View3D *v3d = CTX_wm_view3d(C);
742 Object *ob = ED_object_context(C);
745 /* why is this commented out? */
748 return OPERATOR_CANCELLED;
751 RE_make_sticky(scene, v3d);
753 DAG_id_tag_update(&me->id, 0);
754 WM_event_add_notifier(C, NC_GEOM | ND_DATA, me);
756 return OPERATOR_FINISHED;
759 void MESH_OT_sticky_add(wmOperatorType *ot)
762 ot->name = "Add Sticky";
763 ot->description = "Add sticky UV texture layer";
764 ot->idname = "MESH_OT_sticky_add";
767 ot->poll = layers_poll;
768 ot->exec = mesh_sticky_add_exec;
771 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
774 static int mesh_sticky_remove_exec(bContext *C, wmOperator *UNUSED(op))
776 Object *ob = ED_object_context(C);
780 return OPERATOR_CANCELLED;
782 CustomData_free_layer_active(&me->vdata, CD_MSTICKY, me->totvert);
785 DAG_id_tag_update(&me->id, 0);
786 WM_event_add_notifier(C, NC_GEOM | ND_DATA, me);
788 return OPERATOR_FINISHED;
791 void MESH_OT_sticky_remove(wmOperatorType *ot)
794 ot->name = "Remove Sticky";
795 ot->description = "Remove sticky UV texture layer";
796 ot->idname = "MESH_OT_sticky_remove";
799 ot->poll = layers_poll;
800 ot->exec = mesh_sticky_remove_exec;
803 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
806 /************************** Add Geometry Layers *************************/
808 void ED_mesh_update(Mesh *mesh, bContext *C, int calc_edges, int calc_tessface)
810 int *polyindex = NULL;
811 float (*face_nors)[3];
812 int tessface_input = FALSE;
814 if (mesh->totface > 0 && mesh->totpoly == 0) {
815 BKE_mesh_convert_mfaces_to_mpolys(mesh);
817 /* would only be converting back again, don't bother */
818 tessface_input = TRUE;
820 /* it also happens that converting the faces calculates edges, skip this */
824 if (calc_edges || (mesh->totpoly && mesh->totedge == 0))
825 BKE_mesh_calc_edges(mesh, calc_edges);
828 if (tessface_input == FALSE) {
829 BKE_mesh_tessface_calc(mesh);
833 /* default state is not to have tessface's so make sure this is the case */
834 BKE_mesh_tessface_clear(mesh);
837 /* note on this if/else - looks like these layers are not needed
838 * so rather then add poly-index layer and calculate normals for it
839 * calculate normals only for the mvert's. - campbell */
840 #ifdef USE_BMESH_MPOLY_NORMALS
841 polyindex = CustomData_get_layer(&mesh->fdata, CD_POLYINDEX);
842 /* add a normals layer for tessellated faces, a tessface normal will
843 * contain the normal of the poly the face was tessellated from. */
844 face_nors = CustomData_add_layer(&mesh->fdata, CD_NORMAL, CD_CALLOC, NULL, mesh->totface);
846 mesh_calc_normals_mapping_ex(mesh->mvert, mesh->totvert,
847 mesh->mloop, mesh->mpoly,
848 mesh->totloop, mesh->totpoly,
849 NULL /* polyNors_r */,
850 mesh->mface, mesh->totface,
851 polyindex, face_nors, FALSE);
853 mesh_calc_normals(mesh->mvert, mesh->totvert,
854 mesh->mloop, mesh->mpoly, mesh->totloop, mesh->totpoly,
860 DAG_id_tag_update(&mesh->id, 0);
861 WM_event_add_notifier(C, NC_GEOM | ND_DATA, mesh);
864 static void mesh_add_verts(Mesh *mesh, int len)
873 totvert = mesh->totvert + len;
874 CustomData_copy(&mesh->vdata, &vdata, CD_MASK_MESH, CD_DEFAULT, totvert);
875 CustomData_copy_data(&mesh->vdata, &vdata, 0, 0, mesh->totvert);
877 if (!CustomData_has_layer(&vdata, CD_MVERT))
878 CustomData_add_layer(&vdata, CD_MVERT, CD_CALLOC, NULL, totvert);
880 CustomData_free(&mesh->vdata, mesh->totvert);
882 mesh_update_customdata_pointers(mesh, FALSE);
884 /* scan the input list and insert the new vertices */
886 mvert = &mesh->mvert[mesh->totvert];
887 for (i = 0; i < len; i++, mvert++)
888 mvert->flag |= SELECT;
890 /* set final vertex list size */
891 mesh->totvert = totvert;
894 void ED_mesh_transform(Mesh *me, float *mat)
897 MVert *mvert = me->mvert;
899 for (i = 0; i < me->totvert; i++, mvert++)
900 mul_m4_v3((float (*)[4])mat, mvert->co);
902 mesh_calc_normals_mapping(me->mvert, me->totvert, me->mloop, me->mpoly, me->totloop, me->totpoly, NULL, NULL, 0, NULL, NULL);
905 static void mesh_add_edges(Mesh *mesh, int len)
914 totedge = mesh->totedge + len;
916 /* update customdata */
917 CustomData_copy(&mesh->edata, &edata, CD_MASK_MESH, CD_DEFAULT, totedge);
918 CustomData_copy_data(&mesh->edata, &edata, 0, 0, mesh->totedge);
920 if (!CustomData_has_layer(&edata, CD_MEDGE))
921 CustomData_add_layer(&edata, CD_MEDGE, CD_CALLOC, NULL, totedge);
923 CustomData_free(&mesh->edata, mesh->totedge);
925 mesh_update_customdata_pointers(mesh, FALSE); /* new edges don't change tessellation */
927 /* set default flags */
928 medge = &mesh->medge[mesh->totedge];
929 for (i = 0; i < len; i++, medge++)
930 medge->flag = ME_EDGEDRAW | ME_EDGERENDER | SELECT;
932 mesh->totedge = totedge;
935 static void mesh_add_tessfaces(Mesh *mesh, int len)
944 totface = mesh->totface + len; /* new face count */
946 /* update customdata */
947 CustomData_copy(&mesh->fdata, &fdata, CD_MASK_MESH, CD_DEFAULT, totface);
948 CustomData_copy_data(&mesh->fdata, &fdata, 0, 0, mesh->totface);
950 if (!CustomData_has_layer(&fdata, CD_MFACE))
951 CustomData_add_layer(&fdata, CD_MFACE, CD_CALLOC, NULL, totface);
953 CustomData_free(&mesh->fdata, mesh->totface);
955 mesh_update_customdata_pointers(mesh, TRUE);
957 /* set default flags */
958 mface = &mesh->mface[mesh->totface];
959 for (i = 0; i < len; i++, mface++)
960 mface->flag = ME_FACE_SEL;
962 mesh->totface = totface;
965 static void mesh_add_loops(Mesh *mesh, int len)
973 totloop = mesh->totloop + len; /* new face count */
975 /* update customdata */
976 CustomData_copy(&mesh->ldata, &ldata, CD_MASK_MESH, CD_DEFAULT, totloop);
977 CustomData_copy_data(&mesh->ldata, &ldata, 0, 0, mesh->totloop);
979 if (!CustomData_has_layer(&ldata, CD_MLOOP))
980 CustomData_add_layer(&ldata, CD_MLOOP, CD_CALLOC, NULL, totloop);
982 CustomData_free(&mesh->ldata, mesh->totloop);
984 mesh_update_customdata_pointers(mesh, TRUE);
986 mesh->totloop = totloop;
989 static void mesh_add_polys(Mesh *mesh, int len)
998 totpoly = mesh->totpoly + len; /* new face count */
1000 /* update customdata */
1001 CustomData_copy(&mesh->pdata, &pdata, CD_MASK_MESH, CD_DEFAULT, totpoly);
1002 CustomData_copy_data(&mesh->pdata, &pdata, 0, 0, mesh->totpoly);
1004 if (!CustomData_has_layer(&pdata, CD_MPOLY))
1005 CustomData_add_layer(&pdata, CD_MPOLY, CD_CALLOC, NULL, totpoly);
1007 CustomData_free(&mesh->pdata, mesh->totpoly);
1008 mesh->pdata = pdata;
1009 mesh_update_customdata_pointers(mesh, TRUE);
1011 /* set default flags */
1012 mpoly = &mesh->mpoly[mesh->totpoly];
1013 for (i = 0; i < len; i++, mpoly++)
1014 mpoly->flag = ME_FACE_SEL;
1016 mesh->totpoly = totpoly;
1019 static void mesh_remove_verts(Mesh *mesh, int len)
1026 totvert = mesh->totvert - len;
1027 CustomData_free_elem(&mesh->vdata, totvert, len);
1029 /* set final vertex list size */
1030 mesh->totvert = totvert;
1033 static void mesh_remove_edges(Mesh *mesh, int len)
1040 totedge = mesh->totedge - len;
1041 CustomData_free_elem(&mesh->edata, totedge, len);
1043 mesh->totedge = totedge;
1046 static void mesh_remove_faces(Mesh *mesh, int len)
1053 totface = mesh->totface - len; /* new face count */
1054 CustomData_free_elem(&mesh->fdata, totface, len);
1056 mesh->totface = totface;
1060 void ED_mesh_geometry_add(Mesh *mesh, ReportList *reports, int verts, int edges, int faces)
1062 if (mesh->edit_btmesh) {
1063 BKE_report(reports, RPT_ERROR, "Can't add geometry in edit mode");
1068 mesh_add_verts(mesh, verts);
1070 mesh_add_edges(mesh, edges);
1072 mesh_add_faces(mesh, faces);
1076 void ED_mesh_tessfaces_add(Mesh *mesh, ReportList *reports, int count)
1078 if (mesh->edit_btmesh) {
1079 BKE_report(reports, RPT_ERROR, "Can't add tessfaces in edit mode");
1084 BKE_report(reports, RPT_ERROR, "Can't add tessfaces to a mesh that already has polygons");
1088 mesh_add_tessfaces(mesh, count);
1091 void ED_mesh_edges_add(Mesh *mesh, ReportList *reports, int count)
1093 if (mesh->edit_btmesh) {
1094 BKE_report(reports, RPT_ERROR, "Can't add edges in edit mode");
1098 mesh_add_edges(mesh, count);
1101 void ED_mesh_vertices_add(Mesh *mesh, ReportList *reports, int count)
1103 if (mesh->edit_btmesh) {
1104 BKE_report(reports, RPT_ERROR, "Can't add vertices in edit mode");
1108 mesh_add_verts(mesh, count);
1111 void ED_mesh_faces_remove(Mesh *mesh, ReportList *reports, int count)
1113 if (mesh->edit_btmesh) {
1114 BKE_report(reports, RPT_ERROR, "Can't remove faces in edit mode");
1117 else if (count > mesh->totface) {
1118 BKE_report(reports, RPT_ERROR, "Can't remove more faces than the mesh contains");
1122 mesh_remove_faces(mesh, count);
1125 void ED_mesh_edges_remove(Mesh *mesh, ReportList *reports, int count)
1127 if (mesh->edit_btmesh) {
1128 BKE_report(reports, RPT_ERROR, "Can't remove edges in edit mode");
1131 else if (count > mesh->totedge) {
1132 BKE_report(reports, RPT_ERROR, "Can't remove more edges than the mesh contains");
1136 mesh_remove_edges(mesh, count);
1139 void ED_mesh_vertices_remove(Mesh *mesh, ReportList *reports, int count)
1141 if (mesh->edit_btmesh) {
1142 BKE_report(reports, RPT_ERROR, "Can't remove vertices in edit mode");
1145 else if (count > mesh->totvert) {
1146 BKE_report(reports, RPT_ERROR, "Can't remove more vertices than the mesh contains");
1150 mesh_remove_verts(mesh, count);
1153 void ED_mesh_loops_add(Mesh *mesh, ReportList *reports, int count)
1155 if (mesh->edit_btmesh) {
1156 BKE_report(reports, RPT_ERROR, "Can't add loops in edit mode.");
1160 mesh_add_loops(mesh, count);
1163 void ED_mesh_polys_add(Mesh *mesh, ReportList *reports, int count)
1165 if (mesh->edit_btmesh) {
1166 BKE_report(reports, RPT_ERROR, "Can't add polys in edit mode.");
1170 mesh_add_polys(mesh, count);
1173 void ED_mesh_calc_normals(Mesh *mesh)
1175 #ifdef USE_BMESH_MPOLY_NORMALS
1176 mesh_calc_normals_mapping_ex(mesh->mvert, mesh->totvert,
1177 mesh->mloop, mesh->mpoly, mesh->totloop, mesh->totpoly,
1178 NULL, NULL, 0, NULL, NULL, FALSE);
1180 mesh_calc_normals(mesh->mvert, mesh->totvert,
1181 mesh->mloop, mesh->mpoly, mesh->totloop, mesh->totpoly,
1186 void ED_mesh_calc_tessface(Mesh *mesh)
1188 if (mesh->edit_btmesh) {
1189 BMEdit_RecalcTessellation(mesh->edit_btmesh);
1192 BKE_mesh_tessface_calc(mesh);