Blender V5.0
mesh_data.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2009 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "MEM_guardedalloc.h"
10
11#include "DNA_object_types.h"
12#include "DNA_scene_types.h"
13
14#include "BLI_array.hh"
15#include "BLI_math_constants.h"
16
17#include "BKE_attribute.hh"
18#include "BKE_context.hh"
19#include "BKE_customdata.hh"
20#include "BKE_editmesh.hh"
21#include "BKE_key.hh"
22#include "BKE_library.hh"
23#include "BKE_mesh.hh"
24#include "BKE_mesh_runtime.hh"
25#include "BKE_report.hh"
26
27#include "DEG_depsgraph.hh"
28
29#include "RNA_prototypes.hh"
30
31#include "WM_api.hh"
32#include "WM_types.hh"
33
34#include "BLT_translation.hh"
35
36#include "ED_mesh.hh"
37#include "ED_object.hh"
38#include "ED_paint.hh"
39#include "ED_screen.hh"
40
42
43#include "mesh_intern.hh" /* own include */
44
45using blender::Array;
46using blender::float2;
47using blender::float3;
49using blender::Span;
51
52static CustomData *mesh_customdata_get_type(Mesh *mesh, const char htype, int *r_tot)
53{
55 BMesh *bm = (mesh->runtime->edit_mesh) ? mesh->runtime->edit_mesh->bm : nullptr;
56 int tot;
57
58 switch (htype) {
59 case BM_VERT:
60 if (bm) {
61 data = &bm->vdata;
62 tot = bm->totvert;
63 }
64 else {
65 data = &mesh->vert_data;
66 tot = mesh->verts_num;
67 }
68 break;
69 case BM_EDGE:
70 if (bm) {
71 data = &bm->edata;
72 tot = bm->totedge;
73 }
74 else {
75 data = &mesh->edge_data;
76 tot = mesh->edges_num;
77 }
78 break;
79 case BM_LOOP:
80 if (bm) {
81 data = &bm->ldata;
82 tot = bm->totloop;
83 }
84 else {
85 data = &mesh->corner_data;
86 tot = mesh->corners_num;
87 }
88 break;
89 case BM_FACE:
90 if (bm) {
91 data = &bm->pdata;
92 tot = bm->totface;
93 }
94 else {
95 data = &mesh->face_data;
96 tot = mesh->faces_num;
97 }
98 break;
99 default:
100 BLI_assert(0);
101 tot = 0;
102 data = nullptr;
103 break;
104 }
105
106 if (r_tot) {
107 *r_tot = tot;
108 }
109 return data;
110}
111
112static void mesh_uv_reset_array(float **fuv, const int len)
113{
114 if (len == 3) {
115 fuv[0][0] = 0.0;
116 fuv[0][1] = 0.0;
117
118 fuv[1][0] = 1.0;
119 fuv[1][1] = 0.0;
120
121 fuv[2][0] = 1.0;
122 fuv[2][1] = 1.0;
123 }
124 else if (len == 4) {
125 fuv[0][0] = 0.0;
126 fuv[0][1] = 0.0;
127
128 fuv[1][0] = 1.0;
129 fuv[1][1] = 0.0;
130
131 fuv[2][0] = 1.0;
132 fuv[2][1] = 1.0;
133
134 fuv[3][0] = 0.0;
135 fuv[3][1] = 1.0;
136 /* Make sure we ignore 2-sided faces. */
137 }
138 else if (len > 2) {
139 float fac = 0.0f, dfac = 1.0f / float(len);
140
141 dfac *= float(M_PI) * 2.0f;
142
143 for (int i = 0; i < len; i++) {
144 fuv[i][0] = 0.5f * sinf(fac) + 0.5f;
145 fuv[i][1] = 0.5f * cosf(fac) + 0.5f;
146
147 fac += dfac;
148 }
149 }
150}
151
152static void reset_uvs_bmesh(BMFace *f, const int cd_loop_uv_offset)
153{
155 BMIter liter;
156 BMLoop *l;
157 int i;
158
159 BM_ITER_ELEM_INDEX (l, &liter, f, BM_LOOPS_OF_FACE, i) {
160 fuv[i] = ((float *)BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset));
161 }
162
163 mesh_uv_reset_array(fuv.data(), f->len);
164}
165
167{
169
170 for (int i = 0; i < face.size(); i++) {
171 fuv[i] = &uv_map[face[i]].x;
172 }
173
174 mesh_uv_reset_array(fuv.data(), face.size());
175}
176
177static void reset_uv_map(Mesh *mesh, const StringRef name)
178{
179 using namespace blender;
180 if (BMEditMesh *em = mesh->runtime->edit_mesh.get()) {
181 const int cd_loop_uv_offset = CustomData_get_offset_named(
182 &em->bm->ldata, CD_PROP_FLOAT2, name);
183 BLI_assert(cd_loop_uv_offset >= 0);
184
185 BMFace *efa;
186 BMIter iter;
187 BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
189 continue;
190 }
191 reset_uvs_bmesh(efa, cd_loop_uv_offset);
192 }
193 }
194 else {
195 const OffsetIndices faces = mesh->faces();
196 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
199 for (const int i : faces.index_range()) {
200 reset_uvs_mesh(faces[i], uv_map.span);
201 }
202 uv_map.finish();
203 }
204
205 DEG_id_tag_update(&mesh->id, 0);
206}
207
209{
210 /* could be ldata or pdata */
211 CustomData *ldata = mesh_customdata_get_type(mesh, BM_LOOP, nullptr);
213 reset_uv_map(mesh, name);
214
216}
217
219 Mesh *mesh, const char *name, const bool active_set, const bool do_init, ReportList *reports)
220{
221 /* NOTE: keep in sync with #ED_mesh_color_add. */
222
223 int layernum_dst;
224
225 if (!name) {
226 name = DATA_("UVMap");
227 }
228
230 const std::string unique_name = BKE_attribute_calc_unique_name(owner, name);
231 bool is_init = false;
232
233 if (BMEditMesh *em = mesh->runtime->edit_mesh.get()) {
234 layernum_dst = CustomData_number_of_layers(&em->bm->ldata, CD_PROP_FLOAT2);
235 if (layernum_dst >= MAX_MTFACE) {
236 BKE_reportf(reports, RPT_WARNING, "Cannot add more than %i UV maps", MAX_MTFACE);
237 return -1;
238 }
239
240 BM_data_layer_add_named(em->bm, &em->bm->ldata, CD_PROP_FLOAT2, unique_name.c_str());
242 /* copy data from active UV */
243 if (layernum_dst && do_init) {
244 const int layernum_src = CustomData_get_active_layer(&em->bm->ldata, CD_PROP_FLOAT2);
245 BM_data_layer_copy(em->bm, &em->bm->ldata, CD_PROP_FLOAT2, layernum_src, layernum_dst);
246
247 is_init = true;
248 }
249 if (active_set || layernum_dst == 0) {
250 CustomData_set_layer_active(&em->bm->ldata, CD_PROP_FLOAT2, layernum_dst);
251 }
252 }
253 else {
255 if (layernum_dst >= MAX_MTFACE) {
256 BKE_reportf(reports, RPT_WARNING, "Cannot add more than %i UV maps", MAX_MTFACE);
257 return -1;
258 }
259
260 if (CustomData_has_layer(&mesh->corner_data, CD_PROP_FLOAT2) && do_init) {
262 &mesh->corner_data,
265 mesh->corners_num,
267 nullptr);
268
269 is_init = true;
270 }
271 else {
274 }
275
276 if (active_set || layernum_dst == 0) {
278 }
279 }
280
281 /* don't overwrite our copied coords */
282 if (!is_init && do_init) {
284 }
285
286 DEG_id_tag_update(&mesh->id, 0);
288
289 return layernum_dst;
290}
291
293{
294 const blender::bke::AttributeAccessor attributes = mesh.attributes();
295 return *attributes.lookup_or_default<bool>(name, blender::bke::AttrDomain::Corner, false);
296}
297
299{
300 using namespace blender::bke;
301 char buffer[MAX_CUSTOMDATA_LAYER_NAME];
302 const char *uv_name = CustomData_get_layer_name(&mesh->corner_data, CD_PROP_FLOAT2, uv_index);
304}
305
313
315{
316 using namespace blender::bke;
317 char buffer[MAX_CUSTOMDATA_LAYER_NAME];
318 const char *uv_name = CustomData_get_layer_name(&mesh->corner_data, CD_PROP_FLOAT2, uv_index);
320}
321
322void ED_mesh_uv_ensure(Mesh *mesh, const char *name)
323{
324 int layernum_dst;
325
326 if (BMEditMesh *em = mesh->runtime->edit_mesh.get()) {
327 layernum_dst = CustomData_number_of_layers(&em->bm->ldata, CD_PROP_FLOAT2);
328 if (layernum_dst == 0) {
329 ED_mesh_uv_add(mesh, name, true, true, nullptr);
330 }
331 }
332 else {
333 layernum_dst = CustomData_number_of_layers(&mesh->corner_data, CD_PROP_FLOAT2);
334 if (layernum_dst == 0) {
335 ED_mesh_uv_add(mesh, name, true, true, nullptr);
336 }
337 }
338}
339
341 Mesh *mesh, const char *name, const bool active_set, const bool do_init, ReportList *reports)
342{
343 using namespace blender;
344 /* If no name is supplied, provide a backwards compatible default. */
345 if (!name) {
346 name = "Col";
347 }
348
352
353 if (do_init) {
354 const char *active_name = mesh->active_color_attribute;
355 if (const CustomDataLayer *active_layer = BKE_id_attributes_color_find(&mesh->id, active_name))
356 {
357 if (const BMEditMesh *em = mesh->runtime->edit_mesh.get()) {
358 BMesh &bm = *em->bm;
359 const int src_i = CustomData_get_named_layer(&bm.ldata, CD_PROP_BYTE_COLOR, active_name);
360 const int dst_i = CustomData_get_named_layer(&bm.ldata, CD_PROP_BYTE_COLOR, layer->name);
361 BM_data_layer_copy(&bm, &bm.ldata, CD_PROP_BYTE_COLOR, src_i, dst_i);
362 }
363 else {
364 memcpy(
365 layer->data, active_layer->data, CustomData_get_elem_size(layer) * mesh->corners_num);
366 }
367 }
368 }
369
370 if (active_set) {
372 }
373
374 DEG_id_tag_update(&mesh->id, 0);
376
377 int dummy;
380}
381
383{
384 using namespace blender;
385 BLI_assert(mesh->runtime->edit_mesh == nullptr);
386 if (BKE_color_attribute_supported(*mesh, mesh->active_color_attribute)) {
387 return true;
388 }
389
391 const std::string unique_name = BKE_attribute_calc_unique_name(owner, name);
392 if (!mesh->attributes_for_write().add(unique_name,
396 {
397 return false;
398 }
399
403 DEG_id_tag_update(&mesh->id, 0);
404
405 return true;
406}
407
408/*********************** General poll ************************/
409
410static bool layers_poll(bContext *C)
411{
413 ID *data = (ob) ? static_cast<ID *>(ob->data) : nullptr;
414 return (ob && ID_IS_EDITABLE(ob) && !ID_IS_OVERRIDE_LIBRARY(ob) && ob->type == OB_MESH && data &&
416}
417
418/*********************** UV texture operators ************************/
419
421{
422 if (!layers_poll(C)) {
423 return false;
424 }
425
427 Mesh *mesh = static_cast<Mesh *>(ob->data);
430 if (active != -1) {
431 return true;
432 }
433
434 return false;
435}
436
438{
440 Mesh *mesh = static_cast<Mesh *>(ob->data);
441
442 if (ED_mesh_uv_add(mesh, nullptr, true, true, op->reports) == -1) {
443 return OPERATOR_CANCELLED;
444 }
445
446 if (ob->mode & OB_MODE_TEXTURE_PAINT) {
447 Scene *scene = CTX_data_scene(C);
448 ED_paint_proj_mesh_data_check(*scene, *ob, nullptr, nullptr, nullptr, nullptr);
450 }
451
452 return OPERATOR_FINISHED;
453}
454
456{
457 /* identifiers */
458 ot->name = "Add UV Map";
459 ot->description = "Add UV map";
460 ot->idname = "MESH_OT_uv_texture_add";
461
462 /* API callbacks. */
463 ot->poll = layers_poll;
465
466 /* flags */
468}
469
471{
473 Mesh *mesh = static_cast<Mesh *>(ob->data);
474
478 if (!BKE_attribute_remove(owner, name, op->reports)) {
479 return OPERATOR_CANCELLED;
480 }
481
482 if (ob->mode & OB_MODE_TEXTURE_PAINT) {
483 Scene *scene = CTX_data_scene(C);
484 ED_paint_proj_mesh_data_check(*scene, *ob, nullptr, nullptr, nullptr, nullptr);
486 }
487
490
491 return OPERATOR_FINISHED;
492}
493
495{
496 /* identifiers */
497 ot->name = "Remove UV Map";
498 ot->description = "Remove UV map";
499 ot->idname = "MESH_OT_uv_texture_remove";
500
501 /* API callbacks. */
504
505 /* flags */
507}
508
509/* *** CustomData clear functions, we need an operator for each *** */
510
512 char htype,
513 const eCustomDataType type)
514{
516
517 CustomData *data = mesh_customdata_get_type(mesh, htype, nullptr);
518
520
521 if (CustomData_has_layer(data, type)) {
522 if (mesh->runtime->edit_mesh) {
523 BM_data_layer_free(mesh->runtime->edit_mesh->bm, data, type);
524 }
525 else {
527 }
528
531
532 return OPERATOR_FINISHED;
533 }
534 return OPERATOR_CANCELLED;
535}
536
537/* Clear Mask */
539{
541 if (ob && ob->type == OB_MESH) {
542 Mesh *mesh = static_cast<Mesh *>(ob->data);
543
544 /* special case - can't run this if we're in sculpt mode */
545 if (ob->mode & OB_MODE_SCULPT) {
546 return false;
547 }
548
551 if (CustomData_has_layer_named(data, CD_PROP_FLOAT, ".sculpt_mask")) {
552 return true;
553 }
556 return true;
557 }
558 }
559 }
560 return false;
561}
563{
565 Mesh *mesh = static_cast<Mesh *>(object->data);
567 const bool ret_a = BKE_attribute_remove(owner, ".sculpt_mask", op->reports);
569
570 if (ret_a || ret_b == OPERATOR_FINISHED) {
571 return OPERATOR_FINISHED;
572 }
573 return OPERATOR_CANCELLED;
574}
575
577{
578 /* NOTE: no create_mask yet */
579
580 /* identifiers */
581 ot->name = "Clear Sculpt Mask Data";
582 ot->idname = "MESH_OT_customdata_mask_clear";
583 ot->description = "Clear vertex sculpt masking data from the mesh";
584
585 /* API callbacks. */
588
589 /* flags */
591}
592
598{
600
601 if (ob && ob->type == OB_MESH) {
602 Mesh *mesh = static_cast<Mesh *>(ob->data);
606 }
607 }
608 return -1;
609}
610
612{
613 return (mesh_customdata_skin_state(C) == 0);
614}
615
628
630{
631 /* identifiers */
632 ot->name = "Add Skin Data";
633 ot->idname = "MESH_OT_customdata_skin_add";
634 ot->description = "Add a vertex skin layer";
635
636 /* API callbacks. */
639
640 /* flags */
642}
643
645{
646 return (mesh_customdata_skin_state(C) == 1);
647}
648
653
655{
656 /* identifiers */
657 ot->name = "Clear Skin Data";
658 ot->idname = "MESH_OT_customdata_skin_clear";
659 ot->description = "Clear vertex skin layer";
660
661 /* API callbacks. */
664
665 /* flags */
667}
668
669/* Clear custom loop normals */
671 wmOperator * /*op*/)
672{
673 using namespace blender;
676 return OPERATOR_CANCELLED;
677 }
678
679 if (mesh->runtime->edit_mesh) {
680 BMesh &bm = *mesh->runtime->edit_mesh->bm;
681 BM_data_layer_ensure_named(&bm, &bm.ldata, CD_PROP_INT16_2D, "custom_normal");
682 }
683 else {
684 if (!mesh->attributes_for_write().add<short2>(
686 {
687 return OPERATOR_CANCELLED;
688 }
689 }
690
691 DEG_id_tag_update(&mesh->id, 0);
693
694 return OPERATOR_FINISHED;
695}
696
698{
699 /* identifiers */
700 ot->name = "Add Custom Normals Data";
701 ot->idname = "MESH_OT_customdata_custom_splitnormals_add";
702 ot->description = "Add a custom normals layer, if none exists yet";
703
704 /* API callbacks. */
707
708 /* flags */
710}
711
713 wmOperator * /*op*/)
714{
716
717 if (BMEditMesh *em = mesh->runtime->edit_mesh.get()) {
718 BMesh &bm = *em->bm;
719 if (!CustomData_has_layer_named(&bm.ldata, CD_PROP_INT16_2D, "custom_normal")) {
720 return OPERATOR_CANCELLED;
721 }
722 BM_data_layer_free_named(&bm, &bm.ldata, "custom_normal");
723 if (bm.lnor_spacearr) {
724 BKE_lnor_spacearr_clear(bm.lnor_spacearr);
725 }
726 }
727 else {
728 if (!mesh->attributes_for_write().remove("custom_normal")) {
729 return OPERATOR_CANCELLED;
730 }
731 }
732
733 mesh->tag_custom_normals_changed();
736
737 return OPERATOR_FINISHED;
738}
739
741{
742 /* identifiers */
743 ot->name = "Clear Custom Normals Data";
744 ot->idname = "MESH_OT_customdata_custom_splitnormals_clear";
745 ot->description = "Remove the custom normals layer, if it exists";
746
747 /* API callbacks. */
750
751 /* flags */
753}
754
755static void mesh_add_verts(Mesh *mesh, int len)
756{
757 using namespace blender;
758 if (len == 0) {
759 return;
760 }
761
762 int totvert = mesh->verts_num + len;
763 CustomData vert_data;
765 &mesh->vert_data, &vert_data, CD_MASK_MESH.vmask, CD_SET_DEFAULT, totvert);
766 CustomData_copy_data(&mesh->vert_data, &vert_data, 0, 0, mesh->verts_num);
767
768 if (!CustomData_has_layer_named(&vert_data, CD_PROP_FLOAT3, "position")) {
769 CustomData_add_layer_named(&vert_data, CD_PROP_FLOAT3, CD_SET_DEFAULT, totvert, "position");
770 }
771
772 CustomData_free(&mesh->vert_data);
773 mesh->vert_data = vert_data;
774
776
777 mesh->verts_num = totvert;
778
779 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
781 ".select_vert", bke::AttrDomain::Point);
782 select_vert.span.take_back(len).fill(true);
783 select_vert.finish();
784}
785
786static void mesh_add_edges(Mesh *mesh, int len)
787{
788 using namespace blender;
789 CustomData edge_data;
790 int totedge;
791
792 if (len == 0) {
793 return;
794 }
795
796 totedge = mesh->edges_num + len;
797
798 /* Update custom-data. */
800 &mesh->edge_data, &edge_data, CD_MASK_MESH.emask, CD_SET_DEFAULT, totedge);
801 CustomData_copy_data(&mesh->edge_data, &edge_data, 0, 0, mesh->edges_num);
802
803 if (!CustomData_has_layer_named(&edge_data, CD_PROP_INT32_2D, ".edge_verts")) {
805 &edge_data, CD_PROP_INT32_2D, CD_SET_DEFAULT, totedge, ".edge_verts");
806 }
807
808 CustomData_free(&mesh->edge_data);
809 mesh->edge_data = edge_data;
810
812
813 mesh->edges_num = totedge;
814
815 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
817 ".select_edge", bke::AttrDomain::Edge);
818 select_edge.span.take_back(len).fill(true);
819 select_edge.finish();
820}
821
822static void mesh_add_loops(Mesh *mesh, int len)
823{
824 CustomData ldata;
825 int totloop;
826
827 if (len == 0) {
828 return;
829 }
830
831 totloop = mesh->corners_num + len; /* new face count */
832
833 /* update customdata */
835 &mesh->corner_data, &ldata, CD_MASK_MESH.lmask, CD_SET_DEFAULT, totloop);
836 CustomData_copy_data(&mesh->corner_data, &ldata, 0, 0, mesh->corners_num);
837
838 if (!CustomData_has_layer_named(&ldata, CD_PROP_INT32, ".corner_vert")) {
839 CustomData_add_layer_named(&ldata, CD_PROP_INT32, CD_SET_DEFAULT, totloop, ".corner_vert");
840 }
841 if (!CustomData_has_layer_named(&ldata, CD_PROP_INT32, ".corner_edge")) {
842 CustomData_add_layer_named(&ldata, CD_PROP_INT32, CD_SET_DEFAULT, totloop, ".corner_edge");
843 }
844
846
847 CustomData_free(&mesh->corner_data);
848 mesh->corner_data = ldata;
849
850 mesh->corners_num = totloop;
851
852 /* Keep the last face offset up to date with the corner total (they must be the same). We have
853 * to be careful here though, since the mesh may not be in a valid state at this point. */
854 if (mesh->face_offset_indices) {
855 mesh->face_offsets_for_write().last() = mesh->corners_num;
856 }
857}
858
859static void mesh_add_faces(Mesh *mesh, int len)
860{
861 using namespace blender;
862 CustomData face_data;
863 int faces_num;
864
865 if (len == 0) {
866 return;
867 }
868
869 faces_num = mesh->faces_num + len; /* new face count */
870
871 /* update customdata */
873 &mesh->face_data, &face_data, CD_MASK_MESH.pmask, CD_SET_DEFAULT, faces_num);
874 CustomData_copy_data(&mesh->face_data, &face_data, 0, 0, mesh->faces_num);
875
876 implicit_sharing::resize_trivial_array(&mesh->face_offset_indices,
877 &mesh->runtime->face_offsets_sharing_info,
878 mesh->faces_num == 0 ? 0 : (mesh->faces_num + 1),
879 faces_num + 1);
880 /* Set common values for convenience. */
881 mesh->face_offset_indices[0] = 0;
882 mesh->face_offset_indices[faces_num] = mesh->corners_num;
883
884 CustomData_free(&mesh->face_data);
885 mesh->face_data = face_data;
886
888
889 mesh->faces_num = faces_num;
890
891 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
893 ".select_poly", bke::AttrDomain::Face);
894 select_poly.span.take_back(len).fill(true);
895 select_poly.finish();
896}
897
898/* -------------------------------------------------------------------- */
901
903{
904 if (mesh->runtime->edit_mesh) {
905 BKE_report(reports, RPT_ERROR, "Cannot add vertices in edit mode");
906 return;
907 }
909}
910
912{
913 if (mesh->runtime->edit_mesh) {
914 BKE_report(reports, RPT_ERROR, "Cannot add edges in edit mode");
915 return;
916 }
918}
919
921{
922 if (mesh->runtime->edit_mesh) {
923 BKE_report(reports, RPT_ERROR, "Cannot add loops in edit mode");
924 return;
925 }
927}
928
930{
931 if (mesh->runtime->edit_mesh) {
932 BKE_report(reports, RPT_ERROR, "Cannot add faces in edit mode");
933 return;
934 }
936}
937
939
940/* -------------------------------------------------------------------- */
943
944static void mesh_remove_verts(Mesh *mesh, int len)
945{
946 if (len == 0) {
947 return;
948 }
949 CustomData_ensure_layers_are_mutable(&mesh->vert_data, mesh->verts_num);
950 const int totvert = mesh->verts_num - len;
951 CustomData_free_elem(&mesh->vert_data, totvert, len);
952 mesh->verts_num = totvert;
953}
954
955static void mesh_remove_edges(Mesh *mesh, int len)
956{
957 if (len == 0) {
958 return;
959 }
960 CustomData_ensure_layers_are_mutable(&mesh->edge_data, mesh->edges_num);
961 const int totedge = mesh->edges_num - len;
962 CustomData_free_elem(&mesh->edge_data, totedge, len);
963 mesh->edges_num = totedge;
964}
965
966static void mesh_remove_loops(Mesh *mesh, int len)
967{
968 if (len == 0) {
969 return;
970 }
971 CustomData_ensure_layers_are_mutable(&mesh->corner_data, mesh->corners_num);
972 const int totloop = mesh->corners_num - len;
973 CustomData_free_elem(&mesh->corner_data, totloop, len);
974 mesh->corners_num = totloop;
975}
976
977static void mesh_remove_faces(Mesh *mesh, int len)
978{
979 if (len == 0) {
980 return;
981 }
982 CustomData_ensure_layers_are_mutable(&mesh->face_data, mesh->faces_num);
983 const int faces_num = mesh->faces_num - len;
984 CustomData_free_elem(&mesh->face_data, faces_num, len);
985 mesh->faces_num = faces_num;
986}
987
989{
990 if (mesh->runtime->edit_mesh) {
991 BKE_report(reports, RPT_ERROR, "Cannot remove vertices in edit mode");
992 return;
993 }
994 if (count > mesh->verts_num) {
995 BKE_report(reports, RPT_ERROR, "Cannot remove more vertices than the mesh contains");
996 return;
997 }
998
1000}
1001
1003{
1004 if (mesh->runtime->edit_mesh) {
1005 BKE_report(reports, RPT_ERROR, "Cannot remove edges in edit mode");
1006 return;
1007 }
1008 if (count > mesh->edges_num) {
1009 BKE_report(reports, RPT_ERROR, "Cannot remove more edges than the mesh contains");
1010 return;
1011 }
1012
1014}
1015
1017{
1018 if (mesh->runtime->edit_mesh) {
1019 BKE_report(reports, RPT_ERROR, "Cannot remove loops in edit mode");
1020 return;
1021 }
1022 if (count > mesh->corners_num) {
1023 BKE_report(reports, RPT_ERROR, "Cannot remove more loops than the mesh contains");
1024 return;
1025 }
1026
1028}
1029
1031{
1032 if (mesh->runtime->edit_mesh) {
1033 BKE_report(reports, RPT_ERROR, "Cannot remove polys in edit mode");
1034 return;
1035 }
1036 if (count > mesh->faces_num) {
1037 BKE_report(reports, RPT_ERROR, "Cannot remove more polys than the mesh contains");
1038 return;
1039 }
1040
1042}
1043
1045{
1046 mesh_remove_verts(mesh, mesh->verts_num);
1047 mesh_remove_edges(mesh, mesh->edges_num);
1048 mesh_remove_loops(mesh, mesh->corners_num);
1049 mesh_remove_faces(mesh, mesh->faces_num);
1050}
1051
1053
1054void ED_mesh_report_mirror_ex(ReportList &reports, int totmirr, int totfail, char selectmode)
1055{
1056 const char *elem_type;
1057
1058 if (selectmode & SCE_SELECT_VERTEX) {
1059 elem_type = "vertices";
1060 }
1061 else if (selectmode & SCE_SELECT_EDGE) {
1062 elem_type = "edges";
1063 }
1064 else {
1065 elem_type = "faces";
1066 }
1067
1068 if (totfail) {
1069 BKE_reportf(&reports, RPT_WARNING, "%d %s mirrored, %d failed", totmirr, elem_type, totfail);
1070 }
1071 else {
1072 BKE_reportf(&reports, RPT_INFO, "%d %s mirrored", totmirr, elem_type);
1073 }
1074}
1075
1076void ED_mesh_report_mirror(ReportList &reports, int totmirr, int totfail)
1077{
1078 ED_mesh_report_mirror_ex(reports, totmirr, totfail, SCE_SELECT_VERTEX);
1079}
1080
1082{
1083 BLI_assert(me->runtime->edit_mesh && me->runtime->edit_mesh->bm);
1084
1085 return BKE_keyblock_find_by_index(me->key, me->runtime->edit_mesh->bm->shapenr - 1);
1086}
1087
1089{
1090 Mesh *mesh = static_cast<Mesh *>(CTX_data_pointer_get_type(C, "mesh", &RNA_Mesh).data);
1091 if (mesh != nullptr) {
1092 return mesh;
1093 }
1094
1096 if (ob == nullptr) {
1097 return nullptr;
1098 }
1099
1100 ID *data = (ID *)ob->data;
1101 if (data == nullptr || GS(data->name) != ID_ME) {
1102 return nullptr;
1103 }
1104
1105 return (Mesh *)data;
1106}
1107
1109{
1110 using namespace blender;
1111 const OffsetIndices polys = mesh->faces();
1112 const Span<int> corner_edges = mesh->corner_edges();
1113 const bke::AttributeAccessor attributes = mesh->attributes();
1114 const VArray<bool> mesh_sharp_edges = *attributes.lookup_or_default<bool>(
1115 "sharp_edge", bke::AttrDomain::Edge, false);
1116 const VArraySpan<bool> sharp_faces = *attributes.lookup<bool>("sharp_face",
1118
1119 Array<bool> sharp_edges(mesh->edges_num);
1120 mesh_sharp_edges.materialize(sharp_edges);
1121
1122 threading::parallel_for(polys.index_range(), 1024, [&](const IndexRange range) {
1123 for (const int face_i : range) {
1124 if (!sharp_faces.is_empty() && sharp_faces[face_i]) {
1125 for (const int edge : corner_edges.slice(polys[face_i])) {
1126 sharp_edges[edge] = true;
1127 }
1128 }
1129 }
1130 });
1131
1132 IndexMaskMemory memory;
1133 const IndexMask split_mask = IndexMask::from_bools(sharp_edges, memory);
1134 if (split_mask.is_empty()) {
1135 return;
1136 }
1137
1138 geometry::split_edges(*mesh, split_mask, {});
1139}
void BKE_id_attributes_default_color_set(struct ID *id, std::optional< blender::StringRef > name)
struct CustomDataLayer * BKE_attribute_new(AttributeOwner &owner, blender::StringRef name, eCustomDataType type, blender::bke::AttrDomain domain, struct ReportList *reports)
Definition attribute.cc:382
blender::StringRef BKE_uv_map_pin_name_get(blender::StringRef uv_map_name, char *buffer)
std::string BKE_attribute_calc_unique_name(const AttributeOwner &owner, blender::StringRef name)
Definition attribute.cc:370
bool BKE_attribute_remove(AttributeOwner &owner, blender::StringRef name, struct ReportList *reports)
Definition attribute.cc:512
const struct CustomDataLayer * BKE_id_attributes_color_find(const struct ID *id, blender::StringRef name)
bool BKE_color_attribute_supported(const struct Mesh &mesh, blender::StringRef name)
void BKE_id_attributes_active_color_set(struct ID *id, std::optional< blender::StringRef > name)
Definition attribute.cc:985
PointerRNA CTX_data_pointer_get_type(const bContext *C, const char *member, StructRNA *type)
Scene * CTX_data_scene(const bContext *C)
CustomData interface, see also DNA_customdata_types.h.
int CustomData_get_named_layer(const CustomData *data, eCustomDataType type, blender::StringRef name)
const void * CustomData_get_layer(const CustomData *data, eCustomDataType type)
@ CD_SET_DEFAULT
void * CustomData_add_layer_named(CustomData *data, eCustomDataType type, eCDAllocType alloctype, int totelem, blender::StringRef name)
bool CustomData_has_layer_named(const CustomData *data, eCustomDataType type, blender::StringRef name)
const char * CustomData_get_layer_name(const CustomData *data, eCustomDataType type, int n)
int CustomData_get_offset_named(const CustomData *data, eCustomDataType type, blender::StringRef name)
void CustomData_free(CustomData *data)
void CustomData_free_layers(CustomData *data, eCustomDataType type)
void CustomData_init_layout_from(const CustomData *source, CustomData *dest, eCustomDataMask mask, eCDAllocType alloctype, int totelem)
int CustomData_get_active_layer(const CustomData *data, eCustomDataType type)
void CustomData_free_elem(CustomData *data, int index, int count)
void CustomData_copy_data(const CustomData *source, CustomData *dest, int source_index, int dest_index, int count)
bool CustomData_has_layer(const CustomData *data, eCustomDataType type)
size_t CustomData_get_elem_size(const CustomDataLayer *layer)
void CustomData_ensure_layers_are_mutable(CustomData *data, int totelem)
bool CustomData_layertype_is_singleton(eCustomDataType type)
const char * CustomData_get_active_layer_name(const CustomData *data, eCustomDataType type)
const void * CustomData_add_layer_named_with_data(CustomData *data, eCustomDataType type, void *layer_data, int totelem, blender::StringRef name, const blender::ImplicitSharingInfo *sharing_info)
int CustomData_number_of_layers(const CustomData *data, eCustomDataType type)
void CustomData_set_layer_active(CustomData *data, eCustomDataType type, int n)
const CustomData_MeshMasks CD_MASK_MESH
KeyBlock * BKE_keyblock_find_by_index(Key *key, int index)
Definition key.cc:1907
void BKE_mesh_tessface_clear(Mesh *mesh)
void BKE_mesh_ensure_skin_customdata(Mesh *mesh)
bool BKE_mesh_has_custom_loop_normals(Mesh *mesh)
void BKE_lnor_spacearr_clear(MLoopNorSpaceArray *lnors_spacearr)
void BKE_mesh_runtime_clear_cache(Mesh *mesh)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
@ RPT_INFO
Definition BKE_report.hh:35
@ RPT_ERROR
Definition BKE_report.hh:39
@ RPT_WARNING
Definition BKE_report.hh:38
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:153
#define BLI_assert(a)
Definition BLI_assert.h:46
#define M_PI
#define DATA_(msgid)
void DEG_id_tag_update(ID *id, unsigned int flags)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
#define ID_IS_EDITABLE(_id)
Definition DNA_ID.h:705
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition DNA_ID.h:730
@ ID_ME
#define MAX_CUSTOMDATA_LAYER_NAME
@ CD_PROP_BYTE_COLOR
@ CD_MVERT_SKIN
@ CD_PROP_FLOAT
@ CD_PROP_FLOAT3
@ CD_PROP_INT32_2D
@ CD_PROP_INT32
@ CD_PROP_FLOAT2
@ CD_GRID_PAINT_MASK
@ CD_PROP_INT16_2D
#define MAX_MTFACE
@ OB_MODE_SCULPT
@ OB_MODE_TEXTURE_PAINT
Object is a sort of wrapper for general info.
@ OB_MESH
@ SCE_SELECT_VERTEX
@ SCE_SELECT_EDGE
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
bool ED_paint_proj_mesh_data_check(Scene &scene, Object &ob, bool *r_has_uvs, bool *r_has_mat, bool *r_has_tex, bool *r_has_stencil)
bool ED_operator_editable_mesh(bContext *C)
Read Guarded memory(de)allocation.
#define C
Definition RandGen.cpp:29
#define NC_GEOM
Definition WM_types.hh:393
#define ND_DATA
Definition WM_types.hh:509
#define NC_SCENE
Definition WM_types.hh:378
@ OPTYPE_UNDO
Definition WM_types.hh:182
@ OPTYPE_REGISTER
Definition WM_types.hh:180
#define ND_TOOLSETTINGS
Definition WM_types.hh:449
@ BM_ELEM_SELECT
@ BM_LOOP
#define BM_ELEM_CD_GET_VOID_P(ele, offset)
#define BM_elem_flag_test(ele, hflag)
bool BM_data_layer_free_named(BMesh *bm, CustomData *data, StringRef name)
void BM_data_layer_free(BMesh *bm, CustomData *data, int type)
void BM_data_layer_copy(BMesh *bm, CustomData *data, int type, int src_n, int dst_n)
void BM_data_layer_add_named(BMesh *bm, CustomData *data, int type, const StringRef name)
void BM_uv_map_attr_pin_ensure_for_all_layers(BMesh *bm)
void BM_data_layer_ensure_named(BMesh *bm, CustomData *data, int type, const StringRef name)
#define BM_ITER_MESH(ele, iter, bm, itype)
@ BM_FACES_OF_MESH
@ BM_LOOPS_OF_FACE
#define BM_ITER_ELEM_INDEX(ele, iter, data, itype, indexvar)
BMesh const char void * data
BMesh * bm
#define BM_FACE
#define BM_EDGE
#define BM_VERT
ATTR_WARN_UNUSED_RESULT const BMLoop * l
static AttributeOwner from_id(ID *id)
Definition attribute.cc:44
AttributeSet attributes
static IndexMask from_bools(Span< bool > bools, IndexMaskMemory &memory)
bool is_empty() const
const T * data() const
Definition BLI_array.hh:312
constexpr int64_t size() const
void materialize(MutableSpan< T > r_span) const
GAttributeReader lookup_or_default(StringRef attribute_id, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
GAttributeReader lookup(const StringRef attribute_id) const
GSpanAttributeWriter lookup_or_add_for_write_span(StringRef attribute_id, AttrDomain domain, AttrType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
GAttributeWriter lookup_or_add_for_write(StringRef attribute_id, AttrDomain domain, AttrType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
GSpanAttributeWriter lookup_for_write_span(StringRef attribute_id)
nullptr float
#define GS(x)
#define active
VecBase< short, 2 > short2
int count
void * MEM_dupallocN(const void *vmemh)
Definition mallocn.cc:143
static char faces[256]
void MESH_OT_customdata_mask_clear(wmOperatorType *ot)
Definition mesh_data.cc:576
void ED_mesh_uv_ensure(Mesh *mesh, const char *name)
Definition mesh_data.cc:322
static blender::VArray< bool > get_corner_boolean_attribute(const Mesh &mesh, const StringRef name)
Definition mesh_data.cc:292
void ED_mesh_edges_remove(Mesh *mesh, ReportList *reports, int count)
static bool layers_poll(bContext *C)
Definition mesh_data.cc:410
void ED_mesh_report_mirror(ReportList &reports, int totmirr, int totfail)
static void mesh_remove_loops(Mesh *mesh, int len)
Definition mesh_data.cc:966
bool ED_mesh_color_ensure(Mesh *mesh, const char *name)
Definition mesh_data.cc:382
void MESH_OT_customdata_skin_add(wmOperatorType *ot)
Definition mesh_data.cc:629
static wmOperatorStatus mesh_customdata_mask_clear_exec(bContext *C, wmOperator *op)
Definition mesh_data.cc:562
static wmOperatorStatus mesh_uv_texture_add_exec(bContext *C, wmOperator *op)
Definition mesh_data.cc:437
static wmOperatorStatus mesh_customdata_skin_clear_exec(bContext *C, wmOperator *)
Definition mesh_data.cc:649
void MESH_OT_uv_texture_remove(wmOperatorType *ot)
Definition mesh_data.cc:494
void ED_mesh_edges_add(Mesh *mesh, ReportList *reports, int count)
Definition mesh_data.cc:911
static bool uv_texture_remove_poll(bContext *C)
Definition mesh_data.cc:420
static blender::bke::AttributeWriter< bool > ensure_corner_boolean_attribute(Mesh &mesh, const StringRef name)
Definition mesh_data.cc:306
void ED_mesh_loops_add(Mesh *mesh, ReportList *reports, int count)
Definition mesh_data.cc:920
static void reset_uvs_mesh(const blender::IndexRange face, MutableSpan< float2 > uv_map)
Definition mesh_data.cc:166
blender::bke::AttributeWriter< bool > ED_mesh_uv_map_pin_layer_ensure(Mesh *mesh, const int uv_index)
Definition mesh_data.cc:314
void ED_mesh_verts_add(Mesh *mesh, ReportList *reports, int count)
Definition mesh_data.cc:902
blender::VArray< bool > ED_mesh_uv_map_pin_layer_get(const Mesh *mesh, const int uv_index)
Definition mesh_data.cc:298
void MESH_OT_customdata_skin_clear(wmOperatorType *ot)
Definition mesh_data.cc:654
static void mesh_remove_faces(Mesh *mesh, int len)
Definition mesh_data.cc:977
void ED_mesh_geometry_clear(Mesh *mesh)
static void mesh_add_edges(Mesh *mesh, int len)
Definition mesh_data.cc:786
void ED_mesh_faces_remove(Mesh *mesh, ReportList *reports, int count)
int ED_mesh_uv_add(Mesh *mesh, const char *name, const bool active_set, const bool do_init, ReportList *reports)
Definition mesh_data.cc:218
void ED_mesh_faces_add(Mesh *mesh, ReportList *reports, int count)
Definition mesh_data.cc:929
static bool mesh_customdata_skin_add_poll(bContext *C)
Definition mesh_data.cc:611
static wmOperatorStatus mesh_customdata_skin_add_exec(bContext *C, wmOperator *)
Definition mesh_data.cc:616
static CustomData * mesh_customdata_get_type(Mesh *mesh, const char htype, int *r_tot)
Definition mesh_data.cc:52
void ED_mesh_loops_remove(Mesh *mesh, ReportList *reports, int count)
void ED_mesh_report_mirror_ex(ReportList &reports, int totmirr, int totfail, char selectmode)
static wmOperatorStatus mesh_uv_texture_remove_exec(bContext *C, wmOperator *op)
Definition mesh_data.cc:470
static void mesh_add_faces(Mesh *mesh, int len)
Definition mesh_data.cc:859
void ED_mesh_uv_loop_reset(bContext *C, Mesh *mesh)
Definition mesh_data.cc:208
static int mesh_customdata_skin_state(bContext *C)
Definition mesh_data.cc:597
static void reset_uv_map(Mesh *mesh, const StringRef name)
Definition mesh_data.cc:177
static wmOperatorStatus mesh_customdata_clear_exec__internal(bContext *C, char htype, const eCustomDataType type)
Definition mesh_data.cc:511
int ED_mesh_color_add(Mesh *mesh, const char *name, const bool active_set, const bool do_init, ReportList *reports)
Definition mesh_data.cc:340
void ED_mesh_split_faces(Mesh *mesh)
static void mesh_remove_verts(Mesh *mesh, int len)
Definition mesh_data.cc:944
static void mesh_add_verts(Mesh *mesh, int len)
Definition mesh_data.cc:755
void MESH_OT_uv_texture_add(wmOperatorType *ot)
Definition mesh_data.cc:455
static void mesh_add_loops(Mesh *mesh, int len)
Definition mesh_data.cc:822
static bool mesh_customdata_mask_clear_poll(bContext *C)
Definition mesh_data.cc:538
static wmOperatorStatus mesh_customdata_custom_splitnormals_clear_exec(bContext *C, wmOperator *)
Definition mesh_data.cc:712
static void reset_uvs_bmesh(BMFace *f, const int cd_loop_uv_offset)
Definition mesh_data.cc:152
static void mesh_remove_edges(Mesh *mesh, int len)
Definition mesh_data.cc:955
KeyBlock * ED_mesh_get_edit_shape_key(const Mesh *me)
void MESH_OT_customdata_custom_splitnormals_add(wmOperatorType *ot)
Definition mesh_data.cc:697
static bool mesh_customdata_skin_clear_poll(bContext *C)
Definition mesh_data.cc:644
void ED_mesh_verts_remove(Mesh *mesh, ReportList *reports, int count)
Definition mesh_data.cc:988
static void mesh_uv_reset_array(float **fuv, const int len)
Definition mesh_data.cc:112
static wmOperatorStatus mesh_customdata_custom_splitnormals_add_exec(bContext *C, wmOperator *)
Definition mesh_data.cc:670
void MESH_OT_customdata_custom_splitnormals_clear(wmOperatorType *ot)
Definition mesh_data.cc:740
Mesh * ED_mesh_context(bContext *C)
Object * context_object(const bContext *C)
Object * context_active_object(const bContext *C)
void split_edges(Mesh &mesh, const IndexMask &selected_edges, const bke::AttributeFilter &attribute_filter={})
void resize_trivial_array(T **data, const ImplicitSharingInfo **sharing_info, int64_t old_size, int64_t new_size)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:93
VecBase< float, 2 > float2
VecBase< float, 3 > float3
static void unique_name(bNode *node)
const char * name
#define sinf
#define cosf
Definition DNA_ID.h:414
int corners_num
CustomData edge_data
int edges_num
MeshRuntimeHandle * runtime
CustomData corner_data
CustomData face_data
CustomData vert_data
struct Key * key
int faces_num
int verts_num
void * data
Definition RNA_types.hh:53
struct ReportList * reports
i
Definition text_draw.cc:230
uint len
void WM_main_add_notifier(uint type, void *reference)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
wmOperatorType * ot
Definition wm_files.cc:4237