Blender  V3.3
rna_mesh_utils.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #pragma once
8 
9 /* Macros to help reduce code clutter in rna_mesh.c */
10 
11 /* Define the accessors for a basic CustomDataLayer collection */
12 #define DEFINE_CUSTOMDATA_LAYER_COLLECTION(collection_name, customdata_type, layer_type) \
13  /* check */ \
14  static int rna_##collection_name##_check(CollectionPropertyIterator *UNUSED(iter), void *data) \
15  { \
16  CustomDataLayer *layer = (CustomDataLayer *)data; \
17  return (layer->type != layer_type); \
18  } \
19  /* begin */ \
20  static void rna_Mesh_##collection_name##s_begin(CollectionPropertyIterator *iter, \
21  PointerRNA *ptr) \
22  { \
23  CustomData *data = rna_mesh_##customdata_type(ptr); \
24  if (data) { \
25  rna_iterator_array_begin(iter, \
26  (void *)data->layers, \
27  sizeof(CustomDataLayer), \
28  data->totlayer, \
29  0, \
30  rna_##collection_name##_check); \
31  } \
32  else { \
33  rna_iterator_array_begin(iter, NULL, 0, 0, 0, NULL); \
34  } \
35  } \
36  /* length */ \
37  static int rna_Mesh_##collection_name##s_length(PointerRNA *ptr) \
38  { \
39  CustomData *data = rna_mesh_##customdata_type(ptr); \
40  return data ? CustomData_number_of_layers(data, layer_type) : 0; \
41  } \
42  /* index range */ \
43  static void rna_Mesh_##collection_name##_index_range( \
44  PointerRNA *ptr, int *min, int *max, int *UNUSED(softmin), int *UNUSED(softmax)) \
45  { \
46  CustomData *data = rna_mesh_##customdata_type(ptr); \
47  *min = 0; \
48  *max = data ? CustomData_number_of_layers(data, layer_type) - 1 : 0; \
49  *max = MAX2(0, *max); \
50  }
51 
52 /* Define the accessors for special CustomDataLayers in the collection
53  * (active, render, clone, stencil, etc) */
54 #define DEFINE_CUSTOMDATA_LAYER_COLLECTION_ACTIVEITEM( \
55  collection_name, customdata_type, layer_type, active_type, layer_rna_type) \
56 \
57  static PointerRNA rna_Mesh_##collection_name##_##active_type##_get(PointerRNA *ptr) \
58  { \
59  CustomData *data = rna_mesh_##customdata_type(ptr); \
60  CustomDataLayer *layer; \
61  if (data) { \
62  int index = CustomData_get_##active_type##_layer_index(data, layer_type); \
63  layer = (index == -1) ? NULL : &data->layers[index]; \
64  } \
65  else { \
66  layer = NULL; \
67  } \
68  return rna_pointer_inherit_refine(ptr, &RNA_##layer_rna_type, layer); \
69  } \
70 \
71  static void rna_Mesh_##collection_name##_##active_type##_set( \
72  PointerRNA *ptr, PointerRNA value, struct ReportList *UNUSED(reports)) \
73  { \
74  Mesh *me = rna_mesh(ptr); \
75  CustomData *data = rna_mesh_##customdata_type(ptr); \
76  int a; \
77  if (data) { \
78  CustomDataLayer *layer; \
79  int layer_index = CustomData_get_layer_index(data, layer_type); \
80  for (layer = data->layers + layer_index, a = 0; layer_index + a < data->totlayer; \
81  layer++, a++) { \
82  if (value.data == layer) { \
83  CustomData_set_layer_##active_type(data, layer_type, a); \
84  BKE_mesh_update_customdata_pointers(me, true); \
85  return; \
86  } \
87  } \
88  } \
89  } \
90 \
91  static int rna_Mesh_##collection_name##_##active_type##_index_get(PointerRNA *ptr) \
92  { \
93  CustomData *data = rna_mesh_##customdata_type(ptr); \
94  if (data) { \
95  return CustomData_get_##active_type##_layer(data, layer_type); \
96  } \
97  else { \
98  return 0; \
99  } \
100  } \
101 \
102  static void rna_Mesh_##collection_name##_##active_type##_index_set(PointerRNA *ptr, int value) \
103  { \
104  Mesh *me = rna_mesh(ptr); \
105  CustomData *data = rna_mesh_##customdata_type(ptr); \
106  if (data) { \
107  CustomData_set_layer_##active_type(data, layer_type, value); \
108  BKE_mesh_update_customdata_pointers(me, true); \
109  } \
110  }