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