Blender V5.3
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.hh"
15#include "BLI_path_utils.hh"
16#include "BLI_string.hh"
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
36namespace blender {
37
38static void reload_cachefile(bContext *C, CacheFile *cache_file)
39{
40 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
41 BKE_cachefile_reload(depsgraph, cache_file);
42}
43
45{
46 PropertyPointerRNA *pprop;
47
48 op->customdata = pprop = MEM_new<PropertyPointerRNA>("OpenPropertyPointerRNA");
50}
51
53 wmOperator *op,
54 const wmEvent * /*event*/)
55{
56 if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
57 char filepath[FILE_MAX];
58 Main *bmain = CTX_data_main(C);
59
60 /* Default to the same directory as the blend file. */
61 BLI_path_split_dir_part(BKE_main_blendfile_path(bmain), filepath, sizeof(filepath));
62 RNA_string_set(op->ptr, "filepath", filepath);
63 }
64
65 cachefile_init(C, op);
66
68
70}
71
72static void open_cancel(bContext * /*C*/, wmOperator *op)
73{
74 if (op->customdata) {
75 PropertyPointerRNA *prop_ptr = static_cast<PropertyPointerRNA *>(op->customdata);
76 op->customdata = nullptr;
77 MEM_delete(prop_ptr);
78 }
79}
80
82{
83 if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
84 BKE_report(op->reports, RPT_ERROR, "No filepath given");
85 return OPERATOR_CANCELLED;
86 }
87
88 char filepath[FILE_MAX];
89 RNA_string_get(op->ptr, "filepath", filepath);
90
91 Main *bmain = CTX_data_main(C);
92
93 CacheFile *cache_file = static_cast<CacheFile *>(
94 BKE_libblock_alloc(bmain, ID_CF, BLI_path_basename(filepath), 0));
95 STRNCPY(cache_file->filepath, filepath);
97
98 /* Will be set when running invoke, not exec directly. */
99 if (op->customdata != nullptr) {
100 /* hook into UI */
101 PropertyPointerRNA *pprop = static_cast<PropertyPointerRNA *>(op->customdata);
102 if (pprop->prop != nullptr) {
103 /* When creating new ID blocks, use is already 1, but RNA
104 * pointer see also increases user, so this compensates it. */
105 id_us_min(&cache_file->id);
106
107 PointerRNA idptr = RNA_id_pointer_create(&cache_file->id);
108 RNA_property_pointer_set(&pprop->ptr, pprop->prop, idptr, nullptr);
109 RNA_property_update(C, &pprop->ptr, pprop->prop);
110 }
111
112 op->customdata = nullptr;
113 MEM_delete(pprop);
114 }
115
116 return OPERATOR_FINISHED;
117}
118
120{
121 ot->name = "Open Cache File";
122 ot->description = "Load a cache file";
123 ot->idname = "CACHEFILE_OT_open";
124
127 ot->cancel = open_cancel;
128
136}
137
138/* ***************************** Reload Operator **************************** */
139
141{
142 CacheFile *cache_file = CTX_data_edit_cachefile(C);
143
144 if (cache_file == nullptr) {
145 return OPERATOR_CANCELLED;
146 }
147
148 reload_cachefile(C, cache_file);
149
150 return OPERATOR_FINISHED;
151}
152
154{
155 ot->name = "Refresh Archive";
156 ot->description = "Update objects paths list with new data from the archive";
157 ot->idname = "CACHEFILE_OT_reload";
158
159 /* API callbacks. */
161
162 /* flags */
164}
165
166/* ***************************** Add Layer Operator **************************** */
167
169 wmOperator *op,
170 const wmEvent * /*event*/)
171{
172 if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
173 char filepath[FILE_MAX];
174 Main *bmain = CTX_data_main(C);
175
176 /* Default to the same directory as the blend file. */
177 BLI_path_split_dir_part(BKE_main_blendfile_path(bmain), filepath, sizeof(filepath));
178 RNA_string_set(op->ptr, "filepath", filepath);
179 }
180
181 /* There is no more CacheFile set when returning from the file selector, so store it here. */
183
185
187}
188
190{
191 if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
192 BKE_report(op->reports, RPT_ERROR, "No filepath given");
193 return OPERATOR_CANCELLED;
194 }
195
196 CacheFile *cache_file = static_cast<CacheFile *>(op->customdata);
197
198 if (cache_file == nullptr) {
199 return OPERATOR_CANCELLED;
200 }
201
202 char filepath[FILE_MAX];
203 RNA_string_get(op->ptr, "filepath", filepath);
204
205 CacheFileLayer *layer = BKE_cachefile_add_layer(cache_file, filepath);
206
207 if (layer == nullptr) {
208 WM_global_report(RPT_ERROR, "Could not add a layer to the cache file");
209 return OPERATOR_CANCELLED;
210 }
211
212 reload_cachefile(C, cache_file);
214 return OPERATOR_FINISHED;
215}
216
218{
219 ot->name = "Add layer";
220 ot->description = "Add an override layer to the archive";
221 ot->idname = "CACHEFILE_OT_layer_add";
222
223 /* API callbacks. */
226
234}
235
236/* ***************************** Remove Layer Operator **************************** */
237
239{
240 CacheFile *cache_file = CTX_data_edit_cachefile(C);
241
242 if (cache_file == nullptr) {
243 return OPERATOR_CANCELLED;
244 }
245
248
249 reload_cachefile(C, cache_file);
251 return OPERATOR_FINISHED;
252}
253
255{
256 ot->name = "Add layer";
257 ot->description = "Remove an override layer from the archive";
258 ot->idname = "CACHEFILE_OT_layer_remove";
259
260 /* API callbacks. */
262
263 /* flags */
265}
266
267/* ***************************** Move Layer Operator **************************** */
268
270{
271 CacheFile *cache_file = CTX_data_edit_cachefile(C);
272
273 if (cache_file == nullptr) {
274 return OPERATOR_CANCELLED;
275 }
276
278
279 if (layer == nullptr) {
280 return OPERATOR_CANCELLED;
281 }
282
283 const int dir = RNA_enum_get(op->ptr, "direction");
284
285 if (BLI_listbase_link_move(&cache_file->layers, layer, dir)) {
286 cache_file->active_layer = BLI_findindex(&cache_file->layers, layer) + 1;
287 /* Only reload if something moved, might be expensive. */
288 reload_cachefile(C, cache_file);
290 }
291
292 return OPERATOR_FINISHED;
293}
294
296{
297 static const EnumPropertyItem layer_slot_move[] = {
298 {-1, "UP", 0, "Up", ""},
299 {1, "DOWN", 0, "Down", ""},
300 {0, nullptr, 0, nullptr, nullptr},
301 };
302
303 ot->name = "Move layer";
304 ot->description =
305 "Move layer in the list, layers further down the list will overwrite data from the layers "
306 "higher up";
307 ot->idname = "CACHEFILE_OT_layer_move";
308
309 /* API callbacks. */
311
312 /* flags */
314
315 RNA_def_enum(ot->srna,
316 "direction",
317 layer_slot_move,
318 0,
319 "Direction",
320 "Direction to move the active layer towards");
321}
322
323} // namespace blender
#define FILE_MAX
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define ND_DRAW
Definition WM_types.hh:502
#define NC_OBJECT
Definition WM_types.hh:392
#define layer
void context_active_but_prop_get_templateID(const bContext *C, PointerRNA *r_ptr, PropertyRNA **r_prop)
BMesh Iterators.
void WM_main_add_notifier(uint type, void *reference)
CacheFileLayer * BKE_cachefile_get_active_layer(CacheFile *cache_file)
Definition cachefile.cc:433
char * STRNCPY(char(&dst)[N], const char *src)
int RNA_enum_get(PointerRNA *ptr, const char *name)
static void open_cancel(bContext *, wmOperator *op)
Definition io_cache.cc:72
static wmOperatorStatus cachefile_open_invoke(bContext *C, wmOperator *op, const wmEvent *)
Definition io_cache.cc:52
void RNA_property_update(bContext *C, PointerRNA *ptr, PropertyRNA *prop)
void RNA_property_pointer_set(PointerRNA *ptr, PropertyRNA *prop, PointerRNA ptr_value, ReportList *reports)
CacheFile * CTX_data_edit_cachefile(const bContext *C)
PointerRNA RNA_id_pointer_create(ID *id)
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
static wmOperatorStatus cachefile_open_exec(bContext *C, wmOperator *op)
Definition io_cache.cc:81
static wmOperatorStatus cachefile_layer_open_invoke(bContext *C, wmOperator *op, const wmEvent *)
Definition io_cache.cc:168
void CACHEFILE_OT_layer_move(wmOperatorType *ot)
Definition io_cache.cc:295
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)
void CACHEFILE_OT_layer_add(wmOperatorType *ot)
Definition io_cache.cc:217
void BKE_cachefile_remove_layer(CacheFile *cache_file, CacheFileLayer *layer)
Definition cachefile.cc:439
static void cachefile_init(bContext *C, wmOperator *op)
Definition io_cache.cc:44
static wmOperatorStatus cachefile_layer_add_exec(bContext *C, wmOperator *op)
Definition io_cache.cc:189
const char * BKE_main_blendfile_path(const Main *bmain) ATTR_NONNULL()
Definition main.cc:1058
void CACHEFILE_OT_layer_remove(wmOperatorType *ot)
Definition io_cache.cc:254
void void void const char * BLI_path_basename(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:593
void void BLI_path_split_dir_part(const char *filepath, char *dir, size_t dir_maxncpy) ATTR_NONNULL(1
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
static wmOperatorStatus cachefile_layer_move_exec(bContext *C, wmOperator *op)
Definition io_cache.cc:269
@ WM_FILESEL_RELPATH
Definition WM_api.hh:1329
@ WM_FILESEL_FILEPATH
Definition WM_api.hh:1332
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
bool RNA_struct_property_is_set(PointerRNA *ptr, const char *identifier)
void CACHEFILE_OT_reload(wmOperatorType *ot)
Definition io_cache.cc:153
void CACHEFILE_OT_open(wmOperatorType *ot)
Definition io_cache.cc:119
void WM_global_report(eReportType type, const char *message)
static void reload_cachefile(bContext *C, CacheFile *cache_file)
Definition io_cache.cc:38
@ FILE_OPENFILE
Definition WM_api.hh:1341
Main * CTX_data_main(const bContext *C)
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
std::string RNA_string_get(PointerRNA *ptr, const char *name)
CacheFileLayer * BKE_cachefile_add_layer(CacheFile *cache_file, const char filepath[1024])
static wmOperatorStatus cachefile_reload_exec(bContext *C, wmOperator *)
Definition io_cache.cc:140
void id_us_min(ID *id)
Definition lib_id.cc:441
void * BKE_libblock_alloc(Main *bmain, short type, const char *name, int flag) ATTR_WARN_UNUSED_RESULT
Definition lib_id.cc:1545
void WM_event_add_fileselect(bContext *C, wmOperator *op)
static wmOperatorStatus cachefile_layer_remove_exec(bContext *C, wmOperator *)
Definition io_cache.cc:238
void BKE_cachefile_reload(Depsgraph *depsgraph, CacheFile *cache_file)
Definition cachefile.cc:317
void void void bool BLI_listbase_link_move(ListBase *listbase, void *vlink, int step) ATTR_NONNULL()
Definition listbase.cc:443
ListBaseT< CacheFileLayer > layers
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
void(*) cancel(bContext *C, wmOperator *op)
Definition WM_types.hh:1149
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
struct ReportList * reports