Blender V5.3
bake_items_paths.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10
11#include "BLI_fileops.hh"
12#include "BLI_path_utils.hh"
13#include "BLI_string.hh"
14#include "BLI_string_utils.hh"
15
16namespace blender::bke::bake {
17
18std::string frame_to_file_name(const SubFrame &frame)
19{
20 char file_name_c[FILE_MAX];
21 SNPRINTF(file_name_c, "%011.5f", double(frame));
22 BLI_string_replace_char(file_name_c, '.', '_');
23 return file_name_c;
24}
25
26std::optional<SubFrame> file_name_to_frame(const StringRef file_name)
27{
28 char modified_file_name[FILE_MAX];
29 file_name.copy_utf8_truncated(modified_file_name);
30 BLI_string_replace_char(modified_file_name, '_', '.');
31 try {
32 const SubFrame frame = std::stof(modified_file_name);
33 return frame;
34 }
35 catch (...) {
36 return std::nullopt;
37 }
38}
39
41{
42 if (!BLI_is_dir(meta_dir.c_str())) {
43 return {};
44 }
45
46 direntry *dir_entries = nullptr;
47 const int dir_entries_num = BLI_filelist_dir_contents(meta_dir.c_str(), &dir_entries);
48 BLI_SCOPED_DEFER([&]() { BLI_filelist_free(dir_entries, dir_entries_num); });
49
50 Vector<MetaFile> meta_files;
51 for (const int i : IndexRange(dir_entries_num)) {
52 const direntry &dir_entry = dir_entries[i];
53 const StringRefNull dir_entry_path = dir_entry.path;
54 if (!dir_entry_path.endswith(".json")) {
55 continue;
56 }
57 const std::optional<SubFrame> frame = file_name_to_frame(dir_entry.relname);
58 if (!frame) {
59 continue;
60 }
61 meta_files.append({*frame, dir_entry_path});
62 }
63
64 std::ranges::sort(meta_files,
65 [](const MetaFile &a, const MetaFile &b) { return a.frame < b.frame; });
66
67 return meta_files;
68}
69
71{
72 char meta_dir[FILE_MAX];
73 BLI_path_join(meta_dir, sizeof(meta_dir), root_dir.c_str(), "meta");
74 char blobs_dir[FILE_MAX];
75 BLI_path_join(blobs_dir, sizeof(blobs_dir), root_dir.c_str(), "blobs");
76
77 BakePath bake_path;
78 bake_path.meta_dir = meta_dir;
79 bake_path.blobs_dir = blobs_dir;
80 bake_path.bake_dir = root_dir;
81 return bake_path;
82}
83
84} // namespace blender::bke::bake
File and directory operations.
#define BLI_SCOPED_DEFER(function_to_defer)
#define FILE_MAX
#define BLI_path_join(...)
#define SNPRINTF(dst, format,...)
constexpr IndexRange()=default
constexpr const char * c_str() const
void copy_utf8_truncated(char *dst, int64_t dst_size) const
Definition string_ref.cc:28
constexpr bool endswith(StringRef suffix) const
std::string frame_to_file_name(const SubFrame &frame)
std::optional< SubFrame > file_name_to_frame(StringRef file_name)
Vector< MetaFile > find_sorted_meta_files(StringRefNull meta_dir)
unsigned int BLI_filelist_dir_contents(const char *dirname, struct direntry **r_filelist)
void BLI_string_replace_char(char *str, char src, char dst) ATTR_NONNULL(1)
bool BLI_is_dir(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:457
void BLI_filelist_free(struct direntry *filelist, unsigned int nrentries)
static BakePath from_single_root(StringRefNull root_dir)
std::optional< std::string > bake_dir