From 7974d2bff6f363b75c8317cf1dad3e943fd85df6 Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Tue, 7 Jun 2022 18:00:18 +0200 Subject: CustomData: Add function to free a named layer This can be useful to avoid unnecessary boilerplate in various users of the CustomData API. Split from D14685 and D14769. --- source/blender/blenkernel/BKE_customdata.h | 1 + source/blender/blenkernel/intern/customdata.cc | 15 +++++++++++++++ 2 files changed, 16 insertions(+) (limited to 'source/blender/blenkernel') diff --git a/source/blender/blenkernel/BKE_customdata.h b/source/blender/blenkernel/BKE_customdata.h index 993ab476830..a1101c1df44 100644 --- a/source/blender/blenkernel/BKE_customdata.h +++ b/source/blender/blenkernel/BKE_customdata.h @@ -225,6 +225,7 @@ void *CustomData_add_layer_anonymous(struct CustomData *data, * In edit-mode, use #EDBM_data_layer_free instead of this function. */ bool CustomData_free_layer(struct CustomData *data, int type, int totelem, int index); +bool CustomData_free_layer_named(struct CustomData *data, const char *name, const int totelem); /** * Frees the layer index with the give type. diff --git a/source/blender/blenkernel/intern/customdata.cc b/source/blender/blenkernel/intern/customdata.cc index aed570235aa..bb5b2ee0836 100644 --- a/source/blender/blenkernel/intern/customdata.cc +++ b/source/blender/blenkernel/intern/customdata.cc @@ -20,6 +20,7 @@ #include "BLI_bitmap.h" #include "BLI_color.hh" #include "BLI_endian_switch.h" +#include "BLI_index_range.hh" #include "BLI_math.h" #include "BLI_math_color_blend.h" #include "BLI_math_vector.hh" @@ -27,6 +28,7 @@ #include "BLI_path_util.h" #include "BLI_span.hh" #include "BLI_string.h" +#include "BLI_string_ref.hh" #include "BLI_string_utils.h" #include "BLI_utildefines.h" @@ -57,6 +59,7 @@ using blender::IndexRange; using blender::Span; +using blender::StringRef; using blender::Vector; /* number of layers to add when growing a CustomData object */ @@ -2899,6 +2902,18 @@ bool CustomData_free_layer(CustomData *data, int type, int totelem, int index) return true; } +bool CustomData_free_layer_named(CustomData *data, const char *name, const int totelem) +{ + for (const int i : IndexRange(data->totlayer)) { + const CustomDataLayer &layer = data->layers[i]; + if (StringRef(layer.name) == name) { + CustomData_free_layer(data, layer.type, totelem, i); + return true; + } + } + return false; +} + bool CustomData_free_layer_active(CustomData *data, int type, int totelem) { const int index = CustomData_get_active_layer_index(data, type); -- cgit v1.2.3