remove unused vars
[blender.git] / source / blender / bmesh / operators / mesh_conv.c
1 #include <string.h>
2
3 #include "MEM_guardedalloc.h"
4
5 #include "DNA_listBase.h"
6 #include "DNA_customdata_types.h"
7 #include "DNA_mesh_types.h"
8 #include "DNA_meshdata_types.h"
9 #include "DNA_modifier_types.h"
10 #include "DNA_key_types.h"
11 #include "DNA_object_types.h"
12 #include "DNA_scene_types.h"
13
14 #include "BKE_customdata.h" 
15 #include "BKE_mesh.h"
16 #include "BKE_global.h"
17 #include "BKE_DerivedMesh.h"
18 #include "BKE_cdderivedmesh.h"
19 #include "BKE_key.h"
20 #include "BKE_main.h"
21
22 #include "BLI_utildefines.h"
23 #include "BLI_math.h"
24 #include "BLI_blenlib.h"
25 #include "BLI_edgehash.h"
26 #include "BLI_editVert.h"
27 #include "BLI_scanfill.h"
28 #include "BLI_array.h"
29 #include "BLI_utildefines.h"
30
31 #include "ED_mesh.h"
32
33 #include "mesh_intern.h"
34 #include "bmesh.h"
35 #include "bmesh_private.h"
36
37 /*
38  * MESH CONV.C
39  *
40  * This file contains functions
41  * for converting a Mesh
42  * into a Bmesh, and back again.
43  *
44 */
45
46 void mesh_to_bmesh_exec(BMesh *bm, BMOperator *op) {
47         Object *ob = BMO_Get_Pnt(op, "object");
48         Mesh *me = BMO_Get_Pnt(op, "mesh");
49         MVert *mvert;
50         BLI_array_declare(verts);
51         MEdge *medge;
52         MLoop *ml;
53         MPoly *mpoly;
54         KeyBlock *actkey, *block;
55         BMVert *v, **vt=NULL, **verts = NULL;
56         BMEdge *e, **fedges=NULL, **et = NULL;
57         BMFace *f;
58         BLI_array_declare(fedges);
59         float (*keyco)[3]= NULL;
60         int *keyi;
61         int set_key = BMO_Get_Int(op, "set_shapekey");
62         int totuv, i, j, li, allocsize[4] = {512, 512, 2048, 512};
63
64         if (!me || !me->totvert) return; /*sanity check*/
65         
66         mvert = me->mvert;
67         vt = MEM_mallocN(sizeof(void**)*me->totvert, "mesh to bmesh vtable");
68
69         CustomData_copy(&me->vdata, &bm->vdata, CD_MASK_BMESH, CD_CALLOC, 0);
70         CustomData_copy(&me->edata, &bm->edata, CD_MASK_BMESH, CD_CALLOC, 0);
71         CustomData_copy(&me->ldata, &bm->ldata, CD_MASK_BMESH, CD_CALLOC, 0);
72         CustomData_copy(&me->pdata, &bm->pdata, CD_MASK_BMESH, CD_CALLOC, 0);
73         
74         /*make sure uv layer names are consistent*/
75         totuv = CustomData_number_of_layers(&bm->pdata, CD_MTEXPOLY);
76         for (i=0; i<totuv; i++) {
77                 int li = CustomData_get_layer_index_n(&bm->pdata, CD_MTEXPOLY, i);
78                 CustomData_set_layer_name(&bm->ldata, CD_MLOOPUV, i, bm->pdata.layers[li].name);
79         }
80         
81         if (!CustomData_has_layer(&bm->edata, CD_CREASE))
82                 CustomData_add_layer(&bm->edata, CD_CREASE, CD_ASSIGN, NULL, 0);
83
84         if (!CustomData_has_layer(&bm->edata, CD_BWEIGHT))
85                 CustomData_add_layer(&bm->edata, CD_BWEIGHT, CD_ASSIGN, NULL, 0);
86
87         if (!CustomData_has_layer(&bm->vdata, CD_BWEIGHT))
88                 CustomData_add_layer(&bm->vdata, CD_BWEIGHT, CD_ASSIGN, NULL, 0);
89
90
91         if (me->key && ob->shapenr > me->key->totkey) {
92                 ob->shapenr = me->key->totkey-1;
93         }
94
95         actkey = ob_get_keyblock(ob);
96         if(actkey && actkey->totelem == me->totvert) {
97                 CustomData_add_layer(&bm->vdata, CD_SHAPE_KEYINDEX, CD_ASSIGN, NULL, 0);
98                 
99                 /*check if we need to generate unique ids for the shapekeys.
100                   this also exists in the file reading code, but is here for
101                   a sanity check*/
102                 if (!me->key->uidgen) {
103                         printf("yeek! had to generate shape key uid's in a situation we shouldn't need to!\n");
104                         me->key->uidgen = 1;
105                         for (block=me->key->block.first; block; block=block->next) {
106                                 block->uid = me->key->uidgen++;
107                         }
108                 }
109
110                 keyco= actkey->data;
111                 bm->shapenr= ob->shapenr;
112                 for (i=0, block=me->key->block.first; block; block=block->next, i++) {
113                         CustomData_add_layer_named(&bm->vdata, CD_SHAPEKEY, 
114                                          CD_ASSIGN, NULL, 0, block->name);
115                         
116                         j = CustomData_get_layer_index_n(&bm->vdata, CD_SHAPEKEY, i);
117                         bm->vdata.layers[j].uid = block->uid;
118                 }
119         } else if (actkey) {
120                 printf("shapekey<->mesh mismatch!\n");
121         }
122         
123         CustomData_bmesh_init_pool(&bm->vdata, allocsize[0]);
124         CustomData_bmesh_init_pool(&bm->edata, allocsize[1]);
125         CustomData_bmesh_init_pool(&bm->ldata, allocsize[2]);
126         CustomData_bmesh_init_pool(&bm->pdata, allocsize[3]);
127
128         for (i=0; i<me->totvert; i++, mvert++) {
129                 v = BM_Make_Vert(bm, keyco&&set_key ? keyco[i] : mvert->co, NULL);
130                 normal_short_to_float_v3(v->no, mvert->no);
131
132                 vt[i] = v;
133                 BM_SetIndex(v, i);
134
135                 /*this is necassary for selection counts to work properly*/
136                 if(v->head.flag & BM_SELECT) BM_Select_Vert(bm, v, 1);
137
138                 /*transfer flags*/
139                 v->head.flag = MEFlags_To_BMFlags(mvert->flag, BM_VERT);
140                 BM_SetCDf(&bm->vdata, v, CD_BWEIGHT, (float)mvert->bweight / 255.0f);
141
142                 /*Copy Custom Data*/
143                 CustomData_to_bmesh_block(&me->vdata, &bm->vdata, i, &v->head.data);
144
145                 /*set shapekey data*/
146                 if (me->key) {
147                         /*set shape key original index*/
148                         keyi = CustomData_bmesh_get(&bm->vdata, v->head.data, CD_SHAPE_KEYINDEX);
149                         *keyi = i;
150                         
151                         for (block=me->key->block.first, j=0; block; block=block->next, j++) {
152                                 float *co = CustomData_bmesh_get_n(&bm->vdata, v->head.data, 
153                                                                    CD_SHAPEKEY, j);
154                                 if (co)
155                                         VECCOPY(co, ((float*)block->data)+3*i);
156                         }
157                 }
158         }
159
160         if (!me->totedge) {
161                 MEM_freeN(vt);
162                 return;
163         }
164
165         et = MEM_mallocN(sizeof(void**)*me->totedge, "mesh to bmesh etable");
166
167         medge = me->medge;
168         for (i=0; i<me->totedge; i++, medge++) {
169                 e = BM_Make_Edge(bm, vt[medge->v1], vt[medge->v2], NULL, 0);
170                 et[i] = e;
171                 
172                 /*Copy Custom Data*/
173                 CustomData_to_bmesh_block(&me->edata, &bm->edata, i, &e->head.data);
174                 
175                 BM_SetCDf(&bm->edata, e, CD_CREASE, (float)medge->crease / 255.0f);
176                 BM_SetCDf(&bm->edata, e, CD_BWEIGHT, (float)medge->bweight / 255.0f);
177
178                 /*this is necassary for selection counts to work properly*/
179                 if (e->head.flag & BM_SELECT) BM_Select(bm, e, 1);
180
181                 /*transfer flags*/
182                 e->head.flag = MEFlags_To_BMFlags(medge->flag, BM_EDGE);
183         }
184         
185         if (!me->totpoly) {
186                 MEM_freeN(vt);
187                 MEM_freeN(et);
188                 return;
189         }
190
191         mpoly = me->mpoly;
192         li = 0;
193         for (i=0; i<me->totpoly; i++, mpoly++) {
194                 BMVert *v1, *v2;
195                 BMIter iter;
196                 BMLoop *l;
197
198                 BLI_array_empty(fedges);
199                 BLI_array_empty(verts);
200                 for (j=0; j<mpoly->totloop; j++) {
201                         ml = &me->mloop[mpoly->loopstart+j];
202                         v = vt[ml->v];
203                         e = et[ml->e];
204
205                         BLI_array_growone(fedges);
206                         BLI_array_growone(verts);
207
208                         fedges[j] = e;
209                         verts[j] = v;
210                 }
211                 
212                 v1 = vt[me->mloop[mpoly->loopstart].v];
213                 v2 = vt[me->mloop[mpoly->loopstart+1].v];
214
215                 if (v1 == fedges[0]->v1) v2 = fedges[0]->v2;
216                 else {
217                         v1 = fedges[0]->v2;
218                         v2 = fedges[0]->v1;
219                 }
220         
221                 f = BM_Make_Face(bm, verts, fedges, mpoly->totloop);
222
223                 if (!f) {
224                         printf("Warning! Bad face in mesh"
225                                " \"%s\" at index %d!\n", me->id.name+2, i);
226                         continue;
227                 }
228
229                 /*this is necassary for selection counts to work properly*/
230                 if (f->head.flag & BM_SELECT) BM_Select(bm, f, 1);
231
232                 /*transfer flags*/
233                 f->head.flag = MEFlags_To_BMFlags(mpoly->flag, BM_FACE);
234
235                 f->mat_nr = mpoly->mat_nr;
236                 if (i == me->act_face) bm->act_face = f;
237
238                 /*Copy over loop customdata*/
239                 j = 0;
240                 BM_ITER(l, &iter, bm, BM_LOOPS_OF_FACE, f) {
241                         CustomData_to_bmesh_block(&me->ldata, &bm->ldata, mpoly->loopstart+j, &l->head.data);
242                         li++;
243                         j++;
244                 }
245
246                 /*Copy Custom Data*/
247                 CustomData_to_bmesh_block(&me->pdata, &bm->pdata, i, &f->head.data);
248         }
249
250         BLI_array_free(fedges);
251         BLI_array_free(verts);
252         
253         MEM_freeN(vt);
254         MEM_freeN(et);
255 }
256
257
258 static void loops_to_corners(BMesh *bm, Mesh *me, int findex,
259                              BMFace *f, BMLoop *ls[3], int numTex, int numCol) 
260 {
261         BMLoop *l;
262         MTFace *texface;
263         MTexPoly *texpoly;
264         MCol *mcol;
265         MLoopCol *mloopcol;
266         MLoopUV *mloopuv;
267         int i, j;
268
269         for(i=0; i < numTex; i++){
270                 texface = CustomData_get_n(&me->fdata, CD_MTFACE, findex, i);
271                 texpoly = CustomData_bmesh_get_n(&bm->pdata, f->head.data, CD_MTEXPOLY, i);
272                 
273                 texface->tpage = texpoly->tpage;
274                 texface->flag = texpoly->flag;
275                 texface->transp = texpoly->transp;
276                 texface->mode = texpoly->mode;
277                 texface->tile = texpoly->tile;
278                 texface->unwrap = texpoly->unwrap;
279
280                 for (j=0; j<3; j++) {
281                         l = ls[j];
282                         mloopuv = CustomData_bmesh_get_n(&bm->ldata, l->head.data, CD_MLOOPUV, i);
283                         texface->uv[j][0] = mloopuv->uv[0];
284                         texface->uv[j][1] = mloopuv->uv[1];
285                 }
286         }
287
288         for(i=0; i < numCol; i++){
289                 mcol = CustomData_get_n(&me->fdata, CD_MCOL, findex, i);
290
291                 for (j=0; j<3; j++) {
292                         l = ls[j];
293                         mloopcol = CustomData_bmesh_get_n(&bm->ldata, l->head.data, CD_MLOOPCOL, i);
294                         mcol[j].r = mloopcol->r;
295                         mcol[j].g = mloopcol->g;
296                         mcol[j].b = mloopcol->b;
297                         mcol[j].a = mloopcol->a;
298                 }
299         }
300 }
301
302 void object_load_bmesh_exec(BMesh *bm, BMOperator *op) {
303         Object *ob = BMO_Get_Pnt(op, "object");
304         /* Scene *scene = BMO_Get_Pnt(op, "scene"); */
305         Mesh *me = ob->data;
306
307         BMO_CallOpf(bm, "bmesh_to_mesh mesh=%p object=%p", me, ob);
308
309         /*BMESH_TODO eventually we'll have to handle shapekeys here*/
310 }
311
312 void bmesh_to_mesh_exec(BMesh *bm, BMOperator *op) {
313         Mesh *me = BMO_Get_Pnt(op, "mesh");
314         /* Object *ob = BMO_Get_Pnt(op, "object"); */
315         MLoop *mloop;
316         KeyBlock *block;
317         MPoly *mpoly;
318         MVert *mvert, *oldverts;
319         MEdge *medge;
320         MFace *mface;
321         BMVert *v, *eve;
322         BMEdge *e;
323         BMLoop *l;
324         BMFace *f;
325         BMIter iter, liter;
326         float *facenors = NULL;
327         int i, j, *keyi, ototvert, totloop, totface, numTex, numCol;
328         int dotess = !BMO_Get_Int(op, "notesselation");
329
330         numTex = CustomData_number_of_layers(&bm->pdata, CD_MTEXPOLY);
331         numCol = CustomData_number_of_layers(&bm->ldata, CD_MLOOPCOL);
332         
333         ototvert = me->totvert;
334
335         /* new Vertex block */
336         if(bm->totvert==0) mvert= NULL;
337         else mvert= MEM_callocN(bm->totvert*sizeof(MVert), "loadeditbMesh vert");
338
339         /* new Edge block */
340         if(bm->totedge==0) medge= NULL;
341         else medge= MEM_callocN(bm->totedge*sizeof(MEdge), "loadeditbMesh edge");
342         
343         /*build ngon data*/
344         /* new Ngon Face block */
345         if(bm->totface==0) mpoly = NULL;
346         else mpoly= MEM_callocN(bm->totface*sizeof(MPoly), "loadeditbMesh poly");
347         
348         /*find number of loops to allocate*/
349         totloop = 0;
350         BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
351                 totloop += f->len;
352         }
353
354         if (totloop==0) mloop = NULL;
355         else mloop = MEM_callocN(totloop*sizeof(MLoop), "loadeditbMesh loop");
356
357         /* lets save the old verts just in case we are actually working on
358          * a key ... we now do processing of the keys at the end */
359         oldverts= me->mvert;
360
361         /* don't free this yet */
362         CustomData_set_layer(&me->vdata, CD_MVERT, NULL);
363
364         /* free custom data */
365         CustomData_free(&me->vdata, me->totvert);
366         CustomData_free(&me->edata, me->totedge);
367         CustomData_free(&me->fdata, me->totface);
368         CustomData_free(&me->ldata, me->totloop);
369         CustomData_free(&me->pdata, me->totpoly);
370
371         /* add new custom data */
372         me->totvert= bm->totvert;
373         me->totedge= bm->totedge;
374         me->totloop= totloop;
375         me->totpoly= bm->totface;
376
377         CustomData_copy(&bm->vdata, &me->vdata, CD_MASK_MESH, CD_CALLOC, me->totvert);
378         CustomData_copy(&bm->edata, &me->edata, CD_MASK_MESH, CD_CALLOC, me->totedge);
379         CustomData_copy(&bm->ldata, &me->ldata, CD_MASK_MESH, CD_CALLOC, me->totloop);
380         CustomData_copy(&bm->pdata, &me->pdata, CD_MASK_MESH, CD_CALLOC, me->totpoly);
381
382         CustomData_add_layer(&me->vdata, CD_MVERT, CD_ASSIGN, mvert, me->totvert);
383         CustomData_add_layer(&me->edata, CD_MEDGE, CD_ASSIGN, medge, me->totedge);
384         CustomData_add_layer(&me->ldata, CD_MLOOP, CD_ASSIGN, mloop, me->totloop);
385         CustomData_add_layer(&me->pdata, CD_MPOLY, CD_ASSIGN, mpoly, me->totpoly);
386         
387         i = 0;
388         BM_ITER(v, &iter, bm, BM_VERTS_OF_MESH, NULL) {
389                 float *bweight = CustomData_bmesh_get(&bm->vdata, v->head.data, CD_BWEIGHT);
390
391                 mvert->bweight = bweight ? (char)((*bweight)*255) : 0;
392
393                 VECCOPY(mvert->co, v->co);
394
395                 mvert->no[0] = (short) (v->no[0]*32767.0f);
396                 mvert->no[1] = (short) (v->no[1]*32767.0f);
397                 mvert->no[2] = (short) (v->no[2]*32767.0f);
398                 
399                 mvert->flag = BMFlags_To_MEFlags(v);
400
401                 BM_SetIndex(v, i);
402
403                 /*copy over customdata*/
404                 CustomData_from_bmesh_block(&bm->vdata, &me->vdata, v->head.data, i);
405
406                 i++;
407                 mvert++;
408
409                 CHECK_ELEMENT(bm, v);
410         }
411
412         i = 0;
413         BM_ITER(e, &iter, bm, BM_EDGES_OF_MESH, NULL) {
414                 float *crease = CustomData_bmesh_get(&bm->edata, e->head.data, CD_CREASE);
415                 float *bweight = CustomData_bmesh_get(&bm->edata, e->head.data, CD_BWEIGHT);
416                 
417                 medge->v1 = BM_GetIndex(e->v1);
418                 medge->v2 = BM_GetIndex(e->v2);
419                 medge->crease = crease ? (char)((*crease)*255) : 0;
420                 medge->bweight = bweight ? (char)((*bweight)*255) : 0;
421                 
422                 medge->flag = BMFlags_To_MEFlags(e);
423                 
424                 BM_SetIndex(e, i);
425
426                 /*copy over customdata*/
427                 CustomData_from_bmesh_block(&bm->edata, &me->edata, e->head.data, i);
428
429                 i++;
430                 medge++;
431                 CHECK_ELEMENT(bm, e);
432         }
433
434         /*new scanfill tesselation code*/
435         if (dotess) {
436                 /*first counter number of faces we'll need*/
437                 totface = 0;
438                 BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
439                         EditVert *eve, *lasteve = NULL, *firsteve = NULL;
440                         EditFace *efa;
441                         
442                         BLI_begin_edgefill();
443                         i = 0;
444                         BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
445                                 eve = BLI_addfillvert(l->v->co);
446                                 eve->tmp.p = l;
447                                 
448                                 BM_SetIndex(l, i);
449
450                                 if (lasteve) {
451                                         BLI_addfilledge(lasteve, eve);
452                                 }
453
454                                 lasteve = eve;
455                                 if (!firsteve) firsteve = eve;
456
457                                 i++;
458                         }
459
460                         BLI_addfilledge(lasteve, firsteve);
461                         BLI_edgefill(0);
462
463                         for (efa=fillfacebase.first; efa; efa=efa->next)
464                                 totface++;
465
466                         BLI_end_edgefill();
467                 }
468                 
469                 me->totface = totface;
470
471                 /* new tess face block */
472                 if(totface==0) mface= NULL;
473                 else {
474                         mface= MEM_callocN(totface*sizeof(MFace), "loadeditbMesh face");
475                         facenors = MEM_callocN(totface*sizeof(float)*3, "facenors");
476                 }
477
478                 CustomData_add_layer(&me->fdata, CD_MFACE, CD_ASSIGN, mface, me->totface);
479                 CustomData_add_layer(&me->fdata, CD_NORMAL, CD_ASSIGN, facenors, me->totface);
480                 CustomData_from_bmeshpoly(&me->fdata, &bm->pdata, &bm->ldata, totface);
481
482                 mesh_update_customdata_pointers(me);
483                 
484                 i = 0;
485                 BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
486                         EditVert *eve, *lasteve = NULL, *firsteve = NULL;
487                         EditFace *efa;
488                         BMLoop *ls[3];
489                         
490                         BLI_begin_edgefill();
491                         BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
492                                 eve = BLI_addfillvert(l->v->co);
493                                 eve->tmp.p = l;
494
495                                 if (lasteve) {
496                                         BLI_addfilledge(lasteve, eve);
497                                 }
498
499                                 lasteve = eve;
500                                 if (!firsteve) firsteve = eve;
501                         }
502
503                         BLI_addfilledge(lasteve, firsteve);
504                         BLI_edgefill(0);
505
506                         for (efa=fillfacebase.first; efa; efa=efa->next) {
507                                 ls[0] = efa->v1->tmp.p;
508                                 ls[1] = efa->v2->tmp.p;
509                                 ls[2] = efa->v3->tmp.p;
510                                 
511                                 /*ensure correct winding.  I believe this is
512                                   analogous to bubble sort on three elements.*/
513                                 if (BM_GetIndex(ls[0]) > BM_GetIndex(ls[1])) {
514                                         SWAP(BMLoop*, ls[0], ls[1]);
515                                 }
516                                 if (BM_GetIndex(ls[1]) > BM_GetIndex(ls[2])) {
517                                         SWAP(BMLoop*, ls[1], ls[2]);
518                                 }
519                                 if (BM_GetIndex(ls[0]) > BM_GetIndex(ls[1])) {
520                                         SWAP(BMLoop*, ls[0], ls[1]);
521                                 }
522
523                                 mface->mat_nr = f->mat_nr;
524                                 mface->flag = BMFlags_To_MEFlags(f);
525                                 
526                                 mface->v1 = BM_GetIndex(ls[0]->v);
527                                 mface->v2 = BM_GetIndex(ls[1]->v);
528                                 mface->v3 = BM_GetIndex(ls[2]->v);
529
530                                 test_index_face(mface, &me->fdata, i, 1);
531                                 
532                                 loops_to_corners(bm, me, i, f, ls, numTex, numCol);
533                                 VECCOPY(facenors, ls[0]->f->no);
534
535                                 mface++;
536                                 facenors += 3;
537                                 i++;
538                         }
539                         BLI_end_edgefill();
540                 }
541         }
542
543         i = 0;
544         j = 0;
545         BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
546                 mpoly->loopstart = j;
547                 mpoly->totloop = f->len;
548                 mpoly->mat_nr = f->mat_nr;
549                 mpoly->flag = BMFlags_To_MEFlags(f);
550
551                 l = BMIter_New(&liter, bm, BM_LOOPS_OF_FACE, f);
552                 for ( ; l; l=BMIter_Step(&liter), j++, mloop++) {
553                         mloop->e = BM_GetIndex(l->e);
554                         mloop->v = BM_GetIndex(l->v);
555
556                         /*copy over customdata*/
557                         CustomData_from_bmesh_block(&bm->ldata, &me->ldata, l->head.data, j);
558                         CHECK_ELEMENT(bm, l);
559                         CHECK_ELEMENT(bm, l->e);
560                         CHECK_ELEMENT(bm, l->v);
561                 }
562                 
563                 if (f == bm->act_face) me->act_face = i;
564
565                 /*copy over customdata*/
566                 CustomData_from_bmesh_block(&bm->pdata, &me->pdata, f->head.data, i);
567
568                 i++;
569                 mpoly++;
570                 CHECK_ELEMENT(bm, f);
571         }
572
573         /* patch hook indices and vertex parents */
574         {
575                 Object *ob;
576                 ModifierData *md;
577                 BMVert **vertMap = NULL;
578                 int i,j;
579
580                 for (ob=G.main->object.first; ob; ob=ob->id.next) {
581                         if (ob->parent==ob && ELEM(ob->partype, PARVERT1,PARVERT3)) {
582                                 
583                                 /* duplicate code from below, make it function later...? */
584                                 if (!vertMap) {
585                                         vertMap = MEM_callocN(sizeof(*vertMap)*ototvert, "vertMap");
586                                         
587                                         BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL) {
588                                                 keyi = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_SHAPE_KEYINDEX);
589                                                 if (*keyi != ORIGINDEX_NONE)
590                                                         vertMap[*keyi] = eve;
591                                         }
592                                 }
593                                 if(ob->par1 < ototvert) {
594                                         eve = vertMap[ob->par1];
595                                         if(eve) ob->par1= BM_GetIndex(eve);
596                                 }
597                                 if(ob->par2 < ototvert) {
598                                         eve = vertMap[ob->par2];
599                                         if(eve) ob->par2= BM_GetIndex(eve);
600                                 }
601                                 if(ob->par3 < ototvert) {
602                                         eve = vertMap[ob->par3];
603                                         if(eve) ob->par3= BM_GetIndex(eve);
604                                 }
605                                 
606                         }
607                         if (ob->data==me) {
608                                 for (md=ob->modifiers.first; md; md=md->next) {
609                                         if (md->type==eModifierType_Hook) {
610                                                 HookModifierData *hmd = (HookModifierData*) md;
611
612                                                 if (!vertMap) {
613                                                         vertMap = MEM_callocN(sizeof(*vertMap)*ototvert, "vertMap");
614                                                         
615                                                         BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL) {
616                                                                 keyi = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_SHAPE_KEYINDEX);
617                                                                 if (*keyi != ORIGINDEX_NONE)
618                                                                         vertMap[*keyi] = eve;
619                                                         }
620                                                 }
621                                                 
622                                                 for (i=j=0; i<hmd->totindex; i++) {
623                                                         if(hmd->indexar[i] < ototvert) {
624                                                                 eve = vertMap[hmd->indexar[i]];
625                                                                 
626                                                                 if (eve) {
627                                                                         hmd->indexar[j++] = BM_GetIndex(eve);
628                                                                 }
629                                                         }
630                                                         else j++;
631                                                 }
632
633                                                 hmd->totindex = j;
634                                         }
635                                 }
636                         }
637                 }
638
639                 if (vertMap) MEM_freeN(vertMap);
640         }
641
642         mesh_update_customdata_pointers(me);
643
644         if (me->key) {
645                 KeyBlock *actkey= BLI_findlink(&me->key->block, bm->shapenr-1);
646
647                 /*go through and find any shapekey customdata layers
648                   that might not have corrusponding KeyBlocks, and add them if
649                   necassary.*/
650                 j = 0;
651                 for (i=0; i<bm->vdata.totlayer; i++) {
652                         if (bm->vdata.layers[i].type != CD_SHAPEKEY)
653                                 continue;
654
655                         for (block=me->key->block.first; block; block=block->next) {
656                                 if (block->uid == bm->vdata.layers[i].uid)
657                                         break;
658                         }
659                         
660                         if (!block) {
661                                 block = MEM_callocN(sizeof(KeyBlock), "KeyBlock mesh_conv.c");
662                                 block->type = KEY_LINEAR;
663                                 block->slidermin = 0.0f;
664                                 block->slidermax = 1.0f;
665
666                                 BLI_addtail(&me->key->block, block);
667                                 me->key->totkey++;
668                         }
669
670                         j++;
671                 }
672
673                 for (block=me->key->block.first; block; block=block->next) {
674                         j = 0;
675
676                         for (i=0; i<bm->vdata.totlayer; i++) {
677                                 if (bm->vdata.layers[i].type != CD_SHAPEKEY)
678                                         continue;
679
680                                 if (block->uid == bm->vdata.layers[i].uid) {
681                                         float *fp, *co;
682
683                                         if (block->data)
684                                                 MEM_freeN(block->data);
685                                         block->data = fp = MEM_mallocN(sizeof(float)*3*bm->totvert, "shape key data");
686                                         block->totelem = bm->totvert;
687
688                                         BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL) {
689                                                 co = block==actkey ? eve->co : CustomData_bmesh_get_n(&bm->vdata, eve->head.data, CD_SHAPEKEY, j);
690                                                 
691                                                 VECCOPY(fp, co);
692                                                 fp += 3;
693                                         }
694                                         break;
695                                 }
696
697                                 j++;
698                         }
699
700                         /*if we didn't find a shapekey, tag the block to be reconstructed
701                           via the old method below*/
702                         if (j == CustomData_number_of_layers(&bm->vdata, CD_SHAPEKEY)) {
703                                 block->flag |= KEYBLOCK_MISSING;
704                         }
705                 }
706         }
707
708         /* old method of reconstructing keys via vertice's original key indices,
709            currently used if the new method above fails (which is theoretically
710            possible in certain cases of undo).*/
711         if(me->key) {
712                 float *fp, *newkey, *oldkey;
713                 KeyBlock *currkey;
714                 KeyBlock *actkey= BLI_findlink(&me->key->block, bm->shapenr-1);
715
716                 /* Lets reorder the key data so that things line up roughly
717                  * with the way things were before editmode */
718                 currkey = me->key->block.first;
719                 while(currkey) {
720                         if (!(currkey->flag & KEYBLOCK_MISSING)) {
721                                 currkey = currkey->next;
722                                 continue;
723                         }
724                         
725                         printf("warning: had to hackishly reconstruct shape key \"%s\","
726                                " it may not be correct anymore.\n", currkey->name);
727
728                         currkey->flag &= ~KEYBLOCK_MISSING;
729
730                         fp= newkey= MEM_callocN(me->key->elemsize*bm->totvert,  "currkey->data");
731                         oldkey = currkey->data;
732
733                         eve= BMIter_New(&iter, bm, BM_VERTS_OF_MESH, NULL);
734
735                         i = 0;
736                         mvert = me->mvert;
737                         while(eve) {
738                                 keyi = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_SHAPE_KEYINDEX);
739                                 if (*keyi >= 0 && *keyi < currkey->totelem) { // valid old vertex
740                                         if(currkey == actkey) {
741                                                 if(actkey == me->key->refkey) {
742                                                         VECCOPY(fp, mvert->co);
743                                                 }
744                                                 else {
745                                                         VECCOPY(fp, mvert->co);
746                                                         if(oldverts) {
747                                                                 VECCOPY(mvert->co, oldverts[*keyi].co);
748                                                         }
749                                                 }
750                                         }
751                                         else {
752                                                 if(oldkey) {
753                                                         VECCOPY(fp, oldkey + 3 * *keyi);
754                                                 }
755                                         }
756                                 }
757                                 else {
758                                         VECCOPY(fp, mvert->co);
759                                 }
760                                 fp+= 3;
761                                 ++i;
762                                 ++mvert;
763                                 eve= BMIter_Step(&iter);
764                         }
765                         currkey->totelem= bm->totvert;
766                         if(currkey->data) MEM_freeN(currkey->data);
767                         currkey->data = newkey;
768                         
769                         currkey= currkey->next;
770                 }
771         }
772
773         if(oldverts) MEM_freeN(oldverts);
774 }