1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software Foundation,
14 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15 */
16
17 /** \file
18 * \ingroup bmesh
19 *
20 * BM mesh conversion functions.
21 *
22 * \section bm_mesh_conv_shapekey Converting Shape Keys
23 *
24 * When converting to/from a Mesh/BMesh you can optionally pass a shape key to edit.
25 * This has the effect of editing the shape key-block rather than the original mesh vertex coords
26 * (although additional geometry is still allowed and uses fallback locations on converting).
27 *
28 * While this works for any mesh/bmesh this is made use of by entering and exiting edit-mode.
29 *
30 * There are comments in code but this should help explain the general
31 * intention as to how this works converting from/to bmesh.
32 * \subsection user_pov User Perspective
33 *
34 * - Editmode operations when a shape key-block is active edits only that key-block.
35 * - The first Basis key-block always matches the Mesh verts.
36 * - Changing vertex locations of _any_ Basis
37 * will apply offsets to those shape keys using this as their Basis.
38 *
39 * \subsection enter_editmode Entering EditMode - #BM_mesh_bm_from_me
40 *
41 * - The active key-block is used for BMesh vertex locations on entering edit-mode.
42 * So obviously the meshes vertex locations remain unchanged and the shape key
43 * its self is not being edited directly.
44 * Simply the #BMVert.co is a initialized from active shape key (when its set).
45 * - All key-blocks are added as CustomData layers (read code for details).
46 *
47 * \subsection exit_editmode Exiting EditMode - #BM_mesh_bm_to_me
48 *
49 * This is where the most confusing code is! Won't attempt to document the details here,
50 * for that read the code.
51 * But basics are as follows.
52 *
53 * - Vertex locations (possibly modified from initial active key-block)
54 * are copied directly into #MVert.co
55 * (special confusing note that these may be restored later, when editing the 'Basis', read on).
56 * - if the 'Key' is relative, and the active key-block is the basis for ANY other key-blocks -
57 * get an array of offsets between the new vertex locations and the original shape key
58 * (before entering edit-mode), these offsets get applied later on to inactive key-blocks
59 * using the active one (which we are editing) as their Basis.
60 *
61 * Copying the locations back to the shape keys is quite confusing...
62 * One main area of confusion is that when editing a 'Basis' key-block 'me->key->refkey'
63 * The coords are written into the mesh, from the users perspective the Basis coords are written
64 * into the mesh when exiting edit-mode.
65 *
66 * When _not_ editing the 'Basis', the original vertex locations
67 * (stored in the mesh and unchanged during edit-mode), are copied back into the mesh.
68 *
69 * This has the effect from the users POV of leaving the mesh un-touched,
70 * and only editing the active shape key-block.
71 */
72
73 #include "DNA_key_types.h"
74 #include "DNA_mesh_types.h"
75 #include "DNA_meshdata_types.h"
76 #include "DNA_modifier_types.h"
77 #include "DNA_object_types.h"
78
79 #include "MEM_guardedalloc.h"
80
81 #include "BLI_alloca.h"
82 #include "BLI_listbase.h"
83 #include "BLI_math_vector.h"
84
85 #include "BKE_customdata.h"
86 #include "BKE_mesh.h"
87 #include "BKE_mesh_runtime.h"
88 #include "BKE_multires.h"
89
90 #include "BKE_key.h"
91 #include "BKE_main.h"
92
93 #include "DEG_depsgraph_query.h"
94
95 #include "bmesh.h"
96 #include "intern/bmesh_private.h" /* For element checking. */
97
BM_mesh_cd_flag_ensure(BMesh * bm,Mesh * mesh,const char cd_flag)98 void BM_mesh_cd_flag_ensure(BMesh *bm, Mesh *mesh, const char cd_flag)
99 {
100 const char cd_flag_all = BM_mesh_cd_flag_from_bmesh(bm) | cd_flag;
101 BM_mesh_cd_flag_apply(bm, cd_flag_all);
102 if (mesh) {
103 mesh->cd_flag = cd_flag_all;
104 }
105 }
106
BM_mesh_cd_flag_apply(BMesh * bm,const char cd_flag)107 void BM_mesh_cd_flag_apply(BMesh *bm, const char cd_flag)
108 {
109 /* CustomData_bmesh_init_pool() must run first */
110 BLI_assert(bm->vdata.totlayer == 0 || bm->vdata.pool != NULL);
111 BLI_assert(bm->edata.totlayer == 0 || bm->edata.pool != NULL);
112 BLI_assert(bm->pdata.totlayer == 0 || bm->pdata.pool != NULL);
113
114 if (cd_flag & ME_CDFLAG_VERT_BWEIGHT) {
115 if (!CustomData_has_layer(&bm->vdata, CD_BWEIGHT)) {
116 BM_data_layer_add(bm, &bm->vdata, CD_BWEIGHT);
117 }
118 }
119 else {
120 if (CustomData_has_layer(&bm->vdata, CD_BWEIGHT)) {
121 BM_data_layer_free(bm, &bm->vdata, CD_BWEIGHT);
122 }
123 }
124
125 if (cd_flag & ME_CDFLAG_EDGE_BWEIGHT) {
126 if (!CustomData_has_layer(&bm->edata, CD_BWEIGHT)) {
127 BM_data_layer_add(bm, &bm->edata, CD_BWEIGHT);
128 }
129 }
130 else {
131 if (CustomData_has_layer(&bm->edata, CD_BWEIGHT)) {
132 BM_data_layer_free(bm, &bm->edata, CD_BWEIGHT);
133 }
134 }
135
136 if (cd_flag & ME_CDFLAG_EDGE_CREASE) {
137 if (!CustomData_has_layer(&bm->edata, CD_CREASE)) {
138 BM_data_layer_add(bm, &bm->edata, CD_CREASE);
139 }
140 }
141 else {
142 if (CustomData_has_layer(&bm->edata, CD_CREASE)) {
143 BM_data_layer_free(bm, &bm->edata, CD_CREASE);
144 }
145 }
146 }
147
BM_mesh_cd_flag_from_bmesh(BMesh * bm)148 char BM_mesh_cd_flag_from_bmesh(BMesh *bm)
149 {
150 char cd_flag = 0;
151 if (CustomData_has_layer(&bm->vdata, CD_BWEIGHT)) {
152 cd_flag |= ME_CDFLAG_VERT_BWEIGHT;
153 }
154 if (CustomData_has_layer(&bm->edata, CD_BWEIGHT)) {
155 cd_flag |= ME_CDFLAG_EDGE_BWEIGHT;
156 }
157 if (CustomData_has_layer(&bm->edata, CD_CREASE)) {
158 cd_flag |= ME_CDFLAG_EDGE_CREASE;
159 }
160 return cd_flag;
161 }
162
163 /* Static function for alloc (duplicate in modifiers_bmesh.c) */
bm_face_create_from_mpoly(MPoly * mp,MLoop * ml,BMesh * bm,BMVert ** vtable,BMEdge ** etable)164 static BMFace *bm_face_create_from_mpoly(
165 MPoly *mp, MLoop *ml, BMesh *bm, BMVert **vtable, BMEdge **etable)
166 {
167 BMVert **verts = BLI_array_alloca(verts, mp->totloop);
168 BMEdge **edges = BLI_array_alloca(edges, mp->totloop);
169 int j;
170
171 for (j = 0; j < mp->totloop; j++, ml++) {
172 verts[j] = vtable[ml->v];
173 edges[j] = etable[ml->e];
174 }
175
176 return BM_face_create(bm, verts, edges, mp->totloop, NULL, BM_CREATE_SKIP_CD);
177 }
178
179 /**
180 * \brief Mesh -> BMesh
181 * \param bm: The mesh to write into, while this is typically a newly created BMesh,
182 * merging into existing data is supported.
183 * Note the custom-data layout isn't used.
184 * If more comprehensive merging is needed we should move this into a separate function
185 * since this should be kept fast for edit-mode switching and storing undo steps.
186 *
187 * \warning This function doesn't calculate face normals.
188 */
BM_mesh_bm_from_me(BMesh * bm,const Mesh * me,const struct BMeshFromMeshParams * params)189 void BM_mesh_bm_from_me(BMesh *bm, const Mesh *me, const struct BMeshFromMeshParams *params)
190 {
191 const bool is_new = !(bm->totvert || (bm->vdata.totlayer || bm->edata.totlayer ||
192 bm->pdata.totlayer || bm->ldata.totlayer));
193 MVert *mvert;
194 MEdge *medge;
195 MLoop *mloop;
196 MPoly *mp;
197 KeyBlock *actkey, *block;
198 BMVert *v, **vtable = NULL;
199 BMEdge *e, **etable = NULL;
200 BMFace *f, **ftable = NULL;
201 float(*keyco)[3] = NULL;
202 int totloops, i;
203 CustomData_MeshMasks mask = CD_MASK_BMESH;
204 CustomData_MeshMasks_update(&mask, ¶ms->cd_mask_extra);
205
206 if (!me || !me->totvert) {
207 if (me && is_new) { /* No verts? still copy custom-data layout. */
208 CustomData_copy(&me->vdata, &bm->vdata, mask.vmask, CD_ASSIGN, 0);
209 CustomData_copy(&me->edata, &bm->edata, mask.emask, CD_ASSIGN, 0);
210 CustomData_copy(&me->ldata, &bm->ldata, mask.lmask, CD_ASSIGN, 0);
211 CustomData_copy(&me->pdata, &bm->pdata, mask.pmask, CD_ASSIGN, 0);
212
213 CustomData_bmesh_init_pool(&bm->vdata, me->totvert, BM_VERT);
214 CustomData_bmesh_init_pool(&bm->edata, me->totedge, BM_EDGE);
215 CustomData_bmesh_init_pool(&bm->ldata, me->totloop, BM_LOOP);
216 CustomData_bmesh_init_pool(&bm->pdata, me->totpoly, BM_FACE);
217 }
218 return; /* Sanity check. */
219 }
220
221 if (is_new) {
222 CustomData_copy(&me->vdata, &bm->vdata, mask.vmask, CD_CALLOC, 0);
223 CustomData_copy(&me->edata, &bm->edata, mask.emask, CD_CALLOC, 0);
224 CustomData_copy(&me->ldata, &bm->ldata, mask.lmask, CD_CALLOC, 0);
225 CustomData_copy(&me->pdata, &bm->pdata, mask.pmask, CD_CALLOC, 0);
226 }
227 else {
228 CustomData_bmesh_merge(&me->vdata, &bm->vdata, mask.vmask, CD_CALLOC, bm, BM_VERT);
229 CustomData_bmesh_merge(&me->edata, &bm->edata, mask.emask, CD_CALLOC, bm, BM_EDGE);
230 CustomData_bmesh_merge(&me->ldata, &bm->ldata, mask.lmask, CD_CALLOC, bm, BM_LOOP);
231 CustomData_bmesh_merge(&me->pdata, &bm->pdata, mask.pmask, CD_CALLOC, bm, BM_FACE);
232 }
233
234 /* -------------------------------------------------------------------- */
235 /* Shape Key */
236 int tot_shape_keys = 0;
237 if (me->key != NULL && DEG_is_original_id(&me->id)) {
238 /* Evaluated meshes can be topologically inconsistent with their shape keys.
239 * Shape keys are also already integrated into the state of the evaluated
240 * mesh, so considering them here would kind of apply them twice. */
241 tot_shape_keys = BLI_listbase_count(&me->key->block);
242 }
243 if (is_new == false) {
244 tot_shape_keys = min_ii(tot_shape_keys, CustomData_number_of_layers(&bm->vdata, CD_SHAPEKEY));
245 }
246 const float(**shape_key_table)[3] = tot_shape_keys ?
247 BLI_array_alloca(shape_key_table, tot_shape_keys) :
248 NULL;
249
250 if ((params->active_shapekey != 0) && tot_shape_keys > 0) {
251 actkey = BLI_findlink(&me->key->block, params->active_shapekey - 1);
252 }
253 else {
254 actkey = NULL;
255 }
256
257 if (is_new) {
258 if (tot_shape_keys || params->add_key_index) {
259 CustomData_add_layer(&bm->vdata, CD_SHAPE_KEYINDEX, CD_ASSIGN, NULL, 0);
260 }
261 }
262
263 if (tot_shape_keys) {
264 if (is_new) {
265 /* Check if we need to generate unique ids for the shape-keys.
266 * This also exists in the file reading code, but is here for a sanity check. */
267 if (!me->key->uidgen) {
268 fprintf(stderr,
269 "%s had to generate shape key uid's in a situation we shouldn't need to! "
270 "(bmesh internal error)\n",
271 __func__);
272
273 me->key->uidgen = 1;
274 for (block = me->key->block.first; block; block = block->next) {
275 block->uid = me->key->uidgen++;
276 }
277 }
278 }
279
280 if (actkey && actkey->totelem == me->totvert) {
281 keyco = params->use_shapekey ? actkey->data : NULL;
282 if (is_new) {
283 bm->shapenr = params->active_shapekey;
284 }
285 }
286
287 for (i = 0, block = me->key->block.first; i < tot_shape_keys; block = block->next, i++) {
288 if (is_new) {
289 CustomData_add_layer_named(&bm->vdata, CD_SHAPEKEY, CD_ASSIGN, NULL, 0, block->name);
290 int j = CustomData_get_layer_index_n(&bm->vdata, CD_SHAPEKEY, i);
291 bm->vdata.layers[j].uid = block->uid;
292 }
293 shape_key_table[i] = (const float(*)[3])block->data;
294 }
295 }
296
297 if (is_new) {
298 CustomData_bmesh_init_pool(&bm->vdata, me->totvert, BM_VERT);
299 CustomData_bmesh_init_pool(&bm->edata, me->totedge, BM_EDGE);
300 CustomData_bmesh_init_pool(&bm->ldata, me->totloop, BM_LOOP);
301 CustomData_bmesh_init_pool(&bm->pdata, me->totpoly, BM_FACE);
302
303 BM_mesh_cd_flag_apply(bm, me->cd_flag);
304 }
305
306 const int cd_vert_bweight_offset = CustomData_get_offset(&bm->vdata, CD_BWEIGHT);
307 const int cd_edge_bweight_offset = CustomData_get_offset(&bm->edata, CD_BWEIGHT);
308 const int cd_edge_crease_offset = CustomData_get_offset(&bm->edata, CD_CREASE);
309 const int cd_shape_key_offset = tot_shape_keys ? CustomData_get_offset(&bm->vdata, CD_SHAPEKEY) :
310 -1;
311 const int cd_shape_keyindex_offset = is_new && (tot_shape_keys || params->add_key_index) ?
312 CustomData_get_offset(&bm->vdata, CD_SHAPE_KEYINDEX) :
313 -1;
314
315 vtable = MEM_mallocN(sizeof(BMVert **) * me->totvert, __func__);
316
317 for (i = 0, mvert = me->mvert; i < me->totvert; i++, mvert++) {
318 v = vtable[i] = BM_vert_create(bm, keyco ? keyco[i] : mvert->co, NULL, BM_CREATE_SKIP_CD);
319 BM_elem_index_set(v, i); /* set_ok */
320
321 /* Transfer flag. */
322 v->head.hflag = BM_vert_flag_from_mflag(mvert->flag & ~SELECT);
323
324 /* This is necessary for selection counts to work properly. */
325 if (mvert->flag & SELECT) {
326 BM_vert_select_set(bm, v, true);
327 }
328
329 normal_short_to_float_v3(v->no, mvert->no);
330
331 /* Copy Custom Data */
332 CustomData_to_bmesh_block(&me->vdata, &bm->vdata, i, &v->head.data, true);
333
334 if (cd_vert_bweight_offset != -1) {
335 BM_ELEM_CD_SET_FLOAT(v, cd_vert_bweight_offset, (float)mvert->bweight / 255.0f);
336 }
337
338 /* Set shape key original index. */
339 if (cd_shape_keyindex_offset != -1) {
340 BM_ELEM_CD_SET_INT(v, cd_shape_keyindex_offset, i);
341 }
342
343 /* Set shape-key data. */
344 if (tot_shape_keys) {
345 float(*co_dst)[3] = BM_ELEM_CD_GET_VOID_P(v, cd_shape_key_offset);
346 for (int j = 0; j < tot_shape_keys; j++, co_dst++) {
347 copy_v3_v3(*co_dst, shape_key_table[j][i]);
348 }
349 }
350 }
351 if (is_new) {
352 bm->elem_index_dirty &= ~BM_VERT; /* Added in order, clear dirty flag. */
353 }
354
355 etable = MEM_mallocN(sizeof(BMEdge **) * me->totedge, __func__);
356
357 medge = me->medge;
358 for (i = 0; i < me->totedge; i++, medge++) {
359 e = etable[i] = BM_edge_create(
360 bm, vtable[medge->v1], vtable[medge->v2], NULL, BM_CREATE_SKIP_CD);
361 BM_elem_index_set(e, i); /* set_ok */
362
363 /* Transfer flags. */
364 e->head.hflag = BM_edge_flag_from_mflag(medge->flag & ~SELECT);
365
366 /* This is necessary for selection counts to work properly. */
367 if (medge->flag & SELECT) {
368 BM_edge_select_set(bm, e, true);
369 }
370
371 /* Copy Custom Data */
372 CustomData_to_bmesh_block(&me->edata, &bm->edata, i, &e->head.data, true);
373
374 if (cd_edge_bweight_offset != -1) {
375 BM_ELEM_CD_SET_FLOAT(e, cd_edge_bweight_offset, (float)medge->bweight / 255.0f);
376 }
377 if (cd_edge_crease_offset != -1) {
378 BM_ELEM_CD_SET_FLOAT(e, cd_edge_crease_offset, (float)medge->crease / 255.0f);
379 }
380 }
381 if (is_new) {
382 bm->elem_index_dirty &= ~BM_EDGE; /* Added in order, clear dirty flag. */
383 }
384
385 /* Only needed for selection. */
386 if (me->mselect && me->totselect != 0) {
387 ftable = MEM_mallocN(sizeof(BMFace **) * me->totpoly, __func__);
388 }
389
390 mloop = me->mloop;
391 mp = me->mpoly;
392 for (i = 0, totloops = 0; i < me->totpoly; i++, mp++) {
393 BMLoop *l_iter;
394 BMLoop *l_first;
395
396 f = bm_face_create_from_mpoly(mp, mloop + mp->loopstart, bm, vtable, etable);
397 if (ftable != NULL) {
398 ftable[i] = f;
399 }
400
401 if (UNLIKELY(f == NULL)) {
402 printf(
403 "%s: Warning! Bad face in mesh"
404 " \"%s\" at index %d!, skipping\n",
405 __func__,
406 me->id.name + 2,
407 i);
408 continue;
409 }
410
411 /* Don't use 'i' since we may have skipped the face. */
412 BM_elem_index_set(f, bm->totface - 1); /* set_ok */
413
414 /* Transfer flag. */
415 f->head.hflag = BM_face_flag_from_mflag(mp->flag & ~ME_FACE_SEL);
416
417 /* This is necessary for selection counts to work properly. */
418 if (mp->flag & ME_FACE_SEL) {
419 BM_face_select_set(bm, f, true);
420 }
421
422 f->mat_nr = mp->mat_nr;
423 if (i == me->act_face) {
424 bm->act_face = f;
425 }
426
427 int j = mp->loopstart;
428 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
429 do {
430 /* Don't use 'j' since we may have skipped some faces, hence some loops. */
431 BM_elem_index_set(l_iter, totloops++); /* set_ok */
432
433 /* Save index of corresponding #MLoop. */
434 CustomData_to_bmesh_block(&me->ldata, &bm->ldata, j++, &l_iter->head.data, true);
435 } while ((l_iter = l_iter->next) != l_first);
436
437 /* Copy Custom Data */
438 CustomData_to_bmesh_block(&me->pdata, &bm->pdata, i, &f->head.data, true);
439
440 if (params->calc_face_normal) {
441 BM_face_normal_update(f);
442 }
443 }
444 if (is_new) {
445 bm->elem_index_dirty &= ~(BM_FACE | BM_LOOP); /* Added in order, clear dirty flag. */
446 }
447
448 /* -------------------------------------------------------------------- */
449 /* MSelect clears the array elements (avoid adding multiple times).
450 *
451 * Take care to keep this last and not use (v/e/ftable) after this.
452 */
453
454 if (me->mselect && me->totselect != 0) {
455 MSelect *msel;
456 for (i = 0, msel = me->mselect; i < me->totselect; i++, msel++) {
457 BMElem **ele_p;
458 switch (msel->type) {
459 case ME_VSEL:
460 ele_p = (BMElem **)&vtable[msel->index];
461 break;
462 case ME_ESEL:
463 ele_p = (BMElem **)&etable[msel->index];
464 break;
465 case ME_FSEL:
466 ele_p = (BMElem **)&ftable[msel->index];
467 break;
468 default:
469 continue;
470 }
471
472 if (*ele_p != NULL) {
473 BM_select_history_store_notest(bm, *ele_p);
474 *ele_p = NULL;
475 }
476 }
477 }
478 else {
479 BM_select_history_clear(bm);
480 }
481
482 MEM_freeN(vtable);
483 MEM_freeN(etable);
484 if (ftable) {
485 MEM_freeN(ftable);
486 }
487 }
488
489 /**
490 * \brief BMesh -> Mesh
491 */
bm_to_mesh_vertex_map(BMesh * bm,int ototvert)492 static BMVert **bm_to_mesh_vertex_map(BMesh *bm, int ototvert)
493 {
494 const int cd_shape_keyindex_offset = CustomData_get_offset(&bm->vdata, CD_SHAPE_KEYINDEX);
495 BMVert **vertMap = NULL;
496 BMVert *eve;
497 int i = 0;
498 BMIter iter;
499
500 /* Caller needs to ensure this. */
501 BLI_assert(ototvert > 0);
502
503 vertMap = MEM_callocN(sizeof(*vertMap) * ototvert, "vertMap");
504 if (cd_shape_keyindex_offset != -1) {
505 BM_ITER_MESH_INDEX (eve, &iter, bm, BM_VERTS_OF_MESH, i) {
506 const int keyi = BM_ELEM_CD_GET_INT(eve, cd_shape_keyindex_offset);
507 if ((keyi != ORIGINDEX_NONE) && (keyi < ototvert) &&
508 /* Not fool-proof, but chances are if we have many verts with the same index,
509 * we will want to use the first one,
510 * since the second is more likely to be a duplicate. */
511 (vertMap[keyi] == NULL)) {
512 vertMap[keyi] = eve;
513 }
514 }
515 }
516 else {
517 BM_ITER_MESH_INDEX (eve, &iter, bm, BM_VERTS_OF_MESH, i) {
518 if (i < ototvert) {
519 vertMap[i] = eve;
520 }
521 else {
522 break;
523 }
524 }
525 }
526
527 return vertMap;
528 }
529
530 /**
531 * Returns custom-data shapekey index from a keyblock or -1
532 * \note could split this out into a more generic function.
533 */
bm_to_mesh_shape_layer_index_from_kb(BMesh * bm,KeyBlock * currkey)534 static int bm_to_mesh_shape_layer_index_from_kb(BMesh *bm, KeyBlock *currkey)
535 {
536 int i;
537 int j = 0;
538
539 for (i = 0; i < bm->vdata.totlayer; i++) {
540 if (bm->vdata.layers[i].type == CD_SHAPEKEY) {
541 if (currkey->uid == bm->vdata.layers[i].uid) {
542 return j;
543 }
544 j++;
545 }
546 }
547 return -1;
548 }
549
bmesh_quick_edgedraw_flag(MEdge * med,BMEdge * e)550 BLI_INLINE void bmesh_quick_edgedraw_flag(MEdge *med, BMEdge *e)
551 {
552 /* This is a cheap way to set the edge draw, its not precise and will
553 * pick the first 2 faces an edge uses.
554 * The dot comparison is a little arbitrary, but set so that a 5 subd
555 * IcoSphere won't vanish but subd 6 will (as with pre-bmesh Blender). */
556
557 if (/* (med->flag & ME_EDGEDRAW) && */ /* Assume to be true. */
558 (e->l && (e->l != e->l->radial_next)) &&
559 (dot_v3v3(e->l->f->no, e->l->radial_next->f->no) > 0.9995f)) {
560 med->flag &= ~ME_EDGEDRAW;
561 }
562 else {
563 med->flag |= ME_EDGEDRAW;
564 }
565 }
566
567 /**
568 *
569 * \param bmain: May be NULL in case \a calc_object_remap parameter option is not set.
570 */
BM_mesh_bm_to_me(Main * bmain,BMesh * bm,Mesh * me,const struct BMeshToMeshParams * params)571 void BM_mesh_bm_to_me(Main *bmain, BMesh *bm, Mesh *me, const struct BMeshToMeshParams *params)
572 {
573 MEdge *med;
574 BMVert *v, *eve;
575 BMEdge *e;
576 BMFace *f;
577 BMIter iter;
578 int i, j;
579
580 const int cd_vert_bweight_offset = CustomData_get_offset(&bm->vdata, CD_BWEIGHT);
581 const int cd_edge_bweight_offset = CustomData_get_offset(&bm->edata, CD_BWEIGHT);
582 const int cd_edge_crease_offset = CustomData_get_offset(&bm->edata, CD_CREASE);
583 const int cd_shape_keyindex_offset = CustomData_get_offset(&bm->vdata, CD_SHAPE_KEYINDEX);
584
585 MVert *oldverts = NULL;
586 const int ototvert = me->totvert;
587
588 if (me->key && (cd_shape_keyindex_offset != -1)) {
589 /* Keep the old verts in case we are working on* a key, which is done at the end. */
590
591 /* Use the array in-place instead of duplicating the array. */
592 #if 0
593 oldverts = MEM_dupallocN(me->mvert);
594 #else
595 oldverts = me->mvert;
596 me->mvert = NULL;
597 CustomData_update_typemap(&me->vdata);
598 CustomData_set_layer(&me->vdata, CD_MVERT, NULL);
599 #endif
600 }
601
602 /* Free custom data. */
603 CustomData_free(&me->vdata, me->totvert);
604 CustomData_free(&me->edata, me->totedge);
605 CustomData_free(&me->fdata, me->totface);
606 CustomData_free(&me->ldata, me->totloop);
607 CustomData_free(&me->pdata, me->totpoly);
608
609 /* Add new custom data. */
610 me->totvert = bm->totvert;
611 me->totedge = bm->totedge;
612 me->totloop = bm->totloop;
613 me->totpoly = bm->totface;
614 /* Will be overwritten with a valid value if 'dotess' is set, otherwise we
615 * end up with 'me->totface' and me->mface == NULL which can crash T28625. */
616 me->totface = 0;
617 me->act_face = -1;
618
619 {
620 CustomData_MeshMasks mask = CD_MASK_MESH;
621 CustomData_MeshMasks_update(&mask, ¶ms->cd_mask_extra);
622 CustomData_copy(&bm->vdata, &me->vdata, mask.vmask, CD_CALLOC, me->totvert);
623 CustomData_copy(&bm->edata, &me->edata, mask.emask, CD_CALLOC, me->totedge);
624 CustomData_copy(&bm->ldata, &me->ldata, mask.lmask, CD_CALLOC, me->totloop);
625 CustomData_copy(&bm->pdata, &me->pdata, mask.pmask, CD_CALLOC, me->totpoly);
626 }
627
628 MVert *mvert = bm->totvert ? MEM_callocN(sizeof(MVert) * bm->totvert, "bm_to_me.vert") : NULL;
629 MEdge *medge = bm->totedge ? MEM_callocN(sizeof(MEdge) * bm->totedge, "bm_to_me.edge") : NULL;
630 MLoop *mloop = bm->totloop ? MEM_callocN(sizeof(MLoop) * bm->totloop, "bm_to_me.loop") : NULL;
631 MPoly *mpoly = bm->totface ? MEM_callocN(sizeof(MPoly) * bm->totface, "bm_to_me.poly") : NULL;
632
633 CustomData_add_layer(&me->vdata, CD_MVERT, CD_ASSIGN, mvert, me->totvert);
634 CustomData_add_layer(&me->edata, CD_MEDGE, CD_ASSIGN, medge, me->totedge);
635 CustomData_add_layer(&me->ldata, CD_MLOOP, CD_ASSIGN, mloop, me->totloop);
636 CustomData_add_layer(&me->pdata, CD_MPOLY, CD_ASSIGN, mpoly, me->totpoly);
637
638 me->cd_flag = BM_mesh_cd_flag_from_bmesh(bm);
639
640 /* This is called again, 'dotess' arg is used there. */
641 BKE_mesh_update_customdata_pointers(me, 0);
642
643 i = 0;
644 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
645 copy_v3_v3(mvert->co, v->co);
646 normal_float_to_short_v3(mvert->no, v->no);
647
648 mvert->flag = BM_vert_flag_to_mflag(v);
649
650 BM_elem_index_set(v, i); /* set_inline */
651
652 /* Copy over custom-data. */
653 CustomData_from_bmesh_block(&bm->vdata, &me->vdata, v->head.data, i);
654
655 if (cd_vert_bweight_offset != -1) {
656 mvert->bweight = BM_ELEM_CD_GET_FLOAT_AS_UCHAR(v, cd_vert_bweight_offset);
657 }
658
659 i++;
660 mvert++;
661
662 BM_CHECK_ELEMENT(v);
663 }
664 bm->elem_index_dirty &= ~BM_VERT;
665
666 med = medge;
667 i = 0;
668 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
669 med->v1 = BM_elem_index_get(e->v1);
670 med->v2 = BM_elem_index_get(e->v2);
671
672 med->flag = BM_edge_flag_to_mflag(e);
673
674 BM_elem_index_set(e, i); /* set_inline */
675
676 /* Copy over custom-data. */
677 CustomData_from_bmesh_block(&bm->edata, &me->edata, e->head.data, i);
678
679 bmesh_quick_edgedraw_flag(med, e);
680
681 if (cd_edge_crease_offset != -1) {
682 med->crease = BM_ELEM_CD_GET_FLOAT_AS_UCHAR(e, cd_edge_crease_offset);
683 }
684 if (cd_edge_bweight_offset != -1) {
685 med->bweight = BM_ELEM_CD_GET_FLOAT_AS_UCHAR(e, cd_edge_bweight_offset);
686 }
687
688 i++;
689 med++;
690 BM_CHECK_ELEMENT(e);
691 }
692 bm->elem_index_dirty &= ~BM_EDGE;
693
694 i = 0;
695 j = 0;
696 BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
697 BMLoop *l_iter, *l_first;
698 mpoly->loopstart = j;
699 mpoly->totloop = f->len;
700 mpoly->mat_nr = f->mat_nr;
701 mpoly->flag = BM_face_flag_to_mflag(f);
702
703 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
704 do {
705 mloop->e = BM_elem_index_get(l_iter->e);
706 mloop->v = BM_elem_index_get(l_iter->v);
707
708 /* Copy over custom-data. */
709 CustomData_from_bmesh_block(&bm->ldata, &me->ldata, l_iter->head.data, j);
710
711 j++;
712 mloop++;
713 BM_CHECK_ELEMENT(l_iter);
714 BM_CHECK_ELEMENT(l_iter->e);
715 BM_CHECK_ELEMENT(l_iter->v);
716 } while ((l_iter = l_iter->next) != l_first);
717
718 if (f == bm->act_face) {
719 me->act_face = i;
720 }
721
722 /* Copy over custom-data. */
723 CustomData_from_bmesh_block(&bm->pdata, &me->pdata, f->head.data, i);
724
725 i++;
726 mpoly++;
727 BM_CHECK_ELEMENT(f);
728 }
729
730 /* Patch hook indices and vertex parents. */
731 if (params->calc_object_remap && (ototvert > 0)) {
732 BLI_assert(bmain != NULL);
733 Object *ob;
734 ModifierData *md;
735 BMVert **vertMap = NULL;
736
737 for (ob = bmain->objects.first; ob; ob = ob->id.next) {
738 if ((ob->parent) && (ob->parent->data == me) && ELEM(ob->partype, PARVERT1, PARVERT3)) {
739
740 if (vertMap == NULL) {
741 vertMap = bm_to_mesh_vertex_map(bm, ototvert);
742 }
743
744 if (ob->par1 < ototvert) {
745 eve = vertMap[ob->par1];
746 if (eve) {
747 ob->par1 = BM_elem_index_get(eve);
748 }
749 }
750 if (ob->par2 < ototvert) {
751 eve = vertMap[ob->par2];
752 if (eve) {
753 ob->par2 = BM_elem_index_get(eve);
754 }
755 }
756 if (ob->par3 < ototvert) {
757 eve = vertMap[ob->par3];
758 if (eve) {
759 ob->par3 = BM_elem_index_get(eve);
760 }
761 }
762 }
763 if (ob->data == me) {
764 for (md = ob->modifiers.first; md; md = md->next) {
765 if (md->type == eModifierType_Hook) {
766 HookModifierData *hmd = (HookModifierData *)md;
767
768 if (vertMap == NULL) {
769 vertMap = bm_to_mesh_vertex_map(bm, ototvert);
770 }
771
772 for (i = j = 0; i < hmd->totindex; i++) {
773 if (hmd->indexar[i] < ototvert) {
774 eve = vertMap[hmd->indexar[i]];
775
776 if (eve) {
777 hmd->indexar[j++] = BM_elem_index_get(eve);
778 }
779 }
780 else {
781 j++;
782 }
783 }
784
785 hmd->totindex = j;
786 }
787 }
788 }
789 }
790
791 if (vertMap) {
792 MEM_freeN(vertMap);
793 }
794 }
795
796 BKE_mesh_update_customdata_pointers(me, false);
797
798 {
799 BMEditSelection *selected;
800 me->totselect = BLI_listbase_count(&(bm->selected));
801
802 MEM_SAFE_FREE(me->mselect);
803 if (me->totselect != 0) {
804 me->mselect = MEM_mallocN(sizeof(MSelect) * me->totselect, "Mesh selection history");
805 }
806
807 for (i = 0, selected = bm->selected.first; selected; i++, selected = selected->next) {
808 if (selected->htype == BM_VERT) {
809 me->mselect[i].type = ME_VSEL;
810 }
811 else if (selected->htype == BM_EDGE) {
812 me->mselect[i].type = ME_ESEL;
813 }
814 else if (selected->htype == BM_FACE) {
815 me->mselect[i].type = ME_FSEL;
816 }
817
818 me->mselect[i].index = BM_elem_index_get(selected->ele);
819 }
820 }
821
822 /* See comment below, this logic is in twice. */
823
824 if (me->key) {
825 KeyBlock *currkey;
826 KeyBlock *actkey = BLI_findlink(&me->key->block, bm->shapenr - 1);
827
828 float(*ofs)[3] = NULL;
829
830 /* Go through and find any shape-key custom-data layers
831 * that might not have corresponding KeyBlocks, and add them if necessary. */
832 for (i = 0; i < bm->vdata.totlayer; i++) {
833 if (bm->vdata.layers[i].type != CD_SHAPEKEY) {
834 continue;
835 }
836
837 for (currkey = me->key->block.first; currkey; currkey = currkey->next) {
838 if (currkey->uid == bm->vdata.layers[i].uid) {
839 break;
840 }
841 }
842
843 if (!currkey) {
844 currkey = BKE_keyblock_add(me->key, bm->vdata.layers[i].name);
845 currkey->uid = bm->vdata.layers[i].uid;
846 }
847 }
848
849 /* Editing the base key should update others. */
850 if (/* Only need offsets for relative shape keys. */
851 (me->key->type == KEY_RELATIVE) &&
852
853 /* Unlikely, but the active key may not be valid if the
854 * BMesh and the mesh are out of sync. */
855 (actkey != NULL) &&
856
857 /* Not used here, but 'oldverts' is used later for applying 'ofs'. */
858 (oldverts != NULL) &&
859
860 /* Needed for referencing oldverts. */
861 (cd_shape_keyindex_offset != -1)) {
862
863 const bool act_is_basis = BKE_keyblock_is_basis(me->key, bm->shapenr - 1);
864
865 /* Active key is a base. */
866 if (act_is_basis) {
867 const float(*fp)[3] = actkey->data;
868
869 ofs = MEM_callocN(sizeof(float[3]) * bm->totvert, "currkey->data");
870 mvert = me->mvert;
871 BM_ITER_MESH_INDEX (eve, &iter, bm, BM_VERTS_OF_MESH, i) {
872 const int keyi = BM_ELEM_CD_GET_INT(eve, cd_shape_keyindex_offset);
873
874 /* Could use 'eve->co' or 'mvert->co', they're the same at this point. */
875 if (keyi != ORIGINDEX_NONE && keyi < actkey->totelem) {
876 sub_v3_v3v3(ofs[i], mvert->co, fp[keyi]);
877 }
878 else {
879 /* If there are new vertices in the mesh, we can't propagate the offset
880 * because it will only work for the existing vertices and not the new
881 * ones, creating a mess when doing e.g. subdivide + translate. */
882 MEM_freeN(ofs);
883 ofs = NULL;
884 break;
885 }
886
887 mvert++;
888 }
889 }
890 }
891
892 for (currkey = me->key->block.first; currkey; currkey = currkey->next) {
893 int keyi;
894 const float(*ofs_pt)[3] = ofs;
895 float *newkey, (*oldkey)[3], *fp;
896
897 const int currkey_uuid = bm_to_mesh_shape_layer_index_from_kb(bm, currkey);
898 const int cd_shape_offset = (currkey_uuid == -1) ? -1 :
899 CustomData_get_n_offset(&bm->vdata,
900 CD_SHAPEKEY,
901 currkey_uuid);
902 const bool apply_offset = (cd_shape_offset != -1) && (ofs != NULL) && (currkey != actkey) &&
903 (bm->shapenr - 1 == currkey->relative);
904
905 fp = newkey = MEM_callocN(me->key->elemsize * bm->totvert, "currkey->data");
906 oldkey = currkey->data;
907
908 mvert = me->mvert;
909 BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
910
911 if (currkey == actkey) {
912 copy_v3_v3(fp, eve->co);
913
914 if (actkey != me->key->refkey) { /* Important see bug T30771. */
915 if (cd_shape_keyindex_offset != -1) {
916 if (oldverts) {
917 keyi = BM_ELEM_CD_GET_INT(eve, cd_shape_keyindex_offset);
918 if (keyi != ORIGINDEX_NONE && keyi < currkey->totelem) { /* Valid old vertex. */
919 copy_v3_v3(mvert->co, oldverts[keyi].co);
920 }
921 }
922 }
923 }
924 }
925 else if (cd_shape_offset != -1) {
926 /* In most cases this runs. */
927 copy_v3_v3(fp, BM_ELEM_CD_GET_VOID_P(eve, cd_shape_offset));
928 }
929 else if ((oldkey != NULL) && (cd_shape_keyindex_offset != -1) &&
930 ((keyi = BM_ELEM_CD_GET_INT(eve, cd_shape_keyindex_offset)) != ORIGINDEX_NONE) &&
931 (keyi < currkey->totelem)) {
932 /* Old method of reconstructing keys via vertices original key indices,
933 * currently used if the new method above fails
934 * (which is theoretically possible in certain cases of undo). */
935 copy_v3_v3(fp, oldkey[keyi]);
936 }
937 else {
938 /* Fail! fill in with dummy value. */
939 copy_v3_v3(fp, mvert->co);
940 }
941
942 /* Propagate edited basis offsets to other shapes. */
943 if (apply_offset) {
944 add_v3_v3(fp, *ofs_pt++);
945 /* Apply back new coordinates shape-keys that have offset into BMesh.
946 * Otherwise, in case we call again #BM_mesh_bm_to_me on same BMesh,
947 * we'll apply diff from previous call to #BM_mesh_bm_to_me,
948 * to shape-key values from *original creation of the BMesh*. See T50524. */
949 copy_v3_v3(BM_ELEM_CD_GET_VOID_P(eve, cd_shape_offset), fp);
950 }
951
952 fp += 3;
953 mvert++;
954 }
955
956 currkey->totelem = bm->totvert;
957 if (currkey->data) {
958 MEM_freeN(currkey->data);
959 }
960 currkey->data = newkey;
961 }
962
963 if (ofs) {
964 MEM_freeN(ofs);
965 }
966 }
967
968 /* Run this even when shape keys aren't used since it may be used for hooks or vertex parents. */
969 if (params->update_shapekey_indices) {
970 /* We have written a new shape key, if this mesh is _not_ going to be freed,
971 * update the shape key indices to match the newly updated. */
972 if (cd_shape_keyindex_offset != -1) {
973 BM_ITER_MESH_INDEX (eve, &iter, bm, BM_VERTS_OF_MESH, i) {
974 BM_ELEM_CD_SET_INT(eve, cd_shape_keyindex_offset, i);
975 }
976 }
977 }
978
979 if (oldverts != NULL) {
980 MEM_freeN(oldverts);
981 }
982
983 /* Topology could be changed, ensure #CD_MDISPS are ok. */
984 multires_topology_changed(me);
985
986 /* To be removed as soon as COW is enabled by default.. */
987 BKE_mesh_runtime_clear_geometry(me);
988 }
989
990 /**
991 * A version of #BM_mesh_bm_to_me intended for getting the mesh
992 * to pass to the modifier stack for evaluation,
993 * instead of mode switching (where we make sure all data is kept
994 * and do expensive lookups to maintain shape keys).
995 *
996 * Key differences:
997 *
998 * - Don't support merging with existing mesh.
999 * - Ignore shape-keys.
1000 * - Ignore vertex-parents.
1001 * - Ignore selection history.
1002 * - Uses simpler method to calculate #ME_EDGEDRAW
1003 * - Uses #CD_MASK_DERIVEDMESH instead of #CD_MASK_MESH.
1004 *
1005 * \note Was `cddm_from_bmesh_ex` in 2.7x, removed `MFace` support.
1006 */
BM_mesh_bm_to_me_for_eval(BMesh * bm,Mesh * me,const CustomData_MeshMasks * cd_mask_extra)1007 void BM_mesh_bm_to_me_for_eval(BMesh *bm, Mesh *me, const CustomData_MeshMasks *cd_mask_extra)
1008 {
1009 /* Must be an empty mesh. */
1010 BLI_assert(me->totvert == 0);
1011 BLI_assert(cd_mask_extra == NULL || (cd_mask_extra->vmask & CD_MASK_SHAPEKEY) == 0);
1012
1013 me->totvert = bm->totvert;
1014 me->totedge = bm->totedge;
1015 me->totface = 0;
1016 me->totloop = bm->totloop;
1017 me->totpoly = bm->totface;
1018
1019 CustomData_add_layer(&me->vdata, CD_ORIGINDEX, CD_CALLOC, NULL, bm->totvert);
1020 CustomData_add_layer(&me->edata, CD_ORIGINDEX, CD_CALLOC, NULL, bm->totedge);
1021 CustomData_add_layer(&me->pdata, CD_ORIGINDEX, CD_CALLOC, NULL, bm->totface);
1022
1023 CustomData_add_layer(&me->vdata, CD_MVERT, CD_CALLOC, NULL, bm->totvert);
1024 CustomData_add_layer(&me->edata, CD_MEDGE, CD_CALLOC, NULL, bm->totedge);
1025 CustomData_add_layer(&me->ldata, CD_MLOOP, CD_CALLOC, NULL, bm->totloop);
1026 CustomData_add_layer(&me->pdata, CD_MPOLY, CD_CALLOC, NULL, bm->totface);
1027
1028 /* Don't process shape-keys, we only feed them through the modifier stack as needed,
1029 * e.g. for applying modifiers or the like. */
1030 CustomData_MeshMasks mask = CD_MASK_DERIVEDMESH;
1031 if (cd_mask_extra != NULL) {
1032 CustomData_MeshMasks_update(&mask, cd_mask_extra);
1033 }
1034 mask.vmask &= ~CD_MASK_SHAPEKEY;
1035 CustomData_merge(&bm->vdata, &me->vdata, mask.vmask, CD_CALLOC, me->totvert);
1036 CustomData_merge(&bm->edata, &me->edata, mask.emask, CD_CALLOC, me->totedge);
1037 CustomData_merge(&bm->ldata, &me->ldata, mask.lmask, CD_CALLOC, me->totloop);
1038 CustomData_merge(&bm->pdata, &me->pdata, mask.pmask, CD_CALLOC, me->totpoly);
1039
1040 BKE_mesh_update_customdata_pointers(me, false);
1041
1042 BMIter iter;
1043 BMVert *eve;
1044 BMEdge *eed;
1045 BMFace *efa;
1046 MVert *mvert = me->mvert;
1047 MEdge *medge = me->medge;
1048 MLoop *mloop = me->mloop;
1049 MPoly *mpoly = me->mpoly;
1050 int *index, add_orig;
1051 unsigned int i, j;
1052
1053 const int cd_vert_bweight_offset = CustomData_get_offset(&bm->vdata, CD_BWEIGHT);
1054 const int cd_edge_bweight_offset = CustomData_get_offset(&bm->edata, CD_BWEIGHT);
1055 const int cd_edge_crease_offset = CustomData_get_offset(&bm->edata, CD_CREASE);
1056
1057 me->runtime.deformed_only = true;
1058
1059 /* Don't add origindex layer if one already exists. */
1060 add_orig = !CustomData_has_layer(&bm->pdata, CD_ORIGINDEX);
1061
1062 index = CustomData_get_layer(&me->vdata, CD_ORIGINDEX);
1063
1064 BM_ITER_MESH_INDEX (eve, &iter, bm, BM_VERTS_OF_MESH, i) {
1065 MVert *mv = &mvert[i];
1066
1067 copy_v3_v3(mv->co, eve->co);
1068
1069 BM_elem_index_set(eve, i); /* set_inline */
1070
1071 normal_float_to_short_v3(mv->no, eve->no);
1072
1073 mv->flag = BM_vert_flag_to_mflag(eve);
1074
1075 if (cd_vert_bweight_offset != -1) {
1076 mv->bweight = BM_ELEM_CD_GET_FLOAT_AS_UCHAR(eve, cd_vert_bweight_offset);
1077 }
1078
1079 if (add_orig) {
1080 *index++ = i;
1081 }
1082
1083 CustomData_from_bmesh_block(&bm->vdata, &me->vdata, eve->head.data, i);
1084 }
1085 bm->elem_index_dirty &= ~BM_VERT;
1086
1087 index = CustomData_get_layer(&me->edata, CD_ORIGINDEX);
1088 BM_ITER_MESH_INDEX (eed, &iter, bm, BM_EDGES_OF_MESH, i) {
1089 MEdge *med = &medge[i];
1090
1091 BM_elem_index_set(eed, i); /* set_inline */
1092
1093 med->v1 = BM_elem_index_get(eed->v1);
1094 med->v2 = BM_elem_index_get(eed->v2);
1095
1096 med->flag = BM_edge_flag_to_mflag(eed);
1097
1098 /* Handle this differently to editmode switching,
1099 * only enable draw for single user edges rather than calculating angle. */
1100 if ((med->flag & ME_EDGEDRAW) == 0) {
1101 if (eed->l && eed->l == eed->l->radial_next) {
1102 med->flag |= ME_EDGEDRAW;
1103 }
1104 }
1105
1106 if (cd_edge_crease_offset != -1) {
1107 med->crease = BM_ELEM_CD_GET_FLOAT_AS_UCHAR(eed, cd_edge_crease_offset);
1108 }
1109 if (cd_edge_bweight_offset != -1) {
1110 med->bweight = BM_ELEM_CD_GET_FLOAT_AS_UCHAR(eed, cd_edge_bweight_offset);
1111 }
1112
1113 CustomData_from_bmesh_block(&bm->edata, &me->edata, eed->head.data, i);
1114 if (add_orig) {
1115 *index++ = i;
1116 }
1117 }
1118 bm->elem_index_dirty &= ~BM_EDGE;
1119
1120 index = CustomData_get_layer(&me->pdata, CD_ORIGINDEX);
1121 j = 0;
1122 BM_ITER_MESH_INDEX (efa, &iter, bm, BM_FACES_OF_MESH, i) {
1123 BMLoop *l_iter;
1124 BMLoop *l_first;
1125 MPoly *mp = &mpoly[i];
1126
1127 BM_elem_index_set(efa, i); /* set_inline */
1128
1129 mp->totloop = efa->len;
1130 mp->flag = BM_face_flag_to_mflag(efa);
1131 mp->loopstart = j;
1132 mp->mat_nr = efa->mat_nr;
1133
1134 l_iter = l_first = BM_FACE_FIRST_LOOP(efa);
1135 do {
1136 mloop->v = BM_elem_index_get(l_iter->v);
1137 mloop->e = BM_elem_index_get(l_iter->e);
1138 CustomData_from_bmesh_block(&bm->ldata, &me->ldata, l_iter->head.data, j);
1139
1140 BM_elem_index_set(l_iter, j); /* set_inline */
1141
1142 j++;
1143 mloop++;
1144 } while ((l_iter = l_iter->next) != l_first);
1145
1146 CustomData_from_bmesh_block(&bm->pdata, &me->pdata, efa->head.data, i);
1147
1148 if (add_orig) {
1149 *index++ = i;
1150 }
1151 }
1152 bm->elem_index_dirty &= ~(BM_FACE | BM_LOOP);
1153
1154 me->cd_flag = BM_mesh_cd_flag_from_bmesh(bm);
1155 }
1156