Blender V5.3
blenkernel/intern/mesh.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <optional>
10
11#include "MEM_guardedalloc.h"
12
13/* Allow using deprecated functionality for .blend file I/O. */
14#define DNA_DEPRECATED_ALLOW
15
16#include "DNA_key_types.h"
17#include "DNA_material_types.h"
18#include "DNA_mesh_types.h"
19#include "DNA_meshdata_types.h"
20#include "DNA_object_types.h"
21
22#include "BLI_array_utils.hh"
23#include "BLI_bounds.hh"
24#include "BLI_hash_c.hh"
26#include "BLI_index_range.hh"
27#include "BLI_listbase.hh"
28#include "BLI_math_matrix.hh"
29#include "BLI_math_vector.hh"
30#include "BLI_math_vector_c.hh"
31#include "BLI_memory_counter.hh"
32#include "BLI_resource_scope.hh"
33#include "BLI_set.hh"
34#include "BLI_span.hh"
35#include "BLI_string.hh"
36#include "BLI_task.hh"
37#include "BLI_time.hh"
38#include "BLI_utildefines.hh"
39#include "BLI_vector.hh"
40#include "BLI_virtual_array.hh"
41
42#include "BLT_translation.hh"
43
44#include "BKE_anim_data.hh"
46#include "BKE_attribute.hh"
51#include "BKE_bpath.hh"
52#include "BKE_deform.hh"
53#include "BKE_editmesh.hh"
54#include "BKE_editmesh_cache.hh"
55#include "BKE_global.hh"
56#include "BKE_idtype.hh"
57#include "BKE_key.hh"
58#include "BKE_lib_id.hh"
59#include "BKE_lib_query.hh"
60#include "BKE_main.hh"
61#include "BKE_material.hh"
62#include "BKE_mesh.hh"
64#include "BKE_mesh_runtime.hh"
65#include "BKE_mesh_wrapper.hh"
66#include "BKE_modifier.hh"
67#include "BKE_multires.hh"
68#include "BKE_object.hh"
69#include "BKE_paint_bvh.hh"
70
71#include "DEG_depsgraph.hh"
73
74#include "BLO_read_write.hh"
75
76#include "CLG_log.h"
77
79
80namespace blender {
81
82static CLG_LogRef LOG = {"geom.mesh"};
83
85#define STACK_FIXED_DEPTH 100
86
87static void mesh_tessface_clear_intern(Mesh *mesh, int free_customdata);
88
89static void mesh_init_data(ID *id)
90{
91 Mesh *mesh = reinterpret_cast<Mesh *>(id);
93
94 CustomData_reset(&mesh->vert_data);
95 CustomData_reset(&mesh->edge_data);
96 CustomData_reset(&mesh->fdata_legacy);
97 CustomData_reset(&mesh->face_data);
98 CustomData_reset(&mesh->corner_data);
99
100 new (&mesh->attribute_storage.wrap()) bke::AttributeStorage();
101 mesh->runtime = new bke::MeshRuntime();
102
103 mesh->face_sets_color_seed = BLI_hash_int(BLI_time_now_seconds_i() & UINT_MAX);
104}
105
106static void mesh_copy_data(Main *bmain,
107 std::optional<Library *> owner_library,
108 ID *id_dst,
109 const ID *id_src,
110 const int flag)
111{
112 Mesh *mesh_dst = reinterpret_cast<Mesh *>(id_dst);
113 const Mesh *mesh_src = reinterpret_cast<const Mesh *>(id_src);
114
115 mesh_dst->runtime = new bke::MeshRuntime();
116 mesh_dst->runtime->deformed_only = mesh_src->runtime->deformed_only;
117 /* Subd runtime.mesh_eval is not copied, will need to be reevaluated. */
118 mesh_dst->runtime->wrapper_type = (mesh_src->runtime->wrapper_type == ME_WRAPPER_TYPE_SUBD) ?
120 mesh_src->runtime->wrapper_type;
122 mesh_dst->runtime->cd_mask_extra = mesh_src->runtime->cd_mask_extra;
123 /* Copy face dot tags and edge tags, since meshes may be duplicated after a subsurf modifier or
124 * node, but we still need to be able to draw face center vertices and "optimal edges"
125 * differently. The tags may be cleared explicitly when the topology is changed. */
129 if ((mesh_src->id.tag & ID_TAG_NO_MAIN) == 0) {
130 /* This is a direct copy of a main mesh, so for now it has the same topology. */
131 mesh_dst->runtime->deformed_only = true;
132 }
133 /* This option is set for run-time meshes that have been copied from the current object's mode.
134 * Currently this is used for edit-mesh although it could be used for sculpt or other
135 * kinds of data specific to an object's mode.
136 *
137 * The flag signals that the mesh hasn't been modified from the data that generated it,
138 * allowing us to use the object-mode data for drawing.
139 *
140 * While this could be the caller's responsibility, keep here since it's
141 * highly unlikely we want to create a duplicate and not use it for drawing. */
142 mesh_dst->runtime->is_original_bmesh = false;
143
144 /* Share various derived caches between the source and destination mesh for improved performance
145 * when the source is persistent and edits to the destination mesh don't affect the caches.
146 * Caches will be "un-shared" as necessary later on. */
147 mesh_dst->runtime->bounds_cache = mesh_src->runtime->bounds_cache;
153 mesh_dst->runtime->loose_verts_cache = mesh_src->runtime->loose_verts_cache;
155 mesh_dst->runtime->loose_edges_cache = mesh_src->runtime->loose_edges_cache;
156 mesh_dst->runtime->corner_tris_cache = mesh_src->runtime->corner_tris_cache;
164 mesh_dst->runtime->bvh_cache_edges = mesh_src->runtime->bvh_cache_edges;
165 mesh_dst->runtime->bvh_cache_faces = mesh_src->runtime->bvh_cache_faces;
179 if (mesh_src->runtime->bake_materials) {
180 mesh_dst->runtime->bake_materials = std::make_unique<bke::bake::BakeMaterialsList>(
181 *mesh_src->runtime->bake_materials);
182 }
183
184 /* Only do tessface if we have no faces. */
185 const bool do_tessface = ((mesh_src->totface_legacy != 0) && (mesh_src->faces_num == 0));
186
188
189 if (mesh_src->id.tag & ID_TAG_NO_MAIN) {
190 /* For copies in depsgraph, keep data like #CD_ORIGINDEX and #CD_ORCO. */
192
193 /* Meshes copied during evaluation pass the edit mesh pointer to determine whether a mapping
194 * from the evaluated to the original state is possible. */
195 mesh_dst->runtime->edit_mesh = mesh_src->runtime->edit_mesh;
196 if (const bke::EditMeshData *edit_data = mesh_src->runtime->edit_data.get()) {
197 mesh_dst->runtime->edit_data = std::make_unique<bke::EditMeshData>(*edit_data);
198 }
199 }
200
201 mesh_dst->mat = MEM_dupalloc(mesh_src->mat);
202
204 mesh_dst->active_color_attribute = static_cast<char *>(
205 MEM_dupalloc(mesh_src->active_color_attribute));
206 mesh_dst->default_color_attribute = static_cast<char *>(
207 MEM_dupalloc(mesh_src->default_color_attribute));
208 mesh_dst->active_uv_map_attribute = static_cast<char *>(
209 MEM_dupalloc(mesh_src->active_uv_map_attribute));
210 mesh_dst->default_uv_map_attribute = static_cast<char *>(
211 MEM_dupalloc(mesh_src->default_uv_map_attribute));
212 mesh_dst->stencil_uv_map_attribute = static_cast<char *>(
213 MEM_dupalloc(mesh_src->stencil_uv_map_attribute));
214 mesh_dst->clone_uv_map_attribute = static_cast<char *>(
215 MEM_dupalloc(mesh_src->clone_uv_map_attribute));
216
218 &mesh_src->vert_data, &mesh_dst->vert_data, mask.vmask, mesh_dst->verts_num);
220 &mesh_src->edge_data, &mesh_dst->edge_data, mask.emask, mesh_dst->edges_num);
222 &mesh_src->corner_data, &mesh_dst->corner_data, mask.lmask, mesh_dst->corners_num);
224 &mesh_src->face_data, &mesh_dst->face_data, mask.pmask, mesh_dst->faces_num);
225 new (&mesh_dst->attribute_storage.wrap())
229 &mesh_dst->face_offset_indices,
231 if (do_tessface) {
233 &mesh_src->fdata_legacy, &mesh_dst->fdata_legacy, mask.fmask, mesh_dst->totface_legacy);
234 }
235 else {
236 mesh_tessface_clear_intern(mesh_dst, false);
237 }
238
239 mesh_dst->mselect = MEM_dupalloc(mesh_dst->mselect);
240
241 if (mesh_src->key && (flag & LIB_ID_COPY_SHAPEKEY)) {
242 BKE_id_copy_in_lib(bmain,
243 owner_library,
244 &mesh_src->key->id,
245 &mesh_dst->id,
246 reinterpret_cast<ID **>(&mesh_dst->key),
247 flag);
248 /* It has one user, but its owner reference (added in #id_copy_libmanagement_cb)
249 * is the real owner, remove the reference here, see: #159691. */
250 if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
251 id_us_min(&mesh_dst->key->id);
252 }
253 }
254}
255
256static void mesh_free_data(ID *id)
257{
258 Mesh *mesh = reinterpret_cast<Mesh *>(id);
259
260 CustomData_free(&mesh->vert_data);
261 CustomData_free(&mesh->edge_data);
262 CustomData_free(&mesh->fdata_legacy);
263 CustomData_free(&mesh->corner_data);
264 CustomData_free(&mesh->face_data);
265 mesh->vertex_group_names.free_no_destruct();
266 MEM_SAFE_DELETE(mesh->active_color_attribute);
267 MEM_SAFE_DELETE(mesh->default_color_attribute);
268 MEM_SAFE_DELETE(mesh->active_uv_map_attribute);
269 MEM_SAFE_DELETE(mesh->default_uv_map_attribute);
270 MEM_SAFE_DELETE(mesh->stencil_uv_map_attribute);
271 MEM_SAFE_DELETE(mesh->clone_uv_map_attribute);
272 mesh->attribute_storage.wrap().~AttributeStorage();
273 if (mesh->face_offset_indices) {
274 implicit_sharing::free_shared_data(&mesh->face_offset_indices,
275 &mesh->runtime->face_offsets_sharing_info);
276 }
277 MEM_SAFE_DELETE(mesh->mselect);
278 MEM_SAFE_DELETE(mesh->mat);
279 delete mesh->runtime;
280}
281
283{
284 Mesh *mesh = reinterpret_cast<Mesh *>(id);
285
288 for (int i = 0; i < mesh->totcol; i++) {
290 }
291}
292
293static void mesh_foreach_path(ID *id, BPathForeachPathData *bpath_data)
294{
295 Mesh *mesh = reinterpret_cast<Mesh *>(id);
296 CustomData &data = mesh->corner_data;
297 if (data.external) {
298 /* CustomDataExternal should only be the case for CD_MDISPS, but check all layers regardless.
299 */
300 const Span<CustomDataLayer> layers(data.layers, data.totlayer);
301 if (std::any_of(layers.begin(),
302 layers.end(),
303 [&](const CustomDataLayer &layer) {
304 return CustomData_external_test(&data, eCustomDataType(layer.type));
305 }))
306 {
308 bpath_data, data.external->filepath, sizeof(data.external->filepath));
309 }
310 }
311}
312
314{
315 Mesh *mesh = reinterpret_cast<Mesh *>(id);
316#if 0
317 /* In the future we'll be able to use just this. */
318 mesh->attribute_storage.wrap().foreach_working_space_color(fn);
319#else
320 auto convert_domain = [&fn](CustomData *customdata, size_t size) {
321 for (int i = 0; i < customdata->totlayer; i++) {
322 CustomDataLayer *layer = &customdata->layers[i];
323 if (layer->data && layer->type == CD_PROP_COLOR) {
324 fn.implicit_sharing_array(*reinterpret_cast<ImplicitSharingPtr<> *>(&layer->sharing_info),
325 reinterpret_cast<ColorGeometry4f *&>(layer->data),
326 size);
327 }
328 }
329 };
330
331 convert_domain(&mesh->vert_data, mesh->verts_num);
332 convert_domain(&mesh->edge_data, mesh->edges_num);
333 convert_domain(&mesh->face_data, mesh->faces_num);
334 convert_domain(&mesh->corner_data, mesh->corners_num);
335#endif
336}
337
338static void mesh_blend_write(BlendWriter *writer, ID *id, const void *id_address)
339{
340 using namespace blender::bke;
341 Mesh *mesh = reinterpret_cast<Mesh *>(id);
342 const bool is_undo = writer->is_undo();
343
344 ResourceScope scope;
345 Vector<CustomDataLayer, 16> vert_layers;
346 Vector<CustomDataLayer, 16> edge_layers;
347 Vector<CustomDataLayer, 16> loop_layers;
348 Vector<CustomDataLayer, 16> face_layers;
349 bke::AttributeStorage::BlendWriteData attribute_data{writer, scope};
350
351 /* Cache only - don't write. */
352 mesh->mface = nullptr;
353 mesh->totface_legacy = 0;
354 mesh->fdata_legacy = CustomData{};
355
356 /* Do not store actual geometry data in case this is a library override ID. */
357 if (ID_IS_OVERRIDE_LIBRARY(mesh) && !is_undo) {
358 mesh->verts_num = 0;
359 mesh->vert_data = CustomData{};
360
361 mesh->edges_num = 0;
362 mesh->edge_data = CustomData{};
363
364 mesh->corners_num = 0;
365 mesh->corner_data = CustomData{};
366
367 mesh->faces_num = 0;
368 mesh->face_data = CustomData{};
369 mesh->face_offset_indices = nullptr;
370 }
371 else {
372 /* No need to store data in 5.0 data format, because it uses #CustomData at runtime anyway. */
374 mesh->attribute_storage.wrap(),
375 false,
376 [&](const AttrDomain domain) { return mesh->attributes().domain_size(domain); },
377 attribute_data);
378 CustomData_blend_write_prepare(mesh->vert_data, vert_layers);
379 CustomData_blend_write_prepare(mesh->edge_data, edge_layers);
380 CustomData_blend_write_prepare(mesh->face_data, face_layers);
381 CustomData_blend_write_prepare(mesh->corner_data, loop_layers);
382 if (!is_undo) {
383 mesh_skin_to_legacy(attribute_data, mesh->vert_data, vert_layers, mesh->verts_num);
384 }
385 if (attribute_data.attributes.is_empty()) {
386 mesh->attribute_storage.dna_attributes = nullptr;
387 mesh->attribute_storage.dna_attributes_num = 0;
388 }
389 else {
390 mesh->attribute_storage.dna_attributes = attribute_data.attributes.data();
391 mesh->attribute_storage.dna_attributes_num = attribute_data.attributes.size();
392 }
393 }
394
395 const bke::MeshRuntime *mesh_runtime = mesh->runtime;
396 mesh->runtime = nullptr;
397
398 writer->generated_pointer_tag(mesh->attribute_storage.dna_attributes);
399
400 writer->write_id_struct(id_address, mesh, [](BlendStructWriter<Mesh> &struct_writer) {
401 struct_writer.generated_ptr(offsetof(Mesh, attribute_storage.dna_attributes));
402 });
403 BKE_id_blend_write(writer, &mesh->id);
404
405 BKE_defbase_blend_write(writer, &mesh->vertex_group_names);
406 writer->write_string(mesh->active_color_attribute);
407 writer->write_string(mesh->default_color_attribute);
408 writer->write_string(mesh->active_uv_map_attribute);
409 writer->write_string(mesh->default_uv_map_attribute);
410 writer->write_string(mesh->stencil_uv_map_attribute);
411 writer->write_string(mesh->clone_uv_map_attribute);
412
413 writer->write_pointer_array(mesh->totcol, mesh->mat);
414 writer->write_struct_array(mesh->totselect, mesh->mselect);
415
417 writer, &mesh->vert_data, vert_layers, mesh->verts_num, CD_MASK_MESH.vmask, &mesh->id);
419 writer, &mesh->edge_data, edge_layers, mesh->edges_num, CD_MASK_MESH.emask, &mesh->id);
420 /* `fdata` is cleared above but written so slots align. */
422 writer, &mesh->fdata_legacy, {}, mesh->totface_legacy, CD_MASK_MESH.fmask, &mesh->id);
424 writer, &mesh->corner_data, loop_layers, mesh->corners_num, CD_MASK_MESH.lmask, &mesh->id);
426 writer, &mesh->face_data, face_layers, mesh->faces_num, CD_MASK_MESH.pmask, &mesh->id);
427
428 mesh->attribute_storage.wrap().blend_write(*writer, attribute_data);
429
430 if (mesh->face_offset_indices) {
431 writer->write_shared(
432 mesh->face_offset_indices,
433 sizeof(int) * mesh->faces_num,
434 mesh_runtime->face_offsets_sharing_info,
435 [&]() { writer->write_int32_array(mesh->faces_num + 1, mesh->face_offset_indices); });
436 }
437}
438
439static void mesh_blend_read_data(BlendDataReader *reader, ID *id)
440{
441 Mesh *mesh = reinterpret_cast<Mesh *>(id);
442 BLO_read_pointer_array_and_validate_size(reader, &mesh->mat, &mesh->totcol);
443 /* This check added for python created meshes. */
444 if (!mesh->mat) {
445 mesh->totcol = 0;
446 }
447
448 /* Deprecated pointers to custom data layers are read here for backward compatibility
449 * with files where these were owning pointers rather than a view into custom data.
450 *
451 * Ignore failure to read, these arrays are not further accessed here and blend file
452 * read will abort before versioning runs. */
453 (void)BLO_read_array(reader, &mesh->mvert, mesh->verts_num);
454 (void)BLO_read_array(reader, &mesh->medge, mesh->edges_num);
455 (void)BLO_read_array(reader, &mesh->mface, mesh->totface_legacy);
456 (void)BLO_read_array(reader, &mesh->mtface, mesh->totface_legacy);
457 (void)BLO_read_array(reader, &mesh->dvert, mesh->verts_num);
458 (void)BLO_read_array(reader, &mesh->tface, mesh->totface_legacy);
459 (void)BLO_read_array(reader, &mesh->mcol, mesh->totface_legacy);
460
461 BLO_read_array_and_validate_size(reader, &mesh->mselect, &mesh->totselect);
462
463 BLO_read_struct_list(reader, bDeformGroup, &mesh->vertex_group_names);
464
465 CustomData_blend_read(reader, &mesh->vert_data, mesh->verts_num);
466 CustomData_blend_read(reader, &mesh->edge_data, mesh->edges_num);
467 CustomData_blend_read(reader, &mesh->fdata_legacy, mesh->totface_legacy);
468 CustomData_blend_read(reader, &mesh->corner_data, mesh->corners_num);
469 CustomData_blend_read(reader, &mesh->face_data, mesh->faces_num);
470 mesh->attribute_storage.wrap().blend_read(*reader);
471 if (mesh->deform_verts().is_empty()) {
472 /* Vertex group data was also an owning pointer in old Blender versions.
473 * Don't read them again if they were read as part of #CustomData. */
474 BKE_defvert_blend_read(reader, mesh->verts_num, mesh->dvert);
475 }
476 BLO_read_string(reader, &mesh->active_color_attribute);
477 BLO_read_string(reader, &mesh->default_color_attribute);
478 BLO_read_string(reader, &mesh->active_uv_map_attribute);
479 BLO_read_string(reader, &mesh->default_uv_map_attribute);
480 BLO_read_string(reader, &mesh->stencil_uv_map_attribute);
481 BLO_read_string(reader, &mesh->clone_uv_map_attribute);
482
483 mesh->texspace_flag &= ~ME_TEXSPACE_FLAG_AUTO_EVALUATED;
484
485 mesh->runtime = new bke::MeshRuntime();
486
487 if (mesh->face_offset_indices) {
488 mesh->runtime->face_offsets_sharing_info = BLO_read_shared(
489 reader, &mesh->face_offset_indices, [&]() {
490 if (!BLO_read_array(reader, &mesh->face_offset_indices, int64_t(mesh->faces_num) + 1)) {
491 mesh->faces_num = 0;
492 }
493 return mesh->face_offset_indices ?
494 implicit_sharing::info_for_mem_free(mesh->face_offset_indices) :
495 nullptr;
496 });
497 }
498
499 if (mesh->mselect == nullptr) {
500 mesh->totselect = 0;
501 }
502
503 /* NOTE: this is endianness-sensitive. */
504 /* Each legacy TFace would need to undo the automatic DNA switch of its array of four uint32_t
505 * RGBA colors. */
506}
507
509 .id_code = Mesh::id_type,
510 .id_filter = FILTER_ID_ME,
511 .dependencies_id_types = FILTER_ID_ME | FILTER_ID_MA | FILTER_ID_IM | FILTER_ID_KE,
512 .main_listbase_index = INDEX_ID_ME,
513 .struct_size = sizeof(Mesh),
514 .name = "Mesh",
515 .name_plural = N_("meshes"),
516 .translation_context = BLT_I18NCONTEXT_ID_MESH,
518 .asset_type_info = nullptr,
519
520 .init_data = mesh_init_data,
521 .copy_data = mesh_copy_data,
522 .free_data = mesh_free_data,
523 .make_local = nullptr,
524 .foreach_id = mesh_foreach_id,
525 .foreach_cache = nullptr,
526 .foreach_path = mesh_foreach_path,
527 .foreach_working_space_color = mesh_foreach_working_space_color,
528 .foreach_asset_weak_reference = nullptr,
529 .owner_pointer_get = nullptr,
530
531 .blend_write = mesh_blend_write,
532 .blend_read_data = mesh_blend_read_data,
533 .blend_read_after_liblink = nullptr,
534
535 .blend_read_undo_preserve = nullptr,
536
537 .lib_override_apply_post = nullptr,
538};
539
541{
542 return ELEM(name, "position", ".corner_vert", ".corner_edge", ".edge_verts");
543}
544
546{
547 using namespace bke;
549 if (!CustomData_has_layer_named(&bm->vdata, CD_PROP_FLOAT2, "skin_modifier_radius")) {
550 BM_data_layer_add_named(bm, &bm->vdata, CD_PROP_FLOAT2, "skin_modifier_radius");
551 const int offset = CustomData_get_offset_named(
552 &bm->vdata, CD_PROP_FLOAT2, "skin_modifier_radius");
553 BMVert *v;
554 BMIter iter;
555 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
556 *static_cast<float2 *>(BM_ELEM_CD_GET_VOID_P(v, offset)) = float2(0.25f);
557 }
558 }
559 if (!CustomData_has_layer_named(&bm->vdata, CD_PROP_BOOL, "skin_modifier_root")) {
560 BM_data_layer_add_named(bm, &bm->vdata, CD_PROP_BOOL, "skin_modifier_root");
561 const int offset = CustomData_get_offset_named(
562 &bm->vdata, CD_PROP_BOOL, "skin_modifier_root");
563 /* Mark an arbitrary vertex as root */
564 BMVert *v;
565 BMIter iter;
566 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
567 BM_ELEM_CD_SET_BOOL(v, offset, true);
568 break;
569 }
570 }
571 }
572 else {
573 MutableAttributeAccessor attributes = mesh->attributes_for_write();
574 attributes.add<float2>(
575 "skin_modifier_radius", AttrDomain::Point, AttributeInitValue(float2(0.25f)));
576 if (attributes.add<bool>("skin_modifier_root", AttrDomain::Point, AttributeInitDefaultValue()))
577 {
578 /* Mark an arbitrary vertex as root */
579 if (mesh->verts_num > 0) {
580 AttributeWriter<bool> root = attributes.lookup_for_write<bool>("skin_modifier_root");
581 root.varray.set(0, true);
582 root.finish();
583 }
584 }
585 }
586}
587
589{
591 return CustomData_has_layer_named(&bm->ldata, CD_PROP_INT16_2D, "custom_normal");
592 }
593
594 return mesh->attributes().contains("custom_normal");
595}
596
597namespace bke {
598
600 const StringRef name,
601 AttrDomain domain,
602 bke::AttrType data_type)
603{
605 return;
606 }
607 if (!mesh::is_color_attribute({domain, data_type})) {
608 return;
609 }
610 if (mesh.default_color_attribute) {
611 return;
612 }
613 mesh.default_color_attribute = BLI_strdupn(name.data(), name.size());
614}
615
617 const StringRef name,
618 AttrDomain domain,
619 bke::AttrType data_type)
620{
622 return;
623 }
624 if (!mesh::is_uv_map({domain, data_type})) {
625 return;
626 }
627 if (!mesh.default_uv_map_name().is_empty()) {
628 return;
629 }
630 mesh.uv_maps_default_set(name);
631}
632
634{
635 MutableAttributeAccessor attributes = mesh.attributes_for_write();
636 AttributeInitConstruct attribute_init;
637
638 /* Try to create attributes if they do not exist. */
639 attributes.add("position", AttrDomain::Point, bke::AttrType::Float3, attribute_init);
640 attributes.add(".edge_verts", AttrDomain::Edge, bke::AttrType::Int32_2D, attribute_init);
641 attributes.add(".corner_vert", AttrDomain::Corner, bke::AttrType::Int32, attribute_init);
642 attributes.add(".corner_edge", AttrDomain::Corner, bke::AttrType::Int32, attribute_init);
643}
644
646{
647 StringRefNull found;
648 mesh.attributes().foreach_attribute([&](const AttributeIter &iter) {
649 if (iter.domain == AttrDomain::Corner && iter.data_type == AttrType::Float2) {
650 found = iter.name;
651 iter.stop();
652 }
653 });
654 return found;
655}
656
658{
659 const StringRefNull active_name = mesh.active_uv_map_name();
660 if (!active_name.is_empty()) {
661 return;
662 }
663 const StringRefNull default_name = mesh.default_uv_map_name();
664 if (!default_name.is_empty()) {
665 mesh.uv_maps_active_set(default_name);
666 return;
667 }
669 if (!found.is_empty()) {
670 mesh.uv_maps_active_set(found);
671 }
672}
673
675{
676 const StringRefNull default_name = mesh.default_uv_map_name();
677 if (!default_name.is_empty()) {
678 return;
679 }
681 if (!found.is_empty()) {
682 mesh.uv_maps_default_set(found);
683 }
684}
685
687{
688 bke::AttributeAccessor attributes = mesh.attributes();
689 if (!mesh::is_color_attribute(attributes.lookup_meta_data(mesh.active_color_attribute))) {
690 MEM_SAFE_DELETE(mesh.active_color_attribute);
691 }
692 if (!mesh::is_color_attribute(attributes.lookup_meta_data(mesh.default_color_attribute))) {
693 MEM_SAFE_DELETE(mesh.default_color_attribute);
694 }
695 if (!mesh::is_uv_map(attributes.lookup_meta_data(mesh.active_uv_map_name()))) {
696 MEM_SAFE_DELETE(mesh.active_uv_map_attribute);
697 }
698 if (!mesh::is_uv_map(attributes.lookup_meta_data(mesh.default_uv_map_name()))) {
699 MEM_SAFE_DELETE(mesh.default_uv_map_attribute);
700 }
701 if (!mesh::is_uv_map(attributes.lookup_meta_data(mesh.stencil_uv_map_attribute))) {
702 MEM_SAFE_DELETE(mesh.stencil_uv_map_attribute);
703 }
704 if (!mesh::is_uv_map(attributes.lookup_meta_data(mesh.clone_uv_map_attribute))) {
705 MEM_SAFE_DELETE(mesh.clone_uv_map_attribute);
706 }
707}
708
710{
711 return bounds::merge(a, b);
712}
713
715{
716 return {float3(std::numeric_limits<float>::max()), float3(std::numeric_limits<float>::lowest())};
717}
718
727
728static void partition_faces_recursively(const Span<float3> face_centers,
729 MutableSpan<int> face_indices,
731 int node_index,
732 int depth,
733 const std::optional<Bounds<float3>> &bounds_precalc,
734 const Span<int> material_indices,
735 int target_group_size)
736{
737 if (face_indices.size() <= target_group_size || depth >= STACK_FIXED_DEPTH - 1) {
738 if (!bke::pbvh::leaf_needs_material_split(face_indices, material_indices)) {
739 groups[node_index].children_offset = 0;
740 groups[node_index].faces = Array<int>(face_indices.size(), NoInitialization());
741 std::copy(face_indices.begin(), face_indices.end(), groups[node_index].faces.begin());
742 return;
743 }
744 }
745
746 const int children_start = groups.size();
747 groups[node_index].children_offset = children_start;
748
749 groups.resize(groups.size() + 2);
750 groups[children_start].parent = node_index;
751 groups[children_start + 1].parent = node_index;
752
753 int split;
754 if (!(face_indices.size() <= target_group_size || depth >= STACK_FIXED_DEPTH - 1)) {
756 if (bounds_precalc) {
757 bounds = *bounds_precalc;
758 }
759 else {
761 face_indices.index_range(),
762 1024,
764 [&](const IndexRange range, Bounds<float3> value) {
765 for (const int face : face_indices.slice(range)) {
766 math::min_max(face_centers[face], value.min, value.max);
767 }
768 return value;
769 },
771 }
772 const int axis = math::dominant_axis(bounds.max - bounds.min);
773
775 face_centers, face_indices, axis, math::midpoint(bounds.min[axis], bounds.max[axis]));
776 }
777 else {
778 split = bke::pbvh::partition_material_indices(material_indices, face_indices);
779 }
780
781 partition_faces_recursively(face_centers,
782 face_indices.take_front(split),
783 groups,
784 children_start,
785 depth + 1,
786 std::nullopt,
787 material_indices,
788 target_group_size);
789 partition_faces_recursively(face_centers,
790 face_indices.drop_front(split),
791 groups,
792 children_start + 1,
793 depth + 1,
794 std::nullopt,
795 material_indices,
796 target_group_size);
797}
798
799static void build_vertex_groups_for_leaves(const int verts_num,
800 const OffsetIndices<int> faces,
801 const Span<int> corner_verts,
803{
804 Vector<int> leaf_indices;
805 for (const int i : groups.index_range()) {
806 if (groups[i].children_offset == 0 && !groups[i].faces.is_empty()) {
807 leaf_indices.append(i);
808 }
809 }
810
811 Array<Array<int>> verts_per_leaf(leaf_indices.size(), NoInitialization());
812
813 threading::parallel_for(leaf_indices.index_range(), 8, [&](const IndexRange range) {
814 Set<int> verts;
815 for (const int i : range) {
816 const int group_idx = leaf_indices[i];
817 NonContiguousGroup &group = groups[group_idx];
818 verts.clear();
819 int corners_count = 0;
820
821 for (const int face_index : group.faces) {
822 const IndexRange face = faces[face_index];
823 verts.add_multiple(corner_verts.slice(face));
824 corners_count += face.size();
825 }
826
827 new (&verts_per_leaf[i]) Array<int>(verts.size());
828 std::copy(verts.begin(), verts.end(), verts_per_leaf[i].begin());
829 std::ranges::sort(verts_per_leaf[i]);
830 group.corner_count = corners_count;
831 }
832 });
833
834 Vector<int> owned_verts;
835 Vector<int> shared_verts;
836 BitVector<> vert_used(verts_num);
837
838 for (const int i : leaf_indices.index_range()) {
839 const int group_idx = leaf_indices[i];
840 NonContiguousGroup &group = groups[group_idx];
841 owned_verts.clear();
842 shared_verts.clear();
843
844 for (const int vert : verts_per_leaf[i]) {
845 if (vert_used[vert]) {
846 shared_verts.append(vert);
847 }
848 else {
849 vert_used[vert].set();
850 owned_verts.append(vert);
851 }
852 }
853
854 if (!owned_verts.is_empty()) {
855 group.unique_verts = Array<int>(owned_verts.size());
856 std::copy(owned_verts.begin(), owned_verts.end(), group.unique_verts.begin());
857 }
858
859 if (!shared_verts.is_empty()) {
860 group.shared_verts = Array<int>(shared_verts.size());
861 std::copy(shared_verts.begin(), shared_verts.end(), group.shared_verts.begin());
862 }
863 }
864}
865
867{
868 const Span<float3> vert_positions = mesh.vert_positions();
869 const OffsetIndices<int> faces = mesh.faces();
870 const Span<int> corner_verts = mesh.corner_verts();
871
872 if (faces.is_empty()) {
873 return {};
874 }
875
876 Array<float3> face_centers(faces.size());
878 faces.index_range(),
879 1024,
881 [&](const IndexRange range, const Bounds<float3> &init) {
882 Bounds<float3> current = init;
883 for (const int face : range) {
885 vert_positions, corner_verts.slice(faces[face]));
886 face_centers[face] = bounds.center();
887 current = bounds::merge(current, bounds);
888 }
889 return current;
890 },
892
893 Array<int> prim_face_indices(mesh.faces_num);
894 array_utils::fill_index_range<int>(prim_face_indices);
895
897 groups.resize(1);
898 groups[0].parent = -1;
899 groups[0].children_offset = 0;
900
901 const AttributeAccessor attributes = mesh.attributes();
902 const VArraySpan material_index = *attributes.lookup<int>("material_index", AttrDomain::Face);
903
905 face_centers, prim_face_indices, groups, 0, 0, bounds, material_index, 2500);
906
907 build_vertex_groups_for_leaves(mesh.verts_num, faces, corner_verts, groups);
908
909 return groups;
910}
911
913{
914 BLI_assert(mesh.faces_num != 0);
915 if (mesh.verts_num == 0 || mesh.faces_num == 0) {
916 return;
917 }
918
920
921 Vector<int> new_vert_order;
922 new_vert_order.reserve(mesh.verts_num);
923
924 Vector<int> new_face_order;
925 new_face_order.reserve(mesh.faces_num);
926
927 BitVector<> added_verts(mesh.verts_num, false);
928
929 Vector<int> group_unique_offsets;
930 group_unique_offsets.reserve(local_groups.size() + 1);
931 group_unique_offsets.append(0);
932
933 Vector<int> group_face_offsets;
934 group_face_offsets.reserve(local_groups.size() + 1);
935 group_face_offsets.append(0);
936
937 for (const int group_index : local_groups.index_range()) {
938 const NonContiguousGroup &local_group = local_groups[group_index];
939
940 for (const int vert_idx : local_group.unique_verts) {
941 if (!added_verts[vert_idx]) {
942 new_vert_order.append(vert_idx);
943 added_verts[vert_idx].set();
944 }
945 }
946 group_unique_offsets.append(new_vert_order.size());
947
948 for (const int vert_idx : local_group.shared_verts) {
949 if (!added_verts[vert_idx]) {
950 new_vert_order.append(vert_idx);
951 added_verts[vert_idx].set();
952 }
953 }
954
955 for (const int face_idx : local_group.faces) {
956 new_face_order.append(face_idx);
957 }
958 group_face_offsets.append(new_face_order.size());
959 }
960
961 for (const int vert : IndexRange(mesh.verts_num)) {
962 if (!added_verts[vert]) {
963 new_vert_order.append(vert);
964 added_verts[vert].set();
965 }
966 }
967
968 Array<int> vert_reverse_map(mesh.verts_num);
969 for (const int i : IndexRange(mesh.verts_num)) {
970 vert_reverse_map[new_vert_order[i]] = i;
971 }
972
973 MutableSpan edges = mesh.edges_for_write();
974 for (int2 &edge : edges) {
975 edge.x = vert_reverse_map[edge.x];
976 edge.y = vert_reverse_map[edge.y];
977 }
978
979 MutableSpan<int> corner_verts = mesh.corner_verts_for_write();
980 Array<int> new_corner_verts(corner_verts.size());
981 const OffsetIndices<int> old_faces = mesh.faces();
982
983 int new_corner_idx = 0;
984 for (const int old_face_idx : new_face_order) {
985 const IndexRange face = old_faces[old_face_idx];
986 for (const int corner : face) {
987 new_corner_verts[new_corner_idx] = vert_reverse_map[corner_verts[corner]];
988 new_corner_idx++;
989 }
990 }
991 corner_verts.copy_from(new_corner_verts);
992
993 MutableAttributeAccessor attributes_for_write = mesh.attributes_for_write();
994 attributes_for_write.foreach_attribute([&](const bke::AttributeIter &iter) {
996 return;
997 }
998 if (iter.domain == bke::AttrDomain::Face) {
1000 GMutableSpan attribute_data = attribute.span;
1001 const CPPType &type = attribute_data.type();
1002 GArray<> new_values(type, new_face_order.size(), NoInitialization{});
1003
1004 int new_face_idx = 0;
1005 for (const int old_face_idx : new_face_order) {
1006 type.copy_construct(attribute_data[old_face_idx], new_values[new_face_idx]);
1007 new_face_idx++;
1008 }
1009 attribute_data.copy_from(new_values.as_span());
1010 attribute.finish();
1011 }
1012 else if (iter.domain == bke::AttrDomain::Point) {
1014 GMutableSpan attribute_data = attribute.span;
1015 const CPPType &type = attribute_data.type();
1016 GArray<> new_values(type, new_vert_order.size(), NoInitialization{});
1017
1018 int new_vert_idx = 0;
1019 for (const int old_vert_idx : new_vert_order) {
1020 type.copy_construct(attribute_data[old_vert_idx], new_values[new_vert_idx]);
1021 new_vert_idx++;
1022 }
1023 attribute_data.copy_from(new_values.as_span());
1024 attribute.finish();
1025 }
1026 else if (iter.domain == bke::AttrDomain::Corner && iter.name != ".corner_vert") {
1028 GMutableSpan attribute_data = attribute.span;
1029 const CPPType &type = attribute_data.type();
1030 GArray<> new_values(type, attribute_data.size(), NoInitialization{});
1031
1032 int new_corner_idx = 0;
1033 for (const int old_face_idx : new_face_order) {
1034 const IndexRange face = old_faces[old_face_idx];
1035 for (const int old_corner_idx : face) {
1036 type.copy_construct(attribute_data[old_corner_idx], new_values[new_corner_idx]);
1037 new_corner_idx++;
1038 }
1039 }
1040 attribute_data.copy_from(new_values.as_span());
1041 attribute.finish();
1042 }
1043 });
1044
1045 Vector<int> face_sizes(new_face_order.size());
1046 gather_group_sizes(old_faces, new_face_order, face_sizes);
1047 MutableSpan<int> face_offsets = mesh.face_offsets_for_write();
1048 face_offsets.take_front(face_sizes.size()).copy_from(face_sizes);
1050
1051 for (NonContiguousGroup &local_group : local_groups) {
1052 for (int &vert_idx : local_group.unique_verts) {
1053 vert_idx = vert_reverse_map[vert_idx];
1054 }
1055 for (int &vert_idx : local_group.shared_verts) {
1056 vert_idx = vert_reverse_map[vert_idx];
1057 }
1058 }
1059
1060 Array<MeshGroup> nodes(local_groups.size());
1061
1062 for (const int node_idx : local_groups.index_range()) {
1063 const NonContiguousGroup &local_group = local_groups[node_idx];
1064 MeshGroup &node = nodes[node_idx];
1065
1066 node.parent = local_group.parent;
1067 node.children_offset = local_group.children_offset;
1068 node.corners_count = local_group.corner_count;
1069 node.unique_verts = IndexRange(0, 0);
1070 node.faces = IndexRange(0, 0);
1071 if (local_group.children_offset == 0 && !local_group.faces.is_empty()) {
1072 int unique_start = (node_idx == 0) ? 0 : group_unique_offsets[node_idx];
1073 int unique_end = group_unique_offsets[node_idx + 1];
1074 node.unique_verts = IndexRange(unique_start, unique_end - unique_start);
1075
1076 int face_start = (node_idx == 0) ? 0 : group_face_offsets[node_idx];
1077 int face_end = group_face_offsets[node_idx + 1];
1078 node.faces = IndexRange(face_start, face_end - face_start);
1079
1080 if (!local_group.shared_verts.is_empty()) {
1081 node.shared_verts = Array<int>(local_group.shared_verts.size());
1082 for (const int j : local_group.shared_verts.index_range()) {
1083 node.shared_verts[j] = local_group.shared_verts[j];
1084 }
1085 }
1086 }
1087 }
1088
1089 mesh.tag_positions_changed();
1090 mesh.tag_topology_changed();
1091 mesh.runtime->spatial_groups = std::make_unique<Array<MeshGroup>>(std::move(nodes));
1092}
1093
1094} // namespace bke
1095
1109{
1110 CustomData_free(&mesh.vert_data);
1111 CustomData_free(&mesh.edge_data);
1112 CustomData_free(&mesh.fdata_legacy);
1113 CustomData_free(&mesh.corner_data);
1114 CustomData_free(&mesh.face_data);
1115 mesh.attribute_storage.wrap() = bke::AttributeStorage();
1116 if (mesh.face_offset_indices) {
1117 implicit_sharing::free_shared_data(&mesh.face_offset_indices,
1118 &mesh.runtime->face_offsets_sharing_info);
1119 }
1120 MEM_SAFE_DELETE(mesh.mselect);
1121
1122 mesh.verts_num = 0;
1123 mesh.edges_num = 0;
1124 mesh.totface_legacy = 0;
1125 mesh.corners_num = 0;
1126 mesh.faces_num = 0;
1127 mesh.act_face = -1;
1128 mesh.totselect = 0;
1129}
1130
1132{
1133 mesh.vertex_group_names.free_no_destruct();
1134 MEM_SAFE_DELETE(mesh.active_color_attribute);
1135 MEM_SAFE_DELETE(mesh.default_color_attribute);
1136 MEM_SAFE_DELETE(mesh.active_uv_map_attribute);
1137 MEM_SAFE_DELETE(mesh.default_uv_map_attribute);
1138 MEM_SAFE_DELETE(mesh.stencil_uv_map_attribute);
1139 MEM_SAFE_DELETE(mesh.clone_uv_map_attribute);
1140}
1141
1147
1154
1155static void mesh_tessface_clear_intern(Mesh *mesh, int free_customdata)
1156{
1157 if (free_customdata) {
1158 CustomData_free(&mesh->fdata_legacy);
1159 }
1160 else {
1161 CustomData_reset(&mesh->fdata_legacy);
1162 }
1163
1164 mesh->totface_legacy = 0;
1165}
1166
1167Mesh *BKE_mesh_add(Main *bmain, const char *name)
1168{
1169 return BKE_id_new<Mesh>(bmain, name);
1170}
1171
1173{
1174 BLI_assert(mesh->face_offset_indices == nullptr);
1175 BLI_assert(mesh->runtime->face_offsets_sharing_info == nullptr);
1176 if (mesh->faces_num == 0) {
1177 return;
1178 }
1179 mesh->face_offset_indices = MEM_new_array_uninitialized<int>(size_t(mesh->faces_num) + 1,
1180 __func__);
1181 mesh->runtime->face_offsets_sharing_info = implicit_sharing::info_for_mem_free(
1182 mesh->face_offset_indices);
1183
1184#ifndef NDEBUG
1185 /* Fill offsets with obviously bad values to simplify finding missing initialization. */
1186 mesh->face_offsets_for_write().fill(-1);
1187#endif
1188 /* Set common values for convenience. */
1189 mesh->face_offset_indices[0] = 0;
1190 mesh->face_offset_indices[mesh->faces_num] = mesh->corners_num;
1191}
1192
1193Span<float3> Mesh::vert_positions() const
1194{
1196 this->attribute_storage.wrap(), bke::AttrDomain::Point, "position", this->verts_num)
1197 .value_or(Span<float3>());
1198}
1199MutableSpan<float3> Mesh::vert_positions_for_write()
1200{
1201 return bke::get_mutable_attribute<float3>(
1202 this->attribute_storage.wrap(), bke::AttrDomain::Point, "position", this->verts_num);
1203}
1204
1205Span<int2> Mesh::edges() const
1206{
1207 return bke::get_span_attribute<int2>(
1208 this->attribute_storage.wrap(), bke::AttrDomain::Edge, ".edge_verts", this->edges_num)
1209 .value_or(Span<int2>());
1210}
1211MutableSpan<int2> Mesh::edges_for_write()
1212{
1213 return bke::get_mutable_attribute<int2>(
1214 this->attribute_storage.wrap(), bke::AttrDomain::Edge, ".edge_verts", this->edges_num);
1215}
1216
1217OffsetIndices<int> Mesh::faces() const
1218{
1219 return Span(this->face_offset_indices, this->faces_num + 1);
1220}
1221Span<int> Mesh::face_offsets() const
1222{
1223 if (this->faces_num == 0) {
1224 return {};
1225 }
1226 return {this->face_offset_indices, this->faces_num + 1};
1227}
1228MutableSpan<int> Mesh::face_offsets_for_write()
1229{
1230 if (this->faces_num == 0) {
1231 return {};
1232 }
1233 implicit_sharing::make_trivial_data_mutable(
1234 &this->face_offset_indices, &this->runtime->face_offsets_sharing_info, this->faces_num + 1);
1235 return {this->face_offset_indices, this->faces_num + 1};
1236}
1237
1238Span<int> Mesh::corner_verts() const
1239{
1240 return bke::get_span_attribute<int>(this->attribute_storage.wrap(),
1241 bke::AttrDomain::Corner,
1242 ".corner_vert",
1243 this->corners_num)
1244 .value_or(Span<int>());
1245}
1246MutableSpan<int> Mesh::corner_verts_for_write()
1247{
1248 return bke::get_mutable_attribute<int>(
1249 this->attribute_storage.wrap(), bke::AttrDomain::Corner, ".corner_vert", this->corners_num);
1250}
1251
1252Span<int> Mesh::corner_edges() const
1253{
1254 return bke::get_span_attribute<int>(this->attribute_storage.wrap(),
1255 bke::AttrDomain::Corner,
1256 ".corner_edge",
1257 this->corners_num)
1258 .value_or(Span<int>());
1259}
1260MutableSpan<int> Mesh::corner_edges_for_write()
1261{
1262 return bke::get_mutable_attribute<int>(
1263 this->attribute_storage.wrap(), bke::AttrDomain::Corner, ".corner_edge", this->corners_num);
1264}
1265
1266Span<MDeformVert> Mesh::deform_verts() const
1267{
1268 const MDeformVert *dverts = static_cast<const MDeformVert *>(
1269 CustomData_get_layer(&this->vert_data, CD_MDEFORMVERT));
1270 if (!dverts) {
1271 return {};
1272 }
1273 return {dverts, this->verts_num};
1274}
1275MutableSpan<MDeformVert> Mesh::deform_verts_for_write()
1276{
1277 MDeformVert *dvert = static_cast<MDeformVert *>(
1278 CustomData_get_layer_for_write(&this->vert_data, CD_MDEFORMVERT, this->verts_num));
1279 if (dvert) {
1280 return {dvert, this->verts_num};
1281 }
1282 return {static_cast<MDeformVert *>(CustomData_add_layer(
1283 &this->vert_data, CD_MDEFORMVERT, CD_SET_DEFAULT, this->verts_num)),
1284 this->verts_num};
1285}
1286
1287void Mesh::count_memory(MemoryCounter &memory) const
1288{
1289 memory.add_shared(this->runtime->face_offsets_sharing_info,
1290 this->face_offsets().size_in_bytes());
1291 this->attribute_storage.wrap().count_memory(memory);
1292 CustomData_count_memory(this->vert_data, this->verts_num, memory);
1293 CustomData_count_memory(this->edge_data, this->edges_num, memory);
1294 CustomData_count_memory(this->face_data, this->faces_num, memory);
1295 CustomData_count_memory(this->corner_data, this->corners_num, memory);
1296}
1297
1299{
1300 return bke::AttributeAccessor(this, bke::mesh_attribute_accessor_functions());
1301}
1302
1303bke::MutableAttributeAccessor Mesh::attributes_for_write()
1304{
1305 return bke::MutableAttributeAccessor(this, bke::mesh_attribute_accessor_functions());
1306}
1307
1308VectorSet<StringRefNull> Mesh::uv_map_names() const
1309{
1310 VectorSet<StringRefNull> names;
1311 this->attributes().foreach_attribute([&](const bke::AttributeIter &iter) {
1312 if (bke::mesh::is_uv_map({iter.domain, iter.data_type})) {
1313 names.add_new(iter.name);
1314 }
1315 });
1316 return names;
1317}
1318
1319StringRefNull Mesh::active_uv_map_name() const
1320{
1321 if (const BMesh *bm = BKE_editmesh_bmesh_get(this)) {
1322 const char *name = CustomData_get_active_layer_name(&bm->ldata, CD_PROP_FLOAT2);
1323 return name ? name : "";
1324 }
1325 return this->active_uv_map_attribute ? this->active_uv_map_attribute : "";
1326}
1327
1328StringRefNull Mesh::default_uv_map_name() const
1329{
1330 if (const BMesh *bm = BKE_editmesh_bmesh_get(this)) {
1331 const char *name = CustomData_get_render_layer_name(&bm->ldata, CD_PROP_FLOAT2);
1332 return name ? name : "";
1333 }
1334 return this->default_uv_map_attribute ? this->default_uv_map_attribute : "";
1335}
1336
1337StringRefNull Mesh::active_or_default_uv_map_name() const
1338{
1339 const StringRefNull active_name = this->active_uv_map_name();
1340 if (!active_name.is_empty()) {
1341 return active_name;
1342 }
1343 return this->default_uv_map_name();
1344}
1345
1346void Mesh::uv_maps_active_set(const StringRef name)
1347{
1348 MEM_SAFE_DELETE(this->active_uv_map_attribute);
1349 if (!name.is_empty()) {
1351 }
1352 if (BMesh *bm = BKE_editmesh_bmesh_get_for_write(this)) {
1353 int index = CustomData_get_named_layer_index(&bm->ldata, CD_PROP_FLOAT2, name);
1354 if (index == -1) {
1355 index = CustomData_get_layer_index(&bm->ldata, CD_PROP_FLOAT2);
1356 }
1357 CustomData_set_layer_active_index(&bm->ldata, CD_PROP_FLOAT2, index);
1358 }
1359}
1360
1361void Mesh::uv_maps_default_set(const StringRef name)
1362{
1363 MEM_SAFE_DELETE(this->default_uv_map_attribute);
1364 if (!name.is_empty()) {
1366 }
1367 if (BMesh *bm = BKE_editmesh_bmesh_get_for_write(this)) {
1368 int index = CustomData_get_named_layer_index(&bm->ldata, CD_PROP_FLOAT2, name);
1369 if (index == -1) {
1370 index = CustomData_get_layer_index(&bm->ldata, CD_PROP_FLOAT2);
1371 }
1372 CustomData_set_layer_render_index(&bm->ldata, CD_PROP_FLOAT2, index);
1373 }
1374}
1375
1376Mesh *BKE_mesh_new_nomain(const int verts_num,
1377 const int edges_num,
1378 const int faces_num,
1379 const int corners_num)
1380{
1381 Mesh *mesh = bke::mesh_new_no_attributes(verts_num, edges_num, faces_num, corners_num);
1383 return mesh;
1384}
1385
1386namespace bke {
1387
1388namespace mesh {
1389
1390bool is_uv_map(const AttributeMetaData &meta_data)
1391{
1392 return meta_data.domain == AttrDomain::Corner && meta_data.data_type == AttrType::Float2;
1393}
1394
1395bool is_uv_map(const std::optional<AttributeMetaData> &meta_data)
1396{
1397 return meta_data && is_uv_map(*meta_data);
1398}
1399
1405
1406bool is_color_attribute(const std::optional<bke::AttributeMetaData> &meta_data)
1407{
1408 return meta_data && is_color_attribute(*meta_data);
1409}
1410
1411} // namespace mesh
1412
1413Mesh *mesh_new_no_attributes(const int verts_num,
1414 const int edges_num,
1415 const int faces_num,
1416 const int corners_num)
1417{
1418 Mesh *mesh = static_cast<Mesh *>(BKE_libblock_alloc(
1421
1422 mesh->verts_num = verts_num;
1423 mesh->edges_num = edges_num;
1424 mesh->faces_num = faces_num;
1425 mesh->corners_num = corners_num;
1426
1428
1429 return mesh;
1430}
1431
1432} // namespace bke
1433
1434static void copy_attribute_names(const Mesh &mesh_src, Mesh &mesh_dst)
1435{
1436 if (mesh_src.active_color_attribute) {
1437 MEM_SAFE_DELETE(mesh_dst.active_color_attribute);
1439 }
1440 if (mesh_src.default_color_attribute) {
1441 MEM_SAFE_DELETE(mesh_dst.default_color_attribute);
1443 }
1444 if (mesh_src.active_uv_map_attribute) {
1445 MEM_SAFE_DELETE(mesh_dst.active_uv_map_attribute);
1447 }
1448 if (mesh_src.default_uv_map_attribute) {
1449 MEM_SAFE_DELETE(mesh_dst.default_uv_map_attribute);
1451 }
1452 if (mesh_src.stencil_uv_map_attribute) {
1453 MEM_SAFE_DELETE(mesh_dst.stencil_uv_map_attribute);
1455 }
1456 if (mesh_src.clone_uv_map_attribute) {
1457 MEM_SAFE_DELETE(mesh_dst.clone_uv_map_attribute);
1459 }
1460}
1461
1462void BKE_mesh_copy_parameters(Mesh *me_dst, const Mesh *me_src)
1463{
1464 /* Copy general settings. */
1465 me_dst->editflag = me_src->editflag;
1466 me_dst->flag = me_src->flag;
1467 me_dst->remesh_voxel_size = me_src->remesh_voxel_size;
1469 me_dst->remesh_mode = me_src->remesh_mode;
1470 me_dst->symmetry = me_src->symmetry;
1471
1474
1475 /* Copy texture space. */
1476 me_dst->texspace_flag = me_src->texspace_flag;
1478 copy_v3_v3(me_dst->texspace_size, me_src->texspace_size);
1479
1482}
1483
1484void BKE_mesh_copy_parameters_for_eval(Mesh *me_dst, const Mesh *me_src)
1485{
1486 /* User counts aren't handled, don't copy into a mesh from #G_MAIN. */
1488
1489 STRNCPY(me_dst->id.name, me_src->id.name);
1490 BKE_mesh_copy_parameters(me_dst, me_src);
1491 copy_attribute_names(*me_src, *me_dst);
1492
1493 /* Copy vertex group names. */
1494 BLI_assert(me_dst->vertex_group_names.is_empty());
1496
1497 /* Copy materials. */
1498 if (me_dst->mat != nullptr) {
1499 MEM_delete(me_dst->mat);
1500 }
1501 me_dst->mat = MEM_dupalloc(me_src->mat);
1502 me_dst->totcol = me_src->totcol;
1503
1504 me_dst->runtime->edit_mesh = me_src->runtime->edit_mesh;
1505}
1506
1508 const int verts_num,
1509 const int edges_num,
1510 const int tessface_num,
1511 const int faces_num,
1512 const int corners_num,
1514{
1515 /* Only do tessface if we are creating tessfaces or copying from mesh with only tessfaces. */
1516 const bool do_tessface = (tessface_num ||
1517 ((me_src->totface_legacy != 0) && (me_src->faces_num == 0)));
1518
1519 Mesh *me_dst = BKE_id_new_nomain<Mesh>(nullptr);
1520 STRNCPY(me_dst->id.name, me_src->id.name);
1521
1522 me_dst->mselect = MEM_dupalloc(me_src->mselect);
1523
1524 me_dst->verts_num = verts_num;
1525 me_dst->edges_num = edges_num;
1526 me_dst->faces_num = faces_num;
1527 me_dst->corners_num = corners_num;
1528 me_dst->totface_legacy = tessface_num;
1529
1530 BKE_mesh_copy_parameters_for_eval(me_dst, me_src);
1531
1533 &me_src->vert_data, &me_dst->vert_data, mask.vmask, CD_SET_DEFAULT, verts_num);
1535 &me_src->edge_data, &me_dst->edge_data, mask.emask, CD_SET_DEFAULT, edges_num);
1537 &me_src->face_data, &me_dst->face_data, mask.pmask, CD_SET_DEFAULT, faces_num);
1539 &me_src->corner_data, &me_dst->corner_data, mask.lmask, CD_SET_DEFAULT, corners_num);
1540 if (do_tessface) {
1542 &me_src->fdata_legacy, &me_dst->fdata_legacy, mask.fmask, CD_SET_DEFAULT, tessface_num);
1543 }
1544 else {
1545 mesh_tessface_clear_intern(me_dst, false);
1546 }
1547
1548 /* The destination mesh should at least have valid primary CD layers,
1549 * even in cases where the source mesh does not. */
1552 if (do_tessface && !CustomData_get_layer(&me_dst->fdata_legacy, CD_MFACE)) {
1554 }
1555
1556 bke::MutableAttributeAccessor dst_attrs = me_dst->attributes_for_write();
1557 for (const bke::Attribute &attr : me_src->attribute_storage.wrap()) {
1558 if (dst_attrs.contains(attr.name())) {
1559 continue;
1560 }
1561 dst_attrs.add(attr.name(), attr.domain(), attr.data_type(), bke::AttributeInitDefaultValue());
1562 }
1563
1564 return me_dst;
1565}
1566
1568 const int verts_num,
1569 const int edges_num,
1570 const int faces_num,
1571 const int corners_num)
1572{
1574 me_src, verts_num, edges_num, 0, faces_num, corners_num, CD_MASK_EVERYTHING);
1575}
1576
1578{
1579 return reinterpret_cast<Mesh *>(
1580 BKE_id_copy_ex(nullptr, &source.id, nullptr, LIB_ID_COPY_LOCALIZE));
1581}
1582
1584 const BMeshCreateParams *create_params,
1585 const BMeshFromMeshParams *convert_params)
1586{
1588
1589 BMesh *bm = BM_mesh_create(&allocsize, create_params);
1590 BM_mesh_bm_from_me(bm, mesh, convert_params);
1591
1592 return bm;
1593}
1594
1596 const int active_shapekey,
1597 const bool add_key_index,
1598 const BMeshCreateParams *params)
1599{
1600 BMeshFromMeshParams bmesh_from_mesh_params{};
1601 bmesh_from_mesh_params.calc_face_normal = false;
1602 bmesh_from_mesh_params.calc_vert_normal = false;
1603 bmesh_from_mesh_params.add_key_index = add_key_index;
1604 bmesh_from_mesh_params.use_shapekey = true;
1605 bmesh_from_mesh_params.active_shapekey = active_shapekey;
1606 return BKE_mesh_to_bmesh_ex(mesh, params, &bmesh_from_mesh_params);
1607}
1608
1610 const BMeshToMeshParams *params,
1611 const Mesh *me_settings)
1612{
1613 BLI_assert(params->calc_object_remap == false);
1614 Mesh *mesh = BKE_id_new_nomain<Mesh>(nullptr);
1615 BM_mesh_bm_to_me(nullptr, bm, mesh, params);
1617 return mesh;
1618}
1619
1621 const CustomData_MeshMasks *cd_mask_extra,
1622 const Mesh *me_settings)
1623{
1624 Mesh *mesh = BKE_id_new_nomain<Mesh>(nullptr);
1625 BM_mesh_bm_to_me_for_eval(*bm, *mesh, cd_mask_extra);
1627 return mesh;
1628}
1629
1630static void ensure_orig_index_layer(CustomData &data, const int size)
1631{
1633 return;
1634 }
1635 int *indices = static_cast<int *>(
1637 array_utils::fill_index_range<int>({indices, size});
1638}
1639
1645
1647{
1648 ensure_orig_index_layer(mesh->vert_data, mesh->verts_num);
1649 ensure_orig_index_layer(mesh->edge_data, mesh->edges_num);
1650 ensure_orig_index_layer(mesh->face_data, mesh->faces_num);
1651}
1652
1654{
1655 if (mesh->texspace_flag & ME_TEXSPACE_FLAG_AUTO) {
1656 const Bounds<float3> bounds = mesh->bounds_min_max().value_or(
1657 Bounds(float3(-1.0f), float3(1.0f)));
1658
1659 float texspace_location[3], texspace_size[3];
1660 mid_v3_v3v3(texspace_location, bounds.min, bounds.max);
1661
1662 texspace_size[0] = (bounds.max[0] - bounds.min[0]) / 2.0f;
1663 texspace_size[1] = (bounds.max[1] - bounds.min[1]) / 2.0f;
1664 texspace_size[2] = (bounds.max[2] - bounds.min[2]) / 2.0f;
1665
1666 for (int a = 0; a < 3; a++) {
1667 if (texspace_size[a] == 0.0f) {
1668 texspace_size[a] = 1.0f;
1669 }
1670 else if (texspace_size[a] > 0.0f && texspace_size[a] < 0.00001f) {
1671 texspace_size[a] = 0.00001f;
1672 }
1673 else if (texspace_size[a] < 0.0f && texspace_size[a] > -0.00001f) {
1674 texspace_size[a] = -0.00001f;
1675 }
1676 }
1677
1678 copy_v3_v3(mesh->texspace_location, texspace_location);
1679 copy_v3_v3(mesh->texspace_size, texspace_size);
1680
1681 mesh->texspace_flag |= ME_TEXSPACE_FLAG_AUTO_EVALUATED;
1682 }
1683}
1684
1686{
1687 if ((mesh->texspace_flag & ME_TEXSPACE_FLAG_AUTO) &&
1688 !(mesh->texspace_flag & ME_TEXSPACE_FLAG_AUTO_EVALUATED))
1689 {
1691 }
1692}
1693
1694void BKE_mesh_texspace_get(Mesh *mesh, float r_texspace_location[3], float r_texspace_size[3])
1695{
1697
1698 if (r_texspace_location) {
1699 copy_v3_v3(r_texspace_location, mesh->texspace_location);
1700 }
1701 if (r_texspace_size) {
1702 copy_v3_v3(r_texspace_size, mesh->texspace_size);
1703 }
1704}
1705
1707 char **r_texspace_flag,
1708 float **r_texspace_location,
1709 float **r_texspace_size)
1710{
1712
1713 if (r_texspace_flag != nullptr) {
1714 *r_texspace_flag = &mesh->texspace_flag;
1715 }
1716 if (r_texspace_location != nullptr) {
1717 *r_texspace_location = mesh->texspace_location;
1718 }
1719 if (r_texspace_size != nullptr) {
1720 *r_texspace_size = mesh->texspace_size;
1721 }
1722}
1723
1725{
1726 const Mesh *mesh = id_cast<const Mesh *>(ob->data);
1727 const Mesh *tme = mesh->texcomesh ? mesh->texcomesh : mesh;
1728
1729 Array<float3> result(mesh->verts_num);
1730 const Span<float3> positions = tme->vert_positions();
1731 result.as_mutable_span().take_front(positions.size()).copy_from(positions);
1732 result.as_mutable_span().drop_front(positions.size()).fill(float3(0));
1733
1734 return result;
1735}
1736
1738{
1739 float texspace_location[3], texspace_size[3];
1740
1742 mesh->texcomesh ? mesh->texcomesh : mesh, texspace_location, texspace_size);
1743
1744 if (invert) {
1745 for (const int a : orco.index_range()) {
1746 float3 &co = orco[a];
1747 madd_v3_v3v3v3(co, texspace_location, co, texspace_size);
1748 }
1749 }
1750 else {
1751 for (const int a : orco.index_range()) {
1752 float3 &co = orco[a];
1753 co[0] = (co[0] - texspace_location[0]) / texspace_size[0];
1754 co[1] = (co[1] - texspace_location[1]) / texspace_size[1];
1755 co[2] = (co[2] - texspace_location[2]) / texspace_size[2];
1756 }
1757 }
1758}
1759
1760void BKE_mesh_orco_verts_transform(Mesh *mesh, float (*orco)[3], int totvert, bool invert)
1761{
1762 BKE_mesh_orco_verts_transform(mesh, {reinterpret_cast<float3 *>(orco), totvert}, invert);
1763}
1764
1766{
1767 if (CustomData_has_layer(&mesh->vert_data, CD_ORCO)) {
1768 return;
1769 }
1770
1771 /* Orcos are stored in normalized 0..1 range by convention. */
1773 BKE_mesh_orco_verts_transform(mesh, orcodata, false);
1774 float3 *data = static_cast<float3 *>(
1775 CustomData_add_layer(&mesh->vert_data, CD_ORCO, CD_CONSTRUCT, mesh->verts_num));
1776 MutableSpan(data, mesh->verts_num).copy_from(orcodata);
1777}
1778
1780{
1781 if (ob == nullptr) {
1782 return nullptr;
1783 }
1784 if (ob->type == OB_MESH) {
1785 return id_cast<Mesh *>(ob->data);
1786 }
1787
1788 return nullptr;
1789}
1790
1792{
1793 ID *old = nullptr;
1794
1795 if (ob == nullptr) {
1796 return;
1797 }
1798
1800
1801 if (ob->type == OB_MESH) {
1802 old = ob->data;
1803 if (old) {
1804 id_us_min(old);
1805 }
1806 ob->data = id_cast<ID *>(mesh);
1807 id_us_plus(ob->data);
1808 }
1809
1810 BKE_object_materials_sync_length(bmain, ob, id_cast<ID *>(mesh));
1811
1813}
1814
1816{
1817 using namespace blender::bke;
1818 MutableAttributeAccessor attributes = mesh->attributes_for_write();
1819 AttributeWriter<int> material_indices = attributes.lookup_for_write<int>("material_index");
1820 if (!material_indices) {
1821 return;
1822 }
1823 if (material_indices.domain != AttrDomain::Face) {
1825 return;
1826 }
1827 MutableVArraySpan<int> indices_span(material_indices.varray);
1828 for (const int i : indices_span.index_range()) {
1829 if (indices_span[i] > 0 && indices_span[i] >= index) {
1830 indices_span[i]--;
1831 }
1832 }
1833 indices_span.save();
1834 material_indices.finish();
1835
1837}
1838
1840{
1841 using namespace blender::bke;
1842 const AttributeAccessor attributes = mesh->attributes();
1843 const VArray<int> material_indices = *attributes.lookup_or_default<int>(
1844 "material_index", AttrDomain::Face, 0);
1845 if (material_indices.is_single()) {
1846 return material_indices.get_internal_single() == index;
1847 }
1848 const VArraySpan<int> indices_span(material_indices);
1849 return indices_span.contains(index);
1850}
1851
1853{
1854 using namespace blender::bke;
1855 MutableAttributeAccessor attributes = mesh->attributes_for_write();
1856 attributes.remove("material_index");
1857
1859}
1860
1861void BKE_mesh_material_remap(Mesh *mesh, const uint *remap, uint remap_len)
1862{
1863 using namespace blender::bke;
1864 const short remap_len_short = short(remap_len);
1865
1866#define MAT_NR_REMAP(n) \
1867 if (n < remap_len_short) { \
1868 BLI_assert(n >= 0 && remap[n] < remap_len_short); \
1869 n = remap[n]; \
1870 } \
1871 ((void)0)
1872
1874 BMIter iter;
1875 BMFace *efa;
1876
1877 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
1878 MAT_NR_REMAP(efa->mat_nr);
1879 }
1880 }
1881 else {
1882 MutableAttributeAccessor attributes = mesh->attributes_for_write();
1883 SpanAttributeWriter<int> material_indices = attributes.lookup_or_add_for_write_span<int>(
1884 "material_index", AttrDomain::Face);
1885 if (!material_indices) {
1886 return;
1887 }
1888 for (const int i : material_indices.span.index_range()) {
1889 MAT_NR_REMAP(material_indices.span[i]);
1890 }
1891 material_indices.span.save();
1892 material_indices.finish();
1893 }
1894
1895#undef MAT_NR_REMAP
1896}
1897
1898namespace bke {
1899
1900void mesh_smooth_set(Mesh &mesh, const bool use_smooth, const bool keep_sharp_edges)
1901{
1902 MutableAttributeAccessor attributes = mesh.attributes_for_write();
1903 if (!keep_sharp_edges) {
1904 attributes.remove("sharp_edge");
1905 }
1906 attributes.remove("sharp_face");
1907 if (!use_smooth) {
1908 attributes.add<bool>("sharp_face", AttrDomain::Face, AttributeInitValue(true));
1909 }
1910}
1911
1912void mesh_sharp_edges_set_from_angle(Mesh &mesh, const float angle, const bool keep_sharp_edges)
1913{
1914 MutableAttributeAccessor attributes = mesh.attributes_for_write();
1915 if (angle >= M_PI) {
1916 mesh_smooth_set(mesh, true, keep_sharp_edges);
1917 return;
1918 }
1919 if (angle == 0.0f) {
1920 mesh_smooth_set(mesh, false, keep_sharp_edges);
1921 return;
1922 }
1923 if (!keep_sharp_edges) {
1924 attributes.remove("sharp_edge");
1925 }
1926 SpanAttributeWriter<bool> sharp_edges = attributes.lookup_or_add_for_write_span<bool>(
1927 "sharp_edge", AttrDomain::Edge);
1928 const VArraySpan<bool> sharp_faces = *attributes.lookup<bool>("sharp_face", AttrDomain::Face);
1930 mesh.corner_verts(),
1931 mesh.corner_edges(),
1932 mesh.face_normals(),
1933 mesh.corner_to_face_map(),
1934 sharp_faces,
1935 angle,
1936 sharp_edges.span);
1937 sharp_edges.finish();
1938}
1939
1940} // namespace bke
1941
1942std::optional<Bounds<float3>> Mesh::bounds_min_max() const
1943{
1944 const int verts_num = BKE_mesh_wrapper_vert_len(this);
1945 if (verts_num == 0) {
1946 return std::nullopt;
1947 }
1948 this->runtime->bounds_cache.ensure([&](Bounds<float3> &r_bounds) {
1949 switch (this->runtime->wrapper_type) {
1952 *const_cast<BMesh *>(BKE_editmesh_bmesh_get(this)), *this->runtime->edit_data);
1953 break;
1956 r_bounds = *bounds::min_max(this->vert_positions());
1957 break;
1958 }
1959 });
1960 return this->runtime->bounds_cache.data();
1961}
1962
1963void Mesh::bounds_set_eager(const Bounds<float3> &bounds)
1964{
1965 this->runtime->bounds_cache.ensure([&](Bounds<float3> &r_data) { r_data = bounds; });
1966}
1967
1969{
1970 return mesh.runtime->wrapper_type == ME_WRAPPER_TYPE_BMESH && BKE_editmesh_bmesh_get(&mesh);
1971}
1972
1973std::optional<int> Mesh::material_index_max() const
1974{
1975 this->runtime->max_material_index.ensure([&](std::optional<int> &value) {
1976 if (use_bmesh_material_indices(*this)) {
1977 const BMesh *bm = BKE_editmesh_bmesh_get(this);
1978 if (bm->totface == 0) {
1979 value = std::nullopt;
1980 return;
1981 }
1982 int max_material_index = 0;
1983 BMFace *efa;
1984 BMIter iter;
1985 BM_ITER_MESH (efa, &iter, const_cast<BMesh *>(bm), BM_FACES_OF_MESH) {
1986 max_material_index = std::max<int>(max_material_index, efa->mat_nr);
1987 }
1988 value = max_material_index;
1989 return;
1990 }
1991 if (this->faces_num == 0) {
1992 value = std::nullopt;
1993 return;
1994 }
1995 value = bounds::max<int>(
1996 *this->attributes().lookup_or_default<int>("material_index", bke::AttrDomain::Face, 0));
1997 if (value.has_value()) {
1998 value = std::clamp(*value, 0, MAXMAT);
1999 }
2000 });
2001 return this->runtime->max_material_index.data();
2002}
2003
2004const VectorSet<int> &Mesh::material_indices_used() const
2005{
2006 this->runtime->used_material_indices.ensure([&](VectorSet<int> &r_data) {
2007 const std::optional<int> max_material_index_opt = this->material_index_max();
2008 r_data.clear();
2009 if (!max_material_index_opt.has_value()) {
2010 return;
2011 }
2012 const int max_material_index = *max_material_index_opt;
2013 const auto clamp_material_index = [&](const int index) {
2014 return std::clamp<int>(index, 0, max_material_index);
2015 };
2016
2017 /* Find used indices in parallel and then create the vector set in the end. */
2018 Array<bool> used_indices(max_material_index + 1, false);
2019 if (use_bmesh_material_indices(*this)) {
2020 const BMesh *bm = BKE_editmesh_bmesh_get(this);
2021 BMFace *efa;
2022 BMIter iter;
2023 BM_ITER_MESH (efa, &iter, const_cast<BMesh *>(bm), BM_FACES_OF_MESH) {
2024 used_indices[clamp_material_index(efa->mat_nr)] = true;
2025 }
2026 }
2027 else if (const VArray<int> material_indices = *this->attributes().lookup_or_default<int>(
2028 "material_index", bke::AttrDomain::Face, 0))
2029 {
2030 if (const std::optional<int> single_material_index = material_indices.get_if_single()) {
2031 used_indices[clamp_material_index(*single_material_index)] = true;
2032 }
2033 else {
2034 VArraySpan<int> material_indices_span = material_indices;
2035 threading::parallel_for(
2036 material_indices_span.index_range(), 8096, [&](const IndexRange range) {
2037 for (const int i : range) {
2038 used_indices[clamp_material_index(material_indices_span[i])] = true;
2039 }
2040 });
2041 }
2042 }
2043 for (const int i : used_indices.index_range()) {
2044 if (used_indices[i]) {
2045 r_data.add_new(i);
2046 }
2047 }
2048 });
2049 return this->runtime->used_material_indices.data();
2050}
2051
2052namespace bke {
2053
2054static void translate_positions(MutableSpan<float3> positions, const float3 &translation)
2055{
2056 threading::parallel_for(positions.index_range(), 2048, [&](const IndexRange range) {
2057 for (float3 &position : positions.slice(range)) {
2058 position += translation;
2059 }
2060 });
2061}
2062
2063void mesh_translate(Mesh &mesh, const float3 &translation, const bool do_shape_keys)
2064{
2065 if (math::is_zero(translation)) {
2066 return;
2067 }
2068
2069 std::optional<Bounds<float3>> bounds;
2070 if (mesh.runtime->bounds_cache.is_cached()) {
2071 bounds = mesh.runtime->bounds_cache.data();
2072 }
2073
2074 translate_positions(mesh.vert_positions_for_write(), translation);
2075
2076 if (do_shape_keys && mesh.key) {
2077 for (KeyBlock &kb : mesh.key->block) {
2078 translate_positions({static_cast<float3 *>(kb.data), kb.totelem}, translation);
2079 }
2080 }
2081
2082 mesh.tag_positions_changed_uniformly();
2083
2084 if (bounds) {
2085 bounds->min += translation;
2086 bounds->max += translation;
2087 mesh.bounds_set_eager(*bounds);
2088 }
2089}
2090
2091void mesh_transform(Mesh &mesh, const float4x4 &transform, bool do_shape_keys)
2092{
2093 math::transform_points(transform, mesh.vert_positions_for_write());
2094
2095 if (do_shape_keys && mesh.key) {
2096 for (KeyBlock &kb : mesh.key->block) {
2098 }
2099 }
2100 MutableAttributeAccessor attributes = mesh.attributes_for_write();
2102
2103 mesh.tag_positions_changed();
2104}
2105
2106} // namespace bke
2107
2112
2113/* -------------------------------------------------------------------- */
2114/* MSelect functions (currently used in weight paint mode) */
2115
2117{
2118 MEM_SAFE_DELETE(mesh->mselect);
2119 mesh->totselect = 0;
2120}
2121
2123{
2124 using namespace blender::bke;
2125 MSelect *mselect_src, *mselect_dst;
2126 int i_src, i_dst;
2127
2128 if (mesh->totselect == 0) {
2129 return;
2130 }
2131
2132 mselect_src = mesh->mselect;
2133 mselect_dst = MEM_new_array_uninitialized<MSelect>(size_t(mesh->totselect),
2134 "Mesh selection history");
2135
2136 const AttributeAccessor attributes = mesh->attributes();
2137 const VArray<bool> select_vert = *attributes.lookup_or_default<bool>(
2138 ".select_vert", AttrDomain::Point, false);
2139 const VArray<bool> select_edge = *attributes.lookup_or_default<bool>(
2140 ".select_edge", AttrDomain::Edge, false);
2141 const VArray<bool> select_poly = *attributes.lookup_or_default<bool>(
2142 ".select_poly", AttrDomain::Face, false);
2143
2144 for (i_src = 0, i_dst = 0; i_src < mesh->totselect; i_src++) {
2145 int index = mselect_src[i_src].index;
2146 switch (mselect_src[i_src].type) {
2147 case ME_VSEL: {
2148 if (select_vert[index]) {
2149 mselect_dst[i_dst] = mselect_src[i_src];
2150 i_dst++;
2151 }
2152 break;
2153 }
2154 case ME_ESEL: {
2155 if (select_edge[index]) {
2156 mselect_dst[i_dst] = mselect_src[i_src];
2157 i_dst++;
2158 }
2159 break;
2160 }
2161 case ME_FSEL: {
2162 if (select_poly[index]) {
2163 mselect_dst[i_dst] = mselect_src[i_src];
2164 i_dst++;
2165 }
2166 break;
2167 }
2168 default: {
2170 break;
2171 }
2172 }
2173 }
2174
2175 MEM_delete(mselect_src);
2176
2177 if (i_dst == 0) {
2178 MEM_delete(mselect_dst);
2179 mselect_dst = nullptr;
2180 }
2181 else if (i_dst != mesh->totselect) {
2182 mselect_dst = static_cast<MSelect *>(
2183 MEM_realloc_uninitialized(mselect_dst, sizeof(MSelect) * i_dst));
2184 }
2185
2186 mesh->totselect = i_dst;
2187 mesh->mselect = mselect_dst;
2188}
2189
2191{
2193
2194 for (int i = 0; i < mesh->totselect; i++) {
2195 if ((mesh->mselect[i].index == index) && (mesh->mselect[i].type == type)) {
2196 return i;
2197 }
2198 }
2199
2200 return -1;
2201}
2202
2204{
2206
2207 if (mesh->totselect) {
2208 if (mesh->mselect[mesh->totselect - 1].type == type) {
2209 return mesh->mselect[mesh->totselect - 1].index;
2210 }
2211 }
2212 return -1;
2213}
2214
2216{
2217 const int msel_index = BKE_mesh_mselect_find(mesh, index, type);
2218
2219 if (msel_index == -1) {
2220 /* add to the end */
2221 mesh->mselect = static_cast<MSelect *>(
2222 MEM_realloc_uninitialized(mesh->mselect, sizeof(MSelect) * (mesh->totselect + 1)));
2223 mesh->mselect[mesh->totselect].index = index;
2224 mesh->mselect[mesh->totselect].type = eMSelect_Type(type);
2225 mesh->totselect++;
2226 }
2227 else if (msel_index != mesh->totselect - 1) {
2228 /* move to the end */
2229 std::swap(mesh->mselect[msel_index], mesh->mselect[mesh->totselect - 1]);
2230 }
2231
2232 BLI_assert((mesh->mselect[mesh->totselect - 1].index == index) &&
2233 (mesh->mselect[mesh->totselect - 1].type == type));
2234}
2235
2236void BKE_mesh_count_selected_items(const Mesh *mesh, int r_count[3])
2237{
2238 r_count[0] = r_count[1] = r_count[2] = 0;
2239 if (const BMesh *bm = BKE_editmesh_bmesh_get(mesh)) {
2240 r_count[0] = bm->totvertsel;
2241 r_count[1] = bm->totedgesel;
2242 r_count[2] = bm->totfacesel;
2243 }
2244 /* We could support faces in paint modes. */
2245}
2246
2247/* **** Depsgraph evaluation **** */
2248
2249void BKE_mesh_eval_geometry(Depsgraph *depsgraph, Mesh *mesh)
2250{
2251 DEG_debug_print_eval(depsgraph, __func__, mesh->id.name, mesh);
2253 /* We are here because something did change in the mesh. This means we can not trust the existing
2254 * evaluated mesh, and we don't know what parts of the mesh did change. So we simply delete the
2255 * evaluated mesh and let objects to re-create it with updated settings. */
2256 if (mesh->runtime->mesh_eval != nullptr) {
2257 BKE_id_free(nullptr, mesh->runtime->mesh_eval);
2258 mesh->runtime->mesh_eval = nullptr;
2259 }
2260 if (DEG_is_active(depsgraph)) {
2261 Mesh *mesh_orig = DEG_get_original(mesh);
2262 if (mesh->texspace_flag & ME_TEXSPACE_FLAG_AUTO_EVALUATED) {
2264 copy_v3_v3(mesh_orig->texspace_location, mesh->texspace_location);
2265 copy_v3_v3(mesh_orig->texspace_size, mesh->texspace_size);
2266 }
2267 }
2268}
2269
2270} // namespace blender
support for deformation groups and hooks.
#define BKE_LIB_FOREACHID_PROCESS_IDSUPER(data_, id_super_, cb_flag_)
General operations, lookup, etc. for materials.
General operations, lookup, etc. for blender objects.
A BVH for high poly meshes.
#define M_PI
Platform independent time functions.
#define BLO_read_struct_list(reader, struct_name, list)
#define BLT_I18NCONTEXT_ID_MESH
#define FILTER_ID_MA
Definition DNA_ID.h:1210
#define FILTER_ID_ME
Definition DNA_ID.h:1213
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition DNA_ID.h:736
#define FILTER_ID_KE
Definition DNA_ID.h:1234
#define FILTER_ID_IM
Definition DNA_ID.h:1206
#define MAXMAT
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define STACK_FIXED_DEPTH
#define BLI_assert_unreachable()
#define INIT_DEFAULT_STRUCT_AFTER(struct_dst, member)
#define ELEM(...)
#define BM_ELEM_CD_SET_BOOL(ele, offset, f)
#define BM_ELEM_CD_GET_VOID_P(ele, offset)
#define BM_ITER_MESH(ele, iter, bm, itype)
#define BMALLOC_TEMPLATE_FROM_ME(...)
#define MEM_realloc_uninitialized(vmemh, len)
static btDbvtVolume bounds(btDbvtNode **leaves, int count)
Definition btDbvt.cpp:299
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
BoundBox bounds
AttributeSet attributes
constexpr Span()=default
int64_t size() const
Definition BLI_array.hh:267
IndexRange index_range() const
Definition BLI_array.hh:371
const T * begin() const
Definition BLI_array.hh:332
bool is_empty() const
Definition BLI_array.hh:275
void generated_ptr(int64_t offset)
Definition writefile.cc:986
void copy_construct(const void *src, void *dst) const
GSpan as_span() const
void copy_from(GSpan values)
const CPPType & type() const
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr MutableSpan slice(const int64_t start, const int64_t size) const
Definition BLI_span.hh:579
constexpr MutableSpan drop_front(const int64_t n) const
Definition BLI_span.hh:613
constexpr T * end() const
Definition BLI_span.hh:554
constexpr T * begin() const
Definition BLI_span.hh:550
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
constexpr void copy_from(Span< T > values) const
Definition BLI_span.hh:745
constexpr MutableSpan take_front(const int64_t n) const
Definition BLI_span.hh:635
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:141
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr const T * end() const
Definition BLI_span.hh:228
constexpr const T * begin() const
Definition BLI_span.hh:224
const Key * data() const
void add_new(const Key &key)
int64_t size() const
void append(const T &value)
bool is_empty() const
IndexRange index_range() const
void resize(const int64_t new_size)
void reserve(const int64_t min_capacity)
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
void foreach_attribute(const FunctionRef< void(const AttributeIter &)> fn) const
bool contains(StringRef name) const
std::optional< AttributeMetaData > lookup_meta_data(StringRef name) const
GAttributeReader lookup(const StringRef name) const
std::optional< AttrStorageType > storage_type
bool add(const StringRef name, const AttrDomain domain, const AttrType data_type, const AttributeInit &initializer)
GSpanAttributeWriter lookup_or_add_for_write_span(StringRef name, AttrDomain domain, AttrType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
GAttributeWriter lookup_for_write(StringRef name)
GSpanAttributeWriter lookup_for_write_span(StringRef name)
constexpr IndexRange index_range() const
constexpr bool contains(const T &value) const
constexpr IndexRange index_range() const
constexpr bool is_empty() const
constexpr int64_t size() const
constexpr const char * data() const
void add_shared(const ImplicitSharingInfo *sharing_info, const FunctionRef< void(MemoryCounter &shared_memory)> count_fn)
#define MAT_NR_REMAP(n)
#define offsetof(t, d)
#define attribute(slot)
#define layer
float2x2 invert(float2x2 mat)
#define UINT_MAX
Definition hash_md5.cc:46
void fill_index_range(MutableSpan< T > span, const T start=0)
bool is_uv_map(const AttributeMetaData &meta_data)
void edges_sharp_from_angle_set(OffsetIndices< int > faces, Span< int > corner_verts, Span< int > corner_edges, Span< float3 > face_normals, Span< int > corner_to_face, Span< bool > sharp_faces, const float split_angle, MutableSpan< bool > sharp_edges)
bool is_color_attribute(const AttributeMetaData &meta_data)
Bounds< float3 > calc_face_bounds(const Span< float3 > vert_positions, const Span< int > face_verts)
int partition_along_axis(const Span< float3 > face_centers, MutableSpan< int > faces, const int axis, const float middle)
Definition pbvh.cc:62
bool leaf_needs_material_split(const Span< int > faces, const Span< int > material_indices)
Definition pbvh.cc:132
int partition_material_indices(const Span< int > material_indices, MutableSpan< int > faces)
Definition pbvh.cc:73
static Bounds< float3 > merge_bounds(const Bounds< float3 > &a, const Bounds< float3 > &b)
bool attribute_name_is_anonymous(const StringRef name)
static void translate_positions(MutableSpan< float3 > positions, const float3 &translation)
void mesh_sharp_edges_set_from_angle(Mesh &mesh, float angle, bool keep_sharp_edges=false)
void mesh_skin_to_legacy(AttributeStorage::BlendWriteData &attr_write_data, CustomData &vert_data, Vector< CustomDataLayer, 16 > &vert_layers, int verts_num)
static Vector< NonContiguousGroup > compute_local_mesh_groups(Mesh &mesh)
static Bounds< float3 > negative_bounds()
std::optional< GSpan > get_span_attribute(const AttributeStorage &storage, const AttrDomain domain, const CPPType &cpp_type, const StringRef name, const int64_t domain_size)
static void build_vertex_groups_for_leaves(const int verts_num, const OffsetIndices< int > faces, const Span< int > corner_verts, Vector< NonContiguousGroup > &groups)
void mesh_remove_invalid_attribute_strings(Mesh &mesh)
static StringRefNull get_first_uv_map_name(const Mesh &mesh)
static void partition_faces_recursively(const Span< float3 > face_centers, MutableSpan< int > face_indices, Vector< NonContiguousGroup > &groups, int node_index, int depth, const std::optional< Bounds< float3 > > &bounds_precalc, const Span< int > material_indices, int target_group_size)
void mesh_smooth_set(Mesh &mesh, bool use_smooth, bool keep_sharp_edges=false)
void mesh_apply_spatial_organization(Mesh &mesh)
Mesh * mesh_new_no_attributes(int verts_num, int edges_num, int faces_num, int corners_num)
void mesh_transform(Mesh &mesh, const float4x4 &transform, bool do_shape_keys)
void mesh_ensure_default_uv_attribute_on_add(Mesh &mesh, StringRef name, AttrDomain domain, bke::AttrType data_type)
void transform_custom_normal_attribute(const float4x4 &transform, MutableAttributeAccessor &attributes)
void attribute_storage_blend_write_prepare(AttributeStorage &data, bool use_5_0_compatibility, FunctionRef< int(AttrDomain)> get_domain_size, AttributeStorage::BlendWriteData &write_data)
void mesh_ensure_required_data_layers(Mesh &mesh)
void mesh_ensure_default_color_attribute_on_add(Mesh &mesh, StringRef name, AttrDomain domain, bke::AttrType data_type)
void mesh_translate(Mesh &mesh, const float3 &translation, bool do_shape_keys)
void mesh_ensure_active_uv_map(Mesh &mesh)
void mesh_ensure_default_uv_map(Mesh &mesh)
Bounds< T > merge(const Bounds< T > &a, const Bounds< T > &b)
Definition BLI_bounds.hh:27
void copy_shared_pointer(T *src_ptr, const ImplicitSharingInfo *src_sharing_info, T **r_dst_ptr, const ImplicitSharingInfo **r_dst_sharing_info)
void free_shared_data(T **data, const ImplicitSharingInfo **sharing_info)
const ImplicitSharingInfo * info_for_mem_free(T *data)
void split(const std::string &s, const char delim, std::vector< std::string > &tokens)
Definition abc_util.cc:68
bool is_zero(const T &a)
T midpoint(const T &a, const T &b)
void min_max(const T &value, T &min, T &max)
int dominant_axis(const VecBase< T, 3 > &a)
void transform_points(const float4x4 &transform, MutableSpan< float3 > points, bool use_threading=true)
OffsetIndices< int > accumulate_counts_to_offsets(MutableSpan< int > counts_to_offsets, int start_offset=0)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
Value parallel_reduce(IndexRange range, int64_t grain_size, const Value &identity, const Function &function, const Reduction &reduction)
Definition BLI_task.hh:127
BMesh Iterators.
void CustomData_MeshMasks_update(CustomData_MeshMasks *mask_dst, const CustomData_MeshMasks *mask_src)
Definition customdata.cc:89
const char * BKE_idtype_idcode_to_name(short idcode)
Definition idtype.cc:261
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
IDTypeInfo IDType_ID_ME
void BKE_mesh_copy_parameters(Mesh *me_dst, const Mesh *me_src)
const ImplicitSharingInfo * BLO_read_shared(BlendDataReader *reader, T **data_ptr, FunctionRef< const ImplicitSharingInfo *()> read_fn)
char * STRNCPY(char(&dst)[N], const char *src)
void BKE_defbase_blend_write(BlendWriter *writer, const ListBaseT< bDeformGroup > *defbase)
Definition deform.cc:1597
void BKE_mesh_texspace_get_reference(Mesh *mesh, char **r_texspace_flag, float **r_texspace_location, float **r_texspace_size)
BMesh * BKE_editmesh_bmesh_get_for_write(Mesh *mesh)
Definition editmesh.cc:79
void BKE_mesh_clear_geometry_and_metadata(Mesh *mesh)
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
int64_t BLI_time_now_seconds_i()
Definition time.cc:36
bool DEG_is_active(const Depsgraph *depsgraph)
Definition depsgraph.cc:324
void BLO_read_pointer_array_and_validate_size(BlendDataReader *reader, T **ptr_p, SizeT *array_size)
void id_us_plus(ID *id)
Definition lib_id.cc:433
bool CustomData_has_layer(const CustomData *data, eCustomDataType type)
bool BKE_mesh_material_index_used(Mesh *mesh, short index)
Mesh * BKE_mesh_copy_for_eval(const Mesh &source)
void BKE_mesh_ensure_default_orig_index_customdata_no_check(Mesh *mesh)
void BKE_mesh_mselect_clear(Mesh *mesh)
void BKE_mesh_mselect_active_set(Mesh *mesh, int index, eMSelect_Type type)
static void mesh_tessface_clear_intern(Mesh *mesh, int free_customdata)
void BKE_mesh_face_offsets_ensure_alloc(Mesh *mesh)
void BKE_defvert_blend_read(BlendDataReader *reader, int count, MDeformVert *mdverts)
Definition deform.cc:1621
void DEG_debug_print_eval(Depsgraph *depsgraph, const char *function_name, const char *object_name, const void *object_address)
Mesh * BKE_mesh_from_bmesh_nomain(BMesh *bm, const BMeshToMeshParams *params, const Mesh *me_settings)
char * BLI_strdupn(const char *str, size_t len) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
static void clear_attribute_names(Mesh &mesh)
void BKE_mesh_tessface_clear(Mesh *mesh)
int CustomData_get_layer_index(const CustomData *data, eCustomDataType type)
ATTR_WARN_UNUSED_RESULT const BMVert * v
bool BKE_mesh_attribute_required(StringRef name)
static void ensure_orig_index_layer(CustomData &data, const int size)
MatBase< float, 4, 4 > float4x4
void BKE_mesh_count_selected_items(const Mesh *mesh, int r_count[3])
void BKE_id_blend_write(BlendWriter *writer, ID *id)
Definition lib_id.cc:2890
void BM_mesh_bm_to_me_for_eval(BMesh &bm, Mesh &mesh, const CustomData_MeshMasks *cd_mask_extra)
static void mesh_foreach_path(ID *id, BPathForeachPathData *bpath_data)
@ LIB_ID_CREATE_NO_USER_REFCOUNT
@ LIB_ID_COPY_LOCALIZE
@ LIB_ID_COPY_SHAPEKEY
@ LIB_ID_CREATE_LOCALIZE
static void mesh_clear_geometry(Mesh &mesh)
int BKE_mesh_mselect_find(const Mesh *mesh, int index, eMSelect_Type type)
const char * CustomData_get_render_layer_name(const CustomData *data, eCustomDataType type)
void BM_mesh_bm_from_me(BMesh *bm, const Mesh *mesh, const BMeshFromMeshParams *params)
Mesh -> BMesh.
void BKE_mesh_copy_parameters_for_eval(Mesh *me_dst, const Mesh *me_src)
void CustomData_set_layer_render_index(CustomData *data, eCustomDataType type, int n)
VecBase< int32_t, 2 > int2
void CustomData_init_layout_from(const CustomData *source, CustomData *dest, eCustomDataMask mask, eCDAllocType alloctype, int totelem)
VecBase< float, 2 > float2
BMesh * BKE_mesh_to_bmesh_ex(const Mesh *mesh, const BMeshCreateParams *create_params, const BMeshFromMeshParams *convert_params)
BLI_INLINE unsigned int BLI_hash_int(unsigned int k)
Definition BLI_hash_c.hh:89
bool BKE_mesh_has_custom_loop_normals(Mesh *mesh)
static void mesh_blend_read_data(BlendDataReader *reader, ID *id)
void CustomData_reset(CustomData *data)
void BKE_mesh_material_index_remove(Mesh *mesh, short index)
void CustomData_blend_write(BlendWriter *writer, CustomData *data, Span< CustomDataLayer > layers_to_write, int count, eCustomDataMask cddata_mask, ID *id)
std::optional< Bounds< float3 > > BKE_editmesh_cache_calc_minmax(BMesh &bm, const bke::EditMeshData &emd)
BMesh const char void * data
void BKE_object_materials_sync_length(Main *bmain, Object *ob, ID *id)
void BKE_mesh_mselect_validate(Mesh *mesh)
Mesh * BKE_mesh_from_object(Object *ob)
void BKE_mesh_texspace_get(Mesh *mesh, float r_texspace_location[3], float r_texspace_size[3])
int CustomData_get_offset_named(const CustomData *data, eCustomDataType type, StringRef name)
@ ME_TEXSPACE_FLAG_AUTO
@ ME_TEXSPACE_FLAG_AUTO_EVALUATED
Mesh * BKE_mesh_new_nomain(int verts_num, int edges_num, int faces_num, int corners_num)
void BKE_mesh_runtime_clear_cache(Mesh *mesh)
void BKE_mesh_texspace_ensure(Mesh *mesh)
BMesh * BKE_mesh_to_bmesh(Mesh *mesh, int active_shapekey, bool add_key_index, const BMeshCreateParams *params)
void CustomData_blend_write_prepare(CustomData &data, Vector< CustomDataLayer, 16 > &layers_to_write)
int BKE_mesh_mselect_active_get(const Mesh *mesh, eMSelect_Type type)
void BKE_libblock_init_empty(ID *id) ATTR_NONNULL(1)
Definition lib_id.cc:1550
static bool use_bmesh_material_indices(const Mesh &mesh)
const void * CustomData_get_layer(const CustomData *data, eCustomDataType type)
void multires_force_sculpt_rebuild(Object *object)
Definition multires.cc:332
int BKE_mesh_wrapper_vert_len(const Mesh *mesh)
const char * CustomData_get_active_layer_name(const CustomData *data, eCustomDataType type)
static void copy_attribute_names(const Mesh &mesh_src, Mesh &mesh_dst)
void BKE_id_free(Main *bmain, void *idv)
void BKE_mesh_ensure_skin_customdata(Mesh *mesh)
void BKE_mesh_orco_verts_transform(Mesh *mesh, MutableSpan< float3 > orco, bool invert)
bool BLO_read_array(BlendDataReader *reader, T **ptr_p, const SizeT array_size, const int elems=1)
void BKE_mesh_orco_ensure(Object *ob, Mesh *mesh)
int CustomData_get_named_layer_index(const CustomData *data, eCustomDataType type, StringRef name)
void BLO_read_array_and_validate_size(BlendDataReader *reader, T **ptr_p, SizeT *array_size, const int elems=1)
void * BKE_id_new_nomain(short type, const char *name)
Definition lib_id.cc:1617
const CustomData_MeshMasks CD_MASK_DERIVEDMESH
void BKE_mesh_eval_geometry(Depsgraph *depsgraph, Mesh *mesh)
void CustomData_count_memory(const CustomData &data, int totelem, MemoryCounter &memory)
MINLINE void madd_v3_v3v3v3(float r[3], const float a[3], const float b[3], const float c[3])
void mid_v3_v3v3(float r[3], const float a[3], const float b[3])
T * DEG_get_original(T *id)
bool BKE_bpath_foreach_path_fixed_process(BPathForeachPathData *bpath_data, char *path, size_t path_maxncpy)
Definition bpath.cc:130
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
@ INDEX_ID_ME
Definition DNA_ID.h:1327
const CustomData_MeshMasks CD_MASK_MESH
void CustomData_free(CustomData *data)
void BKE_modifiers_test_object(Object *ob)
static void mesh_free_data(ID *id)
Mesh * BKE_mesh_from_bmesh_for_eval_nomain(BMesh *bm, const CustomData_MeshMasks *cd_mask_extra, const Mesh *me_settings)
void BKE_mesh_assign_object(Main *bmain, Object *ob, Mesh *mesh)
static void mesh_init_data(ID *id)
Mesh * BKE_mesh_new_nomain_from_template(const Mesh *me_src, int verts_num, int edges_num, int faces_num, int corners_num)
void BKE_defgroup_copy_list(ListBaseT< bDeformGroup > *outbase, const ListBaseT< bDeformGroup > *inbase)
Definition deform.cc:75
@ ID_TAG_NO_MAIN
Definition DNA_ID.h:981
@ ID_TAG_COPIED_ON_EVAL
Definition DNA_ID.h:1000
void BKE_mesh_ensure_default_orig_index_customdata(Mesh *mesh)
static void mesh_foreach_id(ID *id, LibraryForeachIDData *data)
const BMesh * BKE_editmesh_bmesh_get(const Object *ob)
Definition editmesh.cc:65
static CLG_LogRef LOG
Definition logging.cpp:12
void BKE_mesh_material_index_clear(Mesh *mesh)
void BKE_mesh_material_remap(Mesh *mesh, const unsigned int *remap, unsigned int remap_len)
void BM_data_layer_add_named(BMesh *bm, CustomData *data, int type, const StringRef name)
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
void CustomData_set_layer_active_index(CustomData *data, eCustomDataType type, int n)
static void mesh_foreach_working_space_color(ID *id, const IDTypeForeachColorFunctionCallback &fn)
void BM_mesh_bm_to_me(Main *bmain, BMesh *bm, Mesh *mesh, const BMeshToMeshParams *params)
void BKE_mesh_texspace_calc(Mesh *mesh)
void * CustomData_get_layer_for_write(CustomData *data, eCustomDataType type, int totelem)
ID * BKE_id_copy_ex(Main *bmain, const ID *id, ID **new_id_p, int flag)
Definition lib_id.cc:851
@ IDWALK_CB_NEVER_SELF
VecBase< float, 3 > float3
@ ME_WRAPPER_TYPE_SUBD
@ ME_WRAPPER_TYPE_MDATA
@ ME_WRAPPER_TYPE_BMESH
Mesh * BKE_mesh_add(Main *bmain, const char *name)
const CustomData_MeshMasks CD_MASK_EVERYTHING
bool CustomData_has_layer_named(const CustomData *data, eCustomDataType type, StringRef name)
static void mesh_copy_data(Main *bmain, std::optional< Library * > owner_library, ID *id_dst, const ID *id_src, const int flag)
BMesh * BM_mesh_create(const BMAllocTemplate *allocsize, const BMeshCreateParams *params)
BMesh Make Mesh.
void id_us_min(ID *id)
Definition lib_id.cc:441
void CustomData_init_from(const CustomData *source, CustomData *dest, eCustomDataMask mask, int totelem)
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1612
void * BKE_libblock_alloc(Main *bmain, short type, const char *name, int flag) ATTR_WARN_UNUSED_RESULT
Definition lib_id.cc:1545
struct ID * BKE_id_copy_in_lib(Main *bmain, std::optional< Library * > owner_library, const ID *id, std::optional< const ID * > new_owner_id, ID **new_id_p, int flag)
Definition lib_id.cc:749
void BKE_mesh_clear_geometry(Mesh *mesh)
void BLO_read_string(BlendDataReader *reader, char **ptr_p)
Definition readfile.cc:6653
static void mesh_blend_write(BlendWriter *writer, ID *id, const void *id_address)
Array< float3 > BKE_mesh_orco_verts_get(const Object *ob)
void * CustomData_add_layer(CustomData *data, eCustomDataType type, eCDAllocType alloctype, int totelem)
void CustomData_blend_read(BlendDataReader *reader, CustomData *data, int count)
Mesh * BKE_mesh_new_nomain_from_template_ex(const Mesh *me_src, int verts_num, int edges_num, int tessface_num, int faces_num, int corners_num, CustomData_MeshMasks mask)
@ IDTYPE_FLAGS_APPEND_IS_REUSABLE
Definition BKE_idtype.hh:50
ustring name
Definition graph/node.h:178
CustomData vdata
CustomData ldata
void write_pointer_array(int64_t num, const void *data)
void write_shared(const void *data, size_t approximate_size_in_bytes, const ImplicitSharingInfo *sharing_info, FunctionRef< void()> write_fn)
void generated_pointer_tag(const void *data)
void write_string(const char *data)
void write_id_struct(const void *id_address, const T *id, BlendStructWriterTypedFn< T > fn=nullptr)
bool is_undo() const
void write_struct_array(const int64_t array_size, const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
char name[258]
Definition DNA_ID.h:433
struct AttributeStorage attribute_storage
float texspace_location[3]
eMesh_EditFlag editflag
struct Material ** mat
float texspace_size[3]
ListBaseT< bDeformGroup > vertex_group_names
struct MSelect * mselect
bke::MeshRuntime * runtime
char * stencil_uv_map_attribute
int * face_offset_indices
int vertex_group_active_index
char * default_uv_map_attribute
eMeshSymmetryType symmetry
char * active_uv_map_attribute
struct Key * key
DNA_DEPRECATED struct MDeformVert * dvert
float remesh_voxel_adaptivity
CustomData corner_data
eMesh_RemeshMode remesh_mode
char * clone_uv_map_attribute
eMesh_Flag flag
CustomData face_data
char * active_color_attribute
CustomData edge_data
CustomData fdata_legacy
char * default_color_attribute
CustomData vert_data
SharedCache< Bounds< float3 > > bounds_cache
SharedCache< std::unique_ptr< BVHTree, BVHTreeDeleter > > bvh_cache_loose_verts
std::unique_ptr< bake::BakeMaterialsList > bake_materials
SharedCache< std::unique_ptr< BVHTree, BVHTreeDeleter > > bvh_cache_loose_edges_no_hidden
SharedCache< LooseGeomCache > verts_no_face_cache
std::shared_ptr< BMEditMesh > edit_mesh
std::unique_ptr< EditMeshData > edit_data
SharedCache< bke::bvh::Tree > bvh_embree_tris_cache
SharedCache< std::unique_ptr< BVHTree, BVHTreeDeleter > > bvh_cache_corner_tris
SharedCache< NormalsCache > vert_normals_cache
SharedCache< Array< int > > vert_to_face_offset_cache
SharedCache< Array< int > > edge_to_corner_map_cache
SharedCache< bke::bvh::Tree > bvh_embree_edges_cache
SharedCache< std::unique_ptr< BVHTree, BVHTreeDeleter > > bvh_cache_edges
SharedCache< Array< int > > corner_to_face_map_cache
SharedCache< NormalsCache > corner_normals_cache
SharedCache< NormalsCache > face_normals_cache
SharedCache< Array< int > > edge_to_corner_offset_cache
SharedCache< std::unique_ptr< BVHTree, BVHTreeDeleter > > bvh_cache_loose_verts_no_hidden
SharedCache< bke::bvh::Tree > bvh_embree_verts_cache
SharedCache< std::unique_ptr< BVHTree, BVHTreeDeleter > > bvh_cache_loose_edges
SharedCache< std::unique_ptr< BVHTree, BVHTreeDeleter > > bvh_cache_faces
SharedCache< Array< int > > corner_tri_faces_cache
SharedCache< LooseGeomCache > loose_edges_cache
SharedCache< Vector< float3 > > face_normals_true_cache
eMeshWrapperType wrapper_type
SharedCache< Array< int > > vert_to_face_map_cache
SharedCache< Vector< float3 > > vert_normals_true_cache
SharedCache< std::optional< int > > max_material_index
SharedCache< Array< int > > vert_to_corner_map_cache
SubsurfRuntimeData * subsurf_runtime_data
SharedCache< LooseGeomCache > loose_verts_cache
SharedCache< std::unique_ptr< BVHTree, BVHTreeDeleter > > bvh_cache_corner_tris_no_hidden
CustomData_MeshMasks cd_mask_extra
const ImplicitSharingInfo * face_offsets_sharing_info
#define N_(msgid)