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;