dont use tface hide or select anymore, since maintaining 2 sets of hide/select data...
[blender.git] / source / blender / blenkernel / intern / DerivedMesh.c
index 308a32439d488aa21f87283a1195dd8bbc415a14..fa8f76178d590d53a7e9a0a358edf00ffd2d7858 100644 (file)
 #include "DNA_object_force.h"
 #include "DNA_object_fluidsim.h" // N_T
 #include "DNA_scene_types.h" // N_T
+#include "DNA_view3d_types.h"
+#include "DNA_screen_types.h"
+#include "DNA_space_types.h"
 
 #include "BLI_arithb.h"
 #include "BLI_blenlib.h"
 #include "BLI_edgehash.h"
 #include "BLI_editVert.h"
+#include "BLI_linklist.h"
 
 #include "BKE_utildefines.h"
 #include "BKE_cdderivedmesh.h"
+#include "BKE_customdata.h"
 #include "BKE_DerivedMesh.h"
 #include "BKE_displist.h"
 #include "BKE_effect.h"
 
 ///////////////////////////////////
 ///////////////////////////////////
-#define DERIVEDMESH_INITIAL_LAYERS 5
+
+MVert *dm_getVertArray(DerivedMesh *dm)
+{
+       MVert *mvert = CustomData_get_layer(&dm->vertData, CD_MVERT);
+
+       if (!mvert) {
+               mvert = CustomData_add_layer(&dm->vertData, CD_MVERT, CD_CALLOC, NULL,
+                       dm->getNumVerts(dm));
+               CustomData_set_layer_flag(&dm->vertData, CD_MVERT, CD_FLAG_TEMPORARY);
+               dm->copyVertArray(dm, mvert);
+       }
+
+       return mvert;
+}
+
+MEdge *dm_getEdgeArray(DerivedMesh *dm)
+{
+       MEdge *medge = CustomData_get_layer(&dm->edgeData, CD_MEDGE);
+
+       if (!medge) {
+               medge = CustomData_add_layer(&dm->edgeData, CD_MEDGE, CD_CALLOC, NULL,
+                       dm->getNumEdges(dm));
+               CustomData_set_layer_flag(&dm->edgeData, CD_MEDGE, CD_FLAG_TEMPORARY);
+               dm->copyEdgeArray(dm, medge);
+       }
+
+       return medge;
+}
+
+MFace *dm_getFaceArray(DerivedMesh *dm)
+{
+       MFace *mface = CustomData_get_layer(&dm->faceData, CD_MFACE);
+
+       if (!mface) {
+               mface = CustomData_add_layer(&dm->faceData, CD_MFACE, CD_CALLOC, NULL,
+                       dm->getNumFaces(dm));
+               CustomData_set_layer_flag(&dm->faceData, CD_MFACE, CD_FLAG_TEMPORARY);
+               dm->copyFaceArray(dm, mface);
+       }
+
+       return mface;
+}
 
 MVert *dm_dupVertArray(DerivedMesh *dm)
 {
        MVert *tmp = MEM_callocN(sizeof(*tmp) * dm->getNumVerts(dm),
                                 "dm_dupVertArray tmp");
 
-       if(tmp) dm->getVertArray(dm, tmp);
+       if(tmp) dm->copyVertArray(dm, tmp);
 
        return tmp;
 }
@@ -104,7 +150,7 @@ MEdge *dm_dupEdgeArray(DerivedMesh *dm)
        MEdge *tmp = MEM_callocN(sizeof(*tmp) * dm->getNumEdges(dm),
                                 "dm_dupEdgeArray tmp");
 
-       if(tmp) dm->getEdgeArray(dm, tmp);
+       if(tmp) dm->copyEdgeArray(dm, tmp);
 
        return tmp;
 }
@@ -114,7 +160,7 @@ MFace *dm_dupFaceArray(DerivedMesh *dm)
        MFace *tmp = MEM_callocN(sizeof(*tmp) * dm->getNumFaces(dm),
                                 "dm_dupFaceArray tmp");
 
-       if(tmp) dm->getFaceArray(dm, tmp);
+       if(tmp) dm->copyFaceArray(dm, tmp);
 
        return tmp;
 }
@@ -122,6 +168,9 @@ MFace *dm_dupFaceArray(DerivedMesh *dm)
 void DM_init_funcs(DerivedMesh *dm)
 {
        /* default function implementations */
+       dm->getVertArray = dm_getVertArray;
+       dm->getEdgeArray = dm_getEdgeArray;
+       dm->getFaceArray = dm_getFaceArray;
        dm->dupVertArray = dm_dupVertArray;
        dm->dupEdgeArray = dm_dupEdgeArray;
        dm->dupFaceArray = dm_dupFaceArray;
@@ -137,104 +186,118 @@ void DM_init_funcs(DerivedMesh *dm)
 void DM_init(DerivedMesh *dm,
              int numVerts, int numEdges, int numFaces)
 {
-       CustomData_init(&dm->vertData, DERIVEDMESH_INITIAL_LAYERS, numVerts,
-                       SUB_ELEMS_VERT);
-       CustomData_init(&dm->edgeData, DERIVEDMESH_INITIAL_LAYERS, numEdges,
-                       SUB_ELEMS_EDGE);
-       CustomData_init(&dm->faceData, DERIVEDMESH_INITIAL_LAYERS, numFaces,
-                       SUB_ELEMS_FACE);
+       CustomData_add_layer(&dm->vertData, CD_ORIGINDEX, CD_CALLOC, NULL, numVerts);
+       CustomData_add_layer(&dm->edgeData, CD_ORIGINDEX, CD_CALLOC, NULL, numEdges);
+       CustomData_add_layer(&dm->faceData, CD_ORIGINDEX, CD_CALLOC, NULL, numFaces);
 
-       CustomData_add_layer(&dm->vertData, LAYERTYPE_ORIGINDEX, 0, NULL);
-       CustomData_add_layer(&dm->edgeData, LAYERTYPE_ORIGINDEX, 0, NULL);
-       CustomData_add_layer(&dm->faceData, LAYERTYPE_ORIGINDEX, 0, NULL);
+       dm->numVertData = numVerts;
+       dm->numEdgeData = numEdges;
+       dm->numFaceData = numFaces;
 
        DM_init_funcs(dm);
+       
+       dm->needsFree = 1;
 }
 
 void DM_from_template(DerivedMesh *dm, DerivedMesh *source,
                       int numVerts, int numEdges, int numFaces)
 {
-       CustomData_from_template(&source->vertData, &dm->vertData, numVerts);
-       CustomData_from_template(&source->edgeData, &dm->edgeData, numEdges);
-       CustomData_from_template(&source->faceData, &dm->faceData, numFaces);
+       CustomData_copy(&source->vertData, &dm->vertData, CD_MASK_DERIVEDMESH,
+                       CD_CALLOC, numVerts);
+       CustomData_copy(&source->edgeData, &dm->edgeData, CD_MASK_DERIVEDMESH,
+                       CD_CALLOC, numEdges);
+       CustomData_copy(&source->faceData, &dm->faceData, CD_MASK_DERIVEDMESH,
+                       CD_CALLOC, numFaces);
+
+       dm->numVertData = numVerts;
+       dm->numEdgeData = numEdges;
+       dm->numFaceData = numFaces;
 
        DM_init_funcs(dm);
+
+       dm->needsFree = 1;
 }
 
-void DM_release(DerivedMesh *dm)
+int DM_release(DerivedMesh *dm)
 {
-       CustomData_free(&dm->vertData);
-       CustomData_free(&dm->edgeData);
-       CustomData_free(&dm->faceData);
+       if (dm->needsFree) {
+               CustomData_free(&dm->vertData, dm->numVertData);
+               CustomData_free(&dm->edgeData, dm->numEdgeData);
+               CustomData_free(&dm->faceData, dm->numFaceData);
+
+               return 1;
+       }
+       else {
+               CustomData_free_temporary(&dm->vertData, dm->numVertData);
+               CustomData_free_temporary(&dm->edgeData, dm->numEdgeData);
+               CustomData_free_temporary(&dm->faceData, dm->numFaceData);
+
+               return 0;
+       }
 }
 
 void DM_to_mesh(DerivedMesh *dm, Mesh *me)
 {
        /* dm might depend on me, so we need to do everything with a local copy */
-       Mesh tmp_me = *me;
-       int numVerts = dm->getNumVerts(dm);
-
-       tmp_me.dvert = NULL;
-       tmp_me.tface = NULL;
-       tmp_me.mcol = NULL;
-
-       tmp_me.totvert = numVerts;
-       tmp_me.totedge = dm->getNumEdges(dm);
-       tmp_me.totface = dm->getNumFaces(dm);
-
-       tmp_me.mvert = dm->dupVertArray(dm);
-       tmp_me.medge = dm->dupEdgeArray(dm);
-       tmp_me.mface = dm->dupFaceArray(dm);
-
-       if(dm->getFaceDataArray(dm, LAYERTYPE_TFACE))
-               tmp_me.tface = MEM_dupallocN(dm->getFaceDataArray(dm,
-                                                                 LAYERTYPE_TFACE));
-       if(dm->getFaceDataArray(dm, LAYERTYPE_MCOL))
-               tmp_me.mcol = MEM_dupallocN(dm->getFaceDataArray(dm,
-                                                                LAYERTYPE_MCOL));
-       if(dm->getVertDataArray(dm, LAYERTYPE_MDEFORMVERT)) {
-               int i;
-               MDeformVert *dv;
+       Mesh tmp = *me;
+       int totvert, totedge, totface;
 
-               tmp_me.dvert = MEM_dupallocN(
-                                   dm->getVertDataArray(dm, LAYERTYPE_MDEFORMVERT));
+       memset(&tmp.vdata, 0, sizeof(tmp.vdata));
+       memset(&tmp.edata, 0, sizeof(tmp.edata));
+       memset(&tmp.fdata, 0, sizeof(tmp.fdata));
 
-               for(i = 0, dv = tmp_me.dvert; i < numVerts; ++i, ++dv)
-                       dv->dw = MEM_dupallocN(dv->dw);
-       }
+       totvert = tmp.totvert = dm->getNumVerts(dm);
+       totedge = tmp.totedge = dm->getNumEdges(dm);
+       totface = tmp.totface = dm->getNumFaces(dm);
 
-       if(me->mvert) MEM_freeN(me->mvert);
-       if(me->dvert) free_dverts(me->dvert, me->totvert);
-       if(me->mface) MEM_freeN(me->mface);
-       if(me->tface) MEM_freeN(me->tface);
-       if(me->mcol) MEM_freeN(me->mcol);
-       if(me->medge) MEM_freeN(me->medge);
+       CustomData_copy(&dm->vertData, &tmp.vdata, CD_MASK_MESH, CD_DUPLICATE, totvert);
+       CustomData_copy(&dm->edgeData, &tmp.edata, CD_MASK_MESH, CD_DUPLICATE, totedge);
+       CustomData_copy(&dm->faceData, &tmp.fdata, CD_MASK_MESH, CD_DUPLICATE, totface);
 
-       /* if the number of verts has changed, remove invalid data */
-       if(numVerts != me->totvert) {
-               if(me->msticky) MEM_freeN(me->msticky);
-               me->msticky = NULL;
+       /* not all DerivedMeshes store their verts/edges/faces in CustomData, so
+          we set them here in case they are missing */
+       if(!CustomData_has_layer(&tmp.vdata, CD_MVERT))
+               CustomData_add_layer(&tmp.vdata, CD_MVERT, CD_ASSIGN, dm->dupVertArray(dm), totvert);
+       if(!CustomData_has_layer(&tmp.edata, CD_MEDGE))
+               CustomData_add_layer(&tmp.edata, CD_MEDGE, CD_ASSIGN, dm->dupEdgeArray(dm), totedge);
+       if(!CustomData_has_layer(&tmp.fdata, CD_MFACE))
+               CustomData_add_layer(&tmp.fdata, CD_MFACE, CD_ASSIGN, dm->dupFaceArray(dm), totface);
+
+       mesh_update_customdata_pointers(&tmp);
 
+       CustomData_free(&me->vdata, me->totvert);
+       CustomData_free(&me->edata, me->totedge);
+       CustomData_free(&me->fdata, me->totface);
+
+       /* if the number of verts has changed, remove invalid data */
+       if(tmp.totvert != me->totvert) {
                if(me->key) me->key->id.us--;
                me->key = NULL;
        }
 
-       *me = tmp_me;
+       *me = tmp;
 }
 
-void DM_add_vert_layer(DerivedMesh *dm, int type, int flag, void *layer)
+void DM_set_only_copy(DerivedMesh *dm, CustomDataMask mask)
 {
-       CustomData_add_layer(&dm->vertData, type, flag, layer);
+       CustomData_set_only_copy(&dm->vertData, mask);
+       CustomData_set_only_copy(&dm->edgeData, mask);
+       CustomData_set_only_copy(&dm->faceData, mask);
 }
 
-void DM_add_edge_layer(DerivedMesh *dm, int type, int flag, void *layer)
+void DM_add_vert_layer(DerivedMesh *dm, int type, int alloctype, void *layer)
 {
-       CustomData_add_layer(&dm->edgeData, type, flag, layer);
+       CustomData_add_layer(&dm->vertData, type, alloctype, layer, dm->numVertData);
 }
 
-void DM_add_face_layer(DerivedMesh *dm, int type, int flag, void *layer)
+void DM_add_edge_layer(DerivedMesh *dm, int type, int alloctype, void *layer)
 {
-       CustomData_add_layer(&dm->faceData, type, flag, layer);
+       CustomData_add_layer(&dm->edgeData, type, alloctype, layer, dm->numEdgeData);
+}
+
+void DM_add_face_layer(DerivedMesh *dm, int type, int alloctype, void *layer)
+{
+       CustomData_add_layer(&dm->faceData, type, alloctype, layer, dm->numFaceData);
 }
 
 void *DM_get_vert_data(DerivedMesh *dm, int index, int type)
@@ -286,24 +349,21 @@ void DM_copy_vert_data(DerivedMesh *source, DerivedMesh *dest,
                        int source_index, int dest_index, int count)
 {
        CustomData_copy_data(&source->vertData, &dest->vertData,
-                            source_index, dest_index,
-                            count);
+                            source_index, dest_index, count);
 }
 
 void DM_copy_edge_data(DerivedMesh *source, DerivedMesh *dest,
                        int source_index, int dest_index, int count)
 {
        CustomData_copy_data(&source->edgeData, &dest->edgeData,
-                            source_index, dest_index,
-                            count);
+                            source_index, dest_index, count);
 }
 
 void DM_copy_face_data(DerivedMesh *source, DerivedMesh *dest,
                        int source_index, int dest_index, int count)
 {
        CustomData_copy_data(&source->faceData, &dest->faceData,
-                            source_index, dest_index,
-                            count);
+                            source_index, dest_index, count);
 }
 
 void DM_free_vert_data(struct DerivedMesh *dm, int index, int count)
