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]: 296d05060d
This commit is contained in:
Campbell Barton
2024-08-06 21:18:40 +10:00
parent f8e06a5f5f
commit ca199ba13f
3 changed files with 21 additions and 1 deletions

View File

@@ -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);

View File

@@ -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) {

View File

@@ -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;