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_mesh_mapping.h"
60 #include "BKE_editmesh.h"
61 #include "BKE_layer.h"
62 #include "BKE_modifier.h"
63 #include "BKE_report.h"
64 #include "BKE_DerivedMesh.h"
65 #include "BKE_object_deform.h"
66 #include "BKE_object.h"
67 #include "BKE_lattice.h"
69 #include "DEG_depsgraph.h"
71 #include "DNA_armature_types.h"
72 #include "RNA_access.h"
73 #include "RNA_define.h"
74 #include "RNA_enum_types.h"
79 #include "ED_object.h"
82 #include "UI_resources.h"
84 #include "object_intern.h"
86 /************************ Exported Functions **********************/
87 static bool vertex_group_use_vert_sel(Object *ob)
89 if (ob->mode == OB_MODE_EDIT) {
92 else if (ob->type == OB_MESH && ((Mesh *)ob->data)->editflag & ME_EDIT_PAINT_VERT_SEL) {
100 static Lattice *vgroup_edit_lattice(Object *ob)
102 Lattice *lt = ob->data;
103 BLI_assert(ob->type == OB_LATTICE);
104 return (lt->editlatt) ? lt->editlatt->latt : lt;
107 bool ED_vgroup_sync_from_pose(Object *ob)
109 Object *armobj = BKE_object_pose_armature_get(ob);
110 if (armobj && (armobj->mode & OB_MODE_POSE)) {
111 struct bArmature *arm = armobj->data;
113 int def_num = defgroup_name_index(ob, arm->act_bone->name);
115 ob->actdef = def_num + 1;
124 * Removes out of range MDeformWeights
126 void ED_vgroup_data_clamp_range(ID *id, const int total)
128 MDeformVert **dvert_arr;
131 if (ED_vgroup_parray_alloc(id, &dvert_arr, &dvert_tot, false)) {
133 for (i = 0; i < dvert_tot; i++) {
134 MDeformVert *dv = dvert_arr[i];
136 for (j = 0; j < dv->totweight; j++) {
137 if (dv->dw[j].def_nr >= total) {
138 defvert_remove_group(dv, &dv->dw[j]);
146 bool ED_vgroup_parray_alloc(ID *id, MDeformVert ***dvert_arr, int *dvert_tot, const bool use_vert_sel)
152 switch (GS(id->name)) {
155 Mesh *me = (Mesh *)id;
157 if (me->edit_btmesh) {
158 BMEditMesh *em = me->edit_btmesh;
160 const int cd_dvert_offset = CustomData_get_offset(&bm->vdata, CD_MDEFORMVERT);
165 if (cd_dvert_offset == -1) {
171 *dvert_arr = MEM_mallocN(sizeof(void *) * i, "vgroup parray from me");
176 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
177 (*dvert_arr)[i] = BM_elem_flag_test(eve, BM_ELEM_SELECT) ?
178 BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset) : NULL;
183 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
184 (*dvert_arr)[i] = BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset);
191 else if (me->dvert) {
192 MVert *mvert = me->mvert;
193 MDeformVert *dvert = me->dvert;
196 *dvert_tot = me->totvert;
197 *dvert_arr = MEM_mallocN(sizeof(void *) * me->totvert, "vgroup parray from me");
200 for (i = 0; i < me->totvert; i++) {
201 (*dvert_arr)[i] = (mvert[i].flag & SELECT) ?
206 for (i = 0; i < me->totvert; i++) {
207 (*dvert_arr)[i] = me->dvert + i;
219 Lattice *lt = (Lattice *)id;
220 lt = (lt->editlatt) ? lt->editlatt->latt : lt;
223 BPoint *def = lt->def;
224 *dvert_tot = lt->pntsu * lt->pntsv * lt->pntsw;
225 *dvert_arr = MEM_mallocN(sizeof(void *) * (*dvert_tot), "vgroup parray from me");
228 for (i = 0; i < *dvert_tot; i++) {
229 (*dvert_arr)[i] = (def->f1 & SELECT) ?
230 <->dvert[i] : NULL;
234 for (i = 0; i < *dvert_tot; i++) {
235 (*dvert_arr)[i] = lt->dvert + i;
250 * For use with tools that use ED_vgroup_parray_alloc with \a use_vert_sel == true.
251 * This finds the unselected mirror deform verts and copies the weights to them from the selected.
253 * \note \a dvert_array has mirrored weights filled in, incase cleanup operations are needed on both.
255 void ED_vgroup_parray_mirror_sync(Object *ob,
256 MDeformVert **dvert_array, const int dvert_tot,
257 const bool *vgroup_validmap, const int vgroup_tot)
259 BMEditMesh *em = BKE_editmesh_from_object(ob);
260 MDeformVert **dvert_array_all = NULL;
263 /* get an array of all verts, not only selected */
264 if (ED_vgroup_parray_alloc(ob->data, &dvert_array_all, &dvert_tot_all, false) == false) {
269 BM_mesh_elem_table_ensure(em->bm, BM_VERT);
273 const int *flip_map = defgroup_flip_map(ob, &flip_map_len, true);
275 for (int i_src = 0; i_src < dvert_tot; i_src++) {
276 if (dvert_array[i_src] != NULL) {
277 /* its selected, check if its mirror exists */
278 int i_dst = ED_mesh_mirror_get_vert(ob, i_src);
279 if (i_dst != -1 && dvert_array_all[i_dst] != NULL) {
280 /* we found a match! */
281 const MDeformVert *dv_src = dvert_array[i_src];
282 MDeformVert *dv_dst = dvert_array_all[i_dst];
284 defvert_mirror_subset(dv_dst, dv_src, vgroup_validmap, vgroup_tot, flip_map, flip_map_len);
286 dvert_array[i_dst] = dvert_array_all[i_dst];
291 MEM_freeN((void *)flip_map);
292 MEM_freeN(dvert_array_all);
296 * Fill in the pointers for mirror verts (as if all mirror verts were selected too).
298 * similar to #ED_vgroup_parray_mirror_sync but only fill in mirror points.
300 void ED_vgroup_parray_mirror_assign(Object *ob,
301 MDeformVert **dvert_array, const int dvert_tot)
303 BMEditMesh *em = BKE_editmesh_from_object(ob);
304 MDeformVert **dvert_array_all = NULL;
308 /* get an array of all verts, not only selected */
309 if (ED_vgroup_parray_alloc(ob->data, &dvert_array_all, &dvert_tot_all, false) == false) {
313 BLI_assert(dvert_tot == dvert_tot_all);
315 BM_mesh_elem_table_ensure(em->bm, BM_VERT);
318 for (i = 0; i < dvert_tot; i++) {
319 if (dvert_array[i] == NULL) {
320 /* its unselected, check if its mirror is */
321 int i_sel = ED_mesh_mirror_get_vert(ob, i);
322 if ((i_sel != -1) && (i_sel != i) && (dvert_array[i_sel])) {
323 /* we found a match! */
324 dvert_array[i] = dvert_array_all[i];
329 MEM_freeN(dvert_array_all);
332 void ED_vgroup_parray_remove_zero(MDeformVert **dvert_array, const int dvert_tot,
333 const bool *vgroup_validmap, const int vgroup_tot,
334 const float epsilon, const bool keep_single)
339 for (i = 0; i < dvert_tot; i++) {
342 /* in case its not selected */
343 if (!(dv = dvert_array[i])) {
352 if (keep_single && dv->totweight == 1)
356 if ((dw->def_nr < vgroup_tot) && vgroup_validmap[dw->def_nr]) {
357 if (dw->weight <= epsilon) {
358 defvert_remove_group(dv, dw);
365 /* matching index only */
366 bool ED_vgroup_array_copy(Object *ob, Object *ob_from)
368 MDeformVert **dvert_array_from = NULL, **dvf;
369 MDeformVert **dvert_array = NULL, **dv;
373 int defbase_tot_from = BLI_listbase_count(&ob_from->defbase);
374 int defbase_tot = BLI_listbase_count(&ob->defbase);
375 bool new_vgroup = false;
380 /* in case we copy vgroup between two objects using same data, we only have to care about object side of things. */
381 if (ob->data != ob_from->data) {
382 ED_vgroup_parray_alloc(ob_from->data, &dvert_array_from, &dvert_tot_from, false);
383 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, false);
385 if ((dvert_array == NULL) && (dvert_array_from != NULL) && BKE_object_defgroup_data_create(ob->data)) {
386 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, false);
390 if (dvert_tot == 0 || (dvert_tot != dvert_tot_from) || dvert_array_from == NULL || dvert_array == NULL) {
392 MEM_freeN(dvert_array);
393 if (dvert_array_from)
394 MEM_freeN(dvert_array_from);
396 if (new_vgroup == true) {
397 /* free the newly added vgroup since it wasn't compatible */
398 BKE_object_defgroup_remove_all(ob);
401 /* if true: both are 0 and nothing needs changing, consider this a success */
402 return (dvert_tot == dvert_tot_from);
407 BLI_freelistN(&ob->defbase);
408 BLI_duplicatelist(&ob->defbase, &ob_from->defbase);
409 ob->actdef = ob_from->actdef;
411 if (defbase_tot_from < defbase_tot) {
412 /* correct vgroup indices because the number of vgroups is being reduced. */
413 int *remap = MEM_mallocN(sizeof(int) * (defbase_tot + 1), __func__);
414 for (i = 0; i <= defbase_tot_from; i++) remap[i] = i;
415 for (; i <= defbase_tot; i++) remap[i] = 0; /* can't use these, so disable */
417 BKE_object_defgroup_remap_update_users(ob, remap);
421 if (dvert_array_from != NULL && dvert_array != NULL) {
422 dvf = dvert_array_from;
425 for (i = 0; i < dvert_tot; i++, dvf++, dv++) {
426 MEM_SAFE_FREE((*dv)->dw);
430 (*dv)->dw = MEM_dupallocN((*dv)->dw);
434 MEM_freeN(dvert_array);
435 MEM_freeN(dvert_array_from);
441 void ED_vgroup_parray_to_weight_array(
442 const MDeformVert **dvert_array, const int dvert_tot,
443 float *dvert_weights, const int def_nr)
447 for (i = 0; i < dvert_tot; i++) {
448 const MDeformVert *dv = dvert_array[i];
449 dvert_weights[i] = dv ? defvert_find_weight(dv, def_nr) : 0.0f;
453 void ED_vgroup_parray_from_weight_array(
454 MDeformVert **dvert_array, const int dvert_tot,
455 const float *dvert_weights, const int def_nr, const bool remove_zero)
459 for (i = 0; i < dvert_tot; i++) {
460 MDeformVert *dv = dvert_array[i];
462 if (dvert_weights[i] > 0.0f) {
463 MDeformWeight *dw = defvert_verify_index(dv, def_nr);
464 BLI_assert(IN_RANGE_INCL(dvert_weights[i], 0.0f, 1.0f));
465 dw->weight = dvert_weights[i];
468 MDeformWeight *dw = defvert_find_index(dv, def_nr);
471 defvert_remove_group(dv, dw);
483 /* TODO, cache flip data to speedup calls within a loop. */
484 static void mesh_defvert_mirror_update_internal(Object *ob,
485 MDeformVert *dvert_dst, MDeformVert *dvert_src,
489 /* all vgroups, add groups where neded */
491 int *flip_map = defgroup_flip_map(ob, &flip_map_len, true);
492 defvert_sync_mapped(dvert_dst, dvert_src, flip_map, flip_map_len, true);
497 MDeformWeight *dw = defvert_verify_index(dvert_dst, defgroup_flip_index(ob, def_nr, 1));
499 dw->weight = defvert_find_weight(dvert_src, def_nr);
504 static void ED_mesh_defvert_mirror_update_em(Object *ob, BMVert *eve, int def_nr, int vidx,
505 const int cd_dvert_offset)
508 BMEditMesh *em = me->edit_btmesh;
510 bool use_topology = (me->editflag & ME_EDIT_MIRROR_TOPO) != 0;
512 eve_mirr = editbmesh_get_x_mirror_vert(ob, em, eve, eve->co, vidx, use_topology);
514 if (eve_mirr && eve_mirr != eve) {
515 MDeformVert *dvert_src = BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset);
516 MDeformVert *dvert_dst = BM_ELEM_CD_GET_VOID_P(eve_mirr, cd_dvert_offset);
517 mesh_defvert_mirror_update_internal(ob, dvert_dst, dvert_src, def_nr);
521 static void ED_mesh_defvert_mirror_update_ob(Object *ob, int def_nr, int vidx)
525 bool use_topology = (me->editflag & ME_EDIT_MIRROR_TOPO) != 0;
530 vidx_mirr = mesh_get_x_mirror_vert(ob, NULL, vidx, use_topology);
532 if ((vidx_mirr) >= 0 && (vidx_mirr != vidx)) {
533 MDeformVert *dvert_src = &me->dvert[vidx];
534 MDeformVert *dvert_dst = &me->dvert[vidx_mirr];
535 mesh_defvert_mirror_update_internal(ob, dvert_dst, dvert_src, def_nr);
540 * Use when adjusting the active vertex weight and apply to mirror vertices.
542 void ED_vgroup_vert_active_mirror(Object *ob, int def_nr)
545 BMEditMesh *em = me->edit_btmesh;
546 MDeformVert *dvert_act;
548 if (me->editflag & ME_EDIT_MIRROR_X) {
551 dvert_act = ED_mesh_active_dvert_get_em(ob, &eve_act);
553 const int cd_dvert_offset = CustomData_get_offset(&em->bm->vdata, CD_MDEFORMVERT);
554 ED_mesh_defvert_mirror_update_em(ob, eve_act, def_nr, -1, cd_dvert_offset);
559 dvert_act = ED_mesh_active_dvert_get_ob(ob, &v_act);
561 ED_mesh_defvert_mirror_update_ob(ob, def_nr, v_act);
567 static void vgroup_remove_weight(Object *ob, const int def_nr)
569 MDeformVert *dvert_act;
572 dvert_act = ED_mesh_active_dvert_get_only(ob);
574 dw = defvert_find_index(dvert_act, def_nr);
575 defvert_remove_group(dvert_act, dw);
579 static bool vgroup_normalize_active_vertex(Object *ob, eVGroupSelect subset_type)
582 BMEditMesh *em = me->edit_btmesh;
585 MDeformVert *dvert_act;
586 int subset_count, vgroup_tot;
587 const bool *vgroup_validmap;
591 dvert_act = ED_mesh_active_dvert_get_em(ob, &eve_act);
594 dvert_act = ED_mesh_active_dvert_get_ob(ob, &v_act);
597 if (dvert_act == NULL) {
601 vgroup_validmap = BKE_object_defgroup_subset_from_select_type(ob, subset_type, &vgroup_tot, &subset_count);
602 defvert_normalize_subset(dvert_act, vgroup_validmap, vgroup_tot);
603 MEM_freeN((void *)vgroup_validmap);
605 if (me->editflag & ME_EDIT_MIRROR_X) {
607 const int cd_dvert_offset = CustomData_get_offset(&em->bm->vdata, CD_MDEFORMVERT);
608 ED_mesh_defvert_mirror_update_em(ob, eve_act, -1, -1, cd_dvert_offset);
611 ED_mesh_defvert_mirror_update_ob(ob, -1, v_act);
618 static void vgroup_copy_active_to_sel(Object *ob, eVGroupSelect subset_type)
621 BMEditMesh *em = me->edit_btmesh;
622 MDeformVert *dvert_act;
623 int i, vgroup_tot, subset_count;
624 const bool *vgroup_validmap = BKE_object_defgroup_subset_from_select_type(ob, subset_type, &vgroup_tot, &subset_count);
629 BMVert *eve, *eve_act;
630 const int cd_dvert_offset = CustomData_get_offset(&em->bm->vdata, CD_MDEFORMVERT);
632 dvert_act = ED_mesh_active_dvert_get_em(ob, &eve_act);
634 BM_ITER_MESH_INDEX (eve, &iter, em->bm, BM_VERTS_OF_MESH, i) {
635 if (BM_elem_flag_test(eve, BM_ELEM_SELECT) && eve != eve_act) {
636 MDeformVert *dv = BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset);
637 defvert_copy_subset(dv, dvert_act, vgroup_validmap, vgroup_tot);
638 if (me->editflag & ME_EDIT_MIRROR_X) {
639 ED_mesh_defvert_mirror_update_em(ob, eve, -1, i, cd_dvert_offset);
649 dvert_act = ED_mesh_active_dvert_get_ob(ob, &v_act);
652 for (i = 0; i < me->totvert; i++, dv++) {
653 if ((me->mvert[i].flag & SELECT) && dv != dvert_act) {
654 defvert_copy_subset(dv, dvert_act, vgroup_validmap, vgroup_tot);
655 if (me->editflag & ME_EDIT_MIRROR_X) {
656 ED_mesh_defvert_mirror_update_ob(ob, -1, i);
663 MEM_freeN((void *)vgroup_validmap);
666 /***********************Start weight transfer (WT)*********************************/
668 static EnumPropertyItem WT_vertex_group_select_item[] = {
670 "ACTIVE", 0, "Active Group", "The active Vertex Group"},
671 {WT_VGROUP_BONE_SELECT,
672 "BONE_SELECT", 0, "Selected Pose Bones", "All Vertex Groups assigned to Selection"},
673 {WT_VGROUP_BONE_DEFORM,
674 "BONE_DEFORM", 0, "Deform Pose Bones", "All Vertex Groups assigned to Deform Bones"},
676 "ALL", 0, "All Groups", "All Vertex Groups"},
677 {0, NULL, 0, NULL, NULL}
680 EnumPropertyItem *ED_object_vgroup_selection_itemf_helper(
681 const bContext *C, PointerRNA *UNUSED(ptr),
682 PropertyRNA *UNUSED(prop), bool *r_free, const unsigned int selection_mask)
685 EnumPropertyItem *item = NULL;
689 if (!C) /* needed for docs and i18n tools */
690 return WT_vertex_group_select_item;
692 ob = CTX_data_active_object(C);
693 if (selection_mask & (1 << WT_VGROUP_ACTIVE))
694 RNA_enum_items_add_value(&item, &totitem, WT_vertex_group_select_item, WT_VGROUP_ACTIVE);
696 if (BKE_object_pose_armature_get(ob)) {
697 if (selection_mask & (1 << WT_VGROUP_BONE_SELECT))
698 RNA_enum_items_add_value(&item, &totitem, WT_vertex_group_select_item, WT_VGROUP_BONE_SELECT);
699 if (selection_mask & (1 << WT_VGROUP_BONE_DEFORM))
700 RNA_enum_items_add_value(&item, &totitem, WT_vertex_group_select_item, WT_VGROUP_BONE_DEFORM);
703 if (selection_mask & (1 << WT_VGROUP_ALL))
704 RNA_enum_items_add_value(&item, &totitem, WT_vertex_group_select_item, WT_VGROUP_ALL);
706 RNA_enum_item_end(&item, &totitem);
712 static EnumPropertyItem *rna_vertex_group_with_single_itemf(bContext *C, PointerRNA *ptr,
713 PropertyRNA *prop, bool *r_free)
715 return ED_object_vgroup_selection_itemf_helper(C, ptr, prop, r_free, WT_VGROUP_MASK_ALL);
718 static EnumPropertyItem *rna_vertex_group_select_itemf(bContext *C, PointerRNA *ptr,
719 PropertyRNA *prop, bool *r_free)
721 return ED_object_vgroup_selection_itemf_helper(C, ptr, prop, r_free, WT_VGROUP_MASK_ALL & ~(1 << WT_VGROUP_ACTIVE));
724 static void vgroup_operator_subset_select_props(wmOperatorType *ot, bool use_active)
728 prop = RNA_def_enum(ot->srna,
729 "group_select_mode", DummyRNA_NULL_items,
730 use_active ? WT_VGROUP_ACTIVE : WT_VGROUP_ALL, "Subset",
731 "Define which subset of Groups shall be used");
734 RNA_def_enum_funcs(prop, rna_vertex_group_with_single_itemf);
737 RNA_def_enum_funcs(prop, rna_vertex_group_select_itemf);
739 RNA_def_property_flag(prop, PROP_ENUM_NO_TRANSLATE);
744 /***********************End weight transfer (WT)***********************************/
746 /* for Mesh in Object mode */
747 /* allows editmode for Lattice */
748 static void ED_vgroup_nr_vert_add(Object *ob,
749 const int def_nr, const int vertnum,
750 const float weight, const int assignmode)
752 /* add the vert to the deform group with the
755 MDeformVert *dvert = NULL;
759 BKE_object_defgroup_array_get(ob->data, &dvert, &tot);
764 /* check that vertnum is valid before trying to get the relevant dvert */
765 if ((vertnum < 0) || (vertnum >= tot))
770 MDeformVert *dv = &dvert[vertnum];
773 /* Lets first check to see if this vert is
774 * already in the weight group -- if so
778 dw = defvert_find_index(dv, def_nr);
781 switch (assignmode) {
786 dw->weight += weight;
787 if (dw->weight >= 1.0f)
790 case WEIGHT_SUBTRACT:
791 dw->weight -= weight;
792 /* if the weight is zero or less than
793 * remove the vert from the deform group
795 if (dw->weight <= 0.0f) {
796 defvert_remove_group(dv, dw);
802 /* if the vert wasn't in the deform group then
803 * we must take a different form of action ...
806 switch (assignmode) {
807 case WEIGHT_SUBTRACT:
808 /* if we are subtracting then we don't
809 * need to do anything
815 /* if we are doing an additive assignment, then
816 * we need to create the deform weight
819 /* we checked if the vertex was added before so no need to test again, simply add */
820 defvert_add_index_notest(dv, def_nr, weight);
827 /* called while not in editmode */
828 void ED_vgroup_vert_add(Object *ob, bDeformGroup *dg, int vertnum, float weight, int assignmode)
830 /* add the vert to the deform group with the
831 * specified assign mode
833 const int def_nr = BLI_findindex(&ob->defbase, dg);
835 MDeformVert *dv = NULL;
838 /* get the deform group number, exit if
843 /* if there's no deform verts then create some,
845 if (BKE_object_defgroup_array_get(ob->data, &dv, &tot) && dv == NULL)
846 BKE_object_defgroup_data_create(ob->data);
848 /* call another function to do the work
850 ED_vgroup_nr_vert_add(ob, def_nr, vertnum, weight, assignmode);
854 /* mesh object mode, lattice can be in editmode */
855 void ED_vgroup_vert_remove(Object *ob, bDeformGroup *dg, int vertnum)
857 /* This routine removes the vertex from the specified
861 /* TODO, this is slow in a loop, better pass def_nr directly, but leave for later... - campbell */
862 const int def_nr = BLI_findindex(&ob->defbase, dg);
865 MDeformVert *dvert = NULL;
868 /* get the deform vertices corresponding to the
871 BKE_object_defgroup_array_get(ob->data, &dvert, &tot);
874 MDeformVert *dv = &dvert[vertnum];
877 dw = defvert_find_index(dv, def_nr);
878 defvert_remove_group(dv, dw); /* dw can be NULL */
883 static float get_vert_def_nr(Object *ob, const int def_nr, const int vertnum)
885 MDeformVert *dv = NULL;
887 /* get the deform vertices corresponding to the vertnum */
888 if (ob->type == OB_MESH) {
891 if (me->edit_btmesh) {
892 BMEditMesh *em = me->edit_btmesh;
893 const int cd_dvert_offset = CustomData_get_offset(&em->bm->vdata, CD_MDEFORMVERT);
894 /* warning, this lookup is _not_ fast */
896 if (cd_dvert_offset != -1 && vertnum < em->bm->totvert) {
898 BM_mesh_elem_table_ensure(em->bm, BM_VERT);
899 eve = BM_vert_at_index(em->bm, vertnum);
900 dv = BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset);
908 if (vertnum >= me->totvert) {
911 dv = &me->dvert[vertnum];
915 else if (ob->type == OB_LATTICE) {
916 Lattice *lt = vgroup_edit_lattice(ob);
919 if (vertnum >= lt->pntsu * lt->pntsv * lt->pntsw) {
922 dv = <->dvert[vertnum];
927 MDeformWeight *dw = defvert_find_index(dv, def_nr);
936 float ED_vgroup_vert_weight(Object *ob, bDeformGroup *dg, int vertnum)
938 const int def_nr = BLI_findindex(&ob->defbase, dg);
944 return get_vert_def_nr(ob, def_nr, vertnum);
947 void ED_vgroup_select_by_name(Object *ob, const char *name)
948 { /* note: ob->actdef==0 signals on painting to create a new one, if a bone in posemode is selected */
949 ob->actdef = defgroup_name_index(ob, name) + 1;
952 /********************** Operator Implementations *********************/
954 /* only in editmode */
955 static void vgroup_select_verts(Object *ob, int select)
957 const int def_nr = ob->actdef - 1;
959 if (!BLI_findlink(&ob->defbase, def_nr)) {
963 if (ob->type == OB_MESH) {
966 if (me->edit_btmesh) {
967 BMEditMesh *em = me->edit_btmesh;
968 const int cd_dvert_offset = CustomData_get_offset(&em->bm->vdata, CD_MDEFORMVERT);
970 if (cd_dvert_offset != -1) {
974 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
975 if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN)) {
976 MDeformVert *dv = BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset);
977 if (defvert_find_index(dv, def_nr)) {
978 BM_vert_select_set(em->bm, eve, select);
983 /* this has to be called, because this function operates on vertices only */
984 if (select) EDBM_select_flush(em); /* vertices to edges/faces */
985 else EDBM_deselect_flush(em);
997 for (i = 0; i < me->totvert; i++, mv++, dv++) {
998 if (!(mv->flag & ME_HIDE)) {
999 if (defvert_find_index(dv, def_nr)) {
1000 if (select) mv->flag |= SELECT;
1001 else mv->flag &= ~SELECT;
1006 paintvert_flush_flags(ob);
1010 else if (ob->type == OB_LATTICE) {
1011 Lattice *lt = vgroup_edit_lattice(ob);
1015 BPoint *bp, *actbp = BKE_lattice_active_point_get(lt);
1020 tot = lt->pntsu * lt->pntsv * lt->pntsw;
1021 for (a = 0, bp = lt->def; a < tot; a++, bp++, dv++) {
1022 if (defvert_find_index(dv, def_nr)) {
1023 if (select) bp->f1 |= SELECT;
1026 if (actbp && bp == actbp) lt->actbp = LT_ACTBP_NONE;
1034 static void vgroup_duplicate(Object *ob)
1036 bDeformGroup *dg, *cdg;
1037 char name[sizeof(dg->name)];
1038 MDeformWeight *dw_org, *dw_cpy;
1039 MDeformVert **dvert_array = NULL;
1040 int i, idg, icdg, dvert_tot = 0;
1042 dg = BLI_findlink(&ob->defbase, (ob->actdef - 1));
1046 if (!strstr(dg->name, "_copy")) {
1047 BLI_snprintf(name, sizeof(name), "%s_copy", dg->name);
1050 BLI_strncpy(name, dg->name, sizeof(name));
1053 cdg = defgroup_duplicate(dg);
1054 BLI_strncpy(cdg->name, name, sizeof(cdg->name));
1055 defgroup_unique_name(cdg, ob);
1057 BLI_addtail(&ob->defbase, cdg);
1059 idg = (ob->actdef - 1);
1060 ob->actdef = BLI_listbase_count(&ob->defbase);
1061 icdg = (ob->actdef - 1);
1063 /* TODO, we might want to allow only copy selected verts here? - campbell */
1064 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, false);
1067 for (i = 0; i < dvert_tot; i++) {
1068 MDeformVert *dv = dvert_array[i];
1069 dw_org = defvert_find_index(dv, idg);
1071 /* defvert_verify_index re-allocs org so need to store the weight first */
1072 const float weight = dw_org->weight;
1073 dw_cpy = defvert_verify_index(dv, icdg);
1074 dw_cpy->weight = weight;
1078 MEM_freeN(dvert_array);
1082 static bool vgroup_normalize(Object *ob)
1085 MDeformVert *dv, **dvert_array = NULL;
1086 int i, dvert_tot = 0;
1087 const int def_nr = ob->actdef - 1;
1089 const int use_vert_sel = vertex_group_use_vert_sel(ob);
1091 if (!BLI_findlink(&ob->defbase, def_nr)) {
1095 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
1098 float weight_max = 0.0f;
1100 for (i = 0; i < dvert_tot; i++) {
1102 /* in case its not selected */
1103 if (!(dv = dvert_array[i])) {
1107 dw = defvert_find_index(dv, def_nr);
1109 weight_max = max_ff(dw->weight, weight_max);
1113 if (weight_max > 0.0f) {
1114 for (i = 0; i < dvert_tot; i++) {
1116 /* in case its not selected */
1117 if (!(dv = dvert_array[i])) {
1121 dw = defvert_find_index(dv, def_nr);
1123 dw->weight /= weight_max;
1125 /* in case of division errors with very low weights */
1126 CLAMP(dw->weight, 0.0f, 1.0f);
1131 MEM_freeN(dvert_array);
1139 /* This finds all of the vertices face-connected to vert by an edge and returns a
1140 * MEM_allocated array of indices of size count.
1141 * count is an int passed by reference so it can be assigned the value of the length here. */
1142 static int *getSurroundingVerts(Mesh *me, int vert, int *count)
1144 MPoly *mp = me->mpoly;
1145 int i = me->totpoly;
1146 /* Instead of looping twice on all polys and loops, and use a temp array, let's rather
1147 * use a BLI_array, with a reasonable starting/reserved size (typically, there are not
1148 * many vertices face-linked to another one, even 8 might be too high...). */
1150 BLI_array_declare(verts);
1152 BLI_array_reserve(verts, 8);
1154 int j = mp->totloop;
1155 int first_l = mp->totloop - 1;
1156 MLoop *ml = &me->mloop[mp->loopstart];
1158 /* XXX This assume a vert can only be once in a poly, even though
1159 * it seems logical to me, not totally sure of that. */
1160 if (ml->v == vert) {
1163 /* We are on the first corner. */
1168 /* We are on the last corner. */
1170 b = me->mloop[mp->loopstart].v;
1177 /* Append a and b verts to array, if not yet present. */
1178 k = BLI_array_count(verts);
1179 /* XXX Maybe a == b is enough? */
1180 while (k-- && !(a == b && a == -1)) {
1183 else if (verts[k] == b)
1187 BLI_array_append(verts, a);
1189 BLI_array_append(verts, b);
1191 /* Vert found in this poly, we can go to next one! */
1199 /* Do not free the array! */
1200 *count = BLI_array_count(verts);
1204 /* get a single point in space by averaging a point cloud (vectors of size 3)
1205 * coord is the place the average is stored, points is the point cloud, count is the number of points in the cloud
1207 static void getSingleCoordinate(MVert *points, int count, float coord[3])
1211 for (i = 0; i < count; i++) {
1212 add_v3_v3(coord, points[i].co);
1214 mul_v3_fl(coord, 1.0f / count);
1217 /* given a plane and a start and end position,
1218 * compute the amount of vertical distance relative to the plane and store it in dists,
1219 * then get the horizontal and vertical change and store them in changes
1221 static void getVerticalAndHorizontalChange(const float norm[3], float d, const float coord[3],
1222 const float start[3], float distToStart,
1223 float *end, float (*changes)[2], float *dists, int index)
1225 /* A = Q - ((Q - P).N)N
1226 * D = (a * x0 + b * y0 +c * z0 + d) */
1227 float projA[3], projB[3];
1230 plane_from_point_normal_v3(plane, coord, norm);
1232 closest_to_plane_normalized_v3(projA, plane, start);
1233 closest_to_plane_normalized_v3(projB, plane, end);
1234 /* (vertical and horizontal refer to the plane's y and xz respectively)
1235 * vertical distance */
1236 dists[index] = dot_v3v3(norm, end) + d;
1237 /* vertical change */
1238 changes[index][0] = dists[index] - distToStart;
1239 //printf("vc %f %f\n", distance(end, projB, 3) - distance(start, projA, 3), changes[index][0]);
1240 /* horizontal change */
1241 changes[index][1] = len_v3v3(projA, projB);
1244 /* I need the derived mesh to be forgotten so the positions are recalculated
1245 * with weight changes (see dm_deform_recalc) */
1246 static void dm_deform_clear(DerivedMesh *dm, Object *ob)
1248 if (ob->derivedDeform && (ob->derivedDeform) == dm) {
1249 ob->derivedDeform->needsFree = 1;
1250 ob->derivedDeform->release(ob->derivedDeform);
1251 ob->derivedDeform = NULL;
1259 /* recalculate the deformation */
1260 static DerivedMesh *dm_deform_recalc(Scene *scene, Object *ob)
1262 return mesh_get_derived_deform(scene, ob, CD_MASK_BAREMESH);
1265 /* by changing nonzero weights, try to move a vertex in me->mverts with index 'index' to
1266 * distToBe distance away from the provided plane strength can change distToBe so that it moves
1267 * towards distToBe by that percentage cp changes how much the weights are adjusted
1268 * to check the distance
1270 * index is the index of the vertex being moved
1271 * norm and d are the plane's properties for the equation: ax + by + cz + d = 0
1272 * coord is a point on the plane
1274 static void moveCloserToDistanceFromPlane(Scene *scene, Object *ob, Mesh *me, int index, float norm[3],
1275 float coord[3], float d, float distToBe, float strength, float cp)
1280 MDeformVert *dvert = me->dvert + index;
1281 int totweight = dvert->totweight;
1283 float oldPos[3] = {0};
1284 float vc, hc, dist = 0.0f;
1286 float (*changes)[2] = MEM_mallocN(sizeof(float *) * totweight * 2, "vertHorzChange");
1287 float *dists = MEM_mallocN(sizeof(float) * totweight, "distance");
1289 /* track if up or down moved it closer for each bone */
1290 int *upDown = MEM_callocN(sizeof(int) * totweight, "upDownTracker");
1292 int *dwIndices = MEM_callocN(sizeof(int) * totweight, "dwIndexTracker");
1298 float originalDistToBe = distToBe;
1301 dm = dm_deform_recalc(scene, ob);
1302 dm->getVert(dm, index, &m);
1303 copy_v3_v3(oldPos, m.co);
1304 distToStart = dot_v3v3(norm, oldPos) + d;
1306 if (distToBe == originalDistToBe) {
1307 distToBe += distToStart - distToStart * strength;
1309 for (i = 0; i < totweight; i++) {
1311 dw = (dvert->dw + i);
1316 dists[i] = distToStart;
1319 for (k = 0; k < 2; k++) {
1321 dm_deform_clear(dm, ob); dm = NULL;
1325 dw->weight *= 1 + cp;
1328 dw->weight /= 1 + cp;
1330 if (dw->weight == oldw) {
1333 dists[i] = distToStart;
1336 if (dw->weight > 1) {
1339 dm = dm_deform_recalc(scene, ob);
1340 dm->getVert(dm, index, &m);
1341 getVerticalAndHorizontalChange(norm, d, coord, oldPos, distToStart, m.co, changes, dists, i);
1349 if (fabsf(dist - distToBe) < fabsf(dists[i] - distToBe)) {
1358 if (fabsf(dists[i] - distToBe) > fabsf(distToStart - distToBe)) {
1361 dists[i] = distToStart;
1366 /* sort the changes by the vertical change */
1367 for (k = 0; k < totweight; k++) {
1371 for (i = k + 1; i < totweight; i++) {
1374 if (fabsf(dist) > fabsf(dists[i])) {
1379 if (bestIndex != k) {
1381 upDown[k] = upDown[bestIndex];
1382 upDown[bestIndex] = ti;
1385 dwIndices[k] = dwIndices[bestIndex];
1386 dwIndices[bestIndex] = ti;
1389 changes[k][0] = changes[bestIndex][0];
1390 changes[bestIndex][0] = tf;
1393 changes[k][1] = changes[bestIndex][1];
1394 changes[bestIndex][1] = tf;
1397 dists[k] = dists[bestIndex];
1398 dists[bestIndex] = tf;
1402 /* find the best change with an acceptable horizontal change */
1403 for (i = 0; i < totweight; i++) {
1404 if (fabsf(changes[i][0]) > fabsf(changes[i][1] * 2.0f)) {
1409 if (bestIndex != -1) {
1411 /* it is a good place to stop if it tries to move the opposite direction
1412 * (relative to the plane) of last time */
1413 if (lastIndex != -1) {
1414 if (wasUp != upDown[bestIndex]) {
1418 lastIndex = bestIndex;
1419 wasUp = upDown[bestIndex];
1420 dw = (dvert->dw + dwIndices[bestIndex]);
1422 if (upDown[bestIndex]) {
1423 dw->weight *= 1 + cp;
1426 dw->weight /= 1 + cp;
1428 if (dw->weight > 1) {
1431 if (oldw == dw->weight) {
1435 dm_deform_clear(dm, ob); dm = NULL;
1438 } while (wasChange && ((distToStart - distToBe) / fabsf(distToStart - distToBe) ==
1439 (dists[bestIndex] - distToBe) / fabsf(dists[bestIndex] - distToBe)));
1444 MEM_freeN(dwIndices);
1447 /* this is used to try to smooth a surface by only adjusting the nonzero weights of a vertex
1448 * but it could be used to raise or lower an existing 'bump.' */
1449 static void vgroup_fix(Scene *scene, Object *ob, float distToBe, float strength, float cp)
1453 Mesh *me = ob->data;
1454 MVert *mvert = me->mvert;
1456 if (!(me->editflag & ME_EDIT_PAINT_VERT_SEL))
1458 for (i = 0; i < me->totvert && mvert; i++, mvert++) {
1459 if (mvert->flag & SELECT) {
1461 if ((verts = getSurroundingVerts(me, i, &count))) {
1463 MVert *p = MEM_callocN(sizeof(MVert) * (count), "deformedPoints");
1466 DerivedMesh *dm = mesh_get_derived_deform(scene, ob, CD_MASK_BAREMESH);
1469 dm->getVert(dm, verts[k], &m);
1474 float d /*, dist */ /* UNUSED */, mag;
1477 getSingleCoordinate(p, count, coord);
1478 dm->getVert(dm, i, &m);
1479 sub_v3_v3v3(norm, m.co, coord);
1480 mag = normalize_v3(norm);
1481 if (mag) { /* zeros fix */
1482 d = -dot_v3v3(norm, coord);
1483 /* dist = (dot_v3v3(norm, m.co) + d); */ /* UNUSED */
1484 moveCloserToDistanceFromPlane(scene, ob, me, i, norm, coord, d, distToBe, strength, cp);
1495 static void vgroup_levels_subset(Object *ob, const bool *vgroup_validmap, const int vgroup_tot,
1496 const int UNUSED(subset_count),
1497 const float offset, const float gain)
1500 MDeformVert *dv, **dvert_array = NULL;
1501 int i, dvert_tot = 0;
1503 const bool use_vert_sel = vertex_group_use_vert_sel(ob);
1504 const bool use_mirror = (ob->type == OB_MESH) ? (((Mesh *)ob->data)->editflag & ME_EDIT_MIRROR_X) != 0 : false;
1506 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
1510 for (i = 0; i < dvert_tot; i++) {
1513 /* in case its not selected */
1514 if (!(dv = dvert_array[i])) {
1520 if (vgroup_validmap[j]) {
1521 dw = defvert_find_index(dv, j);
1523 dw->weight = gain * (dw->weight + offset);
1525 CLAMP(dw->weight, 0.0f, 1.0f);
1531 if (use_mirror && use_vert_sel) {
1532 ED_vgroup_parray_mirror_sync(ob, dvert_array, dvert_tot,
1533 vgroup_validmap, vgroup_tot);
1536 MEM_freeN(dvert_array);
1540 static bool vgroup_normalize_all(
1542 const bool *vgroup_validmap,
1543 const int vgroup_tot,
1544 const int subset_count,
1545 const bool lock_active,
1546 ReportList *reports)
1548 MDeformVert *dv, **dvert_array = NULL;
1549 int i, dvert_tot = 0;
1550 const int def_nr = ob->actdef - 1;
1552 const int use_vert_sel = vertex_group_use_vert_sel(ob);
1554 if (subset_count == 0) {
1555 BKE_report(reports, RPT_ERROR, "No vertex groups to operate on");
1559 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
1562 const int defbase_tot = BLI_listbase_count(&ob->defbase);
1563 bool *lock_flags = BKE_object_defgroup_lock_flags_get(ob, defbase_tot);
1564 bool changed = false;
1566 if ((lock_active == true) &&
1567 (lock_flags != NULL) &&
1568 (def_nr < defbase_tot))
1570 lock_flags[def_nr] = true;
1574 for (i = 0; i < defbase_tot; i++) {
1575 if (lock_flags[i] == false) {
1580 if (i == defbase_tot) {
1581 BKE_report(reports, RPT_ERROR, "All groups are locked");
1586 for (i = 0; i < dvert_tot; i++) {
1587 /* in case its not selected */
1588 if ((dv = dvert_array[i])) {
1590 defvert_normalize_lock_map(dv, vgroup_validmap, vgroup_tot,
1591 lock_flags, defbase_tot);
1593 else if (lock_active) {
1594 defvert_normalize_lock_single(dv, vgroup_validmap, vgroup_tot,
1598 defvert_normalize_subset(dv, vgroup_validmap, vgroup_tot);
1607 MEM_freeN(lock_flags);
1610 MEM_freeN(dvert_array);
1625 static EnumPropertyItem vgroup_lock_actions[] = {
1626 {VGROUP_TOGGLE, "TOGGLE", 0, "Toggle", "Unlock all vertex groups if there is at least one locked group, lock all in other case"},
1627 {VGROUP_LOCK, "LOCK", 0, "Lock", "Lock all vertex groups"},
1628 {VGROUP_UNLOCK, "UNLOCK", 0, "Unlock", "Unlock all vertex groups"},
1629 {VGROUP_INVERT, "INVERT", 0, "Invert", "Invert the lock state of all vertex groups"},
1630 {0, NULL, 0, NULL, NULL}
1633 static void vgroup_lock_all(Object *ob, int action)
1637 if (action == VGROUP_TOGGLE) {
1638 action = VGROUP_LOCK;
1639 for (dg = ob->defbase.first; dg; dg = dg->next) {
1640 if (dg->flag & DG_LOCK_WEIGHT) {
1641 action = VGROUP_UNLOCK;
1647 for (dg = ob->defbase.first; dg; dg = dg->next) {
1650 dg->flag |= DG_LOCK_WEIGHT;
1653 dg->flag &= ~DG_LOCK_WEIGHT;
1656 dg->flag ^= DG_LOCK_WEIGHT;
1662 static void vgroup_invert_subset(Object *ob,
1663 const bool *vgroup_validmap, const int vgroup_tot,
1664 const int UNUSED(subset_count), const bool auto_assign, const bool auto_remove)
1667 MDeformVert *dv, **dvert_array = NULL;
1668 int i, dvert_tot = 0;
1669 const bool use_vert_sel = vertex_group_use_vert_sel(ob);
1670 const bool use_mirror = (ob->type == OB_MESH) ? (((Mesh *)ob->data)->editflag & ME_EDIT_MIRROR_X) != 0 : false;
1672 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
1675 for (i = 0; i < dvert_tot; i++) {
1678 /* in case its not selected */
1679 if (!(dv = dvert_array[i])) {
1686 if (vgroup_validmap[j]) {
1688 dw = defvert_verify_index(dv, j);
1691 dw = defvert_find_index(dv, j);
1695 dw->weight = 1.0f - dw->weight;
1696 CLAMP(dw->weight, 0.0f, 1.0f);
1702 if (use_mirror && use_vert_sel) {
1703 ED_vgroup_parray_mirror_sync(ob, dvert_array, dvert_tot,
1704 vgroup_validmap, vgroup_tot);
1708 ED_vgroup_parray_remove_zero(dvert_array, dvert_tot,
1709 vgroup_validmap, vgroup_tot,
1713 MEM_freeN(dvert_array);
1718 WEIGHT_SMOOTH_ALL = -1,
1719 WEIGHT_SMOOTH_DESELECT = false,
1720 WEIGHT_SMOOTH_SELECT = true,
1723 static void vgroup_smooth_subset(
1724 Object *ob, const bool *vgroup_validmap, const int vgroup_tot,
1725 const int subset_count,
1726 const float fac, const int repeat,
1727 const float fac_expand, const int source)
1729 const float ifac = 1.0f - fac;
1730 MDeformVert **dvert_array = NULL;
1732 int *vgroup_subset_map = BLI_array_alloca(vgroup_subset_map, subset_count);
1733 float *vgroup_subset_weights = BLI_array_alloca(vgroup_subset_weights, subset_count);
1734 const bool use_mirror = (ob->type == OB_MESH) ? (((Mesh *)ob->data)->editflag & ME_EDIT_MIRROR_X) != 0 : false;
1736 const int expand_sign = signum_i(fac_expand);
1737 const float expand = fabsf(fac_expand);
1738 const float iexpand = 1.0f - expand;
1740 BMEditMesh *em = BKE_editmesh_from_object(ob);
1741 BMesh *bm = em ? em->bm : NULL;
1742 Mesh *me = em ? NULL : ob->data;
1747 float *weight_accum_prev;
1748 float *weight_accum_curr;
1750 unsigned int subset_index;
1752 /* vertex indices that will be smoothed, (only to avoid iterating over verts that do nothing) */
1753 unsigned int *verts_used;
1754 STACK_DECLARE(verts_used);
1757 BKE_object_defgroup_subset_to_index_array(vgroup_validmap, vgroup_tot, vgroup_subset_map);
1758 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, false);
1759 memset(vgroup_subset_weights, 0, sizeof(*vgroup_subset_weights) * subset_count);
1762 BM_mesh_elem_table_ensure(bm, BM_VERT);
1763 BM_mesh_elem_index_ensure(bm, BM_VERT);
1769 BKE_mesh_vert_edge_map_create(&emap, &emap_mem, me->medge, me->totvert, me->totedge);
1772 weight_accum_prev = MEM_mallocN(sizeof(*weight_accum_prev) * dvert_tot, __func__);
1773 weight_accum_curr = MEM_mallocN(sizeof(*weight_accum_curr) * dvert_tot, __func__);
1775 verts_used = MEM_mallocN(sizeof(*verts_used) * dvert_tot, __func__);
1776 STACK_INIT(verts_used, dvert_tot);
1779 /* initialize used verts */
1781 for (int i = 0; i < dvert_tot; i++) {
1782 BMVert *v = BM_vert_at_index(bm, i);
1783 if (BM_elem_flag_test(v, BM_ELEM_SELECT)) {
1786 BM_ITER_ELEM (e, &eiter, v, BM_EDGES_OF_VERT) {
1787 BMVert *v_other = BM_edge_other_vert(e, v);
1788 if ((source == WEIGHT_SMOOTH_ALL) ||
1789 (source == (BM_elem_flag_test(v_other, BM_ELEM_SELECT) != 0)))
1791 STACK_PUSH(verts_used, i);
1799 for (int i = 0; i < dvert_tot; i++) {
1800 MVert *v = &me->mvert[i];
1801 if (v->flag & SELECT) {
1802 for (int j = 0; j < emap[i].count; j++) {
1803 MVert *v_other = &me->mvert[emap[i].indices[j]];
1804 if ((source == WEIGHT_SMOOTH_ALL) ||
1805 (source == ((v_other->flag & SELECT) != 0)))
1807 STACK_PUSH(verts_used, i);
1815 for (subset_index = 0; subset_index < subset_count; subset_index++) {
1816 const int def_nr = vgroup_subset_map[subset_index];
1819 ED_vgroup_parray_to_weight_array((const MDeformVert **)dvert_array, dvert_tot, weight_accum_prev, def_nr);
1820 memcpy(weight_accum_curr, weight_accum_prev, sizeof(*weight_accum_curr) * dvert_tot);
1822 for (iter = 0; iter < repeat; iter++) {
1823 unsigned *vi_step, *vi_end = verts_used + STACK_SIZE(verts_used);
1825 /* avoid looping over all verts */
1826 // for (i = 0; i < dvert_tot; i++)
1827 for (vi_step = verts_used; vi_step != vi_end; vi_step++) {
1828 const unsigned int i = *vi_step;
1829 float weight_tot = 0.0f;
1830 float weight = 0.0f;
1832 #define WEIGHT_ACCUMULATE \
1834 float weight_other = weight_accum_prev[i_other]; \
1835 float tot_factor = 1.0f; \
1836 if (expand_sign == 1) { /* expand */ \
1837 if (weight_other < weight_accum_prev[i]) { \
1838 weight_other = (weight_accum_prev[i] * expand) + (weight_other * iexpand); \
1839 tot_factor = iexpand; \
1842 else if (expand_sign == -1) { /* contract */ \
1843 if (weight_other > weight_accum_prev[i]) { \
1844 weight_other = (weight_accum_prev[i] * expand) + (weight_other * iexpand); \
1845 tot_factor = iexpand; \
1848 weight += tot_factor * weight_other; \
1849 weight_tot += tot_factor; \
1854 BMVert *v = BM_vert_at_index(bm, i);
1858 /* checked already */
1859 BLI_assert(BM_elem_flag_test(v, BM_ELEM_SELECT));
1861 BM_ITER_ELEM (e, &eiter, v, BM_EDGES_OF_VERT) {
1862 BMVert *v_other = BM_edge_other_vert(e, v);
1863 if ((source == WEIGHT_SMOOTH_ALL) ||
1864 (source == (BM_elem_flag_test(v_other, BM_ELEM_SELECT) != 0)))
1866 const int i_other = BM_elem_index_get(v_other);
1875 /* checked already */
1876 BLI_assert(me->mvert[i].flag & SELECT);
1878 for (j = 0; j < emap[i].count; j++) {
1879 MEdge *e = &me->medge[emap[i].indices[j]];
1880 const int i_other = (e->v1 == i ? e->v2 : e->v1);
1881 MVert *v_other = &me->mvert[i_other];
1883 if ((source == WEIGHT_SMOOTH_ALL) ||
1884 (source == ((v_other->flag & SELECT) != 0)))
1891 #undef WEIGHT_ACCUMULATE
1893 if (weight_tot != 0.0f) {
1894 weight /= weight_tot;
1895 weight = (weight_accum_prev[i] * ifac) + (weight * fac);
1897 /* should be within range, just clamp because of float precision */
1898 CLAMP(weight, 0.0f, 1.0f);
1899 weight_accum_curr[i] = weight;
1903 SWAP(float *, weight_accum_curr, weight_accum_prev);
1906 ED_vgroup_parray_from_weight_array(dvert_array, dvert_tot, weight_accum_prev, def_nr, true);
1909 MEM_freeN(weight_accum_curr);
1910 MEM_freeN(weight_accum_prev);
1911 MEM_freeN(verts_used);
1918 MEM_freeN(emap_mem);
1922 MEM_freeN(dvert_array);
1924 /* not so efficient to get 'dvert_array' again just so unselected verts are NULL'd */
1926 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, true);
1927 ED_vgroup_parray_mirror_sync(ob, dvert_array, dvert_tot,
1928 vgroup_validmap, vgroup_tot);
1930 MEM_freeN(dvert_array);
1934 static int inv_cmp_mdef_vert_weights(const void *a1, const void *a2)
1936 /* qsort sorts in ascending order. We want descending order to save a memcopy
1937 * so this compare function is inverted from the standard greater than comparison qsort needs.
1938 * A normal compare function is called with two pointer arguments and should return an integer less than, equal to,
1939 * or greater than zero corresponding to whether its first argument is considered less than, equal to,
1940 * or greater than its second argument. This does the opposite. */
1941 const struct MDeformWeight *dw1 = a1, *dw2 = a2;
1943 if (dw1->weight < dw2->weight) return 1;
1944 else if (dw1->weight > dw2->weight) return -1;
1945 else if (&dw1 < &dw2) return 1; /* compare addresses so we have a stable sort algorithm */
1949 /* Used for limiting the number of influencing bones per vertex when exporting
1950 * skinned meshes. if all_deform_weights is True, limit all deform modifiers
1951 * to max_weights regardless of type, otherwise, only limit the number of influencing bones per vertex*/
1952 static int vgroup_limit_total_subset(Object *ob,
1953 const bool *vgroup_validmap,
1954 const int vgroup_tot,
1955 const int subset_count,
1956 const int max_weights)
1958 MDeformVert *dv, **dvert_array = NULL;
1959 int i, dvert_tot = 0;
1960 const int use_vert_sel = vertex_group_use_vert_sel(ob);
1963 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
1966 int num_to_drop = 0;
1968 for (i = 0; i < dvert_tot; i++) {
1970 MDeformWeight *dw_temp;
1971 int bone_count = 0, non_bone_count = 0;
1974 /* in case its not selected */
1975 if (!(dv = dvert_array[i])) {
1979 num_to_drop = subset_count - max_weights;
1981 /* first check if we even need to test further */
1982 if (num_to_drop > 0) {
1983 /* re-pack dw array so that non-bone weights are first, bone-weighted verts at end
1984 * sort the tail, then copy only the truncated array back to dv->dw */
1985 dw_temp = MEM_mallocN(sizeof(MDeformWeight) * dv->totweight, __func__);
1986 bone_count = 0; non_bone_count = 0;
1987 for (j = 0; j < dv->totweight; j++) {
1988 if (LIKELY(dv->dw[j].def_nr < vgroup_tot) &&
1989 vgroup_validmap[dv->dw[j].def_nr])
1991 dw_temp[dv->totweight - 1 - bone_count] = dv->dw[j];
1995 dw_temp[non_bone_count] = dv->dw[j];
1996 non_bone_count += 1;
1999 BLI_assert(bone_count + non_bone_count == dv->totweight);
2000 num_to_drop = bone_count - max_weights;
2001 if (num_to_drop > 0) {
2002 qsort(&dw_temp[non_bone_count], bone_count, sizeof(MDeformWeight), inv_cmp_mdef_vert_weights);
2003 dv->totweight -= num_to_drop;
2004 /* Do we want to clean/normalize here? */
2006 dv->dw = MEM_reallocN(dw_temp, sizeof(MDeformWeight) * dv->totweight);
2007 remove_tot += num_to_drop;
2015 MEM_freeN(dvert_array);
2023 static void vgroup_clean_subset(Object *ob, const bool *vgroup_validmap, const int vgroup_tot, const int UNUSED(subset_count),
2024 const float epsilon, const bool keep_single)
2026 MDeformVert **dvert_array = NULL;
2028 const bool use_vert_sel = vertex_group_use_vert_sel(ob);
2029 const bool use_mirror = (ob->type == OB_MESH) ? (((Mesh *)ob->data)->editflag & ME_EDIT_MIRROR_X) != 0 : false;
2031 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
2034 if (use_mirror && use_vert_sel) {
2035 /* correct behavior in this case isn't well defined
2036 * for now assume both sides are mirrored correctly,
2037 * so cleaning one side also cleans the other */
2038 ED_vgroup_parray_mirror_assign(ob, dvert_array, dvert_tot);
2041 ED_vgroup_parray_remove_zero(dvert_array, dvert_tot,
2042 vgroup_validmap, vgroup_tot,
2043 epsilon, keep_single);
2045 MEM_freeN(dvert_array);
2049 static void vgroup_quantize_subset(Object *ob, const bool *vgroup_validmap, const int vgroup_tot, const int UNUSED(subset_count),
2052 MDeformVert **dvert_array = NULL;
2054 const bool use_vert_sel = vertex_group_use_vert_sel(ob);
2055 const bool use_mirror = (ob->type == OB_MESH) ? (((Mesh *)ob->data)->editflag & ME_EDIT_MIRROR_X) != 0 : false;
2056 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
2059 const float steps_fl = steps;
2063 if (use_mirror && use_vert_sel) {
2064 ED_vgroup_parray_mirror_assign(ob, dvert_array, dvert_tot);
2067 for (i = 0; i < dvert_tot; i++) {
2071 /* in case its not selected */
2072 if (!(dv = dvert_array[i])) {
2076 for (j = 0, dw = dv->dw; j < dv->totweight; j++, dw++) {
2077 if ((dw->def_nr < vgroup_tot) && vgroup_validmap[dw->def_nr]) {
2078 dw->weight = floorf((dw->weight * steps_fl) + 0.5f) / steps_fl;
2079 CLAMP(dw->weight, 0.0f, 1.0f);
2084 MEM_freeN(dvert_array);
2088 static void dvert_mirror_op(MDeformVert *dvert, MDeformVert *dvert_mirr,
2089 const char sel, const char sel_mirr,
2090 const int *flip_map, const int flip_map_len,
2091 const bool mirror_weights, const bool flip_vgroups, const bool all_vgroups,
2092 const int act_vgroup)
2094 BLI_assert(sel || sel_mirr);
2096 if (sel_mirr && sel) {
2098 if (mirror_weights) {
2100 SWAP(MDeformVert, *dvert, *dvert_mirr);
2103 MDeformWeight *dw = defvert_find_index(dvert, act_vgroup);
2104 MDeformWeight *dw_mirr = defvert_find_index(dvert_mirr, act_vgroup);
2106 if (dw && dw_mirr) {
2107 SWAP(float, dw->weight, dw_mirr->weight);
2110 dw_mirr = defvert_verify_index(dvert_mirr, act_vgroup);
2111 dw_mirr->weight = dw->weight;
2112 defvert_remove_group(dvert, dw);
2115 dw = defvert_verify_index(dvert, act_vgroup);
2116 dw->weight = dw_mirr->weight;
2117 defvert_remove_group(dvert_mirr, dw_mirr);
2123 defvert_flip(dvert, flip_map, flip_map_len);
2124 defvert_flip(dvert_mirr, flip_map, flip_map_len);
2128 /* dvert should always be the target, only swaps pointer */
2130 SWAP(MDeformVert *, dvert, dvert_mirr);
2133 if (mirror_weights) {
2135 defvert_copy(dvert, dvert_mirr);
2138 defvert_copy_index(dvert, act_vgroup, dvert_mirr, act_vgroup);
2142 /* flip map already modified for 'all_vgroups' */
2144 defvert_flip(dvert, flip_map, flip_map_len);
2149 /* TODO, vgroup locking */
2150 /* TODO, face masking */
2151 void ED_vgroup_mirror(Object *ob,
2152 const bool mirror_weights, const bool flip_vgroups,
2153 const bool all_vgroups, const bool use_topology,
2154 int *r_totmirr, int *r_totfail)
2157 #define VGROUP_MIRR_OP \
2158 dvert_mirror_op(dvert, dvert_mirr, \
2160 flip_map, flip_map_len, \
2161 mirror_weights, flip_vgroups, \
2162 all_vgroups, def_nr \
2165 BMVert *eve, *eve_mirr;
2166 MDeformVert *dvert, *dvert_mirr;
2168 int *flip_map = NULL, flip_map_len;
2169 const int def_nr = ob->actdef - 1;
2170 int totmirr = 0, totfail = 0;
2172 *r_totmirr = *r_totfail = 0;
2174 if ((mirror_weights == false && flip_vgroups == false) ||
2175 (BLI_findlink(&ob->defbase, def_nr) == NULL))
2181 flip_map = all_vgroups ?
2182 defgroup_flip_map(ob, &flip_map_len, false) :
2183 defgroup_flip_map_single(ob, &flip_map_len, false, def_nr);
2185 BLI_assert(flip_map != NULL);
2187 if (flip_map == NULL) {
2188 /* something went wrong!, possibly no groups */
2197 /* only the active group */
2198 if (ob->type == OB_MESH) {
2199 Mesh *me = ob->data;
2200 BMEditMesh *em = me->edit_btmesh;
2203 const int cd_dvert_offset = CustomData_get_offset(&em->bm->vdata, CD_MDEFORMVERT);
2206 if (cd_dvert_offset == -1) {
2210 EDBM_verts_mirror_cache_begin(em, 0, true, false, use_topology);
2212 BM_mesh_elem_hflag_disable_all(em->bm, BM_VERT, BM_ELEM_TAG, false);
2214 /* Go through the list of editverts and assign them */
2215 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
2216 if (!BM_elem_flag_test(eve, BM_ELEM_TAG)) {
2217 if ((eve_mirr = EDBM_verts_mirror_get(em, eve))) {
2218 if (eve_mirr != eve) {
2219 if (!BM_elem_flag_test(eve_mirr, BM_ELEM_TAG)) {
2220 sel = BM_elem_flag_test(eve, BM_ELEM_SELECT);
2221 sel_mirr = BM_elem_flag_test(eve_mirr, BM_ELEM_SELECT);
2223 if ((sel || sel_mirr) && (eve != eve_mirr)) {
2224 dvert = BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset);
2225 dvert_mirr = BM_ELEM_CD_GET_VOID_P(eve_mirr, cd_dvert_offset);
2231 /* don't use these again */
2232 BM_elem_flag_enable(eve, BM_ELEM_TAG);
2233 BM_elem_flag_enable(eve_mirr, BM_ELEM_TAG);
2242 EDBM_verts_mirror_cache_end(em);
2245 /* object mode / weight paint */
2246 MVert *mv, *mv_mirr;
2247 int vidx, vidx_mirr;
2248 const bool use_vert_sel = (me->editflag & ME_EDIT_PAINT_VERT_SEL) != 0;
2250 if (me->dvert == NULL) {
2254 if (!use_vert_sel) {
2255 sel = sel_mirr = true;
2258 /* tag verts we have used */
2259 for (vidx = 0, mv = me->mvert; vidx < me->totvert; vidx++, mv++) {
2260 mv->flag &= ~ME_VERT_TMP_TAG;
2263 for (vidx = 0, mv = me->mvert; vidx < me->totvert; vidx++, mv++) {
2264 if ((mv->flag & ME_VERT_TMP_TAG) == 0) {
2265 if ((vidx_mirr = mesh_get_x_mirror_vert(ob, NULL, vidx, use_topology)) != -1) {
2266 if (vidx != vidx_mirr) {
2267 mv_mirr = &me->mvert[vidx_mirr];
2268 if ((mv_mirr->flag & ME_VERT_TMP_TAG) == 0) {
2271 sel = mv->flag & SELECT;
2272 sel_mirr = mv_mirr->flag & SELECT;
2275 if (sel || sel_mirr) {
2276 dvert = &me->dvert[vidx];
2277 dvert_mirr = &me->dvert[vidx_mirr];
2283 mv->flag |= ME_VERT_TMP_TAG;
2284 mv_mirr->flag |= ME_VERT_TMP_TAG;
2295 else if (ob->type == OB_LATTICE) {
2296 Lattice *lt = vgroup_edit_lattice(ob);
2300 /* half but found up odd value */
2302 if (lt->pntsu == 1 || lt->dvert == NULL) {
2306 /* unlike editmesh we know that by only looping over the first half of
2307 * the 'u' indices it will cover all points except the middle which is
2308 * ok in this case */
2309 pntsu_half = lt->pntsu / 2;
2311 for (w = 0; w < lt->pntsw; w++) {
2312 for (v = 0; v < lt->pntsv; v++) {
2313 for (u = 0; u < pntsu_half; u++) {
2314 int u_inv = (lt->pntsu - 1) - u;
2316 BPoint *bp, *bp_mirr;
2318 i1 = BKE_lattice_index_from_uvw(lt, u, v, w);
2319 i2 = BKE_lattice_index_from_uvw(lt, u_inv, v, w);
2322 bp_mirr = <->def[i2];
2324 sel = bp->f1 & SELECT;
2325 sel_mirr = bp_mirr->f1 & SELECT;
2327 if (sel || sel_mirr) {
2328 dvert = <->dvert[i1];
2329 dvert_mirr = <->dvert[i2];
2340 /* disabled, confusing when you have an active pose bone */
2342 /* flip active group index */
2343 if (flip_vgroups && flip_map[def_nr] >= 0)
2344 ob->actdef = flip_map[def_nr] + 1;
2348 *r_totmirr = totmirr;
2349 *r_totfail = totfail;
2351 if (flip_map) MEM_freeN(flip_map);
2353 #undef VGROUP_MIRR_OP
2357 static void vgroup_delete_active(Object *ob)
2359 bDeformGroup *dg = BLI_findlink(&ob->defbase, ob->actdef - 1);
2363 BKE_object_defgroup_remove(ob, dg);
2366 /* only in editmode */
2367 static void vgroup_assign_verts(Object *ob, const float weight)
2369 const int def_nr = ob->actdef - 1;
2371 if (!BLI_findlink(&ob->defbase, def_nr))
2374 if (ob->type == OB_MESH) {
2375 Mesh *me = ob->data;
2377 if (me->edit_btmesh) {
2378 BMEditMesh *em = me->edit_btmesh;
2379 int cd_dvert_offset;
2384 if (!CustomData_has_layer(&em->bm->vdata, CD_MDEFORMVERT))
2385 BM_data_layer_add(em->bm, &em->bm->vdata, CD_MDEFORMVERT);
2387 cd_dvert_offset = CustomData_get_offset(&em->bm->vdata, CD_MDEFORMVERT);
2389 /* Go through the list of editverts and assign them */
2390 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
2391 if (BM_elem_flag_test(eve, BM_ELEM_SELECT)) {
2394 dv = BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset); /* can be NULL */
2395 dw = defvert_verify_index(dv, def_nr);
2397 dw->weight = weight;
2408 BKE_object_defgroup_data_create(&me->id);
2414 for (i = 0; i < me->totvert; i++, mv++, dv++) {
2415 if (mv->flag & SELECT) {
2417 dw = defvert_verify_index(dv, def_nr);
2419 dw->weight = weight;
2425 else if (ob->type == OB_LATTICE) {
2426 Lattice *lt = vgroup_edit_lattice(ob);
2431 if (lt->dvert == NULL)
2432 BKE_object_defgroup_data_create(<->id);
2436 tot = lt->pntsu * lt->pntsv * lt->pntsw;
2437 for (a = 0, bp = lt->def; a < tot; a++, bp++, dv++) {
2438 if (bp->f1 & SELECT) {
2441 dw = defvert_verify_index(dv, def_nr);
2443 dw->weight = weight;
2450 /********************** vertex group operators *********************/
2452 static int vertex_group_poll(bContext *C)
2454 Object *ob = ED_object_context(C);
2455 ID *data = (ob) ? ob->data : NULL;
2457 return (ob && !ID_IS_LINKED_DATABLOCK(ob) &&
2458 data && !ID_IS_LINKED_DATABLOCK(data) &&
2459 OB_TYPE_SUPPORT_VGROUP(ob->type) &&
2463 static int vertex_group_supported_poll(bContext *C)
2465 Object *ob = ED_object_context(C);
2466 ID *data = (ob) ? ob->data : NULL;
2467 return (ob && !ID_IS_LINKED_DATABLOCK(ob) && OB_TYPE_SUPPORT_VGROUP(ob->type) &&
2468 data && !ID_IS_LINKED_DATABLOCK(data));
2471 static int vertex_group_mesh_poll(bContext *C)
2473 Object *ob = ED_object_context(C);
2474 ID *data = (ob) ? ob->data : NULL;
2476 return (ob && !ID_IS_LINKED_DATABLOCK(ob) &&
2477 data && !ID_IS_LINKED_DATABLOCK(data) &&
2478 ob->type == OB_MESH &&
2482 static int UNUSED_FUNCTION(vertex_group_mesh_supported_poll)(bContext *C)
2484 Object *ob = ED_object_context(C);
2485 ID *data = (ob) ? ob->data : NULL;
2486 return (ob && !ID_IS_LINKED_DATABLOCK(ob) && ob->type == OB_MESH && data && !ID_IS_LINKED_DATABLOCK(data));
2490 static int UNUSED_FUNCTION(vertex_group_poll_edit) (bContext *C)
2492 Object *ob = ED_object_context(C);
2493 ID *data = (ob) ? ob->data : NULL;
2495 if (!(ob && !ID_IS_LINKED_DATABLOCK(ob) && data && !ID_IS_LINKED_DATABLOCK(data)))
2498 return BKE_object_is_in_editmode_vgroup(ob);
2501 /* editmode _or_ weight paint vertex sel */
2502 static int vertex_group_vert_select_poll_ex(bContext *C, const short ob_type_flag)
2504 Object *ob = ED_object_context(C);
2505 ID *data = (ob) ? ob->data : NULL;
2507 if (!(ob && !ID_IS_LINKED_DATABLOCK(ob) && data && !ID_IS_LINKED_DATABLOCK(data)))
2510 if (ob_type_flag && (((1 << ob->type) & ob_type_flag)) == 0) {
2514 if (BKE_object_is_in_editmode_vgroup(ob)) {
2517 else if (ob->mode & OB_MODE_WEIGHT_PAINT) {
2518 if (BKE_object_is_in_wpaint_select_vert(ob)) {
2522 CTX_wm_operator_poll_msg_set(C, "Vertex select needs to be enabled in weight paint mode");
2531 static int vertex_group_vert_select_poll(bContext *C)
2533 return vertex_group_vert_select_poll_ex(C, 0);
2536 static int vertex_group_mesh_vert_select_poll(bContext *C)
2538 return vertex_group_vert_select_poll_ex(C, (1 << OB_MESH));
2541 /* editmode _or_ weight paint vertex sel and active group unlocked */
2542 static int vertex_group_vert_select_unlocked_poll(bContext *C)
2544 Object *ob = ED_object_context(C);
2545 ID *data = (ob) ? ob->data : NULL;
2547 if (!(ob && !ID_IS_LINKED_DATABLOCK(ob) && data && !ID_IS_LINKED_DATABLOCK(data)))
2550 if (!(BKE_object_is_in_editmode_vgroup(ob) ||
2551 BKE_object_is_in_wpaint_select_vert(ob)))
2556 if (ob->actdef != 0) {
2557 bDeformGroup *dg = BLI_findlink(&ob->defbase, ob->actdef - 1);
2559 return !(dg->flag & DG_LOCK_WEIGHT);
2565 static int vertex_group_vert_select_mesh_poll(bContext *C)
2567 Object *ob = ED_object_context(C);
2568 ID *data = (ob) ? ob->data : NULL;
2570 if (!(ob && !ID_IS_LINKED_DATABLOCK(ob) && data && !ID_IS_LINKED_DATABLOCK(data)))
2573 /* only difference to #vertex_group_vert_select_poll */
2574 if (ob->type != OB_MESH)
2577 return (BKE_object_is_in_editmode_vgroup(ob) ||
2578 BKE_object_is_in_wpaint_select_vert(ob));
2581 static int vertex_group_add_exec(bContext *C, wmOperator *UNUSED(op))
2583 Object *ob = ED_object_context(C);
2585 BKE_object_defgroup_add(ob);
2586 DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
2587 WM_event_add_notifier(C, NC_GEOM | ND_VERTEX_GROUP, ob->data);
2588 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2590 return OPERATOR_FINISHED;
2593 void OBJECT_OT_vertex_group_add(wmOperatorType *ot)
2596 ot->name = "Add Vertex Group";
2597 ot->idname = "OBJECT_OT_vertex_group_add";
2598 ot->description = "Add a new vertex group to the active object";
2601 ot->poll = vertex_group_supported_poll;
2602 ot->exec = vertex_group_add_exec;
2605 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2608 static int vertex_group_remove_exec(bContext *C, wmOperator *op)
2610 Object *ob = ED_object_context(C);
2612 if (RNA_boolean_get(op->ptr, "all"))
2613 BKE_object_defgroup_remove_all(ob);
2614 else if (RNA_boolean_get(op->ptr, "all_unlocked"))
2615 BKE_object_defgroup_remove_all_ex(ob, true);
2617 vgroup_delete_active(ob);
2619 DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
2620 WM_event_add_notifier(C, NC_GEOM | ND_VERTEX_GROUP, ob->data);
2621 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2623 return OPERATOR_FINISHED;
2626 void OBJECT_OT_vertex_group_remove(wmOperatorType *ot)
2629 ot->name = "Remove Vertex Group";
2630 ot->idname = "OBJECT_OT_vertex_group_remove";
2631 ot->description = "Delete the active or all vertex groups from the active object";
2634 ot->poll = vertex_group_poll;
2635 ot->exec = vertex_group_remove_exec;
2638 /* redo operator will fail in this case because vertex groups aren't stored
2639 * in local edit mode stack and toggling "all" property will lead to
2640 * all groups deleted without way to restore them (see [#29527], sergey) */
2641 ot->flag = /*OPTYPE_REGISTER|*/ OPTYPE_UNDO;
2644 RNA_def_boolean(ot->srna, "all", 0, "All", "Remove all vertex groups");
2645 RNA_def_boolean(ot->srna, "all_unlocked", 0, "All Unlocked", "Remove all unlocked vertex groups");
2648 static int vertex_group_assign_exec(bContext *C, wmOperator *UNUSED(op))
2650 ToolSettings *ts = CTX_data_tool_settings(C);
2651 Object *ob = ED_object_context(C);
2653 vgroup_assign_verts(ob, ts->vgroup_weight);
2654 DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
2655 WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
2657 return OPERATOR_FINISHED;
2660 void OBJECT_OT_vertex_group_assign(wmOperatorType *ot)
2663 ot->name = "Assign to Vertex Group";
2664 ot->idname = "OBJECT_OT_vertex_group_assign";
2665 ot->description = "Assign the selected vertices to the active vertex group";
2668 ot->poll = vertex_group_vert_select_unlocked_poll;
2669 ot->exec = vertex_group_assign_exec;
2672 /* redo operator will fail in this case because vertex group assignment
2673 * isn't stored in local edit mode stack and toggling "new" property will
2674 * lead to creating plenty of new vertex groups (see [#29527], sergey) */
2675 ot->flag = /*OPTYPE_REGISTER|*/ OPTYPE_UNDO;
2678 /* NOTE: just a wrapper around vertex_group_assign_exec(), except we add these to a new group */
2679 static int vertex_group_assign_new_exec(bContext *C, wmOperator *op)
2681 /* create new group... */
2682 Object *ob = ED_object_context(C);
2683 BKE_object_defgroup_add(ob);
2685 /* assign selection to new group */
2686 return vertex_group_assign_exec(C, op);
2689 void OBJECT_OT_vertex_group_assign_new(wmOperatorType *ot)
2692 ot->name = "Assign to New Group";
2693 ot->idname = "OBJECT_OT_vertex_group_assign_new";
2694 ot->description = "Assign the selected vertices to a new vertex group";
2697 ot->poll = vertex_group_vert_select_poll;
2698 ot->exec = vertex_group_assign_new_exec;
2701 /* redo operator will fail in this case because vertex group assignment
2702 * isn't stored in local edit mode stack and toggling "new" property will
2703 * lead to creating plenty of new vertex groups (see [#29527], sergey) */
2704 ot->flag = /*OPTYPE_REGISTER|*/ OPTYPE_UNDO;
2707 static int vertex_group_remove_from_exec(bContext *C, wmOperator *op)
2709 const bool use_all_groups = RNA_boolean_get(op->ptr, "use_all_groups");
2710 const bool use_all_verts = RNA_boolean_get(op->ptr, "use_all_verts");
2712 Object *ob = ED_object_context(C);
2714 if (use_all_groups) {
2715 if (BKE_object_defgroup_clear_all(ob, true) == false) {
2716 return OPERATOR_CANCELLED;
2720 bDeformGroup *dg = BLI_findlink(&ob->defbase, ob->actdef - 1);
2722 if ((dg == NULL) || (BKE_object_defgroup_clear(ob, dg, !use_all_verts) == false)) {
2723 return OPERATOR_CANCELLED;
2727 DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
2728 WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
2730 return OPERATOR_FINISHED;
2733 void OBJECT_OT_vertex_group_remove_from(wmOperatorType *ot)
2737 ot->name = "Remove from Vertex Group";
2738 ot->idname = "OBJECT_OT_vertex_group_remove_from";
2739 ot->description = "Remove the selected vertices from active or all vertex group(s)";
2742 ot->poll = vertex_group_vert_select_unlocked_poll;
2743 ot->exec = vertex_group_remove_from_exec;
2746 /* redo operator will fail in this case because vertex groups assignment
2747 * isn't stored in local edit mode stack and toggling "all" property will lead to
2748 * removing vertices from all groups (see [#29527], sergey) */
2749 ot->flag = /*OPTYPE_REGISTER|*/ OPTYPE_UNDO;
2752 prop = RNA_def_boolean(ot->srna, "use_all_groups", 0, "All Groups", "Remove from all groups");
2753 RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2754 prop = RNA_def_boolean(ot->srna, "use_all_verts", 0, "All Verts", "Clear the active group");
2755 RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2758 static int vertex_group_select_exec(bContext *C, wmOperator *UNUSED(op))
2760 Object *ob = ED_object_context(C);
2762 if (!ob || ID_IS_LINKED_DATABLOCK(ob))
2763 return OPERATOR_CANCELLED;
2765 vgroup_select_verts(ob, 1);
2766 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, ob->data);
2768 return OPERATOR_FINISHED;
2771 void OBJECT_OT_vertex_group_select(wmOperatorType *ot)
2774 ot->name = "Select Vertex Group";
2775 ot->idname = "OBJECT_OT_vertex_group_select";
2776 ot->description = "Select all the vertices assigned to the active vertex group";
2779 ot->poll = vertex_group_vert_select_poll;
2780 ot->exec = vertex_group_select_exec;
2783 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2786 static int vertex_group_deselect_exec(bContext *C, wmOperator *UNUSED(op))
2788 Object *ob = ED_object_context(C);
2790 vgroup_select_verts(ob, 0);
2791 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, ob->data);
2793 return OPERATOR_FINISHED;
2796 void OBJECT_OT_vertex_group_deselect(wmOperatorType *ot)
2799 ot->name = "Deselect Vertex Group";
2800 ot->idname = "OBJECT_OT_vertex_group_deselect";
2801 ot->description = "Deselect all selected vertices assigned to the active vertex group";
2804 ot->poll = vertex_group_vert_select_poll;
2805 ot->exec = vertex_group_deselect_exec;
2808 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2811 static int vertex_group_copy_exec(bContext *C, wmOperator *UNUSED(op))
2813 Object *ob = ED_object_context(C);
2815 vgroup_duplicate(ob);
2816 DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
2817 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2818 WM_event_add_notifier(C, NC_GEOM | ND_VERTEX_GROUP, ob->data);
2820 return OPERATOR_FINISHED;
2823 void OBJECT_OT_vertex_group_copy(wmOperatorType *ot)
2826 ot->name = "Copy Vertex Group";
2827 ot->idname = "OBJECT_OT_vertex_group_copy";
2828 ot->description = "Make a copy of the active vertex group";
2831 ot->poll = vertex_group_poll;
2832 ot->exec = vertex_group_copy_exec;
2835 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2838 static int vertex_group_levels_exec(bContext *C, wmOperator *op)
2840 Object *ob = ED_object_context(C);
2842 float offset = RNA_float_get(op->ptr, "offset");
2843 float gain = RNA_float_get(op->ptr, "gain");
2844 eVGroupSelect subset_type = RNA_enum_get(op->ptr, "group_select_mode");
2846 int subset_count, vgroup_tot;
2848 const bool *vgroup_validmap = BKE_object_defgroup_subset_from_select_type(ob, subset_type, &vgroup_tot, &subset_count);
2849 vgroup_levels_subset(ob, vgroup_validmap, vgroup_tot, subset_count, offset, gain);
2850 MEM_freeN((void *)vgroup_validmap);
2852 DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
2853 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2854 WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
2856 return OPERATOR_FINISHED;
2859 void OBJECT_OT_vertex_group_levels(wmOperatorType *ot)
2862 ot->name = "Vertex Group Levels";
2863 ot->idname = "OBJECT_OT_vertex_group_levels";
2864 ot->description = "Add some offset and multiply with some gain the weights of the active vertex group";
2867 ot->poll = vertex_group_poll;
2868 ot->exec = vertex_group_levels_exec;
2871 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2873 vgroup_operator_subset_select_props(ot, true);
2874 RNA_def_float(ot->srna, "offset", 0.f, -1.0, 1.0, "Offset", "Value to add to weights", -1.0f, 1.f);
2875 RNA_def_float(ot->srna, "gain", 1.f, 0.f, FLT_MAX, "Gain", "Value to multiply weights by", 0.0f, 10.f);
2878 static int vertex_group_normalize_exec(bContext *C, wmOperator *UNUSED(op))
2880 Object *ob = ED_object_context(C);
2883 changed = vgroup_normalize(ob);
2886 DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
2887 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2888 WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
2890 return OPERATOR_FINISHED;
2893 return OPERATOR_CANCELLED;
2897 void OBJECT_OT_vertex_group_normalize(wmOperatorType *ot)
2900 ot->name = "Normalize Vertex Group";
2901 ot->idname = "OBJECT_OT_vertex_group_normalize";
2902 ot->description = "Normalize weights of the active vertex group, so that the highest ones are now 1.0";
2905 ot->poll = vertex_group_poll;
2906 ot->exec = vertex_group_normalize_exec;
2909 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2912 static int vertex_group_normalize_all_exec(bContext *C, wmOperator *op)
2914 Object *ob = ED_object_context(C);
2915 bool lock_active = RNA_boolean_get(op->ptr, "lock_active");
2916 eVGroupSelect subset_type = RNA_enum_get(op->ptr, "group_select_mode");
2918 int subset_count, vgroup_tot;
2919 const bool *vgroup_validmap = BKE_object_defgroup_subset_from_select_type(ob, subset_type, &vgroup_tot, &subset_count);
2921 changed = vgroup_normalize_all(ob, vgroup_validmap, vgroup_tot, subset_count, lock_active, op->reports);
2922 MEM_freeN((void *)vgroup_validmap);
2925 DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
2926 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2927 WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
2929 return OPERATOR_FINISHED;
2932 /* allow to adjust settings */
2933 return OPERATOR_FINISHED;
2937 void OBJECT_OT_vertex_group_normalize_all(wmOperatorType *ot)
2940 ot->name = "Normalize All Vertex Groups";
2941 ot->idname = "OBJECT_OT_vertex_group_normalize_all";
2942 ot->description = "Normalize all weights of all vertex groups, "
2943 "so that for each vertex, the sum of all weights is 1.0";
2946 ot->poll = vertex_group_poll;
2947 ot->exec = vertex_group_normalize_all_exec;
2950 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2952 vgroup_operator_subset_select_props(ot, false);
2953 RNA_def_boolean(ot->srna, "lock_active", true, "Lock Active",
2954 "Keep the values of the active group while normalizing others");
2957 static int vertex_group_fix_exec(bContext *C, wmOperator *op)
2959 Object *ob = CTX_data_active_object(C);
2960 Scene *scene = CTX_data_scene(C);
2962 float distToBe = RNA_float_get(op->ptr, "dist");
2963 float strength = RNA_float_get(op->ptr, "strength");
2964 float cp = RNA_float_get(op->ptr, "accuracy");
2965 ModifierData *md = ob->modifiers.first;
2968 if (md->type == eModifierType_Mirror && (md->mode & eModifierMode_Realtime)) {