[Bf-blender-cvs] [f6b72934701] refactor-mesh-hide-generic: Fix conversion to and from BMesh

Hans Goudey noreply at git.blender.org
Sat Jun 4 14:53:56 CEST 2022


Commit: f6b729347017edbf98452c17c1a809728129ed33
Author: Hans Goudey
Date:   Sat Jun 4 14:53:53 2022 +0200
Branches: refactor-mesh-hide-generic
https://developer.blender.org/rBf6b729347017edbf98452c17c1a809728129ed33

Fix conversion to and from BMesh

===================================================================

M	source/blender/blenkernel/BKE_customdata.h
M	source/blender/blenkernel/intern/customdata.cc
M	source/blender/bmesh/intern/bmesh_construct.c
M	source/blender/bmesh/intern/bmesh_mesh_convert.cc

===================================================================

diff --git a/source/blender/blenkernel/BKE_customdata.h b/source/blender/blenkernel/BKE_customdata.h
index 993ab476830..63fea3ddb3f 100644
--- a/source/blender/blenkernel/BKE_customdata.h
+++ b/source/blender/blenkernel/BKE_customdata.h
@@ -141,6 +141,15 @@ void CustomData_copy(const struct CustomData *source,
                      eCDAllocType alloctype,
                      int totelem);
 
+/**
+ * Like #CustomData_copy but skips copying layers that are stored as flags on #BMesh.
+ */
+void CustomData_copy_mesh_to_bmesh(const struct CustomData *source,
+                                   struct CustomData *dest,
+                                   eCustomDataMask mask,
+                                   eCDAllocType alloctype,
+                                   int totelem);
+
 /* BMESH_TODO, not really a public function but readfile.c needs it */
 void CustomData_update_typemap(struct CustomData *data);
 
@@ -154,6 +163,15 @@ bool CustomData_merge(const struct CustomData *source,
                       eCDAllocType alloctype,
                       int totelem);
 
+/**
+ * Like #CustomData_copy but skips copying layers that are stored as flags on #BMesh.
+ */
+bool CustomData_merge_mesh_to_bmesh(const struct CustomData *source,
+                                    struct CustomData *dest,
+                                    eCustomDataMask mask,
+                                    eCDAllocType alloctype,
+                                    int totelem);
+
 /**
  * Reallocate custom data to a new element count.
  * Only affects on data layers which are owned by the CustomData itself,
diff --git a/source/blender/blenkernel/intern/customdata.cc b/source/blender/blenkernel/intern/customdata.cc
index aed570235aa..5ea8daeaa40 100644
--- a/source/blender/blenkernel/intern/customdata.cc
+++ b/source/blender/blenkernel/intern/customdata.cc
@@ -57,6 +57,7 @@
 
 using blender::IndexRange;
 using blender::Span;
+using blender::StringRef;
 using blender::Vector;
 
 /* number of layers to add when growing a CustomData object */
@@ -2324,6 +2325,43 @@ bool CustomData_merge(const CustomData *source,
   return changed;
 }
 
