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): Ove M Henriksen.
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/editors/object/object_vgroup.c
37 #include "MEM_guardedalloc.h"
39 #include "DNA_curve_types.h"
40 #include "DNA_lattice_types.h"
41 #include "DNA_meshdata_types.h"
42 #include "DNA_mesh_types.h"
43 #include "DNA_modifier_types.h"
44 #include "DNA_object_types.h"
45 #include "DNA_scene_types.h"
47 #include "BLI_alloca.h"
48 #include "BLI_array.h"
50 #include "BLI_blenlib.h"
51 #include "BLI_utildefines.h"
52 #include "BLI_linklist_stack.h"
53 #include "BLI_stackdefines.h"
56 #include "BKE_context.h"
57 #include "BKE_customdata.h"
58 #include "BKE_deform.h"
59 #include "BKE_depsgraph.h"
60 #include "BKE_mesh_mapping.h"
61 #include "BKE_editmesh.h"
62 #include "BKE_layer.h"
63 #include "BKE_modifier.h"
64 #include "BKE_report.h"
65 #include "BKE_DerivedMesh.h"
66 #include "BKE_object_deform.h"
67 #include "BKE_object.h"
68 #include "BKE_lattice.h"
70 #include "DNA_armature_types.h"
71 #include "RNA_access.h"
72 #include "RNA_define.h"
73 #include "RNA_enum_types.h"
78 #include "ED_object.h"
81 #include "UI_resources.h"
83 #include "object_intern.h"
85 /************************ Exported Functions **********************/
86 static bool vertex_group_use_vert_sel(Object *ob)
88 if (ob->mode == OB_MODE_EDIT) {
91 else if (ob->type == OB_MESH && ((Mesh *)ob->data)->editflag & ME_EDIT_PAINT_VERT_SEL) {
99 static Lattice *vgroup_edit_lattice(Object *ob)
101 Lattice *lt = ob->data;
102 BLI_assert(ob->type == OB_LATTICE);
103 return (lt->editlatt) ? lt->editlatt->latt : lt;
106 bool ED_vgroup_sync_from_pose(Object *ob)
108 Object *armobj = BKE_object_pose_armature_get(ob);
109 if (armobj && (armobj->mode & OB_MODE_POSE)) {
110 struct bArmature *arm = armobj->data;
112 int def_num = defgroup_name_index(ob, arm->act_bone->name);
114 ob->actdef = def_num + 1;
123 * Removes out of range MDeformWeights
125 void ED_vgroup_data_clamp_range(ID *id, const int total)
127 MDeformVert **dvert_arr;
130 if (ED_vgroup_parray_alloc(id, &dvert_arr, &dvert_tot, false)) {
132 for (i = 0; i < dvert_tot; i++) {
133 MDeformVert *dv = dvert_arr[i];
135 for (j = 0; j < dv->totweight; j++) {
136 if (dv->dw[j].def_nr >= total) {
137 defvert_remove_group(dv, &dv->dw[j]);
145 bool ED_vgroup_parray_alloc(ID *id, MDeformVert ***dvert_arr, int *dvert_tot, const bool use_vert_sel)
151 switch (GS(id->name)) {
154 Mesh *me = (Mesh *)id;
156 if (me->edit_btmesh) {
157 BMEditMesh *em = me->edit_btmesh;
159 const int cd_dvert_offset = CustomData_get_offset(&bm->vdata, CD_MDEFORMVERT);
164 if (cd_dvert_offset == -1) {
170 *dvert_arr = MEM_mallocN(sizeof(void *) * i, "vgroup parray from me");
175 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
176 (*dvert_arr)[i] = BM_elem_flag_test(eve, BM_ELEM_SELECT) ?
177 BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset) : NULL;
182 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
183 (*dvert_arr)[i] = BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset);
190 else if (me->dvert) {
191 MVert *mvert = me->mvert;
192 MDeformVert *dvert = me->dvert;
195 *dvert_tot = me->totvert;
196 *dvert_arr = MEM_mallocN(sizeof(void *) * me->totvert, "vgroup parray from me");
199 for (i = 0; i < me->totvert; i++) {
200 (*dvert_arr)[i] = (mvert[i].flag & SELECT) ?
205 for (i = 0; i < me->totvert; i++) {
206 (*dvert_arr)[i] = me->dvert + i;
218 Lattice *lt = (Lattice *)id;
219 lt = (lt->editlatt) ? lt->editlatt->latt : lt;
222 BPoint *def = lt->def;
223 *dvert_tot = lt->pntsu * lt->pntsv * lt->pntsw;
224 *dvert_arr = MEM_mallocN(sizeof(void *) * (*dvert_tot), "vgroup parray from me");
227 for (i = 0; i < *dvert_tot; i++) {
228 (*dvert_arr)[i] = (def->f1 & SELECT) ?
229 <->dvert[i] : NULL;
233 for (i = 0; i < *dvert_tot; i++) {
234 (*dvert_arr)[i] = lt->dvert + i;
249 * For use with tools that use ED_vgroup_parray_alloc with \a use_vert_sel == true.
250 * This finds the unselected mirror deform verts and copies the weights to them from the selected.
252 * \note \a dvert_array has mirrored weights filled in, incase cleanup operations are needed on both.
254 void ED_vgroup_parray_mirror_sync(Object *ob,
255 MDeformVert **dvert_array, const int dvert_tot,
256 const bool *vgroup_validmap, const int vgroup_tot)
258 BMEditMesh *em = BKE_editmesh_from_object(ob);
259 MDeformVert **dvert_array_all = NULL;
262 /* get an array of all verts, not only selected */
263 if (ED_vgroup_parray_alloc(ob->data, &dvert_array_all, &dvert_tot_all, false) == false) {
268 BM_mesh_elem_table_ensure(em->bm, BM_VERT);
272 const int *flip_map = defgroup_flip_map(ob, &flip_map_len, true);
274 for (int i_src = 0; i_src < dvert_tot; i_src++) {
275 if (dvert_array[i_src] != NULL) {
276 /* its selected, check if its mirror exists */
277 int i_dst = ED_mesh_mirror_get_vert(ob, i_src);
278 if (i_dst != -1 && dvert_array_all[i_dst] != NULL) {
279 /* we found a match! */
280 const MDeformVert *dv_src = dvert_array[i_src];
281 MDeformVert *dv_dst = dvert_array_all[i_dst];
283 defvert_mirror_subset(dv_dst, dv_src, vgroup_validmap, vgroup_tot, flip_map, flip_map_len);
285 dvert_array[i_dst] = dvert_array_all[i_dst];
290 MEM_freeN((void *)flip_map);
291 MEM_freeN(dvert_array_all);
295 * Fill in the pointers for mirror verts (as if all mirror verts were selected too).
297 * similar to #ED_vgroup_parray_mirror_sync but only fill in mirror points.
299 void ED_vgroup_parray_mirror_assign(Object *ob,
300 MDeformVert **dvert_array, const int dvert_tot)
302 BMEditMesh *em = BKE_editmesh_from_object(ob);
303 MDeformVert **dvert_array_all = NULL;
307 /* get an array of all verts, not only selected */
308 if (ED_vgroup_parray_alloc(ob->data, &dvert_array_all, &dvert_tot_all, false) == false) {
312 BLI_assert(dvert_tot == dvert_tot_all);
314 BM_mesh_elem_table_ensure(em->bm, BM_VERT);
317 for (i = 0; i < dvert_tot; i++) {
318 if (dvert_array[i] == NULL) {
319 /* its unselected, check if its mirror is */
320 int i_sel = ED_mesh_mirror_get_vert(ob, i);
321 if ((i_sel != -1) && (i_sel != i) && (dvert_array[i_sel])) {
322 /* we found a match! */
323 dvert_array[i] = dvert_array_all[i];
328 MEM_freeN(dvert_array_all);
331 void ED_vgroup_parray_remove_zero(MDeformVert **dvert_array, const int dvert_tot,
332 const bool *vgroup_validmap, const int vgroup_tot,
333 const float epsilon, const bool keep_single)
338 for (i = 0; i < dvert_tot; i++) {
341 /* in case its not selected */
342 if (!(dv = dvert_array[i])) {
351 if (keep_single && dv->totweight == 1)
355 if ((dw->def_nr < vgroup_tot) && vgroup_validmap[dw->def_nr]) {
356 if (dw->weight <= epsilon) {
357 defvert_remove_group(dv, dw);
364 /* matching index only */
365 bool ED_vgroup_array_copy(Object *ob, Object *ob_from)
367 MDeformVert **dvert_array_from = NULL, **dvf;
368 MDeformVert **dvert_array = NULL, **dv;
372 int defbase_tot_from = BLI_listbase_count(&ob_from->defbase);
373 int defbase_tot = BLI_listbase_count(&ob->defbase);
374 bool new_vgroup = false;
379 /* in case we copy vgroup between two objects using same data, we only have to care about object side of things. */
380 if (ob->data != ob_from->data) {
381 ED_vgroup_parray_alloc(ob_from->data, &dvert_array_from, &dvert_tot_from, false);
382 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, false);
384 if ((dvert_array == NULL) && (dvert_array_from != NULL) && BKE_object_defgroup_data_create(ob->data)) {
385 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, false);
389 if (dvert_tot == 0 || (dvert_tot != dvert_tot_from) || dvert_array_from == NULL || dvert_array == NULL) {
391 MEM_freeN(dvert_array);
392 if (dvert_array_from)
393 MEM_freeN(dvert_array_from);
395 if (new_vgroup == true) {
396 /* free the newly added vgroup since it wasn't compatible */
397 BKE_object_defgroup_remove_all(ob);
400 /* if true: both are 0 and nothing needs changing, consider this a success */
401 return (dvert_tot == dvert_tot_from);
406 BLI_freelistN(&ob->defbase);
407 BLI_duplicatelist(&ob->defbase, &ob_from->defbase);
408 ob->actdef = ob_from->actdef;
410 if (defbase_tot_from < defbase_tot) {
411 /* correct vgroup indices because the number of vgroups is being reduced. */
412 int *remap = MEM_mallocN(sizeof(int) * (defbase_tot + 1), __func__);
413 for (i = 0; i <= defbase_tot_from; i++) remap[i] = i;
414 for (; i <= defbase_tot; i++) remap[i] = 0; /* can't use these, so disable */
416 BKE_object_defgroup_remap_update_users(ob, remap);
420 if (dvert_array_from != NULL && dvert_array != NULL) {
421 dvf = dvert_array_from;
424 for (i = 0; i < dvert_tot; i++, dvf++, dv++) {
425 MEM_SAFE_FREE((*dv)->dw);
429 (*dv)->dw = MEM_dupallocN((*dv)->dw);
433 MEM_freeN(dvert_array);
434 MEM_freeN(dvert_array_from);
440 void ED_vgroup_parray_to_weight_array(
441 const MDeformVert **dvert_array, const int dvert_tot,
442 float *dvert_weights, const int def_nr)
446 for (i = 0; i < dvert_tot; i++) {
447 const MDeformVert *dv = dvert_array[i];
448 dvert_weights[i] = dv ? defvert_find_weight(dv, def_nr) : 0.0f;
452 void ED_vgroup_parray_from_weight_array(
453 MDeformVert **dvert_array, const int dvert_tot,
454 const float *dvert_weights, const int def_nr, const bool remove_zero)
458 for (i = 0; i < dvert_tot; i++) {
459 MDeformVert *dv = dvert_array[i];
461 if (dvert_weights[i] > 0.0f) {
462 MDeformWeight *dw = defvert_verify_index(dv, def_nr);
463 BLI_assert(IN_RANGE_INCL(dvert_weights[i], 0.0f, 1.0f));
464 dw->weight = dvert_weights[i];
467 MDeformWeight *dw = defvert_find_index(dv, def_nr);
470 defvert_remove_group(dv, dw);
482 /* TODO, cache flip data to speedup calls within a loop. */
483 static void mesh_defvert_mirror_update_internal(Object *ob,
484 MDeformVert *dvert_dst, MDeformVert *dvert_src,
488 /* all vgroups, add groups where neded */
490 int *flip_map = defgroup_flip_map(ob, &flip_map_len, true);
491 defvert_sync_mapped(dvert_dst, dvert_src, flip_map, flip_map_len, true);
496 MDeformWeight *dw = defvert_verify_index(dvert_dst, defgroup_flip_index(ob, def_nr, 1));
498 dw->weight = defvert_find_weight(dvert_src, def_nr);
503 static void ED_mesh_defvert_mirror_update_em(Object *ob, BMVert *eve, int def_nr, int vidx,
504 const int cd_dvert_offset)
507 BMEditMesh *em = me->edit_btmesh;
509 bool use_topology = (me->editflag & ME_EDIT_MIRROR_TOPO) != 0;
511 eve_mirr = editbmesh_get_x_mirror_vert(ob, em, eve, eve->co, vidx, use_topology);
513 if (eve_mirr && eve_mirr != eve) {
514 MDeformVert *dvert_src = BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset);
515 MDeformVert *dvert_dst = BM_ELEM_CD_GET_VOID_P(eve_mirr, cd_dvert_offset);
516 mesh_defvert_mirror_update_internal(ob, dvert_dst, dvert_src, def_nr);
520 static void ED_mesh_defvert_mirror_update_ob(Object *ob, int def_nr, int vidx)
524 bool use_topology = (me->editflag & ME_EDIT_MIRROR_TOPO) != 0;
529 vidx_mirr = mesh_get_x_mirror_vert(ob, NULL, vidx, use_topology);
531 if ((vidx_mirr) >= 0 && (vidx_mirr != vidx)) {
532 MDeformVert *dvert_src = &me->dvert[vidx];
533 MDeformVert *dvert_dst = &me->dvert[vidx_mirr];
534 mesh_defvert_mirror_update_internal(ob, dvert_dst, dvert_src, def_nr);
539 * Use when adjusting the active vertex weight and apply to mirror vertices.
541 void ED_vgroup_vert_active_mirror(Object *ob, int def_nr)
544 BMEditMesh *em = me->edit_btmesh;
545 MDeformVert *dvert_act;
547 if (me->editflag & ME_EDIT_MIRROR_X) {
550 dvert_act = ED_mesh_active_dvert_get_em(ob, &eve_act);
552 const int cd_dvert_offset = CustomData_get_offset(&em->bm->vdata, CD_MDEFORMVERT);
553 ED_mesh_defvert_mirror_update_em(ob, eve_act, def_nr, -1, cd_dvert_offset);
558 dvert_act = ED_mesh_active_dvert_get_ob(ob, &v_act);
560 ED_mesh_defvert_mirror_update_ob(ob, def_nr, v_act);
566 static void vgroup_remove_weight(Object *ob, const int def_nr)
568 MDeformVert *dvert_act;
571 dvert_act = ED_mesh_active_dvert_get_only(ob);
573 dw = defvert_find_index(dvert_act, def_nr);
574 defvert_remove_group(dvert_act, dw);
578 static bool vgroup_normalize_active_vertex(Object *ob, eVGroupSelect subset_type)
581 BMEditMesh *em = me->edit_btmesh;
584 MDeformVert *dvert_act;
585 int subset_count, vgroup_tot;
586 const bool *vgroup_validmap;
590 dvert_act = ED_mesh_active_dvert_get_em(ob, &eve_act);
593 dvert_act = ED_mesh_active_dvert_get_ob(ob, &v_act);
596 if (dvert_act == NULL) {
600 vgroup_validmap = BKE_object_defgroup_subset_from_select_type(ob, subset_type, &vgroup_tot, &subset_count);
601 defvert_normalize_subset(dvert_act, vgroup_validmap, vgroup_tot);
602 MEM_freeN((void *)vgroup_validmap);
604 if (me->editflag & ME_EDIT_MIRROR_X) {
606 const int cd_dvert_offset = CustomData_get_offset(&em->bm->vdata, CD_MDEFORMVERT);
607 ED_mesh_defvert_mirror_update_em(ob, eve_act, -1, -1, cd_dvert_offset);
610 ED_mesh_defvert_mirror_update_ob(ob, -1, v_act);
617 static void vgroup_copy_active_to_sel(Object *ob, eVGroupSelect subset_type)
620 BMEditMesh *em = me->edit_btmesh;
621 MDeformVert *dvert_act;
622 int i, vgroup_tot, subset_count;
623 const bool *vgroup_validmap = BKE_object_defgroup_subset_from_select_type(ob, subset_type, &vgroup_tot, &subset_count);
628 BMVert *eve, *eve_act;
629 const int cd_dvert_offset = CustomData_get_offset(&em->bm->vdata, CD_MDEFORMVERT);
631 dvert_act = ED_mesh_active_dvert_get_em(ob, &eve_act);
633 BM_ITER_MESH_INDEX (eve, &iter, em->bm, BM_VERTS_OF_MESH, i) {
634 if (BM_elem_flag_test(eve, BM_ELEM_SELECT) && eve != eve_act) {
635 MDeformVert *dv = BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset);
636 defvert_copy_subset(dv, dvert_act, vgroup_validmap, vgroup_tot);
637 if (me->editflag & ME_EDIT_MIRROR_X) {
638 ED_mesh_defvert_mirror_update_em(ob, eve, -1, i, cd_dvert_offset);
648 dvert_act = ED_mesh_active_dvert_get_ob(ob, &v_act);
651 for (i = 0; i < me->totvert; i++, dv++) {
652 if ((me->mvert[i].flag & SELECT) && dv != dvert_act) {
653 defvert_copy_subset(dv, dvert_act, vgroup_validmap, vgroup_tot);
654 if (me->editflag & ME_EDIT_MIRROR_X) {
655 ED_mesh_defvert_mirror_update_ob(ob, -1, i);
662 MEM_freeN((void *)vgroup_validmap);
665 /***********************Start weight transfer (WT)*********************************/
667 static EnumPropertyItem WT_vertex_group_select_item[] = {
669 "ACTIVE", 0, "Active Group", "The active Vertex Group"},
670 {WT_VGROUP_BONE_SELECT,
671 "BONE_SELECT", 0, "Selected Pose Bones", "All Vertex Groups assigned to Selection"},
672 {WT_VGROUP_BONE_DEFORM,
673 "BONE_DEFORM", 0, "Deform Pose Bones", "All Vertex Groups assigned to Deform Bones"},
675 "ALL", 0, "All Groups", "All Vertex Groups"},
676 {0, NULL, 0, NULL, NULL}
679 EnumPropertyItem *ED_object_vgroup_selection_itemf_helper(
680 const bContext *C, PointerRNA *UNUSED(ptr),
681 PropertyRNA *UNUSED(prop), bool *r_free, const unsigned int selection_mask)
684 EnumPropertyItem *item = NULL;
688 if (!C) /* needed for docs and i18n tools */
689 return WT_vertex_group_select_item;
691 ob = CTX_data_active_object(C);
692 if (selection_mask & (1 << WT_VGROUP_ACTIVE))
693 RNA_enum_items_add_value(&item, &totitem, WT_vertex_group_select_item, WT_VGROUP_ACTIVE);
695 if (BKE_object_pose_armature_get(ob)) {
696 if (selection_mask & (1 << WT_VGROUP_BONE_SELECT))
697 RNA_enum_items_add_value(&item, &totitem, WT_vertex_group_select_item, WT_VGROUP_BONE_SELECT);
698 if (selection_mask & (1 << WT_VGROUP_BONE_DEFORM))
699 RNA_enum_items_add_value(&item, &totitem, WT_vertex_group_select_item, WT_VGROUP_BONE_DEFORM);
702 if (selection_mask & (1 << WT_VGROUP_ALL))
703 RNA_enum_items_add_value(&item, &totitem, WT_vertex_group_select_item, WT_VGROUP_ALL);
705 RNA_enum_item_end(&item, &totitem);
711 static EnumPropertyItem *rna_vertex_group_with_single_itemf(bContext *C, PointerRNA *ptr,
712 PropertyRNA *prop, bool *r_free)
714 return ED_object_vgroup_selection_itemf_helper(C, ptr, prop, r_free, WT_VGROUP_MASK_ALL);
717 static EnumPropertyItem *rna_vertex_group_select_itemf(bContext *C, PointerRNA *ptr,
718 PropertyRNA *prop, bool *r_free)
720 return ED_object_vgroup_selection_itemf_helper(C, ptr, prop, r_free, WT_VGROUP_MASK_ALL & ~(1 << WT_VGROUP_ACTIVE));
723 static void vgroup_operator_subset_select_props(wmOperatorType *ot, bool use_active)
727 prop = RNA_def_enum(ot->srna,
728 "group_select_mode", DummyRNA_NULL_items,
729 use_active ? WT_VGROUP_ACTIVE : WT_VGROUP_ALL, "Subset",
730 "Define which subset of Groups shall be used");
733 RNA_def_enum_funcs(prop, rna_vertex_group_with_single_itemf);
736 RNA_def_enum_funcs(prop, rna_vertex_group_select_itemf);
738 RNA_def_property_flag(prop, PROP_ENUM_NO_TRANSLATE);
743 /***********************End weight transfer (WT)***********************************/
745 /* for Mesh in Object mode */
746 /* allows editmode for Lattice */
747 static void ED_vgroup_nr_vert_add(Object *ob,
748 const int def_nr, const int vertnum,
749 const float weight, const int assignmode)
751 /* add the vert to the deform group with the
754 MDeformVert *dvert = NULL;
758 BKE_object_defgroup_array_get(ob->data, &dvert, &tot);
763 /* check that vertnum is valid before trying to get the relevant dvert */
764 if ((vertnum < 0) || (vertnum >= tot))
769 MDeformVert *dv = &dvert[vertnum];
772 /* Lets first check to see if this vert is
773 * already in the weight group -- if so
777 dw = defvert_find_index(dv, def_nr);
780 switch (assignmode) {
785 dw->weight += weight;
786 if (dw->weight >= 1.0f)
789 case WEIGHT_SUBTRACT:
790 dw->weight -= weight;
791 /* if the weight is zero or less than
792 * remove the vert from the deform group
794 if (dw->weight <= 0.0f) {
795 defvert_remove_group(dv, dw);
801 /* if the vert wasn't in the deform group then
802 * we must take a different form of action ...
805 switch (assignmode) {
806 case WEIGHT_SUBTRACT:
807 /* if we are subtracting then we don't
808 * need to do anything
814 /* if we are doing an additive assignment, then
815 * we need to create the deform weight
818 /* we checked if the vertex was added before so no need to test again, simply add */
819 defvert_add_index_notest(dv, def_nr, weight);
826 /* called while not in editmode */
827 void ED_vgroup_vert_add(Object *ob, bDeformGroup *dg, int vertnum, float weight, int assignmode)
829 /* add the vert to the deform group with the
830 * specified assign mode
832 const int def_nr = BLI_findindex(&ob->defbase, dg);
834 MDeformVert *dv = NULL;
837 /* get the deform group number, exit if
842 /* if there's no deform verts then create some,
844 if (BKE_object_defgroup_array_get(ob->data, &dv, &tot) && dv == NULL)
845 BKE_object_defgroup_data_create(ob->data);
847 /* call another function to do the work
849 ED_vgroup_nr_vert_add(ob, def_nr, vertnum, weight, assignmode);
853 /* mesh object mode, lattice can be in editmode */
854 void ED_vgroup_vert_remove(Object *ob, bDeformGroup *dg, int vertnum)
856 /* This routine removes the vertex from the specified
860 /* TODO, this is slow in a loop, better pass def_nr directly, but leave for later... - campbell */
861 const int def_nr = BLI_findindex(&ob->defbase, dg);
864 MDeformVert *dvert = NULL;
867 /* get the deform vertices corresponding to the
870 BKE_object_defgroup_array_get(ob->data, &dvert, &tot);
873 MDeformVert *dv = &dvert[vertnum];
876 dw = defvert_find_index(dv, def_nr);
877 defvert_remove_group(dv, dw); /* dw can be NULL */
882 static float get_vert_def_nr(Object *ob, const int def_nr, const int vertnum)
884 MDeformVert *dv = NULL;
886 /* get the deform vertices corresponding to the vertnum */
887 if (ob->type == OB_MESH) {
890 if (me->edit_btmesh) {
891 BMEditMesh *em = me->edit_btmesh;
892 const int cd_dvert_offset = CustomData_get_offset(&em->bm->vdata, CD_MDEFORMVERT);
893 /* warning, this lookup is _not_ fast */
895 if (cd_dvert_offset != -1 && vertnum < em->bm->totvert) {
897 BM_mesh_elem_table_ensure(em->bm, BM_VERT);
898 eve = BM_vert_at_index(em->bm, vertnum);
899 dv = BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset);
907 if (vertnum >= me->totvert) {
910 dv = &me->dvert[vertnum];
914 else if (ob->type == OB_LATTICE) {
915 Lattice *lt = vgroup_edit_lattice(ob);
918 if (vertnum >= lt->pntsu * lt->pntsv * lt->pntsw) {
921 dv = <->dvert[vertnum];
926 MDeformWeight *dw = defvert_find_index(dv, def_nr);
935 float ED_vgroup_vert_weight(Object *ob, bDeformGroup *dg, int vertnum)
937 const int def_nr = BLI_findindex(&ob->defbase, dg);
943 return get_vert_def_nr(ob, def_nr, vertnum);
946 void ED_vgroup_select_by_name(Object *ob, const char *name)
947 { /* note: ob->actdef==0 signals on painting to create a new one, if a bone in posemode is selected */
948 ob->actdef = defgroup_name_index(ob, name) + 1;
951 /********************** Operator Implementations *********************/
953 /* only in editmode */
954 static void vgroup_select_verts(Object *ob, int select)
956 const int def_nr = ob->actdef - 1;
958 if (!BLI_findlink(&ob->defbase, def_nr)) {
962 if (ob->type == OB_MESH) {
965 if (me->edit_btmesh) {
966 BMEditMesh *em = me->edit_btmesh;
967 const int cd_dvert_offset = CustomData_get_offset(&em->bm->vdata, CD_MDEFORMVERT);
969 if (cd_dvert_offset != -1) {
973 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
974 if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN)) {
975 MDeformVert *dv = BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset);
976 if (defvert_find_index(dv, def_nr)) {
977 BM_vert_select_set(em->bm, eve, select);
982 /* this has to be called, because this function operates on vertices only */
983 if (select) EDBM_select_flush(em); /* vertices to edges/faces */
984 else EDBM_deselect_flush(em);
996 for (i = 0; i < me->totvert; i++, mv++, dv++) {
997 if (!(mv->flag & ME_HIDE)) {
998 if (defvert_find_index(dv, def_nr)) {
999 if (select) mv->flag |= SELECT;
1000 else mv->flag &= ~SELECT;
1005 paintvert_flush_flags(ob);
1009 else if (ob->type == OB_LATTICE) {
1010 Lattice *lt = vgroup_edit_lattice(ob);
1014 BPoint *bp, *actbp = BKE_lattice_active_point_get(lt);
1019 tot = lt->pntsu * lt->pntsv * lt->pntsw;
1020 for (a = 0, bp = lt->def; a < tot; a++, bp++, dv++) {
1021 if (defvert_find_index(dv, def_nr)) {
1022 if (select) bp->f1 |= SELECT;
1025 if (actbp && bp == actbp) lt->actbp = LT_ACTBP_NONE;
1033 static void vgroup_duplicate(Object *ob)
1035 bDeformGroup *dg, *cdg;
1036 char name[sizeof(dg->name)];
1037 MDeformWeight *dw_org, *dw_cpy;
1038 MDeformVert **dvert_array = NULL;
1039 int i, idg, icdg, dvert_tot = 0;
1041 dg = BLI_findlink(&ob->defbase, (ob->actdef - 1));
1045 if (!strstr(dg->name, "_copy")) {
1046 BLI_snprintf(name, sizeof(name), "%s_copy", dg->name);
1049 BLI_strncpy(name, dg->name, sizeof(name));
1052 cdg = defgroup_duplicate(dg);
1053 BLI_strncpy(cdg->name, name, sizeof(cdg->name));
1054 defgroup_unique_name(cdg, ob);
1056 BLI_addtail(&ob->defbase, cdg);
1058 idg = (ob->actdef - 1);
1059 ob->actdef = BLI_listbase_count(&ob->defbase);
1060 icdg = (ob->actdef - 1);
1062 /* TODO, we might want to allow only copy selected verts here? - campbell */
1063 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, false);
1066 for (i = 0; i < dvert_tot; i++) {
1067 MDeformVert *dv = dvert_array[i];
1068 dw_org = defvert_find_index(dv, idg);
1070 /* defvert_verify_index re-allocs org so need to store the weight first */
1071 const float weight = dw_org->weight;
1072 dw_cpy = defvert_verify_index(dv, icdg);
1073 dw_cpy->weight = weight;
1077 MEM_freeN(dvert_array);
1081 static bool vgroup_normalize(Object *ob)
1084 MDeformVert *dv, **dvert_array = NULL;
1085 int i, dvert_tot = 0;
1086 const int def_nr = ob->actdef - 1;
1088 const int use_vert_sel = vertex_group_use_vert_sel(ob);
1090 if (!BLI_findlink(&ob->defbase, def_nr)) {
1094 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
1097 float weight_max = 0.0f;
1099 for (i = 0; i < dvert_tot; i++) {
1101 /* in case its not selected */
1102 if (!(dv = dvert_array[i])) {
1106 dw = defvert_find_index(dv, def_nr);
1108 weight_max = max_ff(dw->weight, weight_max);
1112 if (weight_max > 0.0f) {
1113 for (i = 0; i < dvert_tot; i++) {
1115 /* in case its not selected */
1116 if (!(dv = dvert_array[i])) {
1120 dw = defvert_find_index(dv, def_nr);
1122 dw->weight /= weight_max;
1124 /* in case of division errors with very low weights */
1125 CLAMP(dw->weight, 0.0f, 1.0f);
1130 MEM_freeN(dvert_array);
1138 /* This finds all of the vertices face-connected to vert by an edge and returns a
1139 * MEM_allocated array of indices of size count.
1140 * count is an int passed by reference so it can be assigned the value of the length here. */
1141 static int *getSurroundingVerts(Mesh *me, int vert, int *count)
1143 MPoly *mp = me->mpoly;
1144 int i = me->totpoly;
1145 /* Instead of looping twice on all polys and loops, and use a temp array, let's rather
1146 * use a BLI_array, with a reasonable starting/reserved size (typically, there are not
1147 * many vertices face-linked to another one, even 8 might be too high...). */
1149 BLI_array_declare(verts);
1151 BLI_array_reserve(verts, 8);
1153 int j = mp->totloop;
1154 int first_l = mp->totloop - 1;
1155 MLoop *ml = &me->mloop[mp->loopstart];
1157 /* XXX This assume a vert can only be once in a poly, even though
1158 * it seems logical to me, not totally sure of that. */
1159 if (ml->v == vert) {
1162 /* We are on the first corner. */
1167 /* We are on the last corner. */
1169 b = me->mloop[mp->loopstart].v;
1176 /* Append a and b verts to array, if not yet present. */
1177 k = BLI_array_count(verts);
1178 /* XXX Maybe a == b is enough? */
1179 while (k-- && !(a == b && a == -1)) {
1182 else if (verts[k] == b)
1186 BLI_array_append(verts, a);
1188 BLI_array_append(verts, b);
1190 /* Vert found in this poly, we can go to next one! */
1198 /* Do not free the array! */
1199 *count = BLI_array_count(verts);
1203 /* get a single point in space by averaging a point cloud (vectors of size 3)
1204 * coord is the place the average is stored, points is the point cloud, count is the number of points in the cloud
1206 static void getSingleCoordinate(MVert *points, int count, float coord[3])
1210 for (i = 0; i < count; i++) {
1211 add_v3_v3(coord, points[i].co);
1213 mul_v3_fl(coord, 1.0f / count);
1216 /* given a plane and a start and end position,
1217 * compute the amount of vertical distance relative to the plane and store it in dists,
1218 * then get the horizontal and vertical change and store them in changes
1220 static void getVerticalAndHorizontalChange(const float norm[3], float d, const float coord[3],
1221 const float start[3], float distToStart,
1222 float *end, float (*changes)[2], float *dists, int index)
1224 /* A = Q - ((Q - P).N)N
1225 * D = (a * x0 + b * y0 +c * z0 + d) */
1226 float projA[3], projB[3];
1229 plane_from_point_normal_v3(plane, coord, norm);
1231 closest_to_plane_normalized_v3(projA, plane, start);
1232 closest_to_plane_normalized_v3(projB, plane, end);
1233 /* (vertical and horizontal refer to the plane's y and xz respectively)
1234 * vertical distance */
1235 dists[index] = dot_v3v3(norm, end) + d;
1236 /* vertical change */
1237 changes[index][0] = dists[index] - distToStart;
1238 //printf("vc %f %f\n", distance(end, projB, 3) - distance(start, projA, 3), changes[index][0]);
1239 /* horizontal change */
1240 changes[index][1] = len_v3v3(projA, projB);
1243 /* I need the derived mesh to be forgotten so the positions are recalculated
1244 * with weight changes (see dm_deform_recalc) */
1245 static void dm_deform_clear(DerivedMesh *dm, Object *ob)
1247 if (ob->derivedDeform && (ob->derivedDeform) == dm) {
1248 ob->derivedDeform->needsFree = 1;
1249 ob->derivedDeform->release(ob->derivedDeform);
1250 ob->derivedDeform = NULL;
1258 /* recalculate the deformation */
1259 static DerivedMesh *dm_deform_recalc(Scene *scene, Object *ob)
1261 return mesh_get_derived_deform(scene, ob, CD_MASK_BAREMESH);
1264 /* by changing nonzero weights, try to move a vertex in me->mverts with index 'index' to
1265 * distToBe distance away from the provided plane strength can change distToBe so that it moves
1266 * towards distToBe by that percentage cp changes how much the weights are adjusted
1267 * to check the distance
1269 * index is the index of the vertex being moved
1270 * norm and d are the plane's properties for the equation: ax + by + cz + d = 0
1271 * coord is a point on the plane
1273 static void moveCloserToDistanceFromPlane(Scene *scene, Object *ob, Mesh *me, int index, float norm[3],
1274 float coord[3], float d, float distToBe, float strength, float cp)
1279 MDeformVert *dvert = me->dvert + index;
1280 int totweight = dvert->totweight;
1282 float oldPos[3] = {0};
1283 float vc, hc, dist = 0.0f;
1285 float (*changes)[2] = MEM_mallocN(sizeof(float *) * totweight * 2, "vertHorzChange");
1286 float *dists = MEM_mallocN(sizeof(float) * totweight, "distance");
1288 /* track if up or down moved it closer for each bone */
1289 int *upDown = MEM_callocN(sizeof(int) * totweight, "upDownTracker");
1291 int *dwIndices = MEM_callocN(sizeof(int) * totweight, "dwIndexTracker");
1297 float originalDistToBe = distToBe;
1300 dm = dm_deform_recalc(scene, ob);
1301 dm->getVert(dm, index, &m);
1302 copy_v3_v3(oldPos, m.co);
1303 distToStart = dot_v3v3(norm, oldPos) + d;
1305 if (distToBe == originalDistToBe) {
1306 distToBe += distToStart - distToStart * strength;
1308 for (i = 0; i < totweight; i++) {
1310 dw = (dvert->dw + i);
1315 dists[i] = distToStart;
1318 for (k = 0; k < 2; k++) {
1320 dm_deform_clear(dm, ob); dm = NULL;
1324 dw->weight *= 1 + cp;
1327 dw->weight /= 1 + cp;
1329 if (dw->weight == oldw) {
1332 dists[i] = distToStart;
1335 if (dw->weight > 1) {
1338 dm = dm_deform_recalc(scene, ob);
1339 dm->getVert(dm, index, &m);
1340 getVerticalAndHorizontalChange(norm, d, coord, oldPos, distToStart, m.co, changes, dists, i);
1348 if (fabsf(dist - distToBe) < fabsf(dists[i] - distToBe)) {
1357 if (fabsf(dists[i] - distToBe) > fabsf(distToStart - distToBe)) {
1360 dists[i] = distToStart;
1365 /* sort the changes by the vertical change */
1366 for (k = 0; k < totweight; k++) {
1370 for (i = k + 1; i < totweight; i++) {
1373 if (fabsf(dist) > fabsf(dists[i])) {
1378 if (bestIndex != k) {
1380 upDown[k] = upDown[bestIndex];
1381 upDown[bestIndex] = ti;
1384 dwIndices[k] = dwIndices[bestIndex];
1385 dwIndices[bestIndex] = ti;
1388 changes[k][0] = changes[bestIndex][0];
1389 changes[bestIndex][0] = tf;
1392 changes[k][1] = changes[bestIndex][1];
1393 changes[bestIndex][1] = tf;
1396 dists[k] = dists[bestIndex];
1397 dists[bestIndex] = tf;
1401 /* find the best change with an acceptable horizontal change */
1402 for (i = 0; i < totweight; i++) {
1403 if (fabsf(changes[i][0]) > fabsf(changes[i][1] * 2.0f)) {
1408 if (bestIndex != -1) {
1410 /* it is a good place to stop if it tries to move the opposite direction
1411 * (relative to the plane) of last time */
1412 if (lastIndex != -1) {
1413 if (wasUp != upDown[bestIndex]) {
1417 lastIndex = bestIndex;
1418 wasUp = upDown[bestIndex];
1419 dw = (dvert->dw + dwIndices[bestIndex]);
1421 if (upDown[bestIndex]) {
1422 dw->weight *= 1 + cp;
1425 dw->weight /= 1 + cp;
1427 if (dw->weight > 1) {
1430 if (oldw == dw->weight) {
1434 dm_deform_clear(dm, ob); dm = NULL;
1437 } while (wasChange && ((distToStart - distToBe) / fabsf(distToStart - distToBe) ==
1438 (dists[bestIndex] - distToBe) / fabsf(dists[bestIndex] - distToBe)));
1443 MEM_freeN(dwIndices);
1446 /* this is used to try to smooth a surface by only adjusting the nonzero weights of a vertex
1447 * but it could be used to raise or lower an existing 'bump.' */
1448 static void vgroup_fix(Scene *scene, Object *ob, float distToBe, float strength, float cp)
1452 Mesh *me = ob->data;
1453 MVert *mvert = me->mvert;
1455 if (!(me->editflag & ME_EDIT_PAINT_VERT_SEL))
1457 for (i = 0; i < me->totvert && mvert; i++, mvert++) {
1458 if (mvert->flag & SELECT) {
1460 if ((verts = getSurroundingVerts(me, i, &count))) {
1462 MVert *p = MEM_callocN(sizeof(MVert) * (count), "deformedPoints");
1465 DerivedMesh *dm = mesh_get_derived_deform(scene, ob, CD_MASK_BAREMESH);
1468 dm->getVert(dm, verts[k], &m);
1473 float d /*, dist */ /* UNUSED */, mag;
1476 getSingleCoordinate(p, count, coord);
1477 dm->getVert(dm, i, &m);
1478 sub_v3_v3v3(norm, m.co, coord);
1479 mag = normalize_v3(norm);
1480 if (mag) { /* zeros fix */
1481 d = -dot_v3v3(norm, coord);
1482 /* dist = (dot_v3v3(norm, m.co) + d); */ /* UNUSED */
1483 moveCloserToDistanceFromPlane(scene, ob, me, i, norm, coord, d, distToBe, strength, cp);
1494 static void vgroup_levels_subset(Object *ob, const bool *vgroup_validmap, const int vgroup_tot,
1495 const int UNUSED(subset_count),
1496 const float offset, const float gain)
1499 MDeformVert *dv, **dvert_array = NULL;
1500 int i, dvert_tot = 0;
1502 const bool use_vert_sel = vertex_group_use_vert_sel(ob);
1503 const bool use_mirror = (ob->type == OB_MESH) ? (((Mesh *)ob->data)->editflag & ME_EDIT_MIRROR_X) != 0 : false;
1505 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
1509 for (i = 0; i < dvert_tot; i++) {
1512 /* in case its not selected */
1513 if (!(dv = dvert_array[i])) {
1519 if (vgroup_validmap[j]) {
1520 dw = defvert_find_index(dv, j);
1522 dw->weight = gain * (dw->weight + offset);
1524 CLAMP(dw->weight, 0.0f, 1.0f);
1530 if (use_mirror && use_vert_sel) {
1531 ED_vgroup_parray_mirror_sync(ob, dvert_array, dvert_tot,
1532 vgroup_validmap, vgroup_tot);
1535 MEM_freeN(dvert_array);
1539 static bool vgroup_normalize_all(
1541 const bool *vgroup_validmap,
1542 const int vgroup_tot,
1543 const int subset_count,
1544 const bool lock_active,
1545 ReportList *reports)
1547 MDeformVert *dv, **dvert_array = NULL;
1548 int i, dvert_tot = 0;
1549 const int def_nr = ob->actdef - 1;
1551 const int use_vert_sel = vertex_group_use_vert_sel(ob);
1553 if (subset_count == 0) {
1554 BKE_report(reports, RPT_ERROR, "No vertex groups to operate on");
1558 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
1561 const int defbase_tot = BLI_listbase_count(&ob->defbase);
1562 bool *lock_flags = BKE_object_defgroup_lock_flags_get(ob, defbase_tot);
1563 bool changed = false;
1565 if ((lock_active == true) &&
1566 (lock_flags != NULL) &&
1567 (def_nr < defbase_tot))
1569 lock_flags[def_nr] = true;
1573 for (i = 0; i < defbase_tot; i++) {
1574 if (lock_flags[i] == false) {
1579 if (i == defbase_tot) {
1580 BKE_report(reports, RPT_ERROR, "All groups are locked");
1585 for (i = 0; i < dvert_tot; i++) {
1586 /* in case its not selected */
1587 if ((dv = dvert_array[i])) {
1589 defvert_normalize_lock_map(dv, vgroup_validmap, vgroup_tot,
1590 lock_flags, defbase_tot);
1592 else if (lock_active) {
1593 defvert_normalize_lock_single(dv, vgroup_validmap, vgroup_tot,
1597 defvert_normalize_subset(dv, vgroup_validmap, vgroup_tot);
1606 MEM_freeN(lock_flags);
1609 MEM_freeN(dvert_array);
1624 static EnumPropertyItem vgroup_lock_actions[] = {
1625 {VGROUP_TOGGLE, "TOGGLE", 0, "Toggle", "Unlock all vertex groups if there is at least one locked group, lock all in other case"},
1626 {VGROUP_LOCK, "LOCK", 0, "Lock", "Lock all vertex groups"},
1627 {VGROUP_UNLOCK, "UNLOCK", 0, "Unlock", "Unlock all vertex groups"},
1628 {VGROUP_INVERT, "INVERT", 0, "Invert", "Invert the lock state of all vertex groups"},
1629 {0, NULL, 0, NULL, NULL}
1632 static void vgroup_lock_all(Object *ob, int action)
1636 if (action == VGROUP_TOGGLE) {
1637 action = VGROUP_LOCK;
1638 for (dg = ob->defbase.first; dg; dg = dg->next) {
1639 if (dg->flag & DG_LOCK_WEIGHT) {
1640 action = VGROUP_UNLOCK;
1646 for (dg = ob->defbase.first; dg; dg = dg->next) {
1649 dg->flag |= DG_LOCK_WEIGHT;
1652 dg->flag &= ~DG_LOCK_WEIGHT;
1655 dg->flag ^= DG_LOCK_WEIGHT;
1661 static void vgroup_invert_subset(Object *ob,
1662 const bool *vgroup_validmap, const int vgroup_tot,
1663 const int UNUSED(subset_count), const bool auto_assign, const bool auto_remove)
1666 MDeformVert *dv, **dvert_array = NULL;
1667 int i, dvert_tot = 0;
1668 const bool use_vert_sel = vertex_group_use_vert_sel(ob);
1669 const bool use_mirror = (ob->type == OB_MESH) ? (((Mesh *)ob->data)->editflag & ME_EDIT_MIRROR_X) != 0 : false;
1671 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
1674 for (i = 0; i < dvert_tot; i++) {
1677 /* in case its not selected */
1678 if (!(dv = dvert_array[i])) {
1685 if (vgroup_validmap[j]) {
1687 dw = defvert_verify_index(dv, j);
1690 dw = defvert_find_index(dv, j);
1694 dw->weight = 1.0f - dw->weight;
1695 CLAMP(dw->weight, 0.0f, 1.0f);
1701 if (use_mirror && use_vert_sel) {
1702 ED_vgroup_parray_mirror_sync(ob, dvert_array, dvert_tot,
1703 vgroup_validmap, vgroup_tot);
1707 ED_vgroup_parray_remove_zero(dvert_array, dvert_tot,
1708 vgroup_validmap, vgroup_tot,
1712 MEM_freeN(dvert_array);
1717 WEIGHT_SMOOTH_ALL = -1,
1718 WEIGHT_SMOOTH_DESELECT = false,
1719 WEIGHT_SMOOTH_SELECT = true,
1722 static void vgroup_smooth_subset(
1723 Object *ob, const bool *vgroup_validmap, const int vgroup_tot,
1724 const int subset_count,
1725 const float fac, const int repeat,
1726 const float fac_expand, const int source)
1728 const float ifac = 1.0f - fac;
1729 MDeformVert **dvert_array = NULL;
1731 int *vgroup_subset_map = BLI_array_alloca(vgroup_subset_map, subset_count);
1732 float *vgroup_subset_weights = BLI_array_alloca(vgroup_subset_weights, subset_count);
1733 const bool use_mirror = (ob->type == OB_MESH) ? (((Mesh *)ob->data)->editflag & ME_EDIT_MIRROR_X) != 0 : false;
1735 const int expand_sign = signum_i(fac_expand);
1736 const float expand = fabsf(fac_expand);
1737 const float iexpand = 1.0f - expand;
1739 BMEditMesh *em = BKE_editmesh_from_object(ob);
1740 BMesh *bm = em ? em->bm : NULL;
1741 Mesh *me = em ? NULL : ob->data;
1746 float *weight_accum_prev;
1747 float *weight_accum_curr;
1749 unsigned int subset_index;
1751 /* vertex indices that will be smoothed, (only to avoid iterating over verts that do nothing) */
1752 unsigned int *verts_used;
1753 STACK_DECLARE(verts_used);
1756 BKE_object_defgroup_subset_to_index_array(vgroup_validmap, vgroup_tot, vgroup_subset_map);
1757 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, false);
1758 memset(vgroup_subset_weights, 0, sizeof(*vgroup_subset_weights) * subset_count);
1761 BM_mesh_elem_table_ensure(bm, BM_VERT);
1762 BM_mesh_elem_index_ensure(bm, BM_VERT);
1768 BKE_mesh_vert_edge_map_create(&emap, &emap_mem, me->medge, me->totvert, me->totedge);
1771 weight_accum_prev = MEM_mallocN(sizeof(*weight_accum_prev) * dvert_tot, __func__);
1772 weight_accum_curr = MEM_mallocN(sizeof(*weight_accum_curr) * dvert_tot, __func__);
1774 verts_used = MEM_mallocN(sizeof(*verts_used) * dvert_tot, __func__);
1775 STACK_INIT(verts_used, dvert_tot);
1778 /* initialize used verts */
1780 for (int i = 0; i < dvert_tot; i++) {
1781 BMVert *v = BM_vert_at_index(bm, i);
1782 if (BM_elem_flag_test(v, BM_ELEM_SELECT)) {
1785 BM_ITER_ELEM (e, &eiter, v, BM_EDGES_OF_VERT) {
1786 BMVert *v_other = BM_edge_other_vert(e, v);
1787 if ((source == WEIGHT_SMOOTH_ALL) ||
1788 (source == (BM_elem_flag_test(v_other, BM_ELEM_SELECT) != 0)))
1790 STACK_PUSH(verts_used, i);
1798 for (int i = 0; i < dvert_tot; i++) {
1799 MVert *v = &me->mvert[i];
1800 if (v->flag & SELECT) {
1801 for (int j = 0; j < emap[i].count; j++) {
1802 MVert *v_other = &me->mvert[emap[i].indices[j]];
1803 if ((source == WEIGHT_SMOOTH_ALL) ||
1804 (source == ((v_other->flag & SELECT) != 0)))
1806 STACK_PUSH(verts_used, i);
1814 for (subset_index = 0; subset_index < subset_count; subset_index++) {
1815 const int def_nr = vgroup_subset_map[subset_index];
1818 ED_vgroup_parray_to_weight_array((const MDeformVert **)dvert_array, dvert_tot, weight_accum_prev, def_nr);
1819 memcpy(weight_accum_curr, weight_accum_prev, sizeof(*weight_accum_curr) * dvert_tot);
1821 for (iter = 0; iter < repeat; iter++) {
1822 unsigned *vi_step, *vi_end = verts_used + STACK_SIZE(verts_used);
1824 /* avoid looping over all verts */
1825 // for (i = 0; i < dvert_tot; i++)
1826 for (vi_step = verts_used; vi_step != vi_end; vi_step++) {
1827 const unsigned int i = *vi_step;
1828 float weight_tot = 0.0f;
1829 float weight = 0.0f;
1831 #define WEIGHT_ACCUMULATE \
1833 float weight_other = weight_accum_prev[i_other]; \
1834 float tot_factor = 1.0f; \
1835 if (expand_sign == 1) { /* expand */ \
1836 if (weight_other < weight_accum_prev[i]) { \
1837 weight_other = (weight_accum_prev[i] * expand) + (weight_other * iexpand); \
1838 tot_factor = iexpand; \
1841 else if (expand_sign == -1) { /* contract */ \
1842 if (weight_other > weight_accum_prev[i]) { \
1843 weight_other = (weight_accum_prev[i] * expand) + (weight_other * iexpand); \
1844 tot_factor = iexpand; \
1847 weight += tot_factor * weight_other; \
1848 weight_tot += tot_factor; \
1853 BMVert *v = BM_vert_at_index(bm, i);
1857 /* checked already */
1858 BLI_assert(BM_elem_flag_test(v, BM_ELEM_SELECT));
1860 BM_ITER_ELEM (e, &eiter, v, BM_EDGES_OF_VERT) {
1861 BMVert *v_other = BM_edge_other_vert(e, v);
1862 if ((source == WEIGHT_SMOOTH_ALL) ||
1863 (source == (BM_elem_flag_test(v_other, BM_ELEM_SELECT) != 0)))
1865 const int i_other = BM_elem_index_get(v_other);
1874 /* checked already */
1875 BLI_assert(me->mvert[i].flag & SELECT);
1877 for (j = 0; j < emap[i].count; j++) {
1878 MEdge *e = &me->medge[emap[i].indices[j]];
1879 const int i_other = (e->v1 == i ? e->v2 : e->v1);
1880 MVert *v_other = &me->mvert[i_other];
1882 if ((source == WEIGHT_SMOOTH_ALL) ||
1883 (source == ((v_other->flag & SELECT) != 0)))
1890 #undef WEIGHT_ACCUMULATE
1892 if (weight_tot != 0.0f) {
1893 weight /= weight_tot;
1894 weight = (weight_accum_prev[i] * ifac) + (weight * fac);
1896 /* should be within range, just clamp because of float precision */
1897 CLAMP(weight, 0.0f, 1.0f);
1898 weight_accum_curr[i] = weight;
1902 SWAP(float *, weight_accum_curr, weight_accum_prev);
1905 ED_vgroup_parray_from_weight_array(dvert_array, dvert_tot, weight_accum_prev, def_nr, true);
1908 MEM_freeN(weight_accum_curr);
1909 MEM_freeN(weight_accum_prev);
1910 MEM_freeN(verts_used);
1917 MEM_freeN(emap_mem);
1921 MEM_freeN(dvert_array);
1923 /* not so efficient to get 'dvert_array' again just so unselected verts are NULL'd */
1925 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, true);
1926 ED_vgroup_parray_mirror_sync(ob, dvert_array, dvert_tot,
1927 vgroup_validmap, vgroup_tot);
1929 MEM_freeN(dvert_array);
1933 static int inv_cmp_mdef_vert_weights(const void *a1, const void *a2)
1935 /* qsort sorts in ascending order. We want descending order to save a memcopy
1936 * so this compare function is inverted from the standard greater than comparison qsort needs.
1937 * A normal compare function is called with two pointer arguments and should return an integer less than, equal to,
1938 * or greater than zero corresponding to whether its first argument is considered less than, equal to,
1939 * or greater than its second argument. This does the opposite. */
1940 const struct MDeformWeight *dw1 = a1, *dw2 = a2;
1942 if (dw1->weight < dw2->weight) return 1;
1943 else if (dw1->weight > dw2->weight) return -1;
1944 else if (&dw1 < &dw2) return 1; /* compare addresses so we have a stable sort algorithm */
1948 /* Used for limiting the number of influencing bones per vertex when exporting
1949 * skinned meshes. if all_deform_weights is True, limit all deform modifiers
1950 * to max_weights regardless of type, otherwise, only limit the number of influencing bones per vertex*/
1951 static int vgroup_limit_total_subset(Object *ob,
1952 const bool *vgroup_validmap,
1953 const int vgroup_tot,
1954 const int subset_count,
1955 const int max_weights)
1957 MDeformVert *dv, **dvert_array = NULL;
1958 int i, dvert_tot = 0;
1959 const int use_vert_sel = vertex_group_use_vert_sel(ob);
1962 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
1965 int num_to_drop = 0;
1967 for (i = 0; i < dvert_tot; i++) {
1969 MDeformWeight *dw_temp;
1970 int bone_count = 0, non_bone_count = 0;
1973 /* in case its not selected */
1974 if (!(dv = dvert_array[i])) {
1978 num_to_drop = subset_count - max_weights;
1980 /* first check if we even need to test further */
1981 if (num_to_drop > 0) {
1982 /* re-pack dw array so that non-bone weights are first, bone-weighted verts at end
1983 * sort the tail, then copy only the truncated array back to dv->dw */
1984 dw_temp = MEM_mallocN(sizeof(MDeformWeight) * dv->totweight, __func__);
1985 bone_count = 0; non_bone_count = 0;
1986 for (j = 0; j < dv->totweight; j++) {
1987 if (LIKELY(dv->dw[j].def_nr < vgroup_tot) &&
1988 vgroup_validmap[dv->dw[j].def_nr])
1990 dw_temp[dv->totweight - 1 - bone_count] = dv->dw[j];
1994 dw_temp[non_bone_count] = dv->dw[j];
1995 non_bone_count += 1;
1998 BLI_assert(bone_count + non_bone_count == dv->totweight);
1999 num_to_drop = bone_count - max_weights;
2000 if (num_to_drop > 0) {
2001 qsort(&dw_temp[non_bone_count], bone_count, sizeof(MDeformWeight), inv_cmp_mdef_vert_weights);
2002 dv->totweight -= num_to_drop;
2003 /* Do we want to clean/normalize here? */
2005 dv->dw = MEM_reallocN(dw_temp, sizeof(MDeformWeight) * dv->totweight);
2006 remove_tot += num_to_drop;
2014 MEM_freeN(dvert_array);
2022 static void vgroup_clean_subset(Object *ob, const bool *vgroup_validmap, const int vgroup_tot, const int UNUSED(subset_count),
2023 const float epsilon, const bool keep_single)
2025 MDeformVert **dvert_array = NULL;
2027 const bool use_vert_sel = vertex_group_use_vert_sel(ob);
2028 const bool use_mirror = (ob->type == OB_MESH) ? (((Mesh *)ob->data)->editflag & ME_EDIT_MIRROR_X) != 0 : false;
2030 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
2033 if (use_mirror && use_vert_sel) {
2034 /* correct behavior in this case isn't well defined
2035 * for now assume both sides are mirrored correctly,
2036 * so cleaning one side also cleans the other */
2037 ED_vgroup_parray_mirror_assign(ob, dvert_array, dvert_tot);
2040 ED_vgroup_parray_remove_zero(dvert_array, dvert_tot,
2041 vgroup_validmap, vgroup_tot,
2042 epsilon, keep_single);
2044 MEM_freeN(dvert_array);
2048 static void vgroup_quantize_subset(Object *ob, const bool *vgroup_validmap, const int vgroup_tot, const int UNUSED(subset_count),
2051 MDeformVert **dvert_array = NULL;
2053 const bool use_vert_sel = vertex_group_use_vert_sel(ob);
2054 const bool use_mirror = (ob->type == OB_MESH) ? (((Mesh *)ob->data)->editflag & ME_EDIT_MIRROR_X) != 0 : false;
2055 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
2058 const float steps_fl = steps;
2062 if (use_mirror && use_vert_sel) {
2063 ED_vgroup_parray_mirror_assign(ob, dvert_array, dvert_tot);
2066 for (i = 0; i < dvert_tot; i++) {
2070 /* in case its not selected */
2071 if (!(dv = dvert_array[i])) {
2075 for (j = 0, dw = dv->dw; j < dv->totweight; j++, dw++) {
2076 if ((dw->def_nr < vgroup_tot) && vgroup_validmap[dw->def_nr]) {
2077 dw->weight = floorf((dw->weight * steps_fl) + 0.5f) / steps_fl;
2078 CLAMP(dw->weight, 0.0f, 1.0f);
2083 MEM_freeN(dvert_array);
2087 static void dvert_mirror_op(MDeformVert *dvert, MDeformVert *dvert_mirr,
2088 const char sel, const char sel_mirr,
2089 const int *flip_map, const int flip_map_len,
2090 const bool mirror_weights, const bool flip_vgroups, const bool all_vgroups,
2091 const int act_vgroup)
2093 BLI_assert(sel || sel_mirr);
2095 if (sel_mirr && sel) {
2097 if (mirror_weights) {
2099 SWAP(MDeformVert, *dvert, *dvert_mirr);
2102 MDeformWeight *dw = defvert_find_index(dvert, act_vgroup);
2103 MDeformWeight *dw_mirr = defvert_find_index(dvert_mirr, act_vgroup);
2105 if (dw && dw_mirr) {
2106 SWAP(float, dw->weight, dw_mirr->weight);
2109 dw_mirr = defvert_verify_index(dvert_mirr, act_vgroup);
2110 dw_mirr->weight = dw->weight;
2111 defvert_remove_group(dvert, dw);
2114 dw = defvert_verify_index(dvert, act_vgroup);
2115 dw->weight = dw_mirr->weight;
2116 defvert_remove_group(dvert_mirr, dw_mirr);
2122 defvert_flip(dvert, flip_map, flip_map_len);
2123 defvert_flip(dvert_mirr, flip_map, flip_map_len);
2127 /* dvert should always be the target, only swaps pointer */
2129 SWAP(MDeformVert *, dvert, dvert_mirr);
2132 if (mirror_weights) {
2134 defvert_copy(dvert, dvert_mirr);
2137 defvert_copy_index(dvert, act_vgroup, dvert_mirr, act_vgroup);
2141 /* flip map already modified for 'all_vgroups' */
2143 defvert_flip(dvert, flip_map, flip_map_len);
2148 /* TODO, vgroup locking */
2149 /* TODO, face masking */
2150 void ED_vgroup_mirror(Object *ob,
2151 const bool mirror_weights, const bool flip_vgroups,
2152 const bool all_vgroups, const bool use_topology,
2153 int *r_totmirr, int *r_totfail)
2156 #define VGROUP_MIRR_OP \
2157 dvert_mirror_op(dvert, dvert_mirr, \
2159 flip_map, flip_map_len, \
2160 mirror_weights, flip_vgroups, \
2161 all_vgroups, def_nr \
2164 BMVert *eve, *eve_mirr;
2165 MDeformVert *dvert, *dvert_mirr;
2167 int *flip_map = NULL, flip_map_len;
2168 const int def_nr = ob->actdef - 1;
2169 int totmirr = 0, totfail = 0;
2171 *r_totmirr = *r_totfail = 0;
2173 if ((mirror_weights == false && flip_vgroups == false) ||
2174 (BLI_findlink(&ob->defbase, def_nr) == NULL))
2180 flip_map = all_vgroups ?
2181 defgroup_flip_map(ob, &flip_map_len, false) :
2182 defgroup_flip_map_single(ob, &flip_map_len, false, def_nr);
2184 BLI_assert(flip_map != NULL);
2186 if (flip_map == NULL) {
2187 /* something went wrong!, possibly no groups */
2196 /* only the active group */
2197 if (ob->type == OB_MESH) {
2198 Mesh *me = ob->data;
2199 BMEditMesh *em = me->edit_btmesh;
2202 const int cd_dvert_offset = CustomData_get_offset(&em->bm->vdata, CD_MDEFORMVERT);
2205 if (cd_dvert_offset == -1) {
2209 EDBM_verts_mirror_cache_begin(em, 0, true, false, use_topology);
2211 BM_mesh_elem_hflag_disable_all(em->bm, BM_VERT, BM_ELEM_TAG, false);
2213 /* Go through the list of editverts and assign them */
2214 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
2215 if (!BM_elem_flag_test(eve, BM_ELEM_TAG)) {
2216 if ((eve_mirr = EDBM_verts_mirror_get(em, eve))) {
2217 if (eve_mirr != eve) {
2218 if (!BM_elem_flag_test(eve_mirr, BM_ELEM_TAG)) {
2219 sel = BM_elem_flag_test(eve, BM_ELEM_SELECT);
2220 sel_mirr = BM_elem_flag_test(eve_mirr, BM_ELEM_SELECT);
2222 if ((sel || sel_mirr) && (eve != eve_mirr)) {
2223 dvert = BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset);
2224 dvert_mirr = BM_ELEM_CD_GET_VOID_P(eve_mirr, cd_dvert_offset);
2230 /* don't use these again */
2231 BM_elem_flag_enable(eve, BM_ELEM_TAG);
2232 BM_elem_flag_enable(eve_mirr, BM_ELEM_TAG);
2241 EDBM_verts_mirror_cache_end(em);
2244 /* object mode / weight paint */
2245 MVert *mv, *mv_mirr;
2246 int vidx, vidx_mirr;
2247 const bool use_vert_sel = (me->editflag & ME_EDIT_PAINT_VERT_SEL) != 0;
2249 if (me->dvert == NULL) {
2253 if (!use_vert_sel) {
2254 sel = sel_mirr = true;
2257 /* tag verts we have used */
2258 for (vidx = 0, mv = me->mvert; vidx < me->totvert; vidx++, mv++) {
2259 mv->flag &= ~ME_VERT_TMP_TAG;
2262 for (vidx = 0, mv = me->mvert; vidx < me->totvert; vidx++, mv++) {
2263 if ((mv->flag & ME_VERT_TMP_TAG) == 0) {
2264 if ((vidx_mirr = mesh_get_x_mirror_vert(ob, NULL, vidx, use_topology)) != -1) {
2265 if (vidx != vidx_mirr) {
2266 mv_mirr = &me->mvert[vidx_mirr];
2267 if ((mv_mirr->flag & ME_VERT_TMP_TAG) == 0) {
2270 sel = mv->flag & SELECT;
2271 sel_mirr = mv_mirr->flag & SELECT;
2274 if (sel || sel_mirr) {
2275 dvert = &me->dvert[vidx];
2276 dvert_mirr = &me->dvert[vidx_mirr];
2282 mv->flag |= ME_VERT_TMP_TAG;
2283 mv_mirr->flag |= ME_VERT_TMP_TAG;
2294 else if (ob->type == OB_LATTICE) {
2295 Lattice *lt = vgroup_edit_lattice(ob);
2299 /* half but found up odd value */
2301 if (lt->pntsu == 1 || lt->dvert == NULL) {
2305 /* unlike editmesh we know that by only looping over the first half of
2306 * the 'u' indices it will cover all points except the middle which is
2307 * ok in this case */
2308 pntsu_half = lt->pntsu / 2;
2310 for (w = 0; w < lt->pntsw; w++) {
2311 for (v = 0; v < lt->pntsv; v++) {
2312 for (u = 0; u < pntsu_half; u++) {
2313 int u_inv = (lt->pntsu - 1) - u;
2315 BPoint *bp, *bp_mirr;
2317 i1 = BKE_lattice_index_from_uvw(lt, u, v, w);
2318 i2 = BKE_lattice_index_from_uvw(lt, u_inv, v, w);
2321 bp_mirr = <->def[i2];
2323 sel = bp->f1 & SELECT;
2324 sel_mirr = bp_mirr->f1 & SELECT;
2326 if (sel || sel_mirr) {
2327 dvert = <->dvert[i1];
2328 dvert_mirr = <->dvert[i2];
2339 /* disabled, confusing when you have an active pose bone */
2341 /* flip active group index */
2342 if (flip_vgroups && flip_map[def_nr] >= 0)
2343 ob->actdef = flip_map[def_nr] + 1;
2347 *r_totmirr = totmirr;
2348 *r_totfail = totfail;
2350 if (flip_map) MEM_freeN(flip_map);
2352 #undef VGROUP_MIRR_OP
2356 static void vgroup_delete_active(Object *ob)
2358 bDeformGroup *dg = BLI_findlink(&ob->defbase, ob->actdef - 1);
2362 BKE_object_defgroup_remove(ob, dg);
2365 /* only in editmode */
2366 static void vgroup_assign_verts(Object *ob, const float weight)
2368 const int def_nr = ob->actdef - 1;
2370 if (!BLI_findlink(&ob->defbase, def_nr))
2373 if (ob->type == OB_MESH) {
2374 Mesh *me = ob->data;
2376 if (me->edit_btmesh) {
2377 BMEditMesh *em = me->edit_btmesh;
2378 int cd_dvert_offset;
2383 if (!CustomData_has_layer(&em->bm->vdata, CD_MDEFORMVERT))
2384 BM_data_layer_add(em->bm, &em->bm->vdata, CD_MDEFORMVERT);
2386 cd_dvert_offset = CustomData_get_offset(&em->bm->vdata, CD_MDEFORMVERT);
2388 /* Go through the list of editverts and assign them */
2389 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
2390 if (BM_elem_flag_test(eve, BM_ELEM_SELECT)) {
2393 dv = BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset); /* can be NULL */
2394 dw = defvert_verify_index(dv, def_nr);
2396 dw->weight = weight;
2407 BKE_object_defgroup_data_create(&me->id);
2413 for (i = 0; i < me->totvert; i++, mv++, dv++) {
2414 if (mv->flag & SELECT) {
2416 dw = defvert_verify_index(dv, def_nr);
2418 dw->weight = weight;
2424 else if (ob->type == OB_LATTICE) {
2425 Lattice *lt = vgroup_edit_lattice(ob);
2430 if (lt->dvert == NULL)
2431 BKE_object_defgroup_data_create(<->id);
2435 tot = lt->pntsu * lt->pntsv * lt->pntsw;
2436 for (a = 0, bp = lt->def; a < tot; a++, bp++, dv++) {
2437 if (bp->f1 & SELECT) {
2440 dw = defvert_verify_index(dv, def_nr);
2442 dw->weight = weight;
2449 /********************** vertex group operators *********************/
2451 static int vertex_group_poll(bContext *C)
2453 Object *ob = ED_object_context(C);
2454 ID *data = (ob) ? ob->data : NULL;
2456 return (ob && !ID_IS_LINKED_DATABLOCK(ob) &&
2457 data && !ID_IS_LINKED_DATABLOCK(data) &&
2458 OB_TYPE_SUPPORT_VGROUP(ob->type) &&
2462 static int vertex_group_supported_poll(bContext *C)
2464 Object *ob = ED_object_context(C);
2465 ID *data = (ob) ? ob->data : NULL;
2466 return (ob && !ID_IS_LINKED_DATABLOCK(ob) && OB_TYPE_SUPPORT_VGROUP(ob->type) &&
2467 data && !ID_IS_LINKED_DATABLOCK(data));
2470 static int vertex_group_mesh_poll(bContext *C)
2472 Object *ob = ED_object_context(C);
2473 ID *data = (ob) ? ob->data : NULL;
2475 return (ob && !ID_IS_LINKED_DATABLOCK(ob) &&
2476 data && !ID_IS_LINKED_DATABLOCK(data) &&
2477 ob->type == OB_MESH &&
2481 static int UNUSED_FUNCTION(vertex_group_mesh_supported_poll)(bContext *C)
2483 Object *ob = ED_object_context(C);
2484 ID *data = (ob) ? ob->data : NULL;
2485 return (ob && !ID_IS_LINKED_DATABLOCK(ob) && ob->type == OB_MESH && data && !ID_IS_LINKED_DATABLOCK(data));
2489 static int UNUSED_FUNCTION(vertex_group_poll_edit) (bContext *C)
2491 Object *ob = ED_object_context(C);
2492 ID *data = (ob) ? ob->data : NULL;
2494 if (!(ob && !ID_IS_LINKED_DATABLOCK(ob) && data && !ID_IS_LINKED_DATABLOCK(data)))
2497 return BKE_object_is_in_editmode_vgroup(ob);
2500 /* editmode _or_ weight paint vertex sel */
2501 static int vertex_group_vert_select_poll_ex(bContext *C, const short ob_type_flag)
2503 Object *ob = ED_object_context(C);
2504 ID *data = (ob) ? ob->data : NULL;
2506 if (!(ob && !ID_IS_LINKED_DATABLOCK(ob) && data && !ID_IS_LINKED_DATABLOCK(data)))
2509 if (ob_type_flag && (((1 << ob->type) & ob_type_flag)) == 0) {
2513 if (BKE_object_is_in_editmode_vgroup(ob)) {
2516 else if (ob->mode & OB_MODE_WEIGHT_PAINT) {
2517 if (BKE_object_is_in_wpaint_select_vert(ob)) {
2521 CTX_wm_operator_poll_msg_set(C, "Vertex select needs to be enabled in weight paint mode");
2530 static int vertex_group_vert_select_poll(bContext *C)
2532 return vertex_group_vert_select_poll_ex(C, 0);
2535 static int vertex_group_mesh_vert_select_poll(bContext *C)
2537 return vertex_group_vert_select_poll_ex(C, (1 << OB_MESH));
2540 /* editmode _or_ weight paint vertex sel and active group unlocked */
2541 static int vertex_group_vert_select_unlocked_poll(bContext *C)
2543 Object *ob = ED_object_context(C);
2544 ID *data = (ob) ? ob->data : NULL;
2546 if (!(ob && !ID_IS_LINKED_DATABLOCK(ob) && data && !ID_IS_LINKED_DATABLOCK(data)))
2549 if (!(BKE_object_is_in_editmode_vgroup(ob) ||
2550 BKE_object_is_in_wpaint_select_vert(ob)))
2555 if (ob->actdef != 0) {
2556 bDeformGroup *dg = BLI_findlink(&ob->defbase, ob->actdef - 1);
2558 return !(dg->flag & DG_LOCK_WEIGHT);
2564 static int vertex_group_vert_select_mesh_poll(bContext *C)
2566 Object *ob = ED_object_context(C);
2567 ID *data = (ob) ? ob->data : NULL;
2569 if (!(ob && !ID_IS_LINKED_DATABLOCK(ob) && data && !ID_IS_LINKED_DATABLOCK(data)))
2572 /* only difference to #vertex_group_vert_select_poll */
2573 if (ob->type != OB_MESH)
2576 return (BKE_object_is_in_editmode_vgroup(ob) ||
2577 BKE_object_is_in_wpaint_select_vert(ob));
2580 static int vertex_group_add_exec(bContext *C, wmOperator *UNUSED(op))
2582 Object *ob = ED_object_context(C);
2584 BKE_object_defgroup_add(ob);
2585 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2586 WM_event_add_notifier(C, NC_GEOM | ND_VERTEX_GROUP, ob->data);
2587 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2589 return OPERATOR_FINISHED;
2592 void OBJECT_OT_vertex_group_add(wmOperatorType *ot)
2595 ot->name = "Add Vertex Group";
2596 ot->idname = "OBJECT_OT_vertex_group_add";
2597 ot->description = "Add a new vertex group to the active object";
2600 ot->poll = vertex_group_supported_poll;
2601 ot->exec = vertex_group_add_exec;
2604 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2607 static int vertex_group_remove_exec(bContext *C, wmOperator *op)
2609 Object *ob = ED_object_context(C);
2611 if (RNA_boolean_get(op->ptr, "all"))
2612 BKE_object_defgroup_remove_all(ob);
2613 else if (RNA_boolean_get(op->ptr, "all_unlocked"))
2614 BKE_object_defgroup_remove_all_ex(ob, true);
2616 vgroup_delete_active(ob);
2618 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2619 WM_event_add_notifier(C, NC_GEOM | ND_VERTEX_GROUP, ob->data);
2620 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2622 return OPERATOR_FINISHED;
2625 void OBJECT_OT_vertex_group_remove(wmOperatorType *ot)
2628 ot->name = "Remove Vertex Group";
2629 ot->idname = "OBJECT_OT_vertex_group_remove";
2630 ot->description = "Delete the active or all vertex groups from the active object";
2633 ot->poll = vertex_group_poll;
2634 ot->exec = vertex_group_remove_exec;
2637 /* redo operator will fail in this case because vertex groups aren't stored
2638 * in local edit mode stack and toggling "all" property will lead to
2639 * all groups deleted without way to restore them (see [#29527], sergey) */
2640 ot->flag = /*OPTYPE_REGISTER|*/ OPTYPE_UNDO;
2643 RNA_def_boolean(ot->srna, "all", 0, "All", "Remove all vertex groups");
2644 RNA_def_boolean(ot->srna, "all_unlocked", 0, "All Unlocked", "Remove all unlocked vertex groups");
2647 static int vertex_group_assign_exec(bContext *C, wmOperator *UNUSED(op))
2649 ToolSettings *ts = CTX_data_tool_settings(C);
2650 Object *ob = ED_object_context(C);
2652 vgroup_assign_verts(ob, ts->vgroup_weight);
2653 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2654 WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
2656 return OPERATOR_FINISHED;
2659 void OBJECT_OT_vertex_group_assign(wmOperatorType *ot)
2662 ot->name = "Assign to Vertex Group";
2663 ot->idname = "OBJECT_OT_vertex_group_assign";
2664 ot->description = "Assign the selected vertices to the active vertex group";
2667 ot->poll = vertex_group_vert_select_unlocked_poll;
2668 ot->exec = vertex_group_assign_exec;
2671 /* redo operator will fail in this case because vertex group assignment
2672 * isn't stored in local edit mode stack and toggling "new" property will
2673 * lead to creating plenty of new vertex groups (see [#29527], sergey) */
2674 ot->flag = /*OPTYPE_REGISTER|*/ OPTYPE_UNDO;
2677 /* NOTE: just a wrapper around vertex_group_assign_exec(), except we add these to a new group */
2678 static int vertex_group_assign_new_exec(bContext *C, wmOperator *op)
2680 /* create new group... */
2681 Object *ob = ED_object_context(C);
2682 BKE_object_defgroup_add(ob);
2684 /* assign selection to new group */
2685 return vertex_group_assign_exec(C, op);
2688 void OBJECT_OT_vertex_group_assign_new(wmOperatorType *ot)
2691 ot->name = "Assign to New Group";
2692 ot->idname = "OBJECT_OT_vertex_group_assign_new";
2693 ot->description = "Assign the selected vertices to a new vertex group";
2696 ot->poll = vertex_group_vert_select_poll;
2697 ot->exec = vertex_group_assign_new_exec;
2700 /* redo operator will fail in this case because vertex group assignment
2701 * isn't stored in local edit mode stack and toggling "new" property will
2702 * lead to creating plenty of new vertex groups (see [#29527], sergey) */
2703 ot->flag = /*OPTYPE_REGISTER|*/ OPTYPE_UNDO;
2706 static int vertex_group_remove_from_exec(bContext *C, wmOperator *op)
2708 const bool use_all_groups = RNA_boolean_get(op->ptr, "use_all_groups");
2709 const bool use_all_verts = RNA_boolean_get(op->ptr, "use_all_verts");
2711 Object *ob = ED_object_context(C);
2713 if (use_all_groups) {
2714 if (BKE_object_defgroup_clear_all(ob, true) == false) {
2715 return OPERATOR_CANCELLED;
2719 bDeformGroup *dg = BLI_findlink(&ob->defbase, ob->actdef - 1);
2721 if ((dg == NULL) || (BKE_object_defgroup_clear(ob, dg, !use_all_verts) == false)) {
2722 return OPERATOR_CANCELLED;
2726 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2727 WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
2729 return OPERATOR_FINISHED;
2732 void OBJECT_OT_vertex_group_remove_from(wmOperatorType *ot)
2736 ot->name = "Remove from Vertex Group";
2737 ot->idname = "OBJECT_OT_vertex_group_remove_from";
2738 ot->description = "Remove the selected vertices from active or all vertex group(s)";
2741 ot->poll = vertex_group_vert_select_unlocked_poll;
2742 ot->exec = vertex_group_remove_from_exec;
2745 /* redo operator will fail in this case because vertex groups assignment
2746 * isn't stored in local edit mode stack and toggling "all" property will lead to
2747 * removing vertices from all groups (see [#29527], sergey) */
2748 ot->flag = /*OPTYPE_REGISTER|*/ OPTYPE_UNDO;
2751 prop = RNA_def_boolean(ot->srna, "use_all_groups", 0, "All Groups", "Remove from all groups");
2752 RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2753 prop = RNA_def_boolean(ot->srna, "use_all_verts", 0, "All Verts", "Clear the active group");
2754 RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2757 static int vertex_group_select_exec(bContext *C, wmOperator *UNUSED(op))
2759 Object *ob = ED_object_context(C);
2761 if (!ob || ID_IS_LINKED_DATABLOCK(ob))
2762 return OPERATOR_CANCELLED;
2764 vgroup_select_verts(ob, 1);
2765 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, ob->data);
2767 return OPERATOR_FINISHED;
2770 void OBJECT_OT_vertex_group_select(wmOperatorType *ot)
2773 ot->name = "Select Vertex Group";
2774 ot->idname = "OBJECT_OT_vertex_group_select";
2775 ot->description = "Select all the vertices assigned to the active vertex group";
2778 ot->poll = vertex_group_vert_select_poll;
2779 ot->exec = vertex_group_select_exec;
2782 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2785 static int vertex_group_deselect_exec(bContext *C, wmOperator *UNUSED(op))
2787 Object *ob = ED_object_context(C);
2789 vgroup_select_verts(ob, 0);
2790 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, ob->data);
2792 return OPERATOR_FINISHED;
2795 void OBJECT_OT_vertex_group_deselect(wmOperatorType *ot)
2798 ot->name = "Deselect Vertex Group";
2799 ot->idname = "OBJECT_OT_vertex_group_deselect";
2800 ot->description = "Deselect all selected vertices assigned to the active vertex group";
2803 ot->poll = vertex_group_vert_select_poll;
2804 ot->exec = vertex_group_deselect_exec;
2807 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2810 static int vertex_group_copy_exec(bContext *C, wmOperator *UNUSED(op))
2812 Object *ob = ED_object_context(C);
2814 vgroup_duplicate(ob);
2815 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2816 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2817 WM_event_add_notifier(C, NC_GEOM | ND_VERTEX_GROUP, ob->data);
2819 return OPERATOR_FINISHED;
2822 void OBJECT_OT_vertex_group_copy(wmOperatorType *ot)
2825 ot->name = "Copy Vertex Group";
2826 ot->idname = "OBJECT_OT_vertex_group_copy";
2827 ot->description = "Make a copy of the active vertex group";
2830 ot->poll = vertex_group_poll;
2831 ot->exec = vertex_group_copy_exec;
2834 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2837 static int vertex_group_levels_exec(bContext *C, wmOperator *op)
2839 Object *ob = ED_object_context(C);
2841 float offset = RNA_float_get(op->ptr, "offset");
2842 float gain = RNA_float_get(op->ptr, "gain");
2843 eVGroupSelect subset_type = RNA_enum_get(op->ptr, "group_select_mode");
2845 int subset_count, vgroup_tot;
2847 const bool *vgroup_validmap = BKE_object_defgroup_subset_from_select_type(ob, subset_type, &vgroup_tot, &subset_count);
2848 vgroup_levels_subset(ob, vgroup_validmap, vgroup_tot, subset_count, offset, gain);
2849 MEM_freeN((void *)vgroup_validmap);
2851 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2852 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2853 WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
2855 return OPERATOR_FINISHED;
2858 void OBJECT_OT_vertex_group_levels(wmOperatorType *ot)
2861 ot->name = "Vertex Group Levels";
2862 ot->idname = "OBJECT_OT_vertex_group_levels";
2863 ot->description = "Add some offset and multiply with some gain the weights of the active vertex group";
2866 ot->poll = vertex_group_poll;
2867 ot->exec = vertex_group_levels_exec;
2870 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2872 vgroup_operator_subset_select_props(ot, true);
2873 RNA_def_float(ot->srna, "offset", 0.f, -1.0, 1.0, "Offset", "Value to add to weights", -1.0f, 1.f);
2874 RNA_def_float(ot->srna, "gain", 1.f, 0.f, FLT_MAX, "Gain", "Value to multiply weights by", 0.0f, 10.f);
2877 static int vertex_group_normalize_exec(bContext *C, wmOperator *UNUSED(op))
2879 Object *ob = ED_object_context(C);
2882 changed = vgroup_normalize(ob);
2885 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2886 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2887 WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
2889 return OPERATOR_FINISHED;
2892 return OPERATOR_CANCELLED;
2896 void OBJECT_OT_vertex_group_normalize(wmOperatorType *ot)
2899 ot->name = "Normalize Vertex Group";
2900 ot->idname = "OBJECT_OT_vertex_group_normalize";
2901 ot->description = "Normalize weights of the active vertex group, so that the highest ones are now 1.0";
2904 ot->poll = vertex_group_poll;
2905 ot->exec = vertex_group_normalize_exec;
2908 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2911 static int vertex_group_normalize_all_exec(bContext *C, wmOperator *op)
2913 Object *ob = ED_object_context(C);
2914 bool lock_active = RNA_boolean_get(op->ptr, "lock_active");
2915 eVGroupSelect subset_type = RNA_enum_get(op->ptr, "group_select_mode");
2917 int subset_count, vgroup_tot;
2918 const bool *vgroup_validmap = BKE_object_defgroup_subset_from_select_type(ob, subset_type, &vgroup_tot, &subset_count);
2920 changed = vgroup_normalize_all(ob, vgroup_validmap, vgroup_tot, subset_count, lock_active, op->reports);
2921 MEM_freeN((void *)vgroup_validmap);
2924 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
2925 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2926 WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
2928 return OPERATOR_FINISHED;
2931 /* allow to adjust settings */
2932 return OPERATOR_FINISHED;
2936 void OBJECT_OT_vertex_group_normalize_all(wmOperatorType *ot)
2939 ot->name = "Normalize All Vertex Groups";
2940 ot->idname = "OBJECT_OT_vertex_group_normalize_all";
2941 ot->description = "Normalize all weights of all vertex groups, "
2942 "so that for each vertex, the sum of all weights is 1.0";
2945 ot->poll = vertex_group_poll;
2946 ot->exec = vertex_group_normalize_all_exec;
2949 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2951 vgroup_operator_subset_select_props(ot, false);
2952 RNA_def_boolean(ot->srna, "lock_active", true, "Lock Active",
2953 "Keep the values of the active group while normalizing others");
2956 static int vertex_group_fix_exec(bContext *C, wmOperator *op)
2958 Object *ob = CTX_data_active_object(C);
2959 Scene *scene = CTX_data_scene(C);
2961 float distToBe = RNA_float_get(op->ptr, "dist");
2962 float strength = RNA_float_get(op->ptr, "strength");
2963 float cp = RNA_float_get(op->ptr, "accuracy");
2964 ModifierData *md = ob->modifiers.first;
2967 if (md->type == eModifierType_Mirror && (md->mode & eModifierMode_Realtime)) {