From ca199ba13f7f400fb42705fe01f2bdb803d33ceb Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Tue, 6 Aug 2024 21:18:40 +1000 Subject: [PATCH] Fix #125353: Select by active material fails to select the active slot When (de)selecting the active material, use the active slot falling back to the first-used slot (for non-active objects). Resolve regression in [0]. Ref !125948 [0]: 296d05060d22714a5fe46af7206aa7066982e9c8 --- source/blender/blenkernel/BKE_material.h | 8 ++++++++ source/blender/blenkernel/intern/material.cc | 11 +++++++++++ source/blender/editors/render/render_shading.cc | 3 ++- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/source/blender/blenkernel/BKE_material.h b/source/blender/blenkernel/BKE_material.h index 6ba7a9cc84a..c0600656784 100644 --- a/source/blender/blenkernel/BKE_material.h +++ b/source/blender/blenkernel/BKE_material.h @@ -115,6 +115,14 @@ bool BKE_object_material_slot_remove(struct Main *bmain, struct Object *ob); bool BKE_object_material_slot_used(struct Object *object, short actcol); int BKE_object_material_index_get(Object *ob, const Material *ma); +/** + * A version of #BKE_object_material_index_get that takes an index to test first. + * + * \param hint_index: When this index is in a valid range, test it first. + * Useful when an active-index is preferred but may not match the material. + */ +int BKE_object_material_index_get_with_hint(Object *ob, const Material *ma, int hint_index); + int BKE_object_material_ensure(Main *bmain, Object *ob, Material *material); struct Material *BKE_gpencil_material(struct Object *ob, short act); diff --git a/source/blender/blenkernel/intern/material.cc b/source/blender/blenkernel/intern/material.cc index d9c06ebeaa5..52faf2eabc5 100644 --- a/source/blender/blenkernel/intern/material.cc +++ b/source/blender/blenkernel/intern/material.cc @@ -844,6 +844,17 @@ int BKE_object_material_index_get(Object *ob, const Material *ma) return -1; } +int BKE_object_material_index_get_with_hint(Object *ob, const Material *ma, const int hint_index) +{ + short *totcol = BKE_object_material_len_p(ob); + if ((hint_index >= 0) && (hint_index < *totcol)) { + if (ma == BKE_object_material_get(ob, hint_index + 1)) { + return hint_index; + } + } + return BKE_object_material_index_get(ob, ma); +} + int BKE_object_material_ensure(Main *bmain, Object *ob, Material *material) { if (!material) { diff --git a/source/blender/editors/render/render_shading.cc b/source/blender/editors/render/render_shading.cc index 4b7e9afae70..bb74cfd29d7 100644 --- a/source/blender/editors/render/render_shading.cc +++ b/source/blender/editors/render/render_shading.cc @@ -397,7 +397,8 @@ static int material_slot_de_select(bContext *C, bool select) continue; } - short mat_nr_active = BKE_object_material_index_get(ob, mat_active); + const short mat_nr_active = BKE_object_material_index_get_with_hint( + ob, mat_active, obact ? obact->actcol - 1 : -1); if (mat_nr_active == -1) { continue;