manually make small changes to sync with trunk
[blender.git] / source / blender / editors / sculpt_paint / paint_vertex.c
index 560ee550aed251e4f0ce1eb6fa8b0511a0a3403e..562a46586b96c314c5fde601674d8ca22903247f 100644 (file)
@@ -61,6 +61,7 @@
 
 #include "RNA_access.h"
 #include "RNA_define.h"
+#include "RNA_enum_types.h"
 
 #include "BKE_DerivedMesh.h"
 #include "BKE_action.h"
@@ -72,7 +73,7 @@
 #include "BKE_modifier.h"
 #include "BKE_object.h"
 #include "BKE_paint.h"
-
+#include "BKE_report.h"
 
 #include "WM_api.h"
 #include "WM_types.h"
@@ -287,6 +288,43 @@ static void make_vertexcol(Object *ob)     /* single ob */
        
 }
 
+/* mirror_vgroup is set to -1 when invalid */
+static void wpaint_mirror_vgroup_ensure(Object *ob, int *vgroup_mirror)
+{
+       bDeformGroup *defgroup= BLI_findlink(&ob->defbase, ob->actdef - 1);
+
+       if(defgroup) {
+               bDeformGroup *curdef;
+               int mirrdef;
+               char name[MAXBONENAME];
+
+               flip_side_name(name, defgroup->name, FALSE);
+
+               if(strcmp(name, defgroup->name) != 0) {
+                       for (curdef= ob->defbase.first, mirrdef= 0; curdef; curdef=curdef->next, mirrdef++) {
+                               if (!strcmp(curdef->name, name)) {
+                                       break;
+                               }
+                       }
+
+                       if(curdef==NULL) {
+                               int olddef= ob->actdef; /* tsk, ED_vgroup_add sets the active defgroup */
+                               curdef= ED_vgroup_add_name(ob, name);
+                               ob->actdef= olddef;
+                       }
+
+                       /* curdef should never be NULL unless this is
+                        * a  lamp and ED_vgroup_add_name fails */
+                       if(curdef) {
+                               *vgroup_mirror= mirrdef;
+                               return;
+                       }
+               }
+       }
+
+       *vgroup_mirror= -1;
+}
+
 static void copy_vpaint_prev(VPaint *vp, unsigned int *mcol, int tot)
 {
        if(vp->vpaint_prev) {
@@ -347,6 +385,7 @@ void vpaint_fill(Object *ob, unsigned int paintcol)
        DAG_id_tag_update(&me->id, 0);
 }
 
+
 /* fills in the selected faces with the current weight and vertex group */
 void wpaint_fill(VPaint *wp, Object *ob, float paintweight)
 {
@@ -362,7 +401,7 @@ void wpaint_fill(VPaint *wp, Object *ob, float paintweight)
        
        me= ob->data;
        if(me==NULL || me->totface==0 || me->dvert==NULL || !me->mface) return;
-
+       
        selected= (me->editflag & ME_EDIT_PAINT_MASK);
 
        indexar= get_indexarray(me);
@@ -381,32 +420,11 @@ void wpaint_fill(VPaint *wp, Object *ob, float paintweight)
        }
        
        vgroup= ob->actdef-1;
-       
-       /* directly copied from weight_paint, should probaby split into a separate function */
+
        /* if mirror painting, find the other group */          
        if(me->editflag & ME_EDIT_MIRROR_X) {
-               bDeformGroup *defgroup= BLI_findlink(&ob->defbase, ob->actdef-1);
-               if(defgroup) {
-                       bDeformGroup *curdef;
-                       int actdef= 0;
-                       char name[32];
-
-                       flip_side_name(name, defgroup->name, FALSE);
-
-                       for (curdef = ob->defbase.first; curdef; curdef=curdef->next, actdef++)
-                               if (!strcmp(curdef->name, name))
-                                       break;
-                       if(curdef==NULL) {
-                               int olddef= ob->actdef; /* tsk, ED_vgroup_add sets the active defgroup */
-                               curdef= ED_vgroup_add_name (ob, name);
-                               ob->actdef= olddef;
-                       }
-                       
-                       if(curdef && curdef!=defgroup)
-                               vgroup_mirror= actdef;
-               }
+               wpaint_mirror_vgroup_ensure(ob, &vgroup_mirror);
        }
-       /* end copy from weight_paint*/
        
        copy_wpaint_prev(wp, me->dvert, me->totvert);
        
@@ -774,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;
@@ -812,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) {
@@ -839,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;
+                       }
                }
        }
        
