Blender  V2.93
io_cache.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2016 Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #include "MEM_guardedalloc.h"
25 
26 #include "DNA_cachefile_types.h"
27 #include "DNA_space_types.h"
28 
29 #include "BLI_path_util.h"
30 #include "BLI_string.h"
31 
32 #include "BKE_cachefile.h"
33 #include "BKE_context.h"
34 #include "BKE_lib_id.h"
35 #include "BKE_main.h"
36 #include "BKE_report.h"
37 
38 #include "RNA_access.h"
39 
40 #include "DEG_depsgraph.h"
41 
42 #include "UI_interface.h"
43 
44 #include "WM_api.h"
45 #include "WM_types.h"
46 
47 #include "io_cache.h"
48 
49 static void cachefile_init(bContext *C, wmOperator *op)
50 {
51  PropertyPointerRNA *pprop;
52 
53  op->customdata = pprop = MEM_callocN(sizeof(PropertyPointerRNA), "OpenPropertyPointerRNA");
55 }
56 
57 static int cachefile_open_invoke(bContext *C, wmOperator *op, const wmEvent *event)
58 {
59  if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
60  char filepath[FILE_MAX];
61  Main *bmain = CTX_data_main(C);
62 
63  BLI_strncpy(filepath, BKE_main_blendfile_path(bmain), sizeof(filepath));
64  BLI_path_extension_replace(filepath, sizeof(filepath), ".abc");
65  RNA_string_set(op->ptr, "filepath", filepath);
66  }
67 
68  cachefile_init(C, op);
69 
71 
73 
74  UNUSED_VARS(event);
75 }
76 
77 static void open_cancel(bContext *UNUSED(C), wmOperator *op)
78 {
79  MEM_freeN(op->customdata);
80  op->customdata = NULL;
81 }
82 
84 {
85  if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
86  BKE_report(op->reports, RPT_ERROR, "No filename given");
87  return OPERATOR_CANCELLED;
88  }
89 
90  char filename[FILE_MAX];
91  RNA_string_get(op->ptr, "filepath", filename);
92 
93  Main *bmain = CTX_data_main(C);
94 
95  CacheFile *cache_file = BKE_libblock_alloc(bmain, ID_CF, BLI_path_basename(filename), 0);
96  BLI_strncpy(cache_file->filepath, filename, FILE_MAX);
98 
99  /* Will be set when running invoke, not exec directly. */
100  if (op->customdata != NULL) {
101  /* hook into UI */
102  PropertyPointerRNA *pprop = op->customdata;
103  if (pprop->prop) {
104  /* When creating new ID blocks, use is already 1, but RNA
105  * pointer see also increases user, so this compensates it. */
106  id_us_min(&cache_file->id);
107 
108  PointerRNA idptr;
109  RNA_id_pointer_create(&cache_file->id, &idptr);
110  RNA_property_pointer_set(&pprop->ptr, pprop->prop, idptr, NULL);
111  RNA_property_update(C, &pprop->ptr, pprop->prop);
112  }
113 
114  MEM_freeN(op->customdata);
115  }
116 
117  return OPERATOR_FINISHED;
118 }
119 
121 {
122  ot->name = "Open Cache File";
123  ot->description = "Load a cache file";
124  ot->idname = "CACHEFILE_OT_open";
125 
128  ot->cancel = open_cancel;
129 
132  FILE_BLENDER,
137 }
138 
139 /* ***************************** Reload Operator **************************** */
140 
142 {
143  CacheFile *cache_file = CTX_data_edit_cachefile(C);
144 
145  if (!cache_file) {
146  return OPERATOR_CANCELLED;
147  }
148 
150  BKE_cachefile_reload(depsgraph, cache_file);
151 
152  return OPERATOR_FINISHED;
153 }
154 
156 {
157  ot->name = "Refresh Archive";
158  ot->description = "Update objects paths list with new data from the archive";
159  ot->idname = "CACHEFILE_OT_reload";
160 
161  /* api callbacks */
163 
164  /* flags */
166 }
void BKE_cachefile_reload(struct Depsgraph *depsgraph, struct CacheFile *cache_file)
Definition: cachefile.c:261
struct Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C)
Definition: context.c:1424
struct CacheFile * CTX_data_edit_cachefile(const bContext *C)
Definition: context.c:1326
struct Main * CTX_data_main(const bContext *C)
Definition: context.c:1018
void id_us_min(struct ID *id)
Definition: lib_id.c:297
void * BKE_libblock_alloc(struct Main *bmain, short type, const char *name, const int flag) ATTR_WARN_UNUSED_RESULT
Definition: lib_id.c:1062
const char * BKE_main_blendfile_path(const struct Main *bmain) ATTR_NONNULL()
void BKE_report(ReportList *reports, ReportType type, const char *message)
Definition: report.c:104
const char * BLI_path_basename(const char *path) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT
Definition: path_util.c:1868
#define FILE_MAX
bool BLI_path_extension_replace(char *path, size_t maxlen, const char *ext) ATTR_NONNULL()
Definition: path_util.c:1571
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t maxncpy) ATTR_NONNULL()
Definition: string.c:108
#define UNUSED_VARS(...)
#define UNUSED(x)
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:51
void DEG_id_tag_update(struct ID *id, int flag)
@ ID_RECALC_COPY_ON_WRITE
Definition: DNA_ID.h:654
@ ID_CF
Definition: DNA_ID_enums.h:90
@ FILE_SORT_DEFAULT
@ FILE_BLENDER
@ FILE_TYPE_ALEMBIC
@ FILE_TYPE_FOLDER
@ FILE_OPENFILE
@ FILE_DEFAULTDISPLAY
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
@ OPERATOR_RUNNING_MODAL
Read Guarded memory(de)allocation.
#define C
Definition: RandGen.cpp:39
void UI_context_active_but_prop_get_templateID(struct bContext *C, struct PointerRNA *r_ptr, struct PropertyRNA **r_prop)
#define WM_FILESEL_RELPATH
Definition: WM_api.h:533
#define WM_FILESEL_FILEPATH
Definition: WM_api.h:537
@ OPTYPE_UNDO
Definition: WM_types.h:155
@ OPTYPE_REGISTER
Definition: WM_types.h:153
const Depsgraph * depsgraph
void CACHEFILE_OT_open(wmOperatorType *ot)
Definition: io_cache.c:120
static int cachefile_open_invoke(bContext *C, wmOperator *op, const wmEvent *event)
Definition: io_cache.c:57
static int cachefile_open_exec(bContext *C, wmOperator *op)
Definition: io_cache.c:83
static void cachefile_init(bContext *C, wmOperator *op)
Definition: io_cache.c:49
static void open_cancel(bContext *UNUSED(C), wmOperator *op)
Definition: io_cache.c:77
static int cachefile_reload_exec(bContext *C, wmOperator *UNUSED(op))
Definition: io_cache.c:141
void CACHEFILE_OT_reload(wmOperatorType *ot)
Definition: io_cache.c:155
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
Definition: rna_access.c:6550
void RNA_id_pointer_create(ID *id, PointerRNA *r_ptr)
Definition: rna_access.c:122
void RNA_property_pointer_set(PointerRNA *ptr, PropertyRNA *prop, PointerRNA ptr_value, ReportList *reports)
Definition: rna_access.c:3673
void RNA_property_update(bContext *C, PointerRNA *ptr, PropertyRNA *prop)
Definition: rna_access.c:2317
void RNA_string_get(PointerRNA *ptr, const char *name, char *value)
Definition: rna_access.c:6514
bool RNA_struct_property_is_set(PointerRNA *ptr, const char *identifier)
Definition: rna_access.c:6685
char filepath[1024]
Definition: BKE_main.h:116
struct PropertyRNA * prop
Definition: RNA_types.h:57
PointerRNA ptr
Definition: RNA_types.h:56
int(* invoke)(struct bContext *, struct wmOperator *, const struct wmEvent *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:752
const char * name
Definition: WM_types.h:721
const char * idname
Definition: WM_types.h:723
void(* cancel)(struct bContext *, struct wmOperator *)
Definition: WM_types.h:760
const char * description
Definition: WM_types.h:726
int(* exec)(struct bContext *, struct wmOperator *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:736
struct ReportList * reports
struct PointerRNA * ptr
void WM_event_add_fileselect(bContext *C, wmOperator *op)
wmOperatorType * ot
Definition: wm_files.c:3156
void WM_operator_properties_filesel(wmOperatorType *ot, int filter, short type, short action, short flag, short display, short sort)