manually make small changes to sync with trunk
[blender.git] / source / blender / editors / sculpt_paint / paint_vertex.c
index 0d79fcc2c58919aaf4a20fa28d44adfb16901644..562a46586b96c314c5fde601674d8ca22903247f 100644 (file)
@@ -301,7 +301,7 @@ static void wpaint_mirror_vgroup_ensure(Object *ob, int *vgroup_mirror)
                flip_side_name(name, defgroup->name, FALSE);
 
                if(strcmp(name, defgroup->name) != 0) {
-                       for (curdef= ob->defbase.first, mirrdef; curdef; curdef=curdef->next, mirrdef++) {
+                       for (curdef= ob->defbase.first, mirrdef= 0; curdef; curdef=curdef->next, mirrdef++) {
                                if (!strcmp(curdef->name, name)) {
                                        break;
                                }
@@ -792,7 +792,7 @@ static float calc_vp_alpha_dl(VPaint *vp, ViewContext *vc, float vpimat[][3], fl
        return alpha;
 }
 
-static void wpaint_blend(VPaint *wp, MDeformWeight *dw, MDeformWeight *uw, float alpha, float paintval, int flip)
+static void wpaint_blend(VPaint *wp, MDeformWeight *dw, MDeformWeight *uw, float alpha, float paintval, int flip, int multipaint)
 {
        Brush *brush = paint_brush(&wp->paint);
        int tool = brush->vertexpaint_tool;
@@ -830,7 +830,8 @@ static void wpaint_blend(VPaint *wp, MDeformWeight *dw, MDeformWeight *uw, float
                if (dw->weight > paintval)
                        dw->weight = paintval*alpha + dw->weight*(1.0f-alpha);
        }
-       CLAMP(dw->weight, 0.0f, 1.0f);
+       // Jason delay clamping until the end so multi-paint can function when the active group is at the limits
+       //CLAMP(dw->weight, 0.0f, 1.0f);
        
        /* if no spray, clip result with orig weight & orig alpha */
        if((wp->flag & VP_SPRAY)==0) {
@@ -857,15 +858,16 @@ static void wpaint_blend(VPaint *wp, MDeformWeight *dw, MDeformWeight *uw, float
                        else
                                testw = uw->weight;
                }
-               CLAMP(testw, 0.0f, 1.0f);
-               
-               if( testw<uw->weight ) {
-                       if(dw->weight < testw) dw->weight= testw;
-                       else if(dw->weight > uw->weight) dw->weight= uw->weight;
-               }
-               else {
-                       if(dw->weight > testw) dw->weight= testw;
-                       else if(dw->weight < uw->weight) dw->weight= uw->weight;
+               //CLAMP(testw, 0.0f, 1.0f);
+               if(!multipaint) {
+                       if( testw<uw->weight ) {
+                               if(dw->weight < testw) dw->weight= testw;
+                               else if(dw->weight > uw->weight) dw->weight= uw->weight;
+                       }
+                       else {
+                               if(dw->weight > testw) dw->weight= testw;
+                               else if(dw->weight < uw->weight) dw->weight= uw->weight;
+                       }
                }
        }
        
@@ -1096,15 +1098,453 @@ static void do_weight_paint_auto_normalize(MDeformVert *dvert,
                }
        }
 }
+// Jason was here: the active group should be involved in auto normalize
+static void do_weight_paint_auto_normalize_all_groups(MDeformVert *dvert, char *map)
+{
+//     MDeformWeight *dw = dvert->dw;
+       float sum=0.0f, fac=0.0f;
+       int i, tot=0;
+
+       if (!map)
+               return;
+
+       for (i=0; i<dvert->totweight; i++) {
+               if (map[dvert->dw[i].def_nr]) {
+                       tot += 1;
+                       sum += dvert->dw[i].weight;
+               }
+       }
+       
+       if (!tot || sum == 1.0f)
+               return;
+
+       fac = sum;
+       fac = fac==0.0f ? 1.0f : 1.0f / fac;
+
+       for (i=0; i<dvert->totweight; i++) {
+               if (map[dvert->dw[i].def_nr]) {
+                       dvert->dw[i].weight *= fac;
+               }
+       }
+}
+/* Jason was here */
+/*
+See if the current deform vertex has a locked group
+*/
+static char has_locked_group(MDeformVert *dvert, char *flags)
+{
+       int i;
+       for(i = 0; i < dvert->totweight; i++) {
+               if(flags[(dvert->dw+i)->def_nr] && (dvert->dw+i)->weight > 0.0f) {
+                       return TRUE;
+               }
+       }
+       return FALSE;
+}
+/*Jason was here
+gen_lck_flags gets the status of "flag" for each bDeformGroup
+in ob->defbase and returns an array containing them
+*/
+static char* gen_lck_flags(Object* ob, int defcnt, char *map)
+{
+       char is_locked = FALSE;
+       int i;
+       //int defcnt = BLI_countlist(&ob->defbase);
+       char *flags = MEM_mallocN(defcnt*sizeof(char), "defflags");
+       bDeformGroup *defgroup;
+       int selected = 0;
+
+       for(i = 0, defgroup = ob->defbase.first; i < defcnt && defgroup; defgroup = defgroup->next, i++) {
+               flags[i] = defgroup->flag;
+               if(flags[i]) {
+                       is_locked = TRUE;
+               }
+       }
+       if(is_locked){
+               return flags;
+       }
+       // don't forget to free it if it is unneeded
+       MEM_freeN(flags);
+       return NULL;
+}
+/* Jason was here */
+static int has_locked_group_selected(int defcnt, char *selection, char *flags) {
+       int i;
+       for(i = 0; i < defcnt; i++) {
+               if(selection[i] && flags[i]) {
+                       return TRUE;
+               }
+       }
+       return FALSE;
+}
+
+/* Jason was here */
+static int has_unselected_unlocked_bone_group(int defcnt, char *selection, int selected, char *flags, char *bone_groups) {
+       int i;
+       if(defcnt == selected) {
+               return FALSE;
+       }
+       for(i = 0; i < defcnt; i++) {
+               if(bone_groups[i] && !selection[i] && !flags[i]) {
+                       return TRUE;
+               }
+       }
+       return FALSE;
+}
+
+/*Jason*/
+static void multipaint_selection(MDeformVert *dvert, float change, char *selection, int defcnt) {
+       int i;
+       MDeformWeight *dw;
+       float val;
+       // make sure they are all at most 1 after the change
+       for(i = 0; i < defcnt; i++) {
+               if(selection[i]) {
+                       dw = defvert_find_index(dvert, i);
+                       if(dw && dw->weight) {
+                               val = dw->weight * change;
+                               if(val > 1) {
+                                       // Jason TODO: when the change is reduced, you need to recheck the earlier values to make sure they are not 0 (precision error)
+                                       change = 1.0f/dw->weight;
+                               }
+                               // the value should never reach zero while multi-painting if it was nonzero beforehand
+                               if(val <= 0) {
+                                       return;
+                               }
+                       }
+               }
+       }
+       // apply the valid change
+       for(i = 0; i < defcnt; i++) {
+               if(selection[i]) {
+                       dw = defvert_find_index(dvert, i);
+                       if(dw && dw->weight) {
+                               dw->weight = dw->weight * change;
+                       }
+               }
+       }
+}
+/*Jason*/
+// move all change onto valid, unchanged groups.  If there is change left over, then return it.
+// assumes there are valid groups to shift weight onto
+static float redistribute_change(MDeformVert *ndv, char *change_status, int changeme, int changeto, char *validmap, float totchange, float total_valid) {
+       float was_change;
+       float change;
+       float oldval;
+       MDeformWeight *ndw;
+       int i;
+       do {
+               // assume there is no change until you see one
+               was_change = FALSE;
+               // change each group by the same amount each time
+               change = totchange/total_valid;
+               for(i = 0; i < ndv->totweight && total_valid && totchange; i++) {
+                       ndw = (ndv->dw+i);
+                       // change only the groups with a valid status
+                       if(change_status[ndw->def_nr] == changeme) {
+                               oldval = ndw->weight;
+                               // if auto normalize is active, don't worry about upper bounds
+                               if(!validmap && ndw->weight + change > 1) {
+                                       totchange -= 1-ndw->weight;
+                                       ndw->weight = 1;
+                                       // stop the changes to this group
+                                       change_status[ndw->def_nr] = changeto;
+                                       total_valid--;
+                               } else if(ndw->weight + change < 0) { // check the lower bound
+                                       totchange -= ndw->weight;
+                                       ndw->weight = 0;
+                                       change_status[ndw->def_nr] = changeto;
+                                       total_valid--;
+                               } else {// a perfectly valid change occurred to ndw->weight
+                                       totchange -= change;
+                                       ndw->weight += change;
+                               }
+                               // see if there was a change
+                               if(oldval != ndw->weight) {
+                                       was_change = TRUE;
+                               }
+                       }
+               }
+       // don't go again if there was no change, if there is no valid group, or there is no change left
+       }while(was_change && total_valid && totchange);
+       // left overs
+       return totchange;
+}
+/*Jason*/
+// observe the changes made to the weights of groups.
+// make sure all locked groups on the vertex have the same deformation
+// by moving the changes made to groups onto other unlocked groups
+static void enforce_locks(MDeformVert *odv, MDeformVert *ndv, int defcnt, char *flags, char *bone_groups, char *validmap) {
+       float totchange = 0.0f;
+       float totchange_allowed = 0.0f;
+       float left_over;
+       float change;
+       int total_valid = 0;
+       int total_changed = 0;
+       int i;
+       MDeformWeight *ndw;
+       MDeformWeight *odw;
+       MDeformWeight *ndw2;
+       MDeformWeight *odw2;
+       int designatedw = -1;
+       int designatedw_changed = FALSE;
+       float storedw;
+       char *change_status;
+       char new_weight_has_zero = FALSE;
+
+       if(!flags || !has_locked_group(ndv, flags)) {
+               return;
+       }
+       // record if a group was changed, unlocked and not changed, or locked
+       change_status = MEM_callocN(sizeof(char)*defcnt, "unlocked_unchanged");
+
+       for(i = 0; i < defcnt; i++) {
+               ndw = defvert_find_index(ndv, i);
+               odw = defvert_find_index(odv, i);
+               // the weights are zero, so we can assume a lot
+               if(!ndw || !odw) {
+                       if (!flags[i] && bone_groups[i]){
+                               defvert_verify_index(odv, i);
+                               defvert_verify_index(ndv, i);
+                               total_valid++;
+                               change_status[i] = 1; // can be altered while redistributing
+                       }
+                       continue;
+               }
+               // locked groups should not be changed
+               if(flags[i]) {
+                       ndw->weight = odw->weight;
+               } else if(ndw->weight != odw->weight) { // changed groups are handled here
+                       totchange += ndw->weight-odw->weight;
+                       change_status[i] = 2; // was altered already
+                       total_changed++;
+                       if(ndw->weight == 0) {
+                               new_weight_has_zero = TRUE;
+                       } else if(designatedw == -1){
+                               designatedw = i;
+                       }
+               } // unchanged, unlocked bone groups are handled here
+               else if (bone_groups[i]){
+                       totchange_allowed += ndw->weight;
+                       total_valid++;
+                       change_status[i] = 1; // can be altered while redistributing
+               }
+       }
+       // if there was any change, redistribute it
+       if(total_changed) {
+               // auto normalize will allow weights to temporarily go above 1 in redistribution
+               if(validmap && total_changed < 0 && total_valid) {
+                       totchange_allowed = total_valid;
+               }
+               // there needs to be change allowed, or you should not bother
+               if(totchange_allowed) {
+                       // the way you modify the unlocked+unchanged groups is different depending
+                       // on whether or not you are painting the weight(s) up or down
+                       if(totchange < 0) {
+                               totchange_allowed = total_valid - totchange_allowed;
+                       } else {
+                               totchange_allowed *= -1;
+                       }
+                       left_over = 0;
+                       if(fabs(totchange_allowed) < fabs(totchange)) {
+                               // this amount goes back onto the changed, unlocked weights
+                               left_over = fabs(fabs(totchange)-fabs(totchange_allowed));
+                               if(totchange > 0) {
+                                       left_over *= -1;
+                               }
+                       }else {
+                               // all of the change will be permitted
+                               totchange_allowed = -totchange;
+                       }
+                       // move the weight evenly between the allowed groups, move excess back onto the used groups based on the change
+                       totchange_allowed = redistribute_change(ndv, change_status, 1, -1, validmap, totchange_allowed, total_valid);
+                       left_over += totchange_allowed;
+                       if(left_over) {
+                               // more than one nonzero weights were changed with the same ratio, so keep them changed that way!
+                               if(total_changed > 1 && !new_weight_has_zero && designatedw >= 0) {
+                                       // this dw is special, it is used as a base to determine how to change the others
+                                       ndw = defvert_find_index(ndv, designatedw);
+                                       odw = defvert_find_index(odv, designatedw);
+                                       storedw = ndw->weight;
+                                       for(i = 0; i < ndv->totweight; i++) {
+                                               if(change_status[ndw->def_nr] == 2) {
+                                                       odw2 = (odv->dw+i);
+                                                       ndw2 = (ndv->dw+i);
+                                                       if(!designatedw_changed) {
+                                                               ndw->weight = (totchange_allowed + odw->weight + odw2->weight)/(1 + ndw2->weight/ndw->weight);
+                                                               designatedw_changed = TRUE;
+                                                       }
+                                                       ndw2->weight = ndw->weight*ndw2->weight/storedw;
+                                               }
+                                       }
+                               }
+                               // a weight was changed to zero, only one weight was changed, or designatedw is still -1
+                               // put weight back as evenly as possible
+                               else {
+                                       redistribute_change(ndv, change_status, 2, -2, validmap, left_over, total_changed);
+                               }
+                       }
+               } else {
+                       // reset the weights
+                       for(i = 0; i < ndv->totweight; i++) {
+                               (ndv->dw+i)->weight = (odv->dw+i)->weight;
+                       }
+               }
+       }
+
+       MEM_freeN(change_status);
+}
+/*Jason*/
+// multi-paint's initial, potential change is computed here based on the user's stroke
+static float get_mp_change(MDeformVert *odv, char *selection, float brush_change) {
+       float selwsum = 0.0f;
+       int i;
+       MDeformWeight *dw;
+       for(i=0; i < odv->totweight; i++) {
+               if(selection[(dw = (odv->dw+i))->def_nr]) {
+                       selwsum += dw->weight;
+               }
+       }
+       if(selwsum && selwsum+brush_change > 0) {
+               return (selwsum+brush_change)/selwsum;
+       }
+       return 0.0f;
+}
+/*Jason*/
+// change the weights back to the wv's weights
+// it assumes you already have the correct pointer index
+static void reset_to_prev(MDeformVert *wv, MDeformVert *dv) {
+       int i;
+       MDeformWeight *d;
+       MDeformWeight *w;
+       for(i = 0; i < dv->totweight; i++) {
+               d = dv->dw+i;
+               w = defvert_find_index(wv, d->def_nr);
+               // if there was no w when there is a d, then the old weight was 0
+               if(w) {
+                       d->weight = w->weight;
+               } else {
+                       d->weight = 0;
+               }
+       }
+}
+/* Jason */
+static void clamp_weights(MDeformVert *dvert) {
+       int i;
+       for (i = 0; i < dvert->totweight; i++) {
+               CLAMP((dvert->dw+i)->weight, 0.0f, 1.0f);
+       }
+}
+/*Jason*/
+/* fresh start to make multi-paint and locking modular */
+static void apply_mp_lcks_normalize(Object *ob, Mesh *me, int index, MDeformWeight *dw, int defcnt, float change, float oldw, float neww, char *selection, int selected, char *bone_groups, char *validmap, char *flags, int multipaint) {
+       MDeformVert *dvert = me->dvert+index;
+       MDeformVert *dv = MEM_mallocN(sizeof (*(me->dvert+index)), "oldMDeformVert");
+
+       int i;
+
+       dv->dw= MEM_dupallocN(dvert->dw);
+       dv->flag = dvert->flag;
+       dv->totweight = dvert->totweight;
+       // do not multi-paint if a locked group is selected or the active group is locked
+       // !flags[dw->def_nr] helps if nothing is selected, but active group is locked
+       if(!flags || flags && !has_locked_group_selected(defcnt, selection, flags) && !flags[dw->def_nr]) {
+               if(multipaint && selected > 1) {
+                       if(change && change!=1) {
+                               multipaint_selection(dvert, change, selection, defcnt);
+                       }
+               } else {// this lets users paint normally, but don't let them paint locked groups
+                       dw->weight = neww;
+               }
+       }
+       clamp_weights(me->dvert+index);
+
+       enforce_locks(dv, dvert, defcnt, flags, bone_groups, validmap);
+
+       do_weight_paint_auto_normalize_all_groups(dvert, validmap);
+       
+       MEM_freeN(dv->dw);
+       MEM_freeN(dv);
+}
+
+/* Jason was here duplicate function I used in DerivedMesh.c*/
+static char* get_selected_defgroups(Object *ob, int defcnt) {
+       bPoseChannel *chan;
+       bPose *pose;
+       bDeformGroup *defgroup;
+       //Bone *bone;
+       char *dg_flags = MEM_callocN(defcnt*sizeof(char), "dg_selected_flags");
+       int i;
+       Object *armob = ED_object_pose_armature(ob);
+
+       if(armob) {
+               pose = armob->pose;
+               for (chan=pose->chanbase.first; chan; chan=chan->next) {
+                       for (i = 0, defgroup = ob->defbase.first; i < defcnt && defgroup; defgroup = defgroup->next, i++) {
+                               if(!strcmp(defgroup->name, chan->bone->name)) {
+                                       dg_flags[i] = (chan->bone->flag & BONE_SELECTED);
+                               }
+                       }
+               }
+       }
+       // the array has whether or not the corresponding group is selected
+       return dg_flags;
+}
+/* TODO move duplicates to header */
+/* Jason was here duplicate function */
+static int count_true(char *list, int len)
+{
+       int i;
+       int cnt = 0;
+       for(i = 0; i < len; i++) {
+               if (list[i]) {
+                       cnt++;
+               }
+       }
+       return cnt;
+}
+// within the current dvert index, get the dw that is selected and has a weight above 0
+// this helps multi-paint
+static int get_first_selected_nonzero_weight(MDeformVert *dvert, char *selection) {
+       int i;
+       MDeformWeight *dw;
+       for(i=0; i< dvert->totweight; i++) {
+               dw = dvert->dw+i;
+               if(selection[dw->def_nr] && dw->weight > 0) {
+                       return i;
+               }
+       }
+       return -1;
+}
+// Jason
+static char *wpaint_make_validmap(Object *ob);
 
 static void do_weight_paint_vertex(VPaint *wp, Object *ob, int index, 
                                   float alpha, float paintweight, int flip, 
-                                  int vgroup_mirror, char *validmap)
+                                  int vgroup_mirror, char *validmap, int multipaint)
 {
        Mesh *me= ob->data;
-       MDeformWeight *dw, *uw;
+       MDeformWeight *dw, *uw, *tdw, *tuw;
        int vgroup= ob->actdef-1;
-       
+
+       /* Jason was here */
+       char *flags;
+       char *bone_groups;
+       char *selection;
+       int selected;
+       float oldw;
+       float neww;
+       float testw;
+       int defcnt;
+       float change = 0;
+       int i;
+       // Need to know which groups are bone groups
+       if(validmap) {
+               bone_groups = validmap;
+       }else {
+               bone_groups = wpaint_make_validmap(ob);
+       }
+
        if(wp->flag & VP_ONLYVGROUP) {
                dw= defvert_find_index(me->dvert+index, vgroup);
                uw= defvert_find_index(wp->wpaint_prev+index, vgroup);
@@ -1115,9 +1555,60 @@ static void do_weight_paint_vertex(VPaint *wp, Object *ob, int index,
        }
        if(dw==NULL || uw==NULL)
                return;
+       /* Jason was here */
+       flags = gen_lck_flags(ob, defcnt = BLI_countlist(&ob->defbase), bone_groups);
+       selection = get_selected_defgroups(ob, defcnt);
+       selected = count_true(selection, defcnt);
+       if(!selected && ob->actdef) {
+               selected = 1;
+       }
        
-       wpaint_blend(wp, dw, uw, alpha, paintweight, flip);
-       do_weight_paint_auto_normalize(me->dvert+index, vgroup, validmap);
+       oldw = dw->weight;
+       wpaint_blend(wp, dw, uw, alpha, paintweight, flip, multipaint && selected >1);
+       neww = dw->weight;
+       dw->weight = oldw;
+       // setup multi-paint
+       if(selected > 1 && multipaint) {
+               tdw = dw;
+               tuw = uw;
+               change = get_mp_change(wp->wpaint_prev+index, selection, neww-oldw);
+               if(change) {
+                       if(!tdw->weight) {
+                               i = get_first_selected_nonzero_weight(me->dvert+index, selection);
+                               if(i>=0) {
+                                       tdw = ((me->dvert+index)->dw+i);
+                                       tuw = defvert_verify_index(wp->wpaint_prev+index, tdw->def_nr);
+                               } else {
+                                       change = 0;
+                               }
+                       }
+                       if(change && tdw->weight * change && tuw->weight) {
+                               if(tdw->weight != tuw->weight) {
+                                       testw = tuw->weight*change;
+                                       if( testw > tuw->weight ) {
+                                               if(change > tdw->weight/tuw->weight) {
+                                                       reset_to_prev(wp->wpaint_prev+index, me->dvert+index);
+                                               } else {
+                                                       change = 0;
+                                               }
+                                       } else {
+                                               if(change < tdw->weight/tuw->weight) {
+                                                       reset_to_prev(wp->wpaint_prev+index, me->dvert+index);
+                                               } else {
+                                                       change = 0;
+                                               }
+                                       }
+                               }
+                       } else {
+                               change = 0;
+                       }
+               }
+       }
+       /* Jason was here */
+       apply_mp_lcks_normalize(ob, me, index, dw, defcnt, change, oldw, neww, selection, selected, bone_groups, validmap, flags, multipaint);
+
+       // dvert may have been altered greatly
+       dw = defvert_find_index(me->dvert+index, vgroup);
 
        if(me->editflag & ME_EDIT_MIRROR_X) {   /* x mirror painting */
                int j= mesh_get_x_mirror_vert(ob, index);
@@ -1127,15 +1618,22 @@ static void do_weight_paint_vertex(VPaint *wp, Object *ob, int index,
                                uw= defvert_verify_index(me->dvert+j, vgroup_mirror);
                        else
                                uw= defvert_verify_index(me->dvert+j, vgroup);
-                               
-                       uw->weight= dw->weight;
-
-                       do_weight_paint_auto_normalize(me->dvert+j, vgroup, validmap);
+                       /* Jason */
+                       //uw->weight= dw->weight;
+                       /* Jason */
+                       apply_mp_lcks_normalize(ob, me, j, uw, defcnt, change, oldw, neww, selection, selected, bone_groups, validmap, flags, multipaint);
                }
        }
+       /* Jason */
+       if(flags) {
+               MEM_freeN(flags);
+       }
+       MEM_freeN(selection);
+       if(!validmap) {
+               MEM_freeN(bone_groups);
+       }
 }
 
-
 /* *************** set wpaint operator ****************** */
 
 static int set_wpaint(bContext *C, wmOperator *UNUSED(op))             /* toggle */
@@ -1297,7 +1795,7 @@ static int wpaint_stroke_test_start(bContext *C, wmOperator *op, wmEvent *UNUSED
        struct WPaintData *wpd;
        Mesh *me;
        float mat[4][4], imat[4][4];
-       
+
        if(scene->obedit) return OPERATOR_CANCELLED;
        
        me= get_mesh(ob);
@@ -1308,13 +1806,14 @@ static int wpaint_stroke_test_start(bContext *C, wmOperator *op, wmEvent *UNUSED
                ED_vgroup_data_create(&me->id);
                WM_event_add_notifier(C, NC_GEOM|ND_DATA, me);
        }
-       
+
+
        /* make mode data storage */
        wpd= MEM_callocN(sizeof(struct WPaintData), "WPaintData");
        paint_stroke_set_mode_data(stroke, wpd);
        view3d_set_viewcontext(C, &wpd->vc);
        wpd->vgroup_mirror= -1;
-       
+
        /*set up auto-normalize, and generate map for detecting which
          vgroups affect deform bones*/
        wpd->auto_normalize = ts->auto_normalize;
@@ -1369,7 +1868,7 @@ static int wpaint_stroke_test_start(bContext *C, wmOperator *op, wmEvent *UNUSED
        if(me->editflag & ME_EDIT_MIRROR_X) {
                wpaint_mirror_vgroup_ensure(ob, &wpd->vgroup_mirror);
        }
-       
+
        return 1;
 }
 
@@ -1501,7 +2000,7 @@ static void wpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, P
                                if(alpha) {
                                        do_weight_paint_vertex(wp, ob, mface->v1, 
                                                alpha, paintweight, flip, wpd->vgroup_mirror, 
-                                               wpd->vgroup_validmap);
+                                               wpd->vgroup_validmap, ts->multipaint);
                                }
                                (me->dvert+mface->v1)->flag= 0;
                        }
@@ -1511,7 +2010,7 @@ static void wpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, P
                                if(alpha) {
                                        do_weight_paint_vertex(wp, ob, mface->v2, 
                                                alpha, paintweight, flip, wpd->vgroup_mirror, 
-                                               wpd->vgroup_validmap);
+                                               wpd->vgroup_validmap, ts->multipaint);
                                }
                                (me->dvert+mface->v2)->flag= 0;
                        }
