Blender V5.3
editaction_gpencil.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2008 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10#include <cmath>
11#include <cstdlib>
12#include <cstring>
13
14#include "MEM_guardedalloc.h"
15
16#include "BLI_listbase.hh"
17#include "BLI_string_utf8.hh"
18#include "BLI_utildefines.hh"
19
21#include "DNA_scene_types.h"
22
23#include "BKE_annotations.h"
24
25#include "ED_anim_api.hh"
26#include "ED_gpencil_legacy.hh"
27#include "ED_keyframes_edit.hh"
28#include "ED_markers.hh"
29
30#include "WM_api.hh"
31
32#include "DEG_depsgraph.hh"
33
34namespace blender {
35
36/* ***************************************** */
37/* NOTE ABOUT THIS FILE:
38 * This file contains code for editing Grease Pencil data in the Action Editor
39 * as a 'keyframes', so that a user can adjust the timing of Grease Pencil drawings.
40 * Therefore, this file mostly contains functions for selecting Grease-Pencil frames.
41 */
42/* ***************************************** */
43/* Generics - Loopers */
44
46 Scene *scene,
47 bool (*gpf_cb)(bGPDframe *, Scene *))
48{
49 /* error checker */
50 if (gpl == nullptr) {
51 return false;
52 }
53
54 /* do loop */
55 for (bGPDframe &gpf : gpl->frames) {
56 /* execute callback */
57 if (gpf_cb(&gpf, scene)) {
58 return true;
59 }
60 }
61
62 /* nothing to return */
63 return false;
64}
65
66/* ****************************************** */
67/* Data Conversion Tools */
68
69void ED_gpencil_layer_make_cfra_list(bGPDlayer *gpl, ListBaseT<CfraElem> *elems, bool onlysel)
70{
71 CfraElem *ce;
72
73 /* error checking */
74 if (ELEM(nullptr, gpl, elems)) {
75 return;
76 }
77
78 /* loop through gp-frames, adding */
79 for (bGPDframe &gpf : gpl->frames) {
80 if ((onlysel == 0) || (gpf.flag & GP_FRAME_SELECT)) {
81 ce = MEM_new_zeroed<CfraElem>("CfraElem");
82
83 ce->cfra = float(gpf.framenum);
84 ce->sel = (gpf.flag & GP_FRAME_SELECT) ? 1 : 0;
85
86 BLI_addtail(elems, ce);
87 }
88 }
89}
90
91/* ***************************************** */
92/* Selection Tools */
93
95{
96 /* error checking */
97 if (gpl == nullptr) {
98 return false;
99 }
100
101 /* stop at the first one found */
102 for (const bGPDframe &gpf : gpl->frames) {
103 if (gpf.flag & GP_FRAME_SELECT) {
104 return true;
105 }
106 }
107
108 /* not found */
109 return false;
110}
111
112/* Helper function: select GP-frame based on SELECT_* mode. */
113static void gpencil_frame_select(bGPDframe *gpf, short select_mode)
114{
115 if (gpf == nullptr) {
116 return;
117 }
118
119 switch (select_mode) {
120 case SELECT_ADD:
121 gpf->flag |= GP_FRAME_SELECT;
122 break;
123 case SELECT_SUBTRACT:
124 gpf->flag &= ~GP_FRAME_SELECT;
125 break;
126 case SELECT_INVERT:
127 gpf->flag ^= GP_FRAME_SELECT;
128 break;
129 }
130}
131
132void ED_gpencil_select_frames(bGPDlayer *gpl, short select_mode)
133{
134 /* error checking */
135 if (gpl == nullptr) {
136 return;
137 }
138
139 /* handle according to mode */
140 for (bGPDframe &gpf : gpl->frames) {
141 gpencil_frame_select(&gpf, select_mode);
142 }
143}
144
146{
147 /* error checking */
148 if (gpl == nullptr) {
149 return;
150 }
151
152 /* now call the standard function */
153 ED_gpencil_select_frames(gpl, mode);
154}
155
156void ED_gpencil_select_frame(bGPDlayer *gpl, int selx, short select_mode)
157{
158 bGPDframe *gpf;
159
160 if (gpl == nullptr) {
161 return;
162 }
163
164 gpf = BKE_annotations_layer_frame_find(gpl, selx);
165
166 if (gpf) {
167 gpencil_frame_select(gpf, select_mode);
168 }
169}
170
171void ED_gpencil_layer_frames_select_box(bGPDlayer *gpl, float min, float max, short select_mode)
172{
173 if (gpl == nullptr) {
174 return;
175 }
176
177 /* only select those frames which are in bounds */
178 for (bGPDframe &gpf : gpl->frames) {
179 if (IN_RANGE(gpf.framenum, min, max)) {
180 gpencil_frame_select(&gpf, select_mode);
181 }
182 }
183}
184
186 bGPDlayer *gpl,
187 short tool,
188 short select_mode)
189{
190 if (gpl == nullptr) {
191 return;
192 }
193
194 /* only select frames which are within the region */
195 for (bGPDframe &gpf : gpl->frames) {
196 /* construct a dummy point coordinate to do this testing with */
197 float pt[2] = {0};
198
199 pt[0] = gpf.framenum;
200 pt[1] = ked->channel_y;
201
202 /* check the necessary regions */
203 if (tool == BEZT_OK_CHANNEL_LASSO) {
204 /* Lasso */
205 if (keyframe_region_lasso_test(static_cast<const KeyframeEdit_LassoData *>(ked->data), pt)) {
206 gpencil_frame_select(&gpf, select_mode);
207 }
208 }
209 else if (tool == BEZT_OK_CHANNEL_CIRCLE) {
210 /* Circle */
211 if (keyframe_region_circle_test(static_cast<const KeyframeEdit_CircleData *>(ked->data), pt))
212 {
213 gpencil_frame_select(&gpf, select_mode);
214 }
215 }
216 }
217}
218
220{
221 gpl->flag |= GP_LAYER_SELECT;
222
223 /* Update other layer status. */
224 if (BKE_annotations_layer_active_get(gpd) != gpl) {
227 }
228}
229
230/* ***************************************** */
231/* Frame Editing Tools */
232
234{
235 bool changed = false;
236
237 /* error checking */
238 if (gpl == nullptr) {
239 return false;
240 }
241
242 /* check for frames to delete */
243 for (bGPDframe &gpf : gpl->frames.items_mutable()) {
244 if (gpf.flag & GP_FRAME_SELECT) {
246 changed = true;
247 }
248 }
249
250 return changed;
251}
252
254{
255 /* error checking */
256 if (gpl == nullptr) {
257 return;
258 }
259
260 /* Duplicate selected frames. */
261 for (bGPDframe &gpf : gpl->frames.items_mutable()) {
262
263 /* duplicate this frame */
264 if (gpf.flag & GP_FRAME_SELECT) {
265 bGPDframe *gpfd;
266
267 /* duplicate frame, and deselect self */
268 gpfd = BKE_annotations_frame_duplicate(&gpf, true);
269 gpf.flag &= ~GP_FRAME_SELECT;
270
271 BLI_insertlinkafter(&gpl->frames, &gpf, gpfd);
272 }
273 }
274}
275
277{
278 if (gpl == nullptr) {
279 return;
280 }
281
282 for (bGPDframe &gpf : gpl->frames) {
283 if (gpf.flag & GP_FRAME_SELECT) {
284 gpf.key_type = type;
285 }
286 }
287}
288
289/* -------------------------------------- */
290/* Copy and Paste Tools:
291 * - The copy/paste buffer currently stores a set of GP_Layers, with temporary
292 * GP_Frames with the necessary strokes
293 * - Unless there is only one element in the buffer,
294 * names are also tested to check for compatibility.
295 * - All pasted frames are offset by the same amount.
296 * This is calculated as the difference in the times of the current frame and the
297 * 'first keyframe' (i.e. the earliest one in all channels).
298 * - The earliest frame is calculated per copy operation.
299 */
300
301/* globals for copy/paste data (like for other copy/paste buffers) */
302static ListBaseT<bGPDlayer> gpencil_anim_copybuf = {nullptr, nullptr};
303static int gpencil_anim_copy_firstframe = 999999999;
304static int gpencil_anim_copy_lastframe = -999999999;
306
316
318{
319 ListBaseT<bAnimListElem> anim_data = {nullptr, nullptr};
320 int filter;
321
322 Scene *scene = ac->scene;
323
324 /* clear buffer first */
326
327 /* filter data */
330 ac, &anim_data, eAnimFilter_Flags(filter), ac->data, eAnimCont_Types(ac->datatype));
331
332 for (bAnimListElem &ale : anim_data) {
333 /* This function only deals with grease pencil layer frames.
334 * This check is needed in the case of a call from the main dope-sheet. */
335 if (ale.type != ANIMTYPE_GPLAYER) {
336 continue;
337 }
338
339 ListBaseT<bGPDframe> copied_frames = {nullptr, nullptr};
340 bGPDlayer *gpl = static_cast<bGPDlayer *>(ale.data);
341
342 /* loop over frames, and copy only selected frames */
343 for (bGPDframe &gpf : gpl->frames) {
344 /* if frame is selected, make duplicate it and its strokes */
345 if (gpf.flag & GP_FRAME_SELECT) {
346 /* make a copy of this frame */
347 bGPDframe *new_frame = BKE_annotations_frame_duplicate(&gpf, true);
348 BLI_addtail(&copied_frames, new_frame);
349
350 /* extend extents for keyframes encountered */
353 }
354 }
355
356 /* create a new layer in buffer if there were keyframes here */
357 if (copied_frames.is_empty() == false) {
358 bGPDlayer *new_layer = MEM_new<bGPDlayer>("GPCopyPasteLayer");
360
361 /* move over copied frames */
362 BLI_movelisttolist(&new_layer->frames, &copied_frames);
363 BLI_assert(copied_frames.first_ == nullptr);
364
365 /* make a copy of the layer's name - for name-based matching later... */
366 STRNCPY_UTF8(new_layer->info, gpl->info);
367 }
368 }
369
370 /* in case 'relative' paste method is used */
372
373 /* clean up */
374 ANIM_animdata_freelist(&anim_data);
375
376 /* report success */
377 return !gpencil_anim_copybuf.is_empty();
378}
379
380bool ED_gpencil_anim_copybuf_paste(bAnimContext *ac, const short offset_mode)
381{
382 Scene *scene = ac->scene;
383 bool no_name = false;
384 int offset = 0;
385
386 /* check if buffer is empty */
387 if (gpencil_anim_copybuf.is_empty()) {
388 return false;
389 }
390
391 /* Check if single channel in buffer (disregard names if so). */
392 if (gpencil_anim_copybuf.first_ == gpencil_anim_copybuf.last()) {
393 no_name = true;
394 }
395
396 /* methods of offset (eKeyPasteOffset) */
397 switch (offset_mode) {
399 offset = (scene->r.cfra - gpencil_anim_copy_firstframe);
400 break;
402 offset = (scene->r.cfra - gpencil_anim_copy_lastframe);
403 break;
405 offset = (scene->r.cfra - gpencil_anim_copy_cfra);
406 break;
408 offset = 0;
409 break;
410 }
411
412 ListBaseT<bAnimListElem> anim_data = {nullptr, nullptr};
413 /* Only paste into selected layers. */
417 ac, &anim_data, eAnimFilter_Flags(filter), ac->data, eAnimCont_Types(ac->datatype));
418 if (anim_data.is_empty()) {
419 /* If no channels are selected at all, make even unselected layers "targets" for pasting. */
423 ac, &anim_data, eAnimFilter_Flags(filter), ac->data, eAnimCont_Types(ac->datatype));
424 }
425
426 for (bAnimListElem &ale : anim_data) {
427 /* Only deal with GPlayers (case of calls from general dope-sheet). */
428 if (ale.type != ANIMTYPE_GPLAYER) {
429 continue;
430 }
431
432 bGPDlayer *gpld = static_cast<bGPDlayer *>(ale.data);
433 bGPDlayer *gpls = nullptr;
434 bGPDframe *gpf;
435
436 /* find suitable layer from buffer to use to paste from */
437 for (gpls = gpencil_anim_copybuf.first(); gpls; gpls = gpls->next) {
438 /* check if layer name matches */
439 if ((no_name) || STREQ(gpls->info, gpld->info)) {
440 break;
441 }
442 }
443
444 /* this situation might occur! */
445 if (gpls == nullptr) {
446 continue;
447 }
448
449 /* add frames from buffer */
450 for (bGPDframe &gpfs : gpls->frames) {
451 /* temporarily apply offset to buffer-frame while copying */
452 gpfs.framenum += offset;
453
454 /* get frame to copy data into (if no frame returned, then just ignore) */
456 if (gpf) {
457 /* Ensure to use same keyframe type. */
458 gpf->key_type = gpfs.key_type;
459
460 /* This should be the right frame... as it may be a pre-existing frame,
461 * must make sure that only compatible stroke types get copied over
462 * - We cannot just add a duplicate frame, as that would cause errors
463 * - For now, we don't check if the types will be compatible since we
464 * don't have enough info to do so. Instead, we simply just paste,
465 * if it works, it will show up.
466 */
467 for (bGPDstroke &gps : gpfs.strokes) {
468 /* Make a copy of stroke, then a copy of its points array. */
469 bGPDstroke *gpsn = BKE_gpencil_stroke_duplicate(&gps, true, true);
470
471 /* append stroke to frame */
472 BLI_addtail(&gpf->strokes, gpsn);
473 }
474
475 /* if no strokes (i.e. new frame) added, free gpf */
476 if (gpf->strokes.is_empty()) {
478 }
479 }
480
481 /* unapply offset from buffer-frame */
482 gpfs.framenum -= offset;
483 }
484
485 /* Tag destination datablock. */
487 }
488
489 /* clean up */
490 ANIM_animdata_freelist(&anim_data);
491 return true;
492}
493
494/* -------------------------------------- */
495/* Snap Tools */
496
497static bool gpencil_frame_snap_nearest(bGPDframe * /*gpf*/, Scene * /*scene*/)
498{
499#if 0 /* NOTE: gpf->framenum is already an int! */
500 if (gpf->flag & GP_FRAME_SELECT) {
501 gpf->framenum = int(floor(gpf->framenum + 0.5));
502 }
503#endif
504 return false;
505}
506
508{
509 float secf = float(scene->frames_per_second());
510 if (gpf->flag & GP_FRAME_SELECT) {
511 gpf->framenum = int(floorf(gpf->framenum / secf + 0.5f) * secf);
512 }
513 return false;
514}
515
517{
518 if (gpf->flag & GP_FRAME_SELECT) {
519 gpf->framenum = scene->r.cfra;
520 }
521 return false;
522}
523
525{
526 if (gpf->flag & GP_FRAME_SELECT) {
528 }
529 return false;
530}
531
532void ED_gpencil_layer_snap_frames(bGPDlayer *gpl, Scene *scene, short mode)
533{
534 switch (mode) {
535 case SNAP_KEYS_NEARFRAME: /* snap to nearest frame */
537 break;
538 case SNAP_KEYS_CURFRAME: /* snap to current frame */
540 break;
541 case SNAP_KEYS_NEARMARKER: /* snap to nearest marker */
543 break;
544 case SNAP_KEYS_NEARSEC: /* snap to nearest second */
546 break;
547 default: /* just in case */
548 break;
549 }
550}
551
552/* -------------------------------------- */
553/* Mirror Tools */
554
556{
557 int diff;
558
559 if (gpf->flag & GP_FRAME_SELECT) {
560 diff = scene->r.cfra - gpf->framenum;
561 gpf->framenum = scene->r.cfra + diff;
562 }
563
564 return false;
565}
566
567static bool gpencil_frame_mirror_yaxis(bGPDframe *gpf, Scene * /*scene*/)
568{
569 int diff;
570
571 if (gpf->flag & GP_FRAME_SELECT) {
572 diff = -gpf->framenum;
573 gpf->framenum = diff;
574 }
575
576 return false;
577}
578
579static bool gpencil_frame_mirror_xaxis(bGPDframe *gpf, Scene * /*scene*/)
580{
581 int diff;
582
583 /* NOTE: since we can't really do this, we just do the same as for yaxis... */
584 if (gpf->flag & GP_FRAME_SELECT) {
585 diff = -gpf->framenum;
586 gpf->framenum = diff;
587 }
588
589 return false;
590}
591
593{
594 static TimeMarker *marker;
595 static short initialized = 0;
596 int diff;
597
598 /* In order for this mirror function to work without
599 * any extra arguments being added, we use the case
600 * of gpf==nullptr to denote that we should find the
601 * marker to mirror over. The static pointer is safe
602 * to use this way, as it will be set to null after
603 * each cycle in which this is called.
604 */
605
606 if (gpf != nullptr) {
607 /* mirroring time */
608 if ((gpf->flag & GP_FRAME_SELECT) && (marker)) {
609 diff = (marker->frame - gpf->framenum);
610 gpf->framenum = (marker->frame + diff);
611 }
612 }
613 else {
614 /* initialization time */
615 if (initialized) {
616 /* reset everything for safety */
617 marker = nullptr;
618 initialized = 0;
619 }
620 else {
621 /* try to find a marker */
622 marker = ED_markers_get_first_selected(&scene->markers);
623 if (marker) {
624 initialized = 1;
625 }
626 }
627 }
628
629 return false;
630}
631
632void ED_gpencil_layer_mirror_frames(bGPDlayer *gpl, Scene *scene, short mode)
633{
634 switch (mode) {
635 case MIRROR_KEYS_CURFRAME: /* mirror over current frame */
637 break;
638 case MIRROR_KEYS_YAXIS: /* mirror over frame 0 */
640 break;
641 case MIRROR_KEYS_XAXIS: /* mirror over value 0 */
643 break;
644 case MIRROR_KEYS_MARKER: /* mirror over marker */
645 gpencil_frame_mirror_marker(nullptr, scene);
647 gpencil_frame_mirror_marker(nullptr, scene);
648 break;
649 default: /* just in case */
651 break;
652 }
653}
654
655/* ***************************************** */
656
657} // namespace blender
#define STRNCPY_UTF8(dst, src)
Read Blender memory allocation and freeing.
#define ND_DATA
Definition WM_types.hh:552
#define NA_EDITED
Definition WM_types.hh:634
#define NC_GPENCIL
Definition WM_types.hh:412
#define IN_RANGE(a, b, c)
#define ELEM(...)
#define STREQ(a, b)
btScalar max
IMETHOD Vector diff(const Vector &a, const Vector &b, double dt)
Definition frames.inl:1166
#define floor
BMesh Iterators.
void WM_main_add_notifier(uint type, void *reference)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BKE_annotations_layer_active_set(struct bGPdata *gpd, struct bGPDlayer *active)
struct bGPDframe * BKE_annotations_layer_frame_get(struct bGPDlayer *gpl, int cframe, eGP_GetFrame_Mode addnew)
void ED_gpencil_layer_frames_select_region(KeyframeEditData *ked, bGPDlayer *gpl, short tool, short select_mode)
void ED_gpencil_layer_mirror_frames(bGPDlayer *gpl, Scene *scene, short mode)
static bool gpencil_frame_mirror_cframe(bGPDframe *gpf, Scene *scene)
void void void BLI_movelisttolist(ListBase *dst, ListBase *src) ATTR_NONNULL(1
Definition listbase.cc:32
@ ANIMTYPE_GPLAYER
@ KEYFRAME_PASTE_OFFSET_CFRA_START
@ KEYFRAME_PASTE_OFFSET_CFRA_RELATIVE
@ KEYFRAME_PASTE_OFFSET_CFRA_END
static bool gpencil_frame_mirror_xaxis(bGPDframe *gpf, Scene *)
struct bGPDframe * BKE_annotations_layer_frame_find(struct bGPDlayer *gpl, int cframe)
void ED_gpencil_layer_frame_select_set(bGPDlayer *gpl, short mode)
static int gpencil_anim_copy_firstframe
bool keyframe_region_lasso_test(const KeyframeEdit_LassoData *data_lasso, const float xy[2])
static bool gpencil_frame_snap_nearmarker(bGPDframe *gpf, Scene *scene)
static bool gpencil_frame_mirror_yaxis(bGPDframe *gpf, Scene *)
bool ED_gpencil_anim_copybuf_copy(bAnimContext *ac)
size_t ANIM_animdata_filter(bAnimContext *ac, ListBaseT< bAnimListElem > *anim_data, const eAnimFilter_Flags filter_mode, void *data, const eAnimCont_Types datatype)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
@ ANIMFILTER_FOREDIT
@ ANIMFILTER_LIST_VISIBLE
@ ANIMFILTER_DATA_VISIBLE
@ ANIMFILTER_NODUPLIS
void DEG_id_tag_update(ID *id, unsigned int flags)
void BLI_insertlinkafter(ListBase *listbase, void *vprevlink, void *vnewlink) ATTR_NONNULL(1)
Definition listbase.cc:339
void BKE_annotations_free_layers(ListBaseT< bGPDlayer > *list)
void ED_gpencil_layer_frames_select_box(bGPDlayer *gpl, float min, float max, short select_mode)
void ED_gpencil_select_frame(bGPDlayer *gpl, int selx, short select_mode)
bool ED_gpencil_layer_frame_select_check(const bGPDlayer *gpl)
static bool gpencil_frame_mirror_marker(bGPDframe *gpf, Scene *scene)
TimeMarker * ED_markers_get_first_selected(ListBaseT< TimeMarker > *markers)
void ED_gpencil_layer_frames_keytype_set(bGPDlayer *gpl, short type)
bool ED_gpencil_anim_copybuf_paste(bAnimContext *ac, const short offset_mode)
struct bGPDstroke * BKE_gpencil_stroke_duplicate(struct bGPDstroke *gps_src, bool dup_points, bool dup_curve)
bool ED_gpencil_layer_frames_delete(bGPDlayer *gpl)
struct bGPDlayer * BKE_annotations_layer_active_get(struct bGPdata *gpd)
static bool gpencil_frame_snap_nearest(bGPDframe *, Scene *)
static bool gpencil_frame_snap_cframe(bGPDframe *gpf, Scene *scene)
void ED_gpencil_layer_frames_duplicate(bGPDlayer *gpl)
void ED_gpencil_layer_make_cfra_list(bGPDlayer *gpl, ListBaseT< CfraElem > *elems, bool onlysel)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
bool keyframe_region_circle_test(const KeyframeEdit_CircleData *data_circle, const float xy[2])
int ED_markers_find_nearest_marker_time(ListBaseT< TimeMarker > *markers, float x)
void ED_gpencil_layer_snap_frames(bGPDlayer *gpl, Scene *scene, short mode)
void ANIM_animdata_freelist(ListBaseT< bAnimListElem > *anim_data)
Definition anim_deps.cc:472
@ ID_RECALC_TRANSFORM
Definition DNA_ID.h:1054
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
bool BKE_annotations_layer_frame_delete(struct bGPDlayer *gpl, struct bGPDframe *gpf)
void ED_gpencil_anim_copybuf_free()
static int gpencil_anim_copy_cfra
static ListBaseT< bGPDlayer > gpencil_anim_copybuf
static int gpencil_anim_copy_lastframe
void ED_gpencil_select_frames(bGPDlayer *gpl, short select_mode)
void ED_gpencil_set_active_channel(bGPdata *gpd, bGPDlayer *gpl)
static void gpencil_frame_select(bGPDframe *gpf, short select_mode)
bool ED_gpencil_layer_frames_looper(bGPDlayer *gpl, Scene *scene, bool(*gpf_cb)(bGPDframe *, Scene *))
static bool gpencil_frame_snap_nearestsec(bGPDframe *gpf, Scene *scene)
struct bGPDframe * BKE_annotations_frame_duplicate(const struct bGPDframe *gpf_src, bool dup_strokes)
#define floorf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
ListBaseT< TimeMarker > markers
struct RenderData r
eAnimCont_Types datatype
ListBaseT< bGPDstroke > strokes
ListBaseT< bGPDframe > frames