2.5
[blender.git] / source / blender / blenkernel / intern / modifier.c
index fc016f0f13015daa5347ee1001e86d065dd3d6ab..80a9f173d6ac119b23c9d0918fc9e670f5d41e90 100644 (file)
@@ -76,7 +76,7 @@
 #include "BKE_main.h"
 #include "BKE_anim.h"
 #include "BKE_bmesh.h"
-#include "BKE_booleanops.h"
+// XXX #include "BKE_booleanops.h"
 #include "BKE_cloth.h"
 #include "BKE_collision.h"
 #include "BKE_cdderivedmesh.h"
@@ -86,6 +86,7 @@
 #include "BKE_displist.h"
 #include "BKE_fluidsim.h"
 #include "BKE_global.h"
+#include "BKE_multires.h"
 #include "BKE_lattice.h"
 #include "BKE_library.h"
 #include "BKE_material.h"
 //XXX #include "LOD_DependKludge.h"
 #include "LOD_decimation.h"
 
+// XXX
+static struct DerivedMesh *NewBooleanDerivedMesh() {return NULL;}
+
 #include "CCGSubSurf.h"
 
 #include "RE_shader_ext.h"
 
 //XXX #include "BIF_meshlaplacian.h"
 
+/* Utility */
+
+static int is_last_displist(Object *ob)
+{
+       Curve *cu = ob->data;
+       static int curvecount=0, totcurve=0;
+
+       if(curvecount == 0){
+               DispList *dl;
+
+               totcurve = 0;
+               for(dl=cu->disp.first; dl; dl=dl->next)
+                       totcurve++;
+       }
+
+       curvecount++;
+
+       if(curvecount == totcurve){
+               curvecount = 0;
+               return 1;
+       }
+
+       return 0;
+}
+
+static DerivedMesh *get_original_dm(Scene *scene, Object *ob, float (*vertexCos)[3], int orco)
+{
+       DerivedMesh *dm= NULL;
+
+       if(ob->type==OB_MESH) {
+               dm = CDDM_from_mesh((Mesh*)(ob->data), ob);
+
+               if(vertexCos) {
+                       CDDM_apply_vert_coords(dm, vertexCos);
+                       //CDDM_calc_normals(dm);
+               }
+               
+               if(orco)
+                       DM_add_vert_layer(dm, CD_ORCO, CD_ASSIGN, get_mesh_orco_verts(ob));
+       }
+       else if(ELEM3(ob->type,OB_FONT,OB_CURVE,OB_SURF)) {
+               Object *tmpobj;
+               Curve *tmpcu;
+
+               if(is_last_displist(ob)) {
+                       /* copies object and modifiers (but not the data) */
+                       tmpobj= copy_object(ob);
+                       tmpcu = (Curve *)tmpobj->data;
+                       tmpcu->id.us--;
+
+                       /* copies the data */
+                       tmpobj->data = copy_curve((Curve *) ob->data);
+
+                       makeDispListCurveTypes(scene, tmpobj, 1);
+                       nurbs_to_mesh(tmpobj);
+
+                       dm = CDDM_from_mesh((Mesh*)(tmpobj->data), tmpobj);
+                       //CDDM_calc_normals(dm);
+
+                       free_libblock_us(&G.main->object, tmpobj);
+               }
+       }
+
+       return dm;
+}
+
 /***/
 
 static int noneModifier_isDisabled(ModifierData *md)
@@ -139,7 +209,7 @@ static void curveModifier_copyData(ModifierData *md, ModifierData *target)
        strncpy(tcmd->name, cmd->name, 32);
 }
 
-CustomDataMask curveModifier_requiredDataMask(ModifierData *md)
+CustomDataMask curveModifier_requiredDataMask(Object *ob, ModifierData *md)
 {
        CurveModifierData *cmd = (CurveModifierData *)md;
        CustomDataMask dataMask = 0;
@@ -183,7 +253,7 @@ static void curveModifier_updateDepgraph(
 
 static void curveModifier_deformVerts(
                                      ModifierData *md, Object *ob, DerivedMesh *derivedData,
-         float (*vertexCos)[3], int numVerts)
+         float (*vertexCos)[3], int numVerts, int useRenderParams, int isFinalCalc)
 {
        CurveModifierData *cmd = (CurveModifierData*) md;
 
@@ -199,7 +269,7 @@ static void curveModifier_deformVertsEM(
 
        if(!derivedData) dm = CDDM_from_editmesh(editData, ob->data);
 
-       curveModifier_deformVerts(md, ob, dm, vertexCos, numVerts);
+       curveModifier_deformVerts(md, ob, dm, vertexCos, numVerts, 0, 0);
 
        if(!derivedData) dm->release(dm);
 }
@@ -215,7 +285,7 @@ static void latticeModifier_copyData(ModifierData *md, ModifierData *target)
        strncpy(tlmd->name, lmd->name, 32);
 }
 
-CustomDataMask latticeModifier_requiredDataMask(ModifierData *md)
+CustomDataMask latticeModifier_requiredDataMask(Object *ob, ModifierData *md)
 {
        LatticeModifierData *lmd = (LatticeModifierData *)md;
        CustomDataMask dataMask = 0;
@@ -272,7 +342,7 @@ static void modifier_vgroup_cache(ModifierData *md, float (*vertexCos)[3])
 
 static void latticeModifier_deformVerts(
                                        ModifierData *md, Object *ob, DerivedMesh *derivedData,
-     float (*vertexCos)[3], int numVerts)
+     float (*vertexCos)[3], int numVerts, int useRenderParams, int isFinalCalc)
 {
        LatticeModifierData *lmd = (LatticeModifierData*) md;
 
@@ -291,7 +361,7 @@ static void latticeModifier_deformVertsEM(
 
        if(!derivedData) dm = CDDM_from_editmesh(editData, ob->data);
 
-       latticeModifier_deformVerts(md, ob, dm, vertexCos, numVerts);
+       latticeModifier_deformVerts(md, ob, dm, vertexCos, numVerts, 0, 0);
 
        if(!derivedData) dm->release(dm);
 }
@@ -598,7 +668,7 @@ static void maskModifier_copyData(ModifierData *md, ModifierData *target)
        strcpy(tmmd->vgroup, mmd->vgroup);
 }
 
-static CustomDataMask maskModifier_requiredDataMask(ModifierData *md)
+static CustomDataMask maskModifier_requiredDataMask(Object *ob, ModifierData *md)
 {
        return (1 << CD_MDEFORMVERT);
 }
@@ -3326,7 +3396,7 @@ static void bevelModifier_copyData(ModifierData *md, ModifierData *target)
        strncpy(tbmd->defgrp_name, bmd->defgrp_name, 32);
 }
 
