Blender V4.5
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
16#include "BKE_attribute.hh"
17#include "BKE_context.hh"
18#include "BKE_customdata.hh"
19#include "BKE_editmesh.hh"
20#include "BKE_key.hh"
21#include "BKE_library.hh"
22#include "BKE_mesh.hh"
23#include "BKE_mesh_runtime.hh"
24#include "BKE_report.hh"
25
26#include "DEG_depsgraph.hh"
27
28#include "RNA_prototypes.hh"
29
30#include "WM_api.hh"
31#include "WM_types.hh"
32
33#include "BLT_translation.hh"
34
35#include "ED_mesh.hh"
36#include "ED_object.hh"
37#include "ED_paint.hh"
38#include "ED_screen.hh"
39
41
42#include "mesh_intern.hh" /* own include */
43
44using blender::Array;
45using blender::float2;
46using blender::float3;
48using blender::Span;
50
51static CustomData *mesh_customdata_get_type(Mesh *mesh, const char htype, int *r_tot)
52{
54 BMesh *bm = (mesh->runtime->edit_mesh) ? mesh->runtime->edit_mesh->bm : nullptr;
55 int tot;
56
57 switch (htype) {
58 case BM_VERT:
59 if (bm) {
60 data = &bm->vdata;
61 tot = bm->totvert;
62 }
63 else {
64 data = &mesh->vert_data;
65 tot = mesh->verts_num;
66 }
67 break;
68 case BM_EDGE:
69 if (bm) {
70 data = &bm->edata;
71 tot = bm->totedge;
72 }
73 else {
74 data = &mesh->edge_data;
75 tot = mesh->edges_num;
76 }
77 break;
78 case BM_LOOP:
79 if (bm) {
80 data = &bm->ldata;
81 tot = bm->totloop;
82 }
83 else {
84 data = &mesh->corner_data;
85 tot = mesh->corners_num;
86 }
87 break;
88 case BM_FACE:
89 if (bm) {
90 data = &bm->pdata;
91 tot = bm->totface;
92 }
93 else {
94 data = &mesh->face_data;
95 tot = mesh->faces_num;
96 }
97 break;
98 default:
99 BLI_assert(0);
100 tot = 0;
101 data = nullptr;
102 break;
103 }
104
105 if (r_tot) {
106 *r_tot = tot;
107 }
108 return data;
109}
110
111static void mesh_uv_reset_array(float **fuv, const int len)
112{
113 if (len == 3) {
114 fuv[0][0] = 0.0;
115 fuv[0][1] = 0.0;
116
117 fuv[1][0] = 1.0;
118 fuv[1][1] = 0.0;
119
120 fuv[2][0] = 1.0;
121 fuv[2][1] = 1.0;
122 }
123 else if (len == 4) {
124 fuv[0][0] = 0.0;
125 fuv[0][1] = 0.0;
126
127 fuv[1][0] = 1.0;
128 fuv[1][1] = 0.0;
129
130 fuv[2][0] = 1.0;
131 fuv[2][1] = 1.0;
132
133 fuv[3][0] = 0.0;
134 fuv[3][1] = 1.0;
135 /* Make sure we ignore 2-sided faces. */
136 }
137 else if (len > 2) {
138 float fac = 0.0f, dfac = 1.0f / float(len);
139
140 dfac *= float(M_PI) * 2.0f;
141
142 for (int i = 0; i < len; i++) {
143 fuv[i][0] = 0.5f * sinf(fac) + 0.5f;
144 fuv[i][1] = 0.5f * cosf(fac) + 0.5f;
145
146 fac += dfac;
147 }
148 }
149}
150
151static void mesh_uv_reset_bmface(BMFace *f, const int cd_loop_uv_offset)
152{
154 BMIter liter;
155 BMLoop *l;
156 int i;
157
158 BM_ITER_ELEM_INDEX (l, &liter, f, BM_LOOPS_OF_FACE, i) {
159 fuv[i] = ((float *)BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset));
160 }
161
162 mesh_uv_reset_array(fuv.data(), f->len);
163}
164
165static void mesh_uv_reset_mface(const blender::IndexRange face, float2 *mloopuv)
166{
168
169 for (int i = 0; i < face.size(); i++) {
170 fuv[i] = mloopuv[face[i]];
171 }
172
173 mesh_uv_reset_array(fuv.data(), face.size());
174}
175
176void ED_mesh_uv_loop_reset_ex(Mesh *mesh, const int layernum)
177{
178 if (BMEditMesh *em = mesh->runtime->edit_mesh.get()) {
179 /* Collect BMesh UVs */
180 const int cd_loop_uv_offset = CustomData_get_n_offset(
181 &em->bm->ldata, CD_PROP_FLOAT2, layernum);
182
183 BMFace *efa;
184 BMIter iter;
185
186 BLI_assert(cd_loop_uv_offset >= 0);
187
188 BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
190 continue;
191 }
192
193 mesh_uv_reset_bmface(efa, cd_loop_uv_offset);
194 }
195 }
196 else {
197 /* Collect Mesh UVs */
199 float2 *mloopuv = static_cast<float2 *>(CustomData_get_layer_n_for_write(
200 &mesh->corner_data, CD_PROP_FLOAT2, layernum, mesh->corners_num));
201
202 const blender::OffsetIndices polys = mesh->faces();
203 for (const int i : polys.index_range()) {
204 mesh_uv_reset_mface(polys[i], mloopuv);
205 }
206 }
207
208 DEG_id_tag_update(&mesh->id, 0);
209}
210
212{
213 /* could be ldata or pdata */
214 CustomData *ldata = mesh_customdata_get_type(mesh, BM_LOOP, nullptr);
215 const int layernum = CustomData_get_active_layer(ldata, CD_PROP_FLOAT2);
216 ED_mesh_uv_loop_reset_ex(mesh, layernum);
217
219}
220
222 Mesh *mesh, const char *name, const bool active_set, const bool do_init, ReportList *reports)
223{
224 /* NOTE: keep in sync with #ED_mesh_color_add. */
225
226 int layernum_dst;
227
228 if (!name) {
229 name = DATA_("UVMap");
230 }
231
233 const std::string unique_name = BKE_attribute_calc_unique_name(owner, name);
234 bool is_init = false;
235
236 if (BMEditMesh *em = mesh->runtime->edit_mesh.get()) {
237 layernum_dst = CustomData_number_of_layers(&em->bm->ldata, CD_PROP_FLOAT2);
238 if (layernum_dst >= MAX_MTFACE) {
239 BKE_reportf(reports, RPT_WARNING, "Cannot add more than %i UV maps", MAX_MTFACE);
240 return -1;
241 }
242
243 BM_data_layer_add_named(em->bm, &em->bm->ldata, CD_PROP_FLOAT2, unique_name.c_str());
245 /* copy data from active UV */
246 if (layernum_dst && do_init) {
247 const int layernum_src = CustomData_get_active_layer(&em->bm->ldata, CD_PROP_FLOAT2);
248 BM_data_layer_copy(em->bm, &em->bm->ldata, CD_PROP_FLOAT2, layernum_src, layernum_dst);
249
250 is_init = true;
251 }
252 if (active_set || layernum_dst == 0) {
253 CustomData_set_layer_active(&em->bm->ldata, CD_PROP_FLOAT2, layernum_dst);
254 }
255 }
256 else {
258 if (layernum_dst >= MAX_MTFACE) {
259 BKE_reportf(reports, RPT_WARNING, "Cannot add more than %i UV maps", MAX_MTFACE);
260 return -1;
261 }
262
263 if (CustomData_has_layer(&mesh->corner_data, CD_PROP_FLOAT2) && do_init) {
265 &mesh->corner_data,
268 mesh->corners_num,
270 nullptr);
271
272 is_init = true;
273 }
274 else {
277 }
278
279 if (active_set || layernum_dst == 0) {
281 }
282 }
283
284 /* don't overwrite our copied coords */
285 if (!is_init && do_init) {
286 ED_mesh_uv_loop_reset_ex(mesh, layernum_dst);
287 }
288
289 DEG_id_tag_update(&mesh->id, 0);
291
292 return layernum_dst;
293}
294
295static const bool *mesh_loop_boolean_custom_data_get_by_name(const Mesh &mesh,
296 const StringRef name)
297{
298 return static_cast<const bool *>(
300}
301
302const bool *ED_mesh_uv_map_vert_select_layer_get(const Mesh *mesh, const int uv_index)
303{
304 using namespace blender::bke;
305 char buffer[MAX_CUSTOMDATA_LAYER_NAME];
306 const char *uv_name = CustomData_get_layer_name(&mesh->corner_data, CD_PROP_FLOAT2, uv_index);
308 *mesh, BKE_uv_map_vert_select_name_get(uv_name, buffer));
309}
310const bool *ED_mesh_uv_map_edge_select_layer_get(const Mesh *mesh, const int uv_index)
311{
312 /* UV map edge selections are stored on face corners (loops) and not on edges
313 * because we need selections per face edge, even when the edge is split in UV space. */
314
315 using namespace blender::bke;
316 char buffer[MAX_CUSTOMDATA_LAYER_NAME];
317 const char *uv_name = CustomData_get_layer_name(&mesh->corner_data, CD_PROP_FLOAT2, uv_index);
319 *mesh, BKE_uv_map_edge_select_name_get(uv_name, buffer));
320}
321
322const bool *ED_mesh_uv_map_pin_layer_get(const Mesh *mesh, const int uv_index)
323{
324 using namespace blender::bke;
325 char buffer[MAX_CUSTOMDATA_LAYER_NAME];
326 const char *uv_name = CustomData_get_layer_name(&mesh->corner_data, CD_PROP_FLOAT2, uv_index);
328 BKE_uv_map_pin_name_get(uv_name, buffer));
329}
330
332{
333 bool *data = static_cast<bool *>(CustomData_get_layer_named_for_write(
334 &mesh.corner_data, CD_PROP_BOOL, name, mesh.corners_num));
335 if (!data) {
336 data = static_cast<bool *>(CustomData_add_layer_named(
337 &mesh.corner_data, CD_PROP_BOOL, CD_SET_DEFAULT, mesh.faces_num, name));
338 }
339 return data;
340}
341
343{
344 using namespace blender::bke;
345 char buffer[MAX_CUSTOMDATA_LAYER_NAME];
346 const char *uv_name = CustomData_get_layer_name(&mesh->corner_data, CD_PROP_FLOAT2, uv_index);
348}
350{
351 using namespace blender::bke;
352 char buffer[MAX_CUSTOMDATA_LAYER_NAME];
353 const char *uv_name = CustomData_get_layer_name(&mesh->corner_data, CD_PROP_FLOAT2, uv_index);
355}
356bool *ED_mesh_uv_map_pin_layer_ensure(Mesh *mesh, const int uv_index)
357{
358 using namespace blender::bke;
359 char buffer[MAX_CUSTOMDATA_LAYER_NAME];
360 const char *uv_name = CustomData_get_layer_name(&mesh->corner_data, CD_PROP_FLOAT2, uv_index);
362}
363
364void ED_mesh_uv_ensure(Mesh *mesh, const char *name)
365{
366 int layernum_dst;
367
368 if (BMEditMesh *em = mesh->runtime->edit_mesh.get()) {
369 layernum_dst = CustomData_number_of_layers(&em->bm->ldata, CD_PROP_FLOAT2);
370 if (layernum_dst == 0) {
371 ED_mesh_uv_add(mesh, name, true, true, nullptr);
372 }
373 }
374 else {
375 layernum_dst = CustomData_number_of_layers(&mesh->corner_data, CD_PROP_FLOAT2);
376 if (layernum_dst == 0) {
377 ED_mesh_uv_add(mesh, name, true, true, nullptr);
378 }
379 }
380}
381
383 Mesh *mesh, const char *name, const bool active_set, const bool do_init, ReportList *reports)
384{
385 using namespace blender;
386 /* If no name is supplied, provide a backwards compatible default. */
387 if (!name) {
388 name = "Col";
389 }
390
394
395 if (do_init) {
396 const char *active_name = mesh->active_color_attribute;
397 if (const CustomDataLayer *active_layer = BKE_id_attributes_color_find(&mesh->id, active_name))
398 {
399 if (const BMEditMesh *em = mesh->runtime->edit_mesh.get()) {
400 BMesh &bm = *em->bm;
401 const int src_i = CustomData_get_named_layer(&bm.ldata, CD_PROP_BYTE_COLOR, active_name);
402 const int dst_i = CustomData_get_named_layer(&bm.ldata, CD_PROP_BYTE_COLOR, layer->name);
403 BM_data_layer_copy(&bm, &bm.ldata, CD_PROP_BYTE_COLOR, src_i, dst_i);
404 }
405 else {
406 memcpy(
407 layer->data, active_layer->data, CustomData_get_elem_size(layer) * mesh->corners_num);
408 }
409 }
410 }
411
412 if (active_set) {
414 }
415
416 DEG_id_tag_update(&mesh->id, 0);
418
419 int dummy;
422}
423
424bool ED_mesh_color_ensure(Mesh *mesh, const char *name)
425{
426 using namespace blender;
427 BLI_assert(mesh->runtime->edit_mesh == nullptr);
428 if (BKE_color_attribute_supported(*mesh, mesh->active_color_attribute)) {
429 return true;
430 }
431
433 const std::string unique_name = BKE_attribute_calc_unique_name(owner, name);
434 if (!mesh->attributes_for_write().add(unique_name,
438 {
439 return false;
440 }
441
445 DEG_id_tag_update(&mesh->id, 0);
446
447 return true;
448}
449
450/*********************** General poll ************************/
451
452static bool layers_poll(bContext *C)
453{
455 ID *data = (ob) ? static_cast<ID *>(ob->data) : nullptr;
456 return (ob && ID_IS_EDITABLE(ob) && !ID_IS_OVERRIDE_LIBRARY(ob) && ob->type == OB_MESH && data &&
458}
459
460/*********************** UV texture operators ************************/
461
463{
464 if (!layers_poll(C)) {
465 return false;
466 }
467
469 Mesh *mesh = static_cast<Mesh *>(ob->data);
472 if (active != -1) {
473 return true;
474 }
475
476 return false;
477}
478
480{
482 Mesh *mesh = static_cast<Mesh *>(ob->data);
483
484 if (ED_mesh_uv_add(mesh, nullptr, true, true, op->reports) == -1) {
485 return OPERATOR_CANCELLED;
486 }
487
488 if (ob->mode & OB_MODE_TEXTURE_PAINT) {
489 Scene *scene = CTX_data_scene(C);
490 ED_paint_proj_mesh_data_check(*scene, *ob, nullptr, nullptr, nullptr, nullptr);
492 }
493
494 return OPERATOR_FINISHED;
495}
496
498{
499 /* identifiers */
500 ot->name = "Add UV Map";
501 ot->description = "Add UV map";
502 ot->idname = "MESH_OT_uv_texture_add";
503
504 /* API callbacks. */
505 ot->poll = layers_poll;
507
508 /* flags */
510}
511
513{
515 Mesh *mesh = static_cast<Mesh *>(ob->data);
516
519 const char *name = CustomData_get_active_layer_name(ldata, CD_PROP_FLOAT2);
520 if (!BKE_attribute_remove(owner, name, op->reports)) {
521 return OPERATOR_CANCELLED;
522 }
523
524 if (ob->mode & OB_MODE_TEXTURE_PAINT) {
525 Scene *scene = CTX_data_scene(C);
526 ED_paint_proj_mesh_data_check(*scene, *ob, nullptr, nullptr, nullptr, nullptr);
528 }
529
532
533 return OPERATOR_FINISHED;
534}
535
537{
538 /* identifiers */
539 ot->name = "Remove UV Map";
540 ot->description = "Remove UV map";
541 ot->idname = "MESH_OT_uv_texture_remove";
542
543 /* API callbacks. */
546
547 /* flags */
549}
550
551/* *** CustomData clear functions, we need an operator for each *** */
552
554 char htype,
555 const eCustomDataType type)
556{
558
559 CustomData *data = mesh_customdata_get_type(mesh, htype, nullptr);
560
562
563 if (CustomData_has_layer(data, type)) {
564 if (mesh->runtime->edit_mesh) {
565 BM_data_layer_free(mesh->runtime->edit_mesh->bm, data, type);
566 }
567 else {
569 }
570
573
574 return OPERATOR_FINISHED;
575 }
576 return OPERATOR_CANCELLED;
577}
578
579/* Clear Mask */
581{
583 if (ob && ob->type == OB_MESH) {
584 Mesh *mesh = static_cast<Mesh *>(ob->data);
585
586 /* special case - can't run this if we're in sculpt mode */
587 if (ob->mode & OB_MODE_SCULPT) {
588 return false;
589 }
590
593 if (CustomData_has_layer_named(data, CD_PROP_FLOAT, ".sculpt_mask")) {
594 return true;
595 }
598 return true;
599 }
600 }
601 }
602 return false;
603}
605{
607 Mesh *mesh = static_cast<Mesh *>(object->data);
609 const bool ret_a = BKE_attribute_remove(owner, ".sculpt_mask", op->reports);
611
612 if (ret_a || ret_b == OPERATOR_FINISHED) {
613 return OPERATOR_FINISHED;
614 }
615 return OPERATOR_CANCELLED;
616}
617
619{
620 /* NOTE: no create_mask yet */
621
622 /* identifiers */
623 ot->name = "Clear Sculpt Mask Data";
624 ot->idname = "MESH_OT_customdata_mask_clear";
625 ot->description = "Clear vertex sculpt masking data from the mesh";
626
627 /* API callbacks. */
630
631 /* flags */
633}
634
640{
642
643 if (ob && ob->type == OB_MESH) {
644 Mesh *mesh = static_cast<Mesh *>(ob->data);
648 }
649 }
650 return -1;
651}
652
654{
655 return (mesh_customdata_skin_state(C) == 0);
656}
657
670
672{
673 /* identifiers */
674 ot->name = "Add Skin Data";
675 ot->idname = "MESH_OT_customdata_skin_add";
676 ot->description = "Add a vertex skin layer";
677
678 /* API callbacks. */
681
682 /* flags */
684}
685
687{
688 return (mesh_customdata_skin_state(C) == 1);
689}
690
695
697{
698 /* identifiers */
699 ot->name = "Clear Skin Data";
700 ot->idname = "MESH_OT_customdata_skin_clear";
701 ot->description = "Clear vertex skin layer";
702
703 /* API callbacks. */
706
707 /* flags */
709}
710
711/* Clear custom loop normals */
713 wmOperator * /*op*/)
714{
715 using namespace blender;
718 return OPERATOR_CANCELLED;
719 }
720
721 if (mesh->runtime->edit_mesh) {
722 BMesh &bm = *mesh->runtime->edit_mesh->bm;
723 BM_data_layer_ensure_named(&bm, &bm.ldata, CD_PROP_INT16_2D, "custom_normal");
724 }
725 else {
726 if (!mesh->attributes_for_write().add<short2>(
728 {
729 return OPERATOR_CANCELLED;
730 }
731 }
732
733 DEG_id_tag_update(&mesh->id, 0);
735
736 return OPERATOR_FINISHED;
737}
738
740{
741 /* identifiers */
742 ot->name = "Add Custom Split Normals Data";
743 ot->idname = "MESH_OT_customdata_custom_splitnormals_add";
744 ot->description = "Add a custom split normals layer, if none exists yet";
745
746 /* API callbacks. */
749
750 /* flags */
752}
753
755 wmOperator * /*op*/)
756{
758
759 if (BMEditMesh *em = mesh->runtime->edit_mesh.get()) {
760 BMesh &bm = *em->bm;
761 if (!CustomData_has_layer_named(&bm.ldata, CD_PROP_INT16_2D, "custom_normal")) {
762 return OPERATOR_CANCELLED;
763 }
764 BM_data_layer_free_named(&bm, &bm.ldata, "custom_normal");
765 if (bm.lnor_spacearr) {
766 BKE_lnor_spacearr_clear(bm.lnor_spacearr);
767 }
768 }
769 else {
770 if (!mesh->attributes_for_write().remove("custom_normal")) {
771 return OPERATOR_CANCELLED;
772 }
773 }
774
775 mesh->tag_custom_normals_changed();
778
779 return OPERATOR_FINISHED;
780}
781
783{
784 /* identifiers */
785 ot->name = "Clear Custom Split Normals Data";
786 ot->idname = "MESH_OT_customdata_custom_splitnormals_clear";
787 ot->description = "Remove the custom split normals layer, if it exists";
788
789 /* API callbacks. */
792
793 /* flags */
795}
796
797static void mesh_add_verts(Mesh *mesh, int len)
798{
799 using namespace blender;
800 if (len == 0) {
801 return;
802 }
803
804 int totvert = mesh->verts_num + len;
805 CustomData vert_data;
807 &mesh->vert_data, &vert_data, CD_MASK_MESH.vmask, CD_SET_DEFAULT, totvert);
808 CustomData_copy_data(&mesh->vert_data, &vert_data, 0, 0, mesh->verts_num);
809
810 if (!CustomData_has_layer_named(&vert_data, CD_PROP_FLOAT3, "position")) {
811 CustomData_add_layer_named(&vert_data, CD_PROP_FLOAT3, CD_SET_DEFAULT, totvert, "position");
812 }
813
814 CustomData_free(&mesh->vert_data);
815 mesh->vert_data = vert_data;
816
818
819 mesh->verts_num = totvert;
820
821 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
823 ".select_vert", bke::AttrDomain::Point);
824 select_vert.span.take_back(len).fill(true);
825 select_vert.finish();
826}
827
828static void mesh_add_edges(Mesh *mesh, int len)
829{
830 using namespace blender;
831 CustomData edge_data;
832 int totedge;
833
834 if (len == 0) {
835 return;
836 }
837
838 totedge = mesh->edges_num + len;
839
840 /* Update custom-data. */
842 &mesh->edge_data, &edge_data, CD_MASK_MESH.emask, CD_SET_DEFAULT, totedge);
843 CustomData_copy_data(&mesh->edge_data, &edge_data, 0, 0, mesh->edges_num);
844
845 if (!CustomData_has_layer_named(&edge_data, CD_PROP_INT32_2D, ".edge_verts")) {
847 &edge_data, CD_PROP_INT32_2D, CD_SET_DEFAULT, totedge, ".edge_verts");
848 }
849
850 CustomData_free(&mesh->edge_data);
851 mesh->edge_data = edge_data;
852
854
855 mesh->edges_num = totedge;
856
857 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
859 ".select_edge", bke::AttrDomain::Edge);
860 select_edge.span.take_back(len).fill(true);
861 select_edge.finish();
862}
863
864static void mesh_add_loops(Mesh *mesh, int len)
865{
866 CustomData ldata;
867 int totloop;
868
869 if (len == 0) {
870 return;
871 }
872
873 totloop = mesh->corners_num + len; /* new face count */
874
875 /* update customdata */
877 &mesh->corner_data, &ldata, CD_MASK_MESH.lmask, CD_SET_DEFAULT, totloop);
878 CustomData_copy_data(&mesh->corner_data, &ldata, 0, 0, mesh->corners_num);
879
880 if (!CustomData_has_layer_named(&ldata, CD_PROP_INT32, ".corner_vert")) {
881 CustomData_add_layer_named(&ldata, CD_PROP_INT32, CD_SET_DEFAULT, totloop, ".corner_vert");
882 }
883 if (!CustomData_has_layer_named(&ldata, CD_PROP_INT32, ".corner_edge")) {
884 CustomData_add_layer_named(&ldata, CD_PROP_INT32, CD_SET_DEFAULT, totloop, ".corner_edge");
885 }
886
888
889 CustomData_free(&mesh->corner_data);
890 mesh->corner_data = ldata;
891
892 mesh->corners_num = totloop;
893
894 /* Keep the last face offset up to date with the corner total (they must be the same). We have
895 * to be careful here though, since the mesh may not be in a valid state at this point. */
896 if (mesh->face_offset_indices) {
897 mesh->face_offsets_for_write().last() = mesh->corners_num;
898 }
899}
900
901static void mesh_add_faces(Mesh *mesh, int len)
902{
903 using namespace blender;
904 CustomData face_data;
905 int faces_num;
906
907 if (len == 0) {
908 return;
909 }
910
911 faces_num = mesh->faces_num + len; /* new face count */
912
913 /* update customdata */
915 &mesh->face_data, &face_data, CD_MASK_MESH.pmask, CD_SET_DEFAULT, faces_num);
916 CustomData_copy_data(&mesh->face_data, &face_data, 0, 0, mesh->faces_num);
917
918 implicit_sharing::resize_trivial_array(&mesh->face_offset_indices,
919 &mesh->runtime->face_offsets_sharing_info,
920 mesh->faces_num == 0 ? 0 : (mesh->faces_num + 1),
921 faces_num + 1);
922 /* Set common values for convenience. */
923 mesh->face_offset_indices[0] = 0;
924 mesh->face_offset_indices[faces_num] = mesh->corners_num;
925
926 CustomData_free(&mesh->face_data);
927 mesh->face_data = face_data;
928
930
931 mesh->faces_num = faces_num;
932
933 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
935 ".select_poly", bke::AttrDomain::Face);
936 select_poly.span.take_back(len).fill(true);
937 select_poly.finish();
938}
939
940/* -------------------------------------------------------------------- */
943
945{
946 if (mesh->runtime->edit_mesh) {
947 BKE_report(reports, RPT_ERROR, "Cannot add vertices in edit mode");
948 return;
949 }
951}
952
954{
955 if (mesh->runtime->edit_mesh) {
956 BKE_report(reports, RPT_ERROR, "Cannot add edges in edit mode");
957 return;
958 }
960}
961
963{
964 if (mesh->runtime->edit_mesh) {
965 BKE_report(reports, RPT_ERROR, "Cannot add loops in edit mode");
966 return;
967 }
969}
970
972{
973 if (mesh->runtime->edit_mesh) {
974 BKE_report(reports, RPT_ERROR, "Cannot add faces in edit mode");
975 return;
976 }
978}
979
981
982/* -------------------------------------------------------------------- */
985
986static void mesh_remove_verts(Mesh *mesh, int len)
987{
988 if (len == 0) {
989 return;
990 }
991 CustomData_ensure_layers_are_mutable(&mesh->vert_data, mesh->verts_num);
992 const int totvert = mesh->verts_num - len;
993 CustomData_free_elem(&mesh->vert_data, totvert, len);
994 mesh->verts_num = totvert;
995}
996
997static void mesh_remove_edges(Mesh *mesh, int len)
998{
999 if (len == 0) {
1000 return;
1001 }
1002 CustomData_ensure_layers_are_mutable(&mesh->edge_data, mesh->edges_num);
1003 const int totedge = mesh->edges_num - len;
1004 CustomData_free_elem(&mesh->edge_data, totedge, len);
1005 mesh->edges_num = totedge;
1006}
1007
1008static void mesh_remove_loops(Mesh *mesh, int len)
1009{
1010 if (len == 0) {
1011 return;
1012 }
1013 CustomData_ensure_layers_are_mutable(&mesh->corner_data, mesh->corners_num);
1014 const int totloop = mesh->corners_num - len;
1015 CustomData_free_elem(&mesh->corner_data, totloop, len);
1016 mesh->corners_num = totloop;
1017}
1018
1019static void mesh_remove_faces(Mesh *mesh, int len)
1020{
1021 if (len == 0) {
1022 return;
1023 }
1024 CustomData_ensure_layers_are_mutable(&mesh->face_data, mesh->faces_num);
1025 const int faces_num = mesh->faces_num - len;
1026 CustomData_free_elem(&mesh->face_data, faces_num, len);
1027 mesh->faces_num = faces_num;
1028}
1029
1031{
1032 if (mesh->runtime->edit_mesh) {
1033 BKE_report(reports, RPT_ERROR, "Cannot remove vertices in edit mode");
1034 return;
1035 }
1036 if (count > mesh->verts_num) {
1037 BKE_report(reports, RPT_ERROR, "Cannot remove more vertices than the mesh contains");
1038 return;
1039 }
1040
1042}
1043
1045{
1046 if (mesh->runtime->edit_mesh) {
1047 BKE_report(reports, RPT_ERROR, "Cannot remove edges in edit mode");
1048 return;
1049 }
1050 if (count > mesh->edges_num) {
1051 BKE_report(reports, RPT_ERROR, "Cannot remove more edges than the mesh contains");
1052 return;
1053 }
1054
1056}
1057
1059{
1060 if (mesh->runtime->edit_mesh) {
1061 BKE_report(reports, RPT_ERROR, "Cannot remove loops in edit mode");
1062 return;
1063 }
1064 if (count > mesh->corners_num) {
1065 BKE_report(reports, RPT_ERROR, "Cannot remove more loops than the mesh contains");
1066 return;
1067 }
1068
1070}
1071
1073{
1074 if (mesh->runtime->edit_mesh) {
1075 BKE_report(reports, RPT_ERROR, "Cannot remove polys in edit mode");
1076 return;
1077 }
1078 if (count > mesh->faces_num) {
1079 BKE_report(reports, RPT_ERROR, "Cannot remove more polys than the mesh contains");
1080 return;
1081 }
1082
1084}
1085
1087{
1088 mesh_remove_verts(mesh, mesh->verts_num);
1089 mesh_remove_edges(mesh, mesh->edges_num);
1090 mesh_remove_loops(mesh, mesh->corners_num);
1091 mesh_remove_faces(mesh, mesh->faces_num);
1092}
1093
1095
1096void ED_mesh_report_mirror_ex(wmOperator *op, int totmirr, int totfail, char selectmode)
1097{
1098 const char *elem_type;
1099
1100 if (selectmode & SCE_SELECT_VERTEX) {
1101 elem_type = "vertices";
1102 }
1103 else if (selectmode & SCE_SELECT_EDGE) {
1104 elem_type = "edges";
1105 }
1106 else {
1107 elem_type = "faces";
1108 }
1109
1110 if (totfail) {
1112 op->reports, RPT_WARNING, "%d %s mirrored, %d failed", totmirr, elem_type, totfail);
1113 }
1114 else {
1115 BKE_reportf(op->reports, RPT_INFO, "%d %s mirrored", totmirr, elem_type);
1116 }
1117}
1118
1119void ED_mesh_report_mirror(wmOperator *op, int totmirr, int totfail)
1120{
1121 ED_mesh_report_mirror_ex(op, totmirr, totfail, SCE_SELECT_VERTEX);
1122}
1123
1125{
1126 BLI_assert(me->runtime->edit_mesh && me->runtime->edit_mesh->bm);
1127
1128 return BKE_keyblock_find_by_index(me->key, me->runtime->edit_mesh->bm->shapenr - 1);
1129}
1130
1132{
1133 Mesh *mesh = static_cast<Mesh *>(CTX_data_pointer_get_type(C, "mesh", &RNA_Mesh).data);
1134 if (mesh != nullptr) {
1135 return mesh;
1136 }
1137
1139 if (ob == nullptr) {
1140 return nullptr;
1141 }
1142
1143 ID *data = (ID *)ob->data;
1144 if (data == nullptr || GS(data->name) != ID_ME) {
1145 return nullptr;
1146 }
1147
1148 return (Mesh *)data;
1149}
1150
1152{
1153 using namespace blender;
1154 const OffsetIndices polys = mesh->faces();
1155 const Span<int> corner_edges = mesh->corner_edges();
1156 const bke::AttributeAccessor attributes = mesh->attributes();
1157 const VArray<bool> mesh_sharp_edges = *attributes.lookup_or_default<bool>(
1158 "sharp_edge", bke::AttrDomain::Edge, false);
1159 const VArraySpan<bool> sharp_faces = *attributes.lookup<bool>("sharp_face",
1161
1162 Array<bool> sharp_edges(mesh->edges_num);
1163 mesh_sharp_edges.materialize(sharp_edges);
1164
1165 threading::parallel_for(polys.index_range(), 1024, [&](const IndexRange range) {
1166 for (const int face_i : range) {
1167 if (!sharp_faces.is_empty() && sharp_faces[face_i]) {
1168 for (const int edge : corner_edges.slice(polys[face_i])) {
1169 sharp_edges[edge] = true;
1170 }
1171 }
1172 }
1173 });
1174
1175 IndexMaskMemory memory;
1176 const IndexMask split_mask = IndexMask::from_bools(sharp_edges, memory);
1177 if (split_mask.is_empty()) {
1178 return;
1179 }
1180
1181 geometry::split_edges(*mesh, split_mask, {});
1182}
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:391
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:383
bool BKE_attribute_remove(AttributeOwner &owner, blender::StringRef name, struct ReportList *reports)
Definition attribute.cc:523
const struct CustomDataLayer * BKE_id_attributes_color_find(const struct ID *id, blender::StringRef name)
blender::StringRef BKE_uv_map_edge_select_name_get(blender::StringRef uv_map_name, char *buffer)
bool BKE_color_attribute_supported(const struct Mesh &mesh, blender::StringRef name)
blender::StringRef BKE_uv_map_vert_select_name_get(blender::StringRef uv_map_name, char *buffer)
void BKE_id_attributes_active_color_set(struct ID *id, std::optional< blender::StringRef > name)
Definition attribute.cc:986
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.
void * CustomData_get_layer_named_for_write(CustomData *data, eCustomDataType type, blender::StringRef name, int totelem)
int CustomData_get_named_layer(const CustomData *data, eCustomDataType type, blender::StringRef name)
const void * CustomData_get_layer(const CustomData *data, eCustomDataType type)
int CustomData_get_n_offset(const CustomData *data, eCustomDataType type, int n)
@ CD_SET_DEFAULT
const void * CustomData_get_layer_named(const CustomData *data, eCustomDataType type, blender::StringRef name)
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)
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)
void * CustomData_get_layer_n_for_write(CustomData *data, eCustomDataType type, int n, int totelem)
const CustomData_MeshMasks CD_MASK_MESH
KeyBlock * BKE_keyblock_find_by_index(Key *key, int index)
Definition key.cc:1940
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
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:126
#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:982
@ ID_ME
@ 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
@ 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:390
#define ND_DATA
Definition WM_types.hh:506
#define NC_SCENE
Definition WM_types.hh:375
#define ND_TOOLSETTINGS
Definition WM_types.hh:446
ReportList * reports
Definition WM_types.hh:1025
@ OPTYPE_UNDO
Definition WM_types.hh:182
@ OPTYPE_REGISTER
Definition WM_types.hh:180
@ 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_uv_map_attr_select_and_pin_ensure(BMesh *bm)
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_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:43
static IndexMask from_bools(Span< bool > bools, IndexMaskMemory &memory)
bool is_empty() const
const T * data() const
Definition BLI_array.hh:301
constexpr int64_t size() const
void materialize(MutableSpan< T > r_span) const
GAttributeReader lookup(const StringRef attribute_id) const
GAttributeReader lookup_or_default(StringRef attribute_id, AttrDomain domain, eCustomDataType data_type, const void *default_value=nullptr) const
GSpanAttributeWriter lookup_or_add_for_write_span(StringRef attribute_id, AttrDomain domain, eCustomDataType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
#define sinf(x)
#define cosf(x)
#define active
VecBase< short, 2 > short2
#define MAX_CUSTOMDATA_LAYER_NAME
#define ID_IS_EDITABLE(_id)
#define ID_IS_OVERRIDE_LIBRARY(_id)
#define GS(a)
#define MAX_MTFACE
int count
void * MEM_dupallocN(const void *vmemh)
Definition mallocn.cc:143
void MESH_OT_customdata_mask_clear(wmOperatorType *ot)
Definition mesh_data.cc:618
void ED_mesh_uv_ensure(Mesh *mesh, const char *name)
Definition mesh_data.cc:364
bool * ED_mesh_uv_map_vert_select_layer_ensure(Mesh *mesh, const int uv_index)
Definition mesh_data.cc:342
void ED_mesh_edges_remove(Mesh *mesh, ReportList *reports, int count)
static bool layers_poll(bContext *C)
Definition mesh_data.cc:452
const bool * ED_mesh_uv_map_edge_select_layer_get(const Mesh *mesh, const int uv_index)
Definition mesh_data.cc:310
static void mesh_uv_reset_bmface(BMFace *f, const int cd_loop_uv_offset)
Definition mesh_data.cc:151
static void mesh_remove_loops(Mesh *mesh, int len)
void ED_mesh_report_mirror_ex(wmOperator *op, int totmirr, int totfail, char selectmode)
bool ED_mesh_color_ensure(Mesh *mesh, const char *name)
Definition mesh_data.cc:424
void MESH_OT_customdata_skin_add(wmOperatorType *ot)
Definition mesh_data.cc:671
const bool * ED_mesh_uv_map_vert_select_layer_get(const Mesh *mesh, const int uv_index)
Definition mesh_data.cc:302
static wmOperatorStatus mesh_customdata_mask_clear_exec(bContext *C, wmOperator *op)
Definition mesh_data.cc:604
static wmOperatorStatus mesh_uv_texture_add_exec(bContext *C, wmOperator *op)
Definition mesh_data.cc:479
static const bool * mesh_loop_boolean_custom_data_get_by_name(const Mesh &mesh, const StringRef name)
Definition mesh_data.cc:295
static wmOperatorStatus mesh_customdata_skin_clear_exec(bContext *C, wmOperator *)
Definition mesh_data.cc:691
bool * ED_mesh_uv_map_edge_select_layer_ensure(Mesh *mesh, const int uv_index)
Definition mesh_data.cc:349
void MESH_OT_uv_texture_remove(wmOperatorType *ot)
Definition mesh_data.cc:536
void ED_mesh_edges_add(Mesh *mesh, ReportList *reports, int count)
Definition mesh_data.cc:953
static bool uv_texture_remove_poll(bContext *C)
Definition mesh_data.cc:462
void ED_mesh_loops_add(Mesh *mesh, ReportList *reports, int count)
Definition mesh_data.cc:962
static bool * ensure_corner_boolean_attribute(Mesh &mesh, const StringRef name)
Definition mesh_data.cc:331
void ED_mesh_report_mirror(wmOperator *op, int totmirr, int totfail)
void ED_mesh_verts_add(Mesh *mesh, ReportList *reports, int count)
Definition mesh_data.cc:944
void MESH_OT_customdata_skin_clear(wmOperatorType *ot)
Definition mesh_data.cc:696
static void mesh_remove_faces(Mesh *mesh, int len)
void ED_mesh_geometry_clear(Mesh *mesh)
static void mesh_add_edges(Mesh *mesh, int len)
Definition mesh_data.cc:828
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:221
void ED_mesh_faces_add(Mesh *mesh, ReportList *reports, int count)
Definition mesh_data.cc:971
static bool mesh_customdata_skin_add_poll(bContext *C)
Definition mesh_data.cc:653
static wmOperatorStatus mesh_customdata_skin_add_exec(bContext *C, wmOperator *)
Definition mesh_data.cc:658
static CustomData * mesh_customdata_get_type(Mesh *mesh, const char htype, int *r_tot)
Definition mesh_data.cc:51
static void mesh_uv_reset_mface(const blender::IndexRange face, float2 *mloopuv)
Definition mesh_data.cc:165
void ED_mesh_loops_remove(Mesh *mesh, ReportList *reports, int count)
static wmOperatorStatus mesh_uv_texture_remove_exec(bContext *C, wmOperator *op)
Definition mesh_data.cc:512
static void mesh_add_faces(Mesh *mesh, int len)
Definition mesh_data.cc:901
void ED_mesh_uv_loop_reset_ex(Mesh *mesh, const int layernum)
Definition mesh_data.cc:176
bool * ED_mesh_uv_map_pin_layer_ensure(Mesh *mesh, const int uv_index)
Definition mesh_data.cc:356
void ED_mesh_uv_loop_reset(bContext *C, Mesh *mesh)
Definition mesh_data.cc:211
static int mesh_customdata_skin_state(bContext *C)
Definition mesh_data.cc:639
static wmOperatorStatus mesh_customdata_clear_exec__internal(bContext *C, char htype, const eCustomDataType type)
Definition mesh_data.cc:553
int ED_mesh_color_add(Mesh *mesh, const char *name, const bool active_set, const bool do_init, ReportList *reports)
Definition mesh_data.cc:382
void ED_mesh_split_faces(Mesh *mesh)
static void mesh_remove_verts(Mesh *mesh, int len)
Definition mesh_data.cc:986
static void mesh_add_verts(Mesh *mesh, int len)
Definition mesh_data.cc:797
void MESH_OT_uv_texture_add(wmOperatorType *ot)
Definition mesh_data.cc:497
static void mesh_add_loops(Mesh *mesh, int len)
Definition mesh_data.cc:864
static bool mesh_customdata_mask_clear_poll(bContext *C)
Definition mesh_data.cc:580
static wmOperatorStatus mesh_customdata_custom_splitnormals_clear_exec(bContext *C, wmOperator *)
Definition mesh_data.cc:754
static void mesh_remove_edges(Mesh *mesh, int len)
Definition mesh_data.cc:997
KeyBlock * ED_mesh_get_edit_shape_key(const Mesh *me)
void MESH_OT_customdata_custom_splitnormals_add(wmOperatorType *ot)
Definition mesh_data.cc:739
static bool mesh_customdata_skin_clear_poll(bContext *C)
Definition mesh_data.cc:686
void ED_mesh_verts_remove(Mesh *mesh, ReportList *reports, int count)
static void mesh_uv_reset_array(float **fuv, const int len)
Definition mesh_data.cc:111
const bool * ED_mesh_uv_map_pin_layer_get(const Mesh *mesh, const int uv_index)
Definition mesh_data.cc:322
static wmOperatorStatus mesh_customdata_custom_splitnormals_add_exec(bContext *C, wmOperator *)
Definition mesh_data.cc:712
void MESH_OT_customdata_custom_splitnormals_clear(wmOperatorType *ot)
Definition mesh_data.cc:782
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)
Definition DNA_ID.h:404
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:4226