Blender V4.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
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
50static int cachefile_open_invoke(bContext *C, wmOperator *op, const wmEvent * /*event*/)
51{
52 if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
53 char filepath[FILE_MAX];
54 Main *bmain = CTX_data_main(C);
55
56 /* Default to the same directory as the blend file. */
57 BLI_path_split_dir_part(BKE_main_blendfile_path(bmain), filepath, sizeof(filepath));
58 RNA_string_set(op->ptr, "filepath", filepath);
59 }
60
61 cachefile_init(C, op);
62
64
66}
67
68static void open_cancel(bContext * /*C*/, wmOperator *op)
69{
70 if (op->customdata) {
71 PropertyPointerRNA *prop_ptr = static_cast<PropertyPointerRNA *>(op->customdata);
72 op->customdata = nullptr;
73 MEM_delete(prop_ptr);
74 }
75}
76
78{
79 if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
80 BKE_report(op->reports, RPT_ERROR, "No filepath given");
81 return OPERATOR_CANCELLED;
82 }
83
84 char filepath[FILE_MAX];
85 RNA_string_get(op->ptr, "filepath", filepath);
86
87 Main *bmain = CTX_data_main(C);
88
89 CacheFile *cache_file = static_cast<CacheFile *>(
90 BKE_libblock_alloc(bmain, ID_CF, BLI_path_basename(filepath), 0));
91 STRNCPY(cache_file->filepath, filepath);
93
94 /* Will be set when running invoke, not exec directly. */
95 if (op->customdata != nullptr) {
96 /* hook into UI */
97 PropertyPointerRNA *pprop = static_cast<PropertyPointerRNA *>(op->customdata);
98 if (pprop->prop != nullptr) {
99 /* When creating new ID blocks, use is already 1, but RNA
100 * pointer see also increases user, so this compensates it. */
101 id_us_min(&cache_file->id);
102
103 PointerRNA idptr = RNA_id_pointer_create(&cache_file->id);
104 RNA_property_pointer_set(&pprop->ptr, pprop->prop, idptr, nullptr);
105 RNA_property_update(C, &pprop->ptr, pprop->prop);
106 }
107
108 op->customdata = nullptr;
109 MEM_delete(pprop);
110 }
111
112 return OPERATOR_FINISHED;
113}
114
133
134/* ***************************** Reload Operator **************************** */
135
137{
138 CacheFile *cache_file = CTX_data_edit_cachefile(C);
139
140 if (cache_file == nullptr) {
141 return OPERATOR_CANCELLED;
142 }
143
144 reload_cachefile(C, cache_file);
145
146 return OPERATOR_FINISHED;
147}
148
150{
151 ot->name = "Refresh Archive";
152 ot->description = "Update objects paths list with new data from the archive";
153 ot->idname = "CACHEFILE_OT_reload";
154
155 /* api callbacks */
157
158 /* flags */
160}
161
162/* ***************************** Add Layer Operator **************************** */
163
164static int cachefile_layer_open_invoke(bContext *C, wmOperator *op, const wmEvent * /*event*/)
165{
166 if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
167 char filepath[FILE_MAX];
168 Main *bmain = CTX_data_main(C);
169
170 /* Default to the same directory as the blend file. */
171 BLI_path_split_dir_part(BKE_main_blendfile_path(bmain), filepath, sizeof(filepath));
172 RNA_string_set(op->ptr, "filepath", filepath);
173 }
174
175 /* There is no more CacheFile set when returning from the file selector, so store it here. */
177
179
181}
182
184{
185 if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
186 BKE_report(op->reports, RPT_ERROR, "No filepath given");
187 return OPERATOR_CANCELLED;
188 }
189
190 CacheFile *cache_file = static_cast<CacheFile *>(op->customdata);
191
192 if (cache_file == nullptr) {
193 return OPERATOR_CANCELLED;
194 }
195
196 char filepath[FILE_MAX];
197 RNA_string_get(op->ptr, "filepath", filepath);
198
199 CacheFileLayer *layer = BKE_cachefile_add_layer(cache_file, filepath);
200
201 if (layer == nullptr) {
202 WM_report(RPT_ERROR, "Could not add a layer to the cache file");
203 return OPERATOR_CANCELLED;
204 }
205
206 reload_cachefile(C, cache_file);
208 return OPERATOR_FINISHED;
209}
210
212{
213 ot->name = "Add layer";
214 ot->description = "Add an override layer to the archive";
215 ot->idname = "CACHEFILE_OT_layer_add";
216
217 /* api callbacks */
220
228}
229
230/* ***************************** Remove Layer Operator **************************** */
231
233{
234 CacheFile *cache_file = CTX_data_edit_cachefile(C);
235
236 if (cache_file == nullptr) {
237 return OPERATOR_CANCELLED;
238 }
239
241 BKE_cachefile_remove_layer(cache_file, layer);
242
243 reload_cachefile(C, cache_file);
245 return OPERATOR_FINISHED;
246}
247
249{
250 ot->name = "Add layer";
251 ot->description = "Remove an override layer from the archive";
252 ot->idname = "CACHEFILE_OT_layer_remove";
253
254 /* api callbacks */
256
257 /* flags */
259}
260
261/* ***************************** Move Layer Operator **************************** */
262
264{
265 CacheFile *cache_file = CTX_data_edit_cachefile(C);
266
267 if (cache_file == nullptr) {
268 return OPERATOR_CANCELLED;
269 }
270
272
273 if (layer == nullptr) {
274 return OPERATOR_CANCELLED;
275 }
276
277 const int dir = RNA_enum_get(op->ptr, "direction");
278
279 if (BLI_listbase_link_move(&cache_file->layers, layer, dir)) {
280 cache_file->active_layer = BLI_findindex(&cache_file->layers, layer) + 1;
281 /* Only reload if something moved, might be expensive. */
282 reload_cachefile(C, cache_file);
284 }
285
286 return OPERATOR_FINISHED;
287}
288
290{
291 static const EnumPropertyItem layer_slot_move[] = {
292 {-1, "UP", 0, "Up", ""},
293 {1, "DOWN", 0, "Down", ""},
294 {0, nullptr, 0, nullptr, nullptr},
295 };
296
297 ot->name = "Move layer";
298 ot->description =
299 "Move layer in the list, layers further down the list will overwrite data from the layers "
300 "higher up";
301 ot->idname = "CACHEFILE_OT_layer_move";
302
303 /* api callbacks */
305
306 /* flags */
308
310 "direction",
311 layer_slot_move,
312 0,
313 "Direction",
314 "Direction to move the active vertex group towards");
315}
void BKE_cachefile_remove_layer(CacheFile *cache_file, CacheFileLayer *layer)
Definition cachefile.cc:458
CacheFileLayer * BKE_cachefile_get_active_layer(CacheFile *cache_file)
Definition cachefile.cc:452
CacheFileLayer * BKE_cachefile_add_layer(CacheFile *cache_file, const char filepath[1024])
Definition cachefile.cc:431
void BKE_cachefile_reload(Depsgraph *depsgraph, CacheFile *cache_file)
Definition cachefile.cc:325
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:1415
void id_us_min(ID *id)
Definition lib_id.cc:359
const char * BKE_main_blendfile_path(const Main *bmain) ATTR_NONNULL()
Definition main.cc:832
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:125
void void void bool BLI_listbase_link_move(ListBase *listbase, void *vlink, int step) ATTR_NONNULL()
Definition listbase.cc:435
int BLI_findindex(const struct ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
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
#define STRNCPY(dst, src)
Definition BLI_string.h:593
void DEG_id_tag_update(ID *id, unsigned int flags)
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1085
@ ID_CF
@ FILE_SORT_DEFAULT
@ FILE_BLENDER
@ FILE_TYPE_ALEMBIC
@ FILE_TYPE_FOLDER
@ FILE_TYPE_USD
@ FILE_DEFAULTDISPLAY
@ OPERATOR_RUNNING_MODAL
Read Guarded memory(de)allocation.
void UI_context_active_but_prop_get_templateID(const bContext *C, PointerRNA *r_ptr, PropertyRNA **r_prop)
@ WM_FILESEL_RELPATH
Definition WM_api.hh:933
@ WM_FILESEL_FILEPATH
Definition WM_api.hh:936
@ FILE_OPENFILE
Definition WM_api.hh:945
@ OPTYPE_UNDO
Definition WM_types.hh:162
@ OPTYPE_REGISTER
Definition WM_types.hh:160
#define ND_DRAW
Definition WM_types.hh:428
#define NC_OBJECT
Definition WM_types.hh:346
const Depsgraph * depsgraph
static void open_cancel(bContext *, wmOperator *op)
Definition io_cache.cc:68
static int cachefile_open_invoke(bContext *C, wmOperator *op, const wmEvent *)
Definition io_cache.cc:50
static void reload_cachefile(bContext *C, CacheFile *cache_file)
Definition io_cache.cc:36
void CACHEFILE_OT_open(wmOperatorType *ot)
Definition io_cache.cc:115
static int cachefile_layer_move_exec(bContext *C, wmOperator *op)
Definition io_cache.cc:263
void CACHEFILE_OT_layer_remove(wmOperatorType *ot)
Definition io_cache.cc:248
static int cachefile_open_exec(bContext *C, wmOperator *op)
Definition io_cache.cc:77
void CACHEFILE_OT_layer_move(wmOperatorType *ot)
Definition io_cache.cc:289
static void cachefile_init(bContext *C, wmOperator *op)
Definition io_cache.cc:42
void CACHEFILE_OT_layer_add(wmOperatorType *ot)
Definition io_cache.cc:211
static int cachefile_reload_exec(bContext *C, wmOperator *)
Definition io_cache.cc:136
static int cachefile_layer_remove_exec(bContext *C, wmOperator *)
Definition io_cache.cc:232
static int cachefile_layer_add_exec(bContext *C, wmOperator *op)
Definition io_cache.cc:183
void CACHEFILE_OT_reload(wmOperatorType *ot)
Definition io_cache.cc:149
static int cachefile_layer_open_invoke(bContext *C, wmOperator *op, const wmEvent *)
Definition io_cache.cc:164
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)
void RNA_string_get(PointerRNA *ptr, const char *name, char *value)
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:49
const char * name
Definition WM_types.hh:990
const char * idname
Definition WM_types.hh:992
int(* invoke)(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1022
int(* exec)(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1006
const char * description
Definition WM_types.hh:996
StructRNA * srna
Definition WM_types.hh:1080
void(* cancel)(bContext *C, wmOperator *op)
Definition WM_types.hh:1028
struct ReportList * reports
struct PointerRNA * ptr
void WM_event_add_fileselect(bContext *C, wmOperator *op)
void WM_report(eReportType type, const char *message)
void WM_main_add_notifier(uint type, void *reference)
wmOperatorType * ot
Definition wm_files.cc:4125
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)