add materials.clear() method, matching other python list method.

This commit is contained in:
Campbell Barton 2013-08-14 11:29:58 +00:00
parent 1979720d03
commit 503b7d5b9a
7 changed files with 119 additions and 27 deletions

@ -69,7 +69,8 @@ bool BKE_curve_minmax(struct Curve *cu, float min[3], float max[3]);
bool BKE_curve_center_median(struct Curve *cu, float cent[3]);
bool BKE_curve_center_bounds(struct Curve *cu, float cent[3]);
void BKE_curve_translate(struct Curve *cu, float offset[3], int do_keys);
void BKE_curve_delete_material_index(struct Curve *cu, int index);
void BKE_curve_material_index_remove(struct Curve *cu, int index);
void BKE_curve_material_index_clear(struct Curve *cu);
ListBase *BKE_curve_nurbs_get(struct Curve *cu);

@ -87,9 +87,9 @@ int object_add_material_slot(struct Object *ob);
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, bool remove_material_slot); /* index is an int because of RNA */
void BKE_material_append_id(struct ID *id, struct Material *ma);
struct Material *BKE_material_pop_id(struct ID *id, int index, bool update_data); /* index is an int because of RNA */
void BKE_material_clear_id(struct ID *id, bool update_data);
/* rendering */
void init_render_material(struct Material *, int, float *);

