Blender V5.0
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.h"
12#include "BLI_listbase.h"
13#include "BLI_math_geom.h"
14#include "BLI_math_vector.h"
15
16#include "DNA_brush_types.h"
17#include "DNA_mesh_types.h"
18#include "DNA_meshdata_types.h"
19#include "DNA_object_types.h"
20#include "DNA_scene_types.h"
21
22#include "RNA_access.hh"
23#include "RNA_define.hh"
24
25#include "BKE_attribute.hh"
26#include "BKE_brush.hh"
27#include "BKE_colortools.hh"
28#include "BKE_context.hh"
29#include "BKE_deform.hh"
30#include "BKE_mesh.hh"
31#include "BKE_mesh_iterators.hh"
32#include "BKE_modifier.hh"
33#include "BKE_object.hh"
34#include "BKE_object_deform.h"
35#include "BKE_paint.hh"
36#include "BKE_report.hh"
37
38#include "DEG_depsgraph.hh"
40
41#include "WM_api.hh"
42#include "WM_types.hh"
43
44#include "ED_armature.hh"
45#include "ED_mesh.hh"
46#include "ED_screen.hh"
47#include "ED_view3d.hh"
48
49#include "UI_interface.hh"
51#include "UI_resources.hh"
52
53#include "paint_intern.hh" /* own include */
54
55/* -------------------------------------------------------------------- */
60
61struct WPaintPrev {
62 /* previous vertex weights */
64 /* allocation size of prev buffers */
65 int tot;
66};
67
69{
70 wpp->wpaint_prev = nullptr;
71 wpp->tot = 0;
72}
73
74static void wpaint_prev_create(WPaintPrev *wpp, MDeformVert *dverts, int dcount)
75{
77
78 if (dverts && dcount) {
79 wpp->wpaint_prev = MEM_malloc_arrayN<MDeformVert>(dcount, __func__);
80 wpp->tot = dcount;
81 BKE_defvert_array_copy(wpp->wpaint_prev, dverts, dcount);
82 }
83}
84
86{
87 if (wpp->wpaint_prev) {
89 }
90 wpp->wpaint_prev = nullptr;
91 wpp->tot = 0;
92}
93
95
96/* -------------------------------------------------------------------- */
99
101{
103
105}
106
108{
110 Scene *scene = CTX_data_scene(C);
113 Mesh *mesh = static_cast<Mesh *>(ob->data);
114 int type = RNA_enum_get(op->ptr, "type");
115
117 op->reports, depsgraph, scene, ob, armob, type, (mesh->symmetry & ME_SYMMETRY_X));
118
119 DEG_id_tag_update(&mesh->id, 0);
122
123 return OPERATOR_FINISHED;
124}
125
127{
128 static const EnumPropertyItem type_items[] = {
129 {ARM_GROUPS_AUTO, "AUTOMATIC", 0, "Automatic", "Automatic weights from bones"},
131 "ENVELOPES",
132 0,
133 "From Envelopes",
134 "Weights from envelopes with user defined radius"},
135 {0, nullptr, 0, nullptr, nullptr},
136 };
137
138 /* identifiers */
139 ot->name = "Weight from Bones";
140 ot->idname = "PAINT_OT_weight_from_bones";
141 ot->description =
142 ("Set the weights of the groups matching the attached armature's selected bones, "
143 "using the distance between the vertices and the bones");
144
145 /* API callbacks. */
147 ot->invoke = WM_menu_invoke;
149
150 /* flags */
152
153 /* properties */
154 ot->prop = RNA_def_enum(
155 ot->srna, "type", type_items, 0, "Type", "Method to use for assigning weights");
156}
157
159
160/* -------------------------------------------------------------------- */
163
170{
172 Mesh *mesh;
173 bool changed = false;
174
176 mesh = BKE_mesh_from_object(vc.obact);
177 const MDeformVert *dvert = mesh->deform_verts().data();
178
179 if (mesh && dvert && vc.v3d && vc.rv3d && (mesh->vertex_group_active_index != 0)) {
180 const bool use_vert_sel = (mesh->editflag & ME_EDIT_PAINT_VERT_SEL) != 0;
181 int v_idx_best = -1;
182 uint index;
183
186
187 if (use_vert_sel) {
189 C, vc.obact, event->mval, ED_MESH_PICK_DEFAULT_VERT_DIST, true, &index))
190 {
191 v_idx_best = index;
192 }
193 }
194 else {
196 {
197 v_idx_best = index;
198 }
199 else if (ED_mesh_pick_face(C, vc.obact, event->mval, ED_MESH_PICK_DEFAULT_FACE_DIST, &index))
200 {
201 /* This relies on knowing the internal workings of #ED_mesh_pick_face_vert() */
203 op->reports, RPT_WARNING, "The modifier used does not support deformed locations");
204 }
205 }
206
207 if (v_idx_best != -1) { /* should always be valid */
209 Brush *brush = BKE_paint_brush(&ts->wpaint->paint);
210 const int vgroup_active = mesh->vertex_group_active_index - 1;
211 float vgroup_weight = BKE_defvert_find_weight(&dvert[v_idx_best], vgroup_active);
212 const int defbase_tot = BLI_listbase_count(&mesh->vertex_group_names);
213 bool use_lock_relative = ts->wpaint_lock_relative;
214 bool *defbase_locked = nullptr, *defbase_unlocked = nullptr;
215
216 if (use_lock_relative) {
217 defbase_locked = BKE_object_defgroup_lock_flags_get(vc.obact, defbase_tot);
218 defbase_unlocked = BKE_object_defgroup_validmap_get(vc.obact, defbase_tot);
219
220 use_lock_relative = BKE_object_defgroup_check_lock_relative(
221 defbase_locked, defbase_unlocked, vgroup_active);
222 }
223
224 /* use combined weight in multipaint mode,
225 * since that's what is displayed to the user in the colors */
226 if (ts->multipaint) {
227 int defbase_tot_sel;
228 bool *defbase_sel = BKE_object_defgroup_selected_get(
229 vc.obact, defbase_tot, &defbase_tot_sel);
230
231 if (defbase_tot_sel > 1) {
234 vc.obact, defbase_tot, defbase_sel, defbase_sel, &defbase_tot_sel);
235 }
236
237 use_lock_relative = use_lock_relative &&
239 defbase_tot, defbase_locked, defbase_sel, defbase_tot_sel);
240
241 bool is_normalized = ts->auto_normalize || use_lock_relative;
243 &dvert[v_idx_best], defbase_tot, defbase_sel, defbase_tot_sel, is_normalized);
244 }
245
246 MEM_freeN(defbase_sel);
247 }
248
249 if (use_lock_relative) {
251 defbase_tot, defbase_locked, defbase_unlocked, defbase_locked, defbase_unlocked);
252
253 vgroup_weight = BKE_defvert_lock_relative_weight(
254 vgroup_weight, &dvert[v_idx_best], defbase_tot, defbase_locked, defbase_unlocked);
255 }
256
257 MEM_SAFE_FREE(defbase_locked);
258 MEM_SAFE_FREE(defbase_unlocked);
259
260 CLAMP(vgroup_weight, 0.0f, 1.0f);
261 BKE_brush_weight_set(&ts->wpaint->paint, brush, vgroup_weight);
262 changed = true;
263 }
264 }
265
266 if (changed) {
267 /* not really correct since the brush didn't change, but redraws the toolbar */
268 WM_main_add_notifier(NC_BRUSH | NA_EDITED, nullptr); /* ts->wpaint->paint.brush */
269
270 return OPERATOR_FINISHED;
271 }
272 return OPERATOR_CANCELLED;
273}
274
276{
277 /* identifiers */
278 ot->name = "Weight Paint Sample Weight";
279 ot->idname = "PAINT_OT_weight_sample";
280 ot->description = "Use the mouse to sample a weight in the 3D view";
281
282 /* API callbacks. */
283 ot->invoke = weight_sample_invoke;
285
286 /* flags */
288}
289
291
292/* -------------------------------------------------------------------- */
295
302{
303 bool found = false;
304 int i = dvert->totweight;
305 MDeformWeight *dw;
306 for (dw = dvert->dw; i > 0; dw++, i--) {
307 if (UNLIKELY(dw->def_nr >= groups.size())) {
308 continue;
309 }
310 groups[dw->def_nr] = true;
311 found = true;
312 }
313 return found;
314}
315
317 wmOperator *op,
318 const wmEvent *event)
319{
322 BLI_assert(vc.v3d && vc.rv3d); /* Ensured by poll. */
323
324 Mesh *mesh = BKE_mesh_from_object(vc.obact);
325 const MDeformVert *dverts = mesh->deform_verts().data();
326 if (BLI_listbase_is_empty(&mesh->vertex_group_names) || (dverts == nullptr)) {
327 BKE_report(op->reports, RPT_WARNING, "No vertex group data");
328 return OPERATOR_CANCELLED;
329 }
330
331 const bool use_vert_sel = (mesh->editflag & ME_EDIT_PAINT_VERT_SEL) != 0;
333
334 bool found = false;
335
338
339 if (use_vert_sel) {
340 /* Extract from the vertex. */
341 uint index;
342 if (ED_mesh_pick_vert(C, vc.obact, event->mval, ED_MESH_PICK_DEFAULT_VERT_DIST, true, &index))
343 {
344 const MDeformVert *dvert = &dverts[index];
345 found |= weight_paint_sample_mark_groups(dvert, groups);
346 }
347 }
348 else {
349 /* Extract from the face. */
350 const blender::OffsetIndices faces = mesh->faces();
351 const blender::Span<int> corner_verts = mesh->corner_verts();
352 uint index;
353 if (ED_mesh_pick_face(C, vc.obact, event->mval, ED_MESH_PICK_DEFAULT_FACE_DIST, &index)) {
354 for (const int vert : corner_verts.slice(faces[index])) {
355 found |= weight_paint_sample_mark_groups(&dverts[vert], groups);
356 }
357 }
358 }
359
360 if (found == false) {
361 BKE_report(op->reports, RPT_WARNING, "No vertex groups found");
362 return OPERATOR_CANCELLED;
363 }
364
366 C, WM_operatortype_name(op->type, op->ptr).c_str(), ICON_NONE);
367 uiLayout *layout = UI_popup_menu_layout(pup);
368 wmOperatorType *ot = WM_operatortype_find("OBJECT_OT_vertex_group_set_active", false);
370 layout->operator_context_set(opcontext);
371 int i = 0;
373 if (groups[i] == false) {
374 continue;
375 }
376 PointerRNA op_ptr = layout->op(
378 RNA_property_enum_set(&op_ptr, ot->prop, i);
379 }
380 UI_popup_menu_end(C, pup);
381
382 return OPERATOR_INTERFACE;
383}
384
386{
387 /* identifiers */
388 ot->name = "Weight Paint Sample Group";
389 ot->idname = "PAINT_OT_weight_sample_group";
390 ot->description = "Select one of the vertex groups available under current mouse position";
391
392 /* API callbacks. */
395
396 /* flags */
398}
399
401
402/* -------------------------------------------------------------------- */
405
406/* fills in the selected faces with the current weight and vertex group */
407static bool weight_paint_set(Object *ob, float paintweight)
408{
409 using namespace blender;
410 Mesh *mesh = static_cast<Mesh *>(ob->data);
411 MDeformWeight *dw, *dw_prev;
412 int vgroup_active, vgroup_mirror = -1;
413 const bool topology = (mesh->editflag & ME_EDIT_MIRROR_TOPO) != 0;
414
415 /* mutually exclusive, could be made into a */
416 const short paint_selmode = ME_EDIT_PAINT_SEL_MODE(mesh);
417
418 const blender::OffsetIndices faces = mesh->faces();
419 const blender::Span<int> corner_verts = mesh->corner_verts();
420 MDeformVert *dvert = mesh->deform_verts_for_write().data();
421
422 if (mesh->faces_num == 0 || dvert == nullptr) {
423 return false;
424 }
425
426 vgroup_active = BKE_object_defgroup_active_index_get(ob) - 1;
427
428 /* if mirror painting, find the other group */
430 vgroup_mirror = ED_wpaint_mirror_vgroup_ensure(ob, vgroup_active);
431 }
432
433 WPaintPrev wpp;
434 wpaint_prev_create(&wpp, dvert, mesh->verts_num);
435
436 const bke::AttributeAccessor attributes = mesh->attributes();
437 const VArraySpan select_vert = *attributes.lookup<bool>(".select_vert", bke::AttrDomain::Point);
438 const VArraySpan select_poly = *attributes.lookup<bool>(".select_poly", bke::AttrDomain::Face);
439
440 for (const int i : faces.index_range()) {
441 if ((paint_selmode == SCE_SELECT_FACE) && !(!select_poly.is_empty() && select_poly[i])) {
442 continue;
443 }
444
445 for (const int vert : corner_verts.slice(faces[i])) {
446 if (!dvert[vert].flag) {
447 if ((paint_selmode == SCE_SELECT_VERTEX) &&
448 !(!select_vert.is_empty() && select_vert[vert]))
449 {
450 continue;
451 }
452
453 dw = BKE_defvert_ensure_index(&dvert[vert], vgroup_active);
454 if (dw) {
455 dw_prev = BKE_defvert_ensure_index(wpp.wpaint_prev + vert, vgroup_active);
456 dw_prev->weight = dw->weight; /* set the undo weight */
457 dw->weight = paintweight;
458
459 if (mesh->symmetry & ME_SYMMETRY_X) {
460 /* x mirror painting */
461 int j = mesh_get_x_mirror_vert(ob, nullptr, vert, topology);
462 if (j >= 0) {
463 /* copy, not paint again */
464 if (vgroup_mirror != -1) {
465 dw = BKE_defvert_ensure_index(dvert + j, vgroup_mirror);
466 dw_prev = BKE_defvert_ensure_index(wpp.wpaint_prev + j, vgroup_mirror);
467 }
468 else {
469 dw = BKE_defvert_ensure_index(dvert + j, vgroup_active);
470 dw_prev = BKE_defvert_ensure_index(wpp.wpaint_prev + j, vgroup_active);
471 }
472 dw_prev->weight = dw->weight; /* set the undo weight */
473 dw->weight = paintweight;
474 }
475 }
476 }
477 dvert[vert].flag = 1;
478 }
479 }
480 }
481
482 {
483 MDeformVert *dv = dvert;
484 for (int index = mesh->verts_num; index != 0; index--, dv++) {
485 dv->flag = 0;
486 }
487 }
488
490
491 DEG_id_tag_update(&mesh->id, 0);
492
493 return true;
494}
495
497{
500 Brush *brush = BKE_paint_brush(&ts->wpaint->paint);
501 float vgroup_weight = BKE_brush_weight_get(&ts->wpaint->paint, brush);
502
503 if (ED_wpaint_ensure_data(C, op->reports, WPAINT_ENSURE_MIRROR, nullptr) == false) {
504 return OPERATOR_CANCELLED;
505 }
506
507 if (weight_paint_set(obact, vgroup_weight)) {
508 ED_region_tag_redraw(CTX_wm_region(C)); /* XXX: should redraw all 3D views. */
509 return OPERATOR_FINISHED;
510 }
511 return OPERATOR_CANCELLED;
512}
513
515{
516 /* identifiers */
517 ot->name = "Set Weight";
518 ot->idname = "PAINT_OT_weight_set";
519 ot->description = "Fill the active vertex group with the current paint weight";
520
521 /* API callbacks. */
524
525 /* flags */
527}
528
530
531/* -------------------------------------------------------------------- */
534
535/* *** VGroups Gradient *** */
547
552
561 const float *sco_start; /* [2] */
562 const float *sco_end; /* [2] */
563 float sco_line_div; /* store (1.0f / len_v2v2(sco_start, sco_end)) */
567 /* only for init */
569
570 /* options */
573 short type;
575};
576
577static void gradientVert_update(WPGradient_userData *grad_data, int index)
578{
579 WPGradient_vertStore *vs = &grad_data->vert_cache->elem[index];
580
581 /* Optionally restrict to assigned vertices only. */
582 if (grad_data->use_vgroup_restrict &&
584 {
585 /* In this case the vertex will never have been touched. */
587 return;
588 }
589
590 float alpha;
591 if (grad_data->type == WPAINT_GRADIENT_TYPE_LINEAR) {
592 alpha = line_point_factor_v2(vs->sco, grad_data->sco_start, grad_data->sco_end);
593 }
594 else {
596 alpha = len_v2v2(grad_data->sco_start, vs->sco) * grad_data->sco_line_div;
597 }
598
599 /* adjust weight */
600 alpha = BKE_brush_curve_strength_clamped(grad_data->brush, std::max(0.0f, alpha), 1.0f);
601
602 if (alpha != 0.0f) {
603 MDeformVert *dv = &grad_data->dvert[index];
604 MDeformWeight *dw = BKE_defvert_ensure_index(dv, grad_data->def_nr);
605 // dw->weight = alpha; // testing
606 int tool = grad_data->brush->blend;
607 float testw;
608
609 /* init if we just added */
610 testw = ED_wpaint_blend_tool(
611 tool, vs->weight_orig, grad_data->weightpaint, alpha * grad_data->brush->alpha);
612 CLAMP(testw, 0.0f, 1.0f);
613 dw->weight = testw;
615 }
616 else {
617 MDeformVert *dv = &grad_data->dvert[index];
619 /* normally we nullptr check, but in this case we know it exists */
620 MDeformWeight *dw = BKE_defvert_find_index(dv, grad_data->def_nr);
621 dw->weight = vs->weight_orig;
622 }
623 else {
624 /* wasn't originally existing, remove */
625 MDeformWeight *dw = BKE_defvert_find_index(dv, grad_data->def_nr);
626 if (dw) {
628 }
629 }
630 vs->flag &= ~WPGradient_vertStore::VGRAD_STORE_IS_MODIFIED;
631 }
632}
633
634static void gradientVertUpdate__mapFunc(void *user_data,
635 int index,
636 const float /*co*/[3],
637 const float /*no*/[3])
638{
639 WPGradient_userData *grad_data = static_cast<WPGradient_userData *>(user_data);
640 WPGradient_vertStore *vs = &grad_data->vert_cache->elem[index];
641
642 if (vs->sco[0] == FLT_MAX) {
643 return;
644 }
645
646 gradientVert_update(grad_data, index);
647}
648
649static void gradientVertInit__mapFunc(void *user_data,
650 int index,
651 const float co[3],
652 const float /*no*/[3])
653{
654 WPGradient_userData *grad_data = static_cast<WPGradient_userData *>(user_data);
655 WPGradient_vertStore *vs = &grad_data->vert_cache->elem[index];
656
657 if (grad_data->hide_vert[index] ||
658 (grad_data->use_select &&
659 (!grad_data->select_vert.is_empty() && !grad_data->select_vert[index])))
660 {
661 copy_v2_fl(vs->sco, FLT_MAX);
662 return;
663 }
664
665 /* run first pass only,
666 * the screen coords of the verts need to be cached because
667 * updating the mesh may move them about (entering feedback loop) */
668 if (BLI_BITMAP_TEST(grad_data->vert_visit, index)) {
669 /* Do not copy FLT_MAX here, for generative modifiers we are getting here
670 * multiple times with the same orig index. */
671 return;
672 }
673
677 {
678 copy_v2_fl(vs->sco, FLT_MAX);
679 return;
680 }
681
682 MDeformVert *dv = &grad_data->dvert[index];
683 const MDeformWeight *dw = BKE_defvert_find_index(dv, grad_data->def_nr);
684 if (dw) {
685 vs->weight_orig = dw->weight;
687 }
688 else {
689 vs->weight_orig = 0.0f;
691 }
692 BLI_BITMAP_ENABLE(grad_data->vert_visit, index);
693 gradientVert_update(grad_data, index);
694}
695
697 wmOperator *op,
698 const wmEvent *event)
699{
700 wmGesture *gesture = static_cast<wmGesture *>(op->customdata);
701 WPGradient_vertStoreBase *vert_cache = static_cast<WPGradient_vertStoreBase *>(
702 gesture->user_data.data);
705
707 BKE_report(op->reports, RPT_WARNING, "Active group is locked, aborting");
709 }
710 else {
711 ret = WM_gesture_straightline_modal(C, op, event);
712 }
713
715 if (event->type == LEFTMOUSE && event->val == KM_RELEASE) { /* XXX, hardcoded */
716 /* generally crap! redo! */
720 }
721 }
722
723 if (ret & OPERATOR_CANCELLED) {
724 if (vert_cache != nullptr) {
725 Mesh *mesh = static_cast<Mesh *>(ob->data);
726 if (vert_cache->wpp.wpaint_prev) {
727 MDeformVert *dvert = mesh->deform_verts_for_write().data();
729 BKE_defvert_array_copy(dvert, vert_cache->wpp.wpaint_prev, mesh->verts_num);
730 wpaint_prev_destroy(&vert_cache->wpp);
731 }
732 MEM_freeN(vert_cache);
733 }
734
737 }
738 else if (ret & OPERATOR_FINISHED) {
739 wpaint_prev_destroy(&vert_cache->wpp);
740 MEM_freeN(vert_cache);
741 }
742
743 return ret;
744}
745
747{
748 using namespace blender;
749 wmGesture *gesture = static_cast<wmGesture *>(op->customdata);
750 WPGradient_vertStoreBase *vert_cache;
751 ARegion *region = CTX_wm_region(C);
752 Scene *scene = CTX_data_scene(C);
754 Mesh *mesh = static_cast<Mesh *>(ob->data);
755 MDeformVert *dverts = mesh->deform_verts_for_write().data();
756 int x_start = RNA_int_get(op->ptr, "xstart");
757 int y_start = RNA_int_get(op->ptr, "ystart");
758 int x_end = RNA_int_get(op->ptr, "xend");
759 int y_end = RNA_int_get(op->ptr, "yend");
760 const float sco_start[2] = {float(x_start), float(y_start)};
761 const float sco_end[2] = {float(x_end), float(y_end)};
762 const bool is_interactive = (gesture != nullptr);
763
765
766 WPGradient_userData data = {nullptr};
767
768 if (is_interactive) {
769 if (gesture->user_data.data == nullptr) {
771 (sizeof(WPGradient_vertStore) * mesh->verts_num),
772 __func__);
773 gesture->user_data.use_free = false;
774 data.is_init = true;
775
777 &((WPGradient_vertStoreBase *)gesture->user_data.data)->wpp, dverts, mesh->verts_num);
778
779 /* On initialization only, convert face -> vert sel. */
780 if (mesh->editflag & ME_EDIT_PAINT_FACE_SEL) {
782 }
783 }
784
785 vert_cache = static_cast<WPGradient_vertStoreBase *>(gesture->user_data.data);
786 }
787 else {
788 if (ED_wpaint_ensure_data(C, op->reports, eWPaintFlag(0), nullptr) == false) {
789 return OPERATOR_CANCELLED;
790 }
791
792 data.is_init = true;
793 vert_cache = static_cast<WPGradient_vertStoreBase *>(MEM_mallocN(
794 sizeof(WPGradient_vertStoreBase) + (sizeof(WPGradient_vertStore) * mesh->verts_num),
795 __func__));
796 }
797
798 const blender::bke::AttributeAccessor attributes = mesh->attributes();
799
800 data.region = region;
801 data.scene = scene;
802 data.mesh = mesh;
803 data.dvert = dverts;
804 data.select_vert = *attributes.lookup<bool>(".select_vert", bke::AttrDomain::Point);
805 data.hide_vert = *attributes.lookup_or_default<bool>(
806 ".hide_vert", bke::AttrDomain::Point, false);
807 data.sco_start = sco_start;
808 data.sco_end = sco_end;
809 data.sco_line_div = 1.0f / len_v2v2(sco_start, sco_end);
811 data.use_select = (mesh->editflag & (ME_EDIT_PAINT_FACE_SEL | ME_EDIT_PAINT_VERT_SEL)) != 0;
812 data.vert_cache = vert_cache;
813 data.vert_visit = nullptr;
814 data.type = RNA_enum_get(op->ptr, "type");
815
816 {
818 VPaint *wp = ts->wpaint;
819 Brush *brush = BKE_paint_brush(&wp->paint);
820
822
823 data.brush = brush;
824 data.weightpaint = BKE_brush_weight_get(&wp->paint, brush);
825 data.use_vgroup_restrict = (ts->wpaint->flag & VP_FLAG_VGROUP_RESTRICT) != 0;
826 }
827
828 ED_view3d_init_mats_rv3d(ob, static_cast<RegionView3D *>(region->regiondata));
829
830 const Object *ob_eval = DEG_get_evaluated(depsgraph, ob);
831 const Mesh *mesh_eval = BKE_object_get_evaluated_mesh(ob_eval);
832 if (data.is_init) {
833 data.vert_visit = BLI_BITMAP_NEW(mesh->verts_num, __func__);
834
836
837 MEM_freeN(data.vert_visit);
838 data.vert_visit = nullptr;
839 }
840 else {
842 }
843
846
847 if (is_interactive == false) {
848 MEM_freeN(vert_cache);
849 }
850
851 if (scene->toolsettings->auto_normalize) {
852 const int vgroup_num = BLI_listbase_count(&mesh->vertex_group_names);
853 bool *lock_flags = BKE_object_defgroup_lock_flags_get(ob, vgroup_num);
854 if (!lock_flags) {
855 lock_flags = MEM_malloc_arrayN<bool>(vgroup_num, "lock_flags");
856 std::memset(lock_flags, 0, vgroup_num); /* Clear to false. */
857 lock_flags[data.def_nr] = true;
858 }
859 bool *vgroup_validmap = BKE_object_defgroup_validmap_get(ob, vgroup_num);
860 if (vgroup_validmap != nullptr) {
861 MDeformVert *dvert = dverts;
862 Span<bool> subset_flags_span = Span(vgroup_validmap, vgroup_num);
863 Span<bool> lock_flags_span = Span(lock_flags, vgroup_num);
864
865 for (int i = 0; i < mesh->verts_num; i++) {
866 if ((data.vert_cache->elem[i].flag & WPGradient_vertStore::VGRAD_STORE_IS_MODIFIED) != 0) {
867 BKE_defvert_normalize_lock_map(dvert[i], subset_flags_span, lock_flags_span);
868 }
869 }
870 MEM_SAFE_FREE(lock_flags);
871 MEM_freeN(vgroup_validmap);
872 }
873 }
874
875 return OPERATOR_FINISHED;
876}
877
879 wmOperator *op,
880 const wmEvent *event)
881{
883
884 if (ED_wpaint_ensure_data(C, op->reports, eWPaintFlag(0), nullptr) == false) {
885 return OPERATOR_CANCELLED;
886 }
887
890 ARegion *region = CTX_wm_region(C);
891 if (region->regiontype == RGN_TYPE_WINDOW) {
892 /* TODO: hard-coded, extend `WM_gesture_straightline_*`. */
893 if (event->type == LEFTMOUSE && event->val == KM_PRESS) {
894 wmGesture *gesture = static_cast<wmGesture *>(op->customdata);
895 gesture->is_active = true;
896 }
897 }
898 }
899 return ret;
900}
901
903{
904 /* defined in DNA_space_types.h */
905 static const EnumPropertyItem gradient_types[] = {
906 {WPAINT_GRADIENT_TYPE_LINEAR, "LINEAR", 0, "Linear", ""},
907 {WPAINT_GRADIENT_TYPE_RADIAL, "RADIAL", 0, "Radial", ""},
908 {0, nullptr, 0, nullptr, nullptr},
909 };
910
911 PropertyRNA *prop;
912
913 /* identifiers */
914 ot->name = "Weight Gradient";
915 ot->idname = "PAINT_OT_weight_gradient";
916 ot->description = "Draw a line to apply a weight gradient to selected vertices";
917
918 /* API callbacks. */
924
925 /* flags */
927
928 prop = RNA_def_enum(ot->srna, "type", gradient_types, 0, "Type", "");
930
932}
933
void BKE_brush_weight_set(Paint *paint, Brush *brush, float value)
Definition brush.cc:1371
float BKE_brush_curve_strength_clamped(const Brush *br, float p, float len)
Definition brush.cc:1637
float BKE_brush_weight_get(const Paint *paint, const Brush *brush)
Definition brush.cc:1364
void BKE_curvemapping_init(CurveMapping *cumap)
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C)
Depsgraph * CTX_data_depsgraph_pointer(const bContext *C)
Object * CTX_data_active_object(const bContext *C)
Scene * CTX_data_scene(const bContext *C)
Main * CTX_data_main(const bContext *C)
ToolSettings * CTX_data_tool_settings(const bContext *C)
ARegion * CTX_wm_region(const bContext *C)
support for deformation groups and hooks.
void BKE_defvert_array_free(MDeformVert *dvert, int totvert)
Definition deform.cc:1074
void BKE_defvert_array_copy(MDeformVert *dst, const MDeformVert *src, int totvert)
Definition deform.cc:1038
int BKE_object_defgroup_active_index_get(const Object *ob)
Definition deform.cc:607
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:1017
MDeformWeight * BKE_defvert_ensure_index(MDeformVert *dv, int defgroup)
Definition deform.cc:825
void BKE_defvert_array_free_elems(MDeformVert *dvert, int totvert)
Definition deform.cc:1056
MDeformWeight * BKE_defvert_find_index(const MDeformVert *dv, int defgroup)
Definition deform.cc:806
float BKE_defvert_find_weight(const MDeformVert *dvert, int defgroup)
Definition deform.cc:774
void BKE_defvert_remove_group(MDeformVert *dvert, MDeformWeight *dw)
Definition deform.cc:880
void BKE_defvert_normalize_lock_map(MDeformVert &dvert, blender::Span< bool > subset_flags, blender::Span< bool > lock_flags)
Definition deform.cc:242
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:972
Mesh * BKE_mesh_from_object(Object *ob)
@ MESH_FOREACH_NOP
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)
Object * BKE_modifiers_is_deformed_by_armature(Object *ob)
General operations, lookup, etc. for blender objects.
Mesh * BKE_object_get_evaluated_mesh(const Object *object_eval)
Functions for dealing with objects and deform verts, used by painting and tools.
bool BKE_object_defgroup_check_lock_relative(const bool *lock_flags, const bool *validmap, int index)
void BKE_object_defgroup_split_locked_validmap(int defbase_tot, const bool *locked, const bool *deform, bool *r_locked, bool *r_unlocked)
bool * BKE_object_defgroup_validmap_get(struct Object *ob, int defbase_tot)
bool * BKE_object_defgroup_lock_flags_get(struct Object *ob, int defbase_tot)
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 BKE_object_defgroup_check_lock_relative_multi(int defbase_tot, const bool *lock_flags, const bool *selected, int sel_tot)
bool BKE_object_defgroup_active_is_locked(const struct Object *ob)
bool * BKE_object_defgroup_selected_get(struct Object *ob, int defbase_tot, int *r_dg_flags_sel_tot)
Brush * BKE_paint_brush(Paint *paint)
Definition paint.cc:645
@ RPT_WARNING
Definition BKE_report.hh:38
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:153
#define BLI_assert(a)
Definition BLI_assert.h:46
#define BLI_BITMAP_NEW(_num, _alloc_string)
Definition BLI_bitmap.h:37
#define BLI_BITMAP_TEST(_bitmap, _index)
Definition BLI_bitmap.h:61
#define BLI_BITMAP_ENABLE(_bitmap, _index)
Definition BLI_bitmap.h:78
unsigned int BLI_bitmap
Definition BLI_bitmap.h:13
BLI_INLINE bool BLI_listbase_is_empty(const ListBase *lb)
#define LISTBASE_FOREACH_INDEX(type, var, list, index_var)
int BLI_listbase_count(const ListBase *listbase) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:524
float line_point_factor_v2(const float p[2], const float l1[2], const float l2[2])
MINLINE float len_v2v2(const float v1[2], const float v2[2]) ATTR_WARN_UNUSED_RESULT
MINLINE void copy_v2_fl(float r[2], float f)
unsigned int uint
#define CLAMP(a, b, c)
#define ENUM_OPERATORS(_type, _max)
#define UNLIKELY(x)
void DEG_id_tag_update(ID *id, unsigned int flags)
void DEG_relations_tag_update(Main *bmain)
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
@ ME_EDIT_PAINT_VERT_SEL
@ ME_EDIT_PAINT_FACE_SEL
@ ME_EDIT_MIRROR_TOPO
@ ME_SYMMETRY_X
#define ME_USING_MIRROR_X_VERTEX_GROUPS(_me)
#define ME_EDIT_PAINT_SEL_MODE(_me)
@ OB_MODE_WEIGHT_PAINT
Object is a sort of wrapper for general info.
@ VP_FLAG_VGROUP_RESTRICT
@ SCE_SELECT_FACE
@ SCE_SELECT_VERTEX
@ RGN_TYPE_WINDOW
@ OPERATOR_CANCELLED
@ OPERATOR_INTERFACE
@ OPERATOR_FINISHED
@ OPERATOR_RUNNING_MODAL
#define ARM_GROUPS_ENVELOPE
#define ARM_GROUPS_AUTO
#define ED_MESH_PICK_DEFAULT_VERT_DIST
Definition ED_mesh.hh:654
int mesh_get_x_mirror_vert(Object *ob, Mesh *mesh_eval, int index, bool use_topology)
Definition meshtools.cc:302
bool ED_mesh_pick_face_vert(bContext *C, Object *ob, const int mval[2], uint dist_px, uint *r_index)
Definition meshtools.cc:627
bool ED_mesh_pick_face(bContext *C, Object *ob, const int mval[2], uint dist_px, uint *r_index)
Definition meshtools.cc:567
#define ED_MESH_PICK_DEFAULT_FACE_DIST
Definition ED_mesh.hh:655
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:780
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:618
@ V3D_PROJ_TEST_CLIP_NEAR
Definition ED_view3d.hh:282
@ V3D_PROJ_TEST_CLIP_BB
Definition ED_view3d.hh:280
eV3DProjStatus ED_view3d_project_float_object(const ARegion *region, const float co[3], float r_co[2], eV3DProjTest flag)
void ED_view3d_init_mats_rv3d(const Object *ob, RegionView3D *rv3d)
@ V3D_PROJ_RET_OK
Definition ED_view3d.hh:256
ViewContext ED_view3d_viewcontext_init(bContext *C, Depsgraph *depsgraph)
void view3d_operator_needs_gpu(const bContext *C)
Read Guarded memory(de)allocation.
#define MEM_SAFE_FREE(v)
@ PROP_SKIP_SAVE
Definition RNA_types.hh:344
#define C
Definition RandGen.cpp:29
void UI_popup_menu_end(bContext *C, uiPopupMenu *pup)
uiPopupMenu * UI_popup_menu_begin(bContext *C, const char *title, int icon) ATTR_NONNULL()
uiLayout * UI_popup_menu_layout(uiPopupMenu *pup)
#define UI_ITEM_NONE
#define NC_GEOM
Definition WM_types.hh:393
#define ND_DRAW
Definition WM_types.hh:461
#define NC_BRUSH
Definition WM_types.hh:385
#define ND_DATA
Definition WM_types.hh:509
@ KM_PRESS
Definition WM_types.hh:311
@ KM_RELEASE
Definition WM_types.hh:312
@ OPTYPE_DEPENDS_ON_CURSOR
Definition WM_types.hh:218
@ OPTYPE_UNDO
Definition WM_types.hh:182
@ OPTYPE_REGISTER
Definition WM_types.hh:180
#define NA_EDITED
Definition WM_types.hh:584
#define NC_OBJECT
Definition WM_types.hh:379
void ED_object_vgroup_calc_from_armature(ReportList *reports, Depsgraph *depsgraph, Scene *scene, Object *ob, Object *par, const int mode, const bool mirror)
BMesh const char void * data
BPy_StructRNA * depsgraph
AttributeSet attributes
constexpr bool is_empty() const
Definition BLI_span.hh:260
constexpr int64_t size() const
Definition BLI_span.hh:493
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:137
constexpr bool is_empty() const
Definition BLI_span.hh:260
GAttributeReader lookup_or_default(StringRef attribute_id, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
GAttributeReader lookup(const StringRef attribute_id) const
nullptr float
void * MEM_mallocN(size_t len, const char *str)
Definition mallocn.cc:128
void * MEM_malloc_arrayN(size_t len, size_t size, const char *str)
Definition mallocn.cc:133
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
static char faces[256]
void mesh_select_face_flush(Mesh &mesh)
float ED_wpaint_blend_tool(int tool, float weight, float paintval, float alpha)
int ED_wpaint_mirror_vgroup_ensure(Object *ob, int vgroup_active)
bool weight_paint_mode_poll(bContext *C)
bool ED_wpaint_ensure_data(bContext *C, ReportList *reports, eWPaintFlag flag, WPaintVGroupIndex *vgroup_index)
bool weight_paint_mode_region_view3d_poll(bContext *C)
@ WPAINT_GRADIENT_TYPE_RADIAL
@ WPAINT_GRADIENT_TYPE_LINEAR
eWPaintFlag
@ WPAINT_ENSURE_MIRROR
bool weight_paint_poll_ignore_tool(bContext *C)
static bool weight_paint_sample_mark_groups(const MDeformVert *dvert, blender::MutableSpan< bool > groups)
static wmOperatorStatus paint_weight_gradient_invoke(bContext *C, wmOperator *op, const wmEvent *event)
void PAINT_OT_weight_set(wmOperatorType *ot)
static bool weight_paint_set(Object *ob, float paintweight)
static wmOperatorStatus weight_sample_group_invoke(bContext *C, wmOperator *op, const wmEvent *event)
static wmOperatorStatus weight_paint_set_exec(bContext *C, wmOperator *op)
void PAINT_OT_weight_sample(wmOperatorType *ot)
static void gradientVert_update(WPGradient_userData *grad_data, int index)
static void wpaint_prev_init(WPaintPrev *wpp)
static wmOperatorStatus paint_weight_gradient_exec(bContext *C, wmOperator *op)
static void gradientVertInit__mapFunc(void *user_data, int index, const float co[3], const float[3])
static bool weight_from_bones_poll(bContext *C)
static void gradientVertUpdate__mapFunc(void *user_data, int index, const float[3], const float[3])
static wmOperatorStatus paint_weight_gradient_modal(bContext *C, wmOperator *op, const wmEvent *event)
void PAINT_OT_weight_from_bones(wmOperatorType *ot)
void PAINT_OT_weight_gradient(wmOperatorType *ot)
static wmOperatorStatus weight_sample_invoke(bContext *C, wmOperator *op, const wmEvent *event)
static wmOperatorStatus weight_from_bones_exec(bContext *C, wmOperator *op)
static void wpaint_prev_destroy(WPaintPrev *wpp)
static void wpaint_prev_create(WPaintPrev *wpp, MDeformVert *dverts, int dcount)
void PAINT_OT_weight_sample_group(wmOperatorType *ot)
return ret
void RNA_property_enum_set(PointerRNA *ptr, PropertyRNA *prop, int value)
int RNA_int_get(PointerRNA *ptr, const char *name)
int RNA_enum_get(PointerRNA *ptr, const char *name)
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
#define FLT_MAX
Definition stdcycles.h:14
void * regiondata
float alpha
struct CurveMapping * curve_distance_falloff
short blend
struct MDeformWeight * dw
unsigned int def_nr
char symmetry
ListBase vertex_group_names
char editflag
int vertex_group_active_index
int faces_num
int verts_num
struct ToolSettings * toolsettings
RegionView3D * rv3d
Definition ED_view3d.hh:80
Scene * scene
Definition ED_view3d.hh:73
View3D * v3d
Definition ED_view3d.hh:78
Object * obact
Definition ED_view3d.hh:75
blender::VArray< bool > hide_vert
WPGradient_vertStoreBase * vert_cache
blender::VArraySpan< bool > select_vert
MDeformVert * wpaint_prev
void operator_context_set(blender::wm::OpCallContext opcontext)
PointerRNA op(wmOperatorType *ot, std::optional< blender::StringRef > name, int icon, blender::wm::OpCallContext context, eUI_Item_Flag flag)
wmEventType type
Definition WM_types.hh:757
short val
Definition WM_types.hh:759
int mval[2]
Definition WM_types.hh:763
wmGenericUserData user_data
Definition WM_types.hh:666
uint is_active
Definition WM_types.hh:639
struct ReportList * reports
struct wmOperatorType * type
struct PointerRNA * ptr
i
Definition text_draw.cc:230
@ WM_CURSOR_EDIT
Definition wm_cursors.hh:19
void WM_main_add_notifier(uint type, void *reference)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
@ LEFTMOUSE
wmOperatorType * ot
Definition wm_files.cc:4237
void WM_gesture_straightline_cancel(bContext *C, wmOperator *op)
wmOperatorStatus WM_gesture_straightline_invoke(bContext *C, wmOperator *op, const wmEvent *event)
wmOperatorStatus WM_gesture_straightline_modal(bContext *C, wmOperator *op, const wmEvent *event)
void WM_operator_properties_gesture_straightline(wmOperatorType *ot, int cursor)
std::string WM_operatortype_name(wmOperatorType *ot, PointerRNA *properties)
wmOperatorType * WM_operatortype_find(const char *idname, bool quiet)
wmOperatorStatus WM_menu_invoke(bContext *C, wmOperator *op, const wmEvent *)
uint8_t flag
Definition wm_window.cc:145