[Bf-blender-cvs] [791d8be685c] refactor-mesh-uv-map-generic: Progress

Hans Goudey noreply at git.blender.org
Fri Aug 26 04:34:49 CEST 2022


Commit: 791d8be685c9aab01dddc806abe10eb45843f66b
Author: Hans Goudey
Date:   Wed Aug 17 18:08:01 2022 -0400
Branches: refactor-mesh-uv-map-generic
https://developer.blender.org/rB791d8be685c9aab01dddc806abe10eb45843f66b

Progress

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

M	source/blender/blenkernel/BKE_customdata.h
M	source/blender/blenkernel/BKE_mesh_legacy_convert.h
M	source/blender/blenkernel/intern/customdata.cc
M	source/blender/blenkernel/intern/mesh.cc
M	source/blender/blenkernel/intern/mesh_legacy_convert.cc
M	source/blender/blenloader/intern/versioning_300.c

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

diff --git a/source/blender/blenkernel/BKE_customdata.h b/source/blender/blenkernel/BKE_customdata.h
index 02a0afd086e..b1ec14534f5 100644
--- a/source/blender/blenkernel/BKE_customdata.h
+++ b/source/blender/blenkernel/BKE_customdata.h
@@ -450,6 +450,12 @@ int CustomData_get_stencil_layer(const struct CustomData *data, int type);
  */
 const char *CustomData_get_active_layer_name(const struct CustomData *data, int type);
 
+/**
+ * Returns name of the default layer of the given type or NULL
+ * if no such active layer is defined.
+ */
+const char *CustomData_get_render_layer_name(const struct CustomData *data, int type);
+
 /**
  * Copies the data from source to the data element at index in the first layer of type
  * no effect if there is no layer of type.
diff --git a/source/blender/blenkernel/BKE_mesh_legacy_convert.h b/source/blender/blenkernel/BKE_mesh_legacy_convert.h
index bbc61d5af5e..0eb94de7929 100644
--- a/source/blender/blenkernel/BKE_mesh_legacy_convert.h
+++ b/source/blender/blenkernel/BKE_mesh_legacy_convert.h
@@ -17,6 +17,9 @@ struct CustomData;
 struct Mesh;
 struct MFace;
 
+void BKE_mesh_legacy_convert_uvs_to_struct(const Mesh *mesh);
+void BKE_mesh_legacy_convert_uvs_to_generic(Mesh *mesh);
+
 /**
  * Convert the hidden element attributes to the old flag format for writing.
  */
diff --git a/source/blender/blenkernel/intern/customdata.cc b/source/blender/blenkernel/intern/customdata.cc
index aa1c5c34f34..41cd74f40a6 100644
--- a/source/blender/blenkernel/intern/customdata.cc
+++ b/source/blender/blenkernel/intern/customdata.cc
@@ -2668,6 +2668,12 @@ const char *CustomData_get_active_layer_name(const CustomData *data, const int t
   return layer_index < 0 ? nullptr : data->layers[layer_index].name;
 }
 
