Blender V5.3
bake_geometry_nodes_modifier_pack.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
11#include "BKE_library.hh"
12#include "BKE_main.hh"
13#include "BKE_packedFile.hh"
14#include "BKE_path_templates.hh"
15#include "BKE_report.hh"
16
17#include "DNA_modifier_types.h"
18#include "DNA_object_types.h"
19
20#include "MOD_nodes.hh"
21
22#include "BLI_fileops.hh"
23#include "BLI_path_utils.hh"
24#include "BLI_string.hh"
25
26#include "DEG_depsgraph.hh"
27
28namespace blender::bke::bake {
29
31 ReportList *reports)
32{
33 if (!BLI_is_dir(directory.c_str())) {
34 BKE_reportf(reports, RPT_ERROR, "%s is not a directory", directory.c_str());
35 return {};
36 }
37
38 direntry *dir_entries = nullptr;
39 const int dir_entries_num = BLI_filelist_dir_contents(directory.c_str(), &dir_entries);
40 BLI_SCOPED_DEFER([&]() { BLI_filelist_free(dir_entries, dir_entries_num); });
41
43 for (const int i : IndexRange(dir_entries_num)) {
44 const direntry &dir_entry = dir_entries[i];
45 const StringRefNull dir_entry_path = dir_entry.path;
46 const StringRefNull name = dir_entry.relname;
47 if (FILENAME_IS_CURRPAR(name.c_str())) {
48 continue;
49 }
50 NodesModifierBakeFile bake_file;
51 bake_file.name = BLI_strdup_null(name.c_str());
52 bake_file.packed_file = BKE_packedfile_new(reports, dir_entry_path.c_str(), "");
53 if (bake_file.packed_file) {
54 bake_files.append(bake_file);
55 }
56 }
57
58 return bake_files;
59}
60
62{
64 bake_path.meta_dir, reports);
65 if (meta_bake_files.is_empty()) {
66 return nullptr;
67 }
68
70 bake_path.blobs_dir, reports);
71
72 NodesModifierPackedBake *packed_bake = MEM_new<NodesModifierPackedBake>(__func__);
73 packed_bake->meta_files_num = meta_bake_files.size();
74 packed_bake->blob_files_num = blob_bake_files.size();
75
76 packed_bake->meta_files = MEM_new_array<NodesModifierBakeFile>(packed_bake->meta_files_num,
77 __func__);
78 packed_bake->blob_files = MEM_new_array<NodesModifierBakeFile>(packed_bake->blob_files_num,
79 __func__);
80
81 uninitialized_copy_n(meta_bake_files.data(), meta_bake_files.size(), packed_bake->meta_files);
82 uninitialized_copy_n(blob_bake_files.data(), blob_bake_files.size(), packed_bake->blob_files);
83
84 return packed_bake;
85}
86
88 const BakePath &bake_path,
89 ReportList *reports)
90{
91 auto unpack_file = [&](const StringRefNull directory, const NodesModifierBakeFile &bake_file) {
92 char file_path[FILE_MAX];
93 BLI_path_join(file_path, sizeof(file_path), directory.c_str(), bake_file.name);
94 if (!BLI_file_ensure_parent_dir_exists(file_path)) {
95 BKE_reportf(reports, RPT_ERROR, "Cannot ensure directory: %s", directory.c_str());
96 return false;
97 }
98 fstream fs(file_path, std::ios::out | std::ios::binary);
99 fs.write(static_cast<const char *>(bake_file.packed_file->data), bake_file.packed_file->size);
100 if (fs.bad()) {
101 BKE_reportf(reports, RPT_ERROR, "Cannot write file: %s", file_path);
102 return false;
103 }
104 return true;
105 };
106
107 for (const NodesModifierBakeFile &bake_file :
108 Span{packed_bake.meta_files, packed_bake.meta_files_num})
109 {
110 if (!unpack_file(bake_path.meta_dir, bake_file)) {
111 return false;
112 }
113 }
114 for (const NodesModifierBakeFile &bake_file :
115 Span{packed_bake.blob_files, packed_bake.blob_files_num})
116 {
117 if (!unpack_file(bake_path.blobs_dir, bake_file)) {
118 return false;
119 }
120 }
121 return true;
122}
123
125 ReportList *reports,
126 Object &object,
129{
130 if (bake.packed) {
132 }
133 const std::optional<bake::BakePath> bake_path = get_node_bake_path(bmain, object, nmd, bake.id);
134 if (!bake_path) {
136 }
137 bake.packed = bake::pack_bake_from_disk(*bake_path, reports);
138 if (!bake.packed) {
140 }
141 nmd.runtime->cache->reset_cache(bake.id);
145}
146
147static bool directory_is_empty(const StringRefNull path)
148{
149 direntry *entries = nullptr;
150 const int entries_num = BLI_filelist_dir_contents(path.c_str(), &entries);
151 BLI_SCOPED_DEFER([&]() { BLI_filelist_free(entries, entries_num); });
152 for (const int i : IndexRange(entries_num)) {
153 const direntry &entry = entries[i];
154 if (FILENAME_IS_CURRPAR(entry.relname)) {
155 continue;
156 }
157 return false;
158 }
159 return true;
160}
161
162static bool disk_bake_exists(const bke::bake::BakePath &path)
163{
164 return !directory_is_empty(path.meta_dir);
165}
166
168 ReportList *reports,
169 Object &object,
172 ePF_FileStatus how)
173{
174 if (!bake.packed) {
176 }
177 if (StringRef(BKE_main_blendfile_path(&bmain)).is_empty()) {
178 BKE_report(reports, RPT_ERROR, "Can only unpack bake if the current .blend file is saved");
180 }
181
183
184 auto prepare_local_path = [&]() {
185 const std::string directory = bake::get_default_node_bake_directory(
186 bmain, object, nmd, bake.id);
188 MEM_SAFE_DELETE(bake.directory);
189 bake.directory = BLI_strdup(BKE_path_template_escape(directory).c_str());
190 const char *base_path = ID_BLEND_PATH(&bmain, &object.id);
191 char absolute_dir[FILE_MAX];
192 STRNCPY(absolute_dir, directory.c_str());
193 BLI_path_abs(absolute_dir, base_path);
194 return bake::BakePath::from_single_root(absolute_dir);
195 };
196 auto prepare_original_path = [&]() {
197 if (const std::optional<bake::BakePath> bake_path = bake::get_node_bake_path(
198 bmain, object, nmd, bake.id))
199 {
200 return *bake_path;
201 }
202 return prepare_local_path();
203 };
204 auto delete_bake_on_disk = [&](const bake::BakePath &bake_path) {
205 BLI_delete(bake_path.meta_dir.c_str(), true, true);
206 BLI_delete(bake_path.blobs_dir.c_str(), true, true);
207 };
208 auto free_packed_bake = [&]() {
210 bake.packed = nullptr;
211 nmd.runtime->cache->reset_cache(bake.id);
212 };
213 auto finalize_on_success = [&]() {
216 };
217
218 switch (how) {
219 case PF_USE_ORIGINAL: {
220 const bake::BakePath bake_path = prepare_original_path();
221 if (!disk_bake_exists(bake_path)) {
222 delete_bake_on_disk(bake_path);
223 if (!bake::unpack_bake_to_disk(*bake.packed, bake_path, reports)) {
225 }
226 }
227 free_packed_bake();
228 return finalize_on_success();
229 }
230 case PF_WRITE_ORIGINAL: {
231 const bake::BakePath bake_path = prepare_original_path();
232 delete_bake_on_disk(bake_path);
233 if (!bake::unpack_bake_to_disk(*bake.packed, bake_path, reports)) {
235 }
236 free_packed_bake();
237 return finalize_on_success();
238 }
239 case PF_USE_LOCAL: {
240 const bake::BakePath bake_path = prepare_local_path();
241 if (!disk_bake_exists(bake_path)) {
242 delete_bake_on_disk(bake_path);
243 if (!bake::unpack_bake_to_disk(*bake.packed, bake_path, reports)) {
245 }
246 }
247 free_packed_bake();
248 return finalize_on_success();
249 }
250 case PF_WRITE_LOCAL: {
251 const bake::BakePath bake_path = prepare_local_path();
252 delete_bake_on_disk(bake_path);
253 if (!bake::unpack_bake_to_disk(*bake.packed, bake_path, reports)) {
255 }
256 free_packed_bake();
257 return finalize_on_success();
258 }
259 case PF_KEEP: {
260 return finalize_on_success();
261 }
262 case PF_REMOVE: {
263 free_packed_bake();
264 return finalize_on_success();
265 }
266 default: {
267 break;
268 }
269 }
271}
272
273} // namespace blender::bke::bake
Functions and classes for evaluating template expressions in filepaths.
File and directory operations.
#define BLI_SCOPED_DEFER(function_to_defer)
#define FILE_MAX
#define BLI_path_join(...)
#define FILENAME_IS_CURRPAR(_n)
#define ID_BLEND_PATH(_bmain, _id)
Definition DNA_ID.h:688
Object is a sort of wrapper for general info.
constexpr IndexRange()=default
constexpr const char * c_str() const
constexpr StringRef()
int64_t size() const
bool is_empty() const
static bool disk_bake_exists(const bke::bake::BakePath &path)
std::optional< BakePath > get_node_bake_path(const Main &bmain, const Object &object, const NodesModifierData &nmd, int node_id)
bool unpack_bake_to_disk(const NodesModifierPackedBake &packed_bake, const BakePath &bake_path, ReportList *reports)
NodesModifierPackedBake * pack_bake_from_disk(const BakePath &bake_path, ReportList *reports)
UnpackGeometryNodesBakeResult unpack_geometry_nodes_bake(Main &bmain, ReportList *reports, Object &object, NodesModifierData &nmd, NodesModifierBake &bake, ePF_FileStatus how)
PackGeometryNodesBakeResult pack_geometry_nodes_bake(Main &bmain, ReportList *reports, Object &object, NodesModifierData &nmd, NodesModifierBake &bake)
static Vector< NodesModifierBakeFile > pack_files_from_directory(const StringRefNull directory, ReportList *reports)
static bool directory_is_empty(const StringRefNull path)
std::string get_default_node_bake_directory(const Main &bmain, const Object &object, const NodesModifierData &nmd, int node_id)
char * STRNCPY(char(&dst)[N], const char *src)
@ NODES_MODIFIER_BAKE_CUSTOM_PATH
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
unsigned int BLI_filelist_dir_contents(const char *dirname, struct direntry **r_filelist)
PackedFile * BKE_packedfile_new(ReportList *reports, const char *filepath_rel, const char *basepath)
const char * BKE_main_blendfile_path(const Main *bmain) ATTR_NONNULL()
Definition main.cc:1058
std::string BKE_path_template_escape(StringRef text)
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
int BLI_delete(const char *path, bool dir, bool recursive) ATTR_NONNULL()
bool BLI_is_dir(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:457
@ NODES_MODIFIER_BAKE_TARGET_PACKED
@ NODES_MODIFIER_BAKE_TARGET_DISK
void BLI_filelist_free(struct direntry *filelist, unsigned int nrentries)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
char * BLI_strdup_null(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_MALLOC
void nodes_modifier_packed_bake_free(NodesModifierPackedBake *packed_bake)
bool BLI_file_ensure_parent_dir_exists(const char *filepath) ATTR_NONNULL(1)
Definition fileops_c.cc:454
void uninitialized_copy_n(const T *src, int64_t n, T *dst)
NodesModifierRuntime * runtime
std::shared_ptr< bke::bake::ModifierCache > cache
Definition MOD_nodes.hh:64
static BakePath from_single_root(StringRefNull root_dir)