Blender  V3.3
io_cache.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2016 Blender Foundation. All rights reserved. */
3 
8 #include "MEM_guardedalloc.h"
9 
10 #include "DNA_cachefile_types.h"
11 #include "DNA_space_types.h"
12 
13 #include "BLI_listbase.h"
14 #include "BLI_path_util.h"
15 #include "BLI_string.h"
16 
17 #include "BKE_cachefile.h"
18 #include "BKE_context.h"
19 #include "BKE_lib_id.h"
20 #include "BKE_main.h"
21 #include "BKE_report.h"
22 
23 #include "RNA_access.h"
24 #include "RNA_define.h"
25 
26 #include "DEG_depsgraph.h"
27 
28 #include "UI_interface.h"
29 
30 #include "WM_api.h"
31 #include "WM_types.h"
32 
33 #include "io_cache.h"
34 
35 static void reload_cachefile(bContext *C, CacheFile *cache_file)
36 {
38  BKE_cachefile_reload(depsgraph, cache_file);
39 }
40 
41 static void cachefile_init(bContext *C, wmOperator *op)
42 {
43  PropertyPointerRNA *pprop;
44 
45  op->customdata = pprop = MEM_callocN(sizeof(PropertyPointerRNA), "OpenPropertyPointerRNA");
47 }
48 
49 static int cachefile_open_invoke(bContext *C, wmOperator *op, const wmEvent *event)
50 {
51  if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
52  char filepath[FILE_MAX];
53  Main *bmain = CTX_data_main(C);
54 
55  BLI_strncpy(filepath, BKE_main_blendfile_path(bmain), sizeof(filepath));
56  BLI_path_extension_replace(filepath, sizeof(filepath), ".abc");
57  RNA_string_set(op->ptr, "filepath", filepath);
58  }
59 
60  cachefile_init(C, op);
61 
63 
65 
66  UNUSED_VARS(event);
67 }
68 
69 static void open_cancel(bContext *UNUSED(C), wmOperator *op)
70 {
71  MEM_freeN(op->customdata);
72  op->customdata = NULL;
73 }
74 
76 {
77  if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
78  BKE_report(op->reports, RPT_ERROR, "No filepath given");
79  return OPERATOR_CANCELLED;
80  }
81 
82  char filepath[FILE_MAX];
83  RNA_string_get(op->ptr, "filepath", filepath);
84 
85  Main *bmain = CTX_data_main(C);
86 
87  CacheFile *cache_file = BKE_libblock_alloc(bmain, ID_CF, BLI_path_basename(filepath), 0);
88  BLI_strncpy(cache_file->filepath, filepath, FILE_MAX);
90 
91  /* Will be set when running invoke, not exec directly. */
92  if (op->customdata != NULL) {
93  /* hook into UI */
94  PropertyPointerRNA *pprop = op->customdata;
95  if (pprop->prop) {
96  /* When creating new ID blocks, use is already 1, but RNA
97  * pointer see also increases user, so this compensates it. */
98  id_us_min(&cache_file->id);
99 
100  PointerRNA idptr;
101  RNA_id_pointer_create(&cache_file->id, &idptr);
102  RNA_property_pointer_set(&pprop->ptr, pprop->prop, idptr, NULL);
103  RNA_property_update(C, &pprop->ptr, pprop->prop);
104  }
105 
106  MEM_freeN(op->customdata);
107  }
108 
109  return OPERATOR_FINISHED;
110 }
111 
113 {
114  ot->name = "Open Cache File";
115  ot->description = "Load a cache file";
116  ot->idname = "CACHEFILE_OT_open";
117 
120  ot->cancel = open_cancel;
121 
124  FILE_BLENDER,
129 }
130 
131 /* ***************************** Reload Operator **************************** */
132 
134 {
135  CacheFile *cache_file = CTX_data_edit_cachefile(C);
136 
137  if (!cache_file) {
138  return OPERATOR_CANCELLED;
139  }
140 
141  reload_cachefile(C, cache_file);
142 
143  return OPERATOR_FINISHED;
144 }
145 
147 {
148  ot->name = "Refresh Archive";
149  ot->description = "Update objects paths list with new data from the archive";
150  ot->idname = "CACHEFILE_OT_reload";
151 
152  /* api callbacks */
154 
155  /* flags */
157 }
158 
159 /* ***************************** Add Layer Operator **************************** */
160 
161 static int cachefile_layer_open_invoke(bContext *C, wmOperator *op, const wmEvent *event)
162 {
163  if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
164  char filepath[FILE_MAX];
165  Main *bmain = CTX_data_main(C);
166 
167  BLI_strncpy(filepath, BKE_main_blendfile_path(bmain), sizeof(filepath));
168  BLI_path_extension_replace(filepath, sizeof(filepath), ".abc");
169  RNA_string_set(op->ptr, "filepath", filepath);
170  }
171 
172  /* There is no more CacheFile set when returning from the file selector, so store it here. */
174 
176 
177  return OPERATOR_RUNNING_MODAL;
178 
179  UNUSED_VARS(event);
180 }
181 
183 {
184  if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
185  BKE_report(op->reports, RPT_ERROR, "No filepath given");
186  return OPERATOR_CANCELLED;
187  }
188 
189  CacheFile *cache_file = op->customdata;
190 
191  if (!cache_file) {
192  return OPERATOR_CANCELLED;
193  }
194 
195  char filepath[FILE_MAX];
196  RNA_string_get(op->ptr, "filepath", filepath);
197 
198  CacheFileLayer *layer = BKE_cachefile_add_layer(cache_file, filepath);
199 
200  if (!layer) {
201  WM_report(RPT_ERROR, "Could not add a layer to the cache file");
202  return OPERATOR_CANCELLED;
203  }
204 
205  reload_cachefile(C, cache_file);
207  return OPERATOR_FINISHED;
208 }
209 
211 {
212  ot->name = "Add layer";
213  ot->description = "Add an override layer to the archive";
214  ot->idname = "CACHEFILE_OT_layer_add";
215 
216  /* api callbacks */
219 
222  FILE_BLENDER,
227 }
228 
229 /* ***************************** Remove Layer Operator **************************** */
230 
232 {
233  CacheFile *cache_file = CTX_data_edit_cachefile(C);
234 
235  if (!cache_file) {
236  return OPERATOR_CANCELLED;
237  }
238 
239  CacheFileLayer *layer = BKE_cachefile_get_active_layer(cache_file);
240  BKE_cachefile_remove_layer(cache_file, layer);
241 
242  reload_cachefile(C, cache_file);
244  return OPERATOR_FINISHED;
245 }
246 
248 {
249  ot->name = "Add layer";
250  ot->description = "Remove an override layer to the archive";
251  ot->idname = "CACHEFILE_OT_layer_remove";
252 
253  /* api callbacks */
255 
256  /* flags */
258 }
259 
260 /* ***************************** Move Layer Operator **************************** */
261 
263 {
264  CacheFile *cache_file = CTX_data_edit_cachefile(C);
265 
266  if (!cache_file) {
267  return OPERATOR_CANCELLED;
268  }
269 
270  CacheFileLayer *layer = BKE_cachefile_get_active_layer(cache_file);
271 
272  if (!layer) {
273  return OPERATOR_CANCELLED;
274  }
275 
276  const int dir = RNA_enum_get(op->ptr, "direction");
277 
278  if (BLI_listbase_link_move(&cache_file->layers, layer, dir)) {
279  cache_file->active_layer = BLI_findindex(&cache_file->layers, layer) + 1;
280  /* Only reload if something moved, might be expensive. */
281  reload_cachefile(C, cache_file);
283  }
284 
285  return OPERATOR_FINISHED;
286 }
287 
289 {
290  static const EnumPropertyItem layer_slot_move[] = {
291  {-1, "UP", 0, "Up", ""},
292  {1, "DOWN", 0, "Down", ""},
293  {0, NULL, 0, NULL, NULL},
294  };
295 
296  ot->name = "Move layer";
297  ot->description =
298  "Move layer in the list, layers further down the list will overwrite data from the layers "
299  "higher up";
300  ot->idname = "CACHEFILE_OT_layer_move";
301 
302  /* api callbacks */
304 
305  /* flags */
307 
309  "direction",
310  layer_slot_move,
311  0,
312  "Direction",
313  "Direction to move the active vertex group towards");
314 }
struct CacheFileLayer * BKE_cachefile_add_layer(struct CacheFile *cache_file, const char filename[1024])
Definition: cachefile.c:432
void BKE_cachefile_remove_layer(struct CacheFile *cache_file, struct CacheFileLayer *layer)
Definition: cachefile.c:457
struct CacheFileLayer * BKE_cachefile_get_active_layer(struct CacheFile *cache_file)
Definition: cachefile.c:452
void BKE_cachefile_reload(struct Depsgraph *depsgraph, struct CacheFile *cache_file)
Definition: cachefile.c:331
struct Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C)
Definition: context.c:1528
struct CacheFile * CTX_data_edit_cachefile(const bContext *C)
Definition: context.c:1400
struct Main * CTX_data_main(const bContext *C)
Definition: context.c:1074
void id_us_min(struct ID *id)
Definition: lib_id.c:313
void * BKE_libblock_alloc(struct Main *bmain, short type, const char *name, int flag) ATTR_WARN_UNUSED_RESULT
Definition: lib_id.c:1050
const char * BKE_main_blendfile_path(const struct Main *bmain) ATTR_NONNULL()
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition: report.c:83
void void void bool BLI_listbase_link_move(ListBase *listbase, void *vlink, int step) ATTR_NONNULL()
Definition: listbase.c:405
int BLI_findindex(const struct ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
const char * BLI_path_basename(const char *path) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT
Definition: path_util.c:1653
#define FILE_MAX
bool BLI_path_extension_replace(char *path, size_t maxlen, const char *ext) ATTR_NONNULL()
Definition: path_util.c:1393
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t maxncpy) ATTR_NONNULL()
Definition: string.c:64
#define UNUSED_VARS(...)
#define UNUSED(x)
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:35
void DEG_id_tag_update(struct ID *id, int flag)
@ ID_RECALC_COPY_ON_WRITE
Definition: DNA_ID.h:834
@ ID_CF
Definition: DNA_ID_enums.h:78
@ FILE_SORT_DEFAULT
@ FILE_BLENDER
@ FILE_TYPE_ALEMBIC
@ FILE_TYPE_FOLDER
@ FILE_DEFAULTDISPLAY
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
@ OPERATOR_RUNNING_MODAL
Read Guarded memory(de)allocation.
#define C
Definition: RandGen.cpp:25
void UI_context_active_but_prop_get_templateID(struct bContext *C, struct PointerRNA *r_ptr, struct PropertyRNA **r_prop)
@ WM_FILESEL_RELPATH
Definition: WM_api.h:752
@ WM_FILESEL_FILEPATH
Definition: WM_api.h:755
@ FILE_OPENFILE
Definition: WM_api.h:764
@ OPTYPE_UNDO
Definition: WM_types.h:148
@ OPTYPE_REGISTER
Definition: WM_types.h:146
#define ND_DRAW
Definition: WM_types.h:410
#define NC_OBJECT
Definition: WM_types.h:329
const Depsgraph * depsgraph
static int cachefile_layer_remove_exec(bContext *C, wmOperator *UNUSED(op))
Definition: io_cache.c:231
static void reload_cachefile(bContext *C, CacheFile *cache_file)
Definition: io_cache.c:35
void CACHEFILE_OT_open(wmOperatorType *ot)
Definition: io_cache.c:112
static int cachefile_open_invoke(bContext *C, wmOperator *op, const wmEvent *event)
Definition: io_cache.c:49
static int cachefile_layer_move_exec(bContext *C, wmOperator *op)
Definition: io_cache.c:262
void CACHEFILE_OT_layer_remove(wmOperatorType *ot)
Definition: io_cache.c:247
static int cachefile_open_exec(bContext *C, wmOperator *op)
Definition: io_cache.c:75
void CACHEFILE_OT_layer_move(wmOperatorType *ot)
Definition: io_cache.c:288
static void cachefile_init(bContext *C, wmOperator *op)
Definition: io_cache.c:41
static int cachefile_layer_open_invoke(bContext *C, wmOperator *op, const wmEvent *event)
Definition: io_cache.c:161
void CACHEFILE_OT_layer_add(wmOperatorType *ot)
Definition: io_cache.c:210
static void open_cancel(bContext *UNUSED(C), wmOperator *op)
Definition: io_cache.c:69
static int cachefile_reload_exec(bContext *C, wmOperator *UNUSED(op))
Definition: io_cache.c:133
static int cachefile_layer_add_exec(bContext *C, wmOperator *op)
Definition: io_cache.c:182
void CACHEFILE_OT_reload(wmOperatorType *ot)
Definition: io_cache.c:146
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:31
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
Definition: rna_access.c:5155
void RNA_id_pointer_create(ID *id, PointerRNA *r_ptr)
Definition: rna_access.c:112
void RNA_property_pointer_set(PointerRNA *ptr, PropertyRNA *prop, PointerRNA ptr_value, ReportList *reports)
Definition: rna_access.c:3532
void RNA_property_update(bContext *C, PointerRNA *ptr, PropertyRNA *prop)
Definition: rna_access.c:2138
void RNA_string_get(PointerRNA *ptr, const char *name, char *value)
Definition: rna_access.c:5116
bool RNA_struct_property_is_set(PointerRNA *ptr, const char *identifier)
Definition: rna_access.c:5301
int RNA_enum_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:5004
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, int default_value, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3783
char filepath[1024]
Definition: BKE_main.h:121
struct PropertyRNA * prop
Definition: RNA_types.h:43
PointerRNA ptr
Definition: RNA_types.h:42
int(* invoke)(struct bContext *, struct wmOperator *, const struct wmEvent *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:919
const char * name
Definition: WM_types.h:888
const char * idname
Definition: WM_types.h:890
void(* cancel)(struct bContext *, struct wmOperator *)
Definition: WM_types.h:927
struct StructRNA * srna
Definition: WM_types.h:969
const char * description
Definition: WM_types.h:893
int(* exec)(struct bContext *, struct wmOperator *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:903
struct ReportList * reports
struct PointerRNA * ptr
void WM_main_add_notifier(unsigned int type, void *reference)
void WM_event_add_fileselect(bContext *C, wmOperator *op)
void WM_report(eReportType type, const char *message)
wmOperatorType * ot
Definition: wm_files.c:3479
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)