2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2016 Blender Foundation.
19 * All rights reserved.
21 * Contributor(s): Kevin Dietrich.
23 * ***** END GPL LICENSE BLOCK *****
26 /** \file blender/blenkernel/intern/cachefile.c
30 #include "DNA_anim_types.h"
31 #include "DNA_cachefile_types.h"
32 #include "DNA_constraint_types.h"
33 #include "DNA_object_types.h"
34 #include "DNA_scene_types.h"
36 #include "BLI_fileops.h"
37 #include "BLI_listbase.h"
38 #include "BLI_path_util.h"
39 #include "BLI_string.h"
40 #include "BLI_threads.h"
41 #include "BLI_utildefines.h"
43 #include "BKE_animsys.h"
44 #include "BKE_cachefile.h"
45 #include "BKE_global.h"
46 #include "BKE_library.h"
48 #include "BKE_modifier.h"
49 #include "BKE_scene.h"
52 # include "ABC_alembic.h"
57 void BKE_cachefiles_init(void)
62 void BKE_cachefiles_exit(void)
67 void *BKE_cachefile_add(Main *bmain, const char *name)
69 CacheFile *cache_file = BKE_libblock_alloc(bmain, ID_CF, name);
71 BKE_cachefile_init(cache_file);
76 void BKE_cachefile_init(CacheFile *cache_file)
78 cache_file->handle = NULL;
79 cache_file->filepath[0] = '\0';
80 cache_file->override_frame = false;
81 cache_file->frame = 0.0f;
82 cache_file->is_sequence = false;
83 cache_file->scale = 1.0f;
84 cache_file->handle_mutex = BLI_mutex_alloc();
85 BLI_listbase_clear(&cache_file->object_paths);
88 /** Free (or release) any data used by this cachefile (does not free the cachefile itself). */
89 void BKE_cachefile_free(CacheFile *cache_file)
91 BKE_animdata_free((ID *)cache_file, false);
94 ABC_free_handle(cache_file->handle);
97 if (cache_file->handle_mutex) {
98 BLI_mutex_free(cache_file->handle_mutex);
100 BLI_freelistN(&cache_file->object_paths);
103 CacheFile *BKE_cachefile_copy(Main *bmain, CacheFile *cache_file)
105 CacheFile *new_cache_file = BKE_libblock_copy(bmain, &cache_file->id);
106 new_cache_file->handle = NULL;
108 BLI_listbase_clear(&cache_file->object_paths);
110 BKE_id_copy_ensure_local(bmain, &cache_file->id, &new_cache_file->id);
112 return new_cache_file;
115 void BKE_cachefile_make_local(Main *bmain, CacheFile *cache_file, const bool lib_local)
117 BKE_id_make_local_generic(bmain, &cache_file->id, true, lib_local);
120 void BKE_cachefile_reload(const Main *bmain, CacheFile *cache_file)
122 char filepath[FILE_MAX];
124 BLI_strncpy(filepath, cache_file->filepath, sizeof(filepath));
125 BLI_path_abs(filepath, ID_BLEND_PATH(bmain, &cache_file->id));
128 if (cache_file->handle) {
129 ABC_free_handle(cache_file->handle);
132 cache_file->handle = ABC_create_handle(filepath, &cache_file->object_paths);
136 void BKE_cachefile_ensure_handle(const Main *bmain, CacheFile *cache_file)
138 BLI_spin_lock(&spin);
139 if (cache_file->handle_mutex == NULL) {
140 cache_file->handle_mutex = BLI_mutex_alloc();
142 BLI_spin_unlock(&spin);
144 BLI_mutex_lock(cache_file->handle_mutex);
146 if (cache_file->handle == NULL) {
147 BKE_cachefile_reload(bmain, cache_file);
150 BLI_mutex_unlock(cache_file->handle_mutex);
153 void BKE_cachefile_update_frame(Main *bmain, Scene *scene, const float ctime, const float fps)
155 CacheFile *cache_file;
156 char filename[FILE_MAX];
158 for (cache_file = bmain->cachefiles.first; cache_file; cache_file = cache_file->id.next) {
159 /* Execute drivers only, as animation has already been done. */
160 BKE_animsys_evaluate_animdata(scene, &cache_file->id, cache_file->adt, ctime, ADT_RECALC_DRIVERS);
162 if (!cache_file->is_sequence) {
166 const float time = BKE_cachefile_time_offset(cache_file, ctime, fps);
168 if (BKE_cachefile_filepath_get(bmain, cache_file, time, filename)) {
170 ABC_free_handle(cache_file->handle);
171 cache_file->handle = ABC_create_handle(filename, NULL);
177 bool BKE_cachefile_filepath_get(
178 const Main *bmain, const CacheFile *cache_file, float frame,
179 char r_filepath[FILE_MAX])
181 BLI_strncpy(r_filepath, cache_file->filepath, FILE_MAX);
182 BLI_path_abs(r_filepath, ID_BLEND_PATH(bmain, &cache_file->id));
187 if (cache_file->is_sequence && BLI_path_frame_get(r_filepath, &fframe, &frame_len)) {
189 BLI_path_frame_strip(r_filepath, true, ext);
190 BLI_path_frame(r_filepath, frame, frame_len);
191 BLI_ensure_extension(r_filepath, FILE_MAX, ext);
193 /* TODO(kevin): store sequence range? */
194 return BLI_exists(r_filepath);
200 float BKE_cachefile_time_offset(CacheFile *cache_file, const float time, const float fps)
202 const float frame = (cache_file->override_frame ? cache_file->frame : time);
203 return cache_file->is_sequence ? frame : frame / fps;
206 /* TODO(kevin): replace this with some depsgraph mechanism, or something similar. */
207 void BKE_cachefile_clean(Scene *scene, CacheFile *cache_file)
209 for (Base *base = scene->base.first; base; base = base->next) {
210 Object *ob = base->object;
212 ModifierData *md = modifiers_findByType(ob, eModifierType_MeshSequenceCache);
215 MeshSeqCacheModifierData *mcmd = (MeshSeqCacheModifierData *)md;
217 if (cache_file == mcmd->cache_file) {
219 if (mcmd->reader != NULL) {
220 CacheReader_free(mcmd->reader);
227 for (bConstraint *con = ob->constraints.first; con; con = con->next) {
228 if (con->type != CONSTRAINT_TYPE_TRANSFORM_CACHE) {
232 bTransformCacheConstraint *data = con->data;
234 if (cache_file == data->cache_file) {
236 if (data->reader != NULL) {
237 CacheReader_free(data->reader);