From d1921b4f2b6bc75e988e3bb81094de1d68995790 Mon Sep 17 00:00:00 2001 From: Greg Zaal Date: Fri, 13 Oct 2023 11:51:58 +0200 Subject: [PATCH] Asset Browser: Add pre+post handlers to asset drag & drop --- source/blender/blenkernel/BKE_callbacks.h | 2 ++ .../blender/python/intern/bpy_app_handlers.cc | 3 ++ .../windowmanager/intern/wm_dragdrop.cc | 34 +++++++++++-------- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/source/blender/blenkernel/BKE_callbacks.h b/source/blender/blenkernel/BKE_callbacks.h index 4f52640d3d5..69e30ddf8d0 100644 --- a/source/blender/blenkernel/BKE_callbacks.h +++ b/source/blender/blenkernel/BKE_callbacks.h @@ -89,6 +89,8 @@ typedef enum { BKE_CB_EVT_SAVE_PRE, BKE_CB_EVT_SAVE_POST, BKE_CB_EVT_SAVE_POST_FAIL, + BKE_CB_EVT_ASSET_DROP_PRE, + BKE_CB_EVT_ASSET_DROP_POST, BKE_CB_EVT_UNDO_PRE, BKE_CB_EVT_UNDO_POST, BKE_CB_EVT_REDO_PRE, diff --git a/source/blender/python/intern/bpy_app_handlers.cc b/source/blender/python/intern/bpy_app_handlers.cc index c9a8dff2f17..ca6f19ec8ea 100644 --- a/source/blender/python/intern/bpy_app_handlers.cc +++ b/source/blender/python/intern/bpy_app_handlers.cc @@ -70,6 +70,9 @@ static PyStructSequence_Field app_cb_info_fields[] = { {"save_post", "on saving a blend file (after). " FILEPATH_SAVE_ARG}, {"save_post_fail", "on failure to save a blend file (after). " FILEPATH_SAVE_ARG}, + {"asset_drop_pre", "on dropping an asset from the asset browser (before). " FILEPATH_LOAD_ARG}, + {"asset_drop_post", "on dropping an asset from the asset browser (after). "}, + {"undo_pre", "on loading an undo step (before)"}, {"undo_post", "on loading an undo step (after)"}, {"redo_pre", "on loading a redo step (before)"}, diff --git a/source/blender/windowmanager/intern/wm_dragdrop.cc b/source/blender/windowmanager/intern/wm_dragdrop.cc index 3074ad9612b..3ce1ddd3d22 100644 --- a/source/blender/windowmanager/intern/wm_dragdrop.cc +++ b/source/blender/windowmanager/intern/wm_dragdrop.cc @@ -27,6 +27,7 @@ #include "BIF_glutil.hh" +#include "BKE_callbacks.h" #include "BKE_context.h" #include "BKE_global.h" #include "BKE_idprop.h" @@ -629,28 +630,31 @@ ID *WM_drag_asset_id_import(const bContext *C, wmDragAsset *asset_drag, const in ViewLayer *view_layer = CTX_data_view_layer(C); View3D *view3d = CTX_wm_view3d(C); + BKE_callback_exec_string(bmain, BKE_CB_EVT_ASSET_DROP_PRE, blend_path.c_str()); + + ID *id = nullptr; switch (eAssetImportMethod(asset_drag->import_method)) { case ASSET_IMPORT_LINK: - return WM_file_link_datablock(bmain, + id = WM_file_link_datablock(bmain, + scene, + view_layer, + view3d, + blend_path.c_str(), + idtype, + name, + flag | (use_relative_path ? FILE_RELPATH : 0)); + case ASSET_IMPORT_APPEND: + id = WM_file_append_datablock(bmain, scene, view_layer, view3d, blend_path.c_str(), idtype, name, - flag | (use_relative_path ? FILE_RELPATH : 0)); - case ASSET_IMPORT_APPEND: - return WM_file_append_datablock(bmain, - scene, - view_layer, - view3d, - blend_path.c_str(), - idtype, - name, - flag | BLO_LIBLINK_APPEND_RECURSIVE | - BLO_LIBLINK_APPEND_ASSET_DATA_CLEAR); + flag | BLO_LIBLINK_APPEND_RECURSIVE | + BLO_LIBLINK_APPEND_ASSET_DATA_CLEAR); case ASSET_IMPORT_APPEND_REUSE: - return WM_file_append_datablock( + id = WM_file_append_datablock( G_MAIN, scene, view_layer, @@ -662,8 +666,8 @@ ID *WM_drag_asset_id_import(const bContext *C, wmDragAsset *asset_drag, const in BLO_LIBLINK_APPEND_LOCAL_ID_REUSE | (use_relative_path ? FILE_RELPATH : 0)); } - BLI_assert_unreachable(); - return nullptr; + BKE_callback_exec_id(bmain, id, BKE_CB_EVT_ASSET_DROP_POST); + return id; } bool WM_drag_asset_will_import_linked(const wmDrag *drag) -- 2.30.2