split weight normalize into 2 operators, normalize and normalize_all.
authorCampbell Barton <ideasman42@gmail.com>
Tue, 20 Oct 2009 13:59:26 +0000 (13:59 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Tue, 20 Oct 2009 13:59:26 +0000 (13:59 +0000)
Added an option for normalize_all that keeps the active group at its existing weight while normaling all other groups around it.
Thsi makes it easy to paint up to 100% where all other groups will use progressivly less until the active group is 100% and all others are 0.

Added weight operators to the toolbar

release/scripts/ui/space_view3d_toolbar.py
source/blender/editors/object/object_intern.h
source/blender/editors/object/object_ops.c
source/blender/editors/object/object_vgroup.c

index f354a3314f2e2caa511483b77e3e5f2f6c21b41e..a552bb7de14fade380054523cc7b05d028b2b027 100644 (file)
@@ -103,7 +103,7 @@ class VIEW3D_PT_tools_meshedit(View3DPanel):
                col.itemO("screen.repeat_history", text="History...")
                col.itemO("screen.redo_last", text="Tweak...")
                
-class VIEW3D_PT_tools_mesheditoptions(View3DPanel):
+class VIEW3D_PT_tools_meshedit_options(View3DPanel):
        __context__ = "mesh_edit"
        __label__ = "Mesh Options"
 
@@ -635,6 +635,22 @@ class VIEW3D_PT_sculpt_options(PaintPanel):
 # ********** default tools for weightpaint ****************
 
 class VIEW3D_PT_tools_weightpaint(View3DPanel):
+       __context__ = "weightpaint"
+       __label__ = "Weight Tools"
+
+       def draw(self, context):
+               layout = self.layout
+               
+               wpaint = context.tool_settings.weight_paint
+
+               col = layout.column()
+               # col.itemL(text="Blend:")
+               col.itemO("object.vertex_group_normalize_all", text="Normalize All")
+               col.itemO("object.vertex_group_normalize", text="Normalize")
+               col.itemO("object.vertex_group_invert", text="Invert")
+               col.itemO("object.vertex_group_clean", text="Clean")
+
+class VIEW3D_PT_tools_weightpaint_options(View3DPanel):
        __context__ = "weightpaint"
        __label__ = "Options"
 
@@ -806,9 +822,10 @@ class VIEW3D_PT_tools_particlemode(View3DPanel):
                sub.active = pe.fade_time
                sub.itemR(pe, "fade_frames", slider=True)
 
+bpy.types.register(VIEW3D_PT_tools_weightpaint)
 bpy.types.register(VIEW3D_PT_tools_objectmode)
 bpy.types.register(VIEW3D_PT_tools_meshedit)
-bpy.types.register(VIEW3D_PT_tools_mesheditoptions)
+bpy.types.register(VIEW3D_PT_tools_meshedit_options)
 bpy.types.register(VIEW3D_PT_tools_curveedit)
 bpy.types.register(VIEW3D_PT_tools_surfaceedit)
 bpy.types.register(VIEW3D_PT_tools_textedit)
@@ -822,6 +839,6 @@ bpy.types.register(VIEW3D_PT_tools_brush_stroke)
 bpy.types.register(VIEW3D_PT_tools_brush_curve)
 bpy.types.register(VIEW3D_PT_sculpt_options)
 bpy.types.register(VIEW3D_PT_tools_vertexpaint)
-bpy.types.register(VIEW3D_PT_tools_weightpaint)
+bpy.types.register(VIEW3D_PT_tools_weightpaint_options)
 bpy.types.register(VIEW3D_PT_tools_projectpaint)
 bpy.types.register(VIEW3D_PT_tools_particlemode)
index 866180f01a0b33b48b6098fc5c628f47dc164c5a..36d897b76c8bc7b267685d2928a07a0c4c529fa0 100644 (file)
@@ -167,6 +167,7 @@ void OBJECT_OT_vertex_group_deselect(struct wmOperatorType *ot);
 void OBJECT_OT_vertex_group_copy_to_linked(struct wmOperatorType *ot);
 void OBJECT_OT_vertex_group_copy(struct wmOperatorType *ot);
 void OBJECT_OT_vertex_group_normalize(struct wmOperatorType *ot);
+void OBJECT_OT_vertex_group_normalize_all(struct wmOperatorType *ot);
 void OBJECT_OT_vertex_group_invert(struct wmOperatorType *ot);
 void OBJECT_OT_vertex_group_clean(struct wmOperatorType *ot);
 void OBJECT_OT_vertex_group_menu(struct wmOperatorType *ot);
index 17487ea6acaab3cbdd4501864e409bde790e5cb5..06b554f0406df5da6480467375b3ad27dd1a8da8 100644 (file)
@@ -164,6 +164,7 @@ void ED_operatortypes_object(void)
        WM_operatortype_append(OBJECT_OT_vertex_group_copy_to_linked);
        WM_operatortype_append(OBJECT_OT_vertex_group_copy);
        WM_operatortype_append(OBJECT_OT_vertex_group_normalize);
+       WM_operatortype_append(OBJECT_OT_vertex_group_normalize_all);
        WM_operatortype_append(OBJECT_OT_vertex_group_invert);
        WM_operatortype_append(OBJECT_OT_vertex_group_clean);
        WM_operatortype_append(OBJECT_OT_vertex_group_menu);
index 7c206ce300a52fb2f87fd3bea1f8820ae8c10378..e5467e509722ef92d0e16dcd2073b12e25bdd764 100644 (file)
@@ -616,9 +616,9 @@ static void vgroup_normalize(Object *ob)
 }
 
 /* TODO - select between groups */