+static bool layer_stored_in_bmesh(const StringRef name)
+{
+  return ELEM(name, ".hide_face", ".hide_edge", ".hide_face");
+}
+
+static CustomData shallow_copy_remove_non_bmesh_attributes(const CustomData &src)
+{
+  Vector<CustomDataLayer> dst_layers;
+  for (const CustomDataLayer &layer : Span<CustomDataLayer>{src.layers, src.totlayer}) {
+    if (layer_stored_in_bmesh(layer.name)) {
+      dst_layers.append(layer);
+    }
+  }
+
+  CustomData dst = src;
+  dst.layers = static_cast<CustomDataLayer *>(
+      MEM_calloc_arrayN(dst_layers.size(), sizeof(CustomDataLayer), __func__));
+  dst.totlayer = dst_layers.size();
+  memcpy(dst.layers, dst_layers.data(), dst_layers.as_span().size_in_bytes());
+
+  CustomData_update_typemap(&dst);
+
+  return dst;
+}
+
+bool CustomData_merge_mesh_to_bmesh(const CustomData *source,
+                                    CustomData *dest,
+                                    eCustomDataMask mask,
+                                    eCDAllocType alloctype,
+                                    int totelem)
+{
+  CustomData source_copy = shallow_copy_remove_non_bmesh_attributes(*source);
+  const bool result = CustomData_merge(&source_copy, dest, mask, alloctype, totelem);
+  MEM_SAFE_FREE(source_copy.layers);
+  return result;
+}
+
 void CustomData_realloc(CustomData *data, int totelem)
 {
   for (int i = 0; i < data->totlayer; i++) {
@@ -2354,6 +2392,17 @@ void CustomData_copy(const CustomData *source,
   CustomData_merge(source, dest, mask, alloctype, totelem);
 }
 
+void CustomData_copy_mesh_to_bmesh(const CustomData *source,
+                                   CustomData *dest,
+                                   eCustomDataMask mask,
+                                   eCDAllocType alloctype,
+                                   int totelem)
+{
+  CustomData source_copy = shallow_copy_remove_non_bmesh_attributes(*source);
+  CustomData_copy(&source_copy, dest, mask, alloctype, totelem);
+  MEM_SAFE_FREE(source_copy.layers);
+}
+
 static void customData_free_layer__internal(CustomDataLayer *layer, int totelem)
 {
   const LayerTypeInfo *typeInfo;
diff --git a/source/blender/bmesh/intern/bmesh_construct.c b/source/blender/bmesh/intern/bmesh_construct.c
index bd3d2d135f1..0ee5545527b 100644
--- a/source/blender/bmesh/intern/bmesh_construct.c
+++ b/source/blender/bmesh/intern/bmesh_construct.c
@@ -512,16 +512,24 @@ void BM_mesh_copy_init_customdata_from_mesh_array(BMesh *bm_dst,
   for (int i = 0; i < me_src_array_len; i++) {
     const Mesh *me_src = me_src_array[i];
     if (i == 0) {
-      CustomData_copy(&me_src->vdata, &bm_dst->vdata, CD_MASK_BMESH.vmask, CD_CALLOC, 0);
-      CustomData_copy(&me_src->edata, &bm_dst->edata, CD_MASK_BMESH.emask, CD_CALLOC, 0);
-      CustomData_copy(&me_src->ldata, &bm_dst->ldata, CD_MASK_BMESH.lmask, CD_CALLOC, 0);
-      CustomData_copy(&me_src->pdata, &bm_dst->pdata, CD_MASK_BMESH.pmask, CD_CALLOC, 0);
+      CustomData_copy_mesh_to_bmesh(
+          &me_src->vdata, &bm_dst->vdata, CD_MASK_BMESH.vmask, CD_CALLOC, 0);
+      CustomData_copy_mesh_to_bmesh(
+          &me_src->edata, &bm_dst->edata, CD_MASK_BMESH.emask, CD_CALLOC, 0);
+      CustomData_copy_mesh_to_bmesh(
+          &me_src->ldata, &bm_dst->ldata, CD_MASK_BMESH.lmask, CD_CALLOC, 0);
+      CustomData_copy_mesh_to_bmesh(
+          &me_src->pdata, &bm_dst->pdata, CD_MASK_BMESH.pmask, CD_CALLOC, 0);
     }
     else {
-      CustomData_merge(&me_src->vdata, &bm_dst->vdata, CD_MASK_BMESH.vmask, CD_CALLOC, 0);
-      CustomData_merge(&me_src->edata, &bm_dst->edata, CD_MASK_BMESH.emask, CD_CALLOC, 0);
-      CustomData_merge(&me_src->ldata, &bm_dst->ldata, CD_MASK_BMESH.lmask, CD_CALLOC, 0);
-      CustomData_merge(&me_src->pdata, &bm_dst->pdata, CD_MASK_BMESH.pmask, CD_CALLOC, 0);
+      CustomData_merge_mesh_to_bmesh(
+          &me_src->vdata, &bm_dst->vdata, CD_MASK_BMESH.vmask, CD_CALLOC, 0);
+      CustomData_merge_mesh_to_bmesh(
+          &me_src->edata, &bm_dst->edata, CD_MASK_BMESH.emask, CD_CALLOC, 0);
+      CustomData_merge_mesh_to_bmesh(
+          &me_src->ldata, &bm_dst->ldata, CD_MASK_BMESH.lmask, CD_CALLOC, 0);
+      CustomData_merge_mesh_to_bmesh(
+          &me_src->pdata, &bm_dst->pdata, CD_MASK_BMESH.pmask, CD_CALLOC, 0);
     }
 
     cd_flag |= me_src->cd_flag;
diff --git a/source/blender/bmesh/intern/bmesh_mesh_convert.cc b/source/blender/bmesh/intern/bmesh_mesh_convert.cc
index 6d3fcee2a8b..35ce9b6b9ff 100644
--- a/source/blender/bmesh/intern/bmesh_mesh_convert.cc
+++ b/source/blender/bmesh/intern/bmesh_mesh_convert.cc
@@ -213,10 +213,10 @@ void BM_mesh_bm_from_me(BMesh *bm, const Mesh *me, const struct BMeshFromMeshPar
 
   if (!me || !me->totvert) {
     if (me && is_new) { /* No verts? still copy custom-data layout. */
-      CustomData_copy(&me->vdata, &bm->vdata, mask.vmask, CD_DEFAULT, 0);
-      CustomData_copy(&me->edata, &bm->edata, mask.emask, CD_DEFAULT, 0);
-      CustomData_copy(&me->ldata, &bm->ldata, mask.lmask, CD_DEFAULT, 0);
-      CustomData_copy(&me->pdata, &bm->pdata, mask.pmask, CD_DEFAULT, 0);
+      CustomData_copy_mesh_to_bmesh(&me->vdata, &bm->vdata, mask.vmask, CD_DEFAULT, 0);
+      CustomData_copy_mesh_to_bmesh(&me->edata, &bm->edata, mask.emask, CD_DEFAULT, 0);
+      CustomData_copy_mesh_to_bmesh(&me->ldata, &bm->ldata, mask.lmask, CD_DEFAULT, 0);
+      CustomData_copy_mesh_to_bmesh(&me->pdata, &bm->pdata, mask.pmask, CD_DEFAULT, 0);
 
       CustomData_bmesh_init_pool(&bm->vdata, me->totvert, BM_VERT);
       CustomData_bmesh_init_pool(&bm->edata, me->totedge, BM_EDGE);
@@ -232,10 +232,10 @@ void BM_mesh_bm_from_me(BMesh *bm, const Mesh *me, const struct BMeshFromMeshPar
   }
 
   if (is_new) {
-    CustomData_copy(&me->vdata, &bm->vdata, mask.vmask, CD_CALLOC, 0);
-    CustomData_copy(&me->edata, &bm->edata, mask.emask, CD_CALLOC, 0);
-    CustomData_copy(&me->ldata, &bm->ldata, mask.lmask, CD_CALLOC, 0);
-    CustomData_copy(&me->pdata, &bm->pdata, mask.pmask, CD_CALLOC, 0);
+    CustomData_copy_mesh_to_bmesh(&me->vdata, &bm->vdata, mask.vmask, CD_CALLOC, 0);
+    CustomData_copy_mesh_to_bmesh(&me->edata, &bm->edata, mask.emask, CD_CALLOC, 0);
+    CustomData_copy_mesh_to_bmesh(&me->ldata, &bm->ldata, mask.lmask, CD_CALLOC, 0);
+    CustomData_copy_mesh_to_bmesh(&me->pdata, &bm->pdata, mask.pmask, CD_CALLOC, 0);
   }
   else {
     CustomData_bmesh_merge(&me->vdata, &bm->vdata, mask.vmask, CD_CALLOC, bm, BM_VERT);
@@ -924,6 +924,11 @@ static void ensure_bool_layer(MutableSpan<bool> &values,
                               const char *name,
                               const int num)
 {
+  if (values.is_empty()) {
+    if (void *layer_data = CustomData_get_layer_named(&data, CD_PROP_BOOL, name)) {
+      values = {(bool *)layer_data, num};
+    }
+  }
   if (values.is_empty()) {
     values = {(bool *)CustomData_add_layer_named(&data, CD_PROP_BOOL, CD_CALLOC, NULL, num, name),
               num};



More information about the Bf-blender-cvs mailing list