fix for crash when painting normalize with no vertex groups, also minor cleanup to...
authorCampbell Barton <ideasman42@gmail.com>
Thu, 6 Oct 2011 01:29:32 +0000 (01:29 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Thu, 6 Oct 2011 01:29:32 +0000 (01:29 +0000)
source/blender/editors/sculpt_paint/paint_vertex.c

index 0c2cb2ee36df89b1a20cf0250402cf727a2515ce..3ba81715b90a57579061f33126578fb8de791d80 100644 (file)
@@ -1114,31 +1114,33 @@ static void do_weight_paint_auto_normalize(MDeformVert *dvert,
 #endif
 
 /* the active group should be involved in auto normalize */
-static void do_weight_paint_auto_normalize_all_groups(MDeformVert *dvert, char *map, char do_auto_normalize)
+static void do_weight_paint_auto_normalize_all_groups(MDeformVert *dvert, const char *vgroup_validmap, char do_auto_normalize)
 {
-//     MDeformWeight *dw = dvert->dw;
-       float sum=0.0f, fac=0.0f;
-       int i, tot=0;
-
-       if (do_auto_normalize == FALSE)
+       if (do_auto_normalize == FALSE) {
                return;
+       }
+       else {
+               float sum= 0.0f, fac;
+               unsigned int i, tot=0;
+               MDeformWeight *dw;
+
+               for (i= dvert->totweight, dw= dvert->dw; i != 0; i--, dw++) {
+                       if (vgroup_validmap[dw->def_nr]) {
+                               tot++;
+                               sum += dw->weight;
+                       }
+               }
 
-       for (i=0; i<dvert->totweight; i++) {
-               if (map[dvert->dw[i].def_nr]) {
-                       tot += 1;
-                       sum += dvert->dw[i].weight;
+               if ((tot == 0) || (sum == 1.0f) || (sum == 0.0f)) {
+                       return;
                }
-       }
-       
-       if (!tot || sum == 1.0f)
-               return;
 
-       fac = sum;
-       fac = fac==0.0f ? 1.0f : 1.0f / fac;
+               fac= 1.0f / sum;
 
-       for (i=0; i<dvert->totweight; i++) {
-               if (map[dvert->dw[i].def_nr]) {
-                       dvert->dw[i].weight *= fac;
+               for (i= dvert->totweight, dw= dvert->dw; i != 0; i--, dw++) {
+                       if (vgroup_validmap[dw->def_nr]) {
+                               dw->weight *= fac;
+                       }
                }
        }
 }
@@ -1828,8 +1830,7 @@ static char *wpaint_make_validmap(Object *ob)
                if (!(md->mode & (eModifierMode_Realtime|eModifierMode_Virtual)))
                        continue;
 
-               if (md->type == eModifierType_Armature) 
-               {
+               if (md->type == eModifierType_Armature) {
                        ArmatureModifierData *amd= (ArmatureModifierData*) md;
 
                        if(amd->object && amd->object->pose) {
@@ -2006,7 +2007,7 @@ static void wpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, P
        wpi.vgroup_validmap=    wpd->vgroup_validmap;
        wpi.do_flip=            RNA_boolean_get(itemptr, "pen_flip");
        wpi.do_multipaint=      (ts->multipaint != 0);
-       wpi.do_auto_normalize=  (ts->auto_normalize != 0);
+       wpi.do_auto_normalize=  ((ts->auto_normalize != 0) && (wpi.vgroup_validmap != NULL));
        /* *** done setting up WeightPaintInfo *** */