Blender V5.3
paint_weight.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
13
14#include "MEM_guardedalloc.h"
15
16#include "BLI_array_utils_c.hh"
17#include "BLI_color_mix.hh"
19#include "BLI_listbase.hh"
20#include "BLI_math_base.hh"
21#include "BLI_task.hh"
22#include "BLI_vector.hh"
23
24#include "DNA_brush_types.h"
25#include "DNA_mesh_types.h"
26#include "DNA_meshdata_types.h"
27#include "DNA_object_types.h"
28#include "DNA_particle_types.h"
29#include "DNA_scene_types.h"
30
31#include "RNA_access.hh"
32
33#include "BKE_attribute.hh"
34#include "BKE_brush.hh"
35#include "BKE_context.hh"
36#include "BKE_deform.hh"
37#include "BKE_editmesh.hh"
38#include "BKE_global.hh"
39#include "BKE_mesh.hh"
40#include "BKE_object_deform.h"
41#include "BKE_object_types.hh"
42#include "BKE_paint.hh"
43#include "BKE_report.hh"
44
45#include "DEG_depsgraph.hh"
46
47#include "WM_api.hh"
48#include "WM_message.hh"
49#include "WM_toolsystem.hh"
50#include "WM_types.hh"
51
52#include "ED_mesh.hh"
53#include "ED_object.hh"
54#include "ED_paint.hh"
55#include "ED_screen.hh"
56#include "ED_view3d.hh"
57
58/* For IMB_BlendMode only. */
59#include "IMB_imbuf.hh"
60
61#include "ED_object_vgroup.hh"
62
63#include "bmesh.hh"
64
65#include "RNA_define.hh"
66
67#include "../paint_intern.hh" /* own include */
68#include "mesh_brush_common.hh"
69#include "mesh_paint.hh"
70#include "sculpt_automask.hh"
71#include "sculpt_intern.hh"
72#include "vw_paint_intern.hh" /* own include */
73
74namespace blender {
75
76using namespace blender::ed::sculpt_paint;
77
80 double value;
81};
82
94 int index;
101 const bool *lock;
102};
103
104/* struct to avoid passing many args each call to do_weight_paint_vertex()
105 * this _could_ be made a part of the operators 'WPaintData' struct, or at
106 * least a member, but for now keep its own struct, initialized on every
107 * paint stroke update - campbell */
109
111
113
114 /* both must add up to 'defbase_tot' */
117
119
120 /* boolean array for locked bones,
121 * length of defbase_tot */
122 const bool *lock_flags;
123 /* boolean array for selected bones,
124 * length of defbase_tot, can't be const because of how it's passed */
125 const bool *defbase_sel;
126 /* same as WeightPaintData.vgroup_validmap,
127 * only added here for convenience */
128 const bool *vgroup_validmap;
129 /* same as WeightPaintData.vgroup_locked/unlocked,
130 * only added here for convenience */
131 const bool *vgroup_locked;
132 const bool *vgroup_unlocked;
133
139
140 float brush_alpha_value; /* result of BKE_brush_alpha_get() */
141};
142
143struct WPaintData : public PaintModeData {
145
147
148 /* variables for auto normalize */
149 const bool *vgroup_validmap; /* stores if vgroups tie to deforming bones or not */
150 const bool *lock_flags;
151 const bool *vgroup_locked; /* mask of locked defbones */
152 const bool *vgroup_unlocked; /* mask of unlocked defbones */
153
154 /* variables for multipaint */
155 const bool *defbase_sel; /* set of selected groups */
156 int defbase_tot_sel; /* number of selected groups */
157 bool do_multipaint; /* true if multipaint enabled and multiple groups selected */
159
161
162 /* original weight values for use in blur/smear */
165
166 /* Keep track of how much each vertex has been painted (non-airbrush only). */
168
169 /* Needed to continuously re-apply over the same weights (#BRUSH_ACCUMULATE disabled).
170 * Lazy initialize as needed (flag is set to 1 to tag it as uninitialized). */
172
174
175 ~WPaintData() override
176 {
177 if (!dvert_prev.is_empty()) {
179 }
180
181 MEM_SAFE_DELETE(defbase_sel);
182 MEM_SAFE_DELETE(vgroup_validmap);
183 MEM_SAFE_DELETE(vgroup_locked);
184 MEM_SAFE_DELETE(vgroup_unlocked);
185 MEM_SAFE_DELETE(lock_flags);
186 MEM_SAFE_DELETE(active.lock);
187 MEM_SAFE_DELETE(mirror.lock);
188 MEM_SAFE_DELETE(precomputed_weight);
189 }
190};
191
193 MDeformVert *dvert_curr,
194 int index)
195{
196 const MDeformVert *dv_curr = &dvert_curr[index];
197 MDeformVert *dv_prev = &dvert_prev[index];
198 if (dv_prev->flag == 1) {
199 dv_prev->flag = 0;
200 BKE_defvert_copy(dv_prev, dv_curr);
201 }
202 return dv_prev;
203}
204
205static float wpaint_blend(const VPaint &wp,
206 float weight,
207 const float alpha,
208 float paintval,
209 const float paint_factor,
210 const bool do_flip)
211{
213 IMB_BlendMode blend = IMB_BlendMode(brush.blend);
214
215 if (do_flip) {
216 switch (blend) {
217 case IMB_BLEND_MIX:
218 paintval = 1.0f - paintval;
219 break;
220 case IMB_BLEND_ADD:
221 blend = IMB_BLEND_SUB;
222 break;
223 case IMB_BLEND_SUB:
224 blend = IMB_BLEND_ADD;
225 break;
227 blend = IMB_BLEND_DARKEN;
228 break;
229 case IMB_BLEND_DARKEN:
230 blend = IMB_BLEND_LIGHTEN;
231 break;
232 default:
233 break;
234 }
235 }
236
237 weight = ED_wpaint_blend_tool(blend, weight, paintval * paint_factor, alpha);
238
239 CLAMP(weight, 0.0f, 1.0f);
240 /* The following is a reasonable lower bound for values that a user may want for weight values,
241 * without this rounding, attempting to paint to an exact value of 0.0 becomes tedious. */
242 constexpr float threshold = 0.0001f;
243 return weight < threshold ? 0.0f : weight;
244}
245
246static float wpaint_clamp_monotonic(float oldval, float curval, float newval)
247{
248 if (newval < oldval) {
249 return std::min(newval, curval);
250 }
251 if (newval > oldval) {
252 return std::max(newval, curval);
253 }
254 return newval;
255}
256
258 float weight, float old_weight, float locked_weight, float free_weight, bool auto_normalize)
259{
260 /* In auto-normalize mode, or when there is no unlocked weight,
261 * compute based on locked weight. */
262 if (auto_normalize || free_weight <= 0.0f) {
263 if (locked_weight < 1.0f - VERTEX_WEIGHT_LOCK_EPSILON) {
264 weight *= (1.0f - locked_weight);
265 }
266 else {
267 weight = 0;
268 }
269 }
270 else {
271 /* When dealing with full unlocked weight, don't paint, as it is always displayed as 1. */
272 if (old_weight >= free_weight) {
273 weight = old_weight;
274 }
275 /* Try to compute a weight value that would produce the desired effect if normalized. */
276 else if (weight < 1.0f) {
277 weight = weight * (free_weight - old_weight) / (1 - weight);
278 }
279 else {
280 weight = 1.0f;
281 }
282 }
283
284 return weight;
285}
286
287/* ----------------------------------------------------- */
288
290 const int defbase_tot,
291 const bool *vgroup_validmap)
292{
293 float sum = 0.0f, fac;
294 uint i, tot = 0;
295 MDeformWeight *dw;
296
297 for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
298 if (dw->def_nr < defbase_tot && vgroup_validmap[dw->def_nr]) {
299 tot++;
300 sum += dw->weight;
301 }
302 }
303
304 if ((tot == 0) || (sum == 1.0f)) {
305 return;
306 }
307
308 if (sum != 0.0f) {
309 fac = 1.0f / sum;
310
311 for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
312 if (dw->def_nr < defbase_tot && vgroup_validmap[dw->def_nr]) {
313 dw->weight *= fac;
314 }
315 }
316 }
317 else {
318 /* hrmf, not a factor in this case */
319 fac = 1.0f / tot;
320
321 for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
322 if (dw->def_nr < defbase_tot && vgroup_validmap[dw->def_nr]) {
323 dw->weight = fac;
324 }
325 }
326 }
327}
328
334 const int defbase_tot,
335 const bool *vgroup_validmap,
336 const bool *lock_flags)
337{
338 float sum = 0.0f, fac;
339 float sum_unlock = 0.0f;
340 float lock_weight = 0.0f;
341 uint i, tot = 0;
342 MDeformWeight *dw;
343
344 if (lock_flags == nullptr) {
345 do_weight_paint_normalize_all(dvert, defbase_tot, vgroup_validmap);
346 return true;
347 }
348
349 for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
350 if (dw->def_nr < defbase_tot && vgroup_validmap[dw->def_nr]) {
351 sum += dw->weight;
352
353 if (lock_flags[dw->def_nr]) {
354 lock_weight += dw->weight;
355 }
356 else {
357 tot++;
358 sum_unlock += dw->weight;
359 }
360 }
361 }
362
363 if (sum == 1.0f) {
364 return true;
365 }
366
367 if (tot == 0) {
368 return false;
369 }
370
371 if (lock_weight >= 1.0f - VERTEX_WEIGHT_LOCK_EPSILON) {
372 /* locked groups make it impossible to fully normalize,
373 * zero out what we can and return false */
374 for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
375 if (dw->def_nr < defbase_tot && vgroup_validmap[dw->def_nr]) {
376 if (lock_flags[dw->def_nr] == false) {
377 dw->weight = 0.0f;
378 }
379 }
380 }
381
382 return (lock_weight == 1.0f);
383 }
384 if (sum_unlock != 0.0f) {
385 fac = (1.0f - lock_weight) / sum_unlock;
386
387 for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
388 if (dw->def_nr < defbase_tot && vgroup_validmap[dw->def_nr]) {
389 if (lock_flags[dw->def_nr] == false) {
390 dw->weight *= fac;
391 /* paranoid but possibly with float error */
392 CLAMP(dw->weight, 0.0f, 1.0f);
393 }
394 }
395 }
396 }
397 else {
398 /* hrmf, not a factor in this case */
399 fac = (1.0f - lock_weight) / tot;
400 /* paranoid but possibly with float error */
401 CLAMP(fac, 0.0f, 1.0f);
402
403 for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
404 if (dw->def_nr < defbase_tot && vgroup_validmap[dw->def_nr]) {
405 if (lock_flags[dw->def_nr] == false) {
406 dw->weight = fac;
407 }
408 }
409 }
410 }
411
412 return true;
413}
414
420 const int defbase_tot,
421 const bool *vgroup_validmap,
422 const bool *lock_flags,
423 const bool *lock_with_active)
424{
425 /* first pass with both active and explicitly locked groups restricted from change */
426
428 dvert, defbase_tot, vgroup_validmap, lock_with_active);
429
430 if (!success) {
440 do_weight_paint_normalize_all_locked(dvert, defbase_tot, vgroup_validmap, lock_flags);
441 }
442}
443
444#if 0 /* UNUSED */
445static bool has_unselected_unlocked_bone_group(int defbase_tot,
446 bool *defbase_sel,
447 int selected,
448 const bool *lock_flags,
449 const bool *vgroup_validmap)
450{
451 int i;
452 if (defbase_tot == selected) {
453 return false;
454 }
455 for (i = 0; i < defbase_tot; i++) {
456 if (vgroup_validmap[i] && !defbase_sel[i] && !lock_flags[i]) {
457 return true;
458 }
459 }
460 return false;
461}
462#endif
464 const int defbase_tot,
465 const bool *defbase_sel,
466 float *change_p)
467{
468 int i;
469 MDeformWeight *dw;
470 float val;
471 float change = *change_p;
472
473 /* verify that the change does not cause values exceeding 1 and clamp it */
474 for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
475 if (dw->def_nr < defbase_tot && defbase_sel[dw->def_nr]) {
476 if (dw->weight) {
477 val = dw->weight * change;
478 if (val > 1) {
479 change = 1.0f / dw->weight;
480 }
481 }
482 }
483 }
484
485 *change_p = change;
486}
487
489 const int defbase_tot,
490 float change,
491 const bool *defbase_sel)
492{
493 int i;
494 MDeformWeight *dw;
495 float val;
496
497 /* in case the change is reduced, you need to recheck
498 * the earlier values to make sure they are not 0
499 * (precision error) */
500 for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
501 if (dw->def_nr < defbase_tot && defbase_sel[dw->def_nr]) {
502 if (dw->weight) {
503 val = dw->weight * change;
504 /* the value should never reach zero while multi-painting if it
505 * was nonzero beforehand */
506 if (val <= 0) {
507 return false;
508 }
509 }
510 }
511 }
512
513 return true;
514}
515
517 const int defbase_tot,
518 float change,
519 const bool *defbase_sel)
520{
521 int i;
522 MDeformWeight *dw;
523
524 for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
525 if (dw->def_nr < defbase_tot && defbase_sel[dw->def_nr]) {
526 if (dw->weight) {
527 dw->weight = dw->weight * change;
528 CLAMP(dw->weight, 0.0f, 1.0f);
529 }
530 }
531 }
532}
533
535 Object &ob,
536 WPaintData &wpd,
537 const WeightPaintInfo &wpi,
538 const uint index,
539 float alpha,
540 float paintweight,
541 float automasking_factor)
542{
543 Mesh *mesh = id_cast<Mesh *>(ob.data);
544 MDeformVert *dv = &wpi.dvert[index];
545 bool topology = (mesh->editflag & ME_EDIT_MIRROR_TOPO) != 0;
546
547 MDeformWeight *dw;
548 float weight_prev, weight_cur;
549 float dw_rel_locked = 0.0f, dw_rel_free = 1.0f;
550
551 int index_mirr;
552 int vgroup_mirr;
553
554 MDeformVert *dv_mirr;
555 MDeformWeight *dw_mirr;
556
557 /* Check if we should mirror vertex groups (X-axis). */
559 index_mirr = mesh_get_x_mirror_vert(&ob, nullptr, index, topology);
560 vgroup_mirr = wpi.mirror.index;
561
562 /* another possible error - mirror group _and_ active group are the same (which is fine),
563 * but we also are painting onto a center vertex - this would paint the same weight twice */
564 if (index_mirr == index && vgroup_mirr == wpi.active.index) {
565 index_mirr = vgroup_mirr = -1;
566 }
567 }
568 else {
569 index_mirr = vgroup_mirr = -1;
570 }
571
572 /* Check if painting should create new deform weight entries. */
573 bool restrict_to_existing = (wp.flag & VP_FLAG_VGROUP_RESTRICT) != 0;
574
575 if (wpi.do_lock_relative || wpi.do_auto_normalize) {
576 /* Without do_lock_relative only dw_rel_locked is reliable, while dw_rel_free may be fake 0. */
578 dw_rel_locked = BKE_defvert_total_selected_weight(dv, wpi.defbase_tot, wpi.vgroup_locked);
579 CLAMP(dw_rel_locked, 0.0f, 1.0f);
580
581 /* Do not create entries if there is not enough free weight to paint.
582 * This logic is the same as in wpaint_undo_lock_relative and auto-normalize. */
583 if (wpi.do_auto_normalize || dw_rel_free <= 0.0f) {
584 if (dw_rel_locked >= 1.0f - VERTEX_WEIGHT_LOCK_EPSILON) {
585 restrict_to_existing = true;
586 }
587 }
588 }
589
590 if (restrict_to_existing) {
591 dw = BKE_defvert_find_index(dv, wpi.active.index);
592 }
593 else {
595 }
596
597 if (dw == nullptr) {
598 return;
599 }
600
601 if (index_mirr != -1) {
602 dv_mirr = &wpi.dvert[index_mirr];
603 if (wp.flag & VP_FLAG_VGROUP_RESTRICT) {
604 dw_mirr = BKE_defvert_find_index(dv_mirr, vgroup_mirr);
605
606 if (dw_mirr == nullptr) {
607 index_mirr = vgroup_mirr = -1;
608 dv_mirr = nullptr;
609 }
610 }
611 else {
612 if (index != index_mirr) {
613 dw_mirr = BKE_defvert_ensure_index(dv_mirr, vgroup_mirr);
614 }
615 else {
616 /* dv and dv_mirr are the same */
617 int totweight_prev = dv_mirr->totweight;
618 int dw_offset = int(dw - dv_mirr->dw);
619 dw_mirr = BKE_defvert_ensure_index(dv_mirr, vgroup_mirr);
620
621 /* if we added another, get our old one back */
622 if (totweight_prev != dv_mirr->totweight) {
623 dw = &dv_mirr->dw[dw_offset];
624 }
625 }
626 }
627 }
628 else {
629 dv_mirr = nullptr;
630 dw_mirr = nullptr;
631 }
632
633 weight_cur = dw->weight;
634
635 /* Handle weight caught up in locked defgroups for Lock Relative. */
636 if (wpi.do_lock_relative) {
637 weight_cur = BKE_defvert_calc_lock_relative_weight(weight_cur, dw_rel_locked, dw_rel_free);
638 }
639
641 MDeformVert *dvert_prev = wpd.dvert_prev.data();
642 MDeformVert *dv_prev = defweight_prev_init(dvert_prev, wpi.dvert.data(), index);
643 if (index_mirr != -1) {
644 defweight_prev_init(dvert_prev, wpi.dvert.data(), index_mirr);
645 }
646
647 weight_prev = BKE_defvert_find_weight(dv_prev, wpi.active.index);
648
649 if (wpi.do_lock_relative) {
651 weight_prev, dv_prev, wpi.defbase_tot, wpi.vgroup_locked, wpi.vgroup_unlocked);
652 }
653 }
654 else {
655 weight_prev = weight_cur;
656 }
657
658 /* If there are no normalize-locks or multipaint,
659 * then there is no need to run the more complicated checks */
660
661 {
662 float new_weight = wpaint_blend(
663 wp, weight_prev, alpha, paintweight, automasking_factor, wpi.do_flip);
664
665 float weight = wpaint_clamp_monotonic(weight_prev, weight_cur, new_weight);
666
667 /* Undo the lock relative weight correction. */
668 if (wpi.do_lock_relative) {
669 if (index_mirr == index) {
670 /* When painting a center vertex with X Mirror and L/R pair,
671 * handle both groups together. This avoids weird fighting
672 * in the non-normalized weight mode. */
673 float orig_weight = dw->weight + dw_mirr->weight;
674 weight = 0.5f *
676 weight * 2, orig_weight, dw_rel_locked, dw_rel_free, wpi.do_auto_normalize);
677 }
678 else {
680 weight, dw->weight, dw_rel_locked, dw_rel_free, wpi.do_auto_normalize);
681 }
682
683 CLAMP(weight, 0.0f, 1.0f);
684 }
685
686 dw->weight = weight;
687
688 /* WATCH IT: take care of the ordering of applying mirror -> normalize,
689 * can give wrong results #26193, least confusing if normalize is done last */
690
691 if (index_mirr != -1) {
692 dw_mirr->weight = dw->weight;
693 }
694
695 if (wpi.do_auto_normalize) {
696 /* note on normalize - this used to be applied after painting and normalize all weights,
697 * in some ways this is good because there is feedback where the more weights involved would
698 * 'resist' so you couldn't instantly zero out other weights by painting 1.0 on the active.
699 *
700 * However this gave a problem since applying mirror, then normalize both verts
701 * the resulting weight won't match on both sides.
702 *
703 * If this 'resisting', slower normalize is nicer, we could call
704 * do_weight_paint_normalize_all() and only use...
705 * do_weight_paint_normalize_all_active() when normalizing the mirror vertex.
706 * - campbell
707 */
709 dv, wpi.defbase_tot, wpi.vgroup_validmap, wpi.lock_flags, wpi.active.lock);
710
711 if (index_mirr != -1) {
712 /* only normalize if this is not a center vertex,
713 * else we get a conflict, normalizing twice */
714 if (index != index_mirr) {
716 dv_mirr, wpi.defbase_tot, wpi.vgroup_validmap, wpi.lock_flags, wpi.mirror.lock);
717 }
718 else {
719 /* This case accounts for:
720 * - Painting onto a center vertex of a mesh.
721 * - X-mirror is enabled.
722 * - Auto normalize is enabled.
723 * - The group you are painting onto has a L / R version.
724 *
725 * We want L/R vgroups to have the same weight but this can't be if both are over 0.5,
726 * We _could_ have special check for that, but this would need its own
727 * normalize function which holds 2 groups from changing at once.
728 *
729 * So! just balance out the 2 weights, it keeps them equal and everything normalized.
730 *
731 * While it won't hit the desired weight immediately as the user waggles their mouse,
732 * constant painting and re-normalizing will get there. this is also just simpler logic.
733 * - campbell */
734 dw_mirr->weight = dw->weight = (dw_mirr->weight + dw->weight) * 0.5f;
735 }
736 }
737 }
738 }
739}
740
742 Object &ob,
743 WPaintData &wpd,
744 const WeightPaintInfo &wpi,
745 const uint index,
746 float alpha,
747 float paintweight,
748 float additional_factor)
749{
750 Mesh *mesh = id_cast<Mesh *>(ob.data);
751 MDeformVert *dv = &wpi.dvert[index];
752 bool topology = (mesh->editflag & ME_EDIT_MIRROR_TOPO) != 0;
753
754 int index_mirr = -1;
755 MDeformVert *dv_mirr = nullptr;
756
757 float curw, curw_real, oldw, neww, change, curw_mirr, change_mirr;
758 float dw_rel_free, dw_rel_locked;
759
760 /* Check if we should mirror vertex groups (X-axis). */
762 index_mirr = mesh_get_x_mirror_vert(&ob, nullptr, index, topology);
763
764 if (!ELEM(index_mirr, -1, index)) {
765 dv_mirr = &wpi.dvert[index_mirr];
766 }
767 else {
768 index_mirr = -1;
769 }
770 }
771
772 /* compute weight change by applying the brush to average or sum of group weights */
775
776 if (curw == 0.0f) {
777 /* NOTE: no weight to assign to this vertex, could add all groups? */
778 return;
779 }
780
781 /* Handle weight caught up in locked defgroups for Lock Relative. */
782 if (wpi.do_lock_relative) {
784 dw_rel_locked = BKE_defvert_total_selected_weight(dv, wpi.defbase_tot, wpi.vgroup_locked);
785 CLAMP(dw_rel_locked, 0.0f, 1.0f);
786
787 curw = BKE_defvert_calc_lock_relative_weight(curw, dw_rel_locked, dw_rel_free);
788 }
789
791 MDeformVert *dvert_prev = wpd.dvert_prev.data();
792 MDeformVert *dv_prev = defweight_prev_init(dvert_prev, wpi.dvert.data(), index);
793 if (index_mirr != -1) {
794 defweight_prev_init(dvert_prev, wpi.dvert.data(), index_mirr);
795 }
796
798 dv_prev, wpi.defbase_tot, wpi.defbase_sel, wpi.defbase_tot_sel, wpi.is_normalized);
799
800 if (wpi.do_lock_relative) {
802 oldw, dv_prev, wpi.defbase_tot, wpi.vgroup_locked, wpi.vgroup_unlocked);
803 }
804 }
805 else {
806 oldw = curw;
807 }
808
809 neww = wpaint_blend(wp, oldw, alpha, paintweight, additional_factor, wpi.do_flip);
810 neww = wpaint_clamp_monotonic(oldw, curw, neww);
811
812 if (wpi.do_lock_relative) {
814 neww, curw_real, dw_rel_locked, dw_rel_free, wpi.do_auto_normalize);
815 }
816
817 change = neww / curw_real;
818
819 /* verify for all groups that 0 < result <= 1 */
820 multipaint_clamp_change(dv, wpi.defbase_tot, wpi.defbase_sel, &change);
821
822 if (dv_mirr != nullptr) {
824 dv_mirr, wpi.defbase_tot, wpi.defbase_sel, wpi.defbase_tot_sel, wpi.is_normalized);
825
826 if (curw_mirr == 0.0f) {
827 /* can't mirror into a zero weight vertex */
828 dv_mirr = nullptr;
829 }
830 else {
831 /* mirror is changed to achieve the same collective weight value */
832 float orig = change_mirr = curw_real * change / curw_mirr;
833
834 multipaint_clamp_change(dv_mirr, wpi.defbase_tot, wpi.defbase_sel, &change_mirr);
835
836 if (!multipaint_verify_change(dv_mirr, wpi.defbase_tot, change_mirr, wpi.defbase_sel)) {
837 return;
838 }
839
840 change *= change_mirr / orig;
841 }
842 }
843
844 if (!multipaint_verify_change(dv, wpi.defbase_tot, change, wpi.defbase_sel)) {
845 return;
846 }
847
848 /* apply validated change to vertex and mirror */
849 multipaint_apply_change(dv, wpi.defbase_tot, change, wpi.defbase_sel);
850
851 if (dv_mirr != nullptr) {
852 multipaint_apply_change(dv_mirr, wpi.defbase_tot, change_mirr, wpi.defbase_sel);
853 }
854
855 if (wpi.do_auto_normalize) {
857 dv, wpi.defbase_tot, wpi.vgroup_validmap, wpi.lock_flags, wpi.active.lock);
858
859 if (dv_mirr != nullptr) {
861 dv_mirr, wpi.defbase_tot, wpi.vgroup_validmap, wpi.lock_flags, wpi.active.lock);
862 }
863 }
864}
865
871static void do_weight_paint_vertex(const VPaint &wp,
872 Object &ob,
873 WPaintData &wpd,
874 const WeightPaintInfo &wpi,
875 const uint index,
876 float final_alpha,
877 float paintweight,
878 float automasking_factor = 1.0f)
879{
880 if (wpi.do_multipaint) {
882 wp, ob, wpd, wpi, index, final_alpha, paintweight, automasking_factor);
883 }
884 else {
886 wp, ob, wpd, wpi, index, final_alpha, paintweight, automasking_factor);
887 }
888}
889
895
904
905 std::optional<float3> get_location(float2 mouse, bool force_original) override;
906 bool test_start(wmOperator *op, float2 mouse) override;
907 void redraw(bool final) override;
908 bool test_cancel() override;
909 void update_step(wmOperator *op, const StrokeStep &stroke_step) override;
910 void done(bool is_cancel, bool stroke_started) override;
911};
912
913std::optional<float3> WeightPaintStroke::get_location(const float2 mouse, bool force_original)
914{
916 *this->depsgraph, this->vc, *this->paint, this->brush, mouse, force_original);
917}
918
919static void init_session_data(const VPaint &wpaint, Object &ob, WPaintData &wpd)
920{
923
924 Mesh *mesh = id_cast<Mesh *>(ob.data);
925
926 /* Create average brush arrays */
927 if (!vwpaint::brush_use_accumulate(wpaint)) {
928 wpd.alpha_weight = Array<float>(mesh->verts_num, 0.0f);
929 MDeformVert initial_value{};
930 /* Use to show this isn't initialized, never apply to the mesh data. */
931 initial_value.flag = 1;
932 wpd.dvert_prev = Array<MDeformVert>(mesh->verts_num, initial_value);
933 }
934}
935
937{
938 Scene &scene = *this->scene;
940 Object &ob = *this->object;
942 WPaintVGroupIndex vgroup_index;
943 int defbase_tot, defbase_tot_sel;
944 bool *defbase_sel;
946 VPaint &wp = *weight_paint_;
947
949 this->evil_C, bmain_, this->object, op->reports, WPAINT_ENSURE_MIRROR, &vgroup_index) ==
950 false)
951 {
952 return false;
953 }
954
955 {
956 /* check if we are attempting to paint onto a locked vertex group,
957 * and other options disallow it from doing anything useful */
958 bDeformGroup *dg;
959 dg = static_cast<bDeformGroup *>(BLI_findlink(&mesh.vertex_group_names, vgroup_index.active));
960 if (dg->flag & DG_LOCK_WEIGHT) {
961 BKE_report(op->reports, RPT_WARNING, "Active group is locked, aborting");
962 return false;
963 }
964 if (vgroup_index.mirror != -1) {
965 dg = static_cast<bDeformGroup *>(
966 BLI_findlink(&mesh.vertex_group_names, vgroup_index.mirror));
967 if (dg->flag & DG_LOCK_WEIGHT) {
968 BKE_report(op->reports, RPT_WARNING, "Mirror group is locked, aborting");
969 return false;
970 }
971 }
972 }
973
974 /* check that multipaint groups are unlocked */
975 defbase_tot = mesh.vertex_group_names.count();
976 defbase_sel = BKE_object_defgroup_selected_get(&ob, defbase_tot, &defbase_tot_sel);
977
978 if (ts.multipaint && defbase_tot_sel > 1) {
979 int i;
980 bDeformGroup *dg;
981
984 &ob, defbase_tot, defbase_sel, defbase_sel, &defbase_tot_sel);
985 }
986
987 for (i = 0; i < defbase_tot; i++) {
988 if (defbase_sel[i]) {
989 dg = static_cast<bDeformGroup *>(BLI_findlink(&mesh.vertex_group_names, i));
990 if (dg->flag & DG_LOCK_WEIGHT) {
991 BKE_report(op->reports, RPT_WARNING, "Multipaint group is locked, aborting");
992 MEM_delete(defbase_sel);
993 return false;
994 }
995 }
996 }
997 }
998
999 std::unique_ptr<WPaintData> wpd = std::make_unique<WPaintData>();
1000 wpd->vc = this->vc;
1001
1003
1004 wpd->active.index = vgroup_index.active;
1005 wpd->mirror.index = vgroup_index.mirror;
1006
1007 /* multipaint */
1008 wpd->defbase_tot = defbase_tot;
1009 wpd->defbase_sel = defbase_sel;
1010 wpd->defbase_tot_sel = defbase_tot_sel > 1 ? defbase_tot_sel : 1;
1011 wpd->do_multipaint = (ts.multipaint && defbase_tot_sel > 1);
1012
1013 /* set up auto-normalize, and generate map for detecting which
1014 * vgroups affect deform bones */
1015 wpd->lock_flags = BKE_object_defgroup_lock_flags_get(&ob, wpd->defbase_tot);
1016 if (ts.auto_normalize || ts.multipaint || wpd->lock_flags != nullptr || ts.wpaint_lock_relative)
1017 {
1018 wpd->vgroup_validmap = BKE_object_defgroup_validmap_get(&ob, wpd->defbase_tot);
1019 }
1020
1021 /* Compute the set of all locked deform groups when Lock Relative is active. */
1022 if (ts.wpaint_lock_relative &&
1024 wpd->lock_flags, wpd->vgroup_validmap, wpd->active.index) &&
1025 (!wpd->do_multipaint || BKE_object_defgroup_check_lock_relative_multi(
1026 defbase_tot, wpd->lock_flags, defbase_sel, defbase_tot_sel)))
1027 {
1028 wpd->do_lock_relative = true;
1029 }
1030
1031 if (wpd->do_lock_relative || (ts.auto_normalize && wpd->lock_flags && !wpd->do_multipaint)) {
1032 bool *unlocked = MEM_dupalloc(wpd->vgroup_validmap);
1033
1034 if (wpd->lock_flags) {
1035 bool *locked = MEM_new_array_uninitialized<bool>(wpd->defbase_tot, __func__);
1037 wpd->defbase_tot, wpd->lock_flags, wpd->vgroup_validmap, locked, unlocked);
1038 wpd->vgroup_locked = locked;
1039 }
1040
1041 wpd->vgroup_unlocked = unlocked;
1042 }
1043
1044 if (wpd->do_multipaint && ts.auto_normalize) {
1045 bool *tmpflags;
1046 tmpflags = MEM_new_array_uninitialized<bool>(defbase_tot, __func__);
1047 if (wpd->lock_flags) {
1048 BLI_array_binary_or(tmpflags, wpd->defbase_sel, wpd->lock_flags, wpd->defbase_tot);
1049 }
1050 else {
1051 memcpy(tmpflags, wpd->defbase_sel, sizeof(*tmpflags) * wpd->defbase_tot);
1052 }
1053 wpd->active.lock = tmpflags;
1054 }
1055 else if (ts.auto_normalize) {
1056 bool *tmpflags;
1057
1058 tmpflags = wpd->lock_flags ? MEM_dupalloc(wpd->lock_flags) :
1059 MEM_new_array_zeroed<bool>(defbase_tot, __func__);
1060 tmpflags[wpd->active.index] = true;
1061 wpd->active.lock = tmpflags;
1062
1063 tmpflags = wpd->lock_flags ? MEM_dupalloc(wpd->lock_flags) :
1064 MEM_new_array_zeroed<bool>(defbase_tot, __func__);
1065 tmpflags[(wpd->mirror.index != -1) ? wpd->mirror.index : wpd->active.index] = true;
1066 wpd->mirror.lock = tmpflags;
1067 }
1068
1069 /* If not previously created, create vertex/weight paint mode session data */
1070 vwpaint::update_cache_invariants(wp, ss, op, mouse);
1071 init_session_data(wp, ob, *wpd);
1072
1073 /* Brush may have changed after initialization. */
1076 wpd->precomputed_weight = MEM_new_array_uninitialized<float>(mesh.verts_num, __func__);
1077 }
1078
1079 mode_data_ = std::move(wpd);
1080
1081 return true;
1082}
1083
1084void WeightPaintStroke::redraw(bool /*final*/) {}
1086{
1087 return false;
1088}
1089
1090static float wpaint_get_active_weight(const MDeformVert &dv, const WeightPaintInfo &wpi)
1091{
1092 float weight;
1093
1094 if (wpi.do_multipaint) {
1096 &dv, wpi.defbase_tot, wpi.defbase_sel, wpi.defbase_tot_sel, wpi.is_normalized);
1097 }
1098 else {
1099 weight = BKE_defvert_find_weight(&dv, wpi.active.index);
1100 }
1101
1102 if (wpi.do_lock_relative) {
1104 weight, &dv, wpi.defbase_tot, wpi.vgroup_locked, wpi.vgroup_unlocked);
1105 }
1106
1107 CLAMP(weight, 0.0f, 1.0f);
1108 return weight;
1109}
1110
1112 Object &ob, const Brush &brush, WPaintData &wpd, WeightPaintInfo &wpi, Mesh &mesh)
1113{
1115 return;
1116 }
1117
1118 threading::parallel_for(IndexRange(mesh.verts_num), 512, [&](const IndexRange range) {
1119 for (const int i : range) {
1120 const MDeformVert &dv = wpi.dvert[i];
1121 wpd.precomputed_weight[i] = wpaint_get_active_weight(dv, wpi);
1122 }
1123 });
1124
1125 wpd.precomputed_weight_ready = true;
1126}
1127
1128/* -------------------------------------------------------------------- */
1131
1133 const IndexMask &node_mask,
1134 FunctionRef<void(IndexRange)> fn)
1135{
1136 /* NOTE: current mirroring code cannot be run in parallel */
1138 fn(node_mask.index_range());
1139 }
1140 else {
1142 node_mask.index_range(), 1, [&](const IndexRange range) { fn(range); });
1143 }
1144}
1145
1146static void filter_factors_with_selection(const Span<bool> select_vert,
1147 const Span<int> verts,
1148 const MutableSpan<float> factors)
1149{
1150 BLI_assert(verts.size() == factors.size());
1151
1152 for (const int i : verts.index_range()) {
1153 if (!select_vert[verts[i]]) {
1154 factors[i] = 0.0f;
1155 }
1156 }
1157}
1158
1159static void do_wpaint_brush_blur(const Depsgraph &depsgraph,
1160 Object &ob,
1161 const Brush &brush,
1162 VPaint &vp,
1163 WPaintData &wpd,
1164 const WeightPaintInfo &wpi,
1165 Mesh &mesh,
1166 const IndexMask &node_mask)
1167{
1170 const StrokeCache &cache = *ss.cache;
1171 const GroupedSpan<int> vert_to_face = mesh.vert_to_face_map();
1172
1173 float brush_size_pressure, brush_alpha_value, brush_alpha_pressure;
1175 ss, vp.paint, brush, &brush_size_pressure, &brush_alpha_value, &brush_alpha_pressure);
1176 const bool use_normal = vwpaint::use_normal(vp);
1177 const bool use_face_sel = (mesh.editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
1178 const bool use_vert_sel = (mesh.editflag & ME_EDIT_PAINT_VERT_SEL) != 0;
1179
1180 const float *sculpt_normal_frontface = brush_frontface_normal_from_falloff_shape(
1181 ss, brush.falloff_shape);
1182
1183 const Span<float3> vert_positions = bke::pbvh::vert_positions_eval(depsgraph, ob);
1184 const OffsetIndices faces = mesh.faces();
1185 const Span<int> corner_verts = mesh.corner_verts();
1186 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(depsgraph, ob);
1187 const bke::AttributeAccessor attributes = mesh.attributes();
1188 const VArraySpan hide_vert = *attributes.lookup<bool>(".hide_vert", bke::AttrDomain::Point);
1189 VArraySpan<bool> select_vert;
1190 if (use_vert_sel || use_face_sel) {
1191 select_vert = *attributes.lookup<bool>(".select_vert", bke::AttrDomain::Point);
1192 }
1193
1194 parallel_nodes_loop_with_mirror_check(mesh, node_mask, [&](const IndexRange range) {
1195 node_mask.slice(range).foreach_index([&](const int i) {
1196 const Span<int> verts = nodes[i].verts();
1198 fill_factor_from_hide(hide_vert, verts, factors);
1199 filter_region_clip_factors(ss, vert_positions, verts, factors);
1200 if (!select_vert.is_empty()) {
1201 filter_factors_with_selection(select_vert, verts, factors);
1202 }
1203
1206 ss, vert_positions, verts, eBrushFalloffShape(brush.falloff_shape), distances);
1207 filter_distances_with_radius(cache.radius, distances, factors);
1208 calc_brush_strength_factors(cache, brush, distances, factors);
1209
1211 depsgraph, ob, cache.automasking.get(), nodes[i], verts, factors);
1212
1213 for (const int i : verts.index_range()) {
1214 const int vert = verts[i];
1215 if (factors[i] == 0.0f) {
1216 continue;
1217 }
1218
1219 /* Get the average face weight */
1220 int total_hit_loops = 0;
1221 float weight_final = 0.0f;
1222 for (const int face : vert_to_face[vert]) {
1223 total_hit_loops += faces[face].size();
1224 for (const int vert : corner_verts.slice(faces[face])) {
1225 weight_final += wpd.precomputed_weight[vert];
1226 }
1227 }
1228
1229 if (total_hit_loops == 0) {
1230 continue;
1231 }
1232
1233 float brush_strength = cache.bstrength;
1234 const float angle_cos = use_normal ?
1235 dot_v3v3(sculpt_normal_frontface, vert_normals[vert]) :
1236 1.0f;
1237 if (!vwpaint::test_brush_angle_falloff(brush, angle_cos)) {
1238 continue;
1239 }
1240
1241 const float final_alpha = factors[i] * brush_strength * brush_alpha_pressure;
1242
1243 if ((brush.flag & BRUSH_ACCUMULATE) == 0) {
1244 if (wpd.alpha_weight[vert] < final_alpha) {
1245 wpd.alpha_weight[vert] = final_alpha;
1246 }
1247 else {
1248 continue;
1249 }
1250 }
1251
1252 weight_final /= total_hit_loops;
1253 do_weight_paint_vertex(vp, ob, wpd, wpi, vert, final_alpha, weight_final);
1254 }
1255 });
1256 });
1257}
1258
1259static void do_wpaint_brush_smear(const Depsgraph &depsgraph,
1260 Object &ob,
1261 const Brush &brush,
1262 VPaint &vp,
1263 WPaintData &wpd,
1264 const WeightPaintInfo &wpi,
1265 Mesh &mesh,
1266 const IndexMask &node_mask)
1267{
1270 const GroupedSpan<int> vert_to_face = mesh.vert_to_face_map();
1271 const StrokeCache &cache = *ss.cache;
1272
1274 /* We need a directional component to calculate the effect of this brush */
1275 return;
1276 }
1277
1278 float brush_size_pressure, brush_alpha_value, brush_alpha_pressure;
1280 ss, vp.paint, brush, &brush_size_pressure, &brush_alpha_value, &brush_alpha_pressure);
1281 const bool use_normal = vwpaint::use_normal(vp);
1282 const bool use_face_sel = (mesh.editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
1283 const bool use_vert_sel = (mesh.editflag & ME_EDIT_PAINT_VERT_SEL) != 0;
1284 float brush_dir[3];
1285
1286 sub_v3_v3v3(brush_dir, cache.location_symm, cache.last_location_symm);
1287 project_plane_v3_v3v3(brush_dir, brush_dir, cache.view_normal_symm);
1288 if (normalize_v3(brush_dir) == 0.0f) {
1289 return;
1290 }
1291
1292 const Span<float3> vert_positions = bke::pbvh::vert_positions_eval(depsgraph, ob);
1293 const OffsetIndices faces = mesh.faces();
1294 const Span<int> corner_verts = mesh.corner_verts();
1295 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(depsgraph, ob);
1296 const bke::AttributeAccessor attributes = mesh.attributes();
1297 const VArraySpan hide_vert = *attributes.lookup<bool>(".hide_vert", bke::AttrDomain::Point);
1298 VArraySpan<bool> select_vert;
1299 if (use_vert_sel || use_face_sel) {
1300 select_vert = *attributes.lookup<bool>(".select_vert", bke::AttrDomain::Point);
1301 }
1302
1303 const float *sculpt_normal_frontface = brush_frontface_normal_from_falloff_shape(
1304 ss, brush.falloff_shape);
1305
1306 parallel_nodes_loop_with_mirror_check(mesh, node_mask, [&](const IndexRange range) {
1307 node_mask.slice(range).foreach_index([&](const int i) {
1308 const Span<int> verts = nodes[i].verts();
1310 fill_factor_from_hide(hide_vert, verts, factors);
1311 filter_region_clip_factors(ss, vert_positions, verts, factors);
1312 if (!select_vert.is_empty()) {
1313 filter_factors_with_selection(select_vert, verts, factors);
1314 }
1315
1318 ss, vert_positions, verts, eBrushFalloffShape(brush.falloff_shape), distances);
1319 filter_distances_with_radius(cache.radius, distances, factors);
1320 calc_brush_strength_factors(cache, brush, distances, factors);
1321
1323 depsgraph, ob, cache.automasking.get(), nodes[i], verts, factors);
1324
1325 for (const int i : verts.index_range()) {
1326 const int vert = verts[i];
1327 if (factors[i] == 0.0f) {
1328 continue;
1329 }
1330
1331 float brush_strength = cache.bstrength;
1332 const float angle_cos = use_normal ?
1333 dot_v3v3(sculpt_normal_frontface, vert_normals[vert]) :
1334 1.0f;
1335 if (!vwpaint::test_brush_angle_falloff(brush, angle_cos)) {
1336 continue;
1337 }
1338
1339 bool do_color = false;
1340 /* Minimum dot product between brush direction and current
1341 * to neighbor direction is 0.0, meaning orthogonal. */
1342 float stroke_dot_max = 0.0f;
1343
1344 /* Get the color of the loop in the opposite direction of the brush movement
1345 * (this callback is specifically for smear.) */
1346 float weight_final = 0.0;
1347 for (const int face : vert_to_face[vert]) {
1348 for (const int vert_other : corner_verts.slice(faces[face])) {
1349 if (vert_other == vert) {
1350 continue;
1351 }
1352
1353 /* Get the direction from the selected vert to the neighbor. */
1354 float other_dir[3];
1355 sub_v3_v3v3(other_dir, vert_positions[vert], vert_positions[vert_other]);
1356 project_plane_v3_v3v3(other_dir, other_dir, cache.view_normal_symm);
1357
1358 normalize_v3(other_dir);
1359
1360 const float stroke_dot = dot_v3v3(other_dir, brush_dir);
1361
1362 if (stroke_dot > stroke_dot_max) {
1363 stroke_dot_max = stroke_dot;
1364 weight_final = wpd.precomputed_weight[vert_other];
1365 do_color = true;
1366 }
1367 }
1368 if (!do_color) {
1369 continue;
1370 }
1371 const float final_alpha = factors[i] * brush_strength * brush_alpha_pressure;
1372 do_weight_paint_vertex(vp, ob, wpd, wpi, vert, final_alpha, weight_final);
1373 }
1374 }
1375 });
1376 });
1377}
1378
1379static void do_wpaint_brush_draw(const Depsgraph &depsgraph,
1380 Object &ob,
1381 const Brush &brush,
1382 VPaint &vp,
1383 WPaintData &wpd,
1384 const WeightPaintInfo &wpi,
1385 Mesh &mesh,
1386 const float strength,
1387 const IndexMask &node_mask)
1388{
1391
1392 const StrokeCache &cache = *ss.cache;
1393 /* NOTE: normally `BKE_brush_weight_get(scene, brush)` is used,
1394 * however in this case we calculate a new weight each time. */
1395 const float paintweight = strength;
1396 float brush_size_pressure, brush_alpha_value, brush_alpha_pressure;
1398 ss, vp.paint, brush, &brush_size_pressure, &brush_alpha_value, &brush_alpha_pressure);
1399 const bool use_normal = vwpaint::use_normal(vp);
1400 const bool use_face_sel = (mesh.editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
1401 const bool use_vert_sel = (mesh.editflag & ME_EDIT_PAINT_VERT_SEL) != 0;
1402
1403 const float *sculpt_normal_frontface = brush_frontface_normal_from_falloff_shape(
1404 ss, brush.falloff_shape);
1405
1406 const Span<float3> vert_positions = bke::pbvh::vert_positions_eval(depsgraph, ob);
1407 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(depsgraph, ob);
1408 const bke::AttributeAccessor attributes = mesh.attributes();
1409 const VArraySpan hide_vert = *attributes.lookup<bool>(".hide_vert", bke::AttrDomain::Point);
1410 VArraySpan<bool> select_vert;
1411 if (use_vert_sel || use_face_sel) {
1412 select_vert = *attributes.lookup<bool>(".select_vert", bke::AttrDomain::Point);
1413 }
1414
1415 parallel_nodes_loop_with_mirror_check(mesh, node_mask, [&](const IndexRange range) {
1416 node_mask.slice(range).foreach_index([&](const int i) {
1417 const Span<int> verts = nodes[i].verts();
1419 fill_factor_from_hide(hide_vert, verts, factors);
1420 filter_region_clip_factors(ss, vert_positions, verts, factors);
1421 if (!select_vert.is_empty()) {
1422 filter_factors_with_selection(select_vert, verts, factors);
1423 }
1424
1427 ss, vert_positions, verts, eBrushFalloffShape(brush.falloff_shape), distances);
1428 filter_distances_with_radius(cache.radius, distances, factors);
1429 calc_brush_strength_factors(cache, brush, distances, factors);
1430
1432 if (cache.automasking) {
1433 automask_factors.reinitialize(verts.size());
1434 automask_factors.fill(1.0f);
1436 depsgraph, ob, *cache.automasking, nodes[i], verts, automask_factors);
1437 scale_factors(factors, automask_factors);
1438 }
1439
1440 for (const int i : verts.index_range()) {
1441 const int vert = verts[i];
1442 if (factors[i] == 0.0f) {
1443 continue;
1444 }
1445 float brush_strength = cache.bstrength;
1446 const float angle_cos = use_normal ?
1447 dot_v3v3(sculpt_normal_frontface, vert_normals[vert]) :
1448 1.0f;
1449 if (!vwpaint::test_brush_angle_falloff(brush, angle_cos)) {
1450 continue;
1451 }
1452 const float final_alpha = factors[i] * brush_strength * brush_alpha_pressure;
1453
1454 if ((brush.flag & BRUSH_ACCUMULATE) == 0) {
1455 if (wpd.alpha_weight[vert] < final_alpha) {
1456 wpd.alpha_weight[vert] = final_alpha;
1457 }
1458 else {
1459 continue;
1460 }
1461 }
1462
1463 const float automask_factor = automask_factors.is_empty() ? 1.0f : automask_factors[i];
1464 do_weight_paint_vertex(vp, ob, wpd, wpi, vert, final_alpha, paintweight, automask_factor);
1465 }
1466 });
1467 });
1468}
1469
1470static float calculate_average_weight(const Depsgraph &depsgraph,
1471 Object &ob,
1472 const Mesh &mesh,
1473 const Brush &brush,
1474 const VPaint &vp,
1475 WeightPaintInfo &wpi,
1476 const IndexMask &node_mask)
1477{
1480 const StrokeCache &cache = *ss.cache;
1481
1482 const bool use_normal = vwpaint::use_normal(vp);
1483 const bool use_face_sel = (mesh.editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
1484 const bool use_vert_sel = (mesh.editflag & ME_EDIT_PAINT_VERT_SEL) != 0;
1485
1486 const float *sculpt_normal_frontface = brush_frontface_normal_from_falloff_shape(
1487 ss, brush.falloff_shape);
1488
1489 const Span<float3> vert_positions = bke::pbvh::vert_positions_eval(depsgraph, ob);
1490 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(depsgraph, ob);
1491 const bke::AttributeAccessor attributes = mesh.attributes();
1492 const VArraySpan hide_vert = *attributes.lookup<bool>(".hide_vert", bke::AttrDomain::Point);
1493 VArraySpan<bool> select_vert;
1494 if (use_vert_sel || use_face_sel) {
1495 select_vert = *attributes.lookup<bool>(".select_vert", bke::AttrDomain::Point);
1496 }
1497
1499 node_mask.index_range(),
1500 1,
1502 [&](const IndexRange range, WPaintAverageAccum accum) {
1503 node_mask.slice(range).foreach_index([&](const int i) {
1504 const Span<int> verts = nodes[i].verts();
1505 Array<float, bke::pbvh::MESH_LEAF_LIMIT> factors(verts.size());
1506 fill_factor_from_hide(hide_vert, verts, factors);
1507 filter_region_clip_factors(ss, vert_positions, verts, factors);
1508 if (!select_vert.is_empty()) {
1509 filter_factors_with_selection(select_vert, verts, factors);
1510 }
1511
1512 Array<float, bke::pbvh::MESH_LEAF_LIMIT> distances(verts.size());
1513 calc_brush_distances(
1514 ss, vert_positions, verts, eBrushFalloffShape(brush.falloff_shape), distances);
1515 filter_distances_with_radius(cache.radius, distances, factors);
1516 calc_brush_strength_factors(cache, brush, distances, factors);
1517
1518 auto_mask::calc_vert_factors(
1519 depsgraph, ob, cache.automasking.get(), nodes[i], verts, factors);
1520
1521 for (const int i : verts.index_range()) {
1522 const int vert = verts[i];
1523 if (factors[i] == 0.0f) {
1524 continue;
1525 }
1526 const float angle_cos = use_normal ?
1527 dot_v3v3(sculpt_normal_frontface, vert_normals[vert]) :
1528 1.0f;
1529 if (angle_cos <= 0.0f) {
1530 continue;
1531 }
1532 const MDeformVert &dv = wpi.dvert[vert];
1533 accum.len++;
1534 accum.value += wpaint_get_active_weight(dv, wpi);
1535 }
1536 });
1537 return accum;
1538 },
1539 [](const WPaintAverageAccum &a, const WPaintAverageAccum &b) {
1540 return WPaintAverageAccum{a.len + b.len, a.value + b.value};
1541 });
1542 return math::safe_divide(value.value, double(value.len));
1543}
1544
1545static void wpaint_paint_leaves(const Depsgraph &depsgraph,
1546 Object &ob,
1547 VPaint &vp,
1548 WPaintData &wpd,
1549 WeightPaintInfo &wpi,
1550 Mesh &mesh,
1551 const IndexMask &node_mask)
1552{
1553 const Brush &brush = *ob.runtime->sculpt_session->cache->brush;
1554
1555 switch (eBrushWeightPaintType(brush.weight_brush_type)) {
1558 depsgraph,
1559 ob,
1560 brush,
1561 vp,
1562 wpd,
1563 wpi,
1564 mesh,
1565 calculate_average_weight(depsgraph, ob, mesh, brush, vp, wpi, node_mask),
1566 node_mask);
1567 break;
1568 }
1570 do_wpaint_brush_smear(depsgraph, ob, brush, vp, wpd, wpi, mesh, node_mask);
1571 break;
1573 do_wpaint_brush_blur(depsgraph, ob, brush, vp, wpd, wpi, mesh, node_mask);
1574 break;
1576 do_wpaint_brush_draw(depsgraph,
1577 ob,
1578 brush,
1579 vp,
1580 wpd,
1581 wpi,
1582 mesh,
1584 node_mask);
1585 break;
1586 }
1587}
1588
1589
1590/* -------------------------------------------------------------------- */
1593
1594void ED_object_wpaintmode_enter_ex(Main &bmain, Depsgraph &depsgraph, Scene &scene, Object &ob)
1595{
1596 ed::sculpt_paint::mode_enter_generic(bmain, depsgraph, scene, ob, OB_MODE_WEIGHT_PAINT);
1597
1600
1601 /* Flush object mode. */
1603}
1604void ED_object_wpaintmode_enter(bContext *C, Depsgraph &depsgraph)
1605{
1606 Main &bmain = *CTX_data_main(C);
1607 Scene &scene = *CTX_data_scene(C);
1609 ED_object_wpaintmode_enter_ex(bmain, depsgraph, scene, ob);
1610}
1611
1612
1613/* -------------------------------------------------------------------- */
1616
1628
1629
1630/* -------------------------------------------------------------------- */
1633
1635{
1636 const Object *ob = CTX_data_active_object(C);
1637
1638 return ob && ob->mode == OB_MODE_WEIGHT_PAINT && (id_cast<const Mesh *>(ob->data))->faces_num;
1639}
1640
1645
1646static bool weight_paint_poll_ex(bContext *C, bool check_tool)
1647{
1648 const Object *ob = CTX_data_active_object(C);
1649 const ScrArea *area;
1650
1651 if ((ob != nullptr) && (ob->mode & OB_MODE_WEIGHT_PAINT) &&
1652 (BKE_paint_brush(&CTX_data_tool_settings(C)->wpaint->paint) != nullptr) &&
1653 (area = CTX_wm_area(C)) && (area->spacetype == SPACE_VIEW3D))
1654 {
1655 const ARegion *region = CTX_wm_region(C);
1656 if (region && ELEM(region->regiontype, RGN_TYPE_WINDOW, RGN_TYPE_HUD)) {
1657 if (!check_tool || WM_toolsystem_active_tool_is_brush(C)) {
1658 return true;
1659 }
1660 }
1661 }
1662 return false;
1663}
1664
1666{
1667 return weight_paint_poll_ex(C, true);
1668}
1669
1671{
1672 return weight_paint_poll_ex(C, false);
1673}
1674
1679{
1680 Main &bmain = *CTX_data_main(C);
1681 wmMsgBus *mbus = CTX_wm_message_bus(C);
1683 const int mode_flag = OB_MODE_WEIGHT_PAINT;
1684 const bool is_mode_set = (ob.mode & mode_flag) != 0;
1685 Scene &scene = *CTX_data_scene(C);
1686
1687 if (!is_mode_set) {
1688 if (!ed::object::mode_compat_set(C, &ob, eObjectMode(mode_flag), op->reports)) {
1689 return OPERATOR_CANCELLED;
1690 }
1691 }
1692
1694
1695 if (is_mode_set) {
1697 }
1698 else {
1699 Depsgraph *depsgraph = CTX_data_depsgraph_on_load(C);
1700 if (depsgraph) {
1702 }
1703 ED_object_wpaintmode_enter_ex(bmain, *depsgraph, scene, ob);
1704 }
1705
1706 ed::object::posemode_set_for_weight_paint(C, &bmain, &ob, is_mode_set);
1707
1708 /* Weight-paint works by overriding colors in mesh,
1709 * so need to make sure we recalculate on enter and
1710 * exit (exit needs doing regardless because we
1711 * should re-deform).
1712 */
1714
1716
1717 WM_msg_publish_rna_prop(mbus, &ob.id, &ob, Object, mode);
1718
1720
1721 return OPERATOR_FINISHED;
1722}
1723
1725{
1726 ot->name = "Weight Paint Mode";
1727 ot->idname = "PAINT_OT_weight_paint_toggle";
1728 ot->description = "Toggle weight paint mode in 3D view";
1729
1732
1734}
1735
1737
1738/* -------------------------------------------------------------------- */
1741
1742static void wpaint_do_paint(const Depsgraph &depsgraph,
1743 const Scene &scene,
1744 const Brush &brush,
1745 Object &ob,
1746 PaintModeData *mode_data)
1747{
1748 WPaintData &wpd = *static_cast<WPaintData *>(mode_data);
1749 VPaint &wp = *scene.toolsettings->wpaint;
1750 Mesh &mesh = *id_cast<Mesh *>(ob.data);
1751 IndexMaskMemory memory;
1752 const IndexMask node_mask = gather_brush_nodes(
1754 vwpaint::update_sculpt_normal(depsgraph, ob, wp, brush, node_mask);
1755
1756 if (auto_mask::is_enabled(wp.paint, ob, &brush)) {
1757 auto_mask::Cache &cache = auto_mask::stroke_cache_ensure(depsgraph, wp.paint, &brush, ob);
1759 cache.calc_cavity_factor(depsgraph, ob, node_mask);
1760 }
1761 }
1762
1763 wpaint_paint_leaves(depsgraph, ob, wp, wpd, wpd.info, mesh, node_mask);
1764}
1765
1766void WeightPaintStroke::update_step(wmOperator * /*op*/, const StrokeStep &stroke_step)
1767{
1768 VPaint &wp = *weight_paint_;
1769 const ToolSettings &ts = *tool_settings_;
1770 const Brush &brush = *BKE_paint_brush(&wp.paint);
1771 WPaintData *wpd = static_cast<WPaintData *>(mode_data_.get());
1772 ViewContext *vc = &this->vc;
1773 Object *ob = this->object;
1774
1776 StrokeCache &cache = *ss.cache;
1777
1779 *this->depsgraph, *vc, wp, this->paint_mode, *ob, *this->base_, stroke_step);
1780
1781 const float brush_alpha_value = BKE_brush_alpha_get(&wp.paint, &brush);
1782
1783 if (wpd == nullptr) {
1784 /* XXX: force a redraw here, since even though we can't paint,
1785 * at least view won't freeze until stroke ends */
1786 ED_region_tag_redraw(this->vc.region);
1787 return;
1788 }
1789
1790 ob = vc->obact;
1791
1792 Mesh &mesh = *id_cast<Mesh *>(ob->data);
1793
1794 WeightPaintInfo wpi;
1795 /* *** setup WeightPaintInfo - pass onto do_weight_paint_vertex *** */
1796 wpi.dvert = mesh.deform_verts_for_write();
1797
1798 wpi.defbase_tot = wpd->defbase_tot;
1799 wpi.defbase_sel = wpd->defbase_sel;
1801
1803 wpi.active = wpd->active;
1804 wpi.mirror = wpd->mirror;
1805 wpi.lock_flags = wpd->lock_flags;
1807 wpi.vgroup_locked = wpd->vgroup_locked;
1810 wpi.do_multipaint = wpd->do_multipaint;
1811 wpi.do_auto_normalize = ((ts.auto_normalize != 0) && (wpi.vgroup_validmap != nullptr) &&
1812 (wpi.do_multipaint || wpi.vgroup_validmap[wpi.active.index]));
1815 wpi.brush_alpha_value = brush_alpha_value;
1816
1817 if (wpd->precomputed_weight) {
1818 precompute_weight_values(*ob, brush, *wpd, wpi, mesh);
1819 }
1820
1821 wpd->info = wpi;
1822 if (mesh.editflag & ME_EDIT_MIRROR_VERTEX_GROUPS) {
1823 /* We don't do any symmetry strokes when mirroring vertex groups. */
1824 cache.bstrength = cache.base_brush_strength;
1826 cache.radial_symmetry_pass = 0;
1828 wpaint_do_paint(*this->depsgraph, *this->scene, brush, *ob, wpd);
1829 }
1830 else {
1832 *this->depsgraph, *this->scene, wp.paint, *ob, wpaint_do_paint, wpd);
1833 }
1834
1835 ss.cache->first_time = false;
1836 copy_v3_v3(cache.last_location, cache.location);
1837
1838 /* Calculate pivot for rotation around selection if needed.
1839 * also needed for "Frame Selected" on last stroke. */
1840 float loc_world[3];
1841 mul_v3_m4v3(loc_world, ob->object_to_world().ptr(), ss.cache->location);
1842 bke::paint::stroke_track_location(*this->paint, loc_world);
1843
1845
1848
1850}
1851
1852void WeightPaintStroke::done(bool /*is_cancel*/, bool /*stroke_started*/)
1853{
1854 Object &ob = *this->object;
1855
1857
1860 }
1861
1862 if (ob.particlesystem.first_) {
1863 for (ParticleSystem &psys : ob.particlesystem) {
1864 for (int i = 0; i < PSYS_TOT_VG; i++) {
1865 if (psys.vgroup[i] == BKE_object_defgroup_active_index_get(&ob)) {
1867 break;
1868 }
1869 }
1870 }
1871 }
1872
1874
1876
1877 MEM_delete(ob.runtime->sculpt_session->cache);
1878 ob.runtime->sculpt_session->cache = nullptr;
1879}
1880
1882{
1883 if (!G.background) {
1885 }
1886
1887 WeightPaintStroke *stroke = MEM_new<WeightPaintStroke>(__func__, C, op, event);
1888 op->customdata = stroke;
1889 vwpaint::init_stroke(*op, *stroke->bmain_, *stroke->paint, *stroke->depsgraph, *stroke->object);
1890
1891 const wmOperatorStatus retval = op->type->modal(C, op, event);
1892 OPERATOR_RETVAL_CHECK(retval);
1893
1894 if (retval == OPERATOR_FINISHED) {
1895 WeightPaintStroke *stroke = static_cast<WeightPaintStroke *>(op->customdata);
1896 if (stroke) {
1897 stroke->finish(C);
1898 MEM_delete(stroke);
1899 }
1900 return OPERATOR_FINISHED;
1901 }
1903
1905
1907}
1908
1910{
1911 WeightPaintStroke *stroke = MEM_new<WeightPaintStroke>(__func__, C, op, nullptr);
1912 op->customdata = stroke;
1913
1914 vwpaint::init_stroke(*op, *stroke->bmain_, *stroke->paint, *stroke->depsgraph, *stroke->object);
1915
1916 stroke->exec(C, op);
1917
1918 MEM_delete(stroke);
1919 return OPERATOR_FINISHED;
1920}
1921
1923{
1924 WeightPaintStroke *stroke = static_cast<WeightPaintStroke *>(op->customdata);
1925 const wmOperatorStatus retval = stroke->modal(C, op, event);
1926
1928 MEM_delete(stroke);
1929 op->customdata = nullptr;
1930 }
1931
1932 return retval;
1933}
1934
1936{
1937 ot->name = "Weight Paint";
1938 ot->idname = "PAINT_OT_weight_paint";
1939 ot->description = "Paint a stroke in the current vertex group's weights";
1940
1941 ot->invoke = wpaint_invoke;
1942 ot->modal = wpaint_modal;
1943 ot->exec = wpaint_exec;
1944 ot->poll = weight_paint_poll;
1945
1947
1950 ot->srna,
1951 "override_location",
1952 false,
1953 "Override Location",
1954 "Override the given \"location\" array by recalculating object space positions from the "
1955 "provided \"mouse_event\" positions");
1957}
1958
1960
1961} // namespace blender
support for deformation groups and hooks.
#define VERTEX_WEIGHT_LOCK_EPSILON
Functions for dealing with objects and deform verts, used by painting and tools.
Generic array manipulation API.
#define BLI_array_binary_or(arr, arr_a, arr_b, arr_len)
#define final(a, b, c)
Definition BLI_hash_c.hh:21
#define ME_USING_MIRROR_X_VERTEX_GROUPS(_me)
Object is a sort of wrapper for general info.
#define OPERATOR_RETVAL_CHECK(ret)
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define ND_DRAW
Definition WM_types.hh:502
#define ND_MODE
Definition WM_types.hh:464
#define NC_SCENE
Definition WM_types.hh:391
#define NC_OBJECT
Definition WM_types.hh:392
#define CLAMP(a, b, c)
#define ELEM(...)
int64_t size() const
Definition BLI_array.hh:267
Array(Allocator allocator={}) noexcept
Definition BLI_array.hh:78
const T * data() const
Definition BLI_array.hh:323
void fill(const T &value) const
Definition BLI_array.hh:283
void reinitialize(const int64_t new_size)
Definition BLI_array.hh:430
bool is_empty() const
Definition BLI_array.hh:275
constexpr IndexRange()=default
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 int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
GAttributeReader lookup(const StringRef name) const
IndexMask slice(IndexRange range) const
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
pbvh::Tree * pbvh_get(Object &object)
void stroke_track_location(Paint &paint, float3 location)
Span< float3 > vert_normals_eval(const Depsgraph &depsgraph, const Object &object_orig)
Definition pbvh.cc:952
Span< float3 > vert_positions_eval(const Depsgraph &depsgraph, const Object &object_orig)
Definition pbvh.cc:933
bool vgroup_sync_from_pose(Object *ob)
void posemode_set_for_weight_paint(bContext *C, Main *bmain, Object *ob, bool is_mode_set)
bool mode_compat_set(bContext *C, Object *ob, eObjectMode mode, ReportList *reports)
Cache & stroke_cache_ensure(const Depsgraph &depsgraph, const Paint &paint, const Brush *brush, Object &ob)
bool is_enabled(const Paint &paint, const Object &object, const Brush *br)
void calc_vert_factors(const Depsgraph &depsgraph, const Object &object, const Cache &automasking, const bke::pbvh::MeshNode &node, Span< int > verts, MutableSpan< float > factors)
void update_cache_variants(Depsgraph &depsgraph, ViewContext &vc, VPaint &vp, const PaintMode paint_mode, Object &ob, Base &base, const PaintStroke::StrokeStep &stroke_step)
bool use_normal(const VPaint &vp)
void update_cache_invariants(VPaint &vp, SculptSession &ss, wmOperator *op, const float mval[2])
void update_sculpt_normal(const Depsgraph &depsgraph, const Object &ob, const VPaint &vp, const Brush &brush, const IndexMask node_mask)
bool brush_use_accumulate_ex(const Brush &brush, const eObjectMode ob_mode)
void get_brush_alpha_data(const SculptSession &ss, const Paint &paint, const Brush &brush, float *r_brush_size_pressure, float *r_brush_alpha_value, float *r_brush_alpha_pressure)
bool brush_use_accumulate(const VPaint &vp)
void smooth_brush_toggle_off(Paint *paint, StrokeCache *cache)
bool test_brush_angle_falloff(const Brush &brush, const float angle_cos)
void init_stroke(const wmOperator &op, Main &main, Paint &paint, Depsgraph &depsgraph, Object &ob)
void mode_exit_generic(Scene &scene, Object &ob, const eObjectMode mode_flag)
IndexMask gather_brush_nodes(const Object &ob, const Brush &brush, IndexMaskMemory &memory, FunctionRef< bool(const bke::pbvh::Node &)> node_ignore_fn)
void mode_enter_generic(Main &bmain, Depsgraph &depsgraph, Scene &scene, Object &ob, const eObjectMode mode_flag)
Definition mesh_paint.cc:76
void calc_brush_strength_factors(const StrokeCache &cache, const Brush &brush, Span< float > distances, MutableSpan< float > factors)
Definition sculpt.cc:7501
void filter_distances_with_radius(float radius, Span< float > distances, MutableSpan< float > factors)
Definition sculpt.cc:7422
void filter_region_clip_factors(const SculptSession &ss, Span< float3 > vert_positions, Span< int > verts, MutableSpan< float > factors)
Definition sculpt.cc:7285
bool stroke_is_first_brush_step_of_symmetry_pass(const StrokeCache &cache)
Definition sculpt.cc:667
void do_symmetrical_brush_actions(const Depsgraph &depsgraph, const Scene &scene, const Paint &paint, Object &object, const BrushActionFn action_fn, PaintModeData *paint_mode_data)
void scale_factors(MutableSpan< float > factors, float strength)
Definition sculpt.cc:7856
std::optional< float3 > stroke_get_location_bvh(Depsgraph &depsgraph, ViewContext &vc, const Sculpt &sd, const Brush *brush, const float mval[2], const bool force_original)
Definition sculpt.cc:4855
void calc_brush_distances(const SculptSession &ss, Span< float3 > vert_positions, Span< int > vert, eBrushFalloffShape falloff_shape, MutableSpan< float > r_distances)
Definition sculpt.cc:7370
const float * brush_frontface_normal_from_falloff_shape(const SculptSession &ss, char falloff_shape)
Definition sculpt.cc:1341
void fill_factor_from_hide(Span< bool > hide_vert, Span< int > verts, MutableSpan< float > r_factors)
Definition sculpt.cc:7077
void cache_calc_brushdata_symm(StrokeCache &cache, const ePaintSymmetryFlags symm, const char axis, const float angle)
Definition sculpt.cc:3725
T safe_divide(const T &a, const T &b)
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
Value parallel_reduce(IndexRange range, int64_t grain_size, const Value &identity, const Function &function, const Reduction &reduction)
Definition BLI_task.hh:127
BMesh Iterators.
static wmOperatorStatus wpaint_exec(bContext *C, wmOperator *op)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static void init_session_data(const VPaint &wpaint, Object &ob, WPaintData &wpd)
void ED_mesh_mirror_topo_table_end(Object *ob)
Definition meshtools.cc:253
ScrArea * CTX_wm_area(const bContext *C)
static void multipaint_apply_change(MDeformVert *dvert, const int defbase_tot, float change, const bool *defbase_sel)
static void filter_factors_with_selection(const Span< bool > select_vert, const Span< int > verts, const MutableSpan< float > factors)
@ RPT_WARNING
Definition BKE_report.hh:40
static wmOperatorStatus wpaint_mode_toggle_exec(bContext *C, wmOperator *op)
@ VP_FLAG_VGROUP_RESTRICT
bool ED_wpaint_ensure_data(bContext *C, ReportList *reports, eWPaintFlag flag, WPaintVGroupIndex *vgroup_index)
MINLINE void copy_v3_v3(float r[3], const float a[3])
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 BKE_object_defgroup_mirror_selection(struct Object *ob, int defbase_tot, const bool *selection, bool *dg_flags_sel, int *r_dg_flags_sel_tot)
bool weight_paint_poll(bContext *C)
void mul_v3_m4v3(float r[3], const float mat[4][4], const float vec[3])
static void parallel_nodes_loop_with_mirror_check(const Mesh &mesh, const IndexMask &node_mask, FunctionRef< void(IndexRange)> fn)
bool * BKE_object_defgroup_validmap_get(struct Object *ob, int defbase_tot)
void ED_mesh_mirror_spatial_table_end(Object *ob)
@ IMB_BLEND_DARKEN
Definition IMB_imbuf.hh:209
@ IMB_BLEND_SUB
Definition IMB_imbuf.hh:206
@ IMB_BLEND_MIX
Definition IMB_imbuf.hh:204
@ IMB_BLEND_LIGHTEN
Definition IMB_imbuf.hh:208
@ IMB_BLEND_ADD
Definition IMB_imbuf.hh:205
void ED_object_wpaintmode_exit(bContext *C)
void BKE_defvert_array_free_elems(MDeformVert *dvert, int totvert)
Definition deform.cc:1061
static void do_wpaint_brush_smear(const Depsgraph &depsgraph, Object &ob, const Brush &brush, VPaint &vp, WPaintData &wpd, const WeightPaintInfo &wpi, Mesh &mesh, const IndexMask &node_mask)
@ WPAINT_BRUSH_TYPE_AVERAGE
@ WPAINT_BRUSH_TYPE_SMEAR
@ WPAINT_BRUSH_TYPE_DRAW
@ WPAINT_BRUSH_TYPE_BLUR
static void precompute_weight_values(Object &ob, const Brush &brush, WPaintData &wpd, WeightPaintInfo &wpi, Mesh &mesh)
int mesh_get_x_mirror_vert(Object *ob, Mesh *mesh_eval, int index, bool use_topology)
Definition meshtools.cc:297
void view3d_operator_needs_gpu(const bContext *C)
static void wpaint_paint_leaves(const Depsgraph &depsgraph, Object &ob, VPaint &vp, WPaintData &wpd, WeightPaintInfo &wpi, Mesh &mesh, const IndexMask &node_mask)
void BKE_object_defgroup_split_locked_validmap(int defbase_tot, const bool *locked, const bool *deform, bool *r_locked, bool *r_unlocked)
void PAINT_OT_weight_paint(wmOperatorType *ot)
static float wpaint_undo_lock_relative(float weight, float old_weight, float locked_weight, float free_weight, bool auto_normalize)
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:681
Base * CTX_data_active_base(const bContext *C)
Global G
Definition blender.cc:57
static void do_wpaint_brush_draw(const Depsgraph &depsgraph, Object &ob, const Brush &brush, VPaint &vp, WPaintData &wpd, const WeightPaintInfo &wpi, Mesh &mesh, const float strength, const IndexMask &node_mask)
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
static wmOperatorStatus wpaint_invoke(bContext *C, wmOperator *op, const wmEvent *event)
static void wpaint_do_paint(const Depsgraph &depsgraph, const Scene &scene, const Brush &brush, Object &ob, PaintModeData *mode_data)
static float wpaint_clamp_monotonic(float oldval, float curval, float newval)
VecBase< float, 2 > float2
@ ME_EDIT_MIRROR_VERTEX_GROUPS
@ 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
float BKE_defvert_calc_lock_relative_weight(float weight, float locked_weight, float unlocked_weight)
Definition deform.cc:994
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
Mesh * BKE_mesh_from_object(Object *ob)
void BKE_mesh_batch_cache_dirty_tag(Mesh *mesh, eMeshBatchDirtyMode mode)
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
static void do_weight_paint_vertex_single(const VPaint &wp, Object &ob, WPaintData &wpd, const WeightPaintInfo &wpi, const uint index, float alpha, float paintweight, float automasking_factor)
void WM_toolsystem_update_from_context_view3d(bContext *C)
MDeformWeight * BKE_defvert_ensure_index(MDeformVert *dv, int defgroup)
Definition deform.cc:830
void PAINT_OT_weight_paint_toggle(wmOperatorType *ot)
ToolSettings * CTX_data_tool_settings(const bContext *C)
bool weight_paint_poll_ignore_tool(bContext *C)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
wmMsgBus * CTX_wm_message_bus(const bContext *C)
static void do_weight_paint_normalize_all_locked_try_active(MDeformVert *dvert, const int defbase_tot, const bool *vgroup_validmap, const bool *lock_flags, const bool *lock_with_active)
static wmOperatorStatus wpaint_modal(bContext *C, wmOperator *op, const wmEvent *event)
static void do_weight_paint_vertex_multi(const VPaint &wp, Object &ob, WPaintData &wpd, const WeightPaintInfo &wpi, const uint index, float alpha, float paintweight, float additional_factor)
void project_plane_v3_v3v3(float out[3], const float p[3], const float v_plane[3])
@ BRUSH_AUTOMASKING_CAVITY_ALL
void paint_stroke_operator_properties(wmOperatorType *ot)
static float wpaint_blend(const VPaint &wp, float weight, const float alpha, float paintval, const float paint_factor, const bool do_flip)
static void do_wpaint_brush_blur(const Depsgraph &depsgraph, Object &ob, const Brush &brush, VPaint &vp, WPaintData &wpd, const WeightPaintInfo &wpi, Mesh &mesh, const IndexMask &node_mask)
bool * BKE_object_defgroup_lock_flags_get(struct Object *ob, int defbase_tot)
const Brush * BKE_paint_brush_for_read(const Paint *paint)
MINLINE float normalize_v3(float n[3])
wmEventHandler_Op * WM_event_add_modal_handler(bContext *C, wmOperator *op)
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)
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
Depsgraph * CTX_data_depsgraph_on_load(const bContext *C)
Main * CTX_data_main(const bContext *C)
bool WM_toolsystem_active_tool_is_brush(const bContext *C)
static void multipaint_clamp_change(MDeformVert *dvert, const int defbase_tot, const bool *defbase_sel, float *change_p)
float BKE_defvert_total_selected_weight(const MDeformVert *dv, int defbase_num, const bool *defbase_sel)
Definition deform.cc:955
float ED_wpaint_blend_tool(const int tool, const float weight, const float paintval, const float alpha)
@ ID_RECALC_PSYS_RESET
Definition DNA_ID.h:1083
@ 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
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
@ PROP_HIDDEN
Definition RNA_types.hh:408
bool BKE_pbvh_node_fully_hidden_get(const bke::pbvh::Node &node)
Definition pbvh.cc:1625
bool ED_operator_region_view3d_active(bContext *C)
static bool weight_paint_poll_ex(bContext *C, bool check_tool)
static bool do_weight_paint_normalize_all_locked(MDeformVert *dvert, const int defbase_tot, const bool *vgroup_validmap, const bool *lock_flags)
Brush * BKE_paint_brush(Paint *paint)
static bool multipaint_verify_change(MDeformVert *dvert, const int defbase_tot, float change, const bool *defbase_sel)
static MDeformVert * defweight_prev_init(MDeformVert *dvert_prev, MDeformVert *dvert_curr, int index)
Scene * CTX_data_scene(const bContext *C)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
@ OPTYPE_BLOCKING
Definition WM_types.hh:192
static float calculate_average_weight(const Depsgraph &depsgraph, Object &ob, const Mesh &mesh, const Brush &brush, const VPaint &vp, WeightPaintInfo &wpi, const IndexMask &node_mask)
float BKE_brush_weight_get(const Paint *paint, const Brush *brush)
Definition brush.cc:1464
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
bool BKE_object_defgroup_check_lock_relative(const bool *lock_flags, const bool *validmap, int index)
static float wpaint_get_active_weight(const MDeformVert &dv, const WeightPaintInfo &wpi)
@ BKE_MESH_BATCH_DIRTY_ALL
Definition BKE_mesh.h:48
static void do_weight_paint_vertex(const VPaint &wp, Object &ob, WPaintData &wpd, const WeightPaintInfo &wpi, const uint index, float final_alpha, float paintweight, float automasking_factor=1.0f)
Object * CTX_data_active_object(const bContext *C)
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
ARegion * CTX_wm_region(const bContext *C)
bool weight_paint_mode_poll(bContext *C)
float BKE_brush_alpha_get(const Paint *paint, const Brush *brush)
Definition brush.cc:1454
void ED_object_wpaintmode_enter_ex(Main &bmain, Depsgraph &depsgraph, Scene &scene, Object &ob)
void ED_object_wpaintmode_exit_ex(Scene &scene, Object &ob)
MDeformWeight * BKE_defvert_find_index(const MDeformVert *dv, int defgroup)
Definition deform.cc:811
static void do_weight_paint_normalize_all(MDeformVert *dvert, const int defbase_tot, const bool *vgroup_validmap)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
@ 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 void init_session_data(Object &ob)
int BKE_object_defgroup_active_index_get(const Object *ob)
Definition deform.cc:613
void ED_object_wpaintmode_enter(bContext *C, Depsgraph &depsgraph)
eRegion_Type regiontype
eBrushWeightPaintType weight_brush_type
struct MDeformWeight * dw
bke::ObjectRuntime * runtime
ListBaseT< struct ParticleSystem > particlesystem
struct ToolSettings * toolsettings
ed::sculpt_paint::StrokeCache * cache
Definition BKE_paint.hh:409
Array< MDeformVert > dvert_prev
Array< float > alpha_weight
const bool * vgroup_unlocked
WeightPaintGroupData active
const bool * vgroup_locked
const bool * vgroup_validmap
WeightPaintInfo info
const bool * defbase_sel
WeightPaintGroupData mirror
const bool * lock_flags
WeightPaintGroupData active
MutableSpan< MDeformVert > dvert
WeightPaintGroupData mirror
WeightPaintStroke(bContext *C, wmOperator *op, const wmEvent *event)
bool test_start(wmOperator *op, float2 mouse) override
void redraw(bool final) override
void done(bool is_cancel, bool stroke_started) override
std::optional< float3 > get_location(float2 mouse, bool force_original) override
void update_step(wmOperator *op, const StrokeStep &stroke_step) override
ebDeformGroup_Flag flag
wmOperatorStatus modal(bContext *C, wmOperator *op, const wmEvent *event)
wmOperatorStatus exec(bContext *C, wmOperator *op)
std::unique_ptr< PaintModeData > mode_data_
std::unique_ptr< auto_mask::Cache > automasking
void calc_cavity_factor(const Depsgraph &depsgraph, const Object &object, const IndexMask &node_mask)
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
wmOperatorStatus(*) modal(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1157
struct ReportList * reports
struct wmOperatorType * type
#define WM_msg_publish_rna_prop(mbus, id_, data_, type_, prop_)