Blender V5.3
grease_pencil_lineart.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 <algorithm>
10
11#include "BLI_listbase.hh"
12
13#include "BKE_context.hh"
14#include "BKE_curves.hh"
15#include "BKE_global.hh"
16#include "BKE_grease_pencil.hh"
17#include "BKE_material.hh"
18#include "BKE_modifier.hh"
19#include "BKE_report.hh"
20#include "BKE_scene.hh"
21#include "BKE_screen.hh"
22
23#include "BLT_translation.hh"
24
25#include "DNA_curves_types.h"
26#include "DNA_modifier_types.h"
27
28#include "RNA_access.hh"
29#include "RNA_define.hh"
30
31#include "ED_grease_pencil.hh"
32#include "ED_view3d.hh"
33
34#include "DEG_depsgraph.hh"
36
37#include "MOD_lineart.hh"
38
39namespace blender {
40
41namespace ed::greasepencil {
42
44{
45 bool is_first = true;
46 for (const ModifierData &md : ob.modifiers) {
48 const auto *lmd = reinterpret_cast<const GreasePencilLineartModifierData *>(&md);
49 if (is_first || (lmd->flags & LINEART_GPENCIL_USE_CACHE)) {
50 info.min_level = std::min<int>(info.min_level, lmd->level_start);
51 info.max_level = std::max<int>(
52 info.max_level, lmd->use_multiple_levels ? lmd->level_end : lmd->level_start);
53 info.edge_types |= lmd->edge_types;
54 info.shadow_selection = std::max(info.shadow_selection, lmd->shadow_selection);
55 info.silhouette_selection = std::max(info.silhouette_selection, lmd->silhouette_selection);
56 is_first = false;
57 }
58 }
59 }
60}
61
82
84{
85 /* This function always gets the first line art modifier regardless of their visibility, because
86 * cached line art configuration are always inside the first line art modifier. */
87 for (ModifierData &i_md : ob.modifiers) {
89 return reinterpret_cast<GreasePencilLineartModifierData *>(&i_md);
90 }
91 }
92 return nullptr;
93}
94
95} // namespace ed::greasepencil
96
99 void *owner;
101 float *progress;
102
103 /* C or ob must have one != nullptr. */
107 Depsgraph *dg;
108 int frame;
114};
115
116static bool clear_strokes(Object *ob, ModifierData *md, int frame)
117{
119 return false;
120 }
122 GreasePencil *grease_pencil = reinterpret_cast<GreasePencil *>(ob->data);
123
125 if (!node || !node->is_layer()) {
126 return false;
127 }
128 bke::greasepencil::Layer &layer = node->as_layer();
129
130 if (layer.start_frame_at(frame) == frame) {
131 bke::greasepencil::Drawing *drawing = grease_pencil->get_drawing_at(layer, frame);
132 if (!drawing) {
133 return false;
134 }
135 drawing->strokes_for_write() = {};
136 }
137
138 return true;
139}
140
142{
144
145 /* Toggle on and off the baked flag as we are only interested in if something else is disabling
146 * it. We can assume that the guard function has already toggled this on for all modifiers that
147 * are sent here. */
149
150 bool enabled = BKE_modifier_is_enabled(
152
154
155 return !enabled;
156}
157
158static bool bake_strokes(Object *ob,
159 Depsgraph *dg,
160 LineartCache **lc,
162 int frame,
163 bool is_first)
164{
165 /* Modifier data sanity check. */
167 return false;
168 }
169
170 GreasePencil *grease_pencil = reinterpret_cast<GreasePencil *>(ob->data);
171
173 if (!node || !node->is_layer()) {
174 return false;
175 }
176 bke::greasepencil::Layer &layer = node->as_layer();
177
178 bke::greasepencil::Drawing *drawing = nullptr;
179 if (layer.start_frame_at(frame) == frame) {
180 drawing = grease_pencil->get_drawing_at(layer, frame);
181 }
182 else {
183 drawing = grease_pencil->insert_frame(layer, frame);
184 }
185 if (!drawing) [[unlikely]] {
186 return false;
187 }
188
189 LineartCache *local_lc = nullptr;
190 const bool should_compute_again = is_first || !(lmd->flags & LINEART_GPENCIL_USE_CACHE);
191 if (!(*lc)) {
194 }
195 else {
196 if (should_compute_again) {
197 MOD_lineart_compute_feature_lines_v3(dg, *lmd, &local_lc, !(ob->dtx & OB_DRAW_IN_FRONT));
199 }
200 else {
201 /* Use the cached result, `*lc` is already valid. */
202 local_lc = *lc;
203 }
205 lmd->cache = local_lc;
206 }
207
209 lmd->cache,
210 ob->world_to_object(),
211 dg,
212 *drawing,
213 lmd->source_type,
214 lmd->source_object,
216 lmd->level_start,
217 lmd->use_multiple_levels ? lmd->level_end : lmd->level_start,
219 lmd->edge_types,
220 lmd->mask_switches,
223 lmd->radius,
224 lmd->opacity,
225 lmd->fill_strokes,
226 lmd->shadow_selection,
229 lmd->vgname,
230 lmd->flags,
231 lmd->calculation_flags);
232
233 if (should_compute_again) {
234 MOD_lineart_clear_cache(&local_lc);
235 }
236
237 return true;
238}
239
240static bool bake_single_target(LineartBakeJob *bj, Object *ob, int frame)
241{
242 bool touched = false;
243 if (G.is_break || ob->type != OB_GREASE_PENCIL) {
244 return false;
245 }
246
247 if (bj->overwrite_frames) {
248 for (ModifierData &md : ob->modifiers) {
250 if (clear_strokes(ob, &md, frame)) {
251 touched = true;
252 }
253 }
254 }
255 }
256
259
260 LineartCache *lc = nullptr;
261 bool is_first = true;
262 for (ModifierData &md : ob->modifiers) {
264 continue;
265 }
267 &md);
269
270 if (bake_strokes(ob, bj->dg, &lc, lmd, frame, is_first)) {
271 touched = true;
272 is_first = false;
273 }
274 }
276
277 return touched;
278}
279
281{
282 for (const int object : bj.objects.index_range()) {
283 Object *ob = bj.objects[object];
284
285 for (ModifierData &md : ob->modifiers) {
288 &md);
290 }
291 }
292 }
293}
294
295static void lineart_bake_startjob(void *customdata, wmJobWorkerStatus *worker_status)
296{
297 LineartBakeJob *bj = static_cast<LineartBakeJob *>(customdata);
298 bj->stop = &worker_status->stop;
299 bj->do_update = &worker_status->do_update;
300 bj->progress = &worker_status->progress;
301
302 guard_modifiers(*bj);
303
305
306 for (int frame = bj->frame_begin; frame <= bj->frame_end; frame += bj->frame_increment) {
307
308 if (G.is_break) {
309 G.is_break = false;
310 break;
311 }
312
313 BKE_scene_frame_set(bj->scene, frame);
316
317 for (const int object : bj->objects.index_range()) {
318 Object *ob = bj->objects[object];
319 if (bake_single_target(bj, ob, frame)) {
322 }
323 }
324
325 /* Update and refresh the progress bar. */
326 *bj->progress = float(frame - bj->frame_begin) / (bj->frame_end - bj->frame_begin);
327 *bj->do_update = true;
328 }
329
330 /* This need to be reset manually. */
331 G.is_break = false;
332
333 /* Restore original frame. */
336}
337
338static void lineart_bake_endjob(void *customdata)
339{
340 LineartBakeJob *bj = static_cast<LineartBakeJob *>(customdata);
341
343
345
346 for (const int object : bj->objects.index_range()) {
347 Object *ob = bj->objects[object];
349 }
350}
351
352static void lineart_bake_job_free(void *customdata)
353{
354 LineartBakeJob *bj = static_cast<LineartBakeJob *>(customdata);
355 MEM_delete(bj);
356}
357
359 wmOperator *op,
360 bool bake_all_targets,
361 bool do_background)
362{
363 LineartBakeJob *bj = MEM_new<LineartBakeJob>(__func__);
364
365 if (!bake_all_targets) {
367 if (!ob || ob->type != OB_GREASE_PENCIL) {
369 "No active object, or active object isn't a Grease Pencil object");
370 return OPERATOR_CANCELLED;
371 }
372 bj->objects.append(ob);
373 }
374 else {
375 /* #CTX_DATA_BEGIN is not available for iterating in objects while using the job system. */
376 CTX_DATA_BEGIN (C, Object *, ob, visible_objects) {
377 for (ModifierData &md : ob->modifiers) {
379 bj->objects.append(ob);
380 break;
381 }
382 }
383 }
385 }
386 bj->C = C;
387 Scene *scene = CTX_data_scene(C);
388 bj->scene = scene;
390 bj->frame_begin = scene->r.sfra;
391 bj->frame_end = scene->r.efra;
392 bj->frame_orig = scene->r.cfra;
393 bj->frame_increment = scene->r.frame_step;
394 bj->overwrite_frames = true;
395
396 if (do_background) {
399 scene,
400 "Baking Line Art...",
403
407
409
411
413
415 }
416
417 wmJobWorkerStatus worker_status = {};
418 lineart_bake_startjob(bj, &worker_status);
419
420 /* Need to call endjob manually to clear interface locking status when bake is not called as
421 * background task, otherwise spaces like 3d viewport can be unresponsive. */
423
424 MEM_delete(bj);
425
426 return OPERATOR_FINISHED;
427}
428
430 wmOperator *op,
431 const wmEvent * /*event*/)
432{
433 bool bake_all = RNA_boolean_get(op->ptr, "bake_all");
434 return lineart_bake_common(C, op, bake_all, true);
435}
437{
438 bool bake_all = RNA_boolean_get(op->ptr, "bake_all");
439 return lineart_bake_common(C, op, bake_all, false);
440}
442 wmOperator *op,
443 const wmEvent * /*event*/)
444{
445 Scene *scene = static_cast<Scene *>(op->customdata);
446
447 /* no running blender, remove handler and pass through. */
450 }
451
453}
454
456{
457 for (ModifierData &md : ob->modifiers) {
459 continue;
460 }
462 &md);
463 GreasePencil *grease_pencil = reinterpret_cast<GreasePencil *>(ob->data);
464
466 if (!node || !node->is_layer()) {
467 return;
468 }
469 bke::greasepencil::Layer &layer = node->as_layer();
470
471 /* Remove all the keyframes in this layer. */
472 grease_pencil->remove_frames(layer, layer.sorted_keys());
473 grease_pencil->insert_frame(layer, 0);
474
476
478 }
480}
481
483{
484 bool clear_all = RNA_boolean_get(op->ptr, "clear_all");
485
486 if (clear_all) {
487 CTX_DATA_BEGIN (C, Object *, ob, visible_objects) {
488 if (ob->type != OB_GREASE_PENCIL) {
489 continue;
490 }
493 }
495 BKE_report(op->reports, RPT_INFO, "All Line Art objects are now cleared of bakes");
496 }
497 else {
499 if (ob->type != OB_GREASE_PENCIL) {
500 return OPERATOR_CANCELLED;
501 }
504 BKE_report(op->reports, RPT_INFO, "Baked strokes are cleared");
505 }
506
507 return OPERATOR_FINISHED;
508}
509
511 wmOperatorType *ot,
512 PointerRNA *properties)
513{
514 const bool bake_all = RNA_boolean_get(properties, "bake_all");
515 if (bake_all) {
516 return TIP_("Bake Line Art for all Grease Pencil objects");
517 }
518 return ot->description ? CTX_IFACE_(ot->translation_context, ot->description) : "";
519}
520
522{
523 ot->name = "Bake Line Art";
524 ot->description = "Bake Line Art for current Grease Pencil object";
525 ot->idname = "OBJECT_OT_lineart_bake_strokes";
526
532
533 RNA_def_boolean(ot->srna, "bake_all", false, "Bake All", "Bake all Line Art modifiers");
534}
535
536static std::string lineart_clear_get_description(bContext * /*C*/,
537 wmOperatorType *ot,
538 PointerRNA *properties)
539{
540 const bool clear_all = RNA_boolean_get(properties, "clear_all");
541 if (clear_all) {
542 return TIP_("Clear all strokes in all Grease Pencil object");
543 }
544 return ot->description ? CTX_IFACE_(ot->translation_context, ot->description) : "";
545}
546
548{
549 ot->name = "Clear Baked Line Art";
550 ot->description = "Clear all strokes in current Grease Pencil object";
551 ot->idname = "OBJECT_OT_lineart_clear";
552
556
557 RNA_def_boolean(ot->srna, "clear_all", false, "Clear All", "Clear all Line Art modifier bakes");
558}
559
565
566} // namespace blender
#define CTX_DATA_BEGIN(C, Type, instance, member)
#define CTX_DATA_END
Low-level operations for curves.
Low-level operations for grease pencil.
General operations, lookup, etc. for materials.
#define TIP_(msgid)
#define CTX_IFACE_(context, msgid)
#define C
Definition RandGen.cpp:29
#define ND_DATA
Definition WM_types.hh:552
#define NC_SCENE
Definition WM_types.hh:391
#define NA_EDITED
Definition WM_types.hh:634
#define ND_FRAME
Definition WM_types.hh:453
#define NC_GPENCIL
Definition WM_types.hh:412
bke::CurvesGeometry & strokes_for_write()
#define layer
bool active_grease_pencil_poll(bContext *C)
void get_lineart_modifier_limits(const Object &ob, ed::greasepencil::LineartLimitInfo &info)
GreasePencilLineartModifierData * get_first_lineart_modifier(const Object &ob)
void set_lineart_modifier_limits(GreasePencilLineartModifierData &lmd, const ed::greasepencil::LineartLimitInfo &info, const bool cache_is_ready)
BMesh Iterators.
void WM_main_add_notifier(uint type, void *reference)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void WM_operatortype_append(void(*opfunc)(wmOperatorType *))
void BKE_scene_graph_update_for_newframe(Depsgraph *depsgraph)
Definition scene.cc:2953
static wmOperatorStatus lineart_bake_strokes_common_modal(bContext *C, wmOperator *op, const wmEvent *)
void DEG_graph_build_from_view_layer(Depsgraph *graph)
void WM_jobs_callbacks(wmJob *wm_job, wm_jobs_start_callback startjob, void(*initjob)(void *), void(*update)(void *), void(*endjob)(void *))
Definition wm_jobs.cc:407
@ REGION_DRAW_LOCK_BAKING
void BKE_spacedata_draw_locks(ARegionDrawLockFlags lock_flags)
Definition screen.cc:558
Depsgraph * CTX_data_depsgraph_pointer(const bContext *C)
@ WM_JOB_TYPE_LINEART
Definition WM_api.hh:2046
static wmOperatorStatus lineart_bake_strokes_invoke(bContext *C, wmOperator *op, const wmEvent *)
@ eModifierType_GreasePencilLineart
void MOD_lineart_clear_cache(LineartCache **lc)
wmWindow * CTX_wm_window(const bContext *C)
void WM_jobs_customdata_set(wmJob *wm_job, void *customdata, void(*free)(void *customdata))
Definition wm_jobs.cc:377
@ WM_JOB_PROGRESS
Definition WM_api.hh:1997
static bool clear_strokes(Object *ob, ModifierData *md, int frame)
void BKE_scene_frame_set(Scene *scene, float frame)
Definition scene.cc:2600
bool MOD_lineart_compute_feature_lines_v3(Depsgraph *depsgraph, GreasePencilLineartModifierData &lmd, LineartCache **cached_result, bool enable_stroke_depth_offset)
void WM_jobs_start(wmWindowManager *wm, wmJob *wm_job)
Definition wm_jobs.cc:498
void MOD_lineart_chain_clear_picked_flag(LineartCache *lc)
Global G
Definition blender.cc:57
void WM_locked_interface_set(wmWindowManager *wm, bool lock)
void WM_locked_interface_set_with_flags(wmWindowManager *wm, short lock_flags)
static void OBJECT_OT_lineart_clear(wmOperatorType *ot)
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
bool WM_jobs_has_running(const wmWindowManager *window_manager, const void *owner, const eWM_JobType type, const eWM_JobFlag exclude_flags)
Definition wm_jobs.cc:809
static wmOperatorStatus lineart_gpencil_clear_strokes_exec(bContext *C, wmOperator *op)
void MOD_lineart_gpencil_generate_v3(const LineartCache *cache, const float4x4 &inverse_mat, Depsgraph *depsgraph, bke::greasepencil::Drawing &drawing, const int8_t source_type, Object *source_object, Collection *source_collection, const int level_start, const int level_end, const int mat_nr, const int16_t edge_types, const uchar mask_switches, const uchar material_mask_bits, const uchar intersection_mask, const float thickness, const float opacity, const bool fill_strokes, const uchar shadow_selection, const uchar silhouette_mode, const char *source_vgname, const char *vgname, const int modifier_flags, const int modifier_calculation_flags)
wmWindowManager * CTX_wm_manager(const bContext *C)
void MOD_lineart_destroy_render_data_v3(GreasePencilLineartModifierData *lmd)
void WM_global_report(eReportType type, const char *message)
static wmOperatorStatus lineart_bake_common(bContext *C, wmOperator *op, bool bake_all_targets, bool do_background)
int BKE_object_material_index_get(Object *ob, const Material *ma)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
static wmOperatorStatus lineart_bake_strokes_exec(bContext *C, wmOperator *op)
static void lineart_gpencil_clear_strokes_exec_common(Object *ob)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
static void OBJECT_OT_lineart_bake_strokes(wmOperatorType *ot)
wmEventHandler_Op * WM_event_add_modal_handler(bContext *C, wmOperator *op)
static void lineart_bake_endjob(void *customdata)
static bool bake_strokes(Object *ob, Depsgraph *dg, LineartCache **lc, GreasePencilLineartModifierData *lmd, int frame, bool is_first)
wmJob * WM_jobs_get(wmWindowManager *wm, wmWindow *win, const void *owner, const char *name, const eWM_JobFlag flag, const eWM_JobType job_type)
Definition wm_jobs.cc:228
static bool lineart_mod_is_disabled(Scene *scene, GreasePencilLineartModifierData *md)
void ED_operatortypes_grease_pencil_lineart()
static void lineart_bake_startjob(void *customdata, wmJobWorkerStatus *worker_status)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
static bool bake_single_target(LineartBakeJob *bj, Object *ob, int frame)
static void lineart_bake_job_free(void *customdata)
Scene * CTX_data_scene(const bContext *C)
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
static std::string lineart_clear_get_description(bContext *, wmOperatorType *ot, PointerRNA *properties)
Scene * DEG_get_evaluated_scene(const Depsgraph *graph)
bool BKE_modifier_is_enabled(const Scene *scene, ModifierData *md, int required_mode)
static void guard_modifiers(LineartBakeJob &bj)
Object * CTX_data_active_object(const bContext *C)
void WM_jobs_timer(wmJob *wm_job, double time_step, uint note, uint endnote, void(*timer_step)(void *))
Definition wm_jobs.cc:393
static std::string lineart_bake_strokes_get_description(bContext *, wmOperatorType *ot, PointerRNA *properties)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
eGreasePencilLineartMaskSwitches mask_switches
GreasePencilLineartModifierSource source_type
ListBaseT< ModifierData > modifiers
eObject_DrawExtraFlag dtx
struct RenderData r
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
const char * translation_context
Definition WM_types.hh:1113
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
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
std::string(*) get_description(bContext *C, wmOperatorType *ot, PointerRNA *ptr)
Definition WM_types.hh:1197
struct ReportList * reports