Blender V5.0
io_cache.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 "MEM_guardedalloc.h"
10
11#include "DNA_cachefile_types.h"
12#include "DNA_space_types.h"
13
14#include "BLI_listbase.h"
15#include "BLI_path_utils.hh"
16#include "BLI_string.h"
17
18#include "BKE_cachefile.hh"
19#include "BKE_context.hh"
20#include "BKE_lib_id.hh"
21#include "BKE_main.hh"
22#include "BKE_report.hh"
23
24#include "RNA_access.hh"
25#include "RNA_define.hh"
26
27#include "DEG_depsgraph.hh"
28
29#include "UI_interface.hh"
30
31#include "WM_api.hh"
32#include "WM_types.hh"
33
34#include "io_cache.hh"
35
36static void reload_cachefile(bContext *C, CacheFile *cache_file)
37{
40}
41
43{
44 PropertyPointerRNA *pprop;
45
46 op->customdata = pprop = MEM_new<PropertyPointerRNA>("OpenPropertyPointerRNA");
48}
49
51 wmOperator *op,
52 const wmEvent * /*event*/)
53{
54 if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
55 char filepath[FILE_MAX];
56 Main *bmain = CTX_data_main(C);
57
58 /* Default to the same directory as the blend file. */
59 BLI_path_split_dir_part(BKE_main_blendfile_path(bmain), filepath, sizeof(filepath));
60 RNA_string_set(op->ptr, "filepath", filepath);
61 }
62
63 cachefile_init(C, op);
64
66
68}
69
70static void open_cancel(bContext * /*C*/, wmOperator *op)
71{
72 if (op->customdata) {
73 PropertyPointerRNA *prop_ptr = static_cast<PropertyPointerRNA *>(op->customdata);
74 op->customdata = nullptr;
75 MEM_delete(prop_ptr);
76 }
77}
78
80{
81 if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
82 BKE_report(op->reports, RPT_ERROR, "No filepath given");
83 return OPERATOR_CANCELLED;
84 }
85
86 char filepath[FILE_MAX];
87 RNA_string_get(op->ptr, "filepath", filepath);
88
89 Main *bmain = CTX_data_main(C);
90
91 CacheFile *cache_file = static_cast<CacheFile *>(
92 BKE_libblock_alloc(bmain, ID_CF, BLI_path_basename(filepath), 0));
93 STRNCPY(cache_file->filepath, filepath);
95
96 /* Will be set when running invoke, not exec directly. */
97 if (op->customdata != nullptr) {
98 /* hook into UI */
99 PropertyPointerRNA *pprop = static_cast<PropertyPointerRNA *>(op->customdata);
100 if (pprop->prop != nullptr) {
101 /* When creating new ID blocks, use is already 1, but RNA
102 * pointer see also increases user, so this compensates it. */
103 id_us_min(&cache_file->id);
104
105 PointerRNA idptr = RNA_id_pointer_create(&cache_file->id);
106 RNA_property_pointer_set(&pprop->ptr, pprop->prop, idptr, nullptr);
107 RNA_property_update(C, &pprop->ptr, pprop->prop);
108 }
109
110 op->customdata = nullptr;
111 MEM_delete(pprop);
112 }
113
114 return OPERATOR_FINISHED;
115}
116
118{
119 ot->name = "Open Cache File";
120 ot->description = "Load a cache file";
121 ot->idname = "CACHEFILE_OT_open";
122
123 ot->invoke = cachefile_open_invoke;
124 ot->exec = cachefile_open_exec;
125 ot->cancel = open_cancel;
126
134}
135
136/* ***************************** Reload Operator **************************** */
137
139{
140 CacheFile *cache_file = CTX_data_edit_cachefile(C);
141
142 if (cache_file == nullptr) {
143 return OPERATOR_CANCELLED;
144 }
145
146 reload_cachefile(C, cache_file);
147
148 return OPERATOR_FINISHED;
149}
150
152{
153 ot->name = "Refresh Archive";
154 ot->description = "Update objects paths list with new data from the archive";
155 ot->idname = "CACHEFILE_OT_reload";
156
157 /* API callbacks. */
159
160 /* flags */
162}
163
164/* ***************************** Add Layer Operator **************************** */
165
167 wmOperator *op,
168 const wmEvent * /*event*/)
169{
170 if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
171 char filepath[FILE_MAX];
172 Main *bmain = CTX_data_main(C);
173
174 /* Default to the same directory as the blend file. */
175 BLI_path_split_dir_part(BKE_main_blendfile_path(bmain), filepath, sizeof(filepath));
176 RNA_string_set(op->ptr, "filepath", filepath);
177 }
178
179 /* There is no more CacheFile set when returning from the file selector, so store it here. */
181
183
185}
186
188{
189 if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
190 BKE_report(op->reports, RPT_ERROR, "No filepath given");
191 return OPERATOR_CANCELLED;
192 }
193
194 CacheFile *cache_file = static_cast<CacheFile *>(op->customdata);
195
196 if (cache_file == nullptr) {
197 return OPERATOR_CANCELLED;
198 }
199
200 char filepath[FILE_MAX];
201 RNA_string_get(op->ptr, "filepath", filepath);
202
203 CacheFileLayer *layer = BKE_cachefile_add_layer(cache_file, filepath);
204
205 if (layer == nullptr) {
206 WM_global_report(RPT_ERROR, "Could not add a layer to the cache file");
207 return OPERATOR_CANCELLED;
208 }
209
210 reload_cachefile(C, cache_file);
212 return OPERATOR_FINISHED;
213}
214
216{
217 ot->name = "Add layer";
218 ot->description = "Add an override layer to the archive";
219 ot->idname = "CACHEFILE_OT_layer_add";
220
221 /* API callbacks. */
224
232}
233
234/* ***************************** Remove Layer Operator **************************** */
235
237{
238 CacheFile *cache_file = CTX_data_edit_cachefile(C);
239
240 if (cache_file == nullptr) {
241 return OPERATOR_CANCELLED;
242 }
243
245 BKE_cachefile_remove_layer(cache_file, layer);
246
247 reload_cachefile(C, cache_file);
249 return OPERATOR_FINISHED;
250}
251
253{
254 ot->name = "Add layer";
255 ot->description = "Remove an override layer from the archive";
256 ot->idname = "CACHEFILE_OT_layer_remove";
257
258 /* API callbacks. */
260
261 /* flags */
263}
264
265/* ***************************** Move Layer Operator **************************** */
266
268{
269 CacheFile *cache_file = CTX_data_edit_cachefile(C);
270
271 if (cache_file == nullptr) {
272 return OPERATOR_CANCELLED;
273 }
274
276
277 if (layer == nullptr) {
278 return OPERATOR_CANCELLED;
279 }
280
281 const int dir = RNA_enum_get(op->ptr, "direction");
282
283 if (BLI_listbase_link_move(&cache_file->layers, layer, dir)) {
284 cache_file->active_layer = BLI_findindex(&cache_file->layers, layer) + 1;
285 /* Only reload if something moved, might be expensive. */
286 reload_cachefile(C, cache_file);
288 }
289
290 return OPERATOR_FINISHED;
291}
292
294{
295 static const EnumPropertyItem layer_slot_move[] = {
296 {-1, "UP", 0, "Up", ""},
297 {1, "DOWN", 0, "Down", ""},
298 {0, nullptr, 0, nullptr, nullptr},
299 };
300
301 ot->name = "Move layer";
302 ot->description =
303 "Move layer in the list, layers further down the list will overwrite data from the layers "
304 "higher up";
305 ot->idname = "CACHEFILE_OT_layer_move";
306
307 /* API callbacks. */
309
310 /* flags */
312
313 RNA_def_enum(ot->srna,
314 "direction",
315 layer_slot_move,
316 0,
317 "Direction",
318 "Direction to move the active vertex group towards");
319}
void BKE_cachefile_remove_layer(CacheFile *cache_file, CacheFileLayer *layer)
Definition cachefile.cc:444
CacheFileLayer * BKE_cachefile_get_active_layer(CacheFile *cache_file)
Definition cachefile.cc:438
CacheFileLayer * BKE_cachefile_add_layer(CacheFile *cache_file, const char filepath[1024])
Definition cachefile.cc:418
void BKE_cachefile_reload(Depsgraph *depsgraph, CacheFile *cache_file)
Definition cachefile.cc:318
CacheFile * CTX_data_edit_cachefile(const bContext *C)
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C)
Main * CTX_data_main(const bContext *C)
void * BKE_libblock_alloc(Main *bmain, short type, const char *name, int flag) ATTR_WARN_UNUSED_RESULT
Definition lib_id.cc:1447
void id_us_min(ID *id)
Definition lib_id.cc:366
const char * BKE_main_blendfile_path(const Main *bmain) ATTR_NONNULL()
Definition main.cc:887
@ RPT_ERROR
Definition BKE_report.hh:39
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:153
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:586
void void void bool BLI_listbase_link_move(ListBase *listbase, void *vlink, int step) ATTR_NONNULL()
Definition listbase.cc:436
void void void const char * BLI_path_basename(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
#define FILE_MAX
void void BLI_path_split_dir_part(const char *filepath, char *dir, size_t dir_maxncpy) ATTR_NONNULL(1
char * STRNCPY(char(&dst)[N], const char *src)
Definition BLI_string.h:693
void DEG_id_tag_update(ID *id, unsigned int flags)
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
@ ID_CF
@ FILE_SORT_DEFAULT
@ FILE_BLENDER
@ FILE_TYPE_ALEMBIC
@ FILE_TYPE_FOLDER
@ FILE_TYPE_USD
@ FILE_DEFAULTDISPLAY
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
@ OPERATOR_RUNNING_MODAL
Read Guarded memory(de)allocation.
#define C
Definition RandGen.cpp:29
void UI_context_active_but_prop_get_templateID(const bContext *C, PointerRNA *r_ptr, PropertyRNA **r_prop)
@ WM_FILESEL_RELPATH
Definition WM_api.hh:1121
@ WM_FILESEL_FILEPATH
Definition WM_api.hh:1124
@ FILE_OPENFILE
Definition WM_api.hh:1133
#define ND_DRAW
Definition WM_types.hh:461
@ OPTYPE_UNDO
Definition WM_types.hh:182
@ OPTYPE_REGISTER
Definition WM_types.hh:180
#define NC_OBJECT
Definition WM_types.hh:379
BPy_StructRNA * depsgraph
static wmOperatorStatus cachefile_layer_move_exec(bContext *C, wmOperator *op)
Definition io_cache.cc:267
static void open_cancel(bContext *, wmOperator *op)
Definition io_cache.cc:70
static wmOperatorStatus cachefile_reload_exec(bContext *C, wmOperator *)
Definition io_cache.cc:138
static void reload_cachefile(bContext *C, CacheFile *cache_file)
Definition io_cache.cc:36
void CACHEFILE_OT_open(wmOperatorType *ot)
Definition io_cache.cc:117
void CACHEFILE_OT_layer_remove(wmOperatorType *ot)
Definition io_cache.cc:252
void CACHEFILE_OT_layer_move(wmOperatorType *ot)
Definition io_cache.cc:293
static wmOperatorStatus cachefile_layer_add_exec(bContext *C, wmOperator *op)
Definition io_cache.cc:187
static void cachefile_init(bContext *C, wmOperator *op)
Definition io_cache.cc:42
static wmOperatorStatus cachefile_layer_open_invoke(bContext *C, wmOperator *op, const wmEvent *)
Definition io_cache.cc:166
void CACHEFILE_OT_layer_add(wmOperatorType *ot)
Definition io_cache.cc:215
static wmOperatorStatus cachefile_open_exec(bContext *C, wmOperator *op)
Definition io_cache.cc:79
void CACHEFILE_OT_reload(wmOperatorType *ot)
Definition io_cache.cc:151
static wmOperatorStatus cachefile_layer_remove_exec(bContext *C, wmOperator *)
Definition io_cache.cc:236
static wmOperatorStatus cachefile_open_invoke(bContext *C, wmOperator *op, const wmEvent *)
Definition io_cache.cc:50
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
void RNA_property_pointer_set(PointerRNA *ptr, PropertyRNA *prop, PointerRNA ptr_value, ReportList *reports)
void RNA_property_update(bContext *C, PointerRNA *ptr, PropertyRNA *prop)
std::string RNA_string_get(PointerRNA *ptr, const char *name)
bool RNA_struct_property_is_set(PointerRNA *ptr, const char *identifier)
int RNA_enum_get(PointerRNA *ptr, const char *name)
PointerRNA RNA_id_pointer_create(ID *id)
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
char filepath[1024]
PropertyRNA * prop
Definition RNA_types.hh:146
struct ReportList * reports
struct PointerRNA * ptr
void WM_event_add_fileselect(bContext *C, wmOperator *op)
void WM_global_report(eReportType type, const char *message)
void WM_main_add_notifier(uint type, void *reference)
wmOperatorType * ot
Definition wm_files.cc:4237
void WM_operator_properties_filesel(wmOperatorType *ot, const int filter, const short type, const eFileSel_Action action, const eFileSel_Flag flag, const short display, const short sort)