GPencil: Move merge similar materials code to BKE

This is required in other places and need to be shared.
This commit is contained in:
Antonio Vazquez 2020-08-12 19:34:49 +02:00
parent 01636ed159
commit cd49c7b5ea
3 changed files with 75 additions and 29 deletions

View File

@ -131,6 +131,11 @@ bool BKE_gpencil_merge_materials_table_get(struct Object *ob,
const float sat_threshold,
const float val_threshold,
struct GHash *r_mat_table);
bool BKE_gpencil_merge_materials(struct Object *ob,
const float hue_threshold,
const float sat_threshold,
const float val_threshold,
int *r_removed);
/* statistics functions */
void BKE_gpencil_stats_update(struct bGPdata *gpd);

View File

@ -1986,6 +1986,73 @@ bool BKE_gpencil_merge_materials_table_get(Object *ob,
return changed;
}
/**
* Merge similar materials
* \param ob: Grease pencil object
* \param hue_threshold: Threshold for Hue
* \param sat_threshold: Threshold for Saturation
* \param val_threshold: Threshold for Value
* \param r_removed: Number of materials removed
* \return True if done
*/
bool BKE_gpencil_merge_materials(Object *ob,
const float hue_threshold,
const float sat_threshold,
const float val_threshold,
int *r_removed)
{
bGPdata *gpd = ob->data;
short *totcol = BKE_object_material_len_p(ob);
if (totcol == 0) {
*r_removed = 0;
return 0;
}
/* Review materials. */
GHash *mat_table = BLI_ghash_int_new(__func__);
bool changed = BKE_gpencil_merge_materials_table_get(
ob, hue_threshold, sat_threshold, val_threshold, mat_table);
*r_removed = BLI_ghash_len(mat_table);
/* Update stroke material index. */
if (changed) {
LISTBASE_FOREACH (bGPDlayer *, gpl, &gpd->layers) {
if (gpl->flag & GP_LAYER_HIDE) {
continue;
}
LISTBASE_FOREACH (bGPDframe *, gpf, &gpl->frames) {
LISTBASE_FOREACH (bGPDstroke *, gps, &gpf->strokes) {
/* Check if the color is editable. */
MaterialGPencilStyle *gp_style = BKE_gpencil_material_settings(ob, gps->mat_nr + 1);
if (gp_style != NULL) {
if (gp_style->flag & GP_MATERIAL_HIDE) {
continue;
}
if (((gpl->flag & GP_LAYER_UNLOCK_COLOR) == 0) &&
(gp_style->flag & GP_MATERIAL_LOCKED)) {
continue;
}
}
if (BLI_ghash_haskey(mat_table, POINTER_FROM_INT(gps->mat_nr))) {
int *idx = BLI_ghash_lookup(mat_table, POINTER_FROM_INT(gps->mat_nr));
gps->mat_nr = POINTER_AS_INT(idx);
}
}
}
}
}
/* Free hash memory. */
BLI_ghash_free(mat_table, NULL, NULL);
return changed;
}
/**
* Calc grease pencil statistics functions.
* \param gpd: Grease pencil data-block

View File

@ -591,35 +591,9 @@ static int gpencil_stroke_merge_material_exec(bContext *C, wmOperator *op)
return OPERATOR_CANCELLED;
}
bool changed = BKE_gpencil_merge_materials_table_get(
ob, hue_threshold, sat_threshold, val_threshold, mat_table);
int removed = BLI_ghash_len(mat_table);
/* Update stroke material index. */
if (changed) {
CTX_DATA_BEGIN (C, bGPDlayer *, gpl, editable_gpencil_layers) {
LISTBASE_FOREACH (bGPDframe *, gpf, &gpl->frames) {
LISTBASE_FOREACH (bGPDstroke *, gps, &gpf->strokes) {
if (ED_gpencil_stroke_can_use(C, gps) == false) {
continue;
}
if (ED_gpencil_stroke_color_use(ob, gpl, gps) == false) {
continue;
}
if (BLI_ghash_haskey(mat_table, POINTER_FROM_INT(gps->mat_nr))) {
int *idx = BLI_ghash_lookup(mat_table, POINTER_FROM_INT(gps->mat_nr));
gps->mat_nr = POINTER_AS_INT(idx);
}
}
}
}
CTX_DATA_END;
}
/* Free hash memory. */
BLI_ghash_free(mat_table, NULL, NULL);
int removed;
bool changed = BKE_gpencil_merge_materials(
ob, hue_threshold, sat_threshold, val_threshold, &removed);
/* notifiers */
if (changed) {