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