Blender  V2.93
rna_mesh_utils.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #pragma once
22 
23 /* Macros to help reduce code clutter in rna_mesh.c */
24 
25 /* Define the accessors for a basic CustomDataLayer collection */
26 #define DEFINE_CUSTOMDATA_LAYER_COLLECTION(collection_name, customdata_type, layer_type) \
27  /* check */ \
28  static int rna_##collection_name##_check(CollectionPropertyIterator *UNUSED(iter), void *data) \
29  { \
30  CustomDataLayer *layer = (CustomDataLayer *)data; \
31  return (layer->type != layer_type); \
32  } \
33  /* begin */ \
34  static void rna_Mesh_##collection_name##s_begin(CollectionPropertyIterator *iter, \
35  PointerRNA *ptr) \
36  { \
37  CustomData *data = rna_mesh_##customdata_type(ptr); \
38  if (data) { \
39  rna_iterator_array_begin(iter, \
40  (void *)data->layers, \
41  sizeof(CustomDataLayer), \
42  data->totlayer, \
43  0, \
44  rna_##collection_name##_check); \
45  } \
46  else { \
47  rna_iterator_array_begin(iter, NULL, 0, 0, 0, NULL); \
48  } \
49  } \
50  /* length */ \
51  static int rna_Mesh_##collection_name##s_length(PointerRNA *ptr) \
52  { \
53  CustomData *data = rna_mesh_##customdata_type(ptr); \
54  return data ? CustomData_number_of_layers(data, layer_type) : 0; \
55  } \
56  /* index range */ \
57  static void rna_Mesh_##collection_name##_index_range( \
58  PointerRNA *ptr, int *min, int *max, int *UNUSED(softmin), int *UNUSED(softmax)) \
59  { \
60  CustomData *data = rna_mesh_##customdata_type(ptr); \
61  *min = 0; \
62  *max = data ? CustomData_number_of_layers(data, layer_type) - 1 : 0; \
63  *max = MAX2(0, *max); \
64  }
65 
66 /* Define the accessors for special CustomDataLayers in the collection
67  * (active, render, clone, stencil, etc) */
68 #define DEFINE_CUSTOMDATA_LAYER_COLLECTION_ACTIVEITEM( \
69  collection_name, customdata_type, layer_type, active_type, layer_rna_type) \
70 \
71  static PointerRNA rna_Mesh_##collection_name##_##active_type##_get(PointerRNA *ptr) \
72  { \
73  CustomData *data = rna_mesh_##customdata_type(ptr); \
74  CustomDataLayer *layer; \
75  if (data) { \
76  int index = CustomData_get_##active_type##_layer_index(data, layer_type); \
77  layer = (index == -1) ? NULL : &data->layers[index]; \
78  } \
79  else { \
80  layer = NULL; \
81  } \
82  return rna_pointer_inherit_refine(ptr, &RNA_##layer_rna_type, layer); \
83  } \
84 \
85  static void rna_Mesh_##collection_name##_##active_type##_set( \
86  PointerRNA *ptr, PointerRNA value, struct ReportList *UNUSED(reports)) \
87  { \
88  Mesh *me = rna_mesh(ptr); \
89  CustomData *data = rna_mesh_##customdata_type(ptr); \
90  int a; \
91  if (data) { \
92  CustomDataLayer *layer; \
93  int layer_index = CustomData_get_layer_index(data, layer_type); \
94  for (layer = data->layers + layer_index, a = 0; layer_index + a < data->totlayer; \
95  layer++, a++) { \
96  if (value.data == layer) { \
97  CustomData_set_layer_##active_type(data, layer_type, a); \
98  BKE_mesh_update_customdata_pointers(me, true); \
99  return; \
100  } \
101  } \
102  } \
103  } \
104 \
105  static int rna_Mesh_##collection_name##_##active_type##_index_get(PointerRNA *ptr) \
106  { \
107  CustomData *data = rna_mesh_##customdata_type(ptr); \
108  if (data) { \
109  return CustomData_get_##active_type##_layer(data, layer_type); \
110  } \
111  else { \
112  return 0; \
113  } \
114  } \
115 \
116  static void rna_Mesh_##collection_name##_##active_type##_index_set(PointerRNA *ptr, int value) \
117  { \
118  Mesh *me = rna_mesh(ptr); \
119  CustomData *data = rna_mesh_##customdata_type(ptr); \
120  if (data) { \
121  CustomData_set_layer_##active_type(data, layer_type, value); \
122  BKE_mesh_update_customdata_pointers(me, true); \
123  } \
124  }