svn merge -r36651:36672 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[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                         
441                         BLI_begin_edgefill();
442                         i = 0;
443                         BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
444                                 eve = BLI_addfillvert(l->v->co);
445                                 eve->tmp.p = l;
446                                 
447                                 BM_SetIndex(l, i);
448
449                                 if (lasteve) {
450                                         BLI_addfilledge(lasteve, eve);
451                                 }
452
453                                 lasteve = eve;
454                                 if (!firsteve) firsteve = eve;
455
456                                 i++;
457                         }
458
459                         BLI_addfilledge(lasteve, firsteve);
460                         totface += BLI_edgefill(0);
461
462                         BLI_end_edgefill();
463                 }
464                 
465                 me->totface = totface;
466
467                 /* new tess face block */
468                 if(totface==0) mface= NULL;
469                 else {
470                         mface= MEM_callocN(totface*sizeof(MFace), "loadeditbMesh face");
471                         facenors = MEM_callocN(totface*sizeof(float)*3, "facenors");
472                 }
473
474                 CustomData_add_layer(&me->fdata, CD_MFACE, CD_ASSIGN, mface, me->totface);
475                 CustomData_add_layer(&me->fdata, CD_NORMAL, CD_ASSIGN, facenors, me->totface);
476                 CustomData_from_bmeshpoly(&me->fdata, &bm->pdata, &bm->ldata, totface);
477
478                 mesh_update_customdata_pointers(me);
479                 
480                 i = 0;
481                 BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
482                         EditVert *eve, *lasteve = NULL, *firsteve = NULL;
483                         EditFace *efa;
484                         BMLoop *ls[3];
485                         
486                         BLI_begin_edgefill();
487                         BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
488                                 eve = BLI_addfillvert(l->v->co);
489                                 eve->tmp.p = l;
490
491                                 if (lasteve) {
492                                         BLI_addfilledge(lasteve, eve);
493                                 }
494
495                                 lasteve = eve;
496                                 if (!firsteve) firsteve = eve;
497                         }
498
499                         BLI_addfilledge(lasteve, firsteve);
500                         BLI_edgefill(0);
501
502                         for (efa=fillfacebase.first; efa; efa=efa->next) {
503                                 ls[0] = efa->v1->tmp.p;
504                                 ls[1] = efa->v2->tmp.p;
505                                 ls[2] = efa->v3->tmp.p;
506                                 
507                                 /*ensure correct winding.  I believe this is
508                                   analogous to bubble sort on three elements.*/
509                                 if (BM_GetIndex(ls[0]) > BM_GetIndex(ls[1])) {
510                                         SWAP(BMLoop*, ls[0], ls[1]);
511                                 }
512                                 if (BM_GetIndex(ls[1]) > BM_GetIndex(ls[2])) {
513                                         SWAP(BMLoop*, ls[1], ls[2]);
514                                 }
515                                 if (BM_GetIndex(ls[0]) > BM_GetIndex(ls[1])) {
516                                         SWAP(BMLoop*, ls[0], ls[1]);
517                                 }
518
519                                 mface->mat_nr = f->mat_nr;
520                                 mface->flag = BMFlags_To_MEFlags(f);
521                                 
522                                 mface->v1 = BM_GetIndex(ls[0]->v);
523                                 mface->v2 = BM_GetIndex(ls[1]->v);
524                                 mface->v3 = BM_GetIndex(ls[2]->v);
525
526                                 test_index_face(mface, &me->fdata, i, 1);
527                                 
528                                 loops_to_corners(bm, me, i, f, ls, numTex, numCol);
529                                 VECCOPY(facenors, ls[0]->f->no);
530
531                                 mface++;
532                                 facenors += 3;
533                                 i++;
534                         }
535                         BLI_end_edgefill();
536                 }
537         }
538
539         i = 0;
540         j = 0;
541         BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
542                 mpoly->loopstart = j;
543                 mpoly->totloop = f->len;
544                 mpoly->mat_nr = f->mat_nr;
545                 mpoly->flag = BMFlags_To_MEFlags(f);
546
547                 l = BMIter_New(&liter, bm, BM_LOOPS_OF_FACE, f);
548                 for ( ; l; l=BMIter_Step(&liter), j++, mloop++) {
549                         mloop->e = BM_GetIndex(l->e);
550                         mloop->v = BM_GetIndex(l->v);
551
552                         /*copy over customdata*/
553                         CustomData_from_bmesh_block(&bm->ldata, &me->ldata, l->head.data, j);
554                         CHECK_ELEMENT(bm, l);
555                         CHECK_ELEMENT(bm, l->e);
556                         CHECK_ELEMENT(bm, l->v);
557                 }
558                 
559                 if (f == bm->act_face) me->act_face = i;
560
561                 /*copy over customdata*/
562                 CustomData_from_bmesh_block(&bm->pdata, &me->pdata, f->head.data, i);
563
564                 i++;
565                 mpoly++;
566                 CHECK_ELEMENT(bm, f);
567         }
568
569         /* patch hook indices and vertex parents */
570         {
571                 Object *ob;
572                 ModifierData *md;
573                 BMVert **vertMap = NULL;
574                 int i,j;
575
576                 for (ob=G.main->object.first; ob; ob=ob->id.next) {
577                         if (ob->parent==ob && ELEM(ob->partype, PARVERT1,PARVERT3)) {
578                                 
579                                 /* duplicate code from below, make it function later...? */
580                                 if (!vertMap) {
581                                         vertMap = MEM_callocN(sizeof(*vertMap)*ototvert, "vertMap");
582                                         
583                                         BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL) {
584                                                 keyi = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_SHAPE_KEYINDEX);
585                                                 if (*keyi != ORIGINDEX_NONE)
586                                                         vertMap[*keyi] = eve;
587                                         }
588                                 }
589                                 if(ob->par1 < ototvert) {
590                                         eve = vertMap[ob->par1];
591                                         if(eve) ob->par1= BM_GetIndex(eve);
592                                 }
593                                 if(ob->par2 < ototvert) {
594                                         eve = vertMap[ob->par2];
595                                         if(eve) ob->par2= BM_GetIndex(eve);
596                                 }
597                                 if(ob->par3 < ototvert) {
598                                         eve = vertMap[ob->par3];
599                                         if(eve) ob->par3= BM_GetIndex(eve);
600                                 }
601                                 
602                         }
603                         if (ob->data==me) {
604                                 for (md=ob->modifiers.first; md; md=md->next) {
605                                         if (md->type==eModifierType_Hook) {
606                                                 HookModifierData *hmd = (HookModifierData*) md;
607
608                                                 if (!vertMap) {
609                                                         vertMap = MEM_callocN(sizeof(*vertMap)*ototvert, "vertMap");
610                                                         
611                                                         BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL) {
612                                                                 keyi = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_SHAPE_KEYINDEX);
613                                                                 if (*keyi != ORIGINDEX_NONE)
614                                                                         vertMap[*keyi] = eve;
615                                                         }
616                                                 }
617                                                 
618                                                 for (i=j=0; i<hmd->totindex; i++) {
619                                                         if(hmd->indexar[i] < ototvert) {
620                                                                 eve = vertMap[hmd->indexar[i]];
621                                                                 
622                                                                 if (eve) {
623                                                                         hmd->indexar[j++] = BM_GetIndex(eve);
624                                                                 }
625                                                         }
626                                                         else j++;
627                                                 }
628
629                                                 hmd->totindex = j;
630                                         }
631                                 }
632                         }
633                 }
634
635                 if (vertMap) MEM_freeN(vertMap);
636         }
637
638         mesh_update_customdata_pointers(me);
639
640         if (me->key) {
641                 KeyBlock *actkey= BLI_findlink(&me->key->block, bm->shapenr-1);
642
643                 /*go through and find any shapekey customdata layers
644                   that might not have corrusponding KeyBlocks, and add them if
645                   necassary.*/
646                 j = 0;
647                 for (i=0; i<bm->vdata.totlayer; i++) {
648                         if (bm->vdata.layers[i].type != CD_SHAPEKEY)
649                                 continue;
650
651                         for (block=me->key->block.first; block; block=block->next) {
652                                 if (block->uid == bm->vdata.layers[i].uid)
653                                         break;
654                         }
655                         
656                         if (!block) {
657                                 block = MEM_callocN(sizeof(KeyBlock), "KeyBlock mesh_conv.c");
658                                 block->type = KEY_LINEAR;
659                                 block->slidermin = 0.0f;
660                                 block->slidermax = 1.0f;
661
662                                 BLI_addtail(&me->key->block, block);
663                                 me->key->totkey++;
664                         }
665
666                         j++;
667                 }
668
669                 for (block=me->key->block.first; block; block=block->next) {
670                         j = 0;
671
672                         for (i=0; i<bm->vdata.totlayer; i++) {
673                                 if (bm->vdata.layers[i].type != CD_SHAPEKEY)
674                                         continue;
675
676                                 if (block->uid == bm->vdata.layers[i].uid) {
677                                         float *fp, *co;
678
679                                         if (block->data)
680                                                 MEM_freeN(block->data);
681                                         block->data = fp = MEM_mallocN(sizeof(float)*3*bm->totvert, "shape key data");
682                                         block->totelem = bm->totvert;
683
684                                         BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL) {
685                                                 co = block==actkey ? eve->co : CustomData_bmesh_get_n(&bm->vdata, eve->head.data, CD_SHAPEKEY, j);
686                                                 
687                                                 VECCOPY(fp, co);
688                                                 fp += 3;
689                                         }
690                                         break;
691                                 }
692
693                                 j++;
694                         }
695
696                         /*if we didn't find a shapekey, tag the block to be reconstructed
697                           via the old method below*/
698                         if (j == CustomData_number_of_layers(&bm->vdata, CD_SHAPEKEY)) {
699                                 block->flag |= KEYBLOCK_MISSING;
700                         }
701                 }
702         }
703
704         /* old method of reconstructing keys via vertice's original key indices,
705            currently used if the new method above fails (which is theoretically
706            possible in certain cases of undo).*/
707         if(me->key) {
708                 float *fp, *newkey, *oldkey;
709                 KeyBlock *currkey;
710                 KeyBlock *actkey= BLI_findlink(&me->key->block, bm->shapenr-1);
711
712                 /* Lets reorder the key data so that things line up roughly
713                  * with the way things were before editmode */
714                 currkey = me->key->block.first;
715                 while(currkey) {
716                         if (!(currkey->flag & KEYBLOCK_MISSING)) {
717                                 currkey = currkey->next;
718                                 continue;
719                         }
720                         
721                         printf("warning: had to hackishly reconstruct shape key \"%s\","
722                                " it may not be correct anymore.\n", currkey->name);
723
724                         currkey->flag &= ~KEYBLOCK_MISSING;
725
726                         fp= newkey= MEM_callocN(me->key->elemsize*bm->totvert,  "currkey->data");
727                         oldkey = currkey->data;
728
729                         eve= BMIter_New(&iter, bm, BM_VERTS_OF_MESH, NULL);
730
731                         i = 0;
732                         mvert = me->mvert;
733                         while(eve) {
734                                 keyi = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_SHAPE_KEYINDEX);
735                                 if (*keyi >= 0 && *keyi < currkey->totelem) { // valid old vertex
736                                         if(currkey == actkey) {
737                                                 if(actkey == me->key->refkey) {
738                                                         VECCOPY(fp, mvert->co);
739                                                 }
740                                                 else {
741                                                         VECCOPY(fp, mvert->co);
742                                                         if(oldverts) {
743                                                                 VECCOPY(mvert->co, oldverts[*keyi].co);
744                                                         }
745                                                 }
746                                         }
747                                         else {
748                                                 if(oldkey) {
749                                                         VECCOPY(fp, oldkey + 3 * *keyi);
750                                                 }
751                                         }
752                                 }
753                                 else {
754                                         VECCOPY(fp, mvert->co);
755                                 }
756                                 fp+= 3;
757                                 ++i;
758                                 ++mvert;
759                                 eve= BMIter_Step(&iter);
760                         }
761                         currkey->totelem= bm->totvert;
762                         if(currkey->data) MEM_freeN(currkey->data);
763                         currkey->data = newkey;
764                         
765                         currkey= currkey->next;
766                 }
767         }
768
769         if(oldverts) MEM_freeN(oldverts);
770 }