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`
This commit is contained in:
parent
a7239c2bad
commit
38b7563a4f
|
@ -228,6 +228,10 @@ struct Material *BKE_gpencil_object_material_ensure_from_brush(struct Main *bmai
|
|||
int BKE_gpencil_object_material_ensure(struct Main *bmain,
|
||||
struct Object *ob,
|
||||
struct Material *material);
|
||||
struct Material *BKE_gpencil_object_material_ensure_by_name(struct Main *bmain,
|
||||
struct Object *ob,
|
||||
const char *name,
|
||||
int *r_index);
|
||||
|
||||
struct Material *BKE_gpencil_object_material_new(struct Main *bmain,
|
||||
struct Object *ob,
|
||||
|
@ -235,7 +239,7 @@ struct Material *BKE_gpencil_object_material_new(struct Main *bmain,
|
|||
int *r_index);
|
||||
|
||||
int BKE_gpencil_object_material_index_get(struct Object *ob, struct Material *ma);
|
||||
int BKE_gpencil_object_material_get_index_name(struct Object *ob, char *name);
|
||||
int BKE_gpencil_object_material_index_get_by_name(struct Object *ob, const char *name);
|
||||
|
||||
struct Material *BKE_gpencil_object_material_from_brush_get(struct Object *ob,
|
||||
struct Brush *brush);
|
||||
|
|
|
@ -2446,7 +2446,7 @@ int BKE_gpencil_object_material_index_get(Object *ob, Material *ma)
|
|||
return -1;
|
||||
}
|
||||
|
||||
int BKE_gpencil_object_material_get_index_name(Object *ob, char *name)
|
||||
int BKE_gpencil_object_material_index_get_by_name(Object *ob, const char *name)
|
||||
{
|
||||
short *totcol = BKE_object_material_len_p(ob);
|
||||
Material *read_ma = NULL;
|
||||
|
@ -2461,6 +2461,19 @@ int BKE_gpencil_object_material_get_index_name(Object *ob, char *name)
|
|||
return -1;
|
||||
}
|
||||
|
||||
Material *BKE_gpencil_object_material_ensure_by_name(Main *bmain,
|
||||
Object *ob,
|
||||
const char *name,
|
||||
int *r_index)
|
||||
{
|
||||
int index = BKE_gpencil_object_material_index_get_by_name(ob, name);
|
||||
if (index != -1) {
|
||||
*r_index = index;
|
||||
return BKE_object_material_get(ob, index + 1);
|
||||
}
|
||||
return BKE_gpencil_object_material_new(bmain, ob, name, r_index);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a default palette.
|
||||
* \param bmain: Main pointer
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
/* ***************************************************************** */
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
/* ***************************************************************** */
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
/* ***************************************************************** */
|
||||
|
|
Loading…
Reference in New Issue