Blender V4.3
animation.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9#include <cstring>
10
11#include "DNA_anim_types.h"
12#include "DNA_scene_types.h"
13#include "DNA_sequence_types.h"
14
15#include "BKE_fcurve.hh"
16
17#include "BLI_ghash.h"
18#include "BLI_listbase.h"
19#include "BLI_string.h"
20
21#include "DEG_depsgraph.hh"
22
23#include "SEQ_animation.hh"
24
26{
27 return scene->adt != nullptr && scene->adt->action != nullptr &&
28 !BLI_listbase_is_empty(&scene->adt->action->curves);
29}
30
32{
33 return scene->adt != nullptr && !BLI_listbase_is_empty(&scene->adt->drivers);
34}
35
36/* r_prefix + [" + escaped_name + "] + \0 */
37#define SEQ_RNAPATH_MAXSTR ((30 + 2 + (SEQ_NAME_MAXSTR * 2) + 2) + 1)
38
39static size_t sequencer_rna_path_prefix(char str[SEQ_RNAPATH_MAXSTR], const char *name)
40{
41 char name_esc[SEQ_NAME_MAXSTR * 2];
42
43 BLI_str_escape(name_esc, name, sizeof(name_esc));
44 return BLI_snprintf_rlen(
45 str, SEQ_RNAPATH_MAXSTR, "sequence_editor.sequences_all[\"%s\"]", name_esc);
46}
47
49{
50 char rna_path[SEQ_RNAPATH_MAXSTR];
51 size_t rna_path_len = sequencer_rna_path_prefix(rna_path, seq->name + 2);
52
53 /* Only allocate `fcurves` if it's needed as it's possible there is no animation for `seq`. */
54 GSet *fcurves = nullptr;
55 LISTBASE_FOREACH (FCurve *, fcurve, fcurve_base) {
56 if (STREQLEN(fcurve->rna_path, rna_path, rna_path_len)) {
57 if (fcurves == nullptr) {
58 fcurves = BLI_gset_ptr_new(__func__);
59 }
60 BLI_gset_add(fcurves, fcurve);
61 }
62 }
63
64 return fcurves;
65}
66
67#undef SEQ_RNAPATH_MAXSTR
68
69void SEQ_offset_animdata(Scene *scene, Sequence *seq, int ofs)
70{
71 if (!SEQ_animation_curves_exist(scene) || ofs == 0) {
72 return;
73 }
74 GSet *fcurves = SEQ_fcurves_by_strip_get(seq, &scene->adt->action->curves);
75 if (fcurves == nullptr) {
76 return;
77 }
78
79 GSET_FOREACH_BEGIN (FCurve *, fcu, fcurves) {
80 uint i;
81 if (fcu->bezt) {
82 for (i = 0; i < fcu->totvert; i++) {
83 BezTriple *bezt = &fcu->bezt[i];
84 bezt->vec[0][0] += ofs;
85 bezt->vec[1][0] += ofs;
86 bezt->vec[2][0] += ofs;
87 }
88 }
89 if (fcu->fpt) {
90 for (i = 0; i < fcu->totvert; i++) {
91 FPoint *fpt = &fcu->fpt[i];
92 fpt->vec[0] += ofs;
93 }
94 }
95 }
97 BLI_gset_free(fcurves, nullptr);
98
99 DEG_id_tag_update(&scene->adt->action->id, ID_RECALC_ANIMATION);
100}
101
103{
104 if (!SEQ_animation_curves_exist(scene)) {
105 return;
106 }
107 GSet *fcurves = SEQ_fcurves_by_strip_get(seq, &scene->adt->action->curves);
108 if (fcurves == nullptr) {
109 return;
110 }
111
112 GSET_FOREACH_BEGIN (FCurve *, fcu, fcurves) {
113 BLI_remlink(&scene->adt->action->curves, fcu);
114 BKE_fcurve_free(fcu);
115 }
117 BLI_gset_free(fcurves, nullptr);
118}
119
121{
122 if (SEQ_animation_curves_exist(scene)) {
123 BLI_movelisttolist(&backup->curves, &scene->adt->action->curves);
124 }
125 if (SEQ_animation_drivers_exist(scene)) {
126 BLI_movelisttolist(&backup->drivers, &scene->adt->drivers);
127 }
128}
129
131{
132 if (!BLI_listbase_is_empty(&backup->curves)) {
133 BLI_movelisttolist(&scene->adt->action->curves, &backup->curves);
134 }
135 if (!BLI_listbase_is_empty(&backup->drivers)) {
136 BLI_movelisttolist(&scene->adt->drivers, &backup->drivers);
137 }
138}
139
140static void seq_animation_duplicate(Scene *scene, Sequence *seq, ListBase *dst, ListBase *src)
141{
142 if (seq->type == SEQ_TYPE_META) {
143 LISTBASE_FOREACH (Sequence *, meta_child, &seq->seqbase) {
144 seq_animation_duplicate(scene, meta_child, dst, src);
145 }
146 }
147
148 GSet *fcurves = SEQ_fcurves_by_strip_get(seq, src);
149 if (fcurves == nullptr) {
150 return;
151 }
152
153 GSET_FOREACH_BEGIN (const FCurve *, fcu, fcurves) {
154 FCurve *fcu_cpy = BKE_fcurve_copy(fcu);
155 BLI_addtail(dst, fcu_cpy);
156 }
158 BLI_gset_free(fcurves, nullptr);
159}
160
162 Sequence *seq,
163 SeqAnimationBackup *backup)
164{
165 if (!BLI_listbase_is_empty(&backup->curves)) {
166 seq_animation_duplicate(scene, seq, &scene->adt->action->curves, &backup->curves);
167 }
168 if (!BLI_listbase_is_empty(&backup->drivers)) {
169 seq_animation_duplicate(scene, seq, &scene->adt->drivers, &backup->drivers);
170 }
171}
FCurve * BKE_fcurve_copy(const FCurve *fcu)
void BKE_fcurve_free(FCurve *fcu)
struct GSet GSet
Definition BLI_ghash.h:341
#define GSET_FOREACH_END()
Definition BLI_ghash.h:541
GSet * BLI_gset_ptr_new(const char *info)
#define GSET_FOREACH_BEGIN(type, var, what)
Definition BLI_ghash.h:535
void BLI_gset_free(GSet *gs, GSetKeyFreeFP keyfreefp)
Definition BLI_ghash.c:1034
bool BLI_gset_add(GSet *gs, void *key)
Definition BLI_ghash.c:966
BLI_INLINE bool BLI_listbase_is_empty(const struct ListBase *lb)
#define LISTBASE_FOREACH(type, var, list)
void void void BLI_movelisttolist(struct ListBase *dst, struct ListBase *src) ATTR_NONNULL(1
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:110
void BLI_remlink(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:130
size_t BLI_snprintf_rlen(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
size_t BLI_str_escape(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
unsigned int uint
#define STREQLEN(a, b, n)
void DEG_id_tag_update(ID *id, unsigned int flags)
@ ID_RECALC_ANIMATION
Definition DNA_ID.h:1044
@ SEQ_TYPE_META
#define SEQ_NAME_MAXSTR
void SEQ_animation_backup_original(Scene *scene, SeqAnimationBackup *backup)
Definition animation.cc:120
#define SEQ_RNAPATH_MAXSTR
Definition animation.cc:37
bool SEQ_animation_drivers_exist(Scene *scene)
Definition animation.cc:31
bool SEQ_animation_curves_exist(Scene *scene)
Definition animation.cc:25
static size_t sequencer_rna_path_prefix(char str[SEQ_RNAPATH_MAXSTR], const char *name)
Definition animation.cc:39
GSet * SEQ_fcurves_by_strip_get(const Sequence *seq, ListBase *fcurve_base)
Definition animation.cc:48
void SEQ_offset_animdata(Scene *scene, Sequence *seq, int ofs)
Definition animation.cc:69
void SEQ_free_animdata(Scene *scene, Sequence *seq)
Definition animation.cc:102
static void seq_animation_duplicate(Scene *scene, Sequence *seq, ListBase *dst, ListBase *src)
Definition animation.cc:140
void SEQ_animation_duplicate_backup_to_scene(Scene *scene, Sequence *seq, SeqAnimationBackup *backup)
Definition animation.cc:161
void SEQ_animation_restore_original(Scene *scene, SeqAnimationBackup *backup)
Definition animation.cc:130
#define str(s)
float vec[3][3]
float vec[2]