-CustomDataMask bevelModifier_requiredDataMask(ModifierData *md)
+CustomDataMask bevelModifier_requiredDataMask(Object *ob, ModifierData *md)
 {
        BevelModifierData *bmd = (BevelModifierData *)md;
        CustomDataMask dataMask = 0;
@@ -3406,7 +3476,7 @@ static void displaceModifier_copyData(ModifierData *md, ModifierData *target)
        strncpy(tdmd->uvlayer_name, dmd->uvlayer_name, 32);
 }
 
-CustomDataMask displaceModifier_requiredDataMask(ModifierData *md)
+CustomDataMask displaceModifier_requiredDataMask(Object *ob, ModifierData *md)
 {
        DisplaceModifierData *dmd = (DisplaceModifierData *)md;
        CustomDataMask dataMask = 0;
@@ -3473,7 +3543,7 @@ static void displaceModifier_updateDepgraph(
        }
 }
 
-static void validate_layer_name(const CustomData *data, int type, char *name)
+static void validate_layer_name(const CustomData *data, int type, char *name, char *outname)
 {
        int index = -1;
 
@@ -3486,8 +3556,10 @@ static void validate_layer_name(const CustomData *data, int type, char *name)
                * deleted, so assign the active layer to name
                */
                index = CustomData_get_active_layer_index(data, CD_MTFACE);
-               strcpy(name, data->layers[index].name);
+               strcpy(outname, data->layers[index].name);
        }
