[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [59110] trunk/blender/source/blender: make materials.pop() and more like pythons list.pop

Campbell Barton ideasman42 at gmail.com
Tue Aug 13 12:21:12 CEST 2013


Revision: 59110
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=59110
Author:   campbellbarton
Date:     2013-08-13 10:21:11 +0000 (Tue, 13 Aug 2013)
Log Message:
-----------
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.

Modified Paths:
--------------
    trunk/blender/source/blender/blenkernel/BKE_material.h
    trunk/blender/source/blender/blenkernel/intern/material.c
    trunk/blender/source/blender/makesrna/intern/rna_ID.c

Modified: trunk/blender/source/blender/blenkernel/BKE_material.h
===================================================================
--- trunk/blender/source/blender/blenkernel/BKE_material.h	2013-08-13 10:09:27 UTC (rev 59109)
+++ trunk/blender/source/blender/blenkernel/BKE_material.h	2013-08-13 10:21:11 UTC (rev 59110)
@@ -88,7 +88,7 @@
 
 /* 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 */
 

Modified: trunk/blender/source/blender/blenkernel/intern/material.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/material.c	2013-08-13 10:09:27 UTC (rev 59109)
+++ trunk/blender/source/blender/blenkernel/intern/material.c	2013-08-13 10:21:11 UTC (rev 59110)
@@ -572,7 +572,7 @@
 	}
 }
 
-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 @@
 			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)));
+			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)--;
-					
-					mat = MEM_callocN(sizeof(void *) * (*totcol), "newmatar");
-					memcpy(mat, *matar, sizeof(void *) * (*totcol));
-					MEM_freeN(*matar);
+				(*totcol)--;
+				*matar = MEM_reallocN(*matar, sizeof(void *) * (*totcol));
+				test_object_materials(G.main, id);
+			}
 
-					*matar = mat;
-					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 @@
 			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++;
+			}
+		}
 	}
 }
 

Modified: trunk/blender/source/blender/makesrna/intern/rna_ID.c
===================================================================
--- trunk/blender/source/blender/makesrna/intern/rna_ID.c	2013-08-13 10:09:27 UTC (rev 59109)
+++ trunk/blender/source/blender/makesrna/intern/rna_ID.c	2013-08-13 10:21:11 UTC (rev 59110)
@@ -353,10 +353,27 @@
 	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);
 	WM_main_add_notifier(NC_OBJECT | ND_OB_SHADING, id);
@@ -476,9 +493,9 @@
 	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);




More information about the Bf-blender-cvs mailing list