-static void vgroup_normalize_all(Object *ob)
+static void vgroup_normalize_all(Object *ob, int lock_active)
 {
-       MDeformWeight *dw;
+       MDeformWeight *dw, *dw_act;
        MDeformVert *dvert, *dvert_array=NULL;
        int i, dvert_tot=0;
        float tot_weight;
@@ -626,25 +626,70 @@ static void vgroup_normalize_all(Object *ob)
        ED_vgroup_give_array(ob->data, &dvert_array, &dvert_tot);
 
        if(dvert_array) {
-               for(i = 0; i < dvert_tot; i++) {
-                       int j;
-                       tot_weight= 0.0f;
-                       dvert = dvert_array+i;
+               if(lock_active) {
+                       int def_nr= ob->actdef-1;
 
-                       j= dvert->totweight;
-                       while(j--) {
-                               dw= dvert->dw + j;
-                               tot_weight += dw->weight;
+                       for(i = 0; i < dvert_tot; i++) {
+                               float lock_iweight= 1.0f;
+                               int j;
+
+                               tot_weight= 0.0f;
+                               dw_act= NULL;
+                               dvert = dvert_array+i;
+
+                               j= dvert->totweight;
+                               while(j--) {
+                                       dw= dvert->dw + j;
+
+                                       if(dw->def_nr==def_nr) {
+                                               dw_act= dw;
+                                               lock_iweight = (1.0f - dw_act->weight);
+                                       }
+                                       else {
+                                               tot_weight += dw->weight;
+                                       }
+                               }
+
+                               if(tot_weight) {
+                                       j= dvert->totweight;
+                                       while(j--) {
+                                               dw= dvert->dw + j;
+                                               if(dw == dw_act) {
+                                                       if (dvert->totweight==1) {
+                                                               dw_act->weight= 1.0f; /* no other weights, set to 1.0 */
+                                                       }
+                                               } else {
+                                                       if(dw->weight > 0.0f)
+                                                               dw->weight = (dw->weight / tot_weight) * lock_iweight;
+                                               }
+
+                                               /* incase of division errors with very low weights */
+                                               CLAMP(dw->weight, 0.0f, 1.0f);
+                                       }
+                               }
                        }
+               }
+               else {
+                       for(i = 0; i < dvert_tot; i++) {
+                               int j;
+                               tot_weight= 0.0f;
+                               dvert = dvert_array+i;
 
-                       if(tot_weight) {
                                j= dvert->totweight;
                                while(j--) {
                                        dw= dvert->dw + j;
-                                       dw->weight /= tot_weight;
+                                       tot_weight += dw->weight;
+                               }
 
-                                       /* incase of division errors with very low weights */
-                                       CLAMP(dw->weight, 0.0f, 1.0f);
+                               if(tot_weight) {
+                                       j= dvert->totweight;
+                                       while(j--) {
+                                               dw= dvert->dw + j;
+                                               dw->weight /= tot_weight;
+
+                                               /* incase of division errors with very low weights */
+                                               CLAMP(dw->weight, 0.0f, 1.0f);
+                                       }
                                }
                        }
                }
@@ -1342,12 +1387,8 @@ void OBJECT_OT_vertex_group_copy(wmOperatorType *ot)
 static int vertex_group_normalize_exec(bContext *C, wmOperator *op)
 {
        Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
-       int all_groups= RNA_boolean_get(op->ptr,"all_groups");
 
-       if(all_groups)
-               vgroup_normalize_all(ob);
-       else
-               vgroup_normalize(ob);
+       vgroup_normalize(ob);
 
        DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
        WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
@@ -1368,8 +1409,36 @@ void OBJECT_OT_vertex_group_normalize(wmOperatorType *ot)
 
        /* flags */
        ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+}
+
+static int vertex_group_normalize_all_exec(bContext *C, wmOperator *op)
+{
+       Object *ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
+       int lock_active= RNA_boolean_get(op->ptr,"lock_active");
+
+       vgroup_normalize_all(ob, lock_active);
+
+       DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
+       WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
+       WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data);
+
+       return OPERATOR_FINISHED;
+}
+
+void OBJECT_OT_vertex_group_normalize_all(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Normalize All Vertex Groups";
+       ot->idname= "OBJECT_OT_vertex_group_normalize_all";
+
+       /* api callbacks */
+       ot->poll= vertex_group_poll;
+       ot->exec= vertex_group_normalize_all_exec;
+
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
 
-       RNA_def_boolean(ot->srna, "all_groups", FALSE, "All Groups", "Normalize all vertex groups.");
+       RNA_def_boolean(ot->srna, "lock_active", TRUE, "Lock Active", "Keep the values of the active group while normalizing others.");
 }
 
 static int vertex_group_invert_exec(bContext *C, wmOperator *op)