1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2016 Blender Foundation.
* All rights reserved.
*/
#include "MEM_guardedalloc.h"
#include "DNA_cachefile_types.h"
#include "DNA_space_types.h"
#include "BLI_listbase.h"
#include "BLI_path_util.h"
#include "BLI_string.h"
#include "BKE_cachefile.h"
#include "BKE_context.h"
#include "BKE_library.h"
#include "BKE_main.h"
#include "BKE_report.h"
#include "RNA_access.h"
#include "UI_interface.h"
#include "WM_api.h"
#include "WM_types.h"
#include "io_cache.h"
static void cachefile_init(bContext *C, wmOperator *op)
{
PropertyPointerRNA *pprop;
op->customdata = pprop = MEM_callocN(sizeof(PropertyPointerRNA), "OpenPropertyPointerRNA");
UI_context_active_but_prop_get_templateID(C, &pprop->ptr, &pprop->prop);
}
static int cachefile_open_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
char filepath[FILE_MAX];
Main *bmain = CTX_data_main(C);
BLI_strncpy(filepath, BKE_main_blendfile_path(bmain), sizeof(filepath));
BLI_path_extension_replace(filepath, sizeof(filepath), ".abc");
RNA_string_set(op->ptr, "filepath", filepath);
}
cachefile_init(C, op);
WM_event_add_fileselect(C, op);
return OPERATOR_RUNNING_MODAL;
UNUSED_VARS(event);
}
static void open_cancel(bContext *UNUSED(C), wmOperator *op)
{
MEM_freeN(op->customdata);
op->customdata = NULL;
}
static int cachefile_open_exec(bContext *C, wmOperator *op)
{
if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
BKE_report(op->reports, RPT_ERROR, "No filename given");
return OPERATOR_CANCELLED;
}
char filename[FILE_MAX];
RNA_string_get(op->ptr, "filepath", filename);
Main *bmain = CTX_data_main(C);
CacheFile *cache_file = BKE_libblock_alloc(bmain, ID_CF, BLI_path_basename(filename), 0);
BLI_strncpy(cache_file->filepath, filename, FILE_MAX);
BKE_cachefile_reload(bmain, cache_file);
/* Will be set when running invoke, not exec directly. */
if (op->customdata != NULL) {
/* hook into UI */
PropertyPointerRNA *pprop = op->customdata;
if (pprop->prop) {
/* when creating new ID blocks, use is already 1, but RNA
* pointer se also increases user, so this compensates it */
id_us_min(&cache_file->id);
PointerRNA idptr;
RNA_id_pointer_create(&cache_file->id, &idptr);
RNA_property_pointer_set(&pprop->ptr, pprop->prop, idptr);
RNA_property_update(C, &pprop->ptr, pprop->prop);
}
MEM_freeN(op->customdata);
}
return OPERATOR_FINISHED;
}
void CACHEFILE_OT_open(wmOperatorType *ot)
{
ot->name = "Open Cache File";
ot->description = "Load a cache file";
ot->idname = "CACHEFILE_OT_open";
ot->invoke = cachefile_open_invoke;
ot->exec = cachefile_open_exec;
ot->cancel = open_cancel;
WM_operator_properties_filesel(ot, FILE_TYPE_ALEMBIC | FILE_TYPE_FOLDER,
FILE_BLENDER, FILE_SAVE, WM_FILESEL_FILEPATH,
FILE_DEFAULTDISPLAY, FILE_SORT_ALPHA);
}
/* ***************************** Reload Operator **************************** */
static int cachefile_reload_exec(bContext *C, wmOperator *op)
{
CacheFile *cache_file = CTX_data_edit_cachefile(C);
if (!cache_file) {
return OPERATOR_CANCELLED;
}
Main *bmain = CTX_data_main(C);
BLI_freelistN(&cache_file->object_paths);
BKE_cachefile_reload(bmain, cache_file);
return OPERATOR_FINISHED;
UNUSED_VARS(op);
}
void CACHEFILE_OT_reload(wmOperatorType *ot)
{
ot->name = "Refresh Archive";
ot->description = "Update objects paths list with new data from the archive";
ot->idname = "CACHEFILE_OT_reload";
/* api callbacks */
ot->exec = cachefile_reload_exec;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
|