@ -188,7 +188,8 @@ void BKE_mesh_from_nurbs_displist(struct Object *ob, struct ListBase *dispbase,
void BKE_mesh_from_nurbs(struct Object *ob);
void BKE_mesh_to_curve_nurblist(struct DerivedMesh *dm, struct ListBase *nurblist, const int edge_users_test);
void BKE_mesh_to_curve(struct Scene *scene, struct Object *ob);
void BKE_mesh_delete_material_index(struct Mesh *me, short index);
void BKE_mesh_material_index_remove(struct Mesh *me, short index);
void BKE_mesh_material_index_clear(struct Mesh *me);
void BKE_mesh_smooth_flag_set(struct Object *meshOb, int enableSmooth);
void BKE_mesh_convert_mfaces_to_mpolys(struct Mesh *mesh);
void BKE_mesh_do_versions_convert_mfaces_to_mpolys(struct Mesh *mesh);

@ -3819,7 +3819,7 @@ void BKE_curve_translate(Curve *cu, float offset[3], int do_keys)
}
}
void BKE_curve_delete_material_index(Curve *cu, int index)
void BKE_curve_material_index_remove(Curve *cu, int index)
{
const int curvetype = BKE_curve_type_get(cu);
@ -3838,8 +3838,32 @@ void BKE_curve_delete_material_index(Curve *cu, int index)
for (nu = cu->nurb.first; nu; nu = nu->next) {
if (nu->mat_nr && nu->mat_nr >= index) {
nu->mat_nr--;
if (curvetype == OB_CURVE)
if (curvetype == OB_CURVE) {
nu->charidx--;
}
}
}
}
}
void BKE_curve_material_index_clear(Curve *cu)
{
const int curvetype = BKE_curve_type_get(cu);
if (curvetype == OB_FONT) {
struct CharInfo *info = cu->strinfo;
int i;
for (i = cu->len - 1; i >= 0; i--, info++) {
info->mat_nr = 0;
}
}
else {
Nurb *nu;
for (nu = cu->nurb.first; nu; nu = nu->next) {
nu->mat_nr = 0;
if (curvetype == OB_CURVE) {
nu->charidx = 0;
}
}
}

@ -537,17 +537,17 @@ short *give_totcolp_id(ID *id)
return NULL;
}
static void data_delete_material_index_id(ID *id, short index)
static void material_data_index_remove_id(ID *id, short index)
{
/* ensure we don't try get materials from non-obdata */
BLI_assert(OB_DATA_SUPPORT_ID(GS(id->name)));
switch (GS(id->name)) {
case ID_ME:
BKE_mesh_delete_material_index((Mesh *)id, index);
BKE_mesh_material_index_remove((Mesh *)id, index);
break;
case ID_CU:
BKE_curve_delete_material_index((Curve *)id, index);
BKE_curve_material_index_remove((Curve *)id, index);
break;
case ID_MB:
/* meta-elems don't have materials atm */
@ -555,7 +555,25 @@ static void data_delete_material_index_id(ID *id, short index)
}
}
void material_append_id(ID *id, Material *ma)
static void material_data_index_clear_id(ID *id)
{
/* ensure we don't try get materials from non-obdata */
BLI_assert(OB_DATA_SUPPORT_ID(GS(id->name)));
switch (GS(id->name)) {
case ID_ME:
BKE_mesh_material_index_clear((Mesh *)id);
break;
case ID_CU:
BKE_curve_material_index_clear((Curve *)id);
break;
case ID_MB:
/* meta-elems don't have materials atm */
break;
}
}
void BKE_material_append_id(ID *id, Material *ma)
{
Material ***matar;
if ((matar = give_matarar_id(id))) {
@ -572,7 +590,7 @@ void material_append_id(ID *id, Material *ma)
}
}
Material *material_pop_id(ID *id, int index_i, bool remove_material_slot)
Material *BKE_material_pop_id(ID *id, int index_i, bool update_data)
{
short index = (short)index_i;
Material *ret = NULL;
@ -597,9 +615,9 @@ Material *material_pop_id(ID *id, int index_i, bool remove_material_slot)
test_object_materials(G.main, id);
}
if (remove_material_slot) {
if (update_data) {
/* decrease mat_nr index */
data_delete_material_index_id(id, index);
material_data_index_remove_id(id, index);
}
}
}
@ -607,6 +625,24 @@ Material *material_pop_id(ID *id, int index_i, bool remove_material_slot)
return ret;
}
void BKE_material_clear_id(struct ID *id, bool update_data)
{
Material ***matar;
if ((matar = give_matarar_id(id))) {
short *totcol = give_totcolp_id(id);
*totcol = 0;
if (*matar) {
MEM_freeN(*matar);
*matar = NULL;
}
if (update_data) {
/* decrease mat_nr index */
material_data_index_clear_id(id);
}
}
}
Material *give_current_material(Object *ob, short act)
{
Material ***matarar, *ma;
@ -1217,7 +1253,7 @@ int object_remove_material_slot(Object *ob)
/* check indices from mesh */
if (ELEM4(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT)) {
data_delete_material_index_id((ID *)ob->data, actcol - 1);
material_data_index_remove_id((ID *)ob->data, actcol - 1);
BKE_displist_free(&ob->disp);
}
@ -1693,7 +1729,7 @@ static short mesh_getmaterialnumber(Mesh *me, Material *ma)
/* append material */
static short mesh_addmaterial(Mesh *me, Material *ma)
{
material_append_id(&me->id, NULL);
BKE_material_append_id(&me->id, NULL);
me->mat[me->totcol - 1] = ma;
id_us_plus(&ma->id);
@ -1832,7 +1868,7 @@ static void convert_tfacematerial(Main *main, Material *ma)
/* remove material from mesh */
for (a = 0; a < me->totcol; ) {
if (me->mat[a] == ma) {
material_pop_id(&me->id, a, true);
BKE_material_pop_id(&me->id, a, true);
}
else {
a++;

@ -1852,20 +1852,37 @@ void BKE_mesh_to_curve(Scene *scene, Object *ob)
}
}
void BKE_mesh_delete_material_index(Mesh *me, short index)
void BKE_mesh_material_index_remove(Mesh *me, short index)
{
MPoly *mp;
MFace *mf;
int i;
for (i = 0; i < me->totpoly; i++) {
MPoly *mp = &((MPoly *) me->mpoly)[i];
if (mp->mat_nr && mp->mat_nr >= index)
for (mp = me->mpoly, i = 0; i < me->totpoly; i++, mp++) {
if (mp->mat_nr && mp->mat_nr >= index) {
mp->mat_nr--;
}
}
for (i = 0; i < me->totface; i++) {
MFace *mf = &((MFace *) me->mface)[i];
if (mf->mat_nr && mf->mat_nr >= index)
for (mf = me->mface, i = 0; i < me->totface; i++, mf++) {
if (mf->mat_nr && mf->mat_nr >= index) {
mf->mat_nr--;
}
}
}
void BKE_mesh_material_index_clear(Mesh *me)
{
MPoly *mp;
MFace *mf;
int i;
for (mp = me->mpoly, i = 0; i < me->totpoly; i++, mp++) {
mp->mat_nr = 0;
}
for (mf = me->mface, i = 0; i < me->totface; i++, mf++) {
mf->mat_nr = 0;
}
}

@ -347,7 +347,7 @@ int rna_IDMaterials_assign_int(PointerRNA *ptr, int key, const PointerRNA *assig
static void rna_IDMaterials_append_id(ID *id, Material *ma)
{
material_append_id(id, ma);
BKE_material_append_id(id, ma);
WM_main_add_notifier(NC_OBJECT | ND_DRAW, id);
WM_main_add_notifier(NC_OBJECT | ND_OB_SHADING, id);
@ -367,7 +367,7 @@ static Material *rna_IDMaterials_pop_id(ID *id, ReportList *reports, int index_i
return NULL;
}
ma = material_pop_id(id, index_i, remove_material_slot);
ma = BKE_material_pop_id(id, index_i, remove_material_slot);
if (*totcol == totcol_orig) {
BKE_report(reports, RPT_ERROR, "No material to removed");
@ -381,6 +381,15 @@ static Material *rna_IDMaterials_pop_id(ID *id, ReportList *reports, int index_i
return ma;
}
static void rna_IDMaterials_clear_id(ID *id, int remove_material_slot)
{
BKE_material_clear_id(id, remove_material_slot);
DAG_id_tag_update(id, OB_RECALC_DATA);
WM_main_add_notifier(NC_OBJECT | ND_DRAW, id);
WM_main_add_notifier(NC_OBJECT | ND_OB_SHADING, id);
}
static void rna_Library_filepath_set(PointerRNA *ptr, const char *value)
{
Library *lib = (Library *)ptr->data;
@ -499,6 +508,10 @@ static void rna_def_ID_materials(BlenderRNA *brna)
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);
func = RNA_def_function(srna, "clear", "rna_IDMaterials_clear_id");
RNA_def_function_ui_description(func, "Remove all materials from the data block");
RNA_def_boolean(func, "update_data", 0, "", "Update data by re-adjusting the material slots assigned");
}
static void rna_def_ID(BlenderRNA *brna)