+       else
+               strcpy(outname, name);
 }
 
 static void get_texture_coords(DisplaceModifierData *dmd, Object *ob,
@@ -3513,12 +3585,11 @@ static void get_texture_coords(DisplaceModifierData *dmd, Object *ob,
                        char *done = MEM_callocN(sizeof(*done) * numVerts,
                                        "get_texture_coords done");
                        int numFaces = dm->getNumFaces(dm);
+                       char uvname[32];
                        MTFace *tf;
 
-                       validate_layer_name(&dm->faceData, CD_MTFACE, dmd->uvlayer_name);
-
-                       tf = CustomData_get_layer_named(&dm->faceData, CD_MTFACE,
-                                       dmd->uvlayer_name);
+                       validate_layer_name(&dm->faceData, CD_MTFACE, dmd->uvlayer_name, uvname);
+                       tf = CustomData_get_layer_named(&dm->faceData, CD_MTFACE, uvname);
 
                        /* verts are given the UV from the first face that uses them */
                        for(i = 0, mf = mface; i < numFaces; ++i, ++mf, ++tf) {
@@ -3682,7 +3753,7 @@ static void displaceModifier_do(
 
 static void displaceModifier_deformVerts(
                                         ModifierData *md, Object *ob, DerivedMesh *derivedData,
-      float (*vertexCos)[3], int numVerts)
+      float (*vertexCos)[3], int numVerts, int useRenderParams, int isFinalCalc)
 {
        DerivedMesh *dm;
 
@@ -3749,7 +3820,7 @@ static void uvprojectModifier_copyData(ModifierData *md, ModifierData *target)
        tumd->aspecty = umd->aspecty;
 }
 
-CustomDataMask uvprojectModifier_requiredDataMask(ModifierData *md)
+CustomDataMask uvprojectModifier_requiredDataMask(Object *ob, ModifierData *md)
 {
        CustomDataMask dataMask = 0;
 
@@ -3814,6 +3885,7 @@ static DerivedMesh *uvprojectModifier_do(UVProjectModifierData *umd,
        Projector projectors[MOD_UVPROJECT_MAXPROJECTORS];
        int num_projectors = 0;
        float aspect;
+       char uvname[32];
        
        if(umd->aspecty != 0) aspect = umd->aspectx / umd->aspecty;
        else aspect = 1.0f;
@@ -3828,12 +3900,11 @@ static DerivedMesh *uvprojectModifier_do(UVProjectModifierData *umd,
        if(!dm->getFaceDataArray(dm, CD_MTFACE)) return dm;
 
        /* make sure we're using an existing layer */
-       validate_layer_name(&dm->faceData, CD_MTFACE, umd->uvlayer_name);
+       validate_layer_name(&dm->faceData, CD_MTFACE, umd->uvlayer_name, uvname);
 
        /* make sure we are not modifying the original UV layer */
        tface = CustomData_duplicate_referenced_layer_named(&dm->faceData,
-                       CD_MTFACE,
-   umd->uvlayer_name);
+                       CD_MTFACE, uvname);
 
        numVerts = dm->getNumVerts(dm);
 
@@ -4054,17 +4125,15 @@ static void decimateModifier_copyData(ModifierData *md, ModifierData *target)
        tdmd->percent = dmd->percent;
 }
 
-//XXX
-#if 0 
 static DerivedMesh *decimateModifier_applyModifier(
                ModifierData *md, Object *ob, DerivedMesh *derivedData,
   int useRenderParams, int isFinalCalc)
 {
-       DecimateModifierData *dmd = (DecimateModifierData*) md;
+       // DecimateModifierData *dmd = (DecimateModifierData*) md;
        DerivedMesh *dm = derivedData, *result = NULL;
        MVert *mvert;
        MFace *mface;
-       LOD_Decimation_Info lod;
+       // LOD_Decimation_Info lod;
        int totvert, totface;
        int a, numTris;
 
@@ -4086,6 +4155,8 @@ static DerivedMesh *decimateModifier_applyModifier(
                goto exit;
        }
 
+       // XXX
+#if 0
        lod.vertex_buffer= MEM_mallocN(3*sizeof(float)*totvert, "vertices");
        lod.vertex_normal_buffer= MEM_mallocN(3*sizeof(float)*totvert, "normals");
        lod.triangle_index_buffer= MEM_mallocN(3*sizeof(int)*numTris, "trias");
@@ -4170,11 +4241,14 @@ static DerivedMesh *decimateModifier_applyModifier(
        MEM_freeN(lod.vertex_buffer);
        MEM_freeN(lod.vertex_normal_buffer);
        MEM_freeN(lod.triangle_index_buffer);
+#else
+       modifier_setError(md, "Modifier not working yet in 2.5.");
+       goto exit;
+#endif
 
 exit:
                return result;
 }
-#endif
 
 /* Smooth */
 
@@ -4212,7 +4286,7 @@ int smoothModifier_isDisabled(ModifierData *md)
        return 0;
 }
 
-CustomDataMask smoothModifier_requiredDataMask(ModifierData *md)
+CustomDataMask smoothModifier_requiredDataMask(Object *ob, ModifierData *md)
 {
        SmoothModifierData *smd = (SmoothModifierData *)md;
        CustomDataMask dataMask = 0;
@@ -4364,7 +4438,7 @@ static void smoothModifier_do(
 
 static void smoothModifier_deformVerts(
                                       ModifierData *md, Object *ob, DerivedMesh *derivedData,
-          float (*vertexCos)[3], int numVerts)
+          float (*vertexCos)[3], int numVerts, int useRenderParams, int isFinalCalc)
 {
        DerivedMesh *dm;
 
@@ -4441,7 +4515,7 @@ int castModifier_isDisabled(ModifierData *md)
        return 0;
 }
 
-CustomDataMask castModifier_requiredDataMask(ModifierData *md)
+CustomDataMask castModifier_requiredDataMask(Object *ob, ModifierData *md)
 {
        CastModifierData *cmd = (CastModifierData *)md;
        CustomDataMask dataMask = 0;
@@ -4944,7 +5018,7 @@ static void castModifier_cuboid_do(
 
 static void castModifier_deformVerts(
                                     ModifierData *md, Object *ob, DerivedMesh *derivedData,
-        float (*vertexCos)[3], int numVerts)
+        float (*vertexCos)[3], int numVerts, int useRenderParams, int isFinalCalc)
 {
        DerivedMesh *dm = derivedData;
        CastModifierData *cmd = (CastModifierData *)md;
@@ -5072,7 +5146,7 @@ static void waveModifier_updateDepgraph(
        }
 }
 
-CustomDataMask waveModifier_requiredDataMask(ModifierData *md)
+CustomDataMask waveModifier_requiredDataMask(Object *ob, ModifierData *md)
 {
        WaveModifierData *wmd = (WaveModifierData *)md;
        CustomDataMask dataMask = 0;
@@ -5112,12 +5186,11 @@ static void wavemod_get_texture_coords(WaveModifierData *wmd, Object *ob,
                        char *done = MEM_callocN(sizeof(*done) * numVerts,
                                        "get_texture_coords done");
                        int numFaces = dm->getNumFaces(dm);
+                       char uvname[32];
                        MTFace *tf;
 
-                       validate_layer_name(&dm->faceData, CD_MTFACE, wmd->uvlayer_name);
-
-                       tf = CustomData_get_layer_named(&dm->faceData, CD_MTFACE,
-                                       wmd->uvlayer_name);
+                       validate_layer_name(&dm->faceData, CD_MTFACE, wmd->uvlayer_name, uvname);
+                       tf = CustomData_get_layer_named(&dm->faceData, CD_MTFACE, uvname);
 
                        /* verts are given the UV from the first face that uses them */
                        for(i = 0, mf = mface; i < numFaces; ++i, ++mf, ++tf) {
@@ -5347,7 +5420,7 @@ static void waveModifier_do(WaveModifierData *md,
 
 static void waveModifier_deformVerts(
                                     ModifierData *md, Object *ob, DerivedMesh *derivedData,
-        float (*vertexCos)[3], int numVerts)
+        float (*vertexCos)[3], int numVerts, int useRenderParams, int isFinalCalc)
 {
        DerivedMesh *dm;
        WaveModifierData *wmd = (WaveModifierData *)md;
@@ -5409,7 +5482,7 @@ static void armatureModifier_copyData(ModifierData *md, ModifierData *target)
        strncpy(tamd->defgrp_name, amd->defgrp_name, 32);
 }
 
-CustomDataMask armatureModifier_requiredDataMask(ModifierData *md)
+CustomDataMask armatureModifier_requiredDataMask(Object *ob, ModifierData *md)
 {
        CustomDataMask dataMask = 0;
 
@@ -5452,7 +5525,7 @@ static void armatureModifier_updateDepgraph(
 
 static void armatureModifier_deformVerts(
                                         ModifierData *md, Object *ob, DerivedMesh *derivedData,
-      float (*vertexCos)[3], int numVerts)
+      float (*vertexCos)[3], int numVerts, int useRenderParams, int isFinalCalc)
 {
        ArmatureModifierData *amd = (ArmatureModifierData*) md;
 
@@ -5523,7 +5596,7 @@ static void hookModifier_copyData(ModifierData *md, ModifierData *target)
        strncpy(thmd->name, hmd->name, 32);
 }
 
-CustomDataMask hookModifier_requiredDataMask(ModifierData *md)
+CustomDataMask hookModifier_requiredDataMask(Object *ob, ModifierData *md)
 {
        HookModifierData *hmd = (HookModifierData *)md;
        CustomDataMask dataMask = 0;
@@ -5573,7 +5646,7 @@ static void hookModifier_updateDepgraph(ModifierData *md, DagForest *forest, Sce
 
 static void hookModifier_deformVerts(
                                     ModifierData *md, Object *ob, DerivedMesh *derivedData,
-        float (*vertexCos)[3], int numVerts)
+        float (*vertexCos)[3], int numVerts, int useRenderParams, int isFinalCalc)
 {
        HookModifierData *hmd = (HookModifierData*) md;
        float vec[3], mat[4][4];
@@ -5694,7 +5767,7 @@ static void hookModifier_deformVertsEM(
 
        if(!derivedData) dm = CDDM_from_editmesh(editData, ob->data);
 
-       hookModifier_deformVerts(md, ob, derivedData, vertexCos, numVerts);
+       hookModifier_deformVerts(md, ob, derivedData, vertexCos, numVerts, 0, 0);
 
        if(!derivedData) dm->release(dm);
 }
@@ -5703,7 +5776,7 @@ static void hookModifier_deformVertsEM(
 
 static void softbodyModifier_deformVerts(
                                         ModifierData *md, Object *ob, DerivedMesh *derivedData,
-      float (*vertexCos)[3], int numVerts)
+      float (*vertexCos)[3], int numVerts, int useRenderParams, int isFinalCalc)
 {
        sbObjectStep(md->scene, ob, (float)md->scene->r.cfra, vertexCos, numVerts);
 }
@@ -5782,7 +5855,7 @@ static void clothModifier_updateDepgraph(
        }
 }
 
-CustomDataMask clothModifier_requiredDataMask(ModifierData *md)
+CustomDataMask clothModifier_requiredDataMask(Object *ob, ModifierData *md)
 {
        CustomDataMask dataMask = 0;
 
@@ -5891,7 +5964,7 @@ static int collisionModifier_dependsOnTime(ModifierData *md)
 
 static void collisionModifier_deformVerts(
                                          ModifierData *md, Object *ob, DerivedMesh *derivedData,
-       float (*vertexCos)[3], int numVerts)
+       float (*vertexCos)[3], int numVerts, int useRenderParams, int isFinalCalc)
 {
        CollisionModifierData *collmd = (CollisionModifierData*) md;
        DerivedMesh *dm = NULL;
@@ -6018,6 +6091,81 @@ static void collisionModifier_deformVerts(
 }
 
 
+
+/* Surface */
+
+static void surfaceModifier_initData(ModifierData *md) 
+{
+       SurfaceModifierData *surmd = (SurfaceModifierData*) md;
+       
+       surmd->bvhtree = NULL;
+}
+
+static void surfaceModifier_freeData(ModifierData *md)
+{
+       SurfaceModifierData *surmd = (SurfaceModifierData*) md;
+       
+       if (surmd)
+       {
+               if(surmd->bvhtree) {
+                       free_bvhtree_from_mesh(surmd->bvhtree);
+                       MEM_freeN(surmd->bvhtree);
+               }
+
+               if(surmd->dm)
+                       surmd->dm->release(surmd->dm);
+               
+               surmd->bvhtree = NULL;
+               surmd->dm = NULL;
+       }
+}
+
+static int surfaceModifier_dependsOnTime(ModifierData *md)
+{
+       return 1;
+}
+
+static void surfaceModifier_deformVerts(
+                                         ModifierData *md, Object *ob, DerivedMesh *derivedData,
+       float (*vertexCos)[3], int numVerts, int useRenderParams, int isFinalCalc)
+{
+       SurfaceModifierData *surmd = (SurfaceModifierData*) md;
+       unsigned int numverts = 0, i = 0;
+       
+       if(surmd->dm)
+               surmd->dm->release(surmd->dm);
+
+       /* if possible use/create DerivedMesh */
+       if(derivedData) surmd->dm = CDDM_copy(derivedData);
+       else surmd->dm = get_original_dm(md->scene, ob, NULL, 0);
+       
+       if(!ob->pd)
+       {
+               printf("surfaceModifier_deformVerts: Should not happen!\n");
+               return;
+       }
+       
+       if(surmd->dm)
+       {
+               CDDM_apply_vert_coords(surmd->dm, vertexCos);
+               CDDM_calc_normals(surmd->dm);
+               
+               numverts = surmd->dm->getNumVerts ( surmd->dm );
+
+               /* convert to global coordinates */
+               for(i = 0; i<numverts; i++)
+                       Mat4MulVecfl(ob->obmat, CDDM_get_vert(surmd->dm, i)->co);
+
+               if(surmd->bvhtree)
+                       free_bvhtree_from_mesh(surmd->bvhtree);
+               else
+                       surmd->bvhtree = MEM_callocN(sizeof(BVHTreeFromMesh), "BVHTreeFromMesh");
+
+               bvhtree_from_mesh_faces(surmd->bvhtree, surmd->dm, 0.0, 2, 6);
+       }
+}
+
+
 /* Boolean */
 
 static void booleanModifier_copyData(ModifierData *md, ModifierData *target)
@@ -6066,22 +6214,44 @@ static DerivedMesh *booleanModifier_applyModifier(
 {
        // XXX doesn't handle derived data
        BooleanModifierData *bmd = (BooleanModifierData*) md;
+       DerivedMesh *dm = mesh_get_derived_final(md->scene, bmd->object, CD_MASK_BAREMESH);
 
        /* we do a quick sanity check */
-       if(((Mesh *)ob->data)->totface > 3
-                   && bmd->object && ((Mesh *)bmd->object->data)->totface > 3) {
-               DerivedMesh *result = NewBooleanDerivedMesh(bmd->object, ob,
+       if(dm && (derivedData->getNumFaces(derivedData) > 3)
+                   && bmd->object && dm->getNumFaces(dm) > 3) {
+               DerivedMesh *result = NewBooleanDerivedMesh(dm, bmd->object, derivedData, ob,
                                1 + bmd->operation);
 
+               if(dm)
+                       dm->release(dm);
+
                /* if new mesh returned, return it; otherwise there was
                * an error, so delete the modifier object */
                if(result)
                        return result;
                else
                        bmd->object = NULL;
-                   }
+       }
+       
+       if(dm)
+                       dm->release(dm);
+
+       return derivedData;
+}
 
-                   return derivedData;
+CustomDataMask booleanModifier_requiredDataMask(Object *ob, ModifierData *md)
+{
+       CustomDataMask dataMask = (1 << CD_MTFACE) + (1 << CD_MEDGE);
+
+       dataMask |= (1 << CD_MDEFORMVERT);
+       
+       /* particles only need this if they are after a non deform modifier, and
+       * the modifier stack will only create them in that case. */
+//     dataMask |= CD_MASK_ORIGSPACE;
+
+//     dataMask |= CD_MASK_ORCO;
+       
+       return dataMask;
 }
 
 /* Particles */
@@ -6117,12 +6287,30 @@ static void particleSystemModifier_copyData(ModifierData *md, ModifierData *targ
        tpsmd->psys = psmd->psys;
 }
 
-CustomDataMask particleSystemModifier_requiredDataMask(ModifierData *md)
+CustomDataMask particleSystemModifier_requiredDataMask(Object *ob, ModifierData *md)
 {
        ParticleSystemModifierData *psmd= (ParticleSystemModifierData*) md;
-       CustomDataMask dataMask = (1 << CD_MTFACE) + (1 << CD_MEDGE);
+       CustomDataMask dataMask = 0;
+       Material *ma;
+       MTex *mtex;
        int i;
 
+       if(!psmd->psys->part)
+               return 0;
+
+       ma= give_current_material(ob, psmd->psys->part->omat);
+       if(ma) {
+               for(i=0; i<MAX_MTEX; i++) {
+                       mtex=ma->mtex[i];
+                       if(mtex && (ma->septex & (1<<i))==0)
+                               if(mtex->pmapto && (mtex->texco & TEXCO_UV))
+                                       dataMask |= (1 << CD_MTFACE);
+               }
+       }
+
+       if(psmd->psys->part->tanfac!=0.0)
+               dataMask |= (1 << CD_MTFACE);
+
        /* ask for vertexgroups if we need them */
        for(i=0; i<PSYS_TOT_VG; i++){
                if(psmd->psys->vgroup[i]){
@@ -6139,38 +6327,15 @@ CustomDataMask particleSystemModifier_requiredDataMask(ModifierData *md)
        
        return dataMask;
 }
-static int is_last_displist(Object *ob)
-{
-       Curve *cu = ob->data;
-       static int curvecount=0, totcurve=0;
-
-       if(curvecount==0){
-               DispList *dl;
-
-               totcurve=0;
-               for(dl=cu->disp.first; dl; dl=dl->next){
-                       totcurve++;
-               }
-       }
 
-       curvecount++;
-
-       if(curvecount==totcurve){
-               curvecount=0;
-               return 1;
-       }
-
-       return 0;
-}
 /* saves the current emitter state for a particle system and calculates particles */
 static void particleSystemModifier_deformVerts(
                                               ModifierData *md, Object *ob, DerivedMesh *derivedData,
-           float (*vertexCos)[3], int numVerts)
+           float (*vertexCos)[3], int numVerts, int useRenderParams, int isFinalCalc)
 {
        DerivedMesh *dm = derivedData;
        ParticleSystemModifierData *psmd= (ParticleSystemModifierData*) md;
        ParticleSystem * psys=0;
-       Mesh *me;
        int needsFree=0;
 
        if(ob->particlesystem.first)
@@ -6178,54 +6343,16 @@ static void particleSystemModifier_deformVerts(
        else
                return;
        
-       /* multires check */
-       if(ob->type == OB_MESH) {
-               me= (Mesh*)ob->data;
-               if(me->mr && me->mr->current != 1)
-                       modifier_setError(md,
-                               "Particles only supported on first multires level.");
-       }
-
        if(!psys_check_enabled(ob, psys))
                return;
 
-       if(dm==0){
-               if(ob->type==OB_MESH){
-                       dm = CDDM_from_mesh((Mesh*)(ob->data), ob);
-
-                       CDDM_apply_vert_coords(dm, vertexCos);
-                       //CDDM_calc_normals(dm);
-                       
-                       DM_add_vert_layer(dm, CD_ORCO, CD_ASSIGN, get_mesh_orco_verts(ob));
-
-                       needsFree=1;
-               }
-               else if(ELEM3(ob->type,OB_FONT,OB_CURVE,OB_SURF)){
-                       Object *tmpobj;
-                       Curve *tmpcu;
-
-                       if(is_last_displist(ob)){
-                               /* copies object and modifiers (but not the data) */
-                               tmpobj= copy_object( ob );
-                               tmpcu = (Curve *)tmpobj->data;
-                               tmpcu->id.us--;
+       if(dm==0) {
+               dm= get_original_dm(md->scene, ob, vertexCos, 1);
 
-                               /* copies the data */
-                               tmpobj->data = copy_curve( (Curve *) ob->data );
-
-                               makeDispListCurveTypes(md->scene, tmpobj, 1 );
-                               nurbs_to_mesh( tmpobj );
-
-                               dm = CDDM_from_mesh((Mesh*)(tmpobj->data), tmpobj);
-                               //CDDM_calc_normals(dm);
-
-                               free_libblock_us( &G.main->object, tmpobj );
+               if(!dm)
+                       return;
 
-                               needsFree=1;
-                       }
-                       else return;
-               }
-               else return;
+               needsFree= 1;
        }
 
        /* clear old dm */
@@ -6253,8 +6380,7 @@ static void particleSystemModifier_deformVerts(
                  psmd->dm->getNumFaces(psmd->dm)!=psmd->totdmface){
                /* in file read dm hasn't really changed but just wasn't saved in file */
 
-               psys->recalc |= PSYS_RECALC_HAIR;
-               psys->recalc |= PSYS_DISTR;
+               psys->recalc |= PSYS_RECALC_RESET;
                psmd->flag |= eParticleSystemFlag_DM_changed;
 
                psmd->totdmvert= psmd->dm->getNumVerts(psmd->dm);
@@ -6382,20 +6508,12 @@ static DerivedMesh * particleInstanceModifier_applyModifier(
        psys->lattice=psys_get_lattice(md->scene, ob, psys);
 
        if(psys->flag & (PSYS_HAIR_DONE|PSYS_KEYED)){
-               float co[3];
-               for(i=0; i< totvert; i++){
-                       dm->getVertCo(dm,i,co);
-                       if(i==0){
-                               min_co=max_co=co[track];
-                       }
-                       else{
-                               if(co[track]<min_co)
-                                       min_co=co[track];
 
-                               if(co[track]>max_co)
-                                       max_co=co[track];
-                       }
-               }
+               float min_r[3], max_r[3];
+               INIT_MINMAX(min_r, max_r);
+               dm->getMinMax(dm, min_r, max_r);                
+               min_co=min_r[track];
+               max_co=max_r[track];
        }
 
        result = CDDM_from_template(dm, maxvert,dm->getNumEdges(dm)*totpart,maxface);
@@ -6496,8 +6614,8 @@ static DerivedMesh * particleInstanceModifier_applyModifier(
        CDDM_calc_normals(result);
 
        if(psys->lattice){
-               end_latt_deform();
-               psys->lattice=0;
+               end_latt_deform(psys->lattice);
+               psys->lattice= NULL;
        }
 
        return result;
@@ -6537,7 +6655,7 @@ static int explodeModifier_dependsOnTime(ModifierData *md)
 {
        return 1;
 }
-CustomDataMask explodeModifier_requiredDataMask(ModifierData *md)
+CustomDataMask explodeModifier_requiredDataMask(Object *ob, ModifierData *md)
 {
        ExplodeModifierData *emd= (ExplodeModifierData*) md;
        CustomDataMask dataMask = 0;
@@ -7203,7 +7321,7 @@ static DerivedMesh * explodeModifier_explodeMesh(ExplodeModifierData *emd,
                        pa= pars+i;
 
                        /* get particle state */
-                       psys_particle_on_emitter(psmd,part->from,pa->num,-1,pa->fuv,pa->foffset,loc0,nor,0,0,0,0);
+                       psys_particle_on_emitter(psmd,part->from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,loc0,nor,0,0,0,0);
                        Mat4MulVecfl(ob->obmat,loc0);
 
                        state.time=cfra;
@@ -7259,7 +7377,7 @@ static DerivedMesh * explodeModifier_explodeMesh(ExplodeModifierData *emd,
 
                *mf = source;
 
-               test_index_face(mf, &explode->faceData, i, (mf->v4 ? 4 : 3));
+               test_index_face(mf, &explode->faceData, i, (orig_v4 ? 4 : 3));
        }
 
        MEM_printmemlist_stats();
@@ -7272,8 +7390,8 @@ static DerivedMesh * explodeModifier_explodeMesh(ExplodeModifierData *emd,
        CDDM_calc_normals(explode);
 
        if(psmd->psys->lattice){
-               end_latt_deform();
-               psmd->psys->lattice=0;
+               end_latt_deform(psmd->psys->lattice);
+               psmd->psys->lattice= NULL;
        }
 
        return explode;
@@ -7323,7 +7441,7 @@ static DerivedMesh * explodeModifier_applyModifier(
                                 if(emd->flag & eExplodeFlag_EdgeSplit){
                                         int *facepa = emd->facepa;
                                         DerivedMesh *splitdm=explodeModifier_splitEdges(emd,dm);
-                                        DerivedMesh *explode=explodeModifier_explodeMesh(emd, psmd, scene, ob, splitdm);
+                                        DerivedMesh *explode=explodeModifier_explodeMesh(emd, psmd, md->scene, ob, splitdm);
 
                                         MEM_freeN(emd->facepa);
                                         emd->facepa=facepa;
@@ -7331,7 +7449,7 @@ static DerivedMesh * explodeModifier_applyModifier(
                                         return explode;
                                 }
                                 else
-                                        return explodeModifier_explodeMesh(emd, psmd, scene, ob, derivedData);
+                                        return explodeModifier_explodeMesh(emd, psmd, md->scene, ob, derivedData);
        }
        return derivedData;
 }
@@ -7451,7 +7569,7 @@ static void meshdeformModifier_copyData(ModifierData *md, ModifierData *target)
        tmmd->object = mmd->object;
 }
 
-CustomDataMask meshdeformModifier_requiredDataMask(ModifierData *md)
+CustomDataMask meshdeformModifier_requiredDataMask(Object *ob, ModifierData *md)
 {      
        MeshDeformModifierData *mmd = (MeshDeformModifierData *)md;
        CustomDataMask dataMask = 0;
@@ -7555,6 +7673,7 @@ static void meshdeformModifier_do(
        DerivedMesh *tmpdm, *cagedm;
        MDeformVert *dvert = NULL;
        MDeformWeight *dw;
+       EditMesh *em = BKE_mesh_get_editmesh(me);
        MVert *cagemvert;
        float imat[4][4], cagemat[4][4], iobmat[4][4], icagemat[3][3], cmat[4][4];
        float weight, totweight, fac, co[3], *weights, (*dco)[3], (*bindcos)[3];
@@ -7564,13 +7683,22 @@ static void meshdeformModifier_do(
                return;
        
        /* get cage derivedmesh */
-       if(me->edit_mesh) {
-               tmpdm= editmesh_get_derived_cage_and_final(md->scene, ob, me->edit_mesh, &cagedm, 0);
+       if(em) {
+               tmpdm= editmesh_get_derived_cage_and_final(md->scene, ob, em, &cagedm, 0);
                if(tmpdm)
                        tmpdm->release(tmpdm);
+               BKE_mesh_end_editmesh(me, em);
        }
        else
                cagedm= mmd->object->derivedFinal;
+
+       /* if we don't have one computed, use derivedmesh from data
+        * without any modifiers */
+       if(!cagedm) {
+               cagedm= get_original_dm(md->scene, mmd->object, NULL, 0);
+               if(cagedm)
+                       cagedm->needsFree= 1;
+       }
        
        if(!cagedm)
                return;
@@ -7701,14 +7829,16 @@ static void meshdeformModifier_do(
 
 static void meshdeformModifier_deformVerts(
                                           ModifierData *md, Object *ob, DerivedMesh *derivedData,
-       float (*vertexCos)[3], int numVerts)
+       float (*vertexCos)[3], int numVerts, int useRenderParams, int isFinalCalc)
 {
        DerivedMesh *dm;
 
-       if(!derivedData && ob->type==OB_MESH)
-               dm= CDDM_from_mesh(ob->data, ob);
-       else
-               dm= derivedData;
+       if (!derivedData) {
+               dm= get_original_dm(md->scene, ob, NULL, 0);
+               if (dm == NULL) return;
+       }
+       else dm= derivedData;
+
 
        modifier_vgroup_cache(md, vertexCos); /* if next modifier needs original vertices */
        
@@ -7735,6 +7865,60 @@ static void meshdeformModifier_deformVertsEM(
                dm->release(dm);
 }
 
+/* Multires */
+static void multiresModifier_initData(ModifierData *md)
+{
+       MultiresModifierData *mmd = (MultiresModifierData*)md;
+
+       mmd->lvl = mmd->totlvl = 1;
+}
+
+static void multiresModifier_freeData(ModifierData *md)
+{
+       MultiresModifierData *mmd = (MultiresModifierData*)md;
+
+       if(mmd->undo_verts)
+               MEM_freeN(mmd->undo_verts);
+}
+
+static void multiresModifier_copyData(ModifierData *md, ModifierData *target)
+{
+       MultiresModifierData *mmd = (MultiresModifierData*) md;
+       MultiresModifierData *tmmd = (MultiresModifierData*) target;
+
+       tmmd->totlvl = mmd->totlvl;
+       tmmd->lvl = mmd->lvl;
+}
+
+static DerivedMesh *multiresModifier_applyModifier(ModifierData *md, Object *ob, DerivedMesh *dm,
+                                                  int useRenderParams, int isFinalCalc)
+{
+       MultiresModifierData *mmd = (MultiresModifierData*)md;
+       Mesh *me = get_mesh(ob);
+       DerivedMesh *final;
+
+       /* TODO: for now just skip a level1 mesh */
+       if(mmd->lvl == 1)
+               return dm;
+
+       final = multires_dm_create_from_derived(mmd, dm, me, useRenderParams, isFinalCalc);
+       if(mmd->undo_signal && mmd->undo_verts && mmd->undo_verts_tot == final->getNumVerts(final)) {
+               int i;
+               MVert *dst = CDDM_get_verts(final);
+               for(i = 0; i < mmd->undo_verts_tot; ++i) {
+                       VecCopyf(dst[i].co, mmd->undo_verts[i].co);
+               }
+               CDDM_calc_normals(final);
+
+               MultiresDM_mark_as_modified(final);
+
+               MEM_freeN(mmd->undo_verts);
+               mmd->undo_signal = 0;
+               mmd->undo_verts = NULL;
+       }
+
+       return final;
+}
 
 /* Shrinkwrap */
 
@@ -7766,7 +7950,7 @@ static void shrinkwrapModifier_copyData(ModifierData *md, ModifierData *target)
        tsmd->subsurfLevels = smd->subsurfLevels;
 }
 
-CustomDataMask shrinkwrapModifier_requiredDataMask(ModifierData *md)
+CustomDataMask shrinkwrapModifier_requiredDataMask(Object *ob, ModifierData *md)
 {
        ShrinkwrapModifierData *smd = (ShrinkwrapModifierData *)md;
        CustomDataMask dataMask = 0;
@@ -7797,10 +7981,10 @@ static void shrinkwrapModifier_foreachObjectLink(ModifierData *md, Object *ob, O
        walk(userData, ob, &smd->auxTarget);
 }
 
-static void shrinkwrapModifier_deformVerts(ModifierData *md, Object *ob, DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts)
+static void shrinkwrapModifier_deformVerts(ModifierData *md, Object *ob, DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts, int useRenderParams, int isFinalCalc)
 {
        DerivedMesh *dm = NULL;
-       CustomDataMask dataMask = shrinkwrapModifier_requiredDataMask(md);
+       CustomDataMask dataMask = shrinkwrapModifier_requiredDataMask(ob, md);
 
        /* We implement requiredDataMask but thats not really usefull since mesh_calc_modifiers pass a NULL derivedData or without the modified vertexs applied */
        if(dataMask)
@@ -7810,7 +7994,7 @@ static void shrinkwrapModifier_deformVerts(ModifierData *md, Object *ob, Derived
                else if(ob->type==OB_LATTICE) dm = NULL;
                else return;
 
-               if(dm != NULL && (dataMask & CD_MVERT))
+               if(dm != NULL && (dataMask & (1<<CD_MVERT)))
                {
                        CDDM_apply_vert_coords(dm, vertexCos);
                        CDDM_calc_normals(dm);
@@ -7826,7 +8010,7 @@ static void shrinkwrapModifier_deformVerts(ModifierData *md, Object *ob, Derived
 static void shrinkwrapModifier_deformVertsEM(ModifierData *md, Object *ob, EditMesh *editData, DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts)
 {
        DerivedMesh *dm = NULL;
-       CustomDataMask dataMask = shrinkwrapModifier_requiredDataMask(md);
+       CustomDataMask dataMask = shrinkwrapModifier_requiredDataMask(ob, md);
 
        if(dataMask)
        {
@@ -7835,7 +8019,7 @@ static void shrinkwrapModifier_deformVertsEM(ModifierData *md, Object *ob, EditM
                else if(ob->type==OB_LATTICE) dm = NULL;
                else return;
 
-               if(dm != NULL && (dataMask & CD_MVERT))
+               if(dm != NULL && (dataMask & (1<<CD_MVERT)))
                {
                        CDDM_apply_vert_coords(dm, vertexCos);
                        CDDM_calc_normals(dm);
@@ -7885,7 +8069,7 @@ static void simpledeformModifier_copyData(ModifierData *md, ModifierData *target
        memcpy(tsmd->limit, smd->limit, sizeof(tsmd->limit));
 }
 
-static CustomDataMask simpledeformModifier_requiredDataMask(ModifierData *md)
+static CustomDataMask simpledeformModifier_requiredDataMask(Object *ob, ModifierData *md)
 {
        SimpleDeformModifierData *smd = (SimpleDeformModifierData *)md;
        CustomDataMask dataMask = 0;
@@ -7911,10 +8095,10 @@ static void simpledeformModifier_updateDepgraph(ModifierData *md, DagForest *for
                dag_add_relation(forest, dag_get_node(forest, smd->origin), obNode, DAG_RL_OB_DATA, "SimpleDeform Modifier");
 }
 
-static void simpledeformModifier_deformVerts(ModifierData *md, Object *ob, DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts)
+static void simpledeformModifier_deformVerts(ModifierData *md, Object *ob, DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts, int useRenderParams, int isFinalCalc)
 {
        DerivedMesh *dm = NULL;
-       CustomDataMask dataMask = simpledeformModifier_requiredDataMask(md);
+       CustomDataMask dataMask = simpledeformModifier_requiredDataMask(ob, md);
 
        /* We implement requiredDataMask but thats not really usefull since mesh_calc_modifiers pass a NULL derivedData or without the modified vertexs applied */
        if(dataMask)
@@ -7941,7 +8125,7 @@ static void simpledeformModifier_deformVerts(ModifierData *md, Object *ob, Deriv
 static void simpledeformModifier_deformVertsEM(ModifierData *md, Object *ob, EditMesh *editData, DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts)
 {
        DerivedMesh *dm = NULL;
-       CustomDataMask dataMask = simpledeformModifier_requiredDataMask(md);
+       CustomDataMask dataMask = simpledeformModifier_requiredDataMask(ob, md);
 
        /* We implement requiredDataMask but thats not really usefull since mesh_calc_modifiers pass a NULL derivedData or without the modified vertexs applied */
        if(dataMask)
@@ -8134,7 +8318,7 @@ ModifierTypeInfo *modifierType_getInfo(ModifierType type)
                mti->flags = eModifierTypeFlag_AcceptsMesh;
                mti->initData = decimateModifier_initData;
                mti->copyData = decimateModifier_copyData;
-               //XXX mti->applyModifier = decimateModifier_applyModifier;
+               mti->applyModifier = decimateModifier_applyModifier;
 
                mti = INIT_TYPE(Smooth);
                mti->type = eModifierTypeType_OnlyDeform;
@@ -8228,16 +8412,24 @@ ModifierTypeInfo *modifierType_getInfo(ModifierType type)
                mti->deformVerts = collisionModifier_deformVerts;
                // mti->copyData = collisionModifier_copyData;
 
+               mti = INIT_TYPE(Surface);
+               mti->type = eModifierTypeType_OnlyDeform;
+               mti->initData = surfaceModifier_initData;
+               mti->flags = eModifierTypeFlag_AcceptsMesh;
+               mti->dependsOnTime = surfaceModifier_dependsOnTime;
+               mti->freeData = surfaceModifier_freeData; 
+               mti->deformVerts = surfaceModifier_deformVerts;
+
                mti = INIT_TYPE(Boolean);
                mti->type = eModifierTypeType_Nonconstructive;
                mti->flags = eModifierTypeFlag_AcceptsMesh
-                               | eModifierTypeFlag_RequiresOriginalData
                                | eModifierTypeFlag_UsesPointCache;
                mti->copyData = booleanModifier_copyData;
                mti->isDisabled = booleanModifier_isDisabled;
                mti->applyModifier = booleanModifier_applyModifier;
                mti->foreachObjectLink = booleanModifier_foreachObjectLink;
                mti->updateDepgraph = booleanModifier_updateDepgraph;
+               mti->requiredDataMask = booleanModifier_requiredDataMask;
 
                mti = INIT_TYPE(MeshDeform);
                mti->type = eModifierTypeType_OnlyDeform;
@@ -8335,6 +8527,14 @@ ModifierTypeInfo *modifierType_getInfo(ModifierType type)
                mti->foreachObjectLink = simpledeformModifier_foreachObjectLink;
                mti->updateDepgraph = simpledeformModifier_updateDepgraph;
 
+               mti = INIT_TYPE(Multires);
+               mti->type = eModifierTypeType_Constructive;
+               mti->flags = eModifierTypeFlag_AcceptsMesh | eModifierTypeFlag_RequiresOriginalData;
+               mti->initData = multiresModifier_initData;
+               mti->freeData = multiresModifier_freeData;
+               mti->copyData = multiresModifier_copyData;
+               mti->applyModifier = multiresModifier_applyModifier;
+
                typeArrInit = 0;
 #undef INIT_TYPE
        }
@@ -8416,8 +8616,6 @@ void modifiers_clearErrors(Object *ob)
                        qRedraw = 1;
                }
        }
-
-// XXX if (qRedraw) allqueue(REDRAWBUTSEDIT, 0);
 }
 
 void modifiers_foreachObjectLink(Object *ob, ObjectWalkFunc walk,
@@ -8483,7 +8681,6 @@ void modifier_setError(ModifierData *md, char *format, ...)
 
        md->error = BLI_strdup(buffer);
 
-// XXX allqueue(REDRAWBUTSEDIT, 0);
 }
 
 /* used for buttons, to find out if the 'draw deformed in editmode' option is
@@ -8540,7 +8737,20 @@ int modifiers_isParticleEnabled(Object *ob)
        return (md && md->mode & (eModifierMode_Realtime | eModifierMode_Render));
 }
 
-LinkNode *modifiers_calcDataMasks(ModifierData *md, CustomDataMask dataMask)
+int modifier_isEnabled(ModifierData *md, int required_mode)
+{
+       ModifierTypeInfo *mti = modifierType_getInfo(md->type);
+
+       if((md->mode & required_mode) != required_mode) return 0;
+       if(mti->isDisabled && mti->isDisabled(md)) return 0;
+       if(md->mode & eModifierMode_DisableTemporary) return 0;
+       if(required_mode & eModifierMode_Editmode)
+               if(!(mti->flags & eModifierTypeFlag_SupportsEditmode)) return 0;
+       
+       return 1;
+}
+
+LinkNode *modifiers_calcDataMasks(Object *ob, ModifierData *md, CustomDataMask dataMask, int required_mode)
 {
        LinkNode *dataMasks = NULL;
        LinkNode *curr, *prev;
@@ -8550,9 +8760,11 @@ LinkNode *modifiers_calcDataMasks(ModifierData *md, CustomDataMask dataMask)
                ModifierTypeInfo *mti = modifierType_getInfo(md->type);
                CustomDataMask mask = 0;
 
-               if(mti->requiredDataMask) mask = mti->requiredDataMask(md);
+               if(modifier_isEnabled(md, required_mode))
+                       if(mti->requiredDataMask)
+                               mask = mti->requiredDataMask(ob, md);
 
-               BLI_linklist_prepend(&dataMasks, (void *)mask);
+               BLI_linklist_prepend(&dataMasks, SET_INT_IN_POINTER(mask));
        }
 
        /* build the list of required data masks - each mask in the list must
@@ -8563,14 +8775,14 @@ LinkNode *modifiers_calcDataMasks(ModifierData *md, CustomDataMask dataMask)
        */
        for(curr = dataMasks, prev = NULL; curr; prev = curr, curr = curr->next) {
                if(prev) {
-                       CustomDataMask prev_mask = (CustomDataMask)prev->link;
-                       CustomDataMask curr_mask = (CustomDataMask)curr->link;
+                       CustomDataMask prev_mask = (CustomDataMask)GET_INT_FROM_POINTER(prev->link);
+                       CustomDataMask curr_mask = (CustomDataMask)GET_INT_FROM_POINTER(curr->link);
 
-                       curr->link = (void *)(curr_mask | prev_mask);
+                       curr->link = SET_INT_IN_POINTER(curr_mask | prev_mask);
                } else {
-                       CustomDataMask curr_mask = (CustomDataMask)curr->link;
+                       CustomDataMask curr_mask = (CustomDataMask)GET_INT_FROM_POINTER(curr->link);
 
-                       curr->link = (void *)(curr_mask | dataMask);
+                       curr->link = SET_INT_IN_POINTER(curr_mask | dataMask);
                }
        }
 
@@ -8711,15 +8923,15 @@ int modifier_isDeformer(ModifierData *md)
        return 0;
 }
 
-int modifiers_isDeformed(Object *ob)
+int modifiers_isDeformed(Scene *scene, Object *ob)
 {
        ModifierData *md = modifiers_getVirtualModifierList(ob);
        
        for (; md; md=md->next) {
-                       // XXX if(ob==obedit && (md->mode & eModifierMode_Editmode)==0);
-                       // else 
+               if(ob==scene->obedit && (md->mode & eModifierMode_Editmode)==0);
+               else 
                        if(modifier_isDeformer(md))
-                       return 1;
+                               return 1;
        }
        return 0;
 }
@@ -8752,8 +8964,10 @@ void modifier_freeTemporaryData(ModifierData *md)
        if(md->type == eModifierType_Armature) {
                ArmatureModifierData *amd= (ArmatureModifierData*)md;
 
-               if(amd->prevCos)
+               if(amd->prevCos) {
                        MEM_freeN(amd->prevCos);
+                       amd->prevCos= NULL;
+               }
        }
 }