Blender V5.3
paint_vertex_weight_ops.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "MEM_guardedalloc.h"
10
11#include "BLI_bitmap.hh"
12#include "BLI_enum_flags.hh"
13#include "BLI_listbase.hh"
14#include "BLI_math_geom_c.hh"
15#include "BLI_math_vector_c.hh"
16
17#include "DNA_brush_types.h"
18#include "DNA_mesh_types.h"
19#include "DNA_meshdata_types.h"
20#include "DNA_object_types.h"
21#include "DNA_scene_types.h"
22
23#include "RNA_access.hh"
24#include "RNA_define.hh"
25
26#include "BKE_attribute.hh"
27#include "BKE_brush.hh"
28#include "BKE_colortools.hh"
29#include "BKE_context.hh"
30#include "BKE_deform.hh"
31#include "BKE_mesh.hh"
32#include "BKE_mesh_iterators.hh"
33#include "BKE_modifier.hh"
34#include "BKE_object.hh"
35#include "BKE_object_deform.h"
36#include "BKE_paint.hh"
37#include "BKE_report.hh"
38
39#include "DEG_depsgraph.hh"
41
42#include "WM_api.hh"
43#include "WM_types.hh"
44
45#include "ED_armature.hh"
46#include "ED_mesh.hh"
47#include "ED_screen.hh"
48#include "ED_view3d.hh"
49
50#include "UI_interface.hh"
52#include "UI_resources.hh"
53
54#include "../paint_intern.hh" /* own include */
55
56namespace blender {
57
58/* -------------------------------------------------------------------- */
63
64struct WPaintPrev {
65 /* previous vertex weights */
67 /* allocation size of prev buffers */
68 int tot;
69};
70
72{
73 wpp->wpaint_prev = nullptr;
74 wpp->tot = 0;
75}
76
77static void wpaint_prev_create(WPaintPrev *wpp, MDeformVert *dverts, int dcount)
78{
80
81 if (dverts && dcount) {
83 wpp->tot = dcount;
84 BKE_defvert_array_copy(wpp->wpaint_prev, dverts, dcount);
85 }
86}
87
89{
90 if (wpp->wpaint_prev) {
92 }
93 wpp->wpaint_prev = nullptr;
94 wpp->tot = 0;
95}
96
98
99/* -------------------------------------------------------------------- */
102
104{
106
108}
109
111{
112 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
113 Scene *scene = CTX_data_scene(C);
116 Mesh *mesh = id_cast<Mesh *>(ob->data);
117 int type = RNA_enum_get(op->ptr, "type");
118
120 op->reports, depsgraph, scene, ob, armob, type, (mesh->symmetry & ME_SYMMETRY_X));
121
125
126 return OPERATOR_FINISHED;
127}
128
130{
131 static const EnumPropertyItem type_items[] = {
132 {ARM_GROUPS_AUTO, "AUTOMATIC", 0, "Automatic", "Automatic weights from bones"},
134 "ENVELOPES",
135 0,
136 "From Envelopes",
137 "Weights from envelopes with user defined radius"},
138 {0, nullptr, 0, nullptr, nullptr},
139 };
140
141 /* identifiers */
142 ot->name = "Weight from Bones";
143 ot->idname = "PAINT_OT_weight_from_bones";
144 ot->description =
145 ("Set the weights of the groups matching the attached armature's bones that have \"Deform\" "
146 "option enabled, using the distance between the vertices and the bones");
147
148 /* API callbacks. */
152
153 /* flags */
155
156 /* properties */
157 ot->prop = RNA_def_enum(
158 ot->srna, "type", type_items, 0, "Type", "Method to use for assigning weights");
159}
160
162
163/* -------------------------------------------------------------------- */
166
173{
174 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
175 Mesh *mesh;
176 bool changed = false;
177
180 const MDeformVert *dvert = mesh->deform_verts().data();
181
182 if (mesh && dvert && vc.v3d && vc.rv3d && (mesh->vertex_group_active_index != 0)) {
183 const bool use_vert_sel = (mesh->editflag & ME_EDIT_PAINT_VERT_SEL) != 0;
184 int v_idx_best = -1;
185 uint index;
186
189
190 if (use_vert_sel) {
192 C, vc.obact, event->mval, ED_MESH_PICK_DEFAULT_VERT_DIST, true, &index))
193 {
194 v_idx_best = index;
195 }
196 }
197 else {
199 {
200 v_idx_best = index;
201 }
203 {
204 /* This relies on knowing the internal workings of #ED_mesh_pick_face_vert() */
206 op->reports, RPT_WARNING, "The modifier used does not support deformed locations");
207 }
208 }
209
210 if (v_idx_best != -1) { /* should always be valid */
213 if (brush == nullptr) {
214 return OPERATOR_CANCELLED;
215 }
216 const int vgroup_active = mesh->vertex_group_active_index - 1;
217 float vgroup_weight = BKE_defvert_find_weight(&dvert[v_idx_best], vgroup_active);
218 const int defbase_tot = mesh->vertex_group_names.count();
219 bool use_lock_relative = ts->wpaint_lock_relative;
220 bool *defbase_locked = nullptr, *defbase_unlocked = nullptr;
221
222 if (use_lock_relative) {
223 defbase_locked = BKE_object_defgroup_lock_flags_get(vc.obact, defbase_tot);
224 defbase_unlocked = BKE_object_defgroup_validmap_get(vc.obact, defbase_tot);
225
226 use_lock_relative = BKE_object_defgroup_check_lock_relative(
227 defbase_locked, defbase_unlocked, vgroup_active);
228 }
229
230 /* use combined weight in multipaint mode,
231 * since that's what is displayed to the user in the colors */
232 if (ts->multipaint) {
233 int defbase_tot_sel;
234 bool *defbase_sel = BKE_object_defgroup_selected_get(
235 vc.obact, defbase_tot, &defbase_tot_sel);
236
237 if (defbase_tot_sel > 1) {
240 vc.obact, defbase_tot, defbase_sel, defbase_sel, &defbase_tot_sel);
241 }
242
243 use_lock_relative = use_lock_relative &&
245 defbase_tot, defbase_locked, defbase_sel, defbase_tot_sel);
246
247 bool is_normalized = ts->auto_normalize || use_lock_relative;
249 &dvert[v_idx_best], defbase_tot, defbase_sel, defbase_tot_sel, is_normalized);
250 }
251
252 MEM_delete(defbase_sel);
253 }
254
255 if (use_lock_relative) {
257 defbase_tot, defbase_locked, defbase_unlocked, defbase_locked, defbase_unlocked);
258
259 vgroup_weight = BKE_defvert_lock_relative_weight(
260 vgroup_weight, &dvert[v_idx_best], defbase_tot, defbase_locked, defbase_unlocked);
261 }
262
263 MEM_SAFE_DELETE(defbase_locked);
264 MEM_SAFE_DELETE(defbase_unlocked);
265
266 CLAMP(vgroup_weight, 0.0f, 1.0f);
267 BKE_brush_weight_set(&ts->wpaint->paint, brush, vgroup_weight);
268 changed = true;
269 }
270 }
271
272 if (changed) {
273 /* not really correct since the brush didn't change, but redraws the toolbar */
274 WM_main_add_notifier(NC_BRUSH | NA_EDITED, nullptr); /* ts->wpaint->paint.brush */
275
276 return OPERATOR_FINISHED;
277 }
278 return OPERATOR_CANCELLED;
279}
280
282{
283 /* identifiers */
284 ot->name = "Weight Paint Sample Weight";
285 ot->idname = "PAINT_OT_weight_sample";
286 ot->description = "Use the mouse to sample a weight in the 3D view";
287
288 /* API callbacks. */
291
292 /* flags */
294}
295
297
298/* -------------------------------------------------------------------- */
301
307{
308 bool found = false;
309 int i = dvert->totweight;
310 MDeformWeight *dw;
311 for (dw = dvert->dw; i > 0; dw++, i--) {
312 if (dw->def_nr >= groups.size()) [[unlikely]] {
313 continue;
314 }
315 groups[dw->def_nr] = true;
316 found = true;
317 }
318 return found;
319}
320
322 wmOperator *op,
323 const wmEvent *event)
324{
325 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
327 BLI_assert(vc.v3d && vc.rv3d); /* Ensured by poll. */
328
330 const MDeformVert *dverts = mesh->deform_verts().data();
331 if (mesh->vertex_group_names.is_empty() || (dverts == nullptr)) {
332 BKE_report(op->reports, RPT_WARNING, "No vertex group data");
333 return OPERATOR_CANCELLED;
334 }
335
336 const bool use_vert_sel = (mesh->editflag & ME_EDIT_PAINT_VERT_SEL) != 0;
337 Array<bool> groups(mesh->vertex_group_names.count(), false);
338
339 bool found = false;
340
343
344 if (use_vert_sel) {
345 /* Extract from the vertex. */
346 uint index;
348 {
349 const MDeformVert *dvert = &dverts[index];
350 found |= weight_paint_sample_mark_groups(dvert, groups);
351 }
352 }
353 else {
354 /* Extract from the face. */
355 const OffsetIndices faces = mesh->faces();
356 const Span<int> corner_verts = mesh->corner_verts();
357 uint index;
359 for (const int vert : corner_verts.slice(faces[index])) {
360 found |= weight_paint_sample_mark_groups(&dverts[vert], groups);
361 }
362 }
363 }
364
365 if (found == false) {
366 BKE_report(op->reports, RPT_WARNING, "No vertex groups found");
367 return OPERATOR_CANCELLED;
368 }
369
371 C, WM_operatortype_name(op->type, op->ptr).c_str(), ICON_NONE);
372 ui::Layout &layout = *popup_menu_layout(pup);
373 wmOperatorType *ot = WM_operatortype_find("OBJECT_OT_vertex_group_set_active", false);
375 layout.operator_context_set(opcontext);
376
377 for (const auto [i, dg] : mesh->vertex_group_names.enumerate()) {
378 if (groups[i] == false) {
379 continue;
380 }
381 PointerRNA op_ptr = layout.op(
383 RNA_property_enum_set(&op_ptr, ot->prop, i);
384 }
385 popup_menu_end(C, pup);
386
387 return OPERATOR_INTERFACE;
388}
389
391{
392 /* identifiers */
393 ot->name = "Weight Paint Sample Group";
394 ot->idname = "PAINT_OT_weight_sample_group";
395 ot->description = "Select one of the vertex groups available under current mouse position";
396
397 /* API callbacks. */
400
401 /* flags */
403}
404
406
407/* -------------------------------------------------------------------- */
410
411/* fills in the selected faces with the current weight and vertex group */
412static bool weight_paint_set(Object *ob, float paintweight)
413{
414 Mesh *mesh = id_cast<Mesh *>(ob->data);
415 MDeformWeight *dw, *dw_prev;
416 int vgroup_active, vgroup_mirror = -1;
417 const bool topology = (mesh->editflag & ME_EDIT_MIRROR_TOPO) != 0;
418
419 /* mutually exclusive, could be made into a */
420 const short paint_selmode = ME_EDIT_PAINT_SEL_MODE(mesh);
421
422 const OffsetIndices faces = mesh->faces();
423 const Span<int> corner_verts = mesh->corner_verts();
424 MDeformVert *dvert = mesh->deform_verts_for_write().data();
425
426 if (mesh->faces_num == 0 || dvert == nullptr) {
427 return false;
428 }
429
430 vgroup_active = BKE_object_defgroup_active_index_get(ob) - 1;
431
432 /* if mirror painting, find the other group */
434 vgroup_mirror = ED_wpaint_mirror_vgroup_ensure(ob, vgroup_active);
435 }
436
437 WPaintPrev wpp;
438 wpaint_prev_create(&wpp, dvert, mesh->verts_num);
439
440 const bke::AttributeAccessor attributes = mesh->attributes();
441 const VArraySpan select_vert = *attributes.lookup<bool>(".select_vert", bke::AttrDomain::Point);
442 const VArraySpan select_poly = *attributes.lookup<bool>(".select_poly", bke::AttrDomain::Face);
443
444 for (const int i : faces.index_range()) {
445 if ((paint_selmode == SCE_SELECT_FACE) && !(!select_poly.is_empty() && select_poly[i])) {
446 continue;
447 }
448
449 for (const int vert : corner_verts.slice(faces[i])) {
450 if (!dvert[vert].flag) {
451 if ((paint_selmode == SCE_SELECT_VERTEX) &&
452 !(!select_vert.is_empty() && select_vert[vert]))
453 {
454 continue;
455 }
456
457 dw = BKE_defvert_ensure_index(&dvert[vert], vgroup_active);
458 if (dw) {
459 dw_prev = BKE_defvert_ensure_index(wpp.wpaint_prev + vert, vgroup_active);
460 dw_prev->weight = dw->weight; /* set the undo weight */
461 dw->weight = paintweight;
462
463 if (mesh->symmetry & ME_SYMMETRY_X) {
464 /* x mirror painting */
465 int j = mesh_get_x_mirror_vert(ob, nullptr, vert, topology);
466 if (j >= 0) {
467 /* copy, not paint again */
468 if (vgroup_mirror != -1) {
469 dw = BKE_defvert_ensure_index(dvert + j, vgroup_mirror);
470 dw_prev = BKE_defvert_ensure_index(wpp.wpaint_prev + j, vgroup_mirror);
471 }
472 else {
473 dw = BKE_defvert_ensure_index(dvert + j, vgroup_active);
474 dw_prev = BKE_defvert_ensure_index(wpp.wpaint_prev + j, vgroup_active);
475 }
476 dw_prev->weight = dw->weight; /* set the undo weight */
477 dw->weight = paintweight;
478 }
479 }
480 }
481 dvert[vert].flag = 1;
482 }
483 }
484 }
485
486 {
487 MDeformVert *dv = dvert;
488 for (int index = mesh->verts_num; index != 0; index--, dv++) {
489 dv->flag = 0;
490 }
491 }
492
494
496
497 return true;
498}
499
501{
505 if (brush == nullptr) {
506 return OPERATOR_CANCELLED;
507 }
508 float vgroup_weight = BKE_brush_weight_get(&ts->wpaint->paint, brush);
509
510 if (ED_wpaint_ensure_data(C, op->reports, WPAINT_ENSURE_MIRROR, nullptr) == false) {
511 return OPERATOR_CANCELLED;
512 }
513
514 if (weight_paint_set(obact, vgroup_weight)) {
515 ED_region_tag_redraw(CTX_wm_region(C)); /* XXX: should redraw all 3D views. */
516 return OPERATOR_FINISHED;
517 }
518 return OPERATOR_CANCELLED;
519}
520
522{
523 /* identifiers */
524 ot->name = "Set Weight";
525 ot->idname = "PAINT_OT_weight_set";
526 ot->description = "Fill the active vertex group with the current paint weight";
527
528 /* API callbacks. */
531
532 /* flags */
534}
535
537
538/* -------------------------------------------------------------------- */
541
542enum {
545};
546
547/* *** VGroups Gradient *** */
559
564
573 const float *sco_start; /* [2] */
574 const float *sco_end; /* [2] */
575 float sco_line_div; /* store (1.0f / len_v2v2(sco_start, sco_end)) */
579 /* only for init */
581
582 /* options */
585 short type;
587};
588
590{
591 WPGradient_vertStore *vs = &grad_data->vert_cache->elem[index];
592
593 /* Optionally restrict to assigned vertices only. */
594 if (grad_data->use_vgroup_restrict &&
596 {
597 /* In this case the vertex will never have been touched. */
599 return;
600 }
601
602 float alpha;
603 if (grad_data->type == WPAINT_GRADIENT_TYPE_LINEAR) {
604 alpha = line_point_factor_v2(vs->sco, grad_data->sco_start, grad_data->sco_end);
605 }
606 else {
608 alpha = len_v2v2(grad_data->sco_start, vs->sco) * grad_data->sco_line_div;
609 }
610
611 /* adjust weight */
612 alpha = BKE_brush_curve_strength_clamped(grad_data->brush, std::max(0.0f, alpha), 1.0f);
613
614 if (alpha != 0.0f) {
615 MDeformVert *dv = &grad_data->dvert[index];
616 MDeformWeight *dw = BKE_defvert_ensure_index(dv, grad_data->def_nr);
617 // dw->weight = alpha; // testing
618 int tool = grad_data->brush->blend;
619 float testw;
620
621 /* init if we just added */
622 testw = ED_wpaint_blend_tool(
623 tool, vs->weight_orig, grad_data->weightpaint, alpha * grad_data->brush->alpha);
624 CLAMP(testw, 0.0f, 1.0f);
625 dw->weight = testw;
627 }
628 else {
629 MDeformVert *dv = &grad_data->dvert[index];
631 /* normally we nullptr check, but in this case we know it exists */
632 MDeformWeight *dw = BKE_defvert_find_index(dv, grad_data->def_nr);
633 dw->weight = vs->weight_orig;
634 }
635 else {
636 /* wasn't originally existing, remove */
637 MDeformWeight *dw = BKE_defvert_find_index(dv, grad_data->def_nr);
638 if (dw) {
640 }
641 }
642 vs->flag &= ~WPGradient_vertStore::VGRAD_STORE_IS_MODIFIED;
643 }
644}
645
646static void gradientVertUpdate__mapFunc(void *user_data,
647 int index,
648 const float /*co*/[3],
649 const float /*no*/[3])
650{
651 WPGradient_userData *grad_data = static_cast<WPGradient_userData *>(user_data);
652 WPGradient_vertStore *vs = &grad_data->vert_cache->elem[index];
653
654 if (vs->sco[0] == FLT_MAX) {
655 return;
656 }
657
658 gradientVert_update(grad_data, index);
659}
660
661static void gradientVertInit__mapFunc(void *user_data,
662 int index,
663 const float co[3],
664 const float /*no*/[3])
665{
666 WPGradient_userData *grad_data = static_cast<WPGradient_userData *>(user_data);
667 WPGradient_vertStore *vs = &grad_data->vert_cache->elem[index];
668
669 if (grad_data->hide_vert[index] ||
670 (grad_data->use_select &&
671 (!grad_data->select_vert.is_empty() && !grad_data->select_vert[index])))
672 {
673 copy_v2_fl(vs->sco, FLT_MAX);
674 return;
675 }
676
677 /* run first pass only,
678 * the screen coords of the verts need to be cached because
679 * updating the mesh may move them about (entering feedback loop) */
680 if (BLI_BITMAP_TEST(grad_data->vert_visit, index)) {
681 /* Do not copy FLT_MAX here, for generative modifiers we are getting here
682 * multiple times with the same orig index. */
683 return;
684 }
685
689 {
690 copy_v2_fl(vs->sco, FLT_MAX);
691 return;
692 }
693
694 MDeformVert *dv = &grad_data->dvert[index];
695 const MDeformWeight *dw = BKE_defvert_find_index(dv, grad_data->def_nr);
696 if (dw) {
697 vs->weight_orig = dw->weight;
699 }
700 else {
701 vs->weight_orig = 0.0f;
703 }
705 gradientVert_update(grad_data, index);
706}
707
709 wmOperator *op,
710 const wmEvent *event)
711{
712 wmGesture *gesture = static_cast<wmGesture *>(op->customdata);
713 WPGradient_vertStoreBase *vert_cache = static_cast<WPGradient_vertStoreBase *>(
714 gesture->user_data.data);
717
719 BKE_report(op->reports, RPT_WARNING, "Active group is locked, aborting");
721 }
722 else {
723 ret = WM_gesture_straightline_modal(C, op, event);
724 }
725
727 if (event->type == LEFTMOUSE && event->val == KM_RELEASE) { /* XXX, hardcoded */
728 /* generally crap! redo! */
732 }
733 }
734
735 if (ret & OPERATOR_CANCELLED) {
736 if (vert_cache != nullptr) {
737 Mesh *mesh = id_cast<Mesh *>(ob->data);
738 if (vert_cache->wpp.wpaint_prev) {
739 MDeformVert *dvert = mesh->deform_verts_for_write().data();
740 BKE_defvert_array_free_elems(dvert, mesh->verts_num);
741 BKE_defvert_array_copy(dvert, vert_cache->wpp.wpaint_prev, mesh->verts_num);
742 wpaint_prev_destroy(&vert_cache->wpp);
743 }
744 MEM_delete(vert_cache);
745 }
746
749 }
750 else if (ret & OPERATOR_FINISHED) {
751 wpaint_prev_destroy(&vert_cache->wpp);
752 MEM_delete(vert_cache);
753 }
754
755 return ret;
756}
757
759{
760 wmGesture *gesture = static_cast<wmGesture *>(op->customdata);
761 WPGradient_vertStoreBase *vert_cache;
762 ARegion *region = CTX_wm_region(C);
763 Scene *scene = CTX_data_scene(C);
765 Mesh *mesh = id_cast<Mesh *>(ob->data);
766 MDeformVert *dverts = mesh->deform_verts_for_write().data();
767 int x_start = RNA_int_get(op->ptr, "xstart");
768 int y_start = RNA_int_get(op->ptr, "ystart");
769 int x_end = RNA_int_get(op->ptr, "xend");
770 int y_end = RNA_int_get(op->ptr, "yend");
771 const float sco_start[2] = {float(x_start), float(y_start)};
772 const float sco_end[2] = {float(x_end), float(y_end)};
773 const bool is_interactive = (gesture != nullptr);
774
775 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
776
777 WPGradient_userData data = {nullptr};
778
779 if (is_interactive) {
780 if (gesture->user_data.data == nullptr) {
781 gesture->user_data.data = MEM_new_uninitialized(
782 sizeof(WPGradient_vertStoreBase) + (sizeof(WPGradient_vertStore) * mesh->verts_num),
783 __func__);
784 gesture->user_data.use_free = false;
785 data.is_init = true;
786
787 wpaint_prev_create(&(static_cast<WPGradient_vertStoreBase *>(gesture->user_data.data))->wpp,
788 dverts,
789 mesh->verts_num);
790
791 /* On initialization only, convert face -> vert sel. */
792 if (mesh->editflag & ME_EDIT_PAINT_FACE_SEL) {
794 }
795 }
796
797 vert_cache = static_cast<WPGradient_vertStoreBase *>(gesture->user_data.data);
798 }
799 else {
800 if (ED_wpaint_ensure_data(C, op->reports, eWPaintFlag(0), nullptr) == false) {
801 return OPERATOR_CANCELLED;
802 }
803
804 data.is_init = true;
805 vert_cache = static_cast<WPGradient_vertStoreBase *>(MEM_new_uninitialized(
806 sizeof(WPGradient_vertStoreBase) + (sizeof(WPGradient_vertStore) * mesh->verts_num),
807 __func__));
808 }
809
810 const bke::AttributeAccessor attributes = mesh->attributes();
811
812 data.region = region;
813 data.scene = scene;
814 data.mesh = mesh;
815 data.dvert = dverts;
816 data.select_vert = *attributes.lookup<bool>(".select_vert", bke::AttrDomain::Point);
817 data.hide_vert = *attributes.lookup_or_default<bool>(
818 ".hide_vert", bke::AttrDomain::Point, false);
819 data.sco_start = sco_start;
820 data.sco_end = sco_end;
821 data.sco_line_div = 1.0f / len_v2v2(sco_start, sco_end);
823 data.use_select = (mesh->editflag & (ME_EDIT_PAINT_FACE_SEL | ME_EDIT_PAINT_VERT_SEL)) != 0;
824 data.vert_cache = vert_cache;
825 data.vert_visit = nullptr;
826 data.type = RNA_enum_get(op->ptr, "type");
827
828 {
830 VPaint *wp = ts->wpaint;
832
833 BKE_curvemapping_init(brush->curve_distance_falloff);
834
835 data.brush = brush;
836 data.weightpaint = BKE_brush_weight_get(&wp->paint, brush);
837 data.use_vgroup_restrict = (ts->wpaint->flag & VP_FLAG_VGROUP_RESTRICT) != 0;
838 }
839
840 ED_view3d_init_mats_rv3d(ob, static_cast<RegionView3D *>(region->regiondata));
841
842 const Object *ob_eval = DEG_get_evaluated(depsgraph, ob);
843 const Mesh *mesh_eval = BKE_object_get_evaluated_mesh(ob_eval);
844 if (data.is_init) {
845 data.vert_visit = BLI_BITMAP_NEW(mesh->verts_num, __func__);
846
848
849 MEM_delete(data.vert_visit);
850 data.vert_visit = nullptr;
851 }
852 else {
854 }
855
858
859 if (is_interactive == false) {
860 MEM_delete(vert_cache);
861 }
862
863 if (scene->toolsettings->auto_normalize) {
864 const int vgroup_num = mesh->vertex_group_names.count();
865 bool *lock_flags = BKE_object_defgroup_lock_flags_get(ob, vgroup_num);
866 if (!lock_flags) {
867 lock_flags = MEM_new_array_uninitialized<bool>(vgroup_num, "lock_flags");
868 std::memset(lock_flags, 0, vgroup_num); /* Clear to false. */
869 lock_flags[data.def_nr] = true;
870 }
871 bool *vgroup_validmap = BKE_object_defgroup_validmap_get(ob, vgroup_num);
872 if (vgroup_validmap != nullptr) {
873 MDeformVert *dvert = dverts;
874 Span<bool> subset_flags_span = Span(vgroup_validmap, vgroup_num);
875 Span<bool> lock_flags_span = Span(lock_flags, vgroup_num);
876
877 for (int i = 0; i < mesh->verts_num; i++) {
878 if ((data.vert_cache->elem[i].flag & WPGradient_vertStore::VGRAD_STORE_IS_MODIFIED) != 0) {
879 BKE_defvert_normalize_lock_map(dvert[i], subset_flags_span, lock_flags_span);
880 }
881 }
882 MEM_SAFE_DELETE(lock_flags);
883 MEM_delete(vgroup_validmap);
884 }
885 }
886
887 return OPERATOR_FINISHED;
888}
889
891 wmOperator *op,
892 const wmEvent *event)
893{
895
896 if (ED_wpaint_ensure_data(C, op->reports, eWPaintFlag(0), nullptr) == false) {
897 return OPERATOR_CANCELLED;
898 }
899
902 ARegion *region = CTX_wm_region(C);
903 if (region->regiontype == RGN_TYPE_WINDOW) {
904 /* TODO: hard-coded, extend `WM_gesture_straightline_*`. */
905 if (event->type == LEFTMOUSE && event->val == KM_PRESS) {
906 wmGesture *gesture = static_cast<wmGesture *>(op->customdata);
907 gesture->is_active = true;
908 }
909 }
910 }
911 return ret;
912}
913
915{
916 /* defined in DNA_space_types.h */
917 static const EnumPropertyItem gradient_types[] = {
918 {WPAINT_GRADIENT_TYPE_LINEAR, "LINEAR", 0, "Linear", ""},
919 {WPAINT_GRADIENT_TYPE_RADIAL, "RADIAL", 0, "Radial", ""},
920 {0, nullptr, 0, nullptr, nullptr},
921 };
922
923 PropertyRNA *prop;
924
925 /* identifiers */
926 ot->name = "Weight Gradient";
927 ot->idname = "PAINT_OT_weight_gradient";
928 ot->description = "Draw a line to apply a weight gradient to selected vertices";
929
930 /* API callbacks. */
936
937 /* flags */
939
940 prop = RNA_def_enum(ot->srna, "type", gradient_types, 0, "Type", "");
942
944}
945
947
948} // namespace blender
support for deformation groups and hooks.
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 ME_USING_MIRROR_X_VERTEX_GROUPS(_me)
#define ME_EDIT_PAINT_SEL_MODE(_me)
Object is a sort of wrapper for general info.
#define ARM_GROUPS_ENVELOPE
#define ARM_GROUPS_AUTO
#define ED_MESH_PICK_DEFAULT_VERT_DIST
Definition ED_mesh.hh:666
#define ED_MESH_PICK_DEFAULT_FACE_DIST
Definition ED_mesh.hh:667
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DRAW
Definition WM_types.hh:502
#define NC_BRUSH
Definition WM_types.hh:398
#define ND_DATA
Definition WM_types.hh:552
#define NA_EDITED
Definition WM_types.hh:634
#define NC_OBJECT
Definition WM_types.hh:392
#define CLAMP(a, b, c)
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:141
constexpr Span()=default
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
GAttributeReader lookup(const StringRef name) const
constexpr bool is_empty() const
#define FLT_MAX
ENUM_OPERATORS(Layer::Flags)
void mesh_select_face_flush(Mesh &mesh)
PopupMenu * popup_menu_begin(bContext *C, const char *title, int icon) ATTR_NONNULL()
BMesh Iterators.
void WM_main_add_notifier(uint type, void *reference)
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ V3D_PROJ_RET_OK
Definition ED_view3d.hh:273
static bool weight_from_bones_poll(bContext *C)
bool ED_mesh_pick_face_vert(bContext *C, Object *ob, const int mval[2], uint dist_px, uint *r_index)
Definition meshtools.cc:621
wmOperatorStatus WM_menu_invoke(bContext *C, wmOperator *op, const wmEvent *)
void BKE_defvert_array_free(MDeformVert *dvert, int totvert)
Definition deform.cc:1079
static void gradientVertInit__mapFunc(void *user_data, int index, const float co[3], const float[3])
int RNA_enum_get(PointerRNA *ptr, const char *name)
@ RPT_WARNING
Definition BKE_report.hh:40
MINLINE void copy_v2_fl(float r[2], float f)
@ VP_FLAG_VGROUP_RESTRICT
bool ED_wpaint_ensure_data(bContext *C, ReportList *reports, eWPaintFlag flag, WPaintVGroupIndex *vgroup_index)
ViewContext ED_view3d_viewcontext_init(bContext *C, Depsgraph *depsgraph)
unsigned int uint
Definition BKE_text.h:55
std::string WM_operatortype_name(wmOperatorType *ot, PointerRNA *properties)
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void BKE_defvert_array_copy(MDeformVert *dst, const MDeformVert *src, int totvert)
Definition deform.cc:1043
static void gradientVertUpdate__mapFunc(void *user_data, int index, const float[3], const float[3])
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)
wmOperatorStatus WM_gesture_straightline_invoke(bContext *C, wmOperator *op, const wmEvent *event)
eV3DProjStatus ED_view3d_project_float_object(const ARegion *region, const float co[3], float r_co[2], eV3DProjTest flag)
Depsgraph * CTX_data_depsgraph_pointer(const bContext *C)
void PAINT_OT_weight_sample_group(wmOperatorType *ot)
bool * BKE_object_defgroup_validmap_get(struct Object *ob, int defbase_tot)
wmOperatorStatus WM_gesture_straightline_modal(bContext *C, wmOperator *op, const wmEvent *event)
static void wpaint_prev_destroy(WPaintPrev *wpp)
void BKE_defvert_array_free_elems(MDeformVert *dvert, int totvert)
Definition deform.cc:1061
Object * BKE_modifiers_is_deformed_by_armature(Object *ob)
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
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 view3d_operator_needs_gpu(const bContext *C)
void BKE_object_defgroup_split_locked_validmap(int defbase_tot, const bool *locked, const bool *deform, bool *r_locked, bool *r_unlocked)
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:681
int ED_wpaint_mirror_vgroup_ensure(Object *ob, const int vgroup_active)
static wmOperatorStatus paint_weight_gradient_invoke(bContext *C, wmOperator *op, const wmEvent *event)
bool * BKE_object_defgroup_selected_get(struct Object *ob, int defbase_tot, int *r_dg_flags_sel_tot)
void BKE_curvemapping_init(CurveMapping *cumap)
float BKE_defvert_find_weight(const MDeformVert *dvert, int defgroup)
Definition deform.cc:779
static wmOperatorStatus weight_paint_set_exec(bContext *C, wmOperator *op)
bool ED_mesh_pick_face(bContext *C, Object *ob, const int mval[2], uint dist_px, uint *r_index)
Definition meshtools.cc:561
static bool weight_paint_set(Object *ob, float paintweight)
@ ME_EDIT_PAINT_VERT_SEL
@ ME_EDIT_PAINT_FACE_SEL
@ ME_EDIT_MIRROR_TOPO
bool weight_paint_mode_region_view3d_poll(bContext *C)
float BKE_defvert_lock_relative_weight(float weight, const MDeformVert *dv, int defbase_num, const bool *defbase_locked, const bool *defbase_unlocked)
Definition deform.cc:1022
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
static wmOperatorStatus paint_weight_gradient_exec(bContext *C, wmOperator *op)
BMesh const char void * data
Mesh * BKE_mesh_from_object(Object *ob)
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
float line_point_factor_v2(const float p[2], const float l1[2], const float l2[2])
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
void WM_operator_properties_gesture_straightline(wmOperatorType *ot, int cursor)
MDeformWeight * BKE_defvert_ensure_index(MDeformVert *dv, int defgroup)
Definition deform.cc:830
ToolSettings * CTX_data_tool_settings(const bContext *C)
void PAINT_OT_weight_sample(wmOperatorType *ot)
void ED_view3d_init_mats_rv3d(const Object *ob, RegionView3D *rv3d)
bool weight_paint_poll_ignore_tool(bContext *C)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
float BKE_brush_curve_strength_clamped(const Brush *br, float p, float len)
Definition brush.cc:1751
bool * BKE_object_defgroup_lock_flags_get(struct Object *ob, int defbase_tot)
wmOperatorType * WM_operatortype_find(const char *idname, bool quiet)
bool BKE_object_defgroup_check_lock_relative_multi(int defbase_tot, const bool *lock_flags, const bool *selected, int sel_tot)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
@ WM_CURSOR_EDIT
Definition wm_cursors.hh:21
unsigned int BLI_bitmap
Definition BLI_bitmap.hh:15
Main * CTX_data_main(const bContext *C)
void PAINT_OT_weight_set(wmOperatorType *ot)
static void wpaint_prev_create(WPaintPrev *wpp, MDeformVert *dverts, int dcount)
void PAINT_OT_weight_from_bones(wmOperatorType *ot)
MINLINE float len_v2v2(const float v1[2], const float v2[2]) ATTR_WARN_UNUSED_RESULT
bool BKE_object_defgroup_active_is_locked(const struct Object *ob)
float ED_wpaint_blend_tool(const int tool, const float weight, const float paintval, const float alpha)
void BKE_brush_weight_set(Paint *paint, Brush *brush, float value)
Definition brush.cc:1472
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
@ V3D_PROJ_TEST_CLIP_NEAR
Definition ED_view3d.hh:298
@ V3D_PROJ_TEST_CLIP_BB
Definition ED_view3d.hh:296
void PAINT_OT_weight_gradient(wmOperatorType *ot)
void BKE_mesh_foreach_mapped_vert(const Mesh *mesh, void(*func)(void *user_data, int index, const float co[3], const float no[3]), void *user_data, MeshForeachFlag flag)
void RNA_property_enum_set(PointerRNA *ptr, PropertyRNA *prop, int value)
static wmOperatorStatus weight_sample_group_invoke(bContext *C, wmOperator *op, const wmEvent *event)
@ KM_RELEASE
Definition WM_types.hh:325
Brush * BKE_paint_brush(Paint *paint)
void DEG_relations_tag_update(Main *bmain)
Scene * CTX_data_scene(const bContext *C)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
@ OPTYPE_DEPENDS_ON_CURSOR
Definition WM_types.hh:228
float BKE_brush_weight_get(const Paint *paint, const Brush *brush)
Definition brush.cc:1464
void ED_object_vgroup_calc_from_armature(ReportList *reports, Depsgraph *, Scene *scene, Object *ob, Object *par, const int mode, const bool mirror)
bool BKE_object_defgroup_check_lock_relative(const bool *lock_flags, const bool *validmap, int index)
void BKE_defvert_remove_group(MDeformVert *dvert, MDeformWeight *dw)
Definition deform.cc:885
bool ED_mesh_pick_vert(bContext *C, Object *ob, const int mval[2], uint dist_px, bool use_zbuf, uint *r_index)
Definition meshtools.cc:774
static bool weight_paint_sample_mark_groups(const MDeformVert *dvert, MutableSpan< bool > groups)
Object * CTX_data_active_object(const bContext *C)
ARegion * CTX_wm_region(const bContext *C)
bool weight_paint_mode_poll(bContext *C)
static void wpaint_prev_init(WPaintPrev *wpp)
static void gradientVert_update(WPGradient_userData *grad_data, int index)
static wmOperatorStatus weight_sample_invoke(bContext *C, wmOperator *op, const wmEvent *event)
static wmOperatorStatus weight_from_bones_exec(bContext *C, wmOperator *op)
MDeformWeight * BKE_defvert_find_index(const MDeformVert *dv, int defgroup)
Definition deform.cc:811
Mesh * BKE_object_get_evaluated_mesh(const Object *object_eval)
Definition object.cc:4436
@ WPAINT_ENSURE_MIRROR
float BKE_defvert_multipaint_collective_weight(const MDeformVert *dv, int defbase_num, const bool *defbase_sel, int defbase_sel_num, bool is_normalized)
Definition deform.cc:977
static wmOperatorStatus paint_weight_gradient_modal(bContext *C, wmOperator *op, const wmEvent *event)
void WM_gesture_straightline_cancel(bContext *C, wmOperator *op)
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
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
eRegion_Type regiontype
struct MDeformWeight * dw
struct ToolSettings * toolsettings
RegionView3D * rv3d
Definition ED_view3d.hh:86
WPGradient_vertStoreBase * vert_cache
void operator_context_set(wm::OpCallContext opcontext)
PointerRNA op(wmOperatorType *ot, std::optional< StringRef > name, int icon, wm::OpCallContext context, eUI_Item_Flag flag)
wmEventType type
Definition WM_types.hh:817
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
void(*) cancel(bContext *C, wmOperator *op)
Definition WM_types.hh:1149
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
wmOperatorStatus(*) modal(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1157
struct ReportList * reports
struct wmOperatorType * type