[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [16427] trunk/blender/source/blender/ python/api2_2x: Python API

Ken Hughes khughes at pacific.edu
Tue Sep 9 01:39:32 CEST 2008


Revision: 16427
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=16427
Author:   khughes
Date:     2008-09-09 01:39:32 +0200 (Tue, 09 Sep 2008)

Log Message:
-----------
Python API
----------
Add access to MTex objects from Lamps and Worlds (first pass).  Since the
MTex structure is slightly difference between materials, lamps, and worlds,
a field is added to the BPy MTex object to distinquish which type it wraps.

Attempting to access attributes which are unique to materials for lamp or
world MTex objects throw an exception.  The next pass will implement MTex
attributes which are specific to Lamps and Worlds.

A new attribute (textures) is added to each module.  It is compatible with
the previous Material.getTextures(), which returns a tuple of either MTex
objects or None.  Surprised we never added an attribute for this before in
all the changes and refactoring.

Modified Paths:
--------------
    trunk/blender/source/blender/python/api2_2x/Lamp.c
    trunk/blender/source/blender/python/api2_2x/MTex.c
    trunk/blender/source/blender/python/api2_2x/MTex.h
    trunk/blender/source/blender/python/api2_2x/Material.c
    trunk/blender/source/blender/python/api2_2x/World.c
    trunk/blender/source/blender/python/api2_2x/doc/Lamp.py
    trunk/blender/source/blender/python/api2_2x/doc/Material.py
    trunk/blender/source/blender/python/api2_2x/doc/Texture.py
    trunk/blender/source/blender/python/api2_2x/doc/World.py

Modified: trunk/blender/source/blender/python/api2_2x/Lamp.c
===================================================================
--- trunk/blender/source/blender/python/api2_2x/Lamp.c	2008-09-08 22:36:32 UTC (rev 16426)
+++ trunk/blender/source/blender/python/api2_2x/Lamp.c	2008-09-08 23:39:32 UTC (rev 16427)
@@ -39,6 +39,7 @@
 #include "BSE_editipo.h"
 #include "mydevice.h"
 #include "Ipo.h"
+#include "MTex.h" 
 #include "constant.h"
 #include "gen_utils.h"
 #include "gen_library.h"
@@ -206,6 +207,7 @@
 static PyObject *Lamp_getCol( BPy_Lamp * self );
 static PyObject *Lamp_getIpo( BPy_Lamp * self );
 static PyObject *Lamp_getComponent( BPy_Lamp * self, void * closure );
+static PyObject *Lamp_getTextures( BPy_Lamp * self );
 static PyObject *Lamp_clearIpo( BPy_Lamp * self );
 static PyObject *Lamp_insertIpoKey( BPy_Lamp * self, PyObject * args );
 static PyObject *Lamp_oldsetIpo( BPy_Lamp * self, PyObject * args );
@@ -500,6 +502,10 @@
 	 (getter)Lamp_getComponent, (setter)Lamp_setComponent,
 	 "Lamp color blue component",
 	 (void *)EXPP_LAMP_COMP_B},
+	{"textures",
+	 (getter)Lamp_getTextures, (setter)NULL,
+     "The Lamp's texture list as a tuple",
+	 NULL},
 	{"Modes",
 	 (getter)Lamp_getModesConst, (setter)NULL,
 	 "Dictionary of values for 'mode' attribute",
@@ -1393,6 +1399,30 @@
 				      "Photon", EXPP_LAMP_TYPE_YF_PHOTON );
 }
 
+static PyObject *Lamp_getTextures( BPy_Lamp * self )
+{
+	int i;
+	PyObject *tuple;
+
+	/* build a texture list */
+	tuple = PyTuple_New( MAX_MTEX );
+	if( !tuple )
+		return EXPP_ReturnPyObjError( PyExc_MemoryError,
+					      "couldn't create PyTuple" );
+
+	for( i = 0; i < MAX_MTEX; ++i ) {
+		struct MTex *mtex = self->lamp->mtex[i];
+		if( mtex ) {
+			PyTuple_SET_ITEM( tuple, i, MTex_CreatePyObject( mtex, ID_LA ) );
+		} else {
+			Py_INCREF( Py_None );
+			PyTuple_SET_ITEM( tuple, i, Py_None );
+		}
+	}
+
+	return tuple;
+}
+
 /* #####DEPRECATED###### */
 
 static PyObject *Lamp_oldsetSamples( BPy_Lamp * self, PyObject * args )

