Commit: 5c4fd526124c348c8f324a2d0a384958c6e793b8
Author: Antonioya
Date:   Wed Aug 8 16:56:56 2018 +0200
Branches: blender2.8
https://developer.blender.org/rB5c4fd526124c348c8f324a2d0a384958c6e793b8

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

===================================================================

M       release/scripts/startup/bl_ui/space_view3d.py
M       source/blender/blenkernel/BKE_gpencil.h
M       source/blender/blenkernel/intern/gpencil.c
M       source/blender/editors/gpencil/gpencil_add_monkey.c
M       source/blender/editors/gpencil/gpencil_add_stroke.c
M       source/blender/editors/gpencil/gpencil_brush.c
M       source/blender/editors/gpencil/gpencil_data.c
M       source/blender/editors/gpencil/gpencil_edit.c
M       source/blender/editors/gpencil/gpencil_fill.c
M       source/blender/editors/gpencil/gpencil_paint.c
M       source/blender/editors/gpencil/gpencil_primitive.c
M       source/blender/gpencil_modifiers/intern/MOD_gpencil_util.c

===================================================================

diff --git a/release/scripts/startup/bl_ui/space_view3d.py 
b/release/scripts/startup/bl_ui/space_view3d.py
index 349832cedf2..72296861ad9 100644
--- a/release/scripts/startup/bl_ui/space_view3d.py
+++ b/release/scripts/startup/bl_ui/space_view3d.py
@@ -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
 
 
diff --git a/source/blender/blenkernel/BKE_gpencil.h 
b/source/blender/blenkernel/BKE_gpencil.h
index 887a7f4f67b..532eaf9a0e7 100644
--- a/source/blender/blenkernel/BKE_gpencil.h
+++ b/source/blender/blenkernel/BKE_gpencil.h
@@ -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);
diff --git a/source/blender/blenkernel/intern/gpencil.c 
b/source/blender/blenkernel/intern/gpencil.c
index b619304d39b..140e4a25882 100644
--- a/source/blender/blenkernel/intern/gpencil.c
+++ b/source/blender/blenkernel/intern/gpencil.c
@@ -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;
+}
diff --git a/source/blender/editors/gpencil/gpencil_add_monkey.c 
b/source/blender/editors/gpencil/gpencil_add_monkey.c
index b0c3675c123..78286e3f672 100644
--- a/source/blender/editors/gpencil/gpencil_add_monkey.c
+++ b/source/blender/editors/gpencil/gpencil_add_monkey.c
@@ -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;
 }
 
 /* ***************************************************************** */
diff --git a/source/blender/editors/gpencil/gpencil_add_stroke.c 
b/source/blender/editors/gpencil/gpencil_add_stroke.c
index f932f98ac1d..c5020593bcb 100644
--- a/source/blender/editors/gpencil/gpencil_add_stroke.c
+++ b/source/blender/editors/gpencil/gpencil_add_stroke.c
@@ -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;
 }
 
 /* ***************************************************************** */
diff --git a/source/blender/editors/gpencil/gpencil_brush.c 
b/source/blender/editors/gpencil/gpencil_brush.c
index ff93e7fc57d..d8be0dd665a 100644
--- a/source/blender/editors/gpencil/gpencil_brush.c
+++ b/source/blender/editors/gpencil/gpencil_brush.c
@@ -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 {
diff --git a/source/blender/editors/gpencil/gpencil_data.c 
b/source/blender/editors/gpencil/gpencil_data.c
index 43721d73a80..faeee4db169 100644
--- a/source/blender/editors/gpencil/gpencil_data.c
+++ b/source/blender/editors/gpencil/gpencil_data.c
@@ -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 (to

@@ Diff output truncated at 10240 characters. @@

_______________________________________________
Bf-blender-cvs mailing list
Bf-blender-cvs@blender.org
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to