2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/editors/object/object_vgroup.c
38 #include "MEM_guardedalloc.h"
40 #include "DNA_cloth_types.h"
41 #include "DNA_curve_types.h"
42 #include "DNA_lattice_types.h"
43 #include "DNA_meshdata_types.h"
44 #include "DNA_mesh_types.h"
45 #include "DNA_modifier_types.h"
46 #include "DNA_object_types.h"
47 #include "DNA_object_force.h"
48 #include "DNA_scene_types.h"
49 #include "DNA_particle_types.h"
51 #include "BLI_array.h"
53 #include "BLI_blenlib.h"
54 #include "BLI_utildefines.h"
56 #include "BKE_context.h"
57 #include "BKE_customdata.h"
58 #include "BKE_deform.h"
59 #include "BKE_depsgraph.h"
60 #include "BKE_global.h"
62 #include "BKE_tessmesh.h"
63 #include "BKE_report.h"
64 #include "BKE_DerivedMesh.h"
66 #include "RNA_access.h"
67 #include "RNA_define.h"
72 #include "ED_object.h"
75 #include "UI_resources.h"
77 #include "object_intern.h"
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);
85 static Lattice *vgroup_edit_lattice(Object *ob)
87 Lattice *lt = ob->data;
88 BLI_assert(ob->type == OB_LATTICE);
89 return (lt->editlatt) ? lt->editlatt->latt : lt;
92 int ED_vgroup_object_is_edit_mode(Object *ob)
94 if (ob->type == OB_MESH)
95 return (BMEdit_FromObject(ob) != NULL);
96 else if (ob->type == OB_LATTICE)
97 return (((Lattice *)ob->data)->editlatt != NULL);
102 bDeformGroup *ED_vgroup_add_name(Object *ob, const char *name)
104 bDeformGroup *defgroup;
106 if (!ob || !OB_TYPE_SUPPORT_VGROUP(ob->type))
109 defgroup = MEM_callocN(sizeof(bDeformGroup), "add deformGroup");
111 BLI_strncpy(defgroup->name, name, sizeof(defgroup->name));
113 BLI_addtail(&ob->defbase, defgroup);
114 defgroup_unique_name(defgroup, ob);
116 ob->actdef = BLI_countlist(&ob->defbase);
121 bDeformGroup *ED_vgroup_add(Object *ob)
123 return ED_vgroup_add_name(ob, "Group");
126 void ED_vgroup_delete(Object *ob, bDeformGroup *defgroup)
128 bDeformGroup *dg = (bDeformGroup *)ob->defbase.first;
139 if (ED_vgroup_object_is_edit_mode(ob))
140 vgroup_delete_edit_mode(ob, dg);
142 vgroup_delete_object_mode(ob, dg);
145 void ED_vgroup_clear(Object *ob)
147 bDeformGroup *dg = (bDeformGroup *)ob->defbase.first;
148 int edit_mode = ED_vgroup_object_is_edit_mode(ob);
151 bDeformGroup *next_dg = dg->next;
154 vgroup_delete_edit_mode(ob, dg);
156 vgroup_delete_object_mode(ob, dg);
162 int ED_vgroup_data_create(ID *id)
164 /* create deform verts */
166 if (GS(id->name) == ID_ME) {
167 Mesh *me = (Mesh *)id;
168 me->dvert = CustomData_add_layer(&me->vdata, CD_MDEFORMVERT, CD_CALLOC, NULL, me->totvert);
171 else if (GS(id->name) == ID_LT) {
172 Lattice *lt = (Lattice *)id;
173 lt->dvert = MEM_callocN(sizeof(MDeformVert) * lt->pntsu * lt->pntsv * lt->pntsw, "lattice deformVert");
181 static int ED_vgroup_give_parray(ID *id, MDeformVert ***dvert_arr, int *dvert_tot, const short use_vert_sel)
187 switch (GS(id->name)) {
190 Mesh *me = (Mesh *)id;
192 if (me->edit_btmesh) {
193 BMEditMesh *em = me->edit_btmesh;
198 if (!CustomData_has_layer(&em->bm->vdata, CD_MDEFORMVERT)) {
204 *dvert_arr = MEM_mallocN(sizeof(void *) * i, "vgroup parray from me");
209 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
210 (*dvert_arr)[i] = BM_elem_flag_test(eve, BM_ELEM_SELECT) ?
211 CustomData_bmesh_get(&em->bm->vdata, eve->head.data, CD_MDEFORMVERT) : NULL;
216 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
217 (*dvert_arr)[i] = CustomData_bmesh_get(&em->bm->vdata, eve->head.data, CD_MDEFORMVERT);
224 else if (me->dvert) {
225 MVert *mvert = me->mvert;
226 MDeformVert *dvert = me->dvert;
229 *dvert_tot = me->totvert;
230 *dvert_arr = MEM_mallocN(sizeof(void *) * me->totvert, "vgroup parray from me");
233 for (i = 0; i < me->totvert; i++) {
234 (*dvert_arr)[i] = (mvert[i].flag & SELECT) ?
239 for (i = 0; i < me->totvert; i++) {
240 (*dvert_arr)[i] = me->dvert + i;
254 Lattice *lt = (Lattice *)id;
255 lt = (lt->editlatt) ? lt->editlatt->latt : lt;
258 BPoint *def = lt->def;
259 *dvert_tot = lt->pntsu * lt->pntsv * lt->pntsw;
260 *dvert_arr = MEM_mallocN(sizeof(void *) * (*dvert_tot), "vgroup parray from me");
263 for (i = 0; i < *dvert_tot; i++) {
264 (*dvert_arr)[i] = (def->f1 & SELECT) ?
265 <->dvert[i] : NULL;
269 for (i = 0; i < *dvert_tot; i++) {
270 (*dvert_arr)[i] = lt->dvert + i;
286 /* returns true if the id type supports weights */
287 int ED_vgroup_give_array(ID *id, MDeformVert **dvert_arr, int *dvert_tot)
290 switch (GS(id->name)) {
293 Mesh *me = (Mesh *)id;
294 *dvert_arr = me->dvert;
295 *dvert_tot = me->totvert;
300 Lattice *lt = (Lattice *)id;
301 lt = (lt->editlatt) ? lt->editlatt->latt : lt;
302 *dvert_arr = lt->dvert;
303 *dvert_tot = lt->pntsu * lt->pntsv * lt->pntsw;
314 /* matching index only */
315 int ED_vgroup_copy_array(Object *ob, Object *ob_from)
317 MDeformVert **dvert_array_from, **dvf;
318 MDeformVert **dvert_array, **dv;
322 int defbase_tot_from = BLI_countlist(&ob_from->defbase);
323 int defbase_tot = BLI_countlist(&ob->defbase);
324 short new_vgroup = FALSE;
326 ED_vgroup_give_parray(ob_from->data, &dvert_array_from, &dvert_tot_from, FALSE);
327 ED_vgroup_give_parray(ob->data, &dvert_array, &dvert_tot, FALSE);
329 if ((dvert_array == NULL) && (dvert_array_from != NULL) && ED_vgroup_data_create(ob->data)) {
330 ED_vgroup_give_parray(ob->data, &dvert_array, &dvert_tot, FALSE);
334 if (ob == ob_from || dvert_tot == 0 || (dvert_tot != dvert_tot_from) || dvert_array_from == NULL || dvert_array == NULL) {
335 if (dvert_array) MEM_freeN(dvert_array);
336 if (dvert_array_from) MEM_freeN(dvert_array_from);
338 if (new_vgroup == TRUE) {
339 /* free the newly added vgroup since it wasn't compatible */
340 vgroup_delete_all(ob);
346 BLI_freelistN(&ob->defbase);
347 BLI_duplicatelist(&ob->defbase, &ob_from->defbase);
348 ob->actdef = ob_from->actdef;
350 if (defbase_tot_from < defbase_tot) {
351 /* correct vgroup indices because the number of vgroups is being reduced. */
352 int *remap = MEM_mallocN(sizeof(int) * (defbase_tot + 1), __func__);
353 for (i = 0; i <= defbase_tot_from; i++) remap[i] = i;
354 for (; i <= defbase_tot; i++) remap[i] = 0; /* can't use these, so disable */
356 vgroup_remap_update_users(ob, remap);
360 dvf = dvert_array_from;
363 for (i = 0; i < dvert_tot; i++, dvf++, dv++) {
365 MEM_freeN((*dv)->dw);
370 (*dv)->dw = MEM_dupallocN((*dv)->dw);
373 MEM_freeN(dvert_array);
374 MEM_freeN(dvert_array_from);
379 /*Copy a single vertex group from source to destination with weights*/
380 int ED_vgroup_copy_single(Object *ob_dst, const Object *ob_src)
382 MDeformVert **dv_array_src;
383 MDeformVert **dv_array_dst;
384 MDeformWeight *dw_dst, *dw_src;
385 int dv_tot_src, dv_tot_dst;
386 int i, index_src, index_dst;
387 bDeformGroup *dg_src, *dg_dst;
389 /*get source deform group*/
390 dg_src= BLI_findlink(&ob_src->defbase, (ob_src->actdef-1));
392 /*Create new and overwrite vertex group on destination without data*/
393 ED_vgroup_delete(ob_dst, defgroup_find_name(ob_dst, dg_src->name));
394 ED_vgroup_add_name(ob_dst, dg_src->name);
396 /*get destination deformgroup*/
397 dg_dst= defgroup_find_name(ob_dst, dg_src->name);
399 /*get vertex group arrays*/
400 ED_vgroup_give_parray(ob_src->data, &dv_array_src, &dv_tot_src, FALSE);
401 ED_vgroup_give_parray(ob_dst->data, &dv_array_dst, &dv_tot_dst, FALSE);
403 /*get indexes of vertex groups*/
404 index_src= BLI_findindex(&ob_src->defbase, dg_src);
405 index_dst= BLI_findindex(&ob_dst->defbase, dg_dst);
407 /*Check if indices are matching, delete and return if not*/
408 if (ob_dst == ob_src || dv_tot_dst == 0 || (dv_tot_dst != dv_tot_src) || dv_array_src == NULL || dv_array_dst == NULL) {
409 ED_vgroup_delete(ob_dst, defgroup_find_name(ob_dst, dg_dst->name));
413 /* Loop through the vertices and copy weight*/
414 for(i=0; i<dv_tot_dst; i++, dv_array_src++, dv_array_dst++) {
415 dw_src= defvert_verify_index(*dv_array_src, index_src);
416 dw_dst= defvert_verify_index(*dv_array_dst, index_dst);
417 dw_dst->weight= dw_src->weight;
423 /*Copy a single vertex group from source to destination with weights by nearest weight*/
424 int ED_vgroup_copy_by_nearest_vertex_single(Object *ob_dst, Object *ob_src)
426 bDeformGroup *dg_src, *dg_dst;
427 MDeformVert **dv_array_src, **dv_array_dst;
428 MDeformWeight *dw_dst, *dw_src;
431 BVHTreeFromMesh tree_mesh_src;
432 BVHTreeNearest nearest;
433 DerivedMesh *dmesh_src;
434 int dv_tot_src, dv_tot_dst, i, index_dst, index_src;
435 float tmp_co[3], tmp_mat[4][4];
437 /*get source deform group*/
438 dg_src= BLI_findlink(&ob_src->defbase, (ob_src->actdef-1));
440 /*Create new and overwrite vertex group on destination without data*/
441 ED_vgroup_delete(ob_dst, defgroup_find_name(ob_dst, dg_src->name));
442 ED_vgroup_add_name(ob_dst, dg_src->name);
444 /*get destination deformgroup*/
445 dg_dst= defgroup_find_name(ob_dst, dg_src->name);
448 me_dst= ob_dst->data;
449 dmesh_src= ob_src->derivedDeform;
452 bvhtree_from_mesh_verts(&tree_mesh_src, dmesh_src, 0.0, 2, 6);
454 /*get vertex group arrays*/
455 ED_vgroup_give_parray(ob_src->data, &dv_array_src, &dv_tot_src, FALSE);
456 ED_vgroup_give_parray(ob_dst->data, &dv_array_dst, &dv_tot_dst, FALSE);
458 /*get indexes of vertex groups*/
459 index_src= BLI_findindex(&ob_src->defbase, dg_src);
460 index_dst= BLI_findindex(&ob_dst->defbase, dg_dst);
463 mv_dst= me_dst->mvert;
465 /*Prepearing transformation matrix*/
466 /*This can be excluded to make a lazy feature that works better when object centers relative to mesh is the same*/
467 invert_m4_m4(ob_src->imat, ob_src->obmat);
468 mult_m4_m4m4(tmp_mat, ob_src->imat, ob_dst->obmat);
470 /* Loop through the vertices and copy weight from nearest weight*/
471 for(i=0; i < me_dst->totvert; i++, mv_dst++, dv_array_dst++){
475 nearest.dist= FLT_MAX;
477 /*Transforming into target space*/
478 mul_v3_m4v3(tmp_co, tmp_mat, mv_dst->co);
480 /*Node tree accelerated search for closest vetex*/
481 BLI_bvhtree_find_nearest(tree_mesh_src.tree, tmp_co, &nearest, tree_mesh_src.nearest_callback, &tree_mesh_src);
484 dw_src= defvert_verify_index(dv_array_src[nearest.index], index_src);
485 dw_dst= defvert_verify_index(*dv_array_dst, index_dst);
486 dw_dst->weight= dw_src->weight;
489 /*free memory and return*/
490 free_bvhtree_from_mesh(&tree_mesh_src);
494 /*Copy a single vertex group from source to destination with weights by nearest weight*/
495 /*TODO: transform into target space as in by_vertex function. postphoned due to easier testing during development*/
496 int ED_vgroup_copy_by_nearest_face_single(Object *ob_dst, Object *ob_src)
498 bDeformGroup *dg_src, *dg_dst;
499 MDeformVert **dv_array_src, **dv_array_dst;
500 MDeformWeight *dw_dst, *dw_src;
503 Mesh *me_dst, *me_src;
504 BVHTreeFromMesh tree_mesh_faces_src = {NULL};
505 BVHTreeNearest nearest;
506 DerivedMesh *dmesh_src;
507 int dv_tot_src, dv_tot_dst, i, index_dst, index_src;
508 float weight/*, tot_dist*/;
510 /*get source deform group*/
511 dg_src= BLI_findlink(&ob_src->defbase, (ob_src->actdef-1));
513 /*Create new and overwrite vertex group on destination without data*/
514 ED_vgroup_delete(ob_dst, defgroup_find_name(ob_dst, dg_src->name));
515 ED_vgroup_add_name(ob_dst, dg_src->name);
517 /*get destination deformgroup*/
518 dg_dst= defgroup_find_name(ob_dst, dg_src->name);
521 me_dst= ob_dst->data;
522 me_src= ob_src->data; /*mfaces does not exist*/
523 dmesh_src= ob_src->derivedDeform; /*sergey- : this might easily be null?? (using ob_src.deriveddeform*/
526 DM_ensure_tessface(dmesh_src);
527 bvhtree_from_mesh_faces(&tree_mesh_faces_src, dmesh_src, 0.0, 2, 6);
530 /*get vertex group arrays*/
531 ED_vgroup_give_parray(ob_src->data, &dv_array_src, &dv_tot_src, FALSE);
532 ED_vgroup_give_parray(ob_dst->data, &dv_array_dst, &dv_tot_dst, FALSE);
534 /*get indexes of vertex groups*/
535 index_src= BLI_findindex(&ob_src->defbase, dg_src);
536 index_dst= BLI_findindex(&ob_dst->defbase, dg_dst);
539 mv_dst= me_dst->mvert;
541 /* Loop through the vertices and copy weight from nearest weight*/
542 for(i=0; i < me_dst->totvert; i++, mv_dst++, dv_array_dst++){
546 nearest.dist= FLT_MAX;
548 /*Node tree accelerated search for closest face*/
549 BLI_bvhtree_find_nearest(tree_mesh_faces_src.tree, mv_dst->co, &nearest, tree_mesh_faces_src.nearest_callback, &tree_mesh_faces_src);
552 printf("test %d %d \n", me_src->mface->v1, (*me_src->mface).v1);
553 mface_src= me_src->mface + nearest.index;
554 /*tot_dist= ()+()+(); use a comparable distance
558 printf("test %d \n", (*mface_src).v1);
559 dv_array_src+= mface_src->v1;
560 dw_src= defvert_verify_index(*dv_array_src, index_src);
561 weight= dw_src->weight;
562 dw_src= defvert_verify_index(dv_array_src[mface_src->v2], index_src);
563 weight+= dw_src->weight;
564 dw_src= defvert_verify_index(dv_array_src[mface_src->v3], index_src);
565 weight+= dw_src->weight;
567 dw_src= defvert_verify_index(dv_array_src[mface_src->v4], index_src);
568 weight+= dw_src->weight;
577 dw_dst= defvert_verify_index(*dv_array_dst, index_dst);
578 dw_dst->weight= weight;
581 /*free memory and return*/
582 free_bvhtree_from_mesh(&tree_mesh_faces_src);
587 /* for Mesh in Object mode */
588 /* allows editmode for Lattice */
589 static void ED_vgroup_nr_vert_add(Object *ob,
590 const int def_nr, const int vertnum,
591 const float weight, const int assignmode)
593 /* add the vert to the deform group with the
596 MDeformVert *dvert = NULL;
600 ED_vgroup_give_array(ob->data, &dvert, &tot);
605 /* check that vertnum is valid before trying to get the relevant dvert */
606 if ((vertnum < 0) || (vertnum >= tot))
611 MDeformVert *dv = &dvert[vertnum];
614 /* Lets first check to see if this vert is
615 * already in the weight group -- if so
619 dw = defvert_find_index(dv, def_nr);
622 switch (assignmode) {
627 dw->weight += weight;
628 if (dw->weight >= 1.0f)
631 case WEIGHT_SUBTRACT:
632 dw->weight -= weight;
633 /* if the weight is zero or less then
634 * remove the vert from the deform group
636 if (dw->weight <= 0.0f) {
637 defvert_remove_group(dv, dw);
643 /* if the vert wasn't in the deform group then
644 * we must take a different form of action ...
647 switch (assignmode) {
648 case WEIGHT_SUBTRACT:
649 /* if we are subtracting then we don't
650 * need to do anything
656 /* if we are doing an additive assignment, then
657 * we need to create the deform weight
660 /* we checked if the vertex was added before so no need to test again, simply add */
661 defvert_add_index_notest(dv, def_nr, weight);
667 /* called while not in editmode */
668 void ED_vgroup_vert_add(Object *ob, bDeformGroup *dg, int vertnum, float weight, int assignmode)
670 /* add the vert to the deform group with the
671 * specified assign mode
673 const int def_nr = BLI_findindex(&ob->defbase, dg);
675 MDeformVert *dv = NULL;
678 /* get the deform group number, exit if
681 if (def_nr < 0) return;
683 /* if there's no deform verts then create some,
685 if (ED_vgroup_give_array(ob->data, &dv, &tot) && dv == NULL)
686 ED_vgroup_data_create(ob->data);
688 /* call another function to do the work
690 ED_vgroup_nr_vert_add(ob, def_nr, vertnum, weight, assignmode);
693 /* mesh object mode, lattice can be in editmode */
694 void ED_vgroup_vert_remove(Object *ob, bDeformGroup *dg, int vertnum)
696 /* This routine removes the vertex from the specified
700 /* TODO, this is slow in a loop, better pass def_nr directly, but leave for later... - campbell */
701 const int def_nr = BLI_findindex(&ob->defbase, dg);
704 MDeformVert *dvert = NULL;
707 /* get the deform vertices corresponding to the
710 ED_vgroup_give_array(ob->data, &dvert, &tot);
713 MDeformVert *dv = &dvert[vertnum];
716 dw = defvert_find_index(dv, def_nr);
717 defvert_remove_group(dv, dw); /* dw can be NULL */
722 static float get_vert_def_nr(Object *ob, const int def_nr, const int vertnum)
724 MDeformVert *dv = NULL;
728 /* get the deform vertices corresponding to the vertnum */
729 if (ob->type == OB_MESH) {
732 if (me->edit_btmesh) {
733 eve = BM_vert_at_index(me->edit_btmesh->bm, vertnum);
737 dv = CustomData_bmesh_get(&me->edit_btmesh->bm->vdata, eve->head.data, CD_MDEFORMVERT);
740 if (vertnum >= me->totvert) {
743 dv = &me->dvert[vertnum];
746 else if (ob->type == OB_LATTICE) {
747 Lattice *lt = vgroup_edit_lattice(ob);
750 if (vertnum >= lt->pntsu * lt->pntsv * lt->pntsw) {
753 dv = <->dvert[vertnum];
758 MDeformWeight *dw = defvert_find_index(dv, def_nr);
767 float ED_vgroup_vert_weight(Object *ob, bDeformGroup *dg, int vertnum)
769 const int def_nr = BLI_findindex(&ob->defbase, dg);
775 return get_vert_def_nr(ob, def_nr, vertnum);
778 void ED_vgroup_select_by_name(Object *ob, const char *name)
779 { /* note: ob->actdef==0 signals on painting to create a new one, if a bone in posemode is selected */
780 ob->actdef = defgroup_name_index(ob, name) + 1;
783 /********************** Operator Implementations *********************/
785 /* only in editmode */
786 static void vgroup_select_verts(Object *ob, int select)
788 const int def_nr = ob->actdef - 1;
791 if (!BLI_findlink(&ob->defbase, def_nr)) {
795 if (ob->type == OB_MESH) {
798 if (me->edit_btmesh) {
799 BMEditMesh *em = me->edit_btmesh;
803 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
804 if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN)) {
805 dv = CustomData_bmesh_get(&em->bm->vdata, eve->head.data, CD_MDEFORMVERT);
806 if (defvert_find_index(dv, def_nr)) {
807 BM_vert_select_set(em->bm, eve, select);
812 /* this has to be called, because this function operates on vertices only */
813 if (select) EDBM_select_flush(em); // vertices to edges/faces
814 else EDBM_deselect_flush(em);
825 for (i = 0; i < me->totvert; i++, mv++, dv++) {
826 if (!(mv->flag & ME_HIDE)) {
827 if (defvert_find_index(dv, def_nr)) {
828 if (select) mv->flag |= SELECT;
829 else mv->flag &= ~SELECT;
834 paintvert_flush_flags(ob);
838 else if (ob->type == OB_LATTICE) {
839 Lattice *lt = vgroup_edit_lattice(ob);
847 tot = lt->pntsu * lt->pntsv * lt->pntsw;
848 for (a = 0, bp = lt->def; a < tot; a++, bp++, dv++) {
849 if (defvert_find_index(dv, def_nr)) {
850 if (select) bp->f1 |= SELECT;
851 else bp->f1 &= ~SELECT;
858 static void vgroup_duplicate(Object *ob)
860 bDeformGroup *dg, *cdg;
861 char name[sizeof(dg->name)];
862 MDeformWeight *dw_org, *dw_cpy;
863 MDeformVert **dvert_array = NULL;
864 int i, idg, icdg, dvert_tot = 0;
866 dg = BLI_findlink(&ob->defbase, (ob->actdef - 1));
870 if (!strstr(dg->name, "_copy")) {
871 BLI_snprintf(name, sizeof(name), "%s_copy", dg->name);
874 BLI_snprintf(name, sizeof(name), "%s", dg->name);
877 cdg = defgroup_duplicate(dg);
878 BLI_strncpy(cdg->name, name, sizeof(cdg->name));
879 defgroup_unique_name(cdg, ob);
881 BLI_addtail(&ob->defbase, cdg);
883 idg = (ob->actdef - 1);
884 ob->actdef = BLI_countlist(&ob->defbase);
885 icdg = (ob->actdef - 1);
887 /* TODO, we might want to allow only copy selected verts here? - campbell */
888 ED_vgroup_give_parray(ob->data, &dvert_array, &dvert_tot, FALSE);
891 for (i = 0; i < dvert_tot; i++) {
892 MDeformVert *dv = dvert_array[i];
893 dw_org = defvert_find_index(dv, idg);
895 /* defvert_verify_index re-allocs org so need to store the weight first */
896 dw_cpy = defvert_verify_index(dv, icdg);
897 dw_cpy->weight = dw_org->weight;
901 MEM_freeN(dvert_array);
905 static void vgroup_normalize(Object *ob)
908 MDeformVert *dv, **dvert_array = NULL;
909 int i, dvert_tot = 0;
910 const int def_nr = ob->actdef - 1;
912 const int use_vert_sel = (ob->type == OB_MESH && ((Mesh *)ob->data)->editflag & ME_EDIT_VERT_SEL) != 0;
914 if (!BLI_findlink(&ob->defbase, def_nr)) {
918 ED_vgroup_give_parray(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
921 float weight_max = 0.0f;
923 for (i = 0; i < dvert_tot; i++) {
925 /* in case its not selected */
926 if (!(dv = dvert_array[i])) {
930 dw = defvert_find_index(dv, def_nr);
932 weight_max = MAX2(dw->weight, weight_max);
936 if (weight_max > 0.0f) {
937 for (i = 0; i < dvert_tot; i++) {
939 /* in case its not selected */
940 if (!(dv = dvert_array[i])) {
944 dw = defvert_find_index(dv, def_nr);
946 dw->weight /= weight_max;
948 /* in case of division errors with very low weights */
949 CLAMP(dw->weight, 0.0f, 1.0f);
954 MEM_freeN(dvert_array);
958 /* This finds all of the vertices face-connected to vert by an edge and returns a
959 * MEM_allocated array of indices of size count.
960 * count is an int passed by reference so it can be assigned the value of the length here. */
961 static int *getSurroundingVerts(Mesh *me, int vert, int *count)
963 MPoly *mp = me->mpoly;
965 /* Instead of looping twice on all polys and loops, and use a temp array, let's rather
966 * use a BLI_array, with a reasonable starting/reserved size (typically, there are not
967 * many vertices face-linked to another one, even 8 might be too high...). */
969 BLI_array_declare(verts);
971 BLI_array_reserve(verts, 8);
974 int first_l = mp->totloop - 1;
975 MLoop *ml = &me->mloop[mp->loopstart];
977 /* XXX This assume a vert can only be once in a poly, even though
978 * it seems logical to me, not totally sure of that. */
982 /* We are on the first corner. */
987 /* We are on the last corner. */
989 b = me->mloop[mp->loopstart].v;
996 /* Append a and b verts to array, if not yet present. */
997 k = BLI_array_count(verts);
998 /* XXX Maybe a == b is enough? */
999 while (k-- && !(a == b && a == -1)) {
1002 else if (verts[k] == b)
1006 BLI_array_append(verts, a);
1008 BLI_array_append(verts, b);
1010 /* Vert found in this poly, we can go to next one! */
1018 /* Do not free the array! */
1019 *count = BLI_array_count(verts);
1023 /* get a single point in space by averaging a point cloud (vectors of size 3)
1024 * coord is the place the average is stored, points is the point cloud, count is the number of points in the cloud
1026 static void getSingleCoordinate(MVert *points, int count, float coord[3])
1030 for (i = 0; i < count; i++) {
1031 add_v3_v3(coord, points[i].co);
1033 mul_v3_fl(coord, 1.0f / count);
1036 /* given a plane and a start and end position,
1037 * compute the amount of vertical distance relative to the plane and store it in dists,
1038 * then get the horizontal and vertical change and store them in changes
1040 static void getVerticalAndHorizontalChange(const float norm[3], float d, const float coord[3],
1041 const float start[3], float distToStart,
1042 float *end, float (*changes)[2], float *dists, int index)
1045 // D = (a*x0 + b*y0 +c*z0 +d)
1046 float projA[3], projB[3];
1048 closest_to_plane_v3(projA, coord, norm, start);
1049 closest_to_plane_v3(projB, coord, norm, end);
1050 // (vertical and horizontal refer to the plane's y and xz respectively)
1051 // vertical distance
1052 dists[index] = dot_v3v3(norm, end) + d;
1054 changes[index][0] = dists[index] - distToStart;
1055 //printf("vc %f %f\n", distance(end, projB, 3)-distance(start, projA, 3), changes[index][0]);
1056 // horizontal change
1057 changes[index][1] = len_v3v3(projA, projB);
1060 // I need the derived mesh to be forgotten so the positions are recalculated with weight changes (see dm_deform_recalc)
1061 static void dm_deform_clear(DerivedMesh *dm, Object *ob)
1063 if (ob->derivedDeform && (ob->derivedDeform) == dm) {
1064 ob->derivedDeform->needsFree = 1;
1065 ob->derivedDeform->release(ob->derivedDeform);
1066 ob->derivedDeform = NULL;
1074 /* recalculate the deformation */
1075 static DerivedMesh *dm_deform_recalc(Scene *scene, Object *ob)
1077 return mesh_get_derived_deform(scene, ob, CD_MASK_BAREMESH);
1080 /* by changing nonzero weights, try to move a vertex in me->mverts with index 'index' to
1081 * distToBe distance away from the provided plane strength can change distToBe so that it moves
1082 * towards distToBe by that percentage cp changes how much the weights are adjusted
1083 * to check the distance
1085 * index is the index of the vertex being moved
1086 * norm and d are the plane's properties for the equation: ax + by + cz + d = 0
1087 * coord is a point on the plane
1089 static void moveCloserToDistanceFromPlane(Scene *scene, Object *ob, Mesh *me, int index, float norm[3],
1090 float coord[3], float d, float distToBe, float strength, float cp)
1095 MDeformVert *dvert = me->dvert + index;
1096 int totweight = dvert->totweight;
1098 float oldPos[3] = {0};
1099 float vc, hc, dist = 0.0f;
1101 float (*changes)[2] = MEM_mallocN(sizeof(float *) * totweight * 2, "vertHorzChange");
1102 float *dists = MEM_mallocN(sizeof(float) * totweight, "distance");
1104 /* track if up or down moved it closer for each bone */
1105 int *upDown = MEM_callocN(sizeof(int) * totweight, "upDownTracker");
1107 int *dwIndices = MEM_callocN(sizeof(int) * totweight, "dwIndexTracker");
1113 float originalDistToBe = distToBe;
1116 dm = dm_deform_recalc(scene, ob);
1117 dm->getVert(dm, index, &m);
1118 copy_v3_v3(oldPos, m.co);
1119 distToStart = dot_v3v3(norm, oldPos) + d;
1121 if (distToBe == originalDistToBe) {
1122 distToBe += distToStart - distToStart * strength;
1124 for (i = 0; i < totweight; i++) {
1126 dw = (dvert->dw + i);
1131 dists[i] = distToStart;
1134 for (k = 0; k < 2; k++) {
1136 dm_deform_clear(dm, ob); dm = NULL;
1140 dw->weight *= 1 + cp;
1143 dw->weight /= 1 + cp;
1145 if (dw->weight == oldw) {
1148 dists[i] = distToStart;
1151 if (dw->weight > 1) {
1154 dm = dm_deform_recalc(scene, ob);
1155 dm->getVert(dm, index, &m);
1156 getVerticalAndHorizontalChange(norm, d, coord, oldPos, distToStart, m.co, changes, dists, i);
1164 if (fabs(dist - distToBe) < fabs(dists[i] - distToBe)) {
1173 if (fabs(dists[i] - distToBe) > fabs(distToStart - distToBe)) {
1176 dists[i] = distToStart;
1181 // sort the changes by the vertical change
1182 for (k = 0; k < totweight; k++) {
1186 for (i = k + 1; i < totweight; i++) {
1189 if (fabs(dist) > fabs(dists[i])) {
1194 if (bestIndex != k) {
1196 upDown[k] = upDown[bestIndex];
1197 upDown[bestIndex] = ti;
1200 dwIndices[k] = dwIndices[bestIndex];
1201 dwIndices[bestIndex] = ti;
1204 changes[k][0] = changes[bestIndex][0];
1205 changes[bestIndex][0] = tf;
1208 changes[k][1] = changes[bestIndex][1];
1209 changes[bestIndex][1] = tf;
1212 dists[k] = dists[bestIndex];
1213 dists[bestIndex] = tf;
1217 // find the best change with an acceptable horizontal change
1218 for (i = 0; i < totweight; i++) {
1219 if (fabs(changes[i][0]) > fabs(changes[i][1] * 2.0f)) {
1224 if (bestIndex != -1) {
1226 // it is a good place to stop if it tries to move the opposite direction
1227 // (relative to the plane) of last time
1228 if (lastIndex != -1) {
1229 if (wasUp != upDown[bestIndex]) {
1233 lastIndex = bestIndex;
1234 wasUp = upDown[bestIndex];
1235 dw = (dvert->dw + dwIndices[bestIndex]);
1237 if (upDown[bestIndex]) {
1238 dw->weight *= 1 + cp;
1241 dw->weight /= 1 + cp;
1243 if (dw->weight > 1) {
1246 if (oldw == dw->weight) {
1250 dm_deform_clear(dm, ob); dm = NULL;
1253 } while (wasChange && ((distToStart - distToBe) / fabsf(distToStart - distToBe) ==
1254 (dists[bestIndex] - distToBe) / fabsf(dists[bestIndex] - distToBe)));
1259 MEM_freeN(dwIndices);
1262 /* this is used to try to smooth a surface by only adjusting the nonzero weights of a vertex
1263 * but it could be used to raise or lower an existing 'bump.' */
1264 static void vgroup_fix(Scene *scene, Object *ob, float distToBe, float strength, float cp)
1268 Mesh *me = ob->data;
1269 MVert *mvert = me->mvert;
1271 if (!(me->editflag & ME_EDIT_VERT_SEL))
1273 for (i = 0; i < me->totvert && mvert; i++, mvert++) {
1274 if (mvert->flag & SELECT) {
1276 if ((verts = getSurroundingVerts(me, i, &count))) {
1278 MVert *p = MEM_callocN(sizeof(MVert) * (count), "deformedPoints");
1281 DerivedMesh *dm = mesh_get_derived_deform(scene, ob, CD_MASK_BAREMESH);
1284 dm->getVert(dm, verts[k], &m);
1289 float d /*, dist */ /* UNUSED */, mag;
1292 getSingleCoordinate(p, count, coord);
1293 dm->getVert(dm, i, &m);
1294 sub_v3_v3v3(norm, m.co, coord);
1295 mag = normalize_v3(norm);
1296 if (mag) { /* zeros fix */
1297 d = -dot_v3v3(norm, coord);
1298 /* dist = (dot_v3v3(norm, m.co) + d); */ /* UNUSED */
1299 moveCloserToDistanceFromPlane(scene, ob, me, i, norm, coord, d, distToBe, strength, cp);
1310 static void vgroup_levels(Object *ob, float offset, float gain)
1313 MDeformVert *dv, **dvert_array = NULL;
1314 int i, dvert_tot = 0;
1315 const int def_nr = ob->actdef - 1;
1317 const int use_vert_sel = (ob->type == OB_MESH && ((Mesh *)ob->data)->editflag & ME_EDIT_VERT_SEL) != 0;
1319 if (!BLI_findlink(&ob->defbase, def_nr)) {
1323 ED_vgroup_give_parray(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
1326 for (i = 0; i < dvert_tot; i++) {
1328 /* in case its not selected */
1329 if (!(dv = dvert_array[i])) {
1333 dw = defvert_find_index(dv, def_nr);
1335 dw->weight = gain * (dw->weight + offset);
1337 CLAMP(dw->weight, 0.0f, 1.0f);
1341 MEM_freeN(dvert_array);
1345 /* TODO - select between groups */
1346 static void vgroup_normalize_all(Object *ob, int lock_active)
1348 MDeformVert *dv, **dvert_array = NULL;
1349 int i, dvert_tot = 0;
1350 const int def_nr = ob->actdef - 1;
1352 const int use_vert_sel = (ob->type == OB_MESH && ((Mesh *)ob->data)->editflag & ME_EDIT_VERT_SEL) != 0;
1354 if (lock_active && !BLI_findlink(&ob->defbase, def_nr)) {
1358 ED_vgroup_give_parray(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
1363 for (i = 0; i < dvert_tot; i++) {
1364 /* in case its not selected */
1365 if (!(dv = dvert_array[i])) {
1369 defvert_normalize_lock(dv, def_nr);
1373 for (i = 0; i < dvert_tot; i++) {
1375 /* in case its not selected */
1376 if (!(dv = dvert_array[i])) {
1380 defvert_normalize(dv);
1384 MEM_freeN(dvert_array);
1389 static void vgroup_lock_all(Object *ob, int action)
1393 if (action == SEL_TOGGLE) {
1394 action = SEL_SELECT;
1395 for (dg = ob->defbase.first; dg; dg = dg->next) {
1396 if (dg->flag & DG_LOCK_WEIGHT) {
1397 action = SEL_DESELECT;
1403 for (dg = ob->defbase.first; dg; dg = dg->next) {
1406 dg->flag |= DG_LOCK_WEIGHT;
1409 dg->flag &= ~DG_LOCK_WEIGHT;
1412 dg->flag ^= DG_LOCK_WEIGHT;
1418 static void vgroup_invert(Object *ob, const short auto_assign, const short auto_remove)
1421 MDeformVert *dv, **dvert_array = NULL;
1422 int i, dvert_tot = 0;
1423 const int def_nr = ob->actdef - 1;
1424 const int use_vert_sel = (ob->type == OB_MESH && ((Mesh *)ob->data)->editflag & ME_EDIT_VERT_SEL) != 0;
1426 if (!BLI_findlink(&ob->defbase, def_nr)) {
1430 ED_vgroup_give_parray(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
1433 for (i = 0; i < dvert_tot; i++) {
1435 /* in case its not selected */
1436 if (!(dv = dvert_array[i])) {
1441 dw = defvert_verify_index(dv, def_nr);
1444 dw = defvert_find_index(dv, def_nr);
1448 dw->weight = 1.0f - dw->weight;
1450 if (auto_remove && dw->weight <= 0.0f) {
1451 defvert_remove_group(dv, dw);
1456 MEM_freeN(dvert_array);
1460 static void vgroup_blend(Object *ob, const float fac)
1464 int i, dvert_tot = 0;
1465 const int def_nr = ob->actdef - 1;
1467 BLI_assert(fac >= 0.0f && fac <= 1.0f);
1469 if (ob->type != OB_MESH) {
1473 if (BLI_findlink(&ob->defbase, def_nr)) {
1474 const float ifac = 1.0f - fac;
1476 BMEditMesh *em = BMEdit_FromObject(ob);
1477 BMesh *bm = em ? em->bm : NULL;
1478 Mesh *me = em ? NULL : ob->data;
1486 MDeformVert *dvert_array = NULL;
1494 BM_mesh_elem_index_ensure(bm, BM_VERT);
1495 dvert_tot = bm->totvert;
1498 dvert_tot = me->totvert;
1499 dvert_array = me->dvert;
1502 vg_weights = MEM_callocN(sizeof(float) * dvert_tot, "vgroup_blend_f");
1503 vg_users = MEM_callocN(sizeof(int) * dvert_tot, "vgroup_blend_i");
1506 BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
1507 sel1 = BM_elem_flag_test(eed->v1, BM_ELEM_SELECT);
1508 sel2 = BM_elem_flag_test(eed->v2, BM_ELEM_SELECT);
1512 /* i1 is always the selected one */
1514 i1 = BM_elem_index_get(eed->v1);
1515 /* i2= BM_elem_index_get(eed->v2); */ /* UNUSED */
1519 /* i2= BM_elem_index_get(eed->v1); */ /* UNUSED */
1520 i1 = BM_elem_index_get(eed->v2);
1524 dv = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_MDEFORMVERT);
1525 dw = defvert_find_index(dv, def_nr);
1527 vg_weights[i1] += dw->weight;
1533 BM_ITER_MESH_INDEX (eve, &iter, bm, BM_VERTS_OF_MESH, i) {
1534 if (BM_elem_flag_test(eve, BM_ELEM_SELECT) && vg_users[i] > 0) {
1535 dv = CustomData_bmesh_get(&bm->vdata, eve->head.data, CD_MDEFORMVERT);
1537 dw = defvert_verify_index(dv, def_nr);
1538 dw->weight = (fac * (vg_weights[i] / (float)vg_users[i])) + (ifac * dw->weight);
1539 /* in case of division errors */
1540 CLAMP(dw->weight, 0.0f, 1.0f);
1545 MEdge *ed = me->medge;
1548 for (i = 0; i < me->totedge; i++, ed++) {
1549 sel1 = me->mvert[ed->v1].flag & SELECT;
1550 sel2 = me->mvert[ed->v2].flag & SELECT;
1554 /* i1 is always the selected one */
1564 dv = &dvert_array[i2];
1565 dw = defvert_find_index(dv, def_nr);
1567 vg_weights[i1] += dw->weight;
1576 for (i = 0; i < dvert_tot; i++, mv++, dv++) {
1577 if ((mv->flag & SELECT) && (vg_users[i] > 0)) {
1578 dw = defvert_verify_index(dv, def_nr);
1579 dw->weight = (fac * (vg_weights[i] / (float)vg_users[i])) + (ifac * dw->weight);
1581 /* in case of division errors */
1582 CLAMP(dw->weight, 0.0f, 1.0f);
1587 MEM_freeN(vg_weights);
1588 MEM_freeN(vg_users);
1592 static void vgroup_clean(Object *ob, const float epsilon, int keep_single)
1595 MDeformVert *dv, **dvert_array = NULL;
1596 int i, dvert_tot = 0;
1597 const int def_nr = ob->actdef - 1;
1598 const int use_vert_sel = (ob->type == OB_MESH && ((Mesh *)ob->data)->editflag & ME_EDIT_VERT_SEL) != 0;
1600 if (!BLI_findlink(&ob->defbase, def_nr)) {
1604 ED_vgroup_give_parray(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
1607 /* only the active group */
1608 for (i = 0; i < dvert_tot; i++) {
1610 /* in case its not selected */
1611 if (!(dv = dvert_array[i])) {
1615 dw = defvert_find_index(dv, def_nr);
1618 if (dw->weight <= epsilon) {
1619 if (keep_single == FALSE || dv->totweight > 1) {
1620 defvert_remove_group(dv, dw); /* dw can be NULL */
1626 MEM_freeN(dvert_array);
1630 static void vgroup_clean_all(Object *ob, const float epsilon, const int keep_single)
1632 MDeformVert **dvert_array = NULL;
1633 int i, dvert_tot = 0;
1634 const int use_vert_sel = (ob->type == OB_MESH && ((Mesh *)ob->data)->editflag & ME_EDIT_VERT_SEL) != 0;
1636 ED_vgroup_give_parray(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
1642 for (i = 0; i < dvert_tot; i++) {
1645 /* in case its not selected */
1646 if (!(dv = dvert_array[i])) {
1654 if (keep_single && dv->totweight == 1)
1659 if (dw->weight <= epsilon) {
1660 defvert_remove_group(dv, dw);
1665 MEM_freeN(dvert_array);
1670 static void dvert_mirror_op(MDeformVert *dvert, MDeformVert *dvert_mirr,
1671 const char sel, const char sel_mirr,
1672 const int *flip_map, const int flip_map_len,
1673 const short mirror_weights, const short flip_vgroups,
1674 const short all_vgroups, const int act_vgroup)
1676 BLI_assert(sel || sel_mirr);
1678 if (sel_mirr && sel) {
1680 if (mirror_weights) {
1682 SWAP(MDeformVert, *dvert, *dvert_mirr);
1685 MDeformWeight *dw = defvert_find_index(dvert, act_vgroup);
1686 MDeformWeight *dw_mirr = defvert_find_index(dvert_mirr, act_vgroup);
1688 if (dw || dw_mirr) {
1689 if (dw_mirr == NULL)
1690 dw_mirr = defvert_verify_index(dvert_mirr, act_vgroup);
1692 dw = defvert_verify_index(dvert, act_vgroup);
1694 SWAP(float, dw->weight, dw_mirr->weight);
1700 defvert_flip(dvert, flip_map, flip_map_len);
1701 defvert_flip(dvert_mirr, flip_map, flip_map_len);
1705 /* dvert should always be the target, only swaps pointer */
1707 SWAP(MDeformVert *, dvert, dvert_mirr);
1710 if (mirror_weights) {
1712 defvert_copy(dvert, dvert_mirr);
1715 defvert_copy_index(dvert, dvert_mirr, act_vgroup);
1719 /* flip map already modified for 'all_vgroups' */
1721 defvert_flip(dvert, flip_map, flip_map_len);
1726 /* TODO, vgroup locking */
1727 /* TODO, face masking */
1728 void ED_vgroup_mirror(Object *ob, const short mirror_weights, const short flip_vgroups, const short all_vgroups)
1731 #define VGROUP_MIRR_OP \
1732 dvert_mirror_op(dvert, dvert_mirr, \
1734 flip_map, flip_map_len, \
1735 mirror_weights, flip_vgroups, \
1736 all_vgroups, def_nr \
1739 BMVert *eve, *eve_mirr;
1740 MDeformVert *dvert, *dvert_mirr;
1741 short sel, sel_mirr;
1742 int *flip_map, flip_map_len;
1743 const int def_nr = ob->actdef - 1;
1745 if ( (mirror_weights == 0 && flip_vgroups == 0) ||
1746 (BLI_findlink(&ob->defbase, def_nr) == NULL) )
1752 flip_map = all_vgroups ?
1753 defgroup_flip_map(ob, &flip_map_len, FALSE) :
1754 defgroup_flip_map_single(ob, &flip_map_len, FALSE, def_nr);
1756 BLI_assert(flip_map != NULL);
1758 if (flip_map == NULL) {
1759 /* something went wrong!, possibly no groups */
1768 /* only the active group */
1769 if (ob->type == OB_MESH) {
1770 Mesh *me = ob->data;
1771 BMEditMesh *em = me->edit_btmesh;
1776 if (!CustomData_has_layer(&em->bm->vdata, CD_MDEFORMVERT)) {
1780 EDBM_verts_mirror_cache_begin(em, FALSE);
1782 /* Go through the list of editverts and assign them */
1783 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
1784 if ((eve_mirr = EDBM_verts_mirror_get(em, eve))) {
1785 sel = BM_elem_flag_test(eve, BM_ELEM_SELECT);
1786 sel_mirr = BM_elem_flag_test(eve_mirr, BM_ELEM_SELECT);
1788 if ((sel || sel_mirr) && (eve != eve_mirr)) {
1789 dvert = CustomData_bmesh_get(&em->bm->vdata, eve->head.data, CD_MDEFORMVERT);
1790 dvert_mirr = CustomData_bmesh_get(&em->bm->vdata, eve_mirr->head.data, CD_MDEFORMVERT);
1791 if (dvert && dvert_mirr) {
1796 /* don't use these again */
1797 EDBM_verts_mirror_cache_clear(em, eve);
1798 EDBM_verts_mirror_cache_clear(em, eve_mirr);
1801 EDBM_verts_mirror_cache_end(em);
1804 /* object mode / weight paint */
1805 MVert *mv, *mv_mirr;
1806 int vidx, vidx_mirr;
1807 const int use_vert_sel = (me->editflag & ME_EDIT_VERT_SEL) != 0;
1809 if (me->dvert == NULL) {
1813 if (!use_vert_sel) {
1814 sel = sel_mirr = TRUE;
1817 /* tag verts we have used */
1818 for (vidx = 0, mv = me->mvert; vidx < me->totvert; vidx++, mv++) {
1819 mv->flag &= ~ME_VERT_TMP_TAG;
1822 for (vidx = 0, mv = me->mvert; vidx < me->totvert; vidx++, mv++) {
1823 if ( ((mv->flag & ME_VERT_TMP_TAG) == 0) &&
1824 ((vidx_mirr = mesh_get_x_mirror_vert(ob, vidx)) != -1) &&
1825 (vidx != vidx_mirr) &&
1826 ((((mv_mirr = me->mvert + vidx_mirr)->flag) & ME_VERT_TMP_TAG) == 0))
1830 sel = mv->flag & SELECT;
1831 sel_mirr = mv_mirr->flag & SELECT;
1834 if (sel || sel_mirr) {
1835 dvert = &me->dvert[vidx];
1836 dvert_mirr = &me->dvert[vidx_mirr];
1841 mv->flag |= ME_VERT_TMP_TAG;
1842 mv_mirr->flag |= ME_VERT_TMP_TAG;
1847 else if (ob->type == OB_LATTICE) {
1848 Lattice *lt = vgroup_edit_lattice(ob);
1852 /* half but found up odd value */
1854 if (lt->pntsu == 1 || lt->dvert == NULL) {
1858 /* unlike editmesh we know that by only looping over the first half of
1859 * the 'u' indices it will cover all points except the middle which is
1860 * ok in this case */
1861 pntsu_half = lt->pntsu / 2;
1863 for (w = 0; w < lt->pntsw; w++) {
1864 for (v = 0; v < lt->pntsv; v++) {
1865 for (u = 0; u < pntsu_half; u++) {
1866 int u_inv = (lt->pntsu - 1) - u;
1868 BPoint *bp, *bp_mirr;
1870 i1 = LT_INDEX(lt, u, v, w);
1871 i2 = LT_INDEX(lt, u_inv, v, w);
1874 bp_mirr = <->def[i2];
1876 sel = bp->f1 & SELECT;
1877 sel_mirr = bp_mirr->f1 & SELECT;
1879 if (sel || sel_mirr) {
1880 dvert = <->dvert[i1];
1881 dvert_mirr = <->dvert[i2];
1892 if (flip_map) MEM_freeN(flip_map);
1894 #undef VGROUP_MIRR_OP
1898 static void vgroup_remap_update_users(Object *ob, int *map)
1900 ExplodeModifierData *emd;
1902 ParticleSystem *psys;
1903 ClothModifierData *clmd;
1904 ClothSimSettings *clsim;
1907 /* these cases don't use names to refer to vertex groups, so when
1908 * they get deleted the numbers get out of sync, this corrects that */
1911 ob->soft->vertgroup = map[ob->soft->vertgroup];
1913 for (md = ob->modifiers.first; md; md = md->next) {
1914 if (md->type == eModifierType_Explode) {
1915 emd = (ExplodeModifierData *)md;
1916 emd->vgroup = map[emd->vgroup];
1918 else if (md->type == eModifierType_Cloth) {
1919 clmd = (ClothModifierData *)md;
1920 clsim = clmd->sim_parms;
1923 clsim->vgroup_mass = map[clsim->vgroup_mass];
1924 clsim->vgroup_bend = map[clsim->vgroup_bend];
1925 clsim->vgroup_struct = map[clsim->vgroup_struct];
1930 for (psys = ob->particlesystem.first; psys; psys = psys->next) {
1931 for (a = 0; a < PSYS_TOT_VG; a++)
1932 psys->vgroup[a] = map[psys->vgroup[a]];
1937 static void vgroup_delete_update_users(Object *ob, int id)
1939 int i, defbase_tot = BLI_countlist(&ob->defbase) + 1;
1940 int *map = MEM_mallocN(sizeof(int) * defbase_tot, "vgroup del");
1942 map[id] = map[0] = 0;
1943 for (i = 1; i < id; i++) map[i] = i;
1944 for (i = id + 1; i < defbase_tot; i++) map[i] = i - 1;
1946 vgroup_remap_update_users(ob, map);
1951 static void vgroup_delete_object_mode(Object *ob, bDeformGroup *dg)
1953 MDeformVert *dvert_array = NULL;
1955 const int def_nr = BLI_findindex(&ob->defbase, dg);
1957 assert(def_nr > -1);
1959 ED_vgroup_give_array(ob->data, &dvert_array, &dvert_tot);
1964 for (i = 0, dv = dvert_array; i < dvert_tot; i++, dv++) {
1967 dw = defvert_find_index(dv, def_nr);
1968 defvert_remove_group(dv, dw); /* dw can be NULL */
1970 /* inline, make into a function if anything else needs to do this */
1971 for (j = 0; j < dv->totweight; j++) {
1972 if (dv->dw[j].def_nr > def_nr) {
1980 vgroup_delete_update_users(ob, def_nr + 1);
1982 /* Remove the group */
1983 BLI_freelinkN(&ob->defbase, dg);
1985 /* Update the active deform index if necessary */
1986 if (ob->actdef > def_nr)
1988 if (ob->actdef < 1 && ob->defbase.first)
1993 /* only in editmode */
1994 /* removes from active defgroup, if allverts==0 only selected vertices */
1995 static void vgroup_active_remove_verts(Object *ob, const int allverts, bDeformGroup *dg)
1998 const int def_nr = BLI_findindex(&ob->defbase, dg);
2000 if (ob->type == OB_MESH) {
2001 Mesh *me = ob->data;
2003 if (me->edit_btmesh) {
2004 BMEditMesh *em = me->edit_btmesh;
2008 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
2009 dv = CustomData_bmesh_get(&em->bm->vdata, eve->head.data, CD_MDEFORMVERT);
2011 if (dv && dv->dw && (allverts || BM_elem_flag_test(eve, BM_ELEM_SELECT))) {
2012 MDeformWeight *dw = defvert_find_index(dv, def_nr);
2013 defvert_remove_group(dv, dw); /* dw can be NULL */
2023 ED_vgroup_data_create(&me->id);
2029 for (i = 0; i < me->totvert; i++, mv++, dv++) {
2030 if (mv->flag & SELECT) {
2031 if (dv->dw && (allverts || (mv->flag & SELECT))) {
2032 MDeformWeight *dw = defvert_find_index(dv, def_nr);
2033 defvert_remove_group(dv, dw); /* dw can be NULL */
2039 else if (ob->type == OB_LATTICE) {
2040 Lattice *lt = vgroup_edit_lattice(ob);
2044 int i, tot = lt->pntsu * lt->pntsv * lt->pntsw;
2046 for (i = 0, bp = lt->def; i < tot; i++, bp++) {
2047 if (allverts || (bp->f1 & SELECT)) {
2052 dw = defvert_find_index(dv, def_nr);
2053 defvert_remove_group(dv, dw); /* dw can be NULL */
2060 static void vgroup_delete_edit_mode(Object *ob, bDeformGroup *dg)
2063 const int dg_index = BLI_findindex(&ob->defbase, dg);
2065 assert(dg_index > -1);
2067 /* Make sure that no verts are using this group */
2068 vgroup_active_remove_verts(ob, TRUE, dg);
2070 /* Make sure that any verts with higher indices are adjusted accordingly */
2071 if (ob->type == OB_MESH) {
2072 Mesh *me = ob->data;
2073 BMEditMesh *em = me->edit_btmesh;
2078 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
2079 dvert = CustomData_bmesh_get(&em->bm->vdata, eve->head.data, CD_MDEFORMVERT);
2082 for (i = 0; i < dvert->totweight; i++)
2083 if (dvert->dw[i].def_nr > dg_index)
2084 dvert->dw[i].def_nr--;
2087 else if (ob->type == OB_LATTICE) {
2088 Lattice *lt = vgroup_edit_lattice(ob);
2090 MDeformVert *dvert = lt->dvert;
2094 tot = lt->pntsu * lt->pntsv * lt->pntsw;
2095 for (a = 0, bp = lt->def; a < tot; a++, bp++, dvert++) {
2096 for (i = 0; i < dvert->totweight; i++) {
2097 if (dvert->dw[i].def_nr > dg_index)
2098 dvert->dw[i].def_nr--;
2104 vgroup_delete_update_users(ob, dg_index + 1);
2106 /* Remove the group */
2107 BLI_freelinkN(&ob->defbase, dg);
2109 /* Update the active deform index if necessary */
2110 if (ob->actdef > dg_index)
2112 if (ob->actdef < 1 && ob->defbase.first)
2115 /* remove all dverts */
2116 if (ob->defbase.first == NULL) {
2117 if (ob->type == OB_MESH) {
2118 Mesh *me = ob->data;
2119 CustomData_free_layer_active(&me->vdata, CD_MDEFORMVERT, me->totvert);
2122 else if (ob->type == OB_LATTICE) {
2123 Lattice *lt = vgroup_edit_lattice(ob);
2125 MEM_freeN(lt->dvert);
2132 static int vgroup_object_in_edit_mode(Object *ob)
2134 if (ob->type == OB_MESH)
2135 return (BMEdit_FromObject(ob) != NULL);
2136 else if (ob->type == OB_LATTICE)
2137 return (((Lattice *)ob->data)->editlatt != NULL);
2142 static int vgroup_object_in_wpaint_vert_select(Object *ob)
2144 if (ob->type == OB_MESH) {
2145 Mesh *me = ob->data;
2146 return ( (ob->mode & OB_MODE_WEIGHT_PAINT) &&
2147 (me->edit_btmesh == NULL) &&
2148 (ME_EDIT_PAINT_SEL_MODE(me) == SCE_SELECT_VERTEX) );
2154 static void vgroup_delete(Object *ob)
2156 bDeformGroup *dg = BLI_findlink(&ob->defbase, ob->actdef - 1);
2160 if (vgroup_object_in_edit_mode(ob))
2161 vgroup_delete_edit_mode(ob, dg);
2163 vgroup_delete_object_mode(ob, dg);
2166 static void vgroup_delete_all(Object *ob)
2168 /* Remove all DVerts */
2169 if (ob->type == OB_MESH) {
2170 Mesh *me = ob->data;
2171 CustomData_free_layer_active(&me->vdata, CD_MDEFORMVERT, me->totvert);
2174 else if (ob->type == OB_LATTICE) {
2175 Lattice *lt = vgroup_edit_lattice(ob);
2177 MEM_freeN(lt->dvert);
2182 /* Remove all DefGroups */
2183 BLI_freelistN(&ob->defbase);
2185 /* Fix counters/indices */
2189 /* only in editmode */
2190 static void vgroup_assign_verts(Object *ob, const float weight)
2193 const int def_nr = ob->actdef - 1;
2195 if (!BLI_findlink(&ob->defbase, def_nr))
2198 if (ob->type == OB_MESH) {
2199 Mesh *me = ob->data;
2201 if (me->edit_btmesh) {
2202 BMEditMesh *em = me->edit_btmesh;
2206 if (!CustomData_has_layer(&em->bm->vdata, CD_MDEFORMVERT))
2207 BM_data_layer_add(em->bm, &em->bm->vdata, CD_MDEFORMVERT);
2209 /* Go through the list of editverts and assign them */
2210 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
2211 if (BM_elem_flag_test(eve, BM_ELEM_SELECT)) {
2213 dv = CustomData_bmesh_get(&em->bm->vdata, eve->head.data, CD_MDEFORMVERT); /* can be NULL */
2214 dw = defvert_verify_index(dv, def_nr);
2216 dw->weight = weight;
2227 ED_vgroup_data_create(&me->id);
2233 for (i = 0; i < me->totvert; i++, mv++, dv++) {
2234 if (mv->flag & SELECT) {
2236 dw = defvert_verify_index(dv, def_nr);
2238 dw->weight = weight;
2244 else if (ob->type == OB_LATTICE) {
2245 Lattice *lt = vgroup_edit_lattice(ob);
2249 if (lt->dvert == NULL)
2250 ED_vgroup_data_create(<->id);
2254 tot = lt->pntsu * lt->pntsv * lt->pntsw;
2255 for (a = 0, bp = lt->def; a < tot; a++, bp++, dv++) {
2256 if (bp->f1 & SELECT) {
2259 dw = defvert_verify_index(dv, def_nr);
2261 dw->weight = weight;
2268 /* only in editmode */
2269 /* removes from all defgroup, if allverts==0 only selected vertices */
2270 static void vgroup_remove_verts(Object *ob, int allverts)
2272 /* To prevent code redundancy, we just use vgroup_active_remove_verts, but that
2273 * only operates on the active vgroup. So we iterate through all groups, by changing
2274 * active group index
2277 for (dg = ob->defbase.first; dg; dg = dg->next) {
2278 vgroup_active_remove_verts(ob, allverts, dg);
2282 /********************** vertex group operators *********************/
2284 static int vertex_group_poll(bContext *C)
2286 Object *ob = ED_object_context(C);
2287 ID *data = (ob) ? ob->data : NULL;
2288 return (ob && !ob->id.lib && OB_TYPE_SUPPORT_VGROUP(ob->type) && data && !data->lib);
2291 static int UNUSED_FUNCTION(vertex_group_poll_edit) (bContext * C)
2293 Object *ob = ED_object_context(C);
2294 ID *data = (ob) ? ob->data : NULL;
2296 if (!(ob && !ob->id.lib && data && !data->lib))
2299 return vgroup_object_in_edit_mode(ob);
2302 /* editmode _or_ weight paint vertex sel */
2303 static int vertex_group_poll_edit_or_wpaint_vert_select(bContext *C)
2305 Object *ob = ED_object_context(C);
2306 ID *data = (ob) ? ob->data : NULL;
2308 if (!(ob && !ob->id.lib && data && !data->lib))
2311 return (vgroup_object_in_edit_mode(ob) ||
2312 vgroup_object_in_wpaint_vert_select(ob));
2315 static int vertex_group_add_exec(bContext *C, wmOperator *UNUSED(op))
2317 Object *ob = ED_object_context(C);
2320 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2321 WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
2322 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2324 return OPERATOR_FINISHED;
2327 void OBJECT_OT_vertex_group_add(wmOperatorType *ot)
2330 ot->name = "Add Vertex Group";
2331 ot->idname = "OBJECT_OT_vertex_group_add";
2332 ot->description = "Add a new vertex group to the active object";
2335 ot->poll = vertex_group_poll;
2336 ot->exec = vertex_group_add_exec;
2339 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2342 static int vertex_group_remove_exec(bContext *C, wmOperator *op)
2344 Object *ob = ED_object_context(C);
2346 if (RNA_boolean_get(op->ptr, "all"))
2347 vgroup_delete_all(ob);
2351 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2352 WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
2353 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2355 return OPERATOR_FINISHED;
2358 void OBJECT_OT_vertex_group_remove(wmOperatorType *ot)
2361 ot->name = "Remove Vertex Group";
2362 ot->idname = "OBJECT_OT_vertex_group_remove";
2363 ot->description = "Delete the active vertex group";
2366 ot->poll = vertex_group_poll;
2367 ot->exec = vertex_group_remove_exec;
2370 /* redo operator will fail in this case because vertex groups aren't stored
2371 * in local edit mode stack and toggling "all" property will lead to
2372 * all groups deleted without way to restore them (see [#29527], sergey) */
2373 ot->flag = /*OPTYPE_REGISTER|*/ OPTYPE_UNDO;
2376 RNA_def_boolean(ot->srna, "all", 0, "All", "Remove from all vertex groups");
2379 static int vertex_group_assign_exec(bContext *C, wmOperator *op)
2381 ToolSettings *ts = CTX_data_tool_settings(C);
2382 Object *ob = ED_object_context(C);
2384 if (RNA_boolean_get(op->ptr, "new"))
2387 vgroup_assign_verts(ob, ts->vgroup_weight);
2388 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2389 WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
2391 return OPERATOR_FINISHED;
2394 void OBJECT_OT_vertex_group_assign(wmOperatorType *ot)
2397 ot->name = "Assign Vertex Group";
2398 ot->idname = "OBJECT_OT_vertex_group_assign";
2399 ot->description = "Assign the selected vertices to the current (or a new) vertex group";
2402 ot->poll = vertex_group_poll_edit_or_wpaint_vert_select;
2403 ot->exec = vertex_group_assign_exec;
2406 /* redo operator will fail in this case because vertex group assignment
2407 * isn't stored in local edit mode stack and toggling "new" property will
2408 * lead to creating plenty of new vertex groups (see [#29527], sergey) */
2409 ot->flag = /*OPTYPE_REGISTER|*/ OPTYPE_UNDO;
2412 RNA_def_boolean(ot->srna, "new", 0, "New", "Assign vertex to new vertex group");
2415 static int vertex_group_remove_from_exec(bContext *C, wmOperator *op)
2417 Object *ob = ED_object_context(C);
2419 if (RNA_boolean_get(op->ptr, "all"))
2420 vgroup_remove_verts(ob, 0);
2422 bDeformGroup *dg = BLI_findlink(&ob->defbase, ob->actdef - 1);
2425 return OPERATOR_CANCELLED;
2428 vgroup_active_remove_verts(ob, FALSE, dg);
2431 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2432 WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
2434 return OPERATOR_FINISHED;
2437 void OBJECT_OT_vertex_group_remove_from(wmOperatorType *ot)
2440 ot->name = "Remove from Vertex Group";
2441 ot->idname = "OBJECT_OT_vertex_group_remove_from";
2442 ot->description = "Remove the selected vertices from the active vertex group";
2445 ot->poll = vertex_group_poll_edit_or_wpaint_vert_select;
2446 ot->exec = vertex_group_remove_from_exec;
2449 /* redo operator will fail in this case because vertex groups assignment
2450 * isn't stored in local edit mode stack and toggling "all" property will lead to
2451 * removing vertices from all groups (see [#29527], sergey) */
2452 ot->flag = /*OPTYPE_REGISTER|*/ OPTYPE_UNDO;
2455 RNA_def_boolean(ot->srna, "all", 0, "All", "Remove from all vertex groups");
2458 static int vertex_group_select_exec(bContext *C, wmOperator *UNUSED(op))
2460 Object *ob = ED_object_context(C);
2462 if (!ob || ob->id.lib)
2463 return OPERATOR_CANCELLED;
2465 vgroup_select_verts(ob, 1);
2466 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, ob->data);
2468 return OPERATOR_FINISHED;
2471 void OBJECT_OT_vertex_group_select(wmOperatorType *ot)
2474 ot->name = "Select Vertex Group";
2475 ot->idname = "OBJECT_OT_vertex_group_select";
2476 ot->description = "Select all the vertices assigned to the active vertex group";
2479 ot->poll = vertex_group_poll_edit_or_wpaint_vert_select;
2480 ot->exec = vertex_group_select_exec;
2483 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2486 static int vertex_group_deselect_exec(bContext *C, wmOperator *UNUSED(op))
2488 Object *ob = ED_object_context(C);
2490 vgroup_select_verts(ob, 0);
2491 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, ob->data);
2493 return OPERATOR_FINISHED;
2496 void OBJECT_OT_vertex_group_deselect(wmOperatorType *ot)
2499 ot->name = "Deselect Vertex Group";
2500 ot->idname = "OBJECT_OT_vertex_group_deselect";
2501 ot->description = "Deselect all selected vertices assigned to the active vertex group";
2504 ot->poll = vertex_group_poll_edit_or_wpaint_vert_select;
2505 ot->exec = vertex_group_deselect_exec;
2508 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2511 static int vertex_group_copy_exec(bContext *C, wmOperator *UNUSED(op))
2513 Object *ob = ED_object_context(C);
2515 vgroup_duplicate(ob);
2516 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2517 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2518 WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
2520 return OPERATOR_FINISHED;
2523 void OBJECT_OT_vertex_group_copy(wmOperatorType *ot)
2526 ot->name = "Copy Vertex Group";
2527 ot->idname = "OBJECT_OT_vertex_group_copy";
2528 ot->description = "Make a copy of the active vertex group";
2531 ot->poll = vertex_group_poll;
2532 ot->exec = vertex_group_copy_exec;
2535 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2538 static int vertex_group_levels_exec(bContext *C, wmOperator *op)
2540 Object *ob = ED_object_context(C);
2542 float offset = RNA_float_get(op->ptr, "offset");
2543 float gain = RNA_float_get(op->ptr, "gain");
2545 vgroup_levels(ob, offset, gain);
2547 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2548 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2549 WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
2551 return OPERATOR_FINISHED;
2554 void OBJECT_OT_vertex_group_levels(wmOperatorType *ot)
2557 ot->name = "Vertex Group Levels";
2558 ot->idname = "OBJECT_OT_vertex_group_levels";
2559 ot->description = "Add some offset and multiply with some gain the weights of the active vertex group";
2562 ot->poll = vertex_group_poll;
2563 ot->exec = vertex_group_levels_exec;
2566 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2568 RNA_def_float(ot->srna, "offset", 0.f, -1.0, 1.0, "Offset", "Value to add to weights", -1.0f, 1.f);
2569 RNA_def_float(ot->srna, "gain", 1.f, 0.f, FLT_MAX, "Gain", "Value to multiply weights by", 0.0f, 10.f);
2572 static int vertex_group_normalize_exec(bContext *C, wmOperator *UNUSED(op))
2574 Object *ob = ED_object_context(C);
2576 vgroup_normalize(ob);
2578 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2579 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2580 WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
2582 return OPERATOR_FINISHED;
2585 void OBJECT_OT_vertex_group_normalize(wmOperatorType *ot)
2588 ot->name = "Normalize Vertex Group";
2589 ot->idname = "OBJECT_OT_vertex_group_normalize";
2590 ot->description = "Normalize weights of the active vertex group, so that the highest ones are now 1.0";
2593 ot->poll = vertex_group_poll;
2594 ot->exec = vertex_group_normalize_exec;
2597 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2600 static int vertex_group_normalize_all_exec(bContext *C, wmOperator *op)
2602 Object *ob = ED_object_context(C);
2603 int lock_active = RNA_boolean_get(op->ptr, "lock_active");
2605 vgroup_normalize_all(ob, lock_active);
2607 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2608 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2609 WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
2611 return OPERATOR_FINISHED;
2614 void OBJECT_OT_vertex_group_normalize_all(wmOperatorType *ot)
2617 ot->name = "Normalize All Vertex Groups";
2618 ot->idname = "OBJECT_OT_vertex_group_normalize_all";
2619 ot->description = "Normalize all weights of all vertex groups, "
2620 "so that for each vertex, the sum of all weights is 1.0";
2623 ot->poll = vertex_group_poll;
2624 ot->exec = vertex_group_normalize_all_exec;
2627 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2629 RNA_def_boolean(ot->srna, "lock_active", TRUE, "Lock Active",
2630 "Keep the values of the active group while normalizing others");
2633 static int vertex_group_fix_exec(bContext *C, wmOperator *op)
2635 Object *ob = CTX_data_active_object(C);
2636 Scene *scene = CTX_data_scene(C);
2638 float distToBe = RNA_float_get(op->ptr, "dist");
2639 float strength = RNA_float_get(op->ptr, "strength");
2640 float cp = RNA_float_get(op->ptr, "accuracy");
2641 ModifierData *md = ob->modifiers.first;
2644 if (md->type == eModifierType_Mirror && (md->mode & eModifierMode_Realtime)) {
2650 if (md && md->type == eModifierType_Mirror) {
2651 BKE_report(op->reports, RPT_ERROR_INVALID_CONTEXT, "This operator does not support an active mirror modifier");
2652 return OPERATOR_CANCELLED;
2654 vgroup_fix(scene, ob, distToBe, strength, cp);
2656 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2657 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2658 WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
2660 return OPERATOR_FINISHED;
2663 void OBJECT_OT_vertex_group_fix(wmOperatorType *ot)
2666 ot->name = "Fix Vertex Group Deform";
2667 ot->idname = "OBJECT_OT_vertex_group_fix";
2668 ot->description = "Modify the position of selected vertices by changing only their respective "
2669 "groups' weights (this tool may be slow for many vertices)";
2672 ot->poll = vertex_group_poll;
2673 ot->exec = vertex_group_fix_exec;
2676 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2677 RNA_def_float(ot->srna, "dist", 0.0f, -FLT_MAX, FLT_MAX, "Distance", "The distance to move to", -10.0f, 10.0f);
2678 RNA_def_float(ot->srna, "strength", 1.f, -2.0f, FLT_MAX, "Strength",
2679 "The distance moved can be changed by this multiplier", -2.0f, 2.0f);
2680 RNA_def_float(ot->srna, "accuracy", 1.0f, 0.05f, FLT_MAX, "Change Sensitivity",
2681 "Change the amount weights are altered with each iteration: lower values are slower", 0.05f, 1.f);
2685 static int vertex_group_lock_exec(bContext *C, wmOperator *op)
2687 Object *ob = CTX_data_active_object(C);
2689 int action = RNA_enum_get(op->ptr, "action");
2691 vgroup_lock_all(ob, action);
2693 return OPERATOR_FINISHED;
2696 void OBJECT_OT_vertex_group_lock(wmOperatorType *ot)
2699 ot->name = "Change the Lock On Vertex Groups";
2700 ot->idname = "OBJECT_OT_vertex_group_lock";
2701 ot->description = "Change the lock state of all vertex groups of active object";
2704 ot->poll = vertex_group_poll;
2705 ot->exec = vertex_group_lock_exec;
2708 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2710 WM_operator_properties_select_all(ot);
2713 static int vertex_group_invert_exec(bContext *C, wmOperator *op)
2715 Object *ob = ED_object_context(C);
2716 int auto_assign = RNA_boolean_get(op->ptr, "auto_assign");
2717 int auto_remove = RNA_boolean_get(op->ptr, "auto_remove");
2719 vgroup_invert(ob, auto_assign, auto_remove);
2720 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2721 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2722 WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
2724 return OPERATOR_FINISHED;
2727 void OBJECT_OT_vertex_group_invert(wmOperatorType *ot)
2730 ot->name = "Invert Vertex Group";
2731 ot->idname = "OBJECT_OT_vertex_group_invert";
2732 ot->description = "Invert active vertex group's weights";
2735 ot->poll = vertex_group_poll;
2736 ot->exec = vertex_group_invert_exec;
2739 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2741 RNA_def_boolean(ot->srna, "auto_assign", TRUE, "Add Weights",
2742 "Add verts from groups that have zero weight before inverting");
2743 RNA_def_boolean(ot->srna, "auto_remove", TRUE, "Remove Weights",
2744 "Remove verts from groups that have zero weight after inverting");
2748 static int vertex_group_blend_exec(bContext *C, wmOperator *op)
2750 Object *ob = ED_object_context(C);
2751 float fac = RNA_float_get(op->ptr, "factor");
2753 vgroup_blend(ob, fac);
2755 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2756 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2757 WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
2759 return OPERATOR_FINISHED;
2762 /* check we have a vertex selection, either in weight paint or editmode */
2763 static int vertex_group_blend_poll(bContext *C)
2765 Object *ob = ED_object_context(C);
2766 ID *data = (ob) ? ob->data : NULL;
2768 if (!(ob && !ob->id.lib && data && !data->lib))
2771 if (vgroup_object_in_edit_mode(ob)) {
2774 else if ((ob->type == OB_MESH) && (ob->mode & OB_MODE_WEIGHT_PAINT)) {
2775 if (ME_EDIT_PAINT_SEL_MODE(((Mesh *)data)) == SCE_SELECT_VERTEX) {
2779 CTX_wm_operator_poll_msg_set(C, "Vertex select needs to be enabled in weight paint mode");
2789 void OBJECT_OT_vertex_group_blend(wmOperatorType *ot)
2794 ot->name = "Blend Vertex Group";
2795 ot->idname = "OBJECT_OT_vertex_group_blend";
2796 ot->description = "Blend selected vertex weights with unselected for the active group";
2799 ot->poll = vertex_group_blend_poll;
2800 ot->exec = vertex_group_blend_exec;
2803 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2805 prop = RNA_def_property(ot->srna, "factor", PROP_FLOAT, PROP_FACTOR);
2806 RNA_def_property_ui_text(prop, "Factor", "");
2807 RNA_def_property_range(prop, 0.0f, 1.0f);
2808 RNA_def_property_float_default(prop, 1.0f);
2812 static int vertex_group_clean_exec(bContext *C, wmOperator *op)
2814 Object *ob = ED_object_context(C);
2816 float limit = RNA_float_get(op->ptr, "limit");
2817 int all_groups = RNA_boolean_get(op->ptr, "all_groups");
2818 int keep_single = RNA_boolean_get(op->ptr, "keep_single");
2820 if (all_groups) vgroup_clean_all(ob, limit, keep_single);
2821 else vgroup_clean(ob, limit, keep_single);
2823 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2824 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2825 WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
2827 return OPERATOR_FINISHED;
2830 void OBJECT_OT_vertex_group_clean(wmOperatorType *ot)
2833 ot->name = "Clean Vertex Group";
2834 ot->idname = "OBJECT_OT_vertex_group_clean";
2835 ot->description = "Remove Vertex Group assignments which aren't required";
2838 ot->poll = vertex_group_poll;
2839 ot->exec = vertex_group_clean_exec;
2842 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2844 RNA_def_float(ot->srna, "limit", 0.01f, 0.0f, 1.0, "Limit", "Remove weights under this limit", 0.001f, 0.99f);
2845 RNA_def_boolean(ot->srna, "all_groups", FALSE, "All Groups", "Clean all vertex groups");
2846 RNA_def_boolean(ot->srna, "keep_single", FALSE, "Keep Single",
2847 "Keep verts assigned to at least one group when cleaning");
2851 static int vertex_group_mirror_exec(bContext *C, wmOperator *op)
2853 Object *ob = ED_object_context(C);
2855 ED_vgroup_mirror(ob,
2856 RNA_boolean_get(op->ptr, "mirror_weights"),
2857 RNA_boolean_get(op->ptr, "flip_group_names"),
2858 RNA_boolean_get(op->ptr, "all_groups"));
2860 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2861 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2862 WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
2864 return OPERATOR_FINISHED;
2867 void OBJECT_OT_vertex_group_mirror(wmOperatorType *ot)
2870 ot->name = "Mirror Vertex Group";
2871 ot->idname = "OBJECT_OT_vertex_group_mirror";
2872 ot->description = "Mirror all vertex groups, flip weights and/or names, editing only selected vertices, "
2873 "flipping when both sides are selected otherwise copy from unselected";
2876 ot->poll = vertex_group_poll;
2877 ot->exec = vertex_group_mirror_exec;
2880 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2883 RNA_def_boolean(ot->srna, "mirror_weights", TRUE, "Mirror Weights", "Mirror weights");
2884 RNA_def_boolean(ot->srna, "flip_group_names", TRUE, "Flip Groups", "Flip vertex group names");
2885 RNA_def_boolean(ot->srna, "all_groups", FALSE, "All Groups", "Mirror all vertex groups weights");
2889 static int vertex_group_copy_to_linked_exec(bContext *C, wmOperator *UNUSED(op))
2891 Scene *scene = CTX_data_scene(C);
2892 Object *ob = ED_object_context(C);
2894 int retval = OPERATOR_CANCELLED;
2896 for (base = scene->base.first; base; base = base->next) {
2897 if (base->object->type == ob->type) {
2898 if (base->object != ob && base->object->data == ob->data) {
2899 BLI_freelistN(&base->object->defbase);
2900 BLI_duplicatelist(&base->object->defbase, &ob->defbase);
2901 base->object->actdef = ob->actdef;
2903 DAG_id_tag_update(&base->object->id, OB_RECALC_DATA);
2904 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, base->object);
2905 WM_event_add_notifier(C, NC_GEOM | ND_DATA, base->object->data);
2907 retval = OPERATOR_FINISHED;
2915 void OBJECT_OT_vertex_group_copy_to_linked(wmOperatorType *ot)
2918 ot->name = "Copy Vertex Groups to Linked";
2919 ot->idname = "OBJECT_OT_vertex_group_copy_to_linked";
2920 ot->description = "Copy Vertex Groups to all users of the same Geometry data";
2923 ot->poll = vertex_group_poll;
2924 ot->exec = vertex_group_copy_to_linked_exec;
2927 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2930 static int vertex_group_copy_to_selected_exec(bContext *C, wmOperator *op)
2932 Object *obact = ED_object_context(C);
2936 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
2939 if (ED_vgroup_copy_array(ob, obact)) change++;
2945 if ((change == 0 && fail == 0) || fail) {
2946 BKE_reportf(op->reports, RPT_ERROR,
2947 "Copy to VGroups to Selected warning done %d, failed %d, object data must have matching indices",
2951 return OPERATOR_FINISHED;
2955 void OBJECT_OT_vertex_group_copy_to_selected(wmOperatorType *ot)
2958 ot->name = "Copy Vertex Group to Selected";
2959 ot->idname = "OBJECT_OT_vertex_group_copy_to_selected";
2960 ot->description = "Copy Vertex Groups to other selected objects with matching indices";
2963 ot->poll = vertex_group_poll;
2964 ot->exec = vertex_group_copy_to_selected_exec;
2967 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2970 static int vertex_group_copy_to_selected_single_exec(bContext *C, wmOperator *op)
2972 Object *obact= CTX_data_active_object(C);
2976 /*Macro to loop through selected objects and perform operation*/
2977 CTX_DATA_BEGIN(C, Object*, obslc, selected_editable_objects)
2979 if(obact != obslc) {
2980 /*Try function for matching number of vertices*/
2981 if(ED_vgroup_copy_single(obslc, obact)) change++;
2982 /*Try function for get weight from closest vertex*/
2983 /*TODO: try this function*/
2984 /*Try function for get weight from closest face*/
2985 else if(ED_vgroup_copy_by_nearest_face_single(obslc, obact)) change++;
2986 /*Trigger error message*/
2988 /*Event notifiers for correct display of data*/
2989 DAG_id_tag_update(&obslc->id, OB_RECALC_DATA);
2990 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obslc);
2991 WM_event_add_notifier(C, NC_GEOM|ND_DATA, obslc->data);
2996 /*Report error when task can not be completed with available functions.*/
2997 if((change == 0 && fail == 0) || fail) {
2998 BKE_reportf(op->reports, RPT_ERROR,
2999 "Copy to VGroups to Selected warning done %d, failed %d, All functions failed!",