From 38b7563a4fbb0f06775e980b7439c0354dc78641 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Tue, 23 Mar 2021 13:57:13 +1100 Subject: Cleanup: de-duplicate gpencil logic to ensure materials - Rename: `BKE_gpencil_object_material_get_index_name`, to `BKE_gpencil_object_material_index_get_by_name` Matching `BKE_gpencil_layer_get_by_name`. - Move logic to ensure named materials into a new function: `BKE_gpencil_object_material_ensure_by_name` --- source/blender/editors/gpencil/gpencil_add_lineart.c | 17 +++-------------- source/blender/editors/gpencil/gpencil_add_monkey.c | 17 +++-------------- source/blender/editors/gpencil/gpencil_add_stroke.c | 17 +++-------------- 3 files changed, 9 insertions(+), 42 deletions(-) (limited to 'source/blender/editors/gpencil') diff --git a/source/blender/editors/gpencil/gpencil_add_lineart.c b/source/blender/editors/gpencil/gpencil_add_lineart.c index cd7e14278a4..6b28c6ec13e 100644 --- a/source/blender/editors/gpencil/gpencil_add_lineart.c +++ b/source/blender/editors/gpencil/gpencil_add_lineart.c @@ -55,19 +55,8 @@ static int gpencil_lineart_material(Main *bmain, const ColorTemplate *pct, const bool fill) { - short *totcol = BKE_object_material_len_p(ob); - Material *ma = NULL; - for (short i = 0; i < *totcol; i++) { - ma = BKE_gpencil_material(ob, i + 1); - if (STREQ(ma->id.name, pct->name)) { - return i; - } - } - - int idx; - - /* create a new one */ - ma = BKE_gpencil_object_material_new(bmain, ob, pct->name, &idx); + int index; + Material *ma = BKE_gpencil_object_material_ensure_by_name(bmain, ob, pct->name, &index); copy_v4_v4(ma->gp_style->stroke_rgba, pct->line); srgb_to_linearrgb_v4(ma->gp_style->stroke_rgba, ma->gp_style->stroke_rgba); @@ -79,7 +68,7 @@ static int gpencil_lineart_material(Main *bmain, ma->gp_style->flag |= GP_MATERIAL_FILL_SHOW; } - return idx; + return index; } /* ***************************************************************** */ diff --git a/source/blender/editors/gpencil/gpencil_add_monkey.c b/source/blender/editors/gpencil/gpencil_add_monkey.c index d86bad7ef3c..4497d963c6d 100644 --- a/source/blender/editors/gpencil/gpencil_add_monkey.c +++ b/source/blender/editors/gpencil/gpencil_add_monkey.c @@ -50,19 +50,8 @@ typedef struct ColorTemplate { static int gpencil_monkey_color( Main *bmain, Object *ob, const ColorTemplate *pct, bool stroke, bool fill) { - short *totcol = BKE_object_material_len_p(ob); - Material *ma = NULL; - for (short i = 0; i < *totcol; i++) { - ma = BKE_gpencil_material(ob, i + 1); - if (STREQ(ma->id.name, pct->name)) { - return i; - } - } - - int idx; - - /* create a new one */ - ma = BKE_gpencil_object_material_new(bmain, ob, pct->name, &idx); + int index; + Material *ma = BKE_gpencil_object_material_ensure_by_name(bmain, ob, pct->name, &index); copy_v4_v4(ma->gp_style->stroke_rgba, pct->line); srgb_to_linearrgb_v4(ma->gp_style->stroke_rgba, ma->gp_style->stroke_rgba); @@ -81,7 +70,7 @@ static int gpencil_monkey_color( ma->gp_style->flag |= GP_MATERIAL_FILL_SHOW; } - return idx; + return index; } /* ***************************************************************** */ diff --git a/source/blender/editors/gpencil/gpencil_add_stroke.c b/source/blender/editors/gpencil/gpencil_add_stroke.c index 1e1a70f9c1d..26237636526 100644 --- a/source/blender/editors/gpencil/gpencil_add_stroke.c +++ b/source/blender/editors/gpencil/gpencil_add_stroke.c @@ -52,19 +52,8 @@ static int gpencil_stroke_material(Main *bmain, const ColorTemplate *pct, const bool fill) { - short *totcol = BKE_object_material_len_p(ob); - Material *ma = NULL; - for (short i = 0; i < *totcol; i++) { - ma = BKE_gpencil_material(ob, i + 1); - if (STREQ(ma->id.name, pct->name)) { - return i; - } - } - - int idx; - - /* create a new one */ - ma = BKE_gpencil_object_material_new(bmain, ob, pct->name, &idx); + int index; + Material *ma = BKE_gpencil_object_material_ensure_by_name(bmain, ob, pct->name, &index); copy_v4_v4(ma->gp_style->stroke_rgba, pct->line); srgb_to_linearrgb_v4(ma->gp_style->stroke_rgba, ma->gp_style->stroke_rgba); @@ -76,7 +65,7 @@ static int gpencil_stroke_material(Main *bmain, ma->gp_style->flag |= GP_MATERIAL_FILL_SHOW; } - return idx; + return index; } /* ***************************************************************** */ -- cgit v1.2.3