[Bf-python] material lists

Yann Vernier yann at algonet.se
Thu Jul 15 20:45:39 CEST 2004


In writing an import script for LDraw files, I found that I wanted to
assign multiple materials to objects. This is usually used for print or
decals, while one colour remains specific to the specific piece - the
main colour. 

However, I found that it was not possible to assign "holes" in the
material lists, nor could I put less in one than another without Blender
crashing. A bit of digging around and here's the patch to fix that.

Crashes removed: assigning different length lists to object and data.
Feature added: assigning None to material slots to assign later slots.
API change: empty slots will appear as None, not disappear.

This leads to an API change for the Python side: if there are holes in
the material lists, they will be represented by None entries. Previously
there was no way to detect the holes, and thus you couldn't tell which
slot any material belonged to unless all 16 were assigned. 

Remaining bug: You can't set colbits to values >=0x8000 since it's
regarded as a signed short. This is a problem if you want to use object
material slot 16, though I suppose the workaround is the ugly method of
negating your bitmask.

This also doesn't fix the behaviour of modifying a mesh's materials then
doing an update; I'm not sure what that does, exactly, but it does not
do what I expected and the source comments are scary.

-- 
PGP fingerprint = 9242 DC15 2502 FEAB E15F  84C6 D538 EC09 5380 5746
-------------- next part --------------
Index: Material.c
===================================================================
RCS file: /cvsroot/bf-blender/blender/source/blender/python/api2_2x/Material.c,v
retrieving revision 1.19
diff -u -r1.19 Material.c
--- Material.c	24 Jun 2004 09:43:12 -0000	1.19
+++ Material.c	15 Jul 2004 18:37:38 -0000
@@ -211,8 +211,10 @@
 
 		while ((mat_iter) && (wanted_mat == NULL)) {
 
-			if (strcmp (name, mat_iter->id.name+2) == 0)
+			if (strcmp (name, mat_iter->id.name+2) == 0) {
 				wanted_mat = (BPy_Material *)Material_CreatePyObject (mat_iter);
+				break;
+			}
 
 			mat_iter = mat_iter->id.next;
 		}
@@ -1734,6 +1736,8 @@
 			ob = Material_CreatePyObject (mat);
 			PyList_Append (list, ob);
 			Py_DECREF (ob); /* because Append increfs */
+		} else {
+			PyList_Append (list, Py_None);
 		}
 	}
 
@@ -1759,6 +1763,9 @@
 		if (Material_CheckPyObject ((PyObject *)pymat)) {
 			mat = pymat->material;
 			matlist[i] = mat;
+		} else if ((PyObject *)pymat == Py_None) {
+			mat = NULL;
+			matlist[i] = mat;
 		}
 
 		else { /* error; illegal type in material list */
@@ -1820,7 +1827,7 @@
 	}	
 	newarray = MEM_callocN(newsize * sizeof(void *), "PtrArray");
 	if (*p) {
-		memcpy(newarray, *p, oldsize);
+		memcpy(newarray, *p, oldsize*sizeof(void*));
 		MEM_freeN(*p);
 	}	
 	*p = newarray;
@@ -1831,26 +1838,27 @@
 {
 		Material	*** p_dataMaterials = give_matarar (object);
 		short				* nmaterials = give_totcolp (object);
+		int		result = 1;
 
 		if (object->totcol > *nmaterials) {
 				/* More object mats than data mats */
-				*nmaterials = object->totcol;
-				return expandPtrArray ((void *) p_dataMaterials,
+				result = expandPtrArray ((void *) p_dataMaterials,
 															 *nmaterials,
 															 object->totcol);
+				*nmaterials = object->totcol;
 		}
 		else {
 				if (object->totcol < *nmaterials) {
 						/* More data mats than object mats */
-						object->totcol = *nmaterials;
-						return expandPtrArray ((void *) &object->mat,
+						result = expandPtrArray ((void *) &object->mat,
 																	 object->totcol,
 																	 *nmaterials);
+						object->totcol = *nmaterials;
 				}
+				/* else No synchronization is needed; they're of equal length */
 		}
 
-		/* No synchronization is needed; they're of equal length */
-		return 1;
+		return result;
 }
 
 void EXPP_incr_mats_us (Material **matlist, int len)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://lists.blender.org/pipermail/bf-python/attachments/20040715/dbf33db6/attachment.sig>


More information about the Bf-python mailing list