Revision: 16184
          
http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=16184
Author:   khughes
Date:     2008-08-19 00:39:55 +0200 (Tue, 19 Aug 2008)

Log Message:
-----------
Python API
----------
Add Material.enabledTextures attribute; this allows users to enable and
disable textures assigned to a material.

Modified Paths:
--------------
    trunk/blender/source/blender/python/api2_2x/Material.c
    trunk/blender/source/blender/python/api2_2x/doc/Material.py

Modified: trunk/blender/source/blender/python/api2_2x/Material.c
===================================================================
--- trunk/blender/source/blender/python/api2_2x/Material.c      2008-08-18 
22:22:56 UTC (rev 16183)
+++ trunk/blender/source/blender/python/api2_2x/Material.c      2008-08-18 
22:39:55 UTC (rev 16184)
@@ -554,6 +554,7 @@
 static int Material_setSssFront( BPy_Material * self, PyObject * value );
 static int Material_setSssBack( BPy_Material * self, PyObject * value );
 static int Material_setSssBack( BPy_Material * self, PyObject * value );
+static int Material_setTexChannel( BPy_Material * self, PyObject * value );
 
 static PyObject *Material_getColorComponent( BPy_Material * self,
                                                        void * closure );
@@ -633,6 +634,7 @@
 static PyObject *Material_getFilter( BPy_Material * self );
 static PyObject *Material_getTranslucency( BPy_Material * self );
 static PyObject *Material_getTextures( BPy_Material * self );
+static PyObject *Material_getTexChannel( BPy_Material * self );
 static PyObject *Material_clearIpo( BPy_Material * self );
 
 static PyObject *Material_setTexture( BPy_Material * self, PyObject * args );
@@ -1140,8 +1142,12 @@
         NULL},
        {"lightGroup",
         (getter)Material_getLightGroup, (setter)Material_setLightGroup,
-        "Set the light group for this material",
+        "The light group for this material",
         NULL},
+       {"enabledTextures",
+        (getter)Material_getTexChannel, (setter)Material_setTexChannel,
+        "Enabled texture channels for this material",
+        NULL},
        {"R",
         (getter)Material_getColorComponent, (setter)Material_setColorComponent,
         "Diffuse color red component",
@@ -1517,6 +1523,36 @@
        return Group_CreatePyObject( self->material->group );
 }
 
+static PyObject *Material_getTexChannel( BPy_Material * self )
+{
+       int i;
+    short mask = 1;
+       PyObject *list = PyList_New(0);
+       if( !list )
+               return EXPP_ReturnPyObjError( PyExc_MemoryError,
+                               "PyList_New() failed" );
+
+       for( i = 0, mask = 1; i < MAX_MTEX ; ++i, mask <<= 1 ) {
+               if( self->material->mtex[i] && (mask & self->material->septex) 
== 0 ) {
+                       PyObject * val = PyInt_FromLong(i);
+                       if( !val ) {
+                               Py_DECREF( list );
+                               return EXPP_ReturnPyObjError( PyExc_MemoryError,
+                                               "PyInt_FromLong() failed" );
+                       }
+                       if( PyList_Append( list, val ) < 0 ) {
+                               Py_DECREF( val );
+                               Py_DECREF( list );
+                               return EXPP_ReturnPyObjError( 
PyExc_RuntimeError,
+                                               "PyList_Append() failed" );
+                       }
+                       Py_DECREF( val );
+               }
+       }
+
+       return list;
+}
+
 static PyObject *Material_getHaloSize( BPy_Material * self )
 {
        return PyFloat_FromDouble( ( double ) self->material->hasize );
@@ -1982,6 +2018,57 @@
        return GenericLib_assignData(value, (void **) &self->material->group, 
NULL, 1, ID_GR, 0);
 }
 
