disable applying constructive modifiers when in sculpt mode with multi-res data,...
[blender.git] / source / blender / editors / object / object_vgroup.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Ove M Henriksen.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/object/object_vgroup.c
29  *  \ingroup edobj
30  */
31
32 #include <string.h>
33 #include <stddef.h>
34 #include <math.h>
35 #include <assert.h>
36
37 #include "MEM_guardedalloc.h"
38
39 #include "DNA_cloth_types.h"
40 #include "DNA_curve_types.h"
41 #include "DNA_lattice_types.h"
42 #include "DNA_meshdata_types.h"
43 #include "DNA_mesh_types.h"
44 #include "DNA_modifier_types.h"
45 #include "DNA_object_types.h"
46 #include "DNA_object_force.h"
47 #include "DNA_scene_types.h"
48 #include "DNA_particle_types.h"
49
50 #include "BLI_array.h"
51 #include "BLI_math.h"
52 #include "BLI_blenlib.h"
53 #include "BLI_utildefines.h"
54
55 #include "BKE_context.h"
56 #include "BKE_customdata.h"
57 #include "BKE_deform.h"
58 #include "BKE_depsgraph.h"
59 #include "BKE_global.h"
60 #include "BKE_mesh.h"
61 #include "BKE_tessmesh.h"
62 #include "BKE_report.h"
63 #include "BKE_DerivedMesh.h"
64 #include "BKE_object_deform.h"
65
66 #include "RNA_access.h"
67 #include "RNA_define.h"
68
69 #include "WM_api.h"
70 #include "WM_types.h"
71
72 #include "ED_object.h"
73 #include "ED_mesh.h"
74
75 #include "UI_resources.h"
76
77 #include "object_intern.h"
78
79 /************************ Exported Functions **********************/
80 static void vgroup_remap_update_users(Object *ob, int *map);
81 static void vgroup_delete_edit_mode(Object *ob, bDeformGroup *defgroup);
82 static void vgroup_delete_object_mode(Object *ob, bDeformGroup *dg);
83 static void vgroup_delete_all(Object *ob);
84
85 static int vertex_group_use_vert_sel(Object *ob)
86 {
87         if (ob->mode == OB_MODE_EDIT) {
88                 return TRUE;
89         }
90         else if (ob->type == OB_MESH && ((Mesh *)ob->data)->editflag & ME_EDIT_VERT_SEL) {
91                 return TRUE;
92         }
93         else {
94                 return FALSE;
95         }
96 }
97
98 static Lattice *vgroup_edit_lattice(Object *ob)
99 {
100         Lattice *lt = ob->data;
101         BLI_assert(ob->type == OB_LATTICE);
102         return (lt->editlatt) ? lt->editlatt->latt : lt;
103 }
104
105 int ED_vgroup_object_is_edit_mode(Object *ob)
106 {
107         if (ob->type == OB_MESH)
108                 return (BMEdit_FromObject(ob) != NULL);
109         else if (ob->type == OB_LATTICE)
110                 return (((Lattice *)ob->data)->editlatt != NULL);
111
112         return 0;
113 }
114
115 bDeformGroup *ED_vgroup_add_name(Object *ob, const char *name)
116 {
117         bDeformGroup *defgroup;
118
119         if (!ob || !OB_TYPE_SUPPORT_VGROUP(ob->type))
120                 return NULL;
121         
122         defgroup = MEM_callocN(sizeof(bDeformGroup), "add deformGroup");
123
124         BLI_strncpy(defgroup->name, name, sizeof(defgroup->name));
125
126         BLI_addtail(&ob->defbase, defgroup);
127         defgroup_unique_name(defgroup, ob);
128
129         ob->actdef = BLI_countlist(&ob->defbase);
130
131         return defgroup;
132 }
133
134 bDeformGroup *ED_vgroup_add(Object *ob) 
135 {
136         return ED_vgroup_add_name(ob, "Group");
137 }
138
139 void ED_vgroup_delete(Object *ob, bDeformGroup *defgroup) 
140 {
141         BLI_assert(BLI_findindex(&ob->defbase, defgroup) != -1);
142
143         if (ED_vgroup_object_is_edit_mode(ob))
144                 vgroup_delete_edit_mode(ob, defgroup);
145         else
146                 vgroup_delete_object_mode(ob, defgroup);
147 }
148
149 void ED_vgroup_clear(Object *ob)
150 {
151         bDeformGroup *dg = (bDeformGroup *)ob->defbase.first;
152         int edit_mode = ED_vgroup_object_is_edit_mode(ob);
153
154         while (dg) {
155                 bDeformGroup *next_dg = dg->next;
156
157                 if (edit_mode)
158                         vgroup_delete_edit_mode(ob, dg);
159                 else
160                         vgroup_delete_object_mode(ob, dg);
161
162                 dg = next_dg;
163         }
164 }
165
166 int ED_vgroup_data_create(ID *id)
167 {
168         /* create deform verts */
169
170         if (GS(id->name) == ID_ME) {
171                 Mesh *me = (Mesh *)id;
172                 me->dvert = CustomData_add_layer(&me->vdata, CD_MDEFORMVERT, CD_CALLOC, NULL, me->totvert);
173                 return TRUE;
174         }
175         else if (GS(id->name) == ID_LT) {
176                 Lattice *lt = (Lattice *)id;
177                 lt->dvert = MEM_callocN(sizeof(MDeformVert) * lt->pntsu * lt->pntsv * lt->pntsw, "lattice deformVert");
178                 return TRUE;
179         }
180         else {
181                 return FALSE;
182         }
183 }
184
185 static int ED_vgroup_give_parray(ID *id, MDeformVert ***dvert_arr, int *dvert_tot, const short use_vert_sel)
186 {
187         *dvert_tot = 0;
188         *dvert_arr = NULL;
189
190         if (id) {
191                 switch (GS(id->name)) {
192                         case ID_ME:
193                         {
194                                 Mesh *me = (Mesh *)id;
195
196                                 if (me->edit_btmesh) {
197                                         BMEditMesh *em = me->edit_btmesh;
198                                         BMIter iter;
199                                         BMVert *eve;
200                                         int i;
201
202                                         if (!CustomData_has_layer(&em->bm->vdata, CD_MDEFORMVERT)) {
203                                                 return 0;
204                                         }
205
206                                         i = em->bm->totvert;
207
208                                         *dvert_arr = MEM_mallocN(sizeof(void *) * i, "vgroup parray from me");
209                                         *dvert_tot = i;
210
211                                         i = 0;
212                                         if (use_vert_sel) {
213                                                 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
214                                                         (*dvert_arr)[i] = BM_elem_flag_test(eve, BM_ELEM_SELECT) ?
215                                                                           CustomData_bmesh_get(&em->bm->vdata, eve->head.data, CD_MDEFORMVERT) : NULL;
216                                                         i++;
217                                                 }
218                                         }
219                                         else {
220                                                 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
221                                                         (*dvert_arr)[i] = CustomData_bmesh_get(&em->bm->vdata, eve->head.data, CD_MDEFORMVERT);
222                                                         i++;
223                                                 }
224                                         }
225
226                                         return 1;
227                                 }
228                                 else if (me->dvert) {
229                                         MVert *mvert = me->mvert;
230                                         MDeformVert *dvert = me->dvert;
231                                         int i;
232
233                                         *dvert_tot = me->totvert;
234                                         *dvert_arr = MEM_mallocN(sizeof(void *) * me->totvert, "vgroup parray from me");
235
236                                         if (use_vert_sel) {
237                                                 for (i = 0; i < me->totvert; i++) {
238                                                         (*dvert_arr)[i] = (mvert[i].flag & SELECT) ?
239                                                                           &dvert[i] : NULL;
240                                                 }
241                                         }
242                                         else {
243                                                 for (i = 0; i < me->totvert; i++) {
244                                                         (*dvert_arr)[i] = me->dvert + i;
245                                                 }
246                                         }
247
248                                         return 1;
249                                 }
250                                 else {
251                                         return 0;
252                                 }
253                         }
254                         case ID_LT:
255                         {
256                                 int i = 0;
257
258                                 Lattice *lt = (Lattice *)id;
259                                 lt = (lt->editlatt) ? lt->editlatt->latt : lt;
260
261                                 if (lt->dvert) {
262                                         BPoint *def = lt->def;
263                                         *dvert_tot = lt->pntsu * lt->pntsv * lt->pntsw;
264                                         *dvert_arr = MEM_mallocN(sizeof(void *) * (*dvert_tot), "vgroup parray from me");
265
266                                         if (use_vert_sel) {
267                                                 for (i = 0; i < *dvert_tot; i++) {
268                                                         (*dvert_arr)[i] = (def->f1 & SELECT) ?
269                                                                            &lt->dvert[i] : NULL;
270                                                 }
271                                         }
272                                         else {
273                                                 for (i = 0; i < *dvert_tot; i++) {
274                                                         (*dvert_arr)[i] = lt->dvert + i;
275                                                 }
276                                         }
277
278                                         return 1;
279                                 }
280                                 else {
281                                         return 0;
282                                 }
283                         }
284                 }
285         }
286
287         return 0;
288 }
289
290 /* returns true if the id type supports weights */
291 int ED_vgroup_give_array(ID *id, MDeformVert **dvert_arr, int *dvert_tot)
292 {
293         if (id) {
294                 switch (GS(id->name)) {
295                         case ID_ME:
296                         {
297                                 Mesh *me = (Mesh *)id;
298                                 *dvert_arr = me->dvert;
299                                 *dvert_tot = me->totvert;
300                                 return TRUE;
301                         }
302                         case ID_LT:
303                         {
304                                 Lattice *lt = (Lattice *)id;
305                                 lt = (lt->editlatt) ? lt->editlatt->latt : lt;
306                                 *dvert_arr = lt->dvert;
307                                 *dvert_tot = lt->pntsu * lt->pntsv * lt->pntsw;
308                                 return TRUE;
309                         }
310                 }
311         }
312
313         *dvert_arr = NULL;
314         *dvert_tot = 0;
315         return FALSE;
316 }
317
318 /* matching index only */
319 int ED_vgroup_copy_array(Object *ob, Object *ob_from)
320 {
321         MDeformVert **dvert_array_from, **dvf;
322         MDeformVert **dvert_array, **dv;
323         int dvert_tot_from;
324         int dvert_tot;
325         int i;
326         int defbase_tot_from = BLI_countlist(&ob_from->defbase);
327         int defbase_tot = BLI_countlist(&ob->defbase);
328         short new_vgroup = FALSE;
329
330         ED_vgroup_give_parray(ob_from->data, &dvert_array_from, &dvert_tot_from, FALSE);
331         ED_vgroup_give_parray(ob->data, &dvert_array, &dvert_tot, FALSE);
332
333         if ((dvert_array == NULL) && (dvert_array_from != NULL) && ED_vgroup_data_create(ob->data)) {
334                 ED_vgroup_give_parray(ob->data, &dvert_array, &dvert_tot, FALSE);
335                 new_vgroup = TRUE;
336         }
337
338         if (ob == ob_from || dvert_tot == 0 || (dvert_tot != dvert_tot_from) || dvert_array_from == NULL || dvert_array == NULL) {
339                 if (dvert_array) MEM_freeN(dvert_array);
340                 if (dvert_array_from) MEM_freeN(dvert_array_from);
341
342                 if (new_vgroup == TRUE) {
343                         /* free the newly added vgroup since it wasn't compatible */
344                         vgroup_delete_all(ob);
345                 }
346                 return 0;
347         }
348
349         /* do the copy */
350         BLI_freelistN(&ob->defbase);
351         BLI_duplicatelist(&ob->defbase, &ob_from->defbase);
352         ob->actdef = ob_from->actdef;
353
354         if (defbase_tot_from < defbase_tot) {
355                 /* correct vgroup indices because the number of vgroups is being reduced. */
356                 int *remap = MEM_mallocN(sizeof(int) * (defbase_tot + 1), __func__);
357                 for (i = 0; i <= defbase_tot_from; i++) remap[i] = i;
358                 for (; i <= defbase_tot; i++) remap[i] = 0;  /* can't use these, so disable */
359
360                 vgroup_remap_update_users(ob, remap);
361                 MEM_freeN(remap);
362         }
363
364         dvf = dvert_array_from;
365         dv = dvert_array;
366
367         for (i = 0; i < dvert_tot; i++, dvf++, dv++) {
368                 if ((*dv)->dw)
369                         MEM_freeN((*dv)->dw);
370
371                 *(*dv) = *(*dvf);
372
373                 if ((*dv)->dw)
374                         (*dv)->dw = MEM_dupallocN((*dv)->dw);
375         }
376
377         MEM_freeN(dvert_array);
378         MEM_freeN(dvert_array_from);
379
380         return 1;
381 }
382
383 /***********************Start weight transfer (WT)*********************************/
384
385 typedef enum WT_VertexGroupMode {
386         WT_REPLACE_ACTIVE_VERTEX_GROUP = 1,
387         WT_REPLACE_ALL_VERTEX_GROUPS = 2
388 } WT_VertexGroupMode;
389
390 typedef enum WT_Method {
391         WT_BY_INDEX = 1,
392         WT_BY_NEAREST_VERTEX = 2,
393         WT_BY_NEAREST_FACE = 3,
394         WT_BY_NEAREST_VERTEX_IN_FACE = 4
395 } WT_Method;
396
397 typedef enum WT_ReplaceMode {
398         WT_REPLACE_ALL_WEIGHTS = 1,
399         WT_REPLACE_EMPTY_WEIGHTS = 2
400 } WT_ReplaceMode;
401
402 static EnumPropertyItem WT_vertex_group_mode_item[] = {
403         {WT_REPLACE_ACTIVE_VERTEX_GROUP, "WT_REPLACE_ACTIVE_VERTEX_GROUP", 1, "Active", "Transfer active vertex group from selected to active mesh"},
404         {WT_REPLACE_ALL_VERTEX_GROUPS, "WT_REPLACE_ALL_VERTEX_GROUPS", 1, "All", "Transfer all vertex groups from selected to active mesh"},
405         {0, NULL, 0, NULL, NULL}
406 };
407
408 static EnumPropertyItem WT_method_item[] = {
409         {WT_BY_INDEX, "WT_BY_INDEX", 1, "Vertex index", "Copy for identical meshes"},
410         {WT_BY_NEAREST_VERTEX, "WT_BY_NEAREST_VERTEX", 1, "Nearest vertex", "Copy weight from closest vertex"},
411         {WT_BY_NEAREST_FACE, "WT_BY_NEAREST_FACE", 1, "Nearest face", "Barycentric interpolation from nearest face"},
412         {WT_BY_NEAREST_VERTEX_IN_FACE, "WT_BY_NEAREST_VERTEX_IN_FACE", 1, "Nearest vertex in face", "Copy weight from closest vertex in nearest face"},
413         {0, NULL, 0, NULL, NULL}
414 };
415
416 static EnumPropertyItem WT_replace_mode_item[] = {
417         {WT_REPLACE_ALL_WEIGHTS, "WT_REPLACE_ALL_WEIGHTS", 1, "All", "Overwrite all weights"},
418         {WT_REPLACE_EMPTY_WEIGHTS, "WT_REPLACE_EMPTY_WEIGHTS", 1, "Empty", "Add weights to vertices with no weight"},
419         {0, NULL, 0, NULL, NULL}
420 };
421
422 /*copy weight*/
423 static void vgroup_transfer_weight(float *r_weight_dst, const float weight_src, const WT_ReplaceMode replace_mode)
424 {
425         switch (replace_mode) {
426                 case WT_REPLACE_ALL_WEIGHTS:
427                         *r_weight_dst = weight_src;
428                         break;
429
430                 case WT_REPLACE_EMPTY_WEIGHTS:
431                         if (*r_weight_dst == 0.0f) {
432                                 *r_weight_dst = weight_src;
433                         }
434                         break;
435
436                 default:
437                         BLI_assert(0);
438                         break;
439         }
440 }
441
442 /* could be exposed externally */
443 static int ed_vgroup_transfer_weight(Object *ob_dst, Object *ob_src, bDeformGroup *dg_src, Scene *scene,
444                                      WT_Method method, WT_ReplaceMode replace_mode, wmOperator *op)
445 {
446         bDeformGroup *dg_dst;
447         Mesh *me_dst, *me_src;
448         DerivedMesh *dmesh_src;
449         BVHTreeFromMesh tree_mesh_vertices_src, tree_mesh_faces_src = {NULL};
450         MDeformVert **dv_array_src, **dv_array_dst, **dv_src, **dv_dst;
451         MVert *mv_dst, *mv_src;
452         MFace *mface_src, *mf;
453         BVHTreeNearest nearest;
454         MDeformWeight *dw_dst, *dw_src;
455         int dv_tot_src, dv_tot_dst, i, v_index, index_dst, index_src, index_nearest, index_nearest_vertex;
456         unsigned int f_index;
457         float weight, tmp_weight[4], tmp_co[3], normal[3], tmp_mat[4][4], dist_v1, dist_v2, dist_v3, dist_v4;
458         const int use_vert_sel = vertex_group_use_vert_sel(ob_dst);
459
460         /* create new and overwrite vertex group on destination without data */
461         if (!defgroup_find_name(ob_dst, dg_src->name)) {
462                 ED_vgroup_delete(ob_dst, defgroup_find_name(ob_dst, dg_src->name));
463                 ED_vgroup_add_name(ob_dst, dg_src->name);
464         }
465
466         /* get destination deformgroup */
467         dg_dst = defgroup_find_name(ob_dst, dg_src->name);
468
469         /* get meshes */
470         dmesh_src = mesh_get_derived_deform(scene, ob_src, CD_MASK_BAREMESH);
471         me_dst = ob_dst->data;
472         me_src = ob_src->data;
473
474         /* sanity check */
475         if (!me_src->dvert) {
476                 BKE_report(op->reports, RPT_ERROR, "Transfer failed (source mesh does not have any vertex groups)");
477                 return 0;
478         }
479
480         /* create data in memory when nothing there */
481         if (!me_dst->dvert) ED_vgroup_data_create(ob_dst->data);
482
483         /* get vertex group arrays */
484         ED_vgroup_give_parray(ob_src->data, &dv_array_src, &dv_tot_src, FALSE);
485         ED_vgroup_give_parray(ob_dst->data, &dv_array_dst, &dv_tot_dst, use_vert_sel);
486
487         /* get indexes of vertex groups */
488         index_src = BLI_findindex(&ob_src->defbase, dg_src);
489         index_dst = BLI_findindex(&ob_dst->defbase, dg_dst);
490
491         /* get vertices */
492         mv_dst = me_dst->mvert;
493         mv_src = dmesh_src->getVertArray(dmesh_src);
494
495         /* prepare transformation matrix */
496         invert_m4_m4(ob_src->imat, ob_src->obmat);
497         mult_m4_m4m4(tmp_mat, ob_src->imat, ob_dst->obmat);
498
499         /* clear weights */
500         if (replace_mode == WT_REPLACE_ALL_WEIGHTS) {
501                 for (i = 0, dv_dst = dv_array_dst; i < me_dst->totvert; i++, dv_dst++) {
502
503                         if (*dv_dst == NULL) continue;
504
505                         dw_dst = defvert_find_index(*dv_dst, index_dst);
506                         /* remove vertex from group */
507                         if (dw_dst) defvert_remove_group(*dv_dst, dw_dst);
508                 }
509         }
510
511         switch (method) {
512
513                 case WT_BY_INDEX:
514                         /* check if indices are matching, delete and return if not */
515                         if (ob_dst == ob_src || dv_tot_dst == 0 || dv_tot_dst != dv_tot_src ||
516                             dv_array_src == NULL || dv_array_dst == NULL)
517                         {
518                                 ED_vgroup_delete(ob_dst, defgroup_find_name(ob_dst, dg_dst->name));
519                                 if (dv_array_src) MEM_freeN(dv_array_src);
520                                 if (dv_array_dst) MEM_freeN(dv_array_dst);
521                                 dmesh_src->release(dmesh_src);
522                                 BKE_report(op->reports, RPT_ERROR, "Transfer failed (indices are not matching)");
523                                 return 0;
524                         }
525
526                         /* loop through the vertices*/
527                         for (i = 0, dv_src = dv_array_src, dv_dst = dv_array_dst; i < me_dst->totvert; i++, dv_dst++, dv_src++, mv_src++, mv_dst++) {
528
529                                 if (*dv_dst == NULL) {
530                                         continue;
531                                 }
532
533                                 /* copy weight */
534                                 dw_src = defvert_find_index(*dv_src, index_src);
535                                 if (dw_src && dw_src->weight) {
536                                         dw_dst = defvert_verify_index(*dv_dst, index_dst);
537                                         vgroup_transfer_weight(&dw_dst->weight, dw_src->weight, replace_mode);
538                                 }
539                         }
540                         break;
541
542                 case WT_BY_NEAREST_VERTEX:
543                         /* make node tree */
544                         bvhtree_from_mesh_verts(&tree_mesh_vertices_src, dmesh_src, FLT_EPSILON, 2, 6);
545
546                         /* loop trough vertices */
547                         for (i = 0, dv_dst = dv_array_dst; i < me_dst->totvert; i++, dv_dst++, mv_dst++) {
548
549                                 if (*dv_dst == NULL) {
550                                         continue;
551                                 }
552
553                                 /* reset nearest */
554                                 nearest.dist = FLT_MAX;
555                                 /* with current binary tree its marginally faster to start searching at the top, as opposed to previous search. */
556                                 nearest.index = -1;
557
558                                 /* transform into target space */
559                                 mul_v3_m4v3(tmp_co, tmp_mat, mv_dst->co);
560
561                                 /* node tree accelerated search for closest vetex */
562                                 BLI_bvhtree_find_nearest(tree_mesh_vertices_src.tree, tmp_co,
563                                                          &nearest, tree_mesh_vertices_src.nearest_callback, &tree_mesh_vertices_src);
564
565                                 /* copy weight that are not NULL including weight value 0. Existing target weights are overwritten prior to this in relevant cases. */
566                                 dw_src = defvert_find_index(dv_array_src[nearest.index], index_src);
567                                 if (dw_src && dw_src->weight) {
568                                         dw_dst = defvert_verify_index(*dv_dst, index_dst);
569                                         vgroup_transfer_weight(&dw_dst->weight, dw_src->weight, replace_mode);
570                                 }
571                         }
572
573                         /* free memory */
574                         free_bvhtree_from_mesh(&tree_mesh_vertices_src);
575                         break;
576
577                 case WT_BY_NEAREST_FACE:
578                         /* get faces */
579                         DM_ensure_tessface(dmesh_src);
580                         mface_src = dmesh_src->getTessFaceArray(dmesh_src);
581
582                         /* make node tree */
583                         bvhtree_from_mesh_faces(&tree_mesh_faces_src, dmesh_src, FLT_EPSILON, 2, 6);
584
585                         /* loop through the vertices */
586                         for (i = 0, dv_dst = dv_array_dst; i < me_dst->totvert; i++, dv_dst++, mv_dst++) {
587
588                                 if (*dv_dst == NULL) {
589                                         continue;
590                                 }
591
592                                 /* reset nearest */
593                                 nearest.dist = FLT_MAX;
594                                 /* with current binary tree its marginally faster to start searching at the top, as opposed to previous search. */
595                                 nearest.index = -1;
596
597                                 /* transform into target space */
598                                 mul_v3_m4v3(tmp_co, tmp_mat, mv_dst->co);
599
600                                 /* node tree accelerated search for closest face */
601                                 BLI_bvhtree_find_nearest(tree_mesh_faces_src.tree, tmp_co,
602                                                          &nearest, tree_mesh_faces_src.nearest_callback, &tree_mesh_faces_src);
603                                 index_nearest = nearest.index;
604
605                                 /* project onto face */
606                                 mf = &mface_src[index_nearest];
607                                 normal_tri_v3(normal, mv_src[mf->v1].co, mv_src[mf->v2].co, mv_src[mf->v3].co);
608                                 project_v3_plane(tmp_co, normal, mv_src[mf->v1].co);
609
610                                 /* interpolate weights over face*/
611                                 f_index = mf->v4 ? 3 : 2;
612                                 if (f_index == 3) {
613                                         interp_weights_face_v3(tmp_weight, mv_src[mf->v1].co, mv_src[mf->v2].co, mv_src[mf->v3].co, mv_src[mf->v4].co, tmp_co);
614                                 }
615                                 else {
616                                         interp_weights_face_v3(tmp_weight, mv_src[mf->v1].co, mv_src[mf->v2].co, mv_src[mf->v3].co, NULL, tmp_co);
617                                 }
618
619                                 /* get weights from face*/
620                                 weight = 0;
621                                 do {
622                                         v_index = (&mf->v1)[f_index];
623                                         weight += tmp_weight[f_index] * defvert_find_weight(dv_array_src[v_index], index_src);
624                                 } while (f_index--);
625
626                                 /* copy weight that are not NULL including weight value 0. Existing target weights are overwritten prior to this in relevant cases. */
627                                 if (weight > 0) {
628                                         dw_dst = defvert_verify_index(*dv_dst, index_dst);
629                                         vgroup_transfer_weight(&dw_dst->weight, weight, replace_mode);
630                                 }
631                         }
632
633                         /* free memory */
634                         free_bvhtree_from_mesh(&tree_mesh_faces_src);
635                         break;
636
637                 case WT_BY_NEAREST_VERTEX_IN_FACE:
638                         /* get faces */
639                         DM_ensure_tessface(dmesh_src);
640                         mface_src = dmesh_src->getTessFaceArray(dmesh_src);
641
642                         /* make node tree */
643                         bvhtree_from_mesh_faces(&tree_mesh_faces_src, dmesh_src, FLT_EPSILON, 2, 6);
644
645                         /* loop through the vertices */
646                         for (i = 0, dv_dst = dv_array_dst; i < me_dst->totvert; i++, dv_dst++, mv_dst++) {
647
648                                 if (*dv_dst == NULL) {
649                                         continue;
650                                 }
651
652                                 /* reset nearest */
653                                 nearest.dist = FLT_MAX;
654                                 /* With current binary tree its marginally faster to start searching at the top, as opposed to previous search. */
655                                 nearest.index = -1;
656
657                                 /* transform into target space */
658                                 mul_v3_m4v3(tmp_co, tmp_mat, mv_dst->co);
659
660                                 /* node tree accelerated search for closest face */
661                                 BLI_bvhtree_find_nearest(tree_mesh_faces_src.tree, tmp_co,
662                                                          &nearest, tree_mesh_faces_src.nearest_callback, &tree_mesh_faces_src);
663                                 index_nearest = nearest.index;
664
665                                 /* get distances */
666                                 mf = &mface_src[index_nearest];
667                                 dist_v1 = len_squared_v3v3(tmp_co, mv_src[mf->v1].co);
668                                 dist_v2 = len_squared_v3v3(tmp_co, mv_src[mf->v2].co);
669                                 dist_v3 = len_squared_v3v3(tmp_co, mv_src[mf->v3].co);
670
671                                 /* get closest vertex */
672                                 f_index = mf->v4 ? 3 : 2;
673                                 if (dist_v1 < dist_v2 && dist_v1 < dist_v3) index_nearest_vertex = mf->v1;
674                                 else if (dist_v2 < dist_v3) index_nearest_vertex = mf->v2;
675                                 else index_nearest_vertex = mf->v3;
676                                 if (f_index == 3) {
677                                         dist_v4 = len_squared_v3v3(tmp_co, mv_src[mf->v4].co);
678                                         if (dist_v4 < dist_v1 && dist_v4 < dist_v2 && dist_v4 < dist_v3) {
679                                                 index_nearest_vertex = mf->v4;
680                                         }
681                                 }
682
683                                 /* copy weight that are not NULL including weight value 0. Existing target weights are overwritten prior to this in relevant cases. */
684                                 dw_src = defvert_find_index(dv_array_src[index_nearest_vertex], index_src);
685                                 if (dw_src && dw_src->weight) {
686                                         dw_dst = defvert_verify_index(*dv_dst, index_dst);
687                                         vgroup_transfer_weight(&dw_dst->weight, dw_src->weight, replace_mode);
688                                 }
689                         }
690
691                         /* free memory */
692                         free_bvhtree_from_mesh(&tree_mesh_faces_src);
693                         break;
694
695                 default:
696                         BLI_assert(0);
697                         break;
698         }
699
700         /*free memory*/
701         if (dv_array_src) MEM_freeN(dv_array_src);
702         if (dv_array_dst) MEM_freeN(dv_array_dst);
703         dmesh_src->release(dmesh_src);
704
705         return 1;
706 }
707
708 /***********************End weight transfer (WT)***********************************/
709
710 /* for Mesh in Object mode */
711 /* allows editmode for Lattice */
712 static void ED_vgroup_nr_vert_add(Object *ob,
713                                   const int def_nr, const int vertnum,
714                                   const float weight, const int assignmode)
715 {
716         /* add the vert to the deform group with the
717          * specified number
718          */
719         MDeformVert *dvert = NULL;
720         int tot;
721
722         /* get the vert */
723         ED_vgroup_give_array(ob->data, &dvert, &tot);
724         
725         if (dvert == NULL)
726                 return;
727
728         /* check that vertnum is valid before trying to get the relevant dvert */
729         if ((vertnum < 0) || (vertnum >= tot))
730                 return;
731
732
733         if (dvert) {
734                 MDeformVert *dv = &dvert[vertnum];
735                 MDeformWeight *dw;
736
737                 /* Lets first check to see if this vert is
738                  * already in the weight group -- if so
739                  * lets update it
740                  */
741
742                 dw = defvert_find_index(dv, def_nr);
743
744                 if (dw) {
745                         switch (assignmode) {
746                                 case WEIGHT_REPLACE:
747                                         dw->weight = weight;
748                                         break;
749                                 case WEIGHT_ADD:
750                                         dw->weight += weight;
751                                         if (dw->weight >= 1.0f)
752                                                 dw->weight = 1.0f;
753                                         break;
754                                 case WEIGHT_SUBTRACT:
755                                         dw->weight -= weight;
756                                         /* if the weight is zero or less then
757                                          * remove the vert from the deform group
758                                          */
759                                         if (dw->weight <= 0.0f) {
760                                                 defvert_remove_group(dv, dw);
761                                         }
762                                         break;
763                         }
764                 }
765                 else {
766                         /* if the vert wasn't in the deform group then
767                          * we must take a different form of action ...
768                          */
769
770                         switch (assignmode) {
771                                 case WEIGHT_SUBTRACT:
772                                         /* if we are subtracting then we don't
773                                          * need to do anything
774                                          */
775                                         return;
776
777                                 case WEIGHT_REPLACE:
778                                 case WEIGHT_ADD:
779                                         /* if we are doing an additive assignment, then
780                                          * we need to create the deform weight
781                                          */
782
783                                         /* we checked if the vertex was added before so no need to test again, simply add */
784                                         defvert_add_index_notest(dv, def_nr, weight);
785                         }
786                 }
787         }
788 }
789
790 /* called while not in editmode */
791 void ED_vgroup_vert_add(Object *ob, bDeformGroup *dg, int vertnum, float weight, int assignmode)
792 {
793         /* add the vert to the deform group with the
794          * specified assign mode
795          */
796         const int def_nr = BLI_findindex(&ob->defbase, dg);
797
798         MDeformVert *dv = NULL;
799         int tot;
800
801         /* get the deform group number, exit if
802          * it can't be found
803          */
804         if (def_nr < 0) return;
805
806         /* if there's no deform verts then create some,
807          */
808         if (ED_vgroup_give_array(ob->data, &dv, &tot) && dv == NULL)
809                 ED_vgroup_data_create(ob->data);
810
811         /* call another function to do the work
812          */
813         ED_vgroup_nr_vert_add(ob, def_nr, vertnum, weight, assignmode);
814 }
815
816 /* mesh object mode, lattice can be in editmode */
817 void ED_vgroup_vert_remove(Object *ob, bDeformGroup *dg, int vertnum)
818 {
819         /* This routine removes the vertex from the specified
820          * deform group.
821          */
822
823         /* TODO, this is slow in a loop, better pass def_nr directly, but leave for later... - campbell */
824         const int def_nr = BLI_findindex(&ob->defbase, dg);
825
826         if (def_nr != -1) {
827                 MDeformVert *dvert = NULL;
828                 int tot;
829
830                 /* get the deform vertices corresponding to the
831                  * vertnum
832                  */
833                 ED_vgroup_give_array(ob->data, &dvert, &tot);
834
835                 if (dvert) {
836                         MDeformVert *dv = &dvert[vertnum];
837                         MDeformWeight *dw;
838
839                         dw = defvert_find_index(dv, def_nr);
840                         defvert_remove_group(dv, dw); /* dw can be NULL */
841                 }
842         }
843 }
844
845 static float get_vert_def_nr(Object *ob, const int def_nr, const int vertnum)
846 {
847         MDeformVert *dv = NULL;
848
849         /* get the deform vertices corresponding to the vertnum */
850         if (ob->type == OB_MESH) {
851                 Mesh *me = ob->data;
852
853                 if (me->edit_btmesh) {
854                         /* warning, this lookup is _not_ fast */
855                         BMVert *eve = BM_vert_at_index(me->edit_btmesh->bm, vertnum);
856                         if (!eve) {
857                                 return 0.0f;
858                         }
859                         dv = CustomData_bmesh_get(&me->edit_btmesh->bm->vdata, eve->head.data, CD_MDEFORMVERT);
860                 }
861                 else {
862                         if (me->dvert) {
863                                 if (vertnum >= me->totvert) {
864                                         return 0.0f;
865                                 }
866                                 dv = &me->dvert[vertnum];
867                         }
868                 }
869         }
870         else if (ob->type == OB_LATTICE) {
871                 Lattice *lt = vgroup_edit_lattice(ob);
872
873                 if (lt->dvert) {
874                         if (vertnum >= lt->pntsu * lt->pntsv * lt->pntsw) {
875                                 return 0.0f;
876                         }
877                         dv = &lt->dvert[vertnum];
878                 }
879         }
880         
881         if (dv) {
882                 MDeformWeight *dw = defvert_find_index(dv, def_nr);
883                 if (dw) {
884                         return dw->weight;
885                 }
886         }
887
888         return -1;
889 }
890
891 float ED_vgroup_vert_weight(Object *ob, bDeformGroup *dg, int vertnum)
892 {
893         const int def_nr = BLI_findindex(&ob->defbase, dg);
894
895         if (def_nr == -1) {
896                 return -1;
897         }
898
899         return get_vert_def_nr(ob, def_nr, vertnum);
900 }
901
902 void ED_vgroup_select_by_name(Object *ob, const char *name)
903 {   /* note: ob->actdef==0 signals on painting to create a new one, if a bone in posemode is selected */
904         ob->actdef = defgroup_name_index(ob, name) + 1;
905 }
906
907 /********************** Operator Implementations *********************/
908
909 /* only in editmode */
910 static void vgroup_select_verts(Object *ob, int select)
911 {
912         const int def_nr = ob->actdef - 1;
913
914         if (!BLI_findlink(&ob->defbase, def_nr)) {
915                 return;
916         }
917
918         if (ob->type == OB_MESH) {
919                 Mesh *me = ob->data;
920
921                 if (me->edit_btmesh) {
922                         BMEditMesh *em = me->edit_btmesh;
923                         BMIter iter;
924                         BMVert *eve;
925
926                         BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
927                                 if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN)) {
928                                         MDeformVert *dv = CustomData_bmesh_get(&em->bm->vdata, eve->head.data, CD_MDEFORMVERT);
929                                         if (defvert_find_index(dv, def_nr)) {
930                                                 BM_vert_select_set(em->bm, eve, select);
931                                         }
932                                 }
933                         }
934
935                         /* this has to be called, because this function operates on vertices only */
936                         if (select) EDBM_select_flush(em);  /* vertices to edges/faces */
937                         else EDBM_deselect_flush(em);
938                 }
939                 else {
940                         if (me->dvert) {
941                                 MVert *mv;
942                                 MDeformVert *dv;
943                                 int i;
944
945                                 mv = me->mvert;
946                                 dv = me->dvert;
947
948                                 for (i = 0; i < me->totvert; i++, mv++, dv++) {
949                                         if (!(mv->flag & ME_HIDE)) {
950                                                 if (defvert_find_index(dv, def_nr)) {
951                                                         if (select) mv->flag |=  SELECT;
952                                                         else mv->flag &= ~SELECT;
953                                                 }
954                                         }
955                                 }
956
957                                 paintvert_flush_flags(ob);
958                         }
959                 }
960         }
961         else if (ob->type == OB_LATTICE) {
962                 Lattice *lt = vgroup_edit_lattice(ob);
963                 
964                 if (lt->dvert) {
965                         MDeformVert *dv;
966                         BPoint *bp;
967                         int a, tot;
968                         
969                         dv = lt->dvert;
970
971                         tot = lt->pntsu * lt->pntsv * lt->pntsw;
972                         for (a = 0, bp = lt->def; a < tot; a++, bp++, dv++) {
973                                 if (defvert_find_index(dv, def_nr)) {
974                                         if (select) bp->f1 |=  SELECT;
975                                         else bp->f1 &= ~SELECT;
976                                 }
977                         }
978                 }
979         }
980 }
981
982 static void vgroup_duplicate(Object *ob)
983 {
984         bDeformGroup *dg, *cdg;
985         char name[sizeof(dg->name)];
986         MDeformWeight *dw_org, *dw_cpy;
987         MDeformVert **dvert_array = NULL;
988         int i, idg, icdg, dvert_tot = 0;
989
990         dg = BLI_findlink(&ob->defbase, (ob->actdef - 1));
991         if (!dg)
992                 return;
993         
994         if (!strstr(dg->name, "_copy")) {
995                 BLI_snprintf(name, sizeof(name), "%s_copy", dg->name);
996         }
997         else {
998                 BLI_snprintf(name, sizeof(name), "%s", dg->name);
999         }
1000
1001         cdg = defgroup_duplicate(dg);
1002         BLI_strncpy(cdg->name, name, sizeof(cdg->name));
1003         defgroup_unique_name(cdg, ob);
1004
1005         BLI_addtail(&ob->defbase, cdg);
1006
1007         idg = (ob->actdef - 1);
1008         ob->actdef = BLI_countlist(&ob->defbase);
1009         icdg = (ob->actdef - 1);
1010
1011         /* TODO, we might want to allow only copy selected verts here? - campbell */
1012         ED_vgroup_give_parray(ob->data, &dvert_array, &dvert_tot, FALSE);
1013
1014         if (dvert_array) {
1015                 for (i = 0; i < dvert_tot; i++) {
1016                         MDeformVert *dv = dvert_array[i];
1017                         dw_org = defvert_find_index(dv, idg);
1018                         if (dw_org) {
1019                                 /* defvert_verify_index re-allocs org so need to store the weight first */
1020                                 const float weight = dw_org->weight;
1021                                 dw_cpy = defvert_verify_index(dv, icdg);
1022                                 dw_cpy->weight = weight;
1023                         }
1024                 }
1025
1026                 MEM_freeN(dvert_array);
1027         }
1028 }
1029
1030 static void vgroup_normalize(Object *ob)
1031 {
1032         MDeformWeight *dw;
1033         MDeformVert *dv, **dvert_array = NULL;
1034         int i, dvert_tot = 0;
1035         const int def_nr = ob->actdef - 1;
1036
1037         const int use_vert_sel = vertex_group_use_vert_sel(ob);
1038
1039         if (!BLI_findlink(&ob->defbase, def_nr)) {
1040                 return;
1041         }
1042
1043         ED_vgroup_give_parray(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
1044
1045         if (dvert_array) {
1046                 float weight_max = 0.0f;
1047
1048                 for (i = 0; i < dvert_tot; i++) {
1049
1050                         /* in case its not selected */
1051                         if (!(dv = dvert_array[i])) {
1052                                 continue;
1053                         }
1054
1055                         dw = defvert_find_index(dv, def_nr);
1056                         if (dw) {
1057                                 weight_max = max_ff(dw->weight, weight_max);
1058                         }
1059                 }
1060
1061                 if (weight_max > 0.0f) {
1062                         for (i = 0; i < dvert_tot; i++) {
1063                                 
1064                                 /* in case its not selected */
1065                                 if (!(dv = dvert_array[i])) {
1066                                         continue;
1067                                 }
1068
1069                                 dw = defvert_find_index(dv, def_nr);
1070                                 if (dw) {
1071                                         dw->weight /= weight_max;
1072                                         
1073                                         /* in case of division errors with very low weights */
1074                                         CLAMP(dw->weight, 0.0f, 1.0f);
1075                                 }
1076                         }
1077                 }
1078
1079                 MEM_freeN(dvert_array);
1080         }
1081 }
1082
1083 /* This finds all of the vertices face-connected to vert by an edge and returns a
1084  * MEM_allocated array of indices of size count.
1085  * count is an int passed by reference so it can be assigned the value of the length here. */
1086 static int *getSurroundingVerts(Mesh *me, int vert, int *count)
1087 {
1088         MPoly *mp = me->mpoly;
1089         int i = me->totpoly;
1090         /* Instead of looping twice on all polys and loops, and use a temp array, let's rather
1091          * use a BLI_array, with a reasonable starting/reserved size (typically, there are not
1092          * many vertices face-linked to another one, even 8 might be too high...). */
1093         int *verts = NULL;
1094         BLI_array_declare(verts);
1095
1096         BLI_array_reserve(verts, 8);
1097         while (i--) {
1098                 int j = mp->totloop;
1099                 int first_l = mp->totloop - 1;
1100                 MLoop *ml = &me->mloop[mp->loopstart];
1101                 while (j--) {
1102                         /* XXX This assume a vert can only be once in a poly, even though
1103                          *     it seems logical to me, not totally sure of that. */
1104                         if (ml->v == vert) {
1105                                 int a, b, k;
1106                                 if (j == first_l) {
1107                                         /* We are on the first corner. */
1108                                         a = ml[1].v;
1109                                         b = ml[j].v;
1110                                 }
1111                                 else if (!j) {
1112                                         /* We are on the last corner. */
1113                                         a = (ml - 1)->v;
1114                                         b = me->mloop[mp->loopstart].v;
1115                                 }
1116                                 else {
1117                                         a = (ml - 1)->v;
1118                                         b = (ml + 1)->v;
1119                                 }
1120
1121                                 /* Append a and b verts to array, if not yet present. */
1122                                 k = BLI_array_count(verts);
1123                                 /* XXX Maybe a == b is enough? */
1124                                 while (k-- && !(a == b && a == -1)) {
1125                                         if (verts[k] == a)
1126                                                 a = -1;
1127                                         else if (verts[k] == b)
1128                                                 b = -1;
1129                                 }
1130                                 if (a != -1)
1131                                         BLI_array_append(verts, a);
1132                                 if (b != -1)
1133                                         BLI_array_append(verts, b);
1134
1135                                 /* Vert found in this poly, we can go to next one! */
1136                                 break;
1137                         }
1138                         ml++;
1139                 }
1140                 mp++;
1141         }
1142
1143         /* Do not free the array! */
1144         *count = BLI_array_count(verts);
1145         return verts;
1146 }
1147
1148 /* get a single point in space by averaging a point cloud (vectors of size 3)
1149  * coord is the place the average is stored, points is the point cloud, count is the number of points in the cloud
1150  */
1151 static void getSingleCoordinate(MVert *points, int count, float coord[3])
1152 {
1153         int i;
1154         zero_v3(coord);
1155         for (i = 0; i < count; i++) {
1156                 add_v3_v3(coord, points[i].co);
1157         }
1158         mul_v3_fl(coord, 1.0f / count);
1159 }
1160
1161 /* given a plane and a start and end position,
1162  * compute the amount of vertical distance relative to the plane and store it in dists,
1163  * then get the horizontal and vertical change and store them in changes
1164  */
1165 static void getVerticalAndHorizontalChange(const float norm[3], float d, const float coord[3],
1166                                            const float start[3], float distToStart,
1167                                            float *end, float (*changes)[2], float *dists, int index)
1168 {
1169         /* A = Q - ((Q - P).N)N
1170          * D = (a * x0 + b * y0 +c * z0 + d) */
1171         float projA[3], projB[3];
1172
1173         closest_to_plane_v3(projA, coord, norm, start);
1174         closest_to_plane_v3(projB, coord, norm, end);
1175         /* (vertical and horizontal refer to the plane's y and xz respectively)
1176          * vertical distance */
1177         dists[index] = dot_v3v3(norm, end) + d;
1178         /* vertical change */
1179         changes[index][0] = dists[index] - distToStart;
1180         //printf("vc %f %f\n", distance(end, projB, 3)-distance(start, projA, 3), changes[index][0]);
1181         /* horizontal change */
1182         changes[index][1] = len_v3v3(projA, projB);
1183 }
1184
1185 /* I need the derived mesh to be forgotten so the positions are recalculated
1186  * with weight changes (see dm_deform_recalc) */
1187 static void dm_deform_clear(DerivedMesh *dm, Object *ob)
1188 {
1189         if (ob->derivedDeform && (ob->derivedDeform) == dm) {
1190                 ob->derivedDeform->needsFree = 1;
1191                 ob->derivedDeform->release(ob->derivedDeform);
1192                 ob->derivedDeform = NULL;
1193         }
1194         else if (dm) {
1195                 dm->needsFree = 1;
1196                 dm->release(dm);
1197         }
1198 }
1199
1200 /* recalculate the deformation */
1201 static DerivedMesh *dm_deform_recalc(Scene *scene, Object *ob)
1202 {
1203         return mesh_get_derived_deform(scene, ob, CD_MASK_BAREMESH);
1204 }
1205
1206 /* by changing nonzero weights, try to move a vertex in me->mverts with index 'index' to
1207  * distToBe distance away from the provided plane strength can change distToBe so that it moves
1208  * towards distToBe by that percentage cp changes how much the weights are adjusted
1209  * to check the distance
1210  *
1211  * index is the index of the vertex being moved
1212  * norm and d are the plane's properties for the equation: ax + by + cz + d = 0
1213  * coord is a point on the plane
1214  */
1215 static void moveCloserToDistanceFromPlane(Scene *scene, Object *ob, Mesh *me, int index, float norm[3],
1216                                           float coord[3], float d, float distToBe, float strength, float cp)
1217 {
1218         DerivedMesh *dm;
1219         MDeformWeight *dw;
1220         MVert m;
1221         MDeformVert *dvert = me->dvert + index;
1222         int totweight = dvert->totweight;
1223         float oldw = 0;
1224         float oldPos[3] = {0};
1225         float vc, hc, dist = 0.0f;
1226         int i, k;
1227         float (*changes)[2] = MEM_mallocN(sizeof(float *) * totweight * 2, "vertHorzChange");
1228         float *dists = MEM_mallocN(sizeof(float) * totweight, "distance");
1229
1230         /* track if up or down moved it closer for each bone */
1231         int *upDown = MEM_callocN(sizeof(int) * totweight, "upDownTracker");
1232
1233         int *dwIndices = MEM_callocN(sizeof(int) * totweight, "dwIndexTracker");
1234         float distToStart;
1235         int bestIndex = 0;
1236         char wasChange;
1237         char wasUp;
1238         int lastIndex = -1;
1239         float originalDistToBe = distToBe;
1240         do {
1241                 wasChange = FALSE;
1242                 dm = dm_deform_recalc(scene, ob);
1243                 dm->getVert(dm, index, &m);
1244                 copy_v3_v3(oldPos, m.co);
1245                 distToStart = dot_v3v3(norm, oldPos) + d;
1246
1247                 if (distToBe == originalDistToBe) {
1248                         distToBe += distToStart - distToStart * strength;
1249                 }
1250                 for (i = 0; i < totweight; i++) {
1251                         dwIndices[i] = i;
1252                         dw = (dvert->dw + i);
1253                         vc = hc = 0;
1254                         if (!dw->weight) {
1255                                 changes[i][0] = 0;
1256                                 changes[i][1] = 0;
1257                                 dists[i] = distToStart;
1258                                 continue;
1259                         }
1260                         for (k = 0; k < 2; k++) {
1261                                 if (dm) {
1262                                         dm_deform_clear(dm, ob); dm = NULL;
1263                                 }
1264                                 oldw = dw->weight;
1265                                 if (k) {
1266                                         dw->weight *= 1 + cp;
1267                                 }
1268                                 else {
1269                                         dw->weight /= 1 + cp;
1270                                 }
1271                                 if (dw->weight == oldw) {
1272                                         changes[i][0] = 0;
1273                                         changes[i][1] = 0;
1274                                         dists[i] = distToStart;
1275                                         break;
1276                                 }
1277                                 if (dw->weight > 1) {
1278                                         dw->weight = 1;
1279                                 }
1280                                 dm = dm_deform_recalc(scene, ob);
1281                                 dm->getVert(dm, index, &m);
1282                                 getVerticalAndHorizontalChange(norm, d, coord, oldPos, distToStart, m.co, changes, dists, i);
1283                                 dw->weight = oldw;
1284                                 if (!k) {
1285                                         vc = changes[i][0];
1286                                         hc = changes[i][1];
1287                                         dist = dists[i];
1288                                 }
1289                                 else {
1290                                         if (fabsf(dist - distToBe) < fabsf(dists[i] - distToBe)) {
1291                                                 upDown[i] = 0;
1292                                                 changes[i][0] = vc;
1293                                                 changes[i][1] = hc;
1294                                                 dists[i] = dist;
1295                                         }
1296                                         else {
1297                                                 upDown[i] = 1;
1298                                         }
1299                                         if (fabsf(dists[i] - distToBe) > fabsf(distToStart - distToBe)) {
1300                                                 changes[i][0] = 0;
1301                                                 changes[i][1] = 0;
1302                                                 dists[i] = distToStart;
1303                                         }
1304                                 }
1305                         }
1306                 }
1307                 /* sort the changes by the vertical change */
1308                 for (k = 0; k < totweight; k++) {
1309                         float tf;
1310                         int ti;
1311                         bestIndex = k;
1312                         for (i = k + 1; i < totweight; i++) {
1313                                 dist = dists[i];
1314
1315                                 if (fabsf(dist) > fabsf(dists[i])) {
1316                                         bestIndex = i;
1317                                 }
1318                         }
1319                         /* switch with k */
1320                         if (bestIndex != k) {
1321                                 ti = upDown[k];
1322                                 upDown[k] = upDown[bestIndex];
1323                                 upDown[bestIndex] = ti;
1324
1325                                 ti = dwIndices[k];
1326                                 dwIndices[k] = dwIndices[bestIndex];
1327                                 dwIndices[bestIndex] = ti;
1328
1329                                 tf = changes[k][0];
1330                                 changes[k][0] = changes[bestIndex][0];
1331                                 changes[bestIndex][0] = tf;
1332
1333                                 tf = changes[k][1];
1334                                 changes[k][1] = changes[bestIndex][1];
1335                                 changes[bestIndex][1] = tf;
1336
1337                                 tf = dists[k];
1338                                 dists[k] = dists[bestIndex];
1339                                 dists[bestIndex] = tf;
1340                         }
1341                 }
1342                 bestIndex = -1;
1343                 /* find the best change with an acceptable horizontal change */
1344                 for (i = 0; i < totweight; i++) {
1345                         if (fabsf(changes[i][0]) > fabsf(changes[i][1] * 2.0f)) {
1346                                 bestIndex = i;
1347                                 break;
1348                         }
1349                 }
1350                 if (bestIndex != -1) {
1351                         wasChange = TRUE;
1352                         /* it is a good place to stop if it tries to move the opposite direction
1353                          * (relative to the plane) of last time */
1354                         if (lastIndex != -1) {
1355                                 if (wasUp != upDown[bestIndex]) {
1356                                         wasChange = FALSE;
1357                                 }
1358                         }
1359                         lastIndex = bestIndex;
1360                         wasUp = upDown[bestIndex];
1361                         dw = (dvert->dw + dwIndices[bestIndex]);
1362                         oldw = dw->weight;
1363                         if (upDown[bestIndex]) {
1364                                 dw->weight *= 1 + cp;
1365                         }
1366                         else {
1367                                 dw->weight /= 1 + cp;
1368                         }
1369                         if (dw->weight > 1) {
1370                                 dw->weight = 1;
1371                         }
1372                         if (oldw == dw->weight) {
1373                                 wasChange = FALSE;
1374                         }
1375                         if (dm) {
1376                                 dm_deform_clear(dm, ob); dm = NULL;
1377                         }
1378                 }
1379         } while (wasChange && ((distToStart - distToBe) / fabsf(distToStart - distToBe) ==
1380                                (dists[bestIndex] - distToBe) / fabsf(dists[bestIndex] - distToBe)));
1381
1382         MEM_freeN(upDown);
1383         MEM_freeN(changes);
1384         MEM_freeN(dists);
1385         MEM_freeN(dwIndices);
1386 }
1387
1388 /* this is used to try to smooth a surface by only adjusting the nonzero weights of a vertex 
1389  * but it could be used to raise or lower an existing 'bump.' */
1390 static void vgroup_fix(Scene *scene, Object *ob, float distToBe, float strength, float cp)
1391 {
1392         int i;
1393
1394         Mesh *me = ob->data;
1395         MVert *mvert = me->mvert;
1396         int *verts = NULL;
1397         if (!(me->editflag & ME_EDIT_VERT_SEL))
1398                 return;
1399         for (i = 0; i < me->totvert && mvert; i++, mvert++) {
1400                 if (mvert->flag & SELECT) {
1401                         int count = 0;
1402                         if ((verts = getSurroundingVerts(me, i, &count))) {
1403                                 MVert m;
1404                                 MVert *p = MEM_callocN(sizeof(MVert) * (count), "deformedPoints");
1405                                 int k;
1406
1407                                 DerivedMesh *dm = mesh_get_derived_deform(scene, ob, CD_MASK_BAREMESH);
1408                                 k = count;
1409                                 while (k--) {
1410                                         dm->getVert(dm, verts[k], &m);
1411                                         p[k] = m;
1412                                 }
1413                                 
1414                                 if (count >= 3) {
1415                                         float d /*, dist */ /* UNUSED */, mag;
1416                                         float coord[3];
1417                                         float norm[3];
1418                                         getSingleCoordinate(p, count, coord);
1419                                         dm->getVert(dm, i, &m);
1420                                         sub_v3_v3v3(norm, m.co, coord);
1421                                         mag = normalize_v3(norm);
1422                                         if (mag) { /* zeros fix */
1423                                                 d = -dot_v3v3(norm, coord);
1424                                                 /* dist = (dot_v3v3(norm, m.co) + d); */ /* UNUSED */
1425                                                 moveCloserToDistanceFromPlane(scene, ob, me, i, norm, coord, d, distToBe, strength, cp);
1426                                         }
1427                                 }
1428
1429                                 MEM_freeN(verts);
1430                                 MEM_freeN(p);
1431                         }
1432                 }
1433         }
1434 }
1435
1436 static void vgroup_levels(Object *ob, float offset, float gain)
1437 {
1438         MDeformWeight *dw;
1439         MDeformVert *dv, **dvert_array = NULL;
1440         int i, dvert_tot = 0;
1441         const int def_nr = ob->actdef - 1;
1442
1443         const int use_vert_sel = vertex_group_use_vert_sel(ob);
1444
1445         if (!BLI_findlink(&ob->defbase, def_nr)) {
1446                 return;
1447         }
1448
1449         ED_vgroup_give_parray(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
1450
1451         if (dvert_array) {
1452                 for (i = 0; i < dvert_tot; i++) {
1453
1454                         /* in case its not selected */
1455                         if (!(dv = dvert_array[i])) {
1456                                 continue;
1457                         }
1458
1459                         dw = defvert_find_index(dv, def_nr);
1460                         if (dw) {
1461                                 dw->weight = gain * (dw->weight + offset);
1462
1463                                 CLAMP(dw->weight, 0.0f, 1.0f);
1464                         }
1465                 }
1466
1467                 MEM_freeN(dvert_array);
1468         }
1469 }
1470
1471 static void vgroup_normalize_all(Object *ob, int lock_active)
1472 {
1473         MDeformVert *dv, **dvert_array = NULL;
1474         int i, dvert_tot = 0;
1475         const int def_nr = ob->actdef - 1;
1476
1477         const int use_vert_sel = vertex_group_use_vert_sel(ob);
1478
1479         if (lock_active && !BLI_findlink(&ob->defbase, def_nr)) {
1480                 return;
1481         }
1482
1483         ED_vgroup_give_parray(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
1484
1485         if (dvert_array) {
1486                 const int defbase_tot = BLI_countlist(&ob->defbase);
1487                 char *lock_flags = BKE_objdef_lock_flags_get(ob, defbase_tot);
1488
1489                 if ((lock_active == TRUE) &&
1490                     (lock_flags != NULL) &&
1491                     (def_nr < defbase_tot))
1492                 {
1493                         lock_flags[def_nr] = TRUE;
1494                 }
1495
1496                 for (i = 0; i < dvert_tot; i++) {
1497                         /* in case its not selected */
1498                         if ((dv = dvert_array[i])) {
1499                                 if (lock_flags) {
1500                                         defvert_normalize_lock_map(dv, lock_flags, defbase_tot);
1501                                 }
1502                                 else if (lock_active) {
1503                                         defvert_normalize_lock_single(dv, def_nr);
1504                                 }
1505                                 else {
1506                                         defvert_normalize(dv);
1507                                 }
1508                         }
1509                 }
1510
1511                 if (lock_flags) {
1512                         MEM_freeN(lock_flags);
1513                 }
1514
1515                 MEM_freeN(dvert_array);
1516         }
1517 }
1518
1519
1520 static void vgroup_lock_all(Object *ob, int action)
1521 {
1522         bDeformGroup *dg;
1523
1524         if (action == SEL_TOGGLE) {
1525                 action = SEL_SELECT;
1526                 for (dg = ob->defbase.first; dg; dg = dg->next) {
1527                         if (dg->flag & DG_LOCK_WEIGHT) {
1528                                 action = SEL_DESELECT;
1529                                 break;
1530                         }
1531                 }
1532         }
1533
1534         for (dg = ob->defbase.first; dg; dg = dg->next) {
1535                 switch (action) {
1536                         case SEL_SELECT:
1537                                 dg->flag |= DG_LOCK_WEIGHT;
1538                                 break;
1539                         case SEL_DESELECT:
1540                                 dg->flag &= ~DG_LOCK_WEIGHT;
1541                                 break;
1542                         case SEL_INVERT:
1543                                 dg->flag ^= DG_LOCK_WEIGHT;
1544                                 break;
1545                 }
1546         }
1547 }
1548
1549 static void vgroup_invert(Object *ob, const short auto_assign, const short auto_remove)
1550 {
1551         MDeformWeight *dw;
1552         MDeformVert *dv, **dvert_array = NULL;
1553         int i, dvert_tot = 0;
1554         const int def_nr = ob->actdef - 1;
1555         const int use_vert_sel = vertex_group_use_vert_sel(ob);
1556
1557         if (!BLI_findlink(&ob->defbase, def_nr)) {
1558                 return;
1559         }
1560
1561         ED_vgroup_give_parray(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
1562
1563         if (dvert_array) {
1564                 for (i = 0; i < dvert_tot; i++) {
1565
1566                         /* in case its not selected */
1567                         if (!(dv = dvert_array[i])) {
1568                                 continue;
1569                         }
1570
1571                         if (auto_assign) {
1572                                 dw = defvert_verify_index(dv, def_nr);
1573                         }
1574                         else {
1575                                 dw = defvert_find_index(dv, def_nr);
1576                         }
1577
1578                         if (dw) {
1579                                 dw->weight = 1.0f - dw->weight;
1580
1581                                 if (auto_remove && dw->weight <= 0.0f) {
1582                                         defvert_remove_group(dv, dw);
1583                                 }
1584                         }
1585                 }
1586
1587                 MEM_freeN(dvert_array);
1588         }
1589 }
1590
1591 static void vgroup_blend(Object *ob, const float fac)
1592 {
1593         MDeformVert *dv;
1594         MDeformWeight *dw;
1595         int i, dvert_tot = 0;
1596         const int def_nr = ob->actdef - 1;
1597
1598         BLI_assert(fac >= 0.0f && fac <= 1.0f);
1599
1600         if (ob->type != OB_MESH) {
1601                 return;
1602         }
1603
1604         if (BLI_findlink(&ob->defbase, def_nr)) {
1605                 const float ifac = 1.0f - fac;
1606
1607                 BMEditMesh *em = BMEdit_FromObject(ob);
1608                 BMesh *bm = em ? em->bm : NULL;
1609                 Mesh  *me = em ? NULL   : ob->data;
1610
1611                 /* bmesh only*/
1612                 BMEdge *eed;
1613                 BMVert *eve;
1614                 BMIter iter;
1615
1616                 /* mesh only */
1617                 MDeformVert *dvert_array = NULL;
1618
1619
1620                 float *vg_weights;
1621                 float *vg_users;
1622                 int sel1, sel2;
1623
1624                 if (bm) {
1625                         BM_mesh_elem_index_ensure(bm, BM_VERT);
1626                         dvert_tot = bm->totvert;
1627                 }
1628                 else {
1629                         dvert_tot = me->totvert;
1630                         dvert_array = me->dvert;
1631                 }
1632
1633                 vg_weights = MEM_callocN(sizeof(float) * dvert_tot, "vgroup_blend_f");
1634                 vg_users = MEM_callocN(sizeof(int) * dvert_tot, "vgroup_blend_i");
1635
1636                 if (bm) {
1637                         BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
1638                                 sel1 = BM_elem_flag_test(eed->v1, BM_ELEM_SELECT);
1639                                 sel2 = BM_elem_flag_test(eed->v2, BM_ELEM_SELECT);
1640
1641                                 if (sel1 != sel2) {
1642                                         int i1 /* , i2 */;
1643                                         /* i1 is always the selected one */
1644                                         if (sel1) {
1645                                                 i1 = BM_elem_index_get(eed->v1);
1646                                                 /* i2 = BM_elem_index_get(eed->v2); */ /* UNUSED */
1647                                                 eve = eed->v2;
1648                                         }
1649                                         else {
1650                                                 /* i2 = BM_elem_index_get(eed->v1); */ /* UNUSED */
1651                                                 i1 = BM_elem_index_get(eed->v2);
1652                                                 eve = eed->v1;
1653                                         }
1654
1655                                         dv = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_MDEFORMVERT);
1656                                         dw = defvert_find_index(dv, def_nr);
1657                                         if (dw) {
1658                                                 vg_weights[i1] += dw->weight;
1659                                         }
1660                                         vg_users[i1]++;
1661                                 }
1662                         }
1663
1664                         BM_ITER_MESH_INDEX (eve, &iter, bm, BM_VERTS_OF_MESH, i) {
1665                                 if (BM_elem_flag_test(eve, BM_ELEM_SELECT) && vg_users[i] > 0) {
1666                                         dv = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_MDEFORMVERT);
1667
1668                                         dw = defvert_verify_index(dv, def_nr);
1669                                         dw->weight = (fac * (vg_weights[i] / (float)vg_users[i])) + (ifac * dw->weight);
1670                                         /* in case of division errors */
1671                                         CLAMP(dw->weight, 0.0f, 1.0f);
1672                                 }
1673                         }
1674                 }
1675                 else {
1676                         MEdge *ed = me->medge;
1677                         MVert *mv;
1678
1679                         for (i = 0; i < me->totedge; i++, ed++) {
1680                                 sel1 = me->mvert[ed->v1].flag & SELECT;
1681                                 sel2 = me->mvert[ed->v2].flag & SELECT;
1682
1683                                 if (sel1 != sel2) {
1684                                         int i1, i2;
1685                                         /* i1 is always the selected one */
1686                                         if (sel1) {
1687                                                 i1 = ed->v1;
1688                                                 i2 = ed->v2;
1689                                         }
1690                                         else {
1691                                                 i2 = ed->v1;
1692                                                 i1 = ed->v2;
1693                                         }
1694
1695                                         dv = &dvert_array[i2];
1696                                         dw = defvert_find_index(dv, def_nr);
1697                                         if (dw) {
1698                                                 vg_weights[i1] += dw->weight;
1699                                         }
1700                                         vg_users[i1]++;
1701                                 }
1702                         }
1703
1704                         mv = me->mvert;
1705                         dv = dvert_array;
1706
1707                         for (i = 0; i < dvert_tot; i++, mv++, dv++) {
1708                                 if ((mv->flag & SELECT) && (vg_users[i] > 0)) {
1709                                         dw = defvert_verify_index(dv, def_nr);
1710                                         dw->weight = (fac * (vg_weights[i] / (float)vg_users[i])) + (ifac * dw->weight);
1711
1712                                         /* in case of division errors */
1713                                         CLAMP(dw->weight, 0.0f, 1.0f);
1714                                 }
1715                         }
1716                 }
1717
1718                 MEM_freeN(vg_weights);
1719                 MEM_freeN(vg_users);
1720         }
1721 }
1722
1723 static int inv_cmp_mdef_vert_weights(const void *a1, const void *a2)
1724 {
1725         /* qsort sorts in ascending order.  We want descending order to save a memcopy
1726          * so this compare function is inverted from the standard greater than comparison qsort needs.
1727          * A normal compare function is called with two pointer arguments and should return an integer less than, equal to,
1728          * or greater than zero corresponding to whether its first argument is considered less than, equal to,
1729          * or greater than its second argument.  This does the opposite. */
1730         const struct MDeformWeight *dw1 = a1, *dw2 = a2;
1731
1732         if      (dw1->weight < dw2->weight) return  1;
1733         else if (dw1->weight > dw2->weight) return -1;
1734         else if (&dw1 < &dw2)               return  1; /* compare addresses so we have a stable sort algorithm */
1735         else                                return -1;
1736 }
1737
1738 /* Used for limiting the number of influencing bones per vertex when exporting
1739  * skinned meshes.  if all_deform_weights is True, limit all deform modifiers
1740  * to max_weights regardless of type, otherwise, only limit the number of influencing bones per vertex*/
1741 static int vertex_group_limit_total(Object *ob,
1742                                     const int max_weights,
1743                                     const int all_deform_weights)
1744 {
1745         MDeformVert *dv, **dvert_array = NULL;
1746         int i, dvert_tot = 0;
1747         const int use_vert_sel = vertex_group_use_vert_sel(ob);
1748         int is_change = FALSE;
1749
1750         ED_vgroup_give_parray(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
1751
1752         if (dvert_array) {
1753                 int defbase_tot = BLI_countlist(&ob->defbase);
1754                 const char *vgroup_validmap = (all_deform_weights == FALSE) ?
1755                             BKE_objdef_validmap_get(ob, defbase_tot) :
1756                             NULL;
1757                 int num_to_drop = 0;
1758
1759                 /* only the active group */
1760                 for (i = 0; i < dvert_tot; i++) {
1761
1762                         /* in case its not selected */
1763                         if (!(dv = dvert_array[i])) {
1764                                 continue;
1765                         }
1766
1767                         if (all_deform_weights) {
1768                                 /* keep only the largest weights, discarding the rest
1769                                  * qsort will put array in descending order because of  invCompare function */
1770                                 num_to_drop = dv->totweight - max_weights;
1771                                 if (num_to_drop > 0) {
1772                                         qsort(dv->dw, dv->totweight, sizeof(MDeformWeight), inv_cmp_mdef_vert_weights);
1773                                         dv->dw = MEM_reallocN(dv->dw, sizeof(MDeformWeight) * max_weights);
1774                                         dv->totweight = max_weights;
1775                                         is_change = TRUE;
1776                                 }
1777                         }
1778                         else {
1779                                 MDeformWeight *dw_temp;
1780                                 int bone_count = 0, non_bone_count = 0;
1781                                 int j;
1782                                 /* only consider vgroups with bone modifiers attached (in vgroup_validmap) */
1783
1784                                 num_to_drop = dv->totweight - max_weights;
1785
1786                                 /* first check if we even need to test further */
1787                                 if (num_to_drop > 0) {
1788                                         /* re-pack dw array so that non-bone weights are first, bone-weighted verts at end
1789                                          * sort the tail, then copy only the truncated array back to dv->dw */
1790                                         dw_temp = MEM_mallocN(sizeof(MDeformWeight) * (dv->totweight), __func__);
1791                                         bone_count = 0; non_bone_count = 0;
1792                                         for (j = 0; j < dv->totweight; j++) {
1793                                                 BLI_assert(dv->dw[j].def_nr < defbase_tot);
1794                                                 if (!vgroup_validmap[(dv->dw[j]).def_nr]) {
1795                                                         dw_temp[non_bone_count] = dv->dw[j];
1796                                                         non_bone_count += 1;
1797                                                 }
1798                                                 else {
1799                                                         dw_temp[dv->totweight - 1 - bone_count] = dv->dw[j];
1800                                                         bone_count += 1;
1801                                                 }
1802                                         }
1803                                         BLI_assert(bone_count + non_bone_count == dv->totweight);
1804                                         num_to_drop = bone_count - max_weights;
1805                                         if (num_to_drop > 0) {
1806                                                 qsort(&dw_temp[non_bone_count], bone_count, sizeof(MDeformWeight), inv_cmp_mdef_vert_weights);
1807                                                 dv->totweight -= num_to_drop;
1808                                                 /* Do we want to clean/normalize here? */
1809                                                 MEM_freeN(dv->dw);
1810                                                 dv->dw = MEM_reallocN(dw_temp, sizeof(MDeformWeight) * dv->totweight);
1811                                                 is_change = TRUE;
1812                                         }
1813                                         else {
1814                                                 MEM_freeN(dw_temp);
1815                                         }
1816                                 }
1817                         }
1818                 }
1819                 MEM_freeN(dvert_array);
1820
1821                 if (vgroup_validmap) {
1822                         MEM_freeN((void *)vgroup_validmap);
1823                 }
1824         }
1825
1826         return is_change;
1827 }
1828
1829 static void vgroup_clean(Object *ob, const float epsilon, int keep_single)
1830 {
1831         MDeformWeight *dw;
1832         MDeformVert *dv, **dvert_array = NULL;
1833         int i, dvert_tot = 0;
1834         const int def_nr = ob->actdef - 1;
1835         const int use_vert_sel = vertex_group_use_vert_sel(ob);
1836
1837         if (!BLI_findlink(&ob->defbase, def_nr)) {
1838                 return;
1839         }
1840
1841         ED_vgroup_give_parray(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
1842
1843         if (dvert_array) {
1844                 /* only the active group */
1845                 for (i = 0; i < dvert_tot; i++) {
1846
1847                         /* in case its not selected */
1848                         if (!(dv = dvert_array[i])) {
1849                                 continue;
1850                         }
1851
1852                         dw = defvert_find_index(dv, def_nr);
1853
1854                         if (dw) {
1855                                 if (dw->weight <= epsilon) {
1856                                         if (keep_single == FALSE || dv->totweight > 1) {
1857                                                 defvert_remove_group(dv, dw); /* dw can be NULL */
1858                                         }
1859                                 }
1860                         }
1861                 }
1862
1863                 MEM_freeN(dvert_array);
1864         }
1865 }
1866
1867 static void vgroup_clean_all(Object *ob, const float epsilon, const int keep_single)
1868 {
1869         MDeformVert **dvert_array = NULL;
1870         int i, dvert_tot = 0;
1871         const int use_vert_sel = vertex_group_use_vert_sel(ob);
1872
1873         ED_vgroup_give_parray(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
1874
1875         if (dvert_array) {
1876                 MDeformVert *dv;
1877                 MDeformWeight *dw;
1878
1879                 for (i = 0; i < dvert_tot; i++) {
1880                         int j;
1881
1882                         /* in case its not selected */
1883                         if (!(dv = dvert_array[i])) {
1884                                 continue;
1885                         }
1886
1887                         j = dv->totweight;
1888
1889                         while (j--) {
1890
1891                                 if (keep_single && dv->totweight == 1)
1892                                         break;
1893
1894                                 dw = dv->dw + j;
1895
1896                                 if (dw->weight <= epsilon) {
1897                                         defvert_remove_group(dv, dw);
1898                                 }
1899                         }
1900                 }
1901
1902                 MEM_freeN(dvert_array);
1903         }
1904 }
1905
1906
1907 static void dvert_mirror_op(MDeformVert *dvert, MDeformVert *dvert_mirr,
1908                             const char sel, const char sel_mirr,
1909                             const int *flip_map, const int flip_map_len,
1910                             const short mirror_weights, const short flip_vgroups,
1911                             const short all_vgroups, const int act_vgroup)
1912 {
1913         BLI_assert(sel || sel_mirr);
1914
1915         if (sel_mirr && sel) {
1916                 /* swap */
1917                 if (mirror_weights) {
1918                         if (all_vgroups) {
1919                                 SWAP(MDeformVert, *dvert, *dvert_mirr);
1920                         }
1921                         else {
1922                                 MDeformWeight *dw =      defvert_find_index(dvert, act_vgroup);
1923                                 MDeformWeight *dw_mirr = defvert_find_index(dvert_mirr, act_vgroup);
1924
1925                                 if (dw || dw_mirr) {
1926                                         if (dw_mirr == NULL)
1927                                                 dw_mirr = defvert_verify_index(dvert_mirr, act_vgroup);
1928                                         if (dw == NULL)
1929                                                 dw = defvert_verify_index(dvert, act_vgroup);
1930
1931                                         SWAP(float, dw->weight, dw_mirr->weight);
1932                                 }
1933                         }
1934                 }
1935
1936                 if (flip_vgroups) {
1937                         defvert_flip(dvert, flip_map, flip_map_len);
1938                         defvert_flip(dvert_mirr, flip_map, flip_map_len);
1939                 }
1940         }
1941         else {
1942                 /* dvert should always be the target, only swaps pointer */
1943                 if (sel_mirr) {
1944                         SWAP(MDeformVert *, dvert, dvert_mirr);
1945                 }
1946
1947                 if (mirror_weights) {
1948                         if (all_vgroups) {
1949                                 defvert_copy(dvert, dvert_mirr);
1950                         }
1951                         else {
1952                                 defvert_copy_index(dvert, dvert_mirr, act_vgroup);
1953                         }
1954                 }
1955
1956                 /* flip map already modified for 'all_vgroups' */
1957                 if (flip_vgroups) {
1958                         defvert_flip(dvert, flip_map, flip_map_len);
1959                 }
1960         }
1961 }
1962
1963 /* TODO, vgroup locking */
1964 /* TODO, face masking */
1965 void ED_vgroup_mirror(Object *ob, const short mirror_weights, const short flip_vgroups, const short all_vgroups)
1966 {
1967
1968 #define VGROUP_MIRR_OP                                                        \
1969         dvert_mirror_op(dvert, dvert_mirr,                                    \
1970                         sel, sel_mirr,                                        \
1971                         flip_map, flip_map_len,                               \
1972                         mirror_weights, flip_vgroups,                         \
1973                         all_vgroups, def_nr                                   \
1974                         )
1975
1976         BMVert *eve, *eve_mirr;
1977         MDeformVert *dvert, *dvert_mirr;
1978         short sel, sel_mirr;
1979         int *flip_map, flip_map_len;
1980         const int def_nr = ob->actdef - 1;
1981
1982         if ((mirror_weights == 0 && flip_vgroups == 0) ||
1983             (BLI_findlink(&ob->defbase, def_nr) == NULL))
1984         {
1985                 return;
1986         }
1987
1988         if (flip_vgroups) {
1989                 flip_map = all_vgroups ?
1990                            defgroup_flip_map(ob, &flip_map_len, FALSE) :
1991                            defgroup_flip_map_single(ob, &flip_map_len, FALSE, def_nr);
1992
1993                 BLI_assert(flip_map != NULL);
1994
1995                 if (flip_map == NULL) {
1996                         /* something went wrong!, possibly no groups */
1997                         return;
1998                 }
1999         }
2000         else {
2001                 flip_map = NULL;
2002                 flip_map_len = 0;
2003         }
2004
2005         /* only the active group */
2006         if (ob->type == OB_MESH) {
2007                 Mesh *me = ob->data;
2008                 BMEditMesh *em = me->edit_btmesh;
2009
2010                 if (em) {
2011                         BMIter iter;
2012
2013                         if (!CustomData_has_layer(&em->bm->vdata, CD_MDEFORMVERT)) {
2014                                 goto cleanup;
2015                         }
2016
2017                         EDBM_verts_mirror_cache_begin(em, FALSE);
2018
2019                         /* Go through the list of editverts and assign them */
2020                         BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
2021                                 if ((eve_mirr = EDBM_verts_mirror_get(em, eve))) {
2022                                         sel = BM_elem_flag_test(eve, BM_ELEM_SELECT);
2023                                         sel_mirr = BM_elem_flag_test(eve_mirr, BM_ELEM_SELECT);
2024
2025                                         if ((sel || sel_mirr) && (eve != eve_mirr)) {
2026                                                 dvert = CustomData_bmesh_get(&em->bm->vdata, eve->head.data, CD_MDEFORMVERT);
2027                                                 dvert_mirr = CustomData_bmesh_get(&em->bm->vdata, eve_mirr->head.data, CD_MDEFORMVERT);
2028                                                 if (dvert && dvert_mirr) {
2029                                                         VGROUP_MIRR_OP;
2030                                                 }
2031                                         }
2032
2033                                         /* don't use these again */
2034                                         EDBM_verts_mirror_cache_clear(em, eve);
2035                                         EDBM_verts_mirror_cache_clear(em, eve_mirr);
2036                                 }
2037                         }
2038                         EDBM_verts_mirror_cache_end(em);
2039                 }
2040                 else {
2041                         /* object mode / weight paint */
2042                         MVert *mv, *mv_mirr;
2043                         int vidx, vidx_mirr;
2044                         const int use_vert_sel = (me->editflag & ME_EDIT_VERT_SEL) != 0;
2045
2046                         if (me->dvert == NULL) {
2047                                 goto cleanup;
2048                         }
2049
2050                         if (!use_vert_sel) {
2051                                 sel = sel_mirr = TRUE;
2052                         }
2053
2054                         /* tag verts we have used */
2055                         for (vidx = 0, mv = me->mvert; vidx < me->totvert; vidx++, mv++) {
2056                                 mv->flag &= ~ME_VERT_TMP_TAG;
2057                         }
2058
2059                         for (vidx = 0, mv = me->mvert; vidx < me->totvert; vidx++, mv++) {
2060                                 if (    ((mv->flag & ME_VERT_TMP_TAG) == 0) &&
2061                                         ((vidx_mirr = mesh_get_x_mirror_vert(ob, vidx)) != -1) &&
2062                                         (vidx != vidx_mirr) &&
2063                                         ((((mv_mirr = me->mvert + vidx_mirr)->flag) & ME_VERT_TMP_TAG) == 0))
2064                                 {
2065
2066                                         if (use_vert_sel) {
2067                                                 sel = mv->flag & SELECT;
2068                                                 sel_mirr = mv_mirr->flag & SELECT;
2069                                         }
2070
2071                                         if (sel || sel_mirr) {
2072                                                 dvert = &me->dvert[vidx];
2073                                                 dvert_mirr = &me->dvert[vidx_mirr];
2074
2075                                                 VGROUP_MIRR_OP;
2076                                         }
2077
2078                                         mv->flag |= ME_VERT_TMP_TAG;
2079                                         mv_mirr->flag |= ME_VERT_TMP_TAG;
2080                                 }
2081                         }
2082                 }
2083         }
2084         else if (ob->type == OB_LATTICE) {
2085                 Lattice *lt = vgroup_edit_lattice(ob);
2086                 int i1, i2;
2087                 int u, v, w;
2088                 int pntsu_half;
2089                 /* half but found up odd value */
2090
2091                 if (lt->pntsu == 1 || lt->dvert == NULL) {
2092                         goto cleanup;
2093                 }
2094
2095                 /* unlike editmesh we know that by only looping over the first half of
2096                  * the 'u' indices it will cover all points except the middle which is
2097                  * ok in this case */
2098                 pntsu_half = lt->pntsu / 2;
2099
2100                 for (w = 0; w < lt->pntsw; w++) {
2101                         for (v = 0; v < lt->pntsv; v++) {
2102                                 for (u = 0; u < pntsu_half; u++) {
2103                                         int u_inv = (lt->pntsu - 1) - u;
2104                                         if (u != u_inv) {
2105                                                 BPoint *bp, *bp_mirr;
2106
2107                                                 i1 = LT_INDEX(lt, u, v, w);
2108                                                 i2 = LT_INDEX(lt, u_inv, v, w);
2109
2110                                                 bp = &lt->def[i1];
2111                                                 bp_mirr = &lt->def[i2];
2112
2113                                                 sel = bp->f1 & SELECT;
2114                                                 sel_mirr = bp_mirr->f1 & SELECT;
2115
2116                                                 if (sel || sel_mirr) {
2117                                                         dvert = &lt->dvert[i1];
2118                                                         dvert_mirr = &lt->dvert[i2];
2119
2120                                                         VGROUP_MIRR_OP;
2121                                                 }
2122                                         }
2123                                 }
2124                         }
2125                 }
2126         }
2127
2128         /* flip active group index */
2129         if (flip_vgroups && flip_map[def_nr] >= 0)
2130                 ob->actdef = flip_map[def_nr] + 1;
2131
2132 cleanup:
2133         if (flip_map) MEM_freeN(flip_map);
2134
2135 #undef VGROUP_MIRR_OP
2136
2137 }
2138
2139 static void vgroup_remap_update_users(Object *ob, int *map)
2140 {
2141         ExplodeModifierData *emd;
2142         ModifierData *md;
2143         ParticleSystem *psys;
2144         ClothModifierData *clmd;
2145         ClothSimSettings *clsim;
2146         int a;
2147
2148         /* these cases don't use names to refer to vertex groups, so when
2149          * they get deleted the numbers get out of sync, this corrects that */
2150
2151         if (ob->soft)
2152                 ob->soft->vertgroup = map[ob->soft->vertgroup];
2153
2154         for (md = ob->modifiers.first; md; md = md->next) {
2155                 if (md->type == eModifierType_Explode) {
2156                         emd = (ExplodeModifierData *)md;
2157                         emd->vgroup = map[emd->vgroup];
2158                 }
2159                 else if (md->type == eModifierType_Cloth) {
2160                         clmd = (ClothModifierData *)md;
2161                         clsim = clmd->sim_parms;
2162
2163                         if (clsim) {
2164                                 clsim->vgroup_mass = map[clsim->vgroup_mass];
2165                                 clsim->vgroup_bend = map[clsim->vgroup_bend];
2166                                 clsim->vgroup_struct = map[clsim->vgroup_struct];
2167                         }
2168                 }
2169         }
2170
2171         for (psys = ob->particlesystem.first; psys; psys = psys->next) {
2172                 for (a = 0; a < PSYS_TOT_VG; a++)
2173                         psys->vgroup[a] = map[psys->vgroup[a]];
2174         }
2175 }
2176
2177
2178 static void vgroup_delete_update_users(Object *ob, int id)
2179 {
2180         int i, defbase_tot = BLI_countlist(&ob->defbase) + 1;
2181         int *map = MEM_mallocN(sizeof(int) * defbase_tot, "vgroup del");
2182
2183         map[id] = map[0] = 0;
2184         for (i = 1; i < id; i++) map[i] = i;
2185         for (i = id + 1; i < defbase_tot; i++) map[i] = i - 1;
2186
2187         vgroup_remap_update_users(ob, map);
2188         MEM_freeN(map);
2189 }
2190
2191
2192 static void vgroup_delete_object_mode(Object *ob, bDeformGroup *dg)
2193 {
2194         MDeformVert *dvert_array = NULL;
2195         int dvert_tot = 0;
2196         const int def_nr = BLI_findindex(&ob->defbase, dg);
2197
2198         assert(def_nr > -1);
2199
2200         ED_vgroup_give_array(ob->data, &dvert_array, &dvert_tot);
2201
2202         if (dvert_array) {
2203                 int i, j;
2204                 MDeformVert *dv;
2205                 for (i = 0, dv = dvert_array; i < dvert_tot; i++, dv++) {
2206                         MDeformWeight *dw;
2207
2208                         dw = defvert_find_index(dv, def_nr);
2209                         defvert_remove_group(dv, dw); /* dw can be NULL */
2210
2211                         /* inline, make into a function if anything else needs to do this */
2212                         for (j = 0; j < dv->totweight; j++) {
2213                                 if (dv->dw[j].def_nr > def_nr) {
2214                                         dv->dw[j].def_nr--;
2215                                 }
2216                         }
2217                         /* done */
2218                 }
2219         }
2220
2221         vgroup_delete_update_users(ob, def_nr + 1);
2222
2223         /* Remove the group */
2224         BLI_freelinkN(&ob->defbase, dg);
2225
2226         /* Update the active deform index if necessary */
2227         if (ob->actdef > def_nr)
2228                 ob->actdef--;
2229         if (ob->actdef < 1 && ob->defbase.first)
2230                 ob->actdef = 1;
2231
2232         /* remove all dverts */
2233         if (ob->defbase.first == NULL) {
2234                 if (ob->type == OB_MESH) {
2235                         Mesh *me = ob->data;
2236                         CustomData_free_layer_active(&me->vdata, CD_MDEFORMVERT, me->totvert);
2237                         me->dvert = NULL;
2238                 }
2239                 else if (ob->type == OB_LATTICE) {
2240                         Lattice *lt = ob->data;
2241                         if (lt->dvert) {
2242                                 MEM_freeN(lt->dvert);
2243                                 lt->dvert = NULL;
2244                         }
2245                 }
2246         }
2247 }
2248
2249 /* only in editmode */
2250 /* removes from active defgroup, if allverts==0 only selected vertices */
2251 static void vgroup_active_remove_verts(Object *ob, const int allverts, bDeformGroup *dg)
2252 {
2253         MDeformVert *dv;
2254         const int def_nr = BLI_findindex(&ob->defbase, dg);
2255
2256         if (ob->type == OB_MESH) {
2257                 Mesh *me = ob->data;
2258
2259                 if (me->edit_btmesh) {
2260                         BMEditMesh *em = me->edit_btmesh;
2261                         BMVert *eve;
2262                         BMIter iter;
2263
2264                         BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
2265                                 dv = CustomData_bmesh_get(&em->bm->vdata, eve->head.data, CD_MDEFORMVERT);
2266
2267                                 if (dv && dv->dw && (allverts || BM_elem_flag_test(eve, BM_ELEM_SELECT))) {
2268                                         MDeformWeight *dw = defvert_find_index(dv, def_nr);
2269                                         defvert_remove_group(dv, dw); /* dw can be NULL */
2270                                 }
2271                         }
2272                 }
2273                 else {
2274                         MVert *mv;
2275                         int i;
2276
2277                         if (!me->dvert) {
2278                                 ED_vgroup_data_create(&me->id);
2279                         }
2280
2281                         mv = me->mvert;
2282                         dv = me->dvert;
2283
2284                         for (i = 0; i < me->totvert; i++, mv++, dv++) {
2285                                 if (mv->flag & SELECT) {
2286                                         if (dv->dw && (allverts || (mv->flag & SELECT))) {
2287                                                 MDeformWeight *dw = defvert_find_index(dv, def_nr);
2288                                                 defvert_remove_group(dv, dw); /* dw can be NULL */
2289                                         }
2290                                 }
2291                         }
2292                 }
2293         }
2294         else if (ob->type == OB_LATTICE) {
2295                 Lattice *lt = vgroup_edit_lattice(ob);
2296                 
2297                 if (lt->dvert) {
2298                         BPoint *bp;
2299                         int i, tot = lt->pntsu * lt->pntsv * lt->pntsw;
2300                                 
2301                         for (i = 0, bp = lt->def; i < tot; i++, bp++) {
2302                                 if (allverts || (bp->f1 & SELECT)) {
2303                                         MDeformWeight *dw;
2304
2305                                         dv = &lt->dvert[i];
2306
2307                                         dw = defvert_find_index(dv, def_nr);
2308                                         defvert_remove_group(dv, dw); /* dw can be NULL */
2309                                 }
2310                         }
2311                 }
2312         }
2313 }
2314
2315 static void vgroup_delete_edit_mode(Object *ob, bDeformGroup *dg)
2316 {
2317         int i;
2318         const int dg_index = BLI_findindex(&ob->defbase, dg);
2319
2320         assert(dg_index > -1);
2321
2322         /* Make sure that no verts are using this group */
2323         vgroup_active_remove_verts(ob, TRUE, dg);
2324
2325         /* Make sure that any verts with higher indices are adjusted accordingly */
2326         if (ob->type == OB_MESH) {
2327                 Mesh *me = ob->data;
2328                 BMEditMesh *em = me->edit_btmesh;
2329                 BMIter iter;
2330                 BMVert *eve;
2331                 MDeformVert *dvert;
2332                 
2333                 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
2334                         dvert = CustomData_bmesh_get(&em->bm->vdata, eve->head.data, CD_MDEFORMVERT);
2335
2336                         if (dvert)
2337                                 for (i = 0; i < dvert->totweight; i++)
2338                                         if (dvert->dw[i].def_nr > dg_index)
2339                                                 dvert->dw[i].def_nr--;
2340                 }
2341         }
2342         else if (ob->type == OB_LATTICE) {
2343                 Lattice *lt = vgroup_edit_lattice(ob);
2344                 BPoint *bp;
2345                 MDeformVert *dvert = lt->dvert;
2346                 int a, tot;
2347                 
2348                 if (dvert) {
2349                         tot = lt->pntsu * lt->pntsv * lt->pntsw;
2350                         for (a = 0, bp = lt->def; a < tot; a++, bp++, dvert++) {
2351                                 for (i = 0; i < dvert->totweight; i++) {
2352                                         if (dvert->dw[i].def_nr > dg_index)
2353                                                 dvert->dw[i].def_nr--;
2354                                 }
2355                         }
2356                 }
2357         }
2358
2359         vgroup_delete_update_users(ob, dg_index + 1);
2360
2361         /* Remove the group */
2362         BLI_freelinkN(&ob->defbase, dg);
2363
2364         /* Update the active deform index if necessary */
2365         if (ob->actdef > dg_index)
2366                 ob->actdef--;
2367         if (ob->actdef < 1 && ob->defbase.first)
2368                 ob->actdef = 1;
2369
2370         /* remove all dverts */
2371         if (ob->defbase.first == NULL) {
2372                 if (ob->type == OB_MESH) {
2373                         Mesh *me = ob->data;
2374                         CustomData_free_layer_active(&me->vdata, CD_MDEFORMVERT, me->totvert);
2375                         me->dvert = NULL;
2376                 }
2377                 else if (ob->type == OB_LATTICE) {
2378                         Lattice *lt = vgroup_edit_lattice(ob);
2379                         if (lt->dvert) {
2380                                 MEM_freeN(lt->dvert);
2381                                 lt->dvert = NULL;
2382                         }
2383                 }
2384         }
2385 }
2386
2387 static int vgroup_object_in_edit_mode(Object *ob)
2388 {
2389         if (ob->type == OB_MESH)
2390                 return (BMEdit_FromObject(ob) != NULL);
2391         else if (ob->type == OB_LATTICE)
2392                 return (((Lattice *)ob->data)->editlatt != NULL);
2393         
2394         return 0;
2395 }
2396
2397 static int vgroup_object_in_wpaint_vert_select(Object *ob)
2398 {
2399         if (ob->type == OB_MESH) {
2400                 Mesh *me = ob->data;
2401                 return ( (ob->mode & OB_MODE_WEIGHT_PAINT) &&
2402                          (me->edit_btmesh == NULL) &&
2403                          (ME_EDIT_PAINT_SEL_MODE(me) == SCE_SELECT_VERTEX) );
2404         }
2405
2406         return 0;
2407 }
2408
2409 static void vgroup_delete(Object *ob)
2410 {
2411         bDeformGroup *dg = BLI_findlink(&ob->defbase, ob->actdef - 1);
2412         if (!dg)
2413                 return;
2414
2415         if (vgroup_object_in_edit_mode(ob))
2416                 vgroup_delete_edit_mode(ob, dg);
2417         else
2418                 vgroup_delete_object_mode(ob, dg);
2419 }
2420
2421 static void vgroup_delete_all(Object *ob)
2422 {
2423         /* Remove all DVerts */
2424         if (ob->type == OB_MESH) {
2425                 Mesh *me = ob->data;
2426                 CustomData_free_layer_active(&me->vdata, CD_MDEFORMVERT, me->totvert);
2427                 me->dvert = NULL;
2428         }
2429         else if (ob->type == OB_LATTICE) {
2430                 Lattice *lt = vgroup_edit_lattice(ob);
2431                 if (lt->dvert) {
2432                         MEM_freeN(lt->dvert);
2433                         lt->dvert = NULL;
2434                 }
2435         }
2436         
2437         /* Remove all DefGroups */
2438         BLI_freelistN(&ob->defbase);
2439         
2440         /* Fix counters/indices */
2441         ob->actdef = 0;
2442 }
2443
2444 /* only in editmode */
2445 static void vgroup_assign_verts(Object *ob, const float weight)
2446 {
2447         const int def_nr = ob->actdef - 1;
2448
2449         if (!BLI_findlink(&ob->defbase, def_nr))
2450                 return;
2451
2452         if (ob->type == OB_MESH) {
2453                 Mesh *me = ob->data;
2454
2455                 if (me->edit_btmesh) {
2456                         BMEditMesh *em = me->edit_btmesh;
2457                         BMIter iter;
2458                         BMVert *eve;
2459
2460                         if (!CustomData_has_layer(&em->bm->vdata, CD_MDEFORMVERT))
2461                                 BM_data_layer_add(em->bm, &em->bm->vdata, CD_MDEFORMVERT);
2462
2463                         /* Go through the list of editverts and assign them */
2464                         BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
2465                                 if (BM_elem_flag_test(eve, BM_ELEM_SELECT)) {
2466                                         MDeformVert *dv;
2467                                         MDeformWeight *dw;
2468                                         dv = CustomData_bmesh_get(&em->bm->vdata, eve->head.data, CD_MDEFORMVERT); /* can be NULL */
2469                                         dw = defvert_verify_index(dv, def_nr);
2470                                         if (dw) {
2471                                                 dw->weight = weight;
2472                                         }
2473                                 }
2474                         }
2475                 }
2476                 else {
2477                         MVert *mv;
2478                         MDeformVert *dv;
2479                         int i;
2480
2481                         if (!me->dvert) {
2482                                 ED_vgroup_data_create(&me->id);
2483                         }
2484
2485                         mv = me->mvert;
2486                         dv = me->dvert;
2487
2488                         for (i = 0; i < me->totvert; i++, mv++, dv++) {
2489                                 if (mv->flag & SELECT) {
2490                                         MDeformWeight *dw;
2491                                         dw = defvert_verify_index(dv, def_nr);
2492                                         if (dw) {
2493                                                 dw->weight = weight;
2494                                         }
2495                                 }
2496                         }
2497                 }
2498         }
2499         else if (ob->type == OB_LATTICE) {
2500                 Lattice *lt = vgroup_edit_lattice(ob);
2501                 MDeformVert *dv;
2502                 BPoint *bp;
2503                 int a, tot;
2504
2505                 if (lt->dvert == NULL)
2506                         ED_vgroup_data_create(&lt->id);
2507
2508                 dv = lt->dvert;
2509
2510                 tot = lt->pntsu * lt->pntsv * lt->pntsw;
2511                 for (a = 0, bp = lt->def; a < tot; a++, bp++, dv++) {
2512                         if (bp->f1 & SELECT) {
2513                                 MDeformWeight *dw;
2514
2515                                 dw = defvert_verify_index(dv, def_nr);
2516                                 if (dw) {
2517                                         dw->weight = weight;
2518                                 }
2519                         }
2520                 }
2521         }
2522 }
2523
2524 /* only in editmode */
2525 /* removes from all defgroup, if allverts==0 only selected vertices */
2526 static void vgroup_remove_verts(Object *ob, int allverts)
2527 {
2528         /* To prevent code redundancy, we just use vgroup_active_remove_verts, but that
2529          * only operates on the active vgroup. So we iterate through all groups, by changing
2530          * active group index
2531          */
2532         bDeformGroup *dg;
2533         for (dg = ob->defbase.first; dg; dg = dg->next) {
2534                 vgroup_active_remove_verts(ob, allverts, dg);
2535         }
2536 }
2537
2538 /********************** vertex group operators *********************/
2539
2540 static int vertex_group_poll(bContext *C)
2541 {
2542         Object *ob = ED_object_context(C);
2543         ID *data = (ob) ? ob->data : NULL;
2544         return (ob && !ob->id.lib && OB_TYPE_SUPPORT_VGROUP(ob->type) && data && !data->lib);
2545 }
2546
2547 static int UNUSED_FUNCTION(vertex_group_poll_edit) (bContext * C)
2548 {
2549         Object *ob = ED_object_context(C);
2550         ID *data = (ob) ? ob->data : NULL;
2551
2552         if (!(ob && !ob->id.lib && data && !data->lib))
2553                 return 0;
2554
2555         return vgroup_object_in_edit_mode(ob);
2556 }
2557
2558 /* editmode _or_ weight paint vertex sel */
2559 static int vertex_group_poll_edit_or_wpaint_vert_select(bContext *C)
2560 {
2561         Object *ob = ED_object_context(C);
2562         ID *data = (ob) ? ob->data : NULL;
2563
2564         if (!(ob && !ob->id.lib && data && !data->lib))
2565                 return 0;
2566
2567         return (vgroup_object_in_edit_mode(ob) ||
2568                 vgroup_object_in_wpaint_vert_select(ob));
2569 }
2570
2571 static int vertex_group_add_exec(bContext *C, wmOperator *UNUSED(op))
2572 {
2573         Object *ob = ED_object_context(C);
2574
2575         ED_vgroup_add(ob);
2576         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2577         WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
2578         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2579         
2580         return OPERATOR_FINISHED;
2581 }
2582
2583 void OBJECT_OT_vertex_group_add(wmOperatorType *ot)
2584 {
2585         /* identifiers */
2586         ot->name = "Add Vertex Group";
2587         ot->idname = "OBJECT_OT_vertex_group_add";
2588         ot->description = "Add a new vertex group to the active object";
2589         
2590         /* api callbacks */
2591         ot->poll = vertex_group_poll;
2592         ot->exec = vertex_group_add_exec;
2593
2594         /* flags */
2595         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2596 }
2597
2598 static int vertex_group_remove_exec(bContext *C, wmOperator *op)
2599 {
2600         Object *ob = ED_object_context(C);
2601
2602         if (RNA_boolean_get(op->ptr, "all"))
2603                 vgroup_delete_all(ob);
2604         else
2605                 vgroup_delete(ob);
2606
2607         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2608         WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
2609         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2610         
2611         return OPERATOR_FINISHED;
2612 }
2613
2614 void OBJECT_OT_vertex_group_remove(wmOperatorType *ot)
2615 {
2616         /* identifiers */
2617         ot->name = "Remove Vertex Group";
2618         ot->idname = "OBJECT_OT_vertex_group_remove";
2619         ot->description = "Delete the active vertex group";
2620         
2621         /* api callbacks */
2622         ot->poll = vertex_group_poll;
2623         ot->exec = vertex_group_remove_exec;
2624
2625         /* flags */
2626         /* redo operator will fail in this case because vertex groups aren't stored
2627          * in local edit mode stack and toggling "all" property will lead to
2628          * all groups deleted without way to restore them (see [#29527], sergey) */
2629         ot->flag = /*OPTYPE_REGISTER|*/ OPTYPE_UNDO;
2630
2631         /* properties */
2632         RNA_def_boolean(ot->srna, "all", 0, "All", "Remove from all vertex groups");
2633 }
2634
2635 static int vertex_group_assign_exec(bContext *C, wmOperator *op)
2636 {
2637         ToolSettings *ts = CTX_data_tool_settings(C);
2638         Object *ob = ED_object_context(C);
2639
2640         if (RNA_boolean_get(op->ptr, "new"))
2641                 ED_vgroup_add(ob);
2642
2643         vgroup_assign_verts(ob, ts->vgroup_weight);
2644         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2645         WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
2646         
2647         return OPERATOR_FINISHED;
2648 }
2649
2650 void OBJECT_OT_vertex_group_assign(wmOperatorType *ot)
2651 {
2652         /* identifiers */
2653         ot->name = "Assign Vertex Group";
2654         ot->idname = "OBJECT_OT_vertex_group_assign";
2655         ot->description = "Assign the selected vertices to the current (or a new) vertex group";
2656         
2657         /* api callbacks */
2658         ot->poll = vertex_group_poll_edit_or_wpaint_vert_select;
2659         ot->exec = vertex_group_assign_exec;
2660
2661         /* flags */
2662         /* redo operator will fail in this case because vertex group assignment
2663          * isn't stored in local edit mode stack and toggling "new" property will
2664          * lead to creating plenty of new vertex groups (see [#29527], sergey) */
2665         ot->flag = /*OPTYPE_REGISTER|*/ OPTYPE_UNDO;
2666
2667         /* properties */
2668         RNA_def_boolean(ot->srna, "new", 0, "New", "Assign vertex to new vertex group");
2669 }
2670
2671 static int vertex_group_remove_from_exec(bContext *C, wmOperator *op)
2672 {
2673         Object *ob = ED_object_context(C);
2674
2675         if (RNA_boolean_get(op->ptr, "all"))
2676                 vgroup_remove_verts(ob, 0);
2677         else {
2678                 bDeformGroup *dg = BLI_findlink(&ob->defbase, ob->actdef - 1);
2679
2680                 if (dg == NULL) {
2681                         return OPERATOR_CANCELLED;
2682                 }
2683
2684                 vgroup_active_remove_verts(ob, FALSE, dg);
2685         }
2686
2687         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2688         WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
2689
2690         return OPERATOR_FINISHED;
2691 }
2692
2693 void OBJECT_OT_vertex_group_remove_from(wmOperatorType *ot)
2694 {
2695         /* identifiers */
2696         ot->name = "Remove from Vertex Group";
2697         ot->idname = "OBJECT_OT_vertex_group_remove_from";
2698         ot->description = "Remove the selected vertices from the active vertex group";
2699
2700         /* api callbacks */
2701         ot->poll = vertex_group_poll_edit_or_wpaint_vert_select;
2702         ot->exec = vertex_group_remove_from_exec;
2703
2704         /* flags */
2705         /* redo operator will fail in this case because vertex groups assignment
2706          * isn't stored in local edit mode stack and toggling "all" property will lead to
2707          * removing vertices from all groups (see [#29527], sergey) */
2708         ot->flag = /*OPTYPE_REGISTER|*/ OPTYPE_UNDO;
2709
2710         /* properties */
2711         RNA_def_boolean(ot->srna, "all", 0, "All", "Remove from all vertex groups");
2712 }
2713
2714 static int vertex_group_select_exec(bContext *C, wmOperator *UNUSED(op))
2715 {
2716         Object *ob = ED_object_context(C);
2717
2718         if (!ob || ob->id.lib)
2719                 return OPERATOR_CANCELLED;
2720
2721         vgroup_select_verts(ob, 1);
2722         WM_event_add_notifier(C, NC_GEOM | ND_SELECT, ob->data);
2723
2724         return OPERATOR_FINISHED;
2725 }
2726
2727 void OBJECT_OT_vertex_group_select(wmOperatorType *ot)
2728 {
2729         /* identifiers */
2730         ot->name = "Select Vertex Group";
2731         ot->idname = "OBJECT_OT_vertex_group_select";
2732         ot->description = "Select all the vertices assigned to the active vertex group";
2733
2734         /* api callbacks */
2735         ot->poll = vertex_group_poll_edit_or_wpaint_vert_select;
2736         ot->exec = vertex_group_select_exec;
2737
2738         /* flags */
2739         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2740 }
2741
2742 static int vertex_group_deselect_exec(bContext *C, wmOperator *UNUSED(op))
2743 {
2744         Object *ob = ED_object_context(C);
2745
2746         vgroup_select_verts(ob, 0);
2747         WM_event_add_notifier(C, NC_GEOM | ND_SELECT, ob->data);
2748
2749         return OPERATOR_FINISHED;
2750 }
2751
2752 void OBJECT_OT_vertex_group_deselect(wmOperatorType *ot)
2753 {
2754         /* identifiers */
2755         ot->name = "Deselect Vertex Group";
2756         ot->idname = "OBJECT_OT_vertex_group_deselect";
2757         ot->description = "Deselect all selected vertices assigned to the active vertex group";
2758
2759         /* api callbacks */
2760         ot->poll = vertex_group_poll_edit_or_wpaint_vert_select;
2761         ot->exec = vertex_group_deselect_exec;
2762
2763         /* flags */
2764         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2765 }
2766
2767 static int vertex_group_copy_exec(bContext *C, wmOperator *UNUSED(op))
2768 {
2769         Object *ob = ED_object_context(C);
2770
2771         vgroup_duplicate(ob);
2772         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2773         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2774         WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
2775
2776         return OPERATOR_FINISHED;
2777 }
2778
2779 void OBJECT_OT_vertex_group_copy(wmOperatorType *ot)
2780 {
2781         /* identifiers */
2782         ot->name = "Copy Vertex Group";
2783         ot->idname = "OBJECT_OT_vertex_group_copy";
2784         ot->description = "Make a copy of the active vertex group";
2785
2786         /* api callbacks */
2787         ot->poll = vertex_group_poll;
2788         ot->exec = vertex_group_copy_exec;
2789
2790         /* flags */
2791         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2792 }
2793
2794 static int vertex_group_levels_exec(bContext *C, wmOperator *op)
2795 {
2796         Object *ob = ED_object_context(C);
2797         
2798         float offset = RNA_float_get(op->ptr, "offset");
2799         float gain = RNA_float_get(op->ptr, "gain");
2800         
2801         vgroup_levels(ob, offset, gain);
2802         
2803         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2804         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2805         WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
2806         
2807         return OPERATOR_FINISHED;
2808 }
2809
2810 void OBJECT_OT_vertex_group_levels(wmOperatorType *ot)
2811 {
2812         /* identifiers */
2813         ot->name = "Vertex Group Levels";
2814         ot->idname = "OBJECT_OT_vertex_group_levels";
2815         ot->description = "Add some offset and multiply with some gain the weights of the active vertex group";
2816         
2817         /* api callbacks */
2818         ot->poll = vertex_group_poll;
2819         ot->exec = vertex_group_levels_exec;
2820         
2821         /* flags */
2822         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2823         
2824         RNA_def_float(ot->srna, "offset", 0.f, -1.0, 1.0, "Offset", "Value to add to weights", -1.0f, 1.f);
2825         RNA_def_float(ot->srna, "gain", 1.f, 0.f, FLT_MAX, "Gain", "Value to multiply weights by", 0.0f, 10.f);
2826 }
2827
2828 static int vertex_group_normalize_exec(bContext *C, wmOperator *UNUSED(op))
2829 {
2830         Object *ob = ED_object_context(C);
2831
2832         vgroup_normalize(ob);
2833
2834         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2835         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2836         WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
2837
2838         return OPERATOR_FINISHED;
2839 }
2840
2841 void OBJECT_OT_vertex_group_normalize(wmOperatorType *ot)
2842 {
2843         /* identifiers */
2844         ot->name = "Normalize Vertex Group";
2845         ot->idname = "OBJECT_OT_vertex_group_normalize";
2846         ot->description = "Normalize weights of the active vertex group, so that the highest ones are now 1.0";
2847
2848         /* api callbacks */
2849         ot->poll = vertex_group_poll;
2850         ot->exec = vertex_group_normalize_exec;
2851
2852         /* flags */
2853         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2854 }
2855
2856 static int vertex_group_normalize_all_exec(bContext *C, wmOperator *op)
2857 {
2858         Object *ob = ED_object_context(C);
2859         int lock_active = RNA_boolean_get(op->ptr, "lock_active");
2860
2861         vgroup_normalize_all(ob, lock_active);
2862
2863         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2864         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2865         WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
2866
2867         return OPERATOR_FINISHED;
2868 }
2869
2870 void OBJECT_OT_vertex_group_normalize_all(wmOperatorType *ot)
2871 {
2872         /* identifiers */
2873         ot->name = "Normalize All Vertex Groups";
2874         ot->idname = "OBJECT_OT_vertex_group_normalize_all";
2875         ot->description = "Normalize all weights of all vertex groups, "
2876                           "so that for each vertex, the sum of all weights is 1.0";
2877
2878         /* api callbacks */
2879         ot->poll = vertex_group_poll;
2880         ot->exec = vertex_group_normalize_all_exec;
2881
2882         /* flags */
2883         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2884
2885         RNA_def_boolean(ot->srna, "lock_active", TRUE, "Lock Active",
2886                         "Keep the values of the active group while normalizing others");
2887 }
2888
2889 static int vertex_group_fix_exec(bContext *C, wmOperator *op)
2890 {
2891         Object *ob = CTX_data_active_object(C);
2892         Scene *scene = CTX_data_scene(C);
2893         
2894         float distToBe = RNA_float_get(op->ptr, "dist");
2895         float strength = RNA_float_get(op->ptr, "strength");
2896         float cp = RNA_float_get(op->ptr, "accuracy");
2897         ModifierData *md = ob->modifiers.first;
2898
2899         while (md) {
2900                 if (md->type == eModifierType_Mirror && (md->mode & eModifierMode_Realtime)) {
2901                         break;
2902                 }
2903                 md = md->next;
2904         }
2905         
2906         if (md && md->type == eModifierType_Mirror) {
2907                 BKE_report(op->reports, RPT_ERROR_INVALID_CONTEXT, "This operator does not support an active mirror modifier");
2908                 return OPERATOR_CANCELLED;
2909         }
2910         vgroup_fix(scene, ob, distToBe, strength, cp);
2911         
2912         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2913         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2914         WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
2915         
2916         return OPERATOR_FINISHED;
2917 }
2918
2919 void OBJECT_OT_vertex_group_fix(wmOperatorType *ot)
2920 {
2921         /* identifiers */
2922         ot->name = "Fix Vertex Group Deform";
2923         ot->idname = "OBJECT_OT_vertex_group_fix";
2924         ot->description = "Modify the position of selected vertices by changing only their respective "
2925                           "groups' weights (this tool may be slow for many vertices)";
2926         
2927         /* api callbacks */
2928         ot->poll = vertex_group_poll;
2929         ot->exec = vertex_group_fix_exec;
2930         
2931         /* flags */
2932         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2933         RNA_def_float(ot->srna, "dist", 0.0f, -FLT_MAX, FLT_MAX, "Distance", "The distance to move to", -10.0f, 10.0f);
2934         RNA_def_float(ot->srna, "strength", 1.f, -2.0f, FLT_MAX, "Strength",
2935                       "The distance moved can be changed by this multiplier", -2.0f, 2.0f);
2936         RNA_def_float(ot->srna, "accuracy", 1.0f, 0.05f, FLT_MAX, "Change Sensitivity",
2937                       "Change the amount weights are altered with each iteration: lower values are slower", 0.05f, 1.f);
2938 }
2939
2940
2941 static int vertex_group_lock_exec(bContext *C, wmOperator *op)
2942 {
2943         Object *ob = CTX_data_active_object(C);
2944
2945         int action = RNA_enum_get(op->ptr, "action");
2946
2947         vgroup_lock_all(ob, action);
2948
2949         return OPERATOR_FINISHED;
2950 }
2951
2952 void OBJECT_OT_vertex_group_lock(wmOperatorType *ot)
2953 {
2954         /* identifiers */
2955         ot->name = "Change the Lock On Vertex Groups";
2956         ot->idname = "OBJECT_OT_vertex_group_lock";
2957         ot->description = "Change the lock state of all vertex groups of active object";
2958
2959         /* api callbacks */
2960         ot->poll = vertex_group_poll;
2961         ot->exec = vertex_group_lock_exec;
2962
2963         /* flags */
2964         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2965
2966         WM_operator_properties_select_all(ot);
2967 }
2968
2969 static int vertex_group_invert_exec(bContext *C, wmOperator *op)
2970 {
2971         Object *ob = ED_object_context(C);
2972         int auto_assign = RNA_boolean_get(op->ptr, "auto_assign");
2973         int auto_remove = RNA_boolean_get(op->ptr, "auto_remove");
2974