diff options
-rw-r--r-- | source/blender/blenkernel/BKE_material.h | 2 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/material.c | 50 | ||||
-rw-r--r-- | source/blender/makesrna/intern/rna_ID.c | 25 |
3 files changed, 45 insertions, 32 deletions
diff --git a/source/blender/blenkernel/BKE_material.h b/source/blender/blenkernel/BKE_material.h index 7c47380f838..5d8406ba76f 100644 --- a/source/blender/blenkernel/BKE_material.h +++ b/source/blender/blenkernel/BKE_material.h @@ -88,7 +88,7 @@ int object_remove_material_slot(struct Object *ob); /* rna api */ void material_append_id(struct ID *id, struct Material *ma); -struct Material *material_pop_id(struct ID *id, int index, int remove_material_slot); /* index is an int because of RNA */ +struct Material *material_pop_id(struct ID *id, int index, bool remove_material_slot); /* index is an int because of RNA */ /* rendering */ diff --git a/source/blender/blenkernel/intern/material.c b/source/blender/blenkernel/intern/material.c index 6dc3c6d7bc6..b9114c24091 100644 --- a/source/blender/blenkernel/intern/material.c +++ b/source/blender/blenkernel/intern/material.c @@ -572,7 +572,7 @@ void material_append_id(ID *id, Material *ma) } } -Material *material_pop_id(ID *id, int index_i, int remove_material_slot) +Material *material_pop_id(ID *id, int index_i, bool remove_material_slot) { short index = (short)index_i; Material *ret = NULL; @@ -583,34 +583,24 @@ Material *material_pop_id(ID *id, int index_i, int remove_material_slot) ret = (*matar)[index]; id_us_min((ID *)ret); - if (remove_material_slot) { - if (*totcol <= 1) { - *totcol = 0; - MEM_freeN(*matar); - *matar = NULL; - } - else { - Material **mat; - if (index + 1 != (*totcol)) - memmove((*matar) + index, (*matar) + (index + 1), sizeof(void *) * ((*totcol) - (index + 1))); - - (*totcol)--; - - mat = MEM_callocN(sizeof(void *) * (*totcol), "newmatar"); - memcpy(mat, *matar, sizeof(void *) * (*totcol)); - MEM_freeN(*matar); - - *matar = mat; - test_object_materials(G.main, id); - } + if (*totcol <= 1) { + *totcol = 0; + MEM_freeN(*matar); + *matar = NULL; + } + else { + if (index + 1 != (*totcol)) + memmove((*matar) + index, (*matar) + (index + 1), sizeof(void *) * ((*totcol) - (index + 1))); + + (*totcol)--; + *matar = MEM_reallocN(*matar, sizeof(void *) * (*totcol)); + test_object_materials(G.main, id); + } + if (remove_material_slot) { /* decrease mat_nr index */ data_delete_material_index_id(id, index); } - - /* don't remove material slot, only clear it*/ - else - (*matar)[index] = NULL; } } @@ -1840,8 +1830,14 @@ static void convert_tfacematerial(Main *main, Material *ma) mf->mat_nr = mat_nr; } /* remove material from mesh */ - for (a = 0; a < me->totcol; ) - if (me->mat[a] == ma) material_pop_id(&me->id, a, 1); else a++; + for (a = 0; a < me->totcol; ) { + if (me->mat[a] == ma) { + material_pop_id(&me->id, a, true); + } + else { + a++; + } + } } } diff --git a/source/blender/makesrna/intern/rna_ID.c b/source/blender/makesrna/intern/rna_ID.c index 0489f85a37f..76337da3261 100644 --- a/source/blender/makesrna/intern/rna_ID.c +++ b/source/blender/makesrna/intern/rna_ID.c @@ -353,9 +353,26 @@ static void rna_IDMaterials_append_id(ID *id, Material *ma) WM_main_add_notifier(NC_OBJECT | ND_OB_SHADING, id); } -static Material *rna_IDMaterials_pop_id(ID *id, int index_i, int remove_material_slot) +static Material *rna_IDMaterials_pop_id(ID *id, ReportList *reports, int index_i, int remove_material_slot) { - Material *ma = material_pop_id(id, index_i, remove_material_slot); + Material *ma; + short *totcol = give_totcolp_id(id); + const short totcol_orig = *totcol; + if (index_i < 0) { + index_i += (*totcol); + } + + if ((index_i < 0) || (index_i >= (*totcol))) { + BKE_report(reports, RPT_ERROR, "Index out of range"); + return NULL; + } + + ma = material_pop_id(id, index_i, remove_material_slot); + + if (*totcol == totcol_orig) { + BKE_report(reports, RPT_ERROR, "No material to removed"); + return NULL; + } DAG_id_tag_update(id, OB_RECALC_DATA); WM_main_add_notifier(NC_OBJECT | ND_DRAW, id); @@ -476,9 +493,9 @@ static void rna_def_ID_materials(BlenderRNA *brna) RNA_def_property_flag(parm, PROP_REQUIRED); func = RNA_def_function(srna, "pop", "rna_IDMaterials_pop_id"); + RNA_def_function_flag(func, FUNC_USE_REPORTS); RNA_def_function_ui_description(func, "Remove a material from the data block"); - parm = RNA_def_int(func, "index", 0, 0, MAXMAT, "", "Index of material to remove", 0, MAXMAT); - RNA_def_property_flag(parm, PROP_REQUIRED); + parm = RNA_def_int(func, "index", -1, -MAXMAT, MAXMAT, "", "Index of material to remove", 0, MAXMAT); RNA_def_boolean(func, "update_data", 0, "", "Update data by re-adjusting the material slots assigned"); parm = RNA_def_pointer(func, "material", "Material", "", "Material to remove"); RNA_def_function_return(func, parm); |