[Bf-blender-cvs] [6514bb05ea5] master: Mesh: Store active & default color attributes with strings

Hans Goudey noreply at git.blender.org
Thu Dec 15 21:22:29 CET 2022


Commit: 6514bb05ea5a138d897151db02cb0bad9fe01968
Author: Hans Goudey
Date:   Thu Dec 15 14:18:57 2022 -0600
Branches: master
https://developer.blender.org/rB6514bb05ea5a138d897151db02cb0bad9fe01968

Mesh: Store active & default color attributes with strings

Attributes are unifying around a name-based API, and we would like to
be able to move away from CustomData in the future. This patch moves
the identification of active and fallback (render) color attributes
to strings on the mesh from flags on CustomDataLayer. This also
removes some ugliness used to retrieve these attributes and maintain
the active status.

The design is described more here: T98366

The patch keeps forward compatibility working until 4.0 with
the same method as the mesh struct of array refactors (T95965).

The strings are allowed to not correspond to an attribute, to allow
setting the active/default attribute independently of actually filling
its data. When applying a modifier, if the strings don't match an
attribute, they will be removed.

The realize instances / join node and join operator take the names from
the first / active input mesh. While other heuristics may be helpful
(and could be a future improvement), just using the first is simple
and predictable.

Differential Revision: https://developer.blender.org/D15169

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

M	source/blender/blenkernel/BKE_attribute.h
M	source/blender/blenkernel/BKE_mesh_legacy_convert.h
M	source/blender/blenkernel/intern/attribute.cc
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/blenkernel/intern/mesh_remesh_voxel.cc
M	source/blender/blenkernel/intern/paint.cc
M	source/blender/blenkernel/intern/pbvh.c
M	source/blender/blenloader/intern/versioning_300.cc
M	source/blender/blenloader/intern/versioning_400.cc
M	source/blender/draw/DRW_pbvh.h
M	source/blender/draw/intern/draw_cache_extract_mesh_render_data.cc
M	source/blender/draw/intern/draw_cache_impl_mesh.cc
M	source/blender/draw/intern/draw_cache_impl_particles.c
M	source/blender/draw/intern/draw_manager_data.cc
M	source/blender/draw/intern/draw_pbvh.cc
M	source/blender/editors/geometry/geometry_attributes.cc
M	source/blender/editors/mesh/mesh_data.cc
M	source/blender/editors/object/object_bake_api.c
M	source/blender/editors/object/object_modifier.cc
M	source/blender/editors/sculpt_paint/paint_image_proj.cc
M	source/blender/editors/sculpt_paint/paint_vertex.cc
M	source/blender/editors/sculpt_paint/paint_vertex_color_ops.cc
M	source/blender/editors/sculpt_paint/sculpt.cc
M	source/blender/editors/sculpt_paint/sculpt_undo.c
M	source/blender/freestyle/intern/blender_interface/BlenderStrokeRenderer.cpp
M	source/blender/geometry/intern/realize_instances.cc
M	source/blender/gpu/intern/gpu_shader_builder_stubs.cc
M	source/blender/io/collada/MeshImporter.cpp
M	source/blender/io/wavefront_obj/exporter/obj_export_file_writer.cc
M	source/blender/io/wavefront_obj/importer/obj_import_mesh.cc
M	source/blender/makesdna/DNA_customdata_types.h
M	source/blender/makesdna/DNA_mesh_types.h
M	source/blender/makesrna/intern/rna_attribute.c
M	source/blender/makesrna/intern/rna_material.c
M	source/blender/makesrna/intern/rna_mesh.c

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

diff --git a/source/blender/blenkernel/BKE_attribute.h b/source/blender/blenkernel/BKE_attribute.h
index 3f4981993eb..eef1459be81 100644
--- a/source/blender/blenkernel/BKE_attribute.h
+++ b/source/blender/blenkernel/BKE_attribute.h
@@ -105,16 +105,6 @@ int BKE_id_attribute_to_index(const struct ID *id,
                               eAttrDomainMask domain_mask,
                               eCustomDataMask layer_mask);
 
