svn merge ^/trunk/blender -r43294:43338
authorCampbell Barton <ideasman42@gmail.com>
Fri, 13 Jan 2012 01:39:57 +0000 (01:39 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Fri, 13 Jan 2012 01:39:57 +0000 (01:39 +0000)
36 files changed:
1  2 
CMakeLists.txt
source/blender/blenkernel/BKE_blender.h
source/blender/blenkernel/intern/displist.c
source/blender/blenkernel/intern/editderivedmesh.c
source/blender/blenlib/BLI_editVert.h
source/blender/blenloader/intern/readfile.c
source/blender/bmesh/editmesh_tools.c
source/blender/editors/curve/editcurve.c
source/blender/editors/curve/editfont.c
source/blender/editors/interface/interface_templates.c
source/blender/editors/mesh/bmesh_tools.c
source/blender/editors/mesh/mesh_data.c
source/blender/editors/object/object_add.c
source/blender/editors/object/object_edit.c
source/blender/editors/object/object_modifier.c
source/blender/editors/object/object_relations.c
source/blender/editors/object/object_transform.c
source/blender/editors/render/render_shading.c
source/blender/editors/screen/screen_ops.c
source/blender/editors/sculpt_paint/paint_image.c
source/blender/editors/sculpt_paint/paint_vertex.c
source/blender/editors/sculpt_paint/sculpt.c
source/blender/editors/space_image/space_image.c
source/blender/editors/space_view3d/drawobject.c
source/blender/editors/space_view3d/view3d_buttons.c
source/blender/editors/space_view3d/view3d_toolbar.c
source/blender/editors/transform/transform.c
source/blender/editors/transform/transform_generics.c
source/blender/editors/transform/transform_snap.c
source/blender/editors/util/undo.c
source/blender/editors/uvedit/uvedit_unwrap_ops.c
source/blender/makesrna/RNA_access.h
source/blender/makesrna/intern/rna_scene.c
source/blender/render/intern/source/convertblender.c
source/blender/windowmanager/intern/wm_init_exit.c
source/gameengine/Converter/BL_BlenderDataConversion.cpp

diff --cc CMakeLists.txt
Simple merge
  
  extern GLubyte stipple_quarttone[128]; /* glutil.c, bad level data */
  
-       em->retopo_paint_data= NULL;
 +
 +BMEditMesh *BMEdit_Create(BMesh *bm)
 +{
 +      BMEditMesh *tm = MEM_callocN(sizeof(BMEditMesh), __func__);
 +
 +      tm->bm = bm;
 +
 +      BMEdit_RecalcTesselation(tm);
 +
 +      return tm;
 +}
 +
 +BMEditMesh *BMEdit_Copy(BMEditMesh *tm)
 +{
 +      BMEditMesh *tm2 = MEM_callocN(sizeof(BMEditMesh), __func__);
 +      *tm2 = *tm;
 +
 +      tm2->derivedCage = tm2->derivedFinal = NULL;
 +
 +      tm2->bm = BM_Copy_Mesh(tm->bm);
 +
 +      /*The tesselation is NOT calculated on the copy here,
 +        because currently all the callers of this function use
 +        it to make a backup copy of the BMEditMesh to restore
 +        it in the case of errors in an operation. For perf
 +        reasons, in that case it makes more sense to do the
 +        tesselation only when/if that copy ends up getting
 +        used.*/
 +      tm2->looptris = NULL;
 +
 +      tm2->vert_index = NULL;
 +      tm2->edge_index = NULL;
 +      tm2->face_index = NULL;
 +
 +      return tm2;
 +}
 +
 +static void BMEdit_RecalcTesselation_intern(BMEditMesh *tm)
 +{
 +      /* use this to avoid locking pthread for _every_ polygon
 +       * and calling the fill function */
 +#define USE_TESSFACE_SPEEDUP
 +
 +      BMesh *bm = tm->bm;
 +      BMLoop *(*looptris)[3]= NULL;
 +      BLI_array_declare(looptris);
 +      BMIter iter, liter;
 +      BMFace *f;
 +      BMLoop *l;
 +      int i = 0, j;
 +
 +      if (tm->looptris) MEM_freeN(tm->looptris);
 +
 +      f = BMIter_New(&iter, bm, BM_FACES_OF_MESH, NULL);
 +      for ( ; f; f=BMIter_Step(&iter)) {
 +              /*don't consider two-edged faces*/
 +              if (f->len < 3) {
 +                      /* do nothing */
 +              }
 +
 +#ifdef USE_TESSFACE_SPEEDUP
 +
 +              /* no need to ensure the loop order, we know its ok */
 +
 +              else if (f->len == 3) {
 +                      BLI_array_growone(looptris);
 +                      l = BMIter_New(&liter, bm, BM_LOOPS_OF_FACE, f);
 +                      for (j=0; l; l=BMIter_Step(&liter), j++) {
 +                              looptris[i][j] = l;
 +                      }
 +                      i += 1;
 +              }
 +              else if (f->len == 4) {
 +                      BMLoop *ltmp[4];
 +                      BLI_array_growitems(looptris, 2);
 +
 +                      l = BMIter_New(&liter, bm, BM_LOOPS_OF_FACE, f);
 +                      for (j=0; l; l=BMIter_Step(&liter), j++) {
 +                              ltmp[j] = l;
 +                      }
 +
 +                      looptris[i][0] = ltmp[0];
 +                      looptris[i][1] = ltmp[1];
 +                      looptris[i][2] = ltmp[2];
 +                      i += 1;
 +
 +                      looptris[i][0] = ltmp[0];
 +                      looptris[i][1] = ltmp[2];
 +                      looptris[i][2] = ltmp[3];
 +                      i += 1;
 +              }
 +
 +#endif /* USE_TESSFACE_SPEEDUP */
 +
 +              else {
 +                      EditVert *v, *lastv=NULL, *firstv=NULL;
 +                      EditEdge *e;
 +                      EditFace *efa;
 +
 +                      BLI_begin_edgefill();
 +                      /*scanfill time*/
 +                      l = BMIter_New(&liter, bm, BM_LOOPS_OF_FACE, f);
 +                      for (j=0; l; l=BMIter_Step(&liter), j++) {
 +                              /*mark order*/
 +                              BM_SetIndex(l, j); /* set_loop */
 +
 +                              v = BLI_addfillvert(l->v->co);
 +                              v->tmp.p = l;
 +
 +                              if (lastv) {
 +                                      e = BLI_addfilledge(lastv, v);
 +                              }
 +
 +                              lastv = v;
 +                              if (firstv==NULL) firstv = v;
 +                      }
 +
 +                      /*complete the loop*/
 +                      BLI_addfilledge(firstv, v);
 +
 +                      BLI_edgefill(2);
 +
 +                      for (efa = fillfacebase.first; efa; efa=efa->next) {
 +                              BMLoop *l1= efa->v1->tmp.p;
 +                              BMLoop *l2= efa->v2->tmp.p;
 +                              BMLoop *l3= efa->v3->tmp.p;
 +
 +                              BLI_array_growone(looptris);
 +
 +                              if (BM_GetIndex(l1) > BM_GetIndex(l2)) { SWAP(BMLoop*, l1, l2); }
 +                              if (BM_GetIndex(l2) > BM_GetIndex(l3)) { SWAP(BMLoop*, l2, l3); }
 +                              if (BM_GetIndex(l1) > BM_GetIndex(l2)) { SWAP(BMLoop*, l1, l2); }
 +
 +                              looptris[i][0] = l1;
 +                              looptris[i][1] = l2;
 +                              looptris[i][2] = l3;
 +                              i += 1;
 +                      }
 +
 +                      BLI_end_edgefill();
 +              }
 +      }
 +
 +      tm->tottri = i;
 +      tm->looptris = looptris;
 +
 +#undef USE_TESSFACE_SPEEDUP
 +
 +}
 +
 +void BMEdit_RecalcTesselation(BMEditMesh *em)
 +{
 +      BMEdit_RecalcTesselation_intern(em);
 +
 +      if (em->derivedFinal && em->derivedFinal == em->derivedCage) {
 +              if (em->derivedFinal->recalcTesselation)
 +                      em->derivedFinal->recalcTesselation(em->derivedFinal);
 +      }
 +      else if (em->derivedFinal) {
 +              if (em->derivedCage->recalcTesselation)
 +                      em->derivedCage->recalcTesselation(em->derivedCage);
 +              if (em->derivedFinal->recalcTesselation)
 +                      em->derivedFinal->recalcTesselation(em->derivedFinal);
 +      }
 +}
 +
 +void BMEdit_UpdateLinkedCustomData(BMEditMesh *em)
 +{
 +      BMesh *bm = em->bm;
 +      int act;
 +
 +      if (CustomData_has_layer(&bm->pdata, CD_MTEXPOLY)) {
 +              act = CustomData_get_active_layer(&bm->pdata, CD_MTEXPOLY);
 +              CustomData_set_layer_active(&bm->ldata, CD_MLOOPUV, act);
 +
 +              act = CustomData_get_render_layer(&bm->pdata, CD_MTEXPOLY);
 +              CustomData_set_layer_render(&bm->ldata, CD_MLOOPUV, act);
 +
 +              act = CustomData_get_clone_layer(&bm->pdata, CD_MTEXPOLY);
 +              CustomData_set_layer_clone(&bm->ldata, CD_MLOOPUV, act);
 +
 +              act = CustomData_get_stencil_layer(&bm->pdata, CD_MTEXPOLY);
 +              CustomData_set_layer_stencil(&bm->ldata, CD_MLOOPUV, act);
 +      }
 +}
 +
 +/*does not free the BMEditMesh struct itself*/
 +void BMEdit_Free(BMEditMesh *em)
 +{
 +      if (em->derivedFinal) {
 +              if (em->derivedFinal!=em->derivedCage) {
 +                      em->derivedFinal->needsFree= 1;
 +                      em->derivedFinal->release(em->derivedFinal);
 +              }
 +              em->derivedFinal= NULL;
 +      }
 +      if (em->derivedCage) {
 +              em->derivedCage->needsFree= 1;
 +              em->derivedCage->release(em->derivedCage);
 +              em->derivedCage= NULL;
 +      }
 +
 +      if (em->looptris) MEM_freeN(em->looptris);
 +
 +      if (em->vert_index) MEM_freeN(em->vert_index);
 +      if (em->edge_index) MEM_freeN(em->edge_index);
 +      if (em->face_index) MEM_freeN(em->face_index);
 +
 +      if (em->bm)
 +              BM_Free_Mesh(em->bm);
 +}
 +
 +/*
 +ok, basic design:
 +
 +the bmesh derivedmesh exposes the mesh as triangles.  it stores pointers
 +to three loops per triangle.  the derivedmesh stores a cache of tesselations
 +for each face.  this cache will smartly update as needed (though at first
 +it'll simply be more brute force).  keeping track of face/edge counts may
 +be a small problbm.
 +
 +this won't be the most efficient thing, considering that internal edges and
 +faces of tesselations are exposed.  looking up an edge by index in particular
 +is likely to be a little slow.
 +*/
 +
 +typedef struct EditDerivedBMesh {
 +      DerivedMesh dm;
 +
 +      Object *ob;
 +      BMEditMesh *tc;
 +
 +      float (*vertexCos)[3];
 +      float (*vertexNos)[3];
 +      float (*faceNos)[3];
 +
 +      /*lookup caches; these are rebuilt on dm->RecalcTesselation()
 +        (or when the derivedmesh is created, of course)*/
 +      GHash *vhash, *ehash, *fhash;
 +      BMVert **vtable;
 +      BMEdge **etable;
 +      BMFace **ftable;
 +
 +      /*private variables, for number of verts/edges/faces
 +        within the above hash/table members*/
 +      int tv, te, tf;
 +} EditDerivedBMesh;
 +
 +static void bmdm_recalc_lookups(EditDerivedBMesh *bmdm)
 +{
 +      BMIter iter;
 +      BMHeader *h;
 +      int a, i, iters[3] = {BM_VERTS_OF_MESH, BM_EDGES_OF_MESH, BM_FACES_OF_MESH};
 +
 +      bmdm->tv = bmdm->tc->bm->totvert;
 +      bmdm->te = bmdm->tc->bm->totedge;
 +      bmdm->tf = bmdm->tc->bm->totface;
 +
 +      if (bmdm->vhash) BLI_ghash_free(bmdm->vhash, NULL, NULL);
 +      if (bmdm->ehash) BLI_ghash_free(bmdm->ehash, NULL, NULL);
 +      if (bmdm->fhash) BLI_ghash_free(bmdm->fhash, NULL, NULL);
 +
 +      bmdm->vhash = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "bmesh derived");
 +      bmdm->ehash = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "bmesh derived");
 +      bmdm->fhash = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "bmesh derived");
 +
 +      if (bmdm->vtable) MEM_freeN(bmdm->vtable);
 +      if (bmdm->etable) MEM_freeN(bmdm->etable);
 +      if (bmdm->ftable) MEM_freeN(bmdm->ftable);
 +
 +      if (bmdm->tc->bm->totvert)
 +              bmdm->vtable = MEM_mallocN(sizeof(void**)*bmdm->tc->bm->totvert, "bmdm->vtable");
 +      else bmdm->vtable = NULL;
 +
 +      if (bmdm->tc->bm->totedge)
 +              bmdm->etable = MEM_mallocN(sizeof(void**)*bmdm->tc->bm->totedge, "bmdm->etable");
 +      else bmdm->etable = NULL;
 +
 +      if (bmdm->tc->bm->totface)
 +              bmdm->ftable = MEM_mallocN(sizeof(void**)*bmdm->tc->bm->totface, "bmdm->ftable");
 +      else bmdm->ftable = NULL;
 +
 +      for (a=0; a<3; a++) {
 +              h = BMIter_New(&iter, bmdm->tc->bm, iters[a], NULL);
 +              for (i=0; h; h=BMIter_Step(&iter), i++) {
 +                      switch (a) {
 +                              case 0:
 +                                      bmdm->vtable[i] = (BMVert*) h;
 +                                      BLI_ghash_insert(bmdm->vhash, h, SET_INT_IN_POINTER(i));
 +                                      break;
 +                              case 1:
 +                                      bmdm->etable[i] = (BMEdge*) h;
 +                                      BLI_ghash_insert(bmdm->ehash, h, SET_INT_IN_POINTER(i));
 +                                      break;
 +                              case 2:
 +                                      bmdm->ftable[i] = (BMFace*) h;
 +                                      BLI_ghash_insert(bmdm->fhash, h, SET_INT_IN_POINTER(i));
 +                                      break;
 +
 +                      }
 +              }
 +      }
 +}
 +
 +static void emDM_calcNormals(DerivedMesh *UNUSED(dm))
 +{
 +      /* Nothing to do: normals are already calculated and stored on the
 +         BMVerts and BMFaces */
 +}
 +
 +static void emDM_recalcTesselation(DerivedMesh *UNUSED(dm))
 +{
 +      //EditDerivedBMesh *bmdm= (EditDerivedBMesh*) dm;
 +
 +      //bmdm_recalc_lookups(bmdm);
 +}
 +
  static void emDM_foreachMappedVert(
                DerivedMesh *dm,
                void (*func)(void *userData, int index, float *co, float *no_f, short *no_s),
@@@ -42,8 -42,6 +42,7 @@@
  #include "BLO_sys_types.h" // for intptr_t support
  
  struct DerivedMesh;
- struct RetopoPaintData;
 +struct BLI_mempool;
  
  /* note; changing this also might affect the undo copy in editmesh.c */
  typedef struct EditVert
@@@ -4133,1470 -4035,27 +4133,645 @@@ static void bevel_mesh(float bsize, in
                efa= efa->next;
        }
  
 -}
 +#ifdef BEV_DEBUG
 +      fprintf(stderr,"bevel_mesh: split\n");
  #endif
 +      
 +      efa= em->faces.first;
 +      while (efa) {
 +              if (efa->f1 & 1) {
 +                      efa->f1-= 1;
 +                      v1= addvertlist(efa->v1->co, efa->v1);
 +                      v1->f= efa->v1->f & ~128;
 +                      efa->v1->tmp.v = v1;
 +
 +                      v1= addvertlist(efa->v2->co, efa->v2);
 +                      v1->f= efa->v2->f & ~128;
 +                      efa->v2->tmp.v = v1;
 +
 +                      v1= addvertlist(efa->v3->co, efa->v3);
 +                      v1->f= efa->v3->f & ~128;
 +                      efa->v3->tmp.v = v1;
 +
 +                      if (efa->v4) {
 +                              v1= addvertlist(efa->v4->co, efa->v4);
 +                              v1->f= efa->v4->f & ~128;
 +                              efa->v4->tmp.v = v1;
 +                      }
  
 -/********************** Rip Operator *************************/
 +                      /* Needs better adaption of creases? */
 +                      addedgelist(efa->e1->v1->tmp.v, 
 +                                              efa->e1->v2->tmp.v, 
 +                                              efa->e1);
 +                      addedgelist(efa->e2->v1->tmp.v,
 +                                              efa->e2->v2->tmp.v, 
 +                                              efa->e2);
 +                      addedgelist(efa->e3->v1->tmp.v,
 +                                              efa->e3->v2->tmp.v,
 +                                              efa->e3);
 +                      if (efa->e4) addedgelist(efa->e4->v1->tmp.v,
 +                                                                       efa->e4->v2->tmp.v,
 +                                                                       efa->e4);
 +
 +                      if(efa->v4) {
 +                              v1 = efa->v1->tmp.v;
 +                              v2 = efa->v2->tmp.v;
 +                              v3 = efa->v3->tmp.v;
 +                              v4 = efa->v4->tmp.v;
 +                              addfacelist(v1, v2, v3, v4, efa,NULL);
 +                      } else {
 +                              v1= efa->v1->tmp.v;
 +                              v2= efa->v2->tmp.v;
 +                              v3= efa->v3->tmp.v;
 +                              addfacelist(v1, v2, v3, 0, efa,NULL);
 +                      }
 +
 +                      efa= efa-> next;
 +              } else {
 +                      efa= efa->next;
 +              }
 +      }
 +      
 +      for(efa= em->faces.first; efa; efa= efa->next) {
 +              if( (efa->v1->f & 128) && (efa->v2->f & 128) && (efa->v3->f & 128) ) {
 +                      if(efa->v4==NULL || (efa->v4->f & 128)) efa->f |= 128;
 +              }
 +      }
  
 -/* helper to find edge for edge_rip */
 -static float mesh_rip_edgedist(ARegion *ar, float mat[][4], float *co1, float *co2, const int mval[2])
 -{
 -      float vec1[2], vec2[2], mvalf[2];
 +      delfaceflag(128); // works with face flag now
  
 -      ED_view3d_project_float(ar, co1, vec1, mat);
 -      ED_view3d_project_float(ar, co2, vec2, mat);
 -      mvalf[0]= (float)mval[0];
 -      mvalf[1]= (float)mval[1];
 +      /* tag all faces for shrink*/
 +      efa= em->faces.first;
 +      while (efa) {
 +              if (faceselectedAND(efa, 1)||allfaces) {
 +                      efa->f1= 2;
 +              }
 +              efa= efa->next;
 +      }
  
 -      return dist_to_line_segment_v2(mvalf, vec1, vec2);
 -}
 +#ifdef BEV_DEBUG
 +      fprintf(stderr,"bevel_mesh: make edge quads\n");
 +#endif
  
 -/* helper for below */
 -static void mesh_rip_setface(EditMesh *em, EditFace *sefa)
 -{
 +      /* find edges that are on each other and make quads between them */
 +
 +      eed= em->edges.first;
 +      while(eed) {
 +              eed->f2= eed->f1= 0;
 +              if ( ((eed->v1->f & eed->v2->f) & 1) || allfaces) 
 +                      eed->f1 |= 4;   /* original edges */
 +              eed->tmp.v = 0;
 +              eed= eed->next;
 +      }
 +
 +      eed= em->edges.first;
 +      while (eed) {
 +              if ( ((eed->f1 & 2)==0) && (eed->f1 & 4) ) {
 +                      eed2= em->edges.first;
 +                      while (eed2) {
 +                              if ( (eed2 != eed) && ((eed2->f1 & 2)==0) && (eed->f1 & 4) ) {
 +                                      if (
 +                                         (eed->v1 != eed2->v1) &&
 +                                         (eed->v1 != eed2->v2) &&                                        
 +                                         (eed->v2 != eed2->v1) &&
 +                                         (eed->v2 != eed2->v2) &&     (
 +                                         ( compare_v3v3(eed->v1->co, eed2->v1->co, limit) &&
 +                                               compare_v3v3(eed->v2->co, eed2->v2->co, limit) ) ||
 +                                         ( compare_v3v3(eed->v1->co, eed2->v2->co, limit) &&
 +                                               compare_v3v3(eed->v2->co, eed2->v1->co, limit) ) ) )
 +                                              {
 +                                              
 +#ifdef BEV_DEBUG                                              
 +                                              fprintf(stderr, "bevel_mesh: edge quad\n");
 +#endif                                                
 +                                              
 +                                              eed->f1 |= 2;   /* these edges are finished */
 +                                              eed2->f1 |= 2;
 +                                              
 +                                              example= NULL;
 +                                              efa= em->faces.first;   /* search example face (for mat_nr, ME_SMOOTH, ...) */
 +                                              while (efa) {
 +                                                      if ( (efa->e1 == eed) ||
 +                                                               (efa->e2 == eed) ||
 +                                                               (efa->e3 == eed) ||
 +                                                               (efa->e4 && (efa->e4 == eed)) ) {
 +                                                              example= efa;
 +                                                              efa= NULL;
 +                                                      }
 +                                                      if (efa) efa= efa->next;
 +                                              }
 +                                              
 +                                              neweve[0]= eed->v1; neweve[1]= eed->v2;
 +                                              neweve[2]= eed2->v1; neweve[3]= eed2->v2;
 +                                              
 +                                              if(exist_face(neweve[0], neweve[1], neweve[2], neweve[3])==0) {
 +                                                      efa= NULL;
 +
 +                                                      if (compare_v3v3(eed->v1->co, eed2->v2->co, limit)) {
 +                                                              efa= addfacelist(neweve[0], neweve[1], neweve[2], neweve[3], example,NULL);
 +                                                      } else {
 +                                                              efa= addfacelist(neweve[0], neweve[2], neweve[3], neweve[1], example,NULL);
 +                                                      }
 +                                              
 +                                                      if(efa) {
 +                                                              float inp;      
 +                                                              normal_tri_v3( efa->n,efa->v1->co, efa->v2->co, efa->v3->co);
 +                                                              inp= efa->n[0]*G.vd->viewmat[0][2] + efa->n[1]*G.vd->viewmat[1][2] + efa->n[2]*G.vd->viewmat[2][2];
 +                                                              if(inp < 0.0) flipface(efa);
 +#ifdef BEV_DEBUG
 +                                                              efa->mat_nr= 1;
 +#endif
 +                                                      } else fprintf(stderr,"bevel_mesh: error creating face\n");
 +                                              }
 +                                              eed2= NULL;
 +                                      }
 +                              }
 +                              if (eed2) eed2= eed2->next;
 +                      }
 +              }
 +              eed= eed->next;
 +      }
 +
 +      eed= em->edges.first;
 +      while(eed) {
 +              eed->f2= eed->f1= 0;
 +              eed->f1= 0;
 +              eed->v1->f1 &= ~1;
 +              eed->v2->f1 &= ~1;              
 +              eed->tmp.v = 0;
 +              eed= eed->next;
 +      }
 +
 +#ifdef BEV_DEBUG
 +      fprintf(stderr,"bevel_mesh: find clusters\n");
 +#endif        
 +
 +      /* Look for vertex clusters */
 +
 +      eve= em->verts.first;
 +      while (eve) {
 +              eve->f &= ~(64|128);
 +              eve->tmp.v = NULL;
 +              eve= eve->next;
 +      }
 +      
 +      /* eve->f: 128: first vertex in a list (->tmp.v) */
 +      /*                64: vertex is in a list */
 +      
 +      eve= em->verts.first;
 +      while (eve) {
 +              eve2= em->verts.first;
 +              eve3= NULL;
 +              while (eve2) {
 +                      if ((eve2 != eve) && ((eve2->f & (64|128))==0)) {
 +                              if (compare_v3v3(eve->co, eve2->co, limit)) {
 +                                      if ((eve->f & (128|64)) == 0) {
 +                                              /* fprintf(stderr,"Found vertex cluster:\n  *\n  *\n"); */
 +                                              eve->f |= 128;
 +                                              eve->tmp.v = eve2;
 +                                              eve3= eve2;
 +                                      } else if ((eve->f & 64) == 0) {
 +                                              /* fprintf(stderr,"  *\n"); */
 +                                              if (eve3) eve3->tmp.v = eve2;
 +                                              eve2->f |= 64;
 +                                              eve3= eve2;
 +                                      }
 +                              }
 +                      }
 +                      eve2= eve2->next;
 +                      if (!eve2) {
 +                              if (eve3) eve3->tmp.v = NULL;
 +                      }
 +              }
 +              eve= eve->next;
 +      }
 +
 +#ifdef BEV_DEBUG
 +      fprintf(stderr,"bevel_mesh: shrink faces\n");
 +#endif        
 +
 +      bevel_shrink_faces(bsize, 2);
 +
 +#ifdef BEV_DEBUG
 +      fprintf(stderr,"bevel_mesh: fill clusters\n");
 +#endif
 +      
 +      /* Make former vertex clusters faces */
 +
 +      eve= em->verts.first;
 +      while (eve) {
 +              eve->f &= ~64;
 +              eve= eve->next;
 +      }
 +
 +      eve= em->verts.first;
 +      while (eve) {
 +              if (eve->f & 128) {
 +                      eve->f &= ~128;
 +                      a= 0;
 +                      neweve[a]= eve;
 +                      eve2 = eve->tmp.v;
 +                      while (eve2) {
 +                              a++;
 +                              neweve[a]= eve2;
 +                              eve2 = eve2->tmp.v;
 +                      }
 +                      a++;
 +                      efa= NULL;
 +                      if (a>=3) {
 +                              example= NULL;
 +                              efa= em->faces.first;   /* search example face */
 +                              while (efa) {
 +                                      if ( (efa->v1 == neweve[0]) ||
 +                                               (efa->v2 == neweve[0]) ||
 +                                               (efa->v3 == neweve[0]) ||
 +                                               (efa->v4 && (efa->v4 == neweve[0])) ) {
 +                                              example= efa;
 +                                              efa= NULL;
 +                                      }
 +                                      if (efa) efa= efa->next;
 +                              }
 +#ifdef BEV_DEBUG                              
 +                              fprintf(stderr,"bevel_mesh: Making %d-gon\n", a);
 +#endif                                
 +                              if (a>4) {
 +                                      cent[0]= cent[1]= cent[2]= 0.0;                         
 +                                      INIT_MINMAX(min, max);                          
 +                                      for (b=0; b<a; b++) {
 +                                              add_v3_v3v3(cent, cent, neweve[b]->co);
 +                                              DO_MINMAX(neweve[b]->co, min, max);
 +                                      }
 +                                      cent[0]= (min[0]+max[0])/2;
 +                                      cent[1]= (min[1]+max[1])/2;
 +                                      cent[2]= (min[2]+max[2])/2;
 +                                      eve2= addvertlist(cent, NULL);
 +                                      eve2->f |= 1;
 +                                      eed= em->edges.first;
 +                                      while (eed) {
 +                                              c= 0;
 +                                              for (b=0; b<a; b++) 
 +                                                      if ((neweve[b]==eed->v1) || (neweve[b]==eed->v2)) c++;
 +                                              if (c==2) {
 +                                                      if(exist_face(eed->v1, eed->v2, eve2, 0)==0) {
 +                                                              efa= addfacelist(eed->v1, eed->v2, eve2, 0, example,NULL);
 +#ifdef BEV_DEBUG
 +                                                              efa->mat_nr= 2;
 +#endif                                                                
 +                                                      }
 +                                              }
 +                                              eed= eed->next;
 +                                      }
 +                              } else if (a==4) {
 +                                      if(exist_face(neweve[0], neweve[1], neweve[2], neweve[3])==0) {
 +                                              /* the order of vertices can be anything, three cases to check */
 +                                              if( convex(neweve[0]->co, neweve[1]->co, neweve[2]->co, neweve[3]->co) ) {
 +                                                      efa= addfacelist(neweve[0], neweve[1], neweve[2], neweve[3], NULL, NULL);
 +                                              }
 +                                              else if( convex(neweve[0]->co, neweve[2]->co, neweve[3]->co, neweve[1]->co) ) {
 +                                                      efa= addfacelist(neweve[0], neweve[2], neweve[3], neweve[1], NULL, NULL);
 +                                              }
 +                                              else if( convex(neweve[0]->co, neweve[2]->co, neweve[1]->co, neweve[3]->co) ) {
 +                                                      efa= addfacelist(neweve[0], neweve[2], neweve[1], neweve[3], NULL, NULL);
 +                                              }
 +                                      }                               
 +                              }
 +                              else if (a==3) {
 +                                      if(exist_face(neweve[0], neweve[1], neweve[2], 0)==0)
 +                                              efa= addfacelist(neweve[0], neweve[1], neweve[2], 0, example, NULL);
 +                              }
 +                              if(efa) {
 +                                      float inp;      
 +                                      normal_tri_v3( efa->n,neweve[0]->co, neweve[1]->co, neweve[2]->co);
 +                                      inp= efa->n[0]*G.vd->viewmat[0][2] + efa->n[1]*G.vd->viewmat[1][2] + efa->n[2]*G.vd->viewmat[2][2];
 +                                      if(inp < 0.0) flipface(efa);
 +#ifdef BEV_DEBUG
 +                                              efa->mat_nr= 2;
 +#endif                                                                                                
 +                              }                               
 +                      }
 +              }
 +              eve= eve->next;
 +      }
 +
 +      eve= em->verts.first;
 +      while (eve) {
 +              eve->f1= 0;
 +              eve->f &= ~(128|64);
 +              eve->tmp.v= NULL;
 +              eve= eve->next;
 +      }
 +      
 +      recalc_editnormals();
 +      waitcursor(0);
 +      countall();
 +      allqueue(REDRAWVIEW3D, 0);
 +      DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
 +      
 +      removedoublesflag(1, 0, limit);
 +
 +      /* flush selected vertices to edges/faces */
 +      EM_select_flush();
 +
 +#undef BEV_DEBUG
 +}
 +
 +static void bevel_mesh_recurs(float bsize, short recurs, int allfaces) 
 +{
 +      float d;
 +      short nr;
 +
 +      d= bsize;
 +      for (nr=0; nr<recurs; nr++) {
 +              bevel_mesh(d, allfaces);
 +              if (nr==0) d /= 3; else d /= 2;
 +      }
 +}
 +
 +void bevel_menu(void)
 +{
 +      BME_Mesh *bm;
 +      BME_TransData_Head *td;
 +      TransInfo *t;
 +      int options, res, gbm_free = 0;
 +
 +      t = BIF_GetTransInfo();
 +      if (!G.editBMesh) {
 +              G.editBMesh = MEM_callocN(sizeof(*(G.editBMesh)),"bevel_menu() G.editBMesh");
 +              gbm_free = 1;
 +      }
 +
 +      G.editBMesh->options = BME_BEVEL_RUNNING | BME_BEVEL_SELECT;
 +      G.editBMesh->res = 1;
 +
 +      while(G.editBMesh->options & BME_BEVEL_RUNNING) {
 +              options = G.editBMesh->options;
 +              res = G.editBMesh->res;
 +              bm = BME_editmesh_to_bmesh(G.editMesh);
 +              BIF_undo_push("Pre-Bevel");
 +              free_editMesh(G.editMesh);
 +              BME_bevel(bm,0.1f,res,options,0,0,&td);
 +              BME_bmesh_to_editmesh(bm, td);
 +              EM_selectmode_flush();
 +              G.editBMesh->bm = bm;
 +              G.editBMesh->td = td;
 +              initTransform(TFM_BEVEL,CTX_BMESH);
 +              Transform();
 +              BME_free_transdata(td);
 +              BME_free_mesh(bm);
 +              if (t->state != TRANS_CONFIRM) {
 +                      BIF_undo();
 +              }
 +              if (options == G.editBMesh->options) {
 +                      G.editBMesh->options &= ~BME_BEVEL_RUNNING;
 +              }
 +      }
 +
 +      if (gbm_free) {
 +              MEM_freeN(G.editBMesh);
 +              G.editBMesh = NULL;
 +      }
 +}
 +
 +
 +void bevel_menu_old()
 +{
 +      char Finished = 0, Canceled = 0, str[100], Recalc = 0;
 +      short mval[2], oval[2], curval[2], event = 0, recurs = 1, nr;
 +      float vec[3], d, drawd=0.0, center[3], fac = 1;
 +
 +      getmouseco_areawin(mval);
 +      oval[0] = mval[0]; oval[1] = mval[1];
 +
 +      // Silly hackish code to initialise the variable (warning if not done)
 +      // while still drawing in the first iteration (and without using another variable)
 +      curval[0] = mval[0] + 1; curval[1] = mval[1] + 1;
 +
 +      // Init grabz for window to vec conversions
 +      initgrabz(-G.vd->ofs[0], -G.vd->ofs[1], -G.vd->ofs[2]);
 +      window_to_3d(center, mval[0], mval[1]);
 +
 +      if(button(&recurs, 1, 4, "Recursion:")==0) return;
 +
 +      for (nr=0; nr<recurs-1; nr++) {
 +              if (nr==0) fac += 1.0f/3.0f; else fac += 1.0f/(3 * nr * 2.0f);
 +      }
 +      
 +      EM_set_flag_all(SELECT);
 +              
 +      SetBlenderCursor(SYSCURSOR);
 +
 +      while (Finished == 0)
 +      {
 +              getmouseco_areawin(mval);
 +              if (mval[0] != curval[0] || mval[1] != curval[1] || (Recalc == 1))
 +              {
 +                      Recalc = 0;
 +                      curval[0] = mval[0];
 +                      curval[1] = mval[1];
 +                      
 +                      window_to_3d(vec, mval[0]-oval[0], mval[1]-oval[1]);
 +                      d = normalize_v3(vec) / 10;
 +
 +
 +                      drawd = d * fac;
 +                      if (G.qual & LR_CTRLKEY)
 +                              drawd = (float) floor(drawd * 10.0f)/10.0f;
 +                      if (G.qual & LR_SHIFTKEY)
 +                              drawd /= 10;
 +
 +                      /*------------- Preview lines--------------- */
 +                      
 +                      /* uses callback mechanism to draw it all in current area */
 +                      scrarea_do_windraw(curarea);                    
 +                      
 +                      /* set window matrix to perspective, default an area returns with buttons transform */
 +                      persp(PERSP_VIEW);
 +                      /* make a copy, for safety */
 +                      glPushMatrix();
 +                      /* multiply with the object transformation */
 +                      mymultmatrix(G.obedit->obmat);
 +                      
 +                      glColor3ub(255, 255, 0);
 +
 +                      // PREVIEW CODE GOES HERE
 +                      bevel_shrink_draw(drawd, 2);
 +
 +                      /* restore matrix transform */
 +                      glPopMatrix();
 +                      
 +                      sprintf(str, "Bevel Size: %.4f          LMB to confirm, RMB to cancel, SPACE to input directly.", drawd);
 +                      headerprint(str);
 +
 +                      /* this also verifies other area/windows for clean swap */
 +                      screen_swapbuffers();
 +
 +                      persp(PERSP_WIN);
 +                      
 +                      glDrawBuffer(GL_FRONT);
 +                      
 +                      BIF_ThemeColor(TH_WIRE);
 +
 +                      setlinestyle(3);
 +                      glBegin(GL_LINE_STRIP); 
 +                              glVertex2sv(mval); 
 +                              glVertex2sv(oval); 
 +                      glEnd();
 +                      setlinestyle(0);
 +                      
 +                      persp(PERSP_VIEW);
 +                      bglFlush(); // flush display for frontbuffer
 +                      glDrawBuffer(GL_BACK);
 +              }
 +              while(qtest()) {
 +                      short val=0;                    
 +                      event= extern_qread(&val);      // extern_qread stores important events for the mainloop to handle 
 +
 +                      /* val==0 on key-release event */
 +                      if(val && (event==ESCKEY || event==RIGHTMOUSE || event==LEFTMOUSE || event==RETKEY || event==ESCKEY)) {
 +                              if (event==RIGHTMOUSE || event==ESCKEY)
 +                                      Canceled = 1;
 +                              Finished = 1;
 +                      }
 +                      else if (val && event==SPACEKEY) {
 +                              if (fbutton(&d, 0.000, 10.000, 10, 0, "Width:")!=0) {
 +                                      drawd = d * fac;
 +                                      Finished = 1;
 +                              }
 +                      }
 +                      else if (val) {
 +                              /* On any other keyboard event, recalc */
 +                              Recalc = 1;
 +                      }
 +
 +              }       
 +      }
 +      if (Canceled==0) {
 +              SetBlenderCursor(BC_WAITCURSOR);
 +              bevel_mesh_recurs(drawd/fac, recurs, 1);
 +              righthandfaces(1);
 +              SetBlenderCursor(SYSCURSOR);
 +              BIF_undo_push("Bevel");
 +      }
 +}
 +
