diff --git a/source/blender/python/api2_2x/Curve.c b/source/blender/python/api2_2x/Curve.c index f495a74a06c..6ac26a02c95 100644 --- a/source/blender/python/api2_2x/Curve.c +++ b/source/blender/python/api2_2x/Curve.c @@ -36,6 +36,7 @@ #include "BKE_global.h" #include "BKE_library.h" #include "BKE_curve.h" +#include "BKE_material.h" #include "MEM_guardedalloc.h" /* because we wil be mallocing memory */ #include "CurNurb.h" #include "SurfNurb.h" @@ -108,6 +109,7 @@ static PyObject *Curve_appendPoint( BPy_Curve * self, PyObject * args ); static PyObject *Curve_appendNurb( BPy_Curve * self, PyObject * args ); static PyObject *Curve_getMaterials( BPy_Curve * self ); +static PyObject *Curve_setMaterials( BPy_Curve * self, PyObject * args ); static PyObject *Curve_getBevOb( BPy_Curve * self ); static PyObject *Curve_setBevOb( BPy_Curve * self, PyObject * args ); @@ -1301,9 +1303,51 @@ PyObject *Curve_update( BPy_Curve * self ) static PyObject *Curve_getMaterials( BPy_Curve * self ) { - return ( EXPP_PyList_fromMaterialList( self->curve->mat, - self->curve->totcol, 1 ) ); + return EXPP_PyList_fromMaterialList( self->curve->mat, + self->curve->totcol, 1 ); +} +static PyObject *Curve_setMaterials( BPy_Curve *self, PyObject * value ) +{ + Material **matlist; + int len; + + if( !PySequence_Check( value ) || + !EXPP_check_sequence_consistency( value, &Material_Type ) ) + return EXPP_ReturnPyObjError( PyExc_TypeError, + "sequence should only contain materials or None)" ); + + len = PySequence_Size( value ); + if( len > 16 ) + return EXPP_ReturnPyObjError( PyExc_TypeError, + "list can't have more than 16 materials" ); + + /* free old material list (if it exists) and adjust user counts */ + if( self->curve->mat ) { + Curve *cur = self->curve; + int i; + for( i = cur->totcol; i-- > 0; ) + if( cur->mat[i] ) + cur->mat[i]->id.us--; + MEM_freeN( cur->mat ); + } + + /* build the new material list, increment user count, store it */ + + matlist = EXPP_newMaterialList_fromPyList( value ); + EXPP_incr_mats_us( matlist, len ); + self->curve->mat = matlist; + self->curve->totcol = (short)len; + +/**@ This is another ugly fix due to the weird material handling of blender. + * it makes sure that object material lists get updated (by their length) + * according to their data material lists, otherwise blender crashes. + * It just stupidly runs through all objects...BAD BAD BAD. + */ + + test_object_materials( ( ID * ) self->curve ); + + Py_RETURN_NONE; } /*****************************************************************************/ @@ -1589,6 +1633,8 @@ static PyObject *CurveGetAttr( BPy_Curve * self, char *name ) return Curve_getTaperOb( self ); else if( strcmp( name, "key" ) == 0 ) return Curve_getKey( self ); + else if( strcmp( name, "materials" ) == 0 ) + return Curve_getMaterials( self ); #if 0 else if( strcmp( name, "numpts" ) == 0 ) return Curve_getNumPoints( self ); @@ -1647,6 +1693,8 @@ static int CurveSetAttr( BPy_Curve * self, char *name, PyObject * value ) error = Curve_setBevOb( self, valtuple ); else if( strcmp( name, "taperob" ) == 0 ) error = Curve_setTaperOb( self, valtuple ); + else if( strcmp( name, "materials" ) == 0 ) + error = Curve_setMaterials( self, value ); else { /* Error */ Py_DECREF( valtuple ); diff --git a/source/blender/python/api2_2x/doc/Curve.py b/source/blender/python/api2_2x/doc/Curve.py index 64dc582ac5a..00ef78ec0eb 100644 --- a/source/blender/python/api2_2x/doc/Curve.py +++ b/source/blender/python/api2_2x/doc/Curve.py @@ -130,6 +130,15 @@ class Curve: @type bevob: Blender L{Object} or None @ivar key: The Key object associated with this Curve, if any. @type key: Blender L{Key} + @ivar materials: The curves's materials. Each curve can reference up to + 16 materials. Empty slots in the curve's list are represented by B{None}. + B{Note}: L{Object.colbits} needs to be set correctly + for each object in order for these materials to be used instead of + the object's materials. + B{Note}: The list that's returned is I{not} linked to the original curve. + curve.materials.append(material) won't do anything. + Use curve.materials += [material] instead. + @type materials: list of L{Material}s """ def getName(): diff --git a/source/blender/python/api2_2x/doc/Mesh.py b/source/blender/python/api2_2x/doc/Mesh.py index 0219613a7e5..2b933c4f04e 100644 --- a/source/blender/python/api2_2x/doc/Mesh.py +++ b/source/blender/python/api2_2x/doc/Mesh.py @@ -718,12 +718,12 @@ class Mesh: B{Note}: L{Object.colbits} needs to be set correctly for each object in order for these materials to be used instead of the object's materials. - B{Note}: Making the material list shorter does not change the faces material indices, - take care when using the faces material indices to reference a material in the materials list. - B{Note}: The the list thats returned is not linked to the original mesh. - mesh.materials.append(material) wont do anything. + B{Note}: Making the material list shorter does not change the face's material indices. + Take care when using the face's material indices to reference a material in this list. + B{Note}: The list that's returned is I{not} linked to the original mesh. + mesh.materials.append(material) won't do anything. Use mesh.materials += [material] instead. - @type materials: list of Materials + @type materials: list of L{Material}s @ivar degr: The max angle for auto smoothing in [1,80]. @type degr: int @ivar maxSmoothAngle: Same as L{degr}. This attribute is only for