make materials.pop() and more like pythons list.pop

- allow negative index values.
- error when invalid index value are passed in.
- remove last item if no index argument is given.

also change behavior to remove the material slot, it was only clearning by default but the list length remained the same.
This commit is contained in:
Campbell Barton
2013-08-13 10:21:11 +00:00
parent 4d5c64372a
commit a8d1c893e8
3 changed files with 45 additions and 32 deletions

View File

@@ -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 */

View File

@@ -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 (*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) {
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);
}
/* 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++;
}
}
}
}

View File

@@ -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);