Modified: trunk/blender/source/blender/python/api2_2x/MTex.c
===================================================================
--- trunk/blender/source/blender/python/api2_2x/MTex.c	2008-09-08 22:36:32 UTC (rev 16426)
+++ trunk/blender/source/blender/python/api2_2x/MTex.c	2008-09-08 23:39:32 UTC (rev 16427)
@@ -26,6 +26,7 @@
  *
  * ***** END GPL LICENSE BLOCK *****
 */
+
 #include "MTex.h" /*This must come first*/
 
 #include "BKE_utildefines.h"
@@ -252,7 +253,7 @@
 	return submodule;
 }
 
-PyObject *MTex_CreatePyObject( MTex * mtex )
+PyObject *MTex_CreatePyObject( MTex * mtex, unsigned short type )
 {
 	BPy_MTex *pymtex;
 
@@ -262,6 +263,7 @@
 					      "couldn't create BPy_MTex PyObject" );
 
 	pymtex->mtex = mtex;
+	pymtex->type = type;
 	return ( PyObject * ) pymtex;
 }
 
@@ -286,7 +288,12 @@
 
 static PyObject *MTex_repr( BPy_MTex * self )
 {
-	return PyString_FromFormat( "[MTex]" );
+	if( self->type == ID_MA )
+		return PyString_FromFormat( "[MTex (Material)]" );
+	else if( self->type == ID_LA )
+		return PyString_FromFormat( "[MTex (Lamp)]" );
+	else
+		return PyString_FromFormat( "[MTex (World)]" );
 }
 
 
@@ -350,6 +357,10 @@
 
 static PyObject *MTex_getUVLayer( BPy_MTex *self, void *closure )
 {
+	if( self->type != ID_MA )
+		return EXPP_ReturnPyObjError( PyExc_AttributeError,
+				"not a Material MTex object" );
+
 	return PyString_FromString(self->mtex->uvname);
 }
 
@@ -364,6 +375,10 @@
 
 static PyObject *MTex_getMapTo( BPy_MTex *self, void *closure )
 {
+	if( self->type != ID_MA )
+		return EXPP_ReturnPyObjError( PyExc_AttributeError,
+				"not a Material MTex object" );
+
 	return PyInt_FromLong( self->mtex->mapto );
 }
 
@@ -371,18 +386,20 @@
 {
 	int mapto;
 
-	if( !PyInt_Check( value ) ) {
+	if( self->type != ID_MA )
+		return EXPP_ReturnIntError( PyExc_AttributeError,
+				"not a material MTex object" );
+
+	if( !PyInt_Check( value ) )
 		return EXPP_ReturnIntError( PyExc_TypeError,
-			"expected an int" );
-	}
+				"expected an int" );
 
 	mapto = PyInt_AsLong( value );
 
 	/* This method is deprecated anyway. */
-	if ( mapto < 0 || mapto > 16383 ) {
+	if ( mapto < 0 || mapto > 16383 )
 		return EXPP_ReturnIntError( PyExc_ValueError,
 			"Value must be a sum of values from Texture.MapTo dictionary" );
-	}
 
 	self->mtex->mapto = (short)mapto;
 
@@ -400,11 +417,9 @@
 	float rgb[3];
 	int i;
 
-	if( !PyArg_ParseTuple( value, "fff",
-		&rgb[0], &rgb[1], &rgb[2] ) )
-
+	if( !PyArg_ParseTuple( value, "fff", &rgb[0], &rgb[1], &rgb[2] ) )
 		return EXPP_ReturnIntError( PyExc_TypeError,
-					      "expected tuple of 3 floats" );
+				"expected tuple of 3 floats" );
 
 	for( i = 0; i < 3; ++i )
 		if( rgb[i] < 0 || rgb[i] > 1 )
