diff options
Diffstat (limited to 'source/blender/editors/io/io_cache.c')
-rw-r--r-- | source/blender/editors/io/io_cache.c | 168 |
1 files changed, 166 insertions, 2 deletions
diff --git a/source/blender/editors/io/io_cache.c b/source/blender/editors/io/io_cache.c index bf20c1f6438..4f71a804986 100644 --- a/source/blender/editors/io/io_cache.c +++ b/source/blender/editors/io/io_cache.c @@ -26,6 +26,7 @@ #include "DNA_cachefile_types.h" #include "DNA_space_types.h" +#include "BLI_listbase.h" #include "BLI_path_util.h" #include "BLI_string.h" @@ -36,6 +37,7 @@ #include "BKE_report.h" #include "RNA_access.h" +#include "RNA_define.h" #include "DEG_depsgraph.h" @@ -46,6 +48,12 @@ #include "io_cache.h" +static void reload_cachefile(bContext *C, CacheFile *cache_file) +{ + Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C); + BKE_cachefile_reload(depsgraph, cache_file); +} + static void cachefile_init(bContext *C, wmOperator *op) { PropertyPointerRNA *pprop; @@ -146,8 +154,7 @@ static int cachefile_reload_exec(bContext *C, wmOperator *UNUSED(op)) return OPERATOR_CANCELLED; } - Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C); - BKE_cachefile_reload(depsgraph, cache_file); + reload_cachefile(C, cache_file); return OPERATOR_FINISHED; } @@ -164,3 +171,160 @@ void CACHEFILE_OT_reload(wmOperatorType *ot) /* flags */ ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; } + +/* ***************************** Add Layer Operator **************************** */ + +static int cachefile_layer_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); + } + + /* There is no more CacheFile set when returning from the file selector, so store it here. */ + op->customdata = CTX_data_edit_cachefile(C); + + WM_event_add_fileselect(C, op); + + return OPERATOR_RUNNING_MODAL; + + UNUSED_VARS(event); +} + +static int cachefile_layer_add_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; + } + + CacheFile *cache_file = op->customdata; + + if (!cache_file) { + return OPERATOR_CANCELLED; + } + + char filename[FILE_MAX]; + RNA_string_get(op->ptr, "filepath", filename); + + CacheFileLayer *layer = BKE_cachefile_add_layer(cache_file, filename); + + if (!layer) { + WM_report(RPT_ERROR, "Could not add a layer to the cache file"); + return OPERATOR_CANCELLED; + } + + reload_cachefile(C, cache_file); + WM_main_add_notifier(NC_OBJECT | ND_DRAW, NULL); + return OPERATOR_FINISHED; +} + +void CACHEFILE_OT_layer_add(wmOperatorType *ot) +{ + ot->name = "Add layer"; + ot->description = "Add an override layer to the archive"; + ot->idname = "CACHEFILE_OT_layer_add"; + + /* api callbacks */ + ot->invoke = cachefile_layer_open_invoke; + ot->exec = cachefile_layer_add_exec; + + WM_operator_properties_filesel(ot, + FILE_TYPE_ALEMBIC | FILE_TYPE_FOLDER, + FILE_BLENDER, + FILE_OPENFILE, + WM_FILESEL_FILEPATH | WM_FILESEL_RELPATH, + FILE_DEFAULTDISPLAY, + FILE_SORT_DEFAULT); +} + +/* ***************************** Remove Layer Operator **************************** */ + +static int cachefile_layer_remove_exec(bContext *C, wmOperator *UNUSED(op)) +{ + CacheFile *cache_file = CTX_data_edit_cachefile(C); + + if (!cache_file) { + return OPERATOR_CANCELLED; + } + + CacheFileLayer *layer = BKE_cachefile_get_active_layer(cache_file); + BKE_cachefile_remove_layer(cache_file, layer); + + reload_cachefile(C, cache_file); + WM_main_add_notifier(NC_OBJECT | ND_DRAW, NULL); + return OPERATOR_FINISHED; +} + +void CACHEFILE_OT_layer_remove(wmOperatorType *ot) +{ + ot->name = "Add layer"; + ot->description = "Remove an override layer to the archive"; + ot->idname = "CACHEFILE_OT_layer_remove"; + + /* api callbacks */ + ot->exec = cachefile_layer_remove_exec; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; +} + +/* ***************************** Move Layer Operator **************************** */ + +static int cachefile_layer_move_exec(bContext *C, wmOperator *op) +{ + CacheFile *cache_file = CTX_data_edit_cachefile(C); + + if (!cache_file) { + return OPERATOR_CANCELLED; + } + + CacheFileLayer *layer = BKE_cachefile_get_active_layer(cache_file); + + if (!layer) { + return OPERATOR_CANCELLED; + } + + const int dir = RNA_enum_get(op->ptr, "direction"); + + if (BLI_listbase_link_move(&cache_file->layers, layer, dir)) { + cache_file->active_layer = BLI_findindex(&cache_file->layers, layer) + 1; + /* Only reload if something moved, might be expensive. */ + reload_cachefile(C, cache_file); + WM_main_add_notifier(NC_OBJECT | ND_DRAW, NULL); + } + + return OPERATOR_FINISHED; +} + +void CACHEFILE_OT_layer_move(wmOperatorType *ot) +{ + static const EnumPropertyItem layer_slot_move[] = { + {-1, "UP", 0, "Up", ""}, + {1, "DOWN", 0, "Down", ""}, + {0, NULL, 0, NULL, NULL}, + }; + + ot->name = "Move layer"; + ot->description = + "Move layer in the list, layers further down the list will overwrite data from the layers " + "higher up"; + ot->idname = "CACHEFILE_OT_layer_move"; + + /* api callbacks */ + ot->exec = cachefile_layer_move_exec; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + + RNA_def_enum(ot->srna, + "direction", + layer_slot_move, + 0, + "Direction", + "Direction to move the active vertex group towards"); +} |