@@ -855,145 +875,195 @@ static void wpaint_blend(VPaint *wp, MDeformWeight *dw, MDeformWeight *uw, float
 
 /* ----------------------------------------------------- */
 
-/* used for 3d view, on active object, assumes me->dvert exists */
-/* if mode==1: */
-/*     samples cursor location, and gives menu with vertex groups to activate */
-/* else */
-/*     sets wp->weight to the closest weight value to vertex */
-/*     note: we cant sample frontbuf, weight colors are interpolated too unpredictable */
-static void sample_wpaint(Scene *scene, ARegion *ar, View3D *UNUSED(v3d), int mode)
+
+/* sets wp->weight to the closest weight value to vertex */
+/* note: we cant sample frontbuf, weight colors are interpolated too unpredictable */
+static int weight_sample_invoke(bContext *C, wmOperator *op, wmEvent *event)
 {
        ViewContext vc;
-       ToolSettings *ts= scene->toolsettings;
-       Object *ob= OBACT;
-       Mesh *me= get_mesh(ob);
-       int index;
-       int mval[2] = {0, 0}, sco[2];
-       int vgroup= ob->actdef-1;
+       Mesh *me;
+       short change= FALSE;
+
+       view3d_set_viewcontext(C, &vc);
+       me= get_mesh(vc.obact);
+
+       if (me && me->dvert && vc.v3d && vc.rv3d) {
+               int index;
+
+               view3d_operator_needs_opengl(C);
+
+               index= view3d_sample_backbuf(&vc, event->mval[0], event->mval[1]);
+
+               if(index && index<=me->totface) {
+                       DerivedMesh *dm= mesh_get_derived_final(vc.scene, vc.obact, CD_MASK_BAREMESH);
 
-       if (!me) return;
-       
-//     getmouseco_areawin(mval);
-       index= view3d_sample_backbuf(&vc, mval[0], mval[1]);
-       
-       if(index && index<=me->totface) {
-               MFace *mface;
-               
-               mface= ((MFace *)me->mface) + index-1;
-               
-               if(mode==1) {   /* sampe which groups are in here */
-                       MDeformVert *dv;
-                       int a, totgroup;
-                       
-                       totgroup= BLI_countlist(&ob->defbase);
-                       if(totgroup) {
-                               int totmenu=0;
-                               int *groups=MEM_callocN(totgroup*sizeof(int), "groups");
-                               
-                               dv= me->dvert+mface->v1;
-                               for(a=0; a<dv->totweight; a++) {
-                                       if (dv->dw[a].def_nr<totgroup)
-                                               groups[dv->dw[a].def_nr]= 1;
-                               }
-                               dv= me->dvert+mface->v2;
-                               for(a=0; a<dv->totweight; a++) {
-                                       if (dv->dw[a].def_nr<totgroup)
-                                               groups[dv->dw[a].def_nr]= 1;
-                               }
-                               dv= me->dvert+mface->v3;
-                               for(a=0; a<dv->totweight; a++) {
-                                       if (dv->dw[a].def_nr<totgroup)
-                                               groups[dv->dw[a].def_nr]= 1;
-                               }
-                               if(mface->v4) {
-                                       dv= me->dvert+mface->v4;
-                                       for(a=0; a<dv->totweight; a++) {
-                                               if (dv->dw[a].def_nr<totgroup)
-                                                       groups[dv->dw[a].def_nr]= 1;
-                                       }
-                               }
-                               for(a=0; a<totgroup; a++)
-                                       if(groups[a]) totmenu++;
-                               
-                               if(totmenu==0) {
-                                       //notice("No Vertex Group Selected");
-                               }
-                               else {
-                                       bDeformGroup *dg;
-                                       short val;
-                                       char item[40], *str= MEM_mallocN(40*totmenu+40, "menu");
-                                       
-                                       strcpy(str, "Vertex Groups %t");
-                                       for(a=0, dg=ob->defbase.first; dg && a<totgroup; a++, dg= dg->next) {
-                                               if(groups[a]) {
-                                                       sprintf(item, "|%s %%x%d", dg->name, a);
-                                                       strcat(str, item);
-                                               }
-                                       }
-                                       
-                                       val= 0; // XXX pupmenu(str);
-                                       if(val>=0) {
-                                               ob->actdef= val+1;
-                                               DAG_id_tag_update(&me->id, 0);
-                                       }
-                                       MEM_freeN(str);
-                               }
-                               MEM_freeN(groups);
-                       }
-//                     else notice("No Vertex Groups in Object");
-               }
-               else {
-                       DerivedMesh *dm;
-                       float w1, w2, w3, w4, co[3], fac;
-                       
-                       dm = mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH);
                        if(dm->getVertCo==NULL) {
-                               //notice("Not supported yet");
+                               BKE_report(op->reports, RPT_WARNING, "The modifier used does not support deformed locations");
                        }
                        else {
-                               /* calc 3 or 4 corner weights */
-                               dm->getVertCo(dm, mface->v1, co);
-                               project_int_noclip(ar, co, sco);
-                               w1= ((mval[0]-sco[0])*(mval[0]-sco[0]) + (mval[1]-sco[1])*(mval[1]-sco[1]));
-                               
-                               dm->getVertCo(dm, mface->v2, co);
-                               project_int_noclip(ar, co, sco);
-                               w2= ((mval[0]-sco[0])*(mval[0]-sco[0]) + (mval[1]-sco[1])*(mval[1]-sco[1]));
-                               
-                               dm->getVertCo(dm, mface->v3, co);
-                               project_int_noclip(ar, co, sco);
-                               w3= ((mval[0]-sco[0])*(mval[0]-sco[0]) + (mval[1]-sco[1])*(mval[1]-sco[1]));
-                               
-                               if(mface->v4) {
-                                       dm->getVertCo(dm, mface->v4, co);
-                                       project_int_noclip(ar, co, sco);
-                                       w4= ((mval[0]-sco[0])*(mval[0]-sco[0]) + (mval[1]-sco[1])*(mval[1]-sco[1]));
-                               }
-                               else w4= 1.0e10;
-                               
-                               fac= MIN4(w1, w2, w3, w4);
-                               if(w1==fac) {
-                                       ts->vgroup_weight= defvert_find_weight(me->dvert+mface->v1, vgroup);
-                               }
-                               else if(w2==fac) {
-                                       ts->vgroup_weight= defvert_find_weight(me->dvert+mface->v2, vgroup);
-                               }
-                               else if(w3==fac) {
-                                       ts->vgroup_weight= defvert_find_weight(me->dvert+mface->v3, vgroup);
-                               }
-                               else if(w4==fac) {
-                                       if(mface->v4) {
-                                               ts->vgroup_weight= defvert_find_weight(me->dvert+mface->v4, vgroup);
+                               MFace *mf= ((MFace *)me->mface) + index-1;
+                               const int vgroup= vc.obact->actdef - 1;
+                               ToolSettings *ts= vc.scene->toolsettings;
+                               float mval_f[2];
+                               int v_idx_best= -1;
+                               int fidx;
+                               float len_best= FLT_MAX;
+
+                               mval_f[0]= (float)event->mval[0];
+                               mval_f[1]= (float)event->mval[1];
+
+                               fidx= mf->v4 ? 3:2;
+                               do {
+                                       float co[3], sco[3], len;
+                                       const int v_idx= (*(&mf->v1 + fidx));
+                                       dm->getVertCo(dm, v_idx, co);
+                                       project_float_noclip(vc.ar, co, sco);
+                                       len= len_squared_v2v2(mval_f, sco);
+                                       if(len < len_best) {
+                                               len_best= len;
+                                               v_idx_best= v_idx;
                                        }
+                               } while (fidx--);
+
+                               if(v_idx_best != -1) { /* should always be valid */
+                                       ts->vgroup_weight= defvert_find_weight(&me->dvert[v_idx_best], vgroup);
+                                       change= TRUE;
                                }
                        }
                        dm->release(dm);
-               }               
-               
+               }
+       }
+
+       if(change) {
+               /* not really correct since the brush didnt change, but redraws the toolbar */
+               WM_main_add_notifier(NC_BRUSH|NA_EDITED, NULL); /* ts->wpaint->paint.brush */
+
+               return OPERATOR_FINISHED;
+       }
+       else {
+               return OPERATOR_CANCELLED;
        }
-       
 }
 
+void PAINT_OT_weight_sample(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Weight Paint Sample Weight";
+       ot->idname= "PAINT_OT_weight_sample";
+
+       /* api callbacks */
+       ot->invoke= weight_sample_invoke;
+       ot->poll= weight_paint_mode_poll;
+
+       /* flags */
+       ot->flag= OPTYPE_UNDO;
+}
+
+/* samples cursor location, and gives menu with vertex groups to activate */
+static EnumPropertyItem *weight_paint_sample_enum_itemf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), int *free)
+{
+       if (C) {
+               wmWindow *win= CTX_wm_window(C);
+               if(win && win->eventstate) {
+                       ViewContext vc;
+                       Mesh *me;
+
+                       view3d_set_viewcontext(C, &vc);
+                       me= get_mesh(vc.obact);
+
+                       if (me && me->dvert && vc.v3d && vc.rv3d) {
+                               int index;
+
+                               view3d_operator_needs_opengl(C);
+
+                               index= view3d_sample_backbuf(&vc, win->eventstate->x - vc.ar->winrct.xmin, win->eventstate->y - vc.ar->winrct.ymin);
+
+                               if(index && index<=me->totface) {
+                                       const int totgroup= BLI_countlist(&vc.obact->defbase);
+                                       if(totgroup) {
+                                               MFace *mf= ((MFace *)me->mface) + index-1;
+                                               int fidx= mf->v4 ? 3:2;
+                                               int *groups= MEM_callocN(totgroup*sizeof(int), "groups");
+                                               int found= FALSE;
+
+                                               do {
+                                                       MDeformVert *dvert= me->dvert + (*(&mf->v1 + fidx));
+                                                       int i= dvert->totweight;
+                                                       MDeformWeight *dw;
+                                                       for(dw= dvert->dw; i > 0; dw++, i--) {
+                                                               groups[dw->def_nr]= TRUE;
+                                                               found= TRUE;
+                                                       }
+                                               } while (fidx--);
+
+                                               if(found==FALSE) {
+                                                       MEM_freeN(groups);
+                                               }
+                                               else {
+                                                       EnumPropertyItem *item= NULL, item_tmp= {0};
+                                                       int totitem= 0;
+                                                       int i= 0;
+                                                       bDeformGroup *dg;
+                                                       for(dg= vc.obact->defbase.first; dg && i<totgroup; i++, dg= dg->next) {
+                                                               if(groups[i]) {
+                                                                       item_tmp.identifier= item_tmp.name= dg->name;
+                                                                       item_tmp.value= i;
+                                                                       RNA_enum_item_add(&item, &totitem, &item_tmp);
+                                                               }
+                                                       }
+
+                                                       RNA_enum_item_end(&item, &totitem);
+                                                       *free= 1;
+
+                                                       MEM_freeN(groups);
+                                                       return item;
+                                               }
+                                       }
+                               }
+                       }
+               }
+       }
+
+       return DummyRNA_NULL_items;
+}
+
+static int weight_sample_group_exec(bContext *C, wmOperator *op)
+{
+       int type= RNA_enum_get(op->ptr, "group");
+       ViewContext vc;
+       view3d_set_viewcontext(C, &vc);
+
+       vc.obact->actdef= type + 1;
+
+       DAG_id_tag_update(&vc.obact->id, OB_RECALC_DATA);
+       WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, vc.obact);
+       return OPERATOR_FINISHED;
+}
+
+/* TODO, we could make this a menu into OBJECT_OT_vertex_group_set_active rather than its own operator */
+void PAINT_OT_weight_sample_group(wmOperatorType *ot)
+{
+       PropertyRNA *prop= NULL;
+
+       /* identifiers */
+       ot->name= "Weight Paint Sample Group";
+       ot->idname= "PAINT_OT_weight_sample_group";
+
+       /* api callbacks */
+       ot->exec= weight_sample_group_exec;
+       ot->invoke= WM_menu_invoke;
+       ot->poll= weight_paint_mode_poll;
+
+       /* flags */
+       ot->flag= OPTYPE_UNDO;
+
+       /* keyingset to use (dynamic enum) */
+       prop= RNA_def_enum(ot->srna, "group", DummyRNA_DEFAULT_items, 0, "Keying Set", "The Keying Set to use");
+       RNA_def_enum_funcs(prop, weight_paint_sample_enum_itemf);
+       ot->prop= prop;
+}
+
+
 static void do_weight_paint_auto_normalize(MDeformVert *dvert, 
                                           int paint_nr, char *map)
 {
@@ -1029,7 +1099,7 @@ 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_change_act_group(MDeformVert *dvert, char *map)
+static void do_weight_paint_auto_normalize_all_groups(MDeformVert *dvert, char *map)
 {
 //     MDeformWeight *dw = dvert->dw;
        float sum=0.0f, fac=0.0f;
@@ -1107,22 +1177,7 @@ static int has_locked_group_selected(int defcnt, char *selection, char *flags) {
        }
        return FALSE;
 }
-/* Jason was here */
-static float get_change_allowed_from_unlocked_bone_groups(MDeformVert *dvert, int def_nr, int defcnt, char *selection, int pos, char *flags, char *bone_groups) {
-       int i;
-       float allowed_totchange = 0.0f;
-       for(i = 0; i < defcnt; i++) {
-               if(def_nr != i && bone_groups[i] && !selection[i] && !flags[i]) {
-                       // positive change
-                       if(pos == 1) {
-                               allowed_totchange -= 1-defvert_verify_index(dvert, i)->weight;
-                       } else {//negative change
-                               allowed_totchange -= defvert_verify_index(dvert, i)->weight;
-                       }
-               }
-       }
-       return allowed_totchange;
-}
+
 /* Jason was here */
 static int has_unselected_unlocked_bone_group(int defcnt, char *selection, int selected, char *flags, char *bone_groups) {
        int i;
@@ -1136,216 +1191,282 @@ static int has_unselected_unlocked_bone_group(int defcnt, char *selection, int s
        }
        return FALSE;
 }
-/*
-The idea behind this function is to get the difference in weight,
-and to redistribute that weight to the unlocked groups
-(if it has to, then it will put some/all of the change
-back onto the original group)
-*/
-static void redistribute_weight_change(Object *ob, MDeformVert *dvert, int index, int def_nr, int multipaint, char *selection, int selected, float change_left, char* flags, int defcnt, char *map)
-{
+
+/*Jason*/
+static void multipaint_selection(MDeformVert *dvert, float change, char *selection, int defcnt) {
        int i;
-       float old_change_left;
-       // make sure the redistribution the same per loop.
-       float change;
-       char was_a_change;
-       int groups_left_that_can_change = 0;
-       char* change_status = MEM_mallocN(defcnt*sizeof(char), "defflags");
        MDeformWeight *dw;
+       float val;
+       // make sure they are all at most 1 after the change
        for(i = 0; i < defcnt; i++) {
-               if(def_nr == i || (multipaint && selection[i]) || !map[i]) {
-                       change_status[i] = FALSE;
-               } else {
-                       change_status[i] = !flags[i];
-               }
-               if(change_status[i]) {
-                       groups_left_that_can_change++;
-                       defvert_verify_index(dvert, i);
-               }
-       }
-       if(groups_left_that_can_change > 0) {
-               change = change_left/groups_left_that_can_change;
-               /* the division could cause it to be zero, so if it is, forget it*/
-               if(change == 0) {
-                       change = change_left;
-               }
-               do {
-                       was_a_change = FALSE;
-                       for(i = 0; i < dvert->totweight; i++) {
-                               dw = defvert_verify_index(dvert, i);
-                               if(!change_status[dw->def_nr]) {
-                                       continue;
+               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;
                                }
-
-                               old_change_left = change_left;
-                               change_left -= change;
-                               // sign change?
-                               if(change_left!=0 && change_left/fabs(change_left) != old_change_left/fabs(old_change_left)) {
-                                       change_left = old_change_left;
-                                       break;
-                               } else {
-                                       dw->weight += change;
-                               }
-                               if(dw->weight >= 1.0f) {
-
-                                       change_left += dw->weight-1.0f;
-                                       dw->weight = 1.0f;
-                                       groups_left_that_can_change--;
-                                       change_status[dw->def_nr] = FALSE;
-
-                               }else if(dw->weight <= 0.0f) {
-
-                                       change_left += dw->weight;
-                                       dw->weight = 0.0f;
-                                       groups_left_that_can_change--;
-                                       change_status[dw->def_nr] = FALSE;
-                               }
-                               /* if it was too small, don't get stuck in an infinite loop! */
-                               if(old_change_left != change_left) {
-                                       was_a_change = TRUE;
+                               // the value should never reach zero while multi-painting if it was nonzero beforehand
+                               if(val <= 0) {
+                                       return;
                                }
                        }
-               } while(groups_left_that_can_change > 0 && change_left != 0.0f && was_a_change);
-       }
-       // now it should never have any left, unless there are precision problems
-       // add any remaining change back to the original weight
-       if(change_left > 0) {
-               if(multipaint) {
-                       for(i = 0; i < defcnt; i++) {
-                               if(selection[i]) {
-                                       defvert_find_index(dvert, i)->weight += change_left/selected;
-                               }
+               }
+       }
+       // 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;
                        }
-               } else {
-                       defvert_find_index(dvert, def_nr)->weight += change_left;
                }
        }
-       MEM_freeN(change_status);
 }
-/* Jason */
-/* get the change that is needed to get a valid multipaint (if it can)*/
-static float get_valid_multipaint_change(MDeformVert *dvert, float neww, float oldw, float allowed_totchange, char* validmap, char* bone_groups, char* selection, int defcnt) {
+/*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;
-       float tchange;
-       MDeformWeight *w;
-       float val;
-       // see if you need to do anything (if it is normalized)
-       float sumw = 0.0f;
-       if(allowed_totchange == 0 || oldw == 0 || !selection) {
-               return FALSE;
-       }
-       change = neww/oldw;
-       if(change == 1 || !change) {
-               return FALSE;
+       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;
        }
-       // see if all changes are valid before doing any
+       // 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++) {
-               if(!selection[i] || !bone_groups[i]) {
-                       continue;
-               }
-               w = defvert_verify_index(dvert, i);
-               // already reached the cap
-               if(change > 1 && w->weight==1) {
-                       return FALSE;
-               }
-               if(w->weight == 0) {
-                       if(selection[i]) {
-                               return FALSE;
+               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;
                }
-               val = w->weight*change;
-               // no success here-I'm not going to force it to try to be just above 0 by changing 'change'
-               if(val <= 0) {
-                       return FALSE;
-               }
-               if (validmap){
-                       sumw += w->weight;
-               }else if(val > 1) {
-                       // use the transitive property to make magic, all of the others
-                       // will still end up within the boundaries if the worst case does
-                       change = 1.0f/w->weight;
+               // 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(validmap && sumw == 1.0f) {
-               return FALSE;
-       }
-       if(allowed_totchange>0) {
-               for(i = 0; i < defcnt; i++) {
-                       w = defvert_find_index(dvert, i);
-                       if(w && selection[i] && bone_groups[i]) {
-                               tchange += w->weight*change;
+       // 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;
                        }
                }
-               tchange = tchange/allowed_totchange;
-               if(tchange < change) {
-                       change = tchange;
+       }
+
+       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;
                }
        }
-       return change;
+       if(selwsum && selwsum+brush_change > 0) {
+               return (selwsum+brush_change)/selwsum;
+       }
+       return 0.0f;
 }
-static float multipaint_vgroups(MDeformVert *dvert, float change, char* bone_groups, char* selection) {
+/*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;
-       float totchange = 0.0f;
-       float old;
+       MDeformWeight *d;
        MDeformWeight *w;
-       for(i = 0; i < dvert->totweight; i++) {
-               w = (dvert->dw+i);
-               if(!bone_groups[w->def_nr] || !selection[w->def_nr] || w->weight == 0) {
-                       continue;
+       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;
                }
-               old = w->weight;
-               w->weight *= change;
-               totchange += w->weight - old;
        }
-       return totchange;
 }
 /* Jason */
-static void check_locks_and_normalize(Object *ob, Mesh *me, int index, int vgroup, MDeformWeight *dw, float oldw, char *validmap, char *flags, int defcnt, char *bone_groups, char *selection, int selected, int multipaint)
-{
-       float change=0.0f;
-       float totchange=0.0f;
-       float allowed_totchange;
-       int def_nr = dw->def_nr;
-       float orig_change = oldw-dw->weight;
-       float neww = dw->weight;
-       dw->weight = oldw;
-       if(flags && (has_locked_group(me->dvert+index, flags) || flags[dw->def_nr])) {
-               if(flags[dw->def_nr] || (multipaint && has_locked_group_selected(defcnt, selection, flags)) || !has_unselected_unlocked_bone_group(defcnt, selection, selected, flags, bone_groups)) {
-                       // cannot change locked groups!
-               } else if((allowed_totchange = get_change_allowed_from_unlocked_bone_groups(me->dvert+index, def_nr, defcnt, selection, (int)(fabs(orig_change)/orig_change), flags, bone_groups)) != 0) {
-                       dw = defvert_find_index(me->dvert+index, def_nr);
-                       if (multipaint && selected > 1) {
-                               if(selection[dw->def_nr] && (change = get_valid_multipaint_change(me->dvert+index, neww, oldw, allowed_totchange, validmap, bone_groups, selection, defcnt))) {
-                                       totchange = multipaint_vgroups(me->dvert+index, change, bone_groups, selection);
-                                       if(totchange !=0){
-                                               redistribute_weight_change(ob, me->dvert+index, index, def_nr, multipaint, selection, selected, totchange, flags, defcnt, bone_groups);
-                                       }
-                               }
-                       } else if(bone_groups[dw->def_nr]) {
-                               totchange = oldw - neww;
-                               if(fabs(totchange) > fabs(allowed_totchange)) {
-                                       totchange = allowed_totchange;
-                               }
-                               if(totchange !=0){
-                                       redistribute_weight_change(ob, me->dvert+index, index, def_nr, FALSE, selection, selected, totchange, flags, defcnt, bone_groups);
-                                       dw->weight -= totchange;
-                               }
-                       }
-               }
-       } else if(bone_groups[dw->def_nr]) {
+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) {
-                       // try to alter the other bone groups in the dvert with the changed dw if possible, if it isn't, change it back
-                       if(selection[dw->def_nr] && (change = get_valid_multipaint_change(me->dvert+index, neww, oldw, -1, validmap, bone_groups, selection, defcnt))) {
-                               multipaint_vgroups(me->dvert+index, change, bone_groups, selection);
+                       if(change && change!=1) {
+                               multipaint_selection(dvert, change, selection, defcnt);
                        }
-               }else {
+               } else {// this lets users paint normally, but don't let them paint locked groups
                        dw->weight = neww;
                }
        }
-       do_weight_paint_auto_normalize_change_act_group(me->dvert+index, validmap);
+       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;
@@ -1366,7 +1487,7 @@ static char* get_selected_defgroups(Object *ob, int defcnt) {
                        }
                }
        }
-       
+       // the array has whether or not the corresponding group is selected
        return dg_flags;
 }
 /* TODO move duplicates to header */
