Blender V5.3
rna_mesh_api.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2009 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstdlib>
10
11#include "RNA_define.hh"
12
14
15#include "BLI_math_base_c.hh"
16
17#include "rna_internal.hh" /* own include */
18
19#ifdef RNA_RUNTIME
20
21# include "DNA_mesh_types.h"
22
23# include "BKE_anim_data.hh"
24# include "BKE_attribute.hh"
25# include "BKE_customdata.hh"
27# include "BKE_mesh.h"
28# include "BKE_mesh.hh"
29# include "BKE_mesh_mapping.hh"
30# include "BKE_mesh_runtime.hh"
31# include "BKE_mesh_tangent.hh"
32# include "BKE_report.hh"
33
34# include "ED_mesh.hh"
35
36# include "DEG_depsgraph.hh"
37
38# include "WM_api.hh"
39
40namespace blender {
41
42static const char *rna_Mesh_unit_test_compare(Mesh *mesh, Mesh *mesh2, float threshold)
43{
44 using namespace bke::compare_geometry;
45 const std::optional<GeoMismatch> mismatch = compare_meshes(*mesh, *mesh2, threshold);
46
47 if (!mismatch) {
48 return "Same";
49 }
50
51 return mismatch_to_string(mismatch.value());
52}
53
54static void rna_Mesh_sharp_from_angle_set(Mesh *mesh, const float angle)
55{
56 mesh->attributes_for_write().remove("sharp_edge");
57 mesh->attributes_for_write().remove("sharp_face");
60}
61
62static void rna_Mesh_calc_tangents(Mesh *mesh, ReportList *reports, const char *uvmap)
63{
64 float4 *r_looptangents;
65 if (CustomData_has_layer(&mesh->corner_data, CD_MLOOPTANGENT)) {
66 r_looptangents = static_cast<float4 *>(
67 CustomData_get_layer_for_write(&mesh->corner_data, CD_MLOOPTANGENT, mesh->corners_num));
68 memset(reinterpret_cast<void *>(r_looptangents), 0, sizeof(float4) * mesh->corners_num);
69 }
70 else {
71 r_looptangents = static_cast<float4 *>(CustomData_add_layer(
72 &mesh->corner_data, CD_MLOOPTANGENT, CD_SET_DEFAULT, mesh->corners_num));
74 }
75
76 if (!uvmap) {
77 uvmap = mesh->active_uv_map_name().c_str();
78 }
79
80 const bke::AttributeAccessor attributes = mesh->attributes();
81 const VArraySpan uv_map = *attributes.lookup<float2>(uvmap, bke::AttrDomain::Corner);
82 if (uv_map.is_empty()) {
83 BKE_reportf(reports,
85 "Tangent space computation needs a UV Map, \"%s\" not found, aborting",
86 uvmap);
87 return;
88 }
89
91 mesh->faces(),
92 mesh->corner_verts(),
93 mesh->corner_normals(),
94 uv_map,
95 {r_looptangents, mesh->corners_num},
96 reports);
97}
98
99static void rna_Mesh_free_tangents(Mesh *mesh)
100{
102}
103
104static void rna_Mesh_calc_corner_tri(Mesh *mesh)
105{
106 mesh->corner_tris();
107}
108
109static void rna_Mesh_calc_smooth_groups(Mesh *mesh,
110 bool use_bitflags,
111 bool use_boundary_vertices_for_bitflags,
112 int **r_poly_group,
113 int *r_poly_group_num,
114 int *r_group_total)
115{
116 *r_poly_group_num = mesh->faces_num;
117 const bke::AttributeAccessor attributes = mesh->attributes();
118 const VArraySpan sharp_edges = *attributes.lookup<bool>("sharp_edge", bke::AttrDomain::Edge);
119 const VArraySpan sharp_faces = *attributes.lookup<bool>("sharp_face", bke::AttrDomain::Face);
120 if (use_bitflags) {
121 *r_poly_group = BKE_mesh_calc_smoothgroups_bitflags(mesh->edges_num,
122 mesh->verts_num,
123 mesh->faces(),
124 mesh->corner_edges(),
125 mesh->corner_verts(),
126 sharp_edges,
127 sharp_faces,
128 use_boundary_vertices_for_bitflags,
129 r_group_total);
130 }
131 else {
132 *r_poly_group = BKE_mesh_calc_smoothgroups(mesh->edges_num,
133 mesh->faces(),
134 mesh->corner_edges(),
135 sharp_edges,
136 sharp_faces,
137 r_group_total);
138 }
139}
140
141static void rna_Mesh_normals_split_custom_set(Mesh *mesh,
142 ReportList *reports,
143 const float *normals,
144 int normals_num)
145{
146 float3 *corner_normals = reinterpret_cast<float3 *>(const_cast<float *>(normals));
147 const int numloops = mesh->corners_num;
148 if (normals_num != numloops * 3) {
149 BKE_reportf(reports,
150 RPT_ERROR,
151 "Number of custom normals is not number of loops (%f / %d)",
152 float(normals_num) / 3.0f,
153 numloops);
154 return;
155 }
156
157 bke::mesh_set_custom_normals(*mesh, {corner_normals, numloops});
158
159 DEG_id_tag_update(&mesh->id, 0);
160}
161
162static void rna_Mesh_normals_split_custom_set_from_vertices(Mesh *mesh,
163 ReportList *reports,
164 const float *normals,
165 int normals_num)
166{
167 float3 *vert_normals = reinterpret_cast<float3 *>(const_cast<float *>(normals));
168 const int numverts = mesh->verts_num;
169 if (normals_num != numverts * 3) {
170 BKE_reportf(reports,
171 RPT_ERROR,
172 "Number of custom normals is not number of vertices (%f / %d)",
173 float(normals_num) / 3.0f,
174 numverts);
175 return;
176 }
177
178 bke::mesh_set_custom_normals_from_verts(*mesh, {vert_normals, numverts});
179
180 DEG_id_tag_update(&mesh->id, 0);
181}
182
183static void rna_Mesh_transform(Mesh *mesh, const float mat[16], bool shape_keys)
184{
185 bke::mesh_transform(*mesh, float4x4(mat), shape_keys);
186
187 DEG_id_tag_update(&mesh->id, 0);
188}
189
190static void rna_Mesh_flip_normals(Mesh *mesh)
191{
195 DEG_id_tag_update(&mesh->id, 0);
196}
197
198static void rna_Mesh_update(Mesh *mesh,
199 bContext *C,
200 const bool calc_edges,
201 const bool calc_edges_loose)
202{
203 if (calc_edges || ((mesh->faces_num || mesh->totface_legacy) && mesh->edges_num == 0)) {
205 }
206
207 if (calc_edges_loose) {
208 mesh->runtime->loose_edges_cache.tag_dirty();
209 }
210
211 /* Default state is not to have tessface's so make sure this is the case. */
213
214 mesh->runtime->vert_normals_cache.tag_dirty();
215 mesh->runtime->face_normals_cache.tag_dirty();
216 mesh->runtime->corner_normals_cache.tag_dirty();
217 mesh->runtime->vert_normals_true_cache.tag_dirty();
218 mesh->runtime->face_normals_true_cache.tag_dirty();
219
220 DEG_id_tag_update(&mesh->id, 0);
222}
223
224static void rna_Mesh_update_gpu_tag(Mesh *mesh)
225{
227}
228
229static void rna_Mesh_count_selected_items(Mesh *mesh, int r_count[3])
230{
232}
233
234static void rna_Mesh_clear_geometry(Mesh *mesh)
235{
237 BKE_animdata_free(&mesh->id, false);
238
240
243}
244
245static bool rna_Mesh_validate(Mesh *mesh, const bool verbose, const bool /*clean_customdata*/)
246{
247 return !bke::mesh_validate(*mesh, verbose);
248}
249
250static bool rna_Mesh_validate_material_indices(Mesh *mesh)
251{
253}
254
255} // namespace blender
256
257#else
258
259namespace blender {
260
262{
263 FunctionRNA *func;
264 PropertyRNA *parm;
265 const int normals_array_dim[] = {1, 3};
266
267 func = RNA_def_function(srna, "transform", "rna_Mesh_transform");
269 "Transform mesh vertices by a matrix "
270 "(Warning: inverts normals if matrix is negative)");
271 parm = RNA_def_float_matrix(func, "matrix", 4, 4, nullptr, 0.0f, 0.0f, "", "Matrix", 0.0f, 0.0f);
273 RNA_def_boolean(func, "shape_keys", false, "", "Transform Shape Keys");
274
275 func = RNA_def_function(srna, "flip_normals", "rna_Mesh_flip_normals");
277 "Invert winding of all polygons "
278 "(clears tessellation, does not handle custom normals)");
279
280 func = RNA_def_function(srna, "set_sharp_from_angle", "rna_Mesh_sharp_from_angle_set");
282 "Reset and fill the \"sharp_edge\" attribute based on the angle "
283 "of faces neighboring manifold edges");
284 RNA_def_float(func,
285 "angle",
286 M_PI,
287 0.0f,
288 M_PI,
289 "Angle",
290 "Angle between faces beyond which edges are marked sharp",
291 0.0f,
292 M_PI);
293
294 func = RNA_def_function(srna, "split_faces", "ED_mesh_split_faces");
295 RNA_def_function_ui_description(func, "Split faces based on the edge angle");
296
297 func = RNA_def_function(srna, "calc_tangents", "rna_Mesh_calc_tangents");
300 func,
301 "Compute tangents and bitangent signs, to be used together with the custom normals "
302 "to get a complete tangent space for normal mapping "
303 "(custom normals are also computed if not yet present)");
304 RNA_def_string(func,
305 "uvmap",
306 nullptr,
308 "",
309 "Name of the UV map to use for tangent space computation");
310
311 func = RNA_def_function(srna, "free_tangents", "rna_Mesh_free_tangents");
312 RNA_def_function_ui_description(func, "Free tangents");
313
314 func = RNA_def_function(srna, "calc_loop_triangles", "rna_Mesh_calc_corner_tri");
316 "Calculate loop triangle tessellation (supports editmode too)");
317
318 func = RNA_def_function(srna, "calc_smooth_groups", "rna_Mesh_calc_smooth_groups");
319 RNA_def_function_ui_description(func, "Calculate smooth groups from sharp edges");
321 func, "use_bitflags", false, "", "Produce bitflags groups instead of simple numeric values");
323 func,
324 "use_boundary_vertices_for_bitflags",
325 false,
326 "",
327 "Also consider different smoothgroups sharing only vertices (but without any common edge) "
328 "as neighbors, preventing them from sharing the same bitflag value. Only effective when "
329 "``use_bitflags`` is set. "
330 "WARNING: Will overflow (run out of available bits) easily with some types of topology, "
331 "e.g. large fans of sharp edges");
332 /* return values */
333 parm = RNA_def_int_array(func, "poly_groups", 1, nullptr, 0, 0, "", "Smooth Groups", 0, 0);
335 parm = RNA_def_int(
336 func, "groups", 0, 0, INT_MAX, "groups", "Total number of groups", 0, INT_MAX);
338
339 func = RNA_def_function(srna, "normals_split_custom_set", "rna_Mesh_normals_split_custom_set");
341 "Define custom normals of this mesh "
342 "(use zero-vectors to keep auto ones)");
344 /* TODO: see how array size of 0 works, this shouldn't be used. */
345 parm = RNA_def_float_array(func, "normals", 1, nullptr, -1.0f, 1.0f, "", "Normals", 0.0f, 0.0f);
346 RNA_def_property_multi_array(parm, 2, normals_array_dim);
348
349 func = RNA_def_function(srna,
350 "normals_split_custom_set_from_vertices",
351 "rna_Mesh_normals_split_custom_set_from_vertices");
353 "Define custom normals of this mesh, from vertices' normals "
354 "(use zero-vectors to keep auto ones)");
356 /* TODO: see how array size of 0 works, this shouldn't be used. */
357 parm = RNA_def_float_array(func, "normals", 1, nullptr, -1.0f, 1.0f, "", "Normals", 0.0f, 0.0f);
358 RNA_def_property_multi_array(parm, 2, normals_array_dim);
360
361 func = RNA_def_function(srna, "update", "rna_Mesh_update");
362 RNA_def_boolean(func, "calc_edges", false, "Calculate Edges", "Force recalculation of edges");
363 RNA_def_boolean(func,
364 "calc_edges_loose",
365 false,
366 "Calculate Loose Edges",
367 "Calculate the loose state of each edge");
369
370 RNA_def_function(srna, "update_gpu_tag", "rna_Mesh_update_gpu_tag");
371
372 func = RNA_def_function(srna, "unit_test_compare", "rna_Mesh_unit_test_compare");
373 RNA_def_pointer(func, "mesh", "Mesh", "", "Mesh to compare to");
375 "threshold",
376 FLT_EPSILON * 60,
377 0.0f,
378 FLT_MAX,
379 "Threshold",
380 "Comparison tolerance threshold",
381 0.0f,
382 FLT_MAX);
383 /* return value */
384 parm = RNA_def_string(
385 func, "result", "nothing", 64, "Return value", "String description of result of comparison");
386 RNA_def_function_return(func, parm);
387
388 func = RNA_def_function(srna, "clear_geometry", "rna_Mesh_clear_geometry");
390 func,
391 "Remove all geometry from the mesh. Note that this does not free shape keys or materials.");
392
393 func = RNA_def_function(srna, "validate", "rna_Mesh_validate");
395 "Validate geometry, return True when the mesh has had "
396 "invalid geometry corrected/removed");
397 RNA_def_boolean(func, "verbose", false, "Verbose", "Output information about the errors found");
399 func, "clean_customdata", true, "Clean Custom Data", "Deprecated, has no effect");
400 parm = RNA_def_boolean(func, "result", false, "Result", "");
401 RNA_def_function_return(func, parm);
402
403 func = RNA_def_function(srna, "validate_material_indices", "rna_Mesh_validate_material_indices");
405 func,
406 "Validate material indices of polygons, return True when the mesh has had "
407 "invalid indices corrected (to default 0)");
408 parm = RNA_def_boolean(func, "result", false, "Result", "");
409 RNA_def_function_return(func, parm);
410
411 func = RNA_def_function(srna, "count_selected_items", "rna_Mesh_count_selected_items ");
412 RNA_def_function_ui_description(func, "Return the number of selected items (vert, edge, face)");
413 parm = RNA_def_int_vector(func, "result", 3, nullptr, 0, INT_MAX, "Result", nullptr, 0, INT_MAX);
414 RNA_def_function_output(func, parm);
415}
416
417} // namespace blender
418
419#endif
CustomData interface, see also DNA_customdata_types.h.
#define M_PI
#define C
Definition RandGen.cpp:29
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DATA
Definition WM_types.hh:552
#define MAX_CUSTOMDATA_LAYER_NAME_NO_PREFIX
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
#define FLT_MAX
#define INT_MAX
#define FLT_EPSILON
std::optional< GeoMismatch > compare_meshes(const Mesh &mesh1, const Mesh &mesh2, float threshold)
Checks if the two meshes are different, returning the type of mismatch if any. Changes in index order...
const char * mismatch_to_string(const GeoMismatch &mismatch)
void calc_uv_tangent_tris_quads(Span< float3 > vert_positions, OffsetIndices< int > faces, Span< int > corner_verts, Span< float3 > corner_normals, Span< float2 > uv_map, MutableSpan< float4 > results, ReportList *reports)
bool mesh_validate_material_indices(Mesh &mesh)
void mesh_sharp_edges_set_from_angle(Mesh &mesh, float angle, bool keep_sharp_edges=false)
void mesh_flip_faces(Mesh &mesh, const IndexMask &selection)
void mesh_calc_edges(Mesh &mesh, bool keep_existing_edges, bool select_new_edges)
void mesh_transform(Mesh &mesh, const float4x4 &transform, bool do_shape_keys)
void mesh_set_custom_normals_from_verts(Mesh &mesh, MutableSpan< float3 > vert_normals)
bool mesh_validate(Mesh &mesh, bool verbose=false, bool allow_missing_edges=false)
void mesh_ensure_required_data_layers(Mesh &mesh)
void mesh_set_custom_normals(Mesh &mesh, MutableSpan< float3 > corner_normals)
BMesh Iterators.
void WM_main_add_notifier(uint type, void *reference)
PropertyRNA * RNA_def_string(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, const int maxlen, const char *ui_name, const char *ui_description)
void RNA_def_function_output(FunctionRNA *, PropertyRNA *ret)
void BKE_mesh_clear_geometry_and_metadata(Mesh *mesh)
bool CustomData_free_layers(CustomData *data, eCustomDataType type)
PropertyRNA * RNA_def_float_array(StructOrFunctionRNA *cont_, const char *identifier, const int len, const float *default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
bool CustomData_has_layer(const CustomData *data, eCustomDataType type)
void RNA_def_property_multi_array(PropertyRNA *prop, int dimension, const int length[])
void RNA_def_function_ui_description(FunctionRNA *func, const char *description)
@ FUNC_USE_REPORTS
@ FUNC_USE_CONTEXT
PropertyRNA * RNA_def_int_vector(StructOrFunctionRNA *cont_, const char *identifier, const int len, const int *default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
PropertyRNA * RNA_def_pointer(StructOrFunctionRNA *cont_, const char *identifier, const char *type, const char *ui_name, const char *ui_description)
void BKE_mesh_tessface_clear(Mesh *mesh)
MatBase< float, 4, 4 > float4x4
void BKE_mesh_count_selected_items(const Mesh *mesh, int r_count[3])
int * BKE_mesh_calc_smoothgroups(int edges_num, OffsetIndices< int > faces, Span< int > corner_edges, Span< bool > sharp_edges, Span< bool > sharp_faces, int *r_totgroup)
VecBase< float, 4 > float4
PropertyRNA * RNA_def_float_factor(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
VecBase< float, 2 > float2
void RNA_def_parameter_flags(PropertyRNA *prop, PropertyFlag flag_property, ParameterFlag flag_parameter)
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_mesh_batch_cache_dirty_tag(Mesh *mesh, eMeshBatchDirtyMode mode)
void BKE_animdata_free(ID *id, bool do_id_user)
Definition anim_data.cc:174
void CustomData_set_layer_flag(CustomData *data, eCustomDataType type, eCustomDataLayer_Flag flag)
void RNA_def_function_return(FunctionRNA *func, PropertyRNA *ret)
FunctionRNA * RNA_def_function(StructRNA *srna, const char *identifier_c_str, const char *call)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void RNA_def_function_flag(FunctionRNA *func, FunctionFlag flag)
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, const int default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
void RNA_api_mesh(StructRNA *srna)
@ ID_RECALC_GEOMETRY_ALL_MODES
Definition DNA_ID.h:1183
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
@ PROP_DYNAMIC
Definition RNA_types.hh:498
PropertyRNA * RNA_def_int_array(StructOrFunctionRNA *cont_, const char *identifier, const int len, const int *default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
void * CustomData_get_layer_for_write(CustomData *data, eCustomDataType type, int totelem)
VecBase< float, 3 > float3
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
PropertyRNA * RNA_def_float_matrix(StructOrFunctionRNA *cont_, const char *identifier, const int rows, const int columns, const float *default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
@ PARM_REQUIRED
Definition RNA_types.hh:631
@ PARM_OUTPUT
Definition RNA_types.hh:632
@ BKE_MESH_BATCH_DIRTY_ALL
Definition BKE_mesh.h:48
PropertyRNA * RNA_def_float(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
void BKE_mesh_runtime_clear_geometry(Mesh *mesh)
void * CustomData_add_layer(CustomData *data, eCustomDataType type, eCDAllocType alloctype, int totelem)
int * BKE_mesh_calc_smoothgroups_bitflags(int edges_num, int verts_num, OffsetIndices< int > faces, Span< int > corner_edges, Span< int > corner_verts, Span< bool > sharp_edges, Span< bool > sharp_faces, bool use_boundary_vertices_for_bitflags, int *r_totgroup)