Fix T56220: Adding Grease Object crashes if link Material is set to Object

This commit is contained in:
Antonio Vazquez 2018-08-08 16:56:56 +02:00
parent 78a6fa1a72
commit 5c4fd52612
Notes: blender-bot 2023-02-14 05:29:15 +01:00
Referenced by issue #56220, Adding Grease Pencil Monkey Object crashes Blender if Link Material is set to Object in User Preferences
12 changed files with 57 additions and 50 deletions

View File

@ -3545,9 +3545,10 @@ class VIEW3D_MT_assign_material(Menu):
def draw(self, context):
layout = self.layout
ob = context.active_object;
ob = context.active_object
for mat in ob.data.materials:
for slot in ob.material_slots:
mat = slot.material
layout.operator("gpencil.stroke_change_color", text=mat.name).material = mat.name

View File

@ -94,6 +94,7 @@ void BKE_gpencil_frame_delete_laststroke(struct bGPDlayer *gpl, struct bGPDframe
/* materials */
void BKE_gpencil_material_index_remove(struct bGPdata *gpd, int index);
void BKE_gpencil_material_remap(struct bGPdata *gpd, const unsigned int *remap, unsigned int remap_len);
int BKE_gpencil_get_material_index(struct Object *ob, struct Material *ma);
/* statistics functions */
void BKE_gpencil_stats_update(struct bGPdata *gpd);

View File

@ -1630,3 +1630,18 @@ void BKE_gpencil_stats_update(bGPdata *gpd)
}
}
/* get material index */
int BKE_gpencil_get_material_index(Object *ob, Material *ma)
{
short *totcol = give_totcolp(ob);
Material *read_ma = NULL;
for (short i = 0; i < *totcol; i++) {
read_ma = give_current_material(ob, i + 1);
if (ma == read_ma) {
return i + 1;
}
}
return 0;
}

View File

