Blender  V3.3
rna_mesh_api.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2009 Blender Foundation. All rights reserved. */
3 
8 #include <stdio.h>
9 #include <stdlib.h>
10 
11 #include "RNA_define.h"
12 
13 #include "DNA_customdata_types.h"
14 
15 #include "BLI_sys_types.h"
16 
17 #include "BLI_utildefines.h"
18 
19 #include "rna_internal.h" /* own include */
20 
21 #ifdef RNA_RUNTIME
22 
23 # include "DNA_mesh_types.h"
24 
25 # include "BKE_mesh.h"
26 # include "BKE_mesh_mapping.h"
27 # include "BKE_mesh_runtime.h"
28 # include "BKE_mesh_tangent.h"
29 # include "ED_mesh.h"
30 
31 static const char *rna_Mesh_unit_test_compare(struct Mesh *mesh,
32  struct Mesh *mesh2,
33  float threshold)
34 {
35  const char *ret = BKE_mesh_cmp(mesh, mesh2, threshold);
36 
37  if (!ret) {
38  ret = "Same";
39  }
40 
41  return ret;
42 }
43 
44 static void rna_Mesh_create_normals_split(Mesh *mesh)
45 {
49  }
50 }
51 
52 static void rna_Mesh_free_normals_split(Mesh *mesh)
53 {
55 }
56 
57 static void rna_Mesh_calc_tangents(Mesh *mesh, ReportList *reports, const char *uvmap)
58 {
59  float(*r_looptangents)[4];
60 
62  r_looptangents = CustomData_get_layer(&mesh->ldata, CD_MLOOPTANGENT);
63  memset(r_looptangents, 0, sizeof(float[4]) * mesh->totloop);
64  }
65  else {
66  r_looptangents = CustomData_add_layer(
69  }
70 
71  /* Compute loop normals if needed. */
74  }
75 
76  BKE_mesh_calc_loop_tangent_single(mesh, uvmap, r_looptangents, reports);
77 }
78 
79 static void rna_Mesh_free_tangents(Mesh *mesh)
80 {
82 }
83 
84 static void rna_Mesh_calc_looptri(Mesh *mesh)
85 {
87 }
88 
89 static void rna_Mesh_calc_smooth_groups(
90  Mesh *mesh, bool use_bitflags, int *r_poly_group_len, int **r_poly_group, int *r_group_total)
91 {
92  *r_poly_group_len = mesh->totpoly;
93  *r_poly_group = BKE_mesh_calc_smoothgroups(mesh->medge,
94  mesh->totedge,
95  mesh->mpoly,
96  mesh->totpoly,
97  mesh->mloop,
98  mesh->totloop,
99  r_group_total,
100  use_bitflags);
101 }
102 
103 static void rna_Mesh_normals_split_custom_do(Mesh *mesh,
104  float (*custom_loopnors)[3],
105  const bool use_vertices)
106 {
107  if (use_vertices) {
109  }
110  else {
111  BKE_mesh_set_custom_normals(mesh, custom_loopnors);
112  }
113 }
114 
115 static void rna_Mesh_normals_split_custom_set(Mesh *mesh,
116  ReportList *reports,
117  int normals_len,
118  float *normals)
119 {
120  float(*loopnors)[3] = (float(*)[3])normals;
121  const int numloops = mesh->totloop;
122 
123  if (normals_len != numloops * 3) {
124  BKE_reportf(reports,
125  RPT_ERROR,
126  "Number of custom normals is not number of loops (%f / %d)",
127  (float)normals_len / 3.0f,
128  numloops);
129  return;
130  }
131 
132  rna_Mesh_normals_split_custom_do(mesh, loopnors, false);
133 
134  DEG_id_tag_update(&mesh->id, 0);
135 }
136 
137 static void rna_Mesh_normals_split_custom_set_from_vertices(Mesh *mesh,
138  ReportList *reports,
139  int normals_len,
140  float *normals)
141 {
142  float(*vertnors)[3] = (float(*)[3])normals;
143  const int numverts = mesh->totvert;
144 
145  if (normals_len != numverts * 3) {
146  BKE_reportf(reports,
147  RPT_ERROR,
148  "Number of custom normals is not number of vertices (%f / %d)",
149  (float)normals_len / 3.0f,
150  numverts);
151  return;
152  }
153 
154  rna_Mesh_normals_split_custom_do(mesh, vertnors, true);
155 
156  DEG_id_tag_update(&mesh->id, 0);
157 }
158 
159 static void rna_Mesh_transform(Mesh *mesh, float mat[16], bool shape_keys)
160 {
161  BKE_mesh_transform(mesh, (float(*)[4])mat, shape_keys);
162 
163  DEG_id_tag_update(&mesh->id, 0);
164 }
165 
166 static void rna_Mesh_flip_normals(Mesh *mesh)
167 {
172 
173  DEG_id_tag_update(&mesh->id, 0);
174 }
175 
176 static void rna_Mesh_split_faces(Mesh *mesh, bool free_loop_normals)
177 {
178  BKE_mesh_split_faces(mesh, free_loop_normals != 0);
179 }
180 
181 static void rna_Mesh_update_gpu_tag(Mesh *mesh)
182 {
184 }
185 
186 static void rna_Mesh_count_selected_items(Mesh *mesh, int r_count[3])
187 {
189 }
190 
191 static void rna_Mesh_clear_geometry(Mesh *mesh)
192 {
194 
197 }
198 
199 #else
200 
202 {
203  FunctionRNA *func;
204  PropertyRNA *parm;
205  const int normals_array_dim[] = {1, 3};
206 
207  func = RNA_def_function(srna, "transform", "rna_Mesh_transform");
209  "Transform mesh vertices by a matrix "
210  "(Warning: inverts normals if matrix is negative)");
211  parm = RNA_def_float_matrix(func, "matrix", 4, 4, NULL, 0.0f, 0.0f, "", "Matrix", 0.0f, 0.0f);
213  RNA_def_boolean(func, "shape_keys", 0, "", "Transform Shape Keys");
214 
215  func = RNA_def_function(srna, "flip_normals", "rna_Mesh_flip_normals");
217  "Invert winding of all polygons "
218  "(clears tessellation, does not handle custom normals)");
219 
220  func = RNA_def_function(srna, "calc_normals", "BKE_mesh_calc_normals");
221  RNA_def_function_ui_description(func, "Calculate vertex normals");
222 
223  func = RNA_def_function(srna, "create_normals_split", "rna_Mesh_create_normals_split");
224  RNA_def_function_ui_description(func, "Empty split vertex normals");
225 
226  func = RNA_def_function(srna, "calc_normals_split", "BKE_mesh_calc_normals_split");
228  "Calculate split vertex normals, which preserve sharp edges");
229 
230  func = RNA_def_function(srna, "free_normals_split", "rna_Mesh_free_normals_split");
231  RNA_def_function_ui_description(func, "Free split vertex normals");
232 
233  func = RNA_def_function(srna, "split_faces", "rna_Mesh_split_faces");
234  RNA_def_function_ui_description(func, "Split faces based on the edge angle");
236  func, "free_loop_normals", 1, "Free Loop Normals", "Free loop normals custom data layer");
237 
238  func = RNA_def_function(srna, "calc_tangents", "rna_Mesh_calc_tangents");
241  func,
242  "Compute tangents and bitangent signs, to be used together with the split normals "
243  "to get a complete tangent space for normal mapping "
244  "(split normals are also computed if not yet present)");
245  parm = RNA_def_string(func,
246  "uvmap",
247  NULL,
249  "",
250  "Name of the UV map to use for tangent space computation");
251 
252  func = RNA_def_function(srna, "free_tangents", "rna_Mesh_free_tangents");
253  RNA_def_function_ui_description(func, "Free tangents");
254 
255  func = RNA_def_function(srna, "calc_loop_triangles", "rna_Mesh_calc_looptri");
257  "Calculate loop triangle tessellation (supports editmode too)");
258 
259  func = RNA_def_function(srna, "calc_smooth_groups", "rna_Mesh_calc_smooth_groups");
260  RNA_def_function_ui_description(func, "Calculate smooth groups from sharp edges");
262  func, "use_bitflags", false, "", "Produce bitflags groups instead of simple numeric values");
263  /* return values */
264  parm = RNA_def_int_array(func, "poly_groups", 1, NULL, 0, 0, "", "Smooth Groups", 0, 0);
266  parm = RNA_def_int(
267  func, "groups", 0, 0, INT_MAX, "groups", "Total number of groups", 0, INT_MAX);
269 
270  func = RNA_def_function(srna, "normals_split_custom_set", "rna_Mesh_normals_split_custom_set");
272  "Define custom split normals of this mesh "
273  "(use zero-vectors to keep auto ones)");
275  /* TODO: see how array size of 0 works, this shouldn't be used. */
276  parm = RNA_def_float_array(func, "normals", 1, NULL, -1.0f, 1.0f, "", "Normals", 0.0f, 0.0f);
277  RNA_def_property_multi_array(parm, 2, normals_array_dim);
279 
280  func = RNA_def_function(srna,
281  "normals_split_custom_set_from_vertices",
282  "rna_Mesh_normals_split_custom_set_from_vertices");
284  func,
285  "Define custom split normals of this mesh, from vertices' normals "
286  "(use zero-vectors to keep auto ones)");
288  /* TODO: see how array size of 0 works, this shouldn't be used. */
289  parm = RNA_def_float_array(func, "normals", 1, NULL, -1.0f, 1.0f, "", "Normals", 0.0f, 0.0f);
290  RNA_def_property_multi_array(parm, 2, normals_array_dim);
292 
293  func = RNA_def_function(srna, "update", "ED_mesh_update");
294  RNA_def_boolean(func, "calc_edges", 0, "Calculate Edges", "Force recalculation of edges");
295  RNA_def_boolean(func,
296  "calc_edges_loose",
297  0,
298  "Calculate Loose Edges",
299  "Calculate the loose state of each edge");
301 
302  RNA_def_function(srna, "update_gpu_tag", "rna_Mesh_update_gpu_tag");
303 
304  func = RNA_def_function(srna, "unit_test_compare", "rna_Mesh_unit_test_compare");
305  RNA_def_pointer(func, "mesh", "Mesh", "", "Mesh to compare to");
307  "threshold",
308  FLT_EPSILON * 60,
309  0.0f,
310  FLT_MAX,
311  "Threshold",
312  "Comparison tolerance threshold",
313  0.0f,
314  FLT_MAX);
315  /* return value */
316  parm = RNA_def_string(
317  func, "result", "nothing", 64, "Return value", "String description of result of comparison");
318  RNA_def_function_return(func, parm);
319 
320  func = RNA_def_function(srna, "clear_geometry", "rna_Mesh_clear_geometry");
322  func,
323  "Remove all geometry from the mesh. Note that this does not free shape keys or materials");
324 
325  func = RNA_def_function(srna, "validate", "BKE_mesh_validate");
327  "Validate geometry, return True when the mesh has had "
328  "invalid geometry corrected/removed");
329  RNA_def_boolean(func, "verbose", false, "Verbose", "Output information about the errors found");
330  RNA_def_boolean(func,
331  "clean_customdata",
332  true,
333  "Clean Custom Data",
334  "Remove temp/cached custom-data layers, like e.g. normals...");
335  parm = RNA_def_boolean(func, "result", 0, "Result", "");
336  RNA_def_function_return(func, parm);
337 
338  func = RNA_def_function(srna, "validate_material_indices", "BKE_mesh_validate_material_indices");
340  func,
341  "Validate material indices of polygons, return True when the mesh has had "
342  "invalid indices corrected (to default 0)");
343  parm = RNA_def_boolean(func, "result", 0, "Result", "");
344  RNA_def_function_return(func, parm);
345 
346  func = RNA_def_function(srna, "count_selected_items", "rna_Mesh_count_selected_items ");
347  RNA_def_function_ui_description(func, "Return the number of selected items (vert, edge, face)");
348  parm = RNA_def_int_vector(func, "result", 3, NULL, 0, INT_MAX, "Result", NULL, 0, INT_MAX);
349  RNA_def_function_output(func, parm);
350 }
351 
352 #endif
typedef float(TangentPoint)[2]
@ CD_CALLOC
void CustomData_free_layers(struct CustomData *data, int type, int totelem)
Definition: customdata.cc:2904
bool CustomData_has_layer(const struct CustomData *data, int type)
void * CustomData_get_layer(const struct CustomData *data, int type)
void * CustomData_add_layer(struct CustomData *data, int type, eCDAllocType alloctype, void *layer, int totelem)
Definition: customdata.cc:2776
void CustomData_set_layer_flag(struct CustomData *data, int type, int flag)
Definition: customdata.cc:2626
const char * BKE_mesh_cmp(struct Mesh *me1, struct Mesh *me2, float thresh)
Definition: mesh.cc:702
void BKE_mesh_tessface_clear(struct Mesh *mesh)
Definition: mesh.cc:1654
void BKE_mesh_batch_cache_dirty_tag(struct Mesh *me, eMeshBatchDirtyMode mode)
void BKE_mesh_clear_geometry(struct Mesh *me)
Definition: mesh.cc:941
void BKE_mesh_calc_normals_split(struct Mesh *mesh)
Definition: mesh.cc:1911
void BKE_mesh_count_selected_items(const struct Mesh *mesh, int r_count[3])
void BKE_mesh_set_custom_normals_from_vertices(struct Mesh *mesh, float(*r_custom_vertnors)[3])
void BKE_mesh_split_faces(struct Mesh *mesh, bool free_loop_normals)
Definition: mesh.cc:2121
void BKE_mesh_normals_tag_dirty(struct Mesh *mesh)
Definition: mesh_normals.cc:95
void BKE_mesh_polygons_flip(struct MPoly *mpoly, struct MLoop *mloop, struct CustomData *ldata, int totpoly)
void BKE_mesh_set_custom_normals(struct Mesh *mesh, float(*r_custom_loopnors)[3])
void BKE_mesh_transform(struct Mesh *me, const float mat[4][4], bool do_keys)
Definition: mesh.cc:1594
int * BKE_mesh_calc_smoothgroups(const struct MEdge *medge, int totedge, const struct MPoly *mpoly, int totpoly, const struct MLoop *mloop, int totloop, int *r_totgroup, bool use_bitflags)
const struct MLoopTri * BKE_mesh_runtime_looptri_ensure(const struct Mesh *mesh)
void BKE_mesh_runtime_clear_geometry(struct Mesh *mesh)
void BKE_mesh_calc_loop_tangent_single(struct Mesh *mesh, const char *uvmap, float(*r_looptangents)[4], struct ReportList *reports)
Definition: mesh_tangent.c:153
@ BKE_MESH_BATCH_DIRTY_ALL
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
void DEG_id_tag_update(struct ID *id, int flag)
@ ID_RECALC_GEOMETRY_ALL_MODES
Definition: DNA_ID.h:884
#define MAX_CUSTOMDATA_LAYER_NAME
@ CD_FLAG_TEMPORARY
@ CD_MLOOPTANGENT
@ PARM_REQUIRED
Definition: RNA_types.h:352
@ PARM_OUTPUT
Definition: RNA_types.h:353
@ FUNC_USE_REPORTS
Definition: RNA_types.h:663
@ FUNC_USE_CONTEXT
Definition: RNA_types.h:662
@ PROP_DYNAMIC
Definition: RNA_types.h:290
#define NC_GEOM
Definition: WM_types.h:343
#define ND_DATA
Definition: WM_types.h:456
ccl_gpu_kernel_postfix ccl_global float int int int int float threshold
MutableSpan< float3 > normals
return ret
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, bool default_value, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3493
PropertyRNA * RNA_def_pointer(StructOrFunctionRNA *cont_, const char *identifier, const char *type, const char *ui_name, const char *ui_description)
Definition: rna_define.c:4170
PropertyRNA * RNA_def_int_array(StructOrFunctionRNA *cont_, const char *identifier, int len, const int *default_value, int hardmin, int hardmax, const char *ui_name, const char *ui_description, int softmin, int softmax)
Definition: rna_define.c:3655
void RNA_def_function_return(FunctionRNA *func, PropertyRNA *ret)
Definition: rna_define.c:4312
PropertyRNA * RNA_def_float_array(StructOrFunctionRNA *cont_, const char *identifier, int len, const float *default_value, float hardmin, float hardmax, const char *ui_name, const char *ui_description, float softmin, float softmax)
Definition: rna_define.c:4076
PropertyRNA * RNA_def_float_matrix(StructOrFunctionRNA *cont_, const char *identifier, int rows, int columns, const float *default_value, float hardmin, float hardmax, const char *ui_name, const char *ui_description, float softmin, float softmax)
Definition: rna_define.c:3954
FunctionRNA * RNA_def_function(StructRNA *srna, const char *identifier, const char *call)
Definition: rna_define.c:4273
void RNA_def_function_output(FunctionRNA *UNUSED(func), PropertyRNA *ret)
Definition: rna_define.c:4337
void RNA_def_property_multi_array(PropertyRNA *prop, int dimension, const int length[])
Definition: rna_define.c:1598
PropertyRNA * RNA_def_float_factor(StructOrFunctionRNA *cont_, const char *identifier, float default_value, float hardmin, float hardmax, const char *ui_name, const char *ui_description, float softmin, float softmax)
Definition: rna_define.c:4144
void RNA_def_function_ui_description(FunctionRNA *func, const char *description)
Definition: rna_define.c:4347
PropertyRNA * RNA_def_int_vector(StructOrFunctionRNA *cont_, const char *identifier, int len, const int *default_value, int hardmin, int hardmax, const char *ui_name, const char *ui_description, int softmin, int softmax)
Definition: rna_define.c:3623
void RNA_def_function_flag(FunctionRNA *func, int flag)
Definition: rna_define.c:4342
PropertyRNA * RNA_def_string(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, int maxlen, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3687
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, int default_value, int hardmin, int hardmax, const char *ui_name, const char *ui_description, int softmin, int softmax)
Definition: rna_define.c:3597
void RNA_def_parameter_flags(PropertyRNA *prop, PropertyFlag flag_property, ParameterFlag flag_parameter)
Definition: rna_define.c:1518
void RNA_api_mesh(StructRNA *srna)
Definition: rna_mesh_api.c:201
struct MEdge * medge
int totedge
int totvert
struct MLoop * mloop
int totpoly
int totloop
struct MPoly * mpoly
CustomData ldata
void WM_main_add_notifier(unsigned int type, void *reference)