From 1602658461d5c2107ba41a0120ae5fcb810227b9 Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Fri, 7 Feb 2025 12:31:26 +0100 Subject: [PATCH] Refactor: Simplify grease pencil brush material API surface To make changes in the following commit more clear. Pull Request: https://projects.blender.org/blender/blender/pulls/134226 --- .../blender/blenkernel/BKE_grease_pencil.hh | 11 +-- .../blenkernel/intern/grease_pencil.cc | 83 ++++++++----------- .../intern/grease_pencil_primitive.cc | 2 +- .../sculpt_paint/grease_pencil_draw_ops.cc | 3 +- .../sculpt_paint/grease_pencil_paint.cc | 11 +-- 5 files changed, 44 insertions(+), 66 deletions(-) diff --git a/source/blender/blenkernel/BKE_grease_pencil.hh b/source/blender/blenkernel/BKE_grease_pencil.hh index a5e0d87ef45..ffa5a800708 100644 --- a/source/blender/blenkernel/BKE_grease_pencil.hh +++ b/source/blender/blenkernel/BKE_grease_pencil.hh @@ -1124,18 +1124,15 @@ Material *BKE_grease_pencil_object_material_ensure_by_name(Main *bmain, Object *ob, const char *name, int *r_index); -Material *BKE_grease_pencil_brush_material_get(Brush *brush); Material *BKE_grease_pencil_object_material_ensure_from_brush(Main *bmain, Object *ob, Brush *brush); -Material *BKE_grease_pencil_object_material_ensure_from_active_input_brush(Main *bmain, - Object *ob, - Brush *brush); -Material *BKE_grease_pencil_object_material_ensure_from_active_input_material(Object *ob); -Material *BKE_grease_pencil_object_material_ensure_active(Object *ob); +Material *BKE_grease_pencil_object_material_alt_ensure_from_brush(Main *bmain, + Object *ob, + Brush *brush); void BKE_grease_pencil_material_remap(GreasePencil *grease_pencil, const uint *remap, int totcol); void BKE_grease_pencil_material_index_remove(GreasePencil *grease_pencil, int index); +bool BKE_grease_pencil_material_index_used(GreasePencil *grease_pencil, int index); bool BKE_grease_pencil_references_cyclic_check(const GreasePencil *id_reference, const GreasePencil *grease_pencil); -bool BKE_grease_pencil_material_index_used(GreasePencil *grease_pencil, int index); diff --git a/source/blender/blenkernel/intern/grease_pencil.cc b/source/blender/blenkernel/intern/grease_pencil.cc index 2c196891c29..2187b2053d8 100644 --- a/source/blender/blenkernel/intern/grease_pencil.cc +++ b/source/blender/blenkernel/intern/grease_pencil.cc @@ -2437,11 +2437,10 @@ Material *BKE_grease_pencil_object_material_new(Main *bmain, Material *BKE_grease_pencil_object_material_from_brush_get(Object *ob, Brush *brush) { - if ((brush) && (brush->gpencil_settings) && + if (brush && brush->gpencil_settings && (brush->gpencil_settings->flag & GP_BRUSH_MATERIAL_PINNED)) { - Material *ma = BKE_grease_pencil_brush_material_get(brush); - return ma; + return brush->gpencil_settings->material; } return BKE_object_material_get(ob, ob->actcol); @@ -2460,73 +2459,61 @@ Material *BKE_grease_pencil_object_material_ensure_by_name(Main *bmain, return BKE_grease_pencil_object_material_new(bmain, ob, name, r_index); } -Material *BKE_grease_pencil_brush_material_get(Brush *brush) +static Material *grease_pencil_object_material_ensure_from_brush_pinned(Main *bmain, + Object *ob, + Brush *brush) { - if (brush == nullptr) { - return nullptr; + Material *ma = (brush->gpencil_settings) ? brush->gpencil_settings->material : nullptr; + + /* 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) { + /* 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); } - if (brush->gpencil_settings == nullptr) { - return nullptr; - } - return brush->gpencil_settings->material; + + return ma; } Material *BKE_grease_pencil_object_material_ensure_from_brush(Main *bmain, Object *ob, Brush *brush) { - if (brush->gpencil_settings->flag & GP_BRUSH_MATERIAL_PINNED) { - Material *ma = BKE_grease_pencil_brush_material_get(brush); - - /* 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) { - /* 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); + /* Use pinned material. */ + if (brush && brush->gpencil_settings && + (brush->gpencil_settings->flag & GP_BRUSH_MATERIAL_PINNED)) + { + if (Material *ma = grease_pencil_object_material_ensure_from_brush_pinned(bmain, ob, brush)) { + return ma; } - return ma; - } - - /* Use the active material instead. */ - return BKE_object_material_get(ob, ob->actcol); -} - -Material *BKE_grease_pencil_object_material_ensure_from_active_input_brush(Main *bmain, - Object *ob, - Brush *brush) -{ - if (brush == nullptr) { - return BKE_grease_pencil_object_material_ensure_from_active_input_material(ob); - } - if (Material *ma = BKE_grease_pencil_object_material_ensure_from_brush(bmain, ob, brush)) { - return ma; - } - if (brush->gpencil_settings->flag & GP_BRUSH_MATERIAL_PINNED) { /* It is easier to just unpin a null material, instead of setting a new one. */ brush->gpencil_settings->flag &= ~GP_BRUSH_MATERIAL_PINNED; } - return BKE_grease_pencil_object_material_ensure_from_active_input_material(ob); -} -Material *BKE_grease_pencil_object_material_ensure_from_active_input_material(Object *ob) -{ + /* Use the active material. */ if (Material *ma = BKE_object_material_get(ob, ob->actcol)) { return ma; } + + /* Fall back to default material. */ return BKE_material_default_gpencil(); } -Material *BKE_grease_pencil_object_material_ensure_active(Object *ob) +Material *BKE_grease_pencil_object_material_alt_ensure_from_brush(Main *bmain, + Object *ob, + Brush *brush) { - Material *ma = BKE_grease_pencil_object_material_ensure_from_active_input_material(ob); - if (ma->gp_style == nullptr) { - BKE_gpencil_material_attr_init(ma); + Material *material_alt = (brush->gpencil_settings) ? brush->gpencil_settings->material_alt : + nullptr; + if (material_alt && BKE_object_material_slot_find_index(ob, material_alt) != -1) { + return material_alt; } - return ma; + + return BKE_grease_pencil_object_material_ensure_from_brush(bmain, ob, brush); } void BKE_grease_pencil_material_remap(GreasePencil *grease_pencil, const uint *remap, int totcol) diff --git a/source/blender/editors/grease_pencil/intern/grease_pencil_primitive.cc b/source/blender/editors/grease_pencil/intern/grease_pencil_primitive.cc index dc328fbbd92..35ea064aa36 100644 --- a/source/blender/editors/grease_pencil/intern/grease_pencil_primitive.cc +++ b/source/blender/editors/grease_pencil/intern/grease_pencil_primitive.cc @@ -735,7 +735,7 @@ static int grease_pencil_primitive_invoke(bContext *C, wmOperator *op, const wmE BKE_curvemapping_init(ts->gp_sculpt.cur_primitive); } - Material *material = BKE_grease_pencil_object_material_ensure_from_active_input_brush( + Material *material = BKE_grease_pencil_object_material_ensure_from_brush( CTX_data_main(C), vc.obact, ptd.brush); ptd.material_index = BKE_object_material_index_get(vc.obact, material); ptd.use_fill = (material->gp_style->flag & GP_MATERIAL_FILL_SHOW) != 0; diff --git a/source/blender/editors/sculpt_paint/grease_pencil_draw_ops.cc b/source/blender/editors/sculpt_paint/grease_pencil_draw_ops.cc index dae7f5b857c..1df01acbb15 100644 --- a/source/blender/editors/sculpt_paint/grease_pencil_draw_ops.cc +++ b/source/blender/editors/sculpt_paint/grease_pencil_draw_ops.cc @@ -1491,8 +1491,7 @@ static bool grease_pencil_fill_init(bContext &C, wmOperator &op) BKE_curvemapping_init(brush.gpencil_settings->curve_rand_saturation); BKE_curvemapping_init(brush.gpencil_settings->curve_rand_value); - Material *material = BKE_grease_pencil_object_material_ensure_from_active_input_brush( - &bmain, &ob, &brush); + Material *material = BKE_grease_pencil_object_material_ensure_from_brush(&bmain, &ob, &brush); const int material_index = BKE_object_material_index_get(&ob, material); const bool invert = RNA_boolean_get(op.ptr, "invert"); diff --git a/source/blender/editors/sculpt_paint/grease_pencil_paint.cc b/source/blender/editors/sculpt_paint/grease_pencil_paint.cc index e97017e3199..59d190bba9d 100644 --- a/source/blender/editors/sculpt_paint/grease_pencil_paint.cc +++ b/source/blender/editors/sculpt_paint/grease_pencil_paint.cc @@ -1243,7 +1243,7 @@ void PaintOperation::on_stroke_begin(const bContext &C, const InputSample &start stroke_random_val_factor_ = rng_.get_float() * 2.0f - 1.0f; } - Material *material = BKE_grease_pencil_object_material_ensure_from_active_input_brush( + Material *material = BKE_grease_pencil_object_material_ensure_from_brush( CTX_data_main(&C), object, brush); const int material_index = BKE_object_material_index_get(object, material); const bool use_fill = (material->gp_style->flag & GP_MATERIAL_FILL_SHOW) != 0; @@ -1736,14 +1736,9 @@ void PaintOperation::on_stroke_done(const bContext &C) if ((settings->flag & GP_BRUSH_OUTLINE_STROKE) != 0) { const float outline_radius = brush->unprojected_radius * settings->outline_fac * 0.5f; const int material_index = [&]() { - Material *material = BKE_grease_pencil_object_material_ensure_from_active_input_brush( + Material *material = BKE_grease_pencil_object_material_alt_ensure_from_brush( CTX_data_main(&C), object, brush); - const int active_index = BKE_object_material_index_get(object, material); - if (settings->material_alt == nullptr) { - return active_index; - } - const int alt_index = BKE_object_material_slot_find_index(object, settings->material_alt); - return (alt_index > -1) ? alt_index - 1 : active_index; + return BKE_object_material_index_get(object, material); }(); outline_stroke(drawing, active_curve,