@@ -335,7 +395,7 @@ void DM_interp_edge_data(DerivedMesh *source, DerivedMesh *dest,
                          int count, int dest_index)
 {
        CustomData_interp(&source->edgeData, &dest->edgeData, src_indices,
-                         weights, (float *)vert_weights, count, dest_index);
+                         weights, (float*)vert_weights, count, dest_index);
 }
 
 void DM_interp_face_data(DerivedMesh *source, DerivedMesh *dest,
@@ -344,621 +404,48 @@ void DM_interp_face_data(DerivedMesh *source, DerivedMesh *dest,
                          int count, int dest_index)
 {
        CustomData_interp(&source->faceData, &dest->faceData, src_indices,
-                         weights, (float *)vert_weights, count, dest_index);
-}
-
-typedef struct {
-       DerivedMesh dm;
-
-       Object *ob;
-       Mesh *me;
-       MVert *verts;
-       float *nors;
-       MCol *wpaintMCol;
-
-       int freeNors, freeVerts;
-} MeshDerivedMesh;
-
-static DispListMesh *meshDM_convertToDispListMesh(DerivedMesh *dm, int allowShared)
-{
-       MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
-       Mesh *me = mdm->me;
-       DispListMesh *dlm = MEM_callocN(sizeof(*dlm), "dlm");
-
-       dlm->totvert = me->totvert;
-       dlm->totedge = me->totedge;
-       dlm->totface = me->totface;
-       dlm->mvert = mdm->verts;
-       dlm->medge = me->medge;
-       dlm->mface = me->mface;
-       dlm->tface = me->tface;
-       dlm->mcol = me->mcol;
-       dlm->nors = mdm->nors;
-       dlm->dontFreeVerts = dlm->dontFreeOther = dlm->dontFreeNors = 1;
-
-       if (!allowShared) {
-               dlm->mvert = MEM_dupallocN(dlm->mvert);
-               if (dlm->nors) dlm->nors = MEM_dupallocN(dlm->nors);
-
-               dlm->dontFreeVerts = dlm->dontFreeNors = 0;
-       }
-
-       return dlm;
-}
-
-static void meshDM_getMinMax(DerivedMesh *dm, float min_r[3], float max_r[3])
-{
-       MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
-       Mesh *me = mdm->me;
-       int i;
-
-       if (me->totvert) {
-               for (i=0; i<me->totvert; i++) {
-                       DO_MINMAX(mdm->verts[i].co, min_r, max_r);
-               }
-       } else {
-               min_r[0] = min_r[1] = min_r[2] = max_r[0] = max_r[1] = max_r[2] = 0.0;
-       }
-}
-
-static void meshDM_getVertCos(DerivedMesh *dm, float (*cos_r)[3])
-{
-       MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
-       Mesh *me = mdm->me;
-       int i;
-
-       for (i=0; i<me->totvert; i++) {
-               cos_r[i][0] = mdm->verts[i].co[0];
-               cos_r[i][1] = mdm->verts[i].co[1];
-               cos_r[i][2] = mdm->verts[i].co[2];
-       }
-}
-
-static void meshDM_getVertCo(DerivedMesh *dm, int index, float co_r[3])
-{
-       MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
-
-       VECCOPY(co_r, mdm->verts[index].co);
-}
-
-static void meshDM_getVertNo(DerivedMesh *dm, int index, float no_r[3])
-{
-       MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
-       short *no = mdm->verts[index].no;
-
-       no_r[0] = no[0]/32767.f;
-       no_r[1] = no[1]/32767.f;
-       no_r[2] = no[2]/32767.f;
-}
-
-static void meshDM_drawVerts(DerivedMesh *dm)
-{
-       MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
-       Mesh *me = mdm->me;
-       int i;
-
-       glBegin(GL_POINTS);
-       for(i=0; i<me->totvert; i++) {
-               glVertex3fv(mdm->verts[i].co);
-       }
-       glEnd();
-}
-static void meshDM_drawUVEdges(DerivedMesh *dm)
-{
-       MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
-       Mesh *me = mdm->me;
-       int i;
-
-       if (me->tface) {
-               glBegin(GL_LINES);
-               for (i=0; i<me->totface; i++) {
-                       TFace *tf = &me->tface[i];
-
-                       if (!(tf->flag&TF_HIDE)) {
-                               glVertex2fv(tf->uv[0]);
-                               glVertex2fv(tf->uv[1]);
-
-                               glVertex2fv(tf->uv[1]);
-                               glVertex2fv(tf->uv[2]);
-
-                               if (!me->mface[i].v4) {
-                                       glVertex2fv(tf->uv[2]);
-                                       glVertex2fv(tf->uv[0]);
-                               } else {
-                                       glVertex2fv(tf->uv[2]);
-                                       glVertex2fv(tf->uv[3]);
-
-                                       glVertex2fv(tf->uv[3]);
-                                       glVertex2fv(tf->uv[0]);
-                               }
-                       }
-               }
-               glEnd();
-       }
-}
-static void meshDM_drawEdges(DerivedMesh *dm, int drawLooseEdges)
-{
-       MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
-       Mesh *me= mdm->me;
-       MEdge *medge= me->medge;
-       int i;
-               
-       glBegin(GL_LINES);
-       for(i=0; i<me->totedge; i++, medge++) {
-               if ((medge->flag&ME_EDGEDRAW) && (drawLooseEdges || !(medge->flag&ME_LOOSEEDGE))) {
-                       glVertex3fv(mdm->verts[medge->v1].co);
-                       glVertex3fv(mdm->verts[medge->v2].co);
-               }
-       }
-       glEnd();
-}
-static void meshDM_drawMappedEdges(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index), void *userData)
-{
-       MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
-       Mesh *me= mdm->me;
-       int i;
-               
-       glBegin(GL_LINES);
-       for (i=0; i<me->totedge; i++) {
-               if (!setDrawOptions || setDrawOptions(userData, i)) {
-                       glVertex3fv(mdm->verts[me->medge[i].v1].co);
-                       glVertex3fv(mdm->verts[me->medge[i].v2].co);
-               }
-       }
-       glEnd();
-}
-static void meshDM_drawLooseEdges(DerivedMesh *dm)
-{
-       MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
-       Mesh *me= mdm->me;
-       MEdge *medge= me->medge;
-       int i;
-
-       glBegin(GL_LINES);
-       for (i=0; i<me->totedge; i++, medge++) {
-               if (medge->flag&ME_LOOSEEDGE) {
-                       glVertex3fv(mdm->verts[medge->v1].co);
-                       glVertex3fv(mdm->verts[medge->v2].co);
-               }
-       }
-       glEnd();
-}
-static void meshDM_drawFacesSolid(DerivedMesh *dm, int (*setMaterial)(int))
-{
-       MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
-       Mesh *me = mdm->me;
-       MVert *mvert= mdm->verts;
-       MFace *mface= me->mface;
-       float *nors = mdm->nors;
-       int a;
-       int glmode=-1, shademodel=-1, matnr=-1, drawCurrentMat=1;
-
-#define PASSVERT(index) {                                              \
-       if (shademodel==GL_SMOOTH) {                            \
-               short *no = mvert[index].no;                    \
-               glNormal3sv(no);                                                \
-       }                                                                                       \
-       glVertex3fv(mvert[index].co);   \
-}
-
-       glBegin(glmode=GL_QUADS);
-       for(a=0; a<me->totface; a++, mface++, nors+=3) {
-               int new_glmode, new_matnr, new_shademodel;
-                       
-               new_glmode = mface->v4?GL_QUADS:GL_TRIANGLES;
-               new_matnr = mface->mat_nr+1;
-               new_shademodel = (mface->flag & ME_SMOOTH)?GL_SMOOTH:GL_FLAT;
-               
-               if (new_glmode!=glmode || new_matnr!=matnr || new_shademodel!=shademodel) {
-                       glEnd();
-
-                       drawCurrentMat = setMaterial(matnr=new_matnr);
-
-                       glShadeModel(shademodel=new_shademodel);
-                       glBegin(glmode=new_glmode);
-               } 
-               
-               if (drawCurrentMat) {
-                       if(shademodel==GL_FLAT) 
-                               glNormal3fv(nors);
-
-                       PASSVERT(mface->v1);
-                       PASSVERT(mface->v2);
-                       PASSVERT(mface->v3);
-                       if (mface->v4) {
-                               PASSVERT(mface->v4);
-                       }
-               }
-       }
-       glEnd();
-
-       glShadeModel(GL_FLAT);
-#undef PASSVERT
-}
-
-static void meshDM_drawFacesColored(DerivedMesh *dm, int useTwoSide, unsigned char *col1, unsigned char *col2)
-{
-       MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
-       Mesh *me= mdm->me;
-       MFace *mface= me->mface;
-       int a, glmode;
-       unsigned char *cp1, *cp2;
-
-       cp1= col1;
-       if(col2) {
-               cp2= col2;
-       } else {
-               cp2= NULL;
-               useTwoSide= 0;
-       }
-
-       /* there's a conflict here... twosided colors versus culling...? */
-       /* defined by history, only texture faces have culling option */
-       /* we need that as mesh option builtin, next to double sided lighting */
-       if(col1 && col2)
-               glEnable(GL_CULL_FACE);
-       
-       glShadeModel(GL_SMOOTH);
-       glBegin(glmode=GL_QUADS);
-       for(a=0; a<me->totface; a++, mface++, cp1+= 16) {
-               int new_glmode= mface->v4?GL_QUADS:GL_TRIANGLES;
-
-               if (new_glmode!=glmode) {
-                       glEnd();
-                       glBegin(glmode= new_glmode);
-               }
-                       
-               glColor3ub(cp1[3], cp1[2], cp1[1]);
-               glVertex3fv( mdm->verts[mface->v1].co );
-               glColor3ub(cp1[7], cp1[6], cp1[5]);
-               glVertex3fv( mdm->verts[mface->v2].co );
-               glColor3ub(cp1[11], cp1[10], cp1[9]);
-               glVertex3fv( mdm->verts[mface->v3].co );
-               if(mface->v4) {
-                       glColor3ub(cp1[15], cp1[14], cp1[13]);
-                       glVertex3fv( mdm->verts[mface->v4].co );
-               }
-                       
-               if(useTwoSide) {
-                       glColor3ub(cp2[11], cp2[10], cp2[9]);
-                       glVertex3fv( mdm->verts[mface->v3].co );
-                       glColor3ub(cp2[7], cp2[6], cp2[5]);
-                       glVertex3fv( mdm->verts[mface->v2].co );
-                       glColor3ub(cp2[3], cp2[2], cp2[1]);
-                       glVertex3fv( mdm->verts[mface->v1].co );
-                       if(mface->v4) {
-                               glColor3ub(cp2[15], cp2[14], cp2[13]);
-                               glVertex3fv( mdm->verts[mface->v4].co );
-                       }
-               }
-               if(col2) cp2+= 16;
-       }
-       glEnd();
-
-       glShadeModel(GL_FLAT);
-       glDisable(GL_CULL_FACE);
-}
-
-static void meshDM_drawFacesTex_common(DerivedMesh *dm, int (*drawParams)(TFace *tface, int matnr), int (*drawParamsMapped)(void *userData, int index), void *userData) 
-{
-       MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
-       Mesh *me = mdm->me;
-       MVert *mvert= mdm->verts;
-       MFace *mface= me->mface;
-       TFace *tface = me->tface;
-       float *nors = mdm->nors;
-       int i;
-
-       for (i=0; i<me->totface; i++) {
-               MFace *mf= &mface[i];
-               TFace *tf = tface?&tface[i]:NULL;
-               int flag;
-               unsigned char *cp= NULL;
-               
-               if (drawParams)
-                       flag = drawParams(tf, mf->mat_nr);
-               else
-                       flag = drawParamsMapped(userData, i);
-
-               if (flag==0) {
-                       continue;
-               } else if (flag==1) {
-                       if (mdm->wpaintMCol) {
-                               cp= (unsigned char*) &mdm->wpaintMCol[i*4];
-                       } else if (tf) {
-                               cp= (unsigned char*) tf->col;
-                       } else if (me->mcol) {
-                               cp= (unsigned char*) &me->mcol[i*4];
-                       }
-               }
-
-               if (!(mf->flag&ME_SMOOTH)) {
-                       glNormal3fv(&nors[i*3]);
-               }
-
-               glBegin(mf->v4?GL_QUADS:GL_TRIANGLES);
-               if (tf) glTexCoord2fv(tf->uv[0]);
-               if (cp) glColor3ub(cp[3], cp[2], cp[1]);
-               if (mf->flag&ME_SMOOTH) glNormal3sv(mvert[mf->v1].no);
-               glVertex3fv(mvert[mf->v1].co);
-                       
-               if (tf) glTexCoord2fv(tf->uv[1]);
-               if (cp) glColor3ub(cp[7], cp[6], cp[5]);
-               if (mf->flag&ME_SMOOTH) glNormal3sv(mvert[mf->v2].no);
-               glVertex3fv(mvert[mf->v2].co);
-
-               if (tf) glTexCoord2fv(tf->uv[2]);
-               if (cp) glColor3ub(cp[11], cp[10], cp[9]);
-               if (mf->flag&ME_SMOOTH) glNormal3sv(mvert[mf->v3].no);
-               glVertex3fv(mvert[mf->v3].co);
-
-               if(mf->v4) {
-                       if (tf) glTexCoord2fv(tf->uv[3]);
-                       if (cp) glColor3ub(cp[15], cp[14], cp[13]);
-                       if (mf->flag&ME_SMOOTH) glNormal3sv(mvert[mf->v4].no);
-                       glVertex3fv(mvert[mf->v4].co);
-               }
-               glEnd();
-       }
-}
-static void meshDM_drawFacesTex(DerivedMesh *dm, int (*setDrawParams)(TFace *tface, int matnr)) 
-{
-       meshDM_drawFacesTex_common(dm, setDrawParams, NULL, NULL);
-}
-static void meshDM_drawMappedFacesTex(DerivedMesh *dm, int (*setDrawParams)(void *userData, int index), void *userData) 
-{
-       meshDM_drawFacesTex_common(dm, NULL, setDrawParams, userData);
-}
-
-static void meshDM_drawMappedFaces(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index, int *drawSmooth_r), void *userData, int useColors) 
-{
-       MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
-       Mesh *me = mdm->me;
-       MVert *mvert= mdm->verts;
-       MFace *mface= me->mface;
-       float *nors= mdm->nors;
-       int i;
-
-       for (i=0; i<me->totface; i++) {
-               MFace *mf= &mface[i];
-               int drawSmooth = (mf->flag & ME_SMOOTH);
-
-               if (!setDrawOptions || setDrawOptions(userData, i, &drawSmooth)) {
-                       unsigned char *cp = NULL;
-
-                       if (useColors) {
-                               if (mdm->wpaintMCol) {
-                                       cp= (unsigned char*) &mdm->wpaintMCol[i*4];
-                               } else if (me->tface) {
-                                       cp= (unsigned char*) me->tface[i].col;
-                               } else if (me->mcol) {
-                                       cp= (unsigned char*) &me->mcol[i*4];
-                               }
-                       }
-
-                       glShadeModel(drawSmooth?GL_SMOOTH:GL_FLAT);
-                       glBegin(mf->v4?GL_QUADS:GL_TRIANGLES);
-
-                       if (!drawSmooth) {
-                               glNormal3fv(&nors[i*3]);
-
-                               if (cp) glColor3ub(cp[3], cp[2], cp[1]);
-                               glVertex3fv(mvert[mf->v1].co);
-                               if (cp) glColor3ub(cp[7], cp[6], cp[5]);
-                               glVertex3fv(mvert[mf->v2].co);
-                               if (cp) glColor3ub(cp[11], cp[10], cp[9]);
-                               glVertex3fv(mvert[mf->v3].co);
-                               if(mf->v4) {
-                                       if (cp) glColor3ub(cp[15], cp[14], cp[13]);
-                                       glVertex3fv(mvert[mf->v4].co);
-                               }
-                       } else {
-                               if (cp) glColor3ub(cp[3], cp[2], cp[1]);
-                               glNormal3sv(mvert[mf->v1].no);
-                               glVertex3fv(mvert[mf->v1].co);
-                               if (cp) glColor3ub(cp[7], cp[6], cp[5]);
-                               glNormal3sv(mvert[mf->v2].no);
-                               glVertex3fv(mvert[mf->v2].co);
-                               if (cp) glColor3ub(cp[11], cp[10], cp[9]);
-                               glNormal3sv(mvert[mf->v3].no);
-                               glVertex3fv(mvert[mf->v3].co);
-                               if(mf->v4) {
-                                       if (cp) glColor3ub(cp[15], cp[14], cp[13]);
-                                       glNormal3sv(mvert[mf->v4].no);
-                                       glVertex3fv(mvert[mf->v4].co);
-                               }
-                       }
-
-                       glEnd();
-               }
-       }
-}
-static int meshDM_getNumVerts(DerivedMesh *dm)
-{
-       MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
-       Mesh *me = mdm->me;
-
-       return me->totvert;
-}
-
-static int meshDM_getNumEdges(DerivedMesh *dm)
-{
-       MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
-       Mesh *me = mdm->me;
-
-       return me->totedge;
-}
-
-static int meshDM_getNumFaces(DerivedMesh *dm)
-{
-       MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
-       Mesh *me = mdm->me;
-
-       return me->totface;
-}
-
-void meshDM_getVert(DerivedMesh *dm, int index, MVert *vert_r)
-{
-       MVert *verts = ((MeshDerivedMesh *)dm)->verts;
-
-       *vert_r = verts[index];
-}
-
-void meshDM_getEdge(DerivedMesh *dm, int index, MEdge *edge_r)
-{
-       Mesh *me = ((MeshDerivedMesh *)dm)->me;
-
-       *edge_r = me->medge[index];
-}
-
-void meshDM_getFace(DerivedMesh *dm, int index, MFace *face_r)
-{
-       Mesh *me = ((MeshDerivedMesh *)dm)->me;
-
-       *face_r = me->mface[index];
-}
-
-void meshDM_getVertArray(DerivedMesh *dm, MVert *vert_r)
-{
-       MeshDerivedMesh *mdm = (MeshDerivedMesh *)dm;
-       memcpy(vert_r, mdm->verts, sizeof(*vert_r) * mdm->me->totvert);
-}
-
-void meshDM_getEdgeArray(DerivedMesh *dm, MEdge *edge_r)
-{
-       MeshDerivedMesh *mdm = (MeshDerivedMesh *)dm;
-       memcpy(edge_r, mdm->me->medge, sizeof(*edge_r) * mdm->me->totedge);
-}
-
-void meshDM_getFaceArray(DerivedMesh *dm, MFace *face_r)
-{
-       MeshDerivedMesh *mdm = (MeshDerivedMesh *)dm;
-       memcpy(face_r, mdm->me->mface, sizeof(*face_r) * mdm->me->totface);
+                         weights, (float*)vert_weights, count, dest_index);
 }
 
-static void meshDM_release(DerivedMesh *dm)
+void DM_swap_face_data(DerivedMesh *dm, int index, int *corner_indices)
 {
-       MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
-
-       DM_release(dm);
-
-       if (mdm->wpaintMCol) MEM_freeN(mdm->wpaintMCol);
-       if (mdm->freeNors) MEM_freeN(mdm->nors);
-       if (mdm->freeVerts) MEM_freeN(mdm->verts);
-       MEM_freeN(mdm);
+       CustomData_swap(&dm->faceData, index, corner_indices);
 }
 
 static DerivedMesh *getMeshDerivedMesh(Mesh *me, Object *ob, float (*vertCos)[3])
 {
-       MeshDerivedMesh *mdm = MEM_callocN(sizeof(*mdm), "mdm");
-
-       DM_init(&mdm->dm, me->totvert, me->totedge, me->totface);
+       DerivedMesh *dm = CDDM_from_mesh(me, ob);
+       int i, dofluidsim;
 
-       mdm->dm.getMinMax = meshDM_getMinMax;
+       dofluidsim = ((ob->fluidsimFlag & OB_FLUIDSIM_ENABLE) &&
+                     (ob->fluidsimSettings->type & OB_FLUIDSIM_DOMAIN)&&
+                     (ob->fluidsimSettings->meshSurface) &&
+                     (1) && (!give_parteff(ob)) && // doesnt work together with particle systems!
+                     (me->totvert == ((Mesh *)(ob->fluidsimSettings->meshSurface))->totvert));
 
-       mdm->dm.convertToDispListMesh = meshDM_convertToDispListMesh;
-       mdm->dm.getNumVerts = meshDM_getNumVerts;
-       mdm->dm.getNumEdges = meshDM_getNumEdges;
-       mdm->dm.getNumFaces = meshDM_getNumFaces;
+       if (vertCos && !dofluidsim)
+               CDDM_apply_vert_coords(dm, vertCos);
 
-       mdm->dm.getVert = meshDM_getVert;
-       mdm->dm.getEdge = meshDM_getEdge;
-       mdm->dm.getFace = meshDM_getFace;
-       mdm->dm.getVertArray = meshDM_getVertArray;
-       mdm->dm.getEdgeArray = meshDM_getEdgeArray;
-       mdm->dm.getFaceArray = meshDM_getFaceArray;
+       CDDM_calc_normals(dm);
 
-       mdm->dm.getVertCos = meshDM_getVertCos;
-       mdm->dm.getVertCo = meshDM_getVertCo;
-       mdm->dm.getVertNo = meshDM_getVertNo;
-
-       mdm->dm.drawVerts = meshDM_drawVerts;
-
-       mdm->dm.drawUVEdges = meshDM_drawUVEdges;
-       mdm->dm.drawEdges = meshDM_drawEdges;
-       mdm->dm.drawLooseEdges = meshDM_drawLooseEdges;
-       
-       mdm->dm.drawFacesSolid = meshDM_drawFacesSolid;
-       mdm->dm.drawFacesColored = meshDM_drawFacesColored;
-       mdm->dm.drawFacesTex = meshDM_drawFacesTex;
-       mdm->dm.drawMappedFaces = meshDM_drawMappedFaces;
-       mdm->dm.drawMappedFacesTex = meshDM_drawMappedFacesTex;
-
-       mdm->dm.drawMappedEdges = meshDM_drawMappedEdges;
-       mdm->dm.drawMappedFaces = meshDM_drawMappedFaces;
-
-       mdm->dm.release = meshDM_release;
-
-       /* add appropriate data layers (don't copy, just reference) */
-       if(me->msticky)
-               DM_add_vert_layer(&mdm->dm, LAYERTYPE_MSTICKY,
-                                 LAYERFLAG_NOFREE, me->msticky);
-       if(me->dvert)
-               DM_add_vert_layer(&mdm->dm, LAYERTYPE_MDEFORMVERT,
-                                 LAYERFLAG_NOFREE, me->dvert);
-
-       if(me->tface)
-               DM_add_face_layer(&mdm->dm, LAYERTYPE_TFACE,
-                                 LAYERFLAG_NOFREE, me->tface);
-       if(me->mcol)
-               DM_add_face_layer(&mdm->dm, LAYERTYPE_MCOL,
-                                 LAYERFLAG_NOFREE, me->mcol);
-
-               /* Works in conjunction with hack during modifier calc */
-       if ((G.f & G_WEIGHTPAINT) && ob==(G.scene->basact?G.scene->basact->object:NULL)) {
-               mdm->wpaintMCol = MEM_dupallocN(me->mcol);
-       }
-
-       mdm->ob = ob;
-       mdm->me = me;
-       mdm->verts = me->mvert;
-       mdm->nors = NULL;
-       mdm->freeNors = 0;
-       mdm->freeVerts = 0;
-
-       if((ob->fluidsimFlag & OB_FLUIDSIM_ENABLE) &&
-                (ob->fluidsimSettings->type & OB_FLUIDSIM_DOMAIN)&&
-          (ob->fluidsimSettings->meshSurface) &&
-                (1) && (!give_parteff(ob)) && // doesnt work together with particle systems!
-                (me->totvert == ((Mesh *)(ob->fluidsimSettings->meshSurface))->totvert) ) {
-               // dont recompute for fluidsim mesh, use from readBobjgz
+       /* apply fluidsim normals */    
+       if (dofluidsim) {
+               // use normals from readBobjgz
                // TODO? check for modifiers!?
-               int i;
-               mesh_calc_normals(mdm->verts, me->totvert, me->mface, me->totface, &mdm->nors);
-               mdm->freeNors = 1;
+               MVert *fsvert = ob->fluidsimSettings->meshSurfNormals;
+               short (*normals)[3] = MEM_mallocN(sizeof(short)*3*me->totvert, "fluidsim nor");
+
                for (i=0; i<me->totvert; i++) {
-                       MVert *mv= &mdm->verts[i];
-                       MVert *fsv; 
-                       fsv = &ob->fluidsimSettings->meshSurfNormals[i];
-                       VECCOPY(mv->no, fsv->no);
+                       VECCOPY(normals[i], fsvert[i].no);
                        //mv->no[0]= 30000; mv->no[1]= mv->no[2]= 0; // DEBUG fixed test normals
                }
-       } else {
-               // recompute normally
 
-               if (vertCos) {
-                       int i;
+               CDDM_apply_vert_normals(dm, normals);
 
-                       /* copy the original verts to preserve flag settings; if this is too
-                        * costly, must at least use MEM_callocN to clear flags */
-                       mdm->verts = MEM_dupallocN( me->mvert );
-                       for (i=0; i<me->totvert; i++) {
-                               VECCOPY(mdm->verts[i].co, vertCos[i]);
-                       }
-                       mesh_calc_normals(mdm->verts, me->totvert, me->mface, me->totface, &mdm->nors);
-                       mdm->freeNors = 1;
-                       mdm->freeVerts = 1;
-               } else {
-                       // XXX this is kinda hacky because we shouldn't really be editing
-                       // the mesh here, however, we can't just call mesh_build_faceNormals(ob)
-                       // because in the case when a key is applied to a mesh the vertex normals
-                       // would never be correctly computed.
-                       mesh_calc_normals(mdm->verts, me->totvert, me->mface, me->totface, &mdm->nors);
-                       mdm->freeNors = 1;
-               }
-       } // fs TEST
+               MEM_freeN(normals);
+       }
 
-       return (DerivedMesh*) mdm;
+       return dm;
 }
 
 ///
