merged from trunk 20741:20848
[blender.git] / source / blender / editors / mesh / editdeform.c
index 845290660c7ae6201f35642bbfb63bb187dd56bf..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"
@@ -93,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++){
@@ -109,8 +111,10 @@ 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:
@@ -219,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));
@@ -254,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;
                }
        }
 }
@@ -331,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))
@@ -394,18 +419,19 @@ 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 if(ob->type==OB_LATTICE) {
                Lattice *lt= def_get_lattice(ob);
@@ -719,13 +745,14 @@ void assign_verts_defgroup (Object *obedit, float weight)
        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;
@@ -758,6 +785,7 @@ void assign_verts_defgroup (Object *obedit, float weight)
                                }
                        }
                }
+               BKE_mesh_end_editmesh(me, em);
        }
                break;
        case OB_LATTICE:
@@ -881,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++){
@@ -910,6 +939,7 @@ void remove_verts_defgroup (Object *obedit, int allverts)
                                }
                        }
                }
+               BKE_mesh_end_editmesh(me, em);
        }
                break;
        case OB_LATTICE:
@@ -989,7 +1019,7 @@ void vgroup_assign_with_menu(Scene *scene, Object *ob)
 {
        VPaint *wp= scene->toolsettings->wpaint;
        int defCount;
-       int mode;
+       int mode= 0;
        
        /* prevent crashes */
        if (wp==NULL || ob==NULL) return;
@@ -1006,11 +1036,11 @@ void vgroup_assign_with_menu(Scene *scene, Object *ob)
        switch (mode) {
                case 1: /* add to new group */
                        add_defgroup(ob);
-                       assign_verts_defgroup(ob, wp->weight);
+                       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, wp->weight);
+                       assign_verts_defgroup(ob, wp->brush->alpha);
                        BIF_undo_push("Assign to vertex group");
                        break;
                case 3: /* remove from current group */
@@ -1031,7 +1061,7 @@ void vgroup_assign_with_menu(Scene *scene, 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;