Blender V5.0
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 =
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 /* Skip read-only points. */
64 if (drawing_weight.point_is_read_only[point_index]) {
65 continue;
66 }
67
68 /* When the point is under the brush, add it to the brush point buffer. */
69 const float2 &co = drawing_weight.point_positions[point_index];
70 this->add_point_under_brush_to_brush_buffer(co, drawing_weight, point_index);
71 }
72 });
73
74 /* Apply the Draw brush to all points in the brush buffer. */
75 threading::parallel_for_each(drawing_weights, [&](DrawingWeightData &drawing_weight) {
76 for (const BrushPoint &point : drawing_weight.points_in_brush) {
77 this->apply_weight_to_point(point, this->brush_weight, drawing_weight);
78
79 /* Normalize weights of bone-deformed vertex groups to 1.0f. */
80 if (this->auto_normalize) {
82 drawing_weight.active_vertex_group,
83 drawing_weight.locked_vgroups,
84 drawing_weight.bone_deformed_vgroups);
85 }
86 }
87
88 if (!drawing_weight.points_in_brush.is_empty()) {
89 changed.store(true, std::memory_order_relaxed);
90 drawing_weight.points_in_brush.clear();
91 }
92 });
93 });
94
95 if (changed) {
98 }
99 }
100
101 void on_stroke_done(const bContext & /*C*/) override {}
102};
103
104std::unique_ptr<GreasePencilStrokeOperation> new_weight_paint_draw_operation(
105 const BrushStrokeMode &stroke_mode)
106{
107 return std::make_unique<DrawWeightPaintOperation>(stroke_mode);
108}
109
110} // 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:1074
@ BRUSH_DIR_IN
#define C
Definition RandGen.cpp:29
#define NC_GEOM
Definition WM_types.hh:393
#define ND_DATA
Definition WM_types.hh:509
int64_t size() const
Definition BLI_array.hh:256
IndexRange index_range() const
Definition BLI_array.hh:360
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)
void normalize_vertex_weights(MDeformVert &dvert, const int active_vertex_group, const Span< bool > vertex_group_is_locked, const Span< bool > vertex_group_is_bone_deformed)
Array< Vector< MutableDrawingInfo > > retrieve_editable_drawings_grouped_per_frame(const Scene &scene, GreasePencil &grease_pencil)
std::unique_ptr< GreasePencilStrokeOperation > new_weight_paint_draw_operation(const BrushStrokeMode &stroke_mode)
void parallel_for_each(Range &&range, const Function &function)
Definition BLI_task.hh:56
BrushStrokeMode
@ BRUSH_STROKE_INVERT
void WM_event_add_notifier(const bContext *C, uint type, void *reference)