@@ -993,14 +480,12 @@ static void emDM_foreachMappedEdge(DerivedMesh *dm, void (*func)(void *userData,
        int i;
 
        if (emdm->vertexCos) {
-               EditVert *eve, *preveve;
+               EditVert *eve;
 
                for (i=0,eve=emdm->em->verts.first; eve; eve= eve->next)
-                       eve->prev = (EditVert*) i++;
+                       eve->tmp.l = (long) i++;
                for(i=0,eed= emdm->em->edges.first; eed; i++,eed= eed->next)
-                       func(userData, i, emdm->vertexCos[(int) eed->v1->prev], emdm->vertexCos[(int) eed->v2->prev]);
-               for (preveve=NULL, eve=emdm->em->verts.first; eve; preveve=eve, eve= eve->next)
-                       eve->prev = preveve;
+                       func(userData, i, emdm->vertexCos[(int) eed->v1->tmp.l], emdm->vertexCos[(int) eed->v2->tmp.l]);
        } else {
                for(i=0,eed= emdm->em->edges.first; eed; i++,eed= eed->next)
                        func(userData, i, eed->v1->co, eed->v2->co);
@@ -1013,22 +498,19 @@ static void emDM_drawMappedEdges(DerivedMesh *dm, int (*setDrawOptions)(void *us
        int i;
 
        if (emdm->vertexCos) {
-               EditVert *eve, *preveve;
+               EditVert *eve;
 
                for (i=0,eve=emdm->em->verts.first; eve; eve= eve->next)
-                       eve->prev = (EditVert*) i++;
+                       eve->tmp.l = (long) i++;
 
                glBegin(GL_LINES);
                for(i=0,eed= emdm->em->edges.first; eed; i++,eed= eed->next) {
                        if(!setDrawOptions || setDrawOptions(userData, i)) {
-                               glVertex3fv(emdm->vertexCos[(int) eed->v1->prev]);
-                               glVertex3fv(emdm->vertexCos[(int) eed->v2->prev]);
+                               glVertex3fv(emdm->vertexCos[(int) eed->v1->tmp.l]);
+                               glVertex3fv(emdm->vertexCos[(int) eed->v2->tmp.l]);
                        }
                }
                glEnd();
-
-               for (preveve=NULL, eve=emdm->em->verts.first; eve; preveve=eve, eve= eve->next)
-                       eve->prev = preveve;
        } else {
                glBegin(GL_LINES);
                for(i=0,eed= emdm->em->edges.first; eed; i++,eed= eed->next) {
@@ -1051,24 +533,21 @@ static void emDM_drawMappedEdgesInterp(DerivedMesh *dm, int (*setDrawOptions)(vo
        int i;
 
        if (emdm->vertexCos) {
-               EditVert *eve, *preveve;
+               EditVert *eve;
 
                for (i=0,eve=emdm->em->verts.first; eve; eve= eve->next)
-                       eve->prev = (EditVert*) i++;
+                       eve->tmp.l = (long) i++;
 
                glBegin(GL_LINES);
                for (i=0,eed= emdm->em->edges.first; eed; i++,eed= eed->next) {
                        if(!setDrawOptions || setDrawOptions(userData, i)) {
                                setDrawInterpOptions(userData, i, 0.0);
-                               glVertex3fv(emdm->vertexCos[(int) eed->v1->prev]);
+                               glVertex3fv(emdm->vertexCos[(int) eed->v1->tmp.l]);
                                setDrawInterpOptions(userData, i, 1.0);
-                               glVertex3fv(emdm->vertexCos[(int) eed->v2->prev]);
+                               glVertex3fv(emdm->vertexCos[(int) eed->v2->tmp.l]);
                        }
                }
                glEnd();
-
-               for (preveve=NULL, eve=emdm->em->verts.first; eve; preveve=eve, eve= eve->next)
-                       eve->prev = preveve;
        } else {
                glBegin(GL_LINES);
                for (i=0,eed= emdm->em->edges.first; eed; i++,eed= eed->next) {
@@ -1087,24 +566,27 @@ static void emDM_drawUVEdges(DerivedMesh *dm)
 {
        EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
        EditFace *efa;
+       MTFace *tf;
 
        glBegin(GL_LINES);
        for(efa= emdm->em->faces.first; efa; efa= efa->next) {
-               if(!(efa->tf.flag&TF_HIDE)) {
-                       glVertex2fv(efa->tf.uv[0]);
-                       glVertex2fv(efa->tf.uv[1]);
+               tf = CustomData_em_get(&emdm->em->fdata, efa->data, CD_MTFACE);
+
+               if(tf && !(efa->h)) {
+                       glVertex2fv(tf->uv[0]);
+                       glVertex2fv(tf->uv[1]);
 
-                       glVertex2fv(efa->tf.uv[1]);
-                       glVertex2fv(efa->tf.uv[2]);
+                       glVertex2fv(tf->uv[1]);
+                       glVertex2fv(tf->uv[2]);
 
                        if (!efa->v4) {
-                               glVertex2fv(efa->tf.uv[2]);
-                               glVertex2fv(efa->tf.uv[0]);
+                               glVertex2fv(tf->uv[2]);
+                               glVertex2fv(tf->uv[0]);
                        } else {
-                               glVertex2fv(efa->tf.uv[2]);
-                               glVertex2fv(efa->tf.uv[3]);
-                               glVertex2fv(efa->tf.uv[3]);
-                               glVertex2fv(efa->tf.uv[0]);
+                               glVertex2fv(tf->uv[2]);
+                               glVertex2fv(tf->uv[3]);
+                               glVertex2fv(tf->uv[3]);
+                               glVertex2fv(tf->uv[0]);
                        }
                }
        }
@@ -1114,10 +596,10 @@ static void emDM_drawUVEdges(DerivedMesh *dm)
 static void emDM__calcFaceCent(EditFace *efa, float cent[3], float (*vertexCos)[3])
 {
        if (vertexCos) {
-               VECCOPY(cent, vertexCos[(int) efa->v1->prev]);
-               VecAddf(cent, cent, vertexCos[(int) efa->v2->prev]);
-               VecAddf(cent, cent, vertexCos[(int) efa->v3->prev]);
-               if (efa->v4) VecAddf(cent, cent, vertexCos[(int) efa->v4->prev]);
+               VECCOPY(cent, vertexCos[(int) efa->v1->tmp.l]);
+               VecAddf(cent, cent, vertexCos[(int) efa->v2->tmp.l]);
+               VecAddf(cent, cent, vertexCos[(int) efa->v3->tmp.l]);
+               if (efa->v4) VecAddf(cent, cent, vertexCos[(int) efa->v4->tmp.l]);
        } else {
                VECCOPY(cent, efa->v1->co);
                VecAddf(cent, cent, efa->v2->co);
@@ -1134,25 +616,20 @@ static void emDM__calcFaceCent(EditFace *efa, float cent[3], float (*vertexCos)[
 static void emDM_foreachMappedFaceCenter(DerivedMesh *dm, void (*func)(void *userData, int index, float *co, float *no), void *userData)
 {
        EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
-       EditVert *eve, *preveve;
+       EditVert *eve;
        EditFace *efa;
        float cent[3];
        int i;
 
        if (emdm->vertexCos) {
                for (i=0,eve=emdm->em->verts.first; eve; eve= eve->next)
-                       eve->prev = (EditVert*) i++;
+                       eve->tmp.l = (long) i++;
        }
 
        for(i=0,efa= emdm->em->faces.first; efa; i++,efa= efa->next) {
                emDM__calcFaceCent(efa, cent, emdm->vertexCos);
                func(userData, i, cent, emdm->vertexCos?emdm->faceNos[i]:efa->n);
        }
-
-       if (emdm->vertexCos) {
-               for (preveve=NULL, eve=emdm->em->verts.first; eve; preveve=eve, eve= eve->next)
-                       eve->prev = preveve;
-       }
 }
 static void emDM_drawMappedFaces(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index, int *drawSmooth_r), void *userData, int useColors)
 {
@@ -1161,10 +638,10 @@ static void emDM_drawMappedFaces(DerivedMesh *dm, int (*setDrawOptions)(void *us
        int i;
 
        if (emdm->vertexCos) {
-               EditVert *eve, *preveve;
+               EditVert *eve;
 
                for (i=0,eve=emdm->em->verts.first; eve; eve= eve->next)
-                       eve->prev = (EditVert*) i++;
+                       eve->tmp.l = (long) i++;
 
                for (i=0,efa= emdm->em->faces.first; efa; i++,efa= efa->next) {
                        int drawSmooth = (efa->flag & ME_SMOOTH);
@@ -1174,28 +651,25 @@ static void emDM_drawMappedFaces(DerivedMesh *dm, int (*setDrawOptions)(void *us
                                glBegin(efa->v4?GL_QUADS:GL_TRIANGLES);
                                if (!drawSmooth) {
                                        glNormal3fv(emdm->faceNos[i]);
-                                       glVertex3fv(emdm->vertexCos[(int) efa->v1->prev]);
-                                       glVertex3fv(emdm->vertexCos[(int) efa->v2->prev]);
-                                       glVertex3fv(emdm->vertexCos[(int) efa->v3->prev]);
-                                       if(efa->v4) glVertex3fv(emdm->vertexCos[(int) efa->v4->prev]);
+                                       glVertex3fv(emdm->vertexCos[(int) efa->v1->tmp.l]);
+                                       glVertex3fv(emdm->vertexCos[(int) efa->v2->tmp.l]);
+                                       glVertex3fv(emdm->vertexCos[(int) efa->v3->tmp.l]);
+                                       if(efa->v4) glVertex3fv(emdm->vertexCos[(int) efa->v4->tmp.l]);
                                } else {
-                                       glNormal3fv(emdm->vertexNos[(int) efa->v1->prev]);
-                                       glVertex3fv(emdm->vertexCos[(int) efa->v1->prev]);
-                                       glNormal3fv(emdm->vertexNos[(int) efa->v2->prev]);
-                                       glVertex3fv(emdm->vertexCos[(int) efa->v2->prev]);
-                                       glNormal3fv(emdm->vertexNos[(int) efa->v3->prev]);
-                                       glVertex3fv(emdm->vertexCos[(int) efa->v3->prev]);
+                                       glNormal3fv(emdm->vertexNos[(int) efa->v1->tmp.l]);
+                                       glVertex3fv(emdm->vertexCos[(int) efa->v1->tmp.l]);
+                                       glNormal3fv(emdm->vertexNos[(int) efa->v2->tmp.l]);
+                                       glVertex3fv(emdm->vertexCos[(int) efa->v2->tmp.l]);
+                                       glNormal3fv(emdm->vertexNos[(int) efa->v3->tmp.l]);
+                                       glVertex3fv(emdm->vertexCos[(int) efa->v3->tmp.l]);
                                        if(efa->v4) {
-                                               glNormal3fv(emdm->vertexNos[(int) efa->v4->prev]);
-                                               glVertex3fv(emdm->vertexCos[(int) efa->v4->prev]);
+                                               glNormal3fv(emdm->vertexNos[(int) efa->v4->tmp.l]);
+                                               glVertex3fv(emdm->vertexCos[(int) efa->v4->tmp.l]);
                                        }
                                }
                                glEnd();
                        }
                }
-
-               for (preveve=NULL, eve=emdm->em->verts.first; eve; preveve=eve, eve= eve->next)
-                       eve->prev = preveve;
        } else {
                for (i=0,efa= emdm->em->faces.first; efa; i++,efa= efa->next) {
                        int drawSmooth = (efa->flag & ME_SMOOTH);
@@ -1356,10 +830,10 @@ void emDM_getFace(DerivedMesh *dm, int index, MFace *face_r)
                }
        }
 
-       test_index_face(face_r, NULL, NULL, ef->v4?4:3);
+       test_index_face(face_r, NULL, 0, ef->v4?4:3);
 }
 
-void emDM_getVertArray(DerivedMesh *dm, MVert *vert_r)
+void emDM_copyVertArray(DerivedMesh *dm, MVert *vert_r)
 {
        EditVert *ev = ((EditMeshDerivedMesh *)dm)->em->verts.first;
 
@@ -1376,16 +850,16 @@ void emDM_getVertArray(DerivedMesh *dm, MVert *vert_r)
        }
 }
 
-void emDM_getEdgeArray(DerivedMesh *dm, MEdge *edge_r)
+void emDM_copyEdgeArray(DerivedMesh *dm, MEdge *edge_r)
 {
        EditMesh *em = ((EditMeshDerivedMesh *)dm)->em;
        EditEdge *ee = em->edges.first;
-       EditVert *ev, *prevev;
+       EditVert *ev;
        int i;
 
-       /* store vert indices in the prev pointer (kind of hacky) */
+       /* store vertex indices in tmp union */
        for(ev = em->verts.first, i = 0; ev; ev = ev->next, ++i)
-               ev->prev = (EditVert*) i++;
+               ev->tmp.l = (long) i++;
 
        for( ; ee; ee = ee->next, ++edge_r) {
                edge_r->crease = (unsigned char) (ee->crease*255.0f);
@@ -1398,728 +872,143 @@ void emDM_getEdgeArray(DerivedMesh *dm, MEdge *edge_r)
                if (!ee->f2) edge_r->flag |= ME_LOOSEEDGE;
 #endif
 
-               edge_r->v1 = (int)ee->v1->prev;
-               edge_r->v2 = (int)ee->v2->prev;
+               edge_r->v1 = (int)ee->v1->tmp.l;
+               edge_r->v2 = (int)ee->v2->tmp.l;
        }
-
-       /* restore prev pointers */
-       for(prevev = NULL, ev = em->verts.first; ev; prevev = ev, ev = ev->next)
-               ev->prev = prevev;
-}
-
-void emDM_getFaceArray(DerivedMesh *dm, MFace *face_r)
-{
-       EditMesh *em = ((EditMeshDerivedMesh *)dm)->em;
-       EditFace *ef = em->faces.first;
-       EditVert *ev, *prevev;
-       int i;
-
-       /* store vert indices in the prev pointer (kind of hacky) */
-       for(ev = em->verts.first, i = 0; ev; ev = ev->next, ++i)
-               ev->prev = (EditVert*) i++;
-
-       for( ; ef; ef = ef->next, ++face_r) {
-               face_r->mat_nr = ef->mat_nr;
-               face_r->flag = ef->flag;
-
-               face_r->v1 = (int)ef->v1->prev;
-               face_r->v2 = (int)ef->v2->prev;
-               face_r->v3 = (int)ef->v3->prev;
-               if(ef->v4) face_r->v4 = (int)ef->v4->prev;
-               else face_r->v4 = 0;
-
-               test_index_face(face_r, NULL, NULL, ef->v4?4:3);
-       }
-
-       /* restore prev pointers */
-       for(prevev = NULL, ev = em->verts.first; ev; prevev = ev, ev = ev->next)
-               ev->prev = prevev;
-}
-
-static void emDM_release(DerivedMesh *dm)
-{
-       EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
-
-       DM_release(dm);
-
-       if (emdm->vertexCos) {
-               MEM_freeN(emdm->vertexCos);
-               MEM_freeN(emdm->vertexNos);
-               MEM_freeN(emdm->faceNos);
-       }
-
-       MEM_freeN(emdm);
-}
-
-static DerivedMesh *getEditMeshDerivedMesh(EditMesh *em, Object *ob,
-                                           float (*vertexCos)[3])
-{
-       EditMeshDerivedMesh *emdm = MEM_callocN(sizeof(*emdm), "emdm");
-       Mesh *me = ob->data;
-
-       DM_init(&emdm->dm, BLI_countlist(&em->verts),
-                        BLI_countlist(&em->edges), BLI_countlist(&em->faces));
-
-       emdm->dm.getMinMax = emDM_getMinMax;
-
-       emdm->dm.getNumVerts = emDM_getNumVerts;
-       emdm->dm.getNumEdges = emDM_getNumEdges;
-       emdm->dm.getNumFaces = emDM_getNumFaces;
-
-       emdm->dm.getVert = emDM_getVert;
-       emdm->dm.getEdge = emDM_getEdge;
-       emdm->dm.getFace = emDM_getFace;
-       emdm->dm.getVertArray = emDM_getVertArray;
-       emdm->dm.getEdgeArray = emDM_getEdgeArray;
-       emdm->dm.getFaceArray = emDM_getFaceArray;
-
-       emdm->dm.foreachMappedVert = emDM_foreachMappedVert;
-       emdm->dm.foreachMappedEdge = emDM_foreachMappedEdge;
-       emdm->dm.foreachMappedFaceCenter = emDM_foreachMappedFaceCenter;
-
-       emdm->dm.drawEdges = emDM_drawEdges;
-       emdm->dm.drawMappedEdges = emDM_drawMappedEdges;
-       emdm->dm.drawMappedEdgesInterp = emDM_drawMappedEdgesInterp;
-       emdm->dm.drawMappedFaces = emDM_drawMappedFaces;
-       emdm->dm.drawUVEdges = emDM_drawUVEdges;
-
-       emdm->dm.release = emDM_release;
-       
-       emdm->em = em;
-       emdm->vertexCos = vertexCos;
-
-       if(me->dvert) {
-               EditVert *eve;
-               int i;
-               DM_add_vert_layer(&emdm->dm, LAYERTYPE_MDEFORMVERT, 0, NULL);
-
-               for(eve = em->verts.first, i = 0; eve; eve = eve->next, ++i) {
-                       if(eve->keyindex != -1)
-                               DM_set_vert_data(&emdm->dm, i, LAYERTYPE_MDEFORMVERT,
-                                                &me->dvert[eve->keyindex]);
-               }
-       }
-
-       if(vertexCos) {
-               EditVert *eve, *preveve;
-               EditFace *efa;
-               int totface = BLI_countlist(&em->faces);
-               int i;
-
-               for (i=0,eve=em->verts.first; eve; eve= eve->next)
-                       eve->prev = (EditVert*) i++;
-
-               emdm->vertexNos = MEM_callocN(sizeof(*emdm->vertexNos)*i, "emdm_vno");
-               emdm->faceNos = MEM_mallocN(sizeof(*emdm->faceNos)*totface, "emdm_vno");
-
-               for(i=0, efa= em->faces.first; efa; i++, efa=efa->next) {
-                       float *v1 = vertexCos[(int) efa->v1->prev];
-                       float *v2 = vertexCos[(int) efa->v2->prev];
-                       float *v3 = vertexCos[(int) efa->v3->prev];
-                       float *no = emdm->faceNos[i];
-                       
-                       if(efa->v4) {
-                               float *v4 = vertexCos[(int) efa->v3->prev];
-
-                               CalcNormFloat4(v1, v2, v3, v4, no);
-                               VecAddf(emdm->vertexNos[(int) efa->v4->prev], emdm->vertexNos[(int) efa->v4->prev], no);
-                       }
-                       else {
-                               CalcNormFloat(v1, v2, v3, no);
-                       }
-
-                       VecAddf(emdm->vertexNos[(int) efa->v1->prev], emdm->vertexNos[(int) efa->v1->prev], no);
-                       VecAddf(emdm->vertexNos[(int) efa->v2->prev], emdm->vertexNos[(int) efa->v2->prev], no);
-                       VecAddf(emdm->vertexNos[(int) efa->v3->prev], emdm->vertexNos[(int) efa->v3->prev], no);
-               }
-
-               for(i=0, eve= em->verts.first; eve; i++, eve=eve->next) {
-                       float *no = emdm->vertexNos[i];
-                       /* following Mesh convention; we use vertex coordinate itself
-                        * for normal in this case */
-                       if (Normalise(no)==0.0) {
-                               VECCOPY(no, vertexCos[i]);
-                               Normalise(no);
-                       }
-               }
-
-               for (preveve=NULL, eve=emdm->em->verts.first; eve; preveve=eve, eve= eve->next)
-                       eve->prev = preveve;
-       }
-
-       return (DerivedMesh*) emdm;
-}
-
-///
-
-typedef struct {
-       DerivedMesh dm;
-
-       DispListMesh *dlm;
-} SSDerivedMesh;
-
-static void ssDM_foreachMappedVert(DerivedMesh *dm, void (*func)(void *userData, int index, float *co, float *no_f, short *no_s), void *userData)
-{
-       SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
-       DispListMesh *dlm = ssdm->dlm;
-       int i;
-       int *index = dm->getVertDataArray(dm, LAYERTYPE_ORIGINDEX);
-
-       for (i=0; i<dlm->totvert; i++, index++) {
-               MVert *mv = &dlm->mvert[i];
-
-               if(*index != ORIGINDEX_NONE)
-                       func(userData, *index, mv->co, NULL, mv->no);
-       }
-}
-static void ssDM_foreachMappedEdge(DerivedMesh *dm, void (*func)(void *userData, int index, float *v0co, float *v1co), void *userData)
-{
-       SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
-       DispListMesh *dlm = ssdm->dlm;
-       int i;
-       int *index = dm->getEdgeDataArray(dm, LAYERTYPE_ORIGINDEX);
-
-       for (i=0; i<dlm->totedge; i++, index++) {
-               MEdge *med = &dlm->medge[i];
-
-               if(*index != ORIGINDEX_NONE)
-                       func(userData, *index, dlm->mvert[med->v1].co,
-                            dlm->mvert[med->v2].co);
-       }
-}
-static void ssDM_drawMappedEdges(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index), void *userData) 
-{
-       SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
-       DispListMesh *dlm = ssdm->dlm;
-       int i;
-       int *index = dm->getEdgeDataArray(dm, LAYERTYPE_ORIGINDEX);
-
-       glBegin(GL_LINES);
-       for(i=0; i<dlm->totedge; i++, index++) {
-               MEdge *med = &dlm->medge[i];
-
-               if(*index != ORIGINDEX_NONE
-                  && (!setDrawOptions || setDrawOptions(userData, *index))) {
-                       glVertex3fv(dlm->mvert[med->v1].co);
-                       glVertex3fv(dlm->mvert[med->v2].co);
-               }
-       }
-       glEnd();
-}
-
-static void ssDM_foreachMappedFaceCenter(DerivedMesh *dm, void (*func)(void *userData, int index, float *co, float *no), void *userData)
-{
-       SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
-       DispListMesh *dlm = ssdm->dlm;
-       int i;
-       int *index = dm->getFaceDataArray(dm, LAYERTYPE_ORIGINDEX);
-
-       for (i=0; i<dlm->totface; i++, index++) {
-               MFace *mf = &dlm->mface[i];
-
-               if(*index != ORIGINDEX_NONE) {
-                       float cent[3];
-                       float no[3];
-
-                       VECCOPY(cent, dlm->mvert[mf->v1].co);
-                       VecAddf(cent, cent, dlm->mvert[mf->v2].co);
-                       VecAddf(cent, cent, dlm->mvert[mf->v3].co);
-
-                       if (mf->v4) {
-                               CalcNormFloat4(dlm->mvert[mf->v1].co, dlm->mvert[mf->v2].co, dlm->mvert[mf->v3].co, dlm->mvert[mf->v4].co, no);
-                               VecAddf(cent, cent, dlm->mvert[mf->v4].co);
-                               VecMulf(cent, 0.25f);
-                       } else {
-                               CalcNormFloat(dlm->mvert[mf->v1].co, dlm->mvert[mf->v2].co, dlm->mvert[mf->v3].co, no);
-                               VecMulf(cent, 0.33333333333f);
-                       }
-
-                       func(userData, *index, cent, no);
-               }
-       }
-}
-static void ssDM_drawVerts(DerivedMesh *dm)
-{
-       SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
-       DispListMesh *dlm = ssdm->dlm;
-       MVert *mvert= dlm->mvert;
-       int i;
-
-       bglBegin(GL_POINTS);
-       for (i=0; i<dlm->totvert; i++) {
-               bglVertex3fv(mvert[i].co);
-       }
-       bglEnd();
-}
-static void ssDM_drawUVEdges(DerivedMesh *dm)
-{
-       SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
-       DispListMesh *dlm = ssdm->dlm;
-       int i;
-
-       if (dlm->tface) {
-               glBegin(GL_LINES);
-               for (i=0; i<dlm->totface; i++) {
-                       TFace *tf = &dlm->tface[i];
-
-                       if (!(tf->flag&TF_HIDE)) {
-                               glVertex2fv(tf->uv[0]);
-                               glVertex2fv(tf->uv[1]);
-
-                               glVertex2fv(tf->uv[1]);
-                               glVertex2fv(tf->uv[2]);
-
-                               if (!dlm->mface[i].v4) {
-                                       glVertex2fv(tf->uv[2]);
-                                       glVertex2fv(tf->uv[0]);
-                               } else {
-                                       glVertex2fv(tf->uv[2]);
-                                       glVertex2fv(tf->uv[3]);
-
-                                       glVertex2fv(tf->uv[3]);
-                                       glVertex2fv(tf->uv[0]);
-                               }
-                       }
-               }
-               glEnd();
-       }
-}
-static void ssDM_drawLooseEdges(DerivedMesh *dm) 
-{
-       SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
-       DispListMesh *dlm = ssdm->dlm;
-       MVert *mvert = dlm->mvert;
-       MEdge *medge= dlm->medge;
-       int i;
-
-       glBegin(GL_LINES);
-       for (i=0; i<dlm->totedge; i++, medge++) {
-               if (medge->flag&ME_LOOSEEDGE) {
-                       glVertex3fv(mvert[medge->v1].co); 
-                       glVertex3fv(mvert[medge->v2].co);
-               }
-       }
-       glEnd();
-}
-static void ssDM_drawEdges(DerivedMesh *dm, int drawLooseEdges) 
-{
-       SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
-       DispListMesh *dlm = ssdm->dlm;
-       MVert *mvert= dlm->mvert;
-       MEdge *medge= dlm->medge;
-       int i;
-       
-       glBegin(GL_LINES);
-       for (i=0; i<dlm->totedge; i++, medge++) {
-               if ((medge->flag&ME_EDGEDRAW) && (drawLooseEdges || !(medge->flag&ME_LOOSEEDGE))) {
-                       glVertex3fv(mvert[medge->v1].co); 
-                       glVertex3fv(mvert[medge->v2].co);
-               }
-       }
-       glEnd();
-}
-static void ssDM_drawFacesSolid(DerivedMesh *dm, int (*setMaterial)(int))
-{
-       SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
-       DispListMesh *dlm = ssdm->dlm;
-       float *nors = dlm->nors;
-       int glmode=-1, shademodel=-1, matnr=-1, drawCurrentMat=1;
-       int i;
-
-#define PASSVERT(ind) {                                                \
-       if (shademodel==GL_SMOOTH)                              \
-               glNormal3sv(dlm->mvert[(ind)].no);      \
-       glVertex3fv(dlm->mvert[(ind)].co);              \
-}
-
-       glBegin(glmode=GL_QUADS);
-       for (i=0; i<dlm->totface; i++) {
-               MFace *mf= &dlm->mface[i];
-               int new_glmode = mf->v4?GL_QUADS:GL_TRIANGLES;
-               int new_shademodel = (mf->flag&ME_SMOOTH)?GL_SMOOTH:GL_FLAT;
-               int new_matnr = mf->mat_nr+1;
-               
-               if(new_glmode!=glmode || new_shademodel!=shademodel || new_matnr!=matnr) {
-                       glEnd();
-
-                       drawCurrentMat = setMaterial(matnr=new_matnr);
-
-                       glShadeModel(shademodel=new_shademodel);
-                       glBegin(glmode=new_glmode);
-               }
-               
-               if (drawCurrentMat) {
-                       if (shademodel==GL_FLAT)
-                               glNormal3fv(&nors[i*3]);
-                               
-                       PASSVERT(mf->v1);
-                       PASSVERT(mf->v2);
-                       PASSVERT(mf->v3);
-                       if (mf->v4)
-                               PASSVERT(mf->v4);
-               }
-       }
-       glEnd();
-       
-#undef PASSVERT
-}
-static void ssDM_drawFacesColored(DerivedMesh *dm, int useTwoSided, unsigned char *vcols1, unsigned char *vcols2)
-{
-       SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
-       DispListMesh *dlm = ssdm->dlm;
-       int i, lmode;
-       
-       glShadeModel(GL_SMOOTH);
-       if (vcols2) {
-               glEnable(GL_CULL_FACE);
-       } else {
-               useTwoSided = 0;
-       }
-               
-#define PASSVERT(vidx, fidx) {                                 \
-       unsigned char *col= &colbase[fidx*4];           \
-       glColor3ub(col[3], col[2], col[1]);                     \
-       glVertex3fv(dlm->mvert[(vidx)].co);                     \
-}
-
-       glBegin(lmode= GL_QUADS);
-       for (i=0; i<dlm->totface; i++) {
-               MFace *mf= &dlm->mface[i];
-               int nmode= mf->v4?GL_QUADS:GL_TRIANGLES;
-               unsigned char *colbase= &vcols1[i*16];
-               
-               if (nmode!=lmode) {
-                       glEnd();
-                       glBegin(lmode= nmode);
-               }
-               
-               PASSVERT(mf->v1, 0);
-               PASSVERT(mf->v2, 1);
-               PASSVERT(mf->v3, 2);
-               if (mf->v4)
-                       PASSVERT(mf->v4, 3);
-               
-               if (useTwoSided) {
-                       unsigned char *colbase= &vcols2[i*16];
-
-                       if (mf->v4)
-                               PASSVERT(mf->v4, 3);
-                       PASSVERT(mf->v3, 2);
-                       PASSVERT(mf->v2, 1);
-                       PASSVERT(mf->v1, 0);
-               }
-       }
-       glEnd();
-
-       if (vcols2)
-               glDisable(GL_CULL_FACE);
-       
-#undef PASSVERT
-}
-
-static void ssDM_drawFacesTex_common(DerivedMesh *dm, int (*drawParams)(TFace *tface, int matnr), int (*drawParamsMapped)(void *userData, int index), void *userData) 
-{
-       SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
-       DispListMesh *dlm = ssdm->dlm;
-       MVert *mvert= dlm->mvert;
-       MFace *mface= dlm->mface;
-       TFace *tface = dlm->tface;
-       float *nors = dlm->nors;
-       int a;
-       int *index = dm->getFaceDataArray(dm, LAYERTYPE_ORIGINDEX);
-       
-       for (a=0; a<dlm->totface; a++, index++) {
-               MFace *mf= &mface[a];
-               TFace *tf = tface?&tface[a]:NULL;
-               int flag = 0;
-               unsigned char *cp= NULL;
-               
-               if (drawParams) {
-                       flag = drawParams(tf, mf->mat_nr);
-               }
-               else {
-                       if(*index != ORIGINDEX_NONE)
-                               flag = drawParamsMapped(userData, *index);
-               }
-
-               if (flag==0) {
-                       continue;
-               } else if (flag==1) {
-                       if (tf) {
-                               cp= (unsigned char*) tf->col;
-                       } else if (dlm->mcol) {
-                               cp= (unsigned char*) &dlm->mcol[a*4];
-                       }
-               }
-
-               if (!(mf->flag&ME_SMOOTH)) {
-                       glNormal3fv(&nors[a*3]);
-               }
-
-               glBegin(mf->v4?GL_QUADS:GL_TRIANGLES);
-               if (tf) glTexCoord2fv(tf->uv[0]);
-               if (cp) glColor3ub(cp[3], cp[2], cp[1]);
-               if (mf->flag&ME_SMOOTH) glNormal3sv(mvert[mf->v1].no);
-               glVertex3fv((mvert+mf->v1)->co);
-                       
-               if (tf) glTexCoord2fv(tf->uv[1]);
-               if (cp) glColor3ub(cp[7], cp[6], cp[5]);
-               if (mf->flag&ME_SMOOTH) glNormal3sv(mvert[mf->v2].no);
-               glVertex3fv((mvert+mf->v2)->co);
-
-               if (tf) glTexCoord2fv(tf->uv[2]);
-               if (cp) glColor3ub(cp[11], cp[10], cp[9]);
-               if (mf->flag&ME_SMOOTH) glNormal3sv(mvert[mf->v3].no);
-               glVertex3fv((mvert+mf->v3)->co);
-
-               if(mf->v4) {
-                       if (tf) glTexCoord2fv(tf->uv[3]);
-                       if (cp) glColor3ub(cp[15], cp[14], cp[13]);
-                       if (mf->flag&ME_SMOOTH) glNormal3sv(mvert[mf->v4].no);
-                       glVertex3fv((mvert+mf->v4)->co);
-               }
-               glEnd();
-       }
-}
-static void ssDM_drawFacesTex(DerivedMesh *dm, int (*setDrawParams)(TFace *tface, int matnr))
-{
-       ssDM_drawFacesTex_common(dm, setDrawParams, NULL, NULL);
-}
-static void ssDM_drawMappedFacesTex(DerivedMesh *dm, int (*setDrawParams)(void *userData, int index), void *userData) 
-{
-       ssDM_drawFacesTex_common(dm, NULL, setDrawParams, userData);
-}
-
-static void ssDM_drawMappedFaces(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index, int *drawSmooth_r), void *userData, int useColors) 
-{
-       SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
-       DispListMesh *dlm = ssdm->dlm;
-       MVert *mvert= dlm->mvert;
-       MFace *mface= dlm->mface;
-       float *nors = dlm->nors;
-       int i;
-       int *index = dm->getFaceDataArray(dm, LAYERTYPE_ORIGINDEX);
-
-       for (i=0; i<dlm->totface; i++, index++) {
-               MFace *mf = &mface[i];
-               int drawSmooth = (mf->flag & ME_SMOOTH);
-
-               if(*index != ORIGINDEX_NONE
-                  && (!setDrawOptions
-                      || setDrawOptions(userData, *index, &drawSmooth))) {
-                       unsigned char *cp = NULL;
-
-                       if (useColors) {
-                               if (dlm->tface) {
-                                       cp= (unsigned char*) dlm->tface[i].col;
-                               } else if (dlm->mcol) {
-                                       cp= (unsigned char*) &dlm->mcol[i*4];
-                               }
-                       }
-
-                       glShadeModel(drawSmooth?GL_SMOOTH:GL_FLAT);
-                       glBegin(mf->v4?GL_QUADS:GL_TRIANGLES);
-
-                       if (!drawSmooth) {
-                               glNormal3fv(&nors[i*3]);
-
-                               if (cp) glColor3ub(cp[3], cp[2], cp[1]);
-                               glVertex3fv(mvert[mf->v1].co);
-                               if (cp) glColor3ub(cp[7], cp[6], cp[5]);
-                               glVertex3fv(mvert[mf->v2].co);
-                               if (cp) glColor3ub(cp[11], cp[10], cp[9]);
-                               glVertex3fv(mvert[mf->v3].co);
-                               if(mf->v4) {
-                                       if (cp) glColor3ub(cp[15], cp[14], cp[13]);
-                                       glVertex3fv(mvert[mf->v4].co);
-                               }
-                       } else {
-                               if (cp) glColor3ub(cp[3], cp[2], cp[1]);
-                               glNormal3sv(mvert[mf->v1].no);
-                               glVertex3fv(mvert[mf->v1].co);
-                               if (cp) glColor3ub(cp[7], cp[6], cp[5]);
-                               glNormal3sv(mvert[mf->v2].no);
-                               glVertex3fv(mvert[mf->v2].co);
-                               if (cp) glColor3ub(cp[11], cp[10], cp[9]);
-                               glNormal3sv(mvert[mf->v3].no);
-                               glVertex3fv(mvert[mf->v3].co);
-                               if(mf->v4) {
-                                       if (cp) glColor3ub(cp[15], cp[14], cp[13]);
-                                       glNormal3sv(mvert[mf->v4].no);
-                                       glVertex3fv(mvert[mf->v4].co);
-                               }
-                       }
-
-                       glEnd();
-               }
-       }
-}
-static void ssDM_getMinMax(DerivedMesh *dm, float min_r[3], float max_r[3])
-{
-       SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
-       int i;
-
-       if (ssdm->dlm->totvert) {
-               for (i=0; i<ssdm->dlm->totvert; i++) {
-                       DO_MINMAX(ssdm->dlm->mvert[i].co, min_r, max_r);
-               }
-       } else {
-               min_r[0] = min_r[1] = min_r[2] = max_r[0] = max_r[1] = max_r[2] = 0.0;
-       }
-}
-
-static void ssDM_getVertCos(DerivedMesh *dm, float (*cos_r)[3])
-{
-       SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
-       int i;
-
-       for (i=0; i<ssdm->dlm->totvert; i++) {
-               cos_r[i][0] = ssdm->dlm->mvert[i].co[0];
-               cos_r[i][1] = ssdm->dlm->mvert[i].co[1];
-               cos_r[i][2] = ssdm->dlm->mvert[i].co[2];
-       }
-}
-
-static int ssDM_getNumVerts(DerivedMesh *dm)
-{
-       SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
-
-       return ssdm->dlm->totvert;
-}
-
-static int ssDM_getNumEdges(DerivedMesh *dm)
-{
-       SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
-
-       return ssdm->dlm->totedge;
-}
-
-static int ssDM_getNumFaces(DerivedMesh *dm)
-{
-       SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
-
-       return ssdm->dlm->totface;
-}
-
-void ssDM_getVert(DerivedMesh *dm, int index, MVert *vert_r)
-{
-       *vert_r = ((SSDerivedMesh *)dm)->dlm->mvert[index];
-}
-
-void ssDM_getEdge(DerivedMesh *dm, int index, MEdge *edge_r)
-{
-       *edge_r = ((SSDerivedMesh *)dm)->dlm->medge[index];
-}
-
-void ssDM_getFace(DerivedMesh *dm, int index, MFace *face_r)
-{
-       *face_r = ((SSDerivedMesh *)dm)->dlm->mface[index];
 }
 
-void ssDM_getVertArray(DerivedMesh *dm, MVert *vert_r)
+void emDM_copyFaceArray(DerivedMesh *dm, MFace *face_r)
 {
-       SSDerivedMesh *ssdm = (SSDerivedMesh *)dm;
-       memcpy(vert_r, ssdm->dlm->mvert, sizeof(*vert_r) * ssdm->dlm->totvert);
-}
+       EditMesh *em = ((EditMeshDerivedMesh *)dm)->em;
+       EditFace *ef = em->faces.first;
+       EditVert *ev;
+       int i;
 
-void ssDM_getEdgeArray(DerivedMesh *dm, MEdge *edge_r)
-{
-       SSDerivedMesh *ssdm = (SSDerivedMesh *)dm;
-       memcpy(edge_r, ssdm->dlm->medge, sizeof(*edge_r) * ssdm->dlm->totedge);
-}
+       /* store vertexes indices in tmp union */
+       for(ev = em->verts.first, i = 0; ev; ev = ev->next, ++i)
+               ev->tmp.l = (long) i;
 
-void ssDM_getFaceArray(DerivedMesh *dm, MFace *face_r)
-{
-       SSDerivedMesh *ssdm = (SSDerivedMesh *)dm;
-       memcpy(face_r, ssdm->dlm->mface, sizeof(*face_r) * ssdm->dlm->totface);
-}
+       for( ; ef; ef = ef->next, ++face_r) {
+               face_r->mat_nr = ef->mat_nr;
+               face_r->flag = ef->flag;
 
-static DispListMesh *ssDM_convertToDispListMesh(DerivedMesh *dm, int allowShared)
-{
-       SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
+               face_r->v1 = (int)ef->v1->tmp.l;
+               face_r->v2 = (int)ef->v2->tmp.l;
+               face_r->v3 = (int)ef->v3->tmp.l;
+               if(ef->v4) face_r->v4 = (int)ef->v4->tmp.l;
+               else face_r->v4 = 0;
 
-       if (allowShared) {
-               return displistmesh_copyShared(ssdm->dlm);
-       } else {
-               return displistmesh_copy(ssdm->dlm);
+               test_index_face(face_r, NULL, 0, ef->v4?4:3);
        }
 }
 
-static void ssDM_release(DerivedMesh *dm)
+static void emDM_release(DerivedMesh *dm)
 {
-       SSDerivedMesh *ssdm = (SSDerivedMesh*) dm;
-
-       DM_release(dm);
+       EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
 
-       displistmesh_free(ssdm->dlm);
+       if (DM_release(dm)) {
+               if (emdm->vertexCos) {
+                       MEM_freeN(emdm->vertexCos);
+                       MEM_freeN(emdm->vertexNos);
+                       MEM_freeN(emdm->faceNos);
+               }
 
-       MEM_freeN(dm);
+               MEM_freeN(emdm);
+       }
 }
 
-DerivedMesh *derivedmesh_from_displistmesh(DispListMesh *dlm, float (*vertexCos)[3])
+static DerivedMesh *getEditMeshDerivedMesh(EditMesh *em, Object *ob,
+                                           float (*vertexCos)[3])
 {
-       SSDerivedMesh *ssdm = MEM_callocN(sizeof(*ssdm), "ssdm");
-
-       DM_init(&ssdm->dm, dlm->totvert, dlm->totedge, dlm->totface);
+       EditMeshDerivedMesh *emdm = MEM_callocN(sizeof(*emdm), "emdm");
 
-       ssdm->dm.getMinMax = ssDM_getMinMax;
+       DM_init(&emdm->dm, BLI_countlist(&em->verts),
+                        BLI_countlist(&em->edges), BLI_countlist(&em->faces));
 
-       ssdm->dm.getNumVerts = ssDM_getNumVerts;
-       ssdm->dm.getNumEdges = ssDM_getNumEdges;
-       ssdm->dm.getNumFaces = ssDM_getNumFaces;
+       emdm->dm.getMinMax = emDM_getMinMax;
 
-       ssdm->dm.getVert = ssDM_getVert;
-       ssdm->dm.getEdge = ssDM_getEdge;
-       ssdm->dm.getFace = ssDM_getFace;
-       ssdm->dm.getVertArray = ssDM_getVertArray;
-       ssdm->dm.getEdgeArray = ssDM_getEdgeArray;
-       ssdm->dm.getFaceArray = ssDM_getFaceArray;
+       emdm->dm.getNumVerts = emDM_getNumVerts;
+       emdm->dm.getNumEdges = emDM_getNumEdges;
+       emdm->dm.getNumFaces = emDM_getNumFaces;
 
-       ssdm->dm.convertToDispListMesh = ssDM_convertToDispListMesh;
+       emdm->dm.getVert = emDM_getVert;
+       emdm->dm.getEdge = emDM_getEdge;
+       emdm->dm.getFace = emDM_getFace;
+       emdm->dm.copyVertArray = emDM_copyVertArray;
+       emdm->dm.copyEdgeArray = emDM_copyEdgeArray;
+       emdm->dm.copyFaceArray = emDM_copyFaceArray;
 
-       ssdm->dm.getVertCos = ssDM_getVertCos;
+       emdm->dm.foreachMappedVert = emDM_foreachMappedVert;
+       emdm->dm.foreachMappedEdge = emDM_foreachMappedEdge;
+       emdm->dm.foreachMappedFaceCenter = emDM_foreachMappedFaceCenter;
 
-       ssdm->dm.drawVerts = ssDM_drawVerts;
+       emdm->dm.drawEdges = emDM_drawEdges;
+       emdm->dm.drawMappedEdges = emDM_drawMappedEdges;
+       emdm->dm.drawMappedEdgesInterp = emDM_drawMappedEdgesInterp;
+       emdm->dm.drawMappedFaces = emDM_drawMappedFaces;
+       emdm->dm.drawUVEdges = emDM_drawUVEdges;
 
-       ssdm->dm.drawUVEdges = ssDM_drawUVEdges;
-       ssdm->dm.drawEdges = ssDM_drawEdges;
-       ssdm->dm.drawLooseEdges = ssDM_drawLooseEdges;
+       emdm->dm.release = emDM_release;
        
-       ssdm->dm.drawFacesSolid = ssDM_drawFacesSolid;
-       ssdm->dm.drawFacesColored = ssDM_drawFacesColored;
-       ssdm->dm.drawFacesTex = ssDM_drawFacesTex;
-       ssdm->dm.drawMappedFaces = ssDM_drawMappedFaces;
-       ssdm->dm.drawMappedFacesTex = ssDM_drawMappedFacesTex;
+       emdm->em = em;
+       emdm->vertexCos = vertexCos;
 
-               /* EM functions */
-       
-       ssdm->dm.foreachMappedVert = ssDM_foreachMappedVert;
-       ssdm->dm.foreachMappedEdge = ssDM_foreachMappedEdge;
-       ssdm->dm.foreachMappedFaceCenter = ssDM_foreachMappedFaceCenter;
-       
-       ssdm->dm.drawMappedEdges = ssDM_drawMappedEdges;
-       ssdm->dm.drawMappedEdgesInterp = NULL; // no way to implement this one
-       
-       ssdm->dm.release = ssDM_release;
-       
-       ssdm->dlm = dlm;
+       if(CustomData_has_layer(&em->vdata, CD_MDEFORMVERT)) {
+               EditVert *eve;
+               int i;
 
-       if (vertexCos) {
+               DM_add_vert_layer(&emdm->dm, CD_MDEFORMVERT, CD_CALLOC, NULL);
+
+               for(eve = em->verts.first, i = 0; eve; eve = eve->next, ++i)
+                       DM_set_vert_data(&emdm->dm, i, CD_MDEFORMVERT,
+                                        CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT));
+       }
+
+       if(vertexCos) {
+               EditVert *eve;
+               EditFace *efa;
+               int totface = BLI_countlist(&em->faces);
                int i;
 
-               for (i=0; i<dlm->totvert; i++) {
-                       VECCOPY(dlm->mvert[i].co, vertexCos[i]);
-               }
+               for (i=0,eve=em->verts.first; eve; eve= eve->next)
+                       eve->tmp.l = (long) i++;
+
+               emdm->vertexNos = MEM_callocN(sizeof(*emdm->vertexNos)*i, "emdm_vno");
+               emdm->faceNos = MEM_mallocN(sizeof(*emdm->faceNos)*totface, "emdm_vno");
 
-               if (dlm->nors && !dlm->dontFreeNors) {
-                       MEM_freeN(dlm->nors);
-                       dlm->nors = 0;
+               for(i=0, efa= em->faces.first; efa; i++, efa=efa->next) {
+                       float *v1 = vertexCos[(int) efa->v1->tmp.l];
+                       float *v2 = vertexCos[(int) efa->v2->tmp.l];
+                       float *v3 = vertexCos[(int) efa->v3->tmp.l];
+                       float *no = emdm->faceNos[i];
+                       
+                       if(efa->v4) {
+                               float *v4 = vertexCos[(int) efa->v3->tmp.l];
+
+                               CalcNormFloat4(v1, v2, v3, v4, no);
+                               VecAddf(emdm->vertexNos[(int) efa->v4->tmp.l], emdm->vertexNos[(int) efa->v4->tmp.l], no);
+                       }
+                       else {
+                               CalcNormFloat(v1, v2, v3, no);
+                       }
+
+                       VecAddf(emdm->vertexNos[(int) efa->v1->tmp.l], emdm->vertexNos[(int) efa->v1->tmp.l], no);
+                       VecAddf(emdm->vertexNos[(int) efa->v2->tmp.l], emdm->vertexNos[(int) efa->v2->tmp.l], no);
+                       VecAddf(emdm->vertexNos[(int) efa->v3->tmp.l], emdm->vertexNos[(int) efa->v3->tmp.l], no);
                }
 
-               mesh_calc_normals(dlm->mvert, dlm->totvert, dlm->mface, dlm->totface, &dlm->nors);
+               for(i=0, eve= em->verts.first; eve; i++, eve=eve->next) {
+                       float *no = emdm->vertexNos[i];
+                       /* following Mesh convention; we use vertex coordinate itself
+                        * for normal in this case */
+                       if (Normalize(no)==0.0) {
+                               VECCOPY(no, vertexCos[i]);
+                               Normalize(no);
+                       }
+               }
        }
 
-       return (DerivedMesh*) ssdm;
+       return (DerivedMesh*) emdm;
 }
 
 #ifdef WITH_VERSE
@@ -2130,7 +1019,8 @@ typedef struct {
        struct VNode *vnode;
        struct VLayer *vertex_layer;
        struct VLayer *polygon_layer;
-       float (*verts)[3];
+       struct ListBase *edges;
+       float (*vertexCos)[3];
 } VDerivedMesh;
 
 /* this function set up border points of verse mesh bounding box */
@@ -2145,7 +1035,7 @@ static void vDM_getMinMax(DerivedMesh *dm, float min_r[3], float max_r[3])
 
        if(vdm->vertex_layer->dl.da.count > 0) {
                while(vvert) {
-                       DO_MINMAX(vdm->verts ? vvert->cos : vvert->co, min_r, max_r);
+                       DO_MINMAX(vdm->vertexCos ? vvert->cos : vvert->co, min_r, max_r);
                        vvert = vvert->next;
                }
        }
@@ -2166,7 +1056,9 @@ static int vDM_getNumVerts(DerivedMesh *dm)
 /* this function return number of 'fake' edges */
 static int vDM_getNumEdges(DerivedMesh *dm)
 {
-       return 0;
+       VDerivedMesh *vdm = (VDerivedMesh*)dm;
+
+       return BLI_countlist(vdm->edges);
 }
 
 /* this function returns number of polygons in polygon layer */
@@ -2182,10 +1074,13 @@ static int vDM_getNumFaces(DerivedMesh *dm)
  * but it return 'indexth' vertex of dynamic list */
 void vDM_getVert(DerivedMesh *dm, int index, MVert *vert_r)
 {
-       VerseVert *vvert = ((VDerivedMesh*)dm)->vertex_layer->dl.lb.first;
+       VDerivedMesh *vdm = (VDerivedMesh*)dm;
+       struct VerseVert *vvert;
        int i;
 
-       for(i=0 ; i<index; i++) vvert = vvert->next;
+       if(!vdm->vertex_layer) return;
+
+       for(vvert = vdm->vertex_layer->dl.lb.first, i=0 ; i<index; i++) vvert = vvert->next;
 
        if(vvert) {
                VECCOPY(vert_r->co, vvert->co);
@@ -2200,25 +1095,59 @@ void vDM_getVert(DerivedMesh *dm, int index, MVert *vert_r)
        }
 }
 
-/* dummy function, because verse mesh doesn't store edges */
+/* this function returns fake verse edge */
 void vDM_getEdge(DerivedMesh *dm, int index, MEdge *edge_r)
 {
-       edge_r->flag = 0;
-       edge_r->crease = 0;
-       edge_r->v1 = 0;
-       edge_r->v2 = 0;
+       VDerivedMesh *vdm = (VDerivedMesh*)dm;
+       struct VerseEdge *vedge;
+       struct VLayer *vert_vlayer = vdm->vertex_layer;
+       struct VerseVert *vvert;
+       int j;
+
+       if(!vdm->vertex_layer || !vdm->edges) return;
+
+       if(vdm->edges->first) {
+               struct VerseVert *vvert1, *vvert2;
+
+               /* store vert indices in tmp union */
+               for(vvert = vdm->vertex_layer->dl.lb.first, j = 0; vvert; vvert = vvert->next, j++)
+                       vvert->tmp.index = j;
+
+               for(vedge = vdm->edges->first; vedge; vedge = vedge->next) {
+                       if(vedge->tmp.index==index) {
+                               vvert1 = BLI_dlist_find_link(&(vert_vlayer->dl), (unsigned int)vedge->v0);
+                               vvert2 = BLI_dlist_find_link(&(vert_vlayer->dl), (unsigned int)vedge->v1);
+                               
+                               if(vvert1 && vvert2) {
+                                       edge_r->v1 = vvert1->tmp.index;
+                                       edge_r->v2 = vvert2->tmp.index;
+                               }
+                               else {
+                                       edge_r->v1 = 0;
+                                       edge_r->v2 = 0;
+                               }
+                               /* not supported yet */
+                               edge_r->flag = 0;
+                               edge_r->crease = 0;
+                               break;
+                       }
+               }
+       }
 }
 
 /* this function doesn't return face with index of access array,
  * but it returns 'indexth' vertex of dynamic list */
 void vDM_getFace(DerivedMesh *dm, int index, MFace *face_r)
 {
-       struct VerseFace *vface = ((VDerivedMesh*)dm)->polygon_layer->dl.lb.first;
-       struct VerseVert *vvert = ((VDerivedMesh*)dm)->vertex_layer->dl.lb.first;
+       VDerivedMesh *vdm = (VDerivedMesh*)dm;
+       struct VerseFace *vface;
+       struct VerseVert *vvert;
        struct VerseVert *vvert0, *vvert1, *vvert2, *vvert3;
        int i;
 
-       for(i = 0; i < index; ++i) vface = vface->next;
+       if(!vdm->vertex_layer || !vdm->polygon_layer) return;
+
+       for(vface = vdm->polygon_layer->dl.lb.first, i = 0; i < index; ++i) vface = vface->next;
 
        face_r->mat_nr = 0;
        face_r->flag = 0;
@@ -2230,7 +1159,7 @@ void vDM_getFace(DerivedMesh *dm, int index, MFace *face_r)
        vvert3 = vface->vvert3;
        if(!vvert3) face_r->v4 = 0;
 
-       for(i = 0; vvert0 || vvert1 || vvert2 || vvert3; i++, vvert = vvert->next) {
+       for(vvert = vdm->vertex_layer->dl.lb.first, i = 0; vvert0 || vvert1 || vvert2 || vvert3; i++, vvert = vvert->next) {
                if(vvert == vvert0) {
                        face_r->v1 = i;
                        vvert0 = NULL;
@@ -2249,15 +1178,18 @@ void vDM_getFace(DerivedMesh *dm, int index, MFace *face_r)
                }
        }
 
-       test_index_face(face_r, NULL, NULL, vface->vvert3?4:3);
+       test_index_face(face_r, NULL, 0, vface->vvert3?4:3);
 }
 
 /* fill array of mvert */
-void vDM_getVertArray(DerivedMesh *dm, MVert *vert_r)
+void vDM_copyVertArray(DerivedMesh *dm, MVert *vert_r)
 {
-       VerseVert *vvert = ((VDerivedMesh *)dm)->vertex_layer->dl.lb.first;
+       VDerivedMesh *vdm = (VDerivedMesh*)dm;
+       struct VerseVert *vvert;
+
+       if(!vdm->vertex_layer) return;
 
-       for( ; vvert; vvert = vvert->next, ++vert_r) {
+       for(vvert = vdm->vertex_layer->dl.lb.first ; vvert; vvert = vvert->next, ++vert_r) {
                VECCOPY(vert_r->co, vvert->co);
 
                vert_r->no[0] = vvert->no[0] * 32767.0;
@@ -2270,22 +1202,58 @@ void vDM_getVertArray(DerivedMesh *dm, MVert *vert_r)
 }
 
 /* dummy function, edges arent supported in verse mesh */
-void vDM_getEdgeArray(DerivedMesh *dm, MEdge *edge_r)
+void vDM_copyEdgeArray(DerivedMesh *dm, MEdge *edge_r)
 {
+       VDerivedMesh *vdm = (VDerivedMesh*)dm;
+
+       if(!vdm->vertex_layer || !vdm->edges) return;
+
+       if(vdm->edges->first) {
+               struct VerseEdge *vedge;
+               struct VLayer *vert_vlayer = vdm->vertex_layer;
+               struct VerseVert *vvert, *vvert1, *vvert2;
+               int j;
+
+               /* store vert indices in tmp union */
+               for(vvert = vdm->vertex_layer->dl.lb.first, j = 0; vvert; vvert = vvert->next, ++j)
+                       vvert->tmp.index = j;
+
+               for(vedge = vdm->edges->first, j=0 ; vedge; vedge = vedge->next, ++edge_r, j++) {
+                       /* create temporary edge index */
+                       vedge->tmp.index = j;
+                       vvert1 = BLI_dlist_find_link(&(vert_vlayer->dl), (unsigned int)vedge->v0);
+                       vvert2 = BLI_dlist_find_link(&(vert_vlayer->dl), (unsigned int)vedge->v1);
+                       if(vvert1 && vvert2) {
+                               edge_r->v1 = vvert1->tmp.index;
+                               edge_r->v2 = vvert2->tmp.index;
+                       }
+                       else {
+                               printf("error: vDM_copyEdgeArray: %d, %d\n", vedge->v0, vedge->v1);
+                               edge_r->v1 = 0;
+                               edge_r->v2 = 0;
+                       }
+                       /* not supported yet */
+                       edge_r->flag = 0;
+                       edge_r->crease = 0;
+               }
+       }
 }
 
 /* fill array of mfaces */
-void vDM_getFaceArray(DerivedMesh *dm, MFace *face_r)
+void vDM_copyFaceArray(DerivedMesh *dm, MFace *face_r)
 {
-       VerseFace *vface = ((VDerivedMesh*)dm)->polygon_layer->dl.lb.first;
-       VerseVert *vvert = ((VDerivedMesh*)dm)->vertex_layer->dl.lb.first;
+       VDerivedMesh *vdm = (VDerivedMesh*)dm;
+       struct VerseFace *vface;
+       struct VerseVert *vvert;
        int i;
-
-       /* store vert indices in the prev pointer (kind of hacky) */
-       for(i = 0; vvert; vvert = vvert->next, ++i)
+       
+       if(!vdm->vertex_layer || !vdm->polygon_layer) return;
+       
+       /* store vertexes indices in tmp union */
+       for(vvert = vdm->vertex_layer->dl.lb.first, i = 0; vvert; vvert = vvert->next, ++i)
                vvert->tmp.index = i;
 
-       for( ; vface; vface = vface->next, ++face_r) {
+       for(vface = vdm->polygon_layer->dl.lb.first; vface; vface = vface->next, ++face_r) {
                face_r->mat_nr = 0;
                face_r->flag = 0;
 
@@ -2295,128 +1263,11 @@ void vDM_getFaceArray(DerivedMesh *dm, MFace *face_r)
                if(vface->vvert3) face_r->v4 = vface->vvert3->tmp.index;
                else face_r->v4 = 0;
 
-               test_index_face(face_r, NULL, NULL, vface->vvert3?4:3);
-       }
-}
-
-/* create diplist mesh from verse mesh */
-static DispListMesh* vDM_convertToDispListMesh(DerivedMesh *dm, int allowShared)
-{
-       VDerivedMesh *vdm = (VDerivedMesh*)dm;
-       DispListMesh *dlm = MEM_callocN(sizeof(*dlm), "dlm");
-       struct VerseVert *vvert;
-       struct VerseFace *vface;
-       struct MVert *mvert=NULL;
-       struct MFace *mface=NULL;
-       float *norms;
-       unsigned int i;
-       EdgeHash *edges;
-
-       if(!vdm->vertex_layer || !vdm->polygon_layer) {
-               dlm->totvert = 0;
-               dlm->totedge = 0;
-               dlm->totface = 0;
-               dlm->dontFreeVerts = dlm->dontFreeOther = dlm->dontFreeNors = 0;
-
-               return dlm;
-       };
-       
-       /* number of vertexes, edges and faces */
-       dlm->totvert = vdm->vertex_layer->dl.da.count;
-       dlm->totface = vdm->polygon_layer->dl.da.count;
-
-       /* create dynamic array of mverts */
-       mvert = (MVert*)MEM_mallocN(sizeof(MVert)*dlm->totvert, "dlm verts");
-       dlm->mvert = mvert;
-       vvert = (VerseVert*)vdm->vertex_layer->dl.lb.first;
-       i = 0;
-       while(vvert) {
-               VECCOPY(mvert->co, vdm->verts ? vvert->cos : vvert->co);
-               VECCOPY(mvert->no, vvert->no);
-               mvert->mat_nr = 0;
-               mvert->flag = 0;
-
-               vvert->tmp.index = i++;
-               mvert++;
-               vvert = vvert->next;
-       }
-
-       edges = BLI_edgehash_new();
-
-       /* create dynamic array of faces */
-       mface = (MFace*)MEM_mallocN(sizeof(MFace)*dlm->totface, "dlm faces");
-       dlm->mface = mface;
-       vface = (VerseFace*)vdm->polygon_layer->dl.lb.first;
-       i = 0;
-       while(vface) {
-               mface->v1 = vface->vvert0->tmp.index;
-               mface->v2 = vface->vvert1->tmp.index;
-               mface->v3 = vface->vvert2->tmp.index;
-               if(!BLI_edgehash_haskey(edges, mface->v1, mface->v2))
-                       BLI_edgehash_insert(edges, mface->v1, mface->v2, NULL);
-               if(!BLI_edgehash_haskey(edges, mface->v2, mface->v3))
-                       BLI_edgehash_insert(edges, mface->v2, mface->v3, NULL);
-               if(vface->vvert3) {
-                       mface->v4 = vface->vvert3->tmp.index;
-                       if(!BLI_edgehash_haskey(edges, mface->v3, mface->v4))
-                               BLI_edgehash_insert(edges, mface->v3, mface->v4, NULL);
-                       if(!BLI_edgehash_haskey(edges, mface->v4, mface->v1))
-                               BLI_edgehash_insert(edges, mface->v4, mface->v1, NULL);
-               } else {
-                       mface->v4 = 0;
-                       if(!BLI_edgehash_haskey(edges, mface->v3, mface->v1))
-                               BLI_edgehash_insert(edges, mface->v3, mface->v1, NULL);
-               }
-
-               mface->pad = 0;
-               mface->mat_nr = 0;
-               mface->flag = 0;
-               mface->edcode = 0;
-
-               test_index_face(mface, NULL, NULL, vface->vvert3?4:3);
-
-               mface++;
-               vface = vface->next;
-       }
-
-       dlm->totedge = BLI_edgehash_size(edges);
-
-       if(dlm->totedge) {
-               EdgeHashIterator *i;
-               MEdge *medge = dlm->medge = (MEdge *)MEM_mallocN(sizeof(MEdge)*dlm->totedge, "mesh_from_verse edge");
-
-               for(i = BLI_edgehashIterator_new(edges); !BLI_edgehashIterator_isDone(i); BLI_edgehashIterator_step(i), ++medge) {
-                       BLI_edgehashIterator_getKey(i, (int*)&medge->v1, (int*)&medge->v2);
-                       medge->crease = medge->pad = medge->flag = 0;
-               }
-               BLI_edgehashIterator_free(i);
+               test_index_face(face_r, NULL, 0, vface->vvert3?4:3);
        }
-
-       BLI_edgehash_free(edges, NULL);
-
-       /* textures and verex colors aren't supported yet */
-       dlm->tface = NULL;
-       dlm->mcol = NULL;
-
-       /* faces normals */
-       norms = (float*)MEM_mallocN(sizeof(float)*3*dlm->totface, "dlm norms");
-       dlm->nors = norms;
-
-       vface = (VerseFace*)vdm->polygon_layer->dl.lb.first;
-       while(vface){
-               VECCOPY(norms, vface->no);
-               norms += 3;
-               vface = vface->next;
-       }
-
-       /* free everything, nothing is shared */
-       dlm->dontFreeVerts = dlm->dontFreeOther = dlm->dontFreeNors = 0;
-
-       return dlm;
 }
 
-/* return coordination of vertex with index ... I suppose, that it will
- * be very hard to do, becuase there can be holes in access array */
+/* return coordination of vertex with index */
 static void vDM_getVertCo(DerivedMesh *dm, int index, float co_r[3])
 {
        VDerivedMesh *vdm = (VDerivedMesh*)dm;
@@ -2425,8 +1276,9 @@ static void vDM_getVertCo(DerivedMesh *dm, int index, float co_r[3])
        if(!vdm->vertex_layer) return;
 
        vvert = BLI_dlist_find_link(&(vdm->vertex_layer->dl), index);
+       
        if(vvert) {
-               VECCOPY(co_r, vdm->verts ? vvert->cos : vvert->co);
+               VECCOPY(co_r, vdm->vertexCos ? vvert->cos : vvert->co);
        }
        else {
                co_r[0] = co_r[1] = co_r[2] = 0.0;
@@ -2444,14 +1296,13 @@ static void vDM_getVertCos(DerivedMesh *dm, float (*cos_r)[3])
 
        vvert = vdm->vertex_layer->dl.lb.first;
        while(vvert) {
-               VECCOPY(cos_r[i], vdm->verts ? vvert->cos : vvert->co);
+               VECCOPY(cos_r[i], vdm->vertexCos ? vvert->cos : vvert->co);
                i++;
                vvert = vvert->next;
        }
 }
 
-/* return normal of vertex with index ... again, it will be hard to
- * implemente, because access array */
+/* return normal of vertex with index */
 static void vDM_getVertNo(DerivedMesh *dm, int index, float no_r[3])
 {
        VDerivedMesh *vdm = (VDerivedMesh*)dm;
@@ -2480,7 +1331,7 @@ static void vDM_drawVerts(DerivedMesh *dm)
 
        bglBegin(GL_POINTS);
        while(vvert) {
-               bglVertex3fv(vdm->verts ? vvert->cos : vvert->co);
+               bglVertex3fv(vdm->vertexCos ? vvert->cos : vvert->co);
                vvert = vvert->next;
        }
        bglEnd();
@@ -2492,21 +1343,22 @@ static void vDM_drawVerts(DerivedMesh *dm)
 static void vDM_drawEdges(DerivedMesh *dm, int drawLooseEdges)
 {
        VDerivedMesh *vdm = (VDerivedMesh*)dm;
-       struct VerseFace *vface;
-
-       if(!vdm->polygon_layer) return;
+       struct VerseEdge *vedge;
+       struct VLayer *vert_vlayer = vdm->vertex_layer;
 
-       vface = vdm->polygon_layer->dl.lb.first;
+       if(vert_vlayer && vdm->edges && (BLI_countlist(vdm->edges) > 0)) {
+               struct VerseVert *vvert1, *vvert2;
 
-       while(vface) {
-               glBegin(GL_LINE_LOOP);
-               glVertex3fv(vdm->verts ? vface->vvert0->cos : vface->vvert0->co);
-               glVertex3fv(vdm->verts ? vface->vvert1->cos : vface->vvert1->co);
-               glVertex3fv(vdm->verts ? vface->vvert2->cos : vface->vvert2->co);
-               if(vface->vvert3) glVertex3fv(vdm->verts ? vface->vvert3->cos : vface->vvert3->co);
+               glBegin(GL_LINES);
+               for(vedge = vdm->edges->first; vedge; vedge = vedge->next) {
+                       vvert1 = BLI_dlist_find_link(&(vert_vlayer->dl), (unsigned int)vedge->v0);
+                       vvert2 = BLI_dlist_find_link(&(vert_vlayer->dl), (unsigned int)vedge->v1);
+                       if(vvert1 && vvert2) {
+                               glVertex3fv(vdm->vertexCos ? vvert1->cos : vvert1->co);
+                               glVertex3fv(vdm->vertexCos ? vvert2->cos : vvert2->co);
+                       }
+               }
                glEnd();
-
-               vface = vface->next;
        }
 }
 
@@ -2530,41 +1382,22 @@ static void vDM_drawFacesSolid(DerivedMesh *dm, int (*setMaterial)(int))
 
        vface = vdm->polygon_layer->dl.lb.first;
 
+       glShadeModel(GL_FLAT);
        while(vface) {
-/*             if((vface->smooth) && (vface->smooth->value)){
-                       glShadeModel(GL_SMOOTH);
-                       glBegin(vface->vvert3?GL_QUADS:GL_TRIANGLES);
-                       glNormal3fv(vface->vvert0->no);
-                       glVertex3fv(vdm->verts ? vface->vvert0->cos : vface->vvert0->co);
-                       glNormal3fv(vface->vvert1->no);
-                       glVertex3fv(vdm->verts ? vface->vvert1->cos : vface->vvert1->co);
-                       glNormal3fv(vface->vvert2->no);
-                       glVertex3fv(vdm->verts ? vface->vvert2->cos : vface->vvert2->co);
-                       if(vface->vvert3){
-                               glNormal3fv(vface->vvert3->no);
-                               glVertex3fv(vdm->verts ? vface->vvert3->cos : vface->vvert3->co);
-                       }
-                       glEnd();
-               }
-               else { */
-                       glShadeModel(GL_FLAT);
-                       glBegin(vface->vvert3?GL_QUADS:GL_TRIANGLES);
-                       glNormal3fv(vface->no);
-                       glVertex3fv(vdm->verts ? vface->vvert0->cos : vface->vvert0->co);
-                       glVertex3fv(vdm->verts ? vface->vvert1->cos : vface->vvert1->co);
-                       glVertex3fv(vdm->verts ? vface->vvert2->cos : vface->vvert2->co);
-                       if(vface->vvert3)
-                               glVertex3fv(vdm->verts ? vface->vvert3->cos : vface->vvert3->co);
-                       glEnd();
-/*             } */
-
+               glBegin(vface->vvert3?GL_QUADS:GL_TRIANGLES);
+               glNormal3fv(vface->no);
+               glVertex3fv(vdm->vertexCos ? vface->vvert0->cos : vface->vvert0->co);
+               glVertex3fv(vdm->vertexCos ? vface->vvert1->cos : vface->vvert1->co);
+               glVertex3fv(vdm->vertexCos ? vface->vvert2->cos : vface->vvert2->co);
+               if(vface->vvert3)
+                       glVertex3fv(vdm->vertexCos ? vface->vvert3->cos : vface->vvert3->co);
+               glEnd();
                vface = vface->next;
        }
-       glShadeModel(GL_FLAT);
 }
 
-/* thsi function should draw mesh with mapped texture, but it isn't supported yet */
-static void vDM_drawFacesTex(DerivedMesh *dm, int (*setDrawOptions)(struct TFace *tface, int matnr))
+/* this function should draw mesh with mapped texture, but it isn't supported yet */
+static void vDM_drawFacesTex(DerivedMesh *dm, int (*setDrawOptions)(MTFace *tface, MCol *mcol, int matnr))
 {
        VDerivedMesh *vdm = (VDerivedMesh*)dm;
        struct VerseFace *vface;
@@ -2575,11 +1408,11 @@ static void vDM_drawFacesTex(DerivedMesh *dm, int (*setDrawOptions)(struct TFace
 
        while(vface) {
                glBegin(vface->vvert3?GL_QUADS:GL_TRIANGLES);
-               glVertex3fv(vdm->verts ? vface->vvert0->cos : vface->vvert0->co);
-               glVertex3fv(vdm->verts ? vface->vvert1->cos : vface->vvert1->co);
-               glVertex3fv(vdm->verts ? vface->vvert2->cos : vface->vvert2->co);
+               glVertex3fv(vdm->vertexCos ? vface->vvert0->cos : vface->vvert0->co);
+               glVertex3fv(vdm->vertexCos ? vface->vvert1->cos : vface->vvert1->co);
+               glVertex3fv(vdm->vertexCos ? vface->vvert2->cos : vface->vvert2->co);
                if(vface->vvert3)
-                       glVertex3fv(vdm->verts ? vface->vvert3->cos : vface->vvert3->co);
+                       glVertex3fv(vdm->vertexCos ? vface->vvert3->cos : vface->vvert3->co);
                glEnd();
 
                vface = vface->next;
@@ -2599,11 +1432,11 @@ static void vDM_drawFacesColored(DerivedMesh *dm, int useTwoSided, unsigned char
 
        while(vface) {
                glBegin(vface->vvert3?GL_QUADS:GL_TRIANGLES);
-               glVertex3fv(vdm->verts ? vface->vvert0->cos : vface->vvert0->co);
-               glVertex3fv(vdm->verts ? vface->vvert1->cos : vface->vvert1->co);
-               glVertex3fv(vdm->verts ? vface->vvert2->cos : vface->vvert2->co);
+               glVertex3fv(vdm->vertexCos ? vface->vvert0->cos : vface->vvert0->co);
+               glVertex3fv(vdm->vertexCos ? vface->vvert1->cos : vface->vvert1->co);
+               glVertex3fv(vdm->vertexCos ? vface->vvert2->cos : vface->vvert2->co);
                if(vface->vvert3)
-                       glVertex3fv(vdm->verts ? vface->vvert3->cos : vface->vvert3->co);
+                       glVertex3fv(vdm->vertexCos ? vface->vvert3->cos : vface->vvert3->co);
                glEnd();
 
                vface = vface->next;
@@ -2673,10 +1506,10 @@ static void vDM_release(DerivedMesh *dm)
 {
        VDerivedMesh *vdm = (VDerivedMesh*)dm;
 
-       DM_release(dm);
-
-       if(vdm->verts) MEM_freeN(vdm->verts);
-       MEM_freeN(vdm);
+       if (DM_release(dm)) {
+               if(vdm->vertexCos) MEM_freeN(vdm->vertexCos);
+               MEM_freeN(vdm);
+       }
 }
 
 /* create derived mesh from verse mesh ... it is used in object mode, when some other client can
@@ -2689,9 +1522,11 @@ DerivedMesh *derivedmesh_from_versemesh(VNode *vnode, float (*vertexCos)[3])
        vdm->vnode = vnode;
        vdm->vertex_layer = find_verse_layer_type((VGeomData*)vnode->data, VERTEX_LAYER);
        vdm->polygon_layer = find_verse_layer_type((VGeomData*)vnode->data, POLYGON_LAYER);
+       vdm->edges = &((VGeomData*)vnode->data)->edges;
 
+       /* vertex and polygon layer has to exist */
        if(vdm->vertex_layer && vdm->polygon_layer)
-               DM_init(&vdm->dm, vdm->vertex_layer->dl.da.count, 0, vdm->polygon_layer->dl.da.count);
+               DM_init(&vdm->dm, vdm->vertex_layer->dl.da.count, BLI_countlist(vdm->edges), vdm->polygon_layer->dl.da.count);
        else
                DM_init(&vdm->dm, 0, 0, 0);
        
@@ -2704,16 +1539,14 @@ DerivedMesh *derivedmesh_from_versemesh(VNode *vnode, float (*vertexCos)[3])
        vdm->dm.getVert = vDM_getVert;
        vdm->dm.getEdge = vDM_getEdge;
        vdm->dm.getFace = vDM_getFace;
-       vdm->dm.getVertArray = vDM_getVertArray;
-       vdm->dm.getEdgeArray = vDM_getEdgeArray;
-       vdm->dm.getFaceArray = vDM_getFaceArray;
+       vdm->dm.copyVertArray = vDM_copyVertArray;
+       vdm->dm.copyEdgeArray = vDM_copyEdgeArray;
+       vdm->dm.copyFaceArray = vDM_copyFaceArray;
        
        vdm->dm.foreachMappedVert = vDM_foreachMappedVert;
        vdm->dm.foreachMappedEdge = vDM_foreachMappedEdge;
        vdm->dm.foreachMappedFaceCenter = vDM_foreachMappedFaceCenter;
 
-       vdm->dm.convertToDispListMesh = vDM_convertToDispListMesh;
-
        vdm->dm.getVertCos = vDM_getVertCos;
        vdm->dm.getVertCo = vDM_getVertCo;
        vdm->dm.getVertNo = vDM_getVertNo;
@@ -2735,28 +1568,7 @@ DerivedMesh *derivedmesh_from_versemesh(VNode *vnode, float (*vertexCos)[3])
 
        vdm->dm.release = vDM_release;
 
-       if(vdm->vertex_layer) {
-               if(vertexCos) {
-                       int i;
-
-                       vdm->verts = MEM_mallocN(sizeof(float)*3*vdm->vertex_layer->dl.da.count, "verse mod vertexes");
-                       vvert = vdm->vertex_layer->dl.lb.first;
-
-                       for(i=0; i<vdm->vertex_layer->dl.da.count && vvert; i++, vvert = vvert->next) {
-                               VECCOPY(vdm->verts[i], vertexCos[i]);
-                               vvert->cos = vdm->verts[i];
-                       }
-               }
-               else {
-                       vdm->verts = NULL;
-                       vvert = vdm->vertex_layer->dl.lb.first;
-
-                       while(vvert) {
-                               vvert->cos = NULL;
-                               vvert = vvert->next;
-                       }
-               }
-       }
+       vdm->vertexCos = vertexCos;
 
        return (DerivedMesh*) vdm;
 }
@@ -2797,13 +1609,45 @@ DerivedMesh *mesh_create_derived_for_modifier(Object *ob, ModifierData *md)
        return dm;
 }
 
+CustomDataMask get_viewedit_datamask()
+{
+       CustomDataMask mask = CD_MASK_BAREMESH;
+       ScrArea *sa;
+
+       /* check if we need tfaces & mcols due to face select or texture paint */
+       if(G.f & G_FACESELECT || G.f & G_TEXTUREPAINT) {
+               mask |= CD_MASK_MTFACE | CD_MASK_MCOL;
+       } else {
+               /* check if we need tfaces & mcols due to view mode */
+               for(sa = G.curscreen->areabase.first; sa; sa = sa->next) {
+                       if(sa->spacetype == SPACE_VIEW3D) {
+                               View3D *view = sa->spacedata.first;
+                               if(view->drawtype == OB_SHADED) {
+                                       /* this includes normals for mesh_create_shadedColors */
+                                       mask |= CD_MASK_MTFACE | CD_MASK_MCOL | CD_MASK_NORMAL;
+                               }
+                               if(view->drawtype == OB_TEXTURE) {
+                                       mask |= CD_MASK_MTFACE | CD_MASK_MCOL;
+                               }
+                       }
+               }
+       }
+
+       /* check if we need mcols due to vertex paint or weightpaint */
+       if(G.f & G_VERTEXPAINT || G.f & G_WEIGHTPAINT)
+               mask |= CD_MASK_MCOL;
+
+       return mask;
+}
+
 static void mesh_calc_modifiers(Object *ob, float (*inputVertexCos)[3],
                                 DerivedMesh **deform_r, DerivedMesh **final_r,
                                 int useRenderParams, int useDeform,
-                                int needMapping)
+                                int needMapping, CustomDataMask dataMask)
 {
        Mesh *me = ob->data;
        ModifierData *md = modifiers_getVirtualModifierList(ob);
+       LinkNode *datamasks, *curr;
        float (*deformedVerts)[3] = NULL;
        DerivedMesh *dm;
        int numVerts = me->totvert;
@@ -2812,6 +1656,12 @@ static void mesh_calc_modifiers(Object *ob, float (*inputVertexCos)[3],
 
        modifiers_clearErrors(ob);
 
+       /* we always want to keep original indices */
+       dataMask |= CD_MASK_ORIGINDEX;
+
+       datamasks = modifiers_calcDataMasks(md, dataMask);
+       curr = datamasks;
+
        if(deform_r) *deform_r = NULL;
        *final_r = NULL;
 
@@ -2839,7 +1689,7 @@ static void mesh_calc_modifiers(Object *ob, float (*inputVertexCos)[3],
                        deformedVerts = mesh_getVertexCos(me, &numVerts);
                
                /* Apply all leading deforming modifiers */
-               for(; md; md = md->next) {
+               for(; md; md = md->next, curr = curr->next) {
                        ModifierTypeInfo *mti = modifierType_getInfo(md->type);
 
                        if((md->mode & required_mode) != required_mode) continue;
@@ -2863,14 +1713,14 @@ static void mesh_calc_modifiers(Object *ob, float (*inputVertexCos)[3],
 #ifdef WITH_VERSE
                        if(me->vnode) *deform_r = derivedmesh_from_versemesh(me->vnode, deformedVerts);
                        else {
-                               *deform_r = CDDM_from_mesh(me);
+                               *deform_r = CDDM_from_mesh(me, ob);
                                if(deformedVerts) {
                                        CDDM_apply_vert_coords(*deform_r, deformedVerts);
                                        CDDM_calc_normals(*deform_r);
                                }
                        }
 #else
-                       *deform_r = CDDM_from_mesh(me);
+                       *deform_r = CDDM_from_mesh(me, ob);
                        if(deformedVerts) {
                                CDDM_apply_vert_coords(*deform_r, deformedVerts);
                                CDDM_calc_normals(*deform_r);
@@ -2902,7 +1752,7 @@ static void mesh_calc_modifiers(Object *ob, float (*inputVertexCos)[3],
        if(me->vnode) dm = derivedmesh_from_versemesh(me->vnode, deformedVerts);
 #endif
 
-       for(; md; md = md->next) {
+       for(; md; md = md->next, curr = curr->next) {
                ModifierTypeInfo *mti = modifierType_getInfo(md->type);
 
                if((md->mode & required_mode) != required_mode) continue;
@@ -2952,7 +1802,7 @@ static void mesh_calc_modifiers(Object *ob, float (*inputVertexCos)[3],
                                        CDDM_calc_normals(dm);
                                }
                        } else {
-                               dm = CDDM_from_mesh(me);
+                               dm = CDDM_from_mesh(me, ob);
 
                                if(deformedVerts) {
                                        CDDM_apply_vert_coords(dm, deformedVerts);
@@ -2960,6 +1810,9 @@ static void mesh_calc_modifiers(Object *ob, float (*inputVertexCos)[3],
                                }
                        }
 
+                       /* set the DerivedMesh to only copy needed data */
+                       DM_set_only_copy(dm, (CustomDataMask)curr->link);
+
                        ndm = mti->applyModifier(md, ob, dm, useRenderParams,
                                                 !inputVertexCos);
 
@@ -2997,14 +1850,14 @@ static void mesh_calc_modifiers(Object *ob, float (*inputVertexCos)[3],
                if(me->vnode)
                        *final_r = derivedmesh_from_versemesh(me->vnode, deformedVerts);
                else {
-                       *final_r = CDDM_from_mesh(me);
+                       *final_r = CDDM_from_mesh(me, ob);
                        if(deformedVerts) {
                                CDDM_apply_vert_coords(*final_r, deformedVerts);
                                CDDM_calc_normals(*final_r);
                        }
                }
 #else
-               *final_r = CDDM_from_mesh(me);
+               *final_r = CDDM_from_mesh(me, ob);
                if(deformedVerts) {
                        CDDM_apply_vert_coords(*final_r, deformedVerts);
                        CDDM_calc_normals(*final_r);
@@ -3015,6 +1868,8 @@ static void mesh_calc_modifiers(Object *ob, float (*inputVertexCos)[3],
        if(deformedVerts && deformedVerts != inputVertexCos)
                MEM_freeN(deformedVerts);
 
+       BLI_linklist_free(datamasks, NULL);
+
        /* restore mesh in any case */
        if(fluidsimMeshUsed) ob->data = ob->fluidsimSettings->orgMesh;
 }
@@ -3033,7 +1888,9 @@ static float (*editmesh_getVertexCos(EditMesh *em, int *numVerts_r))[3]
        return cos;
 }
 
-static void editmesh_calc_modifiers(DerivedMesh **cage_r, DerivedMesh **final_r)
+static void editmesh_calc_modifiers(DerivedMesh **cage_r,
+                                    DerivedMesh **final_r,
+                                    CustomDataMask dataMask)
 {
        Object *ob = G.obedit;
        EditMesh *em = G.editMesh;
@@ -3042,6 +1899,7 @@ static void editmesh_calc_modifiers(DerivedMesh **cage_r, DerivedMesh **final_r)
        DerivedMesh *dm;
        int i, numVerts = 0, cageIndex = modifiers_getCageIndex(ob, NULL);
        int required_mode = eModifierMode_Realtime | eModifierMode_Editmode;
+       LinkNode *datamasks, *curr;
 
        modifiers_clearErrors(ob);
 
@@ -3050,7 +1908,15 @@ static void editmesh_calc_modifiers(DerivedMesh **cage_r, DerivedMesh **final_r)
        }
 
        dm = NULL;
-       for(i = 0, md = ob->modifiers.first; md; i++, md = md->next) {
+       md = ob->modifiers.first;
+
+       /* we always want to keep original indices */
+       dataMask |= CD_MASK_ORIGINDEX;
+
+       datamasks = modifiers_calcDataMasks(md, dataMask);
+
+       curr = datamasks;
+       for(i = 0; md; i++, md = md->next, curr = curr->next) {
                ModifierTypeInfo *mti = modifierType_getInfo(md->type);
 
                if((md->mode & required_mode) != required_mode) continue;
@@ -3112,6 +1978,9 @@ static void editmesh_calc_modifiers(DerivedMesh **cage_r, DerivedMesh **final_r)
                                }
                        }
 
+                       /* set the DerivedMesh to only copy needed data */
+                       DM_set_only_copy(dm, (CustomDataMask)curr->link);
+
                        ndm = mti->applyModifierEM(md, ob, em, dm);
 
                        if (ndm) {
@@ -3141,6 +2010,8 @@ static void editmesh_calc_modifiers(DerivedMesh **cage_r, DerivedMesh **final_r)
                }
        }
 
+       BLI_linklist_free(datamasks, NULL);
+
        /* Yay, we are done. If we have a DerivedMesh and deformed vertices need
         * to apply these back onto the DerivedMesh. If we have no DerivedMesh
         * then we need to build one.
@@ -3152,13 +2023,17 @@ static void editmesh_calc_modifiers(DerivedMesh **cage_r, DerivedMesh **final_r)
 
                CDDM_apply_vert_coords(*final_r, deformedVerts);
                CDDM_calc_normals(*final_r);
-
-               MEM_freeN(deformedVerts);
        } else if (dm) {
                *final_r = dm;
+       } else if (cage_r && *cage_r) {
+               *final_r = *cage_r;
        } else {
                *final_r = getEditMeshDerivedMesh(em, ob, deformedVerts);
+               deformedVerts = NULL;
        }
+
+       if(deformedVerts)
+               MEM_freeN(deformedVerts);
 }
 
 /***/
@@ -3252,16 +2127,18 @@ static void clear_mesh_caches(Object *ob)
        freedisplist(&ob->disp);
 
        if (ob->derivedFinal) {
+               ob->derivedFinal->needsFree = 1;
                ob->derivedFinal->release(ob->derivedFinal);
                ob->derivedFinal= NULL;
        }
        if (ob->derivedDeform) {
+               ob->derivedDeform->needsFree = 1;
                ob->derivedDeform->release(ob->derivedDeform);
                ob->derivedDeform= NULL;
        }
 }
 
-static void mesh_build_data(Object *ob)
+static void mesh_build_data(Object *ob, CustomDataMask dataMask)
 {
        Mesh *me = ob->data;
        float min[3], max[3];
@@ -3274,25 +2151,23 @@ static void mesh_build_data(Object *ob)
                int needMapping = editing && (ob==obact);
 
                if( (G.f & G_WEIGHTPAINT) && ob==obact ) {
-                       MCol *mcol = me->mcol;
-                       TFace *tface =  me->tface;
+                       MCol *wpcol = (MCol*)calc_weightpaint_colors(ob);
+                       int layernum = CustomData_number_of_layers(&me->fdata, CD_MCOL);
 
-                       me->mcol = (MCol*) calc_weightpaint_colors(ob);
-                       if(me->tface) {
-                               me->tface = MEM_dupallocN(me->tface);
-                               mcol_to_tface(me, 1);
-                       }
+                       /* ugly hack here, we temporarily add a new active mcol layer with
+                          weightpaint colors in it, that is then duplicated in CDDM_from_mesh */
+                       CustomData_add_layer(&me->fdata, CD_MCOL, CD_ASSIGN, wpcol, me->totface);
+                       CustomData_set_layer_active(&me->fdata, CD_MCOL, layernum);
 
-                       mesh_calc_modifiers(ob, NULL, &ob->derivedDeform, &ob->derivedFinal, 0, 1,
-                                           needMapping);
+                       mesh_calc_modifiers(ob, NULL, &ob->derivedDeform,
+                                           &ob->derivedFinal, 0, 1,
+                                           needMapping, dataMask);
 
-                       if(me->mcol) MEM_freeN(me->mcol);
-                       if(me->tface) MEM_freeN(me->tface);
-                       me->mcol = mcol;
-                       me->tface = tface;
+                       CustomData_free_layer_active(&me->fdata, CD_MCOL, me->totface);
                } else {
                        mesh_calc_modifiers(ob, NULL, &ob->derivedDeform,
-                                           &ob->derivedFinal, 0, 1, needMapping);
+                                           &ob->derivedFinal, 0, 1,
+                                           needMapping, dataMask);
                }
 
                INIT_MINMAX(min, max);
@@ -3300,10 +2175,14 @@ static void mesh_build_data(Object *ob)
                ob->derivedFinal->getMinMax(ob->derivedFinal, min, max);
 
                boundbox_set_from_min_max(mesh_get_bb(ob->data), min, max);
+
+               ob->derivedFinal->needsFree = 0;
+               ob->derivedDeform->needsFree = 0;
+               ob->lastDataMask = dataMask;
        }
 }
 
-static void editmesh_build_data(void)
+static void editmesh_build_data(CustomDataMask dataMask)
 {
        float min[3], max[3];
 
@@ -3313,32 +2192,38 @@ static void editmesh_build_data(void)
 
        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;
        }
 
-       editmesh_calc_modifiers(&em->derivedCage, &em->derivedFinal);
+       editmesh_calc_modifiers(&em->derivedCage, &em->derivedFinal, dataMask);
+       em->lastDataMask = dataMask;
 
        INIT_MINMAX(min, max);
 
        em->derivedFinal->getMinMax(em->derivedFinal, min, max);
 
        boundbox_set_from_min_max(mesh_get_bb(G.obedit->data), min, max);
+
+       em->derivedFinal->needsFree = 0;
+       em->derivedCage->needsFree = 0;
 }
 
-void makeDispListMesh(Object *ob)
+void makeDerivedMesh(Object *ob, CustomDataMask dataMask)
 {
        if (ob==G.obedit) {
-               editmesh_build_data();
+               editmesh_build_data(dataMask);
        } else {
                PartEff *paf= give_parteff(ob);
                
-               mesh_build_data(ob);
+               mesh_build_data(ob, dataMask);
                
                if(paf) {
                        if((paf->flag & PAF_STATIC) || (ob->recalc & OB_RECALC_TIME)==0)
@@ -3349,104 +2234,168 @@ void makeDispListMesh(Object *ob)
 
 /***/
 
-DerivedMesh *mesh_get_derived_final(Object *ob, int *needsFree_r)
+DerivedMesh *mesh_get_derived_final(Object *ob, CustomDataMask dataMask)
 {
-       if (!ob->derivedFinal) {
-               mesh_build_data(ob);
-       }
+       /* if there's no derived mesh or the last data mask used doesn't include
+        * the data we need, rebuild the derived mesh
+        */
+       if(!ob->derivedFinal || (dataMask & ob->lastDataMask) != dataMask)
+               mesh_build_data(ob, dataMask);
 
-       *needsFree_r = 0;
        return ob->derivedFinal;
 }
 
-DerivedMesh *mesh_get_derived_deform(Object *ob, int *needsFree_r)
+DerivedMesh *mesh_get_derived_deform(Object *ob, CustomDataMask dataMask)
 {
-       if (!ob->derivedDeform) {
-               mesh_build_data(ob);
-       } 
+       /* if there's no derived mesh or the last data mask used doesn't include
+        * the data we need, rebuild the derived mesh
+        */
+       if(!ob->derivedDeform || (dataMask & ob->lastDataMask) != dataMask)
+               mesh_build_data(ob, dataMask);
 
-       *needsFree_r = 0;
        return ob->derivedDeform;
 }
 
-DerivedMesh *mesh_create_derived_render(Object *ob)
+/* Move to multires Pin level, returns a copy of the original vertex coords. */
+float *multires_render_pin(Object *ob, Mesh *me, int *orig_lvl)
 {
-       DerivedMesh *final;
-       Mesh *m= get_mesh(ob);
-       unsigned i;
+       float *vert_copy= NULL;
 
-       /* Goto the pin level for multires */
-       if(m->mr) {
-               m->mr->newlvl= m->mr->pinlvl;
-               multires_set_level(ob,m);
+       if(me->mr) {
+               MultiresLevel *lvl= NULL;
+               int i;
+               
+               /* Make sure all mesh edits are properly stored in the multires data*/
+               multires_update_levels(me, 1);
+       
+               /* Copy the highest level of multires verts */
+               *orig_lvl= me->mr->current;
+               lvl= multires_level_n(me->mr, BLI_countlist(&me->mr->levels));
+               vert_copy= MEM_callocN(sizeof(float)*3*lvl->totvert, "multires vert_copy");
+               for(i=0; i<lvl->totvert; ++i)
+                       VecCopyf(&vert_copy[i*3], lvl->verts[i].co);
+       
+               /* Goto the pin level for multires */
+               me->mr->newlvl= me->mr->pinlvl;
+               multires_set_level(ob, me, 1);
        }
+       
+       return vert_copy;
+}
 
-       mesh_calc_modifiers(ob, NULL, NULL, &final, 1, 1, 0);
+/* Propagate the changes to render level - fails if mesh topology changed */
+void multires_render_final(Object *ob, Mesh *me, DerivedMesh **dm, float *vert_copy, const int orig_lvl)
+{
+       if(me->mr) {
+               if((*dm)->getNumVerts(*dm) == me->totvert &&
+                  (*dm)->getNumFaces(*dm) == me->totface) {
+                       MultiresLevel *lvl= multires_level_n(me->mr, BLI_countlist(&me->mr->levels));
+                       DerivedMesh *old= NULL;
+                       int i;
 
-       /* Propagate the changes to render level - fails if mesh topology changed */
-       if(m->mr) {
-               if(final->getNumVerts(final) == m->totvert &&
-                  final->getNumFaces(final) == m->totface) {
-                       for(i=0; i<m->totvert; ++i)
-                               memcpy(&m->mvert[i], CustomData_get(&final->vertData, i, LAYERTYPE_MVERT), sizeof(MVert));
+                       (*dm)->copyVertArray(*dm, me->mvert);
+                       (*dm)->release(*dm);
 
-                       final->release(final);
-                       
-                       m->mr->newlvl= m->mr->renderlvl;
-                       multires_set_level(ob,m);
-                       final= getMeshDerivedMesh(m,ob,NULL);
+                       me->mr->newlvl= me->mr->renderlvl;
+                       multires_set_level(ob, me, 1);
+                       (*dm)= getMeshDerivedMesh(me, ob, NULL);
+
+                       /* Some of the data in dm is referenced externally, so make a copy */
+                       old= *dm;
+                       (*dm)= CDDM_copy(old);
+                       old->release(old);
+
+                       /* Restore the original verts */
+                       me->mr->newlvl= BLI_countlist(&me->mr->levels);
+                       multires_set_level(ob, me, 1);
+                       for(i=0; i<lvl->totvert; ++i)
+                               VecCopyf(me->mvert[i].co, &vert_copy[i*3]);
                }
-       }       
+               
+               if(vert_copy)
+                       MEM_freeN(vert_copy);
+                       
+               me->mr->newlvl= orig_lvl;
+               multires_set_level(ob, me, 1);
+       }
+}
+
+/* Multires note - if mesh has multires enabled, mesh is first set to the Pin level,
+   where all modifiers are applied, then if the topology hasn't changed, the changes
+   from modifiers are propagated up to the Render level. */
+DerivedMesh *mesh_create_derived_render(Object *ob, CustomDataMask dataMask)
+{
+       DerivedMesh *final;
+       Mesh *me= get_mesh(ob);
+       float *vert_copy= NULL;
+       int orig_lvl= 0;
+       
+       vert_copy= multires_render_pin(ob, me, &orig_lvl);
+       mesh_calc_modifiers(ob, NULL, NULL, &final, 1, 1, 0, dataMask);
+       multires_render_final(ob, me, &final, vert_copy, orig_lvl);
 
        return final;
 }
 
-DerivedMesh *mesh_create_derived_view(Object *ob)
+DerivedMesh *mesh_create_derived_view(Object *ob, CustomDataMask dataMask)
 {
        DerivedMesh *final;
 
-       mesh_calc_modifiers(ob, NULL, NULL, &final, 0, 1, 0);
+       mesh_calc_modifiers(ob, NULL, NULL, &final, 0, 1, 0, dataMask);
 
        return final;
 }
 
-DerivedMesh *mesh_create_derived_no_deform(Object *ob, float (*vertCos)[3])
+DerivedMesh *mesh_create_derived_no_deform(Object *ob, float (*vertCos)[3],
+                                           CustomDataMask dataMask)
 {
        DerivedMesh *final;
-
-       mesh_calc_modifiers(ob, vertCos, NULL, &final, 0, 0, 0);
+       
+       mesh_calc_modifiers(ob, vertCos, NULL, &final, 0, 0, 0, dataMask);
 
        return final;
 }
 
-DerivedMesh *mesh_create_derived_no_deform_render(Object *ob, float (*vertCos)[3])
+DerivedMesh *mesh_create_derived_no_deform_render(Object *ob,
+                                                  float (*vertCos)[3],
+                                                  CustomDataMask dataMask)
 {
        DerivedMesh *final;
+       Mesh *me= get_mesh(ob);
+       float *vert_copy= NULL;
+       int orig_lvl= 0;
 
-       mesh_calc_modifiers(ob, vertCos, NULL, &final, 1, 0, 0);
+       vert_copy= multires_render_pin(ob, me, &orig_lvl);
+       mesh_calc_modifiers(ob, vertCos, NULL, &final, 1, 0, 0, dataMask);
+       multires_render_final(ob, me, &final, vert_copy, orig_lvl);
 
        return final;
 }
 
 /***/
 
-DerivedMesh *editmesh_get_derived_cage_and_final(DerivedMesh **final_r, int *cageNeedsFree_r, int *finalNeedsFree_r)
+DerivedMesh *editmesh_get_derived_cage_and_final(DerivedMesh **final_r,
+                                                 CustomDataMask dataMask)
 {
-       *cageNeedsFree_r = *finalNeedsFree_r = 0;
-
-       if (!G.editMesh->derivedCage)
-               editmesh_build_data();
+       /* if there's no derived mesh or the last data mask used doesn't include
+        * the data we need, rebuild the derived mesh
+        */
+       if(!G.editMesh->derivedCage ||
+          (G.editMesh->lastDataMask & dataMask) != dataMask)
+               editmesh_build_data(dataMask);
 
        *final_r = G.editMesh->derivedFinal;
        return G.editMesh->derivedCage;
 }
 
-DerivedMesh *editmesh_get_derived_cage(int *needsFree_r)
+DerivedMesh *editmesh_get_derived_cage(CustomDataMask dataMask)
 {
-       *needsFree_r = 0;
-
-       if (!G.editMesh->derivedCage)
-               editmesh_build_data();
+       /* if there's no derived mesh or the last data mask used doesn't include
+        * the data we need, rebuild the derived mesh
+        */
+       if(!G.editMesh->derivedCage ||
+          (G.editMesh->lastDataMask & dataMask) != dataMask)
+               editmesh_build_data(dataMask);
 
        return G.editMesh->derivedCage;
 }
@@ -3488,13 +2437,12 @@ float *mesh_get_mapped_verts_nors(Object *ob)
        Mesh *me= ob->data;
        DerivedMesh *dm;
        float *vertexcosnos;
-       int needsFree;
        
        /* lets prevent crashing... */
        if(ob->type!=OB_MESH || me->totvert==0)
                return NULL;
        
-       dm= mesh_get_derived_final(ob, &needsFree);
+       dm= mesh_get_derived_final(ob, CD_MASK_BAREMESH);
        vertexcosnos= MEM_callocN(6*sizeof(float)*me->totvert, "vertexcosnos map");
        
        if(dm->foreachMappedVert) {
@@ -3510,7 +2458,7 @@ float *mesh_get_mapped_verts_nors(Object *ob)
                }
        }
        
-       if (needsFree) dm->release(dm);
+       dm->release(dm);
        return vertexcosnos;
 }
 
@@ -3529,14 +2477,14 @@ float *mesh_get_mapped_verts_nors(Object *ob)
 void writeBobjgz(char *filename, struct Object *ob, int useGlobalCoords, int append, float time) 
 {
        char debugStrBuffer[256];
-       int wri,i,j;
+       int wri,i,j,totvert,totface;
        float wrf;
        gzFile gzf;
-       DispListMesh *dlm = NULL;
        DerivedMesh *dm;
        float vec[3];
        float rotmat[3][3];
-       MFace *mface = NULL;
+       MVert *mvert;
+       MFace *mface;
        //if(append)return; // DEBUG
 
        if(!ob->data || (ob->type!=OB_MESH)) {
@@ -3558,10 +2506,13 @@ void writeBobjgz(char *filename, struct Object *ob, int useGlobalCoords, int app
                return;
        }
 
-       dm = mesh_create_derived_render(ob);
+       dm = mesh_create_derived_render(ob, CD_MASK_BAREMESH);
        //dm = mesh_create_derived_no_deform(ob,NULL);
-       dlm = dm->convertToDispListMesh(dm, 1);
-       mface = dlm->mface;
+
+       mvert = dm->getVertArray(dm);
+       mface = dm->getFaceArray(dm);
+       totvert = dm->getNumVerts(dm);
+       totface = dm->getNumFaces(dm);
 
        // write time value for appended anim mesh
        if(append) {
@@ -3570,10 +2521,11 @@ void writeBobjgz(char *filename, struct Object *ob, int useGlobalCoords, int app
 
        // continue with verts/norms
        if(sizeof(wri)!=4) { snprintf(debugStrBuffer,256,"Writing GZ_BOBJ, Invalid int size %d...\n", wri); elbeemDebugOut(debugStrBuffer); return; } // paranoia check
-       wri = dlm->totvert;
+       wri = dm->getNumVerts(dm);
+       mvert = dm->getVertArray(dm);
        gzwrite(gzf, &wri, sizeof(wri));
        for(i=0; i<wri;i++) {
-               VECCOPY(vec, dlm->mvert[i].co);
+               VECCOPY(vec, mvert[i].co);
                if(useGlobalCoords) { Mat4MulVecfl(ob->obmat, vec); }
                for(j=0; j<3; j++) {
                        wrf = vec[j]; 
@@ -3582,12 +2534,12 @@ void writeBobjgz(char *filename, struct Object *ob, int useGlobalCoords, int app
        }
 
        // should be the same as Vertices.size
-       wri = dlm->totvert;
+       wri = totvert;
        gzwrite(gzf, &wri, sizeof(wri));
        EulToMat3(ob->rot, rotmat);
        for(i=0; i<wri;i++) {
-               VECCOPY(vec, dlm->mvert[i].no);
-               Normalise(vec);
+               VECCOPY(vec, mvert[i].no);
+               Normalize(vec);
                if(useGlobalCoords) { Mat3MulVecfl(rotmat, vec); }
                for(j=0; j<3; j++) {
                        wrf = vec[j];
@@ -3602,12 +2554,12 @@ void writeBobjgz(char *filename, struct Object *ob, int useGlobalCoords, int app
        
                // compute no. of triangles 
                wri = 0;
-               for(i=0; i<dlm->totface; i++) {
+               for(i=0; i<totface; i++) {
                        wri++;
                        if(mface[i].v4) { wri++; }
                }
                gzwrite(gzf, &wri, sizeof(wri));
-               for(i=0; i<dlm->totface; i++) {
+               for(i=0; i<totface; i++) {
 
                        int face[4];
                        face[0] = mface[i].v1;
@@ -3615,15 +2567,15 @@ void writeBobjgz(char *filename, struct Object *ob, int useGlobalCoords, int app
                        face[2] = mface[i].v3;
                        face[3] = mface[i].v4;
                        //snprintf(debugStrBuffer,256,"F %s %d = %d,%d,%d,%d \n",ob->id.name, i, face[0],face[1],face[2],face[3] ); elbeemDebugOut(debugStrBuffer);
-                       //VecSubf(side1, dlm->mvert[face[1]].co,dlm->mvert[face[0]].co);
-                       //VecSubf(side2, dlm->mvert[face[2]].co,dlm->mvert[face[0]].co);
+                       //VecSubf(side1, mvert[face[1]].co,mvert[face[0]].co);
+                       //VecSubf(side2, mvert[face[2]].co,mvert[face[0]].co);
                        //Crossf(norm1,side1,side2);
                        gzwrite(gzf, &(face[0]), sizeof( face[0] )); 
                        gzwrite(gzf, &(face[1]), sizeof( face[1] )); 
                        gzwrite(gzf, &(face[2]), sizeof( face[2] )); 
                        if(face[3]) { 
-                               //VecSubf(side1, dlm->mvert[face[2]].co,dlm->mvert[face[0]].co);
-                               //VecSubf(side2, dlm->mvert[face[3]].co,dlm->mvert[face[0]].co);
+                               //VecSubf(side1, mvert[face[2]].co,mvert[face[0]].co);
+                               //VecSubf(side2, mvert[face[3]].co,mvert[face[0]].co);
                                //Crossf(norm2,side1,side2);
                                //inpf = Inpf(norm1,norm2);
                                //if(inpf>0.) {
@@ -3639,11 +2591,10 @@ void writeBobjgz(char *filename, struct Object *ob, int useGlobalCoords, int app
                }
        }
 
-       snprintf(debugStrBuffer,256,"Done. #Vertices: %d, #Triangles: %d\n", dlm->totvert, dlm->totface ); 
+       snprintf(debugStrBuffer,256,"Done. #Vertices: %d, #Triangles: %d\n", totvert, totface ); 
        elbeemDebugOut(debugStrBuffer);
        
        gzclose( gzf );
-       if(dlm) displistmesh_free(dlm);
        dm->release(dm);
 }
 
@@ -3652,36 +2603,37 @@ void initElbeemMesh(struct Object *ob,
                int *numTriangles, int **triangles,
                int useGlobalCoords) 
 {
-       DispListMesh *dlm = NULL;
        DerivedMesh *dm = NULL;
-       MFace *mface = NULL;
-       int countTris=0, i;
+       MVert *mvert;
+       MFace *mface;
+       int countTris=0, i, totvert, totface;
        float *verts;
        int *tris;
 
-       dm = mesh_create_derived_render(ob);
+       dm = mesh_create_derived_render(ob, CD_MASK_BAREMESH);
        //dm = mesh_create_derived_no_deform(ob,NULL);
-       if(!dm) { *numVertices = *numTriangles = 0; *triangles=NULL; *vertices=NULL; }
-       dlm = dm->convertToDispListMesh(dm, 1);
-       if(!dlm) { dm->release(dm); *numVertices = *numTriangles = 0; *triangles=NULL; *vertices=NULL; }
-       mface = dlm->mface;
-
-       *numVertices = dlm->totvert;
-       verts = MEM_callocN( dlm->totvert*3*sizeof(float), "elbeemmesh_vertices");
-       for(i=0; i<dlm->totvert; i++) {
-               VECCOPY( &verts[i*3], dlm->mvert[i].co);
+
+       mvert = dm->getVertArray(dm);
+       mface = dm->getFaceArray(dm);
+       totvert = dm->getNumVerts(dm);
+       totface = dm->getNumFaces(dm);
+
+       *numVertices = totvert;
+       verts = MEM_callocN( totvert*3*sizeof(float), "elbeemmesh_vertices");
+       for(i=0; i<totvert; i++) {
+               VECCOPY( &verts[i*3], mvert[i].co);
                if(useGlobalCoords) { Mat4MulVecfl(ob->obmat, &verts[i*3]); }
        }
        *vertices = verts;
 
-       for(i=0; i<dlm->totface; i++) {
+       for(i=0; i<totface; i++) {
                countTris++;
                if(mface[i].v4) { countTris++; }
        }
        *numTriangles = countTris;
        tris = MEM_callocN( countTris*3*sizeof(int), "elbeemmesh_triangles");
        countTris = 0;
-       for(i=0; i<dlm->totface; i++) {
+       for(i=0; i<totface; i++) {
                int face[4];
                face[0] = mface[i].v1;
                face[1] = mface[i].v2;
@@ -3701,7 +2653,6 @@ void initElbeemMesh(struct Object *ob,
        }
        *triangles = tris;
 
-       if(dlm) displistmesh_free(dlm);
        dm->release(dm);
 }
 
@@ -3733,14 +2684,16 @@ Mesh* readBobjgz(char *filename, Mesh *orgmesh, float* bbstart, float *bbsize) /
        newmesh->mvert= NULL;
        newmesh->medge= NULL;
        newmesh->mface= NULL;
-       newmesh->tface= NULL;
-       newmesh->dface= NULL;
+       newmesh->mtface= NULL;
 
        newmesh->dvert = NULL;
 
        newmesh->mcol= NULL;
        newmesh->msticky= NULL;
        newmesh->texcomesh= NULL;
+       memset(&newmesh->vdata, 0, sizeof(newmesh->vdata));
+       memset(&newmesh->edata, 0, sizeof(newmesh->edata));
+       memset(&newmesh->fdata, 0, sizeof(newmesh->fdata));
 
        newmesh->key= NULL;
        newmesh->totface = 0;
@@ -3762,7 +2715,7 @@ Mesh* readBobjgz(char *filename, Mesh *orgmesh, float* bbstart, float *bbsize) /
        //if(sizeof(wri)!=4) { snprintf(debugStrBuffer,256,"Reading GZ_BOBJ, Invalid int size %d...\n", wri); return NULL; } // paranoia check
        gotBytes = gzread(gzf, &wri, sizeof(wri));
        newmesh->totvert = wri;
-       newmesh->mvert = MEM_callocN(sizeof(MVert)*newmesh->totvert, "fluidsimDerivedMesh_bobjvertices");
+       newmesh->mvert = CustomData_add_layer(&newmesh->vdata, CD_MVERT, CD_CALLOC, NULL, newmesh->totvert);
        if(debugBobjRead){ snprintf(debugStrBuffer,256,"#vertices %d ", newmesh->totvert); elbeemDebugOut(debugStrBuffer); } //DEBUG
        for(i=0; i<newmesh->totvert;i++) {
                //if(debugBobjRead) snprintf(debugStrBuffer,256,"V %d = ",i);
@@ -3778,7 +2731,7 @@ Mesh* readBobjgz(char *filename, Mesh *orgmesh, float* bbstart, float *bbsize) /
        gotBytes = gzread(gzf, &wri, sizeof(wri));
        if(wri != newmesh->totvert) {
                // complain #vertices has to be equal to #normals, reset&abort
-               MEM_freeN(newmesh->mvert);
+               CustomData_free_layer_active(&newmesh->vdata, CD_MVERT, newmesh->totvert);
                MEM_freeN(newmesh);
                snprintf(debugStrBuffer,256,"Reading GZ_BOBJ, #normals=%d, #vertices=%d, aborting...\n", wri,newmesh->totvert );
                return NULL;
@@ -3798,7 +2751,7 @@ Mesh* readBobjgz(char *filename, Mesh *orgmesh, float* bbstart, float *bbsize) /
        /* compute no. of triangles */
        gotBytes = gzread(gzf, &wri, sizeof(wri));
        newmesh->totface = wri;
-       newmesh->mface = MEM_callocN(sizeof(MFace)*newmesh->totface, "fluidsimDerivedMesh_bobjfaces");
+       newmesh->mface = CustomData_add_layer(&newmesh->fdata, CD_MFACE, CD_CALLOC, NULL, newmesh->totface);
        if(debugBobjRead){ snprintf(debugStrBuffer,256,"#faces %d ", newmesh->totface); elbeemDebugOut(debugStrBuffer); } //DEBUG
        fsface = newmesh->mface;
        for(i=0; i<newmesh->totface; i++) {
@@ -3923,9 +2876,9 @@ void loadFluidsimMesh(Object *srcob, int useRenderParams)
                Mesh *freeFsMesh = srcob->fluidsimSettings->meshSurface;
 
                // similar to free_mesh(...) , but no things like unlink...
-               if(freeFsMesh->mvert){ MEM_freeN(freeFsMesh->mvert); freeFsMesh->mvert=NULL; }
-               if(freeFsMesh->medge){ MEM_freeN(freeFsMesh->medge); freeFsMesh->medge=NULL; }
-               if(freeFsMesh->mface){ MEM_freeN(freeFsMesh->mface); freeFsMesh->mface=NULL; }
+               CustomData_free(&freeFsMesh->vdata, freeFsMesh->totvert);
+               CustomData_free(&freeFsMesh->edata, freeFsMesh->totedge);
+               CustomData_free(&freeFsMesh->fdata, freeFsMesh->totface);
                MEM_freeN(freeFsMesh);
                
                if(srcob->data == srcob->fluidsimSettings->meshSurface)
@@ -4065,13 +3018,15 @@ void fluidsimGetAxisAlignedBB(struct Mesh *mesh, float obmat[][4],
                else {           newmesh = *bbmesh; }
 
                newmesh->totvert = 8;
-               if(!newmesh->mvert) newmesh->mvert = MEM_callocN(sizeof(MVert)*newmesh->totvert, "fluidsimBBMesh_bobjvertices");
+               if(!newmesh->mvert)
+                       newmesh->mvert = CustomData_add_layer(&newmesh->vdata, CD_MVERT, CD_CALLOC, NULL, newmesh->totvert);
                for(i=0; i<8; i++) {
                        for(j=0; j<3; j++) newmesh->mvert[i].co[j] = start[j]; 
                }
 
                newmesh->totface = 6;
-               if(!newmesh->mface) newmesh->mface = MEM_callocN(sizeof(MFace)*newmesh->totface, "fluidsimBBMesh_bobjfaces");
+               if(!newmesh->mface)
+                       newmesh->mface = CustomData_add_layer(&newmesh->fdata, CD_MFACE, CD_CALLOC, NULL, newmesh->totface);
 
                *bbmesh = newmesh;
        }