@@ -1521,7 +2020,7 @@ static void wpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, P
                                if(alpha) {
                                        do_weight_paint_vertex(wp, ob, mface->v3, 
                                                alpha, paintweight, flip, wpd->vgroup_mirror, 
-                                               wpd->vgroup_validmap);
+                                               wpd->vgroup_validmap, ts->multipaint);
                                }
                                (me->dvert+mface->v3)->flag= 0;
                        }
@@ -1532,7 +2031,7 @@ static void wpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, P
                                        if(alpha) {
                                                do_weight_paint_vertex(wp, ob, mface->v4, 
                                                        alpha, paintweight, flip, wpd->vgroup_mirror,
-                                                       wpd->vgroup_validmap);
+                                                       wpd->vgroup_validmap, ts->multipaint);
                                        }
                                        (me->dvert+mface->v4)->flag= 0;
                                }
@@ -1600,6 +2099,13 @@ static int wpaint_invoke(bContext *C, wmOperator *op, wmEvent *event)
        return OPERATOR_RUNNING_MODAL;
 }
 
+static int wpaint_cancel(bContext *C, wmOperator *op)
+{
+       paint_stroke_cancel(C, op);
+
+       return OPERATOR_CANCELLED;
+}
+
 void PAINT_OT_weight_paint(wmOperatorType *ot)
 {
        
@@ -1612,6 +2118,7 @@ void PAINT_OT_weight_paint(wmOperatorType *ot)
        ot->modal= paint_stroke_modal;
        /* ot->exec= vpaint_exec; <-- needs stroke property */
        ot->poll= weight_paint_poll;
+       ot->cancel= wpaint_cancel;
        
        /* flags */
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
@@ -1892,6 +2399,13 @@ static int vpaint_invoke(bContext *C, wmOperator *op, wmEvent *event)
        return OPERATOR_RUNNING_MODAL;
 }
 
+static int vpaint_cancel(bContext *C, wmOperator *op)
+{
+       paint_stroke_cancel(C, op);
+
+       return OPERATOR_CANCELLED;
+}
+
 void PAINT_OT_vertex_paint(wmOperatorType *ot)
 {
        /* identifiers */
@@ -1903,6 +2417,7 @@ void PAINT_OT_vertex_paint(wmOperatorType *ot)
        ot->modal= paint_stroke_modal;
        /* ot->exec= vpaint_exec; <-- needs stroke property */
        ot->poll= vertex_paint_poll;
+       ot->cancel= vpaint_cancel;
        
        /* flags */
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;