-struct CustomDataLayer *BKE_id_attribute_subset_active_get(const struct ID *id,
-                                                           int active_flag,
-                                                           eAttrDomainMask domain_mask,
-                                                           eCustomDataMask mask);
-void BKE_id_attribute_subset_active_set(struct ID *id,
-                                        struct CustomDataLayer *layer,
-                                        int active_flag,
-                                        eAttrDomainMask domain_mask,
-                                        eCustomDataMask mask);
-
 /**
  * Sets up a temporary ID with arbitrary CustomData domains. `r_id` will
  * be zero initialized with ID type id_type and any non-nullptr
@@ -130,10 +120,13 @@ void BKE_id_attribute_copy_domains_temp(short id_type,
                                         const struct CustomData *cdata,
                                         struct ID *r_id);
 
+const char *BKE_id_attributes_active_color_name(const struct ID *id);
+const char *BKE_id_attributes_default_color_name(const struct ID *id);
+
 struct CustomDataLayer *BKE_id_attributes_active_color_get(const struct ID *id);
-void BKE_id_attributes_active_color_set(struct ID *id, struct CustomDataLayer *active_layer);
-struct CustomDataLayer *BKE_id_attributes_render_color_get(const struct ID *id);
-void BKE_id_attributes_render_color_set(struct ID *id, struct CustomDataLayer *active_layer);
+void BKE_id_attributes_active_color_set(struct ID *id, const char *name);
+struct CustomDataLayer *BKE_id_attributes_default_color_get(const struct ID *id);
+void BKE_id_attributes_default_color_set(struct ID *id, const char *name);
 struct CustomDataLayer *BKE_id_attributes_color_find(const struct ID *id, const char *name);
 
 bool BKE_id_attribute_calc_unique_name(struct ID *id, const char *name, char *outname);
diff --git a/source/blender/blenkernel/BKE_mesh_legacy_convert.h b/source/blender/blenkernel/BKE_mesh_legacy_convert.h
index e46942aa9e9..65804e9ac24 100644
--- a/source/blender/blenkernel/BKE_mesh_legacy_convert.h
+++ b/source/blender/blenkernel/BKE_mesh_legacy_convert.h
@@ -85,6 +85,9 @@ void BKE_mesh_legacy_convert_mpoly_to_material_indices(struct Mesh *mesh);
 /** Convert from runtime loose edge cache to legacy edge flag. */
 void BKE_mesh_legacy_convert_loose_edges_to_flag(struct Mesh *mesh);
 