@ -55,11 +55,10 @@ typedef struct ColorTemplate {
/* Add color an ensure duplications (matched by name) */
static int gpencil_monkey_color(Main *bmain, Object *ob, const ColorTemplate *pct)
{
Material *ma = NULL;
Material ***matar = give_matarar(ob);
short *totcol = give_totcolp(ob);
Material *ma = NULL;
for (short i = 0; i < *totcol; i++) {
ma = (*matar)[i];
ma = give_current_material(ob, i + 1);
if (STREQ(ma->id.name, pct->name)) {
return i;
}
@ -73,7 +72,7 @@ static int gpencil_monkey_color(Main *bmain, Object *ob, const ColorTemplate *pc
copy_v4_v4(ma->gp_style->stroke_rgba, pct->line);
copy_v4_v4(ma->gp_style->fill_rgba, pct->fill);
return BKE_object_material_slot_find_index(ob, ma) - 1;
return BKE_gpencil_get_material_index(ob, ma) - 1;
}
/* ***************************************************************** */

View File

@ -55,11 +55,10 @@ typedef struct ColorTemplate {
/* Add color an ensure duplications (matched by name) */
static int gp_stroke_material(Main *bmain, Object *ob, const ColorTemplate *pct)
{
Material *ma = NULL;
Material ***matar = give_matarar(ob);
short *totcol = give_totcolp(ob);
Material *ma = NULL;
for (short i = 0; i < *totcol; i++) {
ma = (*matar)[i];
ma = give_current_material(ob, i + 1);
if (STREQ(ma->id.name, pct->name)) {
return i;
}
@ -73,7 +72,7 @@ static int gp_stroke_material(Main *bmain, Object *ob, const ColorTemplate *pct)
copy_v4_v4(ma->gp_style->stroke_rgba, pct->line);
copy_v4_v4(ma->gp_style->fill_rgba, pct->fill);
return BKE_object_material_slot_find_index(ob, ma) - 1;
return BKE_gpencil_get_material_index(ob, ma) - 1;
}
/* ***************************************************************** */

View File

@ -1053,8 +1053,8 @@ static void gp_brush_clone_add(bContext *C, tGP_BrushEditData *gso)
/* Fix color references */
Material *ma = BLI_ghash_lookup(data->new_colors, &new_stroke->mat_nr);
if ((ma) && (BKE_object_material_slot_find_index(ob, ma) > 0)) {
gps->mat_nr = BKE_object_material_slot_find_index(ob, ma) - 1;
if ((ma) && (BKE_gpencil_get_material_index(ob, ma) > 0)) {
gps->mat_nr = BKE_gpencil_get_material_index(ob, ma) - 1;
CLAMP_MIN(gps->mat_nr, 0);
}
else {

View File

@ -1150,8 +1150,8 @@ static int gp_stroke_change_color_exec(bContext *C, wmOperator *op)
}
}
/* try to find slot */
int idx = BKE_object_material_slot_find_index(ob, ma) - 1;
if (idx == 0) {
int idx = BKE_gpencil_get_material_index(ob, ma) - 1;
if (idx <= 0) {
return OPERATOR_CANCELLED;
}
@ -1230,7 +1230,6 @@ static int gp_stroke_lock_color_exec(bContext *C, wmOperator *UNUSED(op))
Object *ob = CTX_data_active_object(C);
Material ***matar = give_matarar(ob);
short *totcol = give_totcolp(ob);
/* sanity checks */
@ -1239,9 +1238,8 @@ static int gp_stroke_lock_color_exec(bContext *C, wmOperator *UNUSED(op))
/* first lock all colors */
for (short i = 0; i < *totcol; i++) {
Material *tmp_ma = (*matar)[i];
Material *tmp_ma = give_current_material(ob, i + 1);
tmp_ma->gp_style->flag |= GP_STYLE_COLOR_LOCKED;
}
/* loop all selected strokes and unlock any color */
@ -1256,7 +1254,8 @@ static int gp_stroke_lock_color_exec(bContext *C, wmOperator *UNUSED(op))
continue;
}
/* unlock color */
Material *tmp_ma = (*matar)[gps->mat_nr];
Material *tmp_ma = give_current_material(ob, gps->mat_nr + 1);
tmp_ma->gp_style->flag &= ~GP_STYLE_COLOR_LOCKED;
}
}
@ -1896,12 +1895,11 @@ int ED_gpencil_join_objects_exec(bContext *C, wmOperator *op)
obact->actdef = 1;
/* add missing materials reading source materials and checking in destination object */
Material ***matar = give_matarar(ob_src);
short *totcol = give_totcolp(ob_src);
for (short i = 0; i < *totcol; i++) {
Material *tmp_ma = (*matar)[i];
if (BKE_object_material_slot_find_index(ob_dst, tmp_ma) == 0) {
Material *tmp_ma = give_current_material(ob_src, i + 1);
if (BKE_gpencil_get_material_index(ob_dst, tmp_ma) == 0) {
BKE_object_material_slot_add(bmain, ob_dst);
assign_material(bmain, ob_dst, tmp_ma, ob_dst->totcol, BKE_MAT_ASSIGN_USERPREF);
}
@ -1941,7 +1939,7 @@ int ED_gpencil_join_objects_exec(bContext *C, wmOperator *op)
/* reasign material. Look old material and try to find in dst */
ma_src = give_current_material(ob_src, gps->mat_nr + 1);
if (ma_src != NULL) {
idx = BKE_object_material_slot_find_index(ob_dst, ma_src);
idx = BKE_gpencil_get_material_index(ob_dst, ma_src);
if (idx > 0) {
gps->mat_nr = idx - 1;
}
@ -2041,13 +2039,12 @@ static int gpencil_lock_layer_exec(bContext *C, wmOperator *UNUSED(op))
/* first lock and hide all colors */
Material *ma = NULL;
Material ***matar = give_matarar(ob);
short *totcol = give_totcolp(ob);
if ((totcol == 0) || (matar == NULL))
if (totcol == 0)
return OPERATOR_CANCELLED;
for (short i = 0; i < *totcol; i++) {
ma = (*matar)[i];
ma = give_current_material(ob, i + 1);
gp_style = ma->gp_style;
gp_style->flag |= GP_STYLE_COLOR_LOCKED;
gp_style->flag |= GP_STYLE_COLOR_HIDE;
@ -2113,10 +2110,9 @@ static int gpencil_color_isolate_exec(bContext *C, wmOperator *op)
/* Test whether to isolate or clear all flags */
Material *ma = NULL;
Material ***matar = give_matarar(ob);
short *totcol = give_totcolp(ob);
for (short i = 0; i < *totcol; i++) {
ma = (*matar)[i];
ma = give_current_material(ob, i + 1);
/* Skip if this is the active one */
if (ma == active_ma)
continue;
@ -2135,7 +2131,7 @@ static int gpencil_color_isolate_exec(bContext *C, wmOperator *op)
if (isolate) {
/* Set flags on all "other" colors */
for (short i = 0; i < *totcol; i++) {
ma = (*matar)[i];
ma = give_current_material(ob, i + 1);
gp_style = ma->gp_style;
if (gp_style == active_color)
continue;
@ -2146,7 +2142,7 @@ static int gpencil_color_isolate_exec(bContext *C, wmOperator *op)
else {
/* Clear flags - Restore everything else */
for (short i = 0; i < *totcol; i++) {
ma = (*matar)[i];
ma = give_current_material(ob, i + 1);
gp_style = ma->gp_style;
gp_style->flag &= ~flags;
}
@ -2188,16 +2184,15 @@ static int gpencil_color_hide_exec(bContext *C, wmOperator *op)
bool unselected = RNA_boolean_get(op->ptr, "unselected");
Material *ma = NULL;
Material ***matar = give_matarar(ob);
short *totcol = give_totcolp(ob);
if ((totcol == 0) || (matar == NULL))
if (totcol == 0)
return OPERATOR_CANCELLED;
if (unselected) {
/* hide unselected */
MaterialGPencilStyle *color = NULL;
for (short i = 0; i < *totcol; i++) {
ma = (*matar)[i];
ma = give_current_material(ob, i + 1);
color = ma->gp_style;
if (active_color != color) {
color->flag |= GP_STYLE_COLOR_HIDE;
@ -2239,17 +2234,16 @@ static int gpencil_color_reveal_exec(bContext *C, wmOperator *UNUSED(op))
{
Object *ob = CTX_data_active_object(C);
Material *ma = NULL;
Material ***matar = give_matarar(ob);
short *totcol = give_totcolp(ob);
if ((totcol == 0) || (matar == NULL))
if (totcol == 0)
return OPERATOR_CANCELLED;
/* make all colors visible */
MaterialGPencilStyle *gp_style = NULL;
for (short i = 0; i < *totcol; i++) {
ma = (*matar)[i];
ma = give_current_material(ob, i + 1);
gp_style = ma->gp_style;
gp_style->flag &= ~GP_STYLE_COLOR_HIDE;
}
@ -2282,17 +2276,16 @@ static int gpencil_color_lock_all_exec(bContext *C, wmOperator *UNUSED(op))
Object *ob = CTX_data_active_object(C);
Material *ma = NULL;
Material ***matar = give_matarar(ob);
short *totcol = give_totcolp(ob);
if ((totcol == 0) || (matar == NULL))
if (totcol == 0)
return OPERATOR_CANCELLED;
/* make all layers non-editable */
MaterialGPencilStyle *gp_style = NULL;
for (short i = 0; i < *totcol; i++) {
ma = (*matar)[i];
ma = give_current_material(ob, i + 1);
gp_style = ma->gp_style;
gp_style->flag |= GP_STYLE_COLOR_LOCKED;
}
@ -2324,17 +2317,16 @@ static int gpencil_color_unlock_all_exec(bContext *C, wmOperator *UNUSED(op))
{
Object *ob = CTX_data_active_object(C);
Material *ma = NULL;
Material ***matar = give_matarar(ob);
short *totcol = give_totcolp(ob);
if ((totcol == 0) || (matar == NULL))
if (totcol == 0)
return OPERATOR_CANCELLED;
/* make all layers editable again*/
MaterialGPencilStyle *gp_style = NULL;
for (short i = 0; i < *totcol; i++) {
ma = (*matar)[i];
ma = give_current_material(ob, i + 1);
gp_style = ma->gp_style;
gp_style->flag &= ~GP_STYLE_COLOR_LOCKED;
}

View File

@ -732,7 +732,7 @@ GHash *gp_copybuf_validate_colormap(bContext *C)
int *key = BLI_ghashIterator_getKey(&gh_iter);
Material *ma = BLI_ghashIterator_getValue(&gh_iter);
if (BKE_object_material_slot_find_index(ob, ma) == 0) {
if (BKE_gpencil_get_material_index(ob, ma) == 0) {
BKE_object_material_slot_add(bmain, ob);
assign_material(bmain, ob, ma, ob->totcol, BKE_MAT_ASSIGN_USERPREF);
}
@ -980,8 +980,8 @@ static int gp_strokes_paste_exec(bContext *C, wmOperator *op)
/* Remap material */
Material *ma = BLI_ghash_lookup(new_colors, &new_stroke->mat_nr);
if ((ma) && (BKE_object_material_slot_find_index(ob, ma) > 0)) {
gps->mat_nr = BKE_object_material_slot_find_index(ob, ma) - 1;
if ((ma) && (BKE_gpencil_get_material_index(ob, ma) > 0)) {
gps->mat_nr = BKE_gpencil_get_material_index(ob, ma) - 1;
CLAMP_MIN(gps->mat_nr, 0);
}
else {
@ -3184,7 +3184,7 @@ static int gp_stroke_separate_exec(bContext *C, wmOperator *op)
/* add duplicate materials */
ma = give_current_material(ob, gps->mat_nr + 1);
idx = BKE_object_material_slot_find_index(ob_dst, ma);
idx = BKE_gpencil_get_material_index(ob_dst, ma);
if (idx == 0) {
totadd++;

View File

@ -841,7 +841,7 @@ static void gpencil_stroke_from_buffer(tGPDfill *tgpf)
gps->flag |= GP_STROKE_CYCLIC;
gps->flag |= GP_STROKE_3DSPACE;
gps->mat_nr = BKE_object_material_slot_find_index(tgpf->ob, tgpf->mat) - 1;
gps->mat_nr = BKE_gpencil_get_material_index(tgpf->ob, tgpf->mat) - 1;
/* allocate memory for storage points */
gps->totpoints = tgpf->sbuffer_size;

View File

@ -1150,7 +1150,7 @@ static void gp_stroke_newfrombuffer(tGPsdata *p)
}
/* Save material index */
gps->mat_nr = BKE_object_material_slot_find_index(p->ob, p->material) - 1;
gps->mat_nr = BKE_gpencil_get_material_index(p->ob, p->material) - 1;
/* calculate UVs along the stroke */
ED_gpencil_calc_stroke_uv(obact, gps);
@ -1602,7 +1602,7 @@ static void gp_init_colors(tGPsdata *p)
}
/* check if the material is already on object material slots and add it if missing */
if (BKE_object_material_slot_find_index(p->ob, p->material) == 0) {
if (BKE_gpencil_get_material_index(p->ob, p->material) == 0) {
BKE_object_material_slot_add(p->bmain, p->ob);
assign_material(p->bmain, p->ob, ma, p->ob->totcol, BKE_MAT_ASSIGN_USERPREF);
}

View File

@ -177,7 +177,7 @@ static void gp_primitive_set_initdata(bContext *C, tGPDprimitive *tgpi)
gps->flag |= GP_STROKE_CYCLIC;
gps->flag |= GP_STROKE_3DSPACE;
gps->mat_nr = BKE_object_material_slot_find_index(tgpi->ob, tgpi->mat) - 1;
gps->mat_nr = BKE_gpencil_get_material_index(tgpi->ob, tgpi->mat) - 1;
/* allocate memory for storage points, but keep empty */
gps->totpoints = 0;

View File

@ -169,7 +169,7 @@ void gpencil_apply_modifier_material(
DEG_id_tag_update(&newmat->id, DEG_TAG_COPY_ON_WRITE);
}
/* reasign color index */
int idx = BKE_object_material_slot_find_index(ob, newmat);
int idx = BKE_gpencil_get_material_index(ob, newmat);
gps->mat_nr = idx - 1;
}
else {