merged from trunk 20741:20848
[blender.git] / source / blender / editors / mesh / editdeform.c
index 703ae2b9fc806a50d9667815c3b72b21a1ab7a60..3ccd4d56ece7b5daec9b1e515b466308dc8b2885 100644 (file)
@@ -43,6 +43,7 @@
 #include "DNA_object_force.h"
 #include "DNA_scene_types.h"
 #include "DNA_particle_types.h"
+#include "DNA_windowmanager_types.h"
 
 #include "BLI_blenlib.h"
 #include "BLI_editVert.h"
 static void BIF_undo_push() {}
 static void error() {}
 
+static Lattice *def_get_lattice(Object *ob)
+{
+       if(ob->type==OB_LATTICE) {
+               Lattice *lt= ob->data;
+               if(lt->editlatt)
+                       return lt->editlatt;
+               return lt;
+       }
+       return NULL;
+}
+
 /* only in editmode */
 void sel_verts_defgroup (Object *obedit, int select)
 {
@@ -82,9 +94,10 @@ void sel_verts_defgroup (Object *obedit, int select)
        case OB_MESH:
        {
                Mesh *me= ob->data;
-               
-               for (eve=me->edit_mesh->verts.first; eve; eve=eve->next){
-                       dvert= CustomData_em_get(&me->edit_mesh->vdata, eve->data, CD_MDEFORMVERT);
+               EditMesh *em = BKE_mesh_get_editmesh(me);
+
+               for (eve=em->verts.first; eve; eve=eve->next){
+                       dvert= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT);
 
                        if (dvert && dvert->totweight){
                                for (i=0; i<dvert->totweight; i++){
@@ -98,19 +111,24 @@ void sel_verts_defgroup (Object *obedit, int select)
                        }
                }
                /* this has to be called, because this function operates on vertices only */
-               if(select) EM_select_flush(me->edit_mesh);      // vertices to edges/faces
-               else EM_deselect_flush(me->edit_mesh);
+               if(select) EM_select_flush(em); // vertices to edges/faces
+               else EM_deselect_flush(em);
+
+               BKE_mesh_end_editmesh(me, em);
        }
                break;
        case OB_LATTICE:
-               if(editLatt->dvert) {
+       {
+               Lattice *lt= def_get_lattice(ob);
+               
+               if(lt->dvert) {
                        BPoint *bp;
                        int a, tot;
                        
-                       dvert= editLatt->dvert;
+                       dvert= lt->dvert;
 
-                       tot= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
-                       for(a=0, bp= editLatt->def; a<tot; a++, bp++, dvert++) {
+                       tot= lt->pntsu*lt->pntsv*lt->pntsw;
+                       for(a=0, bp= lt->def; a<tot; a++, bp++, dvert++) {
                                for (i=0; i<dvert->totweight; i++){
                                        if (dvert->dw[i].def_nr == (ob->actdef-1)) {
                                                if(select) bp->f1 |= SELECT;
@@ -120,7 +138,8 @@ void sel_verts_defgroup (Object *obedit, int select)
                                        }
                                }
                        }
-               }       
+               }
+       }
                break;
                
        default:
@@ -204,11 +223,10 @@ void duplicate_defgroup ( Object *ob )
        bDeformGroup *dg, *cdg;
        char name[32], s[32];
        MDeformWeight *org, *cpy;
-       MDeformVert *dvert;
-       Mesh *me;
-       int i, idg, icdg;
+       MDeformVert *dvert, *dvert_array;
+       int i, idg, icdg, dvert_tot;
 
-       if (ob->type != OB_MESH)
+       if (ob->type != OB_MESH && ob->type != OB_LATTICE)
                return;
 
        dg = BLI_findlink (&ob->defbase, (ob->actdef-1));
@@ -239,16 +257,28 @@ void duplicate_defgroup ( Object *ob )
        ob->actdef = BLI_countlist (&ob->defbase);
        icdg = (ob->actdef-1);
 
-       me = get_mesh (ob);
-       if (!me->dvert)
+       if(ob->type == OB_MESH) {
+               Mesh *me = get_mesh (ob);
+               dvert_array= me->dvert;
+               dvert_tot= me->totvert;
+       }
+       else if (ob->type == OB_LATTICE) {
+               Lattice *lt= (Lattice *)ob->data;
+               dvert_array= lt->dvert;
+               dvert_tot= lt->pntsu*lt->pntsv*lt->pntsw;
+       }
+       
+       if (!dvert_array)
                return;
 
-       for (i = 0; i < me->totvert; i++) {
-               dvert = me->dvert+i;
+       for (i = 0; i < dvert_tot; i++) {
+               dvert = dvert_array+i;
                org = get_defweight (dvert, idg);
                if (org) {
+                       float weight = org->weight;
+                       /* verify_defweight re-allocs org so need to store the weight first */
                        cpy = verify_defweight (dvert, icdg);
-                       cpy->weight = org->weight;
+                       cpy->weight = weight;
                }
        }
 }
@@ -316,29 +346,39 @@ static void del_defgroup_update_users(Object *ob, int id)
 void del_defgroup_in_object_mode ( Object *ob )
 {
        bDeformGroup *dg;
-       MDeformVert *dvert;
-       Mesh *me;
-       int i, e;
+       MDeformVert *dvert_array, *dvert;
+       
+       int i, e, dvert_tot;
 
-       if ((!ob) || (ob->type != OB_MESH))
+       if ((!ob) || (ob->type != OB_MESH && ob->type != OB_LATTICE))
                return;
 
+       if(ob->type == OB_MESH) {
+               Mesh *me = get_mesh (ob);
+               dvert_array= me->dvert;
+               dvert_tot= me->totvert;
+       }
+       else if (ob->type == OB_LATTICE) {
+               Lattice *lt= (Lattice *)ob->data;
+               dvert_array= lt->dvert;
+               dvert_tot= lt->pntsu*lt->pntsv*lt->pntsw;
+       }
+       
        dg = BLI_findlink (&ob->defbase, (ob->actdef-1));
        if (!dg)
                return;
-
-       me = get_mesh (ob);
-       if (me->dvert) {
-               for (i = 0; i < me->totvert; i++) {
-                       dvert = me->dvert + i;
+       
+       if (dvert_array) {
+               for (i = 0; i < dvert_tot; i++) {
+                       dvert = dvert_array + i;
                        if (dvert) {
                                if (get_defweight (dvert, (ob->actdef-1)))
                                        remove_vert_defgroup (ob, dg, i);
                        }
                }
 
-               for (i = 0; i < me->totvert; i++) {
-                       dvert = me->dvert+i;
+               for (i = 0; i < dvert_tot; i++) {
+                       dvert = dvert_array+i;
                        if (dvert) {
                                for (e = 0; e < dvert->totweight; e++) {
                                        if (dvert->dw[e].def_nr > (ob->actdef-1))
@@ -379,27 +419,29 @@ void del_defgroup (Object *ob)
        /* Make sure that any verts with higher indices are adjusted accordingly */
        if(ob->type==OB_MESH) {
                Mesh *me= ob->data;
-               EditMesh *em = me->edit_mesh;
+               EditMesh *em = BKE_mesh_get_editmesh(me);
                EditVert *eve;
                MDeformVert *dvert;
                
                for (eve=em->verts.first; eve; eve=eve->next){
-                       dvert= CustomData_em_get(&me->edit_mesh->vdata, eve->data, CD_MDEFORMVERT);
+                       dvert= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT);
 
                        if (dvert)
                                for (i=0; i<dvert->totweight; i++)
                                        if (dvert->dw[i].def_nr > (ob->actdef-1))
                                                dvert->dw[i].def_nr--;
                }
+               BKE_mesh_end_editmesh(me, em);
        }
-       else {
+       else if(ob->type==OB_LATTICE) {
+               Lattice *lt= def_get_lattice(ob);
                BPoint *bp;
-               MDeformVert *dvert= editLatt->dvert;
+               MDeformVert *dvert= lt->dvert;
                int a, tot;
                
                if (dvert) {
-                       tot= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
-                       for(a=0, bp= editLatt->def; a<tot; a++, bp++, dvert++) {
+                       tot= lt->pntsu*lt->pntsv*lt->pntsw;
+                       for(a=0, bp= lt->def; a<tot; a++, bp++, dvert++) {
                                for (i=0; i<dvert->totweight; i++){
                                        if (dvert->dw[i].def_nr > (ob->actdef-1))
                                                dvert->dw[i].def_nr--;
@@ -424,10 +466,11 @@ void del_defgroup (Object *ob)
                        CustomData_free_layer_active(&me->vdata, CD_MDEFORMVERT, me->totvert);
                        me->dvert= NULL;
                }
-               else {
-                       if (editLatt->dvert) {
-                               MEM_freeN(editLatt->dvert);
-                               editLatt->dvert= NULL;
+               else if(ob->type==OB_LATTICE) {
+                       Lattice *lt= def_get_lattice(ob);
+                       if (lt->dvert) {
+                               MEM_freeN(lt->dvert);
+                               lt->dvert= NULL;
                        }
                }
        }
@@ -445,10 +488,11 @@ void del_all_defgroups (Object *ob)
                CustomData_free_layer_active(&me->vdata, CD_MDEFORMVERT, me->totvert);
                me->dvert= NULL;
        }
-       else {
-               if (editLatt->dvert) {
-                       MEM_freeN(editLatt->dvert);
-                       editLatt->dvert= NULL;
+       else if(ob->type==OB_LATTICE) {
+               Lattice *lt= def_get_lattice(ob);
+               if (lt->dvert) {
+                       MEM_freeN(lt->dvert);
+                       lt->dvert= NULL;
                }
        }
        
@@ -500,9 +544,8 @@ void remove_vert_def_nr (Object *ob, int def_nr, int vertnum)
                        dvert = ((Mesh*)ob->data)->dvert + vertnum;
        }
        else if(ob->type==OB_LATTICE) {
-               Lattice *lt= ob->data;
+               Lattice *lt= def_get_lattice(ob);
                
-               if(lt->editlatt) lt= lt->editlatt;
                if(lt->dvert)
                        dvert = lt->dvert + vertnum;
        }
@@ -568,10 +611,8 @@ void add_vert_defnr (Object *ob, int def_nr, int vertnum,
                        dv = ((Mesh*)ob->data)->dvert + vertnum;
        }
        else if(ob->type==OB_LATTICE) {
-               Lattice *lt= ob->data;
+               Lattice *lt= def_get_lattice(ob);
                
-               if(lt->editlatt) lt= lt->editlatt;
-                       
                if(lt->dvert)
                        dv = lt->dvert + vertnum;
        }
@@ -678,9 +719,8 @@ void add_vert_to_defgroup (Object *ob, bDeformGroup *dg, int vertnum,
 }
 
 /* Only available in editmode */
-void assign_verts_defgroup (Object *obedit)
+void assign_verts_defgroup (Object *obedit, float weight)
 {
-       float editbutvweight;   /* XXX */
        Object *ob;
        EditVert *eve;
        bDeformGroup *dg, *eg;
@@ -705,13 +745,14 @@ void assign_verts_defgroup (Object *obedit)
        case OB_MESH:
        {
                Mesh *me= ob->data;
-               
-               if (!CustomData_has_layer(&me->edit_mesh->vdata, CD_MDEFORMVERT))
-                       EM_add_data_layer(me->edit_mesh, &me->edit_mesh->vdata, CD_MDEFORMVERT);
+               EditMesh *em = BKE_mesh_get_editmesh(me);
+
+               if (!CustomData_has_layer(&em->vdata, CD_MDEFORMVERT))
+                       EM_add_data_layer(em, &em->vdata, CD_MDEFORMVERT);
 
                /* Go through the list of editverts and assign them */
-               for (eve=me->edit_mesh->verts.first; eve; eve=eve->next){
-                       dvert= CustomData_em_get(&me->edit_mesh->vdata, eve->data, CD_MDEFORMVERT);
+               for (eve=em->verts.first; eve; eve=eve->next){
+                       dvert= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT);
 
                        if (dvert && (eve->f & 1)){
                                done=0;
@@ -722,7 +763,7 @@ void assign_verts_defgroup (Object *obedit)
                                        eg = BLI_findlink (&ob->defbase, dvert->dw[i].def_nr);
                                        /* Find the actual group */
                                        if (eg==dg){
-                                               dvert->dw[i].weight=editbutvweight;
+                                               dvert->dw[i].weight= weight;
                                                done=1;
                                                break;
                                        }
@@ -736,7 +777,7 @@ void assign_verts_defgroup (Object *obedit)
                                        }
                                        dvert->dw=newdw;
 
-                                       dvert->dw[dvert->totweight].weight= editbutvweight;
+                                       dvert->dw[dvert->totweight].weight= weight;
                                        dvert->dw[dvert->totweight].def_nr= ob->actdef-1;
 
                                        dvert->totweight++;
@@ -744,22 +785,24 @@ void assign_verts_defgroup (Object *obedit)
                                }
                        }
                }
+               BKE_mesh_end_editmesh(me, em);
        }
                break;
        case OB_LATTICE:
                {
+                       Lattice *lt= def_get_lattice(ob);
                        BPoint *bp;
                        int a, tot;
                        
-                       if(editLatt->dvert==NULL)
-                               create_dverts(&editLatt->id);
+                       if(lt->dvert==NULL)
+                               create_dverts(&lt->id);
                        
-                       tot= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
-                       for(a=0, bp= editLatt->def; a<tot; a++, bp++) {
+                       tot= lt->pntsu*lt->pntsv*lt->pntsw;
+                       for(a=0, bp= lt->def; a<tot; a++, bp++) {
                                if(bp->f1 & SELECT)
-                                       add_vert_defnr (ob, ob->actdef-1, a, editbutvweight, WEIGHT_REPLACE);
+                                       add_vert_defnr (ob, ob->actdef-1, a, weight, WEIGHT_REPLACE);
                        }
-               }       
+               }
                break;
        default:
                printf ("Assigning deformation groups to unknown object type\n");
@@ -808,9 +851,7 @@ static float get_vert_def_nr (Object *ob, int def_nr, int vertnum)
                        dvert = ((Mesh*)ob->data)->dvert + vertnum;
        }
        else if(ob->type==OB_LATTICE) {
-               Lattice *lt= ob->data;
-               
-               if(lt->editlatt) lt= lt->editlatt;
+               Lattice *lt= def_get_lattice(ob);
                
                if(lt->dvert)
                        dvert = lt->dvert + vertnum;
@@ -868,9 +909,10 @@ void remove_verts_defgroup (Object *obedit, int allverts)
        case OB_MESH:
        {
                Mesh *me= ob->data;
-               
-               for (eve=me->edit_mesh->verts.first; eve; eve=eve->next){
-                       dvert= CustomData_em_get(&me->edit_mesh->vdata, eve->data, CD_MDEFORMVERT);
+               EditMesh *em = BKE_mesh_get_editmesh(me);
+
+               for (eve=em->verts.first; eve; eve=eve->next){
+                       dvert= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT);
                
                        if (dvert && dvert->dw && ((eve->f & 1) || allverts)){
                                for (i=0; i<dvert->totweight; i++){
@@ -897,19 +939,23 @@ void remove_verts_defgroup (Object *obedit, int allverts)
                                }
                        }
                }
+               BKE_mesh_end_editmesh(me, em);
        }
                break;
        case OB_LATTICE:
+       {
+               Lattice *lt= def_get_lattice(ob);
                
-               if(editLatt->dvert) {
+               if(lt->dvert) {
                        BPoint *bp;
-                       int a, tot= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
+                       int a, tot= lt->pntsu*lt->pntsv*lt->pntsw;
                                
-                       for(a=0, bp= editLatt->def; a<tot; a++, bp++) {
+                       for(a=0, bp= lt->def; a<tot; a++, bp++) {
                                if(allverts || (bp->f1 & SELECT))
                                        remove_vert_defgroup (ob, dg, a);
                        }
                }
+       }
                break;
                
        default:
@@ -969,13 +1015,14 @@ void vertexgroup_select_by_name(Object *ob, char *name)
  * and Lattices. (currently only restricted to those two)
  * It is only responsible for 
  */
-void vgroup_assign_with_menu(Object *ob)
+void vgroup_assign_with_menu(Scene *scene, Object *ob)
 {
+       VPaint *wp= scene->toolsettings->wpaint;
        int defCount;
-       int mode;
+       int mode= 0;
        
        /* prevent crashes */
-       if (ob==NULL) return;
+       if (wp==NULL || ob==NULL) return;
        
        defCount= BLI_countlist(&ob->defbase);
        
@@ -989,11 +1036,11 @@ void vgroup_assign_with_menu(Object *ob)
        switch (mode) {
                case 1: /* add to new group */
                        add_defgroup(ob);
-                       assign_verts_defgroup(ob);
+                       assign_verts_defgroup(ob, wp->brush->alpha);
                        BIF_undo_push("Assign to vertex group");
                        break;
                case 2: /* add to current group */
-                       assign_verts_defgroup(ob);
+                       assign_verts_defgroup(ob, wp->brush->alpha);
                        BIF_undo_push("Assign to vertex group");
                        break;
                case 3: /* remove from current group */
@@ -1014,7 +1061,7 @@ void vgroup_assign_with_menu(Object *ob)
 void vgroup_operation_with_menu(Object *ob)
 {
        int defCount;
-       int mode;
+       int mode= 0;
        
        /* prevent crashes and useless cases */
        if (ob==NULL) return;