@@ -478,117 +493,84 @@
 
 static int MTex_setColFac( BPy_MTex *self, PyObject *value, void *closure)
 {
-	float f;
-
-	if ( !PyFloat_Check( value ) )
-		return EXPP_ReturnIntError( PyExc_TypeError,
-						"expected a float" );
-
-	f = (float)PyFloat_AsDouble(value);
-
-	if (f < 0 || f > 1)
-		return EXPP_ReturnIntError( PyExc_ValueError,
-					      "values must be in range [0,1]" );
-
-	self->mtex->colfac = f;
-
-	return 0;
+	return EXPP_setFloatRange( value, &self->mtex->colfac, 0.0f, 1.0f );
 }
 
 static PyObject *MTex_getNorFac( BPy_MTex *self, void *closure )
 {
+	if( self->type == ID_LA )
+		return EXPP_ReturnPyObjError( PyExc_AttributeError,
+				"not a material or world MTex object" );
+
 	return PyFloat_FromDouble(self->mtex->norfac);
 }
 
 static int MTex_setNorFac( BPy_MTex *self, PyObject *value, void *closure)
 {
-	float f;
-
-	if ( !PyFloat_Check( value ) )
-		return EXPP_ReturnIntError( PyExc_TypeError,
-						"expected a float" );
-
-	f = (float)PyFloat_AsDouble(value);
-
-	if (f < 0 || f > 25)
-		return EXPP_ReturnIntError( PyExc_ValueError,
-					      "values must be in range [0,25]" );
-
-	self->mtex->norfac = f;
-
-	return 0;
+	switch( self->type )
+	{
+	case ID_WO:
+		return EXPP_setFloatRange( value, &self->mtex->norfac, 0.0f, 1.0f );
+	case ID_MA:
+		return EXPP_setFloatRange( value, &self->mtex->norfac, 0.0f, 25.0f );
+	default:
+		return EXPP_ReturnIntError( PyExc_AttributeError,
+				"not a material or world MTex object" );
+	}
 }
 
 static PyObject *MTex_getVarFac( BPy_MTex *self, void *closure )
 {
+	if( self->type == ID_LA )
+		return EXPP_ReturnPyObjError( PyExc_AttributeError,
+				"not a material or world MTex object" );
+
 	return PyFloat_FromDouble(self->mtex->varfac);
 }
 
 static int MTex_setVarFac( BPy_MTex *self, PyObject *value, void *closure)
 {
-	float f;
+	if( self->type == ID_LA )
+		return EXPP_ReturnIntError( PyExc_AttributeError,
+				"not a material or world MTex object" );
 
-	if ( !PyFloat_Check( value ) )
-		return EXPP_ReturnIntError( PyExc_TypeError,
-						"expected a float" );
-
-	f = (float)PyFloat_AsDouble(value);
-
-	if (f < 0 || f > 1)
-		return EXPP_ReturnIntError( PyExc_ValueError,
-					      "values must be in range [0,1]" );
-
-	self->mtex->varfac = f;
-
-	return 0;
+	return EXPP_setFloatRange( value, &self->mtex->varfac, 0.0f, 1.0f );
 }
 
 static PyObject *MTex_getDispFac( BPy_MTex *self, void *closure )
 {
+	if( self->type != ID_MA )
+		return EXPP_ReturnPyObjError( PyExc_AttributeError,
+				"not a material MTex object" );
+
 	return PyFloat_FromDouble(self->mtex->dispfac);
 }
 
 static int MTex_setDispFac( BPy_MTex *self, PyObject *value, void *closure)
 {
-	float f;
+	if( self->type != ID_MA )
+		return EXPP_ReturnIntError( PyExc_AttributeError,
+				"not a material MTex object" );
 
-	if ( !PyFloat_Check( value ) )
-		return EXPP_ReturnIntError( PyExc_TypeError,
-						"expected a float" );
-
-	f = (float)PyFloat_AsDouble(value);
-
-	if (f < 0 || f > 1)
-		return EXPP_ReturnIntError( PyExc_ValueError,
-					      "values must be in range [0,1]" );
-
-	self->mtex->dispfac = f;
-
-	return 0;
+	return EXPP_setFloatRange( value, &self->mtex->dispfac, 0.0f, 1.0f );
 }
 
 static PyObject *MTex_getWarpFac( BPy_MTex *self, void *closure )
 {
+	if( self->type != ID_MA )
+		return EXPP_ReturnPyObjError( PyExc_AttributeError,
+				"not a material MTex object" );
+
 	return PyFloat_FromDouble(self->mtex->warpfac);
 }
 
 static int MTex_setWarpFac( BPy_MTex *self, PyObject *value, void *closure)
 {
-	float f;
+	if( self->type != ID_MA )
+		return EXPP_ReturnIntError( PyExc_AttributeError,
+				"not a material MTex object" );
 
-	if ( !PyFloat_Check( value ) )
-		return EXPP_ReturnIntError( PyExc_TypeError,
-						"expected a float" );
-
-	f = (float)PyFloat_AsDouble(value);
-
-	if (f < 0 || f > 1)
-		return EXPP_ReturnIntError( PyExc_ValueError,
-					      "values must be in range [0,1]" );
-
-	self->mtex->warpfac = f;
-
-	return 0;
+	return EXPP_setFloatRange( value, &self->mtex->warpfac, 0.0f, 1.0f );
 }
 
 static PyObject *MTex_getOfs( BPy_MTex *self, void *closure )
