Blender V5.0
rna_mesh_utils.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include "BKE_anonymous_attribute_id.hh" // IWYU pragma: export
12
13/* Macros to help reduce code clutter in rna_mesh.cc */
14
15/* Define the accessors for a basic CustomDataLayer collection, skipping anonymous layers */
16#define DEFINE_CUSTOMDATA_LAYER_COLLECTION(collection_name, customdata_type, layer_type) \
17 /* check */ \
18 [[maybe_unused]] static bool rna_##collection_name##_check(CollectionPropertyIterator *, \
19 void *data) \
20 { \
21 CustomDataLayer *layer = (CustomDataLayer *)data; \
22 return (blender::bke::attribute_name_is_anonymous(layer->name) || layer->type != layer_type); \
23 } \
24 /* begin */ \
25 [[maybe_unused]] static void rna_Mesh_##collection_name##s_begin( \
26 CollectionPropertyIterator *iter, PointerRNA *ptr) \
27 { \
28 CustomData *data = rna_mesh_##customdata_type(ptr); \
29 if (data) { \
30 rna_iterator_array_begin(iter, \
31 ptr, \
32 (void *)data->layers, \
33 sizeof(CustomDataLayer), \
34 data->totlayer, \
35 0, \
36 rna_##collection_name##_check); \
37 } \
38 else { \
39 rna_iterator_array_begin(iter, ptr, NULL, 0, 0, 0, NULL); \
40 } \
41 } \
42 /* length */ \
43 [[maybe_unused]] static int rna_Mesh_##collection_name##s_length(PointerRNA *ptr) \
44 { \
45 CustomData *data = rna_mesh_##customdata_type(ptr); \
46 return data ? CustomData_number_of_layers(data, layer_type) - \
47 CustomData_number_of_anonymous_layers(data, layer_type) : \
48 0; \
49 } \
50 /* index range */ \
51 [[maybe_unused]] static void rna_Mesh_##collection_name##_index_range( \
52 PointerRNA *ptr, int *min, int *max, int *, int *) \
53 { \
54 CustomData *data = rna_mesh_##customdata_type(ptr); \
55 *min = 0; \
56 *max = data ? CustomData_number_of_layers(data, layer_type) - \
57 CustomData_number_of_anonymous_layers(data, layer_type) - 1 : \
58 0; \
59 *max = std::max(0, *max); \
60 }
61
62/* Define the accessors for special CustomDataLayers in the collection
63 * (active, render, clone, stencil, etc) */
64#define DEFINE_CUSTOMDATA_LAYER_COLLECTION_ACTIVEITEM( \
65 collection_name, customdata_type, layer_type, active_type, layer_rna_type) \
66\
67 [[maybe_unused]] static PointerRNA rna_Mesh_##collection_name##_##active_type##_get( \
68 PointerRNA *ptr) \
69 { \
70 CustomData *data = rna_mesh_##customdata_type(ptr); \
71 CustomDataLayer *layer; \
72 if (data) { \
73 int index = CustomData_get_##active_type##_layer_index(data, layer_type); \
74 layer = (index == -1) ? NULL : &data->layers[index]; \
75 } \
76 else { \
77 layer = NULL; \
78 } \
79 return RNA_pointer_create_with_parent(*ptr, &RNA_##layer_rna_type, layer); \
80 } \
81\
82 [[maybe_unused]] static void rna_Mesh_##collection_name##_##active_type##_set( \
83 PointerRNA *ptr, PointerRNA value, ReportList *) \
84 { \
85 Mesh *mesh = rna_mesh(ptr); \
86 CustomData *data = rna_mesh_##customdata_type(ptr); \
87 int a; \
88 if (data) { \
89 CustomDataLayer *layer; \
90 int layer_index = CustomData_get_layer_index(data, layer_type); \
91 for (layer = data->layers + layer_index, a = 0; layer_index + a < data->totlayer; \
92 layer++, a++) \
93 { \
94 if (value.data == layer) { \
95 CustomData_set_layer_##active_type(data, layer_type, a); \
96 BKE_mesh_tessface_clear(mesh); \
97 return; \
98 } \
99 } \
100 } \
101 } \
102\
103 [[maybe_unused]] static int rna_Mesh_##collection_name##_##active_type##_index_get( \
104 PointerRNA *ptr) \
105 { \
106 CustomData *data = rna_mesh_##customdata_type(ptr); \
107 if (data) { \
108 return CustomData_get_##active_type##_layer(data, layer_type); \
109 } \
110 else { \
111 return 0; \
112 } \
113 } \
114\
115 [[maybe_unused]] static void rna_Mesh_##collection_name##_##active_type##_index_set( \
116 PointerRNA *ptr, int value) \
117 { \
118 Mesh *mesh = rna_mesh(ptr); \
119 CustomData *data = rna_mesh_##customdata_type(ptr); \
120 if (data) { \
121 if (UNLIKELY(value < 0)) { \
122 value = 0; \
123 } \
124 else if (value > 0) { \
125 value = min_ii(value, CustomData_number_of_layers(data, layer_type) - 1); \
126 } \
127 CustomData_set_layer_##active_type(data, layer_type, value); \
128 BKE_mesh_tessface_clear(mesh); \
129 } \
130 }