Blender V5.3
cachefile.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2016 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstring>
10#include <optional>
11
12#include "DNA_cachefile_types.h"
13#include "DNA_object_types.h"
14#include "DNA_scene_types.h"
15
16#include "BLI_fileops.hh"
17#include "BLI_ghash.hh"
18#include "BLI_listbase.hh"
19#include "BLI_mutex.hh"
20#include "BLI_path_utils.hh"
21#include "BLI_string.hh"
22#include "BLI_utildefines.hh"
23
24#include "BLT_translation.hh"
25
26#include "BKE_bpath.hh"
27#include "BKE_cachefile.hh"
28#include "BKE_idtype.hh"
29#include "BKE_lib_id.hh"
30#include "BKE_library.hh"
31#include "BKE_main.hh"
32#include "BKE_scene.hh"
33
35
36#include "RE_engine.h"
37
38#include "BLO_read_write.hh"
39
40#include "MEM_guardedalloc.h"
41
42#ifdef WITH_ALEMBIC
43# include "ABC_alembic.h"
44#endif
45
46#ifdef WITH_USD
47# include "usd_api_modifier.hh"
48#endif
49
50namespace blender {
51
52static void cachefile_handle_free(CacheFile *cache_file);
53
54static void cache_file_init_data(ID *id)
55{
56 CacheFile *cache_file = id_cast<CacheFile *>(id);
57
58 cache_file->scale = 1.0f;
60 STRNCPY(cache_file->velocity_name, ".velocities");
61}
62
63static void cache_file_copy_data(Main * /*bmain*/,
64 std::optional<Library *> /*owner_library*/,
65 ID *id_dst,
66 const ID *id_src,
67 const int /*flag*/)
68{
69 CacheFile *cache_file_dst = id_cast<CacheFile *>(id_dst);
70 const CacheFile *cache_file_src = id_cast<const CacheFile *>(id_src);
71
72 cache_file_dst->handle = nullptr;
73 cache_file_dst->handle_readers = nullptr;
74 BLI_duplicatelist(&cache_file_dst->object_paths, &cache_file_src->object_paths);
75 BLI_duplicatelist(&cache_file_dst->layers, &cache_file_src->layers);
76}
77
78static void cache_file_free_data(ID *id)
79{
80 CacheFile *cache_file = id_cast<CacheFile *>(id);
81 cachefile_handle_free(cache_file);
82 cache_file->object_paths.free_no_destruct();
83 cache_file->layers.free_no_destruct();
84}
85
86static void cache_file_foreach_path(ID *id, BPathForeachPathData *bpath_data)
87{
88 CacheFile *cache_file = id_cast<CacheFile *>(id);
90 bpath_data, cache_file->filepath, sizeof(cache_file->filepath));
91}
92
93static void cache_file_blend_write(BlendWriter *writer, ID *id, const void *id_address)
94{
95 CacheFile *cache_file = id_cast<CacheFile *>(id);
96
97 /* Clean up, important in undo case to reduce false detection of changed datablocks. */
98 cache_file->object_paths.clear_no_delete();
99 cache_file->handle = nullptr;
100 memset(cache_file->handle_filepath, 0, sizeof(cache_file->handle_filepath));
101 cache_file->handle_readers = nullptr;
102
103 writer->write_id_struct(id_address, cache_file);
104 BKE_id_blend_write(writer, &cache_file->id);
105
106 /* write layers */
107 for (CacheFileLayer &layer : cache_file->layers) {
108 writer->write_struct(&layer);
109 }
110}
111
113{
114 CacheFile *cache_file = id_cast<CacheFile *>(id);
115 cache_file->object_paths.clear_no_delete();
116 cache_file->handle = nullptr;
117 cache_file->handle_filepath[0] = '\0';
118 cache_file->handle_readers = nullptr;
119
120 /* relink layers */
121 BLO_read_struct_list(reader, CacheFileLayer, &cache_file->layers);
122}
123
125 .id_code = CacheFile::id_type,
126 .id_filter = FILTER_ID_CF,
127 .dependencies_id_types = 0,
128 .main_listbase_index = INDEX_ID_CF,
129 .struct_size = sizeof(CacheFile),
130 .name = "CacheFile",
131 .name_plural = N_("cache_files"),
132 .translation_context = BLT_I18NCONTEXT_ID_CACHEFILE,
134 .asset_type_info = nullptr,
135
136 .init_data = cache_file_init_data,
137 .copy_data = cache_file_copy_data,
138 .free_data = cache_file_free_data,
139 .make_local = nullptr,
140 .foreach_id = nullptr,
141 .foreach_cache = nullptr,
142 .foreach_path = cache_file_foreach_path,
143 .foreach_working_space_color = nullptr,
144 .foreach_asset_weak_reference = nullptr,
145 .owner_pointer_get = nullptr,
146
147 .blend_write = cache_file_blend_write,
148 .blend_read_data = cache_file_blend_read_data,
149 .blend_read_after_liblink = nullptr,
150
151 .blend_read_undo_preserve = nullptr,
152
153 .lib_override_apply_post = nullptr,
154};
155
156#if defined(WITH_ALEMBIC) || defined(WITH_USD)
157/* TODO: make this per cache file to avoid global locks. */
158static Mutex cache_mutex;
159#endif
160
162 CacheReader **reader,
163 Object *object,
164 const char *object_path)
165{
166#if defined(WITH_ALEMBIC) || defined(WITH_USD)
167
168 BLI_assert(cache_file->id.tag & ID_TAG_COPIED_ON_EVAL);
169
170 if (cache_file->handle == nullptr) {
171 return;
172 }
173
174 switch (cache_file->type) {
176# ifdef WITH_ALEMBIC
177 /* Open Alembic cache reader. */
179 cache_file->handle, *reader, object, object_path, cache_file->is_sequence);
180# endif
181 break;
183# ifdef WITH_USD
184 /* Open USD cache reader. */
186 cache_file->handle, *reader, object, object_path);
187# endif
188 break;
190 break;
191 }
192
193 /* Multiple modifiers and constraints can call this function concurrently. */
194 std::lock_guard lock(cache_mutex);
195 if (*reader) {
196 /* Register in set so we can free it when the cache file changes. */
197 if (cache_file->handle_readers == nullptr) {
198 cache_file->handle_readers = MEM_new<CacheFileHandleReaderSet>("CacheFile.handle_readers");
199 }
200 cache_file->handle_readers->add(reader);
201 }
202 else if (cache_file->handle_readers) {
203 /* Remove in case CacheReader_open_alembic_object free the existing reader. */
204 cache_file->handle_readers->remove(reader);
205 }
206#else
207 UNUSED_VARS(cache_file, reader, object, object_path);
208#endif
209}
210
212{
213#if defined(WITH_ALEMBIC) || defined(WITH_USD)
214 /* Multiple modifiers and constraints can call this function concurrently, and
215 * cachefile_handle_free() can also be called at the same time. */
216 std::lock_guard lock(cache_mutex);
217 if (*reader != nullptr) {
218 if (cache_file) {
219 BLI_assert(cache_file->id.tag & ID_TAG_COPIED_ON_EVAL);
220
221 switch (cache_file->type) {
223# ifdef WITH_ALEMBIC
224 ABC_CacheReader_free(*reader);
225# endif
226 break;
228# ifdef WITH_USD
230# endif
231 break;
233 break;
234 }
235 }
236
237 *reader = nullptr;
238
239 if (cache_file && cache_file->handle_readers) {
240 cache_file->handle_readers->remove(reader);
241 }
242 }
243#else
244 UNUSED_VARS(cache_file, reader);
245#endif
246}
247
248static void cachefile_handle_free(CacheFile *cache_file)
249{
250#if defined(WITH_ALEMBIC) || defined(WITH_USD)
251
252 /* Free readers in all modifiers and constraints that use the handle, before
253 * we free the handle itself. */
254 {
255 std::lock_guard lock(cache_mutex);
256 if (cache_file->handle_readers) {
257 for (CacheReader **reader : *cache_file->handle_readers) {
258 if (*reader != nullptr) {
259 switch (cache_file->type) {
261# ifdef WITH_ALEMBIC
262 ABC_CacheReader_free(*reader);
263# endif
264 break;
266# ifdef WITH_USD
268# endif
269 break;
271 break;
272 }
273
274 *reader = nullptr;
275 }
276 }
277
278 MEM_delete(cache_file->handle_readers);
279 cache_file->handle_readers = nullptr;
280 }
281 }
282
283 /* Free handle. */
284 if (cache_file->handle) {
285
286 switch (cache_file->type) {
288# ifdef WITH_ALEMBIC
289 ABC_free_handle(cache_file->handle);
290# endif
291 break;
293# ifdef WITH_USD
294 io::usd::USD_free_handle(cache_file->handle);
295# endif
296 break;
298 break;
299 }
300
301 cache_file->handle = nullptr;
302 }
303
304 cache_file->handle_filepath[0] = '\0';
305#else
306 UNUSED_VARS(cache_file);
307#endif
308}
309
310void *BKE_cachefile_add(Main *bmain, const char *name)
311{
312 CacheFile *cache_file = BKE_id_new<CacheFile>(bmain, name);
313
314 return cache_file;
315}
316
317void BKE_cachefile_reload(Depsgraph *depsgraph, CacheFile *cache_file)
318{
319 /* To force reload, free the handle and tag depsgraph to load it again. */
320 CacheFile *cache_file_eval = DEG_get_evaluated(depsgraph, cache_file);
321 if (cache_file_eval) {
322 cachefile_handle_free(cache_file_eval);
323 }
324
326}
327
328void BKE_cachefile_eval(Main *bmain, Depsgraph *depsgraph, CacheFile *cache_file)
329{
330 BLI_assert(cache_file->id.tag & ID_TAG_COPIED_ON_EVAL);
331
332 /* Compute filepath. */
333 char filepath[FILE_MAX];
334 if (!BKE_cachefile_filepath_get(bmain, depsgraph, cache_file, filepath)) {
335 return;
336 }
337
338 /* Test if filepath change or if we can keep the existing handle. */
339 if (STREQ(cache_file->handle_filepath, filepath)) {
340 return;
341 }
342
343 cachefile_handle_free(cache_file);
344 cache_file->object_paths.free_no_destruct();
345
346#ifdef WITH_ALEMBIC
347 if (BLI_path_extension_check_glob(filepath, "*.abc")) {
348 cache_file->type = CACHEFILE_TYPE_ALEMBIC;
349 cache_file->handle = ABC_create_handle(
350 bmain, filepath, cache_file->layers.first(), &cache_file->object_paths);
351 STRNCPY(cache_file->handle_filepath, filepath);
352 }
353#endif
354#ifdef WITH_USD
355 if (BLI_path_extension_check_glob(filepath, "*.usd;*.usda;*.usdc;*.usdz")) {
356 cache_file->type = CACHEFILE_TYPE_USD;
357 cache_file->handle = io::usd::USD_create_handle(bmain, filepath, &cache_file->object_paths);
358 STRNCPY(cache_file->handle_filepath, filepath);
359 }
360#endif
361
362 if (DEG_is_active(depsgraph)) {
363 /* Flush object paths back to original data-block for UI. */
364 CacheFile *cache_file_orig = DEG_get_original(cache_file);
365 cache_file_orig->object_paths.free_no_destruct();
366 BLI_duplicatelist(&cache_file_orig->object_paths, &cache_file->object_paths);
367 }
368}
369
371 const Depsgraph *depsgraph,
372 const CacheFile *cache_file,
373 char r_filepath[FILE_MAX])
374{
375 BLI_strncpy(r_filepath, cache_file->filepath, FILE_MAX);
376 BLI_path_abs(r_filepath, ID_BLEND_PATH(bmain, &cache_file->id));
377
378 int fframe;
379 int frame_len;
380
381 if (cache_file->is_sequence && BLI_path_frame_get(r_filepath, &fframe, &frame_len)) {
382 Scene *scene = DEG_get_evaluated_scene(depsgraph);
383 const float ctime = BKE_scene_ctime_get(scene);
384 const double fps = double(scene->r.frs_sec) / double(scene->r.frs_sec_base);
385 const int frame = int(BKE_cachefile_time_offset(cache_file, double(ctime), fps));
386
387 char ext[32];
388 BLI_path_frame_strip(r_filepath, ext, sizeof(ext));
389 BLI_path_frame(r_filepath, FILE_MAX, frame, frame_len);
390 BLI_path_extension_ensure(r_filepath, FILE_MAX, ext);
391
392 /* TODO(kevin): store sequence range? */
393 return BLI_exists(r_filepath);
394 }
395
396 return true;
397}
398
399double BKE_cachefile_time_offset(const CacheFile *cache_file, const double time, const double fps)
400{
401 const double time_offset = double(cache_file->frame_offset) / fps;
402 const double frame = (cache_file->override_frame ? double(cache_file->frame) : time);
403 return cache_file->is_sequence ? frame : frame / fps - time_offset;
404}
405
406double BKE_cachefile_frame_offset(const CacheFile *cache_file, const double time)
407{
408 const double time_offset = double(cache_file->frame_offset);
409 const double frame = cache_file->override_frame ? double(cache_file->frame) : time;
410 return cache_file->is_sequence ? frame : frame - time_offset;
411}
412
413CacheFileLayer *BKE_cachefile_add_layer(CacheFile *cache_file, const char filepath[FILE_MAX])
414{
415 for (CacheFileLayer &layer : cache_file->layers) {
416 if (STREQ(layer.filepath, filepath)) {
417 return nullptr;
418 }
419 }
420
421 const int num_layers = cache_file->layers.count();
422
423 CacheFileLayer *layer = MEM_new<CacheFileLayer>("CacheFileLayer");
424 STRNCPY(layer->filepath, filepath);
425
426 BLI_addtail(&cache_file->layers, layer);
427
428 cache_file->active_layer = char(num_layers + 1);
429
430 return layer;
431}
432
434{
435 return static_cast<CacheFileLayer *>(
436 BLI_findlink(&cache_file->layers, cache_file->active_layer - 1));
437}
438
440{
441 cache_file->active_layer = 0;
442 BLI_remlink(&cache_file->layers, layer);
443 MEM_delete(layer);
444}
445
446} // namespace blender
File and directory operations.
#define FILE_MAX
#define BLO_read_struct_list(reader, struct_name, list)
#define BLT_I18NCONTEXT_ID_CACHEFILE
#define FILTER_ID_CF
Definition DNA_ID.h:1227
#define ID_BLEND_PATH(_bmain, _id)
Definition DNA_ID.h:688
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
volatile int lock
#define UNUSED_VARS(...)
#define STREQ(a, b)
bool add(const Key &key)
Definition BLI_set.hh:248
bool remove(const Key &key)
Definition BLI_set.hh:404
#define layer
CacheReader * CacheReader_open_usd_object(CacheArchiveHandle *handle, CacheReader *reader, Object *object, const char *object_path)
void USD_free_handle(CacheArchiveHandle *handle)
void USD_CacheReader_free(CacheReader *reader)
CacheArchiveHandle * USD_create_handle(Main *, const char *filepath, ListBaseT< CacheObjectPath > *object_paths)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
CacheFileLayer * BKE_cachefile_get_active_layer(CacheFile *cache_file)
Definition cachefile.cc:433
double BKE_cachefile_frame_offset(const CacheFile *cache_file, double time)
Definition cachefile.cc:406
static void cache_file_foreach_path(ID *id, BPathForeachPathData *bpath_data)
Definition cachefile.cc:86
static void cachefile_handle_free(CacheFile *cache_file)
Definition cachefile.cc:248
void * BKE_cachefile_add(Main *bmain, const char *name)
Definition cachefile.cc:310
char * STRNCPY(char(&dst)[N], const char *src)
bool BKE_cachefile_filepath_get(const Main *bmain, const Depsgraph *depsgraph, const CacheFile *cache_file, char r_filepath[1024])
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
bool DEG_is_active(const Depsgraph *depsgraph)
Definition depsgraph.cc:324
void void void void void void BLI_duplicatelist(ListBase *dst, const ListBase *src) ATTR_NONNULL(1
Definition listbase.cc:834
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:404
bool BLI_path_frame(char *path, size_t path_maxncpy, int frame, int digits) ATTR_NONNULL(1)
struct CacheArchiveHandle * ABC_create_handle(const struct Main *bmain, const char *filepath, const struct CacheFileLayer *layers, ListBaseT< CacheObjectPath > *object_paths)
void ABC_free_handle(struct CacheArchiveHandle *handle)
void ABC_CacheReader_free(struct CacheReader *reader)
bool BLI_path_frame_get(const char *path, int *r_frame, int *r_digits_len) ATTR_NONNULL(1
void BKE_id_blend_write(BlendWriter *writer, ID *id)
Definition lib_id.cc:2890
void BKE_cachefile_remove_layer(CacheFile *cache_file, CacheFileLayer *layer)
Definition cachefile.cc:439
static void cache_file_free_data(ID *id)
Definition cachefile.cc:78
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:541
bool BLI_path_extension_ensure(char *path, size_t path_maxncpy, const char *ext) ATTR_NONNULL(1
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
void DEG_id_tag_update(ID *id, unsigned int flags)
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
static void cache_file_init_data(ID *id)
Definition cachefile.cc:54
static void cache_file_copy_data(Main *, std::optional< Library * >, ID *id_dst, const ID *id_src, const int)
Definition cachefile.cc:63
@ CACHEFILE_VELOCITY_UNIT_SECOND
T * DEG_get_original(T *id)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
bool BKE_bpath_foreach_path_fixed_process(BPathForeachPathData *bpath_data, char *path, size_t path_maxncpy)
Definition bpath.cc:130
float BKE_scene_ctime_get(const Scene *scene)
Definition scene.cc:2581
@ INDEX_ID_CF
Definition DNA_ID.h:1319
double BKE_cachefile_time_offset(const CacheFile *cache_file, double time, double fps)
Definition cachefile.cc:399
std::mutex Mutex
Definition BLI_mutex.hh:51
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
struct CacheReader * CacheReader_open_alembic_object(struct CacheArchiveHandle *handle, struct CacheReader *reader, struct Object *object, const char *object_path, bool is_sequence)
@ ID_TAG_COPIED_ON_EVAL
Definition DNA_ID.h:1000
bool BLI_path_extension_check_glob(const char *path, const char *ext_fnmatch) ATTR_NONNULL(1
bool void BLI_path_frame_strip(char *path, char *r_ext, size_t ext_maxncpy) ATTR_NONNULL(1
IDTypeInfo IDType_ID_CF
Definition cachefile.cc:124
static void cache_file_blend_write(BlendWriter *writer, ID *id, const void *id_address)
Definition cachefile.cc:93
static void cache_file_blend_read_data(BlendDataReader *reader, ID *id)
Definition cachefile.cc:112
CacheFileLayer * BKE_cachefile_add_layer(CacheFile *cache_file, const char filepath[1024])
Scene * DEG_get_evaluated_scene(const Depsgraph *graph)
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1612
void BKE_cachefile_reader_free(CacheFile *cache_file, CacheReader **reader)
Definition cachefile.cc:211
void BKE_cachefile_eval(Main *bmain, Depsgraph *depsgraph, CacheFile *cache_file)
Definition cachefile.cc:328
void BKE_cachefile_reader_open(CacheFile *cache_file, CacheReader **reader, Object *object, const char *object_path)
Definition cachefile.cc:161
void BKE_cachefile_reload(Depsgraph *depsgraph, CacheFile *cache_file)
Definition cachefile.cc:317
@ IDTYPE_FLAGS_APPEND_IS_REUSABLE
Definition BKE_idtype.hh:50
void write_id_struct(const void *id_address, const T *id, BlendStructWriterTypedFn< T > fn=nullptr)
void write_struct(const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
CacheFileHandleReaderSet * handle_readers
struct CacheArchiveHandle * handle
ListBaseT< CacheObjectPath > object_paths
ListBaseT< CacheFileLayer > layers
eCacheFile_VelocityUnit velocity_unit
struct RenderData r
#define N_(msgid)