Blender V5.3
bmesh_mesh_convert.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
73
74#include <algorithm>
75
76#include "DNA_key_types.h"
77#include "DNA_mesh_types.h"
78#include "DNA_meshdata_types.h"
79#include "DNA_modifier_types.h"
80#include "DNA_object_types.h"
81
82#include "MEM_guardedalloc.h"
83
84#include "BLI_array.hh"
85#include "BLI_index_range.hh"
86#include "BLI_listbase.hh"
87#include "BLI_math_vector_c.hh"
88#include "BLI_span.hh"
89#include "BLI_string.hh"
90#include "BLI_string_ref.hh"
91#include "BLI_task.hh"
92#include "BLI_vector.hh"
93
94#include "BKE_attribute.h"
95#include "BKE_attribute.hh"
97#include "BKE_attribute_math.hh"
98#include "BKE_customdata.hh"
99#include "BKE_mesh.hh"
100#include "BKE_mesh_runtime.hh"
101#include "BKE_multires.hh"
102
103#include "BKE_key.hh"
104#include "BKE_main.hh"
105
106#include "DEG_depsgraph_query.hh"
107
108#include "bmesh.hh"
109
110#include "CLG_log.h"
111
112namespace blender {
113
114static CLG_LogRef LOG = {"geom.bmesh.convert"};
115
116using bke::AttrDomain;
117
119{
120 if (name.is_empty()) {
121 return false;
122 }
123 if (name[0] == '.') {
124 return ELEM(name,
125 ".edge_verts",
126 ".corner_vert",
127 ".corner_edge",
128 ".hide_vert",
129 ".hide_edge",
130 ".hide_poly",
131 ".select_vert",
132 ".select_edge",
133 ".select_poly",
134 ".uv_select_vert",
135 ".uv_select_edge",
136 ".uv_select_face");
137 }
138 return ELEM(name, "position", "uv_seam", "material_index", "sharp_face", "sharp_edge");
139}
140
142 Span<int> face_verts,
143 Span<int> face_edges,
144 Span<BMVert *> vtable,
145 Span<BMEdge *> etable)
146{
147 const int size = face_verts.size();
150
151 for (const int i : IndexRange(size)) {
152 verts[i] = vtable[face_verts[i]];
153 edges[i] = etable[face_edges[i]];
154 }
155
156 return BM_face_create(&bm, verts.data(), edges.data(), size, nullptr, BM_CREATE_SKIP_CD);
157}
158
159static const CustomData &get_bm_custom_data(const BMesh &bm, const bke::AttrDomain domain)
160{
161 switch (domain) {
163 return bm.vdata;
165 return bm.edata;
167 return bm.pdata;
169 return bm.ldata;
170 default:
172 return bm.vdata;
173 }
174}
175
176static const CustomData &get_mesh_custom_data(const Mesh &mesh, const bke::AttrDomain domain)
177{
178 switch (domain) {
180 return mesh.vert_data;
182 return mesh.edge_data;
184 return mesh.face_data;
186 return mesh.corner_data;
187 default:
189 return mesh.vert_data;
190 }
191}
192
194{
195 return const_cast<CustomData &>(get_mesh_custom_data(const_cast<const Mesh &>(mesh), domain));
196}
197
207
212 const bke::AttrDomain domain,
213 CustomData &bm_data)
214{
215 const bke::AttributeStorage &storage = mesh.attribute_storage.wrap();
216 const CustomData &mesh_data = get_mesh_custom_data(mesh, domain);
217
219 std::array<int, CD_NUMTYPES> per_type_index;
220 per_type_index.fill(0);
221 for (const int i : IndexRange(bm_data.totlayer)) {
222 const CustomDataLayer &bm_layer = bm_data.layers[i];
223 const StringRef layer_name = bm_layer.name;
224 const eCustomDataType type = eCustomDataType(bm_layer.type);
225
227 info.type = type;
228 info.bmesh_offset = bm_layer.offset;
229 const bke::Attribute *attr = storage.lookup(layer_name);
230 if (attr && attr->domain() == domain) {
231 switch (attr->storage_type()) {
233 const auto &array_data = std::get<bke::Attribute::ArrayData>(attr->data());
234 info.mesh_data = array_data.data;
236 break;
237 }
239 const auto &single_data = std::get<bke::Attribute::SingleData>(attr->data());
240 info.mesh_data = single_data.value;
241 info.mesh_stride = 0;
242 break;
243 }
244 }
245 }
246 else {
247 const int mesh_layer_index =
248 layer_name.is_empty() ?
249 CustomData_get_layer_index_n(&mesh_data, type, per_type_index[type]) :
250 CustomData_get_named_layer_index(&mesh_data, type, layer_name);
251 if (mesh_layer_index != -1) {
253 info.mesh_data = mesh_data.layers[mesh_layer_index].data;
254 info.mesh_stride = CustomData_get_elem_size(&bm_layer);
255 }
256 }
257 infos.append(info);
258
259 per_type_index[type]++;
260 }
261 return infos;
262}
263
265 const Span<MeshToBMeshLayerInfo> copy_info,
266 const int mesh_index,
267 BMHeader &header)
268{
270 for (const MeshToBMeshLayerInfo &info : copy_info) {
271 if (info.mesh_data) {
273 POINTER_OFFSET(info.mesh_data, info.mesh_stride * mesh_index),
274 POINTER_OFFSET(header.data, info.bmesh_offset));
275 }
276 else {
277 CustomData_data_set_default_value(info.type, POINTER_OFFSET(header.data, info.bmesh_offset));
278 }
279 }
280}
281
283 const bke::AttrDomain domain,
284 const uint64_t cd_type_mask)
285{
286 CustomData custom_data;
287 CustomData_reset(&custom_data);
288 for (const bke::Attribute &attr : mesh.attribute_storage.wrap()) {
289 if (attr.domain() != domain) {
290 continue;
291 }
292 if (BM_attribute_stored_in_bmesh_builtin(attr.name())) {
293 continue;
294 }
295 const eCustomDataType data_type = *bke::attr_type_to_custom_data_type(attr.data_type());
296 if ((CD_TYPE_AS_MASK(data_type) & cd_type_mask) == 0) {
297 continue;
298 }
299 CustomData_add_layer_named(&custom_data, data_type, CD_SET_DEFAULT, 0, attr.name());
300 }
301 const CustomData &mesh_data = get_mesh_custom_data(mesh, domain);
302 for (const CustomDataLayer &layer : Span(mesh_data.layers, mesh_data.totlayer)) {
303 if ((CD_TYPE_AS_MASK(eCustomDataType(layer.type)) & cd_type_mask) == 0) {
304 continue;
305 }
307 &custom_data, eCustomDataType(layer.type), CD_SET_DEFAULT, 0, layer.name);
308 if (layer.type == CD_MDISPS && layer.flag & CD_FLAG_EXTERNAL) {
309 const int new_layer_i = CustomData_get_layer_index(&custom_data, layer.type);
310 BLI_assert(new_layer_i != -1);
311 CustomDataLayer &new_layer = custom_data.layers[new_layer_i];
312 new_layer.flag = layer.flag;
313 }
314 }
315 return custom_data;
316}
317
319{
320 if (!mesh) {
321 /* Sanity check. */
322 return;
323 }
324 const bool is_new = !(bm->totvert || (bm->vdata.totlayer || bm->edata.totlayer ||
326 KeyBlock *actkey;
327 float (*keyco)[3] = nullptr;
330
335
336 const CustomData &mesh_data = get_mesh_custom_data(*mesh, AttrDomain::Corner);
337 if (mesh_data.external) {
338 mesh_ldata.external = MEM_dupalloc(mesh_data.external);
339 }
340
341 Vector<std::string> temporary_layers_to_delete;
342
343 for (const int layer_index :
345 {
346 char buffer[MAX_CUSTOMDATA_LAYER_NAME];
347 {
349 CustomData_get_layer_name(&mesh_ldata, CD_PROP_FLOAT2, layer_index), buffer);
350 if (CustomData_get_named_layer_index(&mesh_ldata, CD_PROP_BOOL, name) < 0) {
352 &mesh_ldata, CD_PROP_BOOL, CD_SET_DEFAULT, mesh->corners_num, name);
353 temporary_layers_to_delete.append(std::string(name));
354 }
355 }
356 }
357
358 BLI_SCOPED_DEFER([&]() {
359 for (const std::string &name : temporary_layers_to_delete) {
360 CustomData_free_layer_named(&mesh_ldata, name);
361 }
362
363 MEM_SAFE_DELETE(mesh_ldata.external);
364
365 MEM_SAFE_DELETE(mesh_vdata.layers);
366 MEM_SAFE_DELETE(mesh_edata.layers);
367 MEM_SAFE_DELETE(mesh_pdata.layers);
368 MEM_SAFE_DELETE(mesh_ldata.layers);
369 });
370
371 if (mesh->verts_num == 0) {
372 if (is_new) {
373 /* No verts? still copy custom-data layout. */
374 CustomData_init_layout_from(&mesh_vdata, &bm->vdata, mask.vmask, CD_CONSTRUCT, 0);
375 CustomData_init_layout_from(&mesh_edata, &bm->edata, mask.emask, CD_CONSTRUCT, 0);
376 CustomData_init_layout_from(&mesh_pdata, &bm->pdata, mask.pmask, CD_CONSTRUCT, 0);
377 CustomData_init_layout_from(&mesh_ldata, &bm->ldata, mask.lmask, CD_CONSTRUCT, 0);
378
383 }
384 return;
385 }
386
387 Span<float3> vert_normals;
388 if (params->calc_vert_normal) {
389 vert_normals = mesh->vert_normals();
390 }
391
392 if (is_new) {
393 CustomData_init_layout_from(&mesh_vdata, &bm->vdata, mask.vmask, CD_SET_DEFAULT, 0);
394 CustomData_init_layout_from(&mesh_edata, &bm->edata, mask.emask, CD_SET_DEFAULT, 0);
395 CustomData_init_layout_from(&mesh_pdata, &bm->pdata, mask.pmask, CD_SET_DEFAULT, 0);
396 CustomData_init_layout_from(&mesh_ldata, &bm->ldata, mask.lmask, CD_SET_DEFAULT, 0);
397 }
398 else {
400 &mesh_vdata, &bm->vdata, mask.vmask, CD_SET_DEFAULT, bm, BM_VERT);
402 &mesh_edata, &bm->edata, mask.emask, CD_SET_DEFAULT, bm, BM_EDGE);
404 &mesh_pdata, &bm->pdata, mask.pmask, CD_SET_DEFAULT, bm, BM_FACE);
406 &mesh_ldata, &bm->ldata, mask.lmask, CD_SET_DEFAULT, bm, BM_LOOP);
407 }
408
409 {
410 const StringRef name = mesh->active_uv_map_name();
413 }
414 {
415 const StringRef name = mesh->default_uv_map_name();
418 }
419
420 /* -------------------------------------------------------------------- */
421 /* Shape Key */
422 int tot_shape_keys = 0;
423 if (mesh->key != nullptr && DEG_is_original(mesh)) {
424 /* Evaluated meshes can be topologically inconsistent with their shape keys.
425 * Shape keys are also already integrated into the state of the evaluated
426 * mesh, so considering them here would kind of apply them twice. */
427 tot_shape_keys = mesh->key->block.count();
428
429 /* Original meshes must never contain a shape-key custom-data layers.
430 *
431 * This may happen if and object's mesh data is accidentally
432 * set to the output from the modifier stack, causing it to be an "original" ID,
433 * even though the data isn't fully compatible (hence this assert).
434 *
435 * This results in:
436 * - The newly created #BMesh having twice the number of custom-data layers.
437 * - When converting the #BMesh back to a regular mesh,
438 * At least one of the extra shape-key blocks will be created in #Mesh.key
439 * depending on the value of #CustomDataLayer.uid.
440 *
441 * We could support mixing both kinds of data if there is a compelling use-case for it.
442 * At the moment it's simplest to assume all original meshes use the key-block and meshes
443 * that are evaluated (through the modifier stack for example) use custom-data layers.
444 */
446 }
447 if (is_new == false) {
448 tot_shape_keys = min_ii(tot_shape_keys, CustomData_number_of_layers(&bm->vdata, CD_SHAPEKEY));
449 }
450 Array<const float (*)[3], 32> shape_key_table_buf(tot_shape_keys);
451 const float (**shape_key_table)[3] = tot_shape_keys ? shape_key_table_buf.data() : nullptr;
452
453 if ((params->active_shapekey != 0) && tot_shape_keys > 0) {
454 actkey = static_cast<KeyBlock *>(BLI_findlink(&mesh->key->block, params->active_shapekey - 1));
455 }
456 else {
457 actkey = nullptr;
458 }
459
460 if (is_new) {
461 if (tot_shape_keys || params->add_key_index) {
463 }
464 }
465
466 if (tot_shape_keys) {
467 if (is_new) {
468 /* Check if we need to generate unique ids for the shape-keys.
469 * This also exists in the file reading code, but is here for a sanity check. */
470 if (!mesh->key->uidgen) {
471 fprintf(stderr,
472 "%s had to generate shape key uid's in a situation we shouldn't need to! "
473 "(bmesh internal error)\n",
474 __func__);
475
476 mesh->key->uidgen = 1;
477 for (KeyBlock &block : mesh->key->block) {
478 block.uid = mesh->key->uidgen++;
479 }
480 }
481 }
482
483 if (actkey && actkey->totelem == mesh->verts_num) {
484 keyco = params->use_shapekey ? static_cast<float (*)[3]>(actkey->data) : nullptr;
485 if (is_new) {
486 bm->shapenr = params->active_shapekey;
487 }
488 }
489
490 int i;
491 KeyBlock *block;
492 for (i = 0, block = mesh->key->block.first(); i < tot_shape_keys; block = block->next, i++) {
493 if (is_new) {
496 bm->vdata.layers[j].uid = block->uid;
497 }
498 shape_key_table[i] = static_cast<const float (*)[3]>(block->data);
499 }
500 }
501
510 if (is_new) {
515 }
516
517 /* Only copy these values over if the source mesh is flagged to be using them.
518 * Even if `bm` has these layers, they may have been added from another mesh, when `!is_new`. */
519 const int cd_shape_key_offset = tot_shape_keys ? CustomData_get_offset(&bm->vdata, CD_SHAPEKEY) :
520 -1;
521 const int cd_shape_keyindex_offset = is_new && (tot_shape_keys || params->add_key_index) ?
523 -1;
524
525 const bke::AttributeAccessor attributes = mesh->attributes();
526 const VArraySpan select_vert = *attributes.lookup<bool>(".select_vert", AttrDomain::Point);
527 const VArraySpan select_edge = *attributes.lookup<bool>(".select_edge", AttrDomain::Edge);
528 const VArraySpan select_poly = *attributes.lookup<bool>(".select_poly", AttrDomain::Face);
529 const VArraySpan hide_vert = *attributes.lookup<bool>(".hide_vert", AttrDomain::Point);
530 const VArraySpan hide_edge = *attributes.lookup<bool>(".hide_edge", AttrDomain::Edge);
531 const VArraySpan hide_poly = *attributes.lookup<bool>(".hide_poly", AttrDomain::Face);
532 const VArraySpan material_indices = *attributes.lookup<int>("material_index", AttrDomain::Face);
533 const VArraySpan sharp_faces = *attributes.lookup<bool>("sharp_face", AttrDomain::Face);
534 const VArraySpan sharp_edges = *attributes.lookup<bool>("sharp_edge", AttrDomain::Edge);
535 const VArraySpan uv_seams = *attributes.lookup<bool>("uv_seam", AttrDomain::Edge);
536
537 const VArraySpan uv_select_vert = *attributes.lookup<bool>(".uv_select_vert",
538 AttrDomain::Corner);
539 const VArraySpan uv_select_edge = *attributes.lookup<bool>(".uv_select_edge",
540 AttrDomain::Corner);
541 const VArraySpan uv_select_face = *attributes.lookup<bool>(".uv_select_face", AttrDomain::Face);
542
543 const bool need_uv_select = is_new && (!uv_select_vert.is_empty() &&
544 !uv_select_edge.is_empty() && !uv_select_face.is_empty());
545
546 const Span<float3> positions = mesh->vert_positions();
547 Array<BMVert *> vtable(mesh->verts_num);
548 for (const int i : positions.index_range()) {
549 BMVert *v = vtable[i] = BM_vert_create(
550 bm, keyco ? keyco[i] : positions[i], nullptr, BM_CREATE_SKIP_CD);
551 BM_elem_index_set(v, i); /* set_ok */
552
553 if (!hide_vert.is_empty() && hide_vert[i]) {
555 }
556 if (!select_vert.is_empty() && select_vert[i]) {
557 BM_vert_select_set(bm, v, true);
558 }
559
560 if (!vert_normals.is_empty()) {
561 copy_v3_v3(v->no, vert_normals[i]);
562 }
563
565
566 /* Set shape key original index. */
567 if (cd_shape_keyindex_offset != -1) {
568 BM_ELEM_CD_SET_INT(v, cd_shape_keyindex_offset, i);
569 }
570
571 /* Set shape-key data. */
572 if (tot_shape_keys) {
573 float (*co_dst)[3] = static_cast<float (*)[3]> BM_ELEM_CD_GET_VOID_P(v, cd_shape_key_offset);
574 for (int j = 0; j < tot_shape_keys; j++, co_dst++) {
575 copy_v3_v3(*co_dst, shape_key_table[j][i]);
576 }
577 }
578 }
579 if (is_new) {
580 bm->elem_index_dirty &= ~BM_VERT; /* Added in order, clear dirty flag. */
581 }
582
583 const Span<int2> edges = mesh->edges();
584 Array<BMEdge *> etable(mesh->edges_num);
585 for (const int i : edges.index_range()) {
586 BMEdge *e = etable[i] = BM_edge_create(
587 bm, vtable[edges[i][0]], vtable[edges[i][1]], nullptr, BM_CREATE_SKIP_CD);
588 BM_elem_index_set(e, i); /* set_ok */
589
590 e->head.hflag = 0;
591 if (!uv_seams.is_empty() && uv_seams[i]) {
593 }
594 if (!hide_edge.is_empty() && hide_edge[i]) {
596 }
597 if (!select_edge.is_empty() && select_edge[i]) {
598 BM_edge_select_set(bm, e, true);
599 }
600 if (!(!sharp_edges.is_empty() && sharp_edges[i])) {
602 }
603
605 }
606 if (is_new) {
607 bm->elem_index_dirty &= ~BM_EDGE; /* Added in order, clear dirty flag. */
608 }
609
610 const OffsetIndices faces = mesh->faces();
611 const Span<int> corner_verts = mesh->corner_verts();
612 const Span<int> corner_edges = mesh->corner_edges();
613
614 /* Only needed for selection. */
615
616 Array<BMFace *> ftable;
617 if (mesh->mselect && mesh->totselect != 0) {
618 ftable.reinitialize(mesh->faces_num);
619 }
620
621 int totloops = 0;
622 for (const int i : faces.index_range()) {
623 const IndexRange face = faces[i];
625 *bm, corner_verts.slice(face), corner_edges.slice(face), vtable, etable);
626 if (!ftable.is_empty()) {
627 ftable[i] = f;
628 }
629
630 if (f == nullptr) [[unlikely]] {
631 printf(
632 "%s: Warning! Bad face in mesh"
633 " \"%s\" at index %d!, skipping\n",
634 __func__,
635 mesh->id.name + 2,
636 i);
637 continue;
638 }
639
640 /* Don't use 'i' since we may have skipped the face. */
641 BM_elem_index_set(f, bm->totface - 1); /* set_ok */
642
643 /* Transfer flag. */
644 if (!(!sharp_faces.is_empty() && sharp_faces[i])) {
646 }
647 if (!hide_poly.is_empty() && hide_poly[i]) {
649 }
650 if (!select_poly.is_empty() && select_poly[i]) {
651 BM_face_select_set(bm, f, true);
652 }
653
654 f->mat_nr = material_indices.is_empty() ? 0 : material_indices[i];
655 if (i == mesh->act_face) {
656 bm->act_face = f;
657 }
658
659 int j = face.start();
660 BMLoop *l_first = BM_FACE_FIRST_LOOP(f);
661 BMLoop *l_iter = l_first;
662 do {
663 /* Don't use 'j' since we may have skipped some faces, hence some loops. */
664 BM_elem_index_set(l_iter, totloops++); /* set_ok */
665
666 mesh_attributes_copy_to_bmesh_block(bm->ldata, loop_info, j, l_iter->head);
667
668 if (need_uv_select) {
669 if (uv_select_vert[j]) {
671 }
672 if (uv_select_edge[j]) {
674 }
675 }
676
677 j++;
678 } while ((l_iter = l_iter->next) != l_first);
679
681
682 if (need_uv_select) {
683 if (uv_select_face[i]) {
685 }
686 }
687
688 if (params->calc_face_normal) {
690 }
691 }
692 if (is_new) {
693 bm->elem_index_dirty &= ~(BM_FACE | BM_LOOP); /* Added in order, clear dirty flag. */
694 }
695
696 bm->uv_select_sync_valid = (need_uv_select && (mesh->flag & ME_FLAG_UV_SELECT_SYNC_VALID)) != 0;
697
698 /* -------------------------------------------------------------------- */
699 /* MSelect clears the array elements (to avoid adding multiple times).
700 *
701 * Take care to keep this last and not use (v/e/ftable) after this.
702 */
703
704 if (mesh->mselect && mesh->totselect != 0) {
705 for (const int i : IndexRange(mesh->totselect)) {
706 const MSelect &msel = mesh->mselect[i];
707
708 BMElem **ele_p;
709 switch (msel.type) {
710 case ME_VSEL:
711 ele_p = reinterpret_cast<BMElem **>(&vtable[msel.index]);
712 break;
713 case ME_ESEL:
714 ele_p = reinterpret_cast<BMElem **>(&etable[msel.index]);
715 break;
716 case ME_FSEL:
717 ele_p = reinterpret_cast<BMElem **>(&ftable[msel.index]);
718 break;
719 default:
720 continue;
721 }
722
723 if (*ele_p != nullptr) {
725 *ele_p = nullptr;
726 }
727 }
728 }
729 else {
731 }
732}
733
737static BMVert **bm_to_mesh_vertex_map(BMesh *bm, const int old_verts_num)
738{
739 const int cd_shape_keyindex_offset = CustomData_get_offset(&bm->vdata, CD_SHAPE_KEYINDEX);
740 BMVert **vertMap = nullptr;
741 BMVert *eve;
742 int i = 0;
743 BMIter iter;
744
745 /* Caller needs to ensure this. */
746 BLI_assert(old_verts_num > 0);
747
748 vertMap = MEM_new_array_zeroed<BMVert *>(old_verts_num, "vertMap");
749 if (cd_shape_keyindex_offset != -1) {
750 BM_ITER_MESH_INDEX (eve, &iter, bm, BM_VERTS_OF_MESH, i) {
751 const int keyi = BM_ELEM_CD_GET_INT(eve, cd_shape_keyindex_offset);
752 if ((keyi != ORIGINDEX_NONE) && (keyi < old_verts_num) &&
753 /* Not fool-proof, but chances are if we have many verts with the same index,
754 * we will want to use the first one,
755 * since the second is more likely to be a duplicate. */
756 (vertMap[keyi] == nullptr))
757 {
758 vertMap[keyi] = eve;
759 }
760 }
761 }
762 else {
763 BM_ITER_MESH_INDEX (eve, &iter, bm, BM_VERTS_OF_MESH, i) {
764 if (i < old_verts_num) {
765 vertMap[i] = eve;
766 }
767 else {
768 break;
769 }
770 }
771 }
772
773 return vertMap;
774}
775
776/* -------------------------------------------------------------------- */
856
862{
863 int i;
864 int j = 0;
865
866 for (i = 0; i < bm->vdata.totlayer; i++) {
867 if (bm->vdata.layers[i].type == CD_SHAPEKEY) {
868 if (currkey->uid == bm->vdata.layers[i].uid) {
869 return j;
870 }
871 j++;
872 }
873 }
874 return -1;
875}
876
890 Key *key,
891 MutableSpan<float3> positions,
892 const bool active_shapekey_to_mvert)
893{
894 KeyBlock *actkey = static_cast<KeyBlock *>(BLI_findlink(&key->block, bm->shapenr - 1));
895
896 /* It's unlikely this ever remains false, check for correctness. */
897 bool actkey_has_layer = false;
898
899 /* Go through and find any shape-key custom-data layers
900 * that might not have corresponding KeyBlocks, and add them if necessary. */
901 for (int i = 0; i < bm->vdata.totlayer; i++) {
902 if (bm->vdata.layers[i].type != CD_SHAPEKEY) {
903 continue;
904 }
905
906 KeyBlock *currkey;
907 for (currkey = key->block.first(); currkey; currkey = currkey->next) {
908 if (currkey->uid == bm->vdata.layers[i].uid) {
909 break;
910 }
911 }
912
913 if (currkey) {
914 if (currkey == actkey) {
915 actkey_has_layer = true;
916 }
917 }
918 else {
919 currkey = BKE_keyblock_add(key, bm->vdata.layers[i].name);
920 currkey->uid = bm->vdata.layers[i].uid;
921 }
922 }
923
924 const int cd_shape_keyindex_offset = CustomData_get_offset(&bm->vdata, CD_SHAPE_KEYINDEX);
925 BMIter iter;
926 BMVert *eve;
927 float (*ofs)[3] = nullptr;
928 std::optional<Array<bool>> dependent;
929
930 /* Editing the basis key updates others. */
931 if ((key->type == KEY_RELATIVE) &&
932 /* The shape-key coordinates used from entering edit-mode are used. */
933 (actkey_has_layer == true) &&
934 /* Original key-indices are only used to check the vertex existed when entering edit-mode. */
935 (cd_shape_keyindex_offset != -1) &&
936 /* Offsets are only needed if the current shape is a basis for others. */
937 (dependent = BKE_keyblock_get_dependent_keys(key, bm->shapenr - 1)).has_value())
938 {
939
940 BLI_assert(actkey != nullptr); /* Assured by `actkey_has_layer` check. */
941 const int actkey_uuid = bm_to_mesh_shape_layer_index_from_kb(bm, actkey);
942
943 /* Since `actkey_has_layer == true`, this must never fail. */
944 BLI_assert(actkey_uuid != -1);
945
946 const int cd_shape_offset = CustomData_get_n_offset(&bm->vdata, CD_SHAPEKEY, actkey_uuid);
947
949 int i;
950 BM_ITER_MESH_INDEX (eve, &iter, bm, BM_VERTS_OF_MESH, i) {
951 const int keyi = BM_ELEM_CD_GET_INT(eve, cd_shape_keyindex_offset);
952 /* Check the vertex existed when entering edit-mode (otherwise don't apply an offset). */
953 if (keyi != ORIGINDEX_NONE) {
954 float *co_orig = static_cast<float *> BM_ELEM_CD_GET_VOID_P(eve, cd_shape_offset);
955 /* Could use 'eve->co' or the destination position, they're the same at this point. */
956 sub_v3_v3v3(ofs[i], eve->co, co_orig);
957 }
958 else {
959 /* If there are new vertices in the mesh, we can't propagate the offset
960 * because it will only work for the existing vertices and not the new
961 * ones, creating a mess when doing e.g. subdivide + translate. */
962 MEM_delete(ofs);
963 ofs = nullptr;
964 dependent.reset();
965 break;
966 }
967 }
968 }
969
970 /* Without this, the real mesh coordinates (uneditable) as soon as you create the Basis shape.
971 * while users might not notice since the shape-key is applied in the viewport,
972 * exporters for example may still use the underlying coordinates, see: #30771 & #96135.
973 *
974 * Needed when editing any shape that isn't the (`key->refkey`), the vertices in mesh positions
975 * currently have vertex coordinates set from the current-shape (initialized from #BMVert.co).
976 * In this case it's important to overwrite these coordinates with the basis-keys coordinates. */
977 bool update_vertex_coords_from_refkey = false;
978 int cd_shape_offset_refkey = -1;
979 if (active_shapekey_to_mvert == false) {
980 if ((actkey != key->refkey) && (cd_shape_keyindex_offset != -1)) {
981 const int refkey_uuid = bm_to_mesh_shape_layer_index_from_kb(bm, key->refkey);
982 if (refkey_uuid != -1) {
983 cd_shape_offset_refkey = CustomData_get_n_offset(&bm->vdata, CD_SHAPEKEY, refkey_uuid);
984 if (cd_shape_offset_refkey != -1) {
985 update_vertex_coords_from_refkey = true;
986 }
987 }
988 }
989 }
990
991 for (const auto [currkey_i, currkey] : key->block.enumerate()) {
992 int keyi;
993 float (*currkey_data)[3];
994
995 const int currkey_uuid = bm_to_mesh_shape_layer_index_from_kb(bm, &currkey);
996 const int cd_shape_offset = (currkey_uuid == -1) ?
997 -1 :
999
1000 /* Common case, the layer data is available, use it where possible. */
1001 if (cd_shape_offset != -1) {
1002 const bool apply_offset = (ofs != nullptr) && (&currkey != actkey) &&
1003 (*dependent)[currkey_i];
1004
1005 if (currkey.data && (currkey.totelem == bm->totvert)) {
1006 /* Use memory in-place. */
1007 }
1008 else {
1009 currkey.data = MEM_realloc_uninitialized(currkey.data, key->elemsize * bm->totvert);
1010 currkey.totelem = bm->totvert;
1011 }
1012 currkey_data = static_cast<float (*)[3]>(currkey.data);
1013
1014 int i;
1015 BM_ITER_MESH_INDEX (eve, &iter, bm, BM_VERTS_OF_MESH, i) {
1016 float *co_orig = static_cast<float *> BM_ELEM_CD_GET_VOID_P(eve, cd_shape_offset);
1017
1018 if (&currkey == actkey) {
1019 copy_v3_v3(currkey_data[i], eve->co);
1020
1021 if (update_vertex_coords_from_refkey) {
1022 BLI_assert(actkey != key->refkey);
1023 keyi = BM_ELEM_CD_GET_INT(eve, cd_shape_keyindex_offset);
1024 if (keyi != ORIGINDEX_NONE) {
1025 float *co_refkey = static_cast<float *> BM_ELEM_CD_GET_VOID_P(
1026 eve, cd_shape_offset_refkey);
1027 copy_v3_v3(positions[i], co_refkey);
1028 }
1029 }
1030 }
1031 else {
1032 copy_v3_v3(currkey_data[i], co_orig);
1033 }
1034
1035 /* Propagate edited basis offsets to other shapes. */
1036 if (apply_offset) {
1037 add_v3_v3(currkey_data[i], ofs[i]);
1038 }
1039
1040 /* Apply back new coordinates shape-keys that have offset into #BMesh.
1041 * Otherwise, in case we call again #BM_mesh_bm_to_me on same #BMesh,
1042 * we'll apply diff from previous call to #BM_mesh_bm_to_me,
1043 * to shape-key values from original creation of the #BMesh. See #50524. */
1044 copy_v3_v3(co_orig, currkey_data[i]);
1045 }
1046 }
1047 else {
1048 /* No original layer data, use fallback information. */
1049 if (currkey.data && (cd_shape_keyindex_offset != -1)) {
1050 CLOG_WARN(&LOG,
1051 "Found shape-key but no CD_SHAPEKEY layers to read from, "
1052 "using existing shape-key data where possible");
1053 }
1054 else {
1055 CLOG_WARN(&LOG,
1056 "Found shape-key but no CD_SHAPEKEY layers to read from, "
1057 "using basis shape-key data");
1058 }
1059
1060 currkey_data = static_cast<float (*)[3]>(
1061 MEM_new_uninitialized(key->elemsize * bm->totvert, "currkey->data"));
1062
1063 int i;
1064 BM_ITER_MESH_INDEX (eve, &iter, bm, BM_VERTS_OF_MESH, i) {
1065
1066 if ((currkey.data != nullptr) && (cd_shape_keyindex_offset != -1) &&
1067 ((keyi = BM_ELEM_CD_GET_INT(eve, cd_shape_keyindex_offset)) != ORIGINDEX_NONE) &&
1068 (keyi < currkey.totelem))
1069 {
1070 /* Reconstruct keys via vertices original key indices.
1071 * WARNING(@ideasman42): `currkey->data` is known to be unreliable as the edit-mesh
1072 * coordinates may be flushed back to the shape-key when exporting or rendering.
1073 * This is a last resort! If this branch is running as part of regular usage
1074 * it can be considered a bug. */
1075 const float (*oldkey)[3] = static_cast<const float (*)[3]>(currkey.data);
1076 copy_v3_v3(currkey_data[i], oldkey[keyi]);
1077 }
1078 else {
1079 /* Fail! fill in with dummy value. */
1080 copy_v3_v3(currkey_data[i], eve->co);
1081 }
1082 }
1083
1084 currkey.totelem = bm->totvert;
1085 if (currkey.data) {
1086 MEM_delete_void(currkey.data);
1087 }
1088 currkey.data = currkey_data;
1089 }
1090 }
1091
1092 MEM_SAFE_DELETE(ofs);
1093}
1094
1096
1098{
1099 (void)bm; /* Unused in the release builds. */
1103
1104 /* The "hide" attributes are stored as flags on #BMesh. */
1108 /* The "selection" attributes are stored as flags on #BMesh. */
1112}
1113
1115 Mesh &mesh,
1116 BMesh &bm,
1117 const int old_totvert)
1118{
1119 BMVert **vertMap = nullptr;
1120 BMVert *eve;
1121
1122 for (Object &ob : bmain.objects) {
1123 if ((ob.parent) && (ob.parent->data == id_cast<ID *>(&mesh)) &&
1125 {
1126
1127 if (vertMap == nullptr) {
1128 vertMap = bm_to_mesh_vertex_map(&bm, old_totvert);
1129 }
1130
1131 if (ob.par1 < old_totvert) {
1132 eve = vertMap[ob.par1];
1133 if (eve) {
1134 ob.par1 = BM_elem_index_get(eve);
1135 }
1136 }
1137 if (ob.par2 < old_totvert) {
1138 eve = vertMap[ob.par2];
1139 if (eve) {
1140 ob.par2 = BM_elem_index_get(eve);
1141 }
1142 }
1143 if (ob.par3 < old_totvert) {
1144 eve = vertMap[ob.par3];
1145 if (eve) {
1146 ob.par3 = BM_elem_index_get(eve);
1147 }
1148 }
1149 }
1150 if (ob.data == id_cast<ID *>(&mesh)) {
1151 for (ModifierData &md : ob.modifiers) {
1152 if (md.type == eModifierType_Hook) {
1153 HookModifierData *hmd = reinterpret_cast<HookModifierData *>(&md);
1154
1155 if (vertMap == nullptr) {
1156 vertMap = bm_to_mesh_vertex_map(&bm, old_totvert);
1157 }
1158 int i, j;
1159 for (i = j = 0; i < hmd->indexar_num; i++) {
1160 if (hmd->indexar[i] < old_totvert) {
1161 eve = vertMap[hmd->indexar[i]];
1162
1163 if (eve) {
1164 hmd->indexar[j++] = BM_elem_index_get(eve);
1165 }
1166 }
1167 else {
1168 j++;
1169 }
1170 }
1171
1172 hmd->indexar_num = j;
1173 }
1174 }
1175 }
1176 }
1177
1178 if (vertMap) {
1179 MEM_delete(vertMap);
1180 }
1181}
1182
1192
1197 const bke::AttrDomain domain,
1198 Mesh &mesh)
1199{
1200 bke::AttributeStorage &storage = mesh.attribute_storage.wrap();
1201 CustomData &mesh_data = get_mesh_custom_data(mesh, domain);
1203 std::array<int, CD_NUMTYPES> per_type_index;
1204 per_type_index.fill(0);
1205 for (const int i : IndexRange(mesh_data.totlayer)) {
1206 const CustomDataLayer &mesh_layer = mesh_data.layers[i];
1207 const eCustomDataType type = eCustomDataType(mesh_layer.type);
1208 const int bm_layer_index =
1209 mesh_layer.name[0] == '\0' ?
1210 CustomData_get_layer_index_n(&bm_data, type, per_type_index[type]) :
1211 CustomData_get_named_layer_index(&bm_data, type, mesh_layer.name);
1212
1213 /* Skip layers that don't exist in `bm_data` or are explicitly set to not be
1214 * copied. The layers are either set separately or shouldn't exist on the mesh. */
1215 if (bm_layer_index == -1) {
1216 continue;
1217 }
1218 const CustomDataLayer &bm_layer = bm_data.layers[bm_layer_index];
1219 if (bm_layer.flag & CD_FLAG_NOCOPY) {
1220 continue;
1221 }
1222
1223 BMeshToMeshLayerInfo info{};
1224 info.type = type;
1225 info.bmesh_offset = bm_layer.offset;
1226 info.mesh_data = mesh_layer.data;
1227 info.elem_size = CustomData_get_elem_size(&mesh_layer);
1228 infos.append(info);
1229
1230 per_type_index[type]++;
1231 }
1232 for (bke::Attribute &attr : storage) {
1233 if (attr.domain() != domain) {
1234 continue;
1235 }
1236 const eCustomDataType cd_type = *bke::attr_type_to_custom_data_type(attr.data_type());
1237 const int bm_layer_index = CustomData_get_named_layer_index(&bm_data, cd_type, attr.name());
1238 if (bm_layer_index == -1) {
1239 continue;
1240 }
1241 const CustomDataLayer &bm_layer = bm_data.layers[bm_layer_index];
1242 if (bm_layer.flag & CD_FLAG_NOCOPY) {
1243 continue;
1244 }
1245 BMeshToMeshLayerInfo info{};
1246 info.type = cd_type;
1247 info.bmesh_offset = bm_layer.offset;
1248 info.mesh_data = std::get<bke::Attribute::ArrayData>(attr.data_for_write()).data;
1249 info.elem_size = bke::attribute_type_to_cpp_type(attr.data_type()).size;
1250 infos.append(info);
1251 }
1252 return infos;
1253}
1254
1257 bool &need_select_vert,
1258 bool &need_hide_vert)
1259{
1260 char hflag = 0;
1261 BMIter iter;
1262 int i;
1263 BMVert *vert;
1264 BM_ITER_MESH_INDEX (vert, &iter, &bm, BM_VERTS_OF_MESH, i) {
1265 BM_elem_index_set(vert, i); /* set_inline */
1266 table[i] = vert;
1267 hflag |= vert->head.hflag;
1268 }
1269 need_select_vert = (hflag & BM_ELEM_SELECT) != 0;
1270 need_hide_vert = (hflag & BM_ELEM_HIDDEN) != 0;
1271}
1272
1275 bool &need_select_edge,
1276 bool &need_hide_edge,
1277 bool &need_sharp_edge,
1278 bool &need_uv_seams)
1279{
1280 char hflag = 0;
1281 BMIter iter;
1282 int i;
1283 BMEdge *edge;
1284 BM_ITER_MESH_INDEX (edge, &iter, &bm, BM_EDGES_OF_MESH, i) {
1285 BM_elem_index_set(edge, i); /* set_inline */
1286 table[i] = edge;
1287 hflag |= edge->head.hflag;
1288 need_sharp_edge |= (edge->head.hflag & BM_ELEM_SMOOTH) == 0;
1289 }
1290 need_select_edge = (hflag & BM_ELEM_SELECT) != 0;
1291 need_hide_edge = (hflag & BM_ELEM_HIDDEN) != 0;
1292 need_uv_seams = (hflag & BM_ELEM_SEAM) != 0;
1293}
1294
1302 MutableSpan<const BMFace *> face_table,
1303 MutableSpan<const BMLoop *> loop_table,
1304 bool &need_select_poly,
1305 bool &need_hide_poly,
1306 bool &need_sharp_face,
1307 bool &need_material_index,
1308 Vector<int> &loop_layers_not_to_copy)
1309{
1310 const CustomData &ldata = bm.ldata;
1311 Vector<int> pin_layers;
1312 for (const int i : IndexRange(CustomData_number_of_layers(&ldata, CD_PROP_FLOAT2))) {
1313 char const *layer_name = CustomData_get_layer_name(&ldata, CD_PROP_FLOAT2, i);
1314 char sub_layer_name[MAX_CUSTOMDATA_LAYER_NAME];
1315 auto add_bool_layer = [&](Vector<int> &layers, const StringRef name) {
1316 const int layer_index = CustomData_get_named_layer_index(&ldata, CD_PROP_BOOL, name);
1317 if (layer_index != -1) {
1318 layers.append(layer_index);
1319 }
1320 };
1321 add_bool_layer(pin_layers, BKE_uv_map_pin_name_get(layer_name, sub_layer_name));
1322 }
1323 Array<int> pin_offsets(pin_layers.size());
1324 for (const int i : pin_layers.index_range()) {
1325 pin_offsets[i] = ldata.layers[pin_layers[i]].offset;
1326 }
1327
1328 Array<bool> need_pin(pin_layers.size(), false);
1329 char hflag = 0;
1330 BMIter iter;
1331 int face_i = 0;
1332 int loop_i = 0;
1333 BMFace *face;
1334 BM_ITER_MESH_INDEX (face, &iter, &bm, BM_FACES_OF_MESH, face_i) {
1335 BM_elem_index_set(face, face_i); /* set_inline */
1336 face_table[face_i] = face;
1337 hflag |= face->head.hflag;
1338 need_sharp_face |= (face->head.hflag & BM_ELEM_SMOOTH) == 0;
1339 need_material_index |= face->mat_nr != 0;
1340
1341 BMLoop *loop = BM_FACE_FIRST_LOOP(face);
1342 for ([[maybe_unused]] const int i : IndexRange(face->len)) {
1343 BM_elem_index_set(loop, loop_i); /* set_inline */
1344 loop_table[loop_i] = loop;
1345 for (const int i : pin_offsets.index_range()) {
1346 if (BM_ELEM_CD_GET_BOOL(loop, pin_offsets[i])) {
1347 need_pin[i] = true;
1348 }
1349 }
1350 loop = loop->next;
1351 loop_i++;
1352 }
1353 }
1354 need_select_poly = (hflag & BM_ELEM_SELECT) != 0;
1355 need_hide_poly = (hflag & BM_ELEM_HIDDEN) != 0;
1356
1357 for (const int i : pin_layers.index_range()) {
1358 if (!need_pin[i]) {
1359 loop_layers_not_to_copy.append(pin_layers[i]);
1360 }
1361 }
1362}
1363
1365 const int mesh_index,
1366 const void *block)
1367{
1368 for (const BMeshToMeshLayerInfo &info : copy_info) {
1370 POINTER_OFFSET(block, info.bmesh_offset),
1371 POINTER_OFFSET(info.mesh_data, info.elem_size * mesh_index));
1372 }
1373}
1374
1377 Array<GSpan, 8> spans_;
1378 Array<std::atomic<bool>, 8> can_be_single_;
1379
1380 public:
1382 const bke::AttrDomain domain,
1383 const Set<StringRef> &skip_names)
1384 {
1385 for (bke::Attribute &attr : storage) {
1386 if (attr.domain() != domain) {
1387 continue;
1388 }
1389 if (skip_names.contains(attr.name())) {
1390 continue;
1391 }
1392 attrs_.append(&attr);
1393 }
1394 spans_.reinitialize(attrs_.size());
1395 can_be_single_.reinitialize(attrs_.size());
1396 for (const int attr_i : attrs_.index_range()) {
1397 BLI_assert(attrs_[attr_i]->storage_type() == bke::AttrStorageType::Array);
1398 const auto &data = std::get<bke::Attribute::ArrayData>(attrs_[attr_i]->data());
1399 const CPPType &type = bke::attribute_type_to_cpp_type(attrs_[attr_i]->data_type());
1400 spans_[attr_i] = GSpan(type, data.data, data.size);
1401 can_be_single_[attr_i] = attrs_[attr_i]->data_type() != bke::AttrType::String;
1402 }
1403 }
1404
1409 void check_range(const IndexRange range)
1410 {
1411 for (const int attr_i : attrs_.index_range()) {
1412 if (!can_be_single_[attr_i].load(std::memory_order_relaxed)) {
1413 continue;
1414 }
1415 bke::attribute_math::to_static_type(attrs_[attr_i]->data_type(), [&]<typename T>() {
1416 const Span<T> data = spans_[attr_i].typed<T>();
1417 const T &first = data.first();
1418 if (std::any_of(range.begin(), range.end(), [&](const int i) { return data[i] != first; }))
1419 {
1420 can_be_single_[attr_i].store(false, std::memory_order_relaxed);
1421 }
1422 });
1423 }
1424 }
1425
1428 {
1429 for (const int attr_i : attrs_.index_range()) {
1430 if (spans_[attr_i].is_empty()) {
1431 continue;
1432 }
1433 if (can_be_single_[attr_i]) {
1434 const GPointer value(spans_[attr_i].type(), spans_[attr_i][0]);
1435 attrs_[attr_i]->assign_data(bke::Attribute::SingleData::from_value(value));
1436 }
1437 }
1438 }
1439};
1440
1442 const Span<const BMVert *> bm_verts,
1443 const Span<BMeshToMeshLayerInfo> copy_info,
1444 AttrSingleValueChecker &single_checker,
1445 MutableSpan<bool> select_vert,
1446 MutableSpan<bool> hide_vert)
1447{
1448 MutableSpan<float3> dst_vert_positions = mesh.vert_positions_for_write();
1449
1450 std::atomic<bool> any_loose_vert = false;
1451 const auto process_verts = [&](const IndexRange range) {
1452 bool any_loose_vert_local = false;
1453 for (const int vert_i : range) {
1454 const BMVert &src_vert = *bm_verts[vert_i];
1455 copy_v3_v3(dst_vert_positions[vert_i], src_vert.co);
1456 bmesh_block_copy_to_mesh_attributes(copy_info, vert_i, src_vert.head.data);
1457 any_loose_vert_local = any_loose_vert_local || src_vert.e == nullptr;
1458 }
1459 if (any_loose_vert_local) {
1460 any_loose_vert.store(true, std::memory_order_relaxed);
1461 }
1462 if (!select_vert.is_empty()) {
1463 for (const int vert_i : range) {
1464 select_vert[vert_i] = BM_elem_flag_test(bm_verts[vert_i], BM_ELEM_SELECT);
1465 }
1466 }
1467 if (!hide_vert.is_empty()) {
1468 for (const int vert_i : range) {
1469 hide_vert[vert_i] = BM_elem_flag_test(bm_verts[vert_i], BM_ELEM_HIDDEN);
1470 }
1471 }
1472 };
1473
1474 process_verts(bm_verts.index_range().take_front(1));
1475
1476 threading::parallel_for(bm_verts.index_range().drop_front(1), 1024, [&](const IndexRange range) {
1477 process_verts(range);
1478 single_checker.check_range(range);
1479 });
1480
1481 if (!any_loose_vert) {
1482 mesh.tag_loose_verts_none();
1483 }
1484}
1485
1487 const Span<const BMEdge *> bm_edges,
1488 const Span<BMeshToMeshLayerInfo> copy_info,
1489 AttrSingleValueChecker &single_checker,
1490 MutableSpan<bool> select_edge,
1491 MutableSpan<bool> hide_edge,
1492 MutableSpan<bool> sharp_edge,
1493 MutableSpan<bool> uv_seams)
1494{
1495 MutableSpan<int2> dst_edges = mesh.edges_for_write();
1496
1497 std::atomic<bool> any_loose_edge = false;
1498 const auto process_edges = [&](const IndexRange range) {
1499 bool any_loose_edge_local = false;
1500 for (const int edge_i : range) {
1501 const BMEdge &src_edge = *bm_edges[edge_i];
1502 dst_edges[edge_i] = int2(BM_elem_index_get(src_edge.v1), BM_elem_index_get(src_edge.v2));
1503 bmesh_block_copy_to_mesh_attributes(copy_info, edge_i, src_edge.head.data);
1504 any_loose_edge_local |= BM_edge_is_wire(&src_edge);
1505 }
1506 if (any_loose_edge_local) {
1507 any_loose_edge.store(true, std::memory_order_relaxed);
1508 }
1509 if (!select_edge.is_empty()) {
1510 for (const int edge_i : range) {
1511 select_edge[edge_i] = BM_elem_flag_test(bm_edges[edge_i], BM_ELEM_SELECT);
1512 }
1513 }
1514 if (!hide_edge.is_empty()) {
1515 for (const int edge_i : range) {
1516 hide_edge[edge_i] = BM_elem_flag_test(bm_edges[edge_i], BM_ELEM_HIDDEN);
1517 }
1518 }
1519 if (!sharp_edge.is_empty()) {
1520 for (const int edge_i : range) {
1521 sharp_edge[edge_i] = !BM_elem_flag_test(bm_edges[edge_i], BM_ELEM_SMOOTH);
1522 }
1523 }
1524 if (!uv_seams.is_empty()) {
1525 for (const int edge_i : range) {
1526 uv_seams[edge_i] = BM_elem_flag_test(bm_edges[edge_i], BM_ELEM_SEAM);
1527 }
1528 }
1529 };
1530
1531 process_edges(bm_edges.index_range().take_front(1));
1532
1533 threading::parallel_for(dst_edges.index_range().drop_front(1), 512, [&](const IndexRange range) {
1534 process_edges(range);
1535 single_checker.check_range(range);
1536 });
1537
1538 if (!any_loose_edge) {
1539 mesh.tag_loose_edges_none();
1540 }
1541}
1542
1544 const Span<const BMFace *> bm_faces,
1545 const Span<BMeshToMeshLayerInfo> copy_info,
1546 AttrSingleValueChecker &single_checker,
1547 MutableSpan<bool> select_poly,
1548 MutableSpan<bool> hide_poly,
1549 MutableSpan<bool> sharp_faces,
1550 MutableSpan<bool> uv_select_face,
1551 MutableSpan<int> material_indices)
1552{
1554
1555 MutableSpan<int> dst_face_offsets = mesh.face_offsets_for_write();
1556
1557 const auto process_faces = [&](const IndexRange range) {
1558 for (const int face_i : range) {
1559 const BMFace &src_face = *bm_faces[face_i];
1560 dst_face_offsets[face_i] = BM_elem_index_get(BM_FACE_FIRST_LOOP(&src_face));
1561 bmesh_block_copy_to_mesh_attributes(copy_info, face_i, src_face.head.data);
1562 }
1563 if (!select_poly.is_empty()) {
1564 for (const int face_i : range) {
1565 select_poly[face_i] = BM_elem_flag_test(bm_faces[face_i], BM_ELEM_SELECT);
1566 }
1567 }
1568 if (!hide_poly.is_empty()) {
1569 for (const int face_i : range) {
1570 hide_poly[face_i] = BM_elem_flag_test(bm_faces[face_i], BM_ELEM_HIDDEN);
1571 }
1572 }
1573 if (!material_indices.is_empty()) {
1574 for (const int face_i : range) {
1575 material_indices[face_i] = bm_faces[face_i]->mat_nr;
1576 }
1577 }
1578 if (!sharp_faces.is_empty()) {
1579 for (const int face_i : range) {
1580 sharp_faces[face_i] = !BM_elem_flag_test(bm_faces[face_i], BM_ELEM_SMOOTH);
1581 }
1582 }
1583 if (!uv_select_face.is_empty()) {
1584 for (const int face_i : range) {
1585 uv_select_face[face_i] = BM_elem_flag_test(bm_faces[face_i], BM_ELEM_SELECT_UV);
1586 }
1587 }
1588 };
1589
1590 process_faces(bm_faces.index_range().take_front(1));
1591
1592 threading::parallel_for(bm_faces.index_range().drop_front(1), 1024, [&](const IndexRange range) {
1593 process_faces(range);
1594 single_checker.check_range(range);
1595 });
1596}
1597
1598static void add_bm_cd_to_mesh(const BMesh &bm,
1599 const bke::AttrDomain domain,
1600 const uint64_t cd_type_mask,
1601 Mesh &mesh)
1602{
1603 const CustomData &bm_data = get_bm_custom_data(bm, domain);
1604 CustomData &mesh_data = get_mesh_custom_data(mesh, domain);
1605 bke::MutableAttributeAccessor attrs = mesh.attributes_for_write();
1606 const int domain_size = attrs.domain_size(domain);
1607 for (const CustomDataLayer &layer : Span(bm_data.layers, bm_data.totlayer)) {
1608 if (layer.flag & CD_FLAG_NOCOPY) {
1609 continue;
1610 }
1611 const eCustomDataType cd_type = eCustomDataType(layer.type);
1612 if (const std::optional<bke::AttrType> attr_type = bke::custom_data_type_to_attr_type(cd_type))
1613 {
1614 attrs.add(layer.name, domain, *attr_type, bke::AttributeInitConstruct());
1615 }
1616 else {
1617 if ((CD_TYPE_AS_MASK(cd_type) & cd_type_mask) == 0) {
1618 continue;
1619 }
1620 CustomData_add_layer_named(&mesh_data, cd_type, CD_CONSTRUCT, domain_size, layer.name);
1621 if (layer.type == CD_MDISPS && layer.flag & CD_FLAG_EXTERNAL) {
1622 const int new_layer_i = CustomData_get_layer_index(&mesh_data, layer.type);
1623 BLI_assert(new_layer_i != -1);
1624 CustomDataLayer &new_layer = mesh_data.layers[new_layer_i];
1625 new_layer.flag = layer.flag;
1626 }
1627 }
1628 }
1629}
1630
1632 const Span<const BMLoop *> bm_loops,
1633 const Span<BMeshToMeshLayerInfo> copy_info,
1634 AttrSingleValueChecker &single_checker,
1635 MutableSpan<bool> uv_select_vert,
1636 MutableSpan<bool> uv_select_edge)
1637{
1638 MutableSpan<int> dst_corner_verts = mesh.corner_verts_for_write();
1639 MutableSpan<int> dst_corner_edges = mesh.corner_edges_for_write();
1640 const bool need_uv_select = !uv_select_vert.is_empty() && !uv_select_edge.is_empty();
1641
1642 const auto process_corners = [&](const IndexRange range) {
1643 for (const int loop_i : range) {
1644 const BMLoop &src_loop = *bm_loops[loop_i];
1645 dst_corner_verts[loop_i] = BM_elem_index_get(src_loop.v);
1646 dst_corner_edges[loop_i] = BM_elem_index_get(src_loop.e);
1647 bmesh_block_copy_to_mesh_attributes(copy_info, loop_i, src_loop.head.data);
1648 }
1649
1650 if (need_uv_select) {
1651 for (const int loop_i : range) {
1652 const BMLoop &src_loop = *bm_loops[loop_i];
1653 uv_select_vert[loop_i] = BM_elem_flag_test(&src_loop, BM_ELEM_SELECT_UV);
1654 uv_select_edge[loop_i] = BM_elem_flag_test(&src_loop, BM_ELEM_SELECT_UV_EDGE);
1655 }
1656 }
1657 };
1658
1659 process_corners(bm_loops.index_range().take_front(1));
1660
1662 dst_corner_verts.index_range().drop_front(1), 1024, [&](const IndexRange range) {
1663 process_corners(range);
1664 single_checker.check_range(range);
1665 });
1666}
1667
1668void BM_mesh_bm_to_me(Main *bmain, BMesh *bm, Mesh *mesh, const BMeshToMeshParams *params)
1669{
1670 const int old_verts_num = mesh->verts_num;
1672 const std::string attributes_active_name = BKE_attributes_active_name_get(owner).value_or("");
1673
1674 if (attributes_active_name.empty()) {
1675 BLI_assert(mesh->attributes_active_index == -1);
1676 mesh->attributes_active_index = -1;
1677 }
1678
1680
1681 mesh->verts_num = bm->totvert;
1682 mesh->edges_num = bm->totedge;
1683 mesh->totface_legacy = 0;
1684 mesh->corners_num = bm->totloop;
1685 mesh->faces_num = bm->totface;
1686 mesh->act_face = -1;
1687
1688 /* Will have been cleared when clearing geometry. */
1689 const bool need_uv_select = CustomData_has_layer(&bm->ldata, CD_PROP_FLOAT2);
1690 if (need_uv_select & bm->uv_select_sync_valid) {
1692 }
1693
1694 bool need_select_vert = false;
1695 bool need_select_edge = false;
1696 bool need_select_poly = false;
1697 bool need_hide_vert = false;
1698 bool need_hide_edge = false;
1699 bool need_hide_poly = false;
1700 bool need_material_index = false;
1701 bool need_sharp_edge = false;
1702 bool need_sharp_face = false;
1703 bool need_uv_seams = false;
1704 Array<const BMVert *> vert_table;
1705 Array<const BMEdge *> edge_table;
1706 Array<const BMFace *> face_table;
1707 Array<const BMLoop *> loop_table;
1708 Vector<int> loop_layers_not_to_copy;
1710 (mesh->faces_num + mesh->edges_num) > 1024,
1711 [&]() {
1712 vert_table.reinitialize(bm->totvert);
1713 bm_vert_table_build(*bm, vert_table, need_select_vert, need_hide_vert);
1714 },
1715 [&]() {
1716 edge_table.reinitialize(bm->totedge);
1717 bm_edge_table_build(
1718 *bm, edge_table, need_select_edge, need_hide_edge, need_sharp_edge, need_uv_seams);
1719 },
1720 [&]() {
1721 face_table.reinitialize(bm->totface);
1722 loop_table.reinitialize(bm->totloop);
1723 bm_face_loop_table_build(*bm,
1724 face_table,
1725 loop_table,
1726 need_select_poly,
1727 need_hide_poly,
1728 need_sharp_face,
1729 need_material_index,
1730 loop_layers_not_to_copy);
1731 for (const int i : loop_layers_not_to_copy) {
1732 bm->ldata.layers[i].flag |= CD_FLAG_NOCOPY;
1733 }
1734 });
1736
1737 {
1738 CustomData_MeshMasks mask = CD_MASK_MESH;
1739 CustomData_MeshMasks_update(&mask, &params->cd_mask_extra);
1740
1741 const CustomData &bm_data = get_bm_custom_data(*bm, AttrDomain::Corner);
1742 CustomData &mesh_data = get_mesh_custom_data(*mesh, AttrDomain::Corner);
1743 if (bm_data.external) {
1744 mesh_data.external = MEM_dupalloc(bm_data.external);
1745 }
1746
1751 }
1752
1753 if (const char *name = CustomData_get_active_layer_name(&bm->ldata, CD_PROP_FLOAT2)) {
1754 MEM_SAFE_DELETE(mesh->active_uv_map_attribute);
1755 mesh->active_uv_map_attribute = BLI_strdup(name);
1756 }
1757 if (const char *name = CustomData_get_render_layer_name(&bm->ldata, CD_PROP_FLOAT2)) {
1758 MEM_SAFE_DELETE(mesh->default_uv_map_attribute);
1759 mesh->default_uv_map_attribute = BLI_strdup(name);
1760 }
1761
1762 /* Add optional mesh attributes before parallel iteration. */
1764 bke::MutableAttributeAccessor attrs = mesh->attributes_for_write();
1774 bke::SpanAttributeWriter<bool> uv_select_vert;
1775 bke::SpanAttributeWriter<bool> uv_select_edge;
1776 bke::SpanAttributeWriter<bool> uv_select_face;
1777 bke::SpanAttributeWriter<int> material_index;
1778 if (need_select_vert) {
1779 select_vert = attrs.lookup_or_add_for_write_only_span<bool>(".select_vert", AttrDomain::Point);
1780 }
1781 if (need_hide_vert) {
1782 hide_vert = attrs.lookup_or_add_for_write_only_span<bool>(".hide_vert", AttrDomain::Point);
1783 }
1784 if (need_select_edge) {
1785 select_edge = attrs.lookup_or_add_for_write_only_span<bool>(".select_edge", AttrDomain::Edge);
1786 }
1787 if (need_sharp_edge) {
1788 sharp_edge = attrs.lookup_or_add_for_write_only_span<bool>("sharp_edge", AttrDomain::Edge);
1789 }
1790 if (need_uv_seams) {
1791 uv_seams = attrs.lookup_or_add_for_write_only_span<bool>("uv_seam", AttrDomain::Edge);
1792 }
1793 if (need_hide_edge) {
1794 hide_edge = attrs.lookup_or_add_for_write_only_span<bool>(".hide_edge", AttrDomain::Edge);
1795 }
1796 if (need_select_poly) {
1797 select_poly = attrs.lookup_or_add_for_write_only_span<bool>(".select_poly", AttrDomain::Face);
1798 }
1799 if (need_hide_poly) {
1800 hide_poly = attrs.lookup_or_add_for_write_only_span<bool>(".hide_poly", AttrDomain::Face);
1801 }
1802 if (need_sharp_face) {
1803 sharp_face = attrs.lookup_or_add_for_write_only_span<bool>("sharp_face", AttrDomain::Face);
1804 }
1805 if (need_uv_select) {
1806 uv_select_vert = attrs.lookup_or_add_for_write_only_span<bool>(".uv_select_vert",
1807 AttrDomain::Corner);
1808 uv_select_edge = attrs.lookup_or_add_for_write_only_span<bool>(".uv_select_edge",
1809 AttrDomain::Corner);
1810 uv_select_face = attrs.lookup_or_add_for_write_only_span<bool>(".uv_select_face",
1811 AttrDomain::Face);
1812 }
1813 if (need_material_index) {
1814 material_index = attrs.lookup_or_add_for_write_only_span<int>("material_index",
1815 AttrDomain::Face);
1816 }
1817
1819 attrs.add<int2>(".edge_verts", bke::AttrDomain::Edge, bke::AttributeInitConstruct());
1820 attrs.add<int>(".corner_vert", bke::AttrDomain::Corner, bke::AttributeInitConstruct());
1821 attrs.add<int>(".corner_edge", bke::AttrDomain::Corner, bke::AttributeInitConstruct());
1822
1831 AttrSingleValueChecker vert_single_checker(
1832 mesh->attribute_storage.wrap(), bke::AttrDomain::Point, {"position"});
1833 AttrSingleValueChecker edge_single_checker(
1834 mesh->attribute_storage.wrap(), bke::AttrDomain::Edge, {".edge_verts"});
1835 AttrSingleValueChecker face_single_checker(
1836 mesh->attribute_storage.wrap(), bke::AttrDomain::Face, {});
1837 AttrSingleValueChecker corner_single_checker(
1838 mesh->attribute_storage.wrap(), bke::AttrDomain::Corner, {".corner_vert", ".corner_edge"});
1839
1840 /* Loop over all elements in parallel, copying attributes and building the Mesh topology. */
1842 (mesh->faces_num + mesh->edges_num) > 1024,
1843 [&]() {
1844 bm_to_mesh_verts(*mesh,
1845 vert_table,
1846 vert_copy_info,
1847 vert_single_checker,
1848 select_vert.span,
1849 hide_vert.span);
1850 if (mesh->key) {
1851 bm_to_mesh_shape(
1852 bm, mesh->key, mesh->vert_positions_for_write(), params->active_shapekey_to_mvert);
1853 }
1854 },
1855 [&]() {
1856 bm_to_mesh_edges(*mesh,
1857 edge_table,
1858 edge_copy_info,
1859 edge_single_checker,
1860 select_edge.span,
1861 hide_edge.span,
1862 sharp_edge.span,
1863 uv_seams.span);
1864 },
1865 [&]() {
1866 bm_to_mesh_faces(*mesh,
1867 face_table,
1868 face_copy_info,
1869 face_single_checker,
1870 select_poly.span,
1871 hide_poly.span,
1872 sharp_face.span,
1873 uv_select_face.span,
1874 material_index.span);
1875 if (bm->act_face) {
1876 mesh->act_face = BM_elem_index_get(bm->act_face);
1877 }
1878 },
1879 [&]() {
1880 bm_to_mesh_loops(*mesh,
1881 loop_table,
1882 corner_copy_info,
1883 corner_single_checker,
1884 uv_select_vert.span,
1885 uv_select_edge.span);
1886 /* Topology could be changed, ensure #CD_MDISPS are ok. */
1888 for (const int i : loop_layers_not_to_copy) {
1889 bm->ldata.layers[i].flag &= ~CD_FLAG_NOCOPY;
1890 }
1891 },
1892 [&]() {
1893 /* Patch hook indices and vertex parents. */
1894 if (params->calc_object_remap && (old_verts_num > 0)) {
1895 bmesh_to_mesh_calc_object_remap(*bmain, *mesh, *bm, old_verts_num);
1896 }
1897 },
1898 [&]() {
1899 mesh->totselect = BLI_listbase_count(&(bm->selected));
1900
1901 MEM_SAFE_DELETE(mesh->mselect);
1902 if (mesh->totselect != 0) {
1903 mesh->mselect = MEM_new_array_uninitialized<MSelect>(mesh->totselect,
1904 "Mesh selection history");
1905 }
1906
1907 for (const auto [i, selected] : bm->selected.enumerate()) {
1908 if (selected.htype == BM_VERT) {
1909 mesh->mselect[i].type = ME_VSEL;
1910 }
1911 else if (selected.htype == BM_EDGE) {
1912 mesh->mselect[i].type = ME_ESEL;
1913 }
1914 else if (selected.htype == BM_FACE) {
1915 mesh->mselect[i].type = ME_FSEL;
1916 }
1917
1918 mesh->mselect[i].index = BM_elem_index_get(selected.ele);
1919 }
1920 },
1921 [&]() {
1922 /* Run this even when shape keys aren't used since it may be used for hooks or vertex
1923 * parents. */
1924 if (params->update_shapekey_indices) {
1925 /* We have written a new shape key, if this mesh is _not_ going to be freed,
1926 * update the shape key indices to match the newly updated. */
1927 const int cd_shape_keyindex_offset = CustomData_get_offset(&bm->vdata,
1928 CD_SHAPE_KEYINDEX);
1929 if (cd_shape_keyindex_offset != -1) {
1930 BMIter iter;
1931 BMVert *vert;
1932 int i;
1933 BM_ITER_MESH_INDEX (vert, &iter, bm, BM_VERTS_OF_MESH, i) {
1934 BM_ELEM_CD_SET_INT(vert, cd_shape_keyindex_offset, i);
1935 }
1936 }
1937 }
1938 });
1939
1940 select_vert.finish();
1941 hide_vert.finish();
1942 select_edge.finish();
1943 hide_edge.finish();
1944 sharp_edge.finish();
1945 uv_seams.finish();
1946 select_poly.finish();
1947 hide_poly.finish();
1948 sharp_face.finish();
1949 uv_select_vert.finish();
1950 uv_select_edge.finish();
1951 uv_select_face.finish();
1952 material_index.finish();
1953
1954 vert_single_checker.optimize_storage();
1955 edge_single_checker.optimize_storage();
1956 face_single_checker.optimize_storage();
1957 corner_single_checker.optimize_storage();
1958
1959 /* Conversion to edit-mesh may have modified the attribute layers.
1960 * Re-resolve the active attribute by name to keep it stable. */
1961 if (!attributes_active_name.empty()) {
1962 BLI_assert(bke::allow_procedural_attribute_access(attributes_active_name));
1963 BKE_attributes_active_set(owner, attributes_active_name);
1964 }
1965 else {
1966 BLI_assert(mesh->attributes_active_index == -1);
1967 mesh->attributes_active_index = -1;
1968 }
1969}
1970
1972 Mesh &mesh,
1974 const bool only_select_and_hide)
1975{
1976 /* NOTE: The function is called from multiple threads with the same input BMesh and different
1977 * mesh objects. */
1978
1979 /* Must be an empty mesh. */
1980 BLI_assert(mesh.verts_num == 0);
1981
1982 /* Just in case, clear the derived geometry caches from the input mesh. */
1984
1985 mesh.verts_num = bm.totvert;
1986 mesh.edges_num = bm.totedge;
1987 mesh.totface_legacy = 0;
1988 mesh.corners_num = bm.totloop;
1989 mesh.faces_num = bm.totface;
1990
1991 /* Will have been cleared when clearing geometry. */
1992 const bool need_uv_select = !only_select_and_hide &&
1994 if (need_uv_select && bm.uv_select_sync_valid) {
1996 }
1997
1998 mesh.runtime->deformed_only = true;
1999
2000 const bool use_threading = (mesh.faces_num + mesh.edges_num) > 1024;
2001
2002 /* In a first pass, update indices of BMesh elements and build tables for easy iteration later.
2003 * Also check if some optional mesh attributes should be added in the next step. Since each
2004 * domain has no effect on others, process the independent domains on separate threads. */
2005 bool need_select_vert = false;
2006 bool need_select_edge = false;
2007 bool need_select_poly = false;
2008 bool need_hide_vert = false;
2009 bool need_hide_edge = false;
2010 bool need_hide_poly = false;
2011 bool need_material_index = false;
2012 bool need_sharp_edge = false;
2013 bool need_sharp_face = false;
2014 bool need_uv_seams = false;
2015
2016 Array<const BMVert *> vert_table;
2017 Array<const BMEdge *> edge_table;
2018 Array<const BMFace *> face_table;
2019 Array<const BMLoop *> loop_table;
2020 Vector<int> loop_layers_not_to_copy;
2022 use_threading,
2023 [&]() {
2024 vert_table.reinitialize(bm.totvert);
2025 bm_vert_table_build(bm, vert_table, need_select_vert, need_hide_vert);
2026 },
2027 [&]() {
2028 edge_table.reinitialize(bm.totedge);
2030 bm, edge_table, need_select_edge, need_hide_edge, need_sharp_edge, need_uv_seams);
2031 },
2032 [&]() {
2033 face_table.reinitialize(bm.totface);
2034 loop_table.reinitialize(bm.totloop);
2036 face_table,
2037 loop_table,
2038 need_select_poly,
2039 need_hide_poly,
2040 need_sharp_face,
2041 need_material_index,
2042 loop_layers_not_to_copy);
2043 for (const int i : loop_layers_not_to_copy) {
2045 }
2046 });
2048
2049 if (only_select_and_hide) {
2050 need_material_index = false;
2051 need_sharp_edge = false;
2052 need_sharp_face = false;
2053 need_uv_seams = false;
2054 }
2055 else {
2056 const CustomData_MeshMasks &mask_final = mask ? *mask : CD_MASK_DERIVEDMESH;
2057
2058 const CustomData &bm_data = get_bm_custom_data(bm, AttrDomain::Corner);
2059 CustomData &mesh_data = get_mesh_custom_data(mesh, AttrDomain::Corner);
2060 if (bm_data.external) {
2061 mesh_data.external = MEM_dupalloc(bm_data.external);
2062 }
2063
2068
2069 if (const char *name = CustomData_get_active_layer_name(&bm.ldata, CD_PROP_FLOAT2)) {
2070 MEM_SAFE_DELETE(mesh.active_uv_map_attribute);
2071 mesh.active_uv_map_attribute = BLI_strdup(name);
2072 }
2073
2074 if (const char *name = CustomData_get_render_layer_name(&bm.ldata, CD_PROP_FLOAT2)) {
2075 MEM_SAFE_DELETE(mesh.default_uv_map_attribute);
2076 mesh.default_uv_map_attribute = BLI_strdup(name);
2077 }
2078 }
2079
2080 /* Add optional mesh attributes before parallel iteration. */
2091 bke::SpanAttributeWriter<bool> uv_select_vert;
2092 bke::SpanAttributeWriter<bool> uv_select_edge;
2093 bke::SpanAttributeWriter<bool> uv_select_face;
2094 bke::SpanAttributeWriter<int> material_index;
2095
2096 bke::MutableAttributeAccessor attrs = mesh.attributes_for_write();
2097 if (need_select_vert) {
2098 select_vert = attrs.lookup_or_add_for_write_only_span<bool>(".select_vert", AttrDomain::Point);
2099 }
2100 if (need_hide_vert) {
2101 hide_vert = attrs.lookup_or_add_for_write_only_span<bool>(".hide_vert", AttrDomain::Point);
2102 }
2103 if (need_select_edge) {
2104 select_edge = attrs.lookup_or_add_for_write_only_span<bool>(".select_edge", AttrDomain::Edge);
2105 }
2106 if (need_sharp_edge) {
2107 sharp_edge = attrs.lookup_or_add_for_write_only_span<bool>("sharp_edge", AttrDomain::Edge);
2108 }
2109 if (need_uv_seams) {
2110 uv_seams = attrs.lookup_or_add_for_write_only_span<bool>("uv_seam", AttrDomain::Edge);
2111 }
2112 if (need_hide_edge) {
2113 hide_edge = attrs.lookup_or_add_for_write_only_span<bool>(".hide_edge", AttrDomain::Edge);
2114 }
2115 if (need_select_poly) {
2116 select_poly = attrs.lookup_or_add_for_write_only_span<bool>(".select_poly", AttrDomain::Face);
2117 }
2118 if (need_hide_poly) {
2119 hide_poly = attrs.lookup_or_add_for_write_only_span<bool>(".hide_poly", AttrDomain::Face);
2120 }
2121 if (need_sharp_face) {
2122 sharp_face = attrs.lookup_or_add_for_write_only_span<bool>("sharp_face", AttrDomain::Face);
2123 }
2124 if (need_uv_select) {
2125 uv_select_vert = attrs.lookup_or_add_for_write_only_span<bool>(".uv_select_vert",
2126 AttrDomain::Corner);
2127 uv_select_edge = attrs.lookup_or_add_for_write_only_span<bool>(".uv_select_edge",
2128 AttrDomain::Corner);
2129 uv_select_face = attrs.lookup_or_add_for_write_only_span<bool>(".uv_select_face",
2130 AttrDomain::Face);
2131 }
2132 if (need_material_index) {
2133 material_index = attrs.lookup_or_add_for_write_only_span<int>("material_index",
2134 AttrDomain::Face);
2135 }
2136
2139 attrs.add<int>(".corner_vert", bke::AttrDomain::Corner, bke::AttributeInitConstruct());
2140 attrs.add<int>(".corner_edge", bke::AttrDomain::Corner, bke::AttributeInitConstruct());
2141
2150 AttrSingleValueChecker vert_single_checker(
2151 mesh.attribute_storage.wrap(), bke::AttrDomain::Point, {"position"});
2152 AttrSingleValueChecker edge_single_checker(
2153 mesh.attribute_storage.wrap(), bke::AttrDomain::Edge, {".edge_verts"});
2154 AttrSingleValueChecker face_single_checker(
2155 mesh.attribute_storage.wrap(), bke::AttrDomain::Face, {});
2156 AttrSingleValueChecker corner_single_checker(
2157 mesh.attribute_storage.wrap(), bke::AttrDomain::Corner, {".corner_vert", ".corner_edge"});
2158
2159 /* Loop over all elements in parallel, copying attributes and building the Mesh topology. */
2161 use_threading,
2162 [&]() {
2164 vert_table,
2165 vert_copy_info,
2166 vert_single_checker,
2167 select_vert.span,
2168 hide_vert.span);
2169 },
2170 [&]() {
2172 edge_table,
2173 edge_copy_info,
2174 edge_single_checker,
2175 select_edge.span,
2176 hide_edge.span,
2177 sharp_edge.span,
2178 uv_seams.span);
2179 },
2180 [&]() {
2182 face_table,
2183 face_copy_info,
2184 face_single_checker,
2185 select_poly.span,
2186 hide_poly.span,
2187 sharp_face.span,
2188 uv_select_face.span,
2189 material_index.span);
2190 if (bm.act_face) {
2191 mesh.act_face = BM_elem_index_get(bm.act_face);
2192 }
2193 },
2194 [&]() {
2196 loop_table,
2197 corner_copy_info,
2198 corner_single_checker,
2199 uv_select_vert.span,
2200 uv_select_edge.span);
2201 for (const int i : loop_layers_not_to_copy) {
2203 }
2204 });
2205
2206 select_vert.finish();
2207 hide_vert.finish();
2208 select_edge.finish();
2209 hide_edge.finish();
2210 sharp_edge.finish();
2211 uv_seams.finish();
2212 select_poly.finish();
2213 hide_poly.finish();
2214 sharp_face.finish();
2215 uv_select_vert.finish();
2216 uv_select_edge.finish();
2217 uv_select_face.finish();
2218 material_index.finish();
2219
2220 vert_single_checker.optimize_storage();
2221 edge_single_checker.optimize_storage();
2222 face_single_checker.optimize_storage();
2223 corner_single_checker.optimize_storage();
2224}
2225
2227{
2228 /* Don't process shape-keys. We only feed them through the modifier stack as needed,
2229 * e.g. for applying modifiers or the like. */
2231 if (cd_mask_extra != nullptr) {
2232 CustomData_MeshMasks_update(&mask, cd_mask_extra);
2233 }
2234 mask.vmask &= ~CD_MASK_SHAPEKEY;
2235
2236 mesh_bm_to_me_compact(bm, mesh, &mask, false);
2237}
2238
2243
2244} // namespace blender
Generic geometry attributes built on CustomData.
CustomData interface, see also DNA_customdata_types.h.
#define ORIGINDEX_NONE
#define BLI_SCOPED_DEFER(function_to_defer)
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define BLI_assert_unreachable()
#define BLI_assert(a)
#define ELEM(...)
#define POINTER_OFFSET(v, ofs)
#define BM_ELEM_CD_GET_BOOL(ele, offset)
#define BM_ELEM_CD_GET_INT(ele, offset)
#define BM_ELEM_CD_SET_INT(ele, offset, f)
#define BM_ELEM_SELECT_UV_EDGE
#define BM_FACE_FIRST_LOOP(p)
#define BM_ELEM_CD_GET_VOID_P(ele, offset)
#define BM_elem_index_get(ele)
#define BM_elem_index_set(ele, index)
#define BM_elem_flag_test(ele, hflag)
#define BM_elem_flag_enable(ele, hflag)
#define BM_ITER_MESH_INDEX(ele, iter, bm, itype, indexvar)
#define BM_select_history_store_notest(bm, ele)
#define BM_FACE
#define BM_EDGE
#define BM_VERT
#define MAX_CUSTOMDATA_LAYER_NAME
#define CD_MASK_PROP_ALL
#define MEM_realloc_uninitialized(vmemh, len)
#define CD_MASK_SHAPEKEY
const T * data() const
Definition BLI_array.hh:323
IndexRange index_range() const
Definition BLI_array.hh:371
void reinitialize(const int64_t new_size)
Definition BLI_array.hh:430
bool is_empty() const
Definition BLI_array.hh:275
void check_range(const IndexRange range)
AttrSingleValueChecker(bke::AttributeStorage &storage, const bke::AttrDomain domain, const Set< StringRef > &skip_names)
static AttributeOwner from_id(ID *id)
GSpan()=default
constexpr Iterator end() const
constexpr IndexRange()=default
constexpr int64_t start() const
constexpr Iterator begin() const
constexpr bool is_empty() const
Definition BLI_span.hh:515
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
bool contains(const Key &key) const
Definition BLI_set.hh:310
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:141
constexpr Span()=default
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
constexpr bool is_empty() const
Definition BLI_span.hh:264
int64_t size() const
void append(const T &value)
IndexRange index_range() const
void fill(const T &value) const
GAttributeReader lookup(const StringRef name) const
int domain_size(const AttrDomain domain) const
AttrStorageType storage_type() const
const DataVariant & data() const
bool add(const StringRef name, const AttrDomain domain, const AttrType data_type, const AttributeInit &initializer)
GSpanAttributeWriter lookup_or_add_for_write_only_span(StringRef name, AttrDomain domain, AttrType data_type)
constexpr IndexRange drop_front(int64_t n) const
constexpr IndexRange take_front(int64_t n) const
constexpr bool is_empty() const
constexpr bool is_empty() const
#define storage(slot, qualifiers)
#define layer
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str)
Definition mallocn.cc:129
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
#define T
void to_static_type(const CPPType &cpp_type, Fn &&fn)
std::optional< eCustomDataType > attr_type_to_custom_data_type(AttrType attr_type)
std::optional< AttrType > custom_data_type_to_attr_type(eCustomDataType data_type)
const CPPType & attribute_type_to_cpp_type(AttrType type)
void parallel_invoke(Functions &&...functions)
Definition BLI_task.hh:197
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 CustomData_MeshMasks_update(CustomData_MeshMasks *mask_dst, const CustomData_MeshMasks *mask_src)
Definition customdata.cc:89
std::optional< StringRefNull > BKE_attributes_active_name_get(AttributeOwner &owner)
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
int CustomData_get_offset(const CustomData *data, eCustomDataType type)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
Definition BKE_text.h:91
BLI_INLINE bool BM_edge_is_wire(const BMEdge *e) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void multires_topology_changed(Mesh *mesh)
Definition multires.cc:733
void CustomData_bmesh_alloc_block(CustomData *data, void **block)
int CustomData_number_of_layers(const CustomData *data, eCustomDataType type)
MINLINE void copy_v3_v3(float r[3], const float a[3])
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
bool CustomData_has_layer(const CustomData *data, eCustomDataType type)
void BKE_mesh_face_offsets_ensure_alloc(Mesh *mesh)
void BM_mesh_bm_to_me_only_select_and_hide(BMesh &bm, Mesh &mesh)
int CustomData_get_n_offset(const CustomData *data, eCustomDataType type, int n)
size_t CustomData_get_elem_size(const CustomDataLayer *layer)
static void bm_to_mesh_edges(Mesh &mesh, const Span< const BMEdge * > bm_edges, const Span< BMeshToMeshLayerInfo > copy_info, AttrSingleValueChecker &single_checker, MutableSpan< bool > select_edge, MutableSpan< bool > hide_edge, MutableSpan< bool > sharp_edge, MutableSpan< bool > uv_seams)
bool CustomData_bmesh_merge_layout(const CustomData *source, CustomData *dest, eCustomDataMask mask, eCDAllocType alloctype, BMesh *bm, char htype)
void BKE_attributes_active_set(AttributeOwner &owner, StringRef name)
int CustomData_get_layer_index(const CustomData *data, eCustomDataType type)
static const CustomData & get_bm_custom_data(const BMesh &bm, const bke::AttrDomain domain)
bool CustomData_free_layer_named(CustomData *data, StringRef name)
ATTR_WARN_UNUSED_RESULT const BMVert * v
BMVert * BM_vert_create(BMesh *bm, const float co[3], const BMVert *v_example, const eBMCreateFlag create_flag)
Main function for creating a new vertex.
BMFace * BM_face_create(BMesh *bm, BMVert *const *verts, BMEdge *const *edges, const int len, const BMFace *f_example, const eBMCreateFlag create_flag)
void MEM_delete_void(void *vmemh)
void BM_mesh_bm_to_me_for_eval(BMesh &bm, Mesh &mesh, const CustomData_MeshMasks *cd_mask_extra)
std::optional< Array< bool > > BKE_keyblock_get_dependent_keys(const Key *key, int index)
Definition key.cc:1875
bool DEG_is_original(const T *id)
const char * CustomData_get_render_layer_name(const CustomData *data, eCustomDataType type)
void BM_mesh_bm_from_me(BMesh *bm, const Mesh *mesh, const BMeshFromMeshParams *params)
Mesh -> BMesh.
static BMFace * bm_face_create_from_mpoly(BMesh &bm, Span< int > face_verts, Span< int > face_edges, Span< BMVert * > vtable, Span< BMEdge * > etable)
static void bm_to_mesh_shape(BMesh *bm, Key *key, MutableSpan< float3 > positions, const bool active_shapekey_to_mvert)
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:541
static Vector< BMeshToMeshLayerInfo > bm_to_mesh_copy_info_calc(const CustomData &bm_data, const bke::AttrDomain domain, Mesh &mesh)
static void bm_vert_table_build(BMesh &bm, MutableSpan< const BMVert * > table, bool &need_select_vert, bool &need_hide_vert)
VecBase< int32_t, 2 > int2
void CustomData_init_layout_from(const CustomData *source, CustomData *dest, eCustomDataMask mask, eCDAllocType alloctype, int totelem)
void CustomData_data_set_default_value(eCustomDataType type, void *elem)
@ BM_CREATE_SKIP_CD
Definition bmesh_core.hh:42
static void assert_bmesh_has_no_mesh_only_attributes(const BMesh &bm)
static void bm_edge_table_build(BMesh &bm, MutableSpan< const BMEdge * > table, bool &need_select_edge, bool &need_hide_edge, bool &need_sharp_edge, bool &need_uv_seams)
void BM_face_normal_update(BMFace *f)
static void bmesh_block_copy_to_mesh_attributes(const Span< BMeshToMeshLayerInfo > copy_info, const int mesh_index, const void *block)
void CustomData_reset(CustomData *data)
static int bm_to_mesh_shape_layer_index_from_kb(BMesh *bm, KeyBlock *currkey)
BMesh const char void * data
void CustomData_data_copy_value(eCustomDataType type, const void *source, void *dest)
void * CustomData_add_layer_named(CustomData *data, eCustomDataType type, eCDAllocType alloctype, int totelem, StringRef name)
static void bm_face_loop_table_build(BMesh &bm, MutableSpan< const BMFace * > face_table, MutableSpan< const BMLoop * > loop_table, bool &need_select_poly, bool &need_hide_poly, bool &need_sharp_face, bool &need_material_index, Vector< int > &loop_layers_not_to_copy)
void BM_face_select_set(BMesh *bm, BMFace *f, const bool select)
Select Face.
static void bm_to_mesh_verts(Mesh &mesh, const Span< const BMVert * > bm_verts, const Span< BMeshToMeshLayerInfo > copy_info, AttrSingleValueChecker &single_checker, MutableSpan< bool > select_vert, MutableSpan< bool > hide_vert)
const CustomData_MeshMasks CD_MASK_BMESH
static BMVert ** bm_to_mesh_vertex_map(BMesh *bm, const int old_verts_num)
BMesh -> Mesh.
static void add_bm_cd_to_mesh(const BMesh &bm, const bke::AttrDomain domain, const uint64_t cd_type_mask, Mesh &mesh)
static void mesh_attributes_copy_to_bmesh_block(CustomData &data, const Span< MeshToBMeshLayerInfo > copy_info, const int mesh_index, BMHeader &header)
eCustomDataMask CD_TYPE_AS_MASK(eCustomDataType type)
const char * CustomData_get_active_layer_name(const CustomData *data, eCustomDataType type)
@ ME_FLAG_UV_SELECT_SYNC_VALID
int BLI_listbase_count(const ListBase *listbase) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:531
void BM_vert_select_set(BMesh *bm, BMVert *v, const bool select)
Select Vert.
int CustomData_get_named_layer_index(const CustomData *data, eCustomDataType type, StringRef name)
static CustomData get_mesh_to_bm_custom_data(const Mesh &mesh, const bke::AttrDomain domain, const uint64_t cd_type_mask)
void CustomData_set_layer_render(CustomData *data, eCustomDataType type, int n)
const CustomData_MeshMasks CD_MASK_DERIVEDMESH
MINLINE void add_v3_v3(float r[3], const float a[3])
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
int CustomData_get_layer_index_n(const CustomData *data, eCustomDataType type, int n)
static void bmesh_to_mesh_calc_object_remap(Main &bmain, Mesh &mesh, BMesh &bm, const int old_totvert)
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
const CustomData_MeshMasks CD_MASK_MESH
const char * CustomData_get_layer_name(const CustomData *data, eCustomDataType type, int n)
KeyBlock * BKE_keyblock_add(Key *key, const char *name)
Definition key.cc:1322
static void bm_to_mesh_faces(Mesh &mesh, const Span< const BMFace * > bm_faces, const Span< BMeshToMeshLayerInfo > copy_info, AttrSingleValueChecker &single_checker, MutableSpan< bool > select_poly, MutableSpan< bool > hide_poly, MutableSpan< bool > sharp_faces, MutableSpan< bool > uv_select_face, MutableSpan< int > material_indices)
void CustomData_bmesh_init_pool(CustomData *data, int totelem, char htype)
void CustomData_set_layer_active(CustomData *data, eCustomDataType type, int n)
static CLG_LogRef LOG
Definition logging.cpp:12
void BM_select_history_clear(BMesh *bm)
void BM_mesh_bm_to_me(Main *bmain, BMesh *bm, Mesh *mesh, const BMeshToMeshParams *params)
VecBase< float, 3 > float3
static void mesh_bm_to_me_compact(BMesh &bm, Mesh &mesh, const CustomData_MeshMasks *mask, const bool only_select_and_hide)
static const CustomData & get_mesh_custom_data(const Mesh &mesh, const bke::AttrDomain domain)
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)
int CustomData_get_named_layer(const CustomData *data, eCustomDataType type, StringRef name)
MINLINE int min_ii(int a, int b)
@ BM_ELEM_SELECT_UV
BMEdge * BM_edge_create(BMesh *bm, BMVert *v1, BMVert *v2, const BMEdge *e_example, const eBMCreateFlag create_flag)
Main function for creating a new edge.
void BKE_mesh_clear_geometry(Mesh *mesh)
bool BM_attribute_stored_in_bmesh_builtin(const StringRef name)
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
static void bm_to_mesh_loops(Mesh &mesh, const Span< const BMLoop * > bm_loops, const Span< BMeshToMeshLayerInfo > copy_info, AttrSingleValueChecker &single_checker, MutableSpan< bool > uv_select_vert, MutableSpan< bool > uv_select_edge)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void BKE_mesh_runtime_clear_geometry(Mesh *mesh)
void * CustomData_add_layer(CustomData *data, eCustomDataType type, eCDAllocType alloctype, int totelem)
static Vector< MeshToBMeshLayerInfo > mesh_to_bm_copy_info_calc(const Mesh &mesh, const bke::AttrDomain domain, CustomData &bm_data)
void BM_edge_select_set(BMesh *bm, BMEdge *e, const bool select)
Select Edge.
void vert(const int vert_id, float4 &position)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
struct BMLoop * next
struct BMEdge * e
struct BMVert * v
struct BMEdge * e
struct CustomData_MeshMasks cd_mask_extra
CustomData pdata
ListBaseT< BMEditSelection > selected
CustomData vdata
CustomData edata
CustomData ldata
bool uv_select_sync_valid
BMFace * act_face
CustomDataExternal * external
struct KeyBlock * next
KeyBlock * refkey
ListBaseT< KeyBlock > block
ShapekeyContainerType type
ListBaseT< Object > objects
Definition BKE_main.hh:388
struct Object * parent
ListBaseT< ModifierData > modifiers
eObject_Partype partype
static SingleData from_value(const GPointer &value)