- /* *********** END BEVEL *********/
- typedef struct SlideUv {
-       float origuv[2];
-       float *uv_up, *uv_down;
-       //float *fuv[4];
-       LinkNode *fuv_list;
- } SlideUv;
- typedef struct SlideVert {
-       EditEdge *up,*down;
-       EditVert origvert;
- } SlideVert;
- int EdgeLoopDelete(void)
- {
-       
-       /* temporal flag setting so we keep UVs when deleting edge loops,
-        * this is a bit of a hack but it works how you would want in almost all cases */
-       short uvcalc_flag_orig = G.scene->toolsettings->uvcalc_flag; 
-       G.scene->toolsettings->uvcalc_flag |= UVCALC_TRANSFORM_CORRECT;
-       
-       if(!EdgeSlide(1, 1)) {
-               return 0;
-       }
-       
-       /* restore uvcalc flag */
-       G.scene->toolsettings->uvcalc_flag = uvcalc_flag_orig;
-       
-       EM_select_more();
-       removedoublesflag(1,0, 0.001);
-       EM_select_flush();
-       DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
-       return 1;
- }
- int EdgeSlide(short immediate, float imperc)
- {
-       NumInput num;
-       EditMesh *em = G.editMesh;
-       EditFace *efa;
-       EditEdge *eed,*first=NULL,*last=NULL, *temp = NULL;
-       EditVert *ev, *nearest;
-       LinkNode *edgelist = NULL, *vertlist=NULL, *look;
-       GHash *vertgh;
-       SlideVert *tempsv;
-       float perc = 0, percp = 0,vertdist, projectMat[4][4], viewMat[4][4];
-       float shiftlabda= 0.0f,len = 0.0f;
-       int i = 0,j, numsel, numadded=0, timesthrough = 0, vertsel=0, prop=1, cancel = 0,flip=0;
-       int wasshift = 0;
-       
-       /* UV correction vars */
-       GHash **uvarray= NULL;
-       int  uvlay_tot= CustomData_number_of_layers(&G.editMesh->fdata, CD_MTFACE);
-       int uvlay_idx;
-       SlideUv *slideuvs=NULL, *suv=NULL, *suv_last=NULL;      
-       float uv_tmp[2];
-       LinkNode *fuv_link;
-       
-       short event, draw=1;
-       short mval[2], mvalo[2];
-       char str[128]; 
-       float labda = 0.0f;
-       
-       initNumInput(&num);
-               
-       view3d_get_object_project_mat(curarea, G.obedit, projectMat, viewMat);
-       
-       mvalo[0] = -1; mvalo[1] = -1; 
-       numsel =0;  
-       
-       // Get number of selected edges and clear some flags
-       for(eed=em->edges.first;eed;eed=eed->next) {
-               eed->f1 = 0;
-               eed->f2 = 0;   
-               if(eed->f & SELECT) numsel++;
-       }
-       
-       for(ev=em->verts.first;ev;ev=ev->next) {
-               ev->f1 = 0;   
-       } 
-       
-       //Make sure each edge only has 2 faces
-       // make sure loop doesn't cross face
-       for(efa=em->faces.first;efa;efa=efa->next) {
-               int ct = 0;
-               if(efa->e1->f & SELECT) {
-                       ct++;
-                       efa->e1->f1++;
-                       if(efa->e1->f1 > 2) {
-                               error("3+ face edge");
-                               return 0;                                
-                       }
-               }
-               if(efa->e2->f & SELECT) {
-                       ct++;
-                       efa->e2->f1++;
-                       if(efa->e2->f1 > 2) {
-                               error("3+ face edge");
-                               return 0;                                
-                       }
-               }
-               if(efa->e3->f & SELECT) {
-                       ct++;
-                       efa->e3->f1++;
-                       if(efa->e3->f1 > 2) {
-                               error("3+ face edge");
-                               return 0;                                
-                       }
-               }
-               if(efa->e4 && efa->e4->f & SELECT) {
-                       ct++;
-                       efa->e4->f1++;
-                       if(efa->e4->f1 > 2) {
-                               error("3+ face edge");
-                               return 0;                                
-                       }
-               }       
-               // Make sure loop is not 2 edges of same face   
-               if(ct > 1) {
-                  error("loop crosses itself");
-                  return 0;   
-               }
-       }          
-       // Get # of selected verts
-       for(ev=em->verts.first;ev;ev=ev->next) { 
-               if(ev->f & SELECT) vertsel++;
-       }       
-          
-       // Test for multiple segments
-       if(vertsel > numsel+1) {
-               error("Was not a single edge loop");
-               return 0;                  
-       }  
-       
-       // Get the edgeloop in order - mark f1 with SELECT once added
-       for(eed=em->edges.first;eed;eed=eed->next) {
-               if((eed->f & SELECT) && !(eed->f1 & SELECT)) {
-                       // If this is the first edge added, just put it in
-                       if(!edgelist) {
-                               BLI_linklist_prepend(&edgelist,eed);
-                               numadded++;
-                               first = eed;
-                               last  = eed; 
-                               eed->f1 = SELECT;
-                       } else {  
-                               if(editedge_getSharedVert(eed, last)) {
-                                       BLI_linklist_append(&edgelist,eed);
-                                       eed->f1 = SELECT;
-                                       numadded++;
-                                       last = eed;                                       
-                               }  else if(editedge_getSharedVert(eed, first)) {
-                                       BLI_linklist_prepend(&edgelist,eed);
-                                       eed->f1 = SELECT;
-                                       numadded++;
-                                       first = eed;                                      
-                               }   
-                       }
-               }   
-               if(eed->next == NULL && numadded != numsel) {
-                       eed=em->edges.first;    
-                       timesthrough++;
-               }
-               
-               // It looks like there was an unexpected case - Hopefully should not happen
-               if(timesthrough >= numsel*2) {
-                       BLI_linklist_free(edgelist,NULL); 
-                       error("could not order loop");
-                       return 0;   
-               }
-       }
-       
-       // Put the verts in order in a linklist
-       look = edgelist;
-       while(look) {
-               eed = look->link;
-               if(!vertlist) {
-                       if(look->next) {
-                               temp = look->next->link;
-                               //This is the first entry takes care of extra vert
-                               if(eed->v1 != temp->v1 && eed->v1 != temp->v2) {
-                                       BLI_linklist_append(&vertlist,eed->v1); 
-                                       eed->v1->f1 = 1; 
-                               } else {
-                                       BLI_linklist_append(&vertlist,eed->v2);  
-                                       eed->v2->f1 = 1; 
-                               }                        
-                       } else {
-                               //This is the case that we only have 1 edge
-                               BLI_linklist_append(&vertlist,eed->v1); 
-                               eed->v1->f1 = 1;                                        
-                       }
-               }               
-               // for all the entries
-               if(eed->v1->f1 != 1) {
-                       BLI_linklist_append(&vertlist,eed->v1); 
-                       eed->v1->f1 = 1;                   
-               } else  if(eed->v2->f1 != 1) {
-                       BLI_linklist_append(&vertlist,eed->v2); 
-                       eed->v2->f1 = 1;                                        
-               } 
-               look = look->next;   
-       }                
-       
-       // populate the SlideVerts
-       
-       vertgh = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp); 
-       look = vertlist;          
-       while(look) {
-               i=0;
-               j=0;
-               ev = look->link;
-               tempsv = (struct SlideVert*)MEM_mallocN(sizeof(struct SlideVert),"SlideVert");
-               tempsv->up = NULL;
-               tempsv->down = NULL;
-               tempsv->origvert.co[0] = ev->co[0];
-               tempsv->origvert.co[1] = ev->co[1];
-               tempsv->origvert.co[2] = ev->co[2];
-               tempsv->origvert.no[0] = ev->no[0];
-               tempsv->origvert.no[1] = ev->no[1];
-               tempsv->origvert.no[2] = ev->no[2];
-               // i is total edges that vert is on
-               // j is total selected edges that vert is on
-               
-               for(eed=em->edges.first;eed;eed=eed->next) {
-                       if(eed->v1 == ev || eed->v2 == ev) {
-                               i++;    
-                               if(eed->f & SELECT) {
-                                        j++;   
-                               }
-                       }               
-               }
-               // If the vert is in the middle of an edge loop, it touches 2 selected edges and 2 unselected edges
-               if(i == 4 && j == 2) {
-                       for(eed=em->edges.first;eed;eed=eed->next) {
-                               if(editedge_containsVert(eed, ev)) {
-                                       if(!(eed->f & SELECT)) {
-                                                if(!tempsv->up) {
-                                                        tempsv->up = eed;
-                                                } else if (!(tempsv->down)) {
-                                                        tempsv->down = eed;  
-                                                }
-                                       }
-                               }               
-                       }                       
-               }
-               // If it is on the end of the loop, it touches 1 selected and as least 2 more unselected
-               if(i >= 3 && j == 1) {
-                       for(eed=em->edges.first;eed;eed=eed->next) {
-                               if(editedge_containsVert(eed, ev) && eed->f & SELECT) {
-                                       for(efa = em->faces.first;efa;efa=efa->next) {
-                                               if(editface_containsEdge(efa, eed)) {
-                                                       if(editedge_containsVert(efa->e1, ev) && efa->e1 != eed) {
-                                                                if(!tempsv->up) {
-                                                                        tempsv->up = efa->e1;
-                                                                } else if (!(tempsv->down)) {
-                                                                        tempsv->down = efa->e1;  
-                                                                }                                                                 
-                                                       }
-                                                       if(editedge_containsVert(efa->e2, ev) && efa->e2 != eed) {
-                                                                if(!tempsv->up) {
-                                                                        tempsv->up = efa->e2;
-                                                                } else if (!(tempsv->down)) {
-                                                                        tempsv->down = efa->e2;  
-                                                                }                                                                 
-                                                       }                                                       
-                                                       if(editedge_containsVert(efa->e3, ev) && efa->e3 != eed) {
-                                                                if(!tempsv->up) {
-                                                                        tempsv->up = efa->e3;
-                                                                } else if (!(tempsv->down)) {
-                                                                        tempsv->down = efa->e3;  
-                                                                }                                                                 
-                                                       }  
-                                                       if(efa->e4) {
-                                                               if(editedge_containsVert(efa->e4, ev) && efa->e4 != eed) {
-                                                                        if(!tempsv->up) {
-                                                                                tempsv->up = efa->e4;
-                                                                        } else if (!(tempsv->down)) {
-                                                                                tempsv->down = efa->e4;  
-                                                                        }                                                                 
-                                                               }
-                                                       }                                                                                                                 
-                                                       
-                                               }
-                                       }
-                               }               
-                       }                       
-               }               
-               if(i > 4 && j == 2) {
-                       BLI_ghash_free(vertgh, NULL, (GHashValFreeFP)MEM_freeN);
-                       BLI_linklist_free(vertlist,NULL); 
-                       BLI_linklist_free(edgelist,NULL); 
-                       return 0;   
-               }
-               BLI_ghash_insert(vertgh,ev,tempsv);
-               
-               look = look->next;   
-       }                 
-    
-       // make sure the UPs nad DOWNs are 'faceloops'
-       // Also find the nearest slidevert to the cursor
-       getmouseco_areawin(mval);
-       look = vertlist;        
-       nearest = NULL;
-       vertdist = -1;  
-       while(look) {   
-               tempsv  = BLI_ghash_lookup(vertgh,(EditVert*)look->link);
-               
-               if(!tempsv->up || !tempsv->down) {
-                       error("Missing rails");
-                       BLI_ghash_free(vertgh, NULL, (GHashValFreeFP)MEM_freeN);
-                       BLI_linklist_free(vertlist,NULL); 
-                       BLI_linklist_free(edgelist,NULL); 
-                       return 0;
-               }
-               if(G.f & G_DRAW_EDGELEN) {
-                       if(!(tempsv->up->f & SELECT)) {
-                               tempsv->up->f |= SELECT;
-                               tempsv->up->f2 |= 16;
-                       } else {
-                               tempsv->up->f2 |= ~16;
-                       }
-                       if(!(tempsv->down->f & SELECT)) {
-                               tempsv->down->f |= SELECT;
-                               tempsv->down->f2 |= 16;
-                       } else {
-                               tempsv->down->f2 |= ~16;
-                       }
-               }
-               if(look->next != NULL) {
-                       SlideVert *sv;
-                       sv = BLI_ghash_lookup(vertgh,(EditVert*)look->next->link);
-                       if(sv) {
-                               float tempdist, co[2];
-                               if(!sharesFace(tempsv->up,sv->up)) {
-                                       EditEdge *swap;
-                                       swap = sv->up;
-                                       sv->up = sv->down;
-                                       sv->down = swap; 
-                               }
-                               view3d_project_float(curarea, tempsv->origvert.co, co, projectMat);
-                               
-                               tempdist = sqrt(pow(co[0] - mval[0],2)+pow(co[1]  - mval[1],2));
-                               if(vertdist < 0) {
-                                       vertdist = tempdist;
-                                       nearest  = (EditVert*)look->link;   
-                               } else if ( tempdist < vertdist ) {
-                                       vertdist = tempdist;
-                                       nearest  = (EditVert*)look->link;       
-                               }               
-                       }
-               }               
-               
-               
-               
-               look = look->next;   
-       }          
-       
-       
-       if (uvlay_tot && (G.scene->toolsettings->uvcalc_flag & UVCALC_TRANSFORM_CORRECT)) {
-               int maxnum = 0;
-               uvarray = MEM_callocN( uvlay_tot * sizeof(GHash *), "SlideUVs Array");
-               suv_last = slideuvs = MEM_callocN( uvlay_tot * (numadded+1) * sizeof(SlideUv), "SlideUVs"); /* uvLayers * verts */
-               suv = NULL;
-               
-               for (uvlay_idx=0; uvlay_idx<uvlay_tot; uvlay_idx++) {
-                       
-                       uvarray[uvlay_idx] = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp); 
-                       
-                       for(ev=em->verts.first;ev;ev=ev->next) {
-                               ev->tmp.l = 0;
-                       }
-                       look = vertlist;
-                       while(look) {
-                               float *uv_new;
-                               tempsv  = BLI_ghash_lookup(vertgh,(EditVert*)look->link);
-                               
-                               ev = look->link;
-                               suv = NULL;
-                               for(efa = em->faces.first;efa;efa=efa->next) {
-                                       if (ev->tmp.l != -1) { /* test for self, in this case its invalid */
-                                               int k=-1; /* face corner */
-                                       
-                                               /* Is this vert in the faces corner? */
-                                               if              (efa->v1==ev)                           k=0;
-                                               else if (efa->v2==ev)                           k=1;
-                                               else if (efa->v3==ev)                           k=2;
-                                               else if (efa->v4 && efa->v4==ev)        k=3;
-                                               
-                                               if (k != -1) {
-                                                       MTFace *tf = CustomData_em_get_n(&em->fdata, efa->data, CD_MTFACE, uvlay_idx);
-                                                       EditVert *ev_up, *ev_down;
-                                                       
-                                                       uv_new = tf->uv[k];
-                               
-                                                       if (ev->tmp.l) {
-                                                               if (fabs(suv->origuv[0]-uv_new[0]) > 0.0001 || fabs(suv->origuv[1]-uv_new[1])) {
-                                                                       ev->tmp.l = -1; /* Tag as invalid */
-                                                                       BLI_linklist_free(suv->fuv_list,NULL);
-                                                                       suv->fuv_list = NULL;
-                                                                       BLI_ghash_remove(uvarray[uvlay_idx],ev, NULL, NULL);
-                                                                       suv = NULL;
-                                                                       break;
-                                                               }
-                                                       } else {
-                                                               ev->tmp.l = 1;
-                                                               suv = suv_last;
-                                                               suv->fuv_list = NULL;
-                                                               suv->uv_up = suv->uv_down = NULL;
-                                                               suv->origuv[0] = uv_new[0];
-                                                               suv->origuv[1] = uv_new[1];
-                                                               
-                                                               BLI_linklist_prepend(&suv->fuv_list, uv_new);
-                                                               BLI_ghash_insert(uvarray[uvlay_idx],ev,suv);
-                                                               
-                                                               suv_last++; /* advance to next slide UV */
-                                                               maxnum++;
-                                                       }
-                               
-                                                       /* Now get the uvs along the up or down edge if we can */
-                                                       if (suv) {
-                                                               if (!suv->uv_up) {
-                                                                       ev_up = editedge_getOtherVert(tempsv->up,ev);
-                                                                       if              (efa->v1==ev_up)                                suv->uv_up = tf->uv[0];
-                                                                       else if (efa->v2==ev_up)                                suv->uv_up = tf->uv[1];
-                                                                       else if (efa->v3==ev_up)                                suv->uv_up = tf->uv[2];
-                                                                       else if (efa->v4 && efa->v4==ev_up)             suv->uv_up = tf->uv[3];
-                                                               }
-                                                               if (!suv->uv_down) { /* if the first face was apart of the up edge, it cant be apart of the down edge */
-                                                                       ev_down = editedge_getOtherVert(tempsv->down,ev);
-                                                                       if              (efa->v1==ev_down)                              suv->uv_down = tf->uv[0];
-                                                                       else if (efa->v2==ev_down)                              suv->uv_down = tf->uv[1];
-                                                                       else if (efa->v3==ev_down)                              suv->uv_down = tf->uv[2];
-                                                                       else if (efa->v4 && efa->v4==ev_down)   suv->uv_down = tf->uv[3];
-                                                               }
-                                       
-                                                               /* Copy the pointers to the face UV's */
-                                                               BLI_linklist_prepend(&suv->fuv_list, uv_new);
-                                                       }
-                                               }
-                                       }
-                               }
-                               look = look->next;
-                       }
-               } /* end uv layer loop */
-       } /* end uvlay_tot */
-       
-       
-       
-       // we should have enough info now to slide
-       len = 0.0f; 
-       
-       percp = -1;
-       while(draw) {
-                /* For the % calculation */   
-               short mval[2];   
-               float rc[2];
-               float v2[2], v3[2];
-               EditVert *centerVert, *upVert, *downVert;
-               
-               getmouseco_areawin(mval);  
-               
-               if (!immediate && (mval[0] == mvalo[0] && mval[1] ==  mvalo[1])) {
-                       PIL_sleep_ms(10);
-               } else {
-                       char *p = str;;
-                       mvalo[0] = mval[0];
-                       mvalo[1] = mval[1];
-                       
-                       tempsv = BLI_ghash_lookup(vertgh,nearest);
-                       centerVert = editedge_getSharedVert(tempsv->up, tempsv->down);
-                       upVert = editedge_getOtherVert(tempsv->up, centerVert);
-                       downVert = editedge_getOtherVert(tempsv->down, centerVert);
-                       view3d_project_float(curarea, upVert->co, v2, projectMat);
-                       view3d_project_float(curarea, downVert->co, v3, projectMat);
-                       /* Determine the % on which the loop should be cut */   
-                       rc[0]= v3[0]-v2[0];   
-                       rc[1]= v3[1]-v2[1];   
-                       len= rc[0]*rc[0]+ rc[1]*rc[1];
-                       if (len==0) {len = 0.0001;}
-                       if ((G.qual & LR_SHIFTKEY)==0) {
-                               wasshift = 0;
-                               labda= ( rc[0]*((mval[0]-v2[0])) + rc[1]*((mval[1]-v2[1])) )/len;   
-                       }
-                       else {
-                               if (wasshift==0) {
-                                       wasshift = 1;
-                                       shiftlabda = labda;
-                               }                                                       
-                               labda= ( rc[0]*((mval[0]-v2[0])) + rc[1]*((mval[1]-v2[1])) )/len / 10.0 + shiftlabda;                           
-                       }
-                       
-                       if(labda<=0.0) labda=0.0;   
-                       else if(labda>=1.0)labda=1.0;   
-                       perc=((1-labda)*2)-1;             
-                       
-                       if(G.qual == 0) {
-                               perc *= 100;
-                               perc = floor(perc);
-                               perc /= 100;
-                       } else if (G.qual == LR_CTRLKEY) {
-                               perc *= 10;
-                               perc = floor(perc);
-                               perc /= 10;                                
-                       }                       
-                       
-                       if(prop == 0) {
-                               len = len_v3v3(upVert->co,downVert->co)*((perc+1)/2);
-                               if(flip == 1) {
-                                       len = len_v3v3(upVert->co,downVert->co) - len;
-                               } 
-                       }
-                       
-                       if (hasNumInput(&num))
-                       {
-                               applyNumInput(&num, &perc);
-                               
-                               if (prop)
-                               {
-                                       perc = MIN2(perc, 1);
-                                       perc = MAX2(perc, -1);
-                               }
-                               else
-                               {
-                                       len = MIN2(perc, len_v3v3(upVert->co,downVert->co));
-                                       len = MAX2(len, 0);
-                               }
-                       }
-                       //Adjust Edgeloop
-                       if(immediate) {
-                               perc = imperc;   
-                       }
-                       percp = perc;
-                       if(prop) {
-                               look = vertlist;          
-                               while(look) { 
-                                       EditVert *tempev;
-                                       ev = look->link;
-                                       tempsv = BLI_ghash_lookup(vertgh,ev);
-                                       
-                                       tempev = editedge_getOtherVert((perc>=0)?tempsv->up:tempsv->down, ev);
-                                       interp_v3_v3v3(ev->co, tempsv->origvert.co, tempev->co, fabs(perc));
-                                       
-                                       if (G.scene->toolsettings->uvcalc_flag & UVCALC_TRANSFORM_CORRECT) {
-                                               for (uvlay_idx=0; uvlay_idx<uvlay_tot; uvlay_idx++) {
-                                                       suv = BLI_ghash_lookup( uvarray[uvlay_idx], ev );
-                                                       if (suv && suv->fuv_list && suv->uv_up && suv->uv_down) {
-                                                               interp_v2_v2v2(uv_tmp, suv->origuv,  (perc>=0)?suv->uv_up:suv->uv_down, fabs(perc));
-                                                               fuv_link = suv->fuv_list;
-                                                               while (fuv_link) {
-                                                                       copy_v2_v2(((float *)fuv_link->link), uv_tmp);
-                                                                       fuv_link = fuv_link->next;
-                                                               }
-                                                       }
-                                               }
-                                       }
-                                       
-                                       look = look->next;       
-                               }
-                       }
-                       else {
-                               //Non prop code  
-                               look = vertlist;          
-                               while(look) { 
-                                       float newlen;
-                                       ev = look->link;
-                                       tempsv = BLI_ghash_lookup(vertgh,ev);
-                                       newlen = (len / len_v3v3(editedge_getOtherVert(tempsv->up,ev)->co,editedge_getOtherVert(tempsv->down,ev)->co));
-                                       if(newlen > 1.0) {newlen = 1.0;}
-                                       if(newlen < 0.0) {newlen = 0.0;}
-                                       if(flip == 0) {
-                                               interp_v3_v3v3(ev->co, editedge_getOtherVert(tempsv->down,ev)->co, editedge_getOtherVert(tempsv->up,ev)->co, fabs(newlen));                                                                     
-                                               if (G.scene->toolsettings->uvcalc_flag & UVCALC_TRANSFORM_CORRECT) {
-                                                       /* dont do anything if no UVs */
-                                                       for (uvlay_idx=0; uvlay_idx<uvlay_tot; uvlay_idx++) {
-                                                               suv = BLI_ghash_lookup( uvarray[uvlay_idx], ev );
-                                                               if (suv && suv->fuv_list && suv->uv_up && suv->uv_down) {
-                                                                       interp_v2_v2v2(uv_tmp, suv->uv_down, suv->uv_up, fabs(newlen));
-                                                                       fuv_link = suv->fuv_list;
-                                                                       while (fuv_link) {
-                                                                               copy_v2_v2(((float *)fuv_link->link), uv_tmp);
-                                                                               fuv_link = fuv_link->next;
-                                                                       }
-                                                               }
-                                                       }
-                                               }
-                                       } else{
-                                               interp_v3_v3v3(ev->co, editedge_getOtherVert(tempsv->up,ev)->co, editedge_getOtherVert(tempsv->down,ev)->co, fabs(newlen));                             
-                                               
-                                               if (G.scene->toolsettings->uvcalc_flag & UVCALC_TRANSFORM_CORRECT) {
-                                                       /* dont do anything if no UVs */
-                                                       for (uvlay_idx=0; uvlay_idx<uvlay_tot; uvlay_idx++) {
-                                                               suv = BLI_ghash_lookup( uvarray[uvlay_idx], ev );
-                                                               if (suv && suv->fuv_list && suv->uv_up && suv->uv_down) {
-                                                                       interp_v2_v2v2(uv_tmp, suv->uv_up, suv->uv_down, fabs(newlen));
-                                                                       fuv_link = suv->fuv_list;
-                                                                       while (fuv_link) {
-                                                                               copy_v2_v2(((float *)fuv_link->link), uv_tmp);
-                                                                               fuv_link = fuv_link->next;
-                                                                       }
-                                                               }
-                                                       }
-                                               }
-                                       }
-                                       look = look->next;       
-                               }
-                       }
-                       
-                        // Highlight the Control Edges
-                       scrarea_do_windraw(curarea);   
-                       persp(PERSP_VIEW);   
-                       glPushMatrix();   
-                       mymultmatrix(G.obedit->obmat);
-                       glColor3ub(0, 255, 0);   
-                       glBegin(GL_LINES);
-                       glVertex3fv(upVert->co);
-                       glVertex3fv(downVert->co);
-                       glEnd(); 
-                       
-                       if(prop == 0) {
-                               // draw start edge for non-prop
-                               glPointSize(5);
-                               glBegin(GL_POINTS);
-                               glColor3ub(255,0,255);
-                               if(flip) {
-                                       glVertex3fv(upVert->co);
-                               } else {
-                                       glVertex3fv(downVert->co);                                      
-                               }
-                               glEnd();        
-                       }
-                       
-                       
-                       glPopMatrix();           
-                       if(prop) {
-                               p += sprintf(str, "(P)ercentage: ");
-                       } else {
-                               p += sprintf(str, "Non (P)rop Length: ");
-                       }
-                       
-                       if (hasNumInput(&num))
-                       {
-                               char num_str[20];
-                               
-                               outputNumInput(&num, num_str);
-                               p += sprintf(p, "%s", num_str);
-                       }
-                       else
-                       {
-                               if (prop)
-                               {
-                                       p += sprintf(p, "%f", perc);
-                               }
-                               else
-                               {
-                                       p += sprintf(p, "%f", len);
-                               }
-                       }
-                       
-                       
-                       if (prop == 0) {
-                               p += sprintf(p, ", Press (F) to flip control side");
-                       }
-                       headerprint(str);
-                       screen_swapbuffers();                   
-               }
-               if(!immediate) {
-                       while(qtest()) {
-                               short val=0;                    
-                               event= extern_qread(&val);      // extern_qread stores important events for the mainloop to handle 
-                                       
-                               /* val==0 on key-release event */
-                               if (val) {
-                                       if(ELEM(event, ESCKEY, RIGHTMOUSE)) {
-                                                       prop = 1; // Go back to prop mode
-                                                       imperc = 0; // This is the % that gets set for immediate
-                                                       immediate = 1; //Run through eval code 1 more time
-                                                       cancel = 1;   // Return -1
-                                                       mvalo[0] = -1;
-                                       } else if(ELEM3(event, PADENTER, LEFTMOUSE, RETKEY)) {
-                                                       draw = 0; // End looping now
-                                       } else if(event==MIDDLEMOUSE) {
-                                                       perc = 0;  
-                                                       immediate = 1;
-                                       } else if(event==PKEY) {
-                                                       initNumInput(&num); /* reset num input */
-                                                       if (prop) {
-                                                               prop = 0;
-                                                               num.flag |= NUM_NO_NEGATIVE;
-                                                       }
-                                                       else {
-                                                               prop = 1;
-                                                       }
-                                                       mvalo[0] = -1;  
-                                       } else if(event==FKEY) {
-                                                       (flip == 1) ? (flip = 0):(flip = 1); 
-                                                       mvalo[0] = -1; 
-                                       } else if(ELEM(event, RIGHTARROWKEY, WHEELUPMOUSE)) { // Scroll through Control Edges
-                                               look = vertlist;        
-                                               while(look) {   
-                                                       if(nearest == (EditVert*)look->link) {
-                                                               if(look->next == NULL) {
-                                                                       nearest =  (EditVert*)vertlist->link;  
-                                                               } else {
-                                                                       nearest = (EditVert*)look->next->link;
-                                                               }        
-                                                               mvalo[0] = -1;
-                                                               break;                          
-                                                       }
-                                                       look = look->next;   
-                                               }         
-                                       } else if(ELEM(event, LEFTARROWKEY, WHEELDOWNMOUSE)) { // Scroll through Control Edges
-                                               look = vertlist;        
-                                               while(look) {   
-                                                       if(look->next) {
-                                                               if(look->next->link == nearest) {
-                                                                       nearest = (EditVert*)look->link;
-                                                                       mvalo[0] = -1;
-                                                                       break;
-                                                               }         
-                                                       } else {
-                                                               if((EditVert*)vertlist->link == nearest) {
-                                                                       nearest = look->link;
-                                                                       mvalo[0] = -1;
-                                                                       break;                                                   
-                                                               }          
-                                                       }   
-                                                       look = look->next;   
-                                               }         
-                                       }
-                                       
-                                       if (handleNumInput(&num, event))
-                                       {
-                                               mvalo[0] = -1; /* NEED A BETTER WAY TO TRIGGER REDRAW */
-                                       }
-                               }
-                               
-                       } 
-               } else {
-                       draw = 0;
-               }
-               DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);      
-       }
-       
-       
-       if(G.f & G_DRAW_EDGELEN) {
-               look = vertlist;
-               while(look) {   
-                       tempsv  = BLI_ghash_lookup(vertgh,(EditVert*)look->link);
-                       if(tempsv != NULL) {
-                               tempsv->up->f &= !SELECT;
-                               tempsv->down->f &= !SELECT;
-                       }
-                       look = look->next;
-               }
-       }
-       
-       force_draw(0);
-       
-       if(!immediate)
-               EM_automerge(0);
-       DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
-       scrarea_queue_winredraw(curarea);                
-       
-       //BLI_ghash_free(edgesgh, freeGHash, NULL); 
-       BLI_ghash_free(vertgh, NULL, (GHashValFreeFP)MEM_freeN);
-       BLI_linklist_free(vertlist,NULL); 
-       BLI_linklist_free(edgelist,NULL); 
-       
-       if (uvlay_tot && (G.scene->toolsettings->uvcalc_flag & UVCALC_TRANSFORM_CORRECT)) {
-               for (uvlay_idx=0; uvlay_idx<uvlay_tot; uvlay_idx++) {
-                       BLI_ghash_free(uvarray[uvlay_idx], NULL, NULL);
-               }
-               MEM_freeN(uvarray);
-               MEM_freeN(slideuvs);
-               
-               suv = suv_last-1;
-               while (suv >= slideuvs) {
-                       if (suv->fuv_list) {
-                               BLI_linklist_free(suv->fuv_list,NULL);
-                       }
-                       suv--;
-               }
-               
-               allqueue(REDRAWIMAGE, 0);
-       }
-       if(cancel == 1) {
-               return -1;
-       }
-       else {
- #ifdef WITH_VERSE
-       if(G.editMesh->vnode) {
-               sync_all_verseverts_with_editverts((VNode*)G.editMesh->vnode);
-               sync_all_versefaces_with_editfaces((VNode*)G.editMesh->vnode);
-       }
- #endif
-       }
-       return 1;
- }
 +/* -------------------- More tools ------------------ */
 +
 +void mesh_set_face_flags(short mode)
 +{
 +      EditMesh *em = G.editMesh;
 +      EditFace *efa;
 +      MTFace *tface;
 +      short   m_tex=0, m_tiles=0, m_shared=0,
 +                      m_light=0, m_invis=0, m_collision=0,
 +                      m_twoside=0, m_obcolor=0, m_halo=0,
 +                      m_billboard=0, m_shadow=0, m_text=0,
 +                      m_sort=0;
 +      short flag = 0, change = 0;
 +      
 +      if (!EM_texFaceCheck()) {
 +              error("not a mesh with uv/image layers");
 +              return;
 +      }
 +      
 +      add_numbut(0, TOG|SHO, "Texture", 0, 0, &m_tex, NULL);
 +      add_numbut(1, TOG|SHO, "Tiles", 0, 0, &m_tiles, NULL);
 +      add_numbut(2, TOG|SHO, "Light", 0, 0, &m_light, NULL);
 +      add_numbut(3, TOG|SHO, "Invisible", 0, 0, &m_invis, NULL);
 +      add_numbut(4, TOG|SHO, "Collision", 0, 0, &m_collision, NULL);
 +      add_numbut(5, TOG|SHO, "Shared", 0, 0, &m_shared, NULL);
 +      add_numbut(6, TOG|SHO, "Twoside", 0, 0, &m_twoside, NULL);
 +      add_numbut(7, TOG|SHO, "ObColor", 0, 0, &m_obcolor, NULL);
 +      add_numbut(8, TOG|SHO, "Halo", 0, 0, &m_halo, NULL);
 +      add_numbut(9, TOG|SHO, "Billboard", 0, 0, &m_billboard, NULL);
 +      add_numbut(10, TOG|SHO, "Shadow", 0, 0, &m_shadow, NULL);
 +      add_numbut(11, TOG|SHO, "Text", 0, 0, &m_text, NULL);
 +      add_numbut(12, TOG|SHO, "Sort", 0, 0, &m_sort, NULL);
 +      
 +      if (!do_clever_numbuts((mode ? "Set Flags" : "Clear Flags"), 13, REDRAW))
 +              return;
 +      
 +      /* these 2 cant both be on */
 +      if (mode) /* are we seeting*/
 +              if (m_halo)
 +                      m_billboard = 0;
 +      
 +      if (m_tex)                      flag |= TF_TEX;
 +      if (m_tiles)            flag |= TF_TILES;
 +      if (m_shared)           flag |= TF_SHAREDCOL;
 +      if (m_light)            flag |= TF_LIGHT;
 +      if (m_invis)            flag |= TF_INVISIBLE;
 +      if (m_collision)        flag |= TF_DYNAMIC;
 +      if (m_twoside)          flag |= TF_TWOSIDE;
 +      if (m_obcolor)          flag |= TF_OBCOL;
 +      if (m_halo)                     flag |= TF_BILLBOARD;
 +      if (m_billboard)        flag |= TF_BILLBOARD2;
 +      if (m_shadow)           flag |= TF_SHADOW;
 +      if (m_text)                     flag |= TF_BMFONT;
 +      if (m_sort)                     flag |= TF_ALPHASORT;
 +      
 +      if (flag==0)
 +              return;
 +      
 +      efa= em->faces.first;
 +      while(efa) {
 +              if(efa->f & SELECT) {
 +                      tface= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
 +                      if (mode)       tface->mode |= flag;
 +                      else            tface->mode &= ~flag;
 +                      change = 1;
 +              }
 +              efa= efa->next;
 +      }
 +      
 +      if (change) {
 +              BIF_undo_push((mode ? "Set Flags" : "Clear Flags"));
 +              
 +              allqueue(REDRAWIMAGE, 0);
 +              allqueue(REDRAWVIEW3D, 0);
 +      }
 +}
 +
 +void mesh_set_smooth_faces(short event)
 +{
 +      EditMesh *em = G.editMesh;
 +      EditFace *efa;
 +
 +      if(G.obedit==0) return;
 +      
 +      if(G.obedit->type != OB_MESH) return;
 +      
 +      efa= em->faces.first;
 +      while(efa) {
 +              if(efa->f & SELECT) {
 +                      if(event==1) efa->flag |= ME_SMOOTH;
 +                      else if(event==0) efa->flag &= ~ME_SMOOTH;
 +              }
 +              efa= efa->next;
 +      }
 +      
 +      DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
 +      allqueue(REDRAWVIEW3D, 0);
 +      
 +      if(event==1) BIF_undo_push("Set Smooth");
 +      else if(event==0) BIF_undo_push("Set Solid");
 +}
 +
 +/* helper to find edge for edge_rip */
 +static float mesh_rip_edgedist(float mat[][4], float *co1, float *co2, short *mval)
 +{
 +      float vec1[3], vec2[3], mvalf[2];
 +      
 +      view3d_project_float(curarea, co1, vec1, mat);
 +      view3d_project_float(curarea, co2, vec2, mat);
 +      mvalf[0]= (float)mval[0];
 +      mvalf[1]= (float)mval[1];
 +      
 +      return dist_to_line_segment_v2(mvalf, vec1, vec2);
 +}
 +
 +/* helper for below */
 +static void mesh_rip_setface(EditFace *sefa)
 +{
        /* put new vertices & edges in best face */
        if(sefa->v1->tmp.v) sefa->v1= sefa->v1->tmp.v;
        if(sefa->v2->tmp.v) sefa->v2= sefa->v2->tmp.v;
index c200a12,0000000..baee3fc
mode 100644,000000..100644
--- /dev/null
@@@ -1,4890 -1,0 +1,4890 @@@
-       if(RNA_property_is_set(op->ptr, "filepath"))
 +/*
 + * ***** BEGIN GPL LICENSE BLOCK *****
 + *
 + * This program is free software; you can redistribute it and/or
 + * modify it under the terms of the GNU General Public License
 + * as published by the Free Software Foundation; either version 2
 + * of the License, or (at your option) any later version.
 + *
 + * This program is distributed in the hope that it will be useful,
 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 + * GNU General Public License for more details.
 + *
 + * You should have received a copy of the GNU General Public License
 + * along with this program; if not, write to the Free Software Foundation,
 + * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 + *
 + * The Original Code is Copyright (C) 2004 by Blender Foundation.
 + * All rights reserved.
 + *
 + * The Original Code is: all of this file.
 + *
 + * Contributor(s): Joseph Eagar
 + *
 + * ***** END GPL LICENSE BLOCK *****
 + */
 +#include <stdlib.h>
 +#include <stdarg.h>
 +#include <string.h>
 +#include <math.h>
 +#include <float.h>
 +
 +#include "MEM_guardedalloc.h"
 +#include "PIL_time.h"
 +
 +#include "BLO_sys_types.h" // for intptr_t support
 +
 +#include "DNA_mesh_types.h"
 +#include "DNA_material_types.h"
 +#include "DNA_meshdata_types.h"
 +#include "DNA_modifier_types.h"
 +#include "DNA_object_types.h"
 +#include "DNA_scene_types.h"
 +#include "DNA_screen_types.h"
 +#include "DNA_view3d_types.h"
 +#include "DNA_key_types.h"
 +
 +#include "RNA_types.h"
 +#include "RNA_define.h"
 +#include "RNA_access.h"
 +
 +#include "BLI_utildefines.h"
 +#include "BLI_blenlib.h"
 +#include "BLI_math.h"
 +#include "BLI_editVert.h"
 +#include "BLI_rand.h"
 +#include "BLI_ghash.h"
 +#include "BLI_linklist.h"
 +#include "BLI_heap.h"
 +#include "BLI_array.h"
 +#include "BLI_smallhash.h"
 +
 +#include "BKE_material.h"
 +#include "BKE_context.h"
 +#include "BKE_customdata.h"
 +#include "BKE_DerivedMesh.h"
 +#include "BKE_cdderivedmesh.h"
 +#include "BKE_depsgraph.h"
 +#include "BKE_global.h"
 +#include "BKE_library.h"
 +#include "BKE_mesh.h"
 +#include "BKE_object.h"
 +#include "BKE_bmesh.h"
 +#include "BKE_report.h"
 +#include "BKE_tessmesh.h"
 +#include "BKE_texture.h"
 +#include "BKE_main.h"
 +
 +#include "BIF_gl.h"
 +#include "BIF_glutil.h"
 +
 +#include "WM_api.h"
 +#include "WM_types.h"
 +
 +#include "ED_mesh.h"
 +#include "ED_view3d.h"
 +#include "ED_util.h"
 +#include "ED_screen.h"
 +#include "ED_transform.h"
 +#include "ED_object.h"
 +
 +#include "UI_interface.h"
 +
 +#include "RE_render_ext.h"
 +
 +#include "mesh_intern.h"
 +#include "bmesh.h"
 +
 +#include "editbmesh_bvh.h"
 +
 +static void add_normal_aligned(float nor[3], const float add[3])
 +{
 +      if(dot_v3v3(nor, add) < -0.9999f)
 +              sub_v3_v3(nor, add);
 +      else
 +              sub_v3_v3(nor, add);
 +}
 +
 +
 +static int subdivide_exec(bContext *C, wmOperator *op)
 +{
 +      ToolSettings *ts = CTX_data_tool_settings(C);
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      int cuts= RNA_int_get(op->ptr,"number_cuts");
 +      float smooth= 0.292f*RNA_float_get(op->ptr, "smoothness");
 +      float fractal= RNA_float_get(op->ptr, "fractal")/2.5;
 +      int flag= 0;
 +
 +      if(smooth != 0.0f)
 +              flag |= B_SMOOTH;
 +      if(fractal != 0.0f)
 +              flag |= B_FRACTAL;
 +      
 +      if (RNA_boolean_get(op->ptr, "quadtri") && 
 +          RNA_enum_get(op->ptr, "quadcorner") == SUBD_STRAIGHT_CUT)
 +      {
 +              RNA_enum_set(op->ptr, "quadcorner", SUBD_INNERVERT);    
 +      }
 +      
 +      BM_esubdivideflag(obedit, em->bm, BM_SELECT, 
 +                        smooth, fractal,
 +                        ts->editbutflag|flag, 
 +                        cuts, 0, RNA_enum_get(op->ptr, "quadcorner"), 
 +                        RNA_boolean_get(op->ptr, "quadtri"),
 +                        1, RNA_int_get(op->ptr, "seed"));
 +
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +/* Note, these values must match delete_mesh() event values */
 +static EnumPropertyItem prop_mesh_cornervert_types[] = {
 +      {SUBD_INNERVERT,     "INNERVERT", 0,      "Inner Vert", ""},
 +      {SUBD_PATH,          "PATH", 0,           "Path", ""},
 +      {SUBD_STRAIGHT_CUT,  "STRAIGHT_CUT", 0,   "Straight Cut", ""},
 +      {SUBD_FAN,           "FAN", 0,            "Fan", ""},
 +      {0, NULL, 0, NULL, NULL}
 +};
 +
 +void MESH_OT_subdivide(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Subdivide";
 +      ot->description= "Subdivide selected edges";
 +      ot->idname= "MESH_OT_subdivide";
 +
 +      /* api callbacks */
 +      ot->exec= subdivide_exec;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      /* properties */
 +      RNA_def_int(ot->srna, "number_cuts", 1, 1, INT_MAX, "Number of Cuts", "", 1, 10);
 +      /* BMESH_TODO, this currently does nothing, just add to stop UI from erroring out! */
 +      RNA_def_float(ot->srna, "smoothness", 0.0f, 0.0f, FLT_MAX, "Smoothness", "Smoothness factor (BMESH TODO)", 0.0f, 1.0f);
 +
 +      RNA_def_boolean(ot->srna, "quadtri", 0, "Quad/Tri Mode", "Tries to prevent ngons");
 +      RNA_def_enum(ot->srna, "quadcorner", prop_mesh_cornervert_types, SUBD_STRAIGHT_CUT, "Quad Corner Type", "How to subdivide quad corners (anything other then Straight Cut will prevent ngons)");
 +
 +      RNA_def_float(ot->srna, "fractal", 0.0f, 0.0f, FLT_MAX, "Fractal", "Fractal randomness factor", 0.0f, 1000.0f);
 +      RNA_def_int(ot->srna, "seed", 0, 0, 10000, "Random Seed", "Seed for the random number generator", 0, 50);
 +}
 +
 +
 +void EMBM_project_snap_verts(bContext *C, ARegion *ar, Object *obedit, BMEditMesh *em)
 +{
 +      BMIter iter;
 +      BMVert *eve;
 +
 +      BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
 +              if (BM_TestHFlag(eve, BM_SELECT)) {
 +                      float mval[2], vec[3], no_dummy[3];
 +                      int dist_dummy;
 +                      mul_v3_m4v3(vec, obedit->obmat, eve->co);
 +                      project_float_noclip(ar, vec, mval);
 +                      if(snapObjectsContext(C, mval, &dist_dummy, vec, no_dummy, SNAP_NOT_OBEDIT)) {
 +                              mul_v3_m4v3(eve->co, obedit->imat, vec);
 +                      }
 +              }
 +      }
 +}
 +
 +
 +/* individual face extrude */
 +/* will use vertex normals for extrusion directions, so *nor is unaffected */
 +static short EDBM_Extrude_face_indiv(BMEditMesh *em, wmOperator *op, const char hflag, float *UNUSED(nor))
 +{
 +      BMOIter siter;
 +      BMIter liter;
 +      BMFace *f;
 +      BMLoop *l;
 +      BMOperator bmop;
 +
 +      EDBM_InitOpf(em, &bmop, op, "extrude_face_indiv faces=%hf", hflag);
 +
 +      /*deselect original verts*/
 +      EDBM_clear_flag_all(em, BM_SELECT);
 +
 +      BMO_Exec_Op(em->bm, &bmop);
 +      
 +      BMO_ITER(f, &siter, em->bm, &bmop, "faceout", BM_FACE) {
 +              BM_Select(em->bm, f, TRUE);
 +
 +              /*set face vertex normals to face normal*/
 +              BM_ITER(l, &liter, em->bm, BM_LOOPS_OF_FACE, f) {
 +                      copy_v3_v3(l->v->no, f->no);
 +              }
 +      }
 +
 +      if (!EDBM_FinishOp(em, &bmop, op, 1)) return 0;
 +
 +      return 's'; // s is shrink/fatten
 +}
 +
 +/* extrudes individual edges */
 +static short EDBM_Extrude_edges_indiv(BMEditMesh *em, wmOperator *op, const char hflag, float *UNUSED(nor))
 +{
 +      BMOperator bmop;
 +
 +      EDBM_InitOpf(em, &bmop, op, "extrude_edge_only edges=%he", hflag);
 +
 +      /*deselect original verts*/
 +      EDBM_clear_flag_all(em, BM_SELECT);
 +
 +      BMO_Exec_Op(em->bm, &bmop);
 +      BMO_HeaderFlag_Buffer(em->bm, &bmop, "geomout", BM_SELECT, BM_VERT|BM_EDGE);
 +
 +      if (!EDBM_FinishOp(em, &bmop, op, 1)) return 0;
 +
 +      return 'n'; // n is normal grab
 +}
 +
 +/* extrudes individual vertices */
 +static short EDBM_Extrude_verts_indiv(BMEditMesh *em, wmOperator *op, const char hflag, float *UNUSED(nor))
 +{
 +      BMOperator bmop;
 +
 +      EDBM_InitOpf(em, &bmop, op, "extrude_vert_indiv verts=%hv", hflag);
 +
 +      /*deselect original verts*/
 +      BMO_UnHeaderFlag_Buffer(em->bm, &bmop, "verts", BM_SELECT, BM_VERT);
 +
 +      BMO_Exec_Op(em->bm, &bmop);
 +      BMO_HeaderFlag_Buffer(em->bm, &bmop, "vertout", BM_SELECT, BM_VERT);
 +
 +      if (!EDBM_FinishOp(em, &bmop, op, 1)) return 0;
 +
 +      return 'g'; // g is grab
 +}
 +
 +static short EDBM_Extrude_edge(Object *obedit, BMEditMesh *em, const char hflag, float *nor)
 +{
 +      BMesh *bm = em->bm;
 +      BMIter iter;
 +      BMOIter siter;
 +      BMOperator extop;
 +      BMEdge *edge;
 +      BMFace *f;
 +      ModifierData *md;
 +      BMHeader *el;
 +      
 +      BMO_Init_Op(bm, &extop, "extrudefaceregion");
 +      BMO_HeaderFlag_To_Slot(bm, &extop, "edgefacein",
 +                             hflag, BM_VERT|BM_EDGE|BM_FACE);
 +
 +      /* If a mirror modifier with clipping is on, we need to adjust some 
 +       * of the cases above to handle edges on the line of symmetry.
 +       */
 +      md = obedit->modifiers.first;
 +      for (; md; md=md->next) {
 +              if ((md->type==eModifierType_Mirror) && (md->mode & eModifierMode_Realtime)) {
 +                      MirrorModifierData *mmd = (MirrorModifierData*) md;     
 +              
 +                      if(mmd->flag & MOD_MIR_CLIPPING) {
 +                              float mtx[4][4];
 +                              if (mmd->mirror_ob) {
 +                                      float imtx[4][4];
 +                                      invert_m4_m4(imtx, mmd->mirror_ob->obmat);
 +                                      mult_m4_m4m4(mtx, imtx, obedit->obmat);
 +                              }
 +
 +                              for (edge=BMIter_New(&iter,bm,BM_EDGES_OF_MESH,NULL);
 +                                   edge; edge=BMIter_Step(&iter))
 +                              {
 +                                      if (BM_TestHFlag(edge, hflag)) {
 +                                              float co1[3], co2[3];
 +
 +                                              copy_v3_v3(co1, edge->v1->co);
 +                                              copy_v3_v3(co2, edge->v2->co);
 +
 +                                              if (mmd->mirror_ob) {
 +                                                      mul_v3_m4v3(co1, mtx, co1);
 +                                                      mul_v3_m4v3(co2, mtx, co2);
 +                                              }
 +
 +                                              if (mmd->flag & MOD_MIR_AXIS_X)
 +                                                      if ( (fabs(co1[0]) < mmd->tolerance) &&
 +                                                               (fabs(co2[0]) < mmd->tolerance) )
 +                                                              BMO_Insert_MapPointer(bm, &extop, "exclude", edge, NULL);
 +
 +                                              if (mmd->flag & MOD_MIR_AXIS_Y)
 +                                                      if ( (fabs(co1[1]) < mmd->tolerance) &&
 +                                                               (fabs(co2[1]) < mmd->tolerance) )
 +                                                              BMO_Insert_MapPointer(bm, &extop, "exclude", edge, NULL);
 +
 +                                              if (mmd->flag & MOD_MIR_AXIS_Z)
 +                                                      if ( (fabs(co1[2]) < mmd->tolerance) &&
 +                                                               (fabs(co2[2]) < mmd->tolerance) )
 +                                                              BMO_Insert_MapPointer(bm, &extop, "exclude", edge, NULL);
 +                                      }
 +                              }
 +                      }
 +              }
 +      }
 +
 +      EDBM_clear_flag_all(em, BM_SELECT);
 +
 +      BMO_Exec_Op(bm, &extop);
 +
 +      nor[0] = nor[1] = nor[2] = 0.0f;
 +      
 +      BMO_ITER(el, &siter, bm, &extop, "geomout", BM_ALL) {
 +              BM_Select(bm, el, TRUE);
 +
 +              if (el->htype == BM_FACE) {
 +                      f = (BMFace*)el;
 +                      add_normal_aligned(nor, f->no);
 +              };
 +      }
 +
 +      normalize_v3(nor);
 +
 +      BMO_Finish_Op(bm, &extop);
 +
 +      if(nor[0]==0.0 && nor[1]==0.0 && nor[2]==0.0) return 'g'; // grab
 +      return 'n'; // normal constraint 
 +
 +}
 +static short EDBM_Extrude_vert(Object *obedit, BMEditMesh *em, const char hflag, float *nor)
 +{
 +      BMIter iter;
 +      BMEdge *eed;
 +              
 +      /*ensure vert flags are consistent for edge selections*/
 +      eed = BMIter_New(&iter, em->bm, BM_EDGES_OF_MESH, NULL);
 +      for ( ; eed; eed=BMIter_Step(&iter)) {
 +              if (BM_TestHFlag(eed, hflag)) {
 +                      if (hflag & BM_SELECT) {
 +                              BM_Select(em->bm, eed->v1, TRUE);
 +                              BM_Select(em->bm, eed->v2, TRUE);
 +                      }
 +
 +                      BM_SetHFlag(eed->v1, hflag & ~BM_SELECT);
 +                      BM_SetHFlag(eed->v2, hflag & ~BM_SELECT);
 +              } else {
 +                      if (BM_TestHFlag(eed->v1, hflag) && BM_TestHFlag(eed->v2, hflag)) {
 +                              if (hflag & BM_SELECT) {
 +                                      BM_Select(em->bm, eed, TRUE);
 +                              }
 +
 +                              BM_SetHFlag(eed, hflag & ~BM_SELECT);
 +                      }
 +              }
 +      }
 +
 +      return EDBM_Extrude_edge(obedit, em, hflag, nor);
 +}
 +
 +static int extrude_repeat_mesh(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em = ((Mesh *)obedit->data)->edit_btmesh;
 +      RegionView3D *rv3d = CTX_wm_region_view3d(C);           
 +              
 +      int steps = RNA_int_get(op->ptr,"steps");
 +      
 +      float offs = RNA_float_get(op->ptr,"offset");
 +      float dvec[3], tmat[3][3], bmat[3][3], nor[3]= {0.0, 0.0, 0.0};
 +      short a;
 +
 +      /* dvec */
 +      dvec[0]= rv3d->persinv[2][0];
 +      dvec[1]= rv3d->persinv[2][1];
 +      dvec[2]= rv3d->persinv[2][2];
 +      normalize_v3(dvec);
 +      dvec[0]*= offs;
 +      dvec[1]*= offs;
 +      dvec[2]*= offs;
 +
 +      /* base correction */
 +      copy_m3_m4(bmat, obedit->obmat);
 +      invert_m3_m3(tmat, bmat);
 +      mul_m3_v3(tmat, dvec);
 +
 +      for(a=0; a<steps; a++) {
 +              EDBM_Extrude_edge(obedit, em, BM_SELECT, nor);
 +              //BMO_CallOpf(em->bm, "extrudefaceregion edgefacein=%hef", BM_SELECT);
 +              BMO_CallOpf(em->bm, "translate vec=%v verts=%hv", (float*)dvec, BM_SELECT);
 +              //extrudeflag(obedit, em, SELECT, nor);
 +              //translateflag(em, SELECT, dvec);
 +      }
 +      
 +      EDBM_RecalcNormals(em);
 +
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_extrude_repeat(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Extrude Repeat Mesh";
 +      ot->description= "Extrude selected vertices, edges or faces repeatedly";
 +      ot->idname= "MESH_OT_extrude_repeat";
 +      
 +      /* api callbacks */
 +      ot->exec= extrude_repeat_mesh;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +      
 +      /* props */
 +      RNA_def_float(ot->srna, "offset", 2.0f, 0.0f, 100.0f, "Offset", "", 0.0f, FLT_MAX);
 +      RNA_def_int(ot->srna, "steps", 10, 0, 180, "Steps", "", 0, INT_MAX);
 +}
 +
 +/* generic extern called extruder */
 +static int EDBM_Extrude_Mesh(Scene *scene, Object *obedit, BMEditMesh *em, wmOperator *op, float *norin)
 +{
 +      short nr, transmode= 0;
 +      float stacknor[3] = {0.0f, 0.0f, 0.0f};
 +      float *nor = norin ? norin : stacknor;
 +
 +      nor[0] = nor[1] = nor[2] = 0.0f;
 +
 +      if(em->selectmode & SCE_SELECT_VERTEX) {
 +              if(em->bm->totvertsel==0) nr= 0;
 +              else if(em->bm->totvertsel==1) nr= 4;
 +              else if(em->bm->totedgesel==0) nr= 4;
 +              else if(em->bm->totfacesel==0) 
 +                      nr= 3; // pupmenu("Extrude %t|Only Edges%x3|Only Vertices%x4");
 +              else if(em->bm->totfacesel==1)
 +                      nr= 1; // pupmenu("Extrude %t|Region %x1|Only Edges%x3|Only Vertices%x4");
 +              else 
 +                      nr= 1; // pupmenu("Extrude %t|Region %x1||Individual Faces %x2|Only Edges%x3|Only Vertices%x4");
 +      }
 +      else if(em->selectmode & SCE_SELECT_EDGE) {
 +              if (em->bm->totedgesel==0) nr = 0;
 +              
 +              nr = 1;
 +              /*else if (em->totedgesel==1) nr = 3;
 +              else if(em->totfacesel==0) nr = 3;
 +              else if(em->totfacesel==1)
 +                      nr= 1; // pupmenu("Extrude %t|Region %x1|Only Edges%x3");
 +              else
 +                      nr= 1; // pupmenu("Extrude %t|Region %x1||Individual Faces %x2|Only Edges%x3");
 +              */
 +      }
 +      else {
 +              if (em->bm->totfacesel == 0) nr = 0;
 +              else if (em->bm->totfacesel == 1) nr = 1;
 +              else
 +                      nr= 1; // pupmenu("Extrude %t|Region %x1||Individual Faces %x2");
 +      }
 +
 +      if(nr<1) return 'g';
 +
 +      if(nr==1 && em->selectmode & SCE_SELECT_VERTEX) 
 +              transmode= EDBM_Extrude_vert(obedit, em, BM_SELECT, nor);
 +      else if (nr == 1) transmode= EDBM_Extrude_edge(obedit, em, BM_SELECT, nor);
 +      else if(nr==4) transmode= EDBM_Extrude_verts_indiv(em, op, BM_SELECT, nor);
 +      else if(nr==3) transmode= EDBM_Extrude_edges_indiv(em, op, BM_SELECT, nor);
 +      else transmode= EDBM_Extrude_face_indiv(em, op, BM_SELECT, nor);
 +      
 +      if(transmode==0) {
 +              BKE_report(op->reports, RPT_ERROR, "Not a valid selection for extrude");
 +      }
 +      else {
 +              
 +                      /* We need to force immediate calculation here because 
 +                      * transform may use derived objects (which are now stale).
 +                      *
 +                      * This shouldn't be necessary, derived queries should be
 +                      * automatically building this data if invalid. Or something.
 +                      */
 +//            DAG_object_flush_update(scene, obedit, OB_RECALC_DATA); 
 +              object_handle_update(scene, obedit);
 +
 +              /* individual faces? */
 +//            BIF_TransformSetUndo("Extrude");
 +              if(nr==2) {
 +//                    initTransform(TFM_SHRINKFATTEN, CTX_NO_PET|CTX_NO_MIRROR);
 +//                    Transform();
 +              }
 +              else {
 +//                    initTransform(TFM_TRANSLATION, CTX_NO_PET|CTX_NO_MIRROR);
 +                      if(transmode=='n') {
 +                              mul_m4_v3(obedit->obmat, nor);
 +                              sub_v3_v3v3(nor, nor, obedit->obmat[3]);
 +//                            BIF_setSingleAxisConstraint(nor, "along normal");
 +                      }
 +//                    Transform();
 +              }
 +      }
 +      
 +      return transmode;
 +}
 +
 +/* extrude without transform */
 +static int mesh_extrude_region_exec(bContext *C, wmOperator *op)
 +{
 +      Scene *scene = CTX_data_scene(C);
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh*)obedit->data)->edit_btmesh;
 +      
 +      EDBM_Extrude_Mesh(scene, obedit, em, op, NULL);
 +
 +      /*This normally happens when pushing undo but modal operators
 +        like this one don't push undo data until after modal mode is
 +        done.*/
 +      EDBM_RecalcNormals(em);
 +      BMEdit_RecalcTesselation(em);
 +
 +      WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit);
 +      
 +      return OPERATOR_FINISHED;       
 +}
 +
 +void MESH_OT_extrude_region(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Extrude Region";
 +      ot->idname= "MESH_OT_extrude_region";
 +      
 +      /* api callbacks */
 +      //ot->invoke= mesh_extrude_region_invoke;
 +      ot->exec= mesh_extrude_region_exec;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      RNA_def_boolean(ot->srna, "mirror", 0, "Mirror Editing", "");
 +}
 +
 +static int mesh_extrude_verts_exec(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh*)obedit->data)->edit_btmesh;
 +      float nor[3];
 +
 +      EDBM_Extrude_verts_indiv(em, op, BM_SELECT, nor);
 +      
 +      WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit);
 +      
 +      return OPERATOR_FINISHED;       
 +}
 +
 +void MESH_OT_extrude_verts_indiv(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Extrude Only Vertices";
 +      ot->idname= "MESH_OT_extrude_verts_indiv";
 +      
 +      /* api callbacks */
 +      ot->exec= mesh_extrude_verts_exec;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      /* to give to transform */
 +      RNA_def_boolean(ot->srna, "mirror", 0, "Mirror Editing", "");
 +}
 +
 +static int mesh_extrude_edges_exec(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh*)obedit->data)->edit_btmesh;
 +      float nor[3];
 +
 +      EDBM_Extrude_edges_indiv(em, op, BM_SELECT, nor);
 +      
 +      WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit);
 +      
 +      return OPERATOR_FINISHED;       
 +}
 +
 +void MESH_OT_extrude_edges_indiv(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Extrude Only Edges";
 +      ot->idname= "MESH_OT_extrude_edges_indiv";
 +      
 +      /* api callbacks */
 +      ot->exec= mesh_extrude_edges_exec;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      /* to give to transform */
 +      RNA_def_boolean(ot->srna, "mirror", 0, "Mirror Editing", "");
 +}
 +
 +static int mesh_extrude_faces_exec(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh*)obedit->data)->edit_btmesh;
 +      float nor[3];
 +
 +      EDBM_Extrude_face_indiv(em, op, BM_SELECT, nor);
 +      
 +      WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit);
 +      
 +      return OPERATOR_FINISHED;       
 +}
 +
 +void MESH_OT_extrude_faces_indiv(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Extrude Individual Faces";
 +      ot->idname= "MESH_OT_extrude_faces_indiv";
 +      
 +      /* api callbacks */
 +      ot->exec= mesh_extrude_faces_exec;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      RNA_def_boolean(ot->srna, "mirror", 0, "Mirror Editing", "");
 +}
 +
 +/* ******************** (de)select all operator **************** */
 +
 +void EDBM_toggle_select_all(BMEditMesh *em) /* exported for UV */
 +{
 +      if(em->bm->totvertsel || em->bm->totedgesel || em->bm->totfacesel)
 +              EDBM_clear_flag_all(em, BM_SELECT);
 +      else 
 +              EDBM_set_flag_all(em, BM_SELECT);
 +}
 +
 +static int mesh_select_all_exec(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      int action = RNA_enum_get(op->ptr, "action");
 +      
 +      switch (action) {
 +      case SEL_TOGGLE:
 +              EDBM_toggle_select_all(em);
 +              break;
 +      case SEL_SELECT:
 +              EDBM_set_flag_all(em, BM_SELECT);
 +              break;
 +      case SEL_DESELECT:
 +              EDBM_clear_flag_all(em, BM_SELECT);
 +              break;
 +      case SEL_INVERT:
 +              EDBM_select_swap(em);
 +              break;
 +      }
 +      
 +      WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_select_all(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Select/Deselect All";
 +      ot->idname= "MESH_OT_select_all";
 +      ot->description= "(de)select all vertices, edges or faces";
 +      
 +      /* api callbacks */
 +      ot->exec= mesh_select_all_exec;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      WM_operator_properties_select_all(ot);
 +}
 +
 +/* *************** add-click-mesh (extrude) operator ************** */
 +/* in trunk see: 'editmesh_add.c' */
 +static int dupli_extrude_cursor(bContext *C, wmOperator *op, wmEvent *event)
 +{
 +      ViewContext vc;
 +      BMVert *v1;
 +      BMIter iter;
 +      float min[3], max[3];
 +      int done= 0;
 +      short use_proj;
 +      
 +      em_setup_viewcontext(C, &vc);
 +      
 +      use_proj= (vc.scene->toolsettings->snap_flag & SCE_SNAP) &&     (vc.scene->toolsettings->snap_mode==SCE_SNAP_MODE_FACE);
 +
 +      INIT_MINMAX(min, max);
 +      
 +      BM_ITER(v1, &iter, vc.em->bm, BM_VERTS_OF_MESH, NULL) {
 +              if (BM_TestHFlag(v1, BM_SELECT)) {
 +                      DO_MINMAX(v1->co, min, max);
 +                      done= 1;
 +              }
 +      }
 +
 +      /* call extrude? */
 +      if(done) {
 +              const short rot_src= RNA_boolean_get(op->ptr, "rotate_source");
 +              BMEdge *eed;
 +              float vec[3], cent[3], mat[3][3];
 +              float nor[3]= {0.0, 0.0, 0.0};
 +
 +              /* 2D normal calc */
 +              float mval_f[2];
 +
 +              mval_f[0]= (float)event->mval[0];
 +              mval_f[1]= (float)event->mval[1];
 +
 +              /* check for edges that are half selected, use for rotation */
 +              done= 0;
 +              BM_ITER(eed, &iter, vc.em->bm, BM_EDGES_OF_MESH, NULL) {
 +                      if (BM_TestHFlag(eed, BM_SELECT)) {
 +                              float co1[3], co2[3];
 +                              mul_v3_m4v3(co1, vc.obedit->obmat, eed->v1->co);
 +                              mul_v3_m4v3(co2, vc.obedit->obmat, eed->v2->co);
 +                              project_float_noclip(vc.ar, co1, co1);
 +                              project_float_noclip(vc.ar, co2, co2);
 +
 +                              /* 2D rotate by 90d while adding.
 +                               *  (x, y) = (y, -x)
 +                               *
 +                               * accumulate the screenspace normal in 2D,
 +                               * with screenspace edge length weighting the result. */
 +                              if(line_point_side_v2(co1, co2, mval_f) >= 0.0f) {
 +                                      nor[0] +=  (co1[1] - co2[1]);
 +                                      nor[1] += -(co1[0] - co2[0]);
 +                              }
 +                              else {
 +                                      nor[0] +=  (co2[1] - co1[1]);
 +                                      nor[1] += -(co2[0] - co1[0]);
 +                              }
 +                      }
 +                      done= 1;
 +              }
 +
 +              if(done) {
 +                      float view_vec[3], cross[3];
 +
 +                      /* convert the 2D nomal into 3D */
 +                      mul_mat3_m4_v3(vc.rv3d->viewinv, nor); /* worldspace */
 +                      mul_mat3_m4_v3(vc.obedit->imat, nor); /* local space */
 +
 +                      /* correct the normal to be aligned on the view plane */
 +                      copy_v3_v3(view_vec, vc.rv3d->viewinv[2]);
 +                      mul_mat3_m4_v3(vc.obedit->imat, view_vec);
 +                      cross_v3_v3v3(cross, nor, view_vec);
 +                      cross_v3_v3v3(nor, view_vec, cross);
 +                      normalize_v3(nor);
 +              }
 +              
 +              /* center */
 +              mid_v3_v3v3(cent, min, max);
 +              copy_v3_v3(min, cent);
 +
 +              mul_m4_v3(vc.obedit->obmat, min);       // view space
 +              view3d_get_view_aligned_coordinate(&vc, min, event->mval, TRUE);
 +              mul_m4_v3(vc.obedit->imat, min); // back in object space
 +
 +              sub_v3_v3(min, cent);
 +              
 +              /* calculate rotation */
 +              unit_m3(mat);
 +              if(done) {
 +                      float dot;
 +
 +                      copy_v3_v3(vec, min);
 +                      normalize_v3(vec);
 +                      dot= dot_v3v3(vec, nor);
 +
 +                      if( fabs(dot)<0.999) {
 +                              float cross[3], si, q1[4];
 +
 +                              cross_v3_v3v3(cross, nor, vec);
 +                              normalize_v3(cross);
 +                              dot= 0.5f*saacos(dot);
 +
 +                              /* halve the rotation if its applied twice */
 +                              if(rot_src) dot *= 0.5f;
 +
 +                              si= (float)sin(dot);
 +                              q1[0]= (float)cos(dot);
 +                              q1[1]= cross[0]*si;
 +                              q1[2]= cross[1]*si;
 +                              q1[3]= cross[2]*si;
 +                              quat_to_mat3( mat,q1);
 +                      }
 +              }
 +              
 +              if(rot_src) {
 +                      EDBM_CallOpf(vc.em, op, "rotate verts=%hv cent=%v mat=%m3",
 +                              BM_SELECT, cent, mat);
 +
 +                      /* also project the source, for retopo workflow */
 +                      if(use_proj)
 +                              EMBM_project_snap_verts(C, vc.ar, vc.obedit, vc.em);
 +              }
 +
 +              EDBM_Extrude_edge(vc.obedit, vc.em, BM_SELECT, nor);
 +              EDBM_CallOpf(vc.em, op, "rotate verts=%hv cent=%v mat=%m3",
 +                      BM_SELECT, cent, mat);
 +              EDBM_CallOpf(vc.em, op, "translate verts=%hv vec=%v",
 +                      BM_SELECT, min);
 +      }
 +      else {
 +              float *curs= give_cursor(vc.scene, vc.v3d);
 +              BMOperator bmop;
 +              BMOIter oiter;
 +              
 +              copy_v3_v3(min, curs);
 +              view3d_get_view_aligned_coordinate(&vc, min, event->mval, 0);
 +
 +              invert_m4_m4(vc.obedit->imat, vc.obedit->obmat); 
 +              mul_m4_v3(vc.obedit->imat, min); // back in object space
 +              
 +              EDBM_InitOpf(vc.em, &bmop, op, "makevert co=%v", min);
 +              BMO_Exec_Op(vc.em->bm, &bmop);
 +
 +              BMO_ITER(v1, &oiter, vc.em->bm, &bmop, "newvertout", BM_VERT) {
 +                      BM_Select(vc.em->bm, v1, TRUE);
 +              }
 +
 +              if (!EDBM_FinishOp(vc.em, &bmop, op, 1))
 +                      return OPERATOR_CANCELLED;
 +      }
 +
 +      if(use_proj)
 +              EMBM_project_snap_verts(C, vc.ar, vc.obedit, vc.em);
 +
 +      /*This normally happens when pushing undo but modal operators
 +        like this one don't push undo data until after modal mode is
 +        done.*/
 +      EDBM_RecalcNormals(vc.em);
 +      BMEdit_RecalcTesselation(vc.em);
 +
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, vc.obedit->data); 
 +      DAG_id_tag_update(vc.obedit->data, OB_RECALC_DATA);
 +      
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_dupli_extrude_cursor(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Duplicate or Extrude at 3D Cursor";
 +      ot->idname= "MESH_OT_dupli_extrude_cursor";
 +      
 +      /* api callbacks */
 +      ot->invoke= dupli_extrude_cursor;
 +      ot->description= "Duplicate and extrude selected vertices, edges or faces towards the mouse cursor";
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      RNA_def_boolean(ot->srna, "rotate_source", 1, "Rotate Source", "Rotate initial selection giving better shape");
 +}
 +
 +static int delete_mesh(bContext *C, Object *obedit, wmOperator *op, int event, Scene *UNUSED(scene))
 +{
 +      BMEditMesh *bem = ((Mesh*)obedit->data)->edit_btmesh;
 +      
 +      if(event<1) return OPERATOR_CANCELLED;
 +
 +      if(event==10 ) {
 +              //"Erase Vertices";
 +
 +              if (!EDBM_CallOpf(bem, op, "del geom=%hv context=%i", BM_SELECT, DEL_VERTS))
 +                      return OPERATOR_CANCELLED;
 +      } 
 +      else if(event==11) {
 +              //"Edge Loop"
 +              if (!EDBM_CallOpf(bem, op, "dissolveedgeloop edges=%he", BM_SELECT))
 +                      return OPERATOR_CANCELLED;
 +      }
 +      else if(event==7) {
 +              //"Dissolve"
 +              if (bem->selectmode & SCE_SELECT_FACE) {
 +                      if (!EDBM_CallOpf(bem, op, "dissolvefaces faces=%hf",BM_SELECT))
 +                              return OPERATOR_CANCELLED;
 +              } else if (bem->selectmode & SCE_SELECT_EDGE) {
 +                      if (!EDBM_CallOpf(bem, op, "dissolveedges edges=%he",BM_SELECT))
 +                              return OPERATOR_CANCELLED;
 +              } else if (bem->selectmode & SCE_SELECT_VERTEX) {
 +                      if (!EDBM_CallOpf(bem, op, "dissolveverts verts=%hv",BM_SELECT))
 +                              return OPERATOR_CANCELLED;
 +              }
 +      }
 +      else if(event==4) {
 +              //Edges and Faces
 +              if (!EDBM_CallOpf(bem, op, "del geom=%hef context=%i", BM_SELECT, DEL_EDGESFACES))
 +                      return OPERATOR_CANCELLED;
 +      } 
 +      else if(event==1) {
 +              //"Erase Edges"
 +              if (!EDBM_CallOpf(bem, op, "del geom=%he context=%i", BM_SELECT, DEL_EDGES))
 +                      return OPERATOR_CANCELLED;
 +      }
 +      else if(event==2) {
 +              //"Erase Faces";
 +              if (!EDBM_CallOpf(bem, op, "del geom=%hf context=%i", BM_SELECT, DEL_FACES))
 +                      return OPERATOR_CANCELLED;
 +      }
 +      else if(event==5) {
 +              //"Erase Only Faces";
 +              if (!EDBM_CallOpf(bem, op, "del geom=%hf context=%d",
 +                                BM_SELECT, DEL_ONLYFACES))
 +                      return OPERATOR_CANCELLED;
 +      }
 +      
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +/* Note, these values must match delete_mesh() event values */
 +static EnumPropertyItem prop_mesh_delete_types[] = {
 +      {7, "DISSOLVE",         0, "Dissolve", ""},
 +      {12, "COLLAPSE", 0, "Collapse", ""},
 +      {10,"VERT",             0, "Vertices", ""},
 +      {1, "EDGE",             0, "Edges", ""},
 +      {2, "FACE",             0, "Faces", ""},
 +      {11, "EDGE_LOOP", 0, "Edge Loop", ""},
 +      {4, "EDGE_FACE", 0, "Edges & Faces", ""},
 +      {5, "ONLY_FACE", 0, "Only Faces", ""},
 +      {0, NULL, 0, NULL, NULL}
 +};
 +
 +static int delete_mesh_exec(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em = ((Mesh*)obedit->data)->edit_btmesh;
 +      Scene *scene = CTX_data_scene(C);
 +      int type = RNA_enum_get(op->ptr, "type");
 +      
 +      if (type != 12) {
 +              if (delete_mesh(C, obedit, op, type, scene) == OPERATOR_CANCELLED)
 +                      return OPERATOR_CANCELLED;
 +              EDBM_clear_flag_all(em, BM_SELECT);
 +      } else {
 +              if (!EDBM_CallOpf(em, op, "collapse edges=%he", BM_SELECT))
 +                      return OPERATOR_CANCELLED;
 +              DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      }
 +
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA|ND_SELECT, obedit);
 +      
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_delete(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Delete";
 +      ot->description= "Delete selected vertices, edges or faces";
 +      ot->idname= "MESH_OT_delete";
 +      
 +      /* api callbacks */
 +      ot->invoke= WM_menu_invoke;
 +      ot->exec= delete_mesh_exec;
 +      
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +      
 +      /*props */
 +      ot->prop = RNA_def_enum(ot->srna, "type", prop_mesh_delete_types, 10, "Type", "Method used for deleting mesh data");
 +}
 +
 +
 +static int addedgeface_mesh_exec(bContext *C, wmOperator *op)
 +{
 +      BMOperator bmop;
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      
 +      if (!EDBM_InitOpf(em, &bmop, op, "contextual_create geom=%hfev", BM_SELECT))
 +              return OPERATOR_CANCELLED;
 +      
 +      BMO_Exec_Op(em->bm, &bmop);
 +      BMO_HeaderFlag_Buffer(em->bm, &bmop, "faceout", BM_SELECT, BM_FACE);
 +
 +      if (!EDBM_FinishOp(em, &bmop, op, 1))
 +              return OPERATOR_CANCELLED;
 +
 +      WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit);
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_edge_face_add(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Make Edge/Face";
 +      ot->description= "Add an edge or face to selected";
 +      ot->idname= "MESH_OT_edge_face_add";
 +      
 +      /* api callbacks */
 +      ot->exec= addedgeface_mesh_exec;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +      
 +}
 +
 +static EnumPropertyItem prop_mesh_edit_types[] = {
 +      {1, "VERT", 0, "Vertices", ""},
 +      {2, "EDGE", 0, "Edges", ""},
 +      {3, "FACE", 0, "Faces", ""},
 +      {0, NULL, 0, NULL, NULL}
 +};
 +
 +static int mesh_selection_type_exec(bContext *C, wmOperator *op)
 +{             
 +      
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      int type = RNA_enum_get(op->ptr,"type");
 +
 +      switch (type) {
 +              case 1:
 +                      em->selectmode = SCE_SELECT_VERTEX;
 +                      break;
 +              case 2:
 +                      em->selectmode = SCE_SELECT_EDGE;
 +                      break;
 +              case 3:
 +                      em->selectmode = SCE_SELECT_FACE;
 +                      break;
 +      }
 +
 +      EDBM_selectmode_set(em);
 +      CTX_data_tool_settings(C)->selectmode = em->selectmode;
 +
 +      WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit);
 +      
 +      return OPERATOR_FINISHED;
 +}
 +
 +static void MESH_OT_selection_type(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Selection Mode";
 +      ot->description= "Set the selection mode type";
 +      ot->idname= "MESH_OT_selection_type";
 +      
 +      /* api callbacks */
 +      ot->invoke= WM_menu_invoke;
 +      ot->exec= mesh_selection_type_exec;
 +      
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +      
 +      /* props */
 +      RNA_def_enum(ot->srna, "type", prop_mesh_edit_types, 0, "Type", "Set the mesh selection type");
 +      RNA_def_boolean(ot->srna, "inclusive", 0, "Inclusive", "Selects geometry around selected geometry, occording to selection mode");       
 +}
 +
 +/* ************************* SEAMS AND EDGES **************** */
 +
 +static int editbmesh_mark_seam(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      Mesh *me= ((Mesh *)obedit->data);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      BMesh *bm = em->bm;
 +      BMEdge *eed;
 +      BMIter iter;
 +      int clear = RNA_boolean_get(op->ptr, "clear");
 +      
 +      /* auto-enable seams drawing */
 +      if(clear==0) {
 +              me->drawflag |= ME_DRAWSEAMS;
 +      }
 +
 +      if(clear) {
 +              BM_ITER(eed, &iter, bm, BM_EDGES_OF_MESH, NULL) {
 +                      if (!BM_TestHFlag(eed, BM_SELECT) || BM_TestHFlag(eed, BM_HIDDEN))
 +                              continue;
 +                      
 +                      BM_ClearHFlag(eed, BM_SEAM);
 +              }
 +      }
 +      else {
 +              BM_ITER(eed, &iter, bm, BM_EDGES_OF_MESH, NULL) {
 +                      if (!BM_TestHFlag(eed, BM_SELECT) || BM_TestHFlag(eed, BM_HIDDEN))
 +                              continue;
 +                      BM_SetHFlag(eed, BM_SEAM);
 +              }
 +      }
 +
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_mark_seam(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Mark Seam";
 +      ot->idname= "MESH_OT_mark_seam";
 +      ot->description= "(un)mark selected edges as a seam";
 +      
 +      /* api callbacks */
 +      ot->exec= editbmesh_mark_seam;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +      
 +      RNA_def_boolean(ot->srna, "clear", 0, "Clear", "");
 +}
 +
 +static int editbmesh_mark_sharp(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      Mesh *me= ((Mesh *)obedit->data);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      BMesh *bm = em->bm;
 +      BMEdge *eed;
 +      BMIter iter;
 +      int clear = RNA_boolean_get(op->ptr, "clear");
 +
 +      /* auto-enable sharp edge drawing */
 +      if(clear == 0) {
 +              me->drawflag |= ME_DRAWSHARP;
 +      }
 +
 +      if(!clear) {
 +              BM_ITER(eed, &iter, bm, BM_EDGES_OF_MESH, NULL) {
 +                      if (!BM_TestHFlag(eed, BM_SELECT) || BM_TestHFlag(eed, BM_HIDDEN))
 +                              continue;
 +                      
 +                      BM_SetHFlag(eed, BM_SHARP);
 +              }
 +      } else {
 +              BM_ITER(eed, &iter, bm, BM_EDGES_OF_MESH, NULL) {
 +                      if (!BM_TestHFlag(eed, BM_SELECT) || BM_TestHFlag(eed, BM_HIDDEN))
 +                              continue;
 +                      
 +                      BM_ClearHFlag(eed, BM_SHARP);
 +              }
 +      }
 +
 +
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_mark_sharp(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Mark Sharp";
 +      ot->idname= "MESH_OT_mark_sharp";
 +      ot->description= "(un)mark selected edges as sharp";
 +      
 +      /* api callbacks */
 +      ot->exec= editbmesh_mark_sharp;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +      
 +      RNA_def_boolean(ot->srna, "clear", 0, "Clear", "");
 +}
 +
 +
 +static int editbmesh_vert_connect(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      BMesh *bm = em->bm;
 +      BMOperator bmop;
 +      int len = 0;
 +      
 +      if (!EDBM_InitOpf(em, &bmop, op, "connectverts verts=%hv", BM_SELECT)) {
 +              return OPERATOR_CANCELLED;
 +      }
 +      BMO_Exec_Op(bm, &bmop);
 +      len = BMO_GetSlot(&bmop, "edgeout")->len;
 +      if (!EDBM_FinishOp(em, &bmop, op, 1)) {
 +              return OPERATOR_CANCELLED;
 +      }
 +      
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return len ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
 +}
 +
 +void MESH_OT_vert_connect(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Vertex Connect";
 +      ot->idname= "MESH_OT_vert_connect";
 +      
 +      /* api callbacks */
 +      ot->exec= editbmesh_vert_connect;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +}
 +
 +static int editbmesh_edge_split(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      BMesh *bm = em->bm;
 +      BMOperator bmop;
 +      int len = 0;
 +      
 +      if (!EDBM_InitOpf(em, &bmop, op, "edgesplit edges=%he numcuts=%d", 
 +                      BM_SELECT, RNA_int_get(op->ptr,"number_cuts"))) {
 +              return OPERATOR_CANCELLED;
 +      }
 +      BMO_Exec_Op(bm, &bmop);
 +      len = BMO_GetSlot(&bmop, "outsplit")->len;
 +      if (!EDBM_FinishOp(em, &bmop, op, 1)) {
 +              return OPERATOR_CANCELLED;
 +      }
 +      
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return len ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
 +}
 +
 +void MESH_OT_edge_split(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Edge Split";
 +      ot->idname= "MESH_OT_edge_split";
 +      
 +      /* api callbacks */
 +      ot->exec= editbmesh_edge_split;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      RNA_def_int(ot->srna, "number_cuts", 1, 1, 10, "Number of Cuts", "", 1, INT_MAX);
 +}
 +
 +/****************** add duplicate operator ***************/
 +
 +static int mesh_duplicate_exec(bContext *C, wmOperator *op)
 +{
 +      Object *ob= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh*)ob->data)->edit_btmesh;
 +      BMOperator bmop;
 +
 +      EDBM_InitOpf(em, &bmop, op, "dupe geom=%hvef", BM_SELECT);
 +      
 +      BMO_Exec_Op(em->bm, &bmop);
 +      EDBM_clear_flag_all(em, BM_SELECT);
 +
 +      BMO_HeaderFlag_Buffer(em->bm, &bmop, "newout", BM_SELECT, BM_ALL);
 +
 +      if (!EDBM_FinishOp(em, &bmop, op, 1))
 +              return OPERATOR_CANCELLED;
 +
 +      DAG_id_tag_update(ob->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data);
 +      
 +      return OPERATOR_FINISHED;
 +}
 +
 +static int mesh_duplicate_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
 +{
 +      WM_cursor_wait(1);
 +      mesh_duplicate_exec(C, op);
 +      WM_cursor_wait(0);
 +      
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_duplicate(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Duplicate";
 +      ot->description= "Duplicate selected vertices, edges or faces";
 +      ot->idname= "MESH_OT_duplicate";
 +      
 +      /* api callbacks */
 +      ot->invoke= mesh_duplicate_invoke;
 +      ot->exec= mesh_duplicate_exec;
 +      
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* to give to transform */
 +      RNA_def_int(ot->srna, "mode", TFM_TRANSLATION, 0, INT_MAX, "Mode", "", 0, INT_MAX);
 +}
 +
 +static int flip_normals(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= (((Mesh *)obedit->data))->edit_btmesh;
 +      
 +      if (!EDBM_CallOpf(em, op, "reversefaces faces=%hf", BM_SELECT))
 +              return OPERATOR_CANCELLED;
 +      
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_flip_normals(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Flip Normals";
 +      ot->description= "Flip the direction of selected face's vertex and face normals";
 +      ot->idname= "MESH_OT_flip_normals";
 +      
 +      /* api callbacks */
 +      ot->exec= flip_normals;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +}
 +
 +float *bm_get_cd_float(CustomData *cdata, void *data, int type)
 +{
 +      float *f = CustomData_bmesh_get(cdata, data, type);
 +
 +      return f;
 +}
 +
 +static const EnumPropertyItem direction_items[]= {
 +      {DIRECTION_CW, "CW", 0, "Clockwise", ""},
 +      {DIRECTION_CCW, "CCW", 0, "Counter Clockwise", ""},
 +      {0, NULL, 0, NULL, NULL}};
 +
 +/* only accepts 1 selected edge, or 2 selected faces */
 +static int edge_rotate_selected(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      BMOperator bmop;
 +      BMEdge *eed;
 +      BMIter iter;
 +      const int do_ccw = RNA_enum_get(op->ptr, "direction") == 1;
 +      int do_deselect= FALSE; /* do we deselect */
 +      
 +      if (!(em->bm->totfacesel == 2 || em->bm->totedgesel == 1)) {
 +              BKE_report(op->reports, RPT_ERROR, "Select one edge or two adjacent faces");
 +              return OPERATOR_CANCELLED;
 +      }
 +
 +      /*first see if we have two adjacent faces*/
 +      BM_ITER(eed, &iter, em->bm, BM_EDGES_OF_MESH, NULL) {
 +              if (BM_Edge_FaceCount(eed) == 2) {
 +                      if ((BM_TestHFlag(eed->l->f, BM_SELECT) && BM_TestHFlag(eed->l->radial_next->f, BM_SELECT))
 +                               && !(BM_TestHFlag(eed->l->f, BM_HIDDEN) || BM_TestHFlag(eed->l->radial_next->f, BM_HIDDEN)))
 +                      {
 +                              break;
 +                      }
 +              }
 +      }
 +      
 +      /*ok, we don't have two adjacent faces, but we do have two selected ones.
 +        that's an error condition.*/
 +      if (!eed && em->bm->totfacesel == 2) {
 +              BKE_report(op->reports, RPT_ERROR, "Select one edge or two adjacent faces");
 +              return OPERATOR_CANCELLED;
 +      }
 +
 +      if (!eed) {
 +              BM_ITER(eed, &iter, em->bm, BM_EDGES_OF_MESH, NULL) {
 +                      if (BM_TestHFlag(eed, BM_SELECT) && !BM_TestHFlag(eed, BM_HIDDEN)) {
 +                              /* de-select the edge before */
 +                              do_deselect = TRUE;
 +                              break;
 +                      }
 +              }
 +      }
 +
 +      /*this should never happen*/
 +      if (!eed)
 +              return OPERATOR_CANCELLED;
 +      
 +      EDBM_InitOpf(em, &bmop, op, "edgerotate edges=%e ccw=%d", eed, do_ccw);
 +
 +      /* avoid adding to the selection if we start off with only a selected edge,
 +       * we could also just deselect the single edge easily but use the BMO api
 +       * since it seems this is more 'correct' */
 +      if(do_deselect) BMO_UnHeaderFlag_Buffer(em->bm, &bmop, "edges", BM_SELECT, BM_EDGE);
 +
 +      BMO_Exec_Op(em->bm, &bmop);
 +      BMO_HeaderFlag_Buffer(em->bm, &bmop, "edgeout", BM_SELECT, BM_EDGE);
 +
 +      if (!EDBM_FinishOp(em, &bmop, op, 1))
 +              return OPERATOR_CANCELLED;
 +
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_edge_rotate(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Rotate Selected Edge";
 +      ot->description= "Rotate selected edge or adjoining faces";
 +      ot->idname= "MESH_OT_edge_rotate";
 +
 +      /* api callbacks */
 +      ot->exec= edge_rotate_selected;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      /* props */
 +      RNA_def_enum(ot->srna, "direction", direction_items, DIRECTION_CW, "direction", "direction to rotate edge around");
 +}
 +
 +/* swap is 0 or 1, if 1 it hides not selected */
 +void EDBM_hide_mesh(BMEditMesh *em, int swap)
 +{
 +      BMIter iter;
 +      BMHeader *h;
 +      int itermode;
 +
 +      if(em==NULL) return;
 +      
 +      if (em->selectmode & SCE_SELECT_VERTEX)
 +              itermode = BM_VERTS_OF_MESH;
 +      else if (em->selectmode & SCE_SELECT_EDGE)
 +              itermode = BM_EDGES_OF_MESH;
 +      else
 +              itermode = BM_FACES_OF_MESH;
 +
 +      BM_ITER(h, &iter, em->bm, itermode, NULL) {
 +              if (BM_TestHFlag(h, BM_SELECT) ^ swap)
 +                      BM_Hide(em->bm, h, 1);
 +      }
 +
 +      EDBM_selectmode_flush(em);
 +
 +      /*original hide flushing comment (OUTDATED): 
 +        hide happens on least dominant select mode, and flushes up, not down! (helps preventing errors in subsurf) */
 +      /*  - vertex hidden, always means edge is hidden too
 +              - edge hidden, always means face is hidden too
 +              - face hidden, only set face hide
 +              - then only flush back down what's absolute hidden
 +      */
 +
 +}
 +
 +static int hide_mesh_exec(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= (((Mesh *)obedit->data))->edit_btmesh;
 +      
 +      EDBM_hide_mesh(em, RNA_boolean_get(op->ptr, "unselected"));
 +              
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;       
 +}
 +
 +void MESH_OT_hide(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Hide Selection";
 +      ot->idname= "MESH_OT_hide";
 +      
 +      /* api callbacks */
 +      ot->exec= hide_mesh_exec;
 +      ot->poll= ED_operator_editmesh;
 +       ot->description= "Hide (un)selected vertices, edges or faces";
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +      
 +      /* props */
 +      RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected");
 +}
 +
 +
 +void EDBM_reveal_mesh(BMEditMesh *em)
 +{
 +      BMIter iter;
 +      BMHeader *ele;
 +      int i, types[3] = {BM_VERTS_OF_MESH, BM_EDGES_OF_MESH, BM_FACES_OF_MESH};
 +      int sels[3] = {
 +              (em->selectmode & SCE_SELECT_VERTEX),
 +              (em->selectmode & SCE_SELECT_EDGE),
 +              (em->selectmode & SCE_SELECT_FACE),
 +      };
 +
 +      /* Use index field to remember what was hidden before all is revealed. */
 +      for (i=0; i<3; i++) {
 +              BM_ITER(ele, &iter, em->bm, types[i], NULL) {
 +                      if (BM_TestHFlag(ele, BM_HIDDEN)) {
 +                              BM_SetHFlag(ele, BM_TMP_TAG);
 +                      }
 +                      else {
 +                              BM_ClearHFlag(ele, BM_TMP_TAG);
 +                      }
 +              }
 +      }
 +
 +      /* Reveal everything */
 +      EDBM_clear_flag_all(em, BM_HIDDEN);
 +
 +      /* Select relevant just-revealed elements */
 +      for (i=0; i<3; i++) {
 +              if (!sels[i]) {
 +                      continue;
 +              }
 +
 +              BM_ITER(ele, &iter, em->bm, types[i], NULL) {
 +                      if (BM_TestHFlag(ele, BM_TMP_TAG)) {
 +                              BM_Select(em->bm, ele, TRUE);
 +                      }
 +              }
 +      }
 +
 +      EDBM_selectmode_flush(em);
 +}
 +
 +static int reveal_mesh_exec(bContext *C, wmOperator *UNUSED(op))
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= (((Mesh *)obedit->data))->edit_btmesh;
 +      
 +      EDBM_reveal_mesh(em);
 +
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;       
 +}
 +
 +void MESH_OT_reveal(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Reveal Hidden";
 +      ot->idname= "MESH_OT_reveal";
 +      ot->description= "Reveal all hidden vertices, edges and faces";
 +      
 +      /* api callbacks */
 +      ot->exec= reveal_mesh_exec;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +}
 +
 +static int normals_make_consistent_exec(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      
 +      /* doflip has to do with bmesh_rationalize_normals, it's an internal
 +       * thing*/
 +      if (!EDBM_CallOpf(em, op, "righthandfaces faces=%hf doflip=%d", BM_SELECT, 1))
 +              return OPERATOR_CANCELLED;
 +
 +      if (RNA_boolean_get(op->ptr, "inside"))
 +              EDBM_CallOpf(em, op, "reversefaces faces=%hf", BM_SELECT);
 +
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;       
 +}
 +
 +void MESH_OT_normals_make_consistent(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Make Normals Consistent";
 +      ot->description= "Make face and vertex normals point either outside or inside the mesh";
 +      ot->idname= "MESH_OT_normals_make_consistent";
 +      
 +      /* api callbacks */
 +      ot->exec= normals_make_consistent_exec;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +      
 +      RNA_def_boolean(ot->srna, "inside", 0, "Inside", "");
 +}
 +
 +
 +
 +static int do_smooth_vertex(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      ModifierData *md;
 +      int mirrx=0, mirry=0, mirrz=0;
 +      int i, repeat;
 +
 +      /* mirror before smooth */
 +      if (((Mesh *)obedit->data)->editflag & ME_EDIT_MIRROR_X) {
 +              EDBM_CacheMirrorVerts(em, TRUE);
 +      }
 +
 +      /* if there is a mirror modifier with clipping, flag the verts that
 +       * are within tolerance of the plane(s) of reflection 
 +       */
 +      for(md=obedit->modifiers.first; md; md=md->next) {
 +              if(md->type==eModifierType_Mirror && (md->mode & eModifierMode_Realtime)) {
 +                      MirrorModifierData *mmd = (MirrorModifierData*) md;     
 +              
 +                      if(mmd->flag & MOD_MIR_CLIPPING) {
 +                              if (mmd->flag & MOD_MIR_AXIS_X)
 +                                      mirrx = 1;
 +                              if (mmd->flag & MOD_MIR_AXIS_Y)
 +                                      mirry = 1;
 +                              if (mmd->flag & MOD_MIR_AXIS_Z)
 +                                      mirrz = 1;
 +                      }
 +              }
 +      }
 +
 +      repeat = RNA_int_get(op->ptr,"repeat");
 +      if (!repeat)
 +              repeat = 1;
 +      
 +      for (i=0; i<repeat; i++) {
 +              if (!EDBM_CallOpf(em, op, "vertexsmooth verts=%hv mirror_clip_x=%d mirror_clip_y=%d mirror_clip_z=%d",
 +                                BM_SELECT, mirrx, mirry, mirrz))
 +              {
 +                      return OPERATOR_CANCELLED;
 +              }
 +      }
 +
 +      /* apply mirror */
 +      if (((Mesh *)obedit->data)->editflag & ME_EDIT_MIRROR_X) {
 +              EDBM_ApplyMirrorCache(em, BM_SELECT, 0);
 +              EDBM_EndMirrorCache(em);
 +      }
 +
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}     
 +      
 +void MESH_OT_vertices_smooth(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Smooth Vertex";
 +      ot->description= "Flatten angles of selected vertices";
 +      ot->idname= "MESH_OT_vertices_smooth";
 +      
 +      /* api callbacks */
 +      ot->exec= do_smooth_vertex;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      RNA_def_int(ot->srna, "repeat", 1, 1, 100, "Number of times to smooth the mesh", "", 1, INT_MAX);
 +}
 +
 +
 +static int bm_test_exec(bContext *C, wmOperator *UNUSED(op))
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      ARegion *ar = CTX_wm_region(C);
 +      View3D *v3d = CTX_wm_view3d(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      BMBVHTree *tree = BMBVH_NewBVH(em, 0, NULL, NULL);
 +      BMIter iter;
 +      BMEdge *e;
 +
 +      /*hide all back edges*/
 +      BM_ITER(e, &iter, em->bm, BM_EDGES_OF_MESH, NULL) {
 +              if (!BM_TestHFlag(e, BM_SELECT))
 +                      continue;
 +
 +              if (!BMBVH_EdgeVisible(tree, e, ar, v3d, obedit))
 +                      BM_Select(em->bm, e, FALSE);
 +      }
 +
 +      BMBVH_FreeBVH(tree);
 +      
 +#if 0 //uv island walker test
 +      BMIter iter, liter;
 +      BMFace *f;
 +      BMLoop *l, *l2;
 +      MLoopUV *luv;
 +      BMWalker walker;
 +      int i=0;
 +
 +      BM_ITER(f, &iter, em->bm, BM_FACES_OF_MESH, NULL) {
 +              BM_ITER(l, &liter, em->bm, BM_LOOPS_OF_FACE, f) {
 +                      luv = CustomData_bmesh_get(&em->bm->ldata, l->head.data, CD_MLOOPUV);
 +              }
 +      }
 +
 +      BMW_Init(&walker, em->bm, BMW_UVISLAND, BMW_NIL_LAY);
 +
 +      BM_ITER(f, &iter, em->bm, BM_FACES_OF_MESH, NULL) {
 +              BM_ITER(l, &liter, em->bm, BM_LOOPS_OF_FACE, f) {
 +                      luv = CustomData_bmesh_get(&em->bm->ldata, l->head.data, CD_MLOOPUV);
 +                      if (luv->flag & MLOOPUV_VERTSEL) {
 +                              l2 = BMW_Begin(&walker, l);
 +                              for (; l2; l2=BMW_Step(&walker)) {
 +                                      luv = CustomData_bmesh_get(&em->bm->ldata, l2->head.data, CD_MLOOPUV);
 +                                      luv->flag |= MLOOPUV_VERTSEL;
 +                              }                               
 +                      }
 +              }
 +      }
 +
 +      BMW_End(&walker);
 +#endif
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}     
 +      
 +void MESH_OT_bm_test(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "BMesh Test Operator";
 +      ot->idname= "MESH_OT_bm_test";
 +      
 +      /* api callbacks */
 +      ot->exec= bm_test_exec;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      //RNA_def_int(ot->srna, "repeat", 1, 1, 100, "Number of times to smooth the mesh", "", 1, INT_MAX);
 +}
 +
 +/********************** Smooth/Solid Operators *************************/
 +
 +static void mesh_set_smooth_faces(BMEditMesh *em, short smooth)
 +{
 +      BMIter iter;
 +      BMFace *efa;
 +
 +      if(em==NULL) return;
 +      
 +      BM_ITER(efa, &iter, em->bm, BM_FACES_OF_MESH, NULL) {
 +              if (BM_TestHFlag(efa, BM_SELECT)) {
 +                      if (smooth)
 +                              BM_SetHFlag(efa, BM_SMOOTH);
 +                      else
 +                              BM_ClearHFlag(efa, BM_SMOOTH);
 +              }
 +      }
 +}
 +
 +static int mesh_faces_shade_smooth_exec(bContext *C, wmOperator *UNUSED(op))
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +
 +      mesh_set_smooth_faces(em, 1);
 +
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_faces_shade_smooth(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Shade Smooth";
 +       ot->description= "Display faces smooth (using vertex normals)";
 +      ot->idname= "MESH_OT_faces_shade_smooth";
 +
 +      /* api callbacks */
 +      ot->exec= mesh_faces_shade_smooth_exec;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +}
 +
 +static int mesh_faces_shade_flat_exec(bContext *C, wmOperator *UNUSED(op))
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +
 +      mesh_set_smooth_faces(em, 0);
 +
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_faces_shade_flat(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Shade Flat";
 +      ot->description= "Display faces flat";
 +      ot->idname= "MESH_OT_faces_shade_flat";
 +
 +      /* api callbacks */
 +      ot->exec= mesh_faces_shade_flat_exec;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +}
 +
 +
 +/********************** UV/Color Operators *************************/
 +
 +
 +static const EnumPropertyItem axis_items[]= {
 +      {OPUVC_AXIS_X, "X", 0, "X", ""},
 +      {OPUVC_AXIS_Y, "Y", 0, "Y", ""},
 +      {0, NULL, 0, NULL, NULL}};
 +
 +static int mesh_rotate_uvs(bContext *C, wmOperator *op)
 +{
 +      Object *ob = CTX_data_edit_object(C);
 +      BMEditMesh *em = ((Mesh*)ob->data)->edit_btmesh;
 +      BMOperator bmop;
 +
 +      /* get the direction from RNA */
 +      int dir = RNA_enum_get(op->ptr, "direction");
 +
 +      /* initialize the bmop using EDBM api, which does various ui error reporting and other stuff */
 +      EDBM_InitOpf(em, &bmop, op, "meshrotateuvs faces=%hf dir=%d", BM_SELECT, dir);
 +
 +      /* execute the operator */
 +      BMO_Exec_Op(em->bm, &bmop);
 +
 +      /* finish the operator */
 +      if( !EDBM_FinishOp(em, &bmop, op, 1) )
 +              return OPERATOR_CANCELLED;
 +
 +
 +      /* dependencies graph and notification stuff */
 +      DAG_id_tag_update(ob->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data);
 +
 +      /* we succeeded */
 +      return OPERATOR_FINISHED;
 +}
 +
 +static int mesh_reverse_uvs(bContext *C, wmOperator *op)
 +{
 +      Object *ob = CTX_data_edit_object(C);
 +      BMEditMesh *em = ((Mesh*)ob->data)->edit_btmesh;
 +      BMOperator bmop;
 +
 +      /* initialize the bmop using EDBM api, which does various ui error reporting and other stuff */
 +      EDBM_InitOpf(em, &bmop, op, "meshreverseuvs faces=%hf", BM_SELECT);
 +
 +      /* execute the operator */
 +      BMO_Exec_Op(em->bm, &bmop);
 +
 +      /* finish the operator */
 +      if( !EDBM_FinishOp(em, &bmop, op, 1) )
 +              return OPERATOR_CANCELLED;
 +
 +      /* dependencies graph and notification stuff */
 +      DAG_id_tag_update(ob->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data);
 +
 +      /* we succeeded */
 +      return OPERATOR_FINISHED;
 +}
 +
 +static int mesh_rotate_colors(bContext *C, wmOperator *op)
 +{
 +      Object *ob = CTX_data_edit_object(C);
 +      BMEditMesh *em = ((Mesh*)ob->data)->edit_btmesh;
 +      BMOperator bmop;
 +
 +      /* get the direction from RNA */
 +      int dir = RNA_enum_get(op->ptr, "direction");
 +
 +      /* initialize the bmop using EDBM api, which does various ui error reporting and other stuff */
 +      EDBM_InitOpf(em, &bmop, op, "meshrotatecolors faces=%hf dir=%d", BM_SELECT, dir);
 +
 +      /* execute the operator */
 +      BMO_Exec_Op(em->bm, &bmop);
 +
 +      /* finish the operator */
 +      if( !EDBM_FinishOp(em, &bmop, op, 1) )
 +              return OPERATOR_CANCELLED;
 +
 +
 +      /* dependencies graph and notification stuff */
 +      DAG_id_tag_update(ob->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data);
 +/*    DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_OBJECT | ND_GEOM_SELECT, ob);
 +*/
 +      /* we succeeded */
 +      return OPERATOR_FINISHED;
 +}
 +
 +
 +static int mesh_reverse_colors(bContext *C, wmOperator *op)
 +{
 +      Object *ob = CTX_data_edit_object(C);
 +      BMEditMesh *em = ((Mesh*)ob->data)->edit_btmesh;
 +      BMOperator bmop;
 +
 +      /* initialize the bmop using EDBM api, which does various ui error reporting and other stuff */
 +      EDBM_InitOpf(em, &bmop, op, "meshreversecolors faces=%hf", BM_SELECT);
 +
 +      /* execute the operator */
 +      BMO_Exec_Op(em->bm, &bmop);
 +
 +      /* finish the operator */
 +      if( !EDBM_FinishOp(em, &bmop, op, 1) )
 +              return OPERATOR_CANCELLED;
 +
 +      DAG_id_tag_update(ob->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data);
 +
 +      /* we succeeded */
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_uvs_rotate(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Rotate UVs";
 +      ot->idname= "MESH_OT_uvs_rotate";
 +
 +      /* api callbacks */
 +      ot->exec= mesh_rotate_uvs;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      /* props */
 +      RNA_def_enum(ot->srna, "direction", direction_items, DIRECTION_CW, "Direction", "Direction to rotate UVs around");
 +}
 +
 +//void MESH_OT_uvs_mirror(wmOperatorType *ot)
 +void MESH_OT_uvs_reverse(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Reverse UVs";
 +      ot->idname= "MESH_OT_uvs_reverse";
 +
 +      /* api callbacks */
 +      ot->exec= mesh_reverse_uvs;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      /* props */
 +      //RNA_def_enum(ot->srna, "axis", axis_items, DIRECTION_CW, "Axis", "Axis to mirror UVs around");
 +}
 +
 +void MESH_OT_colors_rotate(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Rotate Colors";
 +      ot->idname= "MESH_OT_colors_rotate";
 +
 +      /* api callbacks */
 +      ot->exec= mesh_rotate_colors;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      /* props */
 +      RNA_def_enum(ot->srna, "direction", direction_items, DIRECTION_CCW, "Direction", "Direction to rotate edge around");
 +}
 +
 +void MESH_OT_colors_reverse(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Reverse Colors";
 +      ot->idname= "MESH_OT_colors_reverse";
 +
 +      /* api callbacks */
 +      ot->exec= mesh_reverse_colors;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      /* props */
 +      //RNA_def_enum(ot->srna, "axis", axis_items, DIRECTION_CW, "Axis", "Axis to mirror colors around");
 +}
 +
 +
 +static int merge_firstlast(BMEditMesh *em, int first, int uvmerge, wmOperator *wmop)
 +{
 +      BMVert *mergevert;
 +      BMEditSelection *ese;
 +
 +      /* do sanity check in mergemenu in edit.c ?*/
 +      if(first == 0) {
 +              ese = em->bm->selected.last;
 +              mergevert= (BMVert*)ese->data;
 +      }
 +      else{
 +              ese = em->bm->selected.first;
 +              mergevert = (BMVert*)ese->data;
 +      }
 +
 +      if (!BM_TestHFlag(mergevert, BM_SELECT))
 +              return OPERATOR_CANCELLED;
 +      
 +      if (uvmerge) {
 +              if (!EDBM_CallOpf(em, wmop, "pointmerge_facedata verts=%hv snapv=%e", BM_SELECT, mergevert))
 +                      return OPERATOR_CANCELLED;
 +      }
 +
 +      if (!EDBM_CallOpf(em, wmop, "pointmerge verts=%hv mergeco=%v", BM_SELECT, mergevert->co))
 +              return OPERATOR_CANCELLED;
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +static int merge_target(BMEditMesh *em, Scene *scene, View3D *v3d, Object *ob, 
 +                        int target, int uvmerge, wmOperator *wmop)
 +{
 +      BMIter iter;
 +      BMVert *v;
 +      float *vco=NULL, co[3], cent[3] = {0.0f, 0.0f, 0.0f};
 +
 +      if (target) {
 +              vco = give_cursor(scene, v3d);
 +              copy_v3_v3(co, vco);
 +              mul_m4_v3(ob->imat, co);
 +      }
 +      else {
 +              float fac;
 +              int i = 0;
 +              BM_ITER(v, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
 +                      if (!BM_TestHFlag(v, BM_SELECT))
 +                              continue;
 +                      add_v3_v3(cent, v->co);
 +                      i++;
 +              }
 +              
 +              if (!i)
 +                      return OPERATOR_CANCELLED;
 +
 +              fac = 1.0f / (float)i;
 +              mul_v3_fl(cent, fac);
 +              copy_v3_v3(co, cent);
 +              vco = co;
 +      }
 +
 +      if (!vco)
 +              return OPERATOR_CANCELLED;
 +      
 +      if (uvmerge) {
 +              if (!EDBM_CallOpf(em, wmop, "vert_average_facedata verts=%hv", BM_SELECT))
 +                      return OPERATOR_CANCELLED;
 +      }
 +
 +      if (!EDBM_CallOpf(em, wmop, "pointmerge verts=%hv mergeco=%v", BM_SELECT, co))
 +              return OPERATOR_CANCELLED;
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +static int merge_exec(bContext *C, wmOperator *op)
 +{
 +      Scene *scene= CTX_data_scene(C);
 +      View3D *v3d = CTX_wm_view3d(C);
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      int status= 0, uvs= RNA_boolean_get(op->ptr, "uvs");
 +
 +      switch(RNA_enum_get(op->ptr, "type")) {
 +              case 3:
 +                      status = merge_target(em, scene, v3d, obedit, 0, uvs, op);
 +                      break;
 +              case 4:
 +                      status = merge_target(em, scene, v3d, obedit, 1, uvs, op);
 +                      break;
 +              case 1:
 +                      status = merge_firstlast(em, 0, uvs, op);
 +                      break;
 +              case 6:
 +                      status = merge_firstlast(em, 1, uvs, op);
 +                      break;
 +              case 5:
 +                      status = 1;
 +                      if (!EDBM_CallOpf(em, op, "collapse edges=%he", BM_SELECT))
 +                              status = 0;
 +                      break;
 +      }
 +
 +      if(!status)
 +              return OPERATOR_CANCELLED;
 +
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +static EnumPropertyItem merge_type_items[]= {
 +      {6, "FIRST", 0, "At First", ""},
 +      {1, "LAST", 0, "At Last", ""},
 +      {3, "CENTER", 0, "At Center", ""},
 +      {4, "CURSOR", 0, "At Cursor", ""},
 +      {5, "COLLAPSE", 0, "Collapse", ""},
 +      {0, NULL, 0, NULL, NULL}};
 +
 +static EnumPropertyItem *merge_type_itemf(bContext *C, PointerRNA *UNUSED(ptr),  PropertyRNA *UNUSED(prop), int *free)
 +{     
 +      Object *obedit;
 +      EnumPropertyItem *item= NULL;
 +      int totitem= 0;
 +      
 +      if(!C) /* needed for docs */
 +              return merge_type_items;
 +      
 +      obedit= CTX_data_edit_object(C);
 +      if(obedit && obedit->type == OB_MESH) {
 +              BMEditMesh *em= ((Mesh*)obedit->data)->edit_btmesh;
 +
 +              if(em->selectmode & SCE_SELECT_VERTEX) {
 +                      if(em->bm->selected.first && em->bm->selected.last &&
 +                              ((BMEditSelection*)em->bm->selected.first)->htype == BM_VERT && ((BMEditSelection*)em->bm->selected.last)->htype == BM_VERT) {
 +                              RNA_enum_items_add_value(&item, &totitem, merge_type_items, 6);
 +                              RNA_enum_items_add_value(&item, &totitem, merge_type_items, 1);
 +                      }
 +                      else if(em->bm->selected.first && ((BMEditSelection*)em->bm->selected.first)->htype == BM_VERT)
 +                              RNA_enum_items_add_value(&item, &totitem, merge_type_items, 1);
 +                      else if(em->bm->selected.last && ((BMEditSelection*)em->bm->selected.last)->htype == BM_VERT)
 +                              RNA_enum_items_add_value(&item, &totitem, merge_type_items, 6);
 +              }
 +
 +              RNA_enum_items_add_value(&item, &totitem, merge_type_items, 3);
 +              RNA_enum_items_add_value(&item, &totitem, merge_type_items, 4);
 +              RNA_enum_items_add_value(&item, &totitem, merge_type_items, 5);
 +              RNA_enum_item_end(&item, &totitem);
 +
 +              *free= 1;
 +
 +              return item;
 +      }
 +      
 +      return NULL;
 +}
 +
 +void MESH_OT_merge(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Merge";
 +      ot->idname= "MESH_OT_merge";
 +
 +      /* api callbacks */
 +      ot->exec= merge_exec;
 +      ot->invoke= WM_menu_invoke;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      /* properties */
 +      ot->prop= RNA_def_enum(ot->srna, "type", merge_type_items, 3, "Type", "Merge method to use");
 +      RNA_def_enum_funcs(ot->prop, merge_type_itemf);
 +      RNA_def_boolean(ot->srna, "uvs", 1, "UVs", "Move UVs according to merge");
 +}
 +
 +
 +static int removedoublesflag_exec(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      BMOperator bmop;
 +      /* int count; */ /* UNUSED */
 +
 +      EDBM_InitOpf(em, &bmop, op, "finddoubles verts=%hv dist=%f", 
 +              BM_SELECT, RNA_float_get(op->ptr, "mergedist"));
 +      BMO_Exec_Op(em->bm, &bmop);
 +
 +      /* count = BMO_CountSlotMap(em->bm, &bmop, "targetmapout"); */ /* UNUSED */
 +
 +      if (!EDBM_CallOpf(em, op, "weldverts targetmap=%s", &bmop, "targetmapout")) {
 +              BMO_Finish_Op(em->bm, &bmop);
 +              return OPERATOR_CANCELLED;
 +      }
 +
 +      if (!EDBM_FinishOp(em, &bmop, op, 1))
 +              return OPERATOR_CANCELLED;
 +
 +      /*we need a better way of reporting this, since this doesn't work
 +        with the last operator panel correctly.
 +      if(count)
 +      {
 +              sprintf(msg, "Removed %d vertices", count);
 +              BKE_report(op->reports, RPT_INFO, msg);
 +      }
 +      */
 +
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_remove_doubles(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Remove Doubles";
 +      ot->idname= "MESH_OT_remove_doubles";
 +
 +      /* api callbacks */
 +      ot->exec= removedoublesflag_exec;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      RNA_def_float(ot->srna, "mergedist", 0.0001f, 0.000001f, 50.0f, 
 +              "Merge Distance", 
 +              "Minimum distance between elements to merge", 0.00001, 10.0);
 +}
 +
 +/************************ Vertex Path Operator *************************/
 +
 +typedef struct PathNode {
 +      int u;
 +      int visited;
 +      ListBase edges;
 +} PathNode;
 +
 +typedef struct PathEdge {
 +      struct PathEdge *next, *prev;
 +      int v;
 +      float w;
 +} PathEdge;
 +
 +
 +
 +static int select_vertex_path_exec(bContext *C, wmOperator *op)
 +{
 +      Object *ob = CTX_data_edit_object(C);
 +      BMEditMesh *em = ((Mesh*)ob->data)->edit_btmesh;
 +      BMOperator bmop;
 +      BMEditSelection *sv, *ev;
 +
 +      /* get the type from RNA */
 +      int type = RNA_enum_get(op->ptr, "type");
 +
 +      sv = em->bm->selected.last;
 +      if( sv != NULL )
 +              ev = sv->prev;
 +      else return OPERATOR_CANCELLED;
 +      if( ev == NULL )
 +              return OPERATOR_CANCELLED;
 +
 +      if((sv->htype != BM_VERT) || (ev->htype != BM_VERT))
 +              return OPERATOR_CANCELLED;
 +
 +      /* initialize the bmop using EDBM api, which does various ui error reporting and other stuff */
 +      EDBM_InitOpf(em, &bmop, op, "vertexshortestpath startv=%e endv=%e type=%d", sv->data, ev->data, type);
 +
 +      /* execute the operator */
 +      BMO_Exec_Op(em->bm, &bmop);
 +
 +      /* DO NOT clear the existing selection */
 +      /* EDBM_clear_flag_all(em, BM_SELECT); */
 +
 +      /* select the output */
 +      BMO_HeaderFlag_Buffer(em->bm, &bmop, "vertout", BM_SELECT, BM_ALL);
 +
 +      /* finish the operator */
 +      if( !EDBM_FinishOp(em, &bmop, op, 1) )
 +              return OPERATOR_CANCELLED;
 +
 +      EDBM_selectmode_flush(em);
 +
 +      /* dependencies graph and notification stuff */
 +/*    DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_OBJECT | ND_GEOM_SELECT, ob);
 +*/
 +      DAG_id_tag_update(ob->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data);
 +
 +
 +      /* we succeeded */
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_select_vertex_path(wmOperatorType *ot)
 +{
 +      static const EnumPropertyItem type_items[] = {
 +              {VPATH_SELECT_EDGE_LENGTH, "EDGE_LENGTH", 0, "Edge Length", NULL},
 +              {VPATH_SELECT_TOPOLOGICAL, "TOPOLOGICAL", 0, "Topological", NULL},
 +              {0, NULL, 0, NULL, NULL}};
 +
 +      /* identifiers */
 +      ot->name= "Select Vertex Path";
 +      ot->idname= "MESH_OT_select_vertex_path";
 +
 +      /* api callbacks */
 +      ot->exec= select_vertex_path_exec;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      /* properties */
 +      RNA_def_enum(ot->srna, "type", type_items, VPATH_SELECT_EDGE_LENGTH, "Type", "Method to compute distance");
 +}
 +/********************** Rip Operator *************************/
 +
 +/* helper to find edge for edge_rip */
 +static float mesh_rip_edgedist(ARegion *ar, float mat[][4], float *co1, float *co2, const int mval[2])
 +{
 +      float vec1[3], vec2[3], mvalf[2];
 +
 +      ED_view3d_project_float(ar, co1, vec1, mat);
 +      ED_view3d_project_float(ar, co2, vec2, mat);
 +      mvalf[0]= (float)mval[0];
 +      mvalf[1]= (float)mval[1];
 +
 +      return dist_to_line_segment_v2(mvalf, vec1, vec2);
 +}
 +
 +/* based on mouse cursor position, it defines how is being ripped */
 +static int mesh_rip_invoke(bContext *C, wmOperator *op, wmEvent *event)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      ARegion *ar= CTX_wm_region(C);
 +      View3D *v3d = CTX_wm_view3d(C);
 +      RegionView3D *rv3d= CTX_wm_region_view3d(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      BMesh *bm = em->bm;
 +      BMOperator bmop;
 +      BMBVHTree *bvhtree;
 +      BMOIter siter;
 +      BMIter iter, eiter, liter;
 +      BMLoop *l;
 +      BMEdge *e, *e2, *closest = NULL;
 +      BMVert *v, *ripvert = NULL;
 +      int side = 0, i, singlesel = 0;
 +      float projectMat[4][4], fmval[3] = {event->mval[0], event->mval[1]};
 +      float dist = FLT_MAX, d;
 +
 +      ED_view3d_ob_project_mat_get(rv3d, obedit, projectMat);
 +
 +      BM_ITER(e, &iter, em->bm, BM_EDGES_OF_MESH, NULL) {
 +              if (BM_TestHFlag(e, BM_SELECT)) {
 +                      BM_SetHFlag(e, BM_TMP_TAG);
 +              }
 +              else {
 +                      BM_ClearHFlag(e, BM_TMP_TAG);
 +              }
 +      }
 +
 +      /*handle case of one vert selected.  identify
 +        closest edge around that vert to mouse cursor,
 +        then rip two adjacent edges in the vert fan.*/
 +      if (bm->totvertsel == 1 && bm->totedgesel == 0 && bm->totfacesel == 0) {
 +              singlesel = 1;
 +
 +              /*find selected vert*/
 +              BM_ITER(v, &iter, bm, BM_VERTS_OF_MESH, NULL) {
 +                      if (BM_TestHFlag(v, BM_SELECT))
 +                              break;
 +              }
 +
 +              /*this should be impossible, but sanity checks are a good thing*/
 +              if (!v)
 +                      return OPERATOR_CANCELLED;
 +
 +              if (!v->e || !v->e->l) {
 +                      BKE_report(op->reports, RPT_ERROR, "Selected vertex has no faces");
 +                      return OPERATOR_CANCELLED;
 +              }
 +
 +              /*find closest edge to mouse cursor*/
 +              e2 = NULL;
 +              BM_ITER(e, &iter, bm, BM_EDGES_OF_VERT, v) {
 +                      d = mesh_rip_edgedist(ar, projectMat, e->v1->co, e->v2->co, event->mval);
 +                      if (d < dist) {
 +                              dist = d;
 +                              e2 = e;
 +                      }
 +              }
 +
 +              if (!e2)
 +                      return OPERATOR_CANCELLED;
 +
 +              /*rip two adjacent edges*/
 +              if (BM_Edge_FaceCount(e2) == 1 || BM_Vert_FaceCount(v) == 2) {
 +                      l = e2->l;
 +                      ripvert = BM_Rip_Vertex(bm, l->f, v);
 +
 +                      BLI_assert(ripvert);
 +                      if (!ripvert) {
 +                              return OPERATOR_CANCELLED;
 +                      }
 +              } else if (BM_Edge_FaceCount(e2) == 2) {
 +                      l = e2->l;
 +                      e = BM_OtherFaceLoop(e2, l->f, v)->e;
 +                      BM_SetHFlag(e, BM_TMP_TAG);
 +                      BM_Select(bm, e, TRUE);
 +                      
 +                      l = e2->l->radial_next;
 +                      e = BM_OtherFaceLoop(e2, l->f, v)->e;
 +                      BM_SetHFlag(e, BM_TMP_TAG);
 +                      BM_Select(bm, e, TRUE);
 +              }
 +
 +              dist = FLT_MAX;
 +      } else {
 +              /*expand edge selection*/
 +              BM_ITER(v, &iter, bm, BM_VERTS_OF_MESH, NULL) {
 +                      e2 = NULL;
 +                      i = 0;
 +                      BM_ITER(e, &eiter, bm, BM_EDGES_OF_VERT, v) {
 +                              if (BM_TestHFlag(e, BM_TMP_TAG)) {
 +                                      e2 = e;
 +                                      i++;
 +                              }
 +                      }
 +                      
 +                      if (i == 1 && e2->l) {
 +                              l = BM_OtherFaceLoop(e2, e2->l->f, v);
 +                              l = l->radial_next;
 +                              l = BM_OtherFaceLoop(l->e, l->f, v);
 +
 +                              if (l) {
 +                                      BM_Select(bm, l->e, TRUE);
 +                              }
 +                      }
 +              }
 +      }
 +
 +      if (!EDBM_InitOpf(em, &bmop, op, "edgesplit edges=%he", BM_SELECT)) {
 +              return OPERATOR_CANCELLED;
 +      }
 +      
 +      BMO_Exec_Op(bm, &bmop);
 +
 +      /*build bvh tree for edge visibility tests*/
 +      bvhtree = BMBVH_NewBVH(em, 0, NULL, NULL);
 +
 +      for (i=0; i<2; i++) {
 +              BMO_ITER(e, &siter, bm, &bmop, i ? "edgeout2":"edgeout1", BM_EDGE) {
 +                      float cent[3] = {0, 0, 0}, mid[3], vec[3];
 +
 +                      if (!BMBVH_EdgeVisible(bvhtree, e, ar, v3d, obedit) || !e->l)
 +                              continue;
 +
 +                      /* method for calculating distance:
 +                       *
 +                       * for each edge: calculate face center, then made a vector
 +                       * from edge midpoint to face center.  offset edge midpoint
 +                       * by a small amount along this vector. */
 +                      BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, e->l->f) {
 +                              add_v3_v3(cent, l->v->co);
 +                      }
 +                      mul_v3_fl(cent, 1.0f/(float)e->l->f->len);
 +
 +                      mid_v3_v3v3(mid, e->v1->co, e->v2->co);
 +                      sub_v3_v3v3(vec, cent, mid);
 +                      normalize_v3(vec);
 +                      mul_v3_fl(vec, 0.01f);
 +                      add_v3_v3v3(mid, mid, vec);
 +
 +                      /* yay we have our comparison point, now project it */
 +                      ED_view3d_project_float(ar, mid, mid, projectMat);
 +
 +                      d = len_squared_v2v2(fmval, mid);
 +
 +                      if (d < dist) {
 +                              side = i;
 +                              closest = e;
 +                              dist = d;
 +                      }
 +              }
 +      }
 +      
 +      BMBVH_FreeBVH(bvhtree);
 +
 +      EDBM_clear_flag_all(em, BM_SELECT);
 +      BMO_HeaderFlag_Buffer(bm, &bmop, side?"edgeout2":"edgeout1", BM_SELECT, BM_EDGE);
 +
 +      BM_ITER(e, &iter, bm, BM_EDGES_OF_MESH, NULL) {
 +              if (BM_TestHFlag(e, BM_SELECT)) {
 +                      BM_SetHFlag(e, BM_TMP_TAG);
 +              }
 +              else {
 +                      BM_ClearHFlag(e, BM_TMP_TAG);
 +              }
 +      }
 +
 +      /*constrict edge selection again*/
 +      BM_ITER(v, &iter, bm, BM_VERTS_OF_MESH, NULL) {
 +              e2 = NULL;
 +              i = 0;
 +              BM_ITER(e, &eiter, bm, BM_EDGES_OF_VERT, v) {
 +                      if (BM_TestHFlag(e, BM_TMP_TAG)) {
 +                              e2 = e;
 +                              i++;
 +                      }
 +              }
 +              
 +              if (i == 1)  {
 +                      if (singlesel)
 +                              BM_Select(bm, v, FALSE);
 +                      else
 +                              BM_Select(bm, e2, FALSE);
 +              }
 +      }
 +
 +      if (ripvert) {
 +              BM_Select(bm, ripvert, TRUE);
 +      }
 +
 +      EDBM_selectmode_flush(em);
 +
 +      BLI_assert(singlesel ? (bm->totvertsel > 0) : (bm->totedgesel > 0));
 +
 +      if (!EDBM_FinishOp(em, &bmop, op, 1)) {
 +              return OPERATOR_CANCELLED;
 +      }
 +
 +      if (bm->totvertsel == 0) {
 +              return OPERATOR_CANCELLED;
 +      }
 +      
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_rip(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Rip";
 +      ot->idname= "MESH_OT_rip";
 +
 +      /* api callbacks */
 +      ot->invoke= mesh_rip_invoke;
 +      ot->poll= EM_view3d_poll;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      /* to give to transform */
 +      Transform_Properties(ot, P_PROPORTIONAL);
 +      RNA_def_boolean(ot->srna, "mirror", 0, "Mirror Editing", "");
 +}
 +
 +/************************ Shape Operators *************************/
 +
 +/*BMESH_TODO this should be properly encapsulated in a bmop.  but later.*/
 +static void shape_propagate(Object *obedit, BMEditMesh *em, wmOperator *op)
 +{
 +      BMIter iter;
 +      BMVert *eve = NULL;
 +      float *co;
 +      int i, totshape = CustomData_number_of_layers(&em->bm->vdata, CD_SHAPEKEY);
 +
 +      if (!CustomData_has_layer(&em->bm->vdata, CD_SHAPEKEY)) {
 +              BKE_report(op->reports, RPT_ERROR, "Mesh does not have shape keys");
 +              return;
 +      }
 +      
 +      BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
 +              if (!BM_TestHFlag(eve, BM_SELECT) || BM_TestHFlag(eve, BM_HIDDEN))
 +                      continue;
 +
 +              for (i=0; i<totshape; i++) {
 +                      co = CustomData_bmesh_get_n(&em->bm->vdata, eve->head.data, CD_SHAPEKEY, i);
 +                      copy_v3_v3(co, eve->co);
 +              }
 +      }
 +
 +#if 0
 +      //TAG Mesh Objects that share this data
 +      for(base = scene->base.first; base; base = base->next) {
 +              if(base->object && base->object->data == me) {
 +                      base->object->recalc = OB_RECALC_DATA;
 +              }
 +      }
 +#endif
 +
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +}
 +
 +
 +static int shape_propagate_to_all_exec(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      Mesh *me= obedit->data;
 +      BMEditMesh *em= me->edit_btmesh;
 +
 +      shape_propagate(obedit, em, op);
 +
 +      DAG_id_tag_update(&me->id, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, me);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +
 +void MESH_OT_shape_propagate_to_all(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Shape Propagate";
 +      ot->description= "Apply selected vertex locations to all other shape keys";
 +      ot->idname= "MESH_OT_shape_propagate_to_all";
 +
 +      /* api callbacks */
 +      ot->exec= shape_propagate_to_all_exec;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +}
 +
 +/*BMESH_TODO this should be properly encapsulated in a bmop.  but later.*/
 +static int blend_from_shape_exec(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      Mesh *me= obedit->data;
 +      BMEditMesh *em= me->edit_btmesh;
 +      BMVert *eve;
 +      BMIter iter;
 +      float co[3], *sco;
 +      float blend= RNA_float_get(op->ptr, "blend");
 +      int shape= RNA_enum_get(op->ptr, "shape");
 +      int add= RNA_boolean_get(op->ptr, "add");
 +      int totshape;
 +
 +      /*sanity check*/
 +      totshape = CustomData_number_of_layers(&em->bm->vdata, CD_SHAPEKEY);
 +      if (totshape == 0 || shape < 0 || shape >= totshape)
 +              return OPERATOR_CANCELLED;
 +
 +      BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
 +              if (!BM_TestHFlag(eve, BM_SELECT) || BM_TestHFlag(eve, BM_HIDDEN))
 +                      continue;
 +
 +              sco = CustomData_bmesh_get_n(&em->bm->vdata, eve->head.data, CD_SHAPEKEY, shape);
 +              copy_v3_v3(co, sco);
 +
 +
 +              if(add) {
 +                      mul_v3_fl(co, blend);
 +                      add_v3_v3v3(eve->co, eve->co, co);
 +              }
 +              else
 +                      interp_v3_v3v3(eve->co, eve->co, co, blend);
 +              
 +              copy_v3_v3(sco, co);
 +      }
 +
 +      DAG_id_tag_update(&me->id, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, me);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +static EnumPropertyItem *shape_itemf(bContext *C, PointerRNA *UNUSED(ptr),  PropertyRNA *UNUSED(prop), int *free)
 +{     
 +      Object *obedit= CTX_data_edit_object(C);
 +      Mesh *me= (obedit) ? obedit->data : NULL;
 +      BMEditMesh *em = me->edit_btmesh;
 +      EnumPropertyItem *item= NULL;
 +      int totitem= 0;
 +
 +      if(obedit && obedit->type == OB_MESH && CustomData_has_layer(&em->bm->vdata, CD_SHAPEKEY)) {
 +              EnumPropertyItem tmp= {0, "", 0, "", ""};
 +              int a;
 +
 +              for (a=0; a<em->bm->vdata.totlayer; a++) {
 +                      if (em->bm->vdata.layers[a].type != CD_SHAPEKEY)
 +                              continue;
 +
 +                      tmp.value= totitem;
 +                      tmp.identifier= em->bm->vdata.layers[a].name;
 +                      tmp.name= em->bm->vdata.layers[a].name;
 +                      RNA_enum_item_add(&item, &totitem, &tmp);
 +
 +                      totitem++;
 +              }
 +      }
 +
 +      RNA_enum_item_end(&item, &totitem);
 +      *free= 1;
 +
 +      return item;
 +}
 +
 +void MESH_OT_blend_from_shape(wmOperatorType *ot)
 +{
 +      PropertyRNA *prop;
 +      static EnumPropertyItem shape_items[]= {{0, NULL, 0, NULL, NULL}};
 +
 +      /* identifiers */
 +      ot->name= "Blend From Shape";
 +      ot->description= "Blend in shape from a shape key";
 +      ot->idname= "MESH_OT_blend_from_shape";
 +
 +      /* api callbacks */
 +      ot->exec= blend_from_shape_exec;
 +      ot->invoke= WM_operator_props_popup;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      /* properties */
 +      prop= RNA_def_enum(ot->srna, "shape", shape_items, 0, "Shape", "Shape key to use for blending");
 +      RNA_def_enum_funcs(prop, shape_itemf);
 +      RNA_def_float(ot->srna, "blend", 1.0f, -FLT_MAX, FLT_MAX, "Blend", "Blending factor", -2.0f, 2.0f);
 +      RNA_def_boolean(ot->srna, "add", 1, "Add", "Add rather then blend between shapes");
 +}
 +
 +/* BMESH_TODO - some way to select on an arbitrary axis */
 +static int select_axis_exec(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      BMEditSelection *ese = em->bm->selected.last;
 +      int axis= RNA_enum_get(op->ptr, "axis");
 +      int mode= RNA_enum_get(op->ptr, "mode"); /* -1==aligned, 0==neg, 1==pos*/
 +
 +      if (ese==NULL || ese->htype != BM_VERT) {
 +              BKE_report(op->reports, RPT_WARNING, "This operator requires an active vertex (last selected)");
 +              return OPERATOR_CANCELLED;
 +      }
 +      else {
 +              BMVert *ev, *act_vert= (BMVert*)ese->data;
 +              BMIter iter;
 +              float value= act_vert->co[axis];
 +              float limit=  CTX_data_tool_settings(C)->doublimit; // XXX
 +
 +              if(mode==0)
 +                      value -= limit;
 +              else if (mode==1) 
 +                      value += limit;
 +
 +              BM_ITER(ev, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
 +                      if(!BM_TestHFlag(ev, BM_HIDDEN)) {
 +                              switch(mode) {
 +                              case -1: /* aligned */
 +                                      if(fabs(ev->co[axis] - value) < limit)
 +                                              BM_Select(em->bm, ev, TRUE);
 +                                      break;
 +                              case 0: /* neg */
 +                                      if(ev->co[axis] > value)
 +                                              BM_Select(em->bm, ev, TRUE);
 +                                      break;
 +                              case 1: /* pos */
 +                                      if(ev->co[axis] < value)
 +                                              BM_Select(em->bm, ev, TRUE);
 +                                      break;
 +                              }
 +                      }
 +              }
 +      }
 +
 +      EDBM_selectmode_flush(em);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_select_axis(wmOperatorType *ot)
 +{
 +      static EnumPropertyItem axis_mode_items[] = {
 +              {0,  "POSITIVE", 0, "Positive Axis", ""},
 +              {1,  "NEGATIVE", 0, "Negative Axis", ""},
 +              {-1, "ALIGNED",  0, "Aligned Axis", ""},
 +              {0, NULL, 0, NULL, NULL}};
 +
 +      static EnumPropertyItem axis_items_xyz[] = {
 +              {0, "X_AXIS", 0, "X Axis", ""},
 +              {1, "Y_AXIS", 0, "Y Axis", ""},
 +              {2, "Z_AXIS", 0, "Z Axis", ""},
 +              {0, NULL, 0, NULL, NULL}};
 +
 +      /* identifiers */
 +      ot->name= "Select Axis";
 +      ot->description= "Select all data in the mesh on a single axis";
 +      ot->idname= "MESH_OT_select_axis";
 +
 +      /* api callbacks */
 +      ot->exec= select_axis_exec;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      /* properties */
 +      RNA_def_enum(ot->srna, "mode", axis_mode_items, 0, "Axis Mode", "Axis side to use when selecting");
 +      RNA_def_enum(ot->srna, "axis", axis_items_xyz, 0, "Axis", "Select the axis to compare each vertex on");
 +}
 +
 +static int solidify_exec(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      Mesh *me = obedit->data;
 +      BMEditMesh *em = me->edit_btmesh;
 +      BMesh *bm = em->bm;
 +      BMOperator bmop;
 +
 +      float thickness= RNA_float_get(op->ptr, "thickness");
 +
 +      if (!EDBM_InitOpf(em, &bmop, op, "solidify geom=%hf thickness=%f", BM_SELECT, thickness)) {
 +              return OPERATOR_CANCELLED;
 +      }
 +
 +      /* deselect only the faces in the region to be solidified (leave wire
 +         edges and loose verts selected, as there will be no corresponding
 +         geometry selected below) */
 +      BMO_UnHeaderFlag_Buffer(bm, &bmop, "geom", BM_SELECT, BM_FACE);
 +
 +      /* run the solidify operator */
 +      BMO_Exec_Op(bm, &bmop);
 +
 +      /* select the newly generated faces */
 +      BMO_HeaderFlag_Buffer(bm, &bmop, "geomout", BM_SELECT, BM_FACE);
 +
 +      if (!EDBM_FinishOp(em, &bmop, op, 1)) {
 +              return OPERATOR_CANCELLED;
 +      }
 +
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +
 +void MESH_OT_solidify(wmOperatorType *ot)
 +{
 +      PropertyRNA *prop;
 +      /* identifiers */
 +      ot->name= "Solidify";
 +      ot->description= "Create a solid skin by extruding, compensating for sharp angles";
 +      ot->idname= "MESH_OT_solidify";
 +
 +      /* api callbacks */
 +      ot->exec= solidify_exec;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      prop= RNA_def_float(ot->srna, "thickness", 0.01f, -FLT_MAX, FLT_MAX, "thickness", "", -10.0f, 10.0f);
 +      RNA_def_property_ui_range(prop, -10, 10, 0.1, 4);
 +}
 +
 +#define TRAIL_POLYLINE 1 /* For future use, They don't do anything yet */
 +#define TRAIL_FREEHAND 2
 +#define TRAIL_MIXED    3 /* (1|2) */
 +#define TRAIL_AUTO     4 
 +#define       TRAIL_MIDPOINTS 8
 +
 +typedef struct CutCurve {
 +      float  x; 
 +      float  y;
 +} CutCurve;
 +
 +/* ******************************************************************** */
 +/* Knife Subdivide Tool.  Subdivides edges intersected by a mouse trail
 +      drawn by user.
 +      
 +      Currently mapped to KKey when in MeshEdit mode.
 +      Usage:
 +              Hit Shift K, Select Centers or Exact
 +              Hold LMB down to draw path, hit RETKEY.
 +              ESC cancels as expected.
 +   
 +      Contributed by Robert Wenzlaff (Det. Thorn).
 +
 +    2.5 revamp:
 +    - non modal (no menu before cutting)
 +    - exit on mouse release
 +    - polygon/segment drawing can become handled by WM cb later
 +
 +      bmesh port version
 +*/
 +
 +#define KNIFE_EXACT           1
 +#define KNIFE_MIDPOINT        2
 +#define KNIFE_MULTICUT        3
 +
 +static EnumPropertyItem knife_items[]= {
 +      {KNIFE_EXACT, "EXACT", 0, "Exact", ""},
 +      {KNIFE_MIDPOINT, "MIDPOINTS", 0, "Midpoints", ""},
 +      {KNIFE_MULTICUT, "MULTICUT", 0, "Multicut", ""},
 +      {0, NULL, 0, NULL, NULL}
 +};
 +
 +/* seg_intersect() Determines if and where a mouse trail intersects an EditEdge */
 +
 +static float bm_seg_intersect(BMEdge *e, CutCurve *c, int len, char mode,
 +                              struct GHash *gh, int *isected)
 +{
 +#define MAXSLOPE 100000
 +      float  x11, y11, x12=0, y12=0, x2max, x2min, y2max;
 +      float  y2min, dist, lastdist=0, xdiff2, xdiff1;
 +      float  m1, b1, m2, b2, x21, x22, y21, y22, xi;
 +      float  yi, x1min, x1max, y1max, y1min, perc=0; 
 +      float  *scr;
 +      float  threshold = 0.0;
 +      int  i;
 +      
 +      //threshold = 0.000001; /*tolerance for vertex intersection*/
 +      // XXX  threshold = scene->toolsettings->select_thresh / 100;
 +      
 +      /* Get screen coords of verts */
 +      scr = BLI_ghash_lookup(gh, e->v1);
 +      x21=scr[0];
 +      y21=scr[1];
 +      
 +      scr = BLI_ghash_lookup(gh, e->v2);
 +      x22=scr[0];
 +      y22=scr[1];
 +      
 +      xdiff2=(x22-x21);  
 +      if (xdiff2) {
 +              m2=(y22-y21)/xdiff2;
 +              b2= ((x22*y21)-(x21*y22))/xdiff2;
 +      }
 +      else {
 +              m2=MAXSLOPE;  /* Verticle slope  */
 +              b2=x22;      
 +      }
 +
 +      *isected = 0;
 +
 +      /*check for *exact* vertex intersection first*/
 +      if(mode!=KNIFE_MULTICUT) {
 +              for (i=0; i<len; i++) {
 +                      if (i>0) {
 +                              x11=x12;
 +                              y11=y12;
 +                      }
 +                      else {
 +                              x11=c[i].x;
 +                              y11=c[i].y;
 +                      }
 +                      x12=c[i].x;
 +                      y12=c[i].y;
 +                      
 +                      /*test e->v1*/
 +                      if((x11 == x21 && y11 == y21) || (x12 == x21 && y12 == y21)) {
 +                              perc = 0;
 +                              *isected = 1;
 +                              return(perc);
 +                      }
 +                      /*test e->v2*/
 +                      else if((x11 == x22 && y11 == y22) || (x12 == x22 && y12 == y22)) {
 +                              perc = 0;
 +                              *isected = 2;
 +                              return(perc);
 +                      }
 +              }
 +      }
 +      
 +      /*now check for edge interesect (may produce vertex intersection as well)*/
 +      for (i=0; i<len; i++) {
 +              if (i>0) {
 +                      x11=x12;
 +                      y11=y12;
 +              }
 +              else {
 +                      x11=c[i].x;
 +                      y11=c[i].y;
 +              }
 +              x12=c[i].x;
 +              y12=c[i].y;
 +              
 +              /* Perp. Distance from point to line */
 +              if (m2!=MAXSLOPE) dist=(y12-m2*x12-b2);/* /sqrt(m2*m2+1); Only looking for */
 +                      /* change in sign.  Skip extra math */  
 +              else dist=x22-x12;      
 +              
 +              if (i==0) lastdist=dist;
 +              
 +              /* if dist changes sign, and intersect point in edge's Bound Box*/
 +              if ((lastdist*dist)<=0) {
 +                      xdiff1=(x12-x11); /* Equation of line between last 2 points */
 +                      if (xdiff1) {
 +                              m1=(y12-y11)/xdiff1;
 +                              b1= ((x12*y11)-(x11*y12))/xdiff1;
 +                      }
 +                      else{
 +                              m1=MAXSLOPE;
 +                              b1=x12;
 +                      }
 +                      x2max=MAX2(x21,x22)+0.001; /* prevent missed edges   */
 +                      x2min=MIN2(x21,x22)-0.001; /* due to round off error */
 +                      y2max=MAX2(y21,y22)+0.001;
 +                      y2min=MIN2(y21,y22)-0.001;
 +                      
 +                      /* Found an intersect,  calc intersect point */
 +                      if (m1==m2) { /* co-incident lines */
 +                              /* cut at 50% of overlap area*/
 +                              x1max=MAX2(x11, x12);
 +                              x1min=MIN2(x11, x12);
 +                              xi= (MIN2(x2max,x1max)+MAX2(x2min,x1min))/2.0;  
 +                              
 +                              y1max=MAX2(y11, y12);
 +                              y1min=MIN2(y11, y12);
 +                              yi= (MIN2(y2max,y1max)+MAX2(y2min,y1min))/2.0;
 +                      }                       
 +                      else if (m2==MAXSLOPE) {
 +                              xi=x22;
 +                              yi=m1*x22+b1;
 +                      }
 +                      else if (m1==MAXSLOPE) {
 +                              xi=x12;
 +                              yi=m2*x12+b2;
 +                      }
 +                      else {
 +                              xi=(b1-b2)/(m2-m1);
 +                              yi=(b1*m2-m1*b2)/(m2-m1);
 +                      }
 +                      
 +                      /* Intersect inside bounding box of edge?*/
 +                      if ((xi>=x2min)&&(xi<=x2max)&&(yi<=y2max)&&(yi>=y2min)) {
 +                              /*test for vertex intersect that may be 'close enough'*/
 +                              if(mode!=KNIFE_MULTICUT) {
 +                                      if(xi <= (x21 + threshold) && xi >= (x21 - threshold)) {
 +                                              if(yi <= (y21 + threshold) && yi >= (y21 - threshold)) {
 +                                                      *isected = 1;
 +                                                      perc = 0;
 +                                                      break;
 +                                              }
 +                                      }
 +                                      if(xi <= (x22 + threshold) && xi >= (x22 - threshold)) {
 +                                              if(yi <= (y22 + threshold) && yi >= (y22 - threshold)) {
 +                                                      *isected = 2;
 +                                                      perc = 0;
 +                                                      break;
 +                                              }
 +                                      }
 +                              }
 +                              if ((m2<=1.0)&&(m2>=-1.0)) perc = (xi-x21)/(x22-x21);   
 +                              else perc=(yi-y21)/(y22-y21); /*lower slope more accurate*/
 +                              //isect=32768.0*(perc+0.0000153); /* Percentage in 1/32768ths */
 +                              
 +                              break;
 +                      }
 +              }       
 +              lastdist=dist;
 +      }
 +      return(perc);
 +} 
 +
 +#define MAX_CUTS 2048
 +
 +static int knife_cut_exec(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= (((Mesh *)obedit->data))->edit_btmesh;
 +      BMesh *bm = em->bm;
 +      ARegion *ar= CTX_wm_region(C);
 +      BMVert *bv;
 +      BMIter iter;
 +      BMEdge *be;
 +      BMOperator bmop;
 +      CutCurve curve[MAX_CUTS];
 +      struct GHash *gh;
 +      float isect=0.0;
 +      float  *scr, co[4];
 +      int len=0, isected;
 +      short numcuts=1, mode= RNA_int_get(op->ptr, "type");
 +      
 +      /* edit-object needed for matrix, and ar->regiondata for projections to work */
 +      if (ELEM3(NULL, obedit, ar, ar->regiondata))
 +              return OPERATOR_CANCELLED;
 +      
 +      if (bm->totvertsel < 2) {
 +              //error("No edges are selected to operate on");
 +              return OPERATOR_CANCELLED;;
 +      }
 +
 +      /* get the cut curve */
 +      RNA_BEGIN(op->ptr, itemptr, "path") {
 +              
 +              RNA_float_get_array(&itemptr, "loc", (float *)&curve[len]);
 +              len++;
 +              if(len>= MAX_CUTS) break;
 +      }
 +      RNA_END;
 +      
 +      if(len<2) {
 +              return OPERATOR_CANCELLED;
 +      }
 +
 +      /*the floating point coordinates of verts in screen space will be stored in a hash table according to the vertices pointer*/
 +      gh = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "knife cut exec");
 +      for(bv=BMIter_New(&iter, bm, BM_VERTS_OF_MESH, NULL);bv;bv=BMIter_Step(&iter)) {
 +              scr = MEM_mallocN(sizeof(float)*2, "Vertex Screen Coordinates");
 +              copy_v3_v3(co, bv->co);
 +              co[3]= 1.0;
 +              mul_m4_v4(obedit->obmat, co);
 +              project_float(ar, co, scr);
 +              BLI_ghash_insert(gh, bv, scr);
 +      }
 +
 +      if (!EDBM_InitOpf(em, &bmop, op, "esubd")) {
 +              return OPERATOR_CANCELLED;
 +      }
 +
 +      /*store percentage of edge cut for KNIFE_EXACT here.*/
 +      for (be=BMIter_New(&iter, bm, BM_EDGES_OF_MESH, NULL); be; be=BMIter_Step(&iter)) {
 +              if( BM_Selected(bm, be) ) {
 +                      isect= bm_seg_intersect(be, curve, len, mode, gh, &isected);
 +                      
 +                      if (isect != 0.0f) {
 +                              if (mode != KNIFE_MULTICUT && mode != KNIFE_MIDPOINT) {
 +                                      BMO_Insert_MapFloat(bm, &bmop, 
 +                                             "edgepercents",
 +                                             be, isect);
 +
 +                              }
 +                              BMO_SetFlag(bm, be, 1);
 +                      } else BMO_ClearFlag(bm, be, 1);
 +              } else BMO_ClearFlag(bm, be, 1);
 +      }
 +      
 +      BMO_Flag_To_Slot(bm, &bmop, "edges", 1, BM_EDGE);
 +
 +      if (mode == KNIFE_MIDPOINT) numcuts = 1;
 +      BMO_Set_Int(&bmop, "numcuts", numcuts);
 +
 +      BMO_Set_Int(&bmop, "flag", B_KNIFE);
 +      BMO_Set_Int(&bmop, "quadcornertype", SUBD_STRAIGHT_CUT);
 +      BMO_Set_Int(&bmop, "singleedge", 0);
 +      BMO_Set_Int(&bmop, "gridfill", 0);
 +
 +      BMO_Set_Float(&bmop, "radius", 0);
 +      
 +      BMO_Exec_Op(bm, &bmop);
 +      if (!EDBM_FinishOp(em, &bmop, op, 1)) {
 +              return OPERATOR_CANCELLED;
 +      }
 +      
 +      BLI_ghash_free(gh, NULL, (GHashValFreeFP)MEM_freeN);
 +
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_knife_cut(wmOperatorType *ot)
 +{
 +      PropertyRNA *prop;
 +      
 +      ot->name= "Knife Cut";
 +      ot->description= "Cut selected edges and faces into parts";
 +      ot->idname= "MESH_OT_knife_cut";
 +      
 +      ot->invoke= WM_gesture_lines_invoke;
 +      ot->modal= WM_gesture_lines_modal;
 +      ot->exec= knife_cut_exec;
 +      
 +      ot->poll= EM_view3d_poll;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +      
 +      RNA_def_enum(ot->srna, "type", knife_items, KNIFE_EXACT, "Type", "");
 +      prop= RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
 +      RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
 +      
 +      /* internal */
 +      RNA_def_int(ot->srna, "cursor", BC_KNIFECURSOR, 0, INT_MAX, "Cursor", "", 0, INT_MAX);
 +}
 +
 +static int mesh_separate_selected(Main *bmain, Scene *scene, Base *editbase, wmOperator *wmop)
 +{
 +      Base *basenew;
 +      BMIter iter;
 +      BMVert *v;
 +      BMEdge *e;
 +      Object *obedit = editbase->object;
 +      Mesh *me = obedit->data;
 +      BMEditMesh *em = me->edit_btmesh;
 +      BMesh *bmnew;
 +      int allocsize[] = {512, 512, 2048, 512};
 +      
 +      if (!em)
 +              return OPERATOR_CANCELLED;
 +              
 +      bmnew = BM_Make_Mesh(obedit, allocsize);
 +      CustomData_copy(&em->bm->vdata, &bmnew->vdata, CD_MASK_BMESH, CD_CALLOC, 0);
 +      CustomData_copy(&em->bm->edata, &bmnew->edata, CD_MASK_BMESH, CD_CALLOC, 0);
 +      CustomData_copy(&em->bm->ldata, &bmnew->ldata, CD_MASK_BMESH, CD_CALLOC, 0);
 +      CustomData_copy(&em->bm->pdata, &bmnew->pdata, CD_MASK_BMESH, CD_CALLOC, 0);
 +
 +      CustomData_bmesh_init_pool(&bmnew->vdata, allocsize[0]);
 +      CustomData_bmesh_init_pool(&bmnew->edata, allocsize[1]);
 +      CustomData_bmesh_init_pool(&bmnew->ldata, allocsize[2]);
 +      CustomData_bmesh_init_pool(&bmnew->pdata, allocsize[3]);
 +              
 +      basenew= ED_object_add_duplicate(bmain, scene, editbase, USER_DUP_MESH);        /* 0 = fully linked */
 +      assign_matarar(basenew->object, give_matarar(obedit), *give_totcolp(obedit)); /* new in 2.5 */
 +
 +      ED_base_object_select(basenew, BA_DESELECT);
 +      
 +      EDBM_CallOpf(em, wmop, "dupe geom=%hvef dest=%p", BM_SELECT, bmnew);
 +      EDBM_CallOpf(em, wmop, "del geom=%hvef context=%i", BM_SELECT, DEL_FACES);
 +
 +      /*clean up any loose edges*/
 +      BM_ITER(e, &iter, em->bm, BM_EDGES_OF_MESH, NULL) {
 +              if (BM_TestHFlag(e, BM_HIDDEN))
 +                      continue;
 +
 +              if (BM_Edge_FaceCount(e) != 0) {
 +                      BM_Select(em->bm, e, FALSE);
 +              }
 +      }
 +      EDBM_CallOpf(em, wmop, "del geom=%hvef context=%i", BM_SELECT, DEL_EDGES);
 +
 +      /*clean up any loose verts*/
 +      BM_ITER(v, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
 +              if (BM_TestHFlag(v, BM_HIDDEN))
 +                      continue;
 +
 +              if (BM_Vert_EdgeCount(v) != 0) {
 +                      BM_Select(em->bm, v, FALSE);
 +              }
 +      }
 +
 +      EDBM_CallOpf(em, wmop, "del geom=%hvef context=%i", BM_SELECT, DEL_VERTS);
 +
 +      BM_Compute_Normals(bmnew);
 +      BMO_CallOpf(bmnew, "bmesh_to_mesh mesh=%p object=%p", basenew->object->data, basenew->object);
 +              
 +      BM_Free_Mesh(bmnew);
 +      ((Mesh*)basenew->object->data)->edit_btmesh = NULL;
 +      
 +      return 1;
 +}
 +
 +//BMESH_TODO
 +static int mesh_separate_material(Main *UNUSED(bmain), Scene *UNUSED(scene), Base *UNUSED(editbase), wmOperator *UNUSED(wmop))
 +{
 +      return 0;
 +}
 +
 +static int mesh_separate_loose(Main *bmain, Scene *scene, Base *editbase, wmOperator *wmop)
 +{
 +      int i;
 +      BMVert *v;
 +      BMEdge *e;
 +      BMVert *v_seed;
 +      BMWalker walker;
 +      BMIter iter;
 +      int result = 0;
 +      Object *obedit = editbase->object;
 +      Mesh *me = obedit->data;
 +      BMEditMesh *em = me->edit_btmesh;
 +      BMesh *bm = em->bm;
 +      int max_iter = bm->totvert;
 +
 +      /*Clear all selected vertices*/
 +      BM_ITER(v, &iter, bm, BM_VERTS_OF_MESH, NULL) {
 +              BM_Select(bm, v, FALSE);
 +      }
 +
 +      /*Flush the selection to clear edge/face selections to match
 +        selected vertices*/
 +      EDBM_select_flush(em, SCE_SELECT_VERTEX);
 +
 +      /*A "while(true)" loop should work here as each iteration should
 +        select and remove at least one vertex and when all vertices
 +        are selected the loop will break out. But guard against bad
 +        behavior by limiting iterations to the number of vertices in the
 +        original mesh.*/
 +      for(i=0; i<max_iter; i++)
 +      {
 +              /* Get a seed vertex to start the walk */
 +              v_seed = NULL;
 +              BM_ITER(v, &iter, bm, BM_VERTS_OF_MESH, NULL) {
 +                      v_seed = v;
 +                      break;
 +              }
 +
 +              /* No vertices available, can't do anything */
 +              if (v_seed == NULL)
 +              {
 +                      break;
 +              }
 +
 +              /*Select the seed explicitly, in case it has no edges*/
 +              BM_Select(bm, v_seed, TRUE);
 +
 +              /*Walk from the single vertex, selecting everything connected
 +                to it*/
 +              BMW_Init(&walker, bm, BMW_SHELL,  0,0,0,0,  BMW_NIL_LAY);
 +              e = BMW_Begin(&walker, v_seed);
 +              for (; e; e=BMW_Step(&walker)) {
 +                      BM_Select(bm, e->v1, TRUE);
 +                      BM_Select(bm, e->v2, TRUE);
 +              }
 +              BMW_End(&walker);
 +                              
 +              /*Flush the selection to get edge/face selections matching
 +                the vertex selection*/
 +              EDBM_select_flush(em, SCE_SELECT_VERTEX);
 +
 +              if (bm->totvert == bm->totvertsel)
 +              {
 +                      /*Every vertex selected, nothing to separate, work is done*/
 +                      break;
 +              }
 +
 +              /*Move selection into a separate object*/
 +              result |= mesh_separate_selected(bmain, scene, editbase, wmop);
 +      }
 +
 +      return result;
 +}
 +
 +static int mesh_separate_exec(bContext *C, wmOperator *op)
 +{
 +      Main *bmain = CTX_data_main(C);
 +      Scene *scene= CTX_data_scene(C);
 +      Base *base= CTX_data_active_base(C);
 +      int retval= 0, type= RNA_enum_get(op->ptr, "type");
 +      
 +      if(type == 0)
 +              retval= mesh_separate_selected(bmain, scene, base, op);
 +      else if(type == 1)
 +              retval= mesh_separate_material (bmain, scene, base, op);
 +      else if(type == 2)
 +              retval= mesh_separate_loose(bmain, scene, base, op);
 +         
 +      if(retval) {
 +              DAG_id_tag_update(base->object->data, OB_RECALC_DATA);
 +              WM_event_add_notifier(C, NC_GEOM|ND_DATA, base->object->data);
 +              return OPERATOR_FINISHED;
 +      }
 +
 +      return OPERATOR_CANCELLED;
 +}
 +
 +/* *************** Operator: separate parts *************/
 +
 +static EnumPropertyItem prop_separate_types[] = {
 +      {0, "SELECTED", 0, "Selection", ""},
 +      {1, "MATERIAL", 0, "By Material", ""},
 +      {2, "LOOSE", 0, "By loose parts", ""},
 +      {0, NULL, 0, NULL, NULL}
 +};
 +
 +void MESH_OT_separate(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Separate";
 +      ot->description= "Separate selected geometry into a new mesh";
 +      ot->idname= "MESH_OT_separate";
 +      
 +      /* api callbacks */
 +      ot->invoke= WM_menu_invoke;
 +      ot->exec= mesh_separate_exec;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_UNDO;
 +      
 +      ot->prop = RNA_def_enum(ot->srna, "type", prop_separate_types, 0, "Type", "");
 +}
 +
 +
 +static int fill_mesh_exec(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      BMOperator bmop;
 +      
 +      if (!EDBM_InitOpf(em, &bmop, op, "triangle_fill edges=%he", BM_SELECT))
 +              return OPERATOR_CANCELLED;
 +      
 +      BMO_Exec_Op(em->bm, &bmop);
 +      
 +      /*select new geometry*/
 +      BMO_HeaderFlag_Buffer(em->bm, &bmop, "geomout", BM_SELECT, BM_FACE|BM_EDGE);
 +      
 +      if (!EDBM_FinishOp(em, &bmop, op, 1))
 +              return OPERATOR_CANCELLED;
 +      
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +      
 +      return OPERATOR_FINISHED;
 +
 +}
 +
 +void MESH_OT_fill(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Fill";
 +      ot->idname= "MESH_OT_fill";
 +
 +      /* api callbacks */
 +      ot->exec= fill_mesh_exec;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +}
 +
 +static int beautify_fill_exec(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +
 +      if (!EDBM_CallOpf(em, op, "beautify_fill faces=%hf", BM_SELECT))
 +              return OPERATOR_CANCELLED;
 +
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +      
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_beautify_fill(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Beautify Fill";
 +      ot->idname= "MESH_OT_beautify_fill";
 +
 +      /* api callbacks */
 +      ot->exec= beautify_fill_exec;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +}
 +
 +/********************** Quad/Tri Operators *************************/
 +
 +static int quads_convert_to_tris_exec(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +
 +      if (!EDBM_CallOpf(em, op, "triangulate faces=%hf", BM_SELECT))
 +              return OPERATOR_CANCELLED;
 +
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_quads_convert_to_tris(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Quads to Tris";
 +      ot->idname= "MESH_OT_quads_convert_to_tris";
 +
 +      /* api callbacks */
 +      ot->exec= quads_convert_to_tris_exec;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +}
 +
 +static int tris_convert_to_quads_exec(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      int dosharp, douvs, dovcols, domaterials;
 +      float limit = RNA_float_get(op->ptr, "limit");
 +
 +      dosharp = RNA_boolean_get(op->ptr, "sharp");
 +      douvs = RNA_boolean_get(op->ptr, "uvs");
 +      dovcols = RNA_boolean_get(op->ptr, "vcols");
 +      domaterials = RNA_boolean_get(op->ptr, "materials");
 +
 +      if (!EDBM_CallOpf(em, op, 
 +              "join_triangles faces=%hf limit=%f compare_sharp=%i compare_uvs=%i compare_vcols=%i compare_materials=%i", 
 +              BM_SELECT, limit, dosharp, douvs, dovcols, domaterials)) 
 +      {
 +              return OPERATOR_CANCELLED;
 +      }
 +
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_tris_convert_to_quads(wmOperatorType *ot)
 +{
 +      PropertyRNA *prop;
 +
 +      /* identifiers */
 +      ot->name= "Tris to Quads";
 +      ot->idname= "MESH_OT_tris_convert_to_quads";
 +
 +      /* api callbacks */
 +      ot->exec= tris_convert_to_quads_exec;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      prop= RNA_def_float_rotation(ot->srna, "limit", 0, NULL, 0.0f, DEG2RADF(180.0f), "Max Angle", "Angle Limit in Degrees", 0.0f, DEG2RADF(180.0f));
 +      RNA_def_property_float_default(prop, DEG2RADF(40.0f));
 +
 +      RNA_def_boolean(ot->srna, "uvs", 0, "Compare UVs", "");
 +      RNA_def_boolean(ot->srna, "vcols", 0, "Compare VCols", "");
 +      RNA_def_boolean(ot->srna, "sharp", 0, "Compare Sharp", "");
 +      RNA_def_boolean(ot->srna, "materials", 0, "Compare Materials", "");
 +
 +}
 +
 +static int edge_flip_exec(bContext *UNUSED(C), wmOperator *UNUSED(op))
 +{
 +#if 0
 +      Object *obedit= CTX_data_edit_object(C);
 +      EditMesh *em= BKE_mesh_get_editmesh((Mesh *)obedit->data);
 +
 +      edge_flip(em);
 +
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      BKE_mesh_end_editmesh(obedit->data, em);
 +#endif
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_edge_flip(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Edge Flip";
 +      ot->idname= "MESH_OT_edge_flip";
 +
 +      /* api callbacks */
 +      ot->exec= edge_flip_exec;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +}
 +
 +static int split_mesh_exec(bContext *C, wmOperator *op)
 +{
 +      Object *ob= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh*)ob->data)->edit_btmesh;
 +      BMOperator bmop;
 +
 +      EDBM_InitOpf(em, &bmop, op, "split geom=%hvef", BM_SELECT);
 +      BMO_Exec_Op(em->bm, &bmop);
 +      BM_clear_flag_all(em->bm, BM_SELECT);
 +      BMO_HeaderFlag_Buffer(em->bm, &bmop, "geomout", BM_SELECT, BM_ALL);
 +      if (!EDBM_FinishOp(em, &bmop, op, 1)) {
 +              return OPERATOR_CANCELLED;
 +      }
 +
 +      /*Geometry has changed, need to recalc normals and looptris*/
 +      BMEdit_RecalcTesselation(em);
 +      EDBM_RecalcNormals(em);
 +
 +      DAG_id_tag_update(ob->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_split(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Split";
 +      ot->idname= "MESH_OT_split";
 +
 +      /* api callbacks */
 +      ot->exec= split_mesh_exec;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +}
 +
 +
 +static int spin_mesh_exec(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      ToolSettings *ts= CTX_data_tool_settings(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      BMesh *bm= em->bm;
 +      BMOperator spinop;
 +      float cent[3], axis[3], imat[3][3];
 +      float d[3] = {0.0f, 0.0f, 0.0f};
 +      int steps, dupli;
 +      float degr;
 +    
 +      RNA_float_get_array(op->ptr, "center", cent);
 +      RNA_float_get_array(op->ptr, "axis", axis);
 +      steps = RNA_int_get(op->ptr, "steps");
 +      degr = RNA_float_get(op->ptr, "degrees");
 +      if(ts->editbutflag & B_CLOCKWISE) degr= -degr;
 +      dupli = RNA_boolean_get(op->ptr, "dupli");
 +    
 +      /* undo object transformation */
 +      copy_m3_m4(imat, obedit->imat);
 +      sub_v3_v3(cent, obedit->obmat[3]);
 +      mul_m3_v3(imat, cent);
 +      mul_m3_v3(imat, axis);
 +
 +      if (!EDBM_InitOpf(em, &spinop, op,
 +                      "spin geom=%hvef cent=%v axis=%v dvec=%v steps=%d ang=%f dupli=%d",
 +                      BM_SELECT, cent, axis, d, steps, degr, dupli)) {
 +              return OPERATOR_CANCELLED;
 +      }
 +      BMO_Exec_Op(bm, &spinop);
 +      EDBM_clear_flag_all(em, BM_SELECT);
 +      BMO_HeaderFlag_Buffer(bm, &spinop, "lastout", BM_SELECT, BM_ALL);
 +      if (!EDBM_FinishOp(em, &spinop, op, 1)) {
 +              return OPERATOR_CANCELLED;
 +      }
 +
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +/* get center and axis, in global coords */
 +static int spin_mesh_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
 +{
 +      Scene *scene = CTX_data_scene(C);
 +      View3D *v3d = CTX_wm_view3d(C);
 +      RegionView3D *rv3d= ED_view3d_context_rv3d(C);
 +
 +      RNA_float_set_array(op->ptr, "center", give_cursor(scene, v3d));
 +      RNA_float_set_array(op->ptr, "axis", rv3d->viewinv[2]);
 +
 +      return spin_mesh_exec(C, op);
 +}
 +
 +void MESH_OT_spin(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Spin";
 +      ot->idname= "MESH_OT_spin";
 +
 +      /* api callbacks */
 +      ot->invoke= spin_mesh_invoke;
 +      ot->exec= spin_mesh_exec;
 +      ot->poll= EM_view3d_poll;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      /* props */
 +      RNA_def_int(ot->srna, "steps", 9, 0, INT_MAX, "Steps", "Steps", 0, INT_MAX);
 +      RNA_def_boolean(ot->srna, "dupli", 0, "Dupli", "Make Duplicates");
 +      RNA_def_float(ot->srna, "degrees", 90.0f, -FLT_MAX, FLT_MAX, "Degrees", "Degrees", -360.0f, 360.0f);
 +
 +      RNA_def_float_vector(ot->srna, "center", 3, NULL, -FLT_MAX, FLT_MAX, "Center", "Center in global view space", -FLT_MAX, FLT_MAX);
 +      RNA_def_float_vector(ot->srna, "axis", 3, NULL, -1.0f, 1.0f, "Axis", "Axis in global view space", -FLT_MAX, FLT_MAX);
 +
 +}
 +
 +static int screw_mesh_exec(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      BMesh *bm= em->bm;
 +      BMEdge *eed;
 +      BMVert *eve, *v1, *v2;
 +      BMIter iter, eiter;
 +      BMOperator spinop;
 +      float dvec[3], nor[3], cent[3], axis[3];
 +      float imat[3][3];
 +      int steps, turns;
 +      int valence;
 +
 +
 +      turns= RNA_int_get(op->ptr, "turns");
 +      steps= RNA_int_get(op->ptr, "steps");
 +      RNA_float_get_array(op->ptr, "center", cent);
 +      RNA_float_get_array(op->ptr, "axis", axis);
 +
 +      /* undo object transformation */
 +      copy_m3_m4(imat, obedit->imat);
 +      sub_v3_v3(cent, obedit->obmat[3]);
 +      mul_m3_v3(imat, cent);
 +      mul_m3_v3(imat, axis);
 +
 +
 +      /* find two vertices with valence count==1, more or less is wrong */
 +      v1 = NULL;
 +      v2 = NULL;
 +      for(eve = BMIter_New(&iter, em->bm, BM_VERTS_OF_MESH, NULL);
 +          eve; eve = BMIter_Step(&iter)) {
 +
 +              valence = 0;
 +
 +              for(eed = BMIter_New(&eiter, em->bm, BM_EDGES_OF_VERT, eve);
 +                  eed; eed = BMIter_Step(&eiter)) {
 +
 +                      if(BM_TestHFlag(eed, BM_SELECT)) {
 +                              valence++;
 +                      }
 +
 +              }
 +
 +              if(valence == 1) {
 +                      if(v1==NULL) v1 = eve;
 +                      else if(v2==NULL) v2= eve;
 +                      else {
 +                              v1= NULL;
 +                              break;
 +                      }
 +              }
 +      }
 +
 +      if(v1==NULL || v2==NULL) {
 +              BKE_report(op->reports, RPT_ERROR, "You have to select a string of connected vertices too");
 +              return OPERATOR_CANCELLED;
 +      }
 +
 +      /* calculate dvec */
 +      sub_v3_v3v3(dvec, v1->co, v2->co);
 +      mul_v3_fl(dvec, 1.0f/steps);
 +
 +      if(dot_v3v3(nor, dvec)>0.000)
 +              negate_v3(dvec);
 +
 +      if (!EDBM_InitOpf(em, &spinop, op,
 +                      "spin geom=%hvef cent=%v axis=%v dvec=%v steps=%d ang=%f dupli=0",
 +                      BM_SELECT, cent, axis, dvec, turns*steps, 360.0f*turns)) {
 +              return OPERATOR_CANCELLED;
 +      }
 +      BMO_Exec_Op(bm, &spinop);
 +      EDBM_clear_flag_all(em, BM_SELECT);
 +      BMO_HeaderFlag_Buffer(bm, &spinop, "lastout", BM_SELECT, BM_ALL);
 +      if (!EDBM_FinishOp(em, &spinop, op, 1)) {
 +              return OPERATOR_CANCELLED;
 +      }
 +
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +/* get center and axis, in global coords */
 +static int screw_mesh_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
 +{
 +      Scene *scene = CTX_data_scene(C);
 +      View3D *v3d = CTX_wm_view3d(C);
 +      RegionView3D *rv3d= ED_view3d_context_rv3d(C);
 +
 +      RNA_float_set_array(op->ptr, "center", give_cursor(scene, v3d));
 +      RNA_float_set_array(op->ptr, "axis", rv3d->viewinv[1]);
 +
 +      return screw_mesh_exec(C, op);
 +}
 +
 +void MESH_OT_screw(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Screw";
 +      ot->idname= "MESH_OT_screw";
 +
 +      /* api callbacks */
 +      ot->invoke= screw_mesh_invoke;
 +      ot->exec= screw_mesh_exec;
 +      ot->poll= EM_view3d_poll;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      /*props */
 +      RNA_def_int(ot->srna, "steps", 9, 0, INT_MAX, "Steps", "Steps", 0, 256);
 +      RNA_def_int(ot->srna, "turns", 1, 0, INT_MAX, "Turns", "Turns", 0, 256);
 +
 +      RNA_def_float_vector(ot->srna, "center", 3, NULL, -FLT_MAX, FLT_MAX, "Center", "Center in global view space", -FLT_MAX, FLT_MAX);
 +      RNA_def_float_vector(ot->srna, "axis", 3, NULL, -1.0f, 1.0f, "Axis", "Axis in global view space", -FLT_MAX, FLT_MAX);
 +}
 +
 +static int select_by_number_vertices_exec(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh*)obedit->data)->edit_btmesh;
 +      BMFace *efa;
 +      BMIter iter;
 +      int numverts= RNA_int_get(op->ptr, "number");
 +      int type = RNA_enum_get(op->ptr, "type");
 +
 +      for(efa = BMIter_New(&iter, em->bm, BM_FACES_OF_MESH, NULL);
 +          efa; efa = BMIter_Step(&iter)) {
 +
 +              int select = 0;
 +
 +              if(type == 0 && efa->len < numverts) {
 +                      select = 1;
 +              }else if(type == 1 && efa->len == numverts) {
 +                      select = 1;
 +              }else if(type == 2 && efa->len > numverts) {
 +                      select = 1;
 +              }
 +
 +              if(select) {
 +                      BM_Select(em->bm, efa, TRUE);
 +              }
 +      }
 +
 +      EDBM_selectmode_flush(em);
 +
 +      WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_select_by_number_vertices(wmOperatorType *ot)
 +{
 +      static const EnumPropertyItem type_items[] = {
 +          {0, "LESS", 0, "Less Than", ""},
 +          {1, "EQUAL", 0, "Equal To", ""},
 +          {2, "GREATER", 0, "Greater Than", ""},
 +          {0, NULL, 0, NULL, NULL}
 +      };
 +
 +      /* identifiers */
 +      ot->name= "Select by Number of Vertices";
 +      ot->description= "Select vertices or faces by vertex count";
 +      ot->idname= "MESH_OT_select_by_number_vertices";
 +      
 +      /* api callbacks */
 +      ot->exec= select_by_number_vertices_exec;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      /* properties */
 +      RNA_def_int(ot->srna, "number", 4, 3, INT_MAX, "Number of Vertices", "", 3, INT_MAX);
 +      RNA_def_enum(ot->srna, "type", type_items, 1, "Type", "Type of comparison to make");
 +}
 +
 +static int select_loose_verts_exec(bContext *C, wmOperator *UNUSED(op))
 +{
 +      Object *obedit = CTX_data_edit_object(C);
 +      BMEditMesh *em = ((Mesh*)obedit->data)->edit_btmesh;
 +      BMVert *eve;
 +      BMEdge *eed;
 +      BMIter iter;
 +
 +      for(eve = BMIter_New(&iter, em->bm, BM_VERTS_OF_MESH, NULL);
 +          eve; eve = BMIter_Step(&iter)) {
 +
 +              if(!eve->e) {
 +                      BM_Select(em->bm, eve, TRUE);
 +              }
 +      }
 +
 +      for(eed = BMIter_New(&iter, em->bm, BM_EDGES_OF_MESH, NULL);
 +          eed; eed = BMIter_Step(&iter)) {
 +
 +              if(!eed->l) {
 +                      BM_Select(em->bm, eed, TRUE);
 +              }
 +      }
 +
 +      EDBM_selectmode_flush(em);
 +
 +      WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_select_loose_verts(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name = "Select Loose Vertices/Edges";
 +      ot->description = "Select vertices with edges or faces and edges with no faces";
 +      ot->idname = "MESH_OT_select_loose_verts";
 +
 +      /* api callbacks */
 +      ot->exec = select_loose_verts_exec;
 +      ot->poll = ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
 +}
 +
 +#define MIRROR_THRESH 1.0f
 +
 +static int select_mirror_exec(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      BMVert *v1, *v2;
 +      BMIter iter;
 +      int extend= RNA_boolean_get(op->ptr, "extend");
 +
 +      BM_ITER(v1, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
 +              if (!BM_TestHFlag(v1, BM_SELECT) || BM_TestHFlag(v1, BM_HIDDEN)) {
 +                      BM_ClearHFlag(v1, BM_TMP_TAG);
 +              }
 +              else {
 +                      BM_SetHFlag(v1, BM_TMP_TAG);
 +              }
 +      }
 +
 +      EDBM_CacheMirrorVerts(em, TRUE);
 +
 +      if (!extend)
 +              EDBM_clear_flag_all(em, BM_SELECT);
 +
 +      BM_ITER(v1, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
 +              if (!BM_TestHFlag(v1, BM_TMP_TAG) || BM_TestHFlag(v1, BM_HIDDEN))
 +                      continue;
 +
 +              v2= EDBM_GetMirrorVert(em, v1);
 +              if (v2 && !BM_TestHFlag(v2, BM_HIDDEN)) {
 +                      BM_Select(em->bm, v2, TRUE);
 +              }
 +      }
 +
 +      EDBM_EndMirrorCache(em);
 +
 +      WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_select_mirror(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Select Mirror";
 +      ot->description= "Select mesh items at mirrored locations";
 +      ot->idname= "MESH_OT_select_mirror";
 +
 +      /* api callbacks */
 +      ot->exec= select_mirror_exec;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      /* props */
 +      RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Extend the existing selection");
 +}
 +
 +#if 0 /* UNUSED */
 +/********* qsort routines.  not sure how to make these
 +           work, since we aren't using linked lists for
 +           geometry anymore.  might need a sortof "swap"
 +           function for bmesh elements.           *********/
 +
 +typedef struct xvertsort {
 +      float x;
 +      BMVert *v1;
 +} xvertsort;
 +
 +
 +static int vergxco(const void *v1, const void *v2)
 +{
 +      const xvertsort *x1=v1, *x2=v2;
 +
 +      if( x1->x > x2->x ) return 1;
 +      else if( x1->x < x2->x) return -1;
 +      return 0;
 +}
 +
 +struct facesort {
 +      uintptr_t x;
 +      struct EditFace *efa;
 +};
 +
 +static int vergface(const void *v1, const void *v2)
 +{
 +      const struct facesort *x1=v1, *x2=v2;
 +
 +      if( x1->x > x2->x ) return 1;
 +      else if( x1->x < x2->x) return -1;
 +      return 0;
 +}
 +#endif
 +
 +// XXX is this needed?
 +/* called from buttons */
 +#if 0 /* UNUSED */
 +static void xsortvert_flag__doSetX(void *userData, EditVert *UNUSED(eve), int x, int UNUSED(y), int index)
 +{
 +      xvertsort *sortblock = userData;
 +
 +      sortblock[index].x = x;
 +}
 +#endif
 +
 +/* all verts with (flag & 'flag') are sorted */
 +static void xsortvert_flag(bContext *UNUSED(C), int UNUSED(flag))
 +{
 +      /*BMESH_TODO*/
 +#if 0 //hrm, geometry isn't in linked lists anymore. . .
 +      ViewContext vc;
 +      BMEditMesh *em;
 +      BMVert *eve;
 +      BMIter iter;
 +      xvertsort *sortblock;
 +      ListBase tbase;
 +      int i, amount;
 +
 +      em_setup_viewcontext(C, &vc);
 +      em = vc.em;
 +
 +      amount = em->bm->totvert;
 +      sortblock = MEM_callocN(sizeof(xvertsort)*amount,"xsort");
 +      BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
 +              if(BM_TestHFlag(eve, BM_SELECT))
 +                      sortblock[i].v1 = eve;
 +      }
 +      
 +      ED_view3d_init_mats_rv3d(vc.obedit, vc.rv3d);
 +      mesh_foreachScreenVert(&vc, xsortvert_flag__doSetX, sortblock, V3D_CLIP_TEST_OFF);
 +
 +      qsort(sortblock, amount, sizeof(xvertsort), vergxco);
 +
 +              /* make temporal listbase */
 +      tbase.first= tbase.last= 0;
 +      for (i=0; i<amount; i++) {
 +              eve = sortblock[i].v1;
 +
 +              if (eve) {
 +                      BLI_remlink(&vc.em->verts, eve);
 +                      BLI_addtail(&tbase, eve);
 +              }
 +      }
 +
 +      BLI_movelisttolist(&vc.em->verts, &tbase);
 +
 +      MEM_freeN(sortblock);
 +#endif
 +
 +}
 +
 +static int mesh_vertices_sort_exec(bContext *C, wmOperator *UNUSED(op))
 +{
 +      xsortvert_flag(C, SELECT);
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_vertices_sort(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Vertex Sort";
 +      ot->description= "Sort vertex order";
 +      ot->idname= "MESH_OT_vertices_sort";
 +
 +      /* api callbacks */
 +      ot->exec= mesh_vertices_sort_exec;
 +
 +      ot->poll= EM_view3d_poll; /* uses view relative X axis to sort verts */
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +}
 +
 +/* ********************** SORT FACES ******************* */
 +
 +static void permutate(void *list, int num, int size, int *index)
 +{
 +      void *buf;
 +      int len;
 +      int i;
 +
 +      len = num * size;
 +
 +      buf = MEM_mallocN(len, "permutate");
 +      memcpy(buf, list, len);
 +      
 +      for (i = 0; i < num; i++) {
 +              memcpy((char *)list + (i * size), (char *)buf + (index[i] * size), size);
 +      }
 +      MEM_freeN(buf);
 +}
 +
 +/* sort faces on view axis */
 +static float *face_sort_floats;
 +static int float_sort(const void *v1, const void *v2)
 +{
 +      float x1, x2;
 +      
 +      x1 = face_sort_floats[((int *) v1)[0]];
 +      x2 = face_sort_floats[((int *) v2)[0]];
 +      
 +      if( x1 > x2 ) return 1;
 +      else if( x1 < x2 ) return -1;
 +      return 0;
 +}
 +
 +static int sort_faces_exec(bContext *C, wmOperator *op)
 +{
 +      RegionView3D *rv3d= ED_view3d_context_rv3d(C);
 +      View3D *v3d= CTX_wm_view3d(C);
 +      Object *ob= CTX_data_edit_object(C);
 +      Scene *scene= CTX_data_scene(C);
 +      Mesh *me;
 +      CustomDataLayer *layer;
 +      int i, j, *index;
 +      int event;
 +      float reverse = 1;
 +      // XXX int ctrl= 0;
 +      
 +      if (!v3d) return OPERATOR_CANCELLED;
 +
 +      /* This operator work in Object Mode, not in edit mode.
 +       * After talk with Campbell we agree that there is no point to port this to EditMesh right now.
 +       * so for now, we just exit_editmode and enter_editmode at the end of this function.
 +       */
 +      ED_object_exit_editmode(C, EM_FREEDATA);
 +
 +      me= ob->data;
 +      if(me->totpoly==0) {
 +              ED_object_enter_editmode(C, 0);
 +              return OPERATOR_FINISHED;
 +      }
 +
 +      event= RNA_enum_get(op->ptr, "type");
 +
 +      // XXX
 +      //if(ctrl)
 +      //      reverse = -1;
 +      
 +      /* create index list */
 +      index= (int *)MEM_mallocN(sizeof(int) * me->totpoly, "sort faces");
 +      for (i = 0; i < me->totpoly; i++) {
 +              index[i] = i;
 +      }
 +      
 +      face_sort_floats = (float *) MEM_mallocN(sizeof(float) * me->totpoly, "sort faces float");
 +
 +      /* sort index list instead of faces itself 
 +       * and apply this permutation to all face layers
 +       */
 +      if (event == 5) {
 +              /* Random */
 +              for(i=0; i<me->totpoly; i++) {
 +                      face_sort_floats[i] = BLI_frand();
 +              }
 +              qsort(index, me->totpoly, sizeof(int), float_sort);             
 +      } else {
 +              MPoly *mp;
 +              MLoop *ml;
 +              MVert *mv;
 +              float vec[3];
 +              float mat[4][4];
 +              float cur[3];
 +              
 +              if (event == 1)
 +                      mult_m4_m4m4(mat, rv3d->viewmat, OBACT->obmat); /* apply the view matrix to the object matrix */
 +              else if (event == 2) { /* sort from cursor */
 +                      if( v3d && v3d->localvd ) {
 +                              copy_v3_v3(cur, v3d->cursor);
 +                      } else {
 +                              copy_v3_v3(cur, scene->cursor);
 +                      }
 +                      invert_m4_m4(mat, OBACT->obmat);
 +                      mul_m4_v3(mat, cur);
 +              }
 +              
 +              mp= me->mpoly;
 +
 +              for(i=0; i<me->totpoly; i++, mp++) {
 +                      if (event==3) {
 +                              face_sort_floats[i] = ((float)mp->mat_nr)*reverse;
 +                      } else if (event==4) {
 +                              /*selected first*/
 +                              if (mp->flag & ME_FACE_SEL)
 +                                      face_sort_floats[i] = 0.0;
 +                              else
 +                                      face_sort_floats[i] = reverse;
 +                      } else {
 +                              /* find the face's center */
 +                              ml = me->mloop+mp->loopstart;
 +                              zero_v3(vec);
 +                              for (j = 0; j < mp->totloop; j++, ml++) {
 +                                      mv = me->mvert+ml->v;
 +                                      add_v3_v3(vec, mv->co);
 +                              }
 +                              mul_v3_fl(vec, 1.0f / (float)mp->totloop);
 +                              
 +                              if (event == 1) { /* sort on view axis */
 +                                      mul_m4_v3(mat, vec);
 +                                      face_sort_floats[i] = vec[2] * reverse;
 +                              } else if(event == 2) { /* distance from cursor*/
 +                                      face_sort_floats[i] = len_v3v3(cur, vec) * reverse; /* back to front */
 +                              }
 +                      }
 +              }
 +              qsort(index, me->totpoly, sizeof(int), float_sort);
 +      }
 +      
 +      MEM_freeN(face_sort_floats);
 +      for(i = 0; i < me->pdata.totlayer; i++) {
 +              layer = &me->pdata.layers[i];
 +              permutate(layer->data, me->totpoly, CustomData_sizeof(layer->type), index);
 +      }
 +
 +      MEM_freeN(index);
 +      DAG_id_tag_update(ob->data, 0);
 +
 +      /* Return to editmode. */
 +      ED_object_enter_editmode(C, 0);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_sort_faces(wmOperatorType *ot)
 +{
 +      static EnumPropertyItem type_items[]= {
 +              { 1, "VIEW_AXIS", 0, "View Axis", "" },
 +              { 2, "CURSOR_DISTANCE", 0, "Cursor Distance", "" },
 +              { 3, "MATERIAL", 0, "Material", "" },
 +              { 4, "SELECTED", 0, "Selected", "" },
 +              { 5, "RANDOMIZE", 0, "Randomize", "" },
 +              { 0, NULL, 0, NULL, NULL }};
 +
 +      /* identifiers */
 +      ot->name= "Sort Faces"; // XXX (Ctrl to reverse)%t|
 +      ot->description= "The faces of the active Mesh Object are sorted, based on the current view";
 +      ot->idname= "MESH_OT_sort_faces";
 +
 +      /* api callbacks */
 +      ot->invoke= WM_menu_invoke;
 +      ot->exec= sort_faces_exec;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      /* properties */
 +      ot->prop= RNA_def_enum(ot->srna, "type", type_items, 0, "Type", "");
 +}
 +
 +#if 0
 +/* called from buttons */
 +static void hashvert_flag(EditMesh *em, int flag)
 +{
 +      /* switch vertex order using hash table */
 +      EditVert *eve;
 +      struct xvertsort *sortblock, *sb, onth, *newsort;
 +      ListBase tbase;
 +      int amount, a, b;
 +
 +      /* count */
 +      eve= em->verts.first;
 +      amount= 0;
 +      while(eve) {
 +              if(eve->f & flag) amount++;
 +              eve= eve->next;
 +      }
 +      if(amount==0) return;
 +
 +      /* allocate memory */
 +      sb= sortblock= (struct xvertsort *)MEM_mallocN(sizeof(struct xvertsort)*amount,"sortremovedoub");
 +      eve= em->verts.first;
 +      while(eve) {
 +              if(eve->f & flag) {
 +                      sb->v1= eve;
 +                      sb++;
 +              }
 +              eve= eve->next;
 +      }
 +
 +      BLI_srand(1);
 +
 +      sb= sortblock;
 +      for(a=0; a<amount; a++, sb++) {
 +              b= (int)(amount*BLI_drand());
 +              if(b>=0 && b<amount) {
 +                      newsort= sortblock+b;
 +                      onth= *sb;
 +                      *sb= *newsort;
 +                      *newsort= onth;
 +              }
 +      }
 +
 +      /* make temporal listbase */
 +      tbase.first= tbase.last= 0;
 +      sb= sortblock;
 +      while(amount--) {
 +              eve= sb->v1;
 +              BLI_remlink(&em->verts, eve);
 +              BLI_addtail(&tbase, eve);
 +              sb++;
 +      }
 +
 +      BLI_movelisttolist(&em->verts, &tbase);
 +
 +      MEM_freeN(sortblock);
 +
 +}
 +#endif
 +
 +static int mesh_vertices_randomize_exec(bContext *C, wmOperator *UNUSED(op))
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +#if 1 /*BMESH TODO*/
 +      (void)em;
 +#else
 +      hashvert_flag(em, SELECT);
 +#endif
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_vertices_randomize(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Vertex Randomize";
 +      ot->description= "Randomize vertex order";
 +      ot->idname= "MESH_OT_vertices_randomize";
 +
 +      /* api callbacks */
 +      ot->exec= mesh_vertices_randomize_exec;
 +
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +}
 +
 +/******end of qsort stuff ****/
 +
 +
 +static int mesh_noise_exec(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= (((Mesh *)obedit->data))->edit_btmesh;
 +      Material *ma;
 +      Tex *tex;
 +      BMVert *eve;
 +      BMIter iter;
 +      float fac= RNA_float_get(op->ptr, "factor");
 +
 +      if(em==NULL) return OPERATOR_FINISHED;
 +
 +      ma= give_current_material(obedit, obedit->actcol);
 +      if(ma==0 || ma->mtex[0]==0 || ma->mtex[0]->tex==0) {
 +              BKE_report(op->reports, RPT_WARNING, "Mesh has no material or texture assigned");
 +              return OPERATOR_FINISHED;
 +      }
 +      tex= give_current_material_texture(ma);
 +
 +      if(tex->type==TEX_STUCCI) {
 +              float b2, vec[3];
 +              float ofs= tex->turbul/200.0;
 +              BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
 +                      if (BM_TestHFlag(eve, BM_SELECT) && !BM_TestHFlag(eve, BM_HIDDEN)) {
 +                              b2= BLI_hnoise(tex->noisesize, eve->co[0], eve->co[1], eve->co[2]);
 +                              if(tex->stype) ofs*=(b2*b2);
 +                              vec[0]= fac*(b2-BLI_hnoise(tex->noisesize, eve->co[0]+ofs, eve->co[1], eve->co[2]));
 +                              vec[1]= fac*(b2-BLI_hnoise(tex->noisesize, eve->co[0], eve->co[1]+ofs, eve->co[2]));
 +                              vec[2]= fac*(b2-BLI_hnoise(tex->noisesize, eve->co[0], eve->co[1], eve->co[2]+ofs));
 +                              
 +                              add_v3_v3(eve->co, vec);
 +                      }
 +              }
 +      }
 +      else {
 +              BM_ITER(eve, &iter, em->bm, BM_VERTS_OF_MESH, NULL) {
 +                      if (BM_TestHFlag(eve, BM_SELECT) && !BM_TestHFlag(eve, BM_HIDDEN)) {
 +                              float tin, dum;
 +                              externtex(ma->mtex[0], eve->co, &tin, &dum, &dum, &dum, &dum, 0);
 +                              eve->co[2]+= fac*tin;
 +                      }
 +              }
 +      }
 +
 +      EDBM_RecalcNormals(em);
 +
 +      DAG_id_tag_update(obedit->data, 0);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_noise(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Noise";
 +      ot->description= "Use vertex coordinate as texture coordinate";
 +      ot->idname= "MESH_OT_noise";
 +
 +      /* api callbacks */
 +      ot->exec= mesh_noise_exec;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      RNA_def_float(ot->srna, "factor", 0.1f, -FLT_MAX, FLT_MAX, "Factor", "", 0.0f, 1.0f);
 +}
 +
 +/*bevel! yay!!*/
 +static int mesh_bevel_exec(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= (((Mesh *)obedit->data))->edit_btmesh;
 +      BMIter iter;
 +      BMEdge *eed;
 +      BMOperator bmop;
 +      float factor= RNA_float_get(op->ptr, "percent"), fac=factor /*, dfac */ /* UNUSED */, df, s;
 +      int i, recursion = RNA_int_get(op->ptr, "recursion");
 +      const int use_even= RNA_boolean_get(op->ptr, "use_even");
 +      const int use_dist= RNA_boolean_get(op->ptr, "use_dist");
 +      float *w = NULL, ftot;
 +      int li;
 +      BLI_array_declare(w);
 +      
 +      BM_add_data_layer(em->bm, &em->bm->edata, CD_PROP_FLT);
 +      li = CustomData_number_of_layers(&em->bm->edata, CD_PROP_FLT)-1;
 +      
 +      BM_ITER(eed, &iter, em->bm, BM_EDGES_OF_MESH, NULL) {
 +              float d = len_v3v3(eed->v1->co, eed->v2->co);
 +              float *dv = CustomData_bmesh_get_n(&em->bm->edata, eed->head.data, CD_PROP_FLT, li);
 +              
 +              *dv = d;
 +      }
 +      
 +      if(em==NULL) return OPERATOR_CANCELLED;
 +      
 +      /*ugh, stupid math depends somewhat on angles!*/
 +      /* dfac = 1.0/(float)(recursion+1); */ /* UNUSED */
 +      df = 1.0;
 +      for (i=0, ftot=0.0f; i<recursion; i++) {
 +              s = pow(df, 1.25);
 +              
 +              BLI_array_append(w, s);
 +              ftot += s;
 +              
 +              df *= 2.0;
 +      }
 +
 +      mul_vn_fl(w, BLI_array_count(w), 1.0f / (float)ftot);
 +
 +      fac = factor;
 +      for (i=0; i<BLI_array_count(w); i++) {
 +              fac = w[BLI_array_count(w)-i-1]*factor;
 +
 +              if (!EDBM_InitOpf(em, &bmop, op, "bevel geom=%hev percent=%f lengthlayer=%i use_lengths=%i use_even=%i use_dist=%i", BM_SELECT, fac, li, 1, use_even, use_dist))
 +                      return OPERATOR_CANCELLED;
 +              
 +              BMO_Exec_Op(em->bm, &bmop);
 +              if (!EDBM_FinishOp(em, &bmop, op, 1))
 +                      return OPERATOR_CANCELLED;
 +      }
 +      
 +      BM_free_data_layer_n(em->bm, &em->bm->edata, CD_MASK_PROP_FLT, li);
 +      
 +      BLI_array_free(w);
 +      EDBM_RecalcNormals(em);
 +
 +      DAG_id_tag_update(obedit->data, 0);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;
 +}
 +
 +void MESH_OT_bevel(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Bevel";
 +      ot->description= "Edge/Vertex Bevel";
 +      ot->idname= "MESH_OT_bevel";
 +
 +      /* api callbacks */
 +      ot->exec= mesh_bevel_exec;
 +      ot->poll= ED_operator_editmesh;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      RNA_def_float(ot->srna, "percent", 0.5f, -FLT_MAX, FLT_MAX, "Percentage", "", 0.0f, 1.0f);
 +      RNA_def_int(ot->srna, "recursion", 1, 1, 50, "Recursion Level", "Recursion Level", 1, 8);
 +
 +      RNA_def_boolean(ot->srna, "use_even", FALSE, "Even",     "Calculate evenly spaced bevel");
 +      RNA_def_boolean(ot->srna, "use_dist", FALSE, "Distance", "Interpret the percent in blender units");
 +
 +}
 +
 +static int mesh_export_obj_exec(bContext *C, wmOperator *op)
 +{
 +      Object *ob = CTX_data_active_object(C);
 +      DerivedMesh *dm;
 +      Scene *scene = CTX_data_scene(C);
 +      Mesh *me;
 +      Main *bmain = CTX_data_main(C);
 +      MVert *mvert, *mv;
 +      MLoop *mloop, *ml;
 +      MPoly *mpoly, *mp;
 +      MTexPoly *mtexpoly;
 +      MLoopUV *luv, *mloopuv;
 +      /*MLoopCol *mloopcol;*/
 +      FILE *file, *matfile;
 +      int *face_mat_group;
 +      struct {Material *mat; MTexPoly poly; int end;} **matlists;
 +      char str[FILE_MAX], str2[FILE_MAX];
 +      int i, j, c, free;
 +      
 +      if (ob->type != OB_MESH) {
 +              BKE_report(op->reports, RPT_OPERATOR, "Only meshes can be exported");
 +              return OPERATOR_CANCELLED;
 +      }
 +      
 +      RNA_string_get(op->ptr, "filepath", str);
 +      
 +      sprintf(str2, "%s_materials.mtl", str);
 +      file = fopen(str, "wb");
 +      matfile = fopen(str2, "wb");
 +      
 +      if (!file) {
 +              BKE_report(op->reports, RPT_OPERATOR, "Could not open file");
 +              
 +              if (matfile)
 +                      fclose(matfile);
 +              return OPERATOR_CANCELLED;
 +      }
 +      
 +      if (!matfile) {
 +              BKE_report(op->reports, RPT_OPERATOR, "Could not open material file");
 +              
 +              if (file)
 +                      fclose(file);
 +              return OPERATOR_CANCELLED;
 +      }
 +      
 +      me = ob->data;
 +      if (me->edit_btmesh) {
 +              EDBM_LoadEditBMesh(scene, ob);
 +      }
 +      
 +      if (!RNA_boolean_get(op->ptr, "apply_modifiers")) {
 +              dm = CDDM_from_mesh(me, ob);
 +              free = 1;
 +      } else {
 +              dm = mesh_get_derived_final(scene, ob, CD_MASK_DERIVEDMESH);
 +              if (!CDDM_Check(dm)) {
 +                      dm = CDDM_copy(dm, 0);
 +                      free = 1;
 +              } else {
 +                      free = 0;
 +              }
 +      }
 +      
 +      face_mat_group = MEM_callocN(sizeof(int)*dm->numPolyData, "face_mat_group");
 +      
 +      if (MAX2(ob->totcol, me->totcol))
 +              matlists = MEM_callocN(sizeof(*matlists)*MAX2(me->totcol, ob->totcol), "matlists");
 +      else matlists = NULL;
 +      
 +      for (i=0; i<MAX2(ob->totcol, me->totcol); i++) {
 +              matlists[i] = MEM_callocN(sizeof(**matlists), "matlists[i]");
 +              matlists[i][0].end = 1;
 +      }
 +      
 +      
 +      mvert = CDDM_get_verts(dm);
 +      mloop = CDDM_get_loops(dm);
 +      mpoly = CDDM_get_polys(dm);
 +      mtexpoly = CustomData_get_layer(&dm->polyData, CD_MTEXPOLY);
 +      mloopuv = CustomData_get_layer(&dm->loopData, CD_MLOOPUV);
 +      /* mloopcol = CustomData_get_layer(&dm->loopData, CD_MLOOPCOL); */ /* UNUSED */
 +      
 +      if (matlists) {
 +              /*build material list*/
 +              mp = mpoly;
 +              for (i=0; i<dm->numPolyData; i++, (mtexpoly ? mtexpoly++ : NULL), mp++) {
 +                      int found = 0;
 +                      
 +                      j = 0;
 +                      while (!matlists[mp->mat_nr][j].end) {
 +                              Material *mat = ob->matbits[mp->mat_nr] ? ob->mat[mp->mat_nr] : me->mat[mp->mat_nr];
 +                              
 +                              if (matlists[mp->mat_nr][j].mat == mat) {
 +                                      if (mtexpoly) { 
 +                                              if (matlists[mp->mat_nr][j].poly.tpage == mtexpoly->tpage) {
 +                                                      found = 1;
 +                                                      break;
 +                                              }
 +                                      } else {
 +                                              found = 1;
 +                                              break;
 +                                      }
 +                              }
 +                              j++;
 +                      }
 +                      
 +                      if (!found) {
 +                              matlists[mp->mat_nr] = MEM_reallocN(matlists[mp->mat_nr], sizeof(**matlists)*(j+2));
 +                              
 +                              /*add sentinal*/
 +                              matlists[mp->mat_nr][j+1].end = 1;
 +                              matlists[mp->mat_nr][j].end = 0;
 +                              
 +                              if (ob->matbits && ob->matbits[mp->mat_nr]) {
 +                                      matlists[mp->mat_nr][j].mat = ob->mat[mp->mat_nr];
 +                              } else {
 +                                      matlists[mp->mat_nr][j].mat = me->mat[mp->mat_nr];
 +                              }
 +                              
 +                              if (mtexpoly)
 +                                      matlists[mp->mat_nr][j].poly = *mtexpoly;
 +                      }
 +                      
 +                      face_mat_group[i] = j;
 +              }
 +      }
 +
 +      /*write material references*/
 +      fprintf(file, "mtllib %s_materials.mtl\n", str);
 +      fprintf(file, "o %s\n", (ob->id.name+2));
 +      
 +      for (mv=mvert, i=0; i<dm->numVertData; i++, mv++) {
 +              fprintf(file, "v %.8f\t%.8f\t%.8f\n", mv->co[0], mv->co[1], mv->co[2]);
 +              fprintf(file, "vn %.5f\t%.5f\t%.5f\n", (float)mv->no[0]/65535.0f, (float)mv->no[1]/65535.0f, (float)mv->no[2]/65535.0f);
 +      }
 +
 +      /*write texture coordinates*/
 +      if (mloopuv) {
 +              fprintf(file, "\n");
 +              for (mp=mpoly, i=0; i<dm->numPolyData; i++, mp++) {
 +                      luv = mloopuv + mp->loopstart;
 +                      for (j=0; j<mp->totloop; j++, luv++) {
 +                              fprintf(file, "vt %.8f\t%.8f\n", luv->uv[0], luv->uv[1]);
 +                      }
 +              }
 +      }
 +      
 +      fprintf(file, "\n");
 +      c = 0;
 +      for (mp=mpoly, i=0; i<dm->numPolyData; i++, mp++) {
 +              char matname[256];
 +              
 +              if (mp->flag & ME_SMOOTH) {
 +                      fprintf(file, "s 1\n");
 +              } else {
 +                      fprintf(file, "s off\n");
 +              }
 +              
 +              if (matlists) {
 +                      if (matlists[mp->mat_nr][face_mat_group[i]].mat && matlists[mp->mat_nr][face_mat_group[i]].poly.tpage) {
 +                              sprintf(matname, "%s__%s", matlists[mp->mat_nr][face_mat_group[i]].mat->id.name+2, 
 +                                      matlists[mp->mat_nr][face_mat_group[i]].poly.tpage->id.name+2);
 +                      } else if (matlists[mp->mat_nr][face_mat_group[i]].mat) {
 +                              sprintf(matname, "%s", matlists[mp->mat_nr][face_mat_group[i]].mat->id.name+2);
 +                      } else if (matlists[mp->mat_nr][face_mat_group[i]].poly.tpage != NULL) {
 +                              sprintf(matname, "texture_%s", matlists[mp->mat_nr][face_mat_group[i]].poly.tpage->id.name+2);
 +                      } else {
 +                              sprintf(matname, "__null_material_%d_%d", mp->mat_nr, face_mat_group[mp->mat_nr]);
 +                      }
 +                      
 +                      fprintf(file, "usemtl %s\n", matname);
 +              }
 +              
 +              fprintf(file, "f ");
 +              
 +              ml = mloop + mp->loopstart;
 +              luv = mloopuv ? mloopuv + mp->loopstart : NULL;
 +              for (j=0; j<mp->totloop; j++, ml++, (luv ? luv++ : NULL), c++) {
 +                      if (luv) {
 +                              fprintf(file, "%d/%d ", ml->v+1, c+1);
 +                      } else {
 +                              fprintf(file, "%d ", ml->v+1);
 +                      }
 +              }
 +              fprintf(file, "\n");
 +      }
 +      
 +      fclose(file);
 +      
 +      /*write material library*/
 +      fprintf(matfile, "#Blender MTL File\n\n");
 +      for (i=0; i<MAX2(ob->totcol, me->totcol); i++) {
 +              Material *mat;
 +              char basedir[FILE_MAX], filename[FILE_MAX], str3[FILE_MAX];
 +              
 +              j = 0;
 +              while (!matlists[i][j].end) {
 +                      mat = matlists[i][j].mat;
 +                      
 +                      if (mat && matlists[i][j].poly.tpage) {
 +                              fprintf(matfile, "newmtl %s__%s\n", mat->id.name+2, 
 +                                      matlists[i][j].poly.tpage->id.name+2);
 +                      } else if (mat) {
 +                              fprintf(matfile, "newmtl %s\n", mat->id.name+2);
 +                      } else if (matlists[i][j].poly.tpage != NULL) {
 +                              fprintf(matfile, "newmtl texture_%s\n", matlists[i][j].poly.tpage->id.name+2);
 +                      } else {
 +                              fprintf(matfile, "newmtl __null_material_%d_%d\n", i, j);
 +                      }
 +
 +                      if (mat) {
 +                              fprintf(matfile, "Kd %.6f %.6f %.6f\n", mat->r, mat->g, mat->b);
 +                              fprintf(matfile, "Ks %.6f %.6f %.6f\n", mat->specr, mat->specg, mat->specb);
 +                              fprintf(matfile, "Ns %.6f\n", mat->spec*1000.0f);
 +                      } else {
 +                              fprintf(matfile, "Kd %.6f %.6f %.6f\n", 0.45f, 0.45f, 0.45f);
 +                              fprintf(matfile, "Ks %.6f %.6f %.6f\n", 1.0f, 0.4f, 0.1f);
 +                              fprintf(matfile, "Ns %.6f\n", 300.0f);
 +                      }
 +
 +                      fprintf(matfile, "illum 2\n");
 +                      
 +                      if (matlists[i][j].poly.tpage) {
 +                              BLI_strncpy(str2, matlists[i][j].poly.tpage->name, FILE_MAX);
 +                              BLI_strncpy(basedir, bmain->name, FILE_MAX);
 +                              
 +                              BLI_splitdirstring(basedir, filename);
 +                              BLI_cleanup_file(basedir, str2); /* fix any /foo/../foo/ */
 +                              
 +                              if (BLI_exists(str2)) {
 +                                      char rel[3] = {0};
 +                                      
 +                                      BLI_strncpy(str3, str2, FILE_MAX);
 +                                      if (RNA_boolean_get(op->ptr, "relpaths")) {
 +                                              BLI_path_rel(str3, str);
 +                                              
 +                                              if (str3[2] != '.' && str3[2] != '/' && str3[2] != '\\') {
 +                                                      rel[0] = '.';
 +                                                      rel[1] = '/';
 +                                              }
 +                                      }
 +                                              
 +                                      fprintf(matfile, "map_Ka %s%s\n", rel, (str3+2*RNA_boolean_get(op->ptr, "relpaths")));
 +                                      fprintf(matfile, "map_Kd %s%s\n", rel, (str3+2*RNA_boolean_get(op->ptr, "relpaths")));
 +                              }
 +                      }
 +                      
 +                      fprintf(matfile, "\n");
 +                      j++;
 +              }
 +      }
 +      
 +      fclose(matfile);
 +      
 +      for (i=0; i<MAX2(ob->totcol, me->totcol); i++) {
 +              MEM_freeN(matlists[i]);
 +      }
 +      
 +      if (matlists)
 +              MEM_freeN(matlists);
 +      
 +      if (face_mat_group)
 +              MEM_freeN(face_mat_group);
 +
 +      if (free) {
 +              dm->needsFree = 1;
 +              dm->release(dm);
 +      }
 +      
 +      return OPERATOR_FINISHED;
 +}
 +
 +static void export_obj_filesel(bContext *C, wmOperator *op, const char *path)
 +{
 +      RNA_string_set(op->ptr, "filepath", path);
 +      WM_event_add_fileselect(C, op); 
 +}
 +
 +
 +static int export_obj_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
 +{
 +      char filename[FILE_MAX];
 +      
 +      BLI_strncpy(filename, "//untitled.obj", FILE_MAX);
 +
++      if(RNA_struct_property_is_set(op->ptr, "filepath"))
 +              return mesh_export_obj_exec(C, op);
 +      
 +      export_obj_filesel(C, op, filename);
 +
 +      return OPERATOR_RUNNING_MODAL;
 +}
 +
 +
 +void EXPORT_MESH_OT_wavefront(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Export Wavefront OBJ";
 +      ot->description= "Export Wavefront (obj)";
 +      ot->idname= "EXPORT_MESH_OT_wavefront";
 +
 +      /* api callbacks */
 +      ot->exec= mesh_export_obj_exec;
 +      ot->invoke= export_obj_invoke;
 +      ot->poll= ED_operator_object_active;
 +
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +
 +      /* properties */
 +      WM_operator_properties_filesel(ot, FOLDERFILE|TEXTFILE, FILE_SPECIAL, FILE_SAVE, WM_FILESEL_FILEPATH);
 +      
 +      RNA_def_boolean(ot->srna, "apply_modifiers", 0, "Apply Modifiers", "Apply Modifiers");
 +      RNA_def_boolean(ot->srna, "relpaths", 0, "Relative Paths", "Use relative paths for textures");
 +}
 +
 +static int bridge_edge_loops(bContext *C, wmOperator *op)
 +{
 +      Object *obedit= CTX_data_edit_object(C);
 +      BMEditMesh *em= ((Mesh *)obedit->data)->edit_btmesh;
 +      
 +      if (!EDBM_CallOpf(em, op, "bridge_loops edges=%he", BM_SELECT))
 +              return OPERATOR_CANCELLED;
 +      
 +      DAG_id_tag_update(obedit->data, OB_RECALC_DATA);
 +      WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
 +
 +      return OPERATOR_FINISHED;       
 +}
 +
 +void MESH_OT_bridge_edge_loops(wmOperatorType *ot)
 +{
 +      /* identifiers */
 +      ot->name= "Bridge edge loops";
 +      ot->description= "Make faces between two edge loops";
 +      ot->idname= "MESH_OT_bridge_edge_loops";
 +      
 +      /* api callbacks */
 +      ot->exec= bridge_edge_loops;
 +      ot->poll= ED_operator_editmesh;
 +      
 +      /* flags */
 +      ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 +      
 +      RNA_def_boolean(ot->srna, "inside", 0, "Inside", "");
 +}
@@@ -322,10 -321,7 +322,7 @@@ void ED_object_exit_editmode(bContext *
                
  //            if(EM_texFaceCheck())
                
- //            if(retopo_mesh_paint_check())
- //                    retopo_end_okee();
-               
 -              if(me->edit_mesh->totvert>MESH_MAX_VERTS) {
 +              if(me->edit_btmesh->bm->totvert>MESH_MAX_VERTS) {
                        error("Too many vertices");
                        return;
                }
@@@ -1278,10 -1206,10 +1278,10 @@@ static int multires_external_save_invok
        if (!mmd)
                return OPERATOR_CANCELLED;
        
 -      if(CustomData_external_test(&me->fdata, CD_MDISPS))
 +      if(CustomData_external_test(&me->ldata, CD_MDISPS))
                return OPERATOR_CANCELLED;
  
-       if(RNA_property_is_set(op->ptr, "filepath"))
+       if(RNA_struct_property_is_set(op->ptr, "filepath"))
                return multires_external_save_exec(C, op);
        
        op->customdata= me;
d