Added python access to material and texture colorbands.

mat.colorbandDiffuse
	mat.colorbandSpecular
	tex.colorband

gen_utils - removed unused func
This commit is contained in:
2007-03-08 06:35:01 +00:00
parent 4bd5ab628b
commit 8e0704f59a
7 changed files with 193 additions and 21 deletions

View File

@@ -42,6 +42,7 @@
#include "BKE_material.h"
#include "BKE_texture.h"
#include "BKE_idprop.h"
#include "BKE_utildefines.h" /* for CLAMP */
#include "MEM_guardedalloc.h"
#include "BLI_blenlib.h"
#include "BSE_editipo.h"
@@ -520,6 +521,7 @@ static PyObject *Material_getColorComponent( BPy_Material * self,
void * closure );
static PyObject *Material_getOopsLoc( BPy_Material * self );
static PyObject *Material_getOopsSel( BPy_Material * self );
/*static int Material_setSeptex( BPy_Material * self, PyObject * value );
static PyObject *Material_getSeptex( BPy_Material * self );*/
@@ -590,6 +592,8 @@ static PyObject *Material_addScriptLink(BPy_Material * self, PyObject * args );
static PyObject *Material_clearScriptLinks(BPy_Material *self, PyObject *args);
static PyObject *Material_insertIpoKey( BPy_Material * self, PyObject * args );
static PyObject *Material_getColorband( BPy_Material * self, void * type);
int Material_setColorband( BPy_Material * self, PyObject * value, void * type);
static PyObject *Material_copy( BPy_Material * self );
@@ -1062,6 +1066,14 @@ static PyGetSetDef BPy_Material_getseters[] = {
(getter)Material_getColorComponent, (setter)Material_setColorComponent,
"Diffuse color blue component",
(void *) EXPP_MAT_COMP_B },
{"colorbandDiffuse",
(getter)Material_getColorband, (setter)Material_setColorband,
"Set the light group for this material",
(void *) 0},
{"colorbandSpecular",
(getter)Material_getColorband, (setter)Material_setColorband,
"Set the light group for this material",
(void *) 1},
{NULL,NULL,NULL,NULL,NULL} /* Sentinel */
};
@@ -2466,6 +2478,110 @@ static PyObject *Material_repr( BPy_Material * self )
self->material->id.name + 2 );
}
/*****************************************************************************/
/* These functions are used here and in in Texture.c */
/*****************************************************************************/
PyObject *EXPP_PyList_fromColorband( ColorBand *coba )
{
short i;
PyObject *cbls;
PyObject *colls;
if (!coba)
return PyList_New( 0 );
cbls = PyList_New( coba->tot );
for (i=0; i < coba->tot; i++) {
colls = PyList_New( 5 );
PyList_SET_ITEM( colls, 0, PyFloat_FromDouble(coba->data[i].r) );
PyList_SET_ITEM( colls, 1, PyFloat_FromDouble(coba->data[i].g) );
PyList_SET_ITEM( colls, 2, PyFloat_FromDouble(coba->data[i].b) );
PyList_SET_ITEM( colls, 3, PyFloat_FromDouble(coba->data[i].a) );
PyList_SET_ITEM( colls, 4, PyFloat_FromDouble(coba->data[i].pos) );
PyList_SET_ITEM(cbls, i, colls);
}
return cbls;
}
/* make sure you coba is not none before calling this */
int EXPP_Colorband_fromPyList( ColorBand *coba, PyObject * value )
{
short totcol, i;
PyObject *colseq;
PyObject *pyflt;
float f;
if ( !PySequence_Check( value ) )
return ( EXPP_ReturnIntError( PyExc_TypeError,
"Colorband must be a sequence" ) );
totcol = PySequence_Size(value);
if (totcol < 1 || totcol > 31)
return ( EXPP_ReturnIntError( PyExc_ValueError,
"Colorband must be between 1 and 31 in length" ) );
for (i=0; i<totcol; i++) {
colseq = PySequence_GetItem( value, i );
if ( !PySequence_Check( colseq ) || PySequence_Size( colseq ) != 5) {
Py_DECREF ( colseq );
return ( EXPP_ReturnIntError( PyExc_ValueError,
"Colorband colors must be sequences of 5 floats" ) );
}
for (i=0; i<5; i++) {
pyflt = PySequence_GetItem( colseq, i );
if (!PyNumber_Check(pyflt)) {
return ( EXPP_ReturnIntError( PyExc_ValueError,
"Colorband colors must be sequences of 5 floats" ) );
Py_DECREF ( pyflt );
Py_DECREF ( colseq );
}
Py_DECREF ( pyflt );
}
Py_DECREF ( colseq );
}
/* ok, continue - should check for 5 floats, will ignore non floats for now */
coba->tot = totcol;
for (i=0; i<totcol; i++) {
colseq = PySequence_GetItem( value, i );
pyflt = PySequence_GetItem( colseq, 0 );
f = (float)PyFloat_AsDouble( pyflt );
CLAMP(f, 0.0, 1.0);
coba->data[i].r = f;
Py_DECREF ( pyflt );
pyflt = PySequence_GetItem( colseq, 1 );
f = (float)PyFloat_AsDouble( pyflt );
CLAMP(f, 0.0, 1.0);
coba->data[i].g = f;
Py_DECREF ( pyflt );
pyflt = PySequence_GetItem( colseq, 2 );
f = (float)PyFloat_AsDouble( pyflt );
CLAMP(f, 0.0, 1.0);
coba->data[i].b = f;
Py_DECREF ( pyflt );
pyflt = PySequence_GetItem( colseq, 3 );
f = (float)PyFloat_AsDouble( pyflt );
CLAMP(f, 0.0, 1.0);
coba->data[i].a = f;
Py_DECREF ( pyflt );
pyflt = PySequence_GetItem( colseq, 4 );
f = (float)PyFloat_AsDouble( pyflt );
CLAMP(f, 0.0, 1.0);
coba->data[i].pos = f;
Py_DECREF ( pyflt );
Py_DECREF ( colseq );
}
return 0;
}
/*****************************************************************************/
/* These functions are used in NMesh.c and Object.c */
/*****************************************************************************/
@@ -2683,6 +2799,32 @@ static PyObject *Material_getOopsLoc ( BPy_Material * self )
Py_RETURN_NONE;
}
static PyObject *Material_getColorband( BPy_Material * self, void * type)
{
switch( (long)type ) {
case 0: /* these are backwards, but that how it works */
return EXPP_PyList_fromColorband( self->material->ramp_col );
case 1:
return EXPP_PyList_fromColorband( self->material->ramp_spec );
}
Py_RETURN_NONE;
}
int Material_setColorband( BPy_Material * self, PyObject * value, void * type)
{
switch( (long)type ) {
case 0: /* these are backwards, but that how it works */
if (!self->material->ramp_col)
self->material->ramp_col = MEM_callocN( sizeof(ColorBand), "colorband");
return EXPP_Colorband_fromPyList( self->material->ramp_col, value );
case 1:
if (!self->material->ramp_spec)
self->material->ramp_spec = MEM_callocN( sizeof(ColorBand), "colorband");
return EXPP_Colorband_fromPyList( self->material->ramp_spec, value );
}
return 0;
}
static PyObject *Material_getOopsSel ( BPy_Material * self )
{
if( G.soops ) {

View File

@@ -36,6 +36,7 @@
#include <Python.h>
#include "DNA_object_types.h"
#include "DNA_material_types.h"
#include "DNA_texture_types.h" /* colorband */
#include "rgbTuple.h"
/*****************************************************************************/
@@ -63,6 +64,10 @@ PyObject *Material_CreatePyObject( Material * mat );
Material *Material_FromPyObject( PyObject * pyobj );
int Material_CheckPyObject( PyObject * pyobj );
/* colorband tp_getseters */
PyObject *EXPP_PyList_fromColorband( ColorBand *coba );
int EXPP_Colorband_fromPyList( ColorBand *coba, PyObject * value );
/* Some functions needed by NMesh, Curve and friends */
PyObject *EXPP_PyList_fromMaterialList( Material ** matlist, int len,
int all );

View File

@@ -54,6 +54,7 @@
#include "gen_utils.h"
#include "vector.h" /* for Texture_evaluate(vec) */
#include "Material.h" /* for EXPP_Colorband_fromPyList and EXPP_PyList_fromColorband */
#include "RE_shader_ext.h"
/*****************************************************************************/
@@ -201,9 +202,7 @@ static const EXPP_map_pair tex_type_map[] = {
static const EXPP_map_pair tex_flag_map[] = {
/* NOTE "CheckerOdd" and "CheckerEven" are new */
#if 0
{"ColorBand", TEX_COLORBAND },
#endif
{"FlipBlend", TEX_FLIPBLEND},
{"NegAlpha", TEX_NEGALPHA},
{"CheckerOdd",TEX_CHECKER_ODD},
@@ -491,8 +490,11 @@ static int Texture_setImageFlags( BPy_Texture *self, PyObject *args,
static int Texture_setNoiseBasis2( BPy_Texture *self, PyObject *args,
void *type );
static PyObject *Texture_getColorband( BPy_Texture * self);
int Texture_setColorband( BPy_Texture * self, PyObject * value);
static PyObject *Texture_evaluate( BPy_Texture *self, PyObject *args );
/*****************************************************************************/
/* Python BPy_Texture methods table: */
/*****************************************************************************/
@@ -754,6 +756,10 @@ static PyGetSetDef BPy_Texture_getseters[] = {
(getter)Texture_getImageFlags, (setter)Texture_setImageFlags,
"Use of image RGB values for normal mapping enabled ('ImageFlags')",
(void *)TEX_NORMALMAP},
{"colorband",
(getter)Texture_getColorband, (setter)Texture_setColorband,
"Use of image RGB values for normal mapping enabled ('ImageFlags')",
(void *)TEX_NORMALMAP},
{NULL,NULL,NULL,NULL,NULL} /* Sentinel */
};
@@ -1517,9 +1523,7 @@ static int Texture_setFlags( BPy_Texture * self, PyObject * value )
{
int param;
int bitmask = TEX_FLIPBLEND
#if 0
| TEX_COLORBAND
#endif
| TEX_NEGALPHA
| TEX_CHECKER_ODD
| TEX_CHECKER_EVEN;
@@ -2651,6 +2655,18 @@ static PyObject *Texture_oldsetImageFlags( BPy_Texture * self, PyObject * args )
Py_RETURN_NONE;
}
static PyObject *Texture_getColorband( BPy_Texture * self)
{
return EXPP_PyList_fromColorband( self->texture->coba );
}
int Texture_setColorband( BPy_Texture * self, PyObject * value)
{
if (!self->texture->coba)
self->texture->coba = MEM_callocN( sizeof(ColorBand), "colorband");
return EXPP_Colorband_fromPyList( self->texture->coba, value );
}
static PyObject *Texture_evaluate( BPy_Texture * self, PyObject * args )
{
VectorObject *vec_in = NULL;
@@ -2672,4 +2688,3 @@ static PyObject *Texture_evaluate( BPy_Texture * self, PyObject * args )
return newVectorObject(vec, 4, Py_NEW);
}

View File

@@ -282,9 +282,24 @@ class Material:
@type lightGroup: Group or None
@ivar uvlayer: The uv layer name to use, when UV mapping is enabled.
@type uvlayer: string
@ivar colorband: Material colorband, a list of colors,
each color a list of 5 floats [0 - 1], [r,g,b,a,pos].
The colorband can have between 1 and 31 colors.
@type colorband: list
@ivar colorbandDiffuse: Material colorband, a list of colors,
each color a list of 5 floats [0 - 1], [r,g,b,a,pos].
The colorband can have between 1 and 31 colors.
@type colorbandDiffuse: list
@ivar colorbandSpecular: Material colorband, a list of colors,
each color a list of 5 floats [0 - 1], [r,g,b,a,pos].
The colorband can have between 1 and 31 colors.
@type colorbandSpecular: list
@warning: Most member variables assume values in some [Min, Max] interval.
When trying to set them, the given parameter will be clamped to lie in
that range: if val < Min, then val = Min, if val > Max, then val = Max.
When trying to set them, the given parameter will be clamped to lie in
that range: if val < Min, then val = Min, if val > Max, then val = Max.
"""
def getName():

View File

@@ -381,6 +381,10 @@ class Texture:
@ivar weight4: Weight 4 (for Voronoi textures).
Value is clamped to the range [-2.0,2.0].
@type weight4: float
@ivar colorband: Texture colorband, a list of colors,
each color a list of 5 floats [0 - 1], [r,g,b,a,pos].
The colorband can have between 1 and 31 colors.
@type colorband: list
"""
def getExtend():

View File

@@ -164,18 +164,6 @@ ID *GetIdFromList( ListBase * list, char *name )
return id;
}
/*****************************************************************************/
/* Description: This function sets the fake user status of the ID */
/* returns an int error, so from getsetattrs */
/*****************************************************************************/
PyObject *EXPP_GetIdLib( ID * id )
{
if (id->lib)
return PyString_FromString(id->lib->name);
else
return EXPP_incr_ret( Py_None );
}
/*****************************************************************************/
/* Description: These functions set an internal string with the given type */
/* and error_msg arguments. */
@@ -1024,7 +1012,11 @@ PyObject *GenericLib_getLib( void *self )
{
ID *id = ((BPy_GenericLib *)self)->id;
if (!id) return ( EXPP_ReturnPyObjError( PyExc_RuntimeError, "data has been removed" ) );
return EXPP_GetIdLib(id);
if (id->lib)
return PyString_FromString(id->lib->name);
else
return EXPP_incr_ret( Py_None );
}
PyObject *GenericLib_getUsers( void *self )

View File

@@ -69,7 +69,6 @@ PyObject *EXPP_GetModuleConstant(char *module, char *constant);
int StringEqual( const char *string1, const char *string2 );
char *GetIdName( ID * id );
PyObject *EXPP_GetIdLib( ID * id );
int SetIdFakeUser( ID * id, PyObject *value);
ID *GetIdFromList( ListBase * list, char *name );