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;
253 * For use with tools that use ED_vgroup_parray_alloc with \a use_vert_sel == true.
254 * This finds the unselected mirror deform verts and copies the weights to them from the selected.
256 * \note \a dvert_array has mirrored weights filled in, incase cleanup operations are needed on both.
258 void ED_vgroup_parray_mirror_sync(Object *ob,
259 MDeformVert **dvert_array, const int dvert_tot,
260 const bool *vgroup_validmap, const int vgroup_tot)
262 BMEditMesh *em = BKE_editmesh_from_object(ob);
263 MDeformVert **dvert_array_all = NULL;
266 /* get an array of all verts, not only selected */
267 if (ED_vgroup_parray_alloc(ob->data, &dvert_array_all, &dvert_tot_all, false) == false) {
272 BM_mesh_elem_table_ensure(em->bm, BM_VERT);
276 const int *flip_map = defgroup_flip_map(ob, &flip_map_len, true);
278 for (int i_src = 0; i_src < dvert_tot; i_src++) {
279 if (dvert_array[i_src] != NULL) {
280 /* its selected, check if its mirror exists */
281 int i_dst = ED_mesh_mirror_get_vert(ob, i_src);
282 if (i_dst != -1 && dvert_array_all[i_dst] != NULL) {
283 /* we found a match! */
284 const MDeformVert *dv_src = dvert_array[i_src];
285 MDeformVert *dv_dst = dvert_array_all[i_dst];
287 defvert_mirror_subset(dv_dst, dv_src, vgroup_validmap, vgroup_tot, flip_map, flip_map_len);
289 dvert_array[i_dst] = dvert_array_all[i_dst];
294 MEM_freeN((void *)flip_map);
295 MEM_freeN(dvert_array_all);
299 * Fill in the pointers for mirror verts (as if all mirror verts were selected too).
301 * similar to #ED_vgroup_parray_mirror_sync but only fill in mirror points.
303 void ED_vgroup_parray_mirror_assign(Object *ob,
304 MDeformVert **dvert_array, const int dvert_tot)
306 BMEditMesh *em = BKE_editmesh_from_object(ob);
307 MDeformVert **dvert_array_all = NULL;
311 /* get an array of all verts, not only selected */
312 if (ED_vgroup_parray_alloc(ob->data, &dvert_array_all, &dvert_tot_all, false) == false) {
316 BLI_assert(dvert_tot == dvert_tot_all);
318 BM_mesh_elem_table_ensure(em->bm, BM_VERT);
321 for (i = 0; i < dvert_tot; i++) {
322 if (dvert_array[i] == NULL) {
323 /* its unselected, check if its mirror is */
324 int i_sel = ED_mesh_mirror_get_vert(ob, i);
325 if ((i_sel != -1) && (i_sel != i) && (dvert_array[i_sel])) {
326 /* we found a match! */
327 dvert_array[i] = dvert_array_all[i];
332 MEM_freeN(dvert_array_all);
335 void ED_vgroup_parray_remove_zero(MDeformVert **dvert_array, const int dvert_tot,
336 const bool *vgroup_validmap, const int vgroup_tot,
337 const float epsilon, const bool keep_single)
342 for (i = 0; i < dvert_tot; i++) {
345 /* in case its not selected */
346 if (!(dv = dvert_array[i])) {
355 if (keep_single && dv->totweight == 1)
359 if ((dw->def_nr < vgroup_tot) && vgroup_validmap[dw->def_nr]) {
360 if (dw->weight <= epsilon) {
361 defvert_remove_group(dv, dw);
368 /* matching index only */
369 bool ED_vgroup_array_copy(Object *ob, Object *ob_from)
371 MDeformVert **dvert_array_from = NULL, **dvf;
372 MDeformVert **dvert_array = NULL, **dv;
376 int defbase_tot_from = BLI_listbase_count(&ob_from->defbase);
377 int defbase_tot = BLI_listbase_count(&ob->defbase);
378 bool new_vgroup = false;
383 /* in case we copy vgroup between two objects using same data, we only have to care about object side of things. */
384 if (ob->data != ob_from->data) {
385 ED_vgroup_parray_alloc(ob_from->data, &dvert_array_from, &dvert_tot_from, false);
386 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, false);
388 if ((dvert_array == NULL) && (dvert_array_from != NULL) && BKE_object_defgroup_data_create(ob->data)) {
389 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, false);
393 if (dvert_tot == 0 || (dvert_tot != dvert_tot_from) || dvert_array_from == NULL || dvert_array == NULL) {
395 MEM_freeN(dvert_array);
396 if (dvert_array_from)
397 MEM_freeN(dvert_array_from);
399 if (new_vgroup == true) {
400 /* free the newly added vgroup since it wasn't compatible */
401 BKE_object_defgroup_remove_all(ob);
404 /* if true: both are 0 and nothing needs changing, consider this a success */
405 return (dvert_tot == dvert_tot_from);
410 BLI_freelistN(&ob->defbase);
411 BLI_duplicatelist(&ob->defbase, &ob_from->defbase);
412 ob->actdef = ob_from->actdef;
414 if (defbase_tot_from < defbase_tot) {
415 /* correct vgroup indices because the number of vgroups is being reduced. */
416 int *remap = MEM_mallocN(sizeof(int) * (defbase_tot + 1), __func__);
417 for (i = 0; i <= defbase_tot_from; i++) remap[i] = i;
418 for (; i <= defbase_tot; i++) remap[i] = 0; /* can't use these, so disable */
420 BKE_object_defgroup_remap_update_users(ob, remap);
424 if (dvert_array_from != NULL && dvert_array != NULL) {
425 dvf = dvert_array_from;
428 for (i = 0; i < dvert_tot; i++, dvf++, dv++) {
429 MEM_SAFE_FREE((*dv)->dw);
433 (*dv)->dw = MEM_dupallocN((*dv)->dw);
437 MEM_freeN(dvert_array);
438 MEM_freeN(dvert_array_from);
444 void ED_vgroup_parray_to_weight_array(
445 const MDeformVert **dvert_array, const int dvert_tot,
446 float *dvert_weights, const int def_nr)
450 for (i = 0; i < dvert_tot; i++) {
451 const MDeformVert *dv = dvert_array[i];
452 dvert_weights[i] = dv ? defvert_find_weight(dv, def_nr) : 0.0f;
456 void ED_vgroup_parray_from_weight_array(
457 MDeformVert **dvert_array, const int dvert_tot,
458 const float *dvert_weights, const int def_nr, const bool remove_zero)
462 for (i = 0; i < dvert_tot; i++) {
463 MDeformVert *dv = dvert_array[i];
465 if (dvert_weights[i] > 0.0f) {
466 MDeformWeight *dw = defvert_verify_index(dv, def_nr);
467 BLI_assert(IN_RANGE_INCL(dvert_weights[i], 0.0f, 1.0f));
468 dw->weight = dvert_weights[i];
471 MDeformWeight *dw = defvert_find_index(dv, def_nr);
474 defvert_remove_group(dv, dw);
486 /* TODO, cache flip data to speedup calls within a loop. */
487 static void mesh_defvert_mirror_update_internal(Object *ob,
488 MDeformVert *dvert_dst, MDeformVert *dvert_src,
492 /* all vgroups, add groups where neded */
494 int *flip_map = defgroup_flip_map(ob, &flip_map_len, true);
495 defvert_sync_mapped(dvert_dst, dvert_src, flip_map, flip_map_len, true);
500 MDeformWeight *dw = defvert_verify_index(dvert_dst, defgroup_flip_index(ob, def_nr, 1));
502 dw->weight = defvert_find_weight(dvert_src, def_nr);
507 static void ED_mesh_defvert_mirror_update_em(Object *ob, BMVert *eve, int def_nr, int vidx,
508 const int cd_dvert_offset)
511 BMEditMesh *em = me->edit_btmesh;
513 bool use_topology = (me->editflag & ME_EDIT_MIRROR_TOPO) != 0;
515 eve_mirr = editbmesh_get_x_mirror_vert(ob, em, eve, eve->co, vidx, use_topology);
517 if (eve_mirr && eve_mirr != eve) {
518 MDeformVert *dvert_src = BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset);
519 MDeformVert *dvert_dst = BM_ELEM_CD_GET_VOID_P(eve_mirr, cd_dvert_offset);
520 mesh_defvert_mirror_update_internal(ob, dvert_dst, dvert_src, def_nr);
524 static void ED_mesh_defvert_mirror_update_ob(Object *ob, int def_nr, int vidx)
528 bool use_topology = (me->editflag & ME_EDIT_MIRROR_TOPO) != 0;
533 vidx_mirr = mesh_get_x_mirror_vert(ob, NULL, vidx, use_topology);
535 if ((vidx_mirr) >= 0 && (vidx_mirr != vidx)) {
536 MDeformVert *dvert_src = &me->dvert[vidx];
537 MDeformVert *dvert_dst = &me->dvert[vidx_mirr];
538 mesh_defvert_mirror_update_internal(ob, dvert_dst, dvert_src, def_nr);
543 * Use when adjusting the active vertex weight and apply to mirror vertices.
545 void ED_vgroup_vert_active_mirror(Object *ob, int def_nr)
548 BMEditMesh *em = me->edit_btmesh;
549 MDeformVert *dvert_act;
551 if (me->editflag & ME_EDIT_MIRROR_X) {
554 dvert_act = ED_mesh_active_dvert_get_em(ob, &eve_act);
556 const int cd_dvert_offset = CustomData_get_offset(&em->bm->vdata, CD_MDEFORMVERT);
557 ED_mesh_defvert_mirror_update_em(ob, eve_act, def_nr, -1, cd_dvert_offset);
562 dvert_act = ED_mesh_active_dvert_get_ob(ob, &v_act);
564 ED_mesh_defvert_mirror_update_ob(ob, def_nr, v_act);
570 static void vgroup_remove_weight(Object *ob, const int def_nr)
572 MDeformVert *dvert_act;
575 dvert_act = ED_mesh_active_dvert_get_only(ob);
577 dw = defvert_find_index(dvert_act, def_nr);
578 defvert_remove_group(dvert_act, dw);
582 static bool vgroup_normalize_active_vertex(Object *ob, eVGroupSelect subset_type)
585 BMEditMesh *em = me->edit_btmesh;
588 MDeformVert *dvert_act;
589 int subset_count, vgroup_tot;
590 const bool *vgroup_validmap;
594 dvert_act = ED_mesh_active_dvert_get_em(ob, &eve_act);
597 dvert_act = ED_mesh_active_dvert_get_ob(ob, &v_act);
600 if (dvert_act == NULL) {
604 vgroup_validmap = BKE_object_defgroup_subset_from_select_type(ob, subset_type, &vgroup_tot, &subset_count);
605 defvert_normalize_subset(dvert_act, vgroup_validmap, vgroup_tot);
606 MEM_freeN((void *)vgroup_validmap);
608 if (me->editflag & ME_EDIT_MIRROR_X) {
610 const int cd_dvert_offset = CustomData_get_offset(&em->bm->vdata, CD_MDEFORMVERT);
611 ED_mesh_defvert_mirror_update_em(ob, eve_act, -1, -1, cd_dvert_offset);
614 ED_mesh_defvert_mirror_update_ob(ob, -1, v_act);
621 static void vgroup_copy_active_to_sel(Object *ob, eVGroupSelect subset_type)
624 BMEditMesh *em = me->edit_btmesh;
625 MDeformVert *dvert_act;
626 int i, vgroup_tot, subset_count;
627 const bool *vgroup_validmap = BKE_object_defgroup_subset_from_select_type(ob, subset_type, &vgroup_tot, &subset_count);
632 BMVert *eve, *eve_act;
633 const int cd_dvert_offset = CustomData_get_offset(&em->bm->vdata, CD_MDEFORMVERT);
635 dvert_act = ED_mesh_active_dvert_get_em(ob, &eve_act);
637 BM_ITER_MESH_INDEX (eve, &iter, em->bm, BM_VERTS_OF_MESH, i) {
638 if (BM_elem_flag_test(eve, BM_ELEM_SELECT) && eve != eve_act) {
639 MDeformVert *dv = BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset);
640 defvert_copy_subset(dv, dvert_act, vgroup_validmap, vgroup_tot);
641 if (me->editflag & ME_EDIT_MIRROR_X) {
642 ED_mesh_defvert_mirror_update_em(ob, eve, -1, i, cd_dvert_offset);
652 dvert_act = ED_mesh_active_dvert_get_ob(ob, &v_act);
655 for (i = 0; i < me->totvert; i++, dv++) {
656 if ((me->mvert[i].flag & SELECT) && dv != dvert_act) {
657 defvert_copy_subset(dv, dvert_act, vgroup_validmap, vgroup_tot);
658 if (me->editflag & ME_EDIT_MIRROR_X) {
659 ED_mesh_defvert_mirror_update_ob(ob, -1, i);
666 MEM_freeN((void *)vgroup_validmap);
669 /***********************Start weight transfer (WT)*********************************/
671 static EnumPropertyItem WT_vertex_group_select_item[] = {
673 "ACTIVE", 0, "Active Group", "The active Vertex Group"},
674 {WT_VGROUP_BONE_SELECT,
675 "BONE_SELECT", 0, "Selected Pose Bones", "All Vertex Groups assigned to Selection"},
676 {WT_VGROUP_BONE_DEFORM,
677 "BONE_DEFORM", 0, "Deform Pose Bones", "All Vertex Groups assigned to Deform Bones"},
679 "ALL", 0, "All Groups", "All Vertex Groups"},
680 {0, NULL, 0, NULL, NULL}
683 EnumPropertyItem *ED_object_vgroup_selection_itemf_helper(
684 const bContext *C, PointerRNA *UNUSED(ptr),
685 PropertyRNA *UNUSED(prop), bool *r_free, const unsigned int selection_mask)
688 EnumPropertyItem *item = NULL;
692 if (!C) /* needed for docs and i18n tools */
693 return WT_vertex_group_select_item;
695 ob = CTX_data_active_object(C);
696 if (selection_mask & (1 << WT_VGROUP_ACTIVE))
697 RNA_enum_items_add_value(&item, &totitem, WT_vertex_group_select_item, WT_VGROUP_ACTIVE);
699 if (BKE_object_pose_armature_get(ob)) {
700 if (selection_mask & (1 << WT_VGROUP_BONE_SELECT))
701 RNA_enum_items_add_value(&item, &totitem, WT_vertex_group_select_item, WT_VGROUP_BONE_SELECT);
702 if (selection_mask & (1 << WT_VGROUP_BONE_DEFORM))
703 RNA_enum_items_add_value(&item, &totitem, WT_vertex_group_select_item, WT_VGROUP_BONE_DEFORM);
706 if (selection_mask & (1 << WT_VGROUP_ALL))
707 RNA_enum_items_add_value(&item, &totitem, WT_vertex_group_select_item, WT_VGROUP_ALL);
709 RNA_enum_item_end(&item, &totitem);
715 static EnumPropertyItem *rna_vertex_group_with_single_itemf(bContext *C, PointerRNA *ptr,
716 PropertyRNA *prop, bool *r_free)
718 return ED_object_vgroup_selection_itemf_helper(C, ptr, prop, r_free, WT_VGROUP_MASK_ALL);
721 static EnumPropertyItem *rna_vertex_group_select_itemf(bContext *C, PointerRNA *ptr,
722 PropertyRNA *prop, bool *r_free)
724 return ED_object_vgroup_selection_itemf_helper(C, ptr, prop, r_free, WT_VGROUP_MASK_ALL & ~(1 << WT_VGROUP_ACTIVE));
727 static void vgroup_operator_subset_select_props(wmOperatorType *ot, bool use_active)
731 prop = RNA_def_enum(ot->srna,
732 "group_select_mode", DummyRNA_NULL_items,
733 use_active ? WT_VGROUP_ACTIVE : WT_VGROUP_ALL, "Subset",
734 "Define which subset of Groups shall be used");
737 RNA_def_enum_funcs(prop, rna_vertex_group_with_single_itemf);
740 RNA_def_enum_funcs(prop, rna_vertex_group_select_itemf);
742 RNA_def_property_flag(prop, PROP_ENUM_NO_TRANSLATE);
747 /***********************End weight transfer (WT)***********************************/
749 /* for Mesh in Object mode */
750 /* allows editmode for Lattice */
751 static void ED_vgroup_nr_vert_add(Object *ob,
752 const int def_nr, const int vertnum,
753 const float weight, const int assignmode)
755 /* add the vert to the deform group with the
758 MDeformVert *dvert = NULL;
762 BKE_object_defgroup_array_get(ob->data, &dvert, &tot);
767 /* check that vertnum is valid before trying to get the relevant dvert */
768 if ((vertnum < 0) || (vertnum >= tot))
773 MDeformVert *dv = &dvert[vertnum];
776 /* Lets first check to see if this vert is
777 * already in the weight group -- if so
781 dw = defvert_find_index(dv, def_nr);
784 switch (assignmode) {
789 dw->weight += weight;
790 if (dw->weight >= 1.0f)
793 case WEIGHT_SUBTRACT:
794 dw->weight -= weight;
795 /* if the weight is zero or less than
796 * remove the vert from the deform group
798 if (dw->weight <= 0.0f) {
799 defvert_remove_group(dv, dw);
805 /* if the vert wasn't in the deform group then
806 * we must take a different form of action ...
809 switch (assignmode) {
810 case WEIGHT_SUBTRACT:
811 /* if we are subtracting then we don't
812 * need to do anything
818 /* if we are doing an additive assignment, then
819 * we need to create the deform weight
822 /* we checked if the vertex was added before so no need to test again, simply add */
823 defvert_add_index_notest(dv, def_nr, weight);
830 /* called while not in editmode */
831 void ED_vgroup_vert_add(Object *ob, bDeformGroup *dg, int vertnum, float weight, int assignmode)
833 /* add the vert to the deform group with the
834 * specified assign mode
836 const int def_nr = BLI_findindex(&ob->defbase, dg);
838 MDeformVert *dv = NULL;
841 /* get the deform group number, exit if
846 /* if there's no deform verts then create some,
848 if (BKE_object_defgroup_array_get(ob->data, &dv, &tot) && dv == NULL)
849 BKE_object_defgroup_data_create(ob->data);
851 /* call another function to do the work
853 ED_vgroup_nr_vert_add(ob, def_nr, vertnum, weight, assignmode);
857 /* mesh object mode, lattice can be in editmode */
858 void ED_vgroup_vert_remove(Object *ob, bDeformGroup *dg, int vertnum)
860 /* This routine removes the vertex from the specified
864 /* TODO, this is slow in a loop, better pass def_nr directly, but leave for later... - campbell */
865 const int def_nr = BLI_findindex(&ob->defbase, dg);
868 MDeformVert *dvert = NULL;
871 /* get the deform vertices corresponding to the
874 BKE_object_defgroup_array_get(ob->data, &dvert, &tot);
877 MDeformVert *dv = &dvert[vertnum];
880 dw = defvert_find_index(dv, def_nr);
881 defvert_remove_group(dv, dw); /* dw can be NULL */
886 static float get_vert_def_nr(Object *ob, const int def_nr, const int vertnum)
888 MDeformVert *dv = NULL;
890 /* get the deform vertices corresponding to the vertnum */
891 if (ob->type == OB_MESH) {
894 if (me->edit_btmesh) {
895 BMEditMesh *em = me->edit_btmesh;
896 const int cd_dvert_offset = CustomData_get_offset(&em->bm->vdata, CD_MDEFORMVERT);
897 /* warning, this lookup is _not_ fast */
899 if (cd_dvert_offset != -1 && vertnum < em->bm->totvert) {
901 BM_mesh_elem_table_ensure(em->bm, BM_VERT);
902 eve = BM_vert_at_index(em->bm, vertnum);
903 dv = BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset);
911 if (vertnum >= me->totvert) {
914 dv = &me->dvert[vertnum];
918 else if (ob->type == OB_LATTICE) {
919 Lattice *lt = vgroup_edit_lattice(ob);
922 if (vertnum >= lt->pntsu * lt->pntsv * lt->pntsw) {
925 dv = <->dvert[vertnum];
930 MDeformWeight *dw = defvert_find_index(dv, def_nr);
939 float ED_vgroup_vert_weight(Object *ob, bDeformGroup *dg, int vertnum)
941 const int def_nr = BLI_findindex(&ob->defbase, dg);
947 return get_vert_def_nr(ob, def_nr, vertnum);
950 void ED_vgroup_select_by_name(Object *ob, const char *name)
951 { /* note: ob->actdef==0 signals on painting to create a new one, if a bone in posemode is selected */
952 ob->actdef = defgroup_name_index(ob, name) + 1;
955 /********************** Operator Implementations *********************/
957 /* only in editmode */
958 static void vgroup_select_verts(Object *ob, int select)
960 const int def_nr = ob->actdef - 1;
962 if (!BLI_findlink(&ob->defbase, def_nr)) {
966 if (ob->type == OB_MESH) {
969 if (me->edit_btmesh) {
970 BMEditMesh *em = me->edit_btmesh;
971 const int cd_dvert_offset = CustomData_get_offset(&em->bm->vdata, CD_MDEFORMVERT);
973 if (cd_dvert_offset != -1) {
977 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
978 if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN)) {
979 MDeformVert *dv = BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset);
980 if (defvert_find_index(dv, def_nr)) {
981 BM_vert_select_set(em->bm, eve, select);
986 /* this has to be called, because this function operates on vertices only */
987 if (select) EDBM_select_flush(em); /* vertices to edges/faces */
988 else EDBM_deselect_flush(em);
1000 for (i = 0; i < me->totvert; i++, mv++, dv++) {
1001 if (!(mv->flag & ME_HIDE)) {
1002 if (defvert_find_index(dv, def_nr)) {
1003 if (select) mv->flag |= SELECT;
1004 else mv->flag &= ~SELECT;
1009 paintvert_flush_flags(ob);
1013 else if (ob->type == OB_LATTICE) {
1014 Lattice *lt = vgroup_edit_lattice(ob);
1018 BPoint *bp, *actbp = BKE_lattice_active_point_get(lt);
1023 tot = lt->pntsu * lt->pntsv * lt->pntsw;
1024 for (a = 0, bp = lt->def; a < tot; a++, bp++, dv++) {
1025 if (defvert_find_index(dv, def_nr)) {
1026 if (select) bp->f1 |= SELECT;
1029 if (actbp && bp == actbp) lt->actbp = LT_ACTBP_NONE;
1037 static void vgroup_duplicate(Object *ob)
1039 bDeformGroup *dg, *cdg;
1040 char name[sizeof(dg->name)];
1041 MDeformWeight *dw_org, *dw_cpy;
1042 MDeformVert **dvert_array = NULL;
1043 int i, idg, icdg, dvert_tot = 0;
1045 dg = BLI_findlink(&ob->defbase, (ob->actdef - 1));
1049 if (!strstr(dg->name, "_copy")) {
1050 BLI_snprintf(name, sizeof(name), "%s_copy", dg->name);
1053 BLI_strncpy(name, dg->name, sizeof(name));
1056 cdg = defgroup_duplicate(dg);
1057 BLI_strncpy(cdg->name, name, sizeof(cdg->name));
1058 defgroup_unique_name(cdg, ob);
1060 BLI_addtail(&ob->defbase, cdg);
1062 idg = (ob->actdef - 1);
1063 ob->actdef = BLI_listbase_count(&ob->defbase);
1064 icdg = (ob->actdef - 1);
1066 /* TODO, we might want to allow only copy selected verts here? - campbell */
1067 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, false);
1070 for (i = 0; i < dvert_tot; i++) {
1071 MDeformVert *dv = dvert_array[i];
1072 dw_org = defvert_find_index(dv, idg);
1074 /* defvert_verify_index re-allocs org so need to store the weight first */
1075 const float weight = dw_org->weight;
1076 dw_cpy = defvert_verify_index(dv, icdg);
1077 dw_cpy->weight = weight;
1081 MEM_freeN(dvert_array);
1085 static bool vgroup_normalize(Object *ob)
1088 MDeformVert *dv, **dvert_array = NULL;
1089 int i, dvert_tot = 0;
1090 const int def_nr = ob->actdef - 1;
1092 const int use_vert_sel = vertex_group_use_vert_sel(ob);
1094 if (!BLI_findlink(&ob->defbase, def_nr)) {
1098 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
1101 float weight_max = 0.0f;
1103 for (i = 0; i < dvert_tot; i++) {
1105 /* in case its not selected */
1106 if (!(dv = dvert_array[i])) {
1110 dw = defvert_find_index(dv, def_nr);
1112 weight_max = max_ff(dw->weight, weight_max);
1116 if (weight_max > 0.0f) {
1117 for (i = 0; i < dvert_tot; i++) {
1119 /* in case its not selected */
1120 if (!(dv = dvert_array[i])) {
1124 dw = defvert_find_index(dv, def_nr);
1126 dw->weight /= weight_max;
1128 /* in case of division errors with very low weights */
1129 CLAMP(dw->weight, 0.0f, 1.0f);
1134 MEM_freeN(dvert_array);
1142 /* This finds all of the vertices face-connected to vert by an edge and returns a
1143 * MEM_allocated array of indices of size count.
1144 * count is an int passed by reference so it can be assigned the value of the length here. */
1145 static int *getSurroundingVerts(Mesh *me, int vert, int *count)
1147 MPoly *mp = me->mpoly;
1148 int i = me->totpoly;
1149 /* Instead of looping twice on all polys and loops, and use a temp array, let's rather
1150 * use a BLI_array, with a reasonable starting/reserved size (typically, there are not
1151 * many vertices face-linked to another one, even 8 might be too high...). */
1153 BLI_array_declare(verts);
1155 BLI_array_reserve(verts, 8);
1157 int j = mp->totloop;
1158 int first_l = mp->totloop - 1;
1159 MLoop *ml = &me->mloop[mp->loopstart];
1161 /* XXX This assume a vert can only be once in a poly, even though
1162 * it seems logical to me, not totally sure of that. */
1163 if (ml->v == vert) {
1166 /* We are on the first corner. */
1171 /* We are on the last corner. */
1173 b = me->mloop[mp->loopstart].v;
1180 /* Append a and b verts to array, if not yet present. */
1181 k = BLI_array_count(verts);
1182 /* XXX Maybe a == b is enough? */
1183 while (k-- && !(a == b && a == -1)) {
1186 else if (verts[k] == b)
1190 BLI_array_append(verts, a);
1192 BLI_array_append(verts, b);
1194 /* Vert found in this poly, we can go to next one! */
1202 /* Do not free the array! */
1203 *count = BLI_array_count(verts);
1207 /* get a single point in space by averaging a point cloud (vectors of size 3)
1208 * coord is the place the average is stored, points is the point cloud, count is the number of points in the cloud
1210 static void getSingleCoordinate(MVert *points, int count, float coord[3])
1214 for (i = 0; i < count; i++) {
1215 add_v3_v3(coord, points[i].co);
1217 mul_v3_fl(coord, 1.0f / count);
1220 /* given a plane and a start and end position,
1221 * compute the amount of vertical distance relative to the plane and store it in dists,
1222 * then get the horizontal and vertical change and store them in changes
1224 static void getVerticalAndHorizontalChange(const float norm[3], float d, const float coord[3],
1225 const float start[3], float distToStart,
1226 float *end, float (*changes)[2], float *dists, int index)
1228 /* A = Q - ((Q - P).N)N
1229 * D = (a * x0 + b * y0 +c * z0 + d) */
1230 float projA[3], projB[3];
1233 plane_from_point_normal_v3(plane, coord, norm);
1235 closest_to_plane_normalized_v3(projA, plane, start);
1236 closest_to_plane_normalized_v3(projB, plane, end);
1237 /* (vertical and horizontal refer to the plane's y and xz respectively)
1238 * vertical distance */
1239 dists[index] = dot_v3v3(norm, end) + d;
1240 /* vertical change */
1241 changes[index][0] = dists[index] - distToStart;
1242 //printf("vc %f %f\n", distance(end, projB, 3) - distance(start, projA, 3), changes[index][0]);
1243 /* horizontal change */
1244 changes[index][1] = len_v3v3(projA, projB);
1247 /* I need the derived mesh to be forgotten so the positions are recalculated
1248 * with weight changes (see dm_deform_recalc) */
1249 static void dm_deform_clear(DerivedMesh *dm, Object *ob)
1251 if (ob->derivedDeform && (ob->derivedDeform) == dm) {
1252 ob->derivedDeform->needsFree = 1;
1253 ob->derivedDeform->release(ob->derivedDeform);
1254 ob->derivedDeform = NULL;
1262 /* recalculate the deformation */
1263 static DerivedMesh *dm_deform_recalc(EvaluationContext *eval_ctx, Scene *scene, Object *ob)
1265 return mesh_get_derived_deform(eval_ctx, scene, ob, CD_MASK_BAREMESH);
1268 /* by changing nonzero weights, try to move a vertex in me->mverts with index 'index' to
1269 * distToBe distance away from the provided plane strength can change distToBe so that it moves
1270 * towards distToBe by that percentage cp changes how much the weights are adjusted
1271 * to check the distance
1273 * index is the index of the vertex being moved
1274 * norm and d are the plane's properties for the equation: ax + by + cz + d = 0
1275 * coord is a point on the plane
1277 static void moveCloserToDistanceFromPlane(EvaluationContext *eval_ctx, Scene *scene, Object *ob, Mesh *me, int index, float norm[3],
1278 float coord[3], float d, float distToBe, float strength, float cp)
1283 MDeformVert *dvert = me->dvert + index;
1284 int totweight = dvert->totweight;
1286 float oldPos[3] = {0};
1287 float vc, hc, dist = 0.0f;
1289 float (*changes)[2] = MEM_mallocN(sizeof(float *) * totweight * 2, "vertHorzChange");
1290 float *dists = MEM_mallocN(sizeof(float) * totweight, "distance");
1292 /* track if up or down moved it closer for each bone */
1293 int *upDown = MEM_callocN(sizeof(int) * totweight, "upDownTracker");
1295 int *dwIndices = MEM_callocN(sizeof(int) * totweight, "dwIndexTracker");
1301 float originalDistToBe = distToBe;
1304 dm = dm_deform_recalc(eval_ctx, scene, ob);
1305 dm->getVert(dm, index, &m);
1306 copy_v3_v3(oldPos, m.co);
1307 distToStart = dot_v3v3(norm, oldPos) + d;
1309 if (distToBe == originalDistToBe) {
1310 distToBe += distToStart - distToStart * strength;
1312 for (i = 0; i < totweight; i++) {
1314 dw = (dvert->dw + i);
1319 dists[i] = distToStart;
1322 for (k = 0; k < 2; k++) {
1324 dm_deform_clear(dm, ob); dm = NULL;
1328 dw->weight *= 1 + cp;
1331 dw->weight /= 1 + cp;
1333 if (dw->weight == oldw) {
1336 dists[i] = distToStart;
1339 if (dw->weight > 1) {
1342 dm = dm_deform_recalc(eval_ctx, scene, ob);
1343 dm->getVert(dm, index, &m);
1344 getVerticalAndHorizontalChange(norm, d, coord, oldPos, distToStart, m.co, changes, dists, i);
1352 if (fabsf(dist - distToBe) < fabsf(dists[i] - distToBe)) {
1361 if (fabsf(dists[i] - distToBe) > fabsf(distToStart - distToBe)) {
1364 dists[i] = distToStart;
1369 /* sort the changes by the vertical change */
1370 for (k = 0; k < totweight; k++) {
1374 for (i = k + 1; i < totweight; i++) {
1377 if (fabsf(dist) > fabsf(dists[i])) {
1382 if (bestIndex != k) {
1384 upDown[k] = upDown[bestIndex];
1385 upDown[bestIndex] = ti;
1388 dwIndices[k] = dwIndices[bestIndex];
1389 dwIndices[bestIndex] = ti;
1392 changes[k][0] = changes[bestIndex][0];
1393 changes[bestIndex][0] = tf;
1396 changes[k][1] = changes[bestIndex][1];
1397 changes[bestIndex][1] = tf;
1400 dists[k] = dists[bestIndex];
1401 dists[bestIndex] = tf;
1405 /* find the best change with an acceptable horizontal change */
1406 for (i = 0; i < totweight; i++) {
1407 if (fabsf(changes[i][0]) > fabsf(changes[i][1] * 2.0f)) {
1412 if (bestIndex != -1) {
1414 /* it is a good place to stop if it tries to move the opposite direction
1415 * (relative to the plane) of last time */
1416 if (lastIndex != -1) {
1417 if (wasUp != upDown[bestIndex]) {
1421 lastIndex = bestIndex;
1422 wasUp = upDown[bestIndex];
1423 dw = (dvert->dw + dwIndices[bestIndex]);
1425 if (upDown[bestIndex]) {
1426 dw->weight *= 1 + cp;
1429 dw->weight /= 1 + cp;
1431 if (dw->weight > 1) {
1434 if (oldw == dw->weight) {
1438 dm_deform_clear(dm, ob); dm = NULL;
1441 } while (wasChange && ((distToStart - distToBe) / fabsf(distToStart - distToBe) ==
1442 (dists[bestIndex] - distToBe) / fabsf(dists[bestIndex] - distToBe)));
1447 MEM_freeN(dwIndices);
1450 /* this is used to try to smooth a surface by only adjusting the nonzero weights of a vertex
1451 * but it could be used to raise or lower an existing 'bump.' */
1452 static void vgroup_fix(const bContext *C, Scene *scene, Object *ob, float distToBe, float strength, float cp)
1454 EvaluationContext eval_ctx;
1457 CTX_data_eval_ctx(C, &eval_ctx);
1459 Mesh *me = ob->data;
1460 MVert *mvert = me->mvert;
1462 if (!(me->editflag & ME_EDIT_PAINT_VERT_SEL))
1464 for (i = 0; i < me->totvert && mvert; i++, mvert++) {
1465 if (mvert->flag & SELECT) {
1467 if ((verts = getSurroundingVerts(me, i, &count))) {
1469 MVert *p = MEM_callocN(sizeof(MVert) * (count), "deformedPoints");
1472 DerivedMesh *dm = mesh_get_derived_deform(&eval_ctx, scene, ob, CD_MASK_BAREMESH);
1475 dm->getVert(dm, verts[k], &m);
1480 float d /*, dist */ /* UNUSED */, mag;
1483 getSingleCoordinate(p, count, coord);
1484 dm->getVert(dm, i, &m);
1485 sub_v3_v3v3(norm, m.co, coord);
1486 mag = normalize_v3(norm);
1487 if (mag) { /* zeros fix */
1488 d = -dot_v3v3(norm, coord);
1489 /* dist = (dot_v3v3(norm, m.co) + d); */ /* UNUSED */
1490 moveCloserToDistanceFromPlane(&eval_ctx, scene, ob, me, i, norm, coord, d, distToBe, strength, cp);
1501 static void vgroup_levels_subset(Object *ob, const bool *vgroup_validmap, const int vgroup_tot,
1502 const int UNUSED(subset_count),
1503 const float offset, const float gain)
1506 MDeformVert *dv, **dvert_array = NULL;
1507 int i, dvert_tot = 0;
1509 const bool use_vert_sel = vertex_group_use_vert_sel(ob);
1510 const bool use_mirror = (ob->type == OB_MESH) ? (((Mesh *)ob->data)->editflag & ME_EDIT_MIRROR_X) != 0 : false;
1512 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
1516 for (i = 0; i < dvert_tot; i++) {
1519 /* in case its not selected */
1520 if (!(dv = dvert_array[i])) {
1526 if (vgroup_validmap[j]) {
1527 dw = defvert_find_index(dv, j);
1529 dw->weight = gain * (dw->weight + offset);
1531 CLAMP(dw->weight, 0.0f, 1.0f);
1537 if (use_mirror && use_vert_sel) {
1538 ED_vgroup_parray_mirror_sync(ob, dvert_array, dvert_tot,
1539 vgroup_validmap, vgroup_tot);
1542 MEM_freeN(dvert_array);
1546 static bool vgroup_normalize_all(
1548 const bool *vgroup_validmap,
1549 const int vgroup_tot,
1550 const int subset_count,
1551 const bool lock_active,
1552 ReportList *reports)
1554 MDeformVert *dv, **dvert_array = NULL;
1555 int i, dvert_tot = 0;
1556 const int def_nr = ob->actdef - 1;
1558 const int use_vert_sel = vertex_group_use_vert_sel(ob);
1560 if (subset_count == 0) {
1561 BKE_report(reports, RPT_ERROR, "No vertex groups to operate on");
1565 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
1568 const int defbase_tot = BLI_listbase_count(&ob->defbase);
1569 bool *lock_flags = BKE_object_defgroup_lock_flags_get(ob, defbase_tot);
1570 bool changed = false;
1572 if ((lock_active == true) &&
1573 (lock_flags != NULL) &&
1574 (def_nr < defbase_tot))
1576 lock_flags[def_nr] = true;
1580 for (i = 0; i < defbase_tot; i++) {
1581 if (lock_flags[i] == false) {
1586 if (i == defbase_tot) {
1587 BKE_report(reports, RPT_ERROR, "All groups are locked");
1592 for (i = 0; i < dvert_tot; i++) {
1593 /* in case its not selected */
1594 if ((dv = dvert_array[i])) {
1596 defvert_normalize_lock_map(dv, vgroup_validmap, vgroup_tot,
1597 lock_flags, defbase_tot);
1599 else if (lock_active) {
1600 defvert_normalize_lock_single(dv, vgroup_validmap, vgroup_tot,
1604 defvert_normalize_subset(dv, vgroup_validmap, vgroup_tot);
1613 MEM_freeN(lock_flags);
1616 MEM_freeN(dvert_array);
1631 static EnumPropertyItem vgroup_lock_actions[] = {
1632 {VGROUP_TOGGLE, "TOGGLE", 0, "Toggle", "Unlock all vertex groups if there is at least one locked group, lock all in other case"},
1633 {VGROUP_LOCK, "LOCK", 0, "Lock", "Lock all vertex groups"},
1634 {VGROUP_UNLOCK, "UNLOCK", 0, "Unlock", "Unlock all vertex groups"},
1635 {VGROUP_INVERT, "INVERT", 0, "Invert", "Invert the lock state of all vertex groups"},
1636 {0, NULL, 0, NULL, NULL}
1639 static void vgroup_lock_all(Object *ob, int action)
1643 if (action == VGROUP_TOGGLE) {
1644 action = VGROUP_LOCK;
1645 for (dg = ob->defbase.first; dg; dg = dg->next) {
1646 if (dg->flag & DG_LOCK_WEIGHT) {
1647 action = VGROUP_UNLOCK;
1653 for (dg = ob->defbase.first; dg; dg = dg->next) {
1656 dg->flag |= DG_LOCK_WEIGHT;
1659 dg->flag &= ~DG_LOCK_WEIGHT;
1662 dg->flag ^= DG_LOCK_WEIGHT;
1668 static void vgroup_invert_subset(Object *ob,
1669 const bool *vgroup_validmap, const int vgroup_tot,
1670 const int UNUSED(subset_count), const bool auto_assign, const bool auto_remove)
1673 MDeformVert *dv, **dvert_array = NULL;
1674 int i, dvert_tot = 0;
1675 const bool use_vert_sel = vertex_group_use_vert_sel(ob);
1676 const bool use_mirror = (ob->type == OB_MESH) ? (((Mesh *)ob->data)->editflag & ME_EDIT_MIRROR_X) != 0 : false;
1678 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
1681 for (i = 0; i < dvert_tot; i++) {
1684 /* in case its not selected */
1685 if (!(dv = dvert_array[i])) {
1692 if (vgroup_validmap[j]) {
1694 dw = defvert_verify_index(dv, j);
1697 dw = defvert_find_index(dv, j);
1701 dw->weight = 1.0f - dw->weight;
1702 CLAMP(dw->weight, 0.0f, 1.0f);
1708 if (use_mirror && use_vert_sel) {
1709 ED_vgroup_parray_mirror_sync(ob, dvert_array, dvert_tot,
1710 vgroup_validmap, vgroup_tot);
1714 ED_vgroup_parray_remove_zero(dvert_array, dvert_tot,
1715 vgroup_validmap, vgroup_tot,
1719 MEM_freeN(dvert_array);
1724 WEIGHT_SMOOTH_ALL = -1,
1725 WEIGHT_SMOOTH_DESELECT = false,
1726 WEIGHT_SMOOTH_SELECT = true,
1729 static void vgroup_smooth_subset(
1730 Object *ob, const bool *vgroup_validmap, const int vgroup_tot,
1731 const int subset_count,
1732 const float fac, const int repeat,
1733 const float fac_expand, const int source)
1735 const float ifac = 1.0f - fac;
1736 MDeformVert **dvert_array = NULL;
1738 int *vgroup_subset_map = BLI_array_alloca(vgroup_subset_map, subset_count);
1739 float *vgroup_subset_weights = BLI_array_alloca(vgroup_subset_weights, subset_count);
1740 const bool use_mirror = (ob->type == OB_MESH) ? (((Mesh *)ob->data)->editflag & ME_EDIT_MIRROR_X) != 0 : false;
1742 const int expand_sign = signum_i(fac_expand);
1743 const float expand = fabsf(fac_expand);
1744 const float iexpand = 1.0f - expand;
1746 BMEditMesh *em = BKE_editmesh_from_object(ob);
1747 BMesh *bm = em ? em->bm : NULL;
1748 Mesh *me = em ? NULL : ob->data;
1753 float *weight_accum_prev;
1754 float *weight_accum_curr;
1756 unsigned int subset_index;
1758 /* vertex indices that will be smoothed, (only to avoid iterating over verts that do nothing) */
1759 unsigned int *verts_used;
1760 STACK_DECLARE(verts_used);
1763 BKE_object_defgroup_subset_to_index_array(vgroup_validmap, vgroup_tot, vgroup_subset_map);
1764 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, false);
1765 memset(vgroup_subset_weights, 0, sizeof(*vgroup_subset_weights) * subset_count);
1768 BM_mesh_elem_table_ensure(bm, BM_VERT);
1769 BM_mesh_elem_index_ensure(bm, BM_VERT);
1775 BKE_mesh_vert_edge_map_create(&emap, &emap_mem, me->medge, me->totvert, me->totedge);
1778 weight_accum_prev = MEM_mallocN(sizeof(*weight_accum_prev) * dvert_tot, __func__);
1779 weight_accum_curr = MEM_mallocN(sizeof(*weight_accum_curr) * dvert_tot, __func__);
1781 verts_used = MEM_mallocN(sizeof(*verts_used) * dvert_tot, __func__);
1782 STACK_INIT(verts_used, dvert_tot);
1785 /* initialize used verts */
1787 for (int i = 0; i < dvert_tot; i++) {
1788 BMVert *v = BM_vert_at_index(bm, i);
1789 if (BM_elem_flag_test(v, BM_ELEM_SELECT)) {
1792 BM_ITER_ELEM (e, &eiter, v, BM_EDGES_OF_VERT) {
1793 BMVert *v_other = BM_edge_other_vert(e, v);
1794 if ((source == WEIGHT_SMOOTH_ALL) ||
1795 (source == (BM_elem_flag_test(v_other, BM_ELEM_SELECT) != 0)))
1797 STACK_PUSH(verts_used, i);
1805 for (int i = 0; i < dvert_tot; i++) {
1806 MVert *v = &me->mvert[i];
1807 if (v->flag & SELECT) {
1808 for (int j = 0; j < emap[i].count; j++) {
1809 MVert *v_other = &me->mvert[emap[i].indices[j]];
1810 if ((source == WEIGHT_SMOOTH_ALL) ||
1811 (source == ((v_other->flag & SELECT) != 0)))
1813 STACK_PUSH(verts_used, i);
1821 for (subset_index = 0; subset_index < subset_count; subset_index++) {
1822 const int def_nr = vgroup_subset_map[subset_index];
1825 ED_vgroup_parray_to_weight_array((const MDeformVert **)dvert_array, dvert_tot, weight_accum_prev, def_nr);
1826 memcpy(weight_accum_curr, weight_accum_prev, sizeof(*weight_accum_curr) * dvert_tot);
1828 for (iter = 0; iter < repeat; iter++) {
1829 unsigned *vi_step, *vi_end = verts_used + STACK_SIZE(verts_used);
1831 /* avoid looping over all verts */
1832 // for (i = 0; i < dvert_tot; i++)
1833 for (vi_step = verts_used; vi_step != vi_end; vi_step++) {
1834 const unsigned int i = *vi_step;
1835 float weight_tot = 0.0f;
1836 float weight = 0.0f;
1838 #define WEIGHT_ACCUMULATE \
1840 float weight_other = weight_accum_prev[i_other]; \
1841 float tot_factor = 1.0f; \
1842 if (expand_sign == 1) { /* expand */ \
1843 if (weight_other < weight_accum_prev[i]) { \
1844 weight_other = (weight_accum_prev[i] * expand) + (weight_other * iexpand); \
1845 tot_factor = iexpand; \
1848 else if (expand_sign == -1) { /* contract */ \
1849 if (weight_other > weight_accum_prev[i]) { \
1850 weight_other = (weight_accum_prev[i] * expand) + (weight_other * iexpand); \
1851 tot_factor = iexpand; \
1854 weight += tot_factor * weight_other; \
1855 weight_tot += tot_factor; \
1860 BMVert *v = BM_vert_at_index(bm, i);
1864 /* checked already */
1865 BLI_assert(BM_elem_flag_test(v, BM_ELEM_SELECT));
1867 BM_ITER_ELEM (e, &eiter, v, BM_EDGES_OF_VERT) {
1868 BMVert *v_other = BM_edge_other_vert(e, v);
1869 if ((source == WEIGHT_SMOOTH_ALL) ||
1870 (source == (BM_elem_flag_test(v_other, BM_ELEM_SELECT) != 0)))
1872 const int i_other = BM_elem_index_get(v_other);
1881 /* checked already */
1882 BLI_assert(me->mvert[i].flag & SELECT);
1884 for (j = 0; j < emap[i].count; j++) {
1885 MEdge *e = &me->medge[emap[i].indices[j]];
1886 const int i_other = (e->v1 == i ? e->v2 : e->v1);
1887 MVert *v_other = &me->mvert[i_other];
1889 if ((source == WEIGHT_SMOOTH_ALL) ||
1890 (source == ((v_other->flag & SELECT) != 0)))
1897 #undef WEIGHT_ACCUMULATE
1899 if (weight_tot != 0.0f) {
1900 weight /= weight_tot;
1901 weight = (weight_accum_prev[i] * ifac) + (weight * fac);
1903 /* should be within range, just clamp because of float precision */
1904 CLAMP(weight, 0.0f, 1.0f);
1905 weight_accum_curr[i] = weight;
1909 SWAP(float *, weight_accum_curr, weight_accum_prev);
1912 ED_vgroup_parray_from_weight_array(dvert_array, dvert_tot, weight_accum_prev, def_nr, true);
1915 MEM_freeN(weight_accum_curr);
1916 MEM_freeN(weight_accum_prev);
1917 MEM_freeN(verts_used);
1924 MEM_freeN(emap_mem);
1928 MEM_freeN(dvert_array);
1930 /* not so efficient to get 'dvert_array' again just so unselected verts are NULL'd */
1932 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, true);
1933 ED_vgroup_parray_mirror_sync(ob, dvert_array, dvert_tot,
1934 vgroup_validmap, vgroup_tot);
1936 MEM_freeN(dvert_array);
1940 static int inv_cmp_mdef_vert_weights(const void *a1, const void *a2)
1942 /* qsort sorts in ascending order. We want descending order to save a memcopy
1943 * so this compare function is inverted from the standard greater than comparison qsort needs.
1944 * A normal compare function is called with two pointer arguments and should return an integer less than, equal to,
1945 * or greater than zero corresponding to whether its first argument is considered less than, equal to,
1946 * or greater than its second argument. This does the opposite. */
1947 const struct MDeformWeight *dw1 = a1, *dw2 = a2;
1949 if (dw1->weight < dw2->weight) return 1;
1950 else if (dw1->weight > dw2->weight) return -1;
1951 else if (&dw1 < &dw2) return 1; /* compare addresses so we have a stable sort algorithm */
1955 /* Used for limiting the number of influencing bones per vertex when exporting
1956 * skinned meshes. if all_deform_weights is True, limit all deform modifiers
1957 * to max_weights regardless of type, otherwise, only limit the number of influencing bones per vertex*/
1958 static int vgroup_limit_total_subset(Object *ob,
1959 const bool *vgroup_validmap,
1960 const int vgroup_tot,
1961 const int subset_count,
1962 const int max_weights)
1964 MDeformVert *dv, **dvert_array = NULL;
1965 int i, dvert_tot = 0;
1966 const int use_vert_sel = vertex_group_use_vert_sel(ob);
1969 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
1972 int num_to_drop = 0;
1974 for (i = 0; i < dvert_tot; i++) {
1976 MDeformWeight *dw_temp;
1977 int bone_count = 0, non_bone_count = 0;
1980 /* in case its not selected */
1981 if (!(dv = dvert_array[i])) {
1985 num_to_drop = subset_count - max_weights;
1987 /* first check if we even need to test further */
1988 if (num_to_drop > 0) {
1989 /* re-pack dw array so that non-bone weights are first, bone-weighted verts at end
1990 * sort the tail, then copy only the truncated array back to dv->dw */
1991 dw_temp = MEM_mallocN(sizeof(MDeformWeight) * dv->totweight, __func__);
1992 bone_count = 0; non_bone_count = 0;
1993 for (j = 0; j < dv->totweight; j++) {
1994 if (LIKELY(dv->dw[j].def_nr < vgroup_tot) &&
1995 vgroup_validmap[dv->dw[j].def_nr])
1997 dw_temp[dv->totweight - 1 - bone_count] = dv->dw[j];
2001 dw_temp[non_bone_count] = dv->dw[j];
2002 non_bone_count += 1;
2005 BLI_assert(bone_count + non_bone_count == dv->totweight);
2006 num_to_drop = bone_count - max_weights;
2007 if (num_to_drop > 0) {
2008 qsort(&dw_temp[non_bone_count], bone_count, sizeof(MDeformWeight), inv_cmp_mdef_vert_weights);
2009 dv->totweight -= num_to_drop;
2010 /* Do we want to clean/normalize here? */
2012 dv->dw = MEM_reallocN(dw_temp, sizeof(MDeformWeight) * dv->totweight);
2013 remove_tot += num_to_drop;
2021 MEM_freeN(dvert_array);
2029 static void vgroup_clean_subset(Object *ob, const bool *vgroup_validmap, const int vgroup_tot, const int UNUSED(subset_count),
2030 const float epsilon, const bool keep_single)
2032 MDeformVert **dvert_array = NULL;
2034 const bool use_vert_sel = vertex_group_use_vert_sel(ob);
2035 const bool use_mirror = (ob->type == OB_MESH) ? (((Mesh *)ob->data)->editflag & ME_EDIT_MIRROR_X) != 0 : false;
2037 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
2040 if (use_mirror && use_vert_sel) {
2041 /* correct behavior in this case isn't well defined
2042 * for now assume both sides are mirrored correctly,
2043 * so cleaning one side also cleans the other */
2044 ED_vgroup_parray_mirror_assign(ob, dvert_array, dvert_tot);
2047 ED_vgroup_parray_remove_zero(dvert_array, dvert_tot,
2048 vgroup_validmap, vgroup_tot,
2049 epsilon, keep_single);
2051 MEM_freeN(dvert_array);
2055 static void vgroup_quantize_subset(Object *ob, const bool *vgroup_validmap, const int vgroup_tot, const int UNUSED(subset_count),
2058 MDeformVert **dvert_array = NULL;
2060 const bool use_vert_sel = vertex_group_use_vert_sel(ob);
2061 const bool use_mirror = (ob->type == OB_MESH) ? (((Mesh *)ob->data)->editflag & ME_EDIT_MIRROR_X) != 0 : false;
2062 ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
2065 const float steps_fl = steps;
2069 if (use_mirror && use_vert_sel) {
2070 ED_vgroup_parray_mirror_assign(ob, dvert_array, dvert_tot);
2073 for (i = 0; i < dvert_tot; i++) {
2077 /* in case its not selected */
2078 if (!(dv = dvert_array[i])) {
2082 for (j = 0, dw = dv->dw; j < dv->totweight; j++, dw++) {
2083 if ((dw->def_nr < vgroup_tot) && vgroup_validmap[dw->def_nr]) {
2084 dw->weight = floorf((dw->weight * steps_fl) + 0.5f) / steps_fl;
2085 CLAMP(dw->weight, 0.0f, 1.0f);
2090 MEM_freeN(dvert_array);
2094 static void dvert_mirror_op(MDeformVert *dvert, MDeformVert *dvert_mirr,
2095 const char sel, const char sel_mirr,
2096 const int *flip_map, const int flip_map_len,
2097 const bool mirror_weights, const bool flip_vgroups, const bool all_vgroups,
2098 const int act_vgroup)
2100 BLI_assert(sel || sel_mirr);
2102 if (sel_mirr && sel) {
2104 if (mirror_weights) {
2106 SWAP(MDeformVert, *dvert, *dvert_mirr);
2109 MDeformWeight *dw = defvert_find_index(dvert, act_vgroup);
2110 MDeformWeight *dw_mirr = defvert_find_index(dvert_mirr, act_vgroup);
2112 if (dw && dw_mirr) {
2113 SWAP(float, dw->weight, dw_mirr->weight);
2116 dw_mirr = defvert_verify_index(dvert_mirr, act_vgroup);
2117 dw_mirr->weight = dw->weight;
2118 defvert_remove_group(dvert, dw);
2121 dw = defvert_verify_index(dvert, act_vgroup);
2122 dw->weight = dw_mirr->weight;
2123 defvert_remove_group(dvert_mirr, dw_mirr);
2129 defvert_flip(dvert, flip_map, flip_map_len);
2130 defvert_flip(dvert_mirr, flip_map, flip_map_len);
2134 /* dvert should always be the target, only swaps pointer */
2136 SWAP(MDeformVert *, dvert, dvert_mirr);
2139 if (mirror_weights) {
2141 defvert_copy(dvert, dvert_mirr);
2144 defvert_copy_index(dvert, act_vgroup, dvert_mirr, act_vgroup);
2148 /* flip map already modified for 'all_vgroups' */
2150 defvert_flip(dvert, flip_map, flip_map_len);
2155 /* TODO, vgroup locking */
2156 /* TODO, face masking */
2157 void ED_vgroup_mirror(Object *ob,
2158 const bool mirror_weights, const bool flip_vgroups,
2159 const bool all_vgroups, const bool use_topology,
2160 int *r_totmirr, int *r_totfail)
2163 #define VGROUP_MIRR_OP \
2164 dvert_mirror_op(dvert, dvert_mirr, \
2166 flip_map, flip_map_len, \
2167 mirror_weights, flip_vgroups, \
2168 all_vgroups, def_nr \
2171 BMVert *eve, *eve_mirr;
2172 MDeformVert *dvert, *dvert_mirr;
2174 int *flip_map = NULL, flip_map_len;
2175 const int def_nr = ob->actdef - 1;
2176 int totmirr = 0, totfail = 0;
2178 *r_totmirr = *r_totfail = 0;
2180 if ((mirror_weights == false && flip_vgroups == false) ||
2181 (BLI_findlink(&ob->defbase, def_nr) == NULL))
2187 flip_map = all_vgroups ?
2188 defgroup_flip_map(ob, &flip_map_len, false) :
2189 defgroup_flip_map_single(ob, &flip_map_len, false, def_nr);
2191 BLI_assert(flip_map != NULL);
2193 if (flip_map == NULL) {
2194 /* something went wrong!, possibly no groups */
2203 /* only the active group */
2204 if (ob->type == OB_MESH) {
2205 Mesh *me = ob->data;
2206 BMEditMesh *em = me->edit_btmesh;
2209 const int cd_dvert_offset = CustomData_get_offset(&em->bm->vdata, CD_MDEFORMVERT);
2212 if (cd_dvert_offset == -1) {
2216 EDBM_verts_mirror_cache_begin(em, 0, true, false, use_topology);
2218 BM_mesh_elem_hflag_disable_all(em->bm, BM_VERT, BM_ELEM_TAG, false);
2220 /* Go through the list of editverts and assign them */
2221 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
2222 if (!BM_elem_flag_test(eve, BM_ELEM_TAG)) {
2223 if ((eve_mirr = EDBM_verts_mirror_get(em, eve))) {
2224 if (eve_mirr != eve) {
2225 if (!BM_elem_flag_test(eve_mirr, BM_ELEM_TAG)) {
2226 sel = BM_elem_flag_test(eve, BM_ELEM_SELECT);
2227 sel_mirr = BM_elem_flag_test(eve_mirr, BM_ELEM_SELECT);
2229 if ((sel || sel_mirr) && (eve != eve_mirr)) {
2230 dvert = BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset);
2231 dvert_mirr = BM_ELEM_CD_GET_VOID_P(eve_mirr, cd_dvert_offset);
2237 /* don't use these again */
2238 BM_elem_flag_enable(eve, BM_ELEM_TAG);
2239 BM_elem_flag_enable(eve_mirr, BM_ELEM_TAG);
2248 EDBM_verts_mirror_cache_end(em);
2251 /* object mode / weight paint */
2252 MVert *mv, *mv_mirr;
2253 int vidx, vidx_mirr;
2254 const bool use_vert_sel = (me->editflag & ME_EDIT_PAINT_VERT_SEL) != 0;
2256 if (me->dvert == NULL) {
2260 if (!use_vert_sel) {
2261 sel = sel_mirr = true;
2264 /* tag verts we have used */
2265 for (vidx = 0, mv = me->mvert; vidx < me->totvert; vidx++, mv++) {
2266 mv->flag &= ~ME_VERT_TMP_TAG;
2269 for (vidx = 0, mv = me->mvert; vidx < me->totvert; vidx++, mv++) {
2270 if ((mv->flag & ME_VERT_TMP_TAG) == 0) {
2271 if ((vidx_mirr = mesh_get_x_mirror_vert(ob, NULL, vidx, use_topology)) != -1) {
2272 if (vidx != vidx_mirr) {
2273 mv_mirr = &me->mvert[vidx_mirr];
2274 if ((mv_mirr->flag & ME_VERT_TMP_TAG) == 0) {
2277 sel = mv->flag & SELECT;
2278 sel_mirr = mv_mirr->flag & SELECT;
2281 if (sel || sel_mirr) {
2282 dvert = &me->dvert[vidx];
2283 dvert_mirr = &me->dvert[vidx_mirr];
2289 mv->flag |= ME_VERT_TMP_TAG;
2290 mv_mirr->flag |= ME_VERT_TMP_TAG;
2301 else if (ob->type == OB_LATTICE) {
2302 Lattice *lt = vgroup_edit_lattice(ob);
2306 /* half but found up odd value */
2308 if (lt->pntsu == 1 || lt->dvert == NULL) {
2312 /* unlike editmesh we know that by only looping over the first half of
2313 * the 'u' indices it will cover all points except the middle which is
2314 * ok in this case */
2315 pntsu_half = lt->pntsu / 2;
2317 for (w = 0; w < lt->pntsw; w++) {
2318 for (v = 0; v < lt->pntsv; v++) {
2319 for (u = 0; u < pntsu_half; u++) {
2320 int u_inv = (lt->pntsu - 1) - u;
2322 BPoint *bp, *bp_mirr;
2324 i1 = BKE_lattice_index_from_uvw(lt, u, v, w);
2325 i2 = BKE_lattice_index_from_uvw(lt, u_inv, v, w);
2328 bp_mirr = <->def[i2];
2330 sel = bp->f1 & SELECT;
2331 sel_mirr = bp_mirr->f1 & SELECT;
2333 if (sel || sel_mirr) {
2334 dvert = <->dvert[i1];
2335 dvert_mirr = <->dvert[i2];
2346 /* disabled, confusing when you have an active pose bone */
2348 /* flip active group index */
2349 if (flip_vgroups && flip_map[def_nr] >= 0)
2350 ob->actdef = flip_map[def_nr] + 1;
2354 *r_totmirr = totmirr;
2355 *r_totfail = totfail;
2357 if (flip_map) MEM_freeN(flip_map);
2359 #undef VGROUP_MIRR_OP
2363 static void vgroup_delete_active(Object *ob)
2365 bDeformGroup *dg = BLI_findlink(&ob->defbase, ob->actdef - 1);
2369 BKE_object_defgroup_remove(ob, dg);
2372 /* only in editmode */
2373 static void vgroup_assign_verts(Object *ob, const float weight)
2375 const int def_nr = ob->actdef - 1;
2377 if (!BLI_findlink(&ob->defbase, def_nr))
2380 if (ob->type == OB_MESH) {
2381 Mesh *me = ob->data;
2383 if (me->edit_btmesh) {
2384 BMEditMesh *em = me->edit_btmesh;
2385 int cd_dvert_offset;
2390 if (!CustomData_has_layer(&em->bm->vdata, CD_MDEFORMVERT))
2391 BM_data_layer_add(em->bm, &em->bm->vdata, CD_MDEFORMVERT);
2393 cd_dvert_offset = CustomData_get_offset(&em->bm->vdata, CD_MDEFORMVERT);
2395 /* Go through the list of editverts and assign them */
2396 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
2397 if (BM_elem_flag_test(eve, BM_ELEM_SELECT)) {
2400 dv = BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset); /* can be NULL */
2401 dw = defvert_verify_index(dv, def_nr);
2403 dw->weight = weight;
2414 BKE_object_defgroup_data_create(&me->id);
2420 for (i = 0; i < me->totvert; i++, mv++, dv++) {
2421 if (mv->flag & SELECT) {
2423 dw = defvert_verify_index(dv, def_nr);
2425 dw->weight = weight;
2431 else if (ob->type == OB_LATTICE) {
2432 Lattice *lt = vgroup_edit_lattice(ob);
2437 if (lt->dvert == NULL)
2438 BKE_object_defgroup_data_create(<->id);
2442 tot = lt->pntsu * lt->pntsv * lt->pntsw;
2443 for (a = 0, bp = lt->def; a < tot; a++, bp++, dv++) {
2444 if (bp->f1 & SELECT) {
2447 dw = defvert_verify_index(dv, def_nr);
2449 dw->weight = weight;
2456 /********************** vertex group operators *********************/
2458 static int vertex_group_poll(bContext *C)
2460 Object *ob = ED_object_context(C);
2461 ID *data = (ob) ? ob->data : NULL;
2463 return (ob && !ID_IS_LINKED_DATABLOCK(ob) &&
2464 data && !ID_IS_LINKED_DATABLOCK(data) &&
2465 OB_TYPE_SUPPORT_VGROUP(ob->type) &&
2469 static int vertex_group_supported_poll(bContext *C)
2471 Object *ob = ED_object_context(C);
2472 ID *data = (ob) ? ob->data : NULL;
2473 return (ob && !ID_IS_LINKED_DATABLOCK(ob) && OB_TYPE_SUPPORT_VGROUP(ob->type) &&
2474 data && !ID_IS_LINKED_DATABLOCK(data));
2477 static int vertex_group_mesh_poll(bContext *C)
2479 Object *ob = ED_object_context(C);
2480 ID *data = (ob) ? ob->data : NULL;
2482 return (ob && !ID_IS_LINKED_DATABLOCK(ob) &&
2483 data && !ID_IS_LINKED_DATABLOCK(data) &&
2484 ob->type == OB_MESH &&
2488 static int UNUSED_FUNCTION(vertex_group_mesh_supported_poll)(bContext *C)
2490 Object *ob = ED_object_context(C);
2491 ID *data = (ob) ? ob->data : NULL;
2492 return (ob && !ID_IS_LINKED_DATABLOCK(ob) && ob->type == OB_MESH && data && !ID_IS_LINKED_DATABLOCK(data));
2496 static int UNUSED_FUNCTION(vertex_group_poll_edit) (bContext *C)
2498 Object *ob = ED_object_context(C);
2499 ID *data = (ob) ? ob->data : NULL;
2501 if (!(ob && !ID_IS_LINKED_DATABLOCK(ob) && data && !ID_IS_LINKED_DATABLOCK(data)))
2504 return BKE_object_is_in_editmode_vgroup(ob);
2507 /* editmode _or_ weight paint vertex sel */
2508 static int vertex_group_vert_select_poll_ex(bContext *C, const short ob_type_flag)
2510 Object *ob = ED_object_context(C);
2511 ID *data = (ob) ? ob->data : NULL;
2513 if (!(ob && !ID_IS_LINKED_DATABLOCK(ob) && data && !ID_IS_LINKED_DATABLOCK(data)))
2516 if (ob_type_flag && (((1 << ob->type) & ob_type_flag)) == 0) {
2520 if (BKE_object_is_in_editmode_vgroup(ob)) {
2523 else if (ob->mode & OB_MODE_WEIGHT_PAINT) {
2524 if (BKE_object_is_in_wpaint_select_vert(ob)) {
2528 CTX_wm_operator_poll_msg_set(C, "Vertex select needs to be enabled in weight paint mode");
2537 static int vertex_group_vert_select_poll(bContext *C)
2539 return vertex_group_vert_select_poll_ex(C, 0);
2542 static int vertex_group_mesh_vert_select_poll(bContext *C)
2544 return vertex_group_vert_select_poll_ex(C, (1 << OB_MESH));
2547 /* editmode _or_ weight paint vertex sel and active group unlocked */
2548 static int vertex_group_vert_select_unlocked_poll(bContext *C)
2550 Object *ob = ED_object_context(C);
2551 ID *data = (ob) ? ob->data : NULL;
2553 if (!(ob && !ID_IS_LINKED_DATABLOCK(ob) && data && !ID_IS_LINKED_DATABLOCK(data)))
2556 if (!(BKE_object_is_in_editmode_vgroup(ob) ||
2557 BKE_object_is_in_wpaint_select_vert(ob)))
2562 if (ob->actdef != 0) {
2563 bDeformGroup *dg = BLI_findlink(&ob->defbase, ob->actdef - 1);
2565 return !(dg->flag & DG_LOCK_WEIGHT);
2571 static int vertex_group_vert_select_mesh_poll(bContext *C)
2573 Object *ob = ED_object_context(C);
2574 ID *data = (ob) ? ob->data : NULL;
2576 if (!(ob && !ID_IS_LINKED_DATABLOCK(ob) && data && !ID_IS_LINKED_DATABLOCK(data)))
2579 /* only difference to #vertex_group_vert_select_poll */
2580 if (ob->type != OB_MESH)
2583 return (BKE_object_is_in_editmode_vgroup(ob) ||
2584 BKE_object_is_in_wpaint_select_vert(ob));
2587 static int vertex_group_add_exec(bContext *C, wmOperator *UNUSED(op))
2589 Object *ob = ED_object_context(C);
2591 BKE_object_defgroup_add(ob);
2592 DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
2593 WM_event_add_notifier(C, NC_GEOM | ND_VERTEX_GROUP, ob->data);
2594 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2596 return OPERATOR_FINISHED;
2599 void OBJECT_OT_vertex_group_add(wmOperatorType *ot)
2602 ot->name = "Add Vertex Group";
2603 ot->idname = "OBJECT_OT_vertex_group_add";
2604 ot->description = "Add a new vertex group to the active object";
2607 ot->poll = vertex_group_supported_poll;
2608 ot->exec = vertex_group_add_exec;
2611 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2614 static int vertex_group_remove_exec(bContext *C, wmOperator *op)
2616 Object *ob = ED_object_context(C);
2618 if (RNA_boolean_get(op->ptr, "all"))
2619 BKE_object_defgroup_remove_all(ob);
2620 else if (RNA_boolean_get(op->ptr, "all_unlocked"))
2621 BKE_object_defgroup_remove_all_ex(ob, true);
2623 vgroup_delete_active(ob);
2625 DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
2626 WM_event_add_notifier(C, NC_GEOM | ND_VERTEX_GROUP, ob->data);
2627 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2629 return OPERATOR_FINISHED;
2632 void OBJECT_OT_vertex_group_remove(wmOperatorType *ot)
2635 ot->name = "Remove Vertex Group";
2636 ot->idname = "OBJECT_OT_vertex_group_remove";
2637 ot->description = "Delete the active or all vertex groups from the active object";
2640 ot->poll = vertex_group_poll;
2641 ot->exec = vertex_group_remove_exec;
2644 /* redo operator will fail in this case because vertex groups aren't stored
2645 * in local edit mode stack and toggling "all" property will lead to
2646 * all groups deleted without way to restore them (see [#29527], sergey) */
2647 ot->flag = /*OPTYPE_REGISTER|*/ OPTYPE_UNDO;
2650 RNA_def_boolean(ot->srna, "all", 0, "All", "Remove all vertex groups");
2651 RNA_def_boolean(ot->srna, "all_unlocked", 0, "All Unlocked", "Remove all unlocked vertex groups");
2654 static int vertex_group_assign_exec(bContext *C, wmOperator *UNUSED(op))
2656 ToolSettings *ts = CTX_data_tool_settings(C);
2657 Object *ob = ED_object_context(C);
2659 vgroup_assign_verts(ob, ts->vgroup_weight);
2660 DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
2661 WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
2663 return OPERATOR_FINISHED;
2666 void OBJECT_OT_vertex_group_assign(wmOperatorType *ot)
2669 ot->name = "Assign to Vertex Group";
2670 ot->idname = "OBJECT_OT_vertex_group_assign";
2671 ot->description = "Assign the selected vertices to the active vertex group";
2674 ot->poll = vertex_group_vert_select_unlocked_poll;
2675 ot->exec = vertex_group_assign_exec;
2678 /* redo operator will fail in this case because vertex group assignment
2679 * isn't stored in local edit mode stack and toggling "new" property will
2680 * lead to creating plenty of new vertex groups (see [#29527], sergey) */
2681 ot->flag = /*OPTYPE_REGISTER|*/ OPTYPE_UNDO;
2684 /* NOTE: just a wrapper around vertex_group_assign_exec(), except we add these to a new group */
2685 static int vertex_group_assign_new_exec(bContext *C, wmOperator *op)
2687 /* create new group... */
2688 Object *ob = ED_object_context(C);
2689 BKE_object_defgroup_add(ob);
2691 /* assign selection to new group */
2692 return vertex_group_assign_exec(C, op);
2695 void OBJECT_OT_vertex_group_assign_new(wmOperatorType *ot)
2698 ot->name = "Assign to New Group";
2699 ot->idname = "OBJECT_OT_vertex_group_assign_new";
2700 ot->description = "Assign the selected vertices to a new vertex group";
2703 ot->poll = vertex_group_vert_select_poll;
2704 ot->exec = vertex_group_assign_new_exec;
2707 /* redo operator will fail in this case because vertex group assignment
2708 * isn't stored in local edit mode stack and toggling "new" property will
2709 * lead to creating plenty of new vertex groups (see [#29527], sergey) */
2710 ot->flag = /*OPTYPE_REGISTER|*/ OPTYPE_UNDO;
2713 static int vertex_group_remove_from_exec(bContext *C, wmOperator *op)
2715 const bool use_all_groups = RNA_boolean_get(op->ptr, "use_all_groups");
2716 const bool use_all_verts = RNA_boolean_get(op->ptr, "use_all_verts");
2718 Object *ob = ED_object_context(C);
2720 if (use_all_groups) {
2721 if (BKE_object_defgroup_clear_all(ob, true) == false) {
2722 return OPERATOR_CANCELLED;
2726 bDeformGroup *dg = BLI_findlink(&ob->defbase, ob->actdef - 1);
2728 if ((dg == NULL) || (BKE_object_defgroup_clear(ob, dg, !use_all_verts) == false)) {
2729 return OPERATOR_CANCELLED;
2733 DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
2734 WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
2736 return OPERATOR_FINISHED;
2739 void OBJECT_OT_vertex_group_remove_from(wmOperatorType *ot)
2743 ot->name = "Remove from Vertex Group";
2744 ot->idname = "OBJECT_OT_vertex_group_remove_from";
2745 ot->description = "Remove the selected vertices from active or all vertex group(s)";
2748 ot->poll = vertex_group_vert_select_unlocked_poll;
2749 ot->exec = vertex_group_remove_from_exec;
2752 /* redo operator will fail in this case because vertex groups assignment
2753 * isn't stored in local edit mode stack and toggling "all" property will lead to
2754 * removing vertices from all groups (see [#29527], sergey) */
2755 ot->flag = /*OPTYPE_REGISTER|*/ OPTYPE_UNDO;
2758 prop = RNA_def_boolean(ot->srna, "use_all_groups", 0, "All Groups", "Remove from all groups");
2759 RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2760 prop = RNA_def_boolean(ot->srna, "use_all_verts", 0, "All Verts", "Clear the active group");
2761 RNA_def_property_flag(prop, PROP_SKIP_SAVE);
2764 static int vertex_group_select_exec(bContext *C, wmOperator *UNUSED(op))
2766 Object *ob = ED_object_context(C);
2768 if (!ob || ID_IS_LINKED_DATABLOCK(ob))
2769 return OPERATOR_CANCELLED;
2771 vgroup_select_verts(ob, 1);
2772 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, ob->data);
2774 return OPERATOR_FINISHED;
2777 void OBJECT_OT_vertex_group_select(wmOperatorType *ot)
2780 ot->name = "Select Vertex Group";
2781 ot->idname = "OBJECT_OT_vertex_group_select";
2782 ot->description = "Select all the vertices assigned to the active vertex group";
2785 ot->poll = vertex_group_vert_select_poll;
2786 ot->exec = vertex_group_select_exec;
2789 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2792 static int vertex_group_deselect_exec(bContext *C, wmOperator *UNUSED(op))
2794 Object *ob = ED_object_context(C);
2796 vgroup_select_verts(ob, 0);
2797 WM_event_add_notifier(C, NC_GEOM | ND_SELECT, ob->data);
2799 return OPERATOR_FINISHED;
2802 void OBJECT_OT_vertex_group_deselect(wmOperatorType *ot)
2805 ot->name = "Deselect Vertex Group";
2806 ot->idname = "OBJECT_OT_vertex_group_deselect";
2807 ot->description = "Deselect all selected vertices assigned to the active vertex group";
2810 ot->poll = vertex_group_vert_select_poll;
2811 ot->exec = vertex_group_deselect_exec;
2814 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2817 static int vertex_group_copy_exec(bContext *C, wmOperator *UNUSED(op))
2819 Object *ob = ED_object_context(C);
2821 vgroup_duplicate(ob);
2822 DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
2823 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2824 WM_event_add_notifier(C, NC_GEOM | ND_VERTEX_GROUP, ob->data);
2826 return OPERATOR_FINISHED;
2829 void OBJECT_OT_vertex_group_copy(wmOperatorType *ot)
2832 ot->name = "Copy Vertex Group";
2833 ot->idname = "OBJECT_OT_vertex_group_copy";
2834 ot->description = "Make a copy of the active vertex group";
2837 ot->poll = vertex_group_poll;
2838 ot->exec = vertex_group_copy_exec;
2841 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2844 static int vertex_group_levels_exec(bContext *C, wmOperator *op)
2846 Object *ob = ED_object_context(C);
2848 float offset = RNA_float_get(op->ptr, "offset");
2849 float gain = RNA_float_get(op->ptr, "gain");
2850 eVGroupSelect subset_type = RNA_enum_get(op->ptr, "group_select_mode");
2852 int subset_count, vgroup_tot;
2854 const bool *vgroup_validmap = BKE_object_defgroup_subset_from_select_type(ob, subset_type, &vgroup_tot, &subset_count);
2855 vgroup_levels_subset(ob, vgroup_validmap, vgroup_tot, subset_count, offset, gain);
2856 MEM_freeN((void *)vgroup_validmap);
2858 DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
2859 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2860 WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
2862 return OPERATOR_FINISHED;
2865 void OBJECT_OT_vertex_group_levels(wmOperatorType *ot)
2868 ot->name = "Vertex Group Levels";
2869 ot->idname = "OBJECT_OT_vertex_group_levels";
2870 ot->description = "Add some offset and multiply with some gain the weights of the active vertex group";
2873 ot->poll = vertex_group_poll;
2874 ot->exec = vertex_group_levels_exec;
2877 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2879 vgroup_operator_subset_select_props(ot, true);
2880 RNA_def_float(ot->srna, "offset", 0.f, -1.0, 1.0, "Offset", "Value to add to weights", -1.0f, 1.f);
2881 RNA_def_float(ot->srna, "gain", 1.f, 0.f, FLT_MAX, "Gain", "Value to multiply weights by", 0.0f, 10.f);
2884 static int vertex_group_normalize_exec(bContext *C, wmOperator *UNUSED(op))
2886 Object *ob = ED_object_context(C);
2889 changed = vgroup_normalize(ob);
2892 DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
2893 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2894 WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
2896 return OPERATOR_FINISHED;
2899 return OPERATOR_CANCELLED;
2903 void OBJECT_OT_vertex_group_normalize(wmOperatorType *ot)
2906 ot->name = "Normalize Vertex Group";
2907 ot->idname = "OBJECT_OT_vertex_group_normalize";
2908 ot->description = "Normalize weights of the active vertex group, so that the highest ones are now 1.0";
2911 ot->poll = vertex_group_poll;
2912 ot->exec = vertex_group_normalize_exec;
2915 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2918 static int vertex_group_normalize_all_exec(bContext *C, wmOperator *op)
2920 Object *ob = ED_object_context(C);
2921 bool lock_active = RNA_boolean_get(op->ptr, "lock_active");
2922 eVGroupSelect subset_type = RNA_enum_get(op->ptr, "group_select_mode");
2924 int subset_count, vgroup_tot;
2925 const bool *vgroup_validmap = BKE_object_defgroup_subset_from_select_type(ob, subset_type, &vgroup_tot, &subset_count);
2927 changed = vgroup_normalize_all(ob, vgroup_validmap, vgroup_tot, subset_count, lock_active, op->reports);
2928 MEM_freeN((void *)vgroup_validmap);
2931 DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
2932 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2933 WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
2935 return OPERATOR_FINISHED;
2938 /* allow to adjust settings */
2939 return OPERATOR_FINISHED;
2943 void OBJECT_OT_vertex_group_normalize_all(wmOperatorType *ot)
2946 ot->name = "Normalize All Vertex Groups";
2947 ot->idname = "OBJECT_OT_vertex_group_normalize_all";
2948 ot->description = "Normalize all weights of all vertex groups, "
2949 "so that for each vertex, the sum of all weights is 1.0";
2952 ot->poll = vertex_group_poll;
2953 ot->exec = vertex_group_normalize_all_exec;
2956 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2958 vgroup_operator_subset_select_props(ot, false);
2959 RNA_def_boolean(ot->srna, "lock_active", true, "Lock Active",
2960 "Keep the values of the active group while normalizing others");
2963 static int vertex_group_fix_exec(bContext *C, wmOperator *op)
2965 Object *ob = CTX_data_active_object(C);
2966 Scene *scene = CTX_data_scene(C);
2968 float distToBe = RNA_float_get(op->ptr, "dist");
2969 float strength = RNA_float_get(op->ptr, "strength");
2970 float cp = RNA_float_get(op->ptr, "accuracy");
2971 ModifierData *md = ob->modifiers.first;
<