Blender V5.3
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.hh"
16
17#include "BKE_attribute.h"
18#include "BKE_attribute.hh"
20#include "BKE_context.hh"
21#include "BKE_customdata.hh"
22#include "BKE_editmesh.hh"
23#include "BKE_key.hh"
24#include "BKE_library.hh"
25#include "BKE_mesh.hh"
26#include "BKE_mesh_runtime.hh"
27#include "BKE_report.hh"
28
29#include "DEG_depsgraph.hh"
30
31#include "RNA_prototypes.hh"
32
33#include "WM_api.hh"
34#include "WM_types.hh"
35
36#include "BLT_translation.hh"
37
38#include "ED_mesh.hh"
39#include "ED_object.hh"
40#include "ED_paint.hh"
41#include "ED_screen.hh"
42
44
45#include "mesh_intern.hh" /* own include */
46
47namespace blender {
48
49/* -------------------------------------------------------------------- */
52
53static void mesh_uv_reset_array(float **fuv, const int len)
54{
55 if (len == 3) {
56 fuv[0][0] = 0.0;
57 fuv[0][1] = 0.0;
58
59 fuv[1][0] = 1.0;
60 fuv[1][1] = 0.0;
61
62 fuv[2][0] = 1.0;
63 fuv[2][1] = 1.0;
64 }
65 else if (len == 4) {
66 fuv[0][0] = 0.0;
67 fuv[0][1] = 0.0;
68
69 fuv[1][0] = 1.0;
70 fuv[1][1] = 0.0;
71
72 fuv[2][0] = 1.0;
73 fuv[2][1] = 1.0;
74
75 fuv[3][0] = 0.0;
76 fuv[3][1] = 1.0;
77 /* Make sure we ignore 2-sided faces. */
78 }
79 else if (len > 2) {
80 float fac = 0.0f, dfac = 1.0f / float(len);
81
82 dfac *= float(M_PI) * 2.0f;
83
84 for (int i = 0; i < len; i++) {
85 fuv[i][0] = 0.5f * sinf(fac) + 0.5f;
86 fuv[i][1] = 0.5f * cosf(fac) + 0.5f;
87
88 fac += dfac;
89 }
90 }
91}
92
93static void reset_uvs_bmesh(BMFace *f, const int cd_loop_uv_offset)
94{
96 BMIter liter;
97 BMLoop *l;
98 int i;
99
100 BM_ITER_ELEM_INDEX (l, &liter, f, BM_LOOPS_OF_FACE, i) {
101 fuv[i] = (static_cast<float *> BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset));
102 }
103
104 mesh_uv_reset_array(fuv.data(), f->len);
105}
106
107static void reset_uvs_mesh(const IndexRange face, MutableSpan<float2> uv_map)
108{
110
111 for (int i = 0; i < face.size(); i++) {
112 fuv[i] = &uv_map[face[i]].x;
113 }
114
115 mesh_uv_reset_array(fuv.data(), face.size());
116}
117
118static void reset_uv_map(Mesh *mesh, const StringRef name)
119{
121 const int cd_loop_uv_offset = CustomData_get_offset_named(&bm->ldata, CD_PROP_FLOAT2, name);
122 BLI_assert(cd_loop_uv_offset >= 0);
123
124 BMFace *efa;
125 BMIter iter;
126 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
128 continue;
129 }
130 reset_uvs_bmesh(efa, cd_loop_uv_offset);
131 }
132 }
133 else {
134 const OffsetIndices faces = mesh->faces();
135 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
136 bke::SpanAttributeWriter uv_map = attributes.lookup_for_write_span<float2>(name);
138 for (const int i : faces.index_range()) {
139 reset_uvs_mesh(faces[i], uv_map.span);
140 }
141 uv_map.finish();
142 }
143
144 DEG_id_tag_update(&mesh->id, 0);
145}
146
148{
149 reset_uv_map(mesh, mesh->active_uv_map_name());
150
152}
153
154std::optional<std::string> ED_mesh_uv_add(
155 Mesh *mesh, const char *name, const bool active_set, const bool do_init, ReportList *reports)
156{
157 /* NOTE: keep in sync with #ED_mesh_color_add. */
158
159 int layernum_dst;
160
161 if (!name) {
162 name = DATA_("UVMap");
163 }
164
166 const std::string unique_name = BKE_attribute_calc_unique_name(owner, name);
167 bool is_init = false;
168
171 if (layernum_dst >= MAX_MTFACE) {
172 BKE_reportf(reports, RPT_WARNING, "Cannot add more than %i UV maps", MAX_MTFACE);
173 return std::nullopt;
174 }
175
176 BM_data_layer_add_named(bm, &bm->ldata, CD_PROP_FLOAT2, unique_name.c_str());
178 /* copy data from active UV */
179 if (layernum_dst && do_init) {
180 const int layernum_src = CustomData_get_active_layer(&bm->ldata, CD_PROP_FLOAT2);
181 BM_data_layer_copy(bm, &bm->ldata, CD_PROP_FLOAT2, layernum_src, layernum_dst);
182
183 is_init = true;
184 }
185 if (active_set || layernum_dst == 0) {
186 mesh->uv_maps_active_set(unique_name);
187 }
188 }
189 else {
190 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
191 layernum_dst = mesh->uv_map_names().size();
192 if (layernum_dst >= MAX_MTFACE) {
193 BKE_reportf(reports, RPT_WARNING, "Cannot add more than %i UV maps", MAX_MTFACE);
194 return std::nullopt;
195 }
196
197 const StringRef active_name = mesh->active_uv_map_name();
198 if (!active_name.is_empty() && do_init) {
199 const VArray<float2> active_uv_map = *attributes.lookup_or_default<float2>(
200 active_name, bke::AttrDomain::Corner, float2(0));
201 attributes.add<float2>(
202 unique_name, bke::AttrDomain::Corner, bke::AttributeInitVArray(active_uv_map));
203
204 is_init = true;
205 }
206 else {
207 attributes.add<float2>(
209 }
210
211 if (active_set || layernum_dst == 0) {
212 mesh->uv_maps_active_set(unique_name);
213 }
214 }
215
216 /* don't overwrite our copied coords */
217 if (!is_init && do_init) {
218 reset_uv_map(mesh, unique_name);
219 }
220
221 DEG_id_tag_update(&mesh->id, 0);
223
224 return unique_name;
225}
226
228{
229 const bke::AttributeAccessor attributes = mesh.attributes();
230 return *attributes.lookup_or_default<bool>(name, bke::AttrDomain::Corner, false);
231}
232
234{
235 using namespace blender::bke;
236 char buffer[MAX_CUSTOMDATA_LAYER_NAME];
237 const char *uv_name = mesh->uv_map_names()[uv_index].c_str();
239}
240
242{
243 bke::MutableAttributeAccessor attributes = mesh.attributes_for_write();
244 return attributes.lookup_or_add_for_write<bool>(
246}
247
249{
250 using namespace blender::bke;
251 char buffer[MAX_CUSTOMDATA_LAYER_NAME];
252 const char *uv_name = mesh->uv_map_names()[uv_index].c_str();
254}
255
256void ED_mesh_uv_ensure(Mesh *mesh, const char *name)
257{
259 int layernum_dst = CustomData_number_of_layers(&bm->ldata, CD_PROP_FLOAT2);
260 if (layernum_dst == 0) {
261 ED_mesh_uv_add(mesh, name, true, true, nullptr);
262 }
263 }
264 else {
265 if (mesh->uv_map_names().is_empty()) {
266 ED_mesh_uv_add(mesh, name, true, true, nullptr);
267 }
268 }
269}
270
272
273/* -------------------------------------------------------------------- */
276
277std::optional<std::string> ED_mesh_color_add(Mesh *mesh,
278 const char *name,
279 const bool active_set,
280 const bool do_init,
281 ReportList * /*reports*/)
282{
283 /* If no name is supplied, provide a backwards compatible default. */
284 if (!name) {
285 name = "Col";
286 }
287
289 std::string new_name = BKE_attribute_calc_unique_name(owner, name);
290
291 const StringRef active_name = mesh->active_color_attribute;
294 if (do_init) {
295 const BMDataLayerLookup active_attr = BM_data_layer_lookup(*bm, active_name);
296 if (active_attr.type == bke::AttrType::ColorByte &&
297 active_attr.domain == bke::AttrDomain::Corner)
298 {
299 const int src_i = CustomData_get_named_layer(&bm->ldata, CD_PROP_BYTE_COLOR, active_name);
300 const int dst_i = CustomData_get_named_layer(&bm->ldata, CD_PROP_BYTE_COLOR, new_name);
302 }
303 }
304 }
305 else {
306 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
307 attributes.add<ColorGeometry4b>(
309 if (do_init) {
310 if (const VArray active_attr = *attributes.lookup<ColorGeometry4b>(active_name,
312 {
314 new_name);
315 active_attr.materialize(new_attr.span);
316 new_attr.finish();
317 }
318 }
319 }
320
321 if (active_set) {
323 }
324
327
328 return new_name;
329}
330
331bool ED_mesh_color_ensure(Mesh *mesh, const char *name)
332{
333 BLI_assert(mesh->runtime->edit_mesh == nullptr);
334 if (BKE_id_attributes_color_find(&mesh->id, mesh->active_color_attribute)) {
335 return true;
336 }
337
339 const std::string unique_name = BKE_attribute_calc_unique_name(owner, name);
340 if (!mesh->attributes_for_write().add(unique_name,
344 {
345 return false;
346 }
347
348 BKE_id_attributes_active_color_set(&mesh->id, unique_name);
349 BKE_id_attributes_default_color_set(&mesh->id, unique_name);
352
353 return true;
354}
355
357
358/* -------------------------------------------------------------------- */
361
363{
365 ID *data = (ob) ? ob->data : nullptr;
366 return (ob && ID_IS_EDITABLE(ob) && !ID_IS_OVERRIDE_LIBRARY(ob) && ob->type == OB_MESH && data &&
368}
369
371{
372 if (!uv_maps_poll(C)) {
373 return false;
374 }
375
377 Mesh *mesh = id_cast<Mesh *>(ob->data);
378 const StringRef active_name = mesh->active_uv_map_name();
380 if (!CustomData_has_layer_named(&bm->ldata, CD_PROP_FLOAT2, active_name)) {
381 return false;
382 }
383 }
384 else {
385 if (!mesh->uv_map_names().contains_as(active_name)) {
386 return false;
387 }
388 }
389
390 return true;
391}
392
394{
396 Mesh *mesh = id_cast<Mesh *>(ob->data);
397
398 if (!ED_mesh_uv_add(mesh, nullptr, true, true, op->reports)) {
399 return OPERATOR_CANCELLED;
400 }
401
402 if (ob->mode & OB_MODE_TEXTURE_PAINT) {
403 Scene *scene = CTX_data_scene(C);
404 ED_paint_proj_mesh_data_check(*scene, *ob, nullptr, nullptr, nullptr, nullptr);
406 }
407
408 return OPERATOR_FINISHED;
409}
410
412{
413 ot->name = "Add UV Map";
414 ot->description = "Add UV map";
415 ot->idname = "MESH_OT_uv_texture_add";
416
417 ot->poll = uv_maps_poll;
419
421}
422
424{
426 Mesh *mesh = id_cast<Mesh *>(ob->data);
427
429 const StringRef name = mesh->active_uv_map_name();
430 if (!BKE_attribute_remove(owner, name, op->reports)) {
431 return OPERATOR_CANCELLED;
432 }
433
434 if (ob->mode & OB_MODE_TEXTURE_PAINT) {
435 Scene *scene = CTX_data_scene(C);
436 ED_paint_proj_mesh_data_check(*scene, *ob, nullptr, nullptr, nullptr, nullptr);
438 }
439
442
443 return OPERATOR_FINISHED;
444}
445
447{
448 ot->name = "Remove UV Map";
449 ot->description = "Remove UV map";
450 ot->idname = "MESH_OT_uv_texture_remove";
451
454
456}
457
459
460/* -------------------------------------------------------------------- */
463
465{
467 if (!ob) {
468 return false;
469 }
470 if (ob->type != OB_MESH) {
471 return false;
472 }
473 if (ob->mode & OB_MODE_SCULPT) {
474 return false;
475 }
476 Mesh *mesh = id_cast<Mesh *>(ob->data);
478 return false;
479 }
481 if (CustomData_has_layer_named(&bm->vdata, CD_PROP_FLOAT, ".sculpt_mask")) {
482 return true;
483 }
485 return true;
486 }
487 return false;
488 }
489 if (CustomData_has_layer(&mesh->corner_data, CD_GRID_PAINT_MASK)) {
490 return true;
491 }
492 if (mesh->attributes().contains(".sculpt_mask")) {
493 return true;
494 }
495 return false;
496}
497
499{
501 Mesh *mesh = id_cast<Mesh *>(object->data);
503 const bool removed_a = CustomData_free_layer_named(&bm->vdata, ".sculpt_mask");
504 const bool removed_b = CustomData_free_layers(&bm->ldata, CD_GRID_PAINT_MASK);
505 if (!(removed_a || removed_b)) {
506 return OPERATOR_CANCELLED;
507 }
508 }
509 else {
510 const bool removed_a = mesh->attributes_for_write().remove(".sculpt_mask");
511 const bool removed_b = CustomData_free_layers(&mesh->corner_data, CD_GRID_PAINT_MASK);
512 if (!(removed_a || removed_b)) {
513 return OPERATOR_CANCELLED;
514 }
515 }
518 return OPERATOR_FINISHED;
519}
520
522{
524 if (!object || object->type != OB_MESH) {
525 return false;
526 }
527
528 Mesh *mesh = id_cast<Mesh *>(object->data);
530 return false;
531 }
532
534 return CustomData_has_layer_named(&bm->pdata, CD_PROP_INT32, ".sculpt_face_set");
535 }
536
537 return mesh->attributes().contains(".sculpt_face_set");
538}
539
541{
543 Mesh *mesh = id_cast<Mesh *>(object->data);
544
545 bool changed = false;
547 changed = CustomData_free_layer_named(&bm->pdata, ".sculpt_face_set");
548 }
549 else {
550 changed = mesh->attributes_for_write().remove(".sculpt_face_set");
551 }
552
553 if (!changed) {
554 return OPERATOR_CANCELLED;
555 }
556
559
560 return OPERATOR_FINISHED;
561}
562
564{
565 ot->name = "Clear Sculpt Face Sets";
566 ot->idname = "MESH_OT_customdata_face_sets_clear";
567 ot->description = "Clear sculpt face set data from the mesh";
568
571
573}
574
576{
577 ot->name = "Clear Sculpt Mask Data";
578 ot->idname = "MESH_OT_customdata_mask_clear";
579 ot->description = "Clear vertex sculpt masking data from the mesh";
580
583
585}
586
587enum class SkinState {
591};
593{
595 if (!ob) {
596 return SkinState::Invalid;
597 }
598 if (ob->type != OB_MESH) {
599 return SkinState::Invalid;
600 }
601 Mesh *mesh = id_cast<Mesh *>(ob->data);
603 return SkinState::Invalid;
604 }
606 return CustomData_has_layer_named(&bm->vdata, CD_PROP_FLOAT2, "skin_modifier_radius") ?
609 }
610 return mesh->attributes().contains("skin_modifier_radius") ? SkinState::HasSkin :
612}
613
618
631
633{
634 ot->name = "Add Skin Data";
635 ot->idname = "MESH_OT_customdata_skin_add";
636 ot->description = "Add a vertex skin layer";
637
640
642}
643
648
650{
652 Mesh *mesh = id_cast<Mesh *>(object->data);
654 if (!CustomData_has_layer_named(&bm->vdata, CD_PROP_FLOAT2, "skin_modifier_radius")) {
655 return OPERATOR_CANCELLED;
656 }
657 BM_data_layer_free_named(bm, &bm->vdata, "skin_modifier_radius");
658 BM_data_layer_free_named(bm, &bm->vdata, "skin_modifier_root");
659 BM_data_layer_free_named(bm, &bm->vdata, "skin_modifier_loose");
660 }
661 else {
662 if (!mesh->attributes_for_write().remove("skin_modifier_radius")) {
663 return OPERATOR_CANCELLED;
664 }
665 mesh->attributes_for_write().remove("skin_modifier_root");
666 mesh->attributes_for_write().remove("skin_modifier_loose");
667 }
670 return OPERATOR_FINISHED;
671}
672
674{
675 ot->name = "Clear Skin Data";
676 ot->idname = "MESH_OT_customdata_skin_clear";
677 ot->description = "Clear vertex skin layer";
678
681
683}
684
686 wmOperator * /*op*/)
687{
690 if (BM_data_layer_lookup(*bm, "custom_normal")) {
691 return OPERATOR_CANCELLED;
692 }
694 }
695 else {
696 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
698 if (!attributes.add<short2>("custom_normal", bke::AttrDomain::Corner, init)) {
699 return OPERATOR_CANCELLED;
700 }
701 }
702
705
706 return OPERATOR_FINISHED;
707}
708
710{
711 ot->name = "Add Custom Normals Data";
712 ot->idname = "MESH_OT_customdata_custom_splitnormals_add";
713 ot->description = "Add a custom normals layer, if none exists yet";
714
717
719}
720
722 wmOperator * /*op*/)
723{
726 if (!CustomData_has_layer_named(&bm->ldata, CD_PROP_INT16_2D, "custom_normal")) {
727 return OPERATOR_CANCELLED;
728 }
729 BM_data_layer_free_named(bm, &bm->ldata, "custom_normal");
730 if (bm->lnor_spacearr) {
732 }
733 }
734 else {
735 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
736 if (!attributes.remove("custom_normal")) {
737 return OPERATOR_CANCELLED;
738 }
739 }
740
741 mesh->tag_custom_normals_changed();
744
745 return OPERATOR_FINISHED;
746}
747
749{
750 ot->name = "Clear Custom Normals Data";
751 ot->idname = "MESH_OT_customdata_custom_splitnormals_clear";
752 ot->description = "Remove the custom normals layer, if it exists";
753
756
758}
759
761
762/* -------------------------------------------------------------------- */
765
766static void mesh_add_verts(Mesh *mesh, int len)
767{
768 if (len == 0) {
769 return;
770 }
771
772 const int old_size = mesh->verts_num;
773 const int totvert = old_size + len;
774 mesh->attribute_storage.wrap().resize(bke::AttrDomain::Point, totvert);
775 CustomData_realloc(&mesh->vert_data, mesh->verts_num, totvert, CD_SET_DEFAULT);
776
778
779 mesh->verts_num = totvert;
780
781 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
783 attributes, bke::AttrDomain::Point, {}, IndexRange::from_begin_size(old_size, len));
785 ".select_vert", bke::AttrDomain::Point);
786 select_vert.span.take_back(len).fill(true);
787 select_vert.finish();
789}
790
791static void mesh_add_edges(Mesh *mesh, int len)
792{
793 if (len == 0) {
794 return;
795 }
796
797 const int old_size = mesh->edges_num;
798 const int totedge = old_size + len;
799 mesh->attribute_storage.wrap().resize(bke::AttrDomain::Edge, totedge);
800 CustomData_realloc(&mesh->edge_data, mesh->edges_num, totedge, CD_SET_DEFAULT);
801
803
804 mesh->edges_num = totedge;
805
806 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
808 attributes, bke::AttrDomain::Edge, {}, IndexRange::from_begin_size(old_size, len));
810 ".select_edge", bke::AttrDomain::Edge);
811 select_edge.span.take_back(len).fill(true);
812 select_edge.finish();
813 attributes.add<int2>(".edge_verts", bke::AttrDomain::Edge, bke::AttributeInitDefaultValue());
814}
815
816static void mesh_add_loops(Mesh *mesh, int len)
817{
818 if (len == 0) {
819 return;
820 }
821
822 const int old_size = mesh->corners_num;
823 const int totloop = old_size + len;
824 mesh->attribute_storage.wrap().resize(bke::AttrDomain::Corner, totloop);
825 CustomData_realloc(&mesh->corner_data, mesh->corners_num, totloop, CD_SET_DEFAULT);
826
828
829 mesh->corners_num = totloop;
830
831 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
833 attributes, bke::AttrDomain::Corner, {}, IndexRange::from_begin_size(old_size, len));
834 attributes.add<int>(".corner_vert", bke::AttrDomain::Corner, bke::AttributeInitDefaultValue());
835 attributes.add<int>(".corner_edge", bke::AttrDomain::Corner, bke::AttributeInitDefaultValue());
836
837 /* Keep the last face offset up to date with the corner total (they must be the same). We have
838 * to be careful here though, since the mesh may not be in a valid state at this point. */
839 if (mesh->face_offset_indices) {
840 mesh->face_offsets_for_write().last() = mesh->corners_num;
841 }
842}
843
844static void mesh_add_faces(Mesh *mesh, int len)
845{
846 if (len == 0) {
847 return;
848 }
849
850 const int old_size = mesh->faces_num;
851 const int faces_num = old_size + len;
852 mesh->attribute_storage.wrap().resize(bke::AttrDomain::Face, faces_num);
853 CustomData_realloc(&mesh->face_data, mesh->faces_num, faces_num, CD_SET_DEFAULT);
854
855 implicit_sharing::resize_trivial_array(&mesh->face_offset_indices,
856 &mesh->runtime->face_offsets_sharing_info,
857 mesh->faces_num == 0 ? 0 : (mesh->faces_num + 1),
858 faces_num + 1);
859 /* Set common values for convenience. */
860 mesh->face_offset_indices[0] = 0;
861 mesh->face_offset_indices[faces_num] = mesh->corners_num;
862
864
865 mesh->faces_num = faces_num;
866
867 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
869 attributes, bke::AttrDomain::Face, {}, IndexRange::from_begin_size(old_size, len));
871 ".select_poly", bke::AttrDomain::Face);
872 select_poly.span.take_back(len).fill(true);
873 select_poly.finish();
874}
875
876void ED_mesh_verts_add(Mesh *mesh, ReportList *reports, int count)
877{
878 if (mesh->runtime->edit_mesh) {
879 BKE_report(reports, RPT_ERROR, "Cannot add vertices in edit mode");
880 return;
881 }
882 mesh_add_verts(mesh, count);
883}
884
885void ED_mesh_edges_add(Mesh *mesh, ReportList *reports, int count)
886{
887 if (mesh->runtime->edit_mesh) {
888 BKE_report(reports, RPT_ERROR, "Cannot add edges in edit mode");
889 return;
890 }
891 mesh_add_edges(mesh, count);
892}
893
894void ED_mesh_loops_add(Mesh *mesh, ReportList *reports, int count)
895{
896 if (mesh->runtime->edit_mesh) {
897 BKE_report(reports, RPT_ERROR, "Cannot add loops in edit mode");
898 return;
899 }
900 mesh_add_loops(mesh, count);
901}
902
903void ED_mesh_faces_add(Mesh *mesh, ReportList *reports, int count)
904{
905 if (mesh->runtime->edit_mesh) {
906 BKE_report(reports, RPT_ERROR, "Cannot add faces in edit mode");
907 return;
908 }
909 mesh_add_faces(mesh, count);
910}
911
913
914/* -------------------------------------------------------------------- */
917
918static void mesh_remove_verts(Mesh *mesh, int len)
919{
920 if (len == 0) {
921 return;
922 }
923 CustomData_ensure_layers_are_mutable(&mesh->vert_data, mesh->verts_num);
924 const int totvert = mesh->verts_num - len;
925 CustomData_free_elem(&mesh->vert_data, totvert, len);
926 mesh->verts_num = totvert;
927}
928
929static void mesh_remove_edges(Mesh *mesh, int len)
930{
931 if (len == 0) {
932 return;
933 }
934 CustomData_ensure_layers_are_mutable(&mesh->edge_data, mesh->edges_num);
935 const int totedge = mesh->edges_num - len;
936 CustomData_free_elem(&mesh->edge_data, totedge, len);
937 mesh->edges_num = totedge;
938}
939
940static void mesh_remove_loops(Mesh *mesh, int len)
941{
942 if (len == 0) {
943 return;
944 }
945 CustomData_ensure_layers_are_mutable(&mesh->corner_data, mesh->corners_num);
946 const int totloop = mesh->corners_num - len;
947 CustomData_free_elem(&mesh->corner_data, totloop, len);
948 mesh->corners_num = totloop;
949}
950
951static void mesh_remove_faces(Mesh *mesh, int len)
952{
953 if (len == 0) {
954 return;
955 }
956 CustomData_ensure_layers_are_mutable(&mesh->face_data, mesh->faces_num);
957 const int faces_num = mesh->faces_num - len;
958 CustomData_free_elem(&mesh->face_data, faces_num, len);
959 mesh->faces_num = faces_num;
960}
961
962void ED_mesh_verts_remove(Mesh *mesh, ReportList *reports, int count)
963{
964 if (mesh->runtime->edit_mesh) {
965 BKE_report(reports, RPT_ERROR, "Cannot remove vertices in edit mode");
966 return;
967 }
968 if (count > mesh->verts_num) {
969 BKE_report(reports, RPT_ERROR, "Cannot remove more vertices than the mesh contains");
970 return;
971 }
972
973 mesh_remove_verts(mesh, count);
974}
975
976void ED_mesh_edges_remove(Mesh *mesh, ReportList *reports, int count)
977{
978 if (mesh->runtime->edit_mesh) {
979 BKE_report(reports, RPT_ERROR, "Cannot remove edges in edit mode");
980 return;
981 }
982 if (count > mesh->edges_num) {
983 BKE_report(reports, RPT_ERROR, "Cannot remove more edges than the mesh contains");
984 return;
985 }
986
987 mesh_remove_edges(mesh, count);
988}
989
990void ED_mesh_loops_remove(Mesh *mesh, ReportList *reports, int count)
991{
992 if (mesh->runtime->edit_mesh) {
993 BKE_report(reports, RPT_ERROR, "Cannot remove loops in edit mode");
994 return;
995 }
996 if (count > mesh->corners_num) {
997 BKE_report(reports, RPT_ERROR, "Cannot remove more loops than the mesh contains");
998 return;
999 }
1000
1001 mesh_remove_loops(mesh, count);
1002}
1003
1004void ED_mesh_faces_remove(Mesh *mesh, ReportList *reports, int count)
1005{
1006 if (mesh->runtime->edit_mesh) {
1007 BKE_report(reports, RPT_ERROR, "Cannot remove polys in edit mode");
1008 return;
1009 }
1010 if (count > mesh->faces_num) {
1011 BKE_report(reports, RPT_ERROR, "Cannot remove more polys than the mesh contains");
1012 return;
1013 }
1014
1015 mesh_remove_faces(mesh, count);
1016}
1017
1019{
1020 mesh_remove_verts(mesh, mesh->verts_num);
1021 mesh_remove_edges(mesh, mesh->edges_num);
1022 mesh_remove_loops(mesh, mesh->corners_num);
1023 mesh_remove_faces(mesh, mesh->faces_num);
1024}
1025
1027
1028/* -------------------------------------------------------------------- */
1031
1032void ED_mesh_report_mirror_ex(ReportList &reports, int totmirr, int totfail, char selectmode)
1033{
1034 const char *elem_type;
1035
1036 if (selectmode & SCE_SELECT_VERTEX) {
1037 elem_type = "vertices";
1038 }
1039 else if (selectmode & SCE_SELECT_EDGE) {
1040 elem_type = "edges";
1041 }
1042 else {
1043 elem_type = "faces";
1044 }
1045
1046 if (totfail) {
1047 BKE_reportf(&reports, RPT_WARNING, "%d %s mirrored, %d failed", totmirr, elem_type, totfail);
1048 }
1049 else {
1050 BKE_reportf(&reports, RPT_INFO, "%d %s mirrored", totmirr, elem_type);
1051 }
1052}
1053
1054void ED_mesh_report_mirror(ReportList &reports, int totmirr, int totfail)
1055{
1056 ED_mesh_report_mirror_ex(reports, totmirr, totfail, SCE_SELECT_VERTEX);
1057}
1058
1060{
1061 const BMesh *bm = BKE_editmesh_bmesh_get(me);
1062 BLI_assert(me->runtime->edit_mesh && bm);
1063 return BKE_keyblock_find_by_index(me->key, bm->shapenr - 1);
1064}
1065
1067{
1068 Mesh *mesh = static_cast<Mesh *>(CTX_data_pointer_get_type(C, "mesh", RNA_Mesh).data);
1069 if (mesh != nullptr) {
1070 return mesh;
1071 }
1072
1074 if (ob == nullptr) {
1075 return nullptr;
1076 }
1077
1078 ID *data = ob->data;
1079 if (data == nullptr || GS(data->name) != ID_ME) {
1080 return nullptr;
1081 }
1082
1083 return id_cast<Mesh *>(data);
1084}
1085
1087{
1088 const OffsetIndices polys = mesh->faces();
1089 const Span<int> corner_edges = mesh->corner_edges();
1090 const bke::AttributeAccessor attributes = mesh->attributes();
1091 const VArray<bool> mesh_sharp_edges = *attributes.lookup_or_default<bool>(
1092 "sharp_edge", bke::AttrDomain::Edge, false);
1093 const VArraySpan<bool> sharp_faces = *attributes.lookup<bool>("sharp_face",
1095
1096 Array<bool> sharp_edges(mesh->edges_num);
1097 mesh_sharp_edges.materialize(sharp_edges);
1098
1099 threading::parallel_for(polys.index_range(), 1024, [&](const IndexRange range) {
1100 for (const int face_i : range) {
1101 if (!sharp_faces.is_empty() && sharp_faces[face_i]) {
1102 for (const int edge : corner_edges.slice(polys[face_i])) {
1103 sharp_edges[edge] = true;
1104 }
1105 }
1106 }
1107 });
1108
1109 IndexMaskMemory memory;
1110 const IndexMask split_mask = IndexMask::from_bools(sharp_edges, memory);
1111 if (split_mask.is_empty()) {
1112 return;
1113 }
1114
1115 geometry::split_edges(*mesh, split_mask, {});
1116}
1117
1119
1120} // namespace blender
Generic geometry attributes built on CustomData.
CustomData interface, see also DNA_customdata_types.h.
#define M_PI
#define DATA_(msgid)
#define ID_IS_EDITABLE(_id)
Definition DNA_ID.h:708
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition DNA_ID.h:736
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DATA
Definition WM_types.hh:552
#define NC_SCENE
Definition WM_types.hh:391
#define ND_TOOLSETTINGS
Definition WM_types.hh:468
#define BM_ELEM_CD_GET_VOID_P(ele, offset)
#define BM_elem_flag_test(ele, hflag)
#define BM_ITER_MESH(ele, iter, bm, itype)
#define BM_ITER_ELEM_INDEX(ele, iter, data, itype, indexvar)
#define MAX_CUSTOMDATA_LAYER_NAME
#define MAX_MTFACE
const T * data() const
Definition BLI_array.hh:323
static AttributeOwner from_id(ID *id)
GMutableSpan take_back(const int64_t n) const
static IndexMask from_bools(Span< bool > bools, LinearAllocator<> &memory)
constexpr int64_t size() const
static constexpr IndexRange from_begin_size(const int64_t begin, const int64_t size)
void materialize(MutableSpan< T > r_span) const
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
GAttributeReader lookup(const StringRef name) const
bool add(const StringRef name, const AttrDomain domain, const AttrType data_type, const AttributeInit &initializer)
GSpanAttributeWriter lookup_or_add_for_write_span(StringRef name, AttrDomain domain, AttrType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
GAttributeWriter lookup_or_add_for_write(StringRef name, AttrDomain domain, AttrType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
GSpanAttributeWriter lookup_for_write_span(StringRef name)
constexpr bool is_empty() const
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
void fill_attribute_range_default(MutableAttributeAccessor dst_attributes, AttrDomain domain, const AttributeFilter &attribute_filter, IndexRange range)
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:69
BMesh Iterators.
void WM_main_add_notifier(uint type, void *reference)
static void mesh_remove_edges(Mesh *mesh, int len)
Definition mesh_data.cc:929
static wmOperatorStatus mesh_customdata_skin_add_exec(bContext *C, wmOperator *)
Definition mesh_data.cc:619
bool BM_data_layer_free_named(BMesh *bm, CustomData *data, StringRef name)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static SkinState mesh_customdata_skin_state(bContext *C)
Definition mesh_data.cc:592
static void reset_uvs_bmesh(BMFace *f, const int cd_loop_uv_offset)
Definition mesh_data.cc:93
void ED_mesh_edges_remove(Mesh *mesh, ReportList *reports, int count)
Definition mesh_data.cc:976
void MESH_OT_customdata_face_sets_clear(wmOperatorType *ot)
Definition mesh_data.cc:563
static wmOperatorStatus mesh_customdata_mask_clear_exec(bContext *C, wmOperator *)
Definition mesh_data.cc:498
bool BKE_attribute_remove(AttributeOwner &owner, StringRef name, struct ReportList *reports)
void ED_mesh_loops_add(Mesh *mesh, ReportList *reports, int count)
Definition mesh_data.cc:894
void ED_mesh_geometry_clear(Mesh *mesh)
static bool mesh_customdata_skin_add_poll(bContext *C)
Definition mesh_data.cc:614
@ RPT_WARNING
Definition BKE_report.hh:40
void BKE_id_attributes_default_color_set(struct ID *id, std::optional< StringRef > name)
int CustomData_number_of_layers(const CustomData *data, eCustomDataType type)
BMesh * BKE_editmesh_bmesh_get_for_write(Mesh *mesh)
Definition editmesh.cc:79
void ED_mesh_verts_add(Mesh *mesh, ReportList *reports, int count)
Definition mesh_data.cc:876
static wmOperatorStatus mesh_uv_texture_remove_exec(bContext *C, wmOperator *op)
Definition mesh_data.cc:423
bool CustomData_free_layers(CustomData *data, eCustomDataType type)
void CustomData_realloc(CustomData *data, int old_size, int new_size, eCDAllocType alloctype=CD_CONSTRUCT)
static void mesh_remove_verts(Mesh *mesh, int len)
Definition mesh_data.cc:918
void CustomData_free_elem(CustomData *data, int index, int count)
ATTR_WARN_UNUSED_RESULT const BMLoop * l
bool CustomData_has_layer(const CustomData *data, eCustomDataType type)
void MESH_OT_customdata_mask_clear(wmOperatorType *ot)
Definition mesh_data.cc:575
void ED_mesh_edges_add(Mesh *mesh, ReportList *reports, int count)
Definition mesh_data.cc:885
VArray< bool > ED_mesh_uv_map_pin_layer_get(const Mesh *mesh, int uv_index)
Definition mesh_data.cc:233
void ED_mesh_loops_remove(Mesh *mesh, ReportList *reports, int count)
Definition mesh_data.cc:990
void MESH_OT_customdata_custom_splitnormals_clear(wmOperatorType *ot)
Definition mesh_data.cc:748
static void mesh_add_verts(Mesh *mesh, int len)
Definition mesh_data.cc:766
void MESH_OT_customdata_custom_splitnormals_add(wmOperatorType *ot)
Definition mesh_data.cc:709
void ED_mesh_uv_ensure(Mesh *mesh, const char *name)
Definition mesh_data.cc:256
static void mesh_add_faces(Mesh *mesh, int len)
Definition mesh_data.cc:844
static wmOperatorStatus mesh_customdata_custom_splitnormals_add_exec(bContext *C, wmOperator *)
Definition mesh_data.cc:685
void BKE_mesh_tessface_clear(Mesh *mesh)
bool CustomData_free_layer_named(CustomData *data, StringRef name)
std::string BKE_attribute_calc_unique_name(const AttributeOwner &owner, StringRef name)
static void mesh_add_loops(Mesh *mesh, int len)
Definition mesh_data.cc:816
void ED_mesh_uv_loop_reset(bContext *C, Mesh *mesh)
Definition mesh_data.cc:147
static bool mesh_customdata_face_sets_clear_poll(bContext *C)
Definition mesh_data.cc:521
static void mesh_uv_reset_array(float **fuv, const int len)
Definition mesh_data.cc:53
static bool mesh_customdata_mask_clear_poll(bContext *C)
Definition mesh_data.cc:464
void ED_mesh_faces_add(Mesh *mesh, ReportList *reports, int count)
Definition mesh_data.cc:903
static wmOperatorStatus mesh_customdata_face_sets_clear_exec(bContext *C, wmOperator *)
Definition mesh_data.cc:540
KeyBlock * BKE_keyblock_find_by_index(Key *key, int index)
Definition key.cc:1425
static void mesh_remove_loops(Mesh *mesh, int len)
Definition mesh_data.cc:940
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
static bke::AttributeWriter< bool > ensure_corner_boolean_attribute(Mesh &mesh, const StringRef name)
Definition mesh_data.cc:241
void DEG_id_tag_update(ID *id, unsigned int flags)
static wmOperatorStatus mesh_customdata_skin_clear_exec(bContext *C, wmOperator *)
Definition mesh_data.cc:649
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
static VArray< bool > get_corner_boolean_attribute(const Mesh &mesh, const StringRef name)
Definition mesh_data.cc:227
@ OB_MODE_TEXTURE_PAINT
BMesh const char void * data
static bool uv_maps_poll(bContext *C)
Definition mesh_data.cc:362
static void mesh_remove_faces(Mesh *mesh, int len)
Definition mesh_data.cc:951
void BKE_lnor_spacearr_clear(MLoopNorSpaceArray *lnors_spacearr)
int CustomData_get_offset_named(const CustomData *data, eCustomDataType type, StringRef name)
static void mesh_add_edges(Mesh *mesh, int len)
Definition mesh_data.cc:791
void ED_mesh_split_faces(Mesh *mesh)
void BKE_mesh_runtime_clear_cache(Mesh *mesh)
static bool uv_texture_remove_poll(bContext *C)
Definition mesh_data.cc:370
void ED_mesh_faces_remove(Mesh *mesh, ReportList *reports, int count)
bool ED_operator_editable_mesh(bContext *C)
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)
BMDataLayerLookup BM_data_layer_lookup(const BMesh &bm, const StringRef name)
bool ED_mesh_color_ensure(Mesh *mesh, const char *name)
Definition mesh_data.cc:331
void ED_mesh_report_mirror_ex(ReportList &reports, int totmirr, int totfail, char selectmode)
std::optional< std::string > ED_mesh_color_add(Mesh *mesh, const char *name, bool active_set, bool do_init, ReportList *reports)
Definition mesh_data.cc:277
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void BKE_mesh_ensure_skin_customdata(Mesh *mesh)
bke::AttributeWriter< bool > ED_mesh_uv_map_pin_layer_ensure(Mesh *mesh, int uv_index)
Definition mesh_data.cc:248
void MESH_OT_uv_texture_add(wmOperatorType *ot)
Definition mesh_data.cc:411
void MESH_OT_uv_texture_remove(wmOperatorType *ot)
Definition mesh_data.cc:446
Mesh * ED_mesh_context(bContext *C)
void BM_data_layer_ensure_named(BMesh *bm, CustomData *data, int type, const StringRef name)
void ED_mesh_report_mirror(ReportList &reports, int totmirr, int totfail)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
VecBase< int16_t, 2 > short2
void BM_data_layer_copy(BMesh *bm, CustomData *data, int type, int src_n, int dst_n)
void BKE_id_attributes_active_color_set(struct ID *id, std::optional< StringRef > name)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
ID_Type GS(const char *name)
Definition DNA_ID.h:410
void MESH_OT_customdata_skin_add(wmOperatorType *ot)
Definition mesh_data.cc:632
static bool mesh_customdata_skin_clear_poll(bContext *C)
Definition mesh_data.cc:644
int CustomData_get_active_layer(const CustomData *data, eCustomDataType type)
const BMesh * BKE_editmesh_bmesh_get(const Object *ob)
Definition editmesh.cc:65
std::optional< std::string > ED_mesh_uv_add(Mesh *mesh, const char *name, bool active_set, bool do_init, ReportList *reports)
Definition mesh_data.cc:154
void MESH_OT_customdata_skin_clear(wmOperatorType *ot)
Definition mesh_data.cc:673
void BM_data_layer_add_named(BMesh *bm, CustomData *data, int type, const StringRef name)
Scene * CTX_data_scene(const bContext *C)
static void reset_uv_map(Mesh *mesh, const StringRef name)
Definition mesh_data.cc:118
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
void BM_uv_map_attr_pin_ensure_for_all_layers(BMesh *bm)
VecBase< float, 3 > float3
void CustomData_ensure_layers_are_mutable(CustomData *data, int totelem)
void ED_mesh_verts_remove(Mesh *mesh, ReportList *reports, int count)
Definition mesh_data.cc:962
static wmOperatorStatus mesh_customdata_custom_splitnormals_clear_exec(bContext *C, wmOperator *)
Definition mesh_data.cc:721
bool CustomData_has_layer_named(const CustomData *data, eCustomDataType type, StringRef name)
StringRef BKE_uv_map_pin_name_get(StringRef uv_map_name, char *buffer)
static wmOperatorStatus mesh_uv_texture_add_exec(bContext *C, wmOperator *op)
Definition mesh_data.cc:393
int CustomData_get_named_layer(const CustomData *data, eCustomDataType type, StringRef name)
PointerRNA CTX_data_pointer_get_type(const bContext *C, const char *member, StructRNA *type)
KeyBlock * ED_mesh_get_edit_shape_key(const Mesh *me)
static void reset_uvs_mesh(const IndexRange face, MutableSpan< float2 > uv_map)
Definition mesh_data.cc:107
ColorSceneLinearByteEncoded4b< eAlpha::Premultiplied > ColorGeometry4b
bool BKE_id_attributes_color_find(const struct ID *id, StringRef name)
@ Invalid
Definition types.hh:42
#define sinf
#define cosf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
bke::MeshRuntime * runtime
struct Key * key
std::shared_ptr< BMEditMesh > edit_mesh
struct MLoopNorSpaceArray * lnor_spacearr
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
struct ReportList * reports