From 78a6fa1a7294d73a4f35b08d569d03f1afdd71ac Mon Sep 17 00:00:00 2001 From: Antonioya Date: Wed, 8 Aug 2018 13:37:56 +0200 Subject: Cleanup: Move some duplicate code to new function --- .../gpencil_modifiers/intern/MOD_gpencil_util.c | 47 ++++++++++++++++++++++ .../gpencil_modifiers/intern/MOD_gpencil_util.h | 7 ++++ .../gpencil_modifiers/intern/MOD_gpencilcolor.c | 37 +---------------- .../gpencil_modifiers/intern/MOD_gpenciltint.c | 36 +---------------- 4 files changed, 58 insertions(+), 69 deletions(-) (limited to 'source/blender/gpencil_modifiers') diff --git a/source/blender/gpencil_modifiers/intern/MOD_gpencil_util.c b/source/blender/gpencil_modifiers/intern/MOD_gpencil_util.c index 664f4b6976c..21a55e3f970 100644 --- a/source/blender/gpencil_modifiers/intern/MOD_gpencil_util.c +++ b/source/blender/gpencil_modifiers/intern/MOD_gpencil_util.c @@ -33,6 +33,7 @@ #include "MEM_guardedalloc.h" #include "BLI_blenlib.h" +#include "BLI_ghash.h" #include "BLI_utildefines.h" #include "BLI_math_vector.h" #include "BLI_math_color.h" @@ -45,6 +46,7 @@ #include "DNA_gpencil_modifier_types.h" #include "BKE_global.h" +#include "BKE_main.h" #include "BKE_object.h" #include "BKE_lattice.h" #include "BKE_material.h" @@ -52,6 +54,8 @@ #include "BKE_gpencil_modifier.h" #include "BKE_colortools.h" +#include "DEG_depsgraph.h" + #include "MOD_gpencil_modifiertypes.h" #include "MOD_gpencil_util.h" @@ -140,3 +144,46 @@ float get_modifier_point_weight(MDeformVert *dvert, int inverse, int vindex) return weight; } + +/* set material when apply modifiers (used in tint and color modifier) */ +void gpencil_apply_modifier_material( + Main *bmain, Object *ob, Material *mat, + GHash *gh_color, bGPDstroke *gps, bool crt_material) +{ + MaterialGPencilStyle *gp_style = mat->gp_style; + + /* look for color */ + if (crt_material) { + Material *newmat = BLI_ghash_lookup(gh_color, mat->id.name); + if (newmat == NULL) { + BKE_object_material_slot_add(bmain, ob); + newmat = BKE_material_copy(bmain, mat); + newmat->preview = NULL; + + assign_material(bmain, ob, newmat, ob->totcol, BKE_MAT_ASSIGN_USERPREF); + + copy_v4_v4(newmat->gp_style->stroke_rgba, gps->runtime.tmp_stroke_rgba); + copy_v4_v4(newmat->gp_style->fill_rgba, gps->runtime.tmp_fill_rgba); + + BLI_ghash_insert(gh_color, mat->id.name, newmat); + DEG_id_tag_update(&newmat->id, DEG_TAG_COPY_ON_WRITE); + } + /* reasign color index */ + int idx = BKE_object_material_slot_find_index(ob, newmat); + gps->mat_nr = idx - 1; + } + else { + /* reuse existing color (but update only first time) */ + if (BLI_ghash_lookup(gh_color, mat->id.name) == NULL) { + copy_v4_v4(gp_style->stroke_rgba, gps->runtime.tmp_stroke_rgba); + copy_v4_v4(gp_style->fill_rgba, gps->runtime.tmp_fill_rgba); + BLI_ghash_insert(gh_color, mat->id.name, mat); + } + /* update previews (icon and thumbnail) */ + if (mat->preview != NULL) { + mat->preview->flag[ICON_SIZE_ICON] |= PRV_CHANGED; + mat->preview->flag[ICON_SIZE_PREVIEW] |= PRV_CHANGED; + } + DEG_id_tag_update(&mat->id, DEG_TAG_COPY_ON_WRITE); + } +} diff --git a/source/blender/gpencil_modifiers/intern/MOD_gpencil_util.h b/source/blender/gpencil_modifiers/intern/MOD_gpencil_util.h index 39a4947573e..1ca9febbdca 100644 --- a/source/blender/gpencil_modifiers/intern/MOD_gpencil_util.h +++ b/source/blender/gpencil_modifiers/intern/MOD_gpencil_util.h @@ -33,10 +33,13 @@ #ifndef __MOD_GPENCIL_UTIL_H__ #define __MOD_GPENCIL_UTIL_H__ +struct Main; struct Object; struct bGPDlayer; struct bGPDstroke; struct MDeformVert; +struct Material; +struct GHash; bool is_stroke_affected_by_modifier( struct Object *ob, char *mlayername, int mpassindex, int minpoints, @@ -44,4 +47,8 @@ bool is_stroke_affected_by_modifier( float get_modifier_point_weight(struct MDeformVert *dvert, int inverse, int vindex); +void gpencil_apply_modifier_material( + struct Main *bmain, struct Object *ob, struct Material *mat, + struct GHash *gh_color, struct bGPDstroke *gps, bool crt_material); + #endif /* __MOD_GPENCIL_UTIL_H__ */ diff --git a/source/blender/gpencil_modifiers/intern/MOD_gpencilcolor.c b/source/blender/gpencil_modifiers/intern/MOD_gpencilcolor.c index b2df49a8d06..4c087577699 100644 --- a/source/blender/gpencil_modifiers/intern/MOD_gpencilcolor.c +++ b/source/blender/gpencil_modifiers/intern/MOD_gpencilcolor.c @@ -122,41 +122,8 @@ static void bakeModifier( deformStroke(md, depsgraph, ob, gpl, gps); - /* look for color */ - if (mmd->flag & GP_COLOR_CREATE_COLORS) { - Material *newmat = BLI_ghash_lookup(gh_color, mat->id.name); - if (newmat == NULL) { - BKE_object_material_slot_add(bmain, ob); - newmat = BKE_material_copy(bmain, mat); - newmat->preview = NULL; - - assign_material(bmain, ob, newmat, ob->totcol, BKE_MAT_ASSIGN_USERPREF); - - copy_v4_v4(newmat->gp_style->stroke_rgba, gps->runtime.tmp_stroke_rgba); - copy_v4_v4(newmat->gp_style->fill_rgba, gps->runtime.tmp_fill_rgba); - - BLI_ghash_insert(gh_color, mat->id.name, newmat); - DEG_id_tag_update(&newmat->id, DEG_TAG_COPY_ON_WRITE); - } - /* reasign color index */ - int idx = BKE_object_material_slot_find_index(ob, newmat); - gps->mat_nr = idx - 1; - } - else { - /* reuse existing color (but update only first time) */ - if (BLI_ghash_lookup(gh_color, mat->id.name) == NULL) { - copy_v4_v4(gp_style->stroke_rgba, gps->runtime.tmp_stroke_rgba); - copy_v4_v4(gp_style->fill_rgba, gps->runtime.tmp_fill_rgba); - BLI_ghash_insert(gh_color, mat->id.name, mat); - } - /* update previews (icon and thumbnail) */ - if (mat->preview != NULL) { - mat->preview->flag[ICON_SIZE_ICON] |= PRV_CHANGED; - mat->preview->flag[ICON_SIZE_PREVIEW] |= PRV_CHANGED; - } - DEG_id_tag_update(&mat->id, DEG_TAG_COPY_ON_WRITE); - } - + gpencil_apply_modifier_material(bmain, ob, mat, gh_color, gps, + (bool)(mmd->flag & GP_COLOR_CREATE_COLORS)); } } } diff --git a/source/blender/gpencil_modifiers/intern/MOD_gpenciltint.c b/source/blender/gpencil_modifiers/intern/MOD_gpenciltint.c index 08cf1c13692..06212451d48 100644 --- a/source/blender/gpencil_modifiers/intern/MOD_gpenciltint.c +++ b/source/blender/gpencil_modifiers/intern/MOD_gpenciltint.c @@ -130,40 +130,8 @@ static void bakeModifier( deformStroke(md, depsgraph, ob, gpl, gps); - /* look for color */ - if (mmd->flag & GP_TINT_CREATE_COLORS) { - Material *newmat = (Material *)BLI_ghash_lookup(gh_color, mat->id.name); - if (newmat == NULL) { - BKE_object_material_slot_add(bmain, ob); - newmat = BKE_material_copy(bmain, mat); - newmat->preview = NULL; - - assign_material(bmain, ob, newmat, ob->totcol, BKE_MAT_ASSIGN_USERPREF); - - copy_v4_v4(newmat->gp_style->stroke_rgba, gps->runtime.tmp_stroke_rgba); - copy_v4_v4(newmat->gp_style->fill_rgba, gps->runtime.tmp_fill_rgba); - - BLI_ghash_insert(gh_color, mat->id.name, newmat); - DEG_id_tag_update(&newmat->id, DEG_TAG_COPY_ON_WRITE); - } - /* reasign color index */ - int idx = BKE_object_material_slot_find_index(ob, newmat); - gps->mat_nr = idx - 1; - } - else { - /* reuse existing color (but update only first time) */ - if (BLI_ghash_lookup(gh_color, mat->id.name) == NULL) { - copy_v4_v4(gp_style->stroke_rgba, gps->runtime.tmp_stroke_rgba); - copy_v4_v4(gp_style->fill_rgba, gps->runtime.tmp_fill_rgba); - BLI_ghash_insert(gh_color, mat->id.name, mat); - } - /* update previews (icon and thumbnail) */ - if (mat->preview != NULL) { - mat->preview->flag[ICON_SIZE_ICON] |= PRV_CHANGED; - mat->preview->flag[ICON_SIZE_PREVIEW] |= PRV_CHANGED; - } - DEG_id_tag_update(&mat->id, DEG_TAG_COPY_ON_WRITE); - } + gpencil_apply_modifier_material(bmain, ob, mat, gh_color, gps, + (bool)(mmd->flag & GP_TINT_CREATE_COLORS)); } } } -- cgit v1.2.3