creating the editmesh structure was running paint_facesel_test() for every vert and...
[blender.git] / source / blender / editors / mesh / editmesh.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation, full recode 2002-2008
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28
29 #include <stdlib.h>
30 #include <string.h>
31 #include <math.h>
32
33 #include "MEM_guardedalloc.h"
34
35 #include "DNA_scene_types.h"
36 #include "DNA_object_types.h"
37 #include "DNA_key_types.h"
38 #include "DNA_meshdata_types.h"
39 #include "DNA_modifier_types.h"
40
41 #include "BLI_blenlib.h"
42 #include "BLI_math.h"
43 #include "BLI_editVert.h"
44 #include "BLI_dynstr.h"
45 #include "BLI_rand.h"
46
47 #include "BKE_DerivedMesh.h"
48 #include "BKE_context.h"
49 #include "BKE_depsgraph.h"
50 #include "BKE_global.h"
51 #include "BKE_key.h"
52 #include "BKE_main.h"
53 #include "BKE_material.h"
54 #include "BKE_mesh.h"
55 #include "BKE_paint.h"
56 #include "BKE_report.h"
57 #include "BKE_multires.h"
58
59 #include "ED_mesh.h"
60 #include "ED_object.h"
61 #include "ED_retopo.h"
62 #include "ED_screen.h"
63 #include "ED_util.h"
64 #include "ED_view3d.h"
65
66 #include "RNA_access.h"
67 #include "RNA_define.h"
68
69 #include "WM_api.h"
70 #include "WM_types.h"
71
72 /* own include */
73 #include "mesh_intern.h"
74
75 /* 
76 editmesh.c:
77         - add/alloc/free data
78         - hashtables
79         - enter/exit editmode
80 */
81
82 /* XXX */
83 static void BIF_undo_push(const char *UNUSED(arg)) {}
84 static void error(const char *UNUSED(arg)) {}
85
86
87 /* ***************** HASH ********************* */
88
89
90 #define EDHASHSIZE              (512*512)
91 #define EDHASH(a, b)    (a % EDHASHSIZE)
92
93
94 /* ************ ADD / REMOVE / FIND ****************** */
95
96 static void *calloc_em(EditMesh *UNUSED(em), size_t size, size_t nr)
97 {
98         return calloc(size, nr);
99 }
100
101 /* used to bypass normal calloc with fast one */
102 static void *(*callocvert)(EditMesh *, size_t, size_t) = calloc_em;
103 static void *(*callocedge)(EditMesh *, size_t, size_t) = calloc_em;
104 static void *(*callocface)(EditMesh *, size_t, size_t) = calloc_em;
105
106 EditVert *addvertlist(EditMesh *em, float *vec, EditVert *example)
107 {
108         EditVert *eve;
109         static int hashnr= 0;
110
111         eve= callocvert(em, sizeof(EditVert), 1);
112         BLI_addtail(&em->verts, eve);
113         em->totvert++;
114         
115         if(vec) VECCOPY(eve->co, vec);
116
117         eve->hash= hashnr++;
118         if( hashnr>=EDHASHSIZE) hashnr= 0;
119
120         /* new verts get keyindex of -1 since they did not
121          * have a pre-editmode vertex order
122          */
123         eve->keyindex = -1;
124
125         if(example) {
126                 CustomData_em_copy_data(&em->vdata, &em->vdata, example->data, &eve->data);
127                 eve->bweight = example->bweight;
128         }
129         else {
130                 CustomData_em_set_default(&em->vdata, &eve->data);
131         }
132
133         return eve;
134 }
135
136 void free_editvert (EditMesh *em, EditVert *eve)
137 {
138
139         EM_remove_selection(em, eve, EDITVERT);
140         CustomData_em_free_block(&em->vdata, &eve->data);
141         if(eve->fast==0)
142                 free(eve);
143         
144         em->totvert--;
145 }
146
147
148 EditEdge *findedgelist(EditMesh *em, EditVert *v1, EditVert *v2)
149 {
150         EditVert *v3;
151         struct HashEdge *he;
152
153         /* swap ? */
154         if( v1 > v2) {
155                 v3= v2; 
156                 v2= v1; 
157                 v1= v3;
158         }
159         
160         if(em->hashedgetab==NULL)
161                 em->hashedgetab= MEM_callocN(EDHASHSIZE*sizeof(struct HashEdge), "hashedgetab");
162
163         he= em->hashedgetab + EDHASH(v1->hash, v2->hash);
164         
165         while(he) {
166                 
167                 if(he->eed && he->eed->v1==v1 && he->eed->v2==v2) return he->eed;
168                 
169                 he= he->next;
170         }
171         return 0;
172 }
173
174 static void insert_hashedge(EditMesh *em, EditEdge *eed)
175 {
176         /* assuming that eed is not in the list yet, and that a find has been done before */
177         
178         struct HashEdge *first, *he;
179
180         first= em->hashedgetab + EDHASH(eed->v1->hash, eed->v2->hash);
181
182         if( first->eed==0 ) {
183                 first->eed= eed;
184         }
185         else {
186                 he= &eed->hash; 
187                 he->eed= eed;
188                 he->next= first->next;
189                 first->next= he;
190         }
191 }
192
193 static void remove_hashedge(EditMesh *em, EditEdge *eed)
194 {
195         /* assuming eed is in the list */
196         
197         struct HashEdge *first, *he, *prev=NULL;
198
199         he=first= em->hashedgetab + EDHASH(eed->v1->hash, eed->v2->hash);
200
201         while(he) {
202                 if(he->eed == eed) {
203                         /* remove from list */
204                         if(he==first) {
205                                 if(first->next) {
206                                         he= first->next;
207                                         first->eed= he->eed;
208                                         first->next= he->next;
209                                 }
210                                 else he->eed= 0;
211                         }
212                         else {
213                                 prev->next= he->next;
214                         }
215                         return;
216                 }
217                 prev= he;
218                 he= he->next;
219         }
220 }
221
222 EditEdge *addedgelist(EditMesh *em, EditVert *v1, EditVert *v2, EditEdge *example)
223 {
224         EditVert *v3;
225         EditEdge *eed;
226         int swap= 0;
227         
228         if(v1==v2) return NULL;
229         if(v1==NULL || v2==NULL) return NULL;
230
231         /* swap ? */
232         if(v1>v2) {
233                 v3= v2; 
234                 v2= v1; 
235                 v1= v3;
236                 swap= 1;
237         }
238         
239         /* find in hashlist */
240         eed= findedgelist(em, v1, v2);
241
242         if(eed==NULL) {
243         
244                 eed= (EditEdge *)callocedge(em, sizeof(EditEdge), 1);
245                 eed->v1= v1;
246                 eed->v2= v2;
247                 BLI_addtail(&em->edges, eed);
248                 eed->dir= swap;
249                 insert_hashedge(em, eed);
250                 em->totedge++;
251                 
252                 /* copy edge data:
253                    rule is to do this with addedgelist call, before addfacelist */
254                 if(example) {
255                         eed->crease= example->crease;
256                         eed->bweight= example->bweight;
257                         eed->sharp = example->sharp;
258                         eed->seam = example->seam;
259                         eed->h |= (example->h & EM_FGON);
260                 }
261         }
262
263         return eed;
264 }
265
266 void remedge(EditMesh *em, EditEdge *eed)
267 {
268         BLI_remlink(&em->edges, eed);
269         remove_hashedge(em, eed);
270         
271         em->totedge--;
272 }
273
274 void free_editedge(EditMesh *em, EditEdge *eed)
275 {
276         EM_remove_selection(em, eed, EDITEDGE);
277         if(eed->fast==0){ 
278                 free(eed);
279         }
280 }
281
282 void free_editface(EditMesh *em, EditFace *efa)
283 {
284         EM_remove_selection(em, efa, EDITFACE);
285         
286         if (em->act_face==efa) {
287                 EM_set_actFace(em, em->faces.first == efa ? NULL : em->faces.first);
288         }
289                 
290         CustomData_em_free_block(&em->fdata, &efa->data);
291         if(efa->fast==0)
292                 free(efa);
293         
294         em->totface--;
295 }
296
297 void free_vertlist(EditMesh *em, ListBase *edve) 
298 {
299         EditVert *eve, *next;
300
301         if (!edve) return;
302
303         eve= edve->first;
304         while(eve) {
305                 next= eve->next;
306                 free_editvert(em, eve);
307                 eve= next;
308         }
309         edve->first= edve->last= NULL;
310         em->totvert= em->totvertsel= 0;
311 }
312
313 void free_edgelist(EditMesh *em, ListBase *lb)
314 {
315         EditEdge *eed, *next;
316         
317         eed= lb->first;
318         while(eed) {
319                 next= eed->next;
320                 free_editedge(em, eed);
321                 eed= next;
322         }
323         lb->first= lb->last= NULL;
324         em->totedge= em->totedgesel= 0;
325 }
326
327 void free_facelist(EditMesh *em, ListBase *lb)
328 {
329         EditFace *efa, *next;
330         
331         efa= lb->first;
332         while(efa) {
333                 next= efa->next;
334                 free_editface(em, efa);
335                 efa= next;
336         }
337         lb->first= lb->last= NULL;
338         em->totface= em->totfacesel= 0;
339 }
340
341 EditFace *addfacelist(EditMesh *em, EditVert *v1, EditVert *v2, EditVert *v3, EditVert *v4, EditFace *example, EditFace *exampleEdges)
342 {
343         EditFace *efa;
344         EditEdge *e1, *e2=0, *e3=0, *e4=0;
345
346         /* added sanity check... seems to happen for some tools, or for enter editmode for corrupted meshes */
347         if(v1==v4 || v2==v4 || v3==v4) v4= NULL;
348         
349         /* add face to list and do the edges */
350         if(exampleEdges) {
351                 e1= addedgelist(em, v1, v2, exampleEdges->e1);
352                 e2= addedgelist(em, v2, v3, exampleEdges->e2);
353                 if(v4) e3= addedgelist(em, v3, v4, exampleEdges->e3); 
354                 else e3= addedgelist(em, v3, v1, exampleEdges->e3);
355                 if(v4) e4= addedgelist(em, v4, v1, exampleEdges->e4);
356         }
357         else {
358                 e1= addedgelist(em, v1, v2, NULL);
359                 e2= addedgelist(em, v2, v3, NULL);
360                 if(v4) e3= addedgelist(em, v3, v4, NULL); 
361                 else e3= addedgelist(em, v3, v1, NULL);
362                 if(v4) e4= addedgelist(em, v4, v1, NULL);
363         }
364         
365         if(v1==v2 || v2==v3 || v1==v3) return NULL;
366         if(e2==0) return NULL;
367
368         efa= (EditFace *)callocface(em, sizeof(EditFace), 1);
369         efa->v1= v1;
370         efa->v2= v2;
371         efa->v3= v3;
372         efa->v4= v4;
373
374         efa->e1= e1;
375         efa->e2= e2;
376         efa->e3= e3;
377         efa->e4= e4;
378
379         if(example) {
380                 efa->mat_nr= example->mat_nr;
381                 efa->flag= example->flag;
382                 CustomData_em_copy_data(&em->fdata, &em->fdata, example->data, &efa->data);
383         }
384         else {
385                 efa->mat_nr= em->mat_nr;
386
387                 CustomData_em_set_default(&em->fdata, &efa->data);
388         }
389
390         BLI_addtail(&em->faces, efa);
391         em->totface++;
392         
393         if(efa->v4) {
394                 normal_quad_v3( efa->n,efa->v1->co, efa->v2->co, efa->v3->co, efa->v4->co);
395                 cent_quad_v3(efa->cent, efa->v1->co, efa->v2->co, efa->v3->co, efa->v4->co);
396         }
397         else {
398                 normal_tri_v3( efa->n,efa->v1->co, efa->v2->co, efa->v3->co);
399                 cent_tri_v3(efa->cent, efa->v1->co, efa->v2->co, efa->v3->co);
400         }
401
402         return efa;
403 }
404
405 /* ************************ end add/new/find ************  */
406
407 /* ************************ Edit{Vert,Edge,Face} utilss ***************************** */
408
409 /* some nice utility functions */
410
411 EditVert *editedge_getOtherVert(EditEdge *eed, EditVert *eve)
412 {
413         if (eve==eed->v1) {
414                 return eed->v2;
415         } else if (eve==eed->v2) {
416                 return eed->v1;
417         } else {
418                 return NULL;
419         }
420 }
421
422 EditVert *editedge_getSharedVert(EditEdge *eed, EditEdge *eed2) 
423 {
424         if (eed->v1==eed2->v1 || eed->v1==eed2->v2) {
425                 return eed->v1;
426         } else if (eed->v2==eed2->v1 || eed->v2==eed2->v2) {
427                 return eed->v2;
428         } else {
429                 return NULL;
430         }
431 }
432
433 int editedge_containsVert(EditEdge *eed, EditVert *eve) 
434 {
435         return (eed->v1==eve || eed->v2==eve);
436 }
437
438 int editface_containsVert(EditFace *efa, EditVert *eve) 
439 {
440         return (efa->v1==eve || efa->v2==eve || efa->v3==eve || (efa->v4 && efa->v4==eve));
441 }
442
443 int editface_containsEdge(EditFace *efa, EditEdge *eed) 
444 {
445         return (efa->e1==eed || efa->e2==eed || efa->e3==eed || (efa->e4 && efa->e4==eed));
446 }
447
448
449 /* ************************ stuct EditMesh manipulation ***************************** */
450
451 /* fake callocs for fastmalloc below */
452 static void *calloc_fastvert(EditMesh *em, size_t UNUSED(size), size_t UNUSED(nr))
453 {
454         EditVert *eve= em->curvert++;
455         eve->fast= 1;
456         return eve;
457 }
458 static void *calloc_fastedge(EditMesh *em, size_t UNUSED(size), size_t UNUSED(nr))
459 {
460         EditEdge *eed= em->curedge++;
461         eed->fast= 1;
462         return eed;
463 }
464 static void *calloc_fastface(EditMesh *em, size_t UNUSED(size), size_t UNUSED(nr))
465 {
466         EditFace *efa= em->curface++;
467         efa->fast= 1;
468         return efa;
469 }
470
471 /* allocate 1 chunk for all vertices, edges, faces. These get tagged to
472    prevent it from being freed
473 */
474 static void init_editmesh_fastmalloc(EditMesh *em, int totvert, int totedge, int totface)
475 {
476         if(totvert) em->allverts= MEM_callocN(totvert*sizeof(EditVert), "allverts");
477         else em->allverts= NULL;
478         em->curvert= em->allverts;
479         
480         if(totedge==0) totedge= 4*totface;      // max possible
481
482         if(totedge) em->alledges= MEM_callocN(totedge*sizeof(EditEdge), "alledges");
483         else em->alledges= NULL;
484         em->curedge= em->alledges;
485         
486         if(totface) em->allfaces= MEM_callocN(totface*sizeof(EditFace), "allfaces");
487         else em->allfaces= NULL;
488         em->curface= em->allfaces;
489
490         callocvert= calloc_fastvert;
491         callocedge= calloc_fastedge;
492         callocface= calloc_fastface;
493 }
494
495 static void end_editmesh_fastmalloc(void)
496 {
497         callocvert= calloc_em;
498         callocedge= calloc_em;
499         callocface= calloc_em;
500 }
501
502 /* do not free editmesh itself here */
503 void free_editMesh(EditMesh *em)
504 {
505         if(em==NULL) return;
506
507         if(em->verts.first) free_vertlist(em, &em->verts);
508         if(em->edges.first) free_edgelist(em, &em->edges);
509         if(em->faces.first) free_facelist(em, &em->faces);
510         if(em->selected.first) BLI_freelistN(&(em->selected));
511
512         CustomData_free(&em->vdata, 0);
513         CustomData_free(&em->fdata, 0);
514
515         if(em->derivedFinal) {
516                 if (em->derivedFinal!=em->derivedCage) {
517                         em->derivedFinal->needsFree= 1;
518                         em->derivedFinal->release(em->derivedFinal);
519                 }
520                 em->derivedFinal= NULL;
521         }
522         if(em->derivedCage) {
523                 em->derivedCage->needsFree= 1;
524                 em->derivedCage->release(em->derivedCage);
525                 em->derivedCage= NULL;
526         }
527
528         /* DEBUG: hashtabs are slowest part of enter/exit editmode. here a testprint */
529 #if 0
530         if(em->hashedgetab) {
531                 HashEdge *he, *hen;
532                 int a, used=0, max=0, nr;
533                 he= em->hashedgetab;
534                 for(a=0; a<EDHASHSIZE; a++, he++) {
535                         if(he->eed) used++;
536                         hen= he->next;
537                         nr= 0;
538                         while(hen) {
539                                 nr++;
540                                 hen= hen->next;
541                         }
542                         if(max<nr) max= nr;
543                 }
544                 printf("hastab used %d max %d\n", used, max);
545         }
546 #endif
547         if(em->hashedgetab) MEM_freeN(em->hashedgetab);
548         em->hashedgetab= NULL;
549         
550         if(em->allverts) MEM_freeN(em->allverts);
551         if(em->alledges) MEM_freeN(em->alledges);
552         if(em->allfaces) MEM_freeN(em->allfaces);
553         
554         em->allverts= em->curvert= NULL;
555         em->alledges= em->curedge= NULL;
556         em->allfaces= em->curface= NULL;
557         
558         mesh_octree_table(NULL, NULL, NULL, 'e');
559         mesh_mirrtopo_table(NULL, 'e');
560
561         em->totvert= em->totedge= em->totface= 0;
562
563 // XXX  if(em->retopo_paint_data) retopo_free_paint_data(em->retopo_paint_data);
564         em->retopo_paint_data= NULL;
565         em->act_face = NULL;
566 }
567
568 static void editMesh_set_hash(EditMesh *em)
569 {
570         EditEdge *eed;
571
572         if(em->hashedgetab) MEM_freeN(em->hashedgetab);
573         em->hashedgetab= NULL;
574         
575         for(eed=em->edges.first; eed; eed= eed->next)  {
576                 if( findedgelist(em, eed->v1, eed->v2)==NULL )
577                         insert_hashedge(em, eed);
578         }
579
580 }
581
582
583 /* ************************ IN & OUT EDITMODE ***************************** */
584
585
586 static void edge_normal_compare(EditEdge *eed, EditFace *efa1)
587 {
588         EditFace *efa2;
589         float cent1[3], cent2[3];
590         float inp;
591         
592         efa2 = eed->tmp.f;
593         if(efa1==efa2) return;
594         
595         inp= efa1->n[0]*efa2->n[0] + efa1->n[1]*efa2->n[1] + efa1->n[2]*efa2->n[2];
596         if(inp<0.999 && inp >-0.999) eed->f2= 1;
597                 
598         if(efa1->v4) cent_quad_v3(cent1, efa1->v1->co, efa1->v2->co, efa1->v3->co, efa1->v4->co);
599         else cent_tri_v3(cent1, efa1->v1->co, efa1->v2->co, efa1->v3->co);
600         if(efa2->v4) cent_quad_v3(cent2, efa2->v1->co, efa2->v2->co, efa2->v3->co, efa2->v4->co);
601         else cent_tri_v3(cent2, efa2->v1->co, efa2->v2->co, efa2->v3->co);
602         
603         sub_v3_v3v3(cent1, cent2, cent1);
604         normalize_v3(cent1);
605         inp= cent1[0]*efa1->n[0] + cent1[1]*efa1->n[1] + cent1[2]*efa1->n[2]; 
606
607         if(inp < -0.001 ) eed->f1= 1;
608 }
609
610 #if 0
611 typedef struct {
612         EditEdge *eed;
613         float noLen,no[3];
614         int adjCount;
615 } EdgeDrawFlagInfo;
616
617 static int edgeDrawFlagInfo_cmp(const void *av, const void *bv)
618 {
619         const EdgeDrawFlagInfo *a = av;
620         const EdgeDrawFlagInfo *b = bv;
621
622         if (a->noLen<b->noLen) return -1;
623         else if (a->noLen>b->noLen) return 1;
624         else return 0;
625 }
626 #endif
627
628 static void edge_drawflags(Mesh *me, EditMesh *em)
629 {
630         EditVert *eve;
631         EditEdge *eed, *e1, *e2, *e3, *e4;
632         EditFace *efa;
633         
634         /* - count number of times edges are used in faces: 0 en 1 time means draw edge
635          * - edges more than 1 time used: in *tmp.f is pointer to first face
636          * - check all faces, when normal differs to much: draw (flag becomes 1)
637          */
638
639         /* later on: added flags for 'cylinder' and 'sphere' intersection tests in old
640            game engine (2.04)
641          */
642         
643         recalc_editnormals(em);
644         
645         /* init */
646         eve= em->verts.first;
647         while(eve) {
648                 eve->f1= 1;             /* during test it's set at zero */
649                 eve= eve->next;
650         }
651         eed= em->edges.first;
652         while(eed) {
653                 eed->f2= eed->f1= 0;
654                 eed->tmp.f = 0;
655                 eed= eed->next;
656         }
657
658         efa= em->faces.first;
659         while(efa) {
660                 e1= efa->e1;
661                 e2= efa->e2;
662                 e3= efa->e3;
663                 e4= efa->e4;
664                 if(e1->f2<4) e1->f2+= 1;
665                 if(e2->f2<4) e2->f2+= 1;
666                 if(e3->f2<4) e3->f2+= 1;
667                 if(e4 && e4->f2<4) e4->f2+= 1;
668                 
669                 if(e1->tmp.f == 0) e1->tmp.f = (void *) efa;
670                 if(e2->tmp.f == 0) e2->tmp.f = (void *) efa;
671                 if(e3->tmp.f ==0) e3->tmp.f = (void *) efa;
672                 if(e4 && (e4->tmp.f == 0)) e4->tmp.f = (void *) efa;
673                 
674                 efa= efa->next;
675         }
676
677         if(me->drawflag & ME_ALLEDGES) {
678                 efa= em->faces.first;
679                 while(efa) {
680                         if(efa->e1->f2>=2) efa->e1->f2= 1;
681                         if(efa->e2->f2>=2) efa->e2->f2= 1;
682                         if(efa->e3->f2>=2) efa->e3->f2= 1;
683                         if(efa->e4 && efa->e4->f2>=2) efa->e4->f2= 1;
684                         
685                         efa= efa->next;
686                 }               
687         }       
688         else {
689                 
690                 /* handle single-edges for 'test cylinder flag' (old engine) */
691                 
692                 eed= em->edges.first;
693                 while(eed) {
694                         if(eed->f2==1) eed->f1= 1;
695                         eed= eed->next;
696                 }
697
698                 /* all faces, all edges with flag==2: compare normal */
699                 efa= em->faces.first;
700                 while(efa) {
701                         if(efa->e1->f2==2) edge_normal_compare(efa->e1, efa);
702                         else efa->e1->f2= 1;
703                         if(efa->e2->f2==2) edge_normal_compare(efa->e2, efa);
704                         else efa->e2->f2= 1;
705                         if(efa->e3->f2==2) edge_normal_compare(efa->e3, efa);
706                         else efa->e3->f2= 1;
707                         if(efa->e4) {
708                                 if(efa->e4->f2==2) edge_normal_compare(efa->e4, efa);
709                                 else efa->e4->f2= 1;
710                         }
711                         efa= efa->next;
712                 }
713                 
714                 /* sphere collision flag */
715                 
716                 eed= em->edges.first;
717                 while(eed) {
718                         if(eed->f1!=1) {
719                                 eed->v1->f1= eed->v2->f1= 0;
720                         }
721                         eed= eed->next;
722                 }
723                 
724         }
725 }
726
727 /* turns Mesh into editmesh */
728 void make_editMesh(Scene *scene, Object *ob)
729 {
730         Mesh *me= ob->data;
731         MFace *mface;
732         MVert *mvert;
733         MSelect *mselect;
734         KeyBlock *actkey;
735         EditMesh *em;
736         EditVert *eve, **evlist, *eve1, *eve2, *eve3, *eve4;
737         EditFace *efa, *efa_last_sel= NULL;
738         EditEdge *eed;
739         EditSelection *ese;
740         float *co, (*keyco)[3]= NULL;
741         int tot, a, eekadoodle= 0;
742         const short is_paint_sel= paint_facesel_test(ob);
743
744         if(me->edit_mesh==NULL)
745                 me->edit_mesh= MEM_callocN(sizeof(EditMesh), "editmesh");
746         else 
747                 /* because of reload */
748                 free_editMesh(me->edit_mesh);
749         
750         em= me->edit_mesh;
751         
752         em->selectmode= scene->toolsettings->selectmode; // warning needs to be synced
753         em->act_face = NULL;
754         em->totvert= tot= me->totvert;
755         em->totedge= me->totedge;
756         em->totface= me->totface;
757         
758         if(tot==0) {
759                 return;
760         }
761         
762         if(ob->actcol > 0)
763                 em->mat_nr= ob->actcol-1;
764
765         /* initialize fastmalloc for editmesh */
766         init_editmesh_fastmalloc(em, me->totvert, me->totedge, me->totface);
767
768         actkey = ob_get_keyblock(ob);
769         if(actkey) {
770                 /* undo-ing in past for previous editmode sessions gives corrupt 'keyindex' values */
771                 undo_editmode_clear();
772                 keyco= actkey->data;
773                 em->shapenr= ob->shapenr;
774         }
775
776         /* make editverts */
777         CustomData_copy(&me->vdata, &em->vdata, CD_MASK_EDITMESH, CD_CALLOC, 0);
778         mvert= me->mvert;
779
780         evlist= (EditVert **)MEM_mallocN(tot*sizeof(void *),"evlist");
781         for(a=0; a<tot; a++, mvert++) {
782                 
783                 co= mvert->co;
784
785                 /* edit the shape key coordinate if available */
786                 if(keyco && a < actkey->totelem)
787                         co= keyco[a];
788
789                 eve= addvertlist(em, co, NULL);
790                 evlist[a]= eve;
791                 
792                 /* face select sets selection in next loop */
793                 if(!is_paint_sel)
794                         eve->f |= (mvert->flag & 1);
795                 
796                 if (mvert->flag & ME_HIDE) eve->h= 1;           
797                 eve->no[0]= mvert->no[0]/32767.0;
798                 eve->no[1]= mvert->no[1]/32767.0;
799                 eve->no[2]= mvert->no[2]/32767.0;
800
801                 eve->bweight= ((float)mvert->bweight)/255.0f;
802
803                 /* lets overwrite the keyindex of the editvert
804                  * with the order it used to be in before
805                  * editmode
806                  */
807                 eve->keyindex = a;
808
809                 CustomData_to_em_block(&me->vdata, &em->vdata, a, &eve->data);
810         }
811
812         if(actkey && actkey->totelem!=me->totvert);
813         else {
814                 MEdge *medge= me->medge;
815                 
816                 CustomData_copy(&me->edata, &em->edata, CD_MASK_EDITMESH, CD_CALLOC, 0);
817                 /* make edges */
818                 for(a=0; a<me->totedge; a++, medge++) {
819                         eed= addedgelist(em, evlist[medge->v1], evlist[medge->v2], NULL);
820                         /* eed can be zero when v1 and v2 are identical, dxf import does this... */
821                         if(eed) {
822                                 eed->crease= ((float)medge->crease)/255.0f;
823                                 eed->bweight= ((float)medge->bweight)/255.0f;
824                                 
825                                 if(medge->flag & ME_SEAM) eed->seam= 1;
826                                 if(medge->flag & ME_SHARP) eed->sharp = 1;
827                                 if(medge->flag & SELECT) eed->f |= SELECT;
828                                 if(medge->flag & ME_FGON) eed->h= EM_FGON;      // 2 different defines!
829                                 if(medge->flag & ME_HIDE) eed->h |= 1;
830                                 if(em->selectmode==SCE_SELECT_EDGE) 
831                                         EM_select_edge(eed, eed->f & SELECT);           // force edge selection to vertices, seems to be needed ...
832                                 CustomData_to_em_block(&me->edata,&em->edata, a, &eed->data);
833                         }
834                 }
835                 
836                 CustomData_copy(&me->fdata, &em->fdata, CD_MASK_EDITMESH, CD_CALLOC, 0);
837
838                 /* make faces */
839                 mface= me->mface;
840
841                 for(a=0; a<me->totface; a++, mface++) {
842                         eve1= evlist[mface->v1];
843                         eve2= evlist[mface->v2];
844                         if(!mface->v3) eekadoodle= 1;
845                         eve3= evlist[mface->v3];
846                         if(mface->v4) eve4= evlist[mface->v4]; else eve4= NULL;
847                         
848                         efa= addfacelist(em, eve1, eve2, eve3, eve4, NULL, NULL);
849
850                         if(efa) {
851                                 CustomData_to_em_block(&me->fdata, &em->fdata, a, &efa->data);
852
853                                 efa->mat_nr= mface->mat_nr;
854                                 efa->flag= mface->flag & ~ME_HIDE;
855                                 
856                                 /* select and hide face flag */
857                                 if(mface->flag & ME_HIDE) {
858                                         efa->h= 1;
859                                 } else {
860                                         if (a==me->act_face) {
861                                                 EM_set_actFace(em, efa);
862                                         }
863                                         
864                                         /* dont allow hidden and selected */
865                                         if(mface->flag & ME_FACE_SEL) {
866                                                 efa->f |= SELECT;
867                                                 
868                                                 if(is_paint_sel) {
869                                                         EM_select_face(efa, 1); /* flush down */
870                                                 }
871
872                                                 efa_last_sel= efa;
873                                         }
874                                 }
875                         }
876                 }
877         }
878         
879         if(EM_get_actFace(em, 0)==NULL && efa_last_sel) {
880                 EM_set_actFace(em, efa_last_sel);
881         }
882
883         if(eekadoodle)
884                 error("This Mesh has old style edgecodes, please put it in the bugtracker!");
885         
886         MEM_freeN(evlist);
887
888         end_editmesh_fastmalloc();      // resets global function pointers
889         
890         if(me->mselect){
891                 //restore editselections
892                 EM_init_index_arrays(em, 1,1,1);
893                 mselect = me->mselect;
894                 
895                 for(a=0; a<me->totselect; a++, mselect++){
896                         /*check if recorded selection is still valid, if so copy into editmesh*/
897                         if( (mselect->type == EDITVERT && me->mvert[mselect->index].flag & SELECT) || (mselect->type == EDITEDGE && me->medge[mselect->index].flag & SELECT) || (mselect->type == EDITFACE && me->mface[mselect->index].flag & ME_FACE_SEL) ){
898                                 ese = MEM_callocN(sizeof(EditSelection), "Edit Selection");
899                                 ese->type = mselect->type;      
900                                 if(ese->type == EDITVERT) ese->data = EM_get_vert_for_index(mselect->index); else
901                                 if(ese->type == EDITEDGE) ese->data = EM_get_edge_for_index(mselect->index); else
902                                 if(ese->type == EDITFACE) ese->data = EM_get_face_for_index(mselect->index);
903                                 BLI_addtail(&(em->selected),ese);
904                         }
905                 }
906                 EM_free_index_arrays();
907         }
908         /* this creates coherent selections. also needed for older files */
909         EM_selectmode_set(em);
910         /* paranoia check to enforce hide rules */
911         EM_hide_reset(em);
912         /* sets helper flags which arent saved */
913         EM_fgon_flags(em);
914         
915         if (EM_get_actFace(em, 0)==NULL) {
916                 EM_set_actFace(em, em->faces.first ); /* will use the first face, this is so we alwats have an active face */
917         }
918 }
919
920 /* makes Mesh out of editmesh */
921 void load_editMesh(Scene *scene, Object *ob)
922 {
923         Mesh *me= ob->data;
924         MVert *mvert, *oldverts;
925         MEdge *medge;
926         MFace *mface;
927         MSelect *mselect;
928         EditMesh *em= me->edit_mesh;
929         EditVert *eve;
930         EditFace *efa, *efa_act;
931         EditEdge *eed;
932         EditSelection *ese;
933         float *fp, *newkey, *oldkey, nor[3];
934         int i, a, ototvert;
935         
936         /* this one also tests of edges are not in faces: */
937         /* eed->f2==0: not in face, f2==1: draw it */
938         /* eed->f1 : flag for dynaface (cylindertest, old engine) */
939         /* eve->f1 : flag for dynaface (sphere test, old engine) */
940         /* eve->f2 : being used in vertexnormals */
941         edge_drawflags(me, em);
942         
943         EM_stats_update(em);
944         
945         /* new Vertex block */
946         if(em->totvert==0) mvert= NULL;
947         else mvert= MEM_callocN(em->totvert*sizeof(MVert), "loadeditMesh vert");
948
949         /* new Edge block */
950         if(em->totedge==0) medge= NULL;
951         else medge= MEM_callocN(em->totedge*sizeof(MEdge), "loadeditMesh edge");
952         
953         /* new Face block */
954         if(em->totface==0) mface= NULL;
955         else mface= MEM_callocN(em->totface*sizeof(MFace), "loadeditMesh face");
956
957         /* lets save the old verts just in case we are actually working on
958          * a key ... we now do processing of the keys at the end */
959         oldverts= me->mvert;
960         ototvert= me->totvert;
961
962         /* don't free this yet */
963         CustomData_set_layer(&me->vdata, CD_MVERT, NULL);
964
965         /* free custom data */
966         CustomData_free(&me->vdata, me->totvert);
967         CustomData_free(&me->edata, me->totedge);
968         CustomData_free(&me->fdata, me->totface);
969
970         /* add new custom data */
971         me->totvert= em->totvert;
972         me->totedge= em->totedge;
973         me->totface= em->totface;
974
975         CustomData_copy(&em->vdata, &me->vdata, CD_MASK_MESH, CD_CALLOC, me->totvert);
976         CustomData_copy(&em->edata, &me->edata, CD_MASK_MESH, CD_CALLOC, me->totedge);
977         CustomData_copy(&em->fdata, &me->fdata, CD_MASK_MESH, CD_CALLOC, me->totface);
978
979         CustomData_add_layer(&me->vdata, CD_MVERT, CD_ASSIGN, mvert, me->totvert);
980         CustomData_add_layer(&me->edata, CD_MEDGE, CD_ASSIGN, medge, me->totedge);
981         CustomData_add_layer(&me->fdata, CD_MFACE, CD_ASSIGN, mface, me->totface);
982         mesh_update_customdata_pointers(me);
983
984         /* the vertices, use ->tmp.l as counter */
985         eve= em->verts.first;
986         a= 0;
987
988         while(eve) {
989                 VECCOPY(mvert->co, eve->co);
990
991                 mvert->mat_nr= 32767;  /* what was this for, halos? */
992                 
993                 /* vertex normal */
994                 VECCOPY(nor, eve->no);
995                 mul_v3_fl(nor, 32767.0);
996                 VECCOPY(mvert->no, nor);
997
998                 /* note: it used to remove me->dvert when it was not in use, cancelled
999                    that... annoying when you have a fresh vgroup */
1000                 CustomData_from_em_block(&em->vdata, &me->vdata, eve->data, a);
1001
1002                 eve->tmp.l = a++;  /* counter */
1003                         
1004                 mvert->flag= 0;
1005                 mvert->flag |= (eve->f & SELECT);
1006                 if (eve->h) mvert->flag |= ME_HIDE;
1007                 
1008                 mvert->bweight= (char)(255.0*eve->bweight);
1009
1010                 eve= eve->next;
1011                 mvert++;
1012         }
1013
1014         /* the edges */
1015         a= 0;
1016         eed= em->edges.first;
1017         while(eed) {
1018                 medge->v1= (unsigned int) eed->v1->tmp.l;
1019                 medge->v2= (unsigned int) eed->v2->tmp.l;
1020                 
1021                 medge->flag= (eed->f & SELECT) | ME_EDGERENDER;
1022                 if(eed->f2<2) medge->flag |= ME_EDGEDRAW;
1023                 if(eed->f2==0) medge->flag |= ME_LOOSEEDGE;
1024                 if(eed->sharp) medge->flag |= ME_SHARP;
1025                 if(eed->seam) medge->flag |= ME_SEAM;
1026                 if(eed->h & EM_FGON) medge->flag |= ME_FGON;    // different defines yes
1027                 if(eed->h & 1) medge->flag |= ME_HIDE;
1028                 
1029                 medge->crease= (char)(255.0*eed->crease);
1030                 medge->bweight= (char)(255.0*eed->bweight);
1031                 CustomData_from_em_block(&em->edata, &me->edata, eed->data, a);         
1032
1033                 eed->tmp.l = a++;
1034                 
1035                 medge++;
1036                 eed= eed->next;
1037         }
1038
1039         /* the faces */
1040         a = 0;
1041         efa= em->faces.first;
1042         efa_act= EM_get_actFace(em, 0);
1043         i = 0;
1044         me->act_face = -1;
1045         while(efa) {
1046                 mface= &((MFace *) me->mface)[i];
1047                 
1048                 mface->v1= (unsigned int) efa->v1->tmp.l;
1049                 mface->v2= (unsigned int) efa->v2->tmp.l;
1050                 mface->v3= (unsigned int) efa->v3->tmp.l;
1051                 if (efa->v4) mface->v4 = (unsigned int) efa->v4->tmp.l;
1052
1053                 mface->mat_nr= efa->mat_nr;
1054                 
1055                 mface->flag= efa->flag;
1056                 /* bit 0 of flag is already taken for smooth... */
1057                 
1058                 if(efa->h) {
1059                         mface->flag |= ME_HIDE;
1060                         mface->flag &= ~ME_FACE_SEL;
1061                 } else {
1062                         if(efa->f & 1) mface->flag |= ME_FACE_SEL;
1063                         else mface->flag &= ~ME_FACE_SEL;
1064                 }
1065                 
1066                 /* mat_nr in vertex */
1067                 if(me->totcol>1) {
1068                         mvert= me->mvert+mface->v1;
1069                         if(mvert->mat_nr == (char)32767) mvert->mat_nr= mface->mat_nr;
1070                         mvert= me->mvert+mface->v2;
1071                         if(mvert->mat_nr == (char)32767) mvert->mat_nr= mface->mat_nr;
1072                         mvert= me->mvert+mface->v3;
1073                         if(mvert->mat_nr == (char)32767) mvert->mat_nr= mface->mat_nr;
1074                         if(mface->v4) {
1075                                 mvert= me->mvert+mface->v4;
1076                                 if(mvert->mat_nr == (char)32767) mvert->mat_nr= mface->mat_nr;
1077                         }
1078                 }
1079                         
1080                 /* watch: efa->e1->f2==0 means loose edge */ 
1081                         
1082                 if(efa->e1->f2==1) {
1083                         efa->e1->f2= 2;
1084                 }                       
1085                 if(efa->e2->f2==1) {
1086                         efa->e2->f2= 2;
1087                 }
1088                 if(efa->e3->f2==1) {
1089                         efa->e3->f2= 2;
1090                 }
1091                 if(efa->e4 && efa->e4->f2==1) {
1092                         efa->e4->f2= 2;
1093                 }
1094
1095                 CustomData_from_em_block(&em->fdata, &me->fdata, efa->data, i);
1096
1097                 /* no index '0' at location 3 or 4 */
1098                 test_index_face(mface, &me->fdata, i, efa->v4?4:3);
1099                 
1100                 if (efa_act == efa)
1101                         me->act_face = a;
1102
1103                 efa->tmp.l = a++;
1104                 i++;
1105                 efa= efa->next;
1106         }
1107
1108         /* patch hook indices and vertex parents */
1109         {
1110                 Object *ob;
1111                 ModifierData *md;
1112                 EditVert **vertMap = NULL;
1113                 int i,j;
1114
1115                 for (ob=G.main->object.first; ob; ob=ob->id.next) {
1116                         if (ob->parent==ob && ELEM(ob->partype, PARVERT1,PARVERT3)) {
1117                                 
1118                                 /* duplicate code from below, make it function later...? */
1119                                 if (!vertMap) {
1120                                         vertMap = MEM_callocN(sizeof(*vertMap)*ototvert, "vertMap");
1121                                         
1122                                         for (eve=em->verts.first; eve; eve=eve->next) {
1123                                                 if (eve->keyindex!=-1)
1124                                                         vertMap[eve->keyindex] = eve;
1125                                         }
1126                                 }
1127                                 if(ob->par1 < ototvert) {
1128                                         eve = vertMap[ob->par1];
1129                                         if(eve) ob->par1= eve->tmp.l;
1130                                 }
1131                                 if(ob->par2 < ototvert) {
1132                                         eve = vertMap[ob->par2];
1133                                         if(eve) ob->par2= eve->tmp.l;
1134                                 }
1135                                 if(ob->par3 < ototvert) {
1136                                         eve = vertMap[ob->par3];
1137                                         if(eve) ob->par3= eve->tmp.l;
1138                                 }
1139                                 
1140                         }
1141                         if (ob->data==me) {
1142                                 for (md=ob->modifiers.first; md; md=md->next) {
1143                                         if (md->type==eModifierType_Hook) {
1144                                                 HookModifierData *hmd = (HookModifierData*) md;
1145
1146                                                 if (!vertMap) {
1147                                                         vertMap = MEM_callocN(sizeof(*vertMap)*ototvert, "vertMap");
1148
1149                                                         for (eve=em->verts.first; eve; eve=eve->next) {
1150                                                                 if (eve->keyindex!=-1)
1151                                                                         vertMap[eve->keyindex] = eve;
1152                                                         }
1153                                                 }
1154                                                 
1155                                                 for (i=j=0; i<hmd->totindex; i++) {
1156                                                         if(hmd->indexar[i] < ototvert) {
1157                                                                 eve = vertMap[hmd->indexar[i]];
1158                                                                 
1159                                                                 if (eve) {
1160                                                                         hmd->indexar[j++] = eve->tmp.l;
1161                                                                 }
1162                                                         }
1163                                                         else j++;
1164                                                 }
1165
1166                                                 hmd->totindex = j;
1167                                         }
1168                                 }
1169                         }
1170                 }
1171
1172                 if (vertMap) MEM_freeN(vertMap);
1173         }
1174
1175         /* are there keys? */
1176         if(me->key) {
1177                 KeyBlock *currkey;
1178                 KeyBlock *actkey= BLI_findlink(&me->key->block, em->shapenr-1);
1179
1180                 float (*ofs)[3] = NULL;
1181
1182                 /* editing the base key should update others */
1183                 if(me->key->type==KEY_RELATIVE && oldverts) {
1184                         int act_is_basis = 0;
1185                         /* find if this key is a basis for any others */
1186                         for(currkey = me->key->block.first; currkey; currkey= currkey->next) {
1187                                 if(em->shapenr-1 == currkey->relative) {
1188                                         act_is_basis = 1;
1189                                         break;
1190                                 }
1191                         }
1192
1193                         if(act_is_basis) { /* active key is a base */
1194                                 i=0;
1195                                 ofs= MEM_callocN(sizeof(float) * 3 * em->totvert,  "currkey->data");
1196                                 eve= em->verts.first;
1197                                 mvert = me->mvert;
1198                                 while(eve) {
1199                                         VECSUB(ofs[i], mvert->co, oldverts[eve->keyindex].co);
1200                                         eve= eve->next;
1201                                         i++;
1202                                         mvert++;
1203                                 }
1204                         }
1205                 }
1206
1207
1208                 /* Lets reorder the key data so that things line up roughly
1209                  * with the way things were before editmode */
1210                 currkey = me->key->block.first;
1211                 while(currkey) {
1212                         int apply_offset = (ofs && (currkey != actkey) && (em->shapenr-1 == currkey->relative));
1213
1214                         fp= newkey= MEM_callocN(me->key->elemsize*em->totvert,  "currkey->data");
1215                         oldkey = currkey->data;
1216
1217                         eve= em->verts.first;
1218
1219                         i = 0;
1220                         mvert = me->mvert;
1221                         while(eve) {
1222                                 if (eve->keyindex >= 0 && eve->keyindex < currkey->totelem) { // valid old vertex
1223                                         if(currkey == actkey) {
1224                                                 if(actkey == me->key->refkey) {
1225                                                         VECCOPY(fp, mvert->co);
1226                                                 }
1227                                                 else {
1228                                                         VECCOPY(fp, mvert->co);
1229                                                         if(oldverts) {
1230                                                                 VECCOPY(mvert->co, oldverts[eve->keyindex].co);
1231                                                         }
1232                                                 }
1233                                         }
1234                                         else {
1235                                                 if(oldkey) {
1236                                                         VECCOPY(fp, oldkey + 3 * eve->keyindex);
1237                                                 }
1238                                         }
1239                                 }
1240                                 else {
1241                                         VECCOPY(fp, mvert->co);
1242                                 }
1243
1244                                 /* propagate edited basis offsets to other shapes */
1245                                 if(apply_offset) {
1246                                         VECADD(fp, fp, ofs[i]);
1247                                 }
1248
1249                                 fp+= 3;
1250                                 ++i;
1251                                 ++mvert;
1252                                 eve= eve->next;
1253                         }
1254                         currkey->totelem= em->totvert;
1255                         if(currkey->data) MEM_freeN(currkey->data);
1256                         currkey->data = newkey;
1257                         
1258                         currkey= currkey->next;
1259                 }
1260
1261                 if(ofs) MEM_freeN(ofs);
1262         }
1263
1264         if(oldverts) MEM_freeN(oldverts);
1265         
1266         i = 0;
1267         for(ese=em->selected.first; ese; ese=ese->next) i++;
1268         me->totselect = i;
1269         if(i==0) mselect= NULL;
1270         else mselect= MEM_callocN(i*sizeof(MSelect), "loadeditMesh selections");
1271         
1272         if(me->mselect) MEM_freeN(me->mselect);
1273         me->mselect= mselect;
1274         
1275         for(ese=em->selected.first; ese; ese=ese->next){
1276                 mselect->type = ese->type;
1277                 if(ese->type == EDITVERT) mselect->index = ((EditVert*)ese->data)->tmp.l;
1278                 else if(ese->type == EDITEDGE) mselect->index = ((EditEdge*)ese->data)->tmp.l;
1279                 else if(ese->type == EDITFACE) mselect->index = ((EditFace*)ese->data)->tmp.l;
1280                 mselect++;
1281         }
1282         
1283         /* to be sure: clear ->tmp.l pointers */
1284         eve= em->verts.first;
1285         while(eve) {
1286                 eve->tmp.l = 0;
1287                 eve= eve->next;
1288         }
1289         
1290         eed= em->edges.first;
1291         while(eed) { 
1292                 eed->tmp.l = 0;
1293                 eed= eed->next;
1294         }
1295         
1296         efa= em->faces.first;
1297         while(efa) {
1298                 efa->tmp.l = 0;
1299                 efa= efa->next;
1300         }
1301         
1302         /* remake softbody of all users */
1303         if(me->id.us>1) {
1304                 Base *base;
1305                 for(base= scene->base.first; base; base= base->next)
1306                         if(base->object->data==me)
1307                                 base->object->recalc |= OB_RECALC_DATA;
1308         }
1309
1310         mesh_calc_normals(me->mvert, me->totvert, me->mface, me->totface, NULL);
1311
1312         /* topology could be changed, ensure mdisps are ok */
1313         multires_topology_changed(ob);
1314 }
1315
1316 void remake_editMesh(Scene *scene, Object *ob)
1317 {
1318         make_editMesh(scene, ob);
1319         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
1320         BIF_undo_push("Undo all changes");
1321 }
1322
1323 /* *************** Operator: separate parts *************/
1324
1325 static EnumPropertyItem prop_separate_types[] = {
1326         {0, "SELECTED", 0, "Selection", ""},
1327         {1, "MATERIAL", 0, "By Material", ""},
1328         {2, "LOOSE", 0, "By loose parts", ""},
1329         {0, NULL, 0, NULL, NULL}
1330 };
1331
1332 /* return 1: success */
1333 static int mesh_separate_selected(wmOperator *op, Main *bmain, Scene *scene, Base *editbase)
1334 {
1335         EditMesh *em, *emnew;
1336         EditVert *eve, *v1;
1337         EditEdge *eed, *e1;
1338         EditFace *efa, *f1;
1339         Object *obedit;
1340         Mesh *me, *menew;
1341         Base *basenew;
1342         
1343         if(editbase==NULL) return 0;
1344         
1345         obedit= editbase->object;
1346         me= obedit->data;
1347         em= BKE_mesh_get_editmesh(me);
1348         if(me->key) {
1349                 BKE_report(op->reports, RPT_WARNING, "Can't separate mesh with shape keys.");
1350                 BKE_mesh_end_editmesh(me, em);
1351                 return 0;
1352         }
1353         
1354         if(em->selected.first) 
1355                 BLI_freelistN(&(em->selected)); /* clear the selection order */
1356                 
1357         EM_selectmode_set(em);  // enforce full consistent selection flags 
1358         
1359         EM_stats_update(em);
1360         
1361         if(em->totvertsel==0) {
1362                 BKE_mesh_end_editmesh(me, em);
1363                 return 0;
1364         }
1365         
1366         /* we are going to work as follows:
1367          * 1. add a linked duplicate object: this will be the new one, we remember old pointer
1368          * 2. give new object empty mesh and put in editmode
1369          * 3: do a split if needed on current editmesh.
1370          * 4. copy over: all NOT selected verts, edges, faces
1371          * 5. call load_editMesh() on the new object
1372          */
1373         
1374         /* 1 */
1375         basenew= ED_object_add_duplicate(bmain, scene, editbase, 0);    /* 0 = fully linked */
1376         ED_base_object_select(basenew, BA_DESELECT);
1377         
1378         /* 2 */
1379         basenew->object->data= menew= add_mesh(me->id.name+2);  /* empty */
1380         assign_matarar(basenew->object, give_matarar(obedit), *give_totcolp(obedit)); /* new in 2.5 */
1381         me->id.us--;
1382         make_editMesh(scene, basenew->object);
1383         emnew= menew->edit_mesh;
1384         CustomData_copy(&em->vdata, &emnew->vdata, CD_MASK_EDITMESH, CD_DEFAULT, 0);
1385         CustomData_copy(&em->edata, &emnew->edata, CD_MASK_EDITMESH, CD_DEFAULT, 0);
1386         CustomData_copy(&em->fdata, &emnew->fdata, CD_MASK_EDITMESH, CD_DEFAULT, 0);
1387         
1388         /* 3 */
1389         /* SPLIT: first make duplicate */
1390         adduplicateflag(em, SELECT);
1391         /* SPLIT: old faces have 3x flag 128 set, delete these ones */
1392         delfaceflag(em, 128);
1393         /* since we do tricky things with verts/edges/faces, this makes sure all is selected coherent */
1394         EM_selectmode_set(em);
1395         
1396         /* 4 */
1397         /* move over: everything that is selected */
1398         for(eve= em->verts.first; eve; eve= v1) {
1399                 v1= eve->next;
1400                 if(eve->f & SELECT) {
1401                         BLI_remlink(&em->verts, eve);
1402                         BLI_addtail(&emnew->verts, eve);
1403                 }
1404         }
1405         
1406         for(eed= em->edges.first; eed; eed= e1) {
1407                 e1= eed->next;
1408                 if(eed->f & SELECT) {
1409                         BLI_remlink(&em->edges, eed);
1410                         BLI_addtail(&emnew->edges, eed);
1411                 }
1412         }
1413         
1414         for(efa= em->faces.first; efa; efa= f1) {
1415                 f1= efa->next;
1416                 if (efa == em->act_face && (efa->f & SELECT)) {
1417                         EM_set_actFace(em, NULL);
1418                 }
1419
1420                 if(efa->f & SELECT) {
1421                         BLI_remlink(&em->faces, efa);
1422                         BLI_addtail(&emnew->faces, efa);
1423                 }
1424         }
1425
1426         /* 5 */
1427         load_editMesh(scene, basenew->object);
1428         free_editMesh(emnew);
1429         MEM_freeN(menew->edit_mesh);
1430         menew->edit_mesh= NULL;
1431         
1432         /* hashedges are invalid now, make new! */
1433         editMesh_set_hash(em);
1434
1435         DAG_id_flush_update(&obedit->id, OB_RECALC_DATA);       
1436         DAG_id_flush_update(&basenew->object->id, OB_RECALC_DATA);      
1437
1438         BKE_mesh_end_editmesh(me, em);
1439
1440         return 1;
1441 }
1442
1443 /* return 1: success */
1444 static int mesh_separate_material(wmOperator *op, Main *bmain, Scene *scene, Base *editbase)
1445 {
1446         Mesh *me= editbase->object->data;
1447         EditMesh *em= BKE_mesh_get_editmesh(me);
1448         unsigned char curr_mat;
1449         
1450         for (curr_mat = 1; curr_mat < editbase->object->totcol; ++curr_mat) {
1451                 /* clear selection, we're going to use that to select material group */
1452                 EM_clear_flag_all(em, SELECT);
1453                 /* select the material */
1454                 EM_select_by_material(em, curr_mat);
1455                 /* and now separate */
1456                 if(0==mesh_separate_selected(op, bmain, scene, editbase)) {
1457                         BKE_mesh_end_editmesh(me, em);
1458                         return 0;
1459                 }
1460         }
1461
1462         BKE_mesh_end_editmesh(me, em);
1463         return 1;
1464 }
1465
1466 /* return 1: success */
1467 static int mesh_separate_loose(wmOperator *op, Main *bmain, Scene *scene, Base *editbase)
1468 {
1469         Mesh *me;
1470         EditMesh *em;
1471         int doit= 1;
1472         
1473         me= editbase->object->data;
1474         em= BKE_mesh_get_editmesh(me);
1475         
1476         if(me->key) {
1477                 error("Can't separate with vertex keys");
1478                 BKE_mesh_end_editmesh(me, em);
1479                 return 0;
1480         }
1481         
1482         EM_clear_flag_all(em, SELECT);
1483
1484         while(doit) {
1485                 /* Select a random vert to start with */
1486                 EditVert *eve;
1487                 int tot;
1488
1489                 /* check if all verts that are visible have been done */
1490                 for(eve=em->verts.first; eve; eve= eve->next)
1491                         if(!eve->h) break;
1492                 if(eve==NULL) break; /* only hidden verts left, quit early */
1493
1494                 /* first non hidden vert */
1495                 eve->f |= SELECT;
1496                 
1497                 selectconnected_mesh_all(em);
1498                 
1499                 /* don't separate the very last part */
1500                 for(eve=em->verts.first; eve; eve= eve->next)
1501                         if((eve->f & SELECT)==0) break;
1502                 if(eve==NULL) break;
1503
1504                 tot= BLI_countlist(&em->verts);
1505
1506                 /* and now separate */
1507                 doit= mesh_separate_selected(op, bmain, scene, editbase);
1508
1509                 /* with hidden verts this can happen */
1510                 if(tot == BLI_countlist(&em->verts))
1511                         break;
1512         }
1513
1514         BKE_mesh_end_editmesh(me, em);
1515         return 1;
1516 }
1517
1518
1519 static int mesh_separate_exec(bContext *C, wmOperator *op)
1520 {
1521         Main *bmain= CTX_data_main(C);
1522         Scene *scene= CTX_data_scene(C);
1523         Base *base= CTX_data_active_base(C);
1524         int retval= 0, type= RNA_enum_get(op->ptr, "type");
1525         
1526         if(type == 0)
1527                 retval= mesh_separate_selected(op, bmain, scene, base);
1528         else if(type == 1)
1529                 retval= mesh_separate_material(op, bmain, scene, base);
1530         else if(type == 2)
1531                 retval= mesh_separate_loose(op, bmain, scene, base);
1532            
1533         if(retval) {
1534                 WM_event_add_notifier(C, NC_GEOM|ND_DATA, base->object->data);
1535
1536                 // XXX: new object was created, but selection wasn't actually changed
1537                 //      need this for outliner update without adding new ND. nazgul.
1538                 WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene);
1539
1540                 return OPERATOR_FINISHED;
1541         }
1542         return OPERATOR_CANCELLED;
1543 }
1544
1545 void MESH_OT_separate(wmOperatorType *ot)
1546 {
1547         /* identifiers */
1548         ot->name= "Separate";
1549         ot->description= "Separate selected geometry into a new mesh";
1550         ot->idname= "MESH_OT_separate";
1551         
1552         /* api callbacks */
1553         ot->invoke= WM_menu_invoke;
1554         ot->exec= mesh_separate_exec;
1555         ot->poll= ED_operator_editmesh;
1556         
1557         /* flags */
1558         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1559         
1560         ot->prop= RNA_def_enum(ot->srna, "type", prop_separate_types, 0, "Type", "");
1561 }
1562
1563
1564 /* ******************************************** */
1565
1566 /* *************** UNDO ***************************** */
1567 /* new mesh undo, based on pushing editmesh data itself */
1568 /* reuses same code as for global and curve undo... unify that (ton) */
1569
1570 /* only one 'hack', to save memory it doesn't store the first push, but does a remake editmesh */
1571
1572 /* a compressed version of editmesh data */
1573
1574 typedef struct EditVertC
1575 {
1576         float no[3];
1577         float co[3];
1578         unsigned char f, h;
1579         short bweight;
1580         int keyindex;
1581 } EditVertC;
1582
1583 typedef struct EditEdgeC
1584 {
1585         int v1, v2;
1586         unsigned char f, h, seam, sharp, pad;
1587         short crease, bweight, fgoni;
1588 } EditEdgeC;
1589
1590 typedef struct EditFaceC
1591 {
1592         int v1, v2, v3, v4;
1593         unsigned char flag, f, h, fgonf, pad1;
1594         short mat_nr;
1595 } EditFaceC;
1596
1597 typedef struct EditSelectionC{
1598         short type;
1599         int index;
1600 }EditSelectionC;
1601
1602 typedef struct UndoMesh {
1603         EditVertC *verts;
1604         EditEdgeC *edges;
1605         EditFaceC *faces;
1606         EditSelectionC *selected;
1607         int totvert, totedge, totface, totsel;
1608         int selectmode, shapenr;
1609         RetopoPaintData *retopo_paint_data;
1610         char retopo_mode;
1611         CustomData vdata, edata, fdata;
1612 } UndoMesh;
1613
1614 /* for callbacks */
1615
1616 static void free_undoMesh(void *umv)
1617 {
1618         UndoMesh *um= umv;
1619         
1620         if(um->verts) MEM_freeN(um->verts);
1621         if(um->edges) MEM_freeN(um->edges);
1622         if(um->faces) MEM_freeN(um->faces);
1623         if(um->selected) MEM_freeN(um->selected);
1624 // XXX  if(um->retopo_paint_data) retopo_free_paint_data(um->retopo_paint_data);
1625         CustomData_free(&um->vdata, um->totvert);
1626         CustomData_free(&um->edata, um->totedge);
1627         CustomData_free(&um->fdata, um->totface);
1628         MEM_freeN(um);
1629 }
1630
1631 static void *editMesh_to_undoMesh(void *emv)
1632 {
1633         EditMesh *em= (EditMesh *)emv;
1634         UndoMesh *um;
1635         EditVert *eve;
1636         EditEdge *eed;
1637         EditFace *efa;
1638         EditSelection *ese;
1639         EditVertC *evec=NULL;
1640         EditEdgeC *eedc=NULL;
1641         EditFaceC *efac=NULL;
1642         EditSelectionC *esec=NULL;
1643         int a;
1644
1645         um= MEM_callocN(sizeof(UndoMesh), "undomesh");
1646         
1647         um->selectmode = em->selectmode;
1648         um->shapenr = em->shapenr;
1649         
1650         for(eve=em->verts.first; eve; eve= eve->next) um->totvert++;
1651         for(eed=em->edges.first; eed; eed= eed->next) um->totedge++;
1652         for(efa=em->faces.first; efa; efa= efa->next) um->totface++;
1653         for(ese=em->selected.first; ese; ese=ese->next) um->totsel++; 
1654         /* malloc blocks */
1655         
1656         if(um->totvert) evec= um->verts= MEM_callocN(um->totvert*sizeof(EditVertC), "allvertsC");
1657         if(um->totedge) eedc= um->edges= MEM_callocN(um->totedge*sizeof(EditEdgeC), "alledgesC");
1658         if(um->totface) efac= um->faces= MEM_callocN(um->totface*sizeof(EditFaceC), "allfacesC");
1659         if(um->totsel) esec= um->selected= MEM_callocN(um->totsel*sizeof(EditSelectionC), "allselections");
1660
1661         if(um->totvert) CustomData_copy(&em->vdata, &um->vdata, CD_MASK_EDITMESH, CD_CALLOC, um->totvert);
1662         if(um->totedge) CustomData_copy(&em->edata, &um->edata, CD_MASK_EDITMESH, CD_CALLOC, um->totedge);
1663         if(um->totface) CustomData_copy(&em->fdata, &um->fdata, CD_MASK_EDITMESH, CD_CALLOC, um->totface);
1664         
1665         /* now copy vertices */
1666         a = 0;
1667         for(eve=em->verts.first; eve; eve= eve->next, evec++, a++) {
1668                 VECCOPY(evec->co, eve->co);
1669                 VECCOPY(evec->no, eve->no);
1670
1671                 evec->f= eve->f;
1672                 evec->h= eve->h;
1673                 evec->keyindex= eve->keyindex;
1674                 eve->tmp.l = a; /*store index*/
1675                 evec->bweight= (short)(eve->bweight*255.0);
1676
1677                 CustomData_from_em_block(&em->vdata, &um->vdata, eve->data, a);
1678         }
1679         
1680         /* copy edges */
1681         a = 0;
1682         for(eed=em->edges.first; eed; eed= eed->next, eedc++, a++)  {
1683                 eedc->v1= (int)eed->v1->tmp.l;
1684                 eedc->v2= (int)eed->v2->tmp.l;
1685                 eedc->f= eed->f;
1686                 eedc->h= eed->h;
1687                 eedc->seam= eed->seam;
1688                 eedc->sharp= eed->sharp;
1689                 eedc->crease= (short)(eed->crease*255.0);
1690                 eedc->bweight= (short)(eed->bweight*255.0);
1691                 eedc->fgoni= eed->fgoni;
1692                 eed->tmp.l = a; /*store index*/
1693                 CustomData_from_em_block(&em->edata, &um->edata, eed->data, a);
1694         
1695         }
1696         
1697         /* copy faces */
1698         a = 0;
1699         for(efa=em->faces.first; efa; efa= efa->next, efac++, a++) {
1700                 efac->v1= (int)efa->v1->tmp.l;
1701                 efac->v2= (int)efa->v2->tmp.l;
1702                 efac->v3= (int)efa->v3->tmp.l;
1703                 if(efa->v4) efac->v4= (int)efa->v4->tmp.l;
1704                 else efac->v4= -1;
1705                 
1706                 efac->mat_nr= efa->mat_nr;
1707                 efac->flag= efa->flag;
1708                 efac->f= efa->f;
1709                 efac->h= efa->h;
1710                 efac->fgonf= efa->fgonf;
1711
1712                 efa->tmp.l = a; /*store index*/
1713
1714                 CustomData_from_em_block(&em->fdata, &um->fdata, efa->data, a);
1715         }
1716         
1717         a = 0;
1718         for(ese=em->selected.first; ese; ese=ese->next, esec++){
1719                 esec->type = ese->type;
1720                 if(ese->type == EDITVERT) a = esec->index = ((EditVert*)ese->data)->tmp.l; 
1721                 else if(ese->type == EDITEDGE) a = esec->index = ((EditEdge*)ese->data)->tmp.l; 
1722                 else if(ese->type == EDITFACE) a = esec->index = ((EditFace*)ese->data)->tmp.l;
1723         }
1724
1725 // XXX  um->retopo_paint_data= retopo_paint_data_copy(em->retopo_paint_data);
1726 //      um->retopo_mode= scene->toolsettings->retopo_mode;
1727         
1728         return um;
1729 }
1730
1731 static void undoMesh_to_editMesh(void *umv, void *emv)
1732 {
1733         EditMesh *em= (EditMesh *)emv;
1734         UndoMesh *um= (UndoMesh *)umv;
1735         EditVert *eve, **evar=NULL;
1736         EditEdge *eed;
1737         EditFace *efa;
1738         EditSelection *ese;
1739         EditVertC *evec;
1740         EditEdgeC *eedc;
1741         EditFaceC *efac;
1742         EditSelectionC *esec;
1743         int a=0;
1744
1745         free_editMesh(em);
1746         
1747         /* malloc blocks */
1748         memset(em, 0, sizeof(EditMesh));
1749                 
1750         em->selectmode = um->selectmode;
1751         em->shapenr = um->shapenr;
1752         
1753         init_editmesh_fastmalloc(em, um->totvert, um->totedge, um->totface);
1754
1755         CustomData_free(&em->vdata, 0);
1756         CustomData_free(&em->edata, 0);
1757         CustomData_free(&em->fdata, 0);
1758
1759         CustomData_copy(&um->vdata, &em->vdata, CD_MASK_EDITMESH, CD_CALLOC, 0);
1760         CustomData_copy(&um->edata, &em->edata, CD_MASK_EDITMESH, CD_CALLOC, 0);
1761         CustomData_copy(&um->fdata, &em->fdata, CD_MASK_EDITMESH, CD_CALLOC, 0);
1762
1763         /* now copy vertices */
1764
1765         if(um->totvert) evar= MEM_mallocN(um->totvert*sizeof(EditVert *), "vertex ar");
1766         for(a=0, evec= um->verts; a<um->totvert; a++, evec++) {
1767                 eve= addvertlist(em, evec->co, NULL);
1768                 evar[a]= eve;
1769
1770                 VECCOPY(eve->no, evec->no);
1771                 eve->f= evec->f;
1772                 eve->h= evec->h;
1773                 eve->keyindex= evec->keyindex;
1774                 eve->bweight= ((float)evec->bweight)/255.0f;
1775
1776                 CustomData_to_em_block(&um->vdata, &em->vdata, a, &eve->data);
1777         }
1778
1779         /* copy edges */
1780         for(a=0, eedc= um->edges; a<um->totedge; a++, eedc++) {
1781                 eed= addedgelist(em, evar[eedc->v1], evar[eedc->v2], NULL);
1782
1783                 eed->f= eedc->f;
1784                 eed->h= eedc->h;
1785                 eed->seam= eedc->seam;
1786                 eed->sharp= eedc->sharp;
1787                 eed->fgoni= eedc->fgoni;
1788                 eed->crease= ((float)eedc->crease)/255.0f;
1789                 eed->bweight= ((float)eedc->bweight)/255.0f;
1790                 CustomData_to_em_block(&um->edata, &em->edata, a, &eed->data);
1791         }
1792         
1793         /* copy faces */
1794         for(a=0, efac= um->faces; a<um->totface; a++, efac++) {
1795                 if(efac->v4 != -1)
1796                         efa= addfacelist(em, evar[efac->v1], evar[efac->v2], evar[efac->v3], evar[efac->v4], NULL, NULL);
1797                 else 
1798                         efa= addfacelist(em, evar[efac->v1], evar[efac->v2], evar[efac->v3], NULL, NULL ,NULL);
1799
1800                 efa->mat_nr= efac->mat_nr;
1801                 efa->flag= efac->flag;
1802                 efa->f= efac->f;
1803                 efa->h= efac->h;
1804                 efa->fgonf= efac->fgonf;
1805                 
1806                 CustomData_to_em_block(&um->fdata, &em->fdata, a, &efa->data);
1807         }
1808         
1809         end_editmesh_fastmalloc();
1810         if(evar) MEM_freeN(evar);
1811         
1812         em->totvert = um->totvert;
1813         em->totedge = um->totedge;
1814         em->totface = um->totface;
1815         /*restore stored editselections*/
1816         if(um->totsel){
1817                 EM_init_index_arrays(em, 1,1,1);
1818                 for(a=0, esec= um->selected; a<um->totsel; a++, esec++){
1819                         ese = MEM_callocN(sizeof(EditSelection), "Edit Selection");
1820                         ese->type = esec->type;
1821                         if(ese->type == EDITVERT) ese->data = EM_get_vert_for_index(esec->index); else
1822                         if(ese->type == EDITEDGE) ese->data = EM_get_edge_for_index(esec->index); else
1823                         if(ese->type == EDITFACE) ese->data = EM_get_face_for_index(esec->index);
1824                         BLI_addtail(&(em->selected),ese);
1825                 }
1826                 EM_free_index_arrays();
1827         }
1828
1829         /* restore total selections */
1830         EM_nvertices_selected(em);
1831         EM_nedges_selected(em);
1832         EM_nfaces_selected(em);
1833
1834 // XXX  retopo_free_paint();
1835 //      em->retopo_paint_data= retopo_paint_data_copy(um->retopo_paint_data);
1836 //      scene->toolsettings->retopo_mode= um->retopo_mode;
1837 //      if(scene->toolsettings->retopo_mode) {
1838 // XXX          if(G.vd->depths) G.vd->depths->damaged= 1;
1839 //              retopo_queue_updates(G.vd);
1840 //              retopo_paint_view_update(G.vd);
1841 //      }
1842         
1843 }
1844
1845 static void *getEditMesh(bContext *C)
1846 {
1847         Object *obedit= CTX_data_edit_object(C);
1848         if(obedit && obedit->type==OB_MESH) {
1849                 Mesh *me= obedit->data;
1850                 return me->edit_mesh;
1851         }
1852         return NULL;
1853 }
1854
1855 /* and this is all the undo system needs to know */
1856 void undo_push_mesh(bContext *C, char *name)
1857 {
1858         undo_editmode_push(C, name, getEditMesh, free_undoMesh, undoMesh_to_editMesh, editMesh_to_undoMesh, NULL);
1859 }
1860
1861
1862
1863 /* *************** END UNDO *************/
1864
1865 static EditVert **g_em_vert_array = NULL;
1866 static EditEdge **g_em_edge_array = NULL;
1867 static EditFace **g_em_face_array = NULL;
1868
1869 void EM_init_index_arrays(EditMesh *em, int forVert, int forEdge, int forFace)
1870 {
1871         EditVert *eve;
1872         EditEdge *eed;
1873         EditFace *efa;
1874         int i;
1875
1876         if (forVert) {
1877                 em->totvert= BLI_countlist(&em->verts);
1878
1879                 if(em->totvert) {
1880                         g_em_vert_array = MEM_mallocN(sizeof(*g_em_vert_array)*em->totvert, "em_v_arr");
1881
1882                         for (i=0,eve=em->verts.first; eve; i++,eve=eve->next)
1883                                 g_em_vert_array[i] = eve;
1884                 }
1885         }
1886
1887         if (forEdge) {
1888                 em->totedge= BLI_countlist(&em->edges);
1889
1890                 if(em->totedge) {
1891                         g_em_edge_array = MEM_mallocN(sizeof(*g_em_edge_array)*em->totedge, "em_e_arr");
1892
1893                         for (i=0,eed=em->edges.first; eed; i++,eed=eed->next)
1894                                 g_em_edge_array[i] = eed;
1895                 }
1896         }
1897
1898         if (forFace) {
1899                 em->totface= BLI_countlist(&em->faces);
1900
1901                 if(em->totface) {
1902                         g_em_face_array = MEM_mallocN(sizeof(*g_em_face_array)*em->totface, "em_f_arr");
1903
1904                         for (i=0,efa=em->faces.first; efa; i++,efa=efa->next)
1905                                 g_em_face_array[i] = efa;
1906                 }
1907         }
1908 }
1909
1910 void EM_free_index_arrays(void)
1911 {
1912         if (g_em_vert_array) MEM_freeN(g_em_vert_array);
1913         if (g_em_edge_array) MEM_freeN(g_em_edge_array);
1914         if (g_em_face_array) MEM_freeN(g_em_face_array);
1915         g_em_vert_array = NULL;
1916         g_em_edge_array = NULL;
1917         g_em_face_array = NULL;
1918 }
1919
1920 EditVert *EM_get_vert_for_index(int index)
1921 {
1922         return g_em_vert_array?g_em_vert_array[index]:NULL;
1923 }
1924
1925 EditEdge *EM_get_edge_for_index(int index)
1926 {
1927         return g_em_edge_array?g_em_edge_array[index]:NULL;
1928 }
1929
1930 EditFace *EM_get_face_for_index(int index)
1931 {
1932         return g_em_face_array?g_em_face_array[index]:NULL;
1933 }
1934
1935 /* can we edit UV's for this mesh?*/
1936 int EM_texFaceCheck(EditMesh *em)
1937 {
1938         /* some of these checks could be a touch overkill */
1939         if (    (em) &&
1940                         (em->faces.first) &&
1941                         (CustomData_has_layer(&em->fdata, CD_MTFACE)))
1942                 return 1;
1943         return 0;
1944 }
1945
1946 /* can we edit colors for this mesh?*/
1947 int EM_vertColorCheck(EditMesh *em)
1948 {
1949         /* some of these checks could be a touch overkill */
1950         if (    (em) &&
1951                         (em->faces.first) &&
1952                         (CustomData_has_layer(&em->fdata, CD_MCOL)))
1953                 return 1;
1954         return 0;
1955 }
1956
1957
1958 void em_setup_viewcontext(bContext *C, ViewContext *vc)
1959 {
1960         view3d_set_viewcontext(C, vc);
1961         
1962         if(vc->obedit) {
1963                 Mesh *me= vc->obedit->data;
1964                 vc->em= me->edit_mesh;
1965         }
1966 }