vgroup_modifiers: Fixed last problems with WP mode, plus a small fix in weightvg_util.c.
[blender.git] / source / blender / modifiers / intern / MOD_weightvgmix.c
index c4f107de72e85bf6b5f8755a24b0e396319f5333..29e00040bf6c773f81cf3dae81231e7daf4d8fa3 100644 (file)
@@ -220,8 +220,10 @@ static DerivedMesh *applyModifier(ModifierData *md, Object *ob, DerivedMesh *der
                                   int UNUSED(useRenderParams), int UNUSED(isFinalCalc))
 {
        WeightVGMixModifierData *wmd = (WeightVGMixModifierData*) md;
-       DerivedMesh *dm = derivedData;
-       DerivedMesh *ret;
+       DerivedMesh *dm = derivedData, *ret = NULL;
+#if 0
+       Mesh *ob_m = NULL;
+#endif
        MDeformVert *dvert = NULL;
        int numVerts;
        int defgrp_idx, defgrp_idx2 = -1;
@@ -241,34 +243,60 @@ static DerivedMesh *applyModifier(ModifierData *md, Object *ob, DerivedMesh *der
        if ((numVerts == 0) || (ob->defbase.first == NULL))
                return dm;
 
-       /* Create a copy of our dmesh.
-        * TODO: This should be done only if needed !
+       /* Get vgroup idx from its name. */
+       defgrp_idx = defgroup_name_index(ob, wmd->defgrp_name);
+       if (defgrp_idx < 0)
+               return dm;
+       /* Get seconf vgroup idx from its name, if given. */
+       if (wmd->defgrp_name2[0] != (char)0) {
+               defgrp_idx2 = defgroup_name_index(ob, wmd->defgrp_name2);
+               if (defgrp_idx2 < 0)
+                       return dm;
+       }
+
+       /* XXX All this to avoid copying dm when not needed… However, it nearly doubles compute
+        *     time! See scene 5 of the WeighVG test file…
         */
-       if (1) {
-               /* XXX Seems to create problems with weightpaint mode... */
+#if 0
+       /* Get actual dverts (ie vertex group data). */
+       dvert = dm->getVertDataArray(dm, CD_MDEFORMVERT);
+       /* If no dverts, return unmodified data… */
+       if (dvert == NULL)
+               return dm;
+
+       /* Get org mesh, only to test whether affected cdata layer has already been copied
+        * somewhere up in the modifiers stack.
+        */
+       ob_m = get_mesh(ob);
+       if (ob_m == NULL)
+               return dm;
+
+       /* Create a copy of our dmesh, only if our affected cdata layer is the same as org mesh. */
+       if (dvert == CustomData_get_layer(&ob_m->vdata, CD_MDEFORMVERT)) {
+               /* XXX Seems to create problems with weightpaint mode???
+                *     I’m missing something here, I guess…
+                */
 //             DM_set_only_copy(dm, CD_MASK_MDEFORMVERT); /* Only copy defgroup layer. */
                ret = CDDM_copy(dm);
+               dvert = ret->getVertDataArray(ret, CD_MDEFORMVERT);
+               if (dvert == NULL) {
+                       ret->release(ret);
+                       return dm;
+               }
                rel_ret = 1;
        }
        else
                ret = dm;
-
-       /* Get first vgroup idx from its name. */
-       defgrp_idx = defgroup_name_index(ob, wmd->defgrp_name);
-
-       /* Get seconf vgroup idx from its name, if given. */
-       if (wmd->defgrp_name2)
-               defgrp_idx2 = defgroup_name_index(ob, wmd->defgrp_name2);
-
-       /* Get actual dverts (ie vertex group data). */
-       if (defgrp_idx >= 0)
-               dvert = ret->getVertDataArray(ret, CD_MDEFORMVERT);
-       /* If no dverts, return unmodified data… */
-       if ((defgrp_idx < 0) || (dvert == NULL)) {
+#else
+       ret = CDDM_copy(dm);
+       rel_ret = 1;
+       dvert = ret->getVertDataArray(ret, CD_MDEFORMVERT);
+       if (dvert == NULL) {
                if (rel_ret)
                        ret->release(ret);
                return dm;
        }
+#endif
 
        /* Find out which vertices to work on. */
        tidx = MEM_mallocN(sizeof(int) * numVerts, "WeightVGMix Modifier, tidx");
@@ -410,7 +438,7 @@ ModifierTypeInfo modifierType_WeightVGMix = {
        /* structSize */        sizeof(WeightVGMixModifierData),
        /* type */              eModifierTypeType_Nonconstructive,
        /* flags */             eModifierTypeFlag_AcceptsMesh
-                              |eModifierTypeFlag_SupportsMapping
+/*                            |eModifierTypeFlag_SupportsMapping*/
                               |eModifierTypeFlag_SupportsEditmode,
 
        /* copyData */          copyData,