Blender V5.3
mesh_legacy_convert.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
10
11#define DNA_DEPRECATED_ALLOW
12
13#include "MEM_guardedalloc.h"
14
16#include "DNA_mesh_types.h"
17#include "DNA_meshdata_types.h"
18#include "DNA_object_types.h"
19
20#include "BLI_array_utils.hh"
21#include "BLI_listbase.hh"
22#include "BLI_map.hh"
23#include "BLI_math_geom_c.hh"
24#include "BLI_math_matrix_c.hh"
27#include "BLI_memarena.hh"
29#include "BLI_ordered_edge.hh"
30#include "BLI_polyfill_2d.hh"
31#include "BLI_resource_scope.hh"
32#include "BLI_string.hh"
33#include "BLI_string_utf8.hh"
34#include "BLI_task.hh"
35#include "BLI_utildefines.hh"
36
37#include "BKE_attribute.h"
38#include "BKE_attribute.hh"
40#include "BKE_customdata.hh"
41#include "BKE_global.hh"
42#include "BKE_idprop.hh"
43#include "BKE_lib_id.hh"
44#include "BKE_main.hh"
45#include "BKE_main_namemap.hh"
46#include "BKE_mesh.hh"
48#include "BKE_modifier.hh"
49#include "BKE_multires.hh"
50#include "BKE_node.hh"
51#include "BKE_node_runtime.hh"
53
55
56#include "BLT_translation.hh"
57
58namespace blender {
59
60/* -------------------------------------------------------------------- */
63
64struct EdgeSort {
67};
68
69/* edges have to be added with lowest index first for sorting */
70static void to_edgesort(EdgeSort *ed, uint v1, uint v2, char is_loose, short is_draw)
71{
72 if (v1 < v2) {
73 ed->v1 = v1;
74 ed->v2 = v2;
75 }
76 else {
77 ed->v1 = v2;
78 ed->v2 = v1;
79 }
80 ed->is_loose = is_loose;
81 ed->is_draw = is_draw;
82}
83
84static int vergedgesort(const void *v1, const void *v2)
85{
86 const EdgeSort *x1 = static_cast<const EdgeSort *>(v1);
87 const EdgeSort *x2 = static_cast<const EdgeSort *>(v2);
88
89 if (x1->v1 > x2->v1) {
90 return 1;
91 }
92 if (x1->v1 < x2->v1) {
93 return -1;
94 }
95 if (x1->v2 > x2->v2) {
96 return 1;
97 }
98 if (x1->v2 < x2->v2) {
99 return -1;
100 }
101
102 return 0;
103}
104
105/* Create edges based on known verts and faces,
106 * this function is only used when loading very old blend files */
107static void mesh_calc_edges_mdata(const MVert * /*allvert*/,
108 const MFace *allface,
109 MLoop *allloop,
110 const MPoly *allpoly,
111 int /*totvert*/,
112 int totface,
113 int /*totloop*/,
114 int faces_num,
115 MEdge **r_medge,
116 int *r_totedge)
117{
118 const MPoly *mpoly;
119 const MFace *mface;
120 MEdge *edges, *edge;
121 EdgeSort *edsort, *ed;
122 int a, totedge = 0;
123 uint totedge_final = 0;
124 uint edge_index;
125
126 /* we put all edges in array, sort them, and detect doubles that way */
127
128 for (a = totface, mface = allface; a > 0; a--, mface++) {
129 if (mface->v4) {
130 totedge += 4;
131 }
132 else if (mface->v3) {
133 totedge += 3;
134 }
135 else {
136 totedge += 1;
137 }
138 }
139
140 if (totedge == 0) {
141 *r_medge = nullptr;
142 *r_totedge = 0;
143 return;
144 }
145
146 ed = edsort = MEM_new_array_uninitialized<EdgeSort>(totedge, "EdgeSort");
147
148 for (a = totface, mface = allface; a > 0; a--, mface++) {
149 to_edgesort(ed++, mface->v1, mface->v2, !mface->v3, mface->edcode & ME_V1V2);
150 if (mface->v4) {
151 to_edgesort(ed++, mface->v2, mface->v3, 0, mface->edcode & ME_V2V3);
152 to_edgesort(ed++, mface->v3, mface->v4, 0, mface->edcode & ME_V3V4);
153 to_edgesort(ed++, mface->v4, mface->v1, 0, mface->edcode & ME_V4V1);
154 }
155 else if (mface->v3) {
156 to_edgesort(ed++, mface->v2, mface->v3, 0, mface->edcode & ME_V2V3);
157 to_edgesort(ed++, mface->v3, mface->v1, 0, mface->edcode & ME_V3V1);
158 }
159 }
160
161 qsort(edsort, totedge, sizeof(EdgeSort), vergedgesort);
162
163 /* count final amount */
164 for (a = totedge, ed = edsort; a > 1; a--, ed++) {
165 /* edge is unique when it differs from next edge, or is last */
166 if (ed->v1 != (ed + 1)->v1 || ed->v2 != (ed + 1)->v2) {
167 totedge_final++;
168 }
169 }
170 totedge_final++;
171
172 edges = MEM_new_array_zeroed<MEdge>(totedge_final, __func__);
173
174 for (a = totedge, edge = edges, ed = edsort; a > 1; a--, ed++) {
175 /* edge is unique when it differs from next edge, or is last */
176 if (ed->v1 != (ed + 1)->v1 || ed->v2 != (ed + 1)->v2) {
177 edge->v1 = ed->v1;
178 edge->v2 = ed->v2;
179
180 /* order is swapped so extruding this edge as a surface won't flip face normals
181 * with cyclic curves */
182 if (ed->v1 + 1 != ed->v2) {
183 std::swap(edge->v1, edge->v2);
184 }
185 edge++;
186 }
187 else {
188 /* Equal edge, merge the draw-flag. */
189 (ed + 1)->is_draw |= ed->is_draw;
190 }
191 }
192 /* last edge */
193 edge->v1 = ed->v1;
194 edge->v2 = ed->v2;
195
196 MEM_delete(edsort);
197
198 /* set edge members of mloops */
200 hash.reserve(totedge_final);
201 for (edge_index = 0, edge = edges; edge_index < totedge_final; edge_index++, edge++) {
202 hash.add({edge->v1, edge->v2}, edge_index);
203 }
204
205 mpoly = allpoly;
206 for (a = 0; a < faces_num; a++, mpoly++) {
207 MLoop *ml, *ml_next;
208 int i = mpoly->totloop;
209
210 ml_next = allloop + mpoly->loopstart; /* first loop */
211 ml = &ml_next[i - 1]; /* last loop */
212
213 while (i-- != 0) {
214 ml->e = hash.lookup({ml->v, ml_next->v});
215 ml = ml_next;
216 ml_next++;
217 }
218 }
219
220 BLI_assert(totedge_final > 0);
221 *r_medge = edges;
222 *r_totedge = totedge_final;
223}
224
226{
227 MEdge *edges;
228 int totedge = 0;
229 const Span<MVert> verts(
230 static_cast<const MVert *>(CustomData_get_layer(&mesh->vert_data, CD_MVERT)),
231 mesh->verts_num);
232
234 verts.data(),
235 mesh->mface,
236 static_cast<MLoop *>(
237 CustomData_get_layer_for_write(&mesh->corner_data, CD_MLOOP, mesh->corners_num)),
238 static_cast<const MPoly *>(CustomData_get_layer(&mesh->face_data, CD_MPOLY)),
239 verts.size(),
240 mesh->totface_legacy,
241 mesh->corners_num,
242 mesh->faces_num,
243 &edges,
244 &totedge);
245
246 if (totedge == 0) {
247 BLI_assert(edges == nullptr);
248 mesh->edges_num = 0;
249 return;
250 }
251
252 edges = static_cast<MEdge *>(const_cast<void *>(
253 CustomData_add_layer_with_data(&mesh->edge_data, CD_MEDGE, edges, totedge, nullptr)));
254 mesh->edges_num = totedge;
255
256 mesh->tag_topology_changed();
258}
259
261{
262 /* NOTE: We need to keep this for edge creation (for now?), and some old `readfile.cc` code. */
263 MFace *f;
264 int a, b;
265 MFace *mfaces = mesh->mface;
266
267 for (a = b = 0, f = mfaces; a < mesh->totface_legacy; a++, f++) {
268 if (f->v3) {
269 if (a != b) {
270 memcpy(&mfaces[b], f, sizeof(mfaces[b]));
271 CustomData_copy_data(&mesh->fdata_legacy, &mesh->fdata_legacy, a, b, 1);
272 }
273 b++;
274 }
275 }
276 if (a != b) {
277 CustomData_free_elem(&mesh->fdata_legacy, b, a - b);
278 mesh->totface_legacy = b;
279 }
280}
281
283
284/* -------------------------------------------------------------------- */
287
289{
290 if (mesh->cd_flag) [[unlikely]] {
291 return;
292 }
293
294 const Span<MVert> verts(
295 static_cast<const MVert *>(CustomData_get_layer(&mesh->vert_data, CD_MVERT)),
296 mesh->verts_num);
297 const Span<MEdge> edges(
298 static_cast<const MEdge *>(CustomData_get_layer(&mesh->edge_data, CD_MEDGE)),
299 mesh->edges_num);
300
301 for (const MVert &vert : verts) {
302 if (vert.bweight_legacy != 0) {
303 mesh->cd_flag |= ME_CDFLAG_VERT_BWEIGHT;
304 break;
305 }
306 }
307
308 for (const MEdge &edge : edges) {
309 if (edge.bweight_legacy != 0) {
310 mesh->cd_flag |= ME_CDFLAG_EDGE_BWEIGHT;
311 if (mesh->cd_flag & ME_CDFLAG_EDGE_CREASE) {
312 break;
313 }
314 }
315 if (edge.crease_legacy != 0) {
316 mesh->cd_flag |= ME_CDFLAG_EDGE_CREASE;
317 if (mesh->cd_flag & ME_CDFLAG_EDGE_BWEIGHT) {
318 break;
319 }
320 }
321 }
322}
323
325
326/* -------------------------------------------------------------------- */
329
330#define MESH_MLOOPCOL_FROM_MCOL(_mloopcol, _mcol) \
331 { \
332 MLoopCol *mloopcol__tmp = _mloopcol; \
333 const MCol *mcol__tmp = _mcol; \
334 mloopcol__tmp->r = mcol__tmp->b; \
335 mloopcol__tmp->g = mcol__tmp->g; \
336 mloopcol__tmp->b = mcol__tmp->r; \
337 mloopcol__tmp->a = mcol__tmp->a; \
338 } \
339 (void)0
340
342 CustomData *fdata_legacy,
343 const int totface,
344 CustomData *ldata,
345 MFace *mface,
346 int totloop,
347 int findex,
348 int loopstart,
349 int numTex,
350 int numCol)
351{
352 MFace *mf = mface + findex;
353
354 for (int i = 0; i < numTex; i++) {
355 const MTFace *texface = static_cast<const MTFace *>(
356 CustomData_get_n_for_write(fdata_legacy, CD_MTFACE, findex, i, totface));
357
358 float2 *uv = static_cast<float2 *>(
359 CustomData_get_n_for_write(ldata, CD_PROP_FLOAT2, loopstart, i, totloop));
360 copy_v2_v2(*uv, texface->uv[0]);
361 uv++;
362 copy_v2_v2(*uv, texface->uv[1]);
363 uv++;
364 copy_v2_v2(*uv, texface->uv[2]);
365 uv++;
366
367 if (mf->v4) {
368 copy_v2_v2(*uv, texface->uv[3]);
369 uv++;
370 }
371 }
372
373 for (int i = 0; i < numCol; i++) {
374 MLoopCol *mloopcol = static_cast<MLoopCol *>(
375 CustomData_get_n_for_write(ldata, CD_PROP_BYTE_COLOR, loopstart, i, totloop));
376 const MCol *mcol = static_cast<const MCol *>(
377 CustomData_get_n_for_write(fdata_legacy, CD_MCOL, findex, i, totface));
378
379 MESH_MLOOPCOL_FROM_MCOL(mloopcol, &mcol[0]);
380 mloopcol++;
381 MESH_MLOOPCOL_FROM_MCOL(mloopcol, &mcol[1]);
382 mloopcol++;
383 MESH_MLOOPCOL_FROM_MCOL(mloopcol, &mcol[2]);
384 mloopcol++;
385 if (mf->v4) {
386 MESH_MLOOPCOL_FROM_MCOL(mloopcol, &mcol[3]);
387 mloopcol++;
388 }
389 }
390
391 if (CustomData_has_layer(fdata_legacy, CD_TESSLOOPNORMAL)) {
392 float (*loop_normals)[3] = static_cast<float (*)[3]>(
393 CustomData_get_for_write(ldata, loopstart, CD_NORMAL, totloop));
394 const short (*tessloop_normals)[3] = static_cast<short (*)[3]>(
395 CustomData_get_for_write(fdata_legacy, findex, CD_TESSLOOPNORMAL, totface));
396 const int max = mf->v4 ? 4 : 3;
397
398 for (int i = 0; i < max; i++, loop_normals++, tessloop_normals++) {
399 normal_short_to_float_v3(*loop_normals, *tessloop_normals);
400 }
401 }
402
403 if (CustomData_has_layer(fdata_legacy, CD_MDISPS)) {
404 MDisps *ld = static_cast<MDisps *>(
405 CustomData_get_for_write(ldata, loopstart, CD_MDISPS, totloop));
406 const MDisps *fd = static_cast<const MDisps *>(
407 CustomData_get_for_write(fdata_legacy, findex, CD_MDISPS, totface));
408 const float (*disps)[3] = fd->disps;
409 int tot = mf->v4 ? 4 : 3;
410 int corners;
411
412 if (CustomData_external_test(fdata_legacy, CD_MDISPS)) {
413 if (id && fdata_legacy->external) {
414 CustomData_external_add(ldata, id, CD_MDISPS, totloop, fdata_legacy->external->filepath);
415 }
416 }
417
418 corners = multires_mdisp_corners(fd);
419
420 if (corners == 0) {
421 /* Empty #MDisp layers appear in at least one of the `sintel.blend` files.
422 * Not sure why this happens, but it seems fine to just ignore them here.
423 * If `corners == 0` for a non-empty layer though, something went wrong. */
424 BLI_assert(fd->totdisp == 0);
425 }
426 else {
427 const int side = int(sqrtf(float(fd->totdisp / corners)));
428 const int side_sq = side * side;
429
430 for (int i = 0; i < tot; i++, disps += side_sq, ld++) {
431 ld->totdisp = side_sq;
432
433 if (ld->disps) {
434 MEM_delete(ld->disps);
435 }
436
437 ld->disps = MEM_new_array_uninitialized<float[3]>(size_t(side_sq),
438 "converted loop mdisps");
439 if (fd->disps) {
440 memcpy(ld->disps, disps, size_t(side_sq) * sizeof(float[3]));
441 }
442 else {
443 memset(ld->disps, 0, size_t(side_sq) * sizeof(float[3]));
444 }
445 }
446 }
447 }
448}
449
450static void CustomData_to_bmeshpoly(CustomData *fdata_legacy, CustomData *ldata, int totloop)
451{
452 for (int i = 0; i < fdata_legacy->totlayer; i++) {
453 if (fdata_legacy->layers[i].type == CD_MTFACE) {
455 ldata, CD_PROP_FLOAT2, CD_SET_DEFAULT, totloop, fdata_legacy->layers[i].name);
456 }
457 else if (fdata_legacy->layers[i].type == CD_MCOL) {
459 ldata, CD_PROP_BYTE_COLOR, CD_SET_DEFAULT, totloop, fdata_legacy->layers[i].name);
460 }
461 else if (fdata_legacy->layers[i].type == CD_MDISPS) {
463 ldata, CD_MDISPS, CD_SET_DEFAULT, totloop, fdata_legacy->layers[i].name);
464 }
465 else if (fdata_legacy->layers[i].type == CD_TESSLOOPNORMAL) {
467 ldata, CD_NORMAL, CD_SET_DEFAULT, totloop, fdata_legacy->layers[i].name);
468 }
469 }
470}
471
473 CustomData *fdata_legacy,
474 CustomData *ldata,
475 CustomData *pdata,
476 int totedge_i,
477 int totface_i,
478 int /*totloop_i*/,
479 int /*faces_num_i*/,
480 int2 *edges,
481 MFace *mface,
482 int *r_totloop,
483 int *r_faces_num)
484{
485 MFace *mf;
486 MLoop *ml, *mloop;
487 MPoly *poly, *mpoly;
488 int numTex, numCol;
489 int i, j, totloop, faces_num, *polyindex;
490
491 /* just in case some of these layers are filled in (can happen with python created meshes) */
492 CustomData_free(ldata);
493 CustomData_free(pdata);
494
495 faces_num = totface_i;
496 mpoly = static_cast<MPoly *>(CustomData_add_layer(pdata, CD_MPOLY, CD_SET_DEFAULT, faces_num));
497 int *material_indices = static_cast<int *>(
498 CustomData_get_layer_named_for_write(pdata, CD_PROP_INT32, "material_index", faces_num));
499 if (material_indices == nullptr) {
500 material_indices = static_cast<int *>(CustomData_add_layer_named(
501 pdata, CD_PROP_INT32, CD_SET_DEFAULT, faces_num, "material_index"));
502 }
503 bool *sharp_faces = static_cast<bool *>(
504 CustomData_get_layer_named_for_write(pdata, CD_PROP_BOOL, "sharp_face", faces_num));
505 if (!sharp_faces) {
506 sharp_faces = static_cast<bool *>(
507 CustomData_add_layer_named(pdata, CD_PROP_BOOL, CD_SET_DEFAULT, faces_num, "sharp_face"));
508 }
509
510 numTex = CustomData_number_of_layers(fdata_legacy, CD_MTFACE);
511 numCol = CustomData_number_of_layers(fdata_legacy, CD_MCOL);
512
513 totloop = 0;
514 mf = mface;
515 for (i = 0; i < totface_i; i++, mf++) {
516 totloop += mf->v4 ? 4 : 3;
517 }
518
519 mloop = static_cast<MLoop *>(CustomData_add_layer(ldata, CD_MLOOP, CD_SET_DEFAULT, totloop));
520
521 CustomData_to_bmeshpoly(fdata_legacy, ldata, totloop);
522
523 if (id) {
524 /* ensure external data is transferred */
525 /* TODO(sergey): Use multiresModifier_ensure_external_read(). */
526 CustomData_external_read(fdata_legacy, "", id, CD_MASK_MDISPS, totface_i);
527 }
528
530 eh.reserve(totedge_i);
531
532 /* build edge hash */
533 for (i = 0; i < totedge_i; i++) {
534 eh.add(edges[i], i);
535 }
536
537 polyindex = static_cast<int *>(
538 const_cast<void *>(CustomData_get_layer(fdata_legacy, CD_ORIGINDEX)));
539
540 j = 0; /* current loop index */
541 ml = mloop;
542 mf = mface;
543 poly = mpoly;
544 for (i = 0; i < totface_i; i++, mf++, poly++) {
545 poly->loopstart = j;
546
547 poly->totloop = mf->v4 ? 4 : 3;
548
549 material_indices[i] = mf->mat_nr;
550 sharp_faces[i] = (mf->flag & ME_SMOOTH) == 0;
551
552#define ML(v1, v2) \
553 { \
554 ml->v = mf->v1; \
555 ml->e = eh.lookup({mf->v1, mf->v2}); \
556 ml++; \
557 j++; \
558 } \
559 (void)0
560
561 ML(v1, v2);
562 ML(v2, v3);
563 if (mf->v4) {
564 ML(v3, v4);
565 ML(v4, v1);
566 }
567 else {
568 ML(v3, v1);
569 }
570
571#undef ML
572
574 id, fdata_legacy, totface_i, ldata, mface, totloop, i, poly->loopstart, numTex, numCol);
575
576 if (polyindex) {
577 *polyindex = i;
578 polyindex++;
579 }
580 }
581
582 /* NOTE: we don't convert NGons at all, these are not even real ngons,
583 * they have their own UVs, colors etc - it's more an editing feature. */
584
585 *r_faces_num = faces_num;
586 *r_totloop = totloop;
587}
588
589static void update_active_fdata_layers(Mesh &mesh, CustomData *fdata_legacy, CustomData *ldata)
590{
591 int act;
592
593 /* Active/default UV map status is stored by name on Mesh, not in CustomData. */
594 if (CustomData_has_layer(fdata_legacy, CD_MTFACE)) {
595 act = CustomData_get_named_layer(fdata_legacy, CD_MTFACE, mesh.active_uv_map_name());
596 if (act != -1) {
597 CustomData_set_layer_active(fdata_legacy, CD_MTFACE, act);
598 }
599
600 act = CustomData_get_named_layer(fdata_legacy, CD_MTFACE, mesh.default_uv_map_name());
601 if (act != -1) {
602 CustomData_set_layer_render(fdata_legacy, CD_MTFACE, act);
603 }
604 }
605
607 if (mesh.active_color_attribute != nullptr) {
608 act = CustomData_get_named_layer(ldata, CD_PROP_BYTE_COLOR, mesh.active_color_attribute);
609 /* The active color layer may be of #CD_PROP_COLOR type. */
610 if (act != -1) {
611 CustomData_set_layer_active(fdata_legacy, CD_MCOL, act);
612 }
613 }
614
615 if (mesh.default_color_attribute != nullptr) {
616 act = CustomData_get_named_layer(ldata, CD_PROP_BYTE_COLOR, mesh.default_color_attribute);
617 /* The active color layer may be of #CD_PROP_COLOR type. */
618 if (act != -1) {
619 CustomData_set_layer_render(fdata_legacy, CD_MCOL, act);
620 }
621 }
622 }
623}
624
625#ifndef NDEBUG
633 CustomData *ldata,
634 bool fallback)
635{
636 int a_num = 0, b_num = 0;
637# define LAYER_CMP(l_a, t_a, l_b, t_b) \
638 ((a_num += CustomData_number_of_layers(l_a, t_a)) == \
639 (b_num += CustomData_number_of_layers(l_b, t_b)))
640
641 if (!LAYER_CMP(ldata, CD_PROP_FLOAT2, fdata_legacy, CD_MTFACE)) {
642 return false;
643 }
644 if (!LAYER_CMP(ldata, CD_PROP_BYTE_COLOR, fdata_legacy, CD_MCOL)) {
645 return false;
646 }
647 if (!LAYER_CMP(ldata, CD_ORIGSPACE_MLOOP, fdata_legacy, CD_ORIGSPACE)) {
648 return false;
649 }
650 if (!LAYER_CMP(ldata, CD_NORMAL, fdata_legacy, CD_TESSLOOPNORMAL)) {
651 return false;
652 }
653
654# undef LAYER_CMP
655
656 /* if no layers are on either CustomData's,
657 * then there was nothing to do... */
658 return a_num ? true : fallback;
659}
660#endif /* !NDEBUG */
661
663{
665 mesh.attributes().foreach_attribute([&](const bke::AttributeIter &iter) {
667 names.add_new(iter.name);
668 }
669 });
670 return names;
671}
672
673static void add_mface_layers(Mesh &mesh, CustomData *fdata_legacy, CustomData *ldata, int total)
674{
675 /* avoid accumulating extra layers */
676 BLI_assert(!check_matching_legacy_layer_counts(fdata_legacy, ldata, false));
677
678 for (const StringRef name : mesh.uv_map_names()) {
679 CustomData_add_layer_named(fdata_legacy, CD_MTFACE, CD_SET_DEFAULT, total, name);
680 }
681
682 for (const StringRef name : get_mloopcol_names(mesh)) {
683 CustomData_add_layer_named(fdata_legacy, CD_MCOL, CD_SET_DEFAULT, total, name);
684 }
685
686 for (int i = 0; i < ldata->totlayer; i++) {
687 if (ldata->layers[i].type == CD_ORIGSPACE_MLOOP) {
689 fdata_legacy, CD_ORIGSPACE, CD_SET_DEFAULT, total, ldata->layers[i].name);
690 }
691 else if (ldata->layers[i].type == CD_NORMAL) {
693 fdata_legacy, CD_TESSLOOPNORMAL, CD_SET_DEFAULT, total, ldata->layers[i].name);
694 }
695 }
696
697 update_active_fdata_layers(mesh, fdata_legacy, ldata);
698}
699
701{
702 if ((mesh->totface_legacy != 0) && (mesh->faces_num == 0)) [[unlikely]] {
703 /* Pass, otherwise this function clears 'mface' before
704 * versioning 'mface -> mpoly' code kicks in #30583.
705 *
706 * Callers could also check but safer to do here - campbell */
707 }
708 else {
709 const int tottex_original = mesh->uv_map_names().size();
710 const int totcol_original = get_mloopcol_names(*mesh).size();
711
712 const int tottex_tessface = CustomData_number_of_layers(&mesh->fdata_legacy, CD_MTFACE);
713 const int totcol_tessface = CustomData_number_of_layers(&mesh->fdata_legacy, CD_MCOL);
714
715 if (tottex_tessface != tottex_original || totcol_tessface != totcol_original) {
717
718 add_mface_layers(*mesh, &mesh->fdata_legacy, &mesh->corner_data, mesh->totface_legacy);
719
720 /* TODO: add some `--debug-mesh` option. */
721 if (G.debug & G_DEBUG) {
722 /* NOTE(campbell): this warning may be un-called for if we are initializing the mesh for
723 * the first time from #BMesh, rather than giving a warning about this we could be smarter
724 * and check if there was any data to begin with, for now just print the warning with
725 * some info to help troubleshoot what's going on. */
726 printf(
727 "%s: warning! Tessellation uvs or vcol data got out of sync, "
728 "had to reset!\n CD_MTFACE: %d != CD_PROP_FLOAT2: %d || CD_MCOL: %d != "
729 "CD_PROP_BYTE_COLOR: "
730 "%d\n",
731 __func__,
732 tottex_tessface,
733 tottex_original,
734 totcol_tessface,
735 totcol_original);
736 }
737 }
738 }
739}
740
742{
744 &mesh->fdata_legacy,
745 &mesh->corner_data,
746 &mesh->face_data,
747 mesh->edges_num,
748 mesh->totface_legacy,
749 mesh->corners_num,
750 mesh->faces_num,
751 mesh->edges_for_write().data(),
752 static_cast<MFace *>(const_cast<void *>(
753 CustomData_get_layer(&mesh->fdata_legacy, CD_MFACE))),
754 &mesh->corners_num,
755 &mesh->faces_num);
758 mesh->attribute_storage.wrap().remove(".corner_vert");
759 mesh->attribute_storage.wrap().remove(".corner_edge");
760 /* The face and corner domains may have changed size during the conversion above,
761 * ensure attribute storage is in sync, see: #159680. */
762 mesh->attribute_storage.wrap().resize(bke::AttrDomain::Face, mesh->faces_num);
763 mesh->attribute_storage.wrap().resize(bke::AttrDomain::Corner, mesh->corners_num);
765
767}
768
776 CustomData *corner_data)
777{
778 int act;
779
780 if (CustomData_has_layer(fdata_legacy, CD_MTFACE)) {
781 act = CustomData_get_active_layer(fdata_legacy, CD_MTFACE);
783
784 act = CustomData_get_render_layer(fdata_legacy, CD_MTFACE);
786 }
787
788 if (CustomData_has_layer(fdata_legacy, CD_MCOL)) {
789 act = CustomData_get_active_layer(fdata_legacy, CD_MCOL);
791
792 act = CustomData_get_render_layer(fdata_legacy, CD_MCOL);
794 }
795}
796
798{
800 &mesh->fdata_legacy,
801 &mesh->corner_data,
802 &mesh->face_data,
803 mesh->edges_num,
804 mesh->totface_legacy,
805 mesh->corners_num,
806 mesh->faces_num,
807 mesh->edges_for_write().data(),
808 static_cast<MFace *>(const_cast<void *>(
809 CustomData_get_layer(&mesh->fdata_legacy, CD_MFACE))),
810 &mesh->corners_num,
811 &mesh->faces_num);
815
816 CustomData_bmesh_do_versions_update_active_layers(&mesh->fdata_legacy, &mesh->corner_data);
817
819}
820
822
823/* -------------------------------------------------------------------- */
828
829#define MESH_MLOOPCOL_TO_MCOL(_mloopcol, _mcol) \
830 { \
831 const MLoopCol *mloopcol__tmp = _mloopcol; \
832 MCol *mcol__tmp = _mcol; \
833 mcol__tmp->b = mloopcol__tmp->r; \
834 mcol__tmp->g = mloopcol__tmp->g; \
835 mcol__tmp->r = mloopcol__tmp->b; \
836 mcol__tmp->a = mloopcol__tmp->a; \
837 } \
838 (void)0
839
850 CustomData *fdata_legacy,
851 CustomData *corner_data,
852 MFace *mface,
853 uint (*loopindices)[4],
854 const int num_faces)
855{
856 const VectorSet<StringRefNull> uv_names = mesh.uv_map_names();
857 const VectorSet<StringRefNull> mloopcol_names = get_mloopcol_names(mesh);
858 /* NOTE(mont29): performances are sub-optimal when we get a null #MFace,
859 * we could be ~25% quicker with dedicated code.
860 * The issue is, unless having two different functions with nearly the same code,
861 * there's not much ways to solve this. Better IMHO to live with it for now (sigh). */
862 const int numUV = uv_names.size();
863 const int numCol = mloopcol_names.size();
864 const bool hasOrigSpace = CustomData_has_layer(corner_data, CD_ORIGSPACE_MLOOP);
865 const bool hasLoopNormal = CustomData_has_layer(corner_data, CD_NORMAL);
866 int findex, i, j;
867 uint(*lidx)[4];
868
869 const bke::AttributeAccessor attributes = mesh.attributes();
870
871 for (i = 0; i < numUV; i++) {
872 MTFace *texface = static_cast<MTFace *>(
873 CustomData_get_layer_n_for_write(fdata_legacy, CD_MTFACE, i, num_faces));
874 const VArraySpan uv = *attributes.lookup<float2>(uv_names[i], bke::AttrDomain::Corner);
875
876 for (findex = 0, lidx = loopindices; findex < num_faces; lidx++, findex++, texface++) {
877 for (j = (mface ? mface[findex].v4 : (*lidx)[3]) ? 4 : 3; j--;) {
878 copy_v2_v2(texface->uv[j], uv[(*lidx)[j]]);
879 }
880 }
881 }
882
883 for (i = 0; i < numCol; i++) {
884 MCol(*mcol)[4] = static_cast<MCol(*)[4]>(
885 CustomData_get_layer_n_for_write(fdata_legacy, CD_MCOL, i, num_faces));
887 mloopcol_names[i], bke::AttrDomain::Corner);
888 const Span mloopcol = attr.cast<MLoopCol>();
889
890 for (findex = 0, lidx = loopindices; findex < num_faces; lidx++, findex++, mcol++) {
891 for (j = (mface ? mface[findex].v4 : (*lidx)[3]) ? 4 : 3; j--;) {
892 MESH_MLOOPCOL_TO_MCOL(&mloopcol[(*lidx)[j]], &(*mcol)[j]);
893 }
894 }
895 }
896
897 if (hasOrigSpace) {
898 OrigSpaceFace *of = static_cast<OrigSpaceFace *>(
899 const_cast<void *>(CustomData_get_layer(fdata_legacy, CD_ORIGSPACE)));
900 const OrigSpaceLoop *lof = static_cast<const OrigSpaceLoop *>(
902
903 for (findex = 0, lidx = loopindices; findex < num_faces; lidx++, findex++, of++) {
904 for (j = (mface ? mface[findex].v4 : (*lidx)[3]) ? 4 : 3; j--;) {
905 copy_v2_v2(of->uv[j], lof[(*lidx)[j]].uv);
906 }
907 }
908 }
909
910 if (hasLoopNormal) {
911 short (*face_normals)[4][3] = static_cast<short (*)[4][3]>(
912 const_cast<void *>(CustomData_get_layer(fdata_legacy, CD_TESSLOOPNORMAL)));
913 const float (*loop_normals)[3] = static_cast<const float (*)[3]>(
914 CustomData_get_layer(corner_data, CD_NORMAL));
915
916 for (findex = 0, lidx = loopindices; findex < num_faces; lidx++, findex++, face_normals++) {
917 for (j = (mface ? mface[findex].v4 : (*lidx)[3]) ? 4 : 3; j--;) {
918 normal_float_to_short_v3((*face_normals)[j], loop_normals[(*lidx)[j]]);
919 }
920 }
921 }
922}
923
924int BKE_mesh_mface_index_validate(MFace *mface, CustomData *fdata_legacy, int mfindex, int nr)
925{
926 /* first test if the face is legal */
927 if ((mface->v3 || nr == 4) && mface->v3 == mface->v4) {
928 mface->v4 = 0;
929 nr--;
930 }
931 if ((mface->v2 || mface->v4) && mface->v2 == mface->v3) {
932 mface->v3 = mface->v4;
933 mface->v4 = 0;
934 nr--;
935 }
936 if (mface->v1 == mface->v2) {
937 mface->v2 = mface->v3;
938 mface->v3 = mface->v4;
939 mface->v4 = 0;
940 nr--;
941 }
942
943 /* Check corrupt cases, bow-tie geometry,
944 * can't handle these because edge data won't exist so just return 0. */
945 if (nr == 3) {
946 if (
947 /* real edges */
948 mface->v1 == mface->v2 || mface->v2 == mface->v3 || mface->v3 == mface->v1)
949 {
950 return 0;
951 }
952 }
953 else if (nr == 4) {
954 if (
955 /* real edges */
956 mface->v1 == mface->v2 || mface->v2 == mface->v3 || mface->v3 == mface->v4 ||
957 mface->v4 == mface->v1 ||
958 /* across the face */
959 mface->v1 == mface->v3 || mface->v2 == mface->v4)
960 {
961 return 0;
962 }
963 }
964
965 /* prevent a zero at wrong index location */
966 if (nr == 3) {
967 if (mface->v3 == 0) {
968 static int corner_indices[4] = {1, 2, 0, 3};
969
970 std::swap(mface->v1, mface->v2);
971 std::swap(mface->v2, mface->v3);
972
973 if (fdata_legacy) {
974 CustomData_swap_corners(fdata_legacy, mfindex, corner_indices);
975 }
976 }
977 }
978 else if (nr == 4) {
979 if (mface->v3 == 0 || mface->v4 == 0) {
980 static int corner_indices[4] = {2, 3, 0, 1};
981
982 std::swap(mface->v1, mface->v3);
983 std::swap(mface->v2, mface->v4);
984
985 if (fdata_legacy) {
986 CustomData_swap_corners(fdata_legacy, mfindex, corner_indices);
987 }
988 }
989 }
990
991 return nr;
992}
993
995{
996 CustomData *fdata_legacy = &mesh.fdata_legacy;
997 const Span<float3> positions = mesh.vert_positions();
998 const int totloop = mesh.corners_num;
999 const int faces_num = mesh.faces_num;
1000
1001#define USE_TESSFACE_SPEEDUP
1002#define USE_TESSFACE_QUADS
1003
1004/* We abuse #MFace.edcode to tag quad faces. See below for details. */
1005#define TESSFACE_IS_QUAD 1
1006
1007 const int corner_tris_num = poly_to_tri_count(faces_num, totloop);
1008
1009 MFace *mface, *mf;
1010 MemArena *arena = nullptr;
1011 int *mface_to_poly_map;
1012 uint(*lindices)[4];
1013 int poly_index, mface_index;
1014 uint j;
1015
1016 const OffsetIndices faces = mesh.faces();
1017 const Span<int> corner_verts = mesh.corner_verts();
1018 const bke::AttributeAccessor attributes = mesh.attributes();
1019 const VArray material_indices = *attributes.lookup_or_default<int>(
1020 "material_index", bke::AttrDomain::Face, 0);
1021 const VArray sharp_faces = *attributes.lookup_or_default<bool>(
1022 "sharp_face", bke::AttrDomain::Face, false);
1023
1024 /* Allocate the length of `totfaces`, avoid many small reallocations,
1025 * if all faces are triangles it will be correct, `quads == 2x` allocations. */
1026 /* Take care since memory is _not_ zeroed so be sure to initialize each field. */
1027 mface_to_poly_map = MEM_new_array_uninitialized<int>(size_t(corner_tris_num), __func__);
1028 mface = MEM_new_array_uninitialized<MFace>(size_t(corner_tris_num), __func__);
1029 lindices = MEM_new_array_uninitialized<uint[4]>(size_t(corner_tris_num), __func__);
1030
1031 mface_index = 0;
1032 for (poly_index = 0; poly_index < faces_num; poly_index++) {
1033 const uint mp_loopstart = uint(faces[poly_index].start());
1034 const uint mp_totloop = uint(faces[poly_index].size());
1035 uint l1, l2, l3, l4;
1036 uint *lidx;
1037 if (mp_totloop < 3) {
1038 /* Do nothing. */
1039 }
1040
1041#ifdef USE_TESSFACE_SPEEDUP
1042
1043# define ML_TO_MF(i1, i2, i3) \
1044 mface_to_poly_map[mface_index] = poly_index; \
1045 mf = &mface[mface_index]; \
1046 lidx = lindices[mface_index]; \
1047 /* Set loop indices, transformed to vert indices later. */ \
1048 l1 = mp_loopstart + i1; \
1049 l2 = mp_loopstart + i2; \
1050 l3 = mp_loopstart + i3; \
1051 mf->v1 = corner_verts[l1]; \
1052 mf->v2 = corner_verts[l2]; \
1053 mf->v3 = corner_verts[l3]; \
1054 mf->v4 = 0; \
1055 lidx[0] = l1; \
1056 lidx[1] = l2; \
1057 lidx[2] = l3; \
1058 lidx[3] = 0; \
1059 mf->mat_nr = material_indices[poly_index]; \
1060 mf->flag = sharp_faces[poly_index] ? 0 : ME_SMOOTH; \
1061 mf->edcode = eMFace_EdgeCode{}; \
1062 (void)0
1063
1064/* ALMOST IDENTICAL TO DEFINE ABOVE (see EXCEPTION) */
1065# define ML_TO_MF_QUAD() \
1066 mface_to_poly_map[mface_index] = poly_index; \
1067 mf = &mface[mface_index]; \
1068 lidx = lindices[mface_index]; \
1069 /* Set loop indices, transformed to vert indices later. */ \
1070 l1 = mp_loopstart + 0; /* EXCEPTION */ \
1071 l2 = mp_loopstart + 1; /* EXCEPTION */ \
1072 l3 = mp_loopstart + 2; /* EXCEPTION */ \
1073 l4 = mp_loopstart + 3; /* EXCEPTION */ \
1074 mf->v1 = corner_verts[l1]; \
1075 mf->v2 = corner_verts[l2]; \
1076 mf->v3 = corner_verts[l3]; \
1077 mf->v4 = corner_verts[l4]; \
1078 lidx[0] = l1; \
1079 lidx[1] = l2; \
1080 lidx[2] = l3; \
1081 lidx[3] = l4; \
1082 mf->mat_nr = material_indices[poly_index]; \
1083 mf->flag = sharp_faces[poly_index] ? 0 : ME_SMOOTH; \
1084 mf->edcode = eMFace_EdgeCode(TESSFACE_IS_QUAD); \
1085 (void)0
1086
1087 else if (mp_totloop == 3) {
1088 ML_TO_MF(0, 1, 2);
1089 mface_index++;
1090 }
1091 else if (mp_totloop == 4) {
1092# ifdef USE_TESSFACE_QUADS
1093 ML_TO_MF_QUAD();
1094 mface_index++;
1095# else
1096 ML_TO_MF(0, 1, 2);
1097 mface_index++;
1098 ML_TO_MF(0, 2, 3);
1099 mface_index++;
1100# endif
1101 }
1102#endif /* USE_TESSFACE_SPEEDUP */
1103 else {
1104 const float *co_curr, *co_prev;
1105
1106 float normal[3];
1107
1108 float axis_mat[3][3];
1109 float (*projverts)[2];
1110 uint(*tris)[3];
1111
1112 const uint totfilltri = mp_totloop - 2;
1113
1114 if (arena == nullptr) [[unlikely]] {
1115 arena = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, __func__);
1116 }
1117
1118 tris = static_cast<uint(*)[3]>(
1119 BLI_memarena_alloc(arena, sizeof(*tris) * size_t(totfilltri)));
1120 projverts = static_cast<float (*)[2]>(
1121 BLI_memarena_alloc(arena, sizeof(*projverts) * size_t(mp_totloop)));
1122
1123 zero_v3(normal);
1124
1125 /* Calculate the normal, flipped: to get a positive 2D cross product. */
1126 co_prev = positions[corner_verts[mp_loopstart + mp_totloop - 1]];
1127 for (j = 0; j < mp_totloop; j++) {
1128 const int vert = corner_verts[mp_loopstart + j];
1129 co_curr = positions[vert];
1130 add_newell_cross_v3_v3v3(normal, co_prev, co_curr);
1131 co_prev = co_curr;
1132 }
1133 if (normalize_v3(normal) == 0.0f) [[unlikely]] {
1134 normal[2] = 1.0f;
1135 }
1136
1137 /* Project verts to 2D. */
1138 axis_dominant_v3_to_m3_negate(axis_mat, normal);
1139
1140 for (j = 0; j < mp_totloop; j++) {
1141 const int vert = corner_verts[mp_loopstart + j];
1142 mul_v2_m3v3(projverts[j], axis_mat, positions[vert]);
1143 }
1144
1145 BLI_polyfill_calc_arena(projverts, mp_totloop, 1, tris, arena);
1146
1147 /* Apply fill. */
1148 for (j = 0; j < totfilltri; j++) {
1149 uint *tri = tris[j];
1150 lidx = lindices[mface_index];
1151
1152 mface_to_poly_map[mface_index] = poly_index;
1153 mf = &mface[mface_index];
1154
1155 /* Set loop indices, transformed to vert indices later. */
1156 l1 = mp_loopstart + tri[0];
1157 l2 = mp_loopstart + tri[1];
1158 l3 = mp_loopstart + tri[2];
1159
1160 mf->v1 = corner_verts[l1];
1161 mf->v2 = corner_verts[l2];
1162 mf->v3 = corner_verts[l3];
1163 mf->v4 = 0;
1164
1165 lidx[0] = l1;
1166 lidx[1] = l2;
1167 lidx[2] = l3;
1168 lidx[3] = 0;
1169
1170 mf->mat_nr = material_indices ? material_indices[poly_index] : 0;
1171 mf->edcode = eMFace_EdgeCode{};
1172
1173 mface_index++;
1174 }
1175
1176 BLI_memarena_clear(arena);
1177 }
1178 }
1179
1180 if (arena) {
1181 BLI_memarena_free(arena);
1182 arena = nullptr;
1183 }
1184
1185 CustomData_free(fdata_legacy);
1186 const int totface = mface_index;
1187
1188 BLI_assert(totface <= corner_tris_num);
1189
1190 /* Not essential but without this we store over-allocated memory in the #CustomData layers. */
1191 if (corner_tris_num != totface) [[likely]] {
1192 mface = static_cast<MFace *>(
1193 MEM_realloc_uninitialized(mface, sizeof(*mface) * size_t(totface)));
1194 mface_to_poly_map = static_cast<int *>(MEM_realloc_uninitialized(
1195 mface_to_poly_map, sizeof(*mface_to_poly_map) * size_t(totface)));
1196 }
1197
1198 CustomData_add_layer_with_data(fdata_legacy, CD_MFACE, mface, totface, nullptr);
1199
1200 /* #CD_ORIGINDEX will contain an array of indices from tessellation-faces to the polygons
1201 * they are directly tessellated from. */
1202 CustomData_add_layer_with_data(fdata_legacy, CD_ORIGINDEX, mface_to_poly_map, totface, nullptr);
1203 add_mface_layers(mesh, fdata_legacy, &mesh.corner_data, totface);
1204
1205 /* NOTE: quad detection issue - fourth vertex-index vs fourth loop-index:
1206 * Polygons take care of their loops ordering, hence not of their vertices ordering.
1207 * Currently, the #TFace fourth vertex index might be 0 even for a quad.
1208 * However, we know our fourth loop index is never 0 for quads
1209 * (because they are sorted for polygons, and our quads are still mere copies of their polygons).
1210 * So we pass nullptr as #MFace pointer, and #mesh_loops_to_tessdata
1211 * will use the fourth loop index as quad test. */
1212 mesh_loops_to_tessdata(mesh, fdata_legacy, &mesh.corner_data, nullptr, lindices, totface);
1213
1214 /* NOTE: quad detection issue - fourth vert-index vs fourth loop-index:
1215 * ...However, most #TFace code uses `MFace->v4 == 0` test to check whether it is a tri or quad.
1216 * BKE_mesh_mface_index_validate() will check this and rotate the tessellated face if needed.
1217 */
1218#ifdef USE_TESSFACE_QUADS
1219 mf = mface;
1220 for (mface_index = 0; mface_index < totface; mface_index++, mf++) {
1221 if (mf->edcode == TESSFACE_IS_QUAD) {
1222 BKE_mesh_mface_index_validate(mf, fdata_legacy, mface_index, 4);
1223 mf->edcode = eMFace_EdgeCode{};
1224 }
1225 }
1226#endif
1227
1228 MEM_delete(lindices);
1229
1230 mesh.totface_legacy = totface;
1231
1232#undef USE_TESSFACE_SPEEDUP
1233#undef USE_TESSFACE_QUADS
1234
1235#undef ML_TO_MF
1236#undef ML_TO_MF_QUAD
1237}
1238
1245
1247{
1248 if (mesh->faces_num && mesh->totface_legacy == 0) {
1250 }
1251}
1252
1254
1255/* -------------------------------------------------------------------- */
1258
1260{
1261 using namespace bke;
1262 MutableAttributeAccessor attributes = mesh->attributes_for_write();
1263 if (attributes.contains("sharp_face") || !CustomData_get_layer(&mesh->face_data, CD_MPOLY)) {
1264 return;
1265 }
1266 const Span<MPoly> polys(
1267 static_cast<const MPoly *>(CustomData_get_layer(&mesh->face_data, CD_MPOLY)),
1268 mesh->faces_num);
1269 if (std::any_of(polys.begin(),
1270 polys.end(),
1271 [](const MPoly &poly) { return !(poly.flag_legacy & ME_SMOOTH); }))
1272 {
1273 SpanAttributeWriter<bool> sharp_faces = attributes.lookup_or_add_for_write_only_span<bool>(
1274 "sharp_face", AttrDomain::Face);
1275 threading::parallel_for(polys.index_range(), 4096, [&](const IndexRange range) {
1276 for (const int i : range) {
1277 sharp_faces.span[i] = !(polys[i].flag_legacy & ME_SMOOTH);
1278 }
1279 });
1280 sharp_faces.finish();
1281 }
1282 else {
1283 attributes.remove("sharp_face");
1284 }
1285}
1286
1288
1289/* -------------------------------------------------------------------- */
1292
1294{
1295 if (mesh->attributes().contains(".sculpt_face_set")) {
1296 return;
1297 }
1298 void *faceset_data = nullptr;
1299 const ImplicitSharingInfo *faceset_sharing_info = nullptr;
1300 for (const int i : IndexRange(mesh->face_data.totlayer)) {
1301 CustomDataLayer &layer = mesh->face_data.layers[i];
1302 if (layer.type == CD_SCULPT_FACE_SETS) {
1303 faceset_data = layer.data;
1304 faceset_sharing_info = layer.sharing_info;
1305 layer.data = nullptr;
1306 layer.sharing_info = nullptr;
1307 CustomData_free_layer(&mesh->face_data, CD_SCULPT_FACE_SETS, i);
1308 break;
1309 }
1310 }
1311 if (faceset_data != nullptr) {
1314 faceset_data,
1315 mesh->faces_num,
1316 ".sculpt_face_set",
1317 faceset_sharing_info);
1318 }
1319 if (faceset_sharing_info != nullptr) {
1320 faceset_sharing_info->remove_user_and_delete_if_last();
1321 }
1322}
1323
1325
1326/* -------------------------------------------------------------------- */
1329
1331{
1332 if (mesh->attributes().contains("face_maps")) {
1333 return;
1334 }
1335 int *data = nullptr;
1336 const ImplicitSharingInfo *sharing_info = nullptr;
1337 for (const int i : IndexRange(mesh->face_data.totlayer)) {
1338 CustomDataLayer &layer = mesh->face_data.layers[i];
1339 if (layer.type == CD_FACEMAP) {
1340 data = static_cast<int *>(layer.data);
1341 sharing_info = layer.sharing_info;
1342 layer.data = nullptr;
1343 layer.sharing_info = nullptr;
1344 CustomData_free_layer(&mesh->face_data, CD_FACEMAP, i);
1345 break;
1346 }
1347 }
1348 if (!data) {
1349 return;
1350 }
1351
1353 &mesh->face_data, CD_PROP_INT32, data, mesh->faces_num, "face_maps", sharing_info);
1354 if (sharing_info != nullptr) {
1355 sharing_info->remove_user_and_delete_if_last();
1356 }
1357
1359 for (const int i : IndexRange(mesh->faces_num)) {
1360 if (data[i] == -1) {
1361 /* -1 values "didn't have" a face map. */
1362 continue;
1363 }
1364 groups.add(data[i], i);
1365 }
1366
1367 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
1368 for (const auto item : groups.items()) {
1370 ".temp_face_map_" + std::to_string(item.key), bke::AttrDomain::Face);
1371 if (attribute) {
1372 attribute.span.fill_indices(item.value.as_span(), true);
1373 attribute.finish();
1374 }
1375 }
1376}
1377
1379{
1380 for (Mesh &mesh : bmain->meshes) {
1382 }
1383
1384 for (Object &object : bmain->objects) {
1385 if (object.type != OB_MESH) {
1386 continue;
1387 }
1388 Mesh *mesh = id_cast<Mesh *>(object.data);
1389
1390 for (const auto [i, face_map] : object.fmaps.enumerate()) {
1391 mesh->attributes_for_write().rename(".temp_face_map_" + std::to_string(i), face_map.name);
1392 }
1393 object.fmaps.free_no_destruct();
1394 }
1395}
1396
1398
1399/* -------------------------------------------------------------------- */
1402
1404{
1405 if (mesh->mvert && !CustomData_has_layer(&mesh->vert_data, CD_BWEIGHT)) {
1406 const Span<MVert> verts(mesh->mvert, mesh->verts_num);
1407 if (mesh->cd_flag & ME_CDFLAG_VERT_BWEIGHT) {
1408 float *weights = static_cast<float *>(
1409 CustomData_add_layer(&mesh->vert_data, CD_BWEIGHT, CD_CONSTRUCT, verts.size()));
1410 for (const int i : verts.index_range()) {
1411 weights[i] = verts[i].bweight_legacy / 255.0f;
1412 }
1413 }
1414 }
1415
1416 if (mesh->medge && !CustomData_has_layer(&mesh->edge_data, CD_BWEIGHT)) {
1417 const Span<MEdge> edges(mesh->medge, mesh->edges_num);
1418 if (mesh->cd_flag & ME_CDFLAG_EDGE_BWEIGHT) {
1419 float *weights = static_cast<float *>(
1420 CustomData_add_layer(&mesh->edge_data, CD_BWEIGHT, CD_CONSTRUCT, edges.size()));
1421 for (const int i : edges.index_range()) {
1422 weights[i] = edges[i].bweight_legacy / 255.0f;
1423 }
1424 }
1425 }
1426}
1427
1429 const eCustomDataType old_type,
1430 const eCustomDataType new_type,
1431 const int elems_num,
1432 const char *new_name)
1433{
1434 void *data = nullptr;
1435 const ImplicitSharingInfo *sharing_info = nullptr;
1436 for (const int i : IndexRange(custom_data.totlayer)) {
1437 CustomDataLayer &layer = custom_data.layers[i];
1438 if (layer.type == old_type) {
1439 data = layer.data;
1440 sharing_info = layer.sharing_info;
1441 layer.data = nullptr;
1442 layer.sharing_info = nullptr;
1443 CustomData_free_layer(&custom_data, old_type, i);
1444 break;
1445 }
1446 }
1447 if (data != nullptr) {
1449 &custom_data, new_type, data, elems_num, new_name, sharing_info);
1450 }
1451 if (sharing_info != nullptr) {
1452 sharing_info->remove_user_and_delete_if_last();
1453 }
1454}
1455
1457{
1458 if (!mesh->attributes().contains("bevel_weight_vert")) {
1460 mesh->vert_data, CD_BWEIGHT, CD_PROP_FLOAT, mesh->verts_num, "bevel_weight_vert");
1461 }
1462 if (!mesh->attributes().contains("bevel_weight_edge")) {
1464 mesh->edge_data, CD_BWEIGHT, CD_PROP_FLOAT, mesh->edges_num, "bevel_weight_edge");
1465 }
1466}
1467
1469
1470/* -------------------------------------------------------------------- */
1473
1475{
1476 if (!mesh->medge) {
1477 return;
1478 }
1479 if (CustomData_has_layer(&mesh->edge_data, CD_CREASE)) {
1480 return;
1481 }
1482 const Span<MEdge> edges(mesh->medge, mesh->edges_num);
1483 if (mesh->cd_flag & ME_CDFLAG_EDGE_CREASE) {
1484 float *creases = static_cast<float *>(
1485 CustomData_add_layer(&mesh->edge_data, CD_CREASE, CD_CONSTRUCT, edges.size()));
1486 for (const int i : edges.index_range()) {
1487 creases[i] = edges[i].crease_legacy / 255.0f;
1488 }
1489 }
1490}
1491
1493{
1494 if (!mesh->attributes().contains("crease_vert")) {
1496 mesh->vert_data, CD_CREASE, CD_PROP_FLOAT, mesh->verts_num, "crease_vert");
1497 }
1498 if (!mesh->attributes().contains("crease_edge")) {
1500 mesh->edge_data, CD_CREASE, CD_PROP_FLOAT, mesh->edges_num, "crease_edge");
1501 }
1502}
1503
1505
1506/* -------------------------------------------------------------------- */
1509
1511{
1512 using namespace bke;
1513 if (!mesh->medge) {
1514 return;
1515 }
1516 const Span<MEdge> edges(mesh->medge, mesh->edges_num);
1517 MutableAttributeAccessor attributes = mesh->attributes_for_write();
1518 if (attributes.contains("sharp_edge")) {
1519 return;
1520 }
1521 if (std::any_of(edges.begin(),
1522 edges.end(),
1523 [](const MEdge &edge) { return edge.flag_legacy & ME_SHARP; }))
1524 {
1525 SpanAttributeWriter<bool> sharp_edges = attributes.lookup_or_add_for_write_only_span<bool>(
1526 "sharp_edge", AttrDomain::Edge);
1527 threading::parallel_for(edges.index_range(), 4096, [&](const IndexRange range) {
1528 for (const int i : range) {
1529 sharp_edges.span[i] = edges[i].flag_legacy & ME_SHARP;
1530 }
1531 });
1532 sharp_edges.finish();
1533 }
1534}
1535
1537
1538/* -------------------------------------------------------------------- */
1541
1543{
1544 using namespace bke;
1545 if (!mesh->medge) {
1546 return;
1547 }
1548 MutableSpan<MEdge> edges(mesh->medge, mesh->edges_num);
1549 MutableAttributeAccessor attributes = mesh->attributes_for_write();
1550 if (attributes.contains(".uv_seam")) {
1551 return;
1552 }
1553 if (std::any_of(edges.begin(),
1554 edges.end(),
1555 [](const MEdge &edge) { return edge.flag_legacy & ME_SEAM; }))
1556 {
1558 ".uv_seam", AttrDomain::Edge);
1559 threading::parallel_for(edges.index_range(), 4096, [&](const IndexRange range) {
1560 for (const int i : range) {
1561 uv_seams.span[i] = edges[i].flag_legacy & ME_SEAM;
1562 }
1563 });
1564 uv_seams.finish();
1565 }
1566}
1567
1569
1570/* -------------------------------------------------------------------- */
1573
1575{
1576 using namespace bke;
1577 MutableAttributeAccessor attributes = mesh->attributes_for_write();
1578 if (!mesh->mvert || attributes.contains(".hide_vert") || attributes.contains(".hide_edge") ||
1579 attributes.contains(".hide_poly"))
1580 {
1581 return;
1582 }
1583 const Span<MVert> verts(mesh->mvert, mesh->verts_num);
1584 if (std::any_of(verts.begin(),
1585 verts.end(),
1586 [](const MVert &vert) { return vert.flag_legacy & ME_HIDE; }))
1587 {
1589 ".hide_vert", AttrDomain::Point);
1590 threading::parallel_for(verts.index_range(), 4096, [&](IndexRange range) {
1591 for (const int i : range) {
1592 hide_vert.span[i] = verts[i].flag_legacy & ME_HIDE;
1593 }
1594 });
1595 hide_vert.finish();
1596 }
1597
1598 if (mesh->medge) {
1599 const Span<MEdge> edges(mesh->medge, mesh->edges_num);
1600 if (std::any_of(edges.begin(),
1601 edges.end(),
1602 [](const MEdge &edge) { return int(edge.flag_legacy) & ME_HIDE; }))
1603 {
1604 SpanAttributeWriter<bool> hide_edge = attributes.lookup_or_add_for_write_only_span<bool>(
1605 ".hide_edge", AttrDomain::Edge);
1606 threading::parallel_for(edges.index_range(), 4096, [&](IndexRange range) {
1607 for (const int i : range) {
1608 hide_edge.span[i] = int(edges[i].flag_legacy) & ME_HIDE;
1609 }
1610 });
1611 hide_edge.finish();
1612 }
1613 }
1614
1615 const Span<MPoly> polys(
1616 static_cast<const MPoly *>(CustomData_get_layer(&mesh->face_data, CD_MPOLY)),
1617 mesh->faces_num);
1618 if (std::any_of(polys.begin(),
1619 polys.end(),
1620 [](const MPoly &poly) { return int(poly.flag_legacy) & ME_HIDE; }))
1621 {
1622 SpanAttributeWriter<bool> hide_poly = attributes.lookup_or_add_for_write_only_span<bool>(
1623 ".hide_poly", AttrDomain::Face);
1624 threading::parallel_for(polys.index_range(), 4096, [&](IndexRange range) {
1625 for (const int i : range) {
1626 hide_poly.span[i] = int(polys[i].flag_legacy) & ME_HIDE;
1627 }
1628 });
1629 hide_poly.finish();
1630 }
1631}
1632
1634
1635/* -------------------------------------------------------------------- */
1638
1640{
1641 using namespace bke;
1642 MutableAttributeAccessor attributes = mesh->attributes_for_write();
1643 if (!CustomData_has_layer(&mesh->face_data, CD_MPOLY) || attributes.contains("material_index")) {
1644 return;
1645 }
1646 const Span<MPoly> polys(
1647 static_cast<const MPoly *>(CustomData_get_layer(&mesh->face_data, CD_MPOLY)),
1648 mesh->faces_num);
1649 if (std::any_of(
1650 polys.begin(), polys.end(), [](const MPoly &poly) { return poly.mat_nr_legacy != 0; }))
1651 {
1652 SpanAttributeWriter<int> material_indices = attributes.lookup_or_add_for_write_only_span<int>(
1653 "material_index", AttrDomain::Face);
1654 threading::parallel_for(polys.index_range(), 4096, [&](IndexRange range) {
1655 for (const int i : range) {
1656 material_indices.span[i] = polys[i].mat_nr_legacy;
1657 }
1658 });
1659 material_indices.finish();
1660 }
1661}
1662
1664
1665/* -------------------------------------------------------------------- */
1668
1670{
1671 using namespace bke;
1672 if (!CustomData_has_layer(&mesh->corner_data, CD_MLOOPUV)) {
1673 return;
1674 }
1675
1676 /* Store layer names since they will be removed, used to set the active status of new layers.
1677 * Use intermediate #StringRef because the names can be null. */
1678
1679 Array<std::string> uv_names(CustomData_number_of_layers(&mesh->corner_data, CD_MLOOPUV));
1680 for (const int i : uv_names.index_range()) {
1681 uv_names[i] = CustomData_get_layer_name(&mesh->corner_data, CD_MLOOPUV, i);
1682 }
1683 const int active_name_i = uv_names.as_span().first_index_try(
1684 StringRef(CustomData_get_active_layer_name(&mesh->corner_data, CD_MLOOPUV)));
1685 const int default_name_i = uv_names.as_span().first_index_try(
1686 StringRef(CustomData_get_render_layer_name(&mesh->corner_data, CD_MLOOPUV)));
1687
1688 for (const int i : uv_names.index_range()) {
1689 const MLoopUV *mloopuv = static_cast<const MLoopUV *>(
1690 CustomData_get_layer_named(&mesh->corner_data, CD_MLOOPUV, uv_names[i]));
1691 const uint32_t needed_boolean_attributes = threading::parallel_reduce(
1692 IndexRange(mesh->corners_num),
1693 4096,
1694 0,
1695 [&](const IndexRange range, uint32_t init) {
1696 for (const int i : range) {
1697 init |= mloopuv[i].flag;
1698 }
1699 return init;
1700 },
1701 [](const uint32_t a, const uint32_t b) { return a | b; });
1702
1703 float2 *coords = MEM_new_array_uninitialized<float2>(size_t(mesh->corners_num), __func__);
1704 bool *pin = nullptr;
1705 if (needed_boolean_attributes & MLOOPUV_PINNED) {
1706 pin = MEM_new_array_uninitialized<bool>(size_t(mesh->corners_num), __func__);
1707 }
1708
1709 threading::parallel_for(IndexRange(mesh->corners_num), 4096, [&](IndexRange range) {
1710 for (const int i : range) {
1711 coords[i] = mloopuv[i].uv;
1712 }
1713 if (pin) {
1714 for (const int i : range) {
1715 pin[i] = mloopuv[i].flag & MLOOPUV_PINNED;
1716 }
1717 }
1718 });
1719
1720 CustomData_free_layer_named(&mesh->corner_data, uv_names[i]);
1721
1722 /* #CustomData_add_layer_named_with_data will make the name unique, and then we
1723 * find the last added UV layer to get that name. Can't use the newer API like
1724 * #BKE_attribute_calc_unique_name because it uses new attribute storage. */
1725 CustomData_add_layer_named_with_data(
1726 &mesh->corner_data, CD_PROP_FLOAT2, coords, mesh->corners_num, uv_names[i], nullptr);
1727 const int new_layer_i = CustomData_get_layer_index_n(
1728 &mesh->corner_data,
1729 CD_PROP_FLOAT2,
1730 CustomData_number_of_layers(&mesh->corner_data, CD_PROP_FLOAT2) - 1);
1731 uv_names[i] = mesh->corner_data.layers[new_layer_i].name;
1732
1733 char buffer[MAX_CUSTOMDATA_LAYER_NAME];
1734 if (pin) {
1735 CustomData_add_layer_named_with_data(&mesh->corner_data,
1736 CD_PROP_BOOL,
1737 pin,
1738 mesh->corners_num,
1739 BKE_uv_map_pin_name_get(uv_names[i], buffer),
1740 nullptr);
1741 }
1742 }
1743
1744 if (active_name_i != -1) {
1745 CustomData_set_layer_active_index(&mesh->corner_data,
1746 CD_PROP_FLOAT2,
1747 CustomData_get_named_layer_index(&mesh->corner_data,
1748 CD_PROP_FLOAT2,
1749 uv_names[active_name_i]));
1750 }
1751 if (default_name_i != -1) {
1752 CustomData_set_layer_render_index(&mesh->corner_data,
1753 CD_PROP_FLOAT2,
1754 CustomData_get_named_layer_index(&mesh->corner_data,
1755 CD_PROP_FLOAT2,
1756 uv_names[default_name_i]));
1757 }
1758}
1759
1761
1762/* -------------------------------------------------------------------- */
1765
1767{
1768 using namespace bke;
1769 MutableAttributeAccessor attributes = mesh->attributes_for_write();
1770 if (!mesh->mvert || attributes.contains(".select_vert") || attributes.contains(".select_edge") ||
1771 attributes.contains(".select_poly"))
1772 {
1773 return;
1774 }
1775
1776 const Span<MVert> verts(mesh->mvert, mesh->verts_num);
1777 if (std::any_of(
1778 verts.begin(), verts.end(), [](const MVert &vert) { return vert.flag_legacy & SELECT; }))
1779 {
1780 SpanAttributeWriter<bool> select_vert = attributes.lookup_or_add_for_write_only_span<bool>(
1781 ".select_vert", AttrDomain::Point);
1782 threading::parallel_for(verts.index_range(), 4096, [&](IndexRange range) {
1783 for (const int i : range) {
1784 select_vert.span[i] = verts[i].flag_legacy & SELECT;
1785 }
1786 });
1787 select_vert.finish();
1788 }
1789
1790 if (mesh->medge) {
1791 const Span<MEdge> edges(mesh->medge, mesh->edges_num);
1792 if (std::any_of(edges.begin(),
1793 edges.end(),
1794 [](const MEdge &edge) { return edge.flag_legacy & SELECT; }))
1795 {
1796 SpanAttributeWriter<bool> select_edge = attributes.lookup_or_add_for_write_only_span<bool>(
1797 ".select_edge", AttrDomain::Edge);
1798 threading::parallel_for(edges.index_range(), 4096, [&](IndexRange range) {
1799 for (const int i : range) {
1800 select_edge.span[i] = edges[i].flag_legacy & SELECT;
1801 }
1802 });
1803 select_edge.finish();
1804 }
1805 }
1806
1807 const Span<MPoly> polys(
1808 static_cast<const MPoly *>(CustomData_get_layer(&mesh->face_data, CD_MPOLY)),
1809 mesh->faces_num);
1810 if (std::any_of(polys.begin(),
1811 polys.end(),
1812 [](const MPoly &poly) { return poly.flag_legacy & ME_FACE_SEL; }))
1813 {
1814 SpanAttributeWriter<bool> select_poly = attributes.lookup_or_add_for_write_only_span<bool>(
1815 ".select_poly", AttrDomain::Face);
1816 threading::parallel_for(polys.index_range(), 4096, [&](IndexRange range) {
1817 for (const int i : range) {
1818 select_poly.span[i] = polys[i].flag_legacy & ME_FACE_SEL;
1819 }
1820 });
1821 select_poly.finish();
1822 }
1823}
1824
1826
1827/* -------------------------------------------------------------------- */
1830
1832{
1833 using namespace bke;
1834 const MVert *mvert = static_cast<const MVert *>(
1835 CustomData_get_layer(&mesh->vert_data, CD_MVERT));
1836 if (!mvert || CustomData_has_layer_named(&mesh->vert_data, CD_PROP_FLOAT3, "position")) {
1837 return;
1838 }
1839
1840 const Span<MVert> verts(mvert, mesh->verts_num);
1841 MutableSpan<float3> positions(
1842 static_cast<float3 *>(CustomData_add_layer_named(
1843 &mesh->vert_data, CD_PROP_FLOAT3, CD_CONSTRUCT, mesh->verts_num, "position")),
1844 mesh->verts_num);
1845 threading::parallel_for(verts.index_range(), 2048, [&](IndexRange range) {
1846 for (const int i : range) {
1847 positions[i] = verts[i].co_legacy;
1848 }
1849 });
1850
1851 CustomData_free_layers(&mesh->vert_data, CD_MVERT);
1852 mesh->mvert = nullptr;
1853}
1854
1856
1857/* -------------------------------------------------------------------- */
1860
1862{
1863 using namespace bke;
1864 const MEdge *medge = static_cast<const MEdge *>(
1865 CustomData_get_layer(&mesh->edge_data, CD_MEDGE));
1866 if (!medge || CustomData_has_layer_named(&mesh->edge_data, CD_PROP_INT32_2D, ".edge_verts")) {
1867 return;
1868 }
1869
1870 const Span<MEdge> legacy_edges(medge, mesh->edges_num);
1871 MutableSpan<int2> edges(
1872 static_cast<int2 *>(CustomData_add_layer_named(
1873 &mesh->edge_data, CD_PROP_INT32_2D, CD_CONSTRUCT, mesh->edges_num, ".edge_verts")),
1874 mesh->edges_num);
1875 threading::parallel_for(legacy_edges.index_range(), 2048, [&](IndexRange range) {
1876 for (const int i : range) {
1877 edges[i] = int2(legacy_edges[i].v1, legacy_edges[i].v2);
1878 }
1879 });
1880
1881 CustomData_free_layers(&mesh->edge_data, CD_MEDGE);
1882 mesh->medge = nullptr;
1883}
1884
1886
1887/* -------------------------------------------------------------------- */
1890
1892{
1893 /* It's not clear whether the active/render status was stored in the dedicated flags or in the
1894 * generic CustomData layer indices, so convert from both, preferring the explicit flags. */
1895
1896 auto active_from_flags = [&](const CustomData &data) {
1897 if (!mesh->active_color_attribute) {
1898 for (const int i : IndexRange(data.totlayer)) {
1899 if (data.layers[i].flag & CD_FLAG_COLOR_ACTIVE) {
1900 mesh->active_color_attribute = BLI_strdup(data.layers[i].name);
1901 }
1902 }
1903 }
1904 };
1905 auto active_from_indices = [&](const CustomData &data) {
1906 if (!mesh->active_color_attribute) {
1908 if (i != -1) {
1909 mesh->active_color_attribute = BLI_strdup(data.layers[i].name);
1910 }
1911 }
1912 if (!mesh->active_color_attribute) {
1914 if (i != -1) {
1915 mesh->active_color_attribute = BLI_strdup(data.layers[i].name);
1916 }
1917 }
1918 };
1919 auto default_from_flags = [&](const CustomData &data) {
1920 if (!mesh->default_color_attribute) {
1921 for (const int i : IndexRange(data.totlayer)) {
1922 if (data.layers[i].flag & CD_FLAG_COLOR_RENDER) {
1923 mesh->default_color_attribute = BLI_strdup(data.layers[i].name);
1924 }
1925 }
1926 }
1927 };
1928 auto default_from_indices = [&](const CustomData &data) {
1929 if (!mesh->default_color_attribute) {
1931 if (i != -1) {
1932 mesh->default_color_attribute = BLI_strdup(data.layers[i].name);
1933 }
1934 }
1935 if (!mesh->default_color_attribute) {
1937 if (i != -1) {
1938 mesh->default_color_attribute = BLI_strdup(data.layers[i].name);
1939 }
1940 }
1941 };
1942
1943 active_from_flags(mesh->vert_data);
1944 active_from_flags(mesh->corner_data);
1945 active_from_indices(mesh->vert_data);
1946 active_from_indices(mesh->corner_data);
1947
1948 default_from_flags(mesh->vert_data);
1949 default_from_flags(mesh->corner_data);
1950 default_from_indices(mesh->vert_data);
1951 default_from_indices(mesh->corner_data);
1952}
1953
1955
1956/* -------------------------------------------------------------------- */
1959
1961{
1962 if (CustomData_has_layer_named(&mesh->corner_data, CD_PROP_INT32, ".corner_vert") &&
1963 CustomData_has_layer_named(&mesh->corner_data, CD_PROP_INT32, ".corner_edge"))
1964 {
1965 return;
1966 }
1967 const Span<MLoop> loops(
1968 static_cast<const MLoop *>(CustomData_get_layer(&mesh->corner_data, CD_MLOOP)),
1969 mesh->corners_num);
1970 MutableSpan<int> corner_verts(
1971 static_cast<int *>(CustomData_add_layer_named(
1972 &mesh->corner_data, CD_PROP_INT32, CD_CONSTRUCT, mesh->corners_num, ".corner_vert")),
1973 mesh->corners_num);
1974 MutableSpan<int> corner_edges(
1975 static_cast<int *>(CustomData_add_layer_named(
1976 &mesh->corner_data, CD_PROP_INT32, CD_CONSTRUCT, mesh->corners_num, ".corner_edge")),
1977 mesh->corners_num);
1978 threading::parallel_for(loops.index_range(), 2048, [&](IndexRange range) {
1979 for (const int i : range) {
1980 corner_verts[i] = loops[i].v;
1981 corner_edges[i] = loops[i].e;
1982 }
1983 });
1984
1985 CustomData_free_layers(&mesh->corner_data, CD_MLOOP);
1986}
1987
1989
1990/* -------------------------------------------------------------------- */
1993
1994static bool poly_loops_orders_match(const Span<MPoly> polys)
1995{
1996 for (const int i : polys.index_range().drop_back(1)) {
1997 if (polys[i].loopstart > polys[i + 1].loopstart) {
1998 return false;
1999 }
2000 }
2001 return true;
2002}
2003
2005{
2006 if (mesh->face_offset_indices) {
2007 return;
2008 }
2009 const Span<MPoly> polys(
2010 static_cast<const MPoly *>(CustomData_get_layer(&mesh->face_data, CD_MPOLY)),
2011 mesh->faces_num);
2012
2014 MutableSpan<int> offsets = mesh->face_offsets_for_write();
2015
2016 if (poly_loops_orders_match(polys)) {
2017 for (const int i : polys.index_range()) {
2018 offsets[i] = polys[i].loopstart;
2019 }
2020 }
2021 else {
2022 /* Reorder mesh polygons to match the order of their loops. */
2023 Array<int> orig_indices(polys.size());
2025 std::stable_sort(orig_indices.begin(), orig_indices.end(), [polys](const int a, const int b) {
2026 return polys[a].loopstart < polys[b].loopstart;
2027 });
2028 CustomData old_poly_data = mesh->face_data;
2029 CustomData_reset(&mesh->face_data);
2031 &old_poly_data, &mesh->face_data, CD_MASK_MESH.pmask, CD_CONSTRUCT, mesh->faces_num);
2032
2033 int offset = 0;
2034 for (const int i : orig_indices.index_range()) {
2035 offsets[i] = offset;
2036 offset += polys[orig_indices[i]].totloop;
2037 }
2038
2039 threading::parallel_for(orig_indices.index_range(), 1024, [&](const IndexRange range) {
2040 for (const int i : range) {
2041 CustomData_copy_data(&old_poly_data, &mesh->face_data, orig_indices[i], i, 1);
2042 }
2043 });
2044
2045 CustomData_free(&old_poly_data);
2046 }
2047
2048 CustomData_free_layers(&mesh->face_data, CD_MPOLY);
2049}
2050
2052
2053/* -------------------------------------------------------------------- */
2056
2057namespace bke {
2058
2059static bNodeTree *add_auto_smooth_node_tree(Main &bmain, Library *owner_library)
2060{
2062 &bmain, owner_library, DATA_("Auto Smooth"), "GeometryNodeTree");
2063 if (!group->geometry_node_asset_traits) {
2064 group->geometry_node_asset_traits = MEM_new<GeometryNodeAssetTraits>(__func__);
2065 }
2067
2068 group->tree_interface.add_socket(
2069 DATA_("Geometry"), "", "NodeSocketGeometry", NODE_INTERFACE_SOCKET_OUTPUT, nullptr);
2070 group->tree_interface.add_socket(
2071 DATA_("Geometry"), "", "NodeSocketGeometry", NODE_INTERFACE_SOCKET_INPUT, nullptr);
2072 bNodeTreeInterfaceSocket *angle_io_socket = group->tree_interface.add_socket(
2073 DATA_("Angle"), "", "NodeSocketFloat", NODE_INTERFACE_SOCKET_INPUT, nullptr);
2074 auto &angle_data = *static_cast<bNodeSocketValueFloat *>(angle_io_socket->socket_data);
2075 angle_data.min = 0.0f;
2076 angle_data.max = DEG2RADF(180.0f);
2077 angle_data.subtype = PROP_ANGLE;
2078
2079 bNode *group_output = node_add_node(nullptr, *group, "NodeGroupOutput"_ustr);
2080 group_output->location[0] = 480.0f;
2081 group_output->location[1] = -100.0f;
2082 bNode *group_input_angle = node_add_node(nullptr, *group, "NodeGroupInput"_ustr);
2083 group_input_angle->location[0] = -420.0f;
2084 group_input_angle->location[1] = -300.0f;
2085 for (bNodeSocket &socket : group_input_angle->outputs) {
2086 if (!STREQ(socket.identifier, "Socket_2")) {
2087 socket.flag |= SOCK_HIDDEN;
2088 }
2089 }
2090 bNode *group_input_mesh = node_add_node(nullptr, *group, "NodeGroupInput"_ustr);
2091 group_input_mesh->location[0] = -60.0f;
2092 group_input_mesh->location[1] = -100.0f;
2093 for (bNodeSocket &socket : group_input_mesh->outputs) {
2094 if (!STREQ(socket.identifier, "Socket_1")) {
2095 socket.flag |= SOCK_HIDDEN;
2096 }
2097 }
2098 bNode *shade_smooth_edge = node_add_node(nullptr, *group, "GeometryNodeSetShadeSmooth"_ustr);
2099 shade_smooth_edge->custom1 = int16_t(bke::AttrDomain::Edge);
2100 shade_smooth_edge->location[0] = 120.0f;
2101 shade_smooth_edge->location[1] = -100.0f;
2102 bNode *shade_smooth_face = node_add_node(nullptr, *group, "GeometryNodeSetShadeSmooth"_ustr);
2103 shade_smooth_face->custom1 = int16_t(bke::AttrDomain::Face);
2104 shade_smooth_face->location[0] = 300.0f;
2105 shade_smooth_face->location[1] = -100.0f;
2106 bNode *edge_angle = node_add_node(nullptr, *group, "GeometryNodeInputMeshEdgeAngle"_ustr);
2107 edge_angle->location[0] = -420.0f;
2108 edge_angle->location[1] = -220.0f;
2109 bNode *edge_smooth = node_add_node(nullptr, *group, "GeometryNodeInputEdgeSmooth"_ustr);
2110 edge_smooth->location[0] = -60.0f;
2111 edge_smooth->location[1] = -160.0f;
2112 bNode *face_smooth = node_add_node(nullptr, *group, "GeometryNodeInputShadeSmooth"_ustr);
2113 face_smooth->location[0] = -240.0f;
2114 face_smooth->location[1] = -340.0f;
2115 bNode *boolean_and = node_add_node(nullptr, *group, "FunctionNodeBooleanMath"_ustr);
2116 boolean_and->custom1 = NODE_BOOLEAN_MATH_AND;
2117 boolean_and->location[0] = -60.0f;
2118 boolean_and->location[1] = -220.0f;
2119 bNode *less_than_or_equal = node_add_node(nullptr, *group, "FunctionNodeCompare"_ustr);
2120 static_cast<NodeFunctionCompare *>(less_than_or_equal->storage)->operation =
2122 less_than_or_equal->location[0] = -240.0f;
2123 less_than_or_equal->location[1] = -180.0f;
2124
2125 node_add_link(*group,
2126 *edge_angle,
2127 *node_find_socket(*edge_angle, SOCK_OUT, "Unsigned Angle"_ustr),
2128 *less_than_or_equal,
2129 *node_find_socket(*less_than_or_equal, SOCK_IN, "A"_ustr));
2130 node_add_link(*group,
2131 *shade_smooth_face,
2132 *node_find_socket(*shade_smooth_face, SOCK_OUT, "Geometry"_ustr),
2133 *group_output,
2134 *node_find_socket(*group_output, SOCK_IN, "Socket_0"_ustr));
2135 node_add_link(*group,
2136 *group_input_angle,
2137 *node_find_socket(*group_input_angle, SOCK_OUT, "Socket_2"_ustr),
2138 *less_than_or_equal,
2139 *node_find_socket(*less_than_or_equal, SOCK_IN, "B"_ustr));
2140 node_add_link(*group,
2141 *less_than_or_equal,
2142 *node_find_socket(*less_than_or_equal, SOCK_OUT, "Result"_ustr),
2143 *boolean_and,
2144 *node_find_socket(*boolean_and, SOCK_IN, "Boolean"_ustr));
2145 node_add_link(*group,
2146 *face_smooth,
2147 *node_find_socket(*face_smooth, SOCK_OUT, "Smooth"_ustr),
2148 *boolean_and,
2149 *node_find_socket(*boolean_and, SOCK_IN, "Boolean_001"_ustr));
2150 node_add_link(*group,
2151 *group_input_mesh,
2152 *node_find_socket(*group_input_mesh, SOCK_OUT, "Socket_1"_ustr),
2153 *shade_smooth_edge,
2154 *node_find_socket(*shade_smooth_edge, SOCK_IN, "Geometry"_ustr));
2155 node_add_link(*group,
2156 *edge_smooth,
2157 *node_find_socket(*edge_smooth, SOCK_OUT, "Smooth"_ustr),
2158 *shade_smooth_edge,
2159 *node_find_socket(*shade_smooth_edge, SOCK_IN, "Selection"_ustr));
2160 node_add_link(*group,
2161 *shade_smooth_edge,
2162 *node_find_socket(*shade_smooth_edge, SOCK_OUT, "Geometry"_ustr),
2163 *shade_smooth_face,
2164 *node_find_socket(*shade_smooth_face, SOCK_IN, "Geometry"_ustr));
2165 node_add_link(*group,
2166 *boolean_and,
2167 *node_find_socket(*boolean_and, SOCK_OUT, "Boolean"_ustr),
2168 *shade_smooth_edge,
2169 *node_find_socket(*shade_smooth_edge, SOCK_IN, "Shade Smooth"_ustr));
2170
2171 for (bNode &node : group->nodes) {
2172 node_set_selected(node, false);
2173 }
2174
2176
2177 return group;
2178}
2179
2181{
2183 for (const bNode *node : nodes) {
2184 for (const bNodeSocket &socket : node->inputs) {
2185 result.add_new(&socket);
2186 }
2187 for (const bNodeSocket &socket : node->outputs) {
2188 result.add_new(&socket);
2189 }
2190 }
2191 return result;
2192}
2193
2194/* Checks if the node group is the same as the one generated by #create_auto_smooth_modifier. */
2195static bool is_auto_smooth_node_tree(const bNodeTree &group)
2196{
2197 if (group.type != NTREE_GEOMETRY) {
2198 return false;
2199 }
2200 const Span<const bNode *> nodes = group.all_nodes();
2201 if (nodes.size() != 10) {
2202 return false;
2203 }
2204 if (!group.geometry_node_asset_traits) {
2205 return false;
2206 }
2208 return false;
2209 }
2210 const std::array<StringRef, 10> idnames({"NodeGroupOutput",
2211 "NodeGroupInput",
2212 "NodeGroupInput",
2213 "GeometryNodeSetShadeSmooth",
2214 "GeometryNodeSetShadeSmooth",
2215 "GeometryNodeInputMeshEdgeAngle",
2216 "GeometryNodeInputEdgeSmooth",
2217 "GeometryNodeInputShadeSmooth",
2218 "FunctionNodeBooleanMath",
2219 "FunctionNodeCompare"});
2220 for (const int i : nodes.index_range()) {
2221 if (nodes[i]->idname != idnames[i]) {
2222 return false;
2223 }
2224 }
2225 if (nodes[3]->custom1 != int16_t(bke::AttrDomain::Edge)) {
2226 return false;
2227 }
2228 if (nodes[4]->inputs.last()->default_value_typed<bNodeSocketValueBoolean>()->value != 1) {
2229 return false;
2230 }
2231 if (nodes[4]->custom1 != int16_t(bke::AttrDomain::Face)) {
2232 return false;
2233 }
2234 if (nodes[8]->custom1 != NODE_BOOLEAN_MATH_AND) {
2235 return false;
2236 }
2237 if (static_cast<NodeFunctionCompare *>(nodes[9]->storage)->operation != NODE_COMPARE_LESS_EQUAL)
2238 {
2239 return false;
2240 }
2241 if (group.links.count() != 9) {
2242 return false;
2243 }
2244
2245 const std::array<int, 9> link_from_socket_indices({16, 15, 3, 36, 19, 5, 18, 11, 22});
2246 const std::array<int, 9> link_to_socket_indices({23, 0, 24, 20, 21, 8, 9, 12, 10});
2248
2249 for (const auto [i, link] : group.links.enumerate()) {
2250 if (socket_indices.index_of(link.fromsock) != link_from_socket_indices[i]) {
2251 return false;
2252 }
2253 if (socket_indices.index_of(link.tosock) != link_to_socket_indices[i]) {
2254 return false;
2255 }
2256 }
2257
2258 return true;
2259}
2260
2262 Object &object,
2263 const FunctionRef<bNodeTree *(Library *owner_library)> get_node_group,
2264 const float angle)
2265{
2266 auto *md = reinterpret_cast<NodesModifierData *>(BKE_modifier_new(eModifierType_Nodes));
2267 STRNCPY_UTF8(md->modifier.name, DATA_("Auto Smooth"));
2268 BKE_modifier_unique_name(&object.modifiers, &md->modifier);
2269 md->node_group = get_node_group(object.id.lib);
2270 id_us_plus(&md->node_group->id);
2271
2272 IDProperty *system_props = bke::idprop::create_group("NodesModifierProperties").release();
2273
2274 IDProperty *inputs = bke::idprop::create_group("inputs").release();
2275 IDP_AddToGroup(system_props, inputs);
2276
2277 IDProperty *angle_prop_group = bke::idprop::create_group("Socket_2").release();
2278 IDP_AddToGroup(inputs, angle_prop_group);
2279
2280 IDProperty *type_prop =
2282 IDP_AddToGroup(angle_prop_group, type_prop);
2283
2284 IDProperty *angle_prop = idprop::create("value", angle).release();
2285 IDP_AddToGroup(angle_prop_group, angle_prop);
2286
2287 md->modifier.system_properties = system_props;
2288
2289 BKE_modifiers_persistent_uid_init(object, md->modifier);
2290 return &md->modifier;
2291}
2292
2293} // namespace bke
2294
2296{
2297 using namespace bke;
2298
2299 /* Add the node group lazily and share it among all objects in the same library. */
2300 Map<Library *, bNodeTree *> group_by_library;
2301 const auto add_node_group = [&](Library *owner_library) {
2302 if (bNodeTree **group = group_by_library.lookup_ptr(owner_library)) {
2303 /* Node tree has already been found/created for this versioning call. */
2304 return *group;
2305 }
2306 /* Try to find an existing group added by previous versioning to avoid adding duplicates. */
2307 for (bNodeTree &existing_group : bmain.nodetrees) {
2308 if (existing_group.id.lib != owner_library) {
2309 continue;
2310 }
2311 if (is_auto_smooth_node_tree(existing_group)) {
2312 group_by_library.add_new(owner_library, &existing_group);
2313 return &existing_group;
2314 }
2315 }
2316 bNodeTree *new_group = add_auto_smooth_node_tree(bmain, owner_library);
2317 /* Remove the default user. The count is tracked manually when assigning to modifiers. */
2318 id_us_min(&new_group->id);
2319 group_by_library.add_new(owner_library, new_group);
2320 return new_group;
2321 };
2322
2323 for (Object &object : bmain.objects) {
2324 if (object.type != OB_MESH) {
2325 continue;
2326 }
2327 Mesh *mesh = id_cast<Mesh *>(object.data);
2328 const float angle = mesh->smoothresh_legacy;
2329 if (!(mesh->flag & ME_AUTOSMOOTH_LEGACY)) {
2330 continue;
2331 }
2332
2333 /* Auto-smooth disabled sharp edge tagging when the evaluated mesh had custom normals.
2334 * When the original mesh has custom normals, that's a good sign the evaluated mesh will
2335 * have custom normals as well. */
2336 bool has_custom_normals = mesh->attribute_storage.wrap().lookup("custom_normal");
2337 if (has_custom_normals) {
2338 continue;
2339 }
2340
2341 /* The "Weighted Normal" modifier has a "Keep Sharp" option that used to recalculate the sharp
2342 * edge tags based on the mesh's smoothing angle. To keep the same behavior, a new modifier has
2343 * to be added before that modifier when the option is on. */
2344 for (ModifierData &md : object.modifiers) {
2346 has_custom_normals = true;
2347 }
2348 if (md.type == eModifierType_Bevel) {
2349 BevelModifierData *bmd = reinterpret_cast<BevelModifierData *>(&md);
2350 if (bmd->flags & MOD_BEVEL_HARDEN_NORMALS) {
2351 has_custom_normals = true;
2352 }
2353 }
2354 if (md.type == eModifierType_WeightedNormal) {
2355 WeightedNormalModifierData *nmd = reinterpret_cast<WeightedNormalModifierData *>(&md);
2356 if ((nmd->flag & MOD_WEIGHTEDNORMAL_KEEP_SHARP) != 0) {
2357 ModifierData *new_md = create_auto_smooth_modifier(object, add_node_group, angle);
2358 BLI_insertlinkbefore(&object.modifiers, object.modifiers.last(), new_md);
2359 }
2360 }
2361 if (md.type == eModifierType_Nodes) {
2362 NodesModifierData *nmd = reinterpret_cast<NodesModifierData *>(&md);
2363 if (nmd->node_group && !ID_MISSING(nmd->node_group) &&
2365 {
2366 /* This object has already been processed by versioning. If the mesh is linked from
2367 * another file its auto-smooth flag may not be cleared, so this check is necessary to
2368 * avoid adding a duplicate modifier. */
2369 has_custom_normals = true;
2370 break;
2371 }
2372 }
2373 }
2374
2375 /* Some modifiers always generate custom normals which disabled sharp edge tagging, making
2376 * adding a modifier at the end unnecessary. Conceptually this is similar to checking if the
2377 * evaluated mesh had custom normals. */
2378 if (has_custom_normals) {
2379 continue;
2380 }
2381
2382 ModifierData *last_md = object.modifiers.last();
2383 ModifierData *new_md = create_auto_smooth_modifier(object, add_node_group, angle);
2384 if (last_md && last_md->type == eModifierType_Subsurf && has_custom_normals &&
2385 (reinterpret_cast<SubsurfModifierData *>(last_md)->flags &
2387 {
2388 /* Add the auto smooth node group before the last subdivision surface modifier if possible.
2389 * Subdivision surface modifiers have special handling for interpolating custom normals. */
2390 BLI_insertlinkbefore(&object.modifiers, object.modifiers.last(), new_md);
2391 }
2392 else {
2393 BLI_addtail(&object.modifiers, new_md);
2394 }
2395 }
2396
2397 for (Mesh &mesh : bmain.meshes) {
2399 }
2400}
2401
2402namespace bke {
2403
2405{
2406 if (mesh.attributes().contains(".sculpt_mask")) {
2407 return;
2408 }
2409 void *data = nullptr;
2410 const ImplicitSharingInfo *sharing_info = nullptr;
2411 for (const int i : IndexRange(mesh.vert_data.totlayer)) {
2412 CustomDataLayer &layer = mesh.vert_data.layers[i];
2413 if (layer.type == CD_PAINT_MASK) {
2414 data = layer.data;
2415 sharing_info = layer.sharing_info;
2416 layer.data = nullptr;
2417 layer.sharing_info = nullptr;
2418 CustomData_free_layer(&mesh.vert_data, CD_PAINT_MASK, i);
2419 break;
2420 }
2421 }
2422 if (data != nullptr) {
2424 &mesh.vert_data, CD_PROP_FLOAT, data, mesh.verts_num, ".sculpt_mask", sharing_info);
2425 }
2426 if (sharing_info != nullptr) {
2427 sharing_info->remove_user_and_delete_if_last();
2428 }
2429}
2430
2432{
2433 {
2434 void *data = nullptr;
2435 const ImplicitSharingInfo *sharing_info = nullptr;
2436 for (const int i : IndexRange(mesh.edge_data.totlayer)) {
2437 CustomDataLayer &layer = mesh.edge_data.layers[i];
2438 if (layer.type == CD_FREESTYLE_EDGE) {
2439 data = layer.data;
2440 sharing_info = layer.sharing_info;
2441 layer.data = nullptr;
2442 layer.sharing_info = nullptr;
2443 CustomData_free_layer(&mesh.edge_data, CD_FREESTYLE_EDGE, i);
2444 break;
2445 }
2446 }
2447 if (data != nullptr) {
2448 static_assert(sizeof(FreestyleEdge) == sizeof(bool));
2449 static_assert(char(FREESTYLE_EDGE_MARK) == char(true));
2450 Attribute::ArrayData array_data{};
2451 array_data.data = data;
2452 array_data.size = mesh.edges_num;
2453 sharing_info->add_user();
2454 array_data.sharing_info = ImplicitSharingPtr<>(sharing_info);
2455 mesh.attribute_storage.wrap().add(
2456 "freestyle_edge", bke::AttrDomain::Edge, bke::AttrType::Bool, std::move(array_data));
2457 }
2458 if (sharing_info != nullptr) {
2459 sharing_info->remove_user_and_delete_if_last();
2460 }
2461 }
2462 {
2463 void *data = nullptr;
2464 const ImplicitSharingInfo *sharing_info = nullptr;
2465 for (const int i : IndexRange(mesh.face_data.totlayer)) {
2466 CustomDataLayer &layer = mesh.face_data.layers[i];
2467 if (layer.type == CD_FREESTYLE_FACE) {
2468 data = layer.data;
2469 sharing_info = layer.sharing_info;
2470 layer.data = nullptr;
2471 layer.sharing_info = nullptr;
2472 CustomData_free_layer(&mesh.face_data, CD_FREESTYLE_FACE, i);
2473 break;
2474 }
2475 }
2476 if (data != nullptr) {
2477 static_assert(sizeof(FreestyleFace) == sizeof(bool));
2478 static_assert(char(FREESTYLE_FACE_MARK) == char(true));
2479 Attribute::ArrayData array_data{};
2480 array_data.data = data;
2481 array_data.size = mesh.faces_num;
2482 sharing_info->add_user();
2483 array_data.sharing_info = ImplicitSharingPtr<>(sharing_info);
2484 mesh.attribute_storage.wrap().add(
2485 "freestyle_face", bke::AttrDomain::Face, bke::AttrType::Bool, std::move(array_data));
2486 }
2487 if (sharing_info != nullptr) {
2488 sharing_info->remove_user_and_delete_if_last();
2489 }
2490 }
2491}
2492
2494{
2495 if (mesh.attributes().contains("skin_modifier_radius")) {
2496 return;
2497 }
2498 const void *data = nullptr;
2499 for (const int i : IndexRange(mesh.vert_data.totlayer)) {
2500 const CustomDataLayer &layer = mesh.vert_data.layers[i];
2501 if (layer.type == CD_MVERT_SKIN) {
2502 data = layer.data;
2503 break;
2504 }
2505 }
2506 if (data == nullptr) {
2507 return;
2508 }
2509 const Span src(static_cast<const MVertSkin *>(data), mesh.verts_num);
2510
2511 float2 *radius_data = MEM_new_array_uninitialized<float2>(mesh.verts_num, __func__);
2512 bool *root_data = MEM_new_array_uninitialized<bool>(mesh.verts_num, __func__);
2513 bool *loose_data = MEM_new_array_uninitialized<bool>(mesh.verts_num, __func__);
2514 threading::parallel_for(src.index_range(), 4096, [&](const IndexRange range) {
2515 for (const int i : range) {
2516 radius_data[i] = float2(src[i].radius[0], src[i].radius[1]);
2517 root_data[i] = (src[i].flag & MVERT_SKIN_ROOT) != 0;
2518 loose_data[i] = (src[i].flag & MVERT_SKIN_LOOSE) != 0;
2519 }
2520 });
2521
2522 CustomData_free_layers(&mesh.vert_data, CD_MVERT_SKIN);
2523
2524 AttributeStorage &storage = mesh.attribute_storage.wrap();
2525 auto add_array = [&]<typename T>(const StringRef name, const AttrType type, T *array_data) {
2526 Attribute::ArrayData array{};
2527 array.data = array_data;
2528 array.size = mesh.verts_num;
2529 array.sharing_info = ImplicitSharingPtr<>(implicit_sharing::info_for_mem_free(array_data));
2530 storage.add(name, AttrDomain::Point, type, std::move(array));
2531 };
2532 add_array("skin_modifier_radius", AttrType::Float2, radius_data);
2533 add_array("skin_modifier_root", AttrType::Bool, root_data);
2534 add_array("skin_modifier_loose", AttrType::Bool, loose_data);
2535}
2536
2538 CustomData &vert_data,
2539 Vector<CustomDataLayer, 16> &vert_layers,
2540 const int verts_num)
2541{
2542 const AttributeArray *radius_array = nullptr;
2543 const AttributeArray *root_array = nullptr;
2544 const AttributeArray *loose_array = nullptr;
2545 const AttributeSingle *radius_single = nullptr;
2546 const AttributeSingle *root_single = nullptr;
2547 const AttributeSingle *loose_single = nullptr;
2548 Set<StringRef> attrs_to_remove;
2549 for (const int i : attr_write_data.attributes.index_range()) {
2550 const blender::Attribute &dna_attr = attr_write_data.attributes[i];
2551 if (dna_attr.domain != int8_t(AttrDomain::Point)) {
2552 continue;
2553 }
2554 if (dna_attr.data_type == int8_t(AttrType::Float2) &&
2555 STREQ(dna_attr.name, "skin_modifier_radius"))
2556 {
2557 if (dna_attr.storage_type == int8_t(AttrStorageType::Array)) {
2558 radius_array = static_cast<const AttributeArray *>(dna_attr.data);
2559 }
2560 else if (dna_attr.storage_type == int8_t(AttrStorageType::Single)) {
2561 radius_single = static_cast<const AttributeSingle *>(dna_attr.data);
2562 }
2563 attrs_to_remove.add("skin_modifier_radius");
2564 }
2565 else if (dna_attr.data_type == int8_t(AttrType::Bool) &&
2566 STREQ(dna_attr.name, "skin_modifier_root"))
2567 {
2568 if (dna_attr.storage_type == int8_t(AttrStorageType::Array)) {
2569 root_array = static_cast<const AttributeArray *>(dna_attr.data);
2570 }
2571 else if (dna_attr.storage_type == int8_t(AttrStorageType::Single)) {
2572 root_single = static_cast<const AttributeSingle *>(dna_attr.data);
2573 }
2574 attrs_to_remove.add("skin_modifier_root");
2575 }
2576 else if (dna_attr.data_type == int8_t(AttrType::Bool) &&
2577 STREQ(dna_attr.name, "skin_modifier_loose"))
2578 {
2579 if (dna_attr.storage_type == int8_t(AttrStorageType::Array)) {
2580 loose_array = static_cast<const AttributeArray *>(dna_attr.data);
2581 }
2582 else if (dna_attr.storage_type == int8_t(AttrStorageType::Single)) {
2583 loose_single = static_cast<const AttributeSingle *>(dna_attr.data);
2584 }
2585 attrs_to_remove.add("skin_modifier_loose");
2586 }
2587 }
2588 if (radius_array == nullptr && radius_single == nullptr) {
2589 return;
2590 }
2591
2592 const VArray<float2> radius = [&]() {
2593 if (radius_single) {
2594 return VArray<float2>::from_single(*static_cast<const float2 *>(radius_single->data),
2595 verts_num);
2596 }
2598 Span(static_cast<const float2 *>(radius_array->data), verts_num));
2599 }();
2600 const VArray<bool> root = [&]() {
2601 if (root_single) {
2602 return VArray<bool>::from_single(*static_cast<const bool *>(root_single->data), verts_num);
2603 }
2604 if (root_array) {
2605 return VArray<bool>::from_span(Span(static_cast<const bool *>(root_array->data), verts_num));
2606 }
2607 return VArray<bool>::from_single(false, verts_num);
2608 }();
2609 const VArray<bool> loose = [&]() {
2610 if (loose_single) {
2611 return VArray<bool>::from_single(*static_cast<const bool *>(loose_single->data), verts_num);
2612 }
2613 if (loose_array) {
2615 Span(static_cast<const bool *>(loose_array->data), verts_num));
2616 }
2617 return VArray<bool>::from_single(false, verts_num);
2618 }();
2619
2620 MutableSpan legacy_data = attr_write_data.scope.allocator().construct_array<MVertSkin>(
2621 verts_num);
2622 threading::parallel_for(IndexRange(verts_num), 4096, [&](const IndexRange range) {
2623 for (const int i : range) {
2624 legacy_data[i].radius[0] = radius[i][0];
2625 legacy_data[i].radius[1] = radius[i][1];
2626 legacy_data[i].radius[2] = 0.0f;
2627 legacy_data[i].flag = eMVertSkinFlag(0);
2628 if (root[i]) {
2629 legacy_data[i].flag |= MVERT_SKIN_ROOT;
2630 }
2631 if (loose[i]) {
2632 legacy_data[i].flag |= MVERT_SKIN_LOOSE;
2633 }
2634 }
2635 });
2636
2638 layer.type = CD_MVERT_SKIN;
2639 layer.data = legacy_data.data();
2640 vert_layers.append(layer);
2641 std::ranges::stable_sort(vert_layers, [](const CustomDataLayer &a, const CustomDataLayer &b) {
2642 return a.type < b.type;
2643 });
2644 vert_data.totlayer = vert_layers.size();
2645 vert_data.maxlayer = vert_data.totlayer;
2646 if (!vert_data.layers) {
2647 /* #CustomData_blend_write_prepare cleared this because there were no other layers to write.
2648 * We just need a unique address as a signal to write layers like that function does. */
2649 vert_data.layers = reinterpret_cast<CustomDataLayer *>(&vert_data.layers);
2650 }
2651
2652 attr_write_data.attributes.remove_if(
2653 [&](const blender::Attribute &attr) { return attrs_to_remove.contains_as(attr.name); });
2654}
2655
2657{
2658 if (mesh.attributes().contains("custom_normal")) {
2659 return;
2660 }
2661 void *data = nullptr;
2662 const ImplicitSharingInfo *sharing_info = nullptr;
2663 for (const int i : IndexRange(mesh.corner_data.totlayer)) {
2664 CustomDataLayer &layer = mesh.corner_data.layers[i];
2665 if (layer.type == CD_CUSTOMLOOPNORMAL) {
2666 data = layer.data;
2667 sharing_info = layer.sharing_info;
2668 layer.data = nullptr;
2669 layer.sharing_info = nullptr;
2670 CustomData_free_layer(&mesh.corner_data, CD_CUSTOMLOOPNORMAL, i);
2671 break;
2672 }
2673 }
2674 if (data != nullptr) {
2677 data,
2678 mesh.corners_num,
2679 "custom_normal",
2680 sharing_info);
2681 }
2682 if (sharing_info != nullptr) {
2683 sharing_info->remove_user_and_delete_if_last();
2684 }
2685}
2686
2688{
2689 const char *name = CustomData_get_active_layer_name(&mesh.corner_data, CD_PROP_FLOAT2);
2690 if (!name) {
2691 return;
2692 }
2693 const std::string uv_select_vert_name_shared = ".uv_select_vert";
2694 const std::string uv_select_edge_name_shared = ".uv_select_edge";
2695 const std::string uv_select_face_name_shared = ".uv_select_face";
2696
2697 const std::string uv_select_vert_prefix = ".vs.";
2698 const std::string uv_select_edge_prefix = ".es.";
2699
2700 const std::string uv_select_vert_name = uv_select_vert_prefix + name;
2701 const std::string uv_select_edge_name = uv_select_edge_prefix + name;
2702
2703 const int uv_select_vert = CustomData_get_named_layer_index(
2704 &mesh.corner_data, CD_PROP_BOOL, uv_select_vert_name);
2705 const int uv_select_edge = CustomData_get_named_layer_index(
2706 &mesh.corner_data, CD_PROP_BOOL, uv_select_edge_name);
2707
2708 if (uv_select_vert != -1 && uv_select_edge != -1) {
2709 /* Unlikely either exist but ensure there are no duplicate names. */
2710 CustomData_free_layer_named(&mesh.corner_data, uv_select_vert_name_shared);
2711 CustomData_free_layer_named(&mesh.corner_data, uv_select_edge_name_shared);
2712 CustomData_free_layer_named(&mesh.face_data, uv_select_face_name_shared);
2713
2714 STRNCPY_UTF8(mesh.corner_data.layers[uv_select_vert].name, uv_select_vert_name_shared.c_str());
2715 STRNCPY_UTF8(mesh.corner_data.layers[uv_select_edge].name, uv_select_edge_name_shared.c_str());
2716
2717 bool *uv_select_face = MEM_new_array_uninitialized<bool>(mesh.faces_num, __func__);
2720 uv_select_face,
2721 mesh.faces_num,
2722 uv_select_face_name_shared,
2723 nullptr);
2724
2725 /* Create a face selection layer (flush from edges). */
2726 if (mesh.faces_num > 0) {
2727 const OffsetIndices<int> faces = mesh.faces();
2728 const Span<bool> uv_select_edge_data(
2729 static_cast<bool *>(mesh.corner_data.layers[uv_select_edge].data), mesh.corners_num);
2730 threading::parallel_for(faces.index_range(), 1024, [&](const IndexRange range) {
2731 for (const int face : range) {
2732 uv_select_face[face] = !uv_select_edge_data.slice(faces[face]).contains(false);
2733 }
2734 });
2735 }
2736 }
2737
2738 /* Logically a set as names are expected to be unique.
2739 * If there are duplicates, this will remove those too. */
2740 Vector<std::string> attributes_to_remove;
2741 for (const int i : IndexRange(mesh.corner_data.totlayer)) {
2742 const CustomDataLayer &layer = mesh.corner_data.layers[i];
2743 if (layer.type != CD_PROP_BOOL) {
2744 continue;
2745 }
2746 StringRef layer_name = StringRef(layer.name);
2747 if (layer_name.startswith(uv_select_vert_prefix) ||
2748 layer_name.startswith(uv_select_edge_prefix))
2749 {
2750 attributes_to_remove.append(layer.name);
2751 }
2752 }
2753
2754 for (const StringRef name_to_remove : attributes_to_remove) {
2755 CustomData_free_layer_named(&mesh.corner_data, name_to_remove);
2756 }
2757}
2758
2759} // namespace bke
2760
2762
2764{
2765 const int nulegacy_faces = mesh->totface_legacy;
2767 eh.reserve(nulegacy_faces);
2768 MFace *legacy_faces = static_cast<MFace *>(
2769 CustomData_get_layer_for_write(&mesh->fdata_legacy, CD_MFACE, mesh->totface_legacy));
2770
2771 MFace *mf = legacy_faces;
2772 for (int i = 0; i < nulegacy_faces; i++, mf++) {
2773 eh.add({mf->v1, mf->v2});
2774 eh.add({mf->v2, mf->v3});
2775
2776 if (mf->v4) {
2777 eh.add({mf->v3, mf->v4});
2778 eh.add({mf->v4, mf->v1});
2779 }
2780 else {
2781 eh.add({mf->v3, mf->v1});
2782 }
2783 }
2784
2785 const int numEdges = eh.size();
2786
2787 /* write new edges into a temporary CustomData */
2788 CustomData edgeData;
2789 CustomData_reset(&edgeData);
2790 CustomData_add_layer(&edgeData, CD_ORIGINDEX, CD_SET_DEFAULT, numEdges);
2791
2792 int *index = (int *)CustomData_get_layer_for_write(&edgeData, CD_ORIGINDEX, mesh->edges_num);
2793
2794 memset(index, ORIGINDEX_NONE, sizeof(int) * numEdges);
2795
2796 /* free old CustomData and assign new one */
2797 CustomData_free(&mesh->edge_data);
2798 Set<StringRef> edge_attributes;
2799 for (const bke::Attribute &attr : mesh->attribute_storage.wrap()) {
2800 if (attr.domain() == bke::AttrDomain::Edge) {
2801 edge_attributes.add(attr.name());
2802 }
2803 }
2804 for (const StringRef name : edge_attributes) {
2805 mesh->attribute_storage.wrap().remove(name);
2806 }
2807 OrderedEdge *vector_data = eh.extract_vector().release().data;
2809 data.size = numEdges;
2810 data.data = reinterpret_cast<int2 *>(vector_data);
2812 mesh->attribute_storage.wrap().add(
2813 ".edge_verts", bke::AttrDomain::Edge, bke::AttrType::Int32_2D, std::move(data));
2814 mesh->edge_data = edgeData;
2815 mesh->edges_num = numEdges;
2816}
2817
2818} // namespace blender
Generic geometry attributes built on CustomData.
CustomData interface, see also DNA_customdata_types.h.
#define ORIGINDEX_NONE
#define DEG2RADF(_deg)
#define BLI_MEMARENA_STD_BUFSIZE
#define STRNCPY_UTF8(dst, src)
#define DATA_(msgid)
#define ID_MISSING(_id)
Definition DNA_ID.h:695
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define ELEM(...)
#define STREQ(a, b)
#define MAX_CUSTOMDATA_LAYER_NAME
#define MEM_realloc_uninitialized(vmemh, len)
#define CD_MASK_MDISPS
btScalar max
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
constexpr Span()=default
size_t size() const
Span< T > as_span() const
Definition BLI_array.hh:254
const T * end() const
Definition BLI_array.hh:336
IndexRange index_range() const
Definition BLI_array.hh:371
const T * begin() const
Definition BLI_array.hh:332
constexpr IndexRange drop_back(int64_t n) const
MutableSpan< T > construct_array(int64_t size, Args &&...args)
const Value * lookup_ptr(const Key &key) const
Definition BLI_map.hh:508
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:295
void add_new(const Key &key, const Value &value)
Definition BLI_map.hh:265
void reserve(int64_t n)
Definition BLI_map.hh:1028
MapType::ItemIterator items() const
void add(const Key &key, const Value &value)
constexpr T * data() const
Definition BLI_span.hh:545
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
LinearAllocator & allocator()
bool contains_as(const ForwardKey &key) const
Definition BLI_set.hh:314
bool add(const Key &key)
Definition BLI_set.hh:248
constexpr const T * data() const
Definition BLI_span.hh:219
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr const T * end() const
Definition BLI_span.hh:228
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
constexpr const T * begin() const
Definition BLI_span.hh:224
static VArray from_single(T value, const int64_t size)
static VArray from_span(Span< T > values)
bool add(const Key &key)
void reserve(const int64_t n)
int64_t size() const
void add_new(const Key &key)
int64_t size() const
void append(const T &value)
VectorData< T, Allocator > release()
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
bool contains(StringRef name) const
GAttributeReader lookup(const StringRef name) const
AttrStorageType storage_type() const
const DataVariant & data() const
GSpanAttributeWriter lookup_or_add_for_write_span(StringRef name, AttrDomain domain, AttrType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
GSpanAttributeWriter lookup_or_add_for_write_only_span(StringRef name, AttrDomain domain, AttrType data_type)
Span< NewT > constexpr cast() const
#define storage(slot, qualifiers)
#define attribute(slot)
#define layer
#define T
#define ML(v1, v2)
#define LAYER_CMP(l_a, t_a, l_b, t_b)
#define MESH_MLOOPCOL_FROM_MCOL(_mloopcol, _mcol)
#define TESSFACE_IS_QUAD
#define ML_TO_MF_QUAD()
#define MESH_MLOOPCOL_TO_MCOL(_mloopcol, _mcol)
#define ML_TO_MF(i1, i2, i3)
void fill_index_range(MutableSpan< T > span, const T start=0)
std::unique_ptr< IDProperty, IDPropertyDeleter > create(StringRef prop_name, int32_t value, eIDPropertyFlag flags={})
Allocate a new IDProperty of type IDP_INT, set its name and value.
std::unique_ptr< IDProperty, IDPropertyDeleter > create_group(StringRef prop_name, eIDPropertyFlag flags={})
Allocate a new IDProperty of type IDP_GROUP.
static bNodeTree * add_auto_smooth_node_tree(Main &bmain, Library *owner_library)
void mesh_sculpt_mask_to_generic(Mesh &mesh)
void mesh_skin_to_legacy(AttributeStorage::BlendWriteData &attr_write_data, CustomData &vert_data, Vector< CustomDataLayer, 16 > &vert_layers, int verts_num)
void mesh_freestyle_marks_to_generic(Mesh &mesh)
void mesh_convert_customdata_to_storage(Mesh &mesh)
bNode * node_add_node(const bContext *C, bNodeTree &ntree, UString idname, std::optional< int > unique_identifier=std::nullopt)
Definition node.cc:4260
static bool is_auto_smooth_node_tree(const bNodeTree &group)
void mesh_skin_to_generic(Mesh &mesh)
bool node_set_selected(bNode &node, bool select)
Definition node.cc:5455
bNodeLink & node_add_link(bNodeTree &ntree, bNode &fromnode, bNodeSocket &fromsock, bNode &tonode, bNodeSocket &tosock)
Definition node.cc:4638
void mesh_custom_normals_to_generic(Mesh &mesh)
bNodeSocket * node_find_socket(bNode &node, eNodeSocketInOut in_out, UString identifier)
Definition node.cc:3036
void mesh_uv_select_to_single_attribute(Mesh &mesh)
bNodeTree * node_tree_add_in_lib(Main *bmain, Library *owner_library, StringRefNull name, StringRefNull idname)
Definition node.cc:4997
static VectorSet< const bNodeSocket * > build_socket_indices(const Span< const bNode * > nodes)
static ModifierData * create_auto_smooth_modifier(Object &object, const FunctionRef< bNodeTree *(Library *owner_library)> get_node_group, const float angle)
const ImplicitSharingInfo * info_for_mem_free(T *data)
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.
bool CustomData_external_test(CustomData *data, eCustomDataType type)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static VectorSet< StringRefNull > get_mloopcol_names(const Mesh &mesh)
void BKE_mesh_legacy_crease_to_generic(Mesh *mesh)
int CustomData_number_of_layers(const CustomData *data, eCustomDataType type)
MemArena * BLI_memarena_new(size_t bufsize, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL ATTR_NONNULL(2) ATTR_MALLOC
void CustomData_swap_corners(CustomData *data, int index, const int *corner_indices)
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
void * CustomData_get_layer_n_for_write(CustomData *data, eCustomDataType type, int n, int totelem)
bool CustomData_free_layers(CustomData *data, eCustomDataType type)
void CustomData_free_elem(CustomData *data, int index, int count)
void id_us_plus(ID *id)
Definition lib_id.cc:433
const void * CustomData_add_layer_named_with_data(CustomData *data, eCustomDataType type, void *layer_data, int totelem, StringRef name, const ImplicitSharingInfo *sharing_info)
bool CustomData_has_layer(const CustomData *data, eCustomDataType type)
void BKE_mesh_legacy_edge_crease_to_layers(Mesh *mesh)
void CustomData_external_add(CustomData *data, ID *id, eCustomDataType type, int totelem, const char *filepath)
static void to_edgesort(EdgeSort *ed, uint v1, uint v2, char is_loose, short is_draw)
void BKE_mesh_legacy_attribute_flags_to_strings(Mesh *mesh)
MINLINE void copy_v2_v2(float r[2], const float a[2])
bool IDP_AddToGroup(IDProperty *group, IDProperty *prop) ATTR_NONNULL()
Definition idprop.cc:788
void BKE_mesh_face_offsets_ensure_alloc(Mesh *mesh)
void BKE_mesh_legacy_sharp_edges_from_flags(Mesh *mesh)
@ eModifierType_WeightedNormal
void mul_v2_m3v3(float r[2], const float M[3][3], const float a[3])
void BKE_mesh_do_versions_cd_flag_init(Mesh *mesh)
@ MOD_WEIGHTEDNORMAL_KEEP_SHARP
static void bm_corners_to_loops_ex(ID *id, CustomData *fdata_legacy, const int totface, CustomData *ldata, MFace *mface, int totloop, int findex, int loopstart, int numTex, int numCol)
@ GEO_NODE_ASSET_MODIFIER
void BLI_polyfill_calc_arena(const float(*coords)[2], unsigned int coords_num, int coords_sign, unsigned int(*r_tris)[3], struct MemArena *arena)
void BKE_mesh_tessface_clear(Mesh *mesh)
bool CustomData_free_layer_named(CustomData *data, StringRef name)
static bool check_matching_legacy_layer_counts(CustomData *fdata_legacy, CustomData *ldata, bool fallback)
void CustomData_copy_data(const CustomData *source, CustomData *dest, int source_index, int dest_index, int count)
void BKE_mesh_do_versions_convert_mfaces_to_mpolys(Mesh *mesh)
int CustomData_get_active_layer_index(const CustomData *data, eCustomDataType type)
void BKE_mesh_legacy_convert_verts_to_positions(Mesh *mesh)
void BKE_modifiers_persistent_uid_init(const Object &object, ModifierData &md)
int BKE_mesh_mface_index_validate(MFace *mface, CustomData *fdata_legacy, int mfindex, int nr)
@ NODE_BOOLEAN_MATH_AND
void BKE_mesh_legacy_bevel_weight_to_generic(Mesh *mesh)
const void * CustomData_get_layer_named(const CustomData *data, eCustomDataType type, StringRef name)
static void CustomData_bmesh_do_versions_update_active_layers(CustomData *fdata_legacy, CustomData *corner_data)
MINLINE void normal_float_to_short_v3(short out[3], const float in[3])
void BKE_mesh_calc_edges_tessface(Mesh *mesh)
Global G
Definition blender.cc:57
void BKE_modifier_unique_name(ListBaseT< ModifierData > *modifiers, ModifierData *md)
const char * CustomData_get_render_layer_name(const CustomData *data, eCustomDataType type)
void BKE_mesh_legacy_face_set_to_generic(Mesh *mesh)
void * CustomData_get_n_for_write(CustomData *data, eCustomDataType type, int index, int n, int totelem)
void BKE_mesh_legacy_sharp_faces_from_flags(Mesh *mesh)
int CustomData_get_render_layer(const CustomData *data, eCustomDataType type)
void CustomData_set_layer_render_index(CustomData *data, eCustomDataType type, int n)
void BKE_mesh_legacy_convert_loops_to_corners(Mesh *mesh)
unsigned int uint32_t
Definition BKE_text.h:81
const void * CustomData_add_layer_with_data(CustomData *data, eCustomDataType type, void *layer_data, int totelem, const ImplicitSharingInfo *sharing_info)
MINLINE void normal_short_to_float_v3(float out[3], const short in[3])
static void replace_custom_data_layer_with_named(CustomData &custom_data, const eCustomDataType old_type, const eCustomDataType new_type, const int elems_num, const char *new_name)
@ eSubsurfModifierFlag_UseCustomNormals
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
int multires_mdisp_corners(const MDisps *s)
Definition multires.cc:694
static bool poly_loops_orders_match(const Span< MPoly > polys)
static void update_active_fdata_layers(Mesh &mesh, CustomData *fdata_legacy, CustomData *ldata)
void BKE_mesh_tessface_calc(Mesh *mesh)
void CustomData_reset(CustomData *data)
BMesh const char void * data
static void add_mface_layers(Mesh &mesh, CustomData *fdata_legacy, CustomData *ldata, int total)
void BKE_mesh_legacy_convert_flags_to_hide_layers(Mesh *mesh)
void BKE_mesh_legacy_convert_flags_to_selection_layers(Mesh *mesh)
void BKE_mesh_legacy_face_map_to_generic(Main *bmain)
ModifierData * BKE_modifier_new(int type)
static void mesh_loops_to_tessdata(Mesh &mesh, CustomData *fdata_legacy, CustomData *corner_data, MFace *mface, uint(*loopindices)[4], const int num_faces)
void BLI_memarena_clear(MemArena *ma) ATTR_NONNULL(1)
void * CustomData_add_layer_named(CustomData *data, eCustomDataType type, eCDAllocType alloctype, int totelem, StringRef name)
void BKE_ntree_update_after_single_tree_change(Main &bmain, bNodeTree &modified_tree, const NodeTreeUpdateExtraParams &params={})
static int vergedgesort(const void *v1, const void *v2)
void BKE_mesh_legacy_convert_polys_to_offsets(Mesh *mesh)
void BKE_mesh_legacy_convert_uvs_to_generic(Mesh *mesh)
void BKE_mesh_calc_edges_legacy(Mesh *mesh)
@ NODE_COMPARE_LESS_EQUAL
void BKE_mesh_legacy_convert_edges_to_generic(Mesh *mesh)
bool CustomData_free_layer(CustomData *data, eCustomDataType type, int index)
const void * CustomData_get_layer(const CustomData *data, eCustomDataType type)
static void convert_mfaces_to_mpolys(ID *id, CustomData *fdata_legacy, CustomData *ldata, CustomData *pdata, int totedge_i, int totface_i, int, int, int2 *edges, MFace *mface, int *r_totloop, int *r_faces_num)
const char * CustomData_get_active_layer_name(const CustomData *data, eCustomDataType type)
MINLINE int poly_to_tri_count(int poly_count, int corner_count)
MINLINE void zero_v3(float r[3])
@ ME_AUTOSMOOTH_LEGACY
void BKE_mesh_legacy_bevel_weight_to_layers(Mesh *mesh)
void BLI_insertlinkbefore(ListBase *listbase, void *vnextlink, void *vnewlink) ATTR_NONNULL(1)
Definition listbase.cc:378
int CustomData_get_named_layer_index(const CustomData *data, eCustomDataType type, StringRef name)
static void CustomData_to_bmeshpoly(CustomData *fdata_legacy, CustomData *ldata, int totloop)
static void move_face_map_data_to_attributes(Mesh *mesh)
void CustomData_set_layer_render(CustomData *data, eCustomDataType type, int n)
void * BLI_memarena_alloc(MemArena *ma, size_t size) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_ALLOC_SIZE(2)
MINLINE float normalize_v3(float n[3])
void BKE_mesh_strip_loose_faces(Mesh *mesh)
void BKE_main_mesh_legacy_convert_auto_smooth(Main &bmain)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
ATTR_WARN_UNUSED_RESULT const BMVert * v2
const CustomData_MeshMasks CD_MASK_MESH
void CustomData_free(CustomData *data)
const char * CustomData_get_layer_name(const CustomData *data, eCustomDataType type, int n)
int CustomData_get_render_layer_index(const CustomData *data, eCustomDataType type)
MINLINE void add_newell_cross_v3_v3v3(float n[3], const float v_prev[3], const float v_curr[3])
void * CustomData_get_layer_named_for_write(CustomData *data, eCustomDataType type, StringRef name, int totelem)
void BKE_mesh_tessface_ensure(Mesh *mesh)
void CustomData_set_layer_active(CustomData *data, eCustomDataType type, int n)
int CustomData_get_active_layer(const CustomData *data, eCustomDataType type)
void CustomData_set_layer_active_index(CustomData *data, eCustomDataType type, int n)
void * CustomData_get_layer_for_write(CustomData *data, eCustomDataType type, int totelem)
void * CustomData_get_for_write(CustomData *data, int index, eCustomDataType type, int totelem)
VecBase< float, 3 > float3
bool CustomData_has_layer_named(const CustomData *data, eCustomDataType type, StringRef name)
void CustomData_external_read(CustomData *data, StringRefNull basepath, ID *id, eCustomDataMask mask, int totelem)
int CustomData_get_named_layer(const CustomData *data, eCustomDataType type, StringRef name)
void BKE_mesh_legacy_uv_seam_from_flags(Mesh *mesh)
void id_us_min(ID *id)
Definition lib_id.cc:441
static void mesh_ensure_tessellation_customdata(Mesh *mesh)
static void mesh_tessface_calc(Mesh &mesh)
void BKE_mesh_convert_mfaces_to_mpolys(Mesh *mesh)
signed char int8_t
Definition BKE_text.h:76
ColorSceneLinearByteEncoded4b< eAlpha::Premultiplied > ColorGeometry4b
static void mesh_calc_edges_mdata(const MVert *, const MFace *allface, MLoop *allloop, const MPoly *allpoly, int, int totface, int, int faces_num, MEdge **r_medge, int *r_totedge)
void BLI_memarena_free(MemArena *ma) ATTR_NONNULL(1)
void axis_dominant_v3_to_m3_negate(float r_mat[3][3], const float normal[3])
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
signed short int16_t
Definition BKE_text.h:77
void * CustomData_add_layer(CustomData *data, eCustomDataType type, eCDAllocType alloctype, int totelem)
void BKE_mesh_legacy_convert_mpoly_to_material_indices(Mesh *mesh)
#define hash
Definition noise_c.cc:156
#define sqrtf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
CustomDataExternal * external
struct Library * lib
Definition DNA_ID.h:421
eMFace_EdgeCode edcode
ListBaseT< bNodeTree > nodetrees
Definition BKE_main.hh:409
ListBaseT< Object > objects
Definition BKE_main.hh:388
ListBaseT< Mesh > meshes
Definition BKE_main.hh:389
ListBaseT< bNodeLink > links
struct GeometryNodeAssetTraits * geometry_node_asset_traits
bNodeTreeInterface tree_interface
eNodeTree_Type type
ListBaseT< bNode > nodes
ListBaseT< bNodeSocket > outputs