Assets: Add operator & button to regenerate the automatic preview

This makes it possible to trigger a refresh of the data-block preview,
available next to the preview in the Asset Browser sidebar. The previews get
easily outdated and automatically refreshing it all the time is not an option
because it would be a consistently running, quite expensive process. So a
button to cause a refresh should be reasonable.

This button can also be used to switch back from a custom preview to a
generated one. Although that may not be clear, and we should probably think of
a way to explain that better.

Addresses T82719.
This commit is contained in:
Julian Eisel 2020-12-16 11:44:22 +01:00
parent c7a500e3a0
commit 58d818f8be
Notes: blender-bot 2023-02-14 09:34:18 +01:00
Referenced by issue #82719, Add Button to Refresh the Automatic Asset Preview
4 changed files with 39 additions and 2 deletions

View File

@ -609,6 +609,8 @@ class ASSETBROWSER_PT_metadata_preview(asset_utils.AssetMetaDataPanel, Panel):
if bpy.ops.ed.lib_id_load_custom_preview.poll():
col = row.column(align=True)
col.operator("ed.lib_id_load_custom_preview", icon='FILEBROWSER', text="")
col.separator()
col.operator("ed.lib_id_generate_preview", icon='FILE_REFRESH', text="")
class ASSETBROWSER_PT_metadata_details(asset_utils.AssetMetaDataPanel, Panel):

View File

@ -54,6 +54,7 @@ void ED_spacedata_id_remap(struct ScrArea *area,
void ED_OT_flush_edits(struct wmOperatorType *ot);
void ED_OT_lib_id_load_custom_preview(struct wmOperatorType *ot);
void ED_OT_lib_id_generate_preview(struct wmOperatorType *ot);
/* ************** XXX OLD CRUFT WARNING ************* */

View File

@ -5511,6 +5511,7 @@ void ED_operatortypes_screen(void)
WM_operatortype_append(ED_OT_flush_edits);
WM_operatortype_append(ED_OT_lib_id_load_custom_preview);
WM_operatortype_append(ED_OT_lib_id_generate_preview);
}
/** \} */

View File

@ -67,6 +67,7 @@
#include "ED_object.h"
#include "ED_outliner.h"
#include "ED_paint.h"
#include "ED_render.h"
#include "ED_space_api.h"
#include "ED_util.h"
@ -505,7 +506,7 @@ void ED_OT_flush_edits(wmOperatorType *ot)
ot->flag = OPTYPE_INTERNAL;
}
static bool lib_id_load_custom_preview_poll(bContext *C)
static bool lib_id_preview_editing_poll(bContext *C)
{
const PointerRNA idptr = CTX_data_pointer_get(C, "id");
BLI_assert(!idptr.data || RNA_struct_is_ID(idptr.type));
@ -558,7 +559,7 @@ void ED_OT_lib_id_load_custom_preview(wmOperatorType *ot)
ot->idname = "ED_OT_lib_id_load_custom_preview";
/* api callbacks */
ot->poll = lib_id_load_custom_preview_poll;
ot->poll = lib_id_preview_editing_poll;
ot->exec = lib_id_load_custom_preview_exec;
ot->invoke = WM_operator_filesel;
@ -573,3 +574,35 @@ void ED_OT_lib_id_load_custom_preview(wmOperatorType *ot)
FILE_DEFAULTDISPLAY,
FILE_SORT_DEFAULT);
}
static int lib_id_generate_preview_exec(bContext *C, wmOperator *UNUSED(op))
{
PointerRNA idptr = CTX_data_pointer_get(C, "id");
ID *id = idptr.data;
ED_preview_kill_jobs(CTX_wm_manager(C), CTX_data_main(C));
PreviewImage *preview = BKE_previewimg_id_get(id);
if (preview) {
BKE_previewimg_clear(preview);
}
UI_icon_render_id(C, NULL, id, true, true);
WM_event_add_notifier(C, NC_ASSET, NULL);
return OPERATOR_FINISHED;
}
void ED_OT_lib_id_generate_preview(wmOperatorType *ot)
{
ot->name = "Generate Preview";
ot->description = "Create an automatic preview for the selected data-block";
ot->idname = "ED_OT_lib_id_generate_preview";
/* api callbacks */
ot->poll = lib_id_preview_editing_poll;
ot->exec = lib_id_generate_preview_exec;
/* flags */
ot->flag = OPTYPE_INTERNAL;
}