Blender V5.3
annotations.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 <cmath>
10#include <cstddef>
11#include <cstdlib>
12#include <cstring>
13#include <optional>
14
15#include "CLG_log.h"
16
17#include "MEM_guardedalloc.h"
18
19#include "BLI_listbase.hh"
20#include "BLI_math_matrix_c.hh"
21#include "BLI_math_vector_c.hh"
22#include "BLI_string_utf8.hh"
23#include "BLI_string_utils.hh"
24
25#include "BLT_translation.hh"
26
27/* Allow using deprecated functionality for .blend file I/O. */
28#define DNA_DEPRECATED_ALLOW
29#include "DNA_scene_types.h"
30
31#include "DNA_brush_types.h"
33#include "DNA_material_types.h"
34#include "DNA_meshdata_types.h"
35#include "DNA_userdef_types.h"
36
37#include "BKE_action.hh"
38#include "BKE_anim_data.hh"
39#include "BKE_annotations.h"
40#include "BKE_brush.hh"
41#include "BKE_collection.hh"
42#include "BKE_colortools.hh"
43#include "BKE_deform.hh"
44#include "BKE_icons.hh"
45#include "BKE_idtype.hh"
46#include "BKE_image.hh"
47#include "BKE_lib_id.hh"
48#include "BKE_lib_query.hh"
49#include "BKE_main.hh"
50#include "BKE_material.hh"
51#include "BKE_paint.hh"
52
53#include "DEG_depsgraph.hh"
54
55#include "BLI_math_color_c.hh"
56#include "BLI_string_utf8.hh"
57
58#include "BLO_read_write.hh"
59
61
62namespace blender {
63
64/* -------------------------------------------------------------------- */
67
68static CLG_LogRef LOG = {"geom.annotations"};
69
70static void greasepencil_copy_data(Main * /*bmain*/,
71 std::optional<Library *> /*owner_library*/,
72 ID *id_dst,
73 const ID *id_src,
74 const int /*flag*/)
75{
76 bGPdata *gpd_dst = id_cast<bGPdata *>(id_dst);
77 const bGPdata *gpd_src = id_cast<const bGPdata *>(id_src);
78
79 /* duplicate material array */
80 if (gpd_src->mat) {
81 gpd_dst->mat = MEM_dupalloc(gpd_src->mat);
82 }
83
85
86 /* copy layers */
87 gpd_dst->layers.clear_no_delete();
88 for (bGPDlayer &gpl_src : gpd_src->layers) {
89 /* make a copy of source layer and its data */
90
91 /* TODO: here too could add unused flags... */
92 bGPDlayer *gpl_dst = BKE_annotations_layer_duplicate(&gpl_src, true, true);
93
94 /* Apply local layer transform to all frames. Calc the active frame is not enough
95 * because onion skin can use more frames. This is more slow but required here. */
96 if (gpl_dst->actframe != nullptr) {
97 bool transformed = (!is_zero_v3(gpl_dst->location) || !is_zero_v3(gpl_dst->rotation) ||
98 !is_one_v3(gpl_dst->scale));
99 if (transformed) {
101 gpl_dst->layer_mat, gpl_dst->location, gpl_dst->rotation, gpl_dst->scale);
102 bool do_onion = ((gpl_dst->onion_flag & GP_LAYER_ONIONSKIN) != 0);
103 bGPDframe *init_gpf = do_onion ? gpl_dst->frames.first() : gpl_dst->actframe;
104 for (bGPDframe *gpf = init_gpf; gpf; gpf = gpf->next) {
105 for (bGPDstroke &gps : gpf->strokes) {
106 bGPDspoint *pt;
107 int i;
108 for (i = 0, pt = gps.points; i < gps.totpoints; i++, pt++) {
109 mul_m4_v3(gpl_dst->layer_mat, &pt->x);
110 }
111 }
112 /* if not onion, exit loop. */
113 if (!do_onion) {
114 break;
115 }
116 }
117 }
118 }
119
120 BLI_addtail(&gpd_dst->layers, gpl_dst);
121 }
122}
123
125{
126 /* Really not ideal, but for now will do... In theory custom behaviors like not freeing cache
127 * should be handled through specific API, and not be part of the generic one. */
128 BKE_annotations_free_data(id_cast<bGPdata *>(id), true);
129}
130
132{
133 bGPdata *annotations = id_cast<bGPdata *>(id);
134 /* materials */
135 for (int i = 0; i < annotations->totcol; i++) {
137 }
138
139 for (bGPDlayer &gplayer : annotations->layers) {
141 }
142}
143
144static void greasepencil_blend_write(BlendWriter *writer, ID *id, const void *id_address)
145{
146 bGPdata *gpd = id_cast<bGPdata *>(id);
147
148 /* Clean up, important in undo case to reduce false detection of changed data-blocks. */
149 /* XXX not sure why the whole run-time data is not cleared in reading code,
150 * for now mimicking it here. */
151 gpd->runtime.sbuffer = nullptr;
152 gpd->runtime.sbuffer_used = 0;
153 gpd->runtime.sbuffer_size = 0;
154
155 /* write gpd data block to file */
156 writer->write_id_struct(id_address, gpd);
157 BKE_id_blend_write(writer, &gpd->id);
158
160
161 writer->write_pointer_array(gpd->totcol, gpd->mat);
162
163 /* write grease-pencil layers to file */
164 writer->write_struct_list(&gpd->layers);
165 for (bGPDlayer &gpl : gpd->layers) {
166 /* Write mask list. */
167 writer->write_struct_list(&gpl.mask_layers);
168 /* write this layer's frames to file */
169 writer->write_struct_list(&gpl.frames);
170 for (bGPDframe &gpf : gpl.frames) {
171 /* write strokes */
172 writer->write_struct_list(&gpf.strokes);
173 for (bGPDstroke &gps : gpf.strokes) {
174 writer->write_struct_array(gps.totpoints, gps.points);
176 BKE_defvert_blend_write(writer, gps.totpoints, gps.dvert);
177 if (gps.editcurve != nullptr) {
178 bGPDcurve *gpc = gps.editcurve;
179 writer->write_struct(gpc);
181 }
182 }
183 }
184 }
185}
186
188{
189 /* We must firstly have some grease-pencil data to link! */
190 if (gpd == nullptr) {
191 return;
192 }
193
194 /* Ensure full object-mode for linked annotations. */
195 if (ID_IS_LINKED(gpd)) {
201 }
202
203 /* init stroke buffer */
204 gpd->runtime.sbuffer = nullptr;
205 gpd->runtime.sbuffer_used = 0;
206 gpd->runtime.sbuffer_size = 0;
207
208 /* Relink palettes (old palettes deprecated, only to convert old files). */
210 if (gpd->palettes.first() != nullptr) {
211 for (bGPDpalette &palette : gpd->palettes) {
212 BLO_read_struct_list(reader, PaletteColor, &palette.colors);
213 }
214 }
215
217
218 /* Materials. */
220
221 /* Relink layers. */
222 BLO_read_struct_list(reader, bGPDlayer, &gpd->layers);
223
224 for (bGPDlayer &gpl : gpd->layers) {
225 /* Relink frames. */
227
228 BLO_read_struct(reader, bGPDframe, &gpl.actframe);
229
230 gpl.runtime.icon_id = 0;
231
232 /* Relink masks. */
234
235 for (bGPDframe &gpf : gpl.frames) {
236 /* Relink strokes (and their points). */
238
239 for (bGPDstroke &gps : gpf.strokes) {
240 /* Relink stroke points array. */
242 /* Relink geometry. */
244
245 /* Relink stroke edit curve. */
246 BLO_read_struct(reader, bGPDcurve, &gps.editcurve);
247 if (gps.editcurve != nullptr) {
248 /* Relink curve point array. */
249 bGPDcurve *gpc = gps.editcurve;
251 reader, &gps.editcurve->curve_points, &gpc->tot_curve_points);
252 }
253
254 /* Relink weight data. */
255 if (gps.dvert && BLO_read_array(reader, &gps.dvert, gps.totpoints)) {
256 BKE_defvert_blend_read(reader, gps.totpoints, gps.dvert);
257 }
258 }
259 }
260 }
261}
262
264{
265 bGPdata *gpd = id_cast<bGPdata *>(id);
267}
268
270 .id_code = bGPdata::id_type,
271 .id_filter = FILTER_ID_GD_LEGACY,
272 .dependencies_id_types = FILTER_ID_MA,
273 .main_listbase_index = INDEX_ID_GD_LEGACY,
274 .struct_size = sizeof(bGPdata),
275 .name = "Annotation",
276 .name_plural = N_("annotations"),
277 .translation_context = BLT_I18NCONTEXT_ID_GPENCIL,
279 .asset_type_info = nullptr,
280
281 .init_data = nullptr,
282 .copy_data = greasepencil_copy_data,
283 .free_data = greasepencil_free_data,
284 .make_local = nullptr,
285 .foreach_id = greasepencil_foreach_id,
286 .foreach_cache = nullptr,
287 .foreach_path = nullptr,
288 .foreach_working_space_color = nullptr,
289 .foreach_asset_weak_reference = nullptr,
290 .owner_pointer_get = nullptr,
291
292 .blend_write = greasepencil_blend_write,
293 .blend_read_data = greasepencil_blend_read_data,
294 .blend_read_after_liblink = nullptr,
295
296 .blend_read_undo_preserve = nullptr,
297
298 .lib_override_apply_post = nullptr,
299};
300
301/* ************************************************** */
302/* Memory Management */
303
305{
306 if (dvert == nullptr) {
307 return;
308 }
309 MEM_SAFE_DELETE(dvert->dw);
310}
311
313{
314 if (gps == nullptr) {
315 return;
316 }
317
318 if (gps->dvert == nullptr) {
319 return;
320 }
321
322 for (int i = 0; i < gps->totpoints; i++) {
323 MDeformVert *dvert = &gps->dvert[i];
325 }
326}
327
329{
330 if (gps == nullptr) {
331 return;
332 }
333 bGPDcurve *editcurve = gps->editcurve;
334 if (editcurve == nullptr) {
335 return;
336 }
337 MEM_delete(editcurve->curve_points);
338 MEM_delete(editcurve);
339 gps->editcurve = nullptr;
340}
341
343{
344 if (gps == nullptr) {
345 return;
346 }
347 /* free stroke memory arrays, then stroke itself */
348 if (gps->points) {
349 MEM_delete(gps->points);
350 }
351 if (gps->dvert) {
353 MEM_delete(gps->dvert);
354 }
355 if (gps->triangles) {
356 MEM_delete(gps->triangles);
357 }
358 if (gps->editcurve != nullptr) {
360 }
361
362 MEM_delete(gps);
363}
364
366{
367 bool changed = (gpf->strokes.is_empty() == false);
368
369 /* free strokes */
370 for (bGPDstroke &gps : gpf->strokes.items_mutable()) {
372 }
373 gpf->strokes.clear_no_delete();
374
375 return changed;
376}
377
379{
380 bGPDframe *gpf_next;
381
382 /* error checking */
383 if (gpl == nullptr) {
384 return;
385 }
386
387 /* free frames */
388 for (bGPDframe *gpf = gpl->frames.first(); gpf; gpf = gpf_next) {
389 gpf_next = gpf->next;
390
391 /* free strokes and their associated memory */
393 BLI_freelinkN(&gpl->frames, gpf);
394 }
395 gpl->actframe = nullptr;
396}
397
399{
400 /* Free masks. */
401 bGPDlayer_Mask *mask_next = nullptr;
402 for (bGPDlayer_Mask *mask = gpl->mask_layers.first(); mask; mask = mask_next) {
403 mask_next = mask->next;
405 }
406}
407void BKE_annotations_free_layers(ListBaseT<bGPDlayer> *list)
408{
409 bGPDlayer *gpl_next;
410
411 /* error checking */
412 if (list == nullptr) {
413 return;
414 }
415
416 /* delete layers */
417 for (bGPDlayer *gpl = list->first(); gpl; gpl = gpl_next) {
418 gpl_next = gpl->next;
419
420 /* free layers and their data */
422
423 /* Free masks. */
425
426 BLI_freelinkN(list, gpl);
427 }
428}
429
431{
432 for (bGPDpalette &palette : list->items_mutable()) {
433 palette.colors.free_no_destruct();
434 MEM_delete(&palette);
435 }
436 list->clear_no_delete();
437}
438
439void BKE_annotations_free_data(bGPdata *gpd, bool /*free_all*/)
440{
441 /* free layers */
444
445 /* materials */
446 MEM_SAFE_DELETE(gpd->mat);
447
448 gpd->vertex_group_names.free_no_destruct();
449}
450
455
456/* ************************************************** */
457/* Container Creation */
458
460{
461 bGPDframe *gpf = nullptr, *gf = nullptr;
462 short state = 0;
463
464 /* error checking */
465 if (gpl == nullptr) {
466 return nullptr;
467 }
468
469 /* allocate memory for this frame */
470 gpf = MEM_new<bGPDframe>("bGPDframe");
471 gpf->framenum = cframe;
472
473 /* find appropriate place to add frame */
474 if (gpl->frames.first()) {
475 for (gf = gpl->frames.first(); gf; gf = gf->next) {
476 /* check if frame matches one that is supposed to be added */
477 if (gf->framenum == cframe) {
478 state = -1;
479 break;
480 }
481
482 /* if current frame has already exceeded the frame to add, add before */
483 if (gf->framenum > cframe) {
484 BLI_insertlinkbefore(&gpl->frames, gf, gpf);
485 state = 1;
486 break;
487 }
488 }
489 }
490
491 /* check whether frame was added successfully */
492 if (state == -1) {
494 &LOG, "Frame (%d) existed already for this layer_active. Using existing frame", cframe);
495
496 /* free the newly created one, and use the old one instead */
497 MEM_delete(gpf);
498
499 /* return existing frame instead... */
500 BLI_assert(gf != nullptr);
501 gpf = gf;
502 }
503 else if (state == 0) {
504 /* add to end then! */
505 BLI_addtail(&gpl->frames, gpf);
506 }
507
508 /* return frame */
509 return gpf;
510}
511
513{
514 bGPDframe *new_frame;
515 bool found = false;
516
517 /* Error checking/handling */
518 if (gpl == nullptr) {
519 /* no layer */
520 return nullptr;
521 }
522 if (gpl->actframe == nullptr) {
523 /* no active frame, so just create a new one from scratch */
524 return BKE_annotations_frame_addnew(gpl, cframe);
525 }
526
527 /* Create a copy of the frame */
528 new_frame = BKE_annotations_frame_duplicate(gpl->actframe, true);
529
530 /* Find frame to insert it before */
531 for (bGPDframe &gpf : gpl->frames) {
532 if (gpf.framenum > cframe) {
533 /* Add it here */
534 BLI_insertlinkbefore(&gpl->frames, &gpf, new_frame);
535
536 found = true;
537 break;
538 }
539 if (gpf.framenum == cframe) {
540 /* This only happens when we're editing with frame-lock on.
541 * - Delete the new frame and don't do anything else here.
542 */
544 MEM_delete(new_frame);
545 new_frame = nullptr;
546
547 found = true;
548 break;
549 }
550 }
551
552 if (found == false) {
553 /* Add new frame to the end */
554 BLI_addtail(&gpl->frames, new_frame);
555 }
556
557 /* Ensure that frame is set up correctly, and return it */
558 if (new_frame) {
559 new_frame->framenum = cframe;
560 gpl->actframe = new_frame;
561 }
562
563 return new_frame;
564}
565
567 const char *name,
568 const bool setactive,
569 const bool add_to_header)
570{
571 bGPDlayer *gpl = nullptr;
572 bGPDlayer *gpl_active = nullptr;
573
574 /* check that list is ok */
575 if (gpd == nullptr) {
576 return nullptr;
577 }
578
579 /* allocate memory for frame and add to end of list */
580 gpl = MEM_new<bGPDlayer>("bGPDlayer");
581
582 gpl_active = BKE_annotations_layer_active_get(gpd);
583
584 /* Add to data-block. */
585 if (add_to_header) {
586 BLI_addhead(&gpd->layers, gpl);
587 }
588 else {
589 if (gpl_active == nullptr) {
590 BLI_addtail(&gpd->layers, gpl);
591 }
592 else {
593 /* if active layer, add after that layer */
594 BLI_insertlinkafter(&gpd->layers, gpl_active, gpl);
595 }
596 }
597 /* annotation vs GP Object behavior is slightly different */
598 if (gpd->flag & GP_DATA_ANNOTATIONS) {
599 /* set default color of new strokes for this layer */
600 copy_v4_v4(gpl->color, U.gpencil_new_layer_col);
601 gpl->opacity = 1.0f;
602
603 /* set default thickness of new strokes for this layer */
604 gpl->thickness = 3;
605
606 /* Onion colors */
607 ARRAY_SET_ITEMS(gpl->gcolor_prev, 0.302f, 0.851f, 0.302f);
608 ARRAY_SET_ITEMS(gpl->gcolor_next, 0.250f, 0.1f, 1.0f);
609 }
610 else {
611 /* thickness parameter represents "thickness change", not absolute thickness */
612 gpl->thickness = 0;
613 gpl->opacity = 1.0f;
614 /* default channel color */
615 ARRAY_SET_ITEMS(gpl->color, 0.2f, 0.2f, 0.2f);
616 /* Default vertex mix. */
617 gpl->vertex_paint_opacity = 1.0f;
618 /* Enable onion skin. */
620 }
621
622 /* auto-name */
623 STRNCPY_UTF8(gpl->info, DATA_(name));
625 gpl,
626 (gpd->flag & GP_DATA_ANNOTATIONS) ? DATA_("Note") : DATA_("GP_Layer"),
627 '.',
628 offsetof(bGPDlayer, info),
629 sizeof(gpl->info));
630
631 /* Enable always affected by scene lights. */
633
634 /* Init transform. */
635 zero_v3(gpl->location);
636 zero_v3(gpl->rotation);
637 copy_v3_fl(gpl->scale, 1.0f);
638 loc_eul_size_to_mat4(gpl->layer_mat, gpl->location, gpl->rotation, gpl->scale);
640
641 /* make this one the active one */
642 if (setactive) {
644 }
645
646 /* return layer */
647 return gpl;
648}
649
650bGPdata *BKE_annotations_data_addnew(Main *bmain, const char name[])
651{
652 bGPdata *gpd;
653
654 /* allocate memory for a new block */
655 gpd = static_cast<bGPdata *>(BKE_libblock_alloc(bmain, ID_GD_LEGACY, name, 0));
656
657 /* initial settings */
659
660 /* general flags */
661 gpd->flag |= GP_DATA_VIEWALIGN;
662 /* always enable object onion skin switch */
664 /* GP object specific settings */
665 ARRAY_SET_ITEMS(gpd->line_color, 0.6f, 0.6f, 0.6f, 0.5f);
666
668
672
673 /* use adaptive curve resolution by default */
675
676 gpd->zdepth_offset = 0.150f;
677
678 /* grid settings */
679 ARRAY_SET_ITEMS(gpd->grid.color, 0.5f, 0.5f, 0.5f); /* Color */
680 ARRAY_SET_ITEMS(gpd->grid.scale, 1.0f, 1.0f); /* Scale */
681 gpd->grid.lines = GP_DEFAULT_GRID_LINES; /* Number of lines */
682
683 /* Onion-skinning settings (data-block level) */
684 gpd->onion_keytype = -1; /* All by default. */
688 gpd->onion_factor = 0.5f;
689 ARRAY_SET_ITEMS(gpd->gcolor_prev, 0.145098f, 0.419608f, 0.137255f); /* green */
690 ARRAY_SET_ITEMS(gpd->gcolor_next, 0.125490f, 0.082353f, 0.529412f); /* blue */
691 gpd->gstep = 1;
692 gpd->gstep_next = 1;
693
694 return gpd;
695}
696
697/* ************************************************** */
698
699/* ************************************************** */
700/* Data Duplication */
701
703{
704 if (gps_src == nullptr) {
705 return;
706 }
707 BLI_assert(gps_src->totpoints == gps_dst->totpoints);
708
709 BKE_defvert_array_copy(gps_dst->dvert, gps_src->dvert, gps_src->totpoints);
710}
711
713 const bool dup_points,
714 const bool /*dup_curve*/)
715{
716 bGPDstroke *gps_dst = nullptr;
717
718 gps_dst = MEM_dupalloc(gps_src);
719 gps_dst->prev = gps_dst->next = nullptr;
720 gps_dst->triangles = MEM_dupalloc(gps_src->triangles);
721
722 if (dup_points) {
723 gps_dst->points = MEM_dupalloc(gps_src->points);
724
725 if (gps_src->dvert != nullptr) {
726 gps_dst->dvert = MEM_dupalloc(gps_src->dvert);
727 BKE_gpencil_stroke_weights_duplicate(gps_src, gps_dst);
728 }
729 else {
730 gps_dst->dvert = nullptr;
731 }
732 }
733 else {
734 gps_dst->points = nullptr;
735 gps_dst->dvert = nullptr;
736 }
737
738 gps_dst->editcurve = nullptr;
739
740 /* return new stroke */
741 return gps_dst;
742}
743
744bGPDframe *BKE_annotations_frame_duplicate(const bGPDframe *gpf_src, const bool dup_strokes)
745{
746 bGPDstroke *gps_dst = nullptr;
747 bGPDframe *gpf_dst;
748
749 /* error checking */
750 if (gpf_src == nullptr) {
751 return nullptr;
752 }
753
754 /* make a copy of the source frame */
755 gpf_dst = MEM_dupalloc(gpf_src);
756 gpf_dst->prev = gpf_dst->next = nullptr;
757
758 /* Copy strokes. */
759 gpf_dst->strokes.clear_no_delete();
760 if (dup_strokes) {
761 for (bGPDstroke &gps_src : gpf_src->strokes) {
762 /* make copy of source stroke */
763 gps_dst = BKE_gpencil_stroke_duplicate(&gps_src, true, true);
764 BLI_addtail(&gpf_dst->strokes, gps_dst);
765 }
766 }
767
768 /* return new frame */
769 return gpf_dst;
770}
771
773 const bool dup_frames,
774 const bool dup_strokes)
775{
776 bGPDframe *gpf_dst;
777 bGPDlayer *gpl_dst;
778
779 /* error checking */
780 if (gpl_src == nullptr) {
781 return nullptr;
782 }
783
784 /* make a copy of source layer */
785 gpl_dst = MEM_dupalloc(gpl_src);
786 gpl_dst->prev = gpl_dst->next = nullptr;
787
788 /* copy frames */
789 gpl_dst->frames.clear_no_delete();
790 if (dup_frames) {
791 for (bGPDframe &gpf_src : gpl_src->frames) {
792 /* make a copy of source frame */
793 gpf_dst = BKE_annotations_frame_duplicate(&gpf_src, dup_strokes);
794 BLI_addtail(&gpl_dst->frames, gpf_dst);
795
796 /* if source frame was the current layer's 'active' frame, reassign that too */
797 if (&gpf_src == gpl_dst->actframe) {
798 gpl_dst->actframe = gpf_dst;
799 }
800 }
801 }
802
803 /* return new layer */
804 return gpl_dst;
805}
806
807bGPdata *BKE_annotations_data_duplicate(Main *bmain, const bGPdata *gpd_src, bool internal_copy)
808{
809 bGPdata *gpd_dst;
810
811 /* Yuck and super-uber-hyper yuck!!!
812 * Should be replaceable with a no-main copy (LIB_ID_COPY_NO_MAIN etc.), but not sure about it,
813 * so for now keep old code for that one. */
814
815 /* error checking */
816 if (gpd_src == nullptr) {
817 return nullptr;
818 }
819
820 if (internal_copy) {
821 /* make a straight copy for undo buffers used during stroke drawing */
822 gpd_dst = MEM_dupalloc(gpd_src);
823 }
824 else {
825 BLI_assert(bmain != nullptr);
826 gpd_dst = id_cast<bGPdata *>(BKE_id_copy(bmain, &gpd_src->id));
827 }
828
829 /* Copy internal data (layers, etc.) */
830 greasepencil_copy_data(bmain, std::nullopt, &gpd_dst->id, &gpd_src->id, 0);
831
832 /* return new */
833 return gpd_dst;
834}
835
836/* ************************************************** */
837/* GP Layer API */
838
840{
841 /* Sanity check */
842 if (gpl == nullptr) {
843 return false;
844 }
845
846 /* Layer must be: Visible + Editable */
847 if ((gpl->flag & (GP_LAYER_HIDE | GP_LAYER_LOCKED)) == 0) {
848 return true;
849 }
850
851 /* Something failed */
852 return false;
853}
854
856{
857 /* Search in reverse order, since this is often used for playback/adding,
858 * where it's less likely that we're interested in the earlier frames
859 */
860 for (bGPDframe &gpf : gpl->frames.items_reversed()) {
861 if (gpf.framenum == cframe) {
862 return &gpf;
863 }
864 }
865
866 return nullptr;
867}
868
870{
871 bGPDframe *gpf = nullptr;
872 bool found = false;
873
874 /* error checking */
875 if (gpl == nullptr) {
876 return nullptr;
877 }
878
879 /* check if there is already an active frame */
880 if (gpl->actframe) {
881 gpf = gpl->actframe;
882
883 /* do not allow any changes to layer's active frame if layer is locked from changes
884 * or if the layer has been set to stay on the current frame
885 */
886 if (gpl->flag & GP_LAYER_FRAMELOCK) {
887 return gpf;
888 }
889 /* do not allow any changes to actframe if frame has painting tag attached to it */
890 if (gpf->flag & GP_FRAME_PAINT) {
891 return gpf;
892 }
893
894 /* try to find matching frame */
895 if (gpf->framenum < cframe) {
896 for (; gpf; gpf = gpf->next) {
897 if (gpf->framenum == cframe) {
898 found = true;
899 break;
900 }
901 /* If this is the last frame or the next frame is at a later time, we found the right
902 * frame. */
903 if (!(gpf->next) || (gpf->next->framenum > cframe)) {
904 found = true;
905 break;
906 }
907 }
908
909 /* set the appropriate frame */
910 if (addnew) {
911 if ((found) && (gpf->framenum == cframe)) {
912 gpl->actframe = gpf;
913 }
914 else if (addnew == GP_GETFRAME_ADD_COPY) {
915 /* The #BKE_annotations_frame_addcopy function copies the active frame of gpl,
916 * so we need to set the active frame before copying. */
917 gpl->actframe = gpf;
918 gpl->actframe = BKE_annotations_frame_addcopy(gpl, cframe);
919 }
920 else {
921 gpl->actframe = BKE_annotations_frame_addnew(gpl, cframe);
922 }
923 }
924 else if (found) {
925 gpl->actframe = gpf;
926 }
927 else {
928 gpl->actframe = gpl->frames.last();
929 }
930 }
931 else {
932 for (; gpf; gpf = gpf->prev) {
933 if (gpf->framenum <= cframe) {
934 found = true;
935 break;
936 }
937 }
938
939 /* set the appropriate frame */
940 if (addnew) {
941 if ((found) && (gpf->framenum == cframe)) {
942 gpl->actframe = gpf;
943 }
944 else if (addnew == GP_GETFRAME_ADD_COPY) {
945 /* The #BKE_annotations_frame_addcopy function copies the active frame of gpl;
946 * so we need to set the active frame before copying. */
947 gpl->actframe = gpf;
948 gpl->actframe = BKE_annotations_frame_addcopy(gpl, cframe);
949 }
950 else {
951 gpl->actframe = BKE_annotations_frame_addnew(gpl, cframe);
952 }
953 }
954 else if (found) {
955 gpl->actframe = gpf;
956 }
957 else {
958 gpl->actframe = gpl->frames.first();
959 }
960 }
961 }
962 else if (gpl->frames.first()) {
963 /* check which of the ends to start checking from */
964 const int first = (gpl->frames.first())->framenum;
965 const int last = (gpl->frames.last())->framenum;
966
967 if (abs(cframe - first) > abs(cframe - last)) {
968 /* find gp-frame which is less than or equal to cframe */
969 for (gpf = gpl->frames.last(); gpf; gpf = gpf->prev) {
970 if (gpf->framenum <= cframe) {
971 found = true;
972 break;
973 }
974 }
975 }
976 else {
977 /* find gp-frame which is less than or equal to cframe */
978 for (gpf = gpl->frames.first(); gpf; gpf = gpf->next) {
979 if (gpf->framenum <= cframe) {
980 found = true;
981 break;
982 }
983 }
984 }
985
986 /* set the appropriate frame */
987 if (addnew) {
988 if ((found) && (gpf->framenum == cframe)) {
989 gpl->actframe = gpf;
990 }
991 else {
992 gpl->actframe = BKE_annotations_frame_addnew(gpl, cframe);
993 }
994 }
995 else if (found) {
996 gpl->actframe = gpf;
997 }
998 else {
999 /* If delete first frame, need to find one. */
1000 if (gpl->frames.first() != nullptr) {
1001 gpl->actframe = gpl->frames.first();
1002 }
1003 else {
1004 /* Unresolved erogenous situation! */
1005 CLOG_STR_ERROR(&LOG, "cannot find appropriate gp-frame");
1006 /* `gpl->actframe` should still be nullptr. */
1007 }
1008 }
1009 }
1010 else {
1011 /* currently no frames (add if allowed to) */
1012 if (addnew) {
1013 gpl->actframe = BKE_annotations_frame_addnew(gpl, cframe);
1014 }
1015 else {
1016 /* don't do anything... this may be when no frames yet! */
1017 /* gpl->actframe should still be nullptr */
1018 }
1019 }
1020
1021 /* Don't select first frame if greater than current frame. */
1022 if ((gpl->actframe != nullptr) && (gpl->actframe == gpl->frames.first()) &&
1023 (gpl->actframe->framenum > cframe))
1024 {
1025 gpl->actframe = nullptr;
1026 }
1027
1028 /* return */
1029 return gpl->actframe;
1030}
1031
1033{
1034 bool changed = false;
1035
1036 /* error checking */
1037 if (ELEM(nullptr, gpl, gpf)) {
1038 return false;
1039 }
1040
1041 /* if this frame was active, make the previous frame active instead
1042 * since it's tricky to set active frame otherwise
1043 */
1044 if (gpl->actframe == gpf) {
1045 gpl->actframe = gpf->prev;
1046 }
1047
1048 /* free the frame and its data */
1049 changed = BKE_annotations_free_strokes(gpf);
1050 BLI_freelinkN(&gpl->frames, gpf);
1051
1052 return changed;
1053}
1054
1056{
1057 if (name[0] == '\0') {
1058 return nullptr;
1059 }
1060 return static_cast<bGPDlayer *>(BLI_findstring(&gpd->layers, name, offsetof(bGPDlayer, info)));
1061}
1062
1063static int annotations_cb_cmp_frame(void *thunk, const void *a, const void *b)
1064{
1065 const bGPDframe *frame_a = static_cast<const bGPDframe *>(a);
1066 const bGPDframe *frame_b = static_cast<const bGPDframe *>(b);
1067
1068 if (frame_a->framenum < frame_b->framenum) {
1069 return -1;
1070 }
1071 if (frame_a->framenum > frame_b->framenum) {
1072 return 1;
1073 }
1074 if (thunk != nullptr) {
1075 *(static_cast<bool *>(thunk)) = true;
1076 }
1077 /* Sort selected last. */
1078 if ((frame_a->flag & GP_FRAME_SELECT) && ((frame_b->flag & GP_FRAME_SELECT) == 0)) {
1079 return 1;
1080 }
1081 return 0;
1082}
1083
1084void BKE_annotations_layer_frames_sort(bGPDlayer *gpl, bool *r_has_duplicate_frames)
1085{
1086 BLI_listbase_sort_r(&gpl->frames, annotations_cb_cmp_frame, r_has_duplicate_frames);
1087}
1088
1090{
1091 /* error checking */
1092 if (ELEM(nullptr, gpd, gpd->layers.first())) {
1093 return nullptr;
1094 }
1095
1096 /* loop over layers until found (assume only one active) */
1097 for (bGPDlayer &gpl : gpd->layers) {
1098 if (gpl.flag & GP_LAYER_ACTIVE) {
1099 return &gpl;
1100 }
1101 }
1102
1103 /* no active layer found */
1104 return nullptr;
1105}
1106
1108{
1109 /* error checking */
1110 if (ELEM(nullptr, gpd, gpd->layers.first(), active)) {
1111 return;
1112 }
1113
1114 /* loop over layers deactivating all */
1115 for (bGPDlayer &gpl : gpd->layers) {
1116 gpl.flag &= ~GP_LAYER_ACTIVE;
1117 if (gpd->flag & GP_DATA_AUTOLOCK_LAYERS) {
1118 gpl.flag |= GP_LAYER_LOCKED;
1119 }
1120 }
1121
1122 /* set as active one */
1123 active->flag |= GP_LAYER_ACTIVE;
1124 if (gpd->flag & GP_DATA_AUTOLOCK_LAYERS) {
1125 active->flag &= ~GP_LAYER_LOCKED;
1126 }
1127}
1128
1130{
1131 /* error checking */
1132 if (ELEM(nullptr, gpd, gpl)) {
1133 return;
1134 }
1135
1136 /* free layer */
1138
1139 /* Free Masks. */
1141
1142 /* free icon providing preview of icon color */
1144
1145 BLI_freelinkN(&gpd->layers, gpl);
1146}
1147
1149
1150} // namespace blender
Blender kernel action and pose functionality.
support for deformation groups and hooks.
#define BKE_LIB_FOREACHID_PROCESS_IDSUPER(data_, id_super_, cb_flag_)
General operations, lookup, etc. for materials.
#define STRNCPY_UTF8(dst, src)
#define BLO_read_struct_list(reader, struct_name, list)
#define BLO_read_struct(reader, struct_name, ptr_p)
#define BLT_I18NCONTEXT_ID_GPENCIL
#define DATA_(msgid)
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define CLOG_STR_ERROR(clg_ref, str)
Definition CLG_log.h:196
#define FILTER_ID_GD_LEGACY
Definition DNA_ID.h:1204
#define FILTER_ID_MA
Definition DNA_ID.h:1210
#define ID_IS_LINKED(_id)
Definition DNA_ID.h:697
#define GP_DEFAULT_CURVE_EDIT_CORNER_ANGLE
#define GP_DEFAULT_PIX_FACTOR
#define GP_DEFAULT_CURVE_RESOLUTION
#define GP_DEFAULT_GRID_LINES
#define GP_DEFAULT_CURVE_ERROR
Read Blender memory allocation and freeing.
#define ARRAY_SET_ITEMS(...)
#define ELEM(...)
#define offsetof(t, d)
#define abs
const int state
BMesh Iterators.
struct bGPDframe * BKE_annotations_frame_addnew(struct bGPDlayer *gpl, int cframe)
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BKE_annotations_layer_active_set(struct bGPdata *gpd, struct bGPDlayer *active)
static void greasepencil_blend_write(BlendWriter *writer, ID *id, const void *id_address)
struct bGPDframe * BKE_annotations_frame_addcopy(struct bGPDlayer *gpl, int cframe)
static void greasepencil_blend_read_data(BlendDataReader *reader, ID *id)
void BKE_defbase_blend_write(BlendWriter *writer, const ListBaseT< bDeformGroup > *defbase)
Definition deform.cc:1597
struct bGPDframe * BKE_annotations_layer_frame_get(struct bGPDlayer *gpl, int cframe, eGP_GetFrame_Mode addnew)
void BKE_defvert_array_copy(MDeformVert *dst, const MDeformVert *src, int totvert)
Definition deform.cc:1043
void BLI_addhead(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:98
struct bGPdata * BKE_annotations_data_duplicate(struct Main *bmain, const struct bGPdata *gpd, bool internal_copy)
void BLO_read_pointer_array_and_validate_size(BlendDataReader *reader, T **ptr_p, SizeT *array_size)
static void greasepencil_copy_data(Main *, std::optional< Library * >, ID *id_dst, const ID *id_src, const int)
void BKE_annotations_free_stroke_weights(struct bGPDstroke *gps)
struct bGPDlayer * BKE_annotations_layer_addnew(struct bGPdata *gpd, const char *name, bool setactive, bool add_to_header)
void BKE_defvert_blend_read(BlendDataReader *reader, int count, MDeformVert *mdverts)
Definition deform.cc:1621
void BKE_defvert_blend_write(BlendWriter *writer, int count, const MDeformVert *dvlist)
Definition deform.cc:1604
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
void * BLI_findstring(const ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:615
void BKE_annotations_free_frames(struct bGPDlayer *gpl)
void BKE_annotations_free_stroke(struct bGPDstroke *gps)
void BKE_id_blend_write(BlendWriter *writer, ID *id)
Definition lib_id.cc:2890
struct bGPdata * BKE_annotations_data_addnew(struct Main *bmain, const char name[])
IDTypeInfo IDType_ID_GD_LEGACY
struct bGPDframe * BKE_annotations_layer_frame_find(struct bGPDlayer *gpl, int cframe)
void BKE_annotations_layer_delete(struct bGPdata *gpd, struct bGPDlayer *gpl)
void mul_m4_v3(const float M[4][4], float r[3])
void BLI_uniquename(const ListBase *list, void *vlink, const char *defname, char delim, int name_offset, size_t name_maxncpy) ATTR_NONNULL(1
void void void BLI_listbase_sort_r(ListBase *listbase, int(*cmp)(void *, const void *, const void *), void *thunk) ATTR_NONNULL(1
Definition listbase.cc:328
void BKE_gpencil_stroke_weights_duplicate(struct bGPDstroke *gps_src, struct bGPDstroke *gps_dst)
void loc_eul_size_to_mat4(float R[4][4], const float loc[3], const float eul[3], const float size[3])
void DEG_id_tag_update(ID *id, unsigned int flags)
BMesh const char void * data
void BLI_insertlinkafter(ListBase *listbase, void *vprevlink, void *vnewlink) ATTR_NONNULL(1)
Definition listbase.cc:339
void BKE_annotations_free_layers(ListBaseT< bGPDlayer > *list)
struct bGPDlayer * BKE_annotations_layer_named_get(struct bGPdata *gpd, const char *name)
void BKE_annotations_blend_read_data(struct BlendDataReader *reader, struct bGPdata *gpd)
void BLI_freelinkN(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:277
MINLINE bool is_one_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
void BKE_annotations_free_legacy_palette_data(ListBaseT< bGPDpalette > *list)
struct bGPDstroke * BKE_gpencil_stroke_duplicate(struct bGPDstroke *gps_src, bool dup_points, bool dup_curve)
static int annotations_cb_cmp_frame(void *thunk, const void *a, const void *b)
struct bGPDlayer * BKE_annotations_layer_active_get(struct bGPdata *gpd)
MINLINE void zero_v3(float r[3])
void BLI_insertlinkbefore(ListBase *listbase, void *vnextlink, void *vnewlink) ATTR_NONNULL(1)
Definition listbase.cc:378
bool BLO_read_array(BlendDataReader *reader, T **ptr_p, const SizeT array_size, const int elems=1)
void BLO_read_array_and_validate_size(BlendDataReader *reader, T **ptr_p, SizeT *array_size, const int elems=1)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
void BKE_annotations_tag(struct bGPdata *gpd)
void BKE_annotations_free_data(struct bGPdata *gpd, bool free_all)
MINLINE void copy_v3_fl(float r[3], float f)
@ INDEX_ID_GD_LEGACY
Definition DNA_ID.h:1292
bool BKE_annotations_layer_is_editable(const struct bGPDlayer *gpl)
void BKE_annotations_free_layer_masks(struct bGPDlayer *gpl)
@ ID_RECALC_TRANSFORM
Definition DNA_ID.h:1054
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
void BKE_defgroup_copy_list(ListBaseT< bDeformGroup > *outbase, const ListBaseT< bDeformGroup > *inbase)
Definition deform.cc:75
bool BKE_annotations_layer_frame_delete(struct bGPDlayer *gpl, struct bGPDframe *gpf)
void BKE_annotations_free_point_weights(struct MDeformVert *dvert)
static void greasepencil_free_data(ID *id)
static CLG_LogRef LOG
Definition logging.cpp:12
static void greasepencil_foreach_id(ID *id, LibraryForeachIDData *data)
MINLINE void copy_v4_v4(float r[4], const float a[4])
void BKE_annotations_layer_frames_sort(struct bGPDlayer *gpl, bool *r_has_duplicate_frames)
bool BKE_annotations_free_strokes(struct bGPDframe *gpf)
void BKE_annotations_free_stroke_editcurve(struct bGPDstroke *gps)
bool BKE_icon_delete(int icon_id)
Definition icons.cc:545
ID * BKE_id_copy(Main *bmain, const ID *id)
Definition lib_id.cc:856
void * BKE_libblock_alloc(Main *bmain, short type, const char *name, int flag) ATTR_WARN_UNUSED_RESULT
Definition lib_id.cc:1545
MINLINE bool is_zero_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
struct bGPDlayer * BKE_annotations_layer_duplicate(const struct bGPDlayer *gpl_src, bool dup_frames, bool dup_strokes)
struct bGPDframe * BKE_annotations_frame_duplicate(const struct bGPDframe *gpf_src, bool dup_strokes)
@ IDTYPE_FLAGS_APPEND_IS_REUSABLE
Definition BKE_idtype.hh:50
void write_pointer_array(int64_t num, const void *data)
void write_id_struct(const void *id_address, const T *id, BlendStructWriterTypedFn< T > fn=nullptr)
void write_struct_list(const ListBaseT< T > *list, BlendStructWriterTypedFn< T > fn=nullptr)
void write_struct(const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
void write_struct_array(const int64_t array_size, const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
struct MDeformWeight * dw
ListBaseT< bGPDstroke > strokes
ListBaseT< bGPDframe > frames
eGPDlayer_OnionFlag onion_flag
ListBaseT< bGPDlayer_Mask > mask_layers
ListBaseT< bGPDpalettecolor > colors
ListBaseT< bGPDpalette > palettes
ListBaseT< bGPDlayer > layers
ListBaseT< bDeformGroup > vertex_group_names
#define N_(msgid)