+void BKE_mesh_legacy_attribute_flags_to_strings(struct Mesh *mesh);
+void BKE_mesh_legacy_attribute_strings_to_flags(struct Mesh *mesh);
+
 #endif
 
 /**
diff --git a/source/blender/blenkernel/intern/attribute.cc b/source/blender/blenkernel/intern/attribute.cc
index 80647362826..58990c5c024 100644
--- a/source/blender/blenkernel/intern/attribute.cc
+++ b/source/blender/blenkernel/intern/attribute.cc
@@ -145,6 +145,7 @@ bool BKE_id_attribute_rename(ID *id,
                              const char *new_name,
                              ReportList *reports)
 {
+  using namespace blender;
   if (BKE_id_attribute_required(id, old_name)) {
     BLI_assert_msg(0, "Required attribute name is not editable");
     return false;
@@ -166,6 +167,14 @@ bool BKE_id_attribute_rename(ID *id,
 
   char result_name[MAX_CUSTOMDATA_LAYER_NAME];
   BKE_id_attribute_calc_unique_name(id, new_name, result_name);
+
+  if (StringRef(old_name) == BKE_id_attributes_active_color_name(id)) {
+    BKE_id_attributes_active_color_set(id, result_name);
+  }
+  if (StringRef(old_name) == BKE_id_attributes_default_color_name(id)) {
+    BKE_id_attributes_default_color_set(id, result_name);
+  }
+
   BLI_strncpy_utf8(layer->name, result_name, sizeof(layer->name));
 
   return true;
@@ -287,6 +296,7 @@ CustomDataLayer *BKE_id_attribute_duplicate(ID *id, const char *name, ReportList
 
 bool BKE_id_attribute_remove(ID *id, const char *name, ReportList *reports)
 {
+  using namespace blender;
   using namespace blender::bke;
   if (!name || name[0] == '\0') {
     BKE_report(reports, RPT_ERROR, "The attribute name must not be empty");
@@ -306,6 +316,12 @@ bool BKE_id_attribute_remove(ID *id, const char *name, ReportList *reports)
       for (const int domain : IndexRange(ATTR_DOMAIN_NUM)) {
         if (CustomData *data = info[domain].customdata) {
           if (BM_data_layer_free_named(em->bm, data, name)) {
+            if (name == StringRef(mesh->active_color_attribute)) {
+              MEM_SAFE_FREE(mesh->active_color_attribute);
+            }
+            else if (name == StringRef(mesh->default_color_attribute)) {
+              MEM_SAFE_FREE(mesh->default_color_attribute);
+            }
             return true;
           }
         }
@@ -327,6 +343,9 @@ CustomDataLayer *BKE_id_attribute_find(const ID *id,
                                        const int type,
                                        const eAttrDomain domain)
 {
+  if (!name) {
+    return nullptr;
+  }
   DomainInfo info[ATTR_DOMAIN_NUM];
   get_domains(id, info);
 
@@ -350,6 +369,9 @@ CustomDataLayer *BKE_id_attribute_search(ID *id,
                                          const eCustomDataMask type_mask,
                                          const eAttrDomainMask domain_mask)
 {
+  if (!name) {
+    return nullptr;
+  }
   DomainInfo info[ATTR_DOMAIN_NUM];
   get_domains(id, info);
 
@@ -633,104 +655,69 @@ int BKE_id_attribute_to_index(const ID *id,
   return -1;
 }
 
-CustomDataLayer *BKE_id_attribute_subset_active_get(const ID *id,
-                                                    int active_flag,
-                                                    eAttrDomainMask domain_mask,
-                                                    eCustomDataMask mask)
+const char *BKE_id_attributes_active_color_name(const ID *id)
 {
-  DomainInfo info[ATTR_DOMAIN_NUM];
-  eAttrDomain domains[ATTR_DOMAIN_NUM];
-
-  get_domains_types(domains);
-  get_domains(id, info);
-
-  CustomDataLayer *candidate = nullptr;
-  for (int i = 0; i < ARRAY_SIZE(domains); i++) {
-    if (!((1 << domains[i]) & domain_mask) || !info[domains[i]].customdata) {
-      continue;
-    }
-
-    CustomData *cdata = info[domains[i]].customdata;
-
-    for (int j = 0; j < cdata->totlayer; j++) {
-      CustomDataLayer *layer = cdata->layers + j;
-
-      if (!(CD_TYPE_AS_MASK(layer->type) & mask) || (layer->flag & CD_FLAG_TEMPORARY)) {
-        continue;
-      }
-
-      if (layer->flag & active_flag) {
-        return layer;
-      }
-
-      candidate = layer;
-    }
+  if (GS(id->name) == ID_ME) {
+    return reinterpret_cast<const Mesh *>(id)->active_color_attribute;
   }
-
-  return candidate;
+  return nullptr;
 }
 
-void BKE_id_attribute_subset_active_set(ID *id,
-                                        CustomDataLayer *layer,
-                                        int active_flag,
-                                        eAttrDomainMask domain_mask,
-                                        eCustomDataMask mask)
+const char *BKE_id_attributes_default_color_name(const ID *id)
 {
-  DomainInfo info[ATTR_DOMAIN_NUM];
-  eAttrDomain domains[ATTR_DOMAIN_NUM];
-
-  get_domains_types(domains);
-  get_domains(id, info);
-
-  for (int i = 0; i < ATTR_DOMAIN_NUM; i++) {
-    eAttrDomainMask domain_mask2 = (eAttrDomainMask)(1 << domains[i]);
-
-    if (!(domain_mask2 & domain_mask) || !info[domains[i]].customdata) {
-      continue;
-    }
-
-    CustomData *cdata = info[domains[i]].customdata;
-
-    for (int j = 0; j < cdata->totlayer; j++) {
-      CustomDataLayer *layer_iter = cdata->layers + j;
-
-      if (!(CD_TYPE_AS_MASK(layer_iter->type) & mask) || (layer_iter->flag & CD_FLAG_TEMPORARY)) {
-        continue;
-      }
-
-      layer_iter->flag &= ~active_flag;
-    }
+  if (GS(id->name) == ID_ME) {
+    return reinterpret_cast<const Mesh *>(id)->default_color_attribute;
   }
-
-  layer->flag |= active_flag;
+  return nullptr;
 }
 
 CustomDataLayer *BKE_id_attributes_active_color_get(const ID *id)
 {
-  return BKE_id_attribute_subset_active_get(
-      id, CD_FLAG_COLOR_ACTIVE, ATTR_DOMAIN_MASK_COLOR, CD_MASK_COLOR_ALL);
+  return BKE_id_attributes_color_find(id, BKE_id_attributes_active_color_name(id));
 }
 
-void BKE_id_attributes_active_color_set(ID *id, CustomDataLayer *active_layer)
+void BKE_id_attributes_active_color_set(ID *id, const char *name)
 {
-  BKE_id_attribute_subset_active_set(
-      id, active_layer, CD_FLAG_COLOR_ACTIVE, ATTR_DOMAIN_MASK_COLOR, CD_MASK_COLOR_ALL);
+  switch (GS(id->name)) {
+    case ID_ME: {
+      Mesh *mesh = reinterpret_cast<Mesh *>(id);
+      MEM_SAFE_FREE(mesh->active_color_attribute);
+      if (name) {
+        mesh->active_color_attribute = BLI_strdup(name);
+      }
+      break;
+    }
+    default:
+      break;
+  }
 }
 
-CustomDataLayer *BKE_id_attributes_render_color_get(const ID *id)
+CustomDataLayer *BKE_id_attributes_default_color_get(const ID *id)
 {
-  return BKE_id_attribute_subset_active_get(
-      id, CD_FLAG_COLOR_RENDER, ATTR_DOMAIN_MASK_COLOR, CD_MASK_COLOR_ALL);
+  return BKE_id_attributes_color_find(id, BKE_id_attributes_default_color_name(id));
 }
 
-void BKE_id_attributes_render_color_set(ID *id, CustomDataLayer *active_layer)
+void BKE_id_attributes_default_color_set(ID *id, const char *name)
 {
-  BKE_id_attribute_subset_active_set(
-      id, active_layer, CD_FLAG_COLOR_RENDER, ATTR_DOMAIN_MASK_COLOR, CD_MASK_COLOR_ALL);
+  switch (GS(id->name)) {
+    case ID_ME: {
+      Mesh *mesh = reinterpret_cast<Mesh *>(id);
+      MEM_SAFE_FREE(mesh->default_color_attribute);
+      if (name) {
+        mesh->default_color_attribute = BLI_strdup(name);
+      }
+      break;
+    }
+    default:
+      break;
+  }
 }
 
 CustomDataLayer *BKE_id_attributes_color_find(const ID *id, const char *name)
 {
+  if (!name) {
+    return nullptr;
+  }
   CustomDataLayer *layer = 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list