+const char *CustomData_get_render_layer_name(const CustomData *data, const int type)
+{
+  const int layer_index = CustomData_get_render_layer_index(data, type);
+  return layer_index < 0 ? nullptr : data->layers[layer_index].name;
+}
+
 void CustomData_set_layer_active(CustomData *data, const int type, const int n)
 {
   for (int i = 0; i < data->totlayer; i++) {
diff --git a/source/blender/blenkernel/intern/mesh.cc b/source/blender/blenkernel/intern/mesh.cc
index f2057d25882..956dda4985c 100644
--- a/source/blender/blenkernel/intern/mesh.cc
+++ b/source/blender/blenkernel/intern/mesh.cc
@@ -247,6 +247,7 @@ static void mesh_blend_write(BlendWriter *writer, ID *id, const void *id_address
   else {
     if (!BLO_write_is_undo(writer)) {
       BKE_mesh_legacy_convert_hide_layers_to_flags(mesh);
+      BKE_mesh_legacy_convert_uvs_to_struct(mesh);
     }
 
     CustomData_blend_write_prepare(mesh->vdata, vert_layers, {".hide_vert"});
@@ -333,6 +334,7 @@ static void mesh_blend_read_data(BlendDataReader *reader, ID *id)
 
   if (!BLO_read_data_is_undo(reader)) {
     BKE_mesh_legacy_convert_flags_to_hide_layers(mesh);
+    BKE_mesh_legacy_convert_uvs_to_generic(mesh);
   }
 
   /* We don't expect to load normals from files, since they are derived data. */
diff --git a/source/blender/blenkernel/intern/mesh_legacy_convert.cc b/source/blender/blenkernel/intern/mesh_legacy_convert.cc
index 62791032a62..80c14708d78 100644
--- a/source/blender/blenkernel/intern/mesh_legacy_convert.cc
+++ b/source/blender/blenkernel/intern/mesh_legacy_convert.cc
@@ -966,3 +966,87 @@ void BKE_mesh_legacy_convert_flags_to_hide_layers(Mesh *mesh)
 }
 
 /** \} */
+
+/* -------------------------------------------------------------------- */
+/** \name Generic UV Map Conversion
+ * \{ */
+
+void BKE_mesh_legacy_convert_uvs_to_struct(const Mesh *mesh)
+{
+  using namespace blender;
+  using namespace blender::bke;
+  const AttributeAccessor attributes = mesh_attributes(*mesh);
+}
+
+void BKE_mesh_legacy_convert_uvs_to_generic(Mesh *mesh)
+{
+  using namespace blender;
+  using namespace blender::bke;
+  MutableAttributeAccessor attributes = mesh_attributes_for_write(*mesh);
+
+  int totloop = mesh->totloop;
+
+  /* Store layer names since they will be removed.
+   * Use intermediate #StringRef because the names can be null. */
+  const std::string active_uv = StringRef(
+      CustomData_get_active_layer_name(&mesh->ldata, CD_MLOOPUV));
+  const std::string default_uv = StringRef(
+      CustomData_get_render_layer_name(&mesh->ldata, CD_MLOOPUV));
+
+  const int uv_map_num = CustomData_number_of_layers(&mesh->ldata, CD_MLOOPUV);
+  for (const int uv_layer_i : IndexRange(uv_map_num)) {
+    int index = CustomData_get_layer_index_n(&mesh->ldata, CD_MLOOPUV, uv_layer_i);
+    const MLoopUV *luv = static_cast<const MLoopUV *>(
+        CustomData_get_layer_n(&mesh->ldata, CD_MLOOPUV, uv_layer_i));
+    if (!luv) {
+      continue;
+    }
+    uint32_t layerflags = 0;
+
+    const std::string name = CustomData_get_layer_name(&mesh->ldata, CD_MLOOPUV, n);
+    for (int i = 0; i < totloop; i++) {
+      layerflags |= luv[i].flag;
+    }
+
+    attributes.remove(name);
+
+    /* Remove the original name, so we can create an attribute with the same name. */
+    mesh->ldata.layers[index].name[0] = 0;
+
+    UVMap_Data data = BKE_id_attributes_create_uvmap_layers(
+        (ID *)mesh, name.c_str(), NULL, layerflags);
+    for (int i = 0; i < totloop; i++) {
+      copy_v2_v2(data.uv[i], luv[i].uv);
+    }
+
+    if (layerflags & MLOOPUV_VERTSEL) {
+      for (int i = 0; i < totloop; i++) {
+        data.vertsel[i] = luv[i].flag & MLOOPUV_VERTSEL;
+      }
+    }
+
+    if (layerflags & MLOOPUV_EDGESEL) {
+      for (int i = 0; i < totloop; i++) {
+        data.edgesel[i] = luv[i].flag & MLOOPUV_EDGESEL;
+      }
+    }
+
+    if (layerflags & MLOOPUV_PINNED) {
+      for (int i = 0; i < totloop; i++) {
+        data.pinned[i] = luv[i].flag & MLOOPUV_PINNED;
+      }
+    }
+  }
+
+  /* Take care to set the active and render layer before deleting the old layers,
+   * which would change the indices.
+   */
+  CustomData_set_layer_active_index(&mesh->ldata, CD_PROP_FLOAT2, active_index);
+  CustomData_set_layer_render_index(&mesh->ldata, CD_PROP_FLOAT2, render_index);
+
+  CustomData_free_layers(&mesh->ldata, CD_MLOOPUV, totloop);
+
+  mesh->mloopuv = CustomData_get_layer(&mesh->ldata, CD_PROP_FLOAT2);
+}
+
+/** \} */
diff --git a/source/blender/blenloader/intern/versioning_300.c b/source/blender/blenloader/intern/versioning_300.c
index 839b5b5df6c..0ca133d2865 100644
--- a/source/blender/blenloader/intern/versioning_300.c
+++ b/source/blender/blenloader/intern/versioning_300.c
@@ -3319,77 +3319,6 @@ void blo_do_versions_300(FileData *fd, Library *UNUSED(lib), Main *bmain)
    */
   {
     /* Keep this block, even when empty. */
-    LISTBASE_FOREACH (Mesh *, mesh, &bmain->meshes) {
-      int totloop = mesh->totloop;
-
-      const int active = CustomData_get_active_layer(&mesh->ldata, CD_MLOOPUV);
-      const int render = CustomData_get_render_layer(&mesh->ldata, CD_MLOOPUV);
-      int active_index = -1;
-      int render_index = -1;
-
-      int nr_mloopuv_layers = CustomData_number_of_layers(&mesh->ldata, CD_MLOOPUV);
-      for (int n = 0; n < nr_mloopuv_layers; n++) {
-        int index = CustomData_get_layer_index_n(&mesh->ldata, CD_MLOOPUV, n);
-        const MLoopUV *luv = CustomData_get_layer_n(&mesh->ldata, CD_MLOOPUV, n);
-        uint32_t layerflags = 0;
-
-        char name[MAX_CUSTOMDATA_LAYER_NAME];
-        BLI_strncpy(name,
-                    CustomData_get_layer_name(&mesh->ldata, CD_MLOOPUV, n),
-                    MAX_CUSTOMDATA_LAYER_NAME);
-        if (luv) {
-          for (int i = 0; i < totloop; i++) {
-            layerflags |= luv[i].flag;
-          }
-        }
-        /* Remove the original name, so we can create an attribute with the same name. */
-        mesh->ldata.layers[index].name[0] = 0;
-
-        UVMap_Data data = BKE_id_attributes_create_uvmap_layers(
-            (ID *)mesh, name, NULL, layerflags);
-
-        if (n == active) {
-          active_index = data.uv_index;
-        }
-        if (n == render) {
-          render_index = data.uv_index;
-        }
-
-        if (luv) {
-          for (int i = 0; i < totloop; i++) {
-            copy_v2_v2(data.uv[i], luv[i].uv);
-          }
-
-          if (layerflags & MLOOPUV_VERTSEL) {
-            for (int i = 0; i < totloop; i++) {
-              data.vertsel[i] = luv[i].flag & MLOOPUV_VERTSEL;
-            }
-          }
-
-          if (layerflags & MLOOPUV_EDGESEL) {
-            for (int i = 0; i < totloop; i++) {
-              data.edgesel[i] = luv[i].flag & MLOOPUV_EDGESEL;
-            }
-          }
-
-          if (layerflags & MLOOPUV_PINNED) {
-            for (int i = 0; i < totloop; i++) {
-              data.pinned[i] = luv[i].flag & MLOOPUV_PINNED;
-            }
-          }
-        }
-      }
-      /* Take care to set the active and render layer before deleting the old layers,
-       * which would change the indices.
-       */
-      CustomData_set_layer_active_index(&mesh->ldata, CD_PROP_FLOAT2, active_index);
-      CustomData_set_layer_render_index(&mesh->ldata, CD_PROP_FLOAT2, render_index);
-
-      CustomData_free_layers(&mesh->ldata, CD_MLOOPUV, totloop);
-
-      mesh->mloopuv = CustomData_get_layer(&mesh->ldata, CD_PROP_FLOAT2);
-    }
-
     /* Image generation information transferred to tiles. */
     if (!DNA_struct_elem_find(fd->filesdna, "ImageTile", "int", "gen_x")) {
       for (Image *ima = bmain->images.first; ima; ima = ima->id.next) {



More information about the Bf-blender-cvs mailing list