merged from trunk 20741:20848
[blender.git] / source / blender / editors / mesh / editdeform.c
index ca6415d8b76cd7ff9d7d016691c19070372c6765..3ccd4d56ece7b5daec9b1e515b466308dc8b2885 100644 (file)
@@ -94,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++){
@@ -110,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:
@@ -220,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));
@@ -255,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;
                }
        }
 }
@@ -332,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))
@@ -395,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);
@@ -720,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;
@@ -759,6 +785,7 @@ void assign_verts_defgroup (Object *obedit, float weight)
                                }
                        }
                }
+               BKE_mesh_end_editmesh(me, em);
        }
                break;
        case OB_LATTICE:
@@ -882,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++){
@@ -911,6 +939,7 @@ void remove_verts_defgroup (Object *obedit, int allverts)
                                }
                        }
                }
+               BKE_mesh_end_editmesh(me, em);
        }
                break;
        case OB_LATTICE: