From 4b2a4341a76c0dbd2a84cde8d892b48b28ffddb4 Mon Sep 17 00:00:00 2001 From: Julian Eisel Date: Fri, 10 Jan 2025 18:09:30 +0100 Subject: [PATCH] Fix #130611: Grease Pencil: Airbrush affects material of other brushes Don't change the object's active material when adding a material slot. This active material will be used by brushes with no pinned material, while the added slot is meant for the pinned material. The if-statement in `BKE_grease_pencil_object_material_ensure_from_brush()` would change the active unpinned material, even though it was only meant to handle the pinned material. This was not a new issue, it was just unlikely to run into it. Before brush assets were introduced this if-statement would not be executed when activating any of the default brushes. Afterwards it would be, because the material was newly linked (because the brush asset was linked) and so there was no material slot created for it yet. Pull Request: https://projects.blender.org/blender/blender/pulls/132865 --- source/blender/blenkernel/BKE_material.hh | 6 +++++- source/blender/blenkernel/intern/grease_pencil.cc | 6 +++++- source/blender/blenkernel/intern/material.cc | 6 ++++-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/source/blender/blenkernel/BKE_material.hh b/source/blender/blenkernel/BKE_material.hh index 01866dff9a4..34be7392e74 100644 --- a/source/blender/blenkernel/BKE_material.hh +++ b/source/blender/blenkernel/BKE_material.hh @@ -95,7 +95,11 @@ void BKE_object_material_array_assign( Main *bmain, Object *ob, Material ***matar, int totcol, bool to_object_only); short BKE_object_material_slot_find_index(Object *ob, Material *ma); -bool BKE_object_material_slot_add(Main *bmain, Object *ob); +/** + * \param set_active: Set the newly added slot as active material slot of the object. Usually that + * is wanted when adding a material slot, so it's the default. + */ +bool BKE_object_material_slot_add(Main *bmain, Object *ob, bool set_active = true); bool BKE_object_material_slot_remove(Main *bmain, Object *ob); bool BKE_object_material_slot_used(Object *object, short actcol); diff --git a/source/blender/blenkernel/intern/grease_pencil.cc b/source/blender/blenkernel/intern/grease_pencil.cc index a19074ff59b..3df78387948 100644 --- a/source/blender/blenkernel/intern/grease_pencil.cc +++ b/source/blender/blenkernel/intern/grease_pencil.cc @@ -2441,7 +2441,11 @@ Material *BKE_grease_pencil_object_material_ensure_from_brush(Main *bmain, /* check if the material is already on object material slots and add it if missing */ if (ma && BKE_object_material_index_get(ob, ma) < 0) { - BKE_object_material_slot_add(bmain, ob); + /* The object's active material is what's used for the unpinned material. Do not touch it + * while using a pinned material. */ + const bool change_active_material = false; + + BKE_object_material_slot_add(bmain, ob, change_active_material); BKE_object_material_assign(bmain, ob, ma, ob->totcol, BKE_MAT_ASSIGN_USERPREF); } diff --git a/source/blender/blenkernel/intern/material.cc b/source/blender/blenkernel/intern/material.cc index 3250f472082..bf976f4a53a 100644 --- a/source/blender/blenkernel/intern/material.cc +++ b/source/blender/blenkernel/intern/material.cc @@ -1322,7 +1322,7 @@ short BKE_object_material_slot_find_index(Object *ob, Material *ma) return 0; } -bool BKE_object_material_slot_add(Main *bmain, Object *ob) +bool BKE_object_material_slot_add(Main *bmain, Object *ob, const bool set_active) { if (ob == nullptr) { return false; @@ -1332,7 +1332,9 @@ bool BKE_object_material_slot_add(Main *bmain, Object *ob) } BKE_object_material_assign(bmain, ob, nullptr, ob->totcol + 1, BKE_MAT_ASSIGN_USERPREF); - ob->actcol = ob->totcol; + if (set_active) { + ob->actcol = ob->totcol; + } return true; }