Blender  V2.93
deg_eval_runtime_backup_animation.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2019 Blender Foundation.
17  * All rights reserved.
18  */
19 
25 
26 #include "DNA_anim_types.h"
27 
28 #include "BKE_animsys.h"
29 
30 #include "RNA_access.h"
31 #include "RNA_types.h"
32 
33 #include "intern/depsgraph.h"
34 
35 namespace blender::deg {
36 
37 namespace {
38 
39 struct AnimatedPropertyStoreCalbackData {
40  AnimationBackup *backup;
41 
42  /* ID which needs to be stored.
43  * Is used to check possibly nested IDs which f-curves are pointing to. */
44  ID *id;
45 
47 };
48 
49 void animated_property_store_cb(ID *id, FCurve *fcurve, void *data_v)
50 {
51  AnimatedPropertyStoreCalbackData *data = reinterpret_cast<AnimatedPropertyStoreCalbackData *>(
52  data_v);
53  if (fcurve->rna_path == nullptr || fcurve->rna_path[0] == '\0') {
54  return;
55  }
56  if (id != data->id) {
57  return;
58  }
59 
60  /* Resolve path to the property. */
61  PathResolvedRNA resolved_rna;
63  &data->id_pointer_rna, fcurve->rna_path, fcurve->array_index, &resolved_rna)) {
64  return;
65  }
66 
67  /* Read property value. */
68  float value;
69  if (!BKE_animsys_read_from_rna_path(&resolved_rna, &value)) {
70  return;
71  }
72 
73  data->backup->values_backup.append({fcurve->rna_path, fcurve->array_index, value});
74 }
75 
76 } // namespace
77 
78 AnimationValueBackup::AnimationValueBackup(const string &rna_path, int array_index, float value)
79  : rna_path(rna_path), array_index(array_index), value(value)
80 {
81 }
82 
84 {
85  meed_value_backup = !depsgraph->is_active;
86  reset();
87 }
88 
90 {
91 }
92 
94 {
95  /* NOTE: This animation backup nicely preserves values which are animated and
96  * are not touched by frame/depsgraph post_update handler.
97  *
98  * But it makes it impossible to have user edits to animated properties: for
99  * example, translation of object with animated location will not work with
100  * the current version of backup. */
101  return;
102 
103  AnimatedPropertyStoreCalbackData data;
104  data.backup = this;
105  data.id = id;
106  RNA_id_pointer_create(id, &data.id_pointer_rna);
107  BKE_fcurves_id_cb(id, animated_property_store_cb, &data);
108 }
109 
111 {
112  return;
113 
116  for (const AnimationValueBackup &value_backup : values_backup) {
117  /* Resolve path to the property.
118  *
119  * NOTE: Do it again (after storing), since the sub-data pointers might be
120  * changed after copy-on-write. */
121  PathResolvedRNA resolved_rna;
123  value_backup.rna_path.c_str(),
124  value_backup.array_index,
125  &resolved_rna)) {
126  return;
127  }
128 
129  /* Write property value. */
130  if (!BKE_animsys_write_to_rna_path(&resolved_rna, value_backup.value)) {
131  return;
132  }
133  }
134 }
135 
136 } // namespace blender::deg
bool BKE_animsys_write_to_rna_path(struct PathResolvedRNA *anim_rna, const float value)
Definition: anim_sys.c:501
void BKE_fcurves_id_cb(struct ID *id, ID_FCurve_Edit_Callback func, void *user_data)
Definition: anim_data.c:1278
bool BKE_animsys_rna_path_resolve(struct PointerRNA *ptr, const char *rna_path, const int array_index, struct PathResolvedRNA *r_result)
Definition: anim_sys.c:389
bool BKE_animsys_read_from_rna_path(struct PathResolvedRNA *anim_rna, float *r_value)
Definition: anim_sys.c:438
Vector< AnimationValueBackup > values_backup
const Depsgraph * depsgraph
AnimationBackup * backup
PointerRNA id_pointer_rna
void RNA_id_pointer_create(ID *id, PointerRNA *r_ptr)
Definition: rna_access.c:122
char * rna_path
int array_index
Definition: DNA_ID.h:273