4f6db9056e59b994b2566efcf08c111424a7266d
[blender.git] / source / blender / bmesh / operators / bmo_mesh_conv.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Joseph Eagar.
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 #include "MEM_guardedalloc.h"
24
25 #include "DNA_mesh_types.h"
26 #include "DNA_meshdata_types.h"
27 #include "DNA_object_types.h"
28 #include "DNA_key_types.h"
29 #include "DNA_modifier_types.h"
30
31 #include "BKE_mesh.h"
32 #include "BLI_listbase.h"
33 #include "BKE_global.h"
34 #include "BKE_key.h"
35 #include "BKE_main.h"
36 #include "BKE_customdata.h"
37
38 #include "BLI_math.h"
39 #include "BLI_array.h"
40
41 #include "bmesh.h"
42 #include "bmesh_private.h"
43
44 #include "bmesh_operators_private.h" /* own include */
45
46 /*
47  * MESH CONV.C
48  *
49  * This file contains functions
50  * for converting a Mesh
51  * into a Bmesh, and back again.
52  *
53  */
54
55 void mesh_to_bmesh_exec(BMesh *bm, BMOperator *op)
56 {
57         Object *ob = BMO_slot_ptr_get(op, "object");
58         Mesh *me = BMO_slot_ptr_get(op, "mesh");
59         MVert *mvert;
60         BLI_array_declare(verts);
61         MEdge *medge;
62         MLoop *ml;
63         MPoly *mpoly;
64         KeyBlock *actkey, *block;
65         BMVert *v, **vt = NULL, **verts = NULL;
66         BMEdge *e, **fedges = NULL, **et = NULL;
67         BMFace *f;
68         BMLoop *l;
69         BLI_array_declare(fedges);
70         float (*keyco)[3] = NULL;
71         int *keyi;
72         int set_key = BMO_slot_int_get(op, "set_shapekey");
73         int totuv, i, j;
74
75         if (!me || !me->totvert) {
76                 return; /* sanity check */
77         }
78         
79         vt = MEM_mallocN(sizeof(void **) * me->totvert, "mesh to bmesh vtable");
80
81         CustomData_copy(&me->vdata, &bm->vdata, CD_MASK_BMESH, CD_CALLOC, 0);
82         CustomData_copy(&me->edata, &bm->edata, CD_MASK_BMESH, CD_CALLOC, 0);
83         CustomData_copy(&me->ldata, &bm->ldata, CD_MASK_BMESH, CD_CALLOC, 0);
84         CustomData_copy(&me->pdata, &bm->pdata, CD_MASK_BMESH, CD_CALLOC, 0);
85         
86         /* make sure uv layer names are consisten */
87         totuv = CustomData_number_of_layers(&bm->pdata, CD_MTEXPOLY);
88         for (i = 0; i < totuv; i++) {
89                 int li = CustomData_get_layer_index_n(&bm->pdata, CD_MTEXPOLY, i);
90                 CustomData_set_layer_name(&bm->ldata, CD_MLOOPUV, i, bm->pdata.layers[li].name);
91         }
92         
93         if (!CustomData_has_layer(&bm->edata, CD_CREASE))
94                 CustomData_add_layer(&bm->edata, CD_CREASE, CD_ASSIGN, NULL, 0);
95
96         if (!CustomData_has_layer(&bm->edata, CD_BWEIGHT))
97                 CustomData_add_layer(&bm->edata, CD_BWEIGHT, CD_ASSIGN, NULL, 0);
98
99         if (!CustomData_has_layer(&bm->vdata, CD_BWEIGHT))
100                 CustomData_add_layer(&bm->vdata, CD_BWEIGHT, CD_ASSIGN, NULL, 0);
101
102
103         if (me->key && ob->shapenr > me->key->totkey) {
104                 ob->shapenr = me->key->totkey - 1;
105         }
106
107         actkey = ob_get_keyblock(ob);
108         if (actkey && actkey->totelem == me->totvert) {
109                 CustomData_add_layer(&bm->vdata, CD_SHAPE_KEYINDEX, CD_ASSIGN, NULL, 0);
110                 
111                 /* check if we need to generate unique ids for the shapekeys.
112                  * this also exists in the file reading code, but is here for
113                  * a sanity chec */
114                 if (!me->key->uidgen) {
115                         fprintf(stderr,
116                                 "%s had to generate shape key uid's in a situation we shouldn't need to! "
117                                 "(bmesh internal error)\n",
118                                 __func__);
119
120                         me->key->uidgen = 1;
121                         for (block = me->key->block.first; block; block = block->next) {
122                                 block->uid = me->key->uidgen++;
123                         }
124                 }
125
126                 keyco = actkey->data;
127                 bm->shapenr = ob->shapenr;
128                 for (i = 0, block = me->key->block.first; block; block = block->next, i++) {
129                         CustomData_add_layer_named(&bm->vdata, CD_SHAPEKEY,
130                                                    CD_ASSIGN, NULL, 0, block->name);
131                         
132                         j = CustomData_get_layer_index_n(&bm->vdata, CD_SHAPEKEY, i);
133                         bm->vdata.layers[j].uid = block->uid;
134                 }
135         }
136         else if (actkey) {
137                 printf("shapekey<->mesh mismatch!\n");
138         }
139         
140         CustomData_bmesh_init_pool(&bm->vdata, bm_mesh_allocsize_default[0]);
141         CustomData_bmesh_init_pool(&bm->edata, bm_mesh_allocsize_default[1]);
142         CustomData_bmesh_init_pool(&bm->ldata, bm_mesh_allocsize_default[2]);
143         CustomData_bmesh_init_pool(&bm->pdata, bm_mesh_allocsize_default[3]);
144
145         for (i = 0, mvert = me->mvert; i < me->totvert; i++, mvert++) {
146                 v = BM_vert_create(bm, keyco && set_key ? keyco[i] : mvert->co, NULL);
147                 BM_elem_index_set(v, i); /* set_ok */
148                 vt[i] = v;
149
150                 /* transfer flag */
151                 v->head.hflag = BM_vert_flag_from_mflag(mvert->flag);
152
153                 /* this is necassary for selection counts to work properl */
154                 if (BM_elem_flag_test(v, BM_ELEM_SELECT)) BM_vert_select_set(bm, v, TRUE);
155
156                 normal_short_to_float_v3(v->no, mvert->no);
157
158                 BM_elem_float_data_set(&bm->vdata, v, CD_BWEIGHT, (float)mvert->bweight / 255.0f);
159
160                 /* Copy Custom Dat */
161                 CustomData_to_bmesh_block(&me->vdata, &bm->vdata, i, &v->head.data);
162
163                 /* set shapekey dat */
164                 if (me->key) {
165                         /* set shape key original inde */
166                         keyi = CustomData_bmesh_get(&bm->vdata, v->head.data, CD_SHAPE_KEYINDEX);
167                         if (keyi) {
168                                 *keyi = i;
169                         }
170                         
171                         for (block = me->key->block.first, j = 0; block; block = block->next, j++) {
172                                 float *co = CustomData_bmesh_get_n(&bm->vdata, v->head.data, CD_SHAPEKEY, j);
173
174                                 if (co) {
175                                         copy_v3_v3(co, ((float *)block->data) + 3 * i);
176                                 }
177                         }
178                 }
179         }
180
181         bm->elem_index_dirty &= ~BM_VERT; /* added in order, clear dirty flag */
182
183         if (!me->totedge) {
184                 MEM_freeN(vt);
185                 return;
186         }
187
188         et = MEM_mallocN(sizeof(void **) * me->totedge, "mesh to bmesh etable");
189
190         medge = me->medge;
191         for (i = 0; i < me->totedge; i++, medge++) {
192                 e = BM_edge_create(bm, vt[medge->v1], vt[medge->v2], NULL, FALSE);
193                 BM_elem_index_set(e, i); /* set_ok */
194                 et[i] = e;
195
196                 /* transfer flags */
197                 e->head.hflag = BM_edge_flag_from_mflag(medge->flag);
198
199                 /* this is necassary for selection counts to work properly */
200                 if (BM_elem_flag_test(e, BM_ELEM_SELECT)) BM_elem_select_set(bm, e, TRUE);
201                 
202                 /* Copy Custom Dat */
203                 CustomData_to_bmesh_block(&me->edata, &bm->edata, i, &e->head.data);
204                 
205                 BM_elem_float_data_set(&bm->edata, e, CD_CREASE, (float)medge->crease / 255.0f);
206                 BM_elem_float_data_set(&bm->edata, e, CD_BWEIGHT, (float)medge->bweight / 255.0f);
207         }
208
209         bm->elem_index_dirty &= ~BM_EDGE; /* added in order, clear dirty flag */
210
211         mpoly = me->mpoly;
212         for (i = 0; i < me->totpoly; i++, mpoly++) {
213                 BMIter iter;
214
215                 BLI_array_empty(fedges);
216                 BLI_array_empty(verts);
217
218                 BLI_array_growitems(fedges, mpoly->totloop);
219                 BLI_array_growitems(verts, mpoly->totloop);
220
221                 for (j = 0; j < mpoly->totloop; j++) {
222                         ml = &me->mloop[mpoly->loopstart + j];
223                         v = vt[ml->v];
224                         e = et[ml->e];
225
226                         fedges[j] = e;
227                         verts[j] = v;
228                 }
229                 
230                 /* not sure what this block is supposed to do,
231                  * but its unused. so commenting - campbell */
232 #if 0
233                 {
234                         BMVert *v1, *v2;
235                         v1 = vt[me->mloop[mpoly->loopstart].v];
236                         v2 = vt[me->mloop[mpoly->loopstart + 1].v];
237
238                         if (v1 == fedges[0]->v1) {
239                                 v2 = fedges[0]->v2;
240                         }
241                         else {
242                                 v1 = fedges[0]->v2;
243                                 v2 = fedges[0]->v1;
244                         }
245                 }
246 #endif
247
248                 f = BM_face_create(bm, verts, fedges, mpoly->totloop, FALSE);
249
250                 if (!f) {
251                         printf("%s: Warning! Bad face in mesh"
252                                " \"%s\" at index %d!, skipping\n",
253                                __func__, me->id.name + 2, i);
254                         continue;
255                 }
256
257                 /* dont use 'i' since we may have skipped the face */
258                 BM_elem_index_set(f, bm->totface - 1); /* set_ok */
259
260                 /* transfer flag */
261                 f->head.hflag = BM_face_flag_from_mflag(mpoly->flag);
262
263                 /* this is necassary for selection counts to work properl */
264                 if (BM_elem_flag_test(f, BM_ELEM_SELECT)) BM_elem_select_set(bm, f, TRUE);
265
266                 f->mat_nr = mpoly->mat_nr;
267                 if (i == me->act_face) bm->act_face = f;
268
269                 j = 0;
270                 BM_ITER_INDEX(l, &iter, bm, BM_LOOPS_OF_FACE, f, j) {
271                         /* Save index of correspsonding MLoop */
272                         BM_elem_index_set(l, mpoly->loopstart + j); /* set_loop */
273                 }
274
275                 /* Copy Custom Dat */
276                 CustomData_to_bmesh_block(&me->pdata, &bm->pdata, i, &f->head.data);
277         }
278
279         bm->elem_index_dirty &= ~BM_FACE; /* added in order, clear dirty flag */
280
281         {
282                 BMIter fiter;
283                 BMIter liter;
284                 
285                 /* Copy over loop CustomData. Doing this in a separate loop isn't necessary
286                  * but is an optimization, to avoid copying a bunch of interpolated customdata
287                  * for each BMLoop (from previous BMLoops using the same edge), always followed
288                  * by freeing the interpolated data and overwriting it with data from the Mesh. */
289                 BM_ITER(f, &fiter, bm, BM_FACES_OF_MESH, NULL) {
290                         BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
291                                 int li = BM_elem_index_get(l);
292                                 CustomData_to_bmesh_block(&me->ldata, &bm->ldata, li, &l->head.data);
293                                 BM_elem_index_set(l, 0); /* set_loop */
294                         }
295                 }
296         }
297
298         if (me->mselect && me->totselect != 0) {
299                 BMIter iter;
300                 BMVert *vertex;
301                 BMEdge *edge;
302                 BMFace *face;
303                 BMVert **vertex_array = MEM_callocN(sizeof(BMVert *) * bm->totvert,
304                                                     "Selection Conversion Vertex Pointer Array");
305                 BMEdge **edge_array = MEM_callocN(sizeof(BMEdge *) * bm->totedge,
306                                                   "Selection Conversion Edge Pointer Array");
307                 BMFace **face_array = MEM_callocN(sizeof(BMFace *) * bm->totface,
308                                                   "Selection Conversion Face Pointer Array");
309
310                 for (i = 0, vertex = BM_iter_new(&iter, bm, BM_VERTS_OF_MESH, NULL);
311                      vertex;
312                      i++, vertex = BM_iter_step(&iter))
313                 {
314                         vertex_array[i] = vertex;
315                 }
316
317                 for (i = 0, edge = BM_iter_new(&iter, bm, BM_EDGES_OF_MESH, NULL);
318                      edge;
319                      i++, edge = BM_iter_step(&iter))
320                 {
321                         edge_array[i] = edge;
322                 }
323
324                 for (i = 0, face = BM_iter_new(&iter, bm, BM_FACES_OF_MESH, NULL);
325                      face;
326                      i++, face = BM_iter_step(&iter))
327                 {
328                         face_array[i] = face;
329                 }
330
331                 if (me->mselect) {
332                         for (i = 0; i < me->totselect; i++) {
333                                 if (me->mselect[i].type == ME_VSEL) {
334                                         BM_select_history_store(bm, vertex_array[me->mselect[i].index]);
335                                 }
336                                 else if (me->mselect[i].type == ME_ESEL) {
337                                         BM_select_history_store(bm, edge_array[me->mselect[i].index]);
338                                 }
339                                 else if (me->mselect[i].type == ME_FSEL) {
340                                         BM_select_history_store(bm, face_array[me->mselect[i].index]);
341                                 }
342                         }
343                 }
344                 else {
345                         me->totselect = 0;
346                 }
347
348                 MEM_freeN(vertex_array);
349                 MEM_freeN(edge_array);
350                 MEM_freeN(face_array);
351         }
352         else {
353                 me->totselect = 0;
354                 if (me->mselect) {
355                         MEM_freeN(me->mselect);
356                         me->mselect = NULL;
357                 }
358         }
359
360         BLI_array_free(fedges);
361         BLI_array_free(verts);
362         
363         MEM_freeN(vt);
364         MEM_freeN(et);
365 }
366
367 void object_load_bmesh_exec(BMesh *bm, BMOperator *op)
368 {
369         Object *ob = BMO_slot_ptr_get(op, "object");
370         /* Scene *scene = BMO_slot_ptr_get(op, "scene"); */
371         Mesh *me = ob->data;
372
373         BMO_op_callf(bm, "bmesh_to_mesh mesh=%p object=%p notesselation=%i", me, ob, TRUE);
374 }
375
376
377 static BMVert **bmesh_to_mesh_vertex_map(BMesh *bm, int ototvert)
378 {
379         BMVert **vertMap = NULL;
380         BMVert *eve;
381         int index;
382         int i = 0;
383         BMIter iter;
384
385         /* caller needs to ensure this */
386         BLI_assert(ototvert > 0);
387
388         vertMap = MEM_callocN(sizeof(*vertMap)*ototvert, "vertMap");
389         if (CustomData_has_layer(&bm->vdata, CD_SHAPE_KEYINDEX)) {
390                 int *keyi;
391                 BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL) {
392                         keyi = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_SHAPE_KEYINDEX);
393                         if (keyi) {
394                                 if (((index = *keyi) != ORIGINDEX_NONE) && (index < ototvert)) {
395                                         vertMap[index] = eve;
396                                 }
397                         }
398                         else {
399                                 if (i < ototvert) {
400                                         vertMap[i] = eve;
401                                 }
402                         }
403                         i++;
404                 }
405         }
406         else {
407                 BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL) {
408                         if (i < ototvert) {
409                                 vertMap[i] = eve;
410                         }
411                         else {
412                                 break;
413                         }
414                         i++;
415                 }
416         }
417
418         return vertMap;
419 }
420
421 BM_INLINE void bmesh_quick_edgedraw_flag(MEdge *med, BMEdge *e)
422 {
423         /* this is a cheap way to set the edge draw, its not precise and will
424          * pick the first 2 faces an edge uses */
425
426
427         if ( /* (med->flag & ME_EDGEDRAW) && */ /* assume to be true */
428              (e->l && (e->l != e->l->radial_next)) &&
429              (dot_v3v3(e->l->f->no, e->l->radial_next->f->no) > 0.998f))
430         {
431                 med->flag &= ~ME_EDGEDRAW;
432         }
433 }
434
435
436 void bmesh_to_mesh_exec(BMesh *bm, BMOperator *op)
437 {
438         Mesh *me = BMO_slot_ptr_get(op, "mesh");
439         /* Object *ob = BMO_slot_ptr_get(op, "object"); */
440         MLoop *mloop;
441         MPoly *mpoly;
442         MVert *mvert, *oldverts;
443         MEdge *med, *medge;
444         BMVert *v, *eve;
445         BMEdge *e;
446         BMLoop *l;
447         BMFace *f;
448         BMIter iter, liter;
449         int i, j, *keyi, ototvert, totloop;
450         int dotess = !BMO_slot_int_get(op, "notesselation");
451         
452         ototvert = me->totvert;
453
454         /* new Vertex block */
455         if (bm->totvert == 0) mvert = NULL;
456         else mvert = MEM_callocN(bm->totvert * sizeof(MVert), "loadeditbMesh vert");
457
458         /* new Edge block */
459         if (bm->totedge == 0) medge = NULL;
460         else medge = MEM_callocN(bm->totedge * sizeof(MEdge), "loadeditbMesh edge");
461         
462         /* build ngon dat */
463         /* new Ngon Face block */
464         if (bm->totface == 0) mpoly = NULL;
465         else mpoly = MEM_callocN(bm->totface * sizeof(MPoly), "loadeditbMesh poly");
466         
467         /* find number of loops to allocat */
468         totloop = 0;
469         BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
470                 totloop += f->len;
471         }
472
473         if (totloop == 0) mloop = NULL;
474         else mloop = MEM_callocN(totloop * sizeof(MLoop), "loadeditbMesh loop");
475
476         /* lets save the old verts just in case we are actually working on
477          * a key ... we now do processing of the keys at the end */
478         oldverts = me->mvert;
479
480         /* don't free this yet */
481         CustomData_set_layer(&me->vdata, CD_MVERT, NULL);
482
483         /* free custom data */
484         CustomData_free(&me->vdata, me->totvert);
485         CustomData_free(&me->edata, me->totedge);
486         CustomData_free(&me->fdata, me->totface);
487         CustomData_free(&me->ldata, me->totloop);
488         CustomData_free(&me->pdata, me->totpoly);
489
490         /* add new custom data */
491         me->totvert = bm->totvert;
492         me->totedge = bm->totedge;
493         me->totloop = totloop;
494         me->totpoly = bm->totface;
495         /* will be overwritten with a valid value if 'dotess' is set, otherwise we
496          * end up with 'me->totface' and me->mface == NULL which can crash [#28625]
497          */
498         me->totface = 0;
499
500         CustomData_copy(&bm->vdata, &me->vdata, CD_MASK_MESH, CD_CALLOC, me->totvert);
501         CustomData_copy(&bm->edata, &me->edata, CD_MASK_MESH, CD_CALLOC, me->totedge);
502         CustomData_copy(&bm->ldata, &me->ldata, CD_MASK_MESH, CD_CALLOC, me->totloop);
503         CustomData_copy(&bm->pdata, &me->pdata, CD_MASK_MESH, CD_CALLOC, me->totpoly);
504
505         CustomData_add_layer(&me->vdata, CD_MVERT, CD_ASSIGN, mvert, me->totvert);
506         CustomData_add_layer(&me->edata, CD_MEDGE, CD_ASSIGN, medge, me->totedge);
507         CustomData_add_layer(&me->ldata, CD_MLOOP, CD_ASSIGN, mloop, me->totloop);
508         CustomData_add_layer(&me->pdata, CD_MPOLY, CD_ASSIGN, mpoly, me->totpoly);
509
510         /* this is called again, 'dotess' arg is used there */
511         mesh_update_customdata_pointers(me, 0);
512         
513         i = 0;
514         BM_ITER(v, &iter, bm, BM_VERTS_OF_MESH, NULL) {
515                 float *bweight = CustomData_bmesh_get(&bm->vdata, v->head.data, CD_BWEIGHT);
516
517                 mvert->bweight = bweight ? (char)((*bweight) * 255) : 0;
518
519                 copy_v3_v3(mvert->co, v->co);
520                 normal_float_to_short_v3(mvert->no, v->no);
521                 
522                 mvert->flag = BM_vert_flag_to_mflag(v);
523
524                 BM_elem_index_set(v, i); /* set_inline */
525
526                 /* copy over customdat */
527                 CustomData_from_bmesh_block(&bm->vdata, &me->vdata, v->head.data, i);
528
529                 i++;
530                 mvert++;
531
532                 BM_CHECK_ELEMENT(bm, v);
533         }
534         bm->elem_index_dirty &= ~BM_VERT;
535
536         med = medge;
537         i = 0;
538         BM_ITER(e, &iter, bm, BM_EDGES_OF_MESH, NULL) {
539                 float *crease = CustomData_bmesh_get(&bm->edata, e->head.data, CD_CREASE);
540                 float *bweight = CustomData_bmesh_get(&bm->edata, e->head.data, CD_BWEIGHT);
541                 
542                 med->v1 = BM_elem_index_get(e->v1);
543                 med->v2 = BM_elem_index_get(e->v2);
544                 med->crease = crease ? (char)((*crease) * 255) : 0;
545                 med->bweight = bweight ? (char)((*bweight) * 255) : 0;
546                 
547                 med->flag = BM_edge_flag_to_mflag(e);
548                 
549                 BM_elem_index_set(e, i); /* set_inline */
550
551                 /* copy over customdat */
552                 CustomData_from_bmesh_block(&bm->edata, &me->edata, e->head.data, i);
553
554                 bmesh_quick_edgedraw_flag(med, e);
555
556                 i++;
557                 med++;
558                 BM_CHECK_ELEMENT(bm, e);
559         }
560         bm->elem_index_dirty &= ~BM_EDGE;
561
562         i = 0;
563         j = 0;
564         BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
565                 mpoly->loopstart = j;
566                 mpoly->totloop = f->len;
567                 mpoly->mat_nr = f->mat_nr;
568                 mpoly->flag = BM_face_flag_to_mflag(f);
569
570                 l = BM_iter_new(&liter, bm, BM_LOOPS_OF_FACE, f);
571                 for ( ; l; l = BM_iter_step(&liter), j++, mloop++) {
572                         mloop->e = BM_elem_index_get(l->e);
573                         mloop->v = BM_elem_index_get(l->v);
574
575                         /* copy over customdat */
576                         CustomData_from_bmesh_block(&bm->ldata, &me->ldata, l->head.data, j);
577                         BM_CHECK_ELEMENT(bm, l);
578                         BM_CHECK_ELEMENT(bm, l->e);
579                         BM_CHECK_ELEMENT(bm, l->v);
580                 }
581                 
582                 if (f == bm->act_face) me->act_face = i;
583
584                 /* copy over customdat */
585                 CustomData_from_bmesh_block(&bm->pdata, &me->pdata, f->head.data, i);
586
587                 i++;
588                 mpoly++;
589                 BM_CHECK_ELEMENT(bm, f);
590         }
591
592         /* patch hook indices and vertex parents */
593         if (ototvert > 0) {
594                 Object *ob;
595                 ModifierData *md;
596                 BMVert **vertMap = NULL;
597                 int i, j;
598
599                 for (ob = G.main->object.first; ob; ob = ob->id.next) {
600                         if (ob->parent == bm->ob && ELEM(ob->partype, PARVERT1, PARVERT3)) {
601
602                                 if (vertMap == NULL) {
603                                         vertMap = bmesh_to_mesh_vertex_map(bm, ototvert);
604                                 }
605
606                                 if (ob->par1 < ototvert) {
607                                         eve = vertMap[ob->par1];
608                                         if (eve) ob->par1 = BM_elem_index_get(eve);
609                                 }
610                                 if (ob->par2 < ototvert) {
611                                         eve = vertMap[ob->par2];
612                                         if (eve) ob->par2 = BM_elem_index_get(eve);
613                                 }
614                                 if (ob->par3 < ototvert) {
615                                         eve = vertMap[ob->par3];
616                                         if (eve) ob->par3 = BM_elem_index_get(eve);
617                                 }
618                                 
619                         }
620                         if (ob->data == me) {
621                                 for (md = ob->modifiers.first; md; md = md->next) {
622                                         if (md->type == eModifierType_Hook) {
623                                                 HookModifierData *hmd = (HookModifierData *) md;
624
625                                                 if (vertMap == NULL) {
626                                                         vertMap = bmesh_to_mesh_vertex_map(bm, ototvert);
627                                                 }
628                                                 
629                                                 for (i = j = 0; i < hmd->totindex; i++) {
630                                                         if (hmd->indexar[i] < ototvert) {
631                                                                 eve = vertMap[hmd->indexar[i]];
632                                                                 
633                                                                 if (eve) {
634                                                                         hmd->indexar[j++] = BM_elem_index_get(eve);
635                                                                 }
636                                                         }
637                                                         else j++;
638                                                 }
639
640                                                 hmd->totindex = j;
641                                         }
642                                 }
643                         }
644                 }
645
646                 if (vertMap) MEM_freeN(vertMap);
647         }
648
649         if (dotess) {
650                 BKE_mesh_tessface_calc(me);
651         }
652
653         mesh_update_customdata_pointers(me, dotess);
654
655         {
656                 BMEditSelection *selected;
657                 me->totselect = BLI_countlist(&(bm->selected));
658
659                 if (me->mselect) MEM_freeN(me->mselect);
660
661                 me->mselect = MEM_callocN(sizeof(MSelect) * me->totselect, "Mesh selection history");
662
663
664                 for (i = 0, selected = bm->selected.first; selected; i++, selected = selected->next) {
665                         if (selected->htype == BM_VERT) {
666                                 me->mselect[i].type = ME_VSEL;
667
668                         }
669                         else if (selected->htype == BM_EDGE) {
670                                 me->mselect[i].type = ME_ESEL;
671
672                         }
673                         else if (selected->htype == BM_FACE) {
674                                 me->mselect[i].type = ME_FSEL;
675                         }
676
677                         me->mselect[i].index = BM_elem_index_get(selected->data);
678                 }
679         }
680
681         /* see comment below, this logic is in twice */
682
683         if (me->key) {
684                 KeyBlock *currkey;
685                 KeyBlock *actkey = BLI_findlink(&me->key->block, bm->shapenr - 1);
686
687                 float (*ofs)[3] = NULL;
688
689                 /* go through and find any shapekey customdata layers
690                  * that might not have corrusponding KeyBlocks, and add them if
691                  * necassary */
692                 j = 0;
693                 for (i = 0; i < bm->vdata.totlayer; i++) {
694                         if (bm->vdata.layers[i].type != CD_SHAPEKEY)
695                                 continue;
696
697                         for (currkey = me->key->block.first; currkey; currkey = currkey->next) {
698                                 if (currkey->uid == bm->vdata.layers[i].uid)
699                                         break;
700                         }
701                         
702                         if (!currkey) {
703                                 currkey = MEM_callocN(sizeof(KeyBlock), "KeyBlock mesh_conv.c");
704                                 currkey->type = KEY_LINEAR;
705                                 currkey->slidermin = 0.0f;
706                                 currkey->slidermax = 1.0f;
707
708                                 BLI_addtail(&me->key->block, currkey);
709                                 me->key->totkey++;
710                         }
711
712                         j++;
713                 }
714
715
716                 /* editing the base key should update others */
717                 if (me->key->type == KEY_RELATIVE && oldverts) {
718                         int act_is_basis = 0;
719                         /* find if this key is a basis for any others */
720                         for (currkey = me->key->block.first; currkey; currkey = currkey->next) {
721                                 if (bm->shapenr - 1 == currkey->relative) {
722                                         act_is_basis = 1;
723                                         break;
724                                 }
725                         }
726
727                         if (act_is_basis) { /* active key is a base */
728                                 float (*fp)[3] = actkey->data;
729                                 int *keyi;
730                                 i = 0;
731                                 ofs = MEM_callocN(sizeof(float) * 3 * bm->totvert,  "currkey->data");
732                                 mvert = me->mvert;
733                                 BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL) {
734                                         keyi = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_SHAPE_KEYINDEX);
735                                         if (keyi && *keyi != ORIGINDEX_NONE) {
736                                                 sub_v3_v3v3(ofs[i], mvert->co, fp[*keyi]);
737                                         }
738                                         i++;
739                                         mvert++;
740                                 }
741                         }
742                 }
743
744
745                 for (currkey = me->key->block.first; currkey; currkey = currkey->next) {
746                         j = 0;
747
748                         for (i = 0; i < bm->vdata.totlayer; i++) {
749                                 if (bm->vdata.layers[i].type != CD_SHAPEKEY)
750                                         continue;
751
752                                 if (currkey->uid == bm->vdata.layers[i].uid) {
753                                         int apply_offset = (ofs && (currkey != actkey) && (bm->shapenr - 1 == currkey->relative));
754                                         float *fp, *co;
755                                         float (*ofs_pt)[3] = ofs;
756
757                                         if (currkey->data)
758                                                 MEM_freeN(currkey->data);
759                                         currkey->data = fp = MEM_mallocN(sizeof(float) * 3 * bm->totvert, "shape key data");
760                                         currkey->totelem = bm->totvert;
761
762                                         BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL) {
763                                                 co = (currkey == actkey) ?
764                                                             eve->co :
765                                                             CustomData_bmesh_get_n(&bm->vdata, eve->head.data, CD_SHAPEKEY, j);
766                                                 
767                                                 copy_v3_v3(fp, co);
768
769                                                 /* propagate edited basis offsets to other shapes */
770                                                 if (apply_offset) {
771                                                         add_v3_v3(fp, *ofs_pt++);
772                                                 }
773
774                                                 fp += 3;
775                                         }
776                                         break;
777                                 }
778
779                                 j++;
780                         }
781
782                         /* if we didn't find a shapekey, tag the block to be reconstructed
783                          * via the old method below */
784                         if (j == CustomData_number_of_layers(&bm->vdata, CD_SHAPEKEY)) {
785                                 currkey->flag |= KEYBLOCK_MISSING;
786                         }
787                 }
788
789                 if (ofs) MEM_freeN(ofs);
790         }
791
792         /* XXX, code below is from trunk and a duplicate functionality
793          * to the block above.
794          * We should use one or the other, having both means we have to maintain
795          * both and keep them working the same way which is a hassle - campbell */
796
797         /* old method of reconstructing keys via vertice's original key indices,
798          * currently used if the new method above fails (which is theoretically
799          * possible in certain cases of undo) */
800         if (me->key) {
801                 float *fp, *newkey, *oldkey;
802                 KeyBlock *currkey;
803                 KeyBlock *actkey = BLI_findlink(&me->key->block, bm->shapenr - 1);
804
805                 float (*ofs)[3] = NULL;
806
807                 /* editing the base key should update others */
808                 if (me->key->type == KEY_RELATIVE && oldverts) {
809                         int act_is_basis = 0;
810                         /* find if this key is a basis for any others */
811                         for (currkey = me->key->block.first; currkey; currkey = currkey->next) {
812                                 if (bm->shapenr - 1 == currkey->relative) {
813                                         act_is_basis = 1;
814                                         break;
815                                 }
816                         }
817
818                         if (act_is_basis) { /* active key is a base */
819                                 float (*fp)[3] = actkey->data;
820                                 int *keyi;
821                                 i = 0;
822                                 ofs = MEM_callocN(sizeof(float) * 3 * bm->totvert,  "currkey->data");
823                                 mvert = me->mvert;
824                                 BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL) {
825                                         keyi = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_SHAPE_KEYINDEX);
826                                         if (keyi && *keyi != ORIGINDEX_NONE) {
827                                                 sub_v3_v3v3(ofs[i], mvert->co, fp[*keyi]);
828                                         }
829                                         i++;
830                                         mvert++;
831                                 }
832                         }
833                 }
834
835                 /* Lets reorder the key data so that things line up roughly
836                  * with the way things were before editmode */
837                 currkey = me->key->block.first;
838                 while (currkey) {
839                         int apply_offset = (ofs && (currkey != actkey) && (bm->shapenr - 1 == currkey->relative));
840
841                         if (!(currkey->flag & KEYBLOCK_MISSING)) {
842                                 currkey = currkey->next;
843                                 continue;
844                         }
845                         
846                         printf("warning: had to hackishly reconstruct shape key \"%s\","
847                                " it may not be correct anymore.\n", currkey->name);
848
849                         currkey->flag &= ~KEYBLOCK_MISSING;
850
851                         fp = newkey = MEM_callocN(me->key->elemsize * bm->totvert,  "currkey->data");
852                         oldkey = currkey->data;
853
854                         eve = BM_iter_new(&iter, bm, BM_VERTS_OF_MESH, NULL);
855
856                         i = 0;
857                         mvert = me->mvert;
858                         while (eve) {
859                                 keyi = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_SHAPE_KEYINDEX);
860                                 if (!keyi) {
861                                         break;
862                                 }
863                                 if (*keyi >= 0 && *keyi < currkey->totelem) { // valid old vertex
864                                         if (currkey == actkey) {
865                                                 if (actkey == me->key->refkey) {
866                                                         copy_v3_v3(fp, mvert->co);
867                                                 }
868                                                 else {
869                                                         copy_v3_v3(fp, mvert->co);
870                                                         if (oldverts) {
871                                                                 copy_v3_v3(mvert->co, oldverts[*keyi].co);
872                                                         }
873                                                 }
874                                         }
875                                         else {
876                                                 if (oldkey) {
877                                                         copy_v3_v3(fp, oldkey + 3 * *keyi);
878                                                 }
879                                         }
880                                 }
881                                 else {
882                                         copy_v3_v3(fp, mvert->co);
883                                 }
884
885                                 /* propagate edited basis offsets to other shapes */
886                                 if (apply_offset) {
887                                         add_v3_v3(fp, ofs[i]);
888                                 }
889
890                                 fp+= 3;
891                                 ++i;
892                                 ++mvert;
893                                 eve = BM_iter_step(&iter);
894                         }
895                         currkey->totelem = bm->totvert;
896                         if (currkey->data) MEM_freeN(currkey->data);
897                         currkey->data = newkey;
898                         
899                         currkey = currkey->next;
900                 }
901
902                 if (ofs) MEM_freeN(ofs);
903         }
904
905         if (oldverts) MEM_freeN(oldverts);
906 }