+static int Material_setTexChannel( BPy_Material * self, PyObject * value )
+{
+       int i, mask;
+       short septex = 0;
+       int result = 1;
+
+       /* fail if input is not a standard sequence */
+       if( !PyList_Check( value ) && !PyTuple_Check( value ) )
+               return EXPP_ReturnIntError( PyExc_TypeError,
+                                               "expected tuple or list of 
integers" );
+
+       /* get a fast sequence; in Python 2.5, this just return the original
+        * list or tuple and INCREFs it, so we must DECREF */
+       value = PySequence_Fast( value, "" );
+
+       /* set the disable bit for each existing texture */
+       for( i= 0, mask= 1; i < MAX_MTEX; ++i, mask <<= 1 )
+               if( self->material->mtex[i] != NULL )
+                       septex |= mask;
+
+       /* check the list, and build new septex value */
+       for( i= PySequence_Size(value)-1; i >= 0; --i ) {
+               long ival;
+               PyObject *item = PySequence_Fast_GET_ITEM( value, i );
+               if( !PyInt_Check( item ) ) {
+                       PyErr_SetString ( PyExc_TypeError,
+                                       "expected tuple or list of integers" );
+                       goto exit;
+               }
+               ival= PyInt_AsLong( item );
+               if(ival < 0 || ival > MAX_MTEX) {
+                       PyErr_SetString( PyExc_ValueError,
+                                                       "channel value out of 
range" );
+                       goto exit;
+               }
+               ival&= (1<<MAX_MTEX)-1;
+               if( self->material->mtex[(int)ival] == NULL ) {
+                       PyErr_SetString( PyExc_ValueError,
+                                                       "channels must have a 
texture assigned" );
+                       goto exit;
+               }
+               septex&= ~(1<<ival);
+       }
+       self->material->septex= septex;
+       result = 0;
+
+exit:
+       Py_DECREF(value);
+       return result;
+}
+
 static int Material_setAdd( BPy_Material * self, PyObject * value )
 {
        return EXPP_setFloatClamped ( value, &self->material->add,
@@ -2313,9 +2400,6 @@
                                                                
EXPP_MAT_SSS_BACK_MAX);
 }
 
-
-
-
 static PyObject *Material_setTexture( BPy_Material * self, PyObject * args )
 {
        int texnum;

Modified: trunk/blender/source/blender/python/api2_2x/doc/Material.py
===================================================================
--- trunk/blender/source/blender/python/api2_2x/doc/Material.py 2008-08-18 
22:22:56 UTC (rev 16183)
+++ trunk/blender/source/blender/python/api2_2x/doc/Material.py 2008-08-18 
22:39:55 UTC (rev 16184)
@@ -323,6 +323,21 @@
        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
+       @type enabledTextures: list of integers
+       @ivar enabledTextures: The texture channels enabled in this material.
+               The attribute returns is list of integers in the range [0, 9], 
each
+               number representing the respective enabled MTex entry (see
+               L{getTextures()<getTextures>}).  Enabling is done by assigning
+               a list of ints or an empty list.  Attempting to enable a channel
+               which does not have a texture assigned to it will result in a
+               ValueError exception.
+               Example::
+                       mat.enabledTextures = []  # no texture channels are 
enabled
+                       mat.enabledTextures = [0, 6] # texture channels 0 and 6 
are enabled
+                       ch = mat.enabledTextures
+                       ch.append(4)
+                       mat.enabledTextures = ch
+                       print mat.enabledTextures # will print: [0, 4, 6]
        
        @ivar enableSSS:  If True, subsurface scattering will be rendered on 
this material.
        @type enableSSS:  bool
@@ -1010,7 +1025,7 @@
 
        def setTexture(index, texture, texco, mapto):
                """
-               Assign a Blender Texture object to slot number 'number'.
+               Assign a Blender Texture object to channel number 'number'.
                @type index: int
                @param index: material's texture index in [0, 9].
                @type texture: Blender Texture
@@ -1033,7 +1048,7 @@
                Get this Material's Texture list.
                @rtype: list of MTex
                @return: a list of Blender MTex objects.  None is returned for 
each empty
-                               texture slot.
+                               texture channel.
                """
 
        def getScriptLinks (event):


_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to