Blender V4.5
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"
50#include "UI_resources.hh"
51
52#include "paint_intern.hh" /* own include */
53
54/* -------------------------------------------------------------------- */
59
60struct WPaintPrev {
61 /* previous vertex weights */
63 /* allocation size of prev buffers */
64 int tot;
65};
66
68{
69 wpp->wpaint_prev = nullptr;
70 wpp->tot = 0;
71}
72
73static void wpaint_prev_create(WPaintPrev *wpp, MDeformVert *dverts, int dcount)
74{
76
77 if (dverts && dcount) {
78 wpp->wpaint_prev = MEM_malloc_arrayN<MDeformVert>(dcount, __func__);
79 wpp->tot = dcount;
80 BKE_defvert_array_copy(wpp->wpaint_prev, dverts, dcount);
81 }
82}
83
85{
86 if (wpp->wpaint_prev) {
88 }
89 wpp->wpaint_prev = nullptr;
90 wpp->tot = 0;
91}
92
94
95/* -------------------------------------------------------------------- */
98
100{
102
104}
105
107{
109 Scene *scene = CTX_data_scene(C);
112 Mesh *mesh = static_cast<Mesh *>(ob->data);
113 int type = RNA_enum_get(op->ptr, "type");
114
116 op->reports, depsgraph, scene, ob, armob, type, (mesh->symmetry & ME_SYMMETRY_X));
117
118 DEG_id_tag_update(&mesh->id, 0);
121
122 return OPERATOR_FINISHED;
123}
124
126{
127 static const EnumPropertyItem type_items[] = {
128 {ARM_GROUPS_AUTO, "AUTOMATIC", 0, "Automatic", "Automatic weights from bones"},
130 "ENVELOPES",
131 0,
132 "From Envelopes",
133 "Weights from envelopes with user defined radius"},
134 {0, nullptr, 0, nullptr, nullptr},
135 };
136
137 /* identifiers */
138 ot->name = "Weight from Bones";
139 ot->idname = "PAINT_OT_weight_from_bones";
140 ot->description =
141 ("Set the weights of the groups matching the attached armature's selected bones, "
142 "using the distance between the vertices and the bones");
143
144 /* API callbacks. */
146 ot->invoke = WM_menu_invoke;
148
149 /* flags */
151
152 /* properties */
153 ot->prop = RNA_def_enum(
154 ot->srna, "type", type_items, 0, "Type", "Method to use for assigning weights");
155}
156
158
159/* -------------------------------------------------------------------- */
162
169{
171 Mesh *mesh;
172 bool changed = false;
173
175 mesh = BKE_mesh_from_object(vc.obact);
176 const MDeformVert *dvert = mesh->deform_verts().data();
177
178 if (mesh && dvert && vc.v3d && vc.rv3d && (mesh->vertex_group_active_index != 0)) {
179 const bool use_vert_sel = (mesh->editflag & ME_EDIT_PAINT_VERT_SEL) != 0;
180 int v_idx_best = -1;
181 uint index;
182
185
186 if (use_vert_sel) {
188 C, vc.obact, event->mval, ED_MESH_PICK_DEFAULT_VERT_DIST, true, &index))
189 {
190 v_idx_best = index;
191 }
192 }
193 else {
195 {
196 v_idx_best = index;
197 }
198 else if (ED_mesh_pick_face(C, vc.obact, event->mval, ED_MESH_PICK_DEFAULT_FACE_DIST, &index))
199 {
200 /* This relies on knowing the internal workings of #ED_mesh_pick_face_vert() */
202 op->reports, RPT_WARNING, "The modifier used does not support deformed locations");
203 }
204 }
205
206 if (v_idx_best != -1) { /* should always be valid */
208 Brush *brush = BKE_paint_brush(&ts->wpaint->paint);
209 const int vgroup_active = mesh->vertex_group_active_index - 1;
210 float vgroup_weight = BKE_defvert_find_weight(&dvert[v_idx_best], vgroup_active);
211 const int defbase_tot = BLI_listbase_count(&mesh->vertex_group_names);
212 bool use_lock_relative = ts->wpaint_lock_relative;
213 bool *defbase_locked = nullptr, *defbase_unlocked = nullptr;
214
215 if (use_lock_relative) {
216 defbase_locked = BKE_object_defgroup_lock_flags_get(vc.obact, defbase_tot);
217 defbase_unlocked = BKE_object_defgroup_validmap_get(vc.obact, defbase_tot);
218
219 use_lock_relative = BKE_object_defgroup_check_lock_relative(
220 defbase_locked, defbase_unlocked, vgroup_active);
221 }
222
223 /* use combined weight in multipaint mode,
224 * since that's what is displayed to the user in the colors */
225 if (ts->multipaint) {
226 int defbase_tot_sel;
227 bool *defbase_sel = BKE_object_defgroup_selected_get(
228 vc.obact, defbase_tot, &defbase_tot_sel);
229
230 if (defbase_tot_sel > 1) {
233 vc.obact, defbase_tot, defbase_sel, defbase_sel, &defbase_tot_sel);
234 }
235
236 use_lock_relative = use_lock_relative &&
238 defbase_tot, defbase_locked, defbase_sel, defbase_tot_sel);
239
240 bool is_normalized = ts->auto_normalize || use_lock_relative;
242 &dvert[v_idx_best], defbase_tot, defbase_sel, defbase_tot_sel, is_normalized);
243 }
244
245 MEM_freeN(defbase_sel);
246 }
247
248 if (use_lock_relative) {
250 defbase_tot, defbase_locked, defbase_unlocked, defbase_locked, defbase_unlocked);
251
252 vgroup_weight = BKE_defvert_lock_relative_weight(
253 vgroup_weight, &dvert[v_idx_best], defbase_tot, defbase_locked, defbase_unlocked);
254 }
255
256 MEM_SAFE_FREE(defbase_locked);
257 MEM_SAFE_FREE(defbase_unlocked);
258
259 CLAMP(vgroup_weight, 0.0f, 1.0f);
260 BKE_brush_weight_set(vc.scene, brush, vgroup_weight);
261 changed = true;
262 }
263 }
264
265 if (changed) {
266 /* not really correct since the brush didn't change, but redraws the toolbar */
267 WM_main_add_notifier(NC_BRUSH | NA_EDITED, nullptr); /* ts->wpaint->paint.brush */
268
269 return OPERATOR_FINISHED;
270 }
271 return OPERATOR_CANCELLED;
272}
273
275{
276 /* identifiers */
277 ot->name = "Weight Paint Sample Weight";
278 ot->idname = "PAINT_OT_weight_sample";
279 ot->description = "Use the mouse to sample a weight in the 3D view";
280
281 /* API callbacks. */
282 ot->invoke = weight_sample_invoke;
284
285 /* flags */
287}
288
290
291/* -------------------------------------------------------------------- */
294
301{
302 bool found = false;
303 int i = dvert->totweight;
304 MDeformWeight *dw;
305 for (dw = dvert->dw; i > 0; dw++, i--) {
306 if (UNLIKELY(dw->def_nr >= groups.size())) {
307 continue;
308 }
309 groups[dw->def_nr] = true;
310 found = true;
311 }
312 return found;
313}
314
316 wmOperator *op,
317 const wmEvent *event)
318{
321 BLI_assert(vc.v3d && vc.rv3d); /* Ensured by poll. */
322
323 Mesh *mesh = BKE_mesh_from_object(vc.obact);
324 const MDeformVert *dverts = mesh->deform_verts().data();
325 if (BLI_listbase_is_empty(&mesh->vertex_group_names) || (dverts == nullptr)) {
326 BKE_report(op->reports, RPT_WARNING, "No vertex group data");
327 return OPERATOR_CANCELLED;
328 }
329
330 const bool use_vert_sel = (mesh->editflag & ME_EDIT_PAINT_VERT_SEL) != 0;
332
333 bool found = false;
334
337
338 if (use_vert_sel) {
339 /* Extract from the vertex. */
340 uint index;
341 if (ED_mesh_pick_vert(C, vc.obact, event->mval, ED_MESH_PICK_DEFAULT_VERT_DIST, true, &index))
342 {
343 const MDeformVert *dvert = &dverts[index];
344 found |= weight_paint_sample_mark_groups(dvert, groups);
345 }
346 }
347 else {
348 /* Extract from the face. */
349 const blender::OffsetIndices faces = mesh->faces();
350 const blender::Span<int> corner_verts = mesh->corner_verts();
351 uint index;
352 if (ED_mesh_pick_face(C, vc.obact, event->mval, ED_MESH_PICK_DEFAULT_FACE_DIST, &index)) {
353 for (const int vert : corner_verts.slice(faces[index])) {
354 found |= weight_paint_sample_mark_groups(&dverts[vert], groups);
355 }
356 }
357 }
358
359 if (found == false) {
360 BKE_report(op->reports, RPT_WARNING, "No vertex groups found");
361 return OPERATOR_CANCELLED;
362 }
363
365 C, WM_operatortype_name(op->type, op->ptr).c_str(), ICON_NONE);
366 uiLayout *layout = UI_popup_menu_layout(pup);
367 wmOperatorType *ot = WM_operatortype_find("OBJECT_OT_vertex_group_set_active", false);
369 uiLayoutSetOperatorContext(layout, opcontext);
370 int i = 0;
372 if (groups[i] == false) {
373 continue;
374 }
375 PointerRNA op_ptr = layout->op(ot, dg->name, ICON_NONE, WM_OP_EXEC_DEFAULT, UI_ITEM_NONE);
376 RNA_property_enum_set(&op_ptr, ot->prop, i);
377 }
378 UI_popup_menu_end(C, pup);
379
380 return OPERATOR_INTERFACE;
381}
382
384{
385 /* identifiers */
386 ot->name = "Weight Paint Sample Group";
387 ot->idname = "PAINT_OT_weight_sample_group";
388 ot->description = "Select one of the vertex groups available under current mouse position";
389
390 /* API callbacks. */
393
394 /* flags */
396}
397
399
400/* -------------------------------------------------------------------- */
403
404/* fills in the selected faces with the current weight and vertex group */
405static bool weight_paint_set(Object *ob, float paintweight)
406{
407 using namespace blender;
408 Mesh *mesh = static_cast<Mesh *>(ob->data);
409 MDeformWeight *dw, *dw_prev;
410 int vgroup_active, vgroup_mirror = -1;
411 const bool topology = (mesh->editflag & ME_EDIT_MIRROR_TOPO) != 0;
412
413 /* mutually exclusive, could be made into a */
414 const short paint_selmode = ME_EDIT_PAINT_SEL_MODE(mesh);
415
416 const blender::OffsetIndices faces = mesh->faces();
417 const blender::Span<int> corner_verts = mesh->corner_verts();
418 MDeformVert *dvert = mesh->deform_verts_for_write().data();
419
420 if (mesh->faces_num == 0 || dvert == nullptr) {
421 return false;
422 }
423
424 vgroup_active = BKE_object_defgroup_active_index_get(ob) - 1;
425
426 /* if mirror painting, find the other group */
428 vgroup_mirror = ED_wpaint_mirror_vgroup_ensure(ob, vgroup_active);
429 }
430
431 WPaintPrev wpp;
432 wpaint_prev_create(&wpp, dvert, mesh->verts_num);
433
434 const bke::AttributeAccessor attributes = mesh->attributes();
435 const VArraySpan select_vert = *attributes.lookup<bool>(".select_vert", bke::AttrDomain::Point);
436 const VArraySpan select_poly = *attributes.lookup<bool>(".select_poly", bke::AttrDomain::Face);
437
438 for (const int i : faces.index_range()) {
439 if ((paint_selmode == SCE_SELECT_FACE) && !(!select_poly.is_empty() && select_poly[i])) {
440 continue;
441 }
442
443 for (const int vert : corner_verts.slice(faces[i])) {
444 if (!dvert[vert].flag) {
445 if ((paint_selmode == SCE_SELECT_VERTEX) &&
446 !(!select_vert.is_empty() && select_vert[vert]))
447 {
448 continue;
449 }
450
451 dw = BKE_defvert_ensure_index(&dvert[vert], vgroup_active);
452 if (dw) {
453 dw_prev = BKE_defvert_ensure_index(wpp.wpaint_prev + vert, vgroup_active);
454 dw_prev->weight = dw->weight; /* set the undo weight */
455 dw->weight = paintweight;
456
457 if (mesh->symmetry & ME_SYMMETRY_X) {
458 /* x mirror painting */
459 int j = mesh_get_x_mirror_vert(ob, nullptr, vert, topology);
460 if (j >= 0) {
461 /* copy, not paint again */
462 if (vgroup_mirror != -1) {
463 dw = BKE_defvert_ensure_index(dvert + j, vgroup_mirror);
464 dw_prev = BKE_defvert_ensure_index(wpp.wpaint_prev + j, vgroup_mirror);
465 }
466 else {
467 dw = BKE_defvert_ensure_index(dvert + j, vgroup_active);
468 dw_prev = BKE_defvert_ensure_index(wpp.wpaint_prev + j, vgroup_active);
469 }
470 dw_prev->weight = dw->weight; /* set the undo weight */
471 dw->weight = paintweight;
472 }
473 }
474 }
475 dvert[vert].flag = 1;
476 }
477 }
478 }
479
480 {
481 MDeformVert *dv = dvert;
482 for (int index = mesh->verts_num; index != 0; index--, dv++) {
483 dv->flag = 0;
484 }
485 }
486
488
489 DEG_id_tag_update(&mesh->id, 0);
490
491 return true;
492}
493
495{
496 Scene *scene = CTX_data_scene(C);
499 Brush *brush = BKE_paint_brush(&ts->wpaint->paint);
500 float vgroup_weight = BKE_brush_weight_get(scene, brush);
501
502 if (ED_wpaint_ensure_data(C, op->reports, WPAINT_ENSURE_MIRROR, nullptr) == false) {
503 return OPERATOR_CANCELLED;
504 }
505
506 if (weight_paint_set(obact, vgroup_weight)) {
507 ED_region_tag_redraw(CTX_wm_region(C)); /* XXX: should redraw all 3D views. */
508 return OPERATOR_FINISHED;
509 }
510 return OPERATOR_CANCELLED;
511}
512
514{
515 /* identifiers */
516 ot->name = "Set Weight";
517 ot->idname = "PAINT_OT_weight_set";
518 ot->description = "Fill the active vertex group with the current paint weight";
519
520 /* API callbacks. */
523
524 /* flags */
526}
527
529
530/* -------------------------------------------------------------------- */
533
534/* *** VGroups Gradient *** */
546
551
560 const float *sco_start; /* [2] */
561 const float *sco_end; /* [2] */
562 float sco_line_div; /* store (1.0f / len_v2v2(sco_start, sco_end)) */
566 /* only for init */
568
569 /* options */
572 short type;
574};
575
576static void gradientVert_update(WPGradient_userData *grad_data, int index)
577{
578 WPGradient_vertStore *vs = &grad_data->vert_cache->elem[index];
579
580 /* Optionally restrict to assigned vertices only. */
581 if (grad_data->use_vgroup_restrict &&
583 {
584 /* In this case the vertex will never have been touched. */
586 return;
587 }
588
589 float alpha;
590 if (grad_data->type == WPAINT_GRADIENT_TYPE_LINEAR) {
591 alpha = line_point_factor_v2(vs->sco, grad_data->sco_start, grad_data->sco_end);
592 }
593 else {
595 alpha = len_v2v2(grad_data->sco_start, vs->sco) * grad_data->sco_line_div;
596 }
597
598 /* adjust weight */
599 alpha = BKE_brush_curve_strength_clamped(grad_data->brush, std::max(0.0f, alpha), 1.0f);
600
601 if (alpha != 0.0f) {
602 MDeformVert *dv = &grad_data->dvert[index];
603 MDeformWeight *dw = BKE_defvert_ensure_index(dv, grad_data->def_nr);
604 // dw->weight = alpha; // testing
605 int tool = grad_data->brush->blend;
606 float testw;
607
608 /* init if we just added */
609 testw = ED_wpaint_blend_tool(
610 tool, vs->weight_orig, grad_data->weightpaint, alpha * grad_data->brush->alpha);
611 CLAMP(testw, 0.0f, 1.0f);
612 dw->weight = testw;
614 }
615 else {
616 MDeformVert *dv = &grad_data->dvert[index];
618 /* normally we nullptr check, but in this case we know it exists */
619 MDeformWeight *dw = BKE_defvert_find_index(dv, grad_data->def_nr);
620 dw->weight = vs->weight_orig;
621 }
622 else {
623 /* wasn't originally existing, remove */
624 MDeformWeight *dw = BKE_defvert_find_index(dv, grad_data->def_nr);
625 if (dw) {
627 }
628 }
629 vs->flag &= ~WPGradient_vertStore::VGRAD_STORE_IS_MODIFIED;
630 }
631}
632
633static void gradientVertUpdate__mapFunc(void *user_data,
634 int index,
635 const float /*co*/[3],
636 const float /*no*/[3])
637{
638 WPGradient_userData *grad_data = static_cast<WPGradient_userData *>(user_data);
639 WPGradient_vertStore *vs = &grad_data->vert_cache->elem[index];
640
641 if (vs->sco[0] == FLT_MAX) {
642 return;
643 }
644
645 gradientVert_update(grad_data, index);
646}
647
648static void gradientVertInit__mapFunc(void *user_data,
649 int index,
650 const float co[3],
651 const float /*no*/[3])
652{
653 WPGradient_userData *grad_data = static_cast<WPGradient_userData *>(user_data);
654 WPGradient_vertStore *vs = &grad_data->vert_cache->elem[index];
655
656 if (grad_data->hide_vert[index] ||
657 (grad_data->use_select &&
658 (!grad_data->select_vert.is_empty() && !grad_data->select_vert[index])))
659 {
660 copy_v2_fl(vs->sco, FLT_MAX);
661 return;
662 }
663
664 /* run first pass only,
665 * the screen coords of the verts need to be cached because
666 * updating the mesh may move them about (entering feedback loop) */
667 if (BLI_BITMAP_TEST(grad_data->vert_visit, index)) {
668 /* Do not copy FLT_MAX here, for generative modifiers we are getting here
669 * multiple times with the same orig index. */
670 return;
671 }
672
676 {
677 copy_v2_fl(vs->sco, FLT_MAX);
678 return;
679 }
680
681 MDeformVert *dv = &grad_data->dvert[index];
682 const MDeformWeight *dw = BKE_defvert_find_index(dv, grad_data->def_nr);
683 if (dw) {
684 vs->weight_orig = dw->weight;
686 }
687 else {
688 vs->weight_orig = 0.0f;
690 }
691 BLI_BITMAP_ENABLE(grad_data->vert_visit, index);
692 gradientVert_update(grad_data, index);
693}
694
696 wmOperator *op,
697 const wmEvent *event)
698{
699 wmGesture *gesture = static_cast<wmGesture *>(op->customdata);
700 WPGradient_vertStoreBase *vert_cache = static_cast<WPGradient_vertStoreBase *>(
701 gesture->user_data.data);
704
706 BKE_report(op->reports, RPT_WARNING, "Active group is locked, aborting");
708 }
709 else {
710 ret = WM_gesture_straightline_modal(C, op, event);
711 }
712
714 if (event->type == LEFTMOUSE && event->val == KM_RELEASE) { /* XXX, hardcoded */
715 /* generally crap! redo! */
719 }
720 }
721
722 if (ret & OPERATOR_CANCELLED) {
723 if (vert_cache != nullptr) {
724 Mesh *mesh = static_cast<Mesh *>(ob->data);
725 if (vert_cache->wpp.wpaint_prev) {
726 MDeformVert *dvert = mesh->deform_verts_for_write().data();
728 BKE_defvert_array_copy(dvert, vert_cache->wpp.wpaint_prev, mesh->verts_num);
729 wpaint_prev_destroy(&vert_cache->wpp);
730 }
731 MEM_freeN(vert_cache);
732 }
733
736 }
737 else if (ret & OPERATOR_FINISHED) {
738 wpaint_prev_destroy(&vert_cache->wpp);
739 MEM_freeN(vert_cache);
740 }
741
742 return ret;
743}
744
746{
747 using namespace blender;
748 wmGesture *gesture = static_cast<wmGesture *>(op->customdata);
749 WPGradient_vertStoreBase *vert_cache;
750 ARegion *region = CTX_wm_region(C);
751 Scene *scene = CTX_data_scene(C);
753 Mesh *mesh = static_cast<Mesh *>(ob->data);
754 MDeformVert *dverts = mesh->deform_verts_for_write().data();
755 int x_start = RNA_int_get(op->ptr, "xstart");
756 int y_start = RNA_int_get(op->ptr, "ystart");
757 int x_end = RNA_int_get(op->ptr, "xend");
758 int y_end = RNA_int_get(op->ptr, "yend");
759 const float sco_start[2] = {float(x_start), float(y_start)};
760 const float sco_end[2] = {float(x_end), float(y_end)};
761 const bool is_interactive = (gesture != nullptr);
762
764
765 WPGradient_userData data = {nullptr};
766
767 if (is_interactive) {
768 if (gesture->user_data.data == nullptr) {
770 (sizeof(WPGradient_vertStore) * mesh->verts_num),
771 __func__);
772 gesture->user_data.use_free = false;
773 data.is_init = true;
774
776 &((WPGradient_vertStoreBase *)gesture->user_data.data)->wpp, dverts, mesh->verts_num);
777
778 /* On initialization only, convert face -> vert sel. */
779 if (mesh->editflag & ME_EDIT_PAINT_FACE_SEL) {
781 }
782 }
783
784 vert_cache = static_cast<WPGradient_vertStoreBase *>(gesture->user_data.data);
785 }
786 else {
787 if (ED_wpaint_ensure_data(C, op->reports, eWPaintFlag(0), nullptr) == false) {
788 return OPERATOR_CANCELLED;
789 }
790
791 data.is_init = true;
792 vert_cache = static_cast<WPGradient_vertStoreBase *>(MEM_mallocN(
793 sizeof(WPGradient_vertStoreBase) + (sizeof(WPGradient_vertStore) * mesh->verts_num),
794 __func__));
795 }
796
797 const blender::bke::AttributeAccessor attributes = mesh->attributes();
798
799 data.region = region;
800 data.scene = scene;
801 data.mesh = mesh;
802 data.dvert = dverts;
803 data.select_vert = *attributes.lookup<bool>(".select_vert", bke::AttrDomain::Point);
804 data.hide_vert = *attributes.lookup_or_default<bool>(
805 ".hide_vert", bke::AttrDomain::Point, false);
806 data.sco_start = sco_start;
807 data.sco_end = sco_end;
808 data.sco_line_div = 1.0f / len_v2v2(sco_start, sco_end);
810 data.use_select = (mesh->editflag & (ME_EDIT_PAINT_FACE_SEL | ME_EDIT_PAINT_VERT_SEL)) != 0;
811 data.vert_cache = vert_cache;
812 data.vert_visit = nullptr;
813 data.type = RNA_enum_get(op->ptr, "type");
814
815 {
817 VPaint *wp = ts->wpaint;
818 Brush *brush = BKE_paint_brush(&wp->paint);
819
821
822 data.brush = brush;
823 data.weightpaint = BKE_brush_weight_get(scene, brush);
824 data.use_vgroup_restrict = (ts->wpaint->flag & VP_FLAG_VGROUP_RESTRICT) != 0;
825 }
826
827 ED_view3d_init_mats_rv3d(ob, static_cast<RegionView3D *>(region->regiondata));
828
829 const Object *ob_eval = DEG_get_evaluated(depsgraph, ob);
830 const Mesh *mesh_eval = BKE_object_get_evaluated_mesh(ob_eval);
831 if (data.is_init) {
832 data.vert_visit = BLI_BITMAP_NEW(mesh->verts_num, __func__);
833
835
836 MEM_freeN(data.vert_visit);
837 data.vert_visit = nullptr;
838 }
839 else {
841 }
842
845
846 if (is_interactive == false) {
847 MEM_freeN(vert_cache);
848 }
849
850 if (scene->toolsettings->auto_normalize) {
851 const int vgroup_num = BLI_listbase_count(&mesh->vertex_group_names);
852 bool *lock_flags = BKE_object_defgroup_lock_flags_get(ob, vgroup_num);
853 bool *vgroup_validmap = BKE_object_defgroup_validmap_get(ob, vgroup_num);
854 if (vgroup_validmap != nullptr) {
855 MDeformVert *dvert = dverts;
856 for (int i = 0; i < mesh->verts_num; i++) {
857 if ((data.vert_cache->elem[i].flag & WPGradient_vertStore::VGRAD_STORE_IS_MODIFIED) != 0) {
858 if (lock_flags != nullptr) {
860 &dvert[i], vgroup_validmap, vgroup_num, lock_flags, vgroup_num);
861 }
862 else {
863 BKE_defvert_normalize_lock_single(&dvert[i], vgroup_validmap, vgroup_num, data.def_nr);
864 }
865 }
866 }
867 MEM_freeN(vgroup_validmap);
868 }
869 }
870
871 return OPERATOR_FINISHED;
872}
873
875 wmOperator *op,
876 const wmEvent *event)
877{
879
880 if (ED_wpaint_ensure_data(C, op->reports, eWPaintFlag(0), nullptr) == false) {
881 return OPERATOR_CANCELLED;
882 }
883
886 ARegion *region = CTX_wm_region(C);
887 if (region->regiontype == RGN_TYPE_WINDOW) {
888 /* TODO: hard-coded, extend `WM_gesture_straightline_*`. */
889 if (event->type == LEFTMOUSE && event->val == KM_PRESS) {
890 wmGesture *gesture = static_cast<wmGesture *>(op->customdata);
891 gesture->is_active = true;
892 }
893 }
894 }
895 return ret;
896}
897
899{
900 /* defined in DNA_space_types.h */
901 static const EnumPropertyItem gradient_types[] = {
902 {WPAINT_GRADIENT_TYPE_LINEAR, "LINEAR", 0, "Linear", ""},
903 {WPAINT_GRADIENT_TYPE_RADIAL, "RADIAL", 0, "Radial", ""},
904 {0, nullptr, 0, nullptr, nullptr},
905 };
906
907 PropertyRNA *prop;
908
909 /* identifiers */
910 ot->name = "Weight Gradient";
911 ot->idname = "PAINT_OT_weight_gradient";
912 ot->description = "Draw a line to apply a weight gradient to selected vertices";
913
914 /* API callbacks. */
920
921 /* flags */
923
924 prop = RNA_def_enum(ot->srna, "type", gradient_types, 0, "Type", "");
926
928}
929
void BKE_brush_weight_set(const Scene *scene, Brush *brush, float value)
Definition brush.cc:1283
float BKE_brush_weight_get(const Scene *scene, const Brush *brush)
Definition brush.cc:1276
float BKE_brush_curve_strength_clamped(const Brush *br, float p, float len)
Definition brush.cc:1566
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:1063
void BKE_defvert_array_copy(MDeformVert *dst, const MDeformVert *src, int totvert)
Definition deform.cc:1027
void BKE_defvert_normalize_lock_single(MDeformVert *dvert, const bool *vgroup_subset, int vgroup_num, uint def_nr_lock)
Definition deform.cc:296
int BKE_object_defgroup_active_index_get(const Object *ob)
Definition deform.cc:596
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:1006
MDeformWeight * BKE_defvert_ensure_index(MDeformVert *dv, int defgroup)
Definition deform.cc:814
void BKE_defvert_array_free_elems(MDeformVert *dvert, int totvert)
Definition deform.cc:1045
MDeformWeight * BKE_defvert_find_index(const MDeformVert *dv, int defgroup)
Definition deform.cc:795
float BKE_defvert_find_weight(const MDeformVert *dvert, int defgroup)
Definition deform.cc:763
void BKE_defvert_remove_group(MDeformVert *dvert, MDeformWeight *dw)
Definition deform.cc:869
void BKE_defvert_normalize_lock_map(MDeformVert *dvert, const bool *vgroup_subset, int vgroup_num, const bool *lock_flags, int defbase_num)
Definition deform.cc:350
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:961
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:636
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:126
#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:982
@ ME_SYMMETRY_X
@ ME_EDIT_PAINT_VERT_SEL
@ ME_EDIT_PAINT_FACE_SEL
@ ME_EDIT_MIRROR_TOPO
#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.
@ SCE_SELECT_FACE
@ SCE_SELECT_VERTEX
@ VP_FLAG_VGROUP_RESTRICT
@ 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:602
int mesh_get_x_mirror_vert(Object *ob, Mesh *mesh_eval, int index, bool use_topology)
Definition meshtools.cc:911
bool ED_mesh_pick_face_vert(bContext *C, Object *ob, const int mval[2], uint dist_px, uint *r_index)
bool ED_mesh_pick_face(bContext *C, Object *ob, const int mval[2], uint dist_px, uint *r_index)
#define ED_MESH_PICK_DEFAULT_FACE_DIST
Definition ED_mesh.hh:603
bool ED_mesh_pick_vert(bContext *C, Object *ob, const int mval[2], uint dist_px, bool use_zbuf, uint *r_index)
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:639
@ 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.
@ PROP_SKIP_SAVE
Definition RNA_types.hh:330
#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
void uiLayoutSetOperatorContext(uiLayout *layout, wmOperatorCallContext opcontext)
#define NC_GEOM
Definition WM_types.hh:390
#define ND_DRAW
Definition WM_types.hh:458
#define NC_BRUSH
Definition WM_types.hh:382
#define ND_DATA
Definition WM_types.hh:506
@ 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:581
wmOperatorCallContext
Definition WM_types.hh:236
@ WM_OP_EXEC_DEFAULT
Definition WM_types.hh:245
#define NC_OBJECT
Definition WM_types.hh:376
@ KM_PRESS
Definition WM_types.hh:308
@ KM_RELEASE
Definition WM_types.hh:309
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(const StringRef attribute_id) const
GAttributeReader lookup_or_default(StringRef attribute_id, AttrDomain domain, eCustomDataType data_type, const void *default_value=nullptr) const
#define MEM_SAFE_FREE(v)
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 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)
bool ED_wpaint_ensure_data(bContext *C, ReportList *reports, enum eWPaintFlag flag, WPaintVGroupIndex *vgroup_index)
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
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
PointerRNA op(wmOperatorType *ot, std::optional< blender::StringRef > name, int icon, wmOperatorCallContext context, eUI_Item_Flag flag)
wmEventType type
Definition WM_types.hh:754
short val
Definition WM_types.hh:756
int mval[2]
Definition WM_types.hh:760
wmGenericUserData user_data
Definition WM_types.hh:663
uint is_active
Definition WM_types.hh:636
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:4225
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:139