Blender V5.3
object_vgroup.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10#include <cmath>
11#include <cstring>
12
13#include "MEM_guardedalloc.h"
14
15#include "DNA_curve_types.h"
16#include "DNA_lattice_types.h"
17#include "DNA_mesh_types.h"
18#include "DNA_meshdata_types.h"
19#include "DNA_object_types.h"
20#include "DNA_scene_types.h"
21
22#include "BLI_array.hh"
23#include "BLI_bitmap.hh"
24#include "BLI_listbase.hh"
25#include "BLI_string.hh"
26#include "BLI_string_utf8.hh"
27#include "BLI_utildefines.hh"
29#include "BLI_vector.hh"
30
31#include "BKE_attribute.hh"
32#include "BKE_context.hh"
33#include "BKE_curves.hh"
34#include "BKE_customdata.hh"
35#include "BKE_deform.hh"
36#include "BKE_editmesh.hh"
38#include "BKE_lattice.hh"
39#include "BKE_library.hh"
40#include "BKE_mesh.hh"
41#include "BKE_mesh_mapping.hh"
42#include "BKE_modifier.hh"
43#include "BKE_object.hh"
44#include "BKE_object_deform.h"
45#include "BKE_report.hh"
46
47#include "DEG_depsgraph.hh"
49
50#include "BLT_translation.hh"
51
52#include "DNA_armature_types.h"
53#include "RNA_access.hh"
54#include "RNA_define.hh"
55#include "RNA_enum_types.hh"
56
57#include "WM_api.hh"
58#include "WM_types.hh"
59
60#include "ED_curves.hh"
61#include "ED_grease_pencil.hh"
62#include "ED_mesh.hh"
63#include "ED_object.hh"
64#include "ED_object_vgroup.hh"
65#include "ED_screen.hh"
66
67#include "UI_resources.hh"
68
69#include "object_intern.hh"
70
71namespace blender::ed::object {
72
73static bool vertex_group_supported_poll_ex(bContext *C, const Object *ob);
74
75/* -------------------------------------------------------------------- */
78
79static bool object_array_for_wpaint_filter(const Object *ob, void *user_data)
80{
81 bContext *C = static_cast<bContext *>(user_data);
83 return true;
84 }
85 return false;
86}
87
92
94{
95 if (ob->mode == OB_MODE_EDIT) {
96 return true;
97 }
98 if ((ob->type == OB_MESH) &&
99 (id_cast<Mesh *>(ob->data))->editflag & (ME_EDIT_PAINT_VERT_SEL | ME_EDIT_PAINT_FACE_SEL))
100 {
101 return true;
102 }
103 return false;
104}
105
107{
108 Lattice *lt = id_cast<Lattice *>(ob->data);
109 BLI_assert(ob->type == OB_LATTICE);
110 return (lt->editlatt) ? lt->editlatt->latt : lt;
111}
112
114
115/* -------------------------------------------------------------------- */
118
120{
122 if (armobj && (armobj->mode & OB_MODE_POSE)) {
123 bArmature *arm = id_cast<bArmature *>(armobj->data);
124 if (arm->act_bone) {
125 int def_num = BKE_object_defgroup_name_index(ob, arm->act_bone->name);
126 if (def_num != -1) {
128 return true;
129 }
130 }
131 }
132 return false;
133}
134
135void vgroup_data_clamp_range(ID *id, const int total)
136{
137 MDeformVert **dvert_arr;
138 int dvert_tot;
139
140 if (vgroup_parray_alloc(id, &dvert_arr, &dvert_tot, false)) {
141 for (int i = 0; i < dvert_tot; i++) {
142 MDeformVert *dv = dvert_arr[i];
143 for (int j = 0; j < dv->totweight; j++) {
144 if (dv->dw[j].def_nr >= total) {
145 BKE_defvert_remove_group(dv, &dv->dw[j]);
146 j--;
147 }
148 }
149 }
150 }
151}
152
154 MDeformVert ***dvert_arr,
155 int *dvert_tot,
156 const bool use_vert_sel,
157 std::optional<int> current_frame)
158{
159 *dvert_tot = 0;
160 *dvert_arr = nullptr;
161
162 if (id) {
163 switch (GS(id->name)) {
164 case ID_ME: {
165 Mesh *mesh = id_cast<Mesh *>(id);
166
168 const int cd_dvert_offset = CustomData_get_offset(&bm->vdata, CD_MDEFORMVERT);
169 BMIter iter;
170 BMVert *eve;
171 int i;
172
173 if (cd_dvert_offset == -1) {
174 return false;
175 }
176
177 i = bm->totvert;
178
179 *dvert_arr = MEM_new_array_uninitialized<MDeformVert *>(i, __func__);
180 *dvert_tot = i;
181
182 i = 0;
183 if (use_vert_sel) {
184 BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
185 (*dvert_arr)[i] = BM_elem_flag_test(eve, BM_ELEM_SELECT) ?
186 static_cast<MDeformVert *>(
187 BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset)) :
188 nullptr;
189 i++;
190 }
191 }
192 else {
193 BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
194 (*dvert_arr)[i] = static_cast<MDeformVert *>(
195 BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset));
196 i++;
197 }
198 }
199
200 return true;
201 }
202 if (!mesh->deform_verts().is_empty()) {
203 MutableSpan<MDeformVert> dverts = mesh->deform_verts_for_write();
204
205 *dvert_tot = mesh->verts_num;
206 *dvert_arr = MEM_new_array_uninitialized<MDeformVert *>(mesh->verts_num, __func__);
207
208 if (use_vert_sel) {
209 const bke::AttributeAccessor attributes = mesh->attributes();
210 const VArray<bool> select_vert = *attributes.lookup_or_default<bool>(
211 ".select_vert", bke::AttrDomain::Point, false);
212
213 for (int i = 0; i < mesh->verts_num; i++) {
214 (*dvert_arr)[i] = select_vert[i] ? &dverts[i] : nullptr;
215 }
216 }
217 else {
218 for (int i = 0; i < mesh->verts_num; i++) {
219 (*dvert_arr)[i] = &dverts[i];
220 }
221 }
222
223 return true;
224 }
225 return false;
226 }
227 case ID_LT: {
228 Lattice *lt = id_cast<Lattice *>(id);
229 lt = (lt->editlatt) ? lt->editlatt->latt : lt;
230
231 if (lt->dvert) {
232 BPoint *def = lt->def;
233 *dvert_tot = lt->pntsu * lt->pntsv * lt->pntsw;
234 *dvert_arr = MEM_new_array_uninitialized<MDeformVert *>((*dvert_tot), __func__);
235
236 if (use_vert_sel) {
237 for (int i = 0; i < *dvert_tot; i++) {
238 (*dvert_arr)[i] = (def->f1 & SELECT) ? &lt->dvert[i] : nullptr;
239 }
240 }
241 else {
242 for (int i = 0; i < *dvert_tot; i++) {
243 (*dvert_arr)[i] = lt->dvert + i;
244 }
245 }
246
247 return true;
248 }
249 return false;
250 }
251 case ID_GP: {
252 if (!current_frame) {
253 return false;
254 }
255 GreasePencil *grease_pencil = reinterpret_cast<GreasePencil *>(id);
256 bke::greasepencil::Layer *layer = grease_pencil->get_active_layer();
257 if (!layer) {
258 return false;
259 }
260 bke::greasepencil::Drawing *drawing = grease_pencil->get_editable_drawing_at(
261 *layer, *current_frame);
262 if (!drawing) {
263 return false;
264 }
266 MutableSpan<MDeformVert> dverts = curves.deform_verts_for_write();
267
268 if (!dverts.is_empty()) {
269 const int points_num = curves.points_num();
270 *dvert_tot = points_num;
271 *dvert_arr = MEM_new_array_uninitialized<MDeformVert *>(points_num, __func__);
272
273 for (int i = 0; i < points_num; i++) {
274 (*dvert_arr)[i] = &dverts[i];
275 }
276
277 return true;
278 }
279 return false;
280 }
281
282 default:
283 break;
284 }
285 }
286
287 return false;
288}
289
291 MDeformVert **dvert_array,
292 const int dvert_tot,
293 const bool *vgroup_validmap,
294 const int vgroup_tot)
295{
296 MDeformVert **dvert_array_all = nullptr;
297 int dvert_tot_all;
298
299 /* get an array of all verts, not only selected */
300 if (vgroup_parray_alloc(ob->data, &dvert_array_all, &dvert_tot_all, false) == false) {
301 BLI_assert(0);
302 return;
303 }
306 }
307
308 int flip_map_len;
309 const int *flip_map = BKE_object_defgroup_flip_map(ob, true, &flip_map_len);
310
311 for (int i_src = 0; i_src < dvert_tot; i_src++) {
312 if (dvert_array[i_src] != nullptr) {
313 /* its selected, check if its mirror exists */
314 int i_dst = ED_mesh_mirror_get_vert(ob, i_src);
315 if (i_dst != -1 && dvert_array_all[i_dst] != nullptr) {
316 /* we found a match! */
317 const MDeformVert *dv_src = dvert_array[i_src];
318 MDeformVert *dv_dst = dvert_array_all[i_dst];
319
321 dv_dst, dv_src, vgroup_validmap, vgroup_tot, flip_map, flip_map_len);
322
323 dvert_array[i_dst] = dvert_array_all[i_dst];
324 }
325 }
326 }
327
328 MEM_delete(flip_map);
329 MEM_delete(dvert_array_all);
330}
331
332void vgroup_parray_mirror_assign(Object *ob, MDeformVert **dvert_array, const int dvert_tot)
333{
334 MDeformVert **dvert_array_all = nullptr;
335 int dvert_tot_all;
336
337 /* get an array of all verts, not only selected */
338 if (vgroup_parray_alloc(ob->data, &dvert_array_all, &dvert_tot_all, false) == false) {
339 BLI_assert(0);
340 return;
341 }
342 BLI_assert(dvert_tot == dvert_tot_all);
345 }
346
347 for (int i = 0; i < dvert_tot; i++) {
348 if (dvert_array[i] == nullptr) {
349 /* its unselected, check if its mirror is */
350 int i_sel = ED_mesh_mirror_get_vert(ob, i);
351 if ((i_sel != -1) && (i_sel != i) && (dvert_array[i_sel])) {
352 /* we found a match! */
353 dvert_array[i] = dvert_array_all[i];
354 }
355 }
356 }
357
358 MEM_delete(dvert_array_all);
359}
360
362 const int dvert_tot,
363 const bool *vgroup_validmap,
364 const int vgroup_tot,
365 const float epsilon,
366 const bool keep_single)
367{
368 MDeformVert *dv;
369
370 for (int i = 0; i < dvert_tot; i++) {
371 /* in case its not selected */
372 if (!(dv = dvert_array[i])) {
373 continue;
374 }
375
376 int j = dv->totweight;
377
378 while (j--) {
379 MDeformWeight *dw;
380
381 if (keep_single && dv->totweight == 1) {
382 break;
383 }
384
385 dw = dv->dw + j;
386 if ((dw->def_nr < vgroup_tot) && vgroup_validmap[dw->def_nr]) {
387 if (dw->weight <= epsilon) {
389 }
390 }
391 }
392 }
393}
394
395bool vgroup_array_copy(Object *ob, Object *ob_from)
396{
397 MDeformVert **dvert_array_from = nullptr, **dvf;
398 MDeformVert **dvert_array = nullptr, **dv;
399 int dvert_tot_from;
400 int dvert_tot;
401 int i;
402 ListBaseT<bDeformGroup> *defbase_dst = BKE_object_defgroup_list_mutable(ob);
403 const ListBaseT<bDeformGroup> *defbase_src = BKE_object_defgroup_list(ob_from);
404
405 int defbase_tot_from = defbase_src->count();
406 int defbase_tot = defbase_dst->count();
407 bool new_vgroup = false;
408
409 BLI_assert(ob != ob_from);
410
411 if (ob->data == ob_from->data) {
412 return true;
413 }
414
415 /* In case we copy vgroup between two objects using same data,
416 * we only have to care about object side of things. */
417 if (ob->data != ob_from->data) {
418 vgroup_parray_alloc(ob_from->data, &dvert_array_from, &dvert_tot_from, false);
419 vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, false);
420
421 if ((dvert_array == nullptr) && (dvert_array_from != nullptr) &&
423 {
424 vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, false);
425 new_vgroup = true;
426 }
427
428 if (dvert_tot == 0 || (dvert_tot != dvert_tot_from) || dvert_array_from == nullptr ||
429 dvert_array == nullptr)
430 {
431 if (dvert_array) {
432 MEM_delete(dvert_array);
433 }
434 if (dvert_array_from) {
435 MEM_delete(dvert_array_from);
436 }
437
438 if (new_vgroup == true) {
439 /* free the newly added vgroup since it wasn't compatible */
441 }
442
443 /* if true: both are 0 and nothing needs changing, consider this a success */
444 return (dvert_tot == dvert_tot_from);
445 }
446 }
447
448 /* do the copy */
449 defbase_dst->free_no_destruct();
450 BLI_duplicatelist(defbase_dst, defbase_src);
452
453 if (defbase_tot_from < defbase_tot) {
454 /* correct vgroup indices because the number of vgroups is being reduced. */
455 Array<int> remap(defbase_tot + 1);
456 for (i = 0; i <= defbase_tot_from; i++) {
457 remap[i] = i;
458 }
459 for (; i <= defbase_tot; i++) {
460 remap[i] = 0; /* can't use these, so disable */
461 }
462
464 }
465
466 if (dvert_array_from != nullptr && dvert_array != nullptr) {
467 dvf = dvert_array_from;
468 dv = dvert_array;
469
470 for (i = 0; i < dvert_tot; i++, dvf++, dv++) {
471 MEM_SAFE_DELETE((*dv)->dw);
472 *(*dv) = *(*dvf);
473
474 if ((*dv)->dw) {
475 (*dv)->dw = MEM_dupalloc((*dv)->dw);
476 }
477 }
478
479 MEM_delete(dvert_array);
480 MEM_delete(dvert_array_from);
481 }
482
483 return true;
484}
485
487 const int dvert_tot,
488 float *dvert_weights,
489 const int def_nr)
490{
491 for (int i = 0; i < dvert_tot; i++) {
492 const MDeformVert *dv = dvert_array[i];
493 dvert_weights[i] = dv ? BKE_defvert_find_weight(dv, def_nr) : 0.0f;
494 }
495}
496
498 const int dvert_tot,
499 const float *dvert_weights,
500 const int def_nr,
501 const bool remove_zero)
502{
503 int i;
504
505 for (i = 0; i < dvert_tot; i++) {
506 MDeformVert *dv = dvert_array[i];
507 if (dv) {
508 if (dvert_weights[i] > 0.0f) {
509 MDeformWeight *dw = BKE_defvert_ensure_index(dv, def_nr);
510 BLI_assert(IN_RANGE_INCL(dvert_weights[i], 0.0f, 1.0f));
511 dw->weight = dvert_weights[i];
512 }
513 else {
514 MDeformWeight *dw = BKE_defvert_find_index(dv, def_nr);
515 if (dw) {
516 if (remove_zero) {
518 }
519 else {
520 dw->weight = 0.0f;
521 }
522 }
523 }
524 }
525 }
526}
527
528/* TODO: cache flip data to speedup calls within a loop. */
530 MDeformVert *dvert_dst,
531 MDeformVert *dvert_src,
532 const int def_nr)
533{
534 if (def_nr == -1) {
535 /* All vgroups, add groups where needed. */
536 int flip_map_len;
537 int *flip_map = BKE_object_defgroup_flip_map_unlocked(ob, true, &flip_map_len);
538 BKE_defvert_sync_mapped(dvert_dst, dvert_src, flip_map, flip_map_len, true);
539 MEM_delete(flip_map);
540 }
541 else {
542 /* Single vgroup. */
544 BKE_object_defgroup_flip_index(ob, def_nr, true));
545 if (dw) {
546 dw->weight = BKE_defvert_find_weight(dvert_src, def_nr);
547 }
548 }
549}
550
552 Object *ob, BMVert *eve, int def_nr, int vidx, const int cd_dvert_offset)
553{
554 Mesh *mesh = id_cast<Mesh *>(ob->data);
556 BMVert *eve_mirr;
557 bool use_topology = (mesh->editflag & ME_EDIT_MIRROR_TOPO) != 0;
558
559 eve_mirr = editbmesh_get_x_mirror_vert(ob, bm, eve, eve->co, vidx, use_topology);
560
561 if (eve_mirr && eve_mirr != eve) {
562 MDeformVert *dvert_src = static_cast<MDeformVert *>(
563 BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset));
564 MDeformVert *dvert_dst = static_cast<MDeformVert *>(
565 BM_ELEM_CD_GET_VOID_P(eve_mirr, cd_dvert_offset));
566 mesh_defvert_mirror_update_internal(ob, dvert_dst, dvert_src, def_nr);
567 }
568}
569
570static void mesh_defvert_mirror_update_ob(Object *ob, int def_nr, int vidx)
571{
572 int vidx_mirr;
573 Mesh *mesh = id_cast<Mesh *>(ob->data);
574 bool use_topology = (mesh->editflag & ME_EDIT_MIRROR_TOPO) != 0;
575
576 if (vidx == -1) {
577 return;
578 }
579
580 vidx_mirr = mesh_get_x_mirror_vert(ob, nullptr, vidx, use_topology);
581
582 MutableSpan<MDeformVert> dverts = mesh->deform_verts_for_write();
583 if ((vidx_mirr) >= 0 && (vidx_mirr != vidx)) {
584 MDeformVert *dvert_src = &dverts[vidx];
585 MDeformVert *dvert_dst = &dverts[vidx_mirr];
586 mesh_defvert_mirror_update_internal(ob, dvert_dst, dvert_src, def_nr);
587 }
588}
589
591{
592 Mesh *mesh = id_cast<Mesh *>(ob->data);
593 MDeformVert *dvert_act;
594
595 if (mesh->symmetry & ME_SYMMETRY_X) {
597 BMVert *eve_act;
598 dvert_act = ED_mesh_active_dvert_get_em(ob, &eve_act);
599 if (dvert_act) {
600 const int cd_dvert_offset = CustomData_get_offset(&bm->vdata, CD_MDEFORMVERT);
601 mesh_defvert_mirror_update_em(ob, eve_act, def_nr, -1, cd_dvert_offset);
602 }
603 }
604 else {
605 int v_act;
606 dvert_act = ED_mesh_active_dvert_get_ob(ob, &v_act);
607 if (dvert_act) {
608 mesh_defvert_mirror_update_ob(ob, def_nr, v_act);
609 }
610 }
611 }
612}
613
614static void vgroup_remove_weight(Object *ob, const int def_nr)
615{
616 MDeformVert *dvert_act;
617 MDeformWeight *dw;
618
619 dvert_act = ED_mesh_active_dvert_get_only(ob);
620
621 dw = BKE_defvert_find_index(dvert_act, def_nr);
622 BKE_defvert_remove_group(dvert_act, dw);
623}
624
626{
627 Mesh *mesh = id_cast<Mesh *>(ob->data);
628 BMVert *eve_act;
629 int v_act;
630 MDeformVert *dvert_act;
631 int subset_count, vgroup_tot;
632 const bool *vgroup_validmap;
633
635 dvert_act = ED_mesh_active_dvert_get_em(ob, &eve_act);
636 }
637 else {
638 dvert_act = ED_mesh_active_dvert_get_ob(ob, &v_act);
639 }
640
641 if (dvert_act == nullptr) {
642 return false;
643 }
644
646 ob, subset_type, &vgroup_tot, &subset_count);
647
648 bool *lock_flags = BKE_object_defgroup_lock_flags_get(ob, vgroup_tot);
649
650 if (lock_flags) {
652 *dvert_act, Span(vgroup_validmap, vgroup_tot), Span(lock_flags, vgroup_tot));
653 MEM_delete(lock_flags);
654 }
655 else {
656 BKE_defvert_normalize_subset(*dvert_act, Span(vgroup_validmap, vgroup_tot));
657 }
658
659 MEM_delete(vgroup_validmap);
660
661 if (mesh->symmetry & ME_SYMMETRY_X) {
663 const int cd_dvert_offset = CustomData_get_offset(&bm->vdata, CD_MDEFORMVERT);
664 mesh_defvert_mirror_update_em(ob, eve_act, -1, -1, cd_dvert_offset);
665 }
666 else {
667 mesh_defvert_mirror_update_ob(ob, -1, v_act);
668 }
669 }
670
671 return true;
672}
673
674static void vgroup_copy_active_to_sel(Object *ob, eVGroupSelect subset_type)
675{
676 Mesh *mesh = id_cast<Mesh *>(ob->data);
677 MDeformVert *dvert_act;
678 int i, vgroup_tot, subset_count;
679 const bool *vgroup_validmap = BKE_object_defgroup_subset_from_select_type(
680 ob, subset_type, &vgroup_tot, &subset_count);
681
683 BMIter iter;
684 BMVert *eve, *eve_act;
685 const int cd_dvert_offset = CustomData_get_offset(&bm->vdata, CD_MDEFORMVERT);
686
687 dvert_act = ED_mesh_active_dvert_get_em(ob, &eve_act);
688 if (dvert_act) {
689 BM_ITER_MESH_INDEX (eve, &iter, bm, BM_VERTS_OF_MESH, i) {
690 if (BM_elem_flag_test(eve, BM_ELEM_SELECT) && eve != eve_act) {
691 MDeformVert *dv = static_cast<MDeformVert *>(
692 BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset));
693 BKE_defvert_copy_subset(dv, dvert_act, vgroup_validmap, vgroup_tot);
694 if (mesh->symmetry & ME_SYMMETRY_X) {
695 mesh_defvert_mirror_update_em(ob, eve, -1, i, cd_dvert_offset);
696 }
697 }
698 }
699 }
700 }
701 else {
702 const bke::AttributeAccessor attributes = mesh->attributes();
703 const VArray<bool> select_vert = *attributes.lookup_or_default<bool>(
704 ".select_vert", bke::AttrDomain::Point, false);
705
706 int v_act;
707
708 dvert_act = ED_mesh_active_dvert_get_ob(ob, &v_act);
709 if (dvert_act) {
710 MutableSpan<MDeformVert> dverts = mesh->deform_verts_for_write();
711 for (i = 0; i < mesh->verts_num; i++) {
712 if (select_vert[i] && &dverts[i] != dvert_act) {
713 BKE_defvert_copy_subset(&dverts[i], dvert_act, vgroup_validmap, vgroup_tot);
714 if (mesh->symmetry & ME_SYMMETRY_X) {
716 }
717 }
718 }
719 }
720 }
721
722 MEM_delete(vgroup_validmap);
723}
724
726
727/* -------------------------------------------------------------------- */
730
732 {WT_VGROUP_ACTIVE, "ACTIVE", 0, N_("Active Group"), N_("The active Vertex Group")},
734 "BONE_SELECT",
735 0,
736 N_("Selected Pose Bones"),
737 N_("All Vertex Groups assigned to Selection")},
739 "BONE_DEFORM",
740 0,
741 N_("Deform Pose Bones"),
742 N_("All Vertex Groups assigned to Deform Bones")},
743 {WT_VGROUP_ALL, "ALL", 0, N_("All Groups"), N_("All Vertex Groups")},
744 {0, nullptr, 0, nullptr, nullptr},
745};
746
748 PointerRNA * /*ptr*/,
749 PropertyRNA * /*prop*/,
750 bool *r_free,
751 const uint selection_mask)
752{
753 Object *ob;
754 EnumPropertyItem *item = nullptr;
755 int totitem = 0;
756
757 if (C == nullptr) {
758 /* needed for docs and i18n tools */
760 }
761
762 ob = context_object(C);
763 if (selection_mask & (1 << WT_VGROUP_ACTIVE)) {
765 }
766
767 if (ob) {
769 if (selection_mask & (1 << WT_VGROUP_BONE_SELECT)) {
772 }
773 }
774
776 if (selection_mask & (1 << WT_VGROUP_BONE_DEFORM)) {
779 }
780 }
781 }
782
783 if (selection_mask & (1 << WT_VGROUP_ALL)) {
785 }
786
787 RNA_enum_item_end(&item, &totitem);
788 *r_free = true;
789
790 return item;
791}
792
794 PointerRNA *ptr,
795 PropertyRNA *prop,
796 bool *r_free)
797{
798 return vgroup_selection_itemf_helper(C, ptr, prop, r_free, WT_VGROUP_MASK_ALL);
799}
800
802 PointerRNA *ptr,
803 PropertyRNA *prop,
804 bool *r_free)
805{
807 C, ptr, prop, r_free, WT_VGROUP_MASK_ALL & ~(1 << WT_VGROUP_ACTIVE));
808}
809
811{
812 PropertyRNA *prop;
813
814 prop = RNA_def_enum(ot->srna,
815 "group_select_mode",
817 use_active ? WT_VGROUP_ACTIVE : WT_VGROUP_ALL,
818 "Subset",
819 "Define which subset of groups shall be used");
820
821 if (use_active) {
823 }
824 else {
826 }
828 ot->prop = prop;
829}
830
832
833/* -------------------------------------------------------------------- */
840
841/* for Mesh in Object mode */
842/* allows editmode for Lattice */
844 Object *ob, const int def_nr, const int vertnum, const float weight, const int assignmode)
845{
846 /* Add the vert to the deform group with the specified number. */
847 MDeformVert *dvert = nullptr;
848 int tot;
849
850 /* Get the vert. */
851 BKE_object_defgroup_array_get(ob->data, &dvert, &tot);
852
853 if (dvert == nullptr) {
854 return;
855 }
856
857 /* Check that vertnum is valid before trying to get the relevant dvert. */
858 if ((vertnum < 0) || (vertnum >= tot)) {
859 return;
860 }
861
862 MDeformVert *dv = &dvert[vertnum];
863 MDeformWeight *dw;
864
865 /* Lets first check to see if this vert is already in the weight group - if so lets update it. */
866 dw = BKE_defvert_find_index(dv, def_nr);
867
868 if (dw) {
869 switch (assignmode) {
870 case WEIGHT_REPLACE:
871 dw->weight = weight;
872 break;
873 case WEIGHT_ADD:
874 dw->weight += weight;
875 dw->weight = std::min(dw->weight, 1.0f);
876 break;
877 case WEIGHT_SUBTRACT:
878 dw->weight -= weight;
879 /* If the weight is zero or less than remove the vert from the deform group. */
880 if (dw->weight <= 0.0f) {
882 }
883 break;
884 }
885 }
886 else {
887 /* If the vert wasn't in the deform group then we must take a different form of action. */
888
889 switch (assignmode) {
890 case WEIGHT_SUBTRACT:
891 /* If we are subtracting then we don't need to do anything. */
892 return;
893
894 case WEIGHT_REPLACE:
895 case WEIGHT_ADD:
896 /* If we are doing an additive assignment, then we need to create the deform weight. */
897
898 /* We checked if the vertex was added before so no need to test again, simply add. */
899 BKE_defvert_add_index_notest(dv, def_nr, weight);
900 break;
901 }
902 }
903}
904
905void vgroup_vert_add(Object *ob, bDeformGroup *dg, int vertnum, float weight, int assignmode)
906{
907 /* add the vert to the deform group with the
908 * specified assign mode
909 */
910 const ListBaseT<bDeformGroup> *defbase = BKE_object_defgroup_list(ob);
911 const int def_nr = BLI_findindex(defbase, dg);
912
913 MDeformVert *dv = nullptr;
914 int tot;
915
916 /* get the deform group number, exit if
917 * it can't be found
918 */
919 if (def_nr != -1) {
920
921 /* if there's no deform verts then create some,
922 */
923 if (BKE_object_defgroup_array_get(ob->data, &dv, &tot) && dv == nullptr) {
925 }
926
927 /* call another function to do the work
928 */
929 vgroup_nr_vert_add(ob, def_nr, vertnum, weight, assignmode);
930 }
931}
932
933void vgroup_vert_remove(Object *ob, bDeformGroup *dg, int vertnum)
934{
935 /* This routine removes the vertex from the specified
936 * deform group.
937 */
938
939 /* TODO(@ideasman42): This is slow in a loop, better pass def_nr directly,
940 * but leave for later. */
941 const ListBaseT<bDeformGroup> *defbase = BKE_object_defgroup_list(ob);
942 const int def_nr = BLI_findindex(defbase, dg);
943
944 if (def_nr != -1) {
945 MDeformVert *dvert = nullptr;
946 int tot;
947
948 /* get the deform vertices corresponding to the
949 * vertnum
950 */
951 BKE_object_defgroup_array_get(ob->data, &dvert, &tot);
952
953 if (dvert) {
954 MDeformVert *dv = &dvert[vertnum];
955 MDeformWeight *dw;
956
957 dw = BKE_defvert_find_index(dv, def_nr);
958 BKE_defvert_remove_group(dv, dw); /* dw can be nullptr */
959 }
960 }
961}
962
963static float get_vert_def_nr(Object *ob, const int def_nr, const int vertnum)
964{
965 const MDeformVert *dv = nullptr;
966
967 /* get the deform vertices corresponding to the vertnum */
968 if (ob->type == OB_MESH) {
969 Mesh *mesh = id_cast<Mesh *>(ob->data);
970
972 const int cd_dvert_offset = CustomData_get_offset(&bm->vdata, CD_MDEFORMVERT);
973 /* warning, this lookup is _not_ fast */
974
975 if (cd_dvert_offset != -1 && vertnum < bm->totvert) {
976 BMVert *eve;
978 eve = BM_vert_at_index(bm, vertnum);
979 dv = static_cast<const MDeformVert *>(BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset));
980 }
981 else {
982 return 0.0f;
983 }
984 }
985 else {
986 const Span<MDeformVert> dverts = mesh->deform_verts();
987 if (!dverts.is_empty()) {
988 if (vertnum >= mesh->verts_num) {
989 return 0.0f;
990 }
991 dv = &dverts[vertnum];
992 }
993 }
994 }
995 else if (ob->type == OB_LATTICE) {
997
998 if (lt->dvert) {
999 if (vertnum >= lt->pntsu * lt->pntsv * lt->pntsw) {
1000 return 0.0f;
1001 }
1002 dv = &lt->dvert[vertnum];
1003 }
1004 }
1005
1006 if (dv) {
1007 MDeformWeight *dw = BKE_defvert_find_index(dv, def_nr);
1008 if (dw) {
1009 return dw->weight;
1010 }
1011 }
1012
1013 return -1;
1014}
1015
1016float vgroup_vert_weight(Object *ob, bDeformGroup *dg, int vertnum)
1017{
1018 const ListBaseT<bDeformGroup> *defbase = BKE_object_defgroup_list(ob);
1019 const int def_nr = BLI_findindex(defbase, dg);
1020
1021 if (def_nr == -1) {
1022 return -1;
1023 }
1024
1025 return get_vert_def_nr(ob, def_nr, vertnum);
1026}
1027
1028void vgroup_select_by_name(Object *ob, const char *name)
1029{
1030 /* NOTE: `actdef == 0` signals on painting to create a new one,
1031 * if a bone in pose-mode is selected. */
1033}
1034
1036
1037/* -------------------------------------------------------------------- */
1040
1042 const ToolSettings &tool_settings,
1043 const bDeformGroup *def_group,
1044 const bool select,
1045 Object &object)
1046{
1047 using namespace bke;
1048 using namespace ed::greasepencil;
1050 &tool_settings);
1051 GreasePencil *grease_pencil = id_cast<GreasePencil *>(object.data);
1052
1053 Vector<MutableDrawingInfo> drawings = retrieve_editable_drawings(scene, *grease_pencil);
1054 for (MutableDrawingInfo &info : drawings) {
1055 bke::CurvesGeometry &curves = info.drawing.strokes_for_write();
1056 ListBaseT<bDeformGroup> &vertex_group_names = curves.vertex_group_names;
1057
1058 const int def_nr = BKE_defgroup_name_index(&vertex_group_names, def_group->name);
1059 if (def_nr < 0) {
1060 /* No vertices assigned to the group in this drawing. */
1061 continue;
1062 }
1063
1064 const Span<MDeformVert> dverts = curves.deform_verts();
1065 if (dverts.is_empty()) {
1066 continue;
1067 }
1068
1070 curves, selection_domain, bke::AttrType::Bool);
1071 switch (selection_domain) {
1072 case AttrDomain::Point:
1073 threading::parallel_for(curves.points_range(), 4096, [&](const IndexRange range) {
1074 for (const int point_i : range) {
1075 if (BKE_defvert_find_index(&dverts[point_i], def_nr)) {
1076 selection.span.typed<bool>()[point_i] = select;
1077 }
1078 }
1079 });
1080 break;
1081 case AttrDomain::Curve: {
1082 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
1083 threading::parallel_for(curves.curves_range(), 1024, [&](const IndexRange range) {
1084 for (const int curve_i : range) {
1085 const IndexRange points = points_by_curve[curve_i];
1086 bool any_point_in_group = false;
1087 for (const int point_i : points) {
1088 if (BKE_defvert_find_index(&dverts[point_i], def_nr)) {
1089 any_point_in_group = true;
1090 break;
1091 }
1092 }
1093 if (any_point_in_group) {
1094 selection.span.typed<bool>()[curve_i] = select;
1095 }
1096 }
1097 });
1098 break;
1099 }
1100 default:
1102 break;
1103 }
1104 selection.finish();
1105 }
1106
1108}
1109
1110/* only in editmode */
1111static void vgroup_select_verts(const ToolSettings &tool_settings,
1112 Object *ob,
1113 Scene &scene,
1114 int select)
1115{
1116 const int def_nr = BKE_object_defgroup_active_index_get(ob) - 1;
1117
1118 const ListBaseT<bDeformGroup> *defbase = BKE_object_defgroup_list(ob);
1119 const bDeformGroup *def_group = static_cast<bDeformGroup *>(BLI_findlink(defbase, def_nr));
1120 if (!def_group) {
1121 return;
1122 }
1123
1124 if (ob->type == OB_MESH) {
1125 Mesh *mesh = id_cast<Mesh *>(ob->data);
1126
1128 const int cd_dvert_offset = CustomData_get_offset(&bm->vdata, CD_MDEFORMVERT);
1129
1130 if (cd_dvert_offset != -1) {
1131 BMIter iter;
1132 BMVert *eve;
1133
1134 BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
1135 if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN)) {
1136 MDeformVert *dv = static_cast<MDeformVert *>(
1137 BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset));
1138 if (BKE_defvert_find_index(dv, def_nr)) {
1140 }
1141 }
1142 }
1143
1144 /* This has to be called, because this function operates on vertices only.
1145 * Vertices to edges/faces. */
1146 EDBM_select_flush_from_verts(bm, mesh->runtime->edit_mesh->selectmode, select);
1147
1149 }
1150 }
1151 else {
1152 const Span<MDeformVert> dverts = mesh->deform_verts();
1153 if (!dverts.is_empty()) {
1154 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
1155 const VArray<bool> hide_vert = *attributes.lookup_or_default<bool>(
1156 ".hide_vert", bke::AttrDomain::Point, false);
1157 bke::SpanAttributeWriter<bool> select_vert =
1158 attributes.lookup_or_add_for_write_only_span<bool>(".select_vert",
1160
1161 for (const int i : select_vert.span.index_range()) {
1162 if (!hide_vert[i]) {
1163 if (BKE_defvert_find_index(&dverts[i], def_nr)) {
1164 select_vert.span[i] = select;
1165 }
1166 }
1167 }
1168
1169 select_vert.finish();
1171 }
1172 }
1173 }
1174 else if (ob->type == OB_LATTICE) {
1175 Lattice *lt = vgroup_edit_lattice(ob);
1176
1177 if (lt->dvert) {
1178 MDeformVert *dv;
1179 BPoint *bp, *actbp = BKE_lattice_active_point_get(lt);
1180 int a, tot;
1181
1182 dv = lt->dvert;
1183
1184 tot = lt->pntsu * lt->pntsv * lt->pntsw;
1185 for (a = 0, bp = lt->def; a < tot; a++, bp++, dv++) {
1186 if (BKE_defvert_find_index(dv, def_nr)) {
1187 if (select) {
1188 bp->f1 |= SELECT;
1189 }
1190 else {
1191 bp->f1 &= ~SELECT;
1192 if (actbp && bp == actbp) {
1193 lt->actbp = LT_ACTBP_NONE;
1194 }
1195 }
1196 }
1197 }
1198 }
1199 }
1200 else if (ob->type == OB_GREASE_PENCIL) {
1201 vgroup_grease_pencil_select_verts(scene, tool_settings, def_group, select, *ob);
1202 }
1203}
1204
1205static void vgroup_duplicate(Object *ob)
1206{
1207 bDeformGroup *dg, *cdg;
1208 char name[sizeof(dg->name)];
1209 MDeformWeight *dw_org, *dw_cpy;
1210 MDeformVert **dvert_array = nullptr;
1211 int i, idg, icdg, dvert_tot = 0;
1212
1213 ListBaseT<bDeformGroup> *defbase = BKE_object_defgroup_list_mutable(ob);
1214
1215 dg = static_cast<bDeformGroup *>(
1217 if (!dg) {
1218 return;
1219 }
1220
1221 if (!strstr(dg->name, "_copy")) {
1222 SNPRINTF_UTF8(name, "%s_copy", dg->name);
1223 }
1224 else {
1225 STRNCPY_UTF8(name, dg->name);
1226 }
1227
1228 cdg = BKE_defgroup_duplicate(dg);
1229 STRNCPY_UTF8(cdg->name, name);
1231
1232 BLI_addtail(defbase, cdg);
1233
1235 BKE_object_defgroup_active_index_set(ob, defbase->count());
1237
1238 /* TODO(@ideasman42): we might want to allow only copy selected verts here? */
1239 vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, false);
1240
1241 if (dvert_array) {
1242 for (i = 0; i < dvert_tot; i++) {
1243 MDeformVert *dv = dvert_array[i];
1244 dw_org = BKE_defvert_find_index(dv, idg);
1245 if (dw_org) {
1246 /* #BKE_defvert_ensure_index re-allocates org so need to store the weight first. */
1247 const float weight = dw_org->weight;
1248 dw_cpy = BKE_defvert_ensure_index(dv, icdg);
1249 dw_cpy->weight = weight;
1250 }
1251 }
1252
1253 MEM_delete(dvert_array);
1254 }
1255}
1256
1257static bool vgroup_normalize(Object *ob)
1258{
1259 MDeformWeight *dw;
1260 MDeformVert *dv, **dvert_array = nullptr;
1261 int dvert_tot = 0;
1262 const int def_nr = BKE_object_defgroup_active_index_get(ob) - 1;
1263
1264 const bool use_vert_sel = vertex_group_use_vert_sel(ob);
1265
1266 const ListBaseT<bDeformGroup> *defbase = BKE_object_defgroup_list(ob);
1267 if (!BLI_findlink(defbase, def_nr)) {
1268 return false;
1269 }
1270
1271 vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
1272
1273 if (dvert_array) {
1274 float weight_max = 0.0f;
1275
1276 for (int i = 0; i < dvert_tot; i++) {
1277
1278 /* in case its not selected */
1279 if (!(dv = dvert_array[i])) {
1280 continue;
1281 }
1282
1283 dw = BKE_defvert_find_index(dv, def_nr);
1284 if (dw) {
1285 weight_max = max_ff(dw->weight, weight_max);
1286 }
1287 }
1288
1289 if (weight_max > 0.0f) {
1290 for (int i = 0; i < dvert_tot; i++) {
1291
1292 /* in case its not selected */
1293 if (!(dv = dvert_array[i])) {
1294 continue;
1295 }
1296
1297 dw = BKE_defvert_find_index(dv, def_nr);
1298 if (dw) {
1299 dw->weight /= weight_max;
1300
1301 /* in case of division errors with very low weights */
1302 CLAMP(dw->weight, 0.0f, 1.0f);
1303 }
1304 }
1305 }
1306
1307 MEM_delete(dvert_array);
1308
1309 return true;
1310 }
1311
1312 return false;
1313}
1314
1316 const bool *vgroup_validmap,
1317 const int vgroup_tot,
1318 const int /*subset_count*/,
1319 const float offset,
1320 const float gain)
1321{
1322 MDeformWeight *dw;
1323 MDeformVert *dv, **dvert_array = nullptr;
1324 int dvert_tot = 0;
1325
1326 const bool use_vert_sel = vertex_group_use_vert_sel(ob);
1327 const bool use_mirror = (ob->type == OB_MESH) ?
1328 ((id_cast<Mesh *>(ob->data))->symmetry & ME_SYMMETRY_X) != 0 :
1329 false;
1330
1331 vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
1332
1333 if (dvert_array) {
1334
1335 for (int i = 0; i < dvert_tot; i++) {
1336 /* in case its not selected */
1337 if (!(dv = dvert_array[i])) {
1338 continue;
1339 }
1340
1341 int j = vgroup_tot;
1342 while (j--) {
1343 if (vgroup_validmap[j]) {
1344 dw = BKE_defvert_find_index(dv, j);
1345 if (dw) {
1346 dw->weight = gain * (dw->weight + offset);
1347
1348 CLAMP(dw->weight, 0.0f, 1.0f);
1349 }
1350 }
1351 }
1352 }
1353
1354 if (use_mirror && use_vert_sel) {
1355 vgroup_parray_mirror_sync(ob, dvert_array, dvert_tot, vgroup_validmap, vgroup_tot);
1356 }
1357
1358 MEM_delete(dvert_array);
1359 }
1360}
1361
1381 const bool *vgroup_validmap,
1382 const int vgroup_tot,
1383 const bool lock_active,
1384 const bool soft_lock_active,
1385 ReportList *reports,
1386 std::optional<int> current_frame = {})
1387{
1389
1390 const int def_nr = BKE_object_defgroup_active_index_get(ob) - 1;
1391 const bool use_vert_sel = vertex_group_use_vert_sel(ob);
1392 BLI_assert(def_nr < vgroup_tot);
1393
1394 MDeformVert **dvert_array = nullptr;
1395 int dvert_tot = 0;
1396 vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, use_vert_sel, current_frame);
1397
1398 if (!dvert_array) {
1399 return false;
1400 }
1401
1402 Span<bool> subset_flags = Span(vgroup_validmap, vgroup_tot);
1403
1404 MutableSpan<bool> lock_flags = {};
1405 bool *lock_flags_array = BKE_object_defgroup_lock_flags_get(ob, vgroup_tot);
1406 if (lock_flags_array) {
1407 lock_flags = MutableSpan(lock_flags_array, vgroup_tot);
1408 }
1409 else if (lock_active) {
1410 lock_flags_array = MEM_new_array_uninitialized<bool>(vgroup_tot, "lock_flags_array");
1411 std::memset(lock_flags_array, 0, vgroup_tot); /* Clear to false. */
1412 lock_flags = MutableSpan(lock_flags_array, vgroup_tot);
1413 }
1414 if (lock_active) {
1415 lock_flags[def_nr] = true;
1416 }
1417
1418 Vector<bool> soft_lock_flags;
1419 if (soft_lock_active && !lock_active) {
1420 soft_lock_flags = Vector(vgroup_tot, false);
1421 soft_lock_flags[def_nr] = true;
1422 }
1423
1424 const bool all_locked = !lock_flags.is_empty() && !lock_flags.contains(false);
1425 if (all_locked) {
1426 BKE_report(reports, RPT_ERROR, "All groups are locked");
1427 }
1428 else {
1429 /* Normalize. */
1430 for (int i = 0; i < dvert_tot; i++) {
1431 MDeformVert *dv = dvert_array[i];
1432 /* in case its not selected */
1433 if (dv) {
1434 BKE_defvert_normalize_ex(*dv, subset_flags, lock_flags, soft_lock_flags);
1435 }
1436 }
1437 }
1438
1439 MEM_delete(dvert_array);
1440 MEM_SAFE_DELETE(lock_flags_array);
1441
1442 return !all_locked;
1443}
1444
1454 const int def_nr,
1455 const bool soft_lock_active,
1456 ReportList *reports,
1457 std::optional<int> current_frame = {})
1458{
1460 const int defgroup_tot = BKE_object_defgroup_count(ob);
1461 bool *defgroup_validmap = BKE_object_defgroup_validmap_get(ob, defgroup_tot);
1462
1463 /* Only auto-normalize if the active group is bone-deforming. */
1464 if (defgroup_validmap[def_nr] == true) {
1465 int subset_count, vgroup_tot;
1466 const bool *vgroup_validmap = BKE_object_defgroup_subset_from_select_type(
1467 ob, WT_VGROUP_BONE_DEFORM, &vgroup_tot, &subset_count);
1468
1469 vgroup_normalize_all(
1470 ob, vgroup_validmap, vgroup_tot, false, soft_lock_active, reports, current_frame);
1471 MEM_SAFE_DELETE(vgroup_validmap);
1472 }
1473
1474 MEM_SAFE_DELETE(defgroup_validmap);
1475}
1476
1477enum {
1482};
1483
1486 "TOGGLE",
1487 0,
1488 "Toggle",
1489 "Unlock all vertex groups if there is at least one locked group, lock all in other case"},
1490 {VGROUP_LOCK, "LOCK", 0, "Lock", "Lock all vertex groups"},
1491 {VGROUP_UNLOCK, "UNLOCK", 0, "Unlock", "Unlock all vertex groups"},
1492 {VGROUP_INVERT, "INVERT", 0, "Invert", "Invert the lock state of all vertex groups"},
1493 {0, nullptr, 0, nullptr, nullptr},
1494};
1495
1496enum {
1501};
1502
1504 {VGROUP_MASK_ALL, "ALL", 0, "All", "Apply action to all vertex groups"},
1505 {VGROUP_MASK_SELECTED, "SELECTED", 0, "Selected", "Apply to selected vertex groups"},
1506 {VGROUP_MASK_UNSELECTED, "UNSELECTED", 0, "Unselected", "Apply to unselected vertex groups"},
1508 "INVERT_UNSELECTED",
1509 0,
1510 "Invert Unselected",
1511 "Apply the opposite of Lock/Unlock to unselected vertex groups"},
1512 {0, nullptr, 0, nullptr, nullptr},
1513};
1514
1516{
1517 int sel_count = 0, defbase_tot = BKE_object_defgroup_count(ob);
1518 bool *mask;
1519
1520 if (ob->mode & OB_MODE_WEIGHT_PAINT) {
1521 mask = BKE_object_defgroup_selected_get(ob, defbase_tot, &sel_count);
1522
1523 /* Mirror the selection if X Mirror is enabled. */
1525
1527 BKE_object_defgroup_mirror_selection(ob, defbase_tot, mask, mask, &sel_count);
1528 }
1529 }
1530 else {
1531 mask = MEM_new_array_zeroed<bool>(defbase_tot, __func__);
1532 }
1533
1534 const int actdef = BKE_object_defgroup_active_index_get(ob);
1535 if (sel_count == 0 && actdef >= 1 && actdef <= defbase_tot) {
1536 mask[actdef - 1] = true;
1537 }
1538
1539 return mask;
1540}
1541
1542static void vgroup_lock_all(Object *ob, int action, int mask)
1543{
1544 bDeformGroup *dg;
1545 bool *selected = nullptr;
1546 int i;
1547
1548 if (mask != VGROUP_MASK_ALL) {
1549 selected = vgroup_selected_get(ob);
1550 }
1551 const ListBaseT<bDeformGroup> *defbase = BKE_object_defgroup_list(ob);
1552
1553 if (action == VGROUP_TOGGLE) {
1554 action = VGROUP_LOCK;
1555
1556 for (dg = defbase->first(), i = 0; dg; dg = dg->next, i++) {
1557 switch (mask) {
1560 if (!selected[i]) {
1561 continue;
1562 }
1563 break;
1565 if (selected[i]) {
1566 continue;
1567 }
1568 break;
1569 default:
1570 break;
1571 }
1572
1573 if (dg->flag & DG_LOCK_WEIGHT) {
1574 action = VGROUP_UNLOCK;
1575 break;
1576 }
1577 }
1578 }
1579
1580 for (dg = defbase->first(), i = 0; dg; dg = dg->next, i++) {
1581 switch (mask) {
1583 if (!selected[i]) {
1584 continue;
1585 }
1586 break;
1588 if (selected[i]) {
1589 continue;
1590 }
1591 break;
1592 default:
1593 break;
1594 }
1595
1596 switch (action) {
1597 case VGROUP_LOCK:
1598 dg->flag |= DG_LOCK_WEIGHT;
1599 break;
1600 case VGROUP_UNLOCK:
1601 dg->flag &= ~DG_LOCK_WEIGHT;
1602 break;
1603 case VGROUP_INVERT:
1604 dg->flag ^= DG_LOCK_WEIGHT;
1605 break;
1606 }
1607
1608 if (mask == VGROUP_MASK_INVERT_UNSELECTED && !selected[i]) {
1609 dg->flag ^= DG_LOCK_WEIGHT;
1610 }
1611 }
1612
1613 if (selected) {
1614 MEM_delete(selected);
1615 }
1616}
1617
1619 const bool *vgroup_validmap,
1620 const int vgroup_tot,
1621 const int /*subset_count*/,
1622 const bool auto_assign,
1623 const bool auto_remove)
1624{
1625 MDeformWeight *dw;
1626 MDeformVert *dv, **dvert_array = nullptr;
1627 int dvert_tot = 0;
1628 const bool use_vert_sel = vertex_group_use_vert_sel(ob);
1629 const bool use_mirror = (ob->type == OB_MESH) ?
1630 ((id_cast<Mesh *>(ob->data))->symmetry & ME_SYMMETRY_X) != 0 :
1631 false;
1632
1633 vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
1634
1635 if (dvert_array) {
1636 for (int i = 0; i < dvert_tot; i++) {
1637 /* in case its not selected */
1638 if (!(dv = dvert_array[i])) {
1639 continue;
1640 }
1641
1642 int j = vgroup_tot;
1643 while (j--) {
1644
1645 if (vgroup_validmap[j]) {
1646 if (auto_assign) {
1647 dw = BKE_defvert_ensure_index(dv, j);
1648 }
1649 else {
1650 dw = BKE_defvert_find_index(dv, j);
1651 }
1652
1653 if (dw) {
1654 dw->weight = 1.0f - dw->weight;
1655 CLAMP(dw->weight, 0.0f, 1.0f);
1656 }
1657 }
1658 }
1659 }
1660
1661 if (use_mirror && use_vert_sel) {
1662 vgroup_parray_mirror_sync(ob, dvert_array, dvert_tot, vgroup_validmap, vgroup_tot);
1663 }
1664
1665 if (auto_remove) {
1666 vgroup_parray_remove_zero(dvert_array, dvert_tot, vgroup_validmap, vgroup_tot, 0.0f, false);
1667 }
1668
1669 MEM_delete(dvert_array);
1670 }
1671}
1672
1674 const bool *vgroup_validmap,
1675 const int vgroup_tot,
1676 const int subset_count,
1677 const float fac,
1678 const int repeat,
1679 const float fac_expand)
1680{
1681 /* Caller must check, while it's not an error it will do nothing. */
1682 BLI_assert(vgroup_tot > 0 && subset_count > 0);
1683
1684 const float ifac = 1.0f - fac;
1685 MDeformVert **dvert_array = nullptr;
1686 int dvert_tot = 0;
1687 Array<int, 32> vgroup_subset_map(subset_count);
1688 Array<float, 32> vgroup_subset_weights(subset_count);
1689 const bool use_mirror = (ob->type == OB_MESH) ?
1690 ((id_cast<Mesh *>(ob->data))->symmetry & ME_SYMMETRY_X) != 0 :
1691 false;
1692 const bool use_select = vertex_group_use_vert_sel(ob);
1693 const bool use_hide = use_select;
1694
1695 const int expand_sign = signum_i(fac_expand);
1696 const float expand = fabsf(fac_expand);
1697 const float iexpand = 1.0f - expand;
1698
1699 Mesh *mesh = id_cast<Mesh *>(ob->data);
1701
1702 float *weight_accum_prev;
1703 float *weight_accum_curr;
1704
1705 uint subset_index;
1706
1707 /* vertex indices that will be smoothed, (only to avoid iterating over verts that do nothing) */
1708 uint *verts_used;
1709 STACK_DECLARE(verts_used);
1710
1711 BKE_object_defgroup_subset_to_index_array(vgroup_validmap, vgroup_tot, vgroup_subset_map.data());
1712 vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, false);
1713 vgroup_subset_weights.fill(0.0f);
1714
1715 Array<int> vert_to_edge_offsets;
1716 Array<int> vert_to_edge_indices;
1717 GroupedSpan<int> emap;
1718 if (bm) {
1721 }
1722 else {
1724 mesh->edges(), mesh->verts_num, vert_to_edge_offsets, vert_to_edge_indices);
1725 }
1726
1727 weight_accum_prev = MEM_new_array_uninitialized<float>(dvert_tot, __func__);
1728 weight_accum_curr = MEM_new_array_uninitialized<float>(dvert_tot, __func__);
1729
1730 verts_used = MEM_new_array_uninitialized<uint>(dvert_tot, __func__);
1731 STACK_INIT(verts_used, dvert_tot);
1732
1733#define IS_BM_VERT_READ(v) (use_hide ? (BM_elem_flag_test(v, BM_ELEM_HIDDEN) == 0) : true)
1734#define IS_BM_VERT_WRITE(v) (use_select ? (BM_elem_flag_test(v, BM_ELEM_SELECT) != 0) : true)
1735
1736 VArray<bool> hide_vert;
1737 if (mesh && use_hide) {
1738 hide_vert = *mesh->attributes().lookup_or_default<bool>(
1739 ".hide_vert", bke::AttrDomain::Point, false);
1740 }
1741 else {
1742 hide_vert = VArray<bool>::from_single(false, dvert_tot);
1743 }
1744
1745 VArray<bool> select_vert;
1746 if (mesh && use_select) {
1747 select_vert = *mesh->attributes().lookup_or_default<bool>(
1748 ".select_vert", bke::AttrDomain::Point, false);
1749 }
1750 else {
1751 select_vert = VArray<bool>::from_single(true, dvert_tot);
1752 }
1753
1754 /* initialize used verts */
1755 if (bm) {
1756 for (int i = 0; i < dvert_tot; i++) {
1757 BMVert *v = BM_vert_at_index(bm, i);
1758 if (IS_BM_VERT_WRITE(v)) {
1759 BMIter eiter;
1760 BMEdge *e;
1761 BM_ITER_ELEM (e, &eiter, v, BM_EDGES_OF_VERT) {
1762 BMVert *v_other = BM_edge_other_vert(e, v);
1763 if (IS_BM_VERT_READ(v_other)) {
1764 STACK_PUSH(verts_used, i);
1765 break;
1766 }
1767 }
1768 }
1769 }
1770 }
1771 else {
1772 const Span<int2> edges = mesh->edges();
1773 for (int i = 0; i < dvert_tot; i++) {
1774 if (select_vert[i]) {
1775 for (int j = 0; j < emap[i].size(); j++) {
1776 const int2 &edge = edges[emap[i][j]];
1777 const int i_other = (edge[0] == i) ? edge[1] : edge[0];
1778 if (!hide_vert[i_other]) {
1779 STACK_PUSH(verts_used, i);
1780 break;
1781 }
1782 }
1783 }
1784 }
1785 }
1786
1787 for (subset_index = 0; subset_index < subset_count; subset_index++) {
1788 const int def_nr = vgroup_subset_map[subset_index];
1789 int iter;
1790
1792 (const MDeformVert **)dvert_array, dvert_tot, weight_accum_prev, def_nr);
1793 memcpy(weight_accum_curr, weight_accum_prev, sizeof(*weight_accum_curr) * dvert_tot);
1794
1795 for (iter = 0; iter < repeat; iter++) {
1796 uint *vi_step, *vi_end = verts_used + STACK_SIZE(verts_used);
1797
1798 /* avoid looping over all verts */
1799 // for (i = 0; i < dvert_tot; i++)
1800 for (vi_step = verts_used; vi_step != vi_end; vi_step++) {
1801 const uint i = *vi_step;
1802 float weight_tot = 0.0f;
1803 float weight = 0.0f;
1804
1805#define WEIGHT_ACCUMULATE \
1806 { \
1807 float weight_other = weight_accum_prev[i_other]; \
1808 float tot_factor = 1.0f; \
1809 if (expand_sign == 1) { /* expand */ \
1810 if (weight_other < weight_accum_prev[i]) { \
1811 weight_other = (weight_accum_prev[i] * expand) + (weight_other * iexpand); \
1812 tot_factor = iexpand; \
1813 } \
1814 } \
1815 else if (expand_sign == -1) { /* contract */ \
1816 if (weight_other > weight_accum_prev[i]) { \
1817 weight_other = (weight_accum_prev[i] * expand) + (weight_other * iexpand); \
1818 tot_factor = iexpand; \
1819 } \
1820 } \
1821 weight += tot_factor * weight_other; \
1822 weight_tot += tot_factor; \
1823 } \
1824 ((void)0)
1825
1826 if (bm) {
1827 BMVert *v = BM_vert_at_index(bm, i);
1828 BMIter eiter;
1829 BMEdge *e;
1830
1831 /* checked already */
1833
1834 BM_ITER_ELEM (e, &eiter, v, BM_EDGES_OF_VERT) {
1835 BMVert *v_other = BM_edge_other_vert(e, v);
1836 if (IS_BM_VERT_READ(v_other)) {
1837 const int i_other = BM_elem_index_get(v_other);
1838
1840 }
1841 }
1842 }
1843 else {
1844 int j;
1845 const Span<int2> edges = mesh->edges();
1846
1847 /* checked already */
1848 BLI_assert(select_vert[i]);
1849
1850 for (j = 0; j < emap[i].size(); j++) {
1851 const int2 &edge = edges[emap[i][j]];
1852 const int i_other = (edge[0] == i ? edge[1] : edge[0]);
1853 if (!hide_vert[i_other]) {
1855 }
1856 }
1857 }
1858
1859#undef WEIGHT_ACCUMULATE
1860
1861 if (weight_tot != 0.0f) {
1862 weight /= weight_tot;
1863 weight = (weight_accum_prev[i] * ifac) + (weight * fac);
1864
1865 /* should be within range, just clamp because of float precision */
1866 CLAMP(weight, 0.0f, 1.0f);
1867 weight_accum_curr[i] = weight;
1868 }
1869 }
1870
1871 std::swap(weight_accum_curr, weight_accum_prev);
1872 }
1873
1874 vgroup_parray_from_weight_array(dvert_array, dvert_tot, weight_accum_prev, def_nr, true);
1875 }
1876
1877#undef IS_BM_VERT_READ
1878#undef IS_BM_VERT_WRITE
1879
1880 MEM_delete(weight_accum_curr);
1881 MEM_delete(weight_accum_prev);
1882 MEM_delete(verts_used);
1883
1884 if (dvert_array) {
1885 MEM_delete(dvert_array);
1886 }
1887
1888 /* not so efficient to get 'dvert_array' again just so unselected verts are nullptr'd */
1889 if (use_mirror) {
1890 vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, true);
1891 vgroup_parray_mirror_sync(ob, dvert_array, dvert_tot, vgroup_validmap, vgroup_tot);
1892 if (dvert_array) {
1893 MEM_delete(dvert_array);
1894 }
1895 }
1896}
1897
1898static int inv_cmp_mdef_vert_weights(const void *a1, const void *a2)
1899{
1900 /* #qsort sorts in ascending order. We want descending order to save a #memcpy
1901 * so this compare function is inverted from the standard greater than comparison #qsort needs.
1902 * A normal compare function is called with two pointer arguments and should return an integer
1903 * less than, equal to, or greater than zero corresponding to whether its first argument is
1904 * considered less than, equal to, or greater than its second argument.
1905 * This does the opposite. */
1906 const MDeformWeight *dw1 = static_cast<const MDeformWeight *>(a1);
1907 const MDeformWeight *dw2 = static_cast<const MDeformWeight *>(a2);
1908
1909 if (dw1->weight < dw2->weight) {
1910 return 1;
1911 }
1912 if (dw1->weight > dw2->weight) {
1913 return -1;
1914 }
1915
1916 /* Compare address for stable sort algorithm. */
1917 if (&dw1 < &dw2) {
1918 return 1;
1919 }
1920 if (&dw1 > &dw2) {
1921 return -1;
1922 }
1923 return 0;
1924}
1925
1926/* Used for limiting the number of influencing bones per vertex when exporting
1927 * skinned meshes. if all_deform_weights is True, limit all deform modifiers
1928 * to max_weights regardless of type, otherwise,
1929 * only limit the number of influencing bones per vertex. */
1931 const bool *vgroup_validmap,
1932 const int vgroup_tot,
1933 const int subset_count,
1934 const int max_weights)
1935{
1936 MDeformVert *dv, **dvert_array = nullptr;
1937 int i, dvert_tot = 0;
1938 const bool use_vert_sel = vertex_group_use_vert_sel(ob);
1939 int remove_tot = 0;
1940
1941 vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
1942
1943 if (dvert_array) {
1944 int num_to_drop = 0;
1945
1946 for (i = 0; i < dvert_tot; i++) {
1947
1948 MDeformWeight *dw_temp;
1949 int bone_count = 0, non_bone_count = 0;
1950 int j;
1951
1952 /* in case its not selected */
1953 if (!(dv = dvert_array[i])) {
1954 continue;
1955 }
1956
1957 num_to_drop = subset_count - max_weights;
1958
1959 /* first check if we even need to test further */
1960 if (num_to_drop > 0) {
1961 /* re-pack dw array so that non-bone weights are first, bone-weighted verts at end
1962 * sort the tail, then copy only the truncated array back to dv->dw */
1964 bone_count = 0;
1965 non_bone_count = 0;
1966 for (j = 0; j < dv->totweight; j++) {
1967 if (LIKELY(dv->dw[j].def_nr < vgroup_tot) && vgroup_validmap[dv->dw[j].def_nr]) {
1968 dw_temp[dv->totweight - 1 - bone_count] = dv->dw[j];
1969 bone_count += 1;
1970 }
1971 else {
1972 dw_temp[non_bone_count] = dv->dw[j];
1973 non_bone_count += 1;
1974 }
1975 }
1976 BLI_assert(bone_count + non_bone_count == dv->totweight);
1977 num_to_drop = bone_count - max_weights;
1978 if (num_to_drop > 0) {
1979 qsort(&dw_temp[non_bone_count],
1980 bone_count,
1981 sizeof(MDeformWeight),
1983 dv->totweight -= num_to_drop;
1984 /* Do we want to clean/normalize here? */
1985 MEM_delete(dv->dw);
1986 dv->dw = static_cast<MDeformWeight *>(
1987 MEM_realloc_uninitialized(dw_temp, sizeof(MDeformWeight) * dv->totweight));
1988 remove_tot += num_to_drop;
1989 }
1990 else {
1991 MEM_delete(dw_temp);
1992 }
1993 }
1994 }
1995 MEM_delete(dvert_array);
1996 }
1997
1998 return remove_tot;
1999}
2000
2002 const bool *vgroup_validmap,
2003 const int vgroup_tot,
2004 const int /*subset_count*/,
2005 const float epsilon,
2006 const bool keep_single)
2007{
2008 MDeformVert **dvert_array = nullptr;
2009 int dvert_tot = 0;
2010 const bool use_vert_sel = vertex_group_use_vert_sel(ob);
2011 const bool use_mirror = (ob->type == OB_MESH) ?
2012 ((id_cast<Mesh *>(ob->data))->symmetry & ME_SYMMETRY_X) != 0 :
2013 false;
2014
2015 vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
2016
2017 if (dvert_array) {
2018 if (use_mirror && use_vert_sel) {
2019 /* correct behavior in this case isn't well defined
2020 * for now assume both sides are mirrored correctly,
2021 * so cleaning one side also cleans the other */
2022 vgroup_parray_mirror_assign(ob, dvert_array, dvert_tot);
2023 }
2024
2026 dvert_array, dvert_tot, vgroup_validmap, vgroup_tot, epsilon, keep_single);
2027
2028 MEM_delete(dvert_array);
2029 }
2030}
2031
2033 const bool *vgroup_validmap,
2034 const int vgroup_tot,
2035 const int /*subset_count*/,
2036 const int steps)
2037{
2038 MDeformVert **dvert_array = nullptr;
2039 int dvert_tot = 0;
2040 const bool use_vert_sel = vertex_group_use_vert_sel(ob);
2041 const bool use_mirror = (ob->type == OB_MESH) ?
2042 ((id_cast<Mesh *>(ob->data))->symmetry & ME_SYMMETRY_X) != 0 :
2043 false;
2044 vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, use_vert_sel);
2045
2046 if (dvert_array) {
2047 const float steps_fl = steps;
2048 MDeformVert *dv;
2049
2050 if (use_mirror && use_vert_sel) {
2051 vgroup_parray_mirror_assign(ob, dvert_array, dvert_tot);
2052 }
2053
2054 for (int i = 0; i < dvert_tot; i++) {
2055 MDeformWeight *dw;
2056
2057 /* in case its not selected */
2058 if (!(dv = dvert_array[i])) {
2059 continue;
2060 }
2061
2062 int j;
2063 for (j = 0, dw = dv->dw; j < dv->totweight; j++, dw++) {
2064 if ((dw->def_nr < vgroup_tot) && vgroup_validmap[dw->def_nr]) {
2065 dw->weight = floorf((dw->weight * steps_fl) + 0.5f) / steps_fl;
2066 CLAMP(dw->weight, 0.0f, 1.0f);
2067 }
2068 }
2069 }
2070
2071 MEM_delete(dvert_array);
2072 }
2073}
2074
2075static void dvert_mirror_op(MDeformVert *dvert,
2076 MDeformVert *dvert_mirr,
2077 const char sel,
2078 const char sel_mirr,
2079 const int *flip_map,
2080 const int flip_map_len,
2081 const bool mirror_weights,
2082 const bool flip_vgroups,
2083 const bool all_vgroups,
2084 const int act_vgroup)
2085{
2086 BLI_assert(sel || sel_mirr);
2087
2088 if (sel_mirr && sel) {
2089 /* swap */
2090 if (mirror_weights) {
2091 if (all_vgroups) {
2092 std::swap(*dvert, *dvert_mirr);
2093 }
2094 else {
2095 MDeformWeight *dw = BKE_defvert_find_index(dvert, act_vgroup);
2096 MDeformWeight *dw_mirr = BKE_defvert_find_index(dvert_mirr, act_vgroup);
2097
2098 if (dw && dw_mirr) {
2099 std::swap(dw->weight, dw_mirr->weight);
2100 }
2101 else if (dw) {
2102 dw_mirr = BKE_defvert_ensure_index(dvert_mirr, act_vgroup);
2103 dw_mirr->weight = dw->weight;
2104 BKE_defvert_remove_group(dvert, dw);
2105 }
2106 else if (dw_mirr) {
2107 dw = BKE_defvert_ensure_index(dvert, act_vgroup);
2108 dw->weight = dw_mirr->weight;
2109 BKE_defvert_remove_group(dvert_mirr, dw_mirr);
2110 }
2111 }
2112 }
2113
2114 if (flip_vgroups) {
2115 BKE_defvert_flip(dvert, flip_map, flip_map_len);
2116 BKE_defvert_flip(dvert_mirr, flip_map, flip_map_len);
2117 }
2118 }
2119 else {
2120 /* dvert should always be the target, only swaps pointer */
2121 if (sel_mirr) {
2122 std::swap(dvert, dvert_mirr);
2123 }
2124
2125 if (mirror_weights) {
2126 if (all_vgroups) {
2127 BKE_defvert_copy(dvert, dvert_mirr);
2128 }
2129 else {
2130 BKE_defvert_copy_index(dvert, act_vgroup, dvert_mirr, act_vgroup);
2131 }
2132 }
2133
2134 /* flip map already modified for 'all_vgroups' */
2135 if (flip_vgroups) {
2136 BKE_defvert_flip(dvert, flip_map, flip_map_len);
2137 }
2138 }
2139}
2140
2142 const bool mirror_weights,
2143 const bool flip_vgroups,
2144 const bool all_vgroups,
2145 const bool use_topology,
2146 int *r_totmirr,
2147 int *r_totfail)
2148{
2149 /* TODO: vgroup locking. */
2150
2151 const int def_nr = BKE_object_defgroup_active_index_get(ob) - 1;
2152 int totmirr = 0, totfail = 0;
2153
2154 *r_totmirr = *r_totfail = 0;
2155
2156 const ListBaseT<bDeformGroup> *defbase = BKE_object_defgroup_list(ob);
2157
2158 if ((mirror_weights == false && flip_vgroups == false) ||
2159 (BLI_findlink(defbase, def_nr) == nullptr))
2160 {
2161 return;
2162 }
2163
2164 int *flip_map = nullptr;
2165 int flip_map_len;
2166 if (flip_vgroups) {
2167 flip_map = all_vgroups ? BKE_object_defgroup_flip_map(ob, false, &flip_map_len) :
2168 BKE_object_defgroup_flip_map_single(ob, false, def_nr, &flip_map_len);
2169
2170 BLI_assert(flip_map != nullptr);
2171
2172 if (flip_map == nullptr) {
2173 /* something went wrong!, possibly no groups */
2174 return;
2175 }
2176 }
2177 else {
2178 flip_map = nullptr;
2179 flip_map_len = 0;
2180 }
2181
2182 /* only the active group */
2183 if (ob->type == OB_MESH) {
2184 Mesh *mesh = id_cast<Mesh *>(ob->data);
2185
2186 if (BMEditMesh *em = mesh->runtime->edit_mesh.get()) {
2188 const int cd_dvert_offset = CustomData_get_offset(&bm->vdata, CD_MDEFORMVERT);
2189 BMIter iter;
2190
2191 if (cd_dvert_offset == -1) {
2192 goto cleanup;
2193 }
2194
2195 EDBM_verts_mirror_cache_begin(em, bm, 0, true, false, false, use_topology);
2196
2198
2199 /* Go through the list of edit-vertices and assign them. */
2200 BMVert *eve, *eve_mirr;
2201 BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
2202 if (!BM_elem_flag_test(eve, BM_ELEM_TAG)) {
2203 if ((eve_mirr = EDBM_verts_mirror_get(em, bm, eve))) {
2204 if (eve_mirr != eve) {
2205 if (!BM_elem_flag_test(eve_mirr, BM_ELEM_TAG)) {
2206 const bool sel = BM_elem_flag_test(eve, BM_ELEM_SELECT);
2207 const bool sel_mirr = BM_elem_flag_test(eve_mirr, BM_ELEM_SELECT);
2208
2209 if ((sel || sel_mirr) && (eve != eve_mirr)) {
2211 static_cast<MDeformVert *>(BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset)),
2212 static_cast<MDeformVert *>(BM_ELEM_CD_GET_VOID_P(eve_mirr, cd_dvert_offset)),
2213 sel,
2214 sel_mirr,
2215 flip_map,
2216 flip_map_len,
2217 mirror_weights,
2218 flip_vgroups,
2219 all_vgroups,
2220 def_nr);
2221 totmirr++;
2222 }
2223
2224 /* don't use these again */
2227 }
2228 }
2229 }
2230 else {
2231 totfail++;
2232 }
2233 }
2234 }
2236 }
2237 else {
2238 /* object mode / weight paint */
2239 const bool use_sel = (mesh->editflag & (ME_EDIT_PAINT_FACE_SEL | ME_EDIT_PAINT_VERT_SEL)) !=
2240 0;
2241 if (mesh->deform_verts().is_empty()) {
2242 goto cleanup;
2243 }
2244
2245 BLI_bitmap *vert_tag = BLI_BITMAP_NEW(mesh->verts_num, __func__);
2246 MutableSpan<MDeformVert> dverts = mesh->deform_verts_for_write();
2247 const bke::AttributeAccessor attributes = mesh->attributes();
2248 const VArray<bool> select_vert = *attributes.lookup_or_default<bool>(
2249 ".select_vert", bke::AttrDomain::Point, false);
2250
2251 for (int vidx = 0; vidx < mesh->verts_num; vidx++) {
2252 if (!BLI_BITMAP_TEST(vert_tag, vidx)) {
2253 int vidx_mirr;
2254 if ((vidx_mirr = mesh_get_x_mirror_vert(ob, nullptr, vidx, use_topology)) != -1) {
2255 if (vidx != vidx_mirr) {
2256 if (!BLI_BITMAP_TEST(vert_tag, vidx_mirr)) {
2257 const bool sel = use_sel ? select_vert[vidx] : true;
2258 const bool sel_mirr = use_sel ? select_vert[vidx_mirr] : true;
2259
2260 if (sel || sel_mirr) {
2261 dvert_mirror_op(&dverts[vidx],
2262 &dverts[vidx_mirr],
2263 sel,
2264 sel_mirr,
2265 flip_map,
2266 flip_map_len,
2267 mirror_weights,
2268 flip_vgroups,
2269 all_vgroups,
2270 def_nr);
2271 totmirr++;
2272 }
2273
2274 BLI_BITMAP_ENABLE(vert_tag, vidx);
2275 BLI_BITMAP_ENABLE(vert_tag, vidx_mirr);
2276 }
2277 }
2278 }
2279 else {
2280 totfail++;
2281 }
2282 }
2283 }
2284
2285 MEM_delete(vert_tag);
2286 }
2287 }
2288 else if (ob->type == OB_LATTICE) {
2289 Lattice *lt = vgroup_edit_lattice(ob);
2290 /* half but found up odd value */
2291
2292 if (lt->pntsu == 1 || lt->dvert == nullptr) {
2293 goto cleanup;
2294 }
2295
2296 /* unlike editmesh we know that by only looping over the first half of
2297 * the 'u' indices it will cover all points except the middle which is
2298 * ok in this case */
2299 int pntsu_half = lt->pntsu / 2;
2300
2301 for (int w = 0; w < lt->pntsw; w++) {
2302 for (int v = 0; v < lt->pntsv; v++) {
2303 for (int u = 0; u < pntsu_half; u++) {
2304 int u_inv = (lt->pntsu - 1) - u;
2305 if (u != u_inv) {
2306 const int i1 = BKE_lattice_index_from_uvw(lt, u, v, w);
2307 const int i2 = BKE_lattice_index_from_uvw(lt, u_inv, v, w);
2308
2309 const BPoint *bp = &lt->def[i1];
2310 const BPoint *bp_mirr = &lt->def[i2];
2311
2312 const bool sel = bp->f1 & SELECT;
2313 const bool sel_mirr = bp_mirr->f1 & SELECT;
2314
2315 if (sel || sel_mirr) {
2316 dvert_mirror_op(&lt->dvert[i1],
2317 &lt->dvert[i2],
2318 sel,
2319 sel_mirr,
2320 flip_map,
2321 flip_map_len,
2322 mirror_weights,
2323 flip_vgroups,
2324 all_vgroups,
2325 def_nr);
2326 totmirr++;
2327 }
2328 }
2329 }
2330 }
2331 }
2332 }
2333
2334 /* disabled, confusing when you have an active pose bone */
2335#if 0
2336 /* flip active group index */
2337 if (flip_vgroups && flip_map[def_nr] >= 0) {
2338 ob->actdef = flip_map[def_nr] + 1;
2339 }
2340#endif
2341
2342cleanup:
2343 *r_totmirr = totmirr;
2344 *r_totfail = totfail;
2345
2346 if (flip_map) {
2347 MEM_delete(flip_map);
2348 }
2349
2350#undef VGROUP_MIRR_OP
2351}
2352
2354{
2355 const ListBaseT<bDeformGroup> *defbase = BKE_object_defgroup_list(ob);
2356 bDeformGroup *dg = static_cast<bDeformGroup *>(
2358 if (!dg) {
2359 return;
2360 }
2361
2363}
2364
2365/* only in editmode */
2366static void vgroup_assign_verts(Object *ob, Scene &scene, const int def_nr, const float weight)
2367{
2369
2370 if (ob->type == OB_MESH) {
2371 Mesh *mesh = id_cast<Mesh *>(ob->data);
2372
2374 int cd_dvert_offset;
2375
2376 BMIter iter;
2377 BMVert *eve;
2378
2381 }
2382
2383 cd_dvert_offset = CustomData_get_offset(&bm->vdata, CD_MDEFORMVERT);
2384
2385 /* Go through the list of edit-vertices and assign them. */
2386 BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
2388 MDeformVert *dv;
2389 MDeformWeight *dw;
2390 dv = static_cast<MDeformVert *>(
2391 BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset)); /* can be nullptr */
2392 dw = BKE_defvert_ensure_index(dv, def_nr);
2393 if (dw) {
2394 dw->weight = weight;
2395 }
2396 }
2397 }
2398 }
2399 else {
2400 const bke::AttributeAccessor attributes = mesh->attributes();
2401 const VArray<bool> select_vert = *attributes.lookup_or_default<bool>(
2402 ".select_vert", bke::AttrDomain::Point, false);
2403
2404 MutableSpan<MDeformVert> dverts = mesh->deform_verts_for_write();
2405
2406 for (int i = 0; i < mesh->verts_num; i++) {
2407 if (select_vert[i]) {
2408 MDeformWeight *dw;
2409 dw = BKE_defvert_ensure_index(&dverts[i], def_nr);
2410 if (dw) {
2411 dw->weight = weight;
2412 }
2413 }
2414 }
2415 }
2416 }
2417 else if (ob->type == OB_LATTICE) {
2418 Lattice *lt = vgroup_edit_lattice(ob);
2419 MDeformVert *dv;
2420 BPoint *bp;
2421 int a, tot;
2422
2423 if (lt->dvert == nullptr) {
2425 }
2426
2427 dv = lt->dvert;
2428
2429 tot = lt->pntsu * lt->pntsv * lt->pntsw;
2430 for (a = 0, bp = lt->def; a < tot; a++, bp++, dv++) {
2431 if (bp->f1 & SELECT) {
2432 MDeformWeight *dw;
2433
2434 dw = BKE_defvert_ensure_index(dv, def_nr);
2435 if (dw) {
2436 dw->weight = weight;
2437 }
2438 }
2439 }
2440 }
2441 else if (ob->type == OB_GREASE_PENCIL) {
2442 GreasePencil *grease_pencil = id_cast<GreasePencil *>(ob->data);
2443 const bDeformGroup *defgroup = static_cast<const bDeformGroup *>(
2445
2446 {
2447 using namespace ed::greasepencil;
2448 Vector<MutableDrawingInfo> drawings = retrieve_editable_drawings(scene, *grease_pencil);
2449 for (MutableDrawingInfo info : drawings) {
2450 bke::greasepencil::assign_to_vertex_group(info.drawing, defgroup->name, weight);
2451 }
2452 }
2453 }
2454}
2455
2457
2458/* -------------------------------------------------------------------- */
2461
2463{
2465 CTX_wm_operator_poll_msg_set(C, "No active editable object");
2466 return false;
2467 }
2468
2469 if (!OB_TYPE_SUPPORT_VGROUP(ob->type)) {
2470 CTX_wm_operator_poll_msg_set(C, "Object type does not support vertex groups");
2471 return false;
2472 }
2473
2474 /* Data checks. */
2475 const ID *data = static_cast<const ID *>(ob->data);
2476 if (data == nullptr || !ID_IS_EDITABLE(data) || ID_IS_OVERRIDE_LIBRARY(data)) {
2477 CTX_wm_operator_poll_msg_set(C, "Object type \"%s\" does not have editable data");
2478 return false;
2479 }
2480
2481 return true;
2482}
2483
2485{
2486 Object *ob = context_object(C);
2488}
2489
2491{
2493 return false;
2494 }
2495
2496 const ListBaseT<bDeformGroup> *defbase = BKE_object_defgroup_list(ob);
2497 if (defbase->is_empty()) {
2498 CTX_wm_operator_poll_msg_set(C, "Object has no vertex groups");
2499 return false;
2500 }
2501
2502 return true;
2503}
2504
2506{
2507 Object *ob = context_object(C);
2508 return vertex_group_poll_ex(C, ob);
2509}
2510
2512{
2513 Object *ob = context_object(C);
2514
2516 return false;
2517 }
2518
2520}
2521
2522/* editmode _or_ weight paint vertex sel */
2524 const bool needs_select,
2525 const short ob_type_flag)
2526{
2527 Object *ob = context_object(C);
2528
2530 return false;
2531 }
2532
2533 if (ob_type_flag && ((1 << ob->type) & ob_type_flag) == 0) {
2534 return false;
2535 }
2536
2538 return true;
2539 }
2540 if (ob->mode & OB_MODE_WEIGHT_PAINT) {
2541 if (needs_select) {
2543 return true;
2544 }
2545 CTX_wm_operator_poll_msg_set(C, "Vertex select needs to be enabled in weight paint mode");
2546 return false;
2547 }
2548 return true;
2549 }
2550 return false;
2551}
2552
2553#if 0
2554static bool vertex_group_vert_poll(bContext *C)
2555{
2556 return vertex_group_vert_poll_ex(C, false, 0);
2557}
2558#endif
2559
2561{
2562 return vertex_group_vert_poll_ex(C, false, (1 << OB_MESH));
2563}
2564
2566{
2567 return vertex_group_vert_poll_ex(C, true, 0);
2568}
2569
2570#if 0
2571static bool vertex_group_mesh_vert_select_poll(bContext *C)
2572{
2573 return vertex_group_vert_poll_ex(C, true, (1 << OB_MESH));
2574}
2575#endif
2576
2577/* editmode _or_ weight paint vertex sel and active group unlocked */
2579{
2580 Object *ob = context_object(C);
2581
2583 return false;
2584 }
2585
2587 return false;
2588 }
2589
2590 const int def_nr = BKE_object_defgroup_active_index_get(ob);
2591 if (def_nr != 0) {
2592 const ListBaseT<bDeformGroup> *defbase = BKE_object_defgroup_list(ob);
2593 const bDeformGroup *dg = static_cast<const bDeformGroup *>(BLI_findlink(defbase, def_nr - 1));
2594 if (dg && (dg->flag & DG_LOCK_WEIGHT)) {
2595 CTX_wm_operator_poll_msg_set(C, "The active vertex group is locked");
2596 return false;
2597 }
2598 }
2599 return true;
2600}
2601
2603{
2604 Object *ob = context_object(C);
2605
2607 return false;
2608 }
2609
2610 /* only difference to #vertex_group_vert_select_poll */
2611 if (ob->type != OB_MESH) {
2612 return false;
2613 }
2614
2616}
2617
2619
2620/* -------------------------------------------------------------------- */
2623
2636
2638{
2639 /* identifiers */
2640 ot->name = "Add Vertex Group";
2641 ot->idname = "OBJECT_OT_vertex_group_add";
2642 ot->description = "Add a new vertex group to the active object";
2643
2644 /* API callbacks. */
2647
2648 /* flags */
2650}
2651
2653
2654/* -------------------------------------------------------------------- */
2657
2659 Object &ob,
2660 bDeformGroup *dg,
2661 const bool use_selection,
2662 const bool all_drawings = false)
2663{
2664 using namespace ed::greasepencil;
2665 GreasePencil &grease_pencil = *id_cast<GreasePencil *>(ob.data);
2666
2667 if (all_drawings) {
2668 /* When removing vgroup, iterate over all the drawing. */
2669 for (GreasePencilDrawingBase *base : grease_pencil.drawings()) {
2670 if (base->type != GP_DRAWING) {
2671 continue;
2672 }
2673 bke::greasepencil::Drawing &drawing = reinterpret_cast<GreasePencilDrawing *>(base)->wrap();
2674 bke::greasepencil::remove_from_vertex_group(drawing, dg->name, use_selection);
2675 }
2676 /* Remove vgroup from the list. */
2678 }
2679 else {
2680 Vector<MutableDrawingInfo> drawings = retrieve_editable_drawings(scene, grease_pencil);
2681 for (const MutableDrawingInfo &info : drawings) {
2682 bke::greasepencil::remove_from_vertex_group(info.drawing, dg->name, use_selection);
2683 }
2684 }
2685}
2686
2688 Object &ob,
2689 const bool use_selection,
2690 const bool all_drawings = false,
2691 const bool only_unlocked = false)
2692{
2693 const ListBaseT<bDeformGroup> *defbase = BKE_object_defgroup_list(&ob);
2694
2695 bDeformGroup *dg = defbase->first();
2696 while (dg) {
2697 bDeformGroup *next_group = dg->next;
2698 if (!only_unlocked || (dg->flag & DG_LOCK_WEIGHT) == 0) {
2699 grease_pencil_clear_from_vgroup(scene, ob, dg, use_selection, all_drawings);
2700 }
2701 dg = next_group;
2702 }
2703}
2704
2706{
2707 Object *ob = context_object(C);
2708 Scene &scene = *CTX_data_scene(C);
2709 const bool all_vgroup = RNA_boolean_get(op->ptr, "all");
2710 const bool only_unlocked = RNA_boolean_get(op->ptr, "all_unlocked");
2711
2712 if (ob->type == OB_GREASE_PENCIL) {
2713 if (all_vgroup || only_unlocked) {
2714 grease_pencil_clear_from_all_vgroup(scene, *ob, false, true, only_unlocked);
2715 }
2716 else {
2717 const ListBaseT<bDeformGroup> *defbase = BKE_object_defgroup_list(ob);
2718 bDeformGroup *dg = static_cast<bDeformGroup *>(
2720
2721 if (!dg) {
2722 return OPERATOR_CANCELLED;
2723 }
2724 grease_pencil_clear_from_vgroup(scene, *ob, dg, false, true);
2725 }
2726 }
2727 else {
2728 if (all_vgroup || only_unlocked) {
2729 BKE_object_defgroup_remove_all_ex(ob, only_unlocked);
2730 }
2731 else {
2733 }
2734 }
2735
2740
2741 return OPERATOR_FINISHED;
2742}
2743
2745{
2746 /* identifiers */
2747 ot->name = "Remove Vertex Group";
2748 ot->idname = "OBJECT_OT_vertex_group_remove";
2749 ot->description = "Delete the active or all vertex groups from the active object";
2750
2751 /* API callbacks. */
2752 ot->poll = vertex_group_poll;
2754
2755 /* flags */
2756 /* redo operator will fail in this case because vertex groups aren't stored
2757 * in local edit mode stack and toggling "all" property will lead to
2758 * all groups deleted without way to restore them (see #29527, sergey) */
2759 ot->flag = /*OPTYPE_REGISTER|*/ OPTYPE_UNDO;
2760
2761 /* properties */
2762 PropertyRNA *prop = RNA_def_boolean(ot->srna, "all", false, "All", "Remove all vertex groups");
2764 prop = RNA_def_boolean(
2765 ot->srna, "all_unlocked", false, "All Unlocked", "Remove all unlocked vertex groups");
2767}
2768
2770
2771/* -------------------------------------------------------------------- */
2774
2776{
2778 Object *ob = context_object(C);
2779 Scene &scene = *CTX_data_scene(C);
2780
2781 const int def_nr = BKE_object_defgroup_active_index_get(ob) - 1;
2782 const ListBaseT<bDeformGroup> *defbase = BKE_object_defgroup_list(ob);
2783 if (!BLI_findlink(defbase, def_nr)) {
2784 BKE_report(op->reports, RPT_ERROR, "No active vertex group to operate on");
2785 return OPERATOR_CANCELLED;
2786 }
2787
2788 vgroup_assign_verts(ob, scene, def_nr, ts->vgroup_weight);
2789
2790 if (ts->auto_normalize) {
2791 if (ob->type == OB_GREASE_PENCIL) {
2792 const int current_frame = scene.r.cfra;
2794 ob, def_nr, true, op->reports, current_frame);
2795 }
2796 else {
2798 }
2799 }
2800
2803
2804 return OPERATOR_FINISHED;
2805}
2806
2808{
2809 /* identifiers */
2810 ot->name = "Assign to Vertex Group";
2811 ot->idname = "OBJECT_OT_vertex_group_assign";
2812 ot->description = "Assign the selected vertices to the active vertex group";
2813
2814 /* API callbacks. */
2817
2818 /* flags */
2819 /* redo operator will fail in this case because vertex group assignment
2820 * isn't stored in local edit mode stack and toggling "new" property will
2821 * lead to creating plenty of new vertex groups (see #29527, sergey) */
2822 ot->flag = /*OPTYPE_REGISTER|*/ OPTYPE_UNDO;
2823}
2824
2826
2827/* -------------------------------------------------------------------- */
2830
2831/* NOTE: just a wrapper around vertex_group_assign_exec(), except we add these to a new group */
2833{
2834 /* create new group... */
2835 Object *ob = context_object(C);
2837
2838 /* assign selection to new group */
2839 return vertex_group_assign_exec(C, op);
2840}
2841
2843{
2844 /* identifiers */
2845 ot->name = "Assign to New Group";
2846 ot->idname = "OBJECT_OT_vertex_group_assign_new";
2847 ot->description = "Assign the selected vertices to a new vertex group";
2848
2849 /* API callbacks. */
2852
2853 /* flags */
2854 /* redo operator will fail in this case because vertex group assignment
2855 * isn't stored in local edit mode stack and toggling "new" property will
2856 * lead to creating plenty of new vertex groups (see #29527, sergey) */
2857 ot->flag = /*OPTYPE_REGISTER|*/ OPTYPE_UNDO;
2858}
2859
2861
2862/* -------------------------------------------------------------------- */
2865
2867{
2868 const bool use_all_groups = RNA_boolean_get(op->ptr, "use_all_groups");
2869 const bool use_all_verts = RNA_boolean_get(op->ptr, "use_all_verts");
2870 Scene &scene = *CTX_data_scene(C);
2871
2872 Object *ob = context_object(C);
2873
2874 if (use_all_groups) {
2875 if (ob->type == OB_GREASE_PENCIL) {
2876 grease_pencil_clear_from_all_vgroup(scene, *ob, true);
2877 }
2878 else if (BKE_object_defgroup_clear_all(ob, true) == false) {
2879 return OPERATOR_CANCELLED;
2880 }
2881 }
2882 else {
2883 const ListBaseT<bDeformGroup> *defbase = BKE_object_defgroup_list(ob);
2884 bDeformGroup *dg = static_cast<bDeformGroup *>(
2886 if (dg == nullptr) {
2887 return OPERATOR_CANCELLED;
2888 }
2889
2890 if (ob->type == OB_GREASE_PENCIL) {
2891 grease_pencil_clear_from_vgroup(scene, *ob, dg, !use_all_verts);
2892 }
2893 else if (BKE_object_defgroup_clear(ob, dg, !use_all_verts) == false) {
2894 return OPERATOR_CANCELLED;
2895 }
2896 }
2897
2899 if (ts->auto_normalize) {
2900 const int def_nr = BKE_object_defgroup_active_index_get(ob) - 1;
2901 if (def_nr >= 0) {
2902 if (ob->type == OB_GREASE_PENCIL) {
2903 const int current_frame = scene.r.cfra;
2905 ob, def_nr, true, op->reports, current_frame);
2906 }
2907 else {
2909 }
2910 }
2911 }
2912
2915
2916 return OPERATOR_FINISHED;
2917}
2918
2920{
2921 PropertyRNA *prop;
2922 /* identifiers */
2923 ot->name = "Remove from Vertex Group";
2924 ot->idname = "OBJECT_OT_vertex_group_remove_from";
2925 ot->description = "Remove the selected vertices from active or all vertex group(s)";
2926
2927 /* API callbacks. */
2930
2931 /* flags */
2932 /* redo operator will fail in this case because vertex groups assignment
2933 * isn't stored in local edit mode stack and toggling "all" property will lead to
2934 * removing vertices from all groups (see #29527, sergey) */
2935 ot->flag = /*OPTYPE_REGISTER|*/ OPTYPE_UNDO;
2936
2937 /* properties */
2938 prop = RNA_def_boolean(
2939 ot->srna, "use_all_groups", false, "All Groups", "Remove from all groups");
2941 prop = RNA_def_boolean(
2942 ot->srna, "use_all_verts", false, "All Vertices", "Clear the active group");
2944}
2945
2947
2948/* -------------------------------------------------------------------- */
2951
2953{
2954 const ToolSettings &tool_settings = *CTX_data_scene(C)->toolsettings;
2955 Object *ob = context_object(C);
2956 Scene &scene = *CTX_data_scene(C);
2957
2958 if (!ob || !ID_IS_EDITABLE(ob) || ID_IS_OVERRIDE_LIBRARY(ob)) {
2959 return OPERATOR_CANCELLED;
2960 }
2961
2962 vgroup_select_verts(tool_settings, ob, scene, 1);
2965
2966 return OPERATOR_FINISHED;
2967}
2968
2970{
2971 /* identifiers */
2972 ot->name = "Select Vertex Group";
2973 ot->idname = "OBJECT_OT_vertex_group_select";
2974 ot->description = "Select all the vertices assigned to the active vertex group";
2975
2976 /* API callbacks. */
2979
2980 /* flags */
2982}
2983
2985
2986/* -------------------------------------------------------------------- */
2989
2991{
2992 const ToolSettings &tool_settings = *CTX_data_scene(C)->toolsettings;
2993 Object *ob = context_object(C);
2994 Scene &scene = *CTX_data_scene(C);
2995
2996 vgroup_select_verts(tool_settings, ob, scene, 0);
2999
3000 return OPERATOR_FINISHED;
3001}
3002
3004{
3005 /* identifiers */
3006 ot->name = "Deselect Vertex Group";
3007 ot->idname = "OBJECT_OT_vertex_group_deselect";
3008 ot->description = "Deselect all selected vertices assigned to the active vertex group";
3009
3010 /* API callbacks. */
3013
3014 /* flags */
3016}
3017
3019
3020/* -------------------------------------------------------------------- */
3023
3036
3038{
3039 /* identifiers */
3040 ot->name = "Duplicate Vertex Group";
3041 ot->idname = "OBJECT_OT_vertex_group_copy";
3042 ot->description = "Make a copy of the active vertex group";
3043
3044 /* API callbacks. */
3045 ot->poll = vertex_group_poll;
3047
3048 /* flags */
3050}
3051
3053
3054/* -------------------------------------------------------------------- */
3057
3059{
3060 Object *ob = context_object(C);
3061
3062 float offset = RNA_float_get(op->ptr, "offset");
3063 float gain = RNA_float_get(op->ptr, "gain");
3064 eVGroupSelect subset_type = static_cast<eVGroupSelect>(
3065 RNA_enum_get(op->ptr, "group_select_mode"));
3066
3067 int subset_count, vgroup_tot;
3068
3069 const bool *vgroup_validmap = BKE_object_defgroup_subset_from_select_type(
3070 ob, subset_type, &vgroup_tot, &subset_count);
3071 vgroup_levels_subset(ob, vgroup_validmap, vgroup_tot, subset_count, offset, gain);
3072 MEM_delete(vgroup_validmap);
3073
3077
3078 return OPERATOR_FINISHED;
3079}
3080
3082{
3083 /* identifiers */
3084 ot->name = "Vertex Group Levels";
3085 ot->idname = "OBJECT_OT_vertex_group_levels";
3086 ot->description =
3087 "Add some offset and multiply with some gain the weights of the active vertex group";
3088
3089 /* API callbacks. */
3090 ot->poll = vertex_group_poll;
3092
3093 /* flags */
3095
3098 ot->srna, "offset", 0.0f, -1.0, 1.0, "Offset", "Value to add to weights", -1.0f, 1.0f);
3100 ot->srna, "gain", 1.0f, 0.0f, FLT_MAX, "Gain", "Value to multiply weights by", 0.0f, 10.0f);
3101}
3102
3104
3105/* -------------------------------------------------------------------- */
3108
3110{
3111 Object *ob = context_object(C);
3112 bool changed;
3113
3114 changed = vgroup_normalize(ob);
3115
3116 if (changed) {
3120
3121 return OPERATOR_FINISHED;
3122 }
3123 return OPERATOR_CANCELLED;
3124}
3125
3127{
3128 /* identifiers */
3129 ot->name = "Normalize Vertex Group";
3130 ot->idname = "OBJECT_OT_vertex_group_normalize";
3131 ot->description =
3132 "Normalize weights of the active vertex group, so that the highest ones are now 1.0";
3133
3134 /* API callbacks. */
3135 ot->poll = vertex_group_poll;
3137
3138 /* flags */
3140}
3141
3143
3144/* -------------------------------------------------------------------- */
3147
3148/*
3149 * For a given object, determine which target vertex group to normalize.
3150 */
3152{
3153 /* Default to All Groups. */
3154 eVGroupSelect target_group = WT_VGROUP_ALL;
3155
3156 /* If armature is present, and armature is actively deforming the object
3157 * (i.e armature modifier isn't disabled) use BONE DEFORM. */
3159
3160 int defgroup_tot = BKE_object_defgroup_count(ob);
3161 bool *defgroup_validmap = BKE_object_defgroup_validmap_get(ob, defgroup_tot);
3162
3163 for (int i = 0; i < defgroup_tot; i++) {
3164 if (defgroup_validmap[i] == true) {
3165 target_group = WT_VGROUP_BONE_DEFORM;
3166 break;
3167 }
3168 }
3169 MEM_delete(defgroup_validmap);
3170 }
3171
3172 return target_group;
3173}
3174
3176{
3177 Object *ob = context_object(C);
3178
3180
3181 RNA_enum_set(op->ptr, "group_select_mode", target_group);
3182
3183 bool lock_active = RNA_boolean_get(op->ptr, "lock_active");
3184 eVGroupSelect subset_type = static_cast<eVGroupSelect>(
3185 RNA_enum_get(op->ptr, "group_select_mode"));
3186 bool changed;
3187 int subset_count, vgroup_tot;
3188 const bool *vgroup_validmap = BKE_object_defgroup_subset_from_select_type(
3189 ob, subset_type, &vgroup_tot, &subset_count);
3190
3191 if (subset_count == 0) {
3192 BKE_report(op->reports, RPT_ERROR, "No vertex groups to operate on");
3193 changed = false;
3194 }
3195 else {
3196 if (ob->type == OB_GREASE_PENCIL) {
3197 int current_frame = CTX_data_scene(C)->r.cfra;
3198 changed = vgroup_normalize_all(
3199 ob, vgroup_validmap, vgroup_tot, lock_active, false, op->reports, current_frame);
3200 }
3201 else {
3202 changed = vgroup_normalize_all(
3203 ob, vgroup_validmap, vgroup_tot, lock_active, false, op->reports);
3204 }
3205 }
3206
3207 MEM_delete(vgroup_validmap);
3208
3209 if (changed) {
3213
3214 return OPERATOR_FINISHED;
3215 }
3216
3217 /* allow to adjust settings */
3218 return OPERATOR_FINISHED;
3219}
3220
3222{
3223 /* identifiers */
3224 ot->name = "Normalize All Vertex Groups";
3225 ot->idname = "OBJECT_OT_vertex_group_normalize_all";
3226 ot->description =
3227 "Normalize all weights of all vertex groups, "
3228 "so that for each vertex, the sum of all weights is 1.0";
3229
3230 /* API callbacks. */
3231 ot->poll = vertex_group_poll;
3233
3234 /* flags */
3236
3239 "lock_active",
3240 true,
3241 "Lock Active",
3242 "Keep the values of the active group while normalizing others");
3243}
3244
3246
3247/* -------------------------------------------------------------------- */
3250
3252{
3253 Object *ob = context_object(C);
3254
3255 int action = RNA_enum_get(op->ptr, "action");
3256 int mask = RNA_enum_get(op->ptr, "mask");
3257
3258 vgroup_lock_all(ob, action, mask);
3259
3261
3262 return OPERATOR_FINISHED;
3263}
3264
3266 wmOperatorType * /*ot*/,
3267 PointerRNA *ptr)
3268{
3269 int action = RNA_enum_get(ptr, "action");
3270 int mask = RNA_enum_get(ptr, "mask");
3271
3272 /* NOTE: constructing the following string literals can be done in a less verbose way,
3273 * however the resulting strings can't be usefully translated, (via `TIP_`). */
3274 switch (action) {
3275 case VGROUP_LOCK:
3276 switch (mask) {
3277 case VGROUP_MASK_ALL:
3278 return TIP_("Lock all vertex groups of the active object");
3280 return TIP_("Lock selected vertex groups of the active object");
3282 return TIP_("Lock unselected vertex groups of the active object");
3284 return TIP_("Lock selected and unlock unselected vertex groups of the active object");
3285 }
3286 break;
3287 case VGROUP_UNLOCK:
3288 switch (mask) {
3289 case VGROUP_MASK_ALL:
3290 return TIP_("Unlock all vertex groups of the active object");
3292 return TIP_("Unlock selected vertex groups of the active object");
3294 return TIP_("Unlock unselected vertex groups of the active object");
3296 return TIP_("Unlock selected and lock unselected vertex groups of the active object");
3297 }
3298 break;
3299 case VGROUP_TOGGLE:
3300 switch (mask) {
3301 case VGROUP_MASK_ALL:
3302 return TIP_("Toggle locks of all vertex groups of the active object");
3304 return TIP_("Toggle locks of selected vertex groups of the active object");
3306 return TIP_("Toggle locks of unselected vertex groups of the active object");
3308 return TIP_(
3309 "Toggle locks of all and invert unselected vertex groups of the active object");
3310 }
3311 break;
3312 case VGROUP_INVERT:
3313 switch (mask) {
3314 case VGROUP_MASK_ALL:
3315 return TIP_("Invert locks of all vertex groups of the active object");
3318 return TIP_("Invert locks of selected vertex groups of the active object");
3320 return TIP_("Invert locks of unselected vertex groups of the active object");
3321 }
3322 break;
3323 default:
3324 return {};
3325 }
3326 return {};
3327}
3328
3330{
3331 /* identifiers */
3332 ot->name = "Change the Lock On Vertex Groups";
3333 ot->idname = "OBJECT_OT_vertex_group_lock";
3334 ot->description = "Change the lock state of all or some vertex groups of active object";
3335
3336 /* API callbacks. */
3337 ot->poll = vertex_group_poll;
3340
3341 /* flags */
3343
3344 RNA_def_enum(ot->srna,
3345 "action",
3348 "Action",
3349 "Lock action to execute on vertex groups");
3350
3351 RNA_def_enum(ot->srna,
3352 "mask",
3355 "Mask",
3356 "Apply the action based on vertex group selection");
3357}
3358
3360
3361/* -------------------------------------------------------------------- */
3364
3366{
3367 Object *ob = context_object(C);
3368 bool auto_assign = RNA_boolean_get(op->ptr, "auto_assign");
3369 bool auto_remove = RNA_boolean_get(op->ptr, "auto_remove");
3370
3371 eVGroupSelect subset_type = static_cast<eVGroupSelect>(
3372 RNA_enum_get(op->ptr, "group_select_mode"));
3373
3374 int subset_count, vgroup_tot;
3375
3376 const bool *vgroup_validmap = BKE_object_defgroup_subset_from_select_type(
3377 ob, subset_type, &vgroup_tot, &subset_count);
3378 vgroup_invert_subset(ob, vgroup_validmap, vgroup_tot, subset_count, auto_assign, auto_remove);
3379 MEM_delete(vgroup_validmap);
3380
3384
3385 return OPERATOR_FINISHED;
3386}
3387
3389{
3390 /* identifiers */
3391 ot->name = "Invert Vertex Group";
3392 ot->idname = "OBJECT_OT_vertex_group_invert";
3393 ot->description = "Invert active vertex group's weights";
3394
3395 /* API callbacks. */
3396 ot->poll = vertex_group_poll;
3398
3399 /* flags */
3401
3404 "auto_assign",
3405 true,
3406 "Add Weights",
3407 "Add vertices from groups that have zero weight before inverting");
3409 "auto_remove",
3410 true,
3411 "Remove Weights",
3412 "Remove vertices from groups that have zero weight after inverting");
3413}
3414
3416
3417/* -------------------------------------------------------------------- */
3420
3422{
3423 const float fac = RNA_float_get(op->ptr, "factor");
3424 const int repeat = RNA_int_get(op->ptr, "repeat");
3425 const eVGroupSelect subset_type = static_cast<eVGroupSelect>(
3426 RNA_enum_get(op->ptr, "group_select_mode"));
3427 const float fac_expand = RNA_float_get(op->ptr, "expand");
3428
3429 bool has_vgroup_multi = false;
3431 for (Object *ob : objects) {
3432 int subset_count, vgroup_tot;
3433
3434 bool *vgroup_validmap = BKE_object_defgroup_subset_from_select_type(
3435 ob, subset_type, &vgroup_tot, &subset_count);
3436
3437 if (vgroup_tot) {
3438 const bool *locked_vgroups = BKE_object_defgroup_lock_flags_get(ob, vgroup_tot);
3439 if (locked_vgroups) {
3440 /* Remove locked groups from the vgroup valid map. */
3441 for (int i = 0; i < vgroup_tot; i++) {
3442 if (vgroup_validmap[i] && locked_vgroups[i]) {
3443 vgroup_validmap[i] = false;
3444 subset_count--;
3445 }
3446 }
3447 }
3448 MEM_SAFE_DELETE(locked_vgroups);
3449
3450 has_vgroup_multi = true;
3451
3452 if (subset_count) {
3454 ob, vgroup_validmap, vgroup_tot, subset_count, fac, repeat, fac_expand);
3455
3459 }
3460 }
3461
3462 MEM_delete(vgroup_validmap);
3463 }
3464
3465 /* NOTE: typically we would return canceled if no changes were made (`changed_multi`).
3466 * In this case it's important only to do this if none of the objects *could* be changed.
3467 * TODO: skip meshes without any selected vertices.
3468 *
3469 * The reason this is a special case is returning canceled prevents the `group_select_mode`
3470 * from being changed, where this setting could have been the reason no change was possible. */
3471 if (!has_vgroup_multi) {
3472 BKE_reportf(op->reports, RPT_WARNING, "No meshes with vertex groups found");
3473 return OPERATOR_CANCELLED;
3474 }
3475 return OPERATOR_FINISHED;
3476}
3477
3479{
3480 /* identifiers */
3481 ot->name = "Smooth Vertex Weights";
3482 ot->idname = "OBJECT_OT_vertex_group_smooth";
3483 ot->description = "Smooth weights for selected vertices";
3484
3485 /* API callbacks. */
3488
3489 /* flags */
3491
3493 RNA_def_float(ot->srna, "factor", 0.5f, 0.0f, 1.0, "Factor", "", 0.0f, 1.0f);
3494 RNA_def_int(ot->srna, "repeat", 1, 1, 10000, "Iterations", "", 1, 200);
3495
3496 RNA_def_float(ot->srna,
3497 "expand",
3498 0.0f,
3499 -1.0f,
3500 1.0,
3501 "Expand/Contract",
3502 "Expand/contract weights",
3503 -1.0f,
3504 1.0f);
3505}
3506
3508
3509/* -------------------------------------------------------------------- */
3512
3514{
3515 const float limit = RNA_float_get(op->ptr, "limit");
3516 const bool keep_single = RNA_boolean_get(op->ptr, "keep_single");
3517 const eVGroupSelect subset_type = static_cast<eVGroupSelect>(
3518 RNA_enum_get(op->ptr, "group_select_mode"));
3519
3521 for (Object *ob : objects) {
3522 int subset_count, vgroup_tot;
3523
3524 const bool *vgroup_validmap = BKE_object_defgroup_subset_from_select_type(
3525 ob, subset_type, &vgroup_tot, &subset_count);
3526
3527 vgroup_clean_subset(ob, vgroup_validmap, vgroup_tot, subset_count, limit, keep_single);
3528 MEM_delete(vgroup_validmap);
3529
3533 }
3534
3535 return OPERATOR_FINISHED;
3536}
3537
3539{
3540 /* identifiers */
3541 ot->name = "Clean Vertex Group Weights";
3542 ot->idname = "OBJECT_OT_vertex_group_clean";
3543 ot->description = "Remove vertex group assignments which are not required";
3544
3545 /* API callbacks. */
3546 ot->poll = vertex_group_poll;
3548
3549 /* flags */
3551
3553 RNA_def_float(ot->srna,
3554 "limit",
3555 0.0f,
3556 0.0f,
3557 1.0,
3558 "Limit",
3559 "Remove vertices which weight is below or equal to this limit",
3560 0.0f,
3561 0.99f);
3563 "keep_single",
3564 false,
3565 "Keep Single",
3566 "Keep verts assigned to at least one group when cleaning");
3567}
3568
3570
3571/* -------------------------------------------------------------------- */
3574
3576{
3577 Object *ob = context_object(C);
3578
3579 const int steps = RNA_int_get(op->ptr, "steps");
3580 eVGroupSelect subset_type = static_cast<eVGroupSelect>(
3581 RNA_enum_get(op->ptr, "group_select_mode"));
3582
3583 int subset_count, vgroup_tot;
3584
3585 const bool *vgroup_validmap = BKE_object_defgroup_subset_from_select_type(
3586 ob, subset_type, &vgroup_tot, &subset_count);
3587 vgroup_quantize_subset(ob, vgroup_validmap, vgroup_tot, subset_count, steps);
3588 MEM_delete(vgroup_validmap);
3589
3593
3594 return OPERATOR_FINISHED;
3595}
3596
3598{
3599 /* identifiers */
3600 ot->name = "Quantize Vertex Weights";
3601 ot->idname = "OBJECT_OT_vertex_group_quantize";
3602 ot->description = "Set weights to a fixed number of steps";
3603
3604 /* API callbacks. */
3605 ot->poll = vertex_group_poll;
3607
3608 /* flags */
3610
3612 RNA_def_int(ot->srna, "steps", 4, 1, 1000, "Steps", "Number of steps between 0 and 1", 1, 100);
3613}
3614
3616
3617/* -------------------------------------------------------------------- */
3620
3622{
3623 const int limit = RNA_int_get(op->ptr, "limit");
3624 const eVGroupSelect subset_type = static_cast<eVGroupSelect>(
3625 RNA_enum_get(op->ptr, "group_select_mode"));
3626 int remove_multi_count = 0;
3627
3629 for (Object *ob : objects) {
3630
3631 int subset_count, vgroup_tot;
3632 const bool *vgroup_validmap = BKE_object_defgroup_subset_from_select_type(
3633 ob, subset_type, &vgroup_tot, &subset_count);
3634 const int remove_count = vgroup_limit_total_subset(
3635 ob, vgroup_validmap, vgroup_tot, subset_count, limit);
3636 MEM_delete(vgroup_validmap);
3637
3638 if (remove_count != 0) {
3642 }
3643 remove_multi_count += remove_count;
3644 }
3645
3646 if (remove_multi_count) {
3647 BKE_reportf(op->reports,
3648 remove_multi_count ? RPT_INFO : RPT_WARNING,
3649 "%d vertex weights limited",
3650 remove_multi_count);
3651
3652 return OPERATOR_FINISHED;
3653 }
3654
3655 /* NOTE: would normally return canceled, except we want the redo
3656 * UI to show up for users to change */
3657 return OPERATOR_FINISHED;
3658}
3659
3661{
3662 /* identifiers */
3663 ot->name = "Limit Number of Weights per Vertex";
3664 ot->idname = "OBJECT_OT_vertex_group_limit_total";
3665 ot->description =
3666 "Limit deform weights associated with a vertex to a specified number by removing lowest "
3667 "weights";
3668
3669 /* API callbacks. */
3670 ot->poll = vertex_group_poll;
3672
3673 /* flags */
3675
3677 RNA_def_int(ot->srna, "limit", 4, 1, 32, "Limit", "Maximum number of deform weights", 1, 32);
3678}
3679
3681
3682/* -------------------------------------------------------------------- */
3685
3687{
3688 Object *ob = context_object(C);
3689 int totmirr = 0, totfail = 0;
3690
3691 vgroup_mirror(ob,
3692 RNA_boolean_get(op->ptr, "mirror_weights"),
3693 RNA_boolean_get(op->ptr, "flip_group_names"),
3694 RNA_boolean_get(op->ptr, "all_groups"),
3695 RNA_boolean_get(op->ptr, "use_topology"),
3696 &totmirr,
3697 &totfail);
3698
3699 ED_mesh_report_mirror(*op->reports, totmirr, totfail);
3700
3705
3706 return OPERATOR_FINISHED;
3707}
3708
3710{
3711 /* identifiers */
3712 ot->name = "Mirror Vertex Group";
3713 ot->idname = "OBJECT_OT_vertex_group_mirror";
3714 ot->description =
3715 "Mirror vertex group, flip weights and/or names, editing only selected vertices, "
3716 "flipping when both sides are selected otherwise copy from unselected";
3717
3718 /* API callbacks. */
3719 ot->poll = vertex_group_poll;
3721
3722 /* flags */
3724
3725 /* properties */
3726 RNA_def_boolean(ot->srna, "mirror_weights", true, "Mirror Weights", "Mirror weights");
3728 ot->srna, "flip_group_names", true, "Flip Group Names", "Flip vertex group names");
3729 RNA_def_boolean(ot->srna, "all_groups", false, "All Groups", "Mirror all vertex groups weights");
3731 ot->srna,
3732 "use_topology",
3733 false,
3734 "Topology Mirror",
3735 "Use topology based mirroring (for when both sides of mesh have matching, unique topology)");
3736}
3737
3739
3740/* -------------------------------------------------------------------- */
3743
3745{
3746 Object *obact = context_object(C);
3747 int changed_tot = 0;
3748 int fail = 0;
3749
3750 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects) {
3751 if (obact != ob && BKE_object_supports_vertex_groups(ob)) {
3752 if (vgroup_array_copy(ob, obact)) {
3756 changed_tot++;
3757 }
3758 else {
3759 fail++;
3760 }
3761 }
3762 }
3764
3765 if ((changed_tot == 0 && fail == 0) || fail) {
3766 BKE_reportf(op->reports,
3767 RPT_ERROR,
3768 "Copy vertex groups to selected: %d done, %d failed (object data must support "
3769 "vertex groups and have matching indices)",
3770 changed_tot,
3771 fail);
3772 }
3773
3774 return OPERATOR_FINISHED;
3775}
3776
3778{
3779 /* identifiers */
3780 ot->name = "Copy Vertex Group to Selected";
3781 ot->idname = "OBJECT_OT_vertex_group_copy_to_selected";
3782 ot->description = "Replace vertex groups of selected objects by vertex groups of active object";
3783
3784 /* API callbacks. */
3785 ot->poll = vertex_group_poll;
3787
3788 /* flags */
3790}
3791
3793
3794/* -------------------------------------------------------------------- */
3797
3799{
3800 Object *ob = context_object(C);
3801 int nr = RNA_enum_get(op->ptr, "group");
3802
3803 BLI_assert(nr + 1 >= 0);
3805
3808
3809 return OPERATOR_FINISHED;
3810}
3811
3813 PointerRNA * /*ptr*/,
3814 PropertyRNA * /*prop*/,
3815 bool *r_free)
3816{
3817 if (C == nullptr) {
3819 }
3820
3821 Object *ob = context_object(C);
3822 EnumPropertyItem tmp = {0, "", 0, "", ""};
3823 EnumPropertyItem *item = nullptr;
3824 bDeformGroup *def;
3825 int a, totitem = 0;
3826
3827 if (!ob) {
3829 }
3830
3831 const ListBaseT<bDeformGroup> *defbase = BKE_object_defgroup_list(ob);
3832 for (a = 0, def = defbase->first(); def; def = def->next, a++) {
3833 tmp.value = a;
3834 tmp.icon = ICON_GROUP_VERTEX;
3835 tmp.identifier = def->name;
3836 tmp.name = def->name;
3837 RNA_enum_item_add(&item, &totitem, &tmp);
3838 }
3839
3840 RNA_enum_item_end(&item, &totitem);
3841 *r_free = true;
3842
3843 return item;
3844}
3845
3847{
3848 PropertyRNA *prop;
3849
3850 /* identifiers */
3851 ot->name = "Set Active Vertex Group";
3852 ot->idname = "OBJECT_OT_vertex_group_set_active";
3853 ot->description = "Set the active vertex group";
3854
3855 /* API callbacks. */
3856 ot->poll = vertex_group_poll;
3858 ot->invoke = WM_menu_invoke;
3859
3860 /* flags */
3862
3863 /* properties */
3864 prop = RNA_def_enum(
3865 ot->srna, "group", rna_enum_dummy_NULL_items, 0, "Group", "Vertex group to set as active");
3868 ot->prop = prop;
3869}
3870
3872
3873/* -------------------------------------------------------------------- */
3876
3877/* creates the name_array parameter for vgroup_do_remap, call this before fiddling
3878 * with the order of vgroups then call vgroup_do_remap after */
3879static char *vgroup_init_remap(Object *ob)
3880{
3881 const ListBaseT<bDeformGroup> *defbase = BKE_object_defgroup_list(ob);
3882 int defbase_tot = defbase->count();
3883 char *name_array = MEM_new_array_uninitialized<char>(MAX_VGROUP_NAME * defbase_tot,
3884 "sort vgroups");
3885 char *name;
3886
3887 name = name_array;
3888 for (const bDeformGroup &def : *defbase) {
3889 BLI_strncpy_utf8(name, def.name, MAX_VGROUP_NAME);
3890 name += MAX_VGROUP_NAME;
3891 }
3892
3893 return name_array;
3894}
3895
3896static wmOperatorStatus vgroup_do_remap(Object *ob, const char *name_array, wmOperator *op)
3897{
3898 MDeformVert *dvert = nullptr;
3899 const bDeformGroup *def;
3900 const ListBaseT<bDeformGroup> *defbase = BKE_object_defgroup_list(ob);
3901 int defbase_tot = defbase->count();
3902
3903 /* Needs a dummy index at the start. */
3904 int *sort_map_update = MEM_new_array_uninitialized<int>((defbase_tot + 1), __func__);
3905 int *sort_map = sort_map_update + 1;
3906
3907 const char *name;
3908 int i;
3909
3910 name = name_array;
3911 for (def = defbase->first(), i = 0; def; def = def->next, i++) {
3912 sort_map[i] = BKE_defgroup_name_index(defbase, name);
3913 name += MAX_VGROUP_NAME;
3914
3915 BLI_assert(sort_map[i] != -1);
3916 }
3917
3918 if (ob->type == OB_GREASE_PENCIL) {
3919 /* For Grease Pencil objects we don't have to do anything, because all drawings in the object
3920 * store their own set of #vertex_group_names. So changing the vertex group order on object
3921 * level is just a UI matter, no remapping in drawings is needed. */
3922 }
3923 else if (ob->mode == OB_MODE_EDIT) {
3924 if (ob->type == OB_MESH) {
3926 const int cd_dvert_offset = CustomData_get_offset(&bm->vdata, CD_MDEFORMVERT);
3927
3928 if (cd_dvert_offset != -1) {
3929 BMIter iter;
3930 BMVert *eve;
3931
3932 BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
3933 dvert = static_cast<MDeformVert *>(BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset));
3934 if (dvert->totweight) {
3935 BKE_defvert_remap(dvert, sort_map, defbase_tot);
3936 }
3937 }
3938 }
3939 }
3940 else {
3941 BKE_report(op->reports, RPT_ERROR, "Editmode lattice is not supported yet");
3942 MEM_delete(sort_map_update);
3943 return OPERATOR_CANCELLED;
3944 }
3945 }
3946 else {
3947 int dvert_tot = 0;
3948 BKE_object_defgroup_array_get(ob->data, &dvert, &dvert_tot);
3949
3950 /* Create as necessary. */
3951 if (dvert) {
3952 while (dvert_tot--) {
3953 if (dvert->totweight) {
3954 BKE_defvert_remap(dvert, sort_map, defbase_tot);
3955 }
3956 dvert++;
3957 }
3958 }
3959 }
3960
3961 /* update users */
3962 for (i = 0; i < defbase_tot; i++) {
3963 sort_map[i]++;
3964 }
3965
3966 sort_map_update[0] = 0;
3967 BKE_object_defgroup_remap_update_users(ob, sort_map_update);
3968
3969 BLI_assert(sort_map_update[BKE_object_defgroup_active_index_get(ob)] >= 0);
3971 sort_map_update[BKE_object_defgroup_active_index_get(ob)]);
3972
3973 MEM_delete(sort_map_update);
3974
3975 return OPERATOR_FINISHED;
3976}
3977
3978static int vgroup_sort_name(const void *def_a_ptr, const void *def_b_ptr)
3979{
3980 const bDeformGroup *def_a = static_cast<const bDeformGroup *>(def_a_ptr);
3981 const bDeformGroup *def_b = static_cast<const bDeformGroup *>(def_b_ptr);
3982
3983 return BLI_strcasecmp_natural(def_a->name, def_b->name);
3984}
3985
3990static void vgroup_sort_bone_hierarchy(Object *ob, ListBaseT<Bone> *bonebase)
3991{
3992 if (bonebase == nullptr) {
3994 if (armobj != nullptr) {
3995 bArmature *armature = id_cast<bArmature *>(armobj->data);
3996 bonebase = &armature->bonebase;
3997 }
3998 }
3999 ListBaseT<bDeformGroup> *defbase = BKE_object_defgroup_list_mutable(ob);
4000
4001 if (bonebase != nullptr) {
4002 for (Bone &bone : bonebase->items_reversed()) {
4003 bDeformGroup *dg = BKE_object_defgroup_find_name(ob, bone.name);
4004 vgroup_sort_bone_hierarchy(ob, &bone.childbase);
4005
4006 if (dg != nullptr) {
4007 BLI_remlink(defbase, dg);
4008 BLI_addhead(defbase, dg);
4009 }
4010 }
4011 }
4012}
4013
4014enum {
4017};
4018
4020{
4021 Object *ob = context_object(C);
4022 char *name_array;
4024 int sort_type = RNA_enum_get(op->ptr, "sort_type");
4025
4026 /* Init remapping. */
4027 name_array = vgroup_init_remap(ob);
4028
4029 ListBaseT<bDeformGroup> *defbase = BKE_object_defgroup_list_mutable(ob);
4030
4031 /* Sort vgroup names. */
4032 switch (sort_type) {
4033 case SORT_TYPE_NAME:
4035 break;
4037 vgroup_sort_bone_hierarchy(ob, nullptr);
4038 break;
4039 }
4040
4041 /* Remap vgroup data to map to correct names. */
4042 ret = vgroup_do_remap(ob, name_array, op);
4043
4044 if (ret != OPERATOR_CANCELLED) {
4047 }
4048
4049 if (name_array) {
4050 MEM_delete(name_array);
4051 }
4052
4053 return ret;
4054}
4055
4057{
4058 static const EnumPropertyItem vgroup_sort_type[] = {
4059 {SORT_TYPE_NAME, "NAME", 0, "Name", ""},
4060 {SORT_TYPE_BONEHIERARCHY, "BONE_HIERARCHY", 0, "Bone Hierarchy", ""},
4061 {0, nullptr, 0, nullptr, nullptr},
4062 };
4063
4064 ot->name = "Sort Vertex Groups";
4065 ot->idname = "OBJECT_OT_vertex_group_sort";
4066 ot->description = "Sort vertex groups";
4067
4068 /* API callbacks. */
4069 ot->poll = vertex_group_poll;
4071
4072 /* flags */
4074
4075 RNA_def_enum(ot->srna, "sort_type", vgroup_sort_type, SORT_TYPE_NAME, "Sort Type", "Sort type");
4076}
4077
4079
4080/* -------------------------------------------------------------------- */
4083
4085{
4086 Object *ob = context_object(C);
4087 bDeformGroup *def;
4088 char *name_array;
4089 int dir = RNA_enum_get(op->ptr, "direction");
4091
4092 ListBaseT<bDeformGroup> *defbase = BKE_object_defgroup_list_mutable(ob);
4093
4094 def = static_cast<bDeformGroup *>(
4096 if (!def) {
4097 return OPERATOR_CANCELLED;
4098 }
4099
4100 name_array = vgroup_init_remap(ob);
4101
4102 if (BLI_listbase_link_move(defbase, def, dir)) {
4103 ret = vgroup_do_remap(ob, name_array, op);
4104
4105 if (ret != OPERATOR_CANCELLED) {
4108 }
4109 }
4110
4111 if (name_array) {
4112 MEM_delete(name_array);
4113 }
4114
4115 return ret;
4116}
4117
4119{
4120 static const EnumPropertyItem vgroup_slot_move[] = {
4121 {-1, "UP", 0, "Up", ""},
4122 {1, "DOWN", 0, "Down", ""},
4123 {0, nullptr, 0, nullptr, nullptr},
4124 };
4125
4126 /* identifiers */
4127 ot->name = "Move Vertex Group";
4128 ot->idname = "OBJECT_OT_vertex_group_move";
4129 ot->description = "Move the active vertex group up/down in the list";
4130
4131 /* API callbacks. */
4132 ot->poll = vertex_group_poll;
4133 ot->exec = vgroup_move_exec;
4134
4135 /* flags */
4137
4138 RNA_def_enum(ot->srna,
4139 "direction",
4140 vgroup_slot_move,
4141 0,
4142 "Direction",
4143 "Direction to move the active vertex group towards");
4144}
4145
4147
4148/* -------------------------------------------------------------------- */
4151
4152static void vgroup_copy_active_to_sel_single(Object *ob, const int def_nr)
4153{
4154 MDeformVert *dvert_act;
4155
4156 Mesh *mesh = id_cast<Mesh *>(ob->data);
4157 int i;
4158
4160 const int cd_dvert_offset = CustomData_get_offset(&bm->vdata, CD_MDEFORMVERT);
4161 BMIter iter;
4162 BMVert *eve, *eve_act;
4163
4164 dvert_act = ED_mesh_active_dvert_get_em(ob, &eve_act);
4165 if (dvert_act == nullptr) {
4166 return;
4167 }
4168
4169 BM_ITER_MESH_INDEX (eve, &iter, bm, BM_VERTS_OF_MESH, i) {
4170 if (BM_elem_flag_test(eve, BM_ELEM_SELECT) && (eve != eve_act)) {
4171 MDeformVert *dvert_dst = static_cast<MDeformVert *>(
4172 BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset));
4173
4174 BKE_defvert_copy_index(dvert_dst, def_nr, dvert_act, def_nr);
4175
4176 if (mesh->symmetry & ME_SYMMETRY_X) {
4177 mesh_defvert_mirror_update_em(ob, eve, -1, i, cd_dvert_offset);
4178 }
4179 }
4180 }
4181
4182 if (mesh->symmetry & ME_SYMMETRY_X) {
4183 mesh_defvert_mirror_update_em(ob, eve_act, -1, -1, cd_dvert_offset);
4184 }
4185 }
4186 else {
4187 int v_act;
4188
4189 dvert_act = ED_mesh_active_dvert_get_ob(ob, &v_act);
4190 if (dvert_act == nullptr) {
4191 return;
4192 }
4193
4194 MutableSpan<MDeformVert> dverts = mesh->deform_verts_for_write();
4195 const bke::AttributeAccessor attributes = mesh->attributes();
4196 const VArray<bool> select_vert = *attributes.lookup_or_default<bool>(
4197 ".select_vert", bke::AttrDomain::Point, false);
4198
4199 for (i = 0; i < mesh->verts_num; i++) {
4200 if (select_vert[i] && (&dverts[i] != dvert_act)) {
4201 BKE_defvert_copy_index(&dverts[i], def_nr, dvert_act, def_nr);
4202
4203 if (mesh->symmetry & ME_SYMMETRY_X) {
4205 }
4206 }
4207 }
4208
4209 if (mesh->symmetry & ME_SYMMETRY_X) {
4210 mesh_defvert_mirror_update_ob(ob, -1, v_act);
4211 }
4212 }
4213}
4214
4215static bool check_vertex_group_accessible(wmOperator *op, Object *ob, int def_nr)
4216{
4217 const ListBaseT<bDeformGroup> *defbase = BKE_object_defgroup_list(ob);
4218 bDeformGroup *dg = static_cast<bDeformGroup *>(BLI_findlink(defbase, def_nr));
4219
4220 if (!dg) {
4221 BKE_report(op->reports, RPT_ERROR, "Invalid vertex group index");
4222 return false;
4223 }
4224
4225 if (dg->flag & DG_LOCK_WEIGHT) {
4226 BKE_report(op->reports, RPT_ERROR, "Vertex group is locked");
4227 return false;
4228 }
4229
4230 return true;
4231}
4232
4234{
4235 Object *ob = context_object(C);
4236 const int def_nr = RNA_int_get(op->ptr, "weight_group");
4237
4238 if (!check_vertex_group_accessible(op, ob, def_nr)) {
4239 return OPERATOR_CANCELLED;
4240 }
4241
4243
4246
4247 return OPERATOR_FINISHED;
4248}
4249
4251{
4252 PropertyRNA *prop;
4253
4254 ot->name = "Paste Weight to Selected";
4255 ot->idname = "OBJECT_OT_vertex_weight_paste";
4256 ot->description =
4257 "Copy this group's weight to other selected vertices (disabled if vertex group is locked)";
4258
4259 /* API callbacks. */
4262
4263 /* flags */
4265
4266 prop = RNA_def_int(ot->srna,
4267 "weight_group",
4268 -1,
4269 -1,
4270 INT_MAX,
4271 "Weight Index",
4272 "Index of source weight in active vertex group",
4273 -1,
4274 INT_MAX);
4276}
4277
4279
4280/* -------------------------------------------------------------------- */
4283
4285{
4286 Object *ob = context_object(C);
4287 const int def_nr = RNA_int_get(op->ptr, "weight_group");
4288
4289 if (!check_vertex_group_accessible(op, ob, def_nr)) {
4290 return OPERATOR_CANCELLED;
4291 }
4292
4293 vgroup_remove_weight(ob, def_nr);
4294
4297
4298 return OPERATOR_FINISHED;
4299}
4300
4302{
4303 PropertyRNA *prop;
4304
4305 ot->name = "Delete Weight";
4306 ot->idname = "OBJECT_OT_vertex_weight_delete";
4307 ot->description = "Delete this weight from the vertex (disabled if vertex group is locked)";
4308
4309 /* API callbacks. */
4312
4313 /* flags */
4315
4316 prop = RNA_def_int(ot->srna,
4317 "weight_group",
4318 -1,
4319 -1,
4320 INT_MAX,
4321 "Weight Index",
4322 "Index of source weight in active vertex group",
4323 -1,
4324 INT_MAX);
4326}
4327
4329
4330/* -------------------------------------------------------------------- */
4333
4335{
4336 Object *ob = context_object(C);
4337 const int wg_index = RNA_int_get(op->ptr, "weight_group");
4338
4339 if (wg_index != -1) {
4340 BKE_object_defgroup_active_index_set(ob, wg_index + 1);
4343 }
4344
4345 return OPERATOR_FINISHED;
4346}
4347
4349{
4350 PropertyRNA *prop;
4351
4352 ot->name = "Set Active Group";
4353 ot->idname = "OBJECT_OT_vertex_weight_set_active";
4354 ot->description = "Set as active vertex group";
4355
4356 /* API callbacks. */
4359
4360 /* flags */
4362
4363 prop = RNA_def_int(ot->srna,
4364 "weight_group",
4365 -1,
4366 -1,
4367 INT_MAX,
4368 "Weight Index",
4369 "Index of source weight in active vertex group",
4370 -1,
4371 INT_MAX);
4373}
4374
4376
4377/* -------------------------------------------------------------------- */
4380
4382 wmOperator * /*op*/)
4383{
4384 Object *ob = context_object(C);
4386 eVGroupSelect subset_type = static_cast<eVGroupSelect>(ts->vgroupsubset);
4387 bool changed;
4388
4389 changed = vgroup_normalize_active_vertex(ob, subset_type);
4390
4391 if (changed) {
4394
4395 return OPERATOR_FINISHED;
4396 }
4397 return OPERATOR_CANCELLED;
4398}
4399
4401{
4402
4403 ot->name = "Normalize Active";
4404 ot->idname = "OBJECT_OT_vertex_weight_normalize_active_vertex";
4405 ot->description = "Normalize active vertex's weights";
4406
4407 /* API callbacks. */
4410
4411 /* flags */
4413}
4414
4416
4417/* -------------------------------------------------------------------- */
4420
4422{
4423 Object *ob = context_object(C);
4425 eVGroupSelect subset_type = static_cast<eVGroupSelect>(ts->vgroupsubset);
4426
4427 vgroup_copy_active_to_sel(ob, subset_type);
4428
4431
4432 return OPERATOR_FINISHED;
4433}
4434
4436{
4437
4438 ot->name = "Copy Active";
4439 ot->idname = "OBJECT_OT_vertex_weight_copy";
4440 ot->description = "Copy weights from active to selected";
4441
4442 /* API callbacks. */
4445
4446 /* flags */
4448}
4449
4451
4452} // namespace blender::ed::object
#define CTX_DATA_BEGIN(C, Type, instance, member)
#define CTX_DATA_END
Low-level operations for curves.
CustomData interface, see also DNA_customdata_types.h.
support for deformation groups and hooks.
Utility functions for vertex groups in grease pencil objects.
#define SELECT
Definition BKE_mask.hh:19
General operations, lookup, etc. for blender objects.
Functions for dealing with objects and deform verts, used by painting and tools.
#define BLI_BITMAP_NEW(_num, _alloc_string)
Definition BLI_bitmap.hh:39
#define BLI_BITMAP_TEST(_bitmap, _index)
Definition BLI_bitmap.hh:63
#define BLI_BITMAP_ENABLE(_bitmap, _index)
Definition BLI_bitmap.hh:80
#define SNPRINTF_UTF8(dst, format,...)
#define STRNCPY_UTF8(dst, src)
#define STACK_PUSH(stack, val)
#define STACK_DECLARE(stack)
#define STACK_SIZE(stack)
#define STACK_INIT(stack, stack_num)
#define TIP_(msgid)
#define ID_IS_EDITABLE(_id)
Definition DNA_ID.h:708
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition DNA_ID.h:736
#define LT_ACTBP_NONE
#define ME_USING_MIRROR_X_VERTEX_GROUPS(_me)
Object is a sort of wrapper for general info.
#define MAX_VGROUP_NAME
#define OB_TYPE_SUPPORT_VGROUP(_type)
#define WT_VGROUP_MASK_ALL
#define WEIGHT_REPLACE
#define WEIGHT_ADD
#define WEIGHT_SUBTRACT
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DRAW
Definition WM_types.hh:502
#define ND_DATA
Definition WM_types.hh:552
#define ND_VERTEX_GROUP
Definition WM_types.hh:553
#define ND_SELECT
Definition WM_types.hh:551
#define NC_OBJECT
Definition WM_types.hh:392
#define CLAMP(a, b, c)
#define UNUSED_FUNCTION(x)
#define BLI_assert_unreachable()
#define IN_RANGE_INCL(a, b, c)
#define LIKELY(x)
#define BM_ELEM_CD_GET_VOID_P(ele, offset)
#define BM_elem_index_get(ele)
#define BM_elem_flag_test(ele, hflag)
#define BM_elem_flag_enable(ele, hflag)
#define BM_ITER_ELEM(ele, iter, data, itype)
#define BM_ITER_MESH(ele, iter, bm, itype)
#define BM_ITER_MESH_INDEX(ele, iter, bm, itype, indexvar)
#define MEM_realloc_uninitialized(vmemh, len)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
constexpr Span()=default
const T * data() const
Definition BLI_array.hh:323
void fill(const T &value) const
Definition BLI_array.hh:283
constexpr bool contains(const T &value) const
Definition BLI_span.hh:721
constexpr bool is_empty() const
Definition BLI_span.hh:515
constexpr Span()=default
constexpr bool is_empty() const
Definition BLI_span.hh:264
static VArray from_single(T value, const int64_t size)
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
GSpanAttributeWriter lookup_or_add_for_write_only_span(StringRef name, AttrDomain domain, AttrType data_type)
bke::CurvesGeometry & strokes_for_write()
#define FLT_MAX
#define INT_MAX
#define layer
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str)
Definition mallocn.cc:129
bool remove_from_vertex_group(Drawing &drawing, StringRef name, bool use_selection)
void assign_to_vertex_group(Drawing &drawing, StringRef name, float weight)
GroupedSpan< int > build_vert_to_edge_map(Span< int2 > edges, int verts_num, Array< int > &r_offsets, Array< int > &r_indices)
bke::GSpanAttributeWriter ensure_selection_attribute(bke::CurvesGeometry &curves, bke::AttrDomain selection_domain, bke::AttrType create_type, StringRef attribute_name)
void vgroup_parray_to_weight_array(const MDeformVert **dvert_array, int dvert_tot, float *dvert_weights, int def_nr)
void OBJECT_OT_vertex_weight_paste(wmOperatorType *ot)
static void grease_pencil_clear_from_all_vgroup(Scene &scene, Object &ob, const bool use_selection, const bool all_drawings=false, const bool only_unlocked=false)
static wmOperatorStatus vertex_group_limit_total_exec(bContext *C, wmOperator *op)
static wmOperatorStatus vertex_group_normalize_all_exec(bContext *C, wmOperator *op)
void OBJECT_OT_vertex_group_normalize(wmOperatorType *ot)
static wmOperatorStatus vertex_weight_copy_exec(bContext *C, wmOperator *)
void OBJECT_OT_vertex_group_sort(wmOperatorType *ot)
void OBJECT_OT_vertex_group_levels(wmOperatorType *ot)
static void vgroup_smooth_subset(Object *ob, const bool *vgroup_validmap, const int vgroup_tot, const int subset_count, const float fac, const int repeat, const float fac_expand)
static bool object_array_for_wpaint_filter(const Object *ob, void *user_data)
void OBJECT_OT_vertex_weight_set_active(wmOperatorType *ot)
static wmOperatorStatus vertex_weight_normalize_active_vertex_exec(bContext *C, wmOperator *)
static void vgroup_quantize_subset(Object *ob, const bool *vgroup_validmap, const int vgroup_tot, const int, const int steps)
static int vgroup_sort_name(const void *def_a_ptr, const void *def_b_ptr)
static bool vgroup_normalize_active_vertex(Object *ob, eVGroupSelect subset_type)
static bool vgroup_normalize(Object *ob)
void OBJECT_OT_vertex_group_set_active(wmOperatorType *ot)
void OBJECT_OT_vertex_group_smooth(wmOperatorType *ot)
void OBJECT_OT_vertex_group_lock(wmOperatorType *ot)
static const EnumPropertyItem vgroup_lock_actions[]
static const EnumPropertyItem * rna_vertex_group_select_itemf(bContext *C, PointerRNA *ptr, PropertyRNA *prop, bool *r_free)
static void vgroup_nr_vert_add(Object *ob, const int def_nr, const int vertnum, const float weight, const int assignmode)
static wmOperatorStatus vertex_group_levels_exec(bContext *C, wmOperator *op)
void vgroup_data_clamp_range(ID *id, int total)
void vgroup_parray_from_weight_array(MDeformVert **dvert_array, int dvert_tot, const float *dvert_weights, int def_nr, bool remove_zero)
bool vgroup_array_copy(Object *ob, Object *ob_from)
static wmOperatorStatus vertex_group_assign_exec(bContext *C, wmOperator *op)
static void vgroup_copy_active_to_sel_single(Object *ob, const int def_nr)
void vgroup_parray_remove_zero(MDeformVert **dvert_array, int dvert_tot, const bool *vgroup_validmap, int vgroup_tot, float epsilon, bool keep_single)
static wmOperatorStatus vertex_weight_paste_exec(bContext *C, wmOperator *op)
static const EnumPropertyItem * rna_vertex_group_with_single_itemf(bContext *C, PointerRNA *ptr, PropertyRNA *prop, bool *r_free)
static wmOperatorStatus vertex_weight_set_active_exec(bContext *C, wmOperator *op)
void OBJECT_OT_vertex_weight_normalize_active_vertex(wmOperatorType *ot)
static void mesh_defvert_mirror_update_internal(Object *ob, MDeformVert *dvert_dst, MDeformVert *dvert_src, const int def_nr)
void OBJECT_OT_vertex_group_assign(wmOperatorType *ot)
bool vgroup_sync_from_pose(Object *ob)
void OBJECT_OT_vertex_group_limit_total(wmOperatorType *ot)
static wmOperatorStatus vertex_group_copy_exec(bContext *C, wmOperator *)
static int vgroup_limit_total_subset(Object *ob, const bool *vgroup_validmap, const int vgroup_tot, const int subset_count, const int max_weights)
static void vgroup_duplicate(Object *ob)
static wmOperatorStatus vertex_group_clean_exec(bContext *C, wmOperator *op)
bool vgroup_parray_alloc(ID *id, MDeformVert ***dvert_arr, int *dvert_tot, bool use_vert_sel, std::optional< int > current_frame={})
static const EnumPropertyItem vgroup_lock_mask[]
void OBJECT_OT_vertex_group_copy_to_selected(wmOperatorType *ot)
static bool vertex_group_supported_poll(bContext *C)
static void mesh_defvert_mirror_update_em(Object *ob, BMVert *eve, int def_nr, int vidx, const int cd_dvert_offset)
void OBJECT_OT_vertex_weight_copy(wmOperatorType *ot)
static void vgroup_grease_pencil_select_verts(const Scene &scene, const ToolSettings &tool_settings, const bDeformGroup *def_group, const bool select, Object &object)
void vgroup_parray_mirror_assign(Object *ob, MDeformVert **dvert_array, int dvert_tot)
static bool UNUSED_FUNCTION vertex_group_poll_edit(bContext *C)
static wmOperatorStatus vertex_group_invert_exec(bContext *C, wmOperator *op)
static void vgroup_clean_subset(Object *ob, const bool *vgroup_validmap, const int vgroup_tot, const int, const float epsilon, const bool keep_single)
static wmOperatorStatus vertex_group_lock_exec(bContext *C, wmOperator *op)
static wmOperatorStatus vertex_group_assign_new_exec(bContext *C, wmOperator *op)
Object * context_object(const bContext *C)
static void dvert_mirror_op(MDeformVert *dvert, MDeformVert *dvert_mirr, const char sel, const char sel_mirr, const int *flip_map, const int flip_map_len, const bool mirror_weights, const bool flip_vgroups, const bool all_vgroups, const int act_vgroup)
static wmOperatorStatus vertex_group_add_exec(bContext *C, wmOperator *)
static Lattice * vgroup_edit_lattice(Object *ob)
void OBJECT_OT_vertex_group_mirror(wmOperatorType *ot)
float vgroup_vert_weight(Object *ob, bDeformGroup *dg, int vertnum)
static wmOperatorStatus set_active_group_exec(bContext *C, wmOperator *op)
static void vgroup_assign_verts(Object *ob, Scene &scene, const int def_nr, const float weight)
static char * vgroup_init_remap(Object *ob)
static wmOperatorStatus vertex_group_select_exec(bContext *C, wmOperator *)
void OBJECT_OT_vertex_group_deselect(wmOperatorType *ot)
static bool vertex_group_vert_select_poll(bContext *C)
static void vgroup_copy_active_to_sel(Object *ob, eVGroupSelect subset_type)
static bool vertex_group_poll_ex(bContext *C, Object *ob)
static wmOperatorStatus vertex_group_remove_from_exec(bContext *C, wmOperator *op)
static void vgroup_delete_active(Object *ob)
void OBJECT_OT_vertex_group_invert(wmOperatorType *ot)
void OBJECT_OT_vertex_group_move(wmOperatorType *ot)
static void grease_pencil_clear_from_vgroup(Scene &scene, Object &ob, bDeformGroup *dg, const bool use_selection, const bool all_drawings=false)
void vgroup_mirror(Object *ob, bool mirror_weights, bool flip_vgroups, bool all_vgroups, bool use_topology, int *r_totmirr, int *r_totfail)
static wmOperatorStatus vertex_group_mirror_exec(bContext *C, wmOperator *op)
static bool vgroup_normalize_all(Object *ob, const bool *vgroup_validmap, const int vgroup_tot, const bool lock_active, const bool soft_lock_active, ReportList *reports, std::optional< int > current_frame={})
void OBJECT_OT_vertex_group_remove_from(wmOperatorType *ot)
const EnumPropertyItem * vgroup_selection_itemf_helper(const bContext *C, PointerRNA *ptr, PropertyRNA *prop, bool *r_free, unsigned int selection_mask)
static wmOperatorStatus vertex_group_copy_to_selected_exec(bContext *C, wmOperator *op)
static void vgroup_normalize_all_deform_if_active_is_deform(Object *ob, const int def_nr, const bool soft_lock_active, ReportList *reports, std::optional< int > current_frame={})
static void mesh_defvert_mirror_update_ob(Object *ob, int def_nr, int vidx)
static bool vertex_group_vert_select_unlocked_poll(bContext *C)
static int inv_cmp_mdef_vert_weights(const void *a1, const void *a2)
static const EnumPropertyItem * vgroup_itemf(bContext *C, PointerRNA *, PropertyRNA *, bool *r_free)
static void vgroup_invert_subset(Object *ob, const bool *vgroup_validmap, const int vgroup_tot, const int, const bool auto_assign, const bool auto_remove)
void vgroup_select_by_name(Object *ob, const char *name)
static eVGroupSelect normalize_vertex_group_target(Object *ob)
static wmOperatorStatus vgroup_do_remap(Object *ob, const char *name_array, wmOperator *op)
void OBJECT_OT_vertex_group_assign_new(wmOperatorType *ot)
static wmOperatorStatus vertex_group_deselect_exec(bContext *C, wmOperator *)
static void vgroup_operator_subset_select_props(wmOperatorType *ot, bool use_active)
void OBJECT_OT_vertex_weight_delete(wmOperatorType *ot)
static wmOperatorStatus vertex_weight_delete_exec(bContext *C, wmOperator *op)
static bool vertex_group_vert_poll_ex(bContext *C, const bool needs_select, const short ob_type_flag)
static void vgroup_sort_bone_hierarchy(Object *ob, ListBaseT< Bone > *bonebase)
static bool vertex_group_supported_poll_ex(bContext *C, const Object *ob)
void OBJECT_OT_vertex_group_copy(wmOperatorType *ot)
void vgroup_parray_mirror_sync(Object *ob, MDeformVert **dvert_array, int dvert_tot, const bool *vgroup_validmap, int vgroup_tot)
static wmOperatorStatus vertex_group_smooth_exec(bContext *C, wmOperator *op)
static void vgroup_select_verts(const ToolSettings &tool_settings, Object *ob, Scene &scene, int select)
static bool vertex_group_mesh_vert_poll(bContext *C)
static bool vertex_group_poll(bContext *C)
static float get_vert_def_nr(Object *ob, const int def_nr, const int vertnum)
void OBJECT_OT_vertex_group_quantize(wmOperatorType *ot)
static Vector< Object * > object_array_for_wpaint(bContext *C)
static void vgroup_levels_subset(Object *ob, const bool *vgroup_validmap, const int vgroup_tot, const int, const float offset, const float gain)
static wmOperatorStatus vgroup_move_exec(bContext *C, wmOperator *op)
static wmOperatorStatus vertex_group_quantize_exec(bContext *C, wmOperator *op)
static std::string vertex_group_lock_get_description(bContext *, wmOperatorType *, PointerRNA *ptr)
void vgroup_vert_add(Object *ob, bDeformGroup *dg, int vertnum, float weight, int assignmode)
static const EnumPropertyItem WT_vertex_group_select_item[]
void OBJECT_OT_vertex_group_remove(wmOperatorType *ot)
void vgroup_vert_remove(Object *ob, bDeformGroup *dg, int vertnum)
static bool vertex_group_use_vert_sel(Object *ob)
static wmOperatorStatus vertex_group_sort_exec(bContext *C, wmOperator *op)
static bool check_vertex_group_accessible(wmOperator *op, Object *ob, int def_nr)
void OBJECT_OT_vertex_group_add(wmOperatorType *ot)
static wmOperatorStatus vertex_group_normalize_exec(bContext *C, wmOperator *)
Vector< Object * > objects_in_mode_or_selected(bContext *C, bool(*filter_fn)(const Object *ob, void *user_data), void *filter_user_data)
void OBJECT_OT_vertex_group_clean(wmOperatorType *ot)
void OBJECT_OT_vertex_group_select(wmOperatorType *ot)
static void vgroup_remove_weight(Object *ob, const int def_nr)
void vgroup_vert_active_mirror(Object *ob, int def_nr)
static void vgroup_lock_all(Object *ob, int action, int mask)
static bool vertex_group_vert_select_mesh_poll(bContext *C)
static wmOperatorStatus vertex_group_remove_exec(bContext *C, wmOperator *op)
static bool * vgroup_selected_get(Object *ob)
void OBJECT_OT_vertex_group_normalize_all(wmOperatorType *ot)
BLI_INLINE Texture * wrap(MTLTexture *texture)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
int CustomData_get_offset(const CustomData *data, eCustomDataType type)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
struct MDeformVert * BKE_object_defgroup_data_create(struct ID *id)
void RNA_def_enum_funcs(PropertyRNA *prop, EnumPropertyItemFunc itemfunc)
void BKE_object_defgroup_unique_name(bDeformGroup *dg, Object *ob)
Definition deform.cc:758
wmOperatorStatus WM_menu_invoke(bContext *C, wmOperator *op, const wmEvent *)
void BM_mesh_elem_index_ensure(BMesh *bm, const char htype)
void BM_data_layer_add(BMesh *bm, CustomData *data, int type)
void RNA_enum_item_add(EnumPropertyItem **items, int *totitem, const EnumPropertyItem *item)
bool BKE_object_is_in_editmode_vgroup(const Object *ob)
Definition object.cc:1927
void BKE_object_defgroup_remap_update_users(struct Object *ob, const int *map)
int RNA_enum_get(PointerRNA *ptr, const char *name)
ListBaseT< bDeformGroup > * BKE_object_defgroup_list_mutable(Object *ob)
Definition deform.cc:602
@ RPT_WARNING
Definition BKE_report.hh:40
int char char int int int BLI_strcasecmp_natural(const char *s1, const char *s2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
const ListBaseT< bDeformGroup > * BKE_object_defgroup_list(const Object *ob)
Definition deform.cc:591
MDeformVert * ED_mesh_active_dvert_get_em(Object *ob, BMVert **r_eve)
Definition meshtools.cc:845
BMesh * BKE_editmesh_bmesh_get_for_write(Mesh *mesh)
Definition editmesh.cc:79
unsigned int uint
Definition BKE_text.h:55
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void BLI_addhead(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:98
int BKE_object_defgroup_count(const Object *ob)
Definition deform.cc:608
bool BKE_object_defgroup_array_get(struct ID *id, struct MDeformVert **dvert_arr, int *dvert_tot)
bool ED_operator_object_active_local_editable_ex(bContext *C, const Object *ob)
void BKE_object_defgroup_mirror_selection(struct Object *ob, int defbase_tot, const bool *selection, bool *dg_flags_sel, int *r_dg_flags_sel_tot)
void BKE_defvert_remap(MDeformVert *dvert, const int *map, int map_len)
Definition deform.cc:223
void BM_mesh_elem_table_ensure(BMesh *bm, const char htype)
bDeformGroup * BKE_object_defgroup_find_name(const Object *ob, StringRef name)
Definition deform.cc:530
BMVert * EDBM_verts_mirror_get(BMEditMesh *em, BMesh *bm, BMVert *v)
bool CustomData_has_layer(const CustomData *data, eCustomDataType type)
void void void void void void BLI_duplicatelist(ListBase *dst, const ListBase *src) ATTR_NONNULL(1
Definition listbase.cc:834
bool BKE_object_defgroup_clear_all(struct Object *ob, bool use_selection)
bool * BKE_object_defgroup_validmap_get(struct Object *ob, int defbase_tot)
Object * BKE_modifiers_is_deformed_by_armature(Object *ob)
char * BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
MDeformVert * ED_mesh_active_dvert_get_ob(Object *ob, int *r_index)
Definition meshtools.cc:872
@ WT_VGROUP_BONE_SELECT
@ WT_VGROUP_BONE_DEFORM
int mesh_get_x_mirror_vert(Object *ob, Mesh *mesh_eval, int index, bool use_topology)
Definition meshtools.cc:297
int RNA_int_get(PointerRNA *ptr, const char *name)
void BM_mesh_elem_hflag_disable_all(BMesh *bm, const char htype, const char hflag, const bool respecthide)
void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
ATTR_WARN_UNUSED_RESULT const BMVert * v
void BKE_object_defgroup_active_index_set(Object *ob, int new_index)
Definition deform.cc:618
int ED_mesh_mirror_get_vert(Object *ob, int index)
Definition meshtools.cc:367
void EDBM_verts_mirror_cache_begin(BMEditMesh *em, BMesh *bm, int axis, bool use_self, bool use_select, bool respecthide, bool use_topology)
int * BKE_object_defgroup_flip_map(const Object *ob, bool use_default, int *r_flip_map_num)
Definition deform.cc:673
BLI_INLINE BMVert * BM_edge_other_vert(BMEdge *e, const BMVert *v) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
int BKE_defgroup_name_index(const ListBaseT< bDeformGroup > *defbase, StringRef name)
Definition deform.cc:544
void BKE_object_defgroup_remove_all(struct Object *ob)
bool * BKE_object_defgroup_selected_get(struct Object *ob, int defbase_tot, int *r_dg_flags_sel_tot)
float BKE_defvert_find_weight(const MDeformVert *dvert, int defgroup)
Definition deform.cc:779
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:541
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:593
bool BKE_object_supports_vertex_groups(const Object *ob)
Definition deform.cc:465
void BKE_defvert_copy_subset(MDeformVert *dvert_dst, const MDeformVert *dvert_src, const bool *vgroup_subset, int vgroup_num)
Definition deform.cc:102
void CTX_wm_operator_poll_msg_set(bContext *C, const char *msg)
VecBase< int32_t, 2 > int2
int * BKE_object_defgroup_flip_map_single(const Object *ob, bool use_default, int defgroup, int *r_flip_map_num)
Definition deform.cc:685
@ ME_EDIT_PAINT_VERT_SEL
@ ME_EDIT_PAINT_FACE_SEL
@ ME_EDIT_MIRROR_TOPO
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
void BKE_defvert_normalize_ex(MDeformVert &dvert, Span< bool > subset_flags, Span< bool > lock_flags, Span< bool > soft_lock_flags)
Definition deform.cc:252
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
MINLINE int signum_i(float a)
BMesh const char void * data
Mesh * BKE_mesh_from_object(Object *ob)
Object * BKE_object_pose_armature_get(Object *ob)
Definition object.cc:2507
int BKE_lattice_index_from_uvw(const Lattice *lt, int u, int v, int w)
Definition lattice.cc:199
bke::AttrDomain ED_grease_pencil_edit_selection_domain_get(const ToolSettings *tool_settings)
void BKE_defvert_copy_index(MDeformVert *dvert_dst, int defgroup_dst, const MDeformVert *dvert_src, int defgroup_src)
Definition deform.cc:153
bDeformGroup * BKE_defgroup_duplicate(const bDeformGroup *ingroup)
Definition deform.cc:85
void BKE_defvert_sync_mapped(MDeformVert *dvert_dst, const MDeformVert *dvert_src, const int *flip_map, int flip_map_num, bool use_ensure)
Definition deform.cc:197
void BKE_defvert_mirror_subset(MDeformVert *dvert_dst, const MDeformVert *dvert_src, const bool *vgroup_subset, int vgroup_num, const int *flip_map, int flip_map_num)
Definition deform.cc:115
MDeformWeight * BKE_defvert_ensure_index(MDeformVert *dv, int defgroup)
Definition deform.cc:830
int BKE_object_defgroup_name_index(const Object *ob, StringRef name)
Definition deform.cc:597
void RNA_enum_items_add_value(EnumPropertyItem **items, int *totitem, const EnumPropertyItem *item, int value)
int BKE_object_defgroup_flip_index(const Object *ob, int index, bool use_default)
Definition deform.cc:720
bool BKE_object_defgroup_clear(struct Object *ob, struct bDeformGroup *dg, bool use_selection)
void RNA_enum_item_end(EnumPropertyItem **items, int *totitem)
ToolSettings * CTX_data_tool_settings(const bContext *C)
void BKE_object_defgroup_subset_to_index_array(const bool *defgroup_validmap, int defgroup_tot, int *r_defgroup_subset_map)
BPoint * BKE_lattice_active_point_get(Lattice *lt)
Definition lattice.cc:590
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
const EnumPropertyItem rna_enum_dummy_NULL_items[]
Definition rna_rna.cc:28
void paintvert_flush_flags(Object *ob)
Definition editface.cc:774
BLI_INLINE BMVert * BM_vert_at_index(BMesh *bm, const int index)
int BLI_listbase_count(const ListBase *listbase) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:531
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void BM_vert_select_set(BMesh *bm, BMVert *v, const bool select)
Select Vert.
void void BLI_listbase_sort(ListBase *listbase, int(*cmp)(const void *, const void *)) ATTR_NONNULL(1
Definition listbase.cc:319
void BKE_defvert_normalize_subset(MDeformVert &dvert, Span< bool > subset_flags)
Definition deform.cc:235
void BKE_defvert_flip(MDeformVert *dvert, const int *flip_map, int flip_map_num)
Definition deform.cc:418
bool * BKE_object_defgroup_lock_flags_get(struct Object *ob, int defbase_tot)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, const int default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
void EDBM_verts_mirror_cache_end(BMEditMesh *em)
void ED_mesh_report_mirror(ReportList &reports, int totmirr, int totfail)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
unsigned int BLI_bitmap
Definition BLI_bitmap.hh:15
Main * CTX_data_main(const bContext *C)
struct bDeformGroup * BKE_object_defgroup_add(struct Object *ob)
MINLINE float max_ff(float a, float b)
void BKE_defvert_add_index_notest(MDeformVert *dv, int defgroup, float weight)
Definition deform.cc:861
@ ID_RECALC_SELECT
Definition DNA_ID.h:1101
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
void BKE_defvert_copy(MDeformVert *dvert_dst, const MDeformVert *dvert_src)
Definition deform.cc:130
ID_Type GS(const char *name)
Definition DNA_ID.h:410
bool BKE_object_is_in_wpaint_select_vert(const Object *ob)
Definition object.cc:2012
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
@ PROP_ENUM_NO_TRANSLATE
Definition RNA_types.hh:502
@ PROP_HIDDEN
Definition RNA_types.hh:408
void BKE_object_defgroup_remove_all_ex(struct Object *ob, bool only_unlocked)
BMVert * editbmesh_get_x_mirror_vert(Object *ob, BMesh *bm, BMVert *eve, const float co[3], int index, bool use_topology)
Definition meshtools.cc:358
bool EDBM_uvselect_clear(BMesh *bm)
void DEG_relations_tag_update(Main *bmain)
Scene * CTX_data_scene(const bContext *C)
MDeformVert * ED_mesh_active_dvert_get_only(Object *ob)
Definition meshtools.cc:886
bool * BKE_object_defgroup_subset_from_select_type(struct Object *ob, enum eVGroupSelect subset_type, int *r_defgroup_tot, int *r_subset_count)
void BKE_object_defgroup_remove(struct Object *ob, struct bDeformGroup *defgroup)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
void BKE_defvert_remove_group(MDeformVert *dvert, MDeformWeight *dw)
Definition deform.cc:885
void EDBM_select_flush_from_verts(BMesh *bm, short selectmode, bool select)
PropertyRNA * RNA_def_float(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
float RNA_float_get(PointerRNA *ptr, const char *name)
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
MDeformWeight * BKE_defvert_find_index(const MDeformVert *dv, int defgroup)
Definition deform.cc:811
void void void bool BLI_listbase_link_move(ListBase *listbase, void *vlink, int step) ATTR_NONNULL()
Definition listbase.cc:443
int * BKE_object_defgroup_flip_map_unlocked(const Object *ob, bool use_default, int *r_flip_map_num)
Definition deform.cc:678
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void BKE_defvert_normalize_lock_map(MDeformVert &dvert, Span< bool > subset_flags, Span< bool > lock_flags)
Definition deform.cc:245
int BKE_object_defgroup_active_index_get(const Object *ob)
Definition deform.cc:613
#define IS_BM_VERT_READ(v)
#define IS_BM_VERT_WRITE(v)
#define WEIGHT_ACCUMULATE
#define floorf
#define fabsf
CustomData vdata
struct Lattice * latt
struct EditLatt * editlatt
struct MDeformVert * dvert
struct BPoint * def
struct MDeformWeight * dw
DNA_DEPRECATED unsigned short actdef
struct RenderData r
struct ToolSettings * toolsettings
ListBaseT< Bone > bonebase
struct bDeformGroup * next
ebDeformGroup_Flag flag
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
std::string(*) get_description(bContext *C, wmOperatorType *ot, PointerRNA *ptr)
Definition WM_types.hh:1197
struct ReportList * reports
#define N_(msgid)