@@ -601,16 +583,24 @@
 {
 	float f[3];
 	int i;
+	float max;
 
 	if( !PyArg_ParseTuple( value, "fff", &f[0], &f[1], &f[2] ) )
-
 		return EXPP_ReturnIntError( PyExc_TypeError,
 					      "expected tuple of 3 floats" );
 
+	if( self->type == ID_MA )
+		max = 10.0f;
+	else
+		max = 20.0f;
+
 	for( i = 0; i < 3; ++i )
-		if( f[i] < -10 || f[i] > 10 )
-			return EXPP_ReturnIntError( PyExc_ValueError,
-					      "values must be in range [-10,10]" );
+		if( f[i] < -max || f[i] > max ) {
+			char errstr[64];
+			sprintf( errstr, "values must be in range [-%6.0f,%6.0f]",
+					max, max );
+			return EXPP_ReturnIntError( PyExc_ValueError, errstr );
+		}
 
 	self->mtex->ofs[0] = f[0];
 	self->mtex->ofs[1] = f[1];
@@ -649,6 +639,10 @@
 
 static PyObject *MTex_getMapping( BPy_MTex *self, void *closure )
 {
+	if( self->type != ID_MA )
+		return EXPP_ReturnPyObjError( PyExc_AttributeError,
+				"not a material MTex object" );
+
 	return PyInt_FromLong( self->mtex->mapping );
 }
 
@@ -656,6 +650,11 @@
 {
 	int n;
 
+	if( self->type != ID_MA )
+		return EXPP_ReturnIntError( PyExc_AttributeError,
+				"not a material MTex object" );
+
+
 	if ( !PyInt_Check( value ) )
 		return EXPP_ReturnIntError( PyExc_TypeError,
 				"Value must be member of Texture.Mappings dictionary" );
@@ -664,8 +663,7 @@
 
 /*	if (n != MTEX_FLAT && n != MTEX_TUBE && n != MTEX_CUBE &&
 		n != MTEX_SPHERE) */
-	if (n < 0 || n > 3)
-	{
+	if (n < 0 || n > 3) {
 		return EXPP_ReturnIntError( PyExc_ValueError,
 			    "Value must be member of Texture.Mappings dictionary" );
 	}
@@ -696,6 +694,10 @@
 
 static PyObject *MTex_getProjX( BPy_MTex *self, void *closure )
 {
+	if( self->type != ID_MA )
+		return EXPP_ReturnPyObjError( PyExc_AttributeError,
+				"not a material MTex object" );
+
 	return PyInt_FromLong( self->mtex->projx );
 }
 

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list