vgroup_modifiers: Fixed last problems with WP mode, plus a small fix in weightvg_util.c.
[blender.git] / source / blender / modifiers / intern / MOD_weightvgedit.c
index c8d7ab52ce898a6f647378b53d777c8d2e27e601..57c93cdfddf51617deda5606d5aaa394c0f435b2 100644 (file)
@@ -71,8 +71,8 @@ static void initData(ModifierData *md)
        wmd->cmap_curve             = curvemapping_add(1, 0.0, 0.0, 1.0, 1.0);
        curvemapping_initialize(wmd->cmap_curve);
 
-       wmd->clamp_min_weight       = 0.0f;
-       wmd->clamp_max_weight       = 1.0f;
+       wmd->clamp_weight_min       = 0.0f;
+       wmd->clamp_weight_max       = 1.0f;
 
        wmd->add_threshold          = 0.01f;
        wmd->rem_threshold          = 0.01f;
@@ -104,8 +104,8 @@ static void copyData(ModifierData *md, ModifierData *target)
        twmd->map_new_max            = wmd->map_new_max;
        twmd->cmap_curve             = curvemapping_copy(wmd->cmap_curve);
 
-       twmd->clamp_min_weight       = wmd->clamp_min_weight;
-       twmd->clamp_max_weight       = wmd->clamp_max_weight;
+       twmd->clamp_weight_min       = wmd->clamp_weight_min;
+       twmd->clamp_weight_max       = wmd->clamp_weight_max;
 
        twmd->add_threshold          = wmd->add_threshold;
        twmd->rem_threshold          = wmd->rem_threshold;
@@ -190,8 +190,10 @@ static DerivedMesh *applyModifier(ModifierData *md, Object *ob, DerivedMesh *der
                                   int UNUSED(useRenderParams), int UNUSED(isFinalCalc))
 {
        WeightVGEditModifierData *wmd = (WeightVGEditModifierData*) md;
-       DerivedMesh *dm = derivedData;
-       DerivedMesh *ret;
+       DerivedMesh *dm = derivedData, *ret = NULL;
+#if 0
+       Mesh *ob_m = NULL;
+#endif
        MDeformVert *dvert = NULL;
        float *org_w = NULL; /* Array original weights. */
        float *new_w = NULL; /* Array new weights. */
@@ -217,30 +219,54 @@ 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, i.e. if dm has the org data !
+       /* Get vgroup idx from its name. */
+       defgrp_idx = defgroup_name_index(ob, wmd->defgrp_name);
+       if (defgrp_idx < 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 vgroup idx from its name. */
-       defgrp_idx = defgroup_name_index(ob, wmd->defgrp_name);
-
-       /* 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
 
        /* Get org weights, assuming 0.0 for vertices not in given vgroup. */
        org_w = MEM_mallocN(sizeof(float) * numVerts, "WeightVGEdit Modifier, org_w");
@@ -287,7 +313,7 @@ static DerivedMesh *applyModifier(ModifierData *md, Object *ob, DerivedMesh *der
        /* Do clamping. */
        if (do_clamp) {
                for (i = 0; i < numVerts; i++)
-                       CLAMP(org_w[i], wmd->clamp_min_weight, wmd->clamp_max_weight);
+                       CLAMP(org_w[i], wmd->clamp_weight_min, wmd->clamp_weight_max);
        }
 
        /* Update/add/remove from vgroup. */
@@ -320,7 +346,7 @@ ModifierTypeInfo modifierType_WeightVGEdit = {
        /* structSize */        sizeof(WeightVGEditModifierData),
        /* type */              eModifierTypeType_Nonconstructive,
        /* flags */             eModifierTypeFlag_AcceptsMesh
-                              |eModifierTypeFlag_SupportsMapping
+/*                            |eModifierTypeFlag_SupportsMapping*/
                               |eModifierTypeFlag_SupportsEditmode,
 
        /* copyData */          copyData,