Fix #126872: Cannot assign custom preview to node asset

Caused by 03ae57dd8b
Custom preview operator uses `lib_id_preview_editing_poll`. To enable
this operator in UI, move the new condition to `lib_id_generate_preview_poll`
poll function so adding custom preview to unsupported type would
still be possible.

Co-authored-by: Julian Eisel

Pull Request: https://projects.blender.org/blender/blender/pulls/126874
This commit is contained in:
Pratik Borhade
2024-08-31 13:14:24 +02:00
committed by Pratik Borhade
parent d31c633f4c
commit de487a6712
3 changed files with 33 additions and 19 deletions

View File

@@ -75,7 +75,7 @@ void ED_preview_free_dbase();
/**
* Check if \a id is supported by the automatic preview render.
*/
bool ED_preview_id_is_supported(const ID *id);
bool ED_preview_id_is_supported(const ID *id, const char **r_disabled_hint = nullptr);
void ED_preview_set_visibility(Main *pr_main,
Scene *scene,

View File

@@ -27,6 +27,8 @@
#include "BLI_time.h"
#include "BLI_utildefines.h"
#include "BLT_translation.hh"
#include "BLO_readfile.hh"
#include "DNA_brush_types.h"
@@ -1892,22 +1894,37 @@ static void icon_preview_free(void *customdata)
MEM_freeN(ip);
}
bool ED_preview_id_is_supported(const ID *id)
bool ED_preview_id_is_supported(const ID *id, const char **r_disabled_hint)
{
if (id == nullptr) {
return false;
}
if (GS(id->name) == ID_NT) {
/* Node groups don't support standard preview generation. */
return false;
/* Get both the result and the "potential" disabled hint. After that we can decide if the
* disabled hint needs to be returned to the caller. */
const auto [result, disabled_hint] = [id]() -> std::pair<bool, const char *> {
switch (GS(id->name)) {
case ID_NT:
return {false, RPT_("Node groups do not support automatic previews")};
case ID_OB:
return {object_preview_is_type_supported((const Object *)id),
RPT_("Object type does not support automatic previews")};
case ID_GR:
return {
collection_preview_contains_geometry_recursive((const Collection *)id),
RPT_("Collection does not contain object types that can be rendered for the automatic "
"preview")};
default:
return {BKE_previewimg_id_get_p(id) != nullptr,
RPT_("Data-block type does not support automatic previews")};
}
}();
if (result == false && disabled_hint && r_disabled_hint) {
*r_disabled_hint = disabled_hint;
}
if (GS(id->name) == ID_OB) {
return object_preview_is_type_supported((const Object *)id);
}
if (GS(id->name) == ID_GR) {
return collection_preview_contains_geometry_recursive((const Collection *)id);
}
return BKE_previewimg_id_get_p(id) != nullptr;
return result;
}
void ED_preview_icon_render(

View File

@@ -60,10 +60,6 @@ static bool lib_id_preview_editing_poll(bContext *C)
CTX_wm_operator_poll_msg_set(C, "Data-block does not support previews");
return false;
}
if (!ED_preview_id_is_supported(id)) {
CTX_wm_operator_poll_msg_set(C, "Object type does not support previews");
return false;
}
return true;
}
@@ -147,8 +143,9 @@ static bool lib_id_generate_preview_poll(bContext *C)
const PointerRNA idptr = CTX_data_pointer_get(C, "id");
const ID *id = (ID *)idptr.data;
if (GS(id->name) == ID_NT) {
CTX_wm_operator_poll_msg_set(C, "Can't generate automatic preview for node group");
const char *disabled_hint = nullptr;
if (!ED_preview_id_is_supported(id, &disabled_hint)) {
CTX_wm_operator_poll_msg_set(C, disabled_hint);
return false;
}
@@ -192,7 +189,7 @@ static void ED_OT_lib_id_generate_preview(wmOperatorType *ot)
static bool lib_id_generate_preview_from_object_poll(bContext *C)
{
if (!lib_id_preview_editing_poll(C)) {
if (!lib_id_generate_preview_poll(C)) {
return false;
}
if (CTX_data_active_object(C) == nullptr) {