diff --git a/source/blender/blenkernel/BKE_paint.hh b/source/blender/blenkernel/BKE_paint.hh index 3e6d6857715..788c4b78bd4 100644 --- a/source/blender/blenkernel/BKE_paint.hh +++ b/source/blender/blenkernel/BKE_paint.hh @@ -207,7 +207,7 @@ void BKE_paint_brush_set(Paint *paint, Brush *br); * Set the active brush of given paint struct, and store the weak asset reference to it. * \note Takes ownership of the given `weak_asset_reference`. */ -void BKE_paint_brush_asset_set(Paint *paint, +bool BKE_paint_brush_asset_set(Paint *paint, Brush *brush, AssetWeakReference *weak_asset_reference); diff --git a/source/blender/blenkernel/intern/paint.cc b/source/blender/blenkernel/intern/paint.cc index 5d5db90ed4c..500c7636a09 100644 --- a/source/blender/blenkernel/intern/paint.cc +++ b/source/blender/blenkernel/intern/paint.cc @@ -691,10 +691,17 @@ static void paint_brush_asset_update(Paint &paint, paint.brush_asset_reference = brush_asset_reference; } -void BKE_paint_brush_asset_set(Paint *paint, +bool BKE_paint_brush_asset_set(Paint *paint, Brush *brush, AssetWeakReference *weak_asset_reference) { + /* Should not happen for users if brush assets are properly filtered by mode, but still protect + * against it in case of invalid API usage. */ + if (paint->runtime.ob_mode != brush->ob_mode) { + BKE_asset_weak_reference_free(&weak_asset_reference); + return false; + } + BKE_paint_brush_set(paint, brush); paint_brush_asset_update(*paint, brush, weak_asset_reference); } diff --git a/source/blender/editors/sculpt_paint/curves_sculpt_ops.cc b/source/blender/editors/sculpt_paint/curves_sculpt_ops.cc index dde42e006a7..f597dd89014 100644 --- a/source/blender/editors/sculpt_paint/curves_sculpt_ops.cc +++ b/source/blender/editors/sculpt_paint/curves_sculpt_ops.cc @@ -18,6 +18,7 @@ #include "BKE_modifier.hh" #include "BKE_object.hh" #include "BKE_paint.hh" +#include "BKE_report.h" #include "WM_api.hh" #include "WM_message.hh" @@ -1165,7 +1166,7 @@ static void SCULPT_CURVES_OT_min_distance_edit(wmOperatorType *ot) /* -------------------------------------------------------------------- */ -static int brush_asset_select_exec(bContext *C, wmOperator * /*op*/) +static int brush_asset_select_exec(bContext *C, wmOperator *op) { /* This operator currently covers both cases: the file/asset browser file list and the asset list * used for the asset-view template. Once the asset list design is used by the Asset Browser, @@ -1179,8 +1180,17 @@ static int brush_asset_select_exec(bContext *C, wmOperator * /*op*/) Brush *brush = BKE_brush_asset_runtime_ensure(CTX_data_main(C), brush_asset_reference); ToolSettings *tool_settings = CTX_data_tool_settings(C); + /* Either takes ownership of the brush_asset_reference, or frees it. */ - BKE_paint_brush_asset_set(&tool_settings->curves_sculpt->paint, brush, brush_asset_reference); + if (!BKE_paint_brush_asset_set( + &tool_settings->curves_sculpt->paint, brush, brush_asset_reference)) + { + /* Note brush datablock was still added, so was not a no-op. */ + BKE_report(op->reports, RPT_WARNING, "Unable to select brush, wrong object mode"); + return OPERATOR_FINISHED; + } + + WM_main_add_notifier(NC_SCENE | ND_TOOLSETTINGS, nullptr); return OPERATOR_FINISHED; }