From 874244e67f436ff7bc7cc59a146993ca69a47455 Mon Sep 17 00:00:00 2001 From: Martijn Versteegh Date: Wed, 15 Feb 2023 15:10:12 +0100 Subject: [PATCH 1/4] Fix: Skip anonymous uv layers for the python API --- source/blender/blenkernel/BKE_customdata.h | 1 + .../blender/blenkernel/intern/customdata.cc | 14 +++++++ source/blender/makesrna/intern/rna_mesh.c | 2 +- .../blender/makesrna/intern/rna_mesh_utils.h | 42 +++++++++++++++++++ 4 files changed, 58 insertions(+), 1 deletion(-) diff --git a/source/blender/blenkernel/BKE_customdata.h b/source/blender/blenkernel/BKE_customdata.h index 9cddc39f43c..297cd3c2433 100644 --- a/source/blender/blenkernel/BKE_customdata.h +++ b/source/blender/blenkernel/BKE_customdata.h @@ -282,6 +282,7 @@ bool CustomData_has_layer(const struct CustomData *data, int type); * Returns the number of layers with this type. */ int CustomData_number_of_layers(const struct CustomData *data, int type); +int CustomData_number_of_anonymous_layers(const struct CustomData *data, int type); int CustomData_number_of_layers_typemask(const struct CustomData *data, eCustomDataMask mask); /** diff --git a/source/blender/blenkernel/intern/customdata.cc b/source/blender/blenkernel/intern/customdata.cc index 75a8c865170..ef6f458ee40 100644 --- a/source/blender/blenkernel/intern/customdata.cc +++ b/source/blender/blenkernel/intern/customdata.cc @@ -2991,6 +2991,20 @@ int CustomData_number_of_layers(const CustomData *data, const int type) return number; } +int CustomData_number_of_anonymous_layers(const CustomData *data, const int type) +{ + int number = 0; + + for (int i = 0; i < data->totlayer; i++) { + if (data->layers[i].type == type && data->layers[i].anonymous_id != nullptr) { + number++; + } + } + + return number; +} + + int CustomData_number_of_layers_typemask(const CustomData *data, const eCustomDataMask mask) { int number = 0; diff --git a/source/blender/makesrna/intern/rna_mesh.c b/source/blender/makesrna/intern/rna_mesh.c index 0f9b11905fa..7d353118cc0 100644 --- a/source/blender/makesrna/intern/rna_mesh.c +++ b/source/blender/makesrna/intern/rna_mesh.c @@ -964,7 +964,7 @@ static void rna_MPoly_freestyle_face_mark_set(PointerRNA *ptr, bool value) /* uv_layers */ -DEFINE_CUSTOMDATA_LAYER_COLLECTION(uv_layer, ldata, CD_PROP_FLOAT2) +DEFINE_CUSTOMDATA_LAYER_COLLECTION_SKIP_ANONYMOUS(uv_layer, ldata, CD_PROP_FLOAT2) DEFINE_CUSTOMDATA_LAYER_COLLECTION_ACTIVEITEM( uv_layer, ldata, CD_PROP_FLOAT2, active, MeshUVLoopLayer) DEFINE_CUSTOMDATA_LAYER_COLLECTION_ACTIVEITEM( diff --git a/source/blender/makesrna/intern/rna_mesh_utils.h b/source/blender/makesrna/intern/rna_mesh_utils.h index 495c58d7b30..b53eb9c0077 100644 --- a/source/blender/makesrna/intern/rna_mesh_utils.h +++ b/source/blender/makesrna/intern/rna_mesh_utils.h @@ -49,6 +49,48 @@ *max = MAX2(0, *max); \ } +/* Define the accessors for a basic CustomDataLayer collection, skipping anonymous layers */ +#define DEFINE_CUSTOMDATA_LAYER_COLLECTION_SKIP_ANONYMOUS(collection_name, customdata_type, layer_type) \ + /* check */ \ + static int rna_##collection_name##_check(CollectionPropertyIterator *UNUSED(iter), void *data) \ + { \ + CustomDataLayer *layer = (CustomDataLayer *)data; \ + return (layer->anonymous_id != NULL || layer->type != layer_type); \ + } \ + /* begin */ \ + static void rna_Mesh_##collection_name##s_begin(CollectionPropertyIterator *iter, \ + PointerRNA *ptr) \ + { \ + CustomData *data = rna_mesh_##customdata_type(ptr); \ + if (data) { \ + rna_iterator_array_begin(iter, \ + (void *)data->layers, \ + sizeof(CustomDataLayer), \ + data->totlayer, \ + 0, \ + rna_##collection_name##_check); \ + } \ + else { \ + rna_iterator_array_begin(iter, NULL, 0, 0, 0, NULL); \ + } \ + } \ + /* length */ \ + static int rna_Mesh_##collection_name##s_length(PointerRNA *ptr) \ + { \ + CustomData *data = rna_mesh_##customdata_type(ptr); \ + return data ? CustomData_number_of_layers(data, layer_type) - CustomData_number_of_anonymous_layers(data, layer_type) : 0; \ + } \ + /* index range */ \ + static void rna_Mesh_##collection_name##_index_range( \ + PointerRNA *ptr, int *min, int *max, int *UNUSED(softmin), int *UNUSED(softmax)) \ + { \ + CustomData *data = rna_mesh_##customdata_type(ptr); \ + *min = 0; \ + *max = data ? CustomData_number_of_layers(data, layer_type) - CustomData_number_of_anonymous_layers(data, layer_type) - 1 : 0; \ + *max = MAX2(0, *max); \ + } + + /* Define the accessors for special CustomDataLayers in the collection * (active, render, clone, stencil, etc) */ #define DEFINE_CUSTOMDATA_LAYER_COLLECTION_ACTIVEITEM( \ -- 2.30.2 From 65bf552140a030e7fc6d37a0318ca82e8f3e475c Mon Sep 17 00:00:00 2001 From: Martijn Versteegh Date: Wed, 15 Feb 2023 19:13:51 +0100 Subject: [PATCH 2/4] Always skip anonymous layers. --- source/blender/makesrna/intern/rna_mesh.c | 2 +- .../blender/makesrna/intern/rna_mesh_utils.h | 42 +------------------ 2 files changed, 2 insertions(+), 42 deletions(-) diff --git a/source/blender/makesrna/intern/rna_mesh.c b/source/blender/makesrna/intern/rna_mesh.c index 7d353118cc0..0f9b11905fa 100644 --- a/source/blender/makesrna/intern/rna_mesh.c +++ b/source/blender/makesrna/intern/rna_mesh.c @@ -964,7 +964,7 @@ static void rna_MPoly_freestyle_face_mark_set(PointerRNA *ptr, bool value) /* uv_layers */ -DEFINE_CUSTOMDATA_LAYER_COLLECTION_SKIP_ANONYMOUS(uv_layer, ldata, CD_PROP_FLOAT2) +DEFINE_CUSTOMDATA_LAYER_COLLECTION(uv_layer, ldata, CD_PROP_FLOAT2) DEFINE_CUSTOMDATA_LAYER_COLLECTION_ACTIVEITEM( uv_layer, ldata, CD_PROP_FLOAT2, active, MeshUVLoopLayer) DEFINE_CUSTOMDATA_LAYER_COLLECTION_ACTIVEITEM( diff --git a/source/blender/makesrna/intern/rna_mesh_utils.h b/source/blender/makesrna/intern/rna_mesh_utils.h index b53eb9c0077..933202f116c 100644 --- a/source/blender/makesrna/intern/rna_mesh_utils.h +++ b/source/blender/makesrna/intern/rna_mesh_utils.h @@ -8,49 +8,9 @@ /* Macros to help reduce code clutter in rna_mesh.c */ -/* Define the accessors for a basic CustomDataLayer collection */ -#define DEFINE_CUSTOMDATA_LAYER_COLLECTION(collection_name, customdata_type, layer_type) \ - /* check */ \ - static int rna_##collection_name##_check(CollectionPropertyIterator *UNUSED(iter), void *data) \ - { \ - CustomDataLayer *layer = (CustomDataLayer *)data; \ - return (layer->type != layer_type); \ - } \ - /* begin */ \ - static void rna_Mesh_##collection_name##s_begin(CollectionPropertyIterator *iter, \ - PointerRNA *ptr) \ - { \ - CustomData *data = rna_mesh_##customdata_type(ptr); \ - if (data) { \ - rna_iterator_array_begin(iter, \ - (void *)data->layers, \ - sizeof(CustomDataLayer), \ - data->totlayer, \ - 0, \ - rna_##collection_name##_check); \ - } \ - else { \ - rna_iterator_array_begin(iter, NULL, 0, 0, 0, NULL); \ - } \ - } \ - /* length */ \ - static int rna_Mesh_##collection_name##s_length(PointerRNA *ptr) \ - { \ - CustomData *data = rna_mesh_##customdata_type(ptr); \ - return data ? CustomData_number_of_layers(data, layer_type) : 0; \ - } \ - /* index range */ \ - static void rna_Mesh_##collection_name##_index_range( \ - PointerRNA *ptr, int *min, int *max, int *UNUSED(softmin), int *UNUSED(softmax)) \ - { \ - CustomData *data = rna_mesh_##customdata_type(ptr); \ - *min = 0; \ - *max = data ? CustomData_number_of_layers(data, layer_type) - 1 : 0; \ - *max = MAX2(0, *max); \ - } /* Define the accessors for a basic CustomDataLayer collection, skipping anonymous layers */ -#define DEFINE_CUSTOMDATA_LAYER_COLLECTION_SKIP_ANONYMOUS(collection_name, customdata_type, layer_type) \ +#define DEFINE_CUSTOMDATA_LAYER_COLLECTION(collection_name, customdata_type, layer_type) \ /* check */ \ static int rna_##collection_name##_check(CollectionPropertyIterator *UNUSED(iter), void *data) \ { \ -- 2.30.2 From fb23cdde01013182246d24d9a12be06129635e1a Mon Sep 17 00:00:00 2001 From: Martijn Versteegh Date: Wed, 15 Feb 2023 19:20:02 +0100 Subject: [PATCH 3/4] Use more modern syntax for the loops to count number of layers --- source/blender/blenkernel/intern/customdata.cc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/source/blender/blenkernel/intern/customdata.cc b/source/blender/blenkernel/intern/customdata.cc index ef6f458ee40..0a5a8eb91bf 100644 --- a/source/blender/blenkernel/intern/customdata.cc +++ b/source/blender/blenkernel/intern/customdata.cc @@ -2982,8 +2982,8 @@ int CustomData_number_of_layers(const CustomData *data, const int type) { int number = 0; - for (int i = 0; i < data->totlayer; i++) { - if (data->layers[i].type == type) { + for (const CustomDataLayer &layer : Span(data->layers, data->totlayer)) { + if (layer.type == type) { number++; } } @@ -2995,8 +2995,8 @@ int CustomData_number_of_anonymous_layers(const CustomData *data, const int type { int number = 0; - for (int i = 0; i < data->totlayer; i++) { - if (data->layers[i].type == type && data->layers[i].anonymous_id != nullptr) { + for (const CustomDataLayer &layer : Span(data->layers, data->totlayer)) { + if (layer.type == type && layer.anonymous_id != nullptr) { number++; } } @@ -3009,8 +3009,8 @@ int CustomData_number_of_layers_typemask(const CustomData *data, const eCustomDa { int number = 0; - for (int i = 0; i < data->totlayer; i++) { - if (mask & CD_TYPE_AS_MASK(data->layers[i].type)) { + for (const CustomDataLayer &layer : Span(data->layers, data->totlayer)) { + if (mask & CD_TYPE_AS_MASK(layer.type)) { number++; } } -- 2.30.2 From ecbf269d7fdd6340233dd3f68a9a04236b9899c8 Mon Sep 17 00:00:00 2001 From: Martijn Versteegh Date: Wed, 15 Feb 2023 21:51:32 +0100 Subject: [PATCH 4/4] Revert "Use more modern syntax for the loops to count number of layers" This reverts commit fb23cdde01013182246d24d9a12be06129635e1a. --- source/blender/blenkernel/intern/customdata.cc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/source/blender/blenkernel/intern/customdata.cc b/source/blender/blenkernel/intern/customdata.cc index 0a5a8eb91bf..ef6f458ee40 100644 --- a/source/blender/blenkernel/intern/customdata.cc +++ b/source/blender/blenkernel/intern/customdata.cc @@ -2982,8 +2982,8 @@ int CustomData_number_of_layers(const CustomData *data, const int type) { int number = 0; - for (const CustomDataLayer &layer : Span(data->layers, data->totlayer)) { - if (layer.type == type) { + for (int i = 0; i < data->totlayer; i++) { + if (data->layers[i].type == type) { number++; } } @@ -2995,8 +2995,8 @@ int CustomData_number_of_anonymous_layers(const CustomData *data, const int type { int number = 0; - for (const CustomDataLayer &layer : Span(data->layers, data->totlayer)) { - if (layer.type == type && layer.anonymous_id != nullptr) { + for (int i = 0; i < data->totlayer; i++) { + if (data->layers[i].type == type && data->layers[i].anonymous_id != nullptr) { number++; } } @@ -3009,8 +3009,8 @@ int CustomData_number_of_layers_typemask(const CustomData *data, const eCustomDa { int number = 0; - for (const CustomDataLayer &layer : Span(data->layers, data->totlayer)) { - if (mask & CD_TYPE_AS_MASK(layer.type)) { + for (int i = 0; i < data->totlayer; i++) { + if (mask & CD_TYPE_AS_MASK(data->layers[i].type)) { number++; } } -- 2.30.2