svn merge -r38500:38600 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         {
251                 BMIter iter;
252                 BMVert *vertex;
253                 BMEdge *edge;
254                 BMFace *face;
255                 BMVert **vertex_array = MEM_callocN(sizeof(BMVert *) * bm->totvert,
256                                                   "Selection Conversion Vertex Pointer Array");
257                 BMEdge **edge_array = MEM_callocN(sizeof(BMEdge *) * bm->totedge,
258                                                 "Selection Conversion Edge Pointer Array");
259                 BMFace **face_array = MEM_callocN(sizeof(BMFace *) * bm->totface,
260                                                 "Selection Conversion Face Pointer Array");
261
262                 for(i = 0, vertex = BMIter_New(&iter, bm, BM_VERTS_OF_MESH, NULL);
263                     vertex; i++, vertex = BMIter_Step(&iter)){
264                         vertex_array[i] = vertex;
265                 }
266
267                 for(i = 0, edge = BMIter_New(&iter, bm, BM_EDGES_OF_MESH, NULL);
268                     edge; i++, edge = BMIter_Step(&iter)){
269                         edge_array[i] = edge;
270                 }
271
272                 for(i = 0, face = BMIter_New(&iter, bm, BM_FACES_OF_MESH, NULL);
273                     face; i++, face = BMIter_Step(&iter)){
274                         face_array[i] = face;
275                 }
276
277                 if(me->mselect) {
278                         for(i = 0; i < me->totselect; i++){
279                                 if(me->mselect[i].type == ME_VSEL){
280                                         BM_store_selection(bm, vertex_array[me->mselect[i].index]);
281                                 }else if(me->mselect[i].type == ME_ESEL){
282                                         BM_store_selection(bm, edge_array[me->mselect[i].index]);
283                                 }else if(me->mselect[i].type == ME_FSEL){
284                                         BM_store_selection(bm, face_array[me->mselect[i].index]);
285                                 }
286                         }
287                 }
288                 else {
289                         me->totselect= 0;
290                 }
291
292                 MEM_freeN(vertex_array);
293                 MEM_freeN(edge_array);
294                 MEM_freeN(face_array);
295         }
296
297         BLI_array_free(fedges);
298         BLI_array_free(verts);
299         
300         MEM_freeN(vt);
301         MEM_freeN(et);
302 }
303
304
305 static void loops_to_corners(BMesh *bm, Mesh *me, int findex,
306                              BMFace *f, BMLoop *ls[3], int numTex, int numCol) 
307 {
308         BMLoop *l;
309         MTFace *texface;
310         MTexPoly *texpoly;
311         MCol *mcol;
312         MLoopCol *mloopcol;
313         MLoopUV *mloopuv;
314         int i, j;
315
316         for(i=0; i < numTex; i++){
317                 texface = CustomData_get_n(&me->fdata, CD_MTFACE, findex, i);
318                 texpoly = CustomData_bmesh_get_n(&bm->pdata, f->head.data, CD_MTEXPOLY, i);
319                 
320                 texface->tpage = texpoly->tpage;
321                 texface->flag = texpoly->flag;
322                 texface->transp = texpoly->transp;
323                 texface->mode = texpoly->mode;
324                 texface->tile = texpoly->tile;
325                 texface->unwrap = texpoly->unwrap;
326
327                 for (j=0; j<3; j++) {
328                         l = ls[j];
329                         mloopuv = CustomData_bmesh_get_n(&bm->ldata, l->head.data, CD_MLOOPUV, i);
330                         texface->uv[j][0] = mloopuv->uv[0];
331                         texface->uv[j][1] = mloopuv->uv[1];
332                 }
333         }
334
335         for(i=0; i < numCol; i++){
336                 mcol = CustomData_get_n(&me->fdata, CD_MCOL, findex, i);
337
338                 for (j=0; j<3; j++) {
339                         l = ls[j];
340                         mloopcol = CustomData_bmesh_get_n(&bm->ldata, l->head.data, CD_MLOOPCOL, i);
341                         mcol[j].r = mloopcol->r;
342                         mcol[j].g = mloopcol->g;
343                         mcol[j].b = mloopcol->b;
344                         mcol[j].a = mloopcol->a;
345                 }
346         }
347 }
348
349 void object_load_bmesh_exec(BMesh *bm, BMOperator *op) {
350         Object *ob = BMO_Get_Pnt(op, "object");
351         /* Scene *scene = BMO_Get_Pnt(op, "scene"); */
352         Mesh *me = ob->data;
353
354         BMO_CallOpf(bm, "bmesh_to_mesh mesh=%p object=%p", me, ob);
355 }
356
357 void bmesh_to_mesh_exec(BMesh *bm, BMOperator *op) {
358         Mesh *me = BMO_Get_Pnt(op, "mesh");
359         /* Object *ob = BMO_Get_Pnt(op, "object"); */
360         MLoop *mloop;
361         KeyBlock *block;
362         MPoly *mpoly;
363         MVert *mvert, *oldverts;
364         MEdge *medge;
365         MFace *mface;
366         BMVert *v, *eve;
367         BMEdge *e;
368         BMLoop *l;
369         BMFace *f;
370         BMIter iter, liter;
371         float *facenors = NULL;
372         int i, j, *keyi, ototvert, totloop, totface, numTex, numCol;
373         int dotess = !BMO_Get_Int(op, "notesselation");
374
375         numTex = CustomData_number_of_layers(&bm->pdata, CD_MTEXPOLY);
376         numCol = CustomData_number_of_layers(&bm->ldata, CD_MLOOPCOL);
377         
378         ototvert = me->totvert;
379
380         /* new Vertex block */
381         if(bm->totvert==0) mvert= NULL;
382         else mvert= MEM_callocN(bm->totvert*sizeof(MVert), "loadeditbMesh vert");
383
384         /* new Edge block */
385         if(bm->totedge==0) medge= NULL;
386         else medge= MEM_callocN(bm->totedge*sizeof(MEdge), "loadeditbMesh edge");
387         
388         /*build ngon data*/
389         /* new Ngon Face block */
390         if(bm->totface==0) mpoly = NULL;
391         else mpoly= MEM_callocN(bm->totface*sizeof(MPoly), "loadeditbMesh poly");
392         
393         /*find number of loops to allocate*/
394         totloop = 0;
395         BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
396                 totloop += f->len;
397         }
398
399         if (totloop==0) mloop = NULL;
400         else mloop = MEM_callocN(totloop*sizeof(MLoop), "loadeditbMesh loop");
401
402         /* lets save the old verts just in case we are actually working on
403          * a key ... we now do processing of the keys at the end */
404         oldverts= me->mvert;
405
406         /* don't free this yet */
407         CustomData_set_layer(&me->vdata, CD_MVERT, NULL);
408
409         /* free custom data */
410         CustomData_free(&me->vdata, me->totvert);
411         CustomData_free(&me->edata, me->totedge);
412         CustomData_free(&me->fdata, me->totface);
413         CustomData_free(&me->ldata, me->totloop);
414         CustomData_free(&me->pdata, me->totpoly);
415
416         /* add new custom data */
417         me->totvert= bm->totvert;
418         me->totedge= bm->totedge;
419         me->totloop= totloop;
420         me->totpoly= bm->totface;
421
422         CustomData_copy(&bm->vdata, &me->vdata, CD_MASK_MESH, CD_CALLOC, me->totvert);
423         CustomData_copy(&bm->edata, &me->edata, CD_MASK_MESH, CD_CALLOC, me->totedge);
424         CustomData_copy(&bm->ldata, &me->ldata, CD_MASK_MESH, CD_CALLOC, me->totloop);
425         CustomData_copy(&bm->pdata, &me->pdata, CD_MASK_MESH, CD_CALLOC, me->totpoly);
426
427         CustomData_add_layer(&me->vdata, CD_MVERT, CD_ASSIGN, mvert, me->totvert);
428         CustomData_add_layer(&me->edata, CD_MEDGE, CD_ASSIGN, medge, me->totedge);
429         CustomData_add_layer(&me->ldata, CD_MLOOP, CD_ASSIGN, mloop, me->totloop);
430         CustomData_add_layer(&me->pdata, CD_MPOLY, CD_ASSIGN, mpoly, me->totpoly);
431         
432         i = 0;
433         BM_ITER(v, &iter, bm, BM_VERTS_OF_MESH, NULL) {
434                 float *bweight = CustomData_bmesh_get(&bm->vdata, v->head.data, CD_BWEIGHT);
435
436                 mvert->bweight = bweight ? (char)((*bweight)*255) : 0;
437
438                 VECCOPY(mvert->co, v->co);
439
440                 mvert->no[0] = (short) (v->no[0]*32767.0f);
441                 mvert->no[1] = (short) (v->no[1]*32767.0f);
442                 mvert->no[2] = (short) (v->no[2]*32767.0f);
443                 
444                 mvert->flag = BMFlags_To_MEFlags(v);
445
446                 BM_SetIndex(v, i);
447
448                 /*copy over customdata*/
449                 CustomData_from_bmesh_block(&bm->vdata, &me->vdata, v->head.data, i);
450
451                 i++;
452                 mvert++;
453
454                 CHECK_ELEMENT(bm, v);
455         }
456
457         i = 0;
458         BM_ITER(e, &iter, bm, BM_EDGES_OF_MESH, NULL) {
459                 float *crease = CustomData_bmesh_get(&bm->edata, e->head.data, CD_CREASE);
460                 float *bweight = CustomData_bmesh_get(&bm->edata, e->head.data, CD_BWEIGHT);
461                 
462                 medge->v1 = BM_GetIndex(e->v1);
463                 medge->v2 = BM_GetIndex(e->v2);
464                 medge->crease = crease ? (char)((*crease)*255) : 0;
465                 medge->bweight = bweight ? (char)((*bweight)*255) : 0;
466                 
467                 medge->flag = BMFlags_To_MEFlags(e);
468                 
469                 BM_SetIndex(e, i);
470
471                 /*copy over customdata*/
472                 CustomData_from_bmesh_block(&bm->edata, &me->edata, e->head.data, i);
473
474                 i++;
475                 medge++;
476                 CHECK_ELEMENT(bm, e);
477         }
478
479         /*new scanfill tesselation code*/
480         if (dotess) {
481                 /*first counter number of faces we'll need*/
482                 totface = 0;
483                 BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
484                         EditVert *eve, *lasteve = NULL, *firsteve = NULL;
485                         
486                         BLI_begin_edgefill();
487                         i = 0;
488                         BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
489                                 eve = BLI_addfillvert(l->v->co);
490                                 eve->tmp.p = l;
491                                 
492                                 BM_SetIndex(l, i);
493
494                                 if (lasteve) {
495                                         BLI_addfilledge(lasteve, eve);
496                                 }
497
498                                 lasteve = eve;
499                                 if (!firsteve) firsteve = eve;
500
501                                 i++;
502                         }
503
504                         BLI_addfilledge(lasteve, firsteve);
505                         totface += BLI_edgefill(0);
506
507                         BLI_end_edgefill();
508                 }
509                 
510                 me->totface = totface;
511
512                 /* new tess face block */
513                 if(totface==0) mface= NULL;
514                 else {
515                         mface= MEM_callocN(totface*sizeof(MFace), "loadeditbMesh face");
516                         facenors = MEM_callocN(totface*sizeof(float)*3, "facenors");
517                 }
518
519                 CustomData_add_layer(&me->fdata, CD_MFACE, CD_ASSIGN, mface, me->totface);
520                 CustomData_add_layer(&me->fdata, CD_NORMAL, CD_ASSIGN, facenors, me->totface);
521                 CustomData_from_bmeshpoly(&me->fdata, &bm->pdata, &bm->ldata, totface);
522
523                 mesh_update_customdata_pointers(me);
524                 
525                 i = 0;
526                 BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
527                         EditVert *eve, *lasteve = NULL, *firsteve = NULL;
528                         EditFace *efa;
529                         BMLoop *ls[3];
530                         
531                         BLI_begin_edgefill();
532                         BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
533                                 eve = BLI_addfillvert(l->v->co);
534                                 eve->tmp.p = l;
535
536                                 if (lasteve) {
537                                         BLI_addfilledge(lasteve, eve);
538                                 }
539
540                                 lasteve = eve;
541                                 if (!firsteve) firsteve = eve;
542                         }
543
544                         BLI_addfilledge(lasteve, firsteve);
545                         BLI_edgefill(0);
546
547                         for (efa=fillfacebase.first; efa; efa=efa->next) {
548                                 ls[0] = efa->v1->tmp.p;
549                                 ls[1] = efa->v2->tmp.p;
550                                 ls[2] = efa->v3->tmp.p;
551                                 
552                                 /*ensure correct winding.  I believe this is
553                                   analogous to bubble sort on three elements.*/
554                                 if (BM_GetIndex(ls[0]) > BM_GetIndex(ls[1])) {
555                                         SWAP(BMLoop*, ls[0], ls[1]);
556                                 }
557                                 if (BM_GetIndex(ls[1]) > BM_GetIndex(ls[2])) {
558                                         SWAP(BMLoop*, ls[1], ls[2]);
559                                 }
560                                 if (BM_GetIndex(ls[0]) > BM_GetIndex(ls[1])) {
561                                         SWAP(BMLoop*, ls[0], ls[1]);
562                                 }
563
564                                 mface->mat_nr = f->mat_nr;
565                                 mface->flag = BMFlags_To_MEFlags(f);
566                                 
567                                 mface->v1 = BM_GetIndex(ls[0]->v);
568                                 mface->v2 = BM_GetIndex(ls[1]->v);
569                                 mface->v3 = BM_GetIndex(ls[2]->v);
570
571                                 test_index_face(mface, &me->fdata, i, 1);
572                                 
573                                 loops_to_corners(bm, me, i, f, ls, numTex, numCol);
574                                 VECCOPY(facenors, ls[0]->f->no);
575
576                                 mface++;
577                                 facenors += 3;
578                                 i++;
579                         }
580                         BLI_end_edgefill();
581                 }
582         }
583
584         i = 0;
585         j = 0;
586         BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
587                 mpoly->loopstart = j;
588                 mpoly->totloop = f->len;
589                 mpoly->mat_nr = f->mat_nr;
590                 mpoly->flag = BMFlags_To_MEFlags(f);
591
592                 l = BMIter_New(&liter, bm, BM_LOOPS_OF_FACE, f);
593                 for ( ; l; l=BMIter_Step(&liter), j++, mloop++) {
594                         mloop->e = BM_GetIndex(l->e);
595                         mloop->v = BM_GetIndex(l->v);
596
597                         /*copy over customdata*/
598                         CustomData_from_bmesh_block(&bm->ldata, &me->ldata, l->head.data, j);
599                         CHECK_ELEMENT(bm, l);
600                         CHECK_ELEMENT(bm, l->e);
601                         CHECK_ELEMENT(bm, l->v);
602                 }
603                 
604                 if (f == bm->act_face) me->act_face = i;
605
606                 /*copy over customdata*/
607                 CustomData_from_bmesh_block(&bm->pdata, &me->pdata, f->head.data, i);
608
609                 i++;
610                 mpoly++;
611                 CHECK_ELEMENT(bm, f);
612         }
613
614         /* patch hook indices and vertex parents */
615         {
616                 Object *ob;
617                 ModifierData *md;
618                 BMVert **vertMap = NULL;
619                 int i,j;
620
621                 for (ob=G.main->object.first; ob; ob=ob->id.next) {
622                         if (ob->parent==ob && ELEM(ob->partype, PARVERT1,PARVERT3)) {
623                                 
624                                 /* duplicate code from below, make it function later...? */
625                                 if (!vertMap) {
626                                         vertMap = MEM_callocN(sizeof(*vertMap)*ototvert, "vertMap");
627                                         
628                                         BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL) {
629                                                 keyi = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_SHAPE_KEYINDEX);
630                                                 if (*keyi != ORIGINDEX_NONE)
631                                                         vertMap[*keyi] = eve;
632                                         }
633                                 }
634                                 if(ob->par1 < ototvert) {
635                                         eve = vertMap[ob->par1];
636                                         if(eve) ob->par1= BM_GetIndex(eve);
637                                 }
638                                 if(ob->par2 < ototvert) {
639                                         eve = vertMap[ob->par2];
640                                         if(eve) ob->par2= BM_GetIndex(eve);
641                                 }
642                                 if(ob->par3 < ototvert) {
643                                         eve = vertMap[ob->par3];
644                                         if(eve) ob->par3= BM_GetIndex(eve);
645                                 }
646                                 
647                         }
648                         if (ob->data==me) {
649                                 for (md=ob->modifiers.first; md; md=md->next) {
650                                         if (md->type==eModifierType_Hook) {
651                                                 HookModifierData *hmd = (HookModifierData*) md;
652
653                                                 if (!vertMap) {
654                                                         vertMap = MEM_callocN(sizeof(*vertMap)*ototvert, "vertMap");
655                                                         
656                                                         BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL) {
657                                                                 keyi = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_SHAPE_KEYINDEX);
658                                                                 if (*keyi != ORIGINDEX_NONE)
659                                                                         vertMap[*keyi] = eve;
660                                                         }
661                                                 }
662                                                 
663                                                 for (i=j=0; i<hmd->totindex; i++) {
664                                                         if(hmd->indexar[i] < ototvert) {
665                                                                 eve = vertMap[hmd->indexar[i]];
666                                                                 
667                                                                 if (eve) {
668                                                                         hmd->indexar[j++] = BM_GetIndex(eve);
669                                                                 }
670                                                         }
671                                                         else j++;
672                                                 }
673
674                                                 hmd->totindex = j;
675                                         }
676                                 }
677                         }
678                 }
679
680                 if (vertMap) MEM_freeN(vertMap);
681         }
682
683         mesh_update_customdata_pointers(me);
684
685         {
686                 BMEditSelection *selected;
687                 me->totselect = BLI_countlist(&(bm->selected));
688
689                 me->mselect = MEM_callocN(sizeof(MSelect) * me->totselect, "Mesh selection history");
690
691
692                 for(i = 0, selected = bm->selected.first; selected; i++, selected = selected->next){
693                         if(selected->type == BM_VERT){
694                                 me->mselect[i].type = ME_VSEL;
695
696                         }else if(selected->type == BM_EDGE){
697                                 me->mselect[i].type = ME_ESEL;
698
699                         }else if(selected->type == BM_FACE){
700                                 me->mselect[i].type = ME_FSEL;
701                         }
702
703                         me->mselect[i].index = BM_GetIndex(selected->data);
704                 }
705         }
706
707         if (me->key) {
708                 KeyBlock *actkey= BLI_findlink(&me->key->block, bm->shapenr-1);
709
710                 /*go through and find any shapekey customdata layers
711                   that might not have corrusponding KeyBlocks, and add them if
712                   necassary.*/
713                 j = 0;
714                 for (i=0; i<bm->vdata.totlayer; i++) {
715                         if (bm->vdata.layers[i].type != CD_SHAPEKEY)
716                                 continue;
717
718                         for (block=me->key->block.first; block; block=block->next) {
719                                 if (block->uid == bm->vdata.layers[i].uid)
720                                         break;
721                         }
722                         
723                         if (!block) {
724                                 block = MEM_callocN(sizeof(KeyBlock), "KeyBlock mesh_conv.c");
725                                 block->type = KEY_LINEAR;
726                                 block->slidermin = 0.0f;
727                                 block->slidermax = 1.0f;
728
729                                 BLI_addtail(&me->key->block, block);
730                                 me->key->totkey++;
731                         }
732
733                         j++;
734                 }
735
736                 for (block=me->key->block.first; block; block=block->next) {
737                         j = 0;
738
739                         for (i=0; i<bm->vdata.totlayer; i++) {
740                                 if (bm->vdata.layers[i].type != CD_SHAPEKEY)
741                                         continue;
742
743                                 if (block->uid == bm->vdata.layers[i].uid) {
744                                         float *fp, *co;
745
746                                         if (block->data)
747                                                 MEM_freeN(block->data);
748                                         block->data = fp = MEM_mallocN(sizeof(float)*3*bm->totvert, "shape key data");
749                                         block->totelem = bm->totvert;
750
751                                         BM_ITER(eve, &iter, bm, BM_VERTS_OF_MESH, NULL) {
752                                                 co = block==actkey ? eve->co : CustomData_bmesh_get_n(&bm->vdata, eve->head.data, CD_SHAPEKEY, j);
753                                                 
754                                                 VECCOPY(fp, co);
755                                                 fp += 3;
756                                         }
757                                         break;
758                                 }
759
760                                 j++;
761                         }
762
763                         /*if we didn't find a shapekey, tag the block to be reconstructed
764                           via the old method below*/
765                         if (j == CustomData_number_of_layers(&bm->vdata, CD_SHAPEKEY)) {
766                                 block->flag |= KEYBLOCK_MISSING;
767                         }
768                 }
769         }
770
771         /* old method of reconstructing keys via vertice's original key indices,
772            currently used if the new method above fails (which is theoretically
773            possible in certain cases of undo).*/
774         if(me->key) {
775                 float *fp, *newkey, *oldkey;
776                 KeyBlock *currkey;
777                 KeyBlock *actkey= BLI_findlink(&me->key->block, bm->shapenr-1);
778
779                 /* Lets reorder the key data so that things line up roughly
780                  * with the way things were before editmode */
781                 currkey = me->key->block.first;
782                 while(currkey) {
783                         if (!(currkey->flag & KEYBLOCK_MISSING)) {
784                                 currkey = currkey->next;
785                                 continue;
786                         }
787                         
788                         printf("warning: had to hackishly reconstruct shape key \"%s\","
789                                " it may not be correct anymore.\n", currkey->name);
790
791                         currkey->flag &= ~KEYBLOCK_MISSING;
792
793                         fp= newkey= MEM_callocN(me->key->elemsize*bm->totvert,  "currkey->data");
794                         oldkey = currkey->data;
795
796                         eve= BMIter_New(&iter, bm, BM_VERTS_OF_MESH, NULL);
797
798                         i = 0;
799                         mvert = me->mvert;
800                         while(eve) {
801                                 keyi = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_SHAPE_KEYINDEX);
802                                 if (*keyi >= 0 && *keyi < currkey->totelem) { // valid old vertex
803                                         if(currkey == actkey) {
804                                                 if(actkey == me->key->refkey) {
805                                                         VECCOPY(fp, mvert->co);
806                                                 }
807                                                 else {
808                                                         VECCOPY(fp, mvert->co);
809                                                         if(oldverts) {
810                                                                 VECCOPY(mvert->co, oldverts[*keyi].co);
811                                                         }
812                                                 }
813                                         }
814                                         else {
815                                                 if(oldkey) {
816                                                         VECCOPY(fp, oldkey + 3 * *keyi);
817                                                 }
818                                         }
819                                 }
820                                 else {
821                                         VECCOPY(fp, mvert->co);
822                                 }
823                                 fp+= 3;
824                                 ++i;
825                                 ++mvert;
826                                 eve= BMIter_Step(&iter);
827                         }
828                         currkey->totelem= bm->totvert;
829                         if(currkey->data) MEM_freeN(currkey->data);
830                         currkey->data = newkey;
831                         
832                         currkey= currkey->next;
833                 }
834         }
835
836         if(oldverts) MEM_freeN(oldverts);
837 }