Blender V4.3
grease_pencil_weight_draw.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6
8
10 public:
12 {
13 this->stroke_mode = stroke_mode;
14 }
15
16 void on_stroke_begin(const bContext &C, const InputSample &start_sample) override
17 {
18 using namespace blender::ed::greasepencil;
19
20 this->get_brush_settings(C, start_sample);
23
24 /* Get the add/subtract mode of the draw brush. */
25 this->invert_brush_weight = (this->brush->flag & BRUSH_DIR_IN) != 0;
26 if (this->stroke_mode == BRUSH_STROKE_INVERT) {
28 }
29
30 /* Get editable drawings grouped per frame number. When multi-frame editing is disabled, this
31 * is just one group for the current frame. When multi-frame editing is enabled, the selected
32 * keyframes are grouped per frame number. */
33 const Scene *scene = CTX_data_scene(&C);
34 Array<Vector<MutableDrawingInfo>> drawings_per_frame =
35 retrieve_editable_drawings_grouped_per_frame(*scene, *this->grease_pencil);
36
37 this->drawing_weight_data = Array<Array<DrawingWeightData>>(drawings_per_frame.size());
38
39 /* Get weight data for all drawings in this frame group. */
40 for (const int frame_group : drawings_per_frame.index_range()) {
41 const Vector<MutableDrawingInfo> &drawings = drawings_per_frame[frame_group];
42 this->init_weight_data_for_drawings(C, drawings, frame_group);
43 }
44 }
45
46 void on_stroke_extended(const bContext &C, const InputSample &extension_sample) override
47 {
48 using namespace blender::ed::greasepencil;
49
50 this->get_mouse_input_sample(extension_sample);
51
52 /* Iterate over the drawings grouped per frame number. Collect all stroke points under the
53 * brush and draw weight on them. */
54 std::atomic<bool> changed = false;
56 this->drawing_weight_data.index_range(), [&](const int frame_group) {
57 Array<DrawingWeightData> &drawing_weights = this->drawing_weight_data[frame_group];
58
59 /* For all layers at this key frame, collect the stroke points under the brush in a
60 * buffer. */
61 threading::parallel_for_each(drawing_weights, [&](DrawingWeightData &drawing_weight) {
62 for (const int point_index : drawing_weight.point_positions.index_range()) {
63 const float2 &co = drawing_weight.point_positions[point_index];
64
65 /* When the point is under the brush, add it to the brush point buffer. */
66 this->add_point_under_brush_to_brush_buffer(co, drawing_weight, point_index);
67 }
68 });
69
70 /* Apply the Draw brush to all points in the brush buffer. */
71 threading::parallel_for_each(drawing_weights, [&](DrawingWeightData &drawing_weight) {
72 for (const BrushPoint &point : drawing_weight.points_in_brush) {
73 this->apply_weight_to_point(point, this->brush_weight, drawing_weight);
74
75 /* Normalize weights of bone-deformed vertex groups to 1.0f. */
76 if (this->auto_normalize) {
77 normalize_vertex_weights(drawing_weight.deform_verts[point.drawing_point_index],
78 drawing_weight.active_vertex_group,
79 drawing_weight.locked_vgroups,
80 drawing_weight.bone_deformed_vgroups);
81 }
82 }
83
84 if (!drawing_weight.points_in_brush.is_empty()) {
85 changed = true;
86 drawing_weight.points_in_brush.clear();
87 }
88 });
89 });
90
91 if (changed) {
94 }
95 }
96
97 void on_stroke_done(const bContext & /*C*/) override {}
98};
99
100std::unique_ptr<GreasePencilStrokeOperation> new_weight_paint_draw_operation(
101 const BrushStrokeMode &stroke_mode)
102{
103 return std::make_unique<DrawWeightPaintOperation>(stroke_mode);
104}
105
106} // namespace blender::ed::sculpt_paint::greasepencil
Scene * CTX_data_scene(const bContext *C)
void DEG_id_tag_update(ID *id, unsigned int flags)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1041
@ BRUSH_DIR_IN
#define NC_GEOM
Definition WM_types.hh:360
#define ND_DATA
Definition WM_types.hh:475
int64_t size() const
Definition BLI_array.hh:245
IndexRange index_range() const
Definition BLI_array.hh:349
void on_stroke_begin(const bContext &C, const InputSample &start_sample) override
void on_stroke_extended(const bContext &C, const InputSample &extension_sample) override
void get_mouse_input_sample(const InputSample &input_sample, const float brush_widen_factor=1.0f)
void get_brush_settings(const bContext &C, const InputSample &start_sample)
void init_weight_data_for_drawings(const bContext &C, const Span< ed::greasepencil::MutableDrawingInfo > &drawings, const int frame_group)
void apply_weight_to_point(const BrushPoint &point, const float target_weight, DrawingWeightData &drawing_weight)
std::unique_ptr< GreasePencilStrokeOperation > new_weight_paint_draw_operation(const BrushStrokeMode &brush_mode)
void parallel_for_each(Range &&range, const Function &function)
Definition BLI_task.hh:58
BrushStrokeMode
@ BRUSH_STROKE_INVERT
void WM_event_add_notifier(const bContext *C, uint type, void *reference)