@@ -1382,6 +1503,19 @@ static int count_true(char *list, int len)
        }
        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);
 
@@ -1390,7 +1524,7 @@ static void do_weight_paint_vertex(VPaint *wp, Object *ob, int index,
                                   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 */
@@ -1399,7 +1533,12 @@ static void do_weight_paint_vertex(VPaint *wp, Object *ob, int index,
        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 {
@@ -1423,10 +1562,51 @@ static void do_weight_paint_vertex(VPaint *wp, Object *ob, int index,
        if(!selected && ob->actdef) {
                selected = 1;
        }
+       
        oldw = dw->weight;
-       wpaint_blend(wp, dw, uw, alpha, paintweight, flip);
+       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 */
-       check_locks_and_normalize(ob, me, index, vgroup, dw, oldw, validmap, flags, defcnt, bone_groups, selection, selected, multipaint);
+       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);
 
@@ -1439,20 +1619,16 @@ static void do_weight_paint_vertex(VPaint *wp, Object *ob, int index,
                        else
                                uw= defvert_verify_index(me->dvert+j, vgroup);
                        /* Jason */
-                       oldw = uw->weight;
-
-                       uw->weight= dw->weight;
+                       //uw->weight= dw->weight;
                        /* Jason */
-                       check_locks_and_normalize(ob, me, j, vgroup, uw, oldw, validmap, flags, defcnt, bone_groups, selection, selected, multipaint);
+                       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);
        }
-       if(selection) {
-               MEM_freeN(selection);
-       }
+       MEM_freeN(selection);
        if(!validmap) {
                MEM_freeN(bone_groups);
        }
@@ -1690,26 +1866,7 @@ static int wpaint_stroke_test_start(bContext *C, wmOperator *op, wmEvent *UNUSED
        
        /* if mirror painting, find the other group */
        if(me->editflag & ME_EDIT_MIRROR_X) {
-               bDeformGroup *defgroup= BLI_findlink(&ob->defbase, ob->actdef-1);
-               if(defgroup) {
-                       bDeformGroup *curdef;
-                       int actdef= 0;
-                       char name[32];
-
-                       flip_side_name(name, defgroup->name, FALSE);
-
-                       for (curdef = ob->defbase.first; curdef; curdef=curdef->next, actdef++)
-                               if (!strcmp(curdef->name, name))
-                                       break;
-                       if(curdef==NULL) {
-                               int olddef= ob->actdef; /* tsk, ED_vgroup_add sets the active defgroup */
-                               curdef= ED_vgroup_add_name (ob, name);
-                               ob->actdef= olddef;
-                       }
-                       
-                       if(curdef && curdef!=defgroup)
-                               wpd->vgroup_mirror= actdef;
-               }
+               wpaint_mirror_vgroup_ensure(ob, &wpd->vgroup_mirror);
        }
 
        return 1;
@@ -1942,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)
 {
        
@@ -1954,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;
@@ -2234,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 */
@@ -2245,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;