From fc6aac39167994661fb02274d8a80721b96b7cb8 Mon Sep 17 00:00:00 2001 From: Antonioya Date: Fri, 18 Jan 2019 10:52:38 +0100 Subject: GP: Create materials when separate active layer The materials were not created when used the active layer option of the separate operator. --- source/blender/editors/gpencil/gpencil_edit.c | 34 ++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) (limited to 'source/blender/editors/gpencil/gpencil_edit.c') diff --git a/source/blender/editors/gpencil/gpencil_edit.c b/source/blender/editors/gpencil/gpencil_edit.c index a601e0635ed..ac3c080e019 100644 --- a/source/blender/editors/gpencil/gpencil_edit.c +++ b/source/blender/editors/gpencil/gpencil_edit.c @@ -3497,6 +3497,12 @@ static int gp_stroke_separate_exec(bContext *C, wmOperator *op) if (ELEM(NULL, gpd_src)) { return OPERATOR_CANCELLED; } + + if ((mode == GP_SEPARATE_LAYER) && (BLI_listbase_count(&gpd_src->layers) == 1)) { + BKE_report(op->reports, RPT_ERROR, "Cannot separate an object with one layer only"); + return OPERATOR_CANCELLED; + } + const bool is_multiedit = (bool)GPENCIL_MULTIEDIT_SESSIONS_ON(gpd_src); /* create a new object */ @@ -3504,7 +3510,6 @@ static int gp_stroke_separate_exec(bContext *C, wmOperator *op) ob_dst = base_new->object; ob_dst->mode = OB_MODE_OBJECT; /* create new grease pencil datablock */ - // XXX: check usercounts gpd_dst = BKE_gpencil_data_addnew(bmain, gpd_src->id.name + 2); ob_dst->data = (bGPdata *)gpd_dst; @@ -3632,8 +3637,35 @@ static int gp_stroke_separate_exec(bContext *C, wmOperator *op) gpl->prev = gpl->next = NULL; /* relink to destination datablock */ BLI_addtail(&gpd_dst->layers, gpl); + + /* add duplicate materials */ + for (bGPDframe *gpf = gpl->frames.first; gpf; gpf = gpf->next) { + for (bGPDstroke *gps = gpf->strokes.first; gps; gps = gps->next) { + /* skip strokes that are invalid for current view */ + if (ED_gpencil_stroke_can_use(C, gps) == false) { + continue; + } + ma = give_current_material(ob, gps->mat_nr + 1); + idx = BKE_gpencil_get_material_index(ob_dst, ma); + if (idx == 0) { + totadd++; + ob_dst->actcol = totadd; + ob_dst->totcol = totadd; + + if (totadd > totslots) { + BKE_object_material_slot_add(bmain, ob_dst); + } + + assign_material(bmain, ob_dst, ma, ob_dst->totcol, BKE_MAT_ASSIGN_USERPREF); + idx = totadd; + } + /* reasign material */ + gps->mat_nr = idx - 1; + } + } } } + DEG_id_tag_update(&gpd_src->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY); DEG_id_tag_update(&gpd_dst->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY); -- cgit v1.2.3