diff options
author | Hans Goudey <h.goudey@me.com> | 2022-06-14 17:25:24 +0300 |
---|---|---|
committer | Hans Goudey <h.goudey@me.com> | 2022-06-14 17:41:43 +0300 |
commit | 58a67e6fb6df527ed45c0a554efd387e770bdc42 (patch) | |
tree | ea4fdb342e7d98a236dd929d2e8fbbbbdcb72e8a /source/blender/blenkernel/intern | |
parent | 3012eca3503779697208f24d9f0a09494cd51455 (diff) |
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`.
Diffstat (limited to 'source/blender/blenkernel/intern')
-rw-r--r-- | source/blender/blenkernel/intern/attribute.cc | 53 |
1 files changed, 18 insertions, 35 deletions
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) |