Blender V5.3
grease_pencil_pen.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9
10#include "BKE_attribute.hh"
11#include "BKE_context.hh"
12#include "BKE_curves.hh"
13#include "BKE_curves_utils.hh"
14#include "BKE_deform.hh"
15#include "BKE_grease_pencil.hh"
16#include "BKE_material.hh"
17#include "BKE_report.hh"
18
19#include "BLI_array_utils.hh"
20
21#include "BLT_translation.hh"
22
23#include "WM_api.hh"
24#include "WM_types.hh"
25
26#include "RNA_access.hh"
27#include "RNA_define.hh"
28#include "RNA_enum_types.hh"
29
30#include "DEG_depsgraph.hh"
31
32#include "DNA_material_types.h"
33
34#include "ED_curves.hh"
35#include "ED_grease_pencil.hh"
36#include "ED_screen.hh"
37#include "ED_view3d.hh"
38
39#include "UI_resources.hh"
40
41namespace blender {
42
43namespace ed::greasepencil {
44
46 public:
49
50 /* Helper class to project screen space coordinates to 3D. */
52
53 float3 project(const float2 &screen_co) const override
54 {
55 return this->placement.project(screen_co);
56 }
57
58 IndexMask all_selected_points(const int curves_index, IndexMaskMemory &memory) const override
59 {
60 const MutableDrawingInfo &info = this->drawings[curves_index];
62 *this->vc.obact,
63 info.drawing,
64 info.layer_index,
65 this->vc.v3d->overlay.handle_display,
66 memory);
67 }
68
70 IndexMaskMemory &memory) const override
71 {
72 const MutableDrawingInfo &info = this->drawings[curves_index];
74 *this->vc.obact,
75 info.drawing,
76 info.layer_index,
77 this->vc.v3d->overlay.handle_display,
78 memory);
79 }
80
81 IndexMask editable_curves(const int curves_index, IndexMaskMemory &memory) const override
82 {
83 const MutableDrawingInfo &info = this->drawings[curves_index];
85 *this->vc.obact, info.drawing, info.layer_index, memory);
86 }
87
88 void tag_curve_changed(const int curves_index) const override
89 {
90 const MutableDrawingInfo &info = this->drawings[curves_index];
92 }
93
94 bke::CurvesGeometry &get_curves(const int curves_index) const override
95 {
96 const MutableDrawingInfo &info = this->drawings[curves_index];
97 return info.drawing.strokes_for_write();
98 }
99
100 IndexRange curves_range() const override
101 {
102 return this->drawings.index_range();
103 }
104
105 void single_point_attributes(bke::CurvesGeometry &curves, const int curves_index) const override
106 {
107 const MutableDrawingInfo &info = this->drawings[curves_index];
108 info.drawing.opacities_for_write().last() = 1.0f;
109 bke::MutableAttributeAccessor attributes = curves.attributes_for_write();
110
111 if (bke::SpanAttributeWriter aspect_ratios = attributes.lookup_for_write_span<float>(
112 "aspect_ratio"))
113 {
114 aspect_ratios.span.last() = 1.0f;
115 aspect_ratios.finish();
116 }
117
118 if (bke::SpanAttributeWriter u_scales = attributes.lookup_for_write_span<float>("u_scale")) {
119 u_scales.span.last() = 1.0f;
120 u_scales.finish();
121 }
122
123 if (bke::SpanAttributeWriter fill_opacities = attributes.lookup_for_write_span<float>(
124 "fill_opacity"))
125 {
126 fill_opacities.span.last() = 1.0f;
127 fill_opacities.finish();
128 }
129 }
130
131 bool can_create_new_curve(wmOperator *op) const override
132 {
133 if (!this->grease_pencil->has_active_layer()) {
134 BKE_report(op->reports, RPT_ERROR, "No active Grease Pencil layer");
135 return false;
136 }
137
138 bke::greasepencil::Layer &layer = *this->grease_pencil->get_active_layer();
139 if (!layer.is_editable()) {
140 BKE_report(op->reports, RPT_ERROR, "Active layer is locked or hidden");
141 return false;
142 }
143
144 const int material_index = this->vc.obact->actcol - 1;
145 Material *material = BKE_object_material_get(this->vc.obact, material_index + 1);
146 /* The editable materials are unlocked and not hidden. */
147 if (material != nullptr && material->gp_style != nullptr &&
148 ((material->gp_style->flag & GP_MATERIAL_LOCKED) != 0 ||
149 (material->gp_style->flag & GP_MATERIAL_HIDE) != 0))
150 {
151 BKE_report(op->reports, RPT_ERROR, "Active Material is locked or hidden");
152 return false;
153 }
154
155 /* Ensure a drawing at the current keyframe. */
156 bool inserted_keyframe = false;
158 *this->vc.scene, *this->grease_pencil, layer, false, inserted_keyframe))
159 {
160 BKE_report(op->reports, RPT_ERROR, "No Grease Pencil frame to draw on");
161 return false;
162 }
163
164 /* We should insert the keyframe when initializing not here. */
165 BLI_assert(!inserted_keyframe);
166 BLI_assert(this->active_drawing_index != std::nullopt);
167
168 return true;
169 }
170
171 void update_view(bContext *C) const override
172 {
173 GreasePencil *grease_pencil = this->grease_pencil;
174
177
179 }
180
181 std::optional<wmOperatorStatus> initialize(bContext *C,
182 wmOperator *op,
183 const wmEvent * /*event*/) override
184 {
186 BKE_report(op->reports, RPT_ERROR, "Selection Mode must be Points");
187 return OPERATOR_CANCELLED;
188 }
189
190 GreasePencil *grease_pencil = id_cast<GreasePencil *>(this->vc.obact->data);
191 this->grease_pencil = grease_pencil;
193
194 /* Initialize helper class for projecting screen space coordinates. */
196 *this->vc.region,
197 *view3d,
198 *this->vc.obact,
199 grease_pencil->get_active_layer());
202 }
203 else if (placement.use_project_to_stroke()) {
205 }
206
207 bool inserted_keyframe = false;
208 /* For the pen tool, we don't want the auto-key to create an empty keyframe, so we
209 * duplicate the previous key. */
210 const bool use_duplicate_previous_key = true;
211 for (bke::greasepencil::Layer *layer : grease_pencil->layers_for_write()) {
212 if (layer->is_editable()) {
215 *layer,
216 use_duplicate_previous_key,
217 inserted_keyframe);
218 }
219 }
220
221 /* Update the view. */
222 if (inserted_keyframe) {
224 }
225
226 this->placement = placement;
227 this->drawings = retrieve_editable_drawings(*this->vc.scene, *this->grease_pencil);
228
229 for (const int drawing_index : this->drawings.index_range()) {
230 const MutableDrawingInfo &info = this->drawings[drawing_index];
231 const bke::greasepencil::Layer &layer = this->grease_pencil->layer(info.layer_index);
232 this->layer_to_object_per_curves.append(layer.local_transform());
233 this->layer_to_world_per_curves.append(layer.to_world_space(*this->vc.obact));
234 }
235
236 this->active_drawing_index = std::nullopt;
237 const bke::greasepencil::Layer *active_layer = this->grease_pencil->get_active_layer();
238
239 if (active_layer != nullptr) {
240 const bke::greasepencil::Drawing *active_drawing =
241 this->grease_pencil->get_editable_drawing_at(*active_layer, this->vc.scene->r.cfra);
242
243 for (const int drawing_index : this->drawings.index_range()) {
244 const MutableDrawingInfo &info = this->drawings[drawing_index];
245
246 if (active_drawing == &info.drawing) {
247 BLI_assert(this->active_drawing_index == std::nullopt);
248 this->active_drawing_index = drawing_index;
249 }
250 }
251 }
252
253 return std::nullopt;
254 }
255};
256
257/* Exit and free memory. */
259{
261
262 /* Clear status message area. */
263 ED_workspace_status_text(C, nullptr);
264
266
267 ptd->update_view(C);
268
269 MEM_delete(ptd);
270 /* Clear pointer. */
271 op->customdata = nullptr;
272}
273
274/* Invoke handler: Initialize the operator. */
276{
277 /* Allocate new data. */
278 GreasePencilPenToolOperation *ptd_pointer = MEM_new<GreasePencilPenToolOperation>(__func__);
279 op->customdata = ptd_pointer;
280 GreasePencilPenToolOperation &ptd = *ptd_pointer;
281
282 const wmOperatorStatus result = ptd.invoke(C, op, event);
285 }
286 return result;
287}
288
289/* Modal handler: Events handling during interactive part. */
291{
293 op->customdata);
294
295 const wmOperatorStatus result = ptd.modal(C, op, event);
298 }
299 return result;
300}
301
303{
304 /* Identifiers. */
305 ot->name = "Grease Pencil Pen";
306 ot->idname = "GREASE_PENCIL_OT_pen";
307 ot->description = "Construct and edit splines";
308
309 /* Callbacks. */
312
313 /* Flags. */
314 ot->flag = OPTYPE_UNDO;
315
316 /* Properties. */
318}
319
320} // namespace ed::greasepencil
321
326
328{
330 WM_modalkeymap_assign(keymap, "GREASE_PENCIL_OT_pen");
331}
332
333} // namespace blender
Low-level operations for curves.
Low-level operations for curves.
support for deformation groups and hooks.
Low-level operations for grease pencil.
General operations, lookup, etc. for materials.
#define C
Definition RandGen.cpp:29
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DATA
Definition WM_types.hh:552
#define NA_EDITED
Definition WM_types.hh:634
#define NC_GPENCIL
Definition WM_types.hh:412
IndexRange index_range() const
GSpanAttributeWriter lookup_for_write_span(StringRef name)
MutableSpan< float > opacities_for_write()
bke::CurvesGeometry & strokes_for_write()
wmOperatorStatus invoke(bContext *C, wmOperator *op, const wmEvent *event)
wmOperatorStatus modal(bContext *C, wmOperator *op, const wmEvent *event)
void cache_viewport_depths(Depsgraph *depsgraph, ARegion *region, View3D *view3d)
float3 project(float2 co, bool &clipped) const
std::optional< wmOperatorStatus > initialize(bContext *C, wmOperator *op, const wmEvent *) override
IndexMask all_selected_points(const int curves_index, IndexMaskMemory &memory) const override
float3 project(const float2 &screen_co) const override
IndexMask visible_bezier_handle_points(const int curves_index, IndexMaskMemory &memory) const override
void tag_curve_changed(const int curves_index) const override
IndexMask editable_curves(const int curves_index, IndexMaskMemory &memory) const override
void single_point_attributes(bke::CurvesGeometry &curves, const int curves_index) const override
bke::CurvesGeometry & get_curves(const int curves_index) const override
#define layer
void pen_tool_common_props(wmOperatorType *ot)
wmKeyMap * ensure_keymap(wmKeyConfig *keyconf)
bool ensure_active_keyframe(const Scene &scene, GreasePencil &grease_pencil, bke::greasepencil::Layer &layer, const bool duplicate_previous_key, bool &r_inserted_keyframe)
static void grease_pencil_pen_exit(bContext *C, wmOperator *op)
IndexMask retrieve_editable_and_all_selected_points(Object &object, const bke::greasepencil::Drawing &drawing, const int layer_index, const eHandleDisplay handle_display, IndexMaskMemory &memory)
static void GREASE_PENCIL_OT_pen(wmOperatorType *ot)
IndexMask retrieve_visible_bezier_handle_points(Object &object, const bke::greasepencil::Drawing &drawing, const int layer_index, const eHandleDisplay handle_display, IndexMaskMemory &memory)
static wmOperatorStatus grease_pencil_pen_modal(bContext *C, wmOperator *op, const wmEvent *event)
IndexMask retrieve_editable_strokes(Object &object, const bke::greasepencil::Drawing &drawing, int layer_index, IndexMaskMemory &memory)
Vector< MutableDrawingInfo > retrieve_editable_drawings(const Scene &scene, GreasePencil &grease_pencil)
static wmOperatorStatus grease_pencil_pen_invoke(bContext *C, wmOperator *op, const wmEvent *event)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void WM_operatortype_append(void(*opfunc)(wmOperatorType *))
Depsgraph * CTX_data_depsgraph_pointer(const bContext *C)
Material * BKE_object_material_get(Object *ob, short act)
void ED_grease_pencil_pentool_modal_keymap(wmKeyConfig *keyconf)
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:681
VecBase< float, 2 > float2
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
void WM_cursor_modal_restore(wmWindow *win)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void ED_operatortypes_grease_pencil_pen()
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
void WM_modalkeymap_assign(wmKeyMap *km, const char *opname)
@ OPTYPE_UNDO
Definition WM_types.hh:190
VecBase< float, 3 > float3
void ED_workspace_status_text(bContext *C, const char *str)
Definition area.cc:1153
View3D * CTX_wm_view3d(const bContext *C)
struct RenderData r
struct ToolSettings * toolsettings
eGPencil_Selectmode_types gpencil_selectmode_edit
const char * description
Definition WM_types.hh:1115
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