Welcome to mirror list, hosted at ThFree Co, Russian Federation.

animation.c « intern « sequencer « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 82dc5970a7fef895f229b2cfdeb653bc1791dc74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* SPDX-License-Identifier: GPL-2.0-or-later
 * Copyright 2022 Blender Foundation. All rights reserved. */

/** \file
 * \ingroup sequencer
 */

#include <string.h>

#include "DNA_anim_types.h"
#include "DNA_scene_types.h"
#include "DNA_sequence_types.h"

#include "BKE_animsys.h"
#include "BKE_fcurve.h"

#include "BLI_ghash.h"
#include "BLI_listbase.h"
#include "BLI_string.h"

#include "DEG_depsgraph.h"

#include "SEQ_animation.h"

static bool seq_animation_curves_exist(Scene *scene)
{
  if (scene->adt == NULL || scene->adt->action == NULL ||
      BLI_listbase_is_empty(&scene->adt->action->curves)) {
    return false;
  }
  return true;
}

/* r_prefix + [" + escaped_name + "] + \0 */
#define SEQ_RNAPATH_MAXSTR ((30 + 2 + (SEQ_NAME_MAXSTR * 2) + 2) + 1)

static size_t sequencer_rna_path_prefix(char str[SEQ_RNAPATH_MAXSTR], const char *name)
{
  char name_esc[SEQ_NAME_MAXSTR * 2];

  BLI_str_escape(name_esc, name, sizeof(name_esc));
  return BLI_snprintf_rlen(
      str, SEQ_RNAPATH_MAXSTR, "sequence_editor.sequences_all[\"%s\"]", name_esc);
}

GSet *SEQ_fcurves_by_strip_get(const Sequence *seq, ListBase *fcurve_base)
{
  char rna_path[SEQ_RNAPATH_MAXSTR];
  size_t rna_path_len = sequencer_rna_path_prefix(rna_path, seq->name + 2);

  /* Only allocate `fcurves` if it's needed as it's possible there is no animation for `seq`. */
  GSet *fcurves = NULL;
  LISTBASE_FOREACH (FCurve *, fcurve, fcurve_base) {
    if (STREQLEN(fcurve->rna_path, rna_path, rna_path_len)) {
      if (fcurves == NULL) {
        fcurves = BLI_gset_ptr_new(__func__);
      }
      BLI_gset_add(fcurves, fcurve);
    }
  }

  return fcurves;
}

#undef SEQ_RNAPATH_MAXSTR

void SEQ_offset_animdata(Scene *scene, Sequence *seq, int ofs)
{
  if (!seq_animation_curves_exist(scene) || ofs == 0) {
    return;
  }
  GSet *fcurves = SEQ_fcurves_by_strip_get(seq, &scene->adt->action->curves);
  if (fcurves == NULL) {
    return;
  }

  GSET_FOREACH_BEGIN (FCurve *, fcu, fcurves) {
    unsigned int i;
    if (fcu->bezt) {
      for (i = 0; i < fcu->totvert; i++) {
        BezTriple *bezt = &fcu->bezt[i];
        bezt->vec[0][0] += ofs;
        bezt->vec[1][0] += ofs;
        bezt->vec[2][0] += ofs;
      }
    }
    if (fcu->fpt) {
      for (i = 0; i < fcu->totvert; i++) {
        FPoint *fpt = &fcu->fpt[i];
        fpt->vec[0] += ofs;
      }
    }
  }
  GSET_FOREACH_END();
  BLI_gset_free(fcurves, NULL);

  DEG_id_tag_update(&scene->adt->action->id, ID_RECALC_ANIMATION);
}

void SEQ_free_animdata(Scene *scene, Sequence *seq)
{
  if (!seq_animation_curves_exist(scene)) {
    return;
  }
  GSet *fcurves = SEQ_fcurves_by_strip_get(seq, &scene->adt->action->curves);
  if (fcurves == NULL) {
    return;
  }

  GSET_FOREACH_BEGIN (FCurve *, fcu, fcurves) {
    BLI_remlink(&scene->adt->action->curves, fcu);
    BKE_fcurve_free(fcu);
  }
  GSET_FOREACH_END();
  BLI_gset_free(fcurves, NULL);
}

void SEQ_animation_backup_original(Scene *scene, ListBase *list)
{
  if (scene->adt == NULL || scene->adt->action == NULL ||
      BLI_listbase_is_empty(&scene->adt->action->curves)) {
    return;
  }

  BLI_movelisttolist(list, &scene->adt->action->curves);
}

void SEQ_animation_restore_original(Scene *scene, ListBase *list)
{
  if (scene->adt == NULL || scene->adt->action == NULL || BLI_listbase_is_empty(list)) {
    return;
  }

  BLI_movelisttolist(&scene->adt->action->curves, list);
}

void SEQ_animation_duplicate(Scene *scene, Sequence *seq, ListBase *list)
{
  if (BLI_listbase_is_empty(list)) {
    return;
  }
  GSet *fcurves = SEQ_fcurves_by_strip_get(seq, list);
  if (fcurves == NULL) {
    return;
  }

  GSET_FOREACH_BEGIN (FCurve *, fcu, fcurves) {
    FCurve *fcu_cpy = BKE_fcurve_copy(fcu);
    BLI_addtail(&scene->adt->action->curves, fcu_cpy);
  }
  GSET_FOREACH_END();
  BLI_gset_free(fcurves, NULL);
}