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
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user