Blender  V2.93
rna_mesh_api.c
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  * The Original Code is Copyright (C) 2009 Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #include <stdio.h>
25 #include <stdlib.h>
26 
27 #include "RNA_define.h"
28 
29 #include "DNA_customdata_types.h"
30 
31 #include "BLI_sys_types.h"
32 
33 #include "BLI_utildefines.h"
34 
35 #include "rna_internal.h" /* own include */
36 
37 #ifdef RNA_RUNTIME
38 
39 # include "DNA_mesh_types.h"
40 
41 # include "BKE_mesh.h"
42 # include "BKE_mesh_mapping.h"
43 # include "BKE_mesh_runtime.h"
44 # include "BKE_mesh_tangent.h"
45 # include "ED_mesh.h"
46 
47 static const char *rna_Mesh_unit_test_compare(struct Mesh *mesh,
48  struct Mesh *mesh2,
49  float threshold)
50 {
51  const char *ret = BKE_mesh_cmp(mesh, mesh2, threshold);
52 
53  if (!ret) {
54  ret = "Same";
55  }
56 
57  return ret;
58 }
59 
60 static void rna_Mesh_create_normals_split(Mesh *mesh)
61 {
65  }
66 }
67 
68 static void rna_Mesh_free_normals_split(Mesh *mesh)
69 {
71 }
72 
73 static void rna_Mesh_calc_tangents(Mesh *mesh, ReportList *reports, const char *uvmap)
74 {
75  float(*r_looptangents)[4];
76 
78  r_looptangents = CustomData_get_layer(&mesh->ldata, CD_MLOOPTANGENT);
79  memset(r_looptangents, 0, sizeof(float[4]) * mesh->totloop);
80  }
81  else {
82  r_looptangents = CustomData_add_layer(
85  }
86 
87  /* Compute loop normals if needed. */
90  }
91 
92  BKE_mesh_calc_loop_tangent_single(mesh, uvmap, r_looptangents, reports);
93 }
94 
95 static void rna_Mesh_free_tangents(Mesh *mesh)
96 {
98 }
99 
100 static void rna_Mesh_calc_looptri(Mesh *mesh)
101 {
103 }
104 
105 static void rna_Mesh_calc_smooth_groups(
106  Mesh *mesh, bool use_bitflags, int *r_poly_group_len, int **r_poly_group, int *r_group_total)
107 {
108  *r_poly_group_len = mesh->totpoly;
109  *r_poly_group = BKE_mesh_calc_smoothgroups(mesh->medge,
110  mesh->totedge,
111  mesh->mpoly,
112  mesh->totpoly,
113  mesh->mloop,
114  mesh->totloop,
115  r_group_total,
116  use_bitflags);
117 }
118 
119 static void rna_Mesh_normals_split_custom_do(Mesh *mesh,
120  float (*custom_loopnors)[3],
121  const bool use_vertices)
122 {
123  if (use_vertices) {
125  }
126  else {
127  BKE_mesh_set_custom_normals(mesh, custom_loopnors);
128  }
129 }
130 
131 static void rna_Mesh_normals_split_custom_set(Mesh *mesh,
132  ReportList *reports,
133  int normals_len,
134  float *normals)
135 {
136  float(*loopnors)[3] = (float(*)[3])normals;
137  const int numloops = mesh->totloop;
138 
139  if (normals_len != numloops * 3) {
140  BKE_reportf(reports,
141  RPT_ERROR,
142  "Number of custom normals is not number of loops (%f / %d)",
143  (float)normals_len / 3.0f,
144  numloops);
145  return;
146  }
147 
148  rna_Mesh_normals_split_custom_do(mesh, loopnors, false);
149 
150  DEG_id_tag_update(&mesh->id, 0);
151 }
152 
153 static void rna_Mesh_normals_split_custom_set_from_vertices(Mesh *mesh,
154  ReportList *reports,
155  int normals_len,
156  float *normals)
157 {
158  float(*vertnors)[3] = (float(*)[3])normals;
159  const int numverts = mesh->totvert;
160 
161  if (normals_len != numverts * 3) {
162  BKE_reportf(reports,
163  RPT_ERROR,
164  "Number of custom normals is not number of vertices (%f / %d)",
165  (float)normals_len / 3.0f,
166  numverts);
167  return;
168  }
169 
170  rna_Mesh_normals_split_custom_do(mesh, vertnors, true);
171 
172  DEG_id_tag_update(&mesh->id, 0);
173 }
174 
175 static void rna_Mesh_transform(Mesh *mesh, float *mat, bool shape_keys)
176 {
177  BKE_mesh_transform(mesh, (float(*)[4])mat, shape_keys);
178 
179  DEG_id_tag_update(&mesh->id, 0);
180 }
181 
182 static void rna_Mesh_flip_normals(Mesh *mesh)
183 {
188 
189  DEG_id_tag_update(&mesh->id, 0);
190 }
191 
192 static void rna_Mesh_split_faces(Mesh *mesh, bool free_loop_normals)
193 {
194  BKE_mesh_split_faces(mesh, free_loop_normals != 0);
195 }
196 
197 static void rna_Mesh_update_gpu_tag(Mesh *mesh)
198 {
200 }
201 
202 static void rna_Mesh_count_selected_items(Mesh *mesh, int r_count[3])
203 {
205 }
206 
207 static void rna_Mesh_clear_geometry(Mesh *mesh)
208 {
210 
213 }
214 
215 #else
216 
218 {
219  FunctionRNA *func;
220  PropertyRNA *parm;
221  const int normals_array_dim[] = {1, 3};
222 
223  func = RNA_def_function(srna, "transform", "rna_Mesh_transform");
225  "Transform mesh vertices by a matrix "
226  "(Warning: inverts normals if matrix is negative)");
227  parm = RNA_def_float_matrix(func, "matrix", 4, 4, NULL, 0.0f, 0.0f, "", "Matrix", 0.0f, 0.0f);
229  RNA_def_boolean(func, "shape_keys", 0, "", "Transform Shape Keys");
230 
231  func = RNA_def_function(srna, "flip_normals", "rna_Mesh_flip_normals");
233  "Invert winding of all polygons "
234  "(clears tessellation, does not handle custom normals)");
235 
236  func = RNA_def_function(srna, "calc_normals", "BKE_mesh_calc_normals");
237  RNA_def_function_ui_description(func, "Calculate vertex normals");
238 
239  func = RNA_def_function(srna, "create_normals_split", "rna_Mesh_create_normals_split");
240  RNA_def_function_ui_description(func, "Empty split vertex normals");
241 
242  func = RNA_def_function(srna, "calc_normals_split", "BKE_mesh_calc_normals_split");
244  "Calculate split vertex normals, which preserve sharp edges");
245 
246  func = RNA_def_function(srna, "free_normals_split", "rna_Mesh_free_normals_split");
247  RNA_def_function_ui_description(func, "Free split vertex normals");
248 
249  func = RNA_def_function(srna, "split_faces", "rna_Mesh_split_faces");
250  RNA_def_function_ui_description(func, "Split faces based on the edge angle");
252  func, "free_loop_normals", 1, "Free Loop Notmals", "Free loop normals custom data layer");
253 
254  func = RNA_def_function(srna, "calc_tangents", "rna_Mesh_calc_tangents");
257  func,
258  "Compute tangents and bitangent signs, to be used together with the split normals "
259  "to get a complete tangent space for normal mapping "
260  "(split normals are also computed if not yet present)");
261  parm = RNA_def_string(func,
262  "uvmap",
263  NULL,
265  "",
266  "Name of the UV map to use for tangent space computation");
267 
268  func = RNA_def_function(srna, "free_tangents", "rna_Mesh_free_tangents");
269  RNA_def_function_ui_description(func, "Free tangents");
270 
271  func = RNA_def_function(srna, "calc_loop_triangles", "rna_Mesh_calc_looptri");
273  "Calculate loop triangle tessellation (supports editmode too)");
274 
275  func = RNA_def_function(srna, "calc_smooth_groups", "rna_Mesh_calc_smooth_groups");
276  RNA_def_function_ui_description(func, "Calculate smooth groups from sharp edges");
278  func, "use_bitflags", false, "", "Produce bitflags groups instead of simple numeric values");
279  /* return values */
280  parm = RNA_def_int_array(func, "poly_groups", 1, NULL, 0, 0, "", "Smooth Groups", 0, 0);
282  parm = RNA_def_int(
283  func, "groups", 0, 0, INT_MAX, "groups", "Total number of groups", 0, INT_MAX);
285 
286  func = RNA_def_function(srna, "normals_split_custom_set", "rna_Mesh_normals_split_custom_set");
288  "Define custom split normals of this mesh "
289  "(use zero-vectors to keep auto ones)");
291  /* TODO, see how array size of 0 works, this shouldn't be used */
292  parm = RNA_def_float_array(func, "normals", 1, NULL, -1.0f, 1.0f, "", "Normals", 0.0f, 0.0f);
293  RNA_def_property_multi_array(parm, 2, normals_array_dim);
295 
296  func = RNA_def_function(srna,
297  "normals_split_custom_set_from_vertices",
298  "rna_Mesh_normals_split_custom_set_from_vertices");
300  func,
301  "Define custom split normals of this mesh, from vertices' normals "
302  "(use zero-vectors to keep auto ones)");
304  /* TODO, see how array size of 0 works, this shouldn't be used */
305  parm = RNA_def_float_array(func, "normals", 1, NULL, -1.0f, 1.0f, "", "Normals", 0.0f, 0.0f);
306  RNA_def_property_multi_array(parm, 2, normals_array_dim);
308 
309  func = RNA_def_function(srna, "update", "ED_mesh_update");
310  RNA_def_boolean(func, "calc_edges", 0, "Calculate Edges", "Force recalculation of edges");
311  RNA_def_boolean(func,
312  "calc_edges_loose",
313  0,
314  "Calculate Loose Edges",
315  "Calculate the loose state of each edge");
317 
318  RNA_def_function(srna, "update_gpu_tag", "rna_Mesh_update_gpu_tag");
319 
320  func = RNA_def_function(srna, "unit_test_compare", "rna_Mesh_unit_test_compare");
321  RNA_def_pointer(func, "mesh", "Mesh", "", "Mesh to compare to");
323  "threshold",
324  FLT_EPSILON * 60,
325  0.0f,
326  FLT_MAX,
327  "Threshold",
328  "Comparison tolerance threshold",
329  0.0f,
330  FLT_MAX);
331  /* return value */
332  parm = RNA_def_string(
333  func, "result", "nothing", 64, "Return value", "String description of result of comparison");
334  RNA_def_function_return(func, parm);
335 
336  func = RNA_def_function(srna, "clear_geometry", "rna_Mesh_clear_geometry");
338  func,
339  "Remove all geometry from the mesh. Note that this does not free shape keys or materials");
340 
341  func = RNA_def_function(srna, "validate", "BKE_mesh_validate");
343  "Validate geometry, return True when the mesh has had "
344  "invalid geometry corrected/removed");
345  RNA_def_boolean(func, "verbose", false, "Verbose", "Output information about the errors found");
346  RNA_def_boolean(func,
347  "clean_customdata",
348  true,
349  "Clean Custom Data",
350  "Remove temp/cached custom-data layers, like e.g. normals...");
351  parm = RNA_def_boolean(func, "result", 0, "Result", "");
352  RNA_def_function_return(func, parm);
353 
354  func = RNA_def_function(srna, "validate_material_indices", "BKE_mesh_validate_material_indices");
356  func,
357  "Validate material indices of polygons, return True when the mesh has had "
358  "invalid indices corrected (to default 0)");
359  parm = RNA_def_boolean(func, "result", 0, "Result", "");
360  RNA_def_function_return(func, parm);
361 
362  func = RNA_def_function(srna, "count_selected_items", "rna_Mesh_count_selected_items ");
363  RNA_def_function_ui_description(func, "Return the number of selected items (vert, edge, face)");
364  parm = RNA_def_int_vector(func, "result", 3, NULL, 0, INT_MAX, "Result", NULL, 0, INT_MAX);
365  RNA_def_function_output(func, parm);
366 }
367 
368 #endif
typedef float(TangentPoint)[2]
@ CD_CALLOC
void CustomData_free_layers(struct CustomData *data, int type, int totelem)
Definition: customdata.c:2716
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.c:2620
void CustomData_set_layer_flag(struct CustomData *data, int type, int flag)
Definition: customdata.c:2475
const char * BKE_mesh_cmp(struct Mesh *me1, struct Mesh *me2, float thresh)
Definition: mesh.c:598
void BKE_mesh_tessface_clear(struct Mesh *mesh)
Definition: mesh.c:1567
void BKE_mesh_calc_normals(struct Mesh *me)
void BKE_mesh_batch_cache_dirty_tag(struct Mesh *me, eMeshBatchDirtyMode mode)
Definition: mesh_runtime.c:251
void BKE_mesh_clear_geometry(struct Mesh *me)
Definition: mesh.c:827
void BKE_mesh_calc_normals_split(struct Mesh *mesh)
Definition: mesh.c:1865
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.c:2073
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.c:1485
int * BKE_mesh_calc_smoothgroups(const struct MEdge *medge, const int totedge, const struct MPoly *mpoly, const int totpoly, const struct MLoop *mloop, const int totloop, int *r_totgroup, const bool use_bitflags)
const struct MLoopTri * BKE_mesh_runtime_looptri_ensure(struct Mesh *mesh)
Definition: mesh_runtime.c:155
void BKE_mesh_runtime_clear_geometry(struct Mesh *mesh)
Definition: mesh_runtime.c:226
void BKE_mesh_calc_loop_tangent_single(struct Mesh *mesh, const char *uvmap, float(*r_looptangents)[4], struct ReportList *reports)
Definition: mesh_tangent.c:181
@ BKE_MESH_BATCH_DIRTY_ALL
void BKE_reportf(ReportList *reports, ReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
void DEG_id_tag_update(struct ID *id, int flag)
@ ID_RECALC_GEOMETRY
Definition: DNA_ID.h:611
#define MAX_CUSTOMDATA_LAYER_NAME
@ CD_MLOOPTANGENT
@ CD_FLAG_TEMPORARY
@ PARM_REQUIRED
Definition: RNA_types.h:337
@ PARM_OUTPUT
Definition: RNA_types.h:338
@ FUNC_USE_REPORTS
Definition: RNA_types.h:578
@ FUNC_USE_CONTEXT
Definition: RNA_types.h:577
@ PROP_DYNAMIC
Definition: RNA_types.h:275
#define NC_GEOM
Definition: WM_types.h:294
#define ND_DATA
Definition: WM_types.h:408
static float normals[][3]
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:3481
PropertyRNA * RNA_def_pointer(StructOrFunctionRNA *cont_, const char *identifier, const char *type, const char *ui_name, const char *ui_description)
Definition: rna_define.c:4159
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:3643
void RNA_def_function_return(FunctionRNA *func, PropertyRNA *ret)
Definition: rna_define.c:4302
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:4065
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:3943
FunctionRNA * RNA_def_function(StructRNA *srna, const char *identifier, const char *call)
Definition: rna_define.c:4262
void RNA_def_function_output(FunctionRNA *UNUSED(func), PropertyRNA *ret)
Definition: rna_define.c:4327
void RNA_def_property_multi_array(PropertyRNA *prop, int dimension, const int length[])
Definition: rna_define.c:1629
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:4133
void RNA_def_function_ui_description(FunctionRNA *func, const char *description)
Definition: rna_define.c:4337
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:3611
void RNA_def_function_flag(FunctionRNA *func, int flag)
Definition: rna_define.c:4332
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:3675
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:3585
void RNA_def_parameter_flags(PropertyRNA *prop, PropertyFlag flag_property, ParameterFlag flag_parameter)
Definition: rna_define.c:1547
void RNA_api_mesh(StructRNA *srna)
Definition: rna_mesh_api.c:217
struct MEdge * medge
struct CustomData pdata ldata
int totedge
int totvert
struct MLoop * mloop
int totpoly
int totloop
struct MPoly * mpoly
void WM_main_add_notifier(unsigned int type, void *reference)