[Bf-blender-cvs] [58a67e6fb6d] master: Attributes: Adjustments to duplicate attribute API function

Hans Goudey noreply at git.blender.org
Tue Jun 14 16:42:06 CEST 2022


Commit: 58a67e6fb6df527ed45c0a554efd387e770bdc42
Author: Hans Goudey
Date:   Tue Jun 14 16:25:24 2022 +0200
Branches: master
https://developer.blender.org/rB58a67e6fb6df527ed45c0a554efd387e770bdc42

Attributes: Adjustments to duplicate attribute API function

Use a name argument, for the same reasons as 6eea5f70e3b79e3c668.
Also reuse the layer and unique name creation in `BKE_id_attribute_new`
instead of reimplementing it. Also include a few miscellaneous cleanups
like using const variables and `std::string`.

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

M	source/blender/blenkernel/BKE_attribute.h
M	source/blender/blenkernel/intern/attribute.cc
M	source/blender/editors/geometry/geometry_attributes.cc

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

diff --git a/source/blender/blenkernel/BKE_attribute.h b/source/blender/blenkernel/BKE_attribute.h
index 5755ddb3018..18993546e83 100644
--- a/source/blender/blenkernel/BKE_attribute.h
+++ b/source/blender/blenkernel/BKE_attribute.h
@@ -64,7 +64,7 @@ bool BKE_id_attribute_remove(struct ID *id, const char *name, struct ReportList
  * Creates a duplicate attribute layer.
  */
 struct CustomDataLayer *BKE_id_attribute_duplicate(struct ID *id,
-                                                   struct CustomDataLayer *layer,
+                                                   const char *name,
                                                    struct ReportList *reports);
 
 struct CustomDataLayer *BKE_id_attribute_find(const struct ID *id,
diff --git a/source/blender/blenkernel/intern/attribute.cc b/source/blender/blenkernel/intern/attribute.cc
index b2ea8b833b3..7bc51881f1f 100644
--- a/source/blender/blenkernel/intern/attribute.cc
+++ b/source/blender/blenkernel/intern/attribute.cc
@@ -208,53 +208,36 @@ CustomDataLayer *BKE_id_attribute_new(
   return (index == -1) ? nullptr : &(customdata->layers[index]);
 }
 
-CustomDataLayer *BKE_id_attribute_duplicate(ID *id, CustomDataLayer *layer, ReportList *reports)
+CustomDataLayer *BKE_id_attribute_duplicate(ID *id, const char *name, ReportList *reports)
 {
-  DomainInfo info[ATTR_DOMAIN_NUM];
-  get_domains(id, info);
-
-  eCustomDataType type = (eCustomDataType)layer->type;
-  eAttrDomain domain = BKE_id_attribute_domain(id, layer);
-
-  CustomData *customdata = info[domain].customdata;
-  if (customdata == nullptr) {
-    BKE_report(reports, RPT_ERROR, "Attribute domain not supported by this geometry type");
+  const CustomDataLayer *src_layer = BKE_id_attribute_search(
+      id, name, CD_MASK_PROP_ALL, ATTR_DOMAIN_MASK_ALL);
+  if (src_layer == nullptr) {
+    BKE_report(reports, RPT_ERROR, "Attribute is not part of this geometry");
     return nullptr;
   }
 
-  char name[MAX_CUSTOMDATA_LAYER_NAME];
-  char uniquename[MAX_CUSTOMDATA_LAYER_NAME];
+  const eCustomDataType type = (eCustomDataType)src_layer->type;
+  const eAttrDomain domain = BKE_id_attribute_domain(id, src_layer);
 
   /* Make a copy of name in case CustomData API reallocates the layers. */
-  BLI_strncpy(name, layer->name, MAX_CUSTOMDATA_LAYER_NAME);
-  BKE_id_attribute_calc_unique_name(id, layer->name, uniquename);
+  const std::string name_copy = name;
 
-  switch (GS(id->name)) {
-    case ID_ME: {
-      Mesh *me = (Mesh *)id;
-      BMEditMesh *em = me->edit_mesh;
-      if (em != nullptr) {
-        BM_data_layer_add_named(em->bm, customdata, type, uniquename);
-      }
-      else {
-        CustomData_add_layer_named(
-            customdata, type, CD_DEFAULT, nullptr, info[domain].length, uniquename);
-      }
-      break;
-    }
-    default: {
-      CustomData_add_layer_named(
-          customdata, type, CD_DEFAULT, nullptr, info[domain].length, uniquename);
-      break;
-    }
+  DomainInfo info[ATTR_DOMAIN_NUM];
+  get_domains(id, info);
+  CustomData *customdata = info[domain].customdata;
+
+  CustomDataLayer *new_layer = BKE_id_attribute_new(id, name_copy.c_str(), type, domain, reports);
+  if (new_layer == nullptr) {
+    return nullptr;
   }
 
-  int from_index = CustomData_get_named_layer_index(customdata, type, name);
-  int to_index = CustomData_get_named_layer_index(customdata, type, uniquename);
+  const int from_index = CustomData_get_named_layer_index(customdata, type, name_copy.c_str());
+  const int to_index = CustomData_get_named_layer_index(customdata, type, new_layer->name);
   CustomData_copy_data_layer(
       customdata, customdata, from_index, to_index, 0, 0, info[domain].length);
 
-  return (to_index == -1) ? nullptr : &(customdata->layers[to_index]);
+  return new_layer;
 }
 
 bool BKE_id_attribute_remove(ID *id, const char *name, ReportList *reports)
diff --git a/source/blender/editors/geometry/geometry_attributes.cc b/source/blender/editors/geometry/geometry_attributes.cc
index 1a3bf6a2958..79639097fbe 100644
--- a/source/blender/editors/geometry/geometry_attributes.cc
+++ b/source/blender/editors/geometry/geometry_attributes.cc
@@ -518,13 +518,13 @@ static int geometry_color_attribute_duplicate_exec(bContext *C, wmOperator *op)
 {
   Object *ob = ED_object_context(C);
   ID *id = static_cast<ID *>(ob->data);
-  CustomDataLayer *layer = BKE_id_attributes_active_color_get(id);
+  const CustomDataLayer *layer = BKE_id_attributes_active_color_get(id);
 
   if (layer == nullptr) {
     return OPERATOR_CANCELLED;
   }
 
-  CustomDataLayer *newLayer = BKE_id_attribute_duplicate(id, layer, op->reports);
+  CustomDataLayer *newLayer = BKE_id_attribute_duplicate(id, layer->name, op->reports);
 
   BKE_id_attributes_active_color_set(id, newLayer);



More information about the Bf-blender-cvs mailing list