[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [11284] branches/pyapi_devel/source/ blender/python/api2_2x: New constants interface, accessed thru bpy. classes dictionary.

Ken Hughes khughes at pacific.edu
Mon Jul 16 04:52:19 CEST 2007


Revision: 11284
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=11284
Author:   khughes
Date:     2007-07-16 04:52:15 +0200 (Mon, 16 Jul 2007)

Log Message:
-----------
New constants interface, accessed thru bpy.classes dictionary.  Only the Object
class has been converted, mainly to show how to use the new functions.
Documentation on the API can be found here (ignore the link name):

http://wiki.blender.org/index.php/BlenderDev/Bitfields

Modified Paths:
--------------
    branches/pyapi_devel/source/blender/python/api2_2x/Object.c
    branches/pyapi_devel/source/blender/python/api2_2x/Object.h
    branches/pyapi_devel/source/blender/python/api2_2x/bpy.c

Added Paths:
-----------
    branches/pyapi_devel/source/blender/python/api2_2x/Const.c
    branches/pyapi_devel/source/blender/python/api2_2x/Const.h
    branches/pyapi_devel/source/blender/python/api2_2x/bpy_class.c
    branches/pyapi_devel/source/blender/python/api2_2x/bpy_class.h

Added: branches/pyapi_devel/source/blender/python/api2_2x/Const.c
===================================================================
--- branches/pyapi_devel/source/blender/python/api2_2x/Const.c	                        (rev 0)
+++ branches/pyapi_devel/source/blender/python/api2_2x/Const.c	2007-07-16 02:52:15 UTC (rev 11284)
@@ -0,0 +1,596 @@
+/* 
+ * $Id: constant.c,v 1.22 2006/12/26 21:12:34 khughes Exp $
+ *
+ * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version. The Blender
+ * Foundation also sells licenses for use in proprietary software under
+ * the Blender License.  See http://www.blender.org/BL/ for information
+ * about this.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+ * All rights reserved.
+ *
+ * This is a new part of Blender.
+ *
+ * Contributor(s): Ken Hughes
+ *
+ * ***** END GPL/BL DUAL LICENSE BLOCK *****
+*/
+
+#include "Const.h" /*This must come first */
+
+#include "gen_utils.h"
+#include "gen_library.h"
+
+/*
+ * Creates a new constant object
+ */
+
+static BPy_const *new_const( constDefinition *defn, PyTypeObject *type )
+{
+	BPy_const *constant;
+
+	constant = (BPy_const *) PyObject_NEW( BPy_const, type );
+
+	if( constant == NULL )
+		return (BPy_const *)EXPP_ReturnPyObjError(PyExc_MemoryError,
+				"couldn't create Const object");
+
+	constant->defn = defn;
+	return constant;
+}
+
+/*------------------TYPE_OBJECT IMPLEMENTATION--------------------------*/
+static PyObject *ConstCategory_getAttro(BPy_const *self, PyObject *value)
+{
+	char *name = PyString_AS_STRING( value );
+	unsigned int count= self->defn->tot_members;
+	constIdents *ptr;
+
+	if(!strcmp(name, "__members__")) {
+
+		/* return a list containing all the constant names */
+
+		PyObject* list;
+		list = PyList_New( count );
+		if( !list )
+			return EXPP_ReturnPyObjError( PyExc_MemoryError,
+					"unable to create list" );
+		for( ptr= self->defn->members; count; ++ptr )
+			PyList_SET_ITEM( list, --count,
+					PyString_FromString( ptr->repr ) );
+
+#ifdef CONST_BITFIELDS
+		/* For bitfield constant categories, add pseudo-constants for
+		 * for "no bits on" and "all bits on" */
+		if( self->utype == EXPP_CONST_BIT ) {
+			PyObject *extra = PyString_FromString( "NULL" );
+			PyList_Append( list, extra ); Py_DECREF( extra );
+			extra = PyString_FromString( "ALL" );
+			PyList_Append( list, extra ); Py_DECREF( extra );
+		}
+#endif
+		return list;
+	} else {
+#ifdef CONST_BITFIELDS
+	/* for bitfields, check for special pseudo-constants */
+		if( self->utype == EXPP_CONST_BIT ) {
+			/* if "no bits on", return empty bit field */
+			if( !strcmp(name, "NULL") )
+				return PyConst_NewBit( self->defn, (long)0 );
+			/* if "all bits on", return full bit field */
+			else if( !strcmp(name, "ALL") ) {
+				long bits= 0;
+				for( ptr= self->defn->members; count; ++ptr, --count )
+					bits |= ptr->value.b;
+				return PyConst_NewBit( self->defn, bits );
+			}
+		}
+#endif
+
+	/* search for the constant by name, and return a new object */
+
+		for( ptr= self->defn->members; count; ++ptr, --count ) {
+			if( *name == *ptr->repr && !strcmp( name, ptr->repr ) )
+				switch ( self->utype ) {
+				case EXPP_CONST_INT:
+					return PyConst_NewInt( self->defn, ptr->value.i );
+				case EXPP_CONST_FLOAT:
+					return PyConst_NewFloat( self->defn, ptr->value.f );
+
+#ifdef CONST_BITFIELDS
+				case EXPP_CONST_BIT:
+					return PyConst_NewBit( self->defn, ptr->value.b );
+#endif
+				}
+		}
+	}
+
+	/* otherwise pass the argument along */
+	return PyObject_GenericGetAttr( (PyObject *)self, value );
+}
+
+/*------------------------tp_repr------------------------*/
+static PyObject *Const_repr(BPy_const * self)
+{
+	switch( self->utype ) {
+
+		/* for ints and floats, just return the constant category name */
+	case EXPP_CONST_INT:
+	case EXPP_CONST_FLOAT:
+		{
+			unsigned int count= self->defn->tot_members;
+			constIdents *ptr;
+			for( ptr= self->defn->members; count; ++ptr, --count ) {
+				if( self->value.i == ptr->value.i )
+					return PyString_FromFormat( "[Const \"%s\" (%s)]",
+						self->defn->name, ptr->repr );
+			}
+			return EXPP_ReturnPyObjError( PyExc_RuntimeError,
+					"unknown constant value" );
+		}
+
+#ifdef CONST_BITFIELDS
+		/* for bitfields, return a string representing the bit settings */
+	case EXPP_CONST_BIT:
+		{
+			char values[1024], *sptr= values;
+			constIdents *ptr;
+			unsigned int count= self->defn->tot_members;
+			*sptr = 0;
+			for( ptr= self->defn->members; count; ++ptr, --count ) {
+				if( self->value.i & ptr->value.i ) {
+					if( sptr != values )
+						*sptr++ = ',';
+					strcpy( sptr, ptr->repr );
+					sptr += strlen( ptr->repr );
+				}
+			}
+			return PyString_FromFormat( "[Const (%s), \"%s\"]",
+					values, self->defn->name );
+		}
+#endif
+	default:
+		return EXPP_ReturnPyObjError( PyExc_RuntimeError,
+				"unknown constant type" );
+	}
+}
+
+static PyObject *ConstCategory_repr(BPy_const * self)
+{
+	char values[1024], *sptr= values;
+	constIdents *ptr;
+	unsigned int count= self->defn->tot_members;
+	*sptr = 0;
+	switch( self->utype ) {
+		/* all categories just return the category name also */
+	case EXPP_CONST_INT:
+	case EXPP_CONST_FLOAT:
+		for( ptr= self->defn->members; count; ++ptr, --count ) {
+			if( sptr != values )
+				*sptr++ = ',';
+			strcpy( sptr, ptr->repr );
+			sptr += strlen( ptr->repr );
+		}
+		break;
+#ifdef CONST_BITFIELDS
+	case EXPP_CONST_BIT:
+		for( ptr= self->defn->members; count; ++ptr, --count ) {
+			if( self->value.i & ptr->value.i ) {
+				if( sptr != values )
+					*sptr++ = ',';
+				strcpy( sptr, ptr->repr );
+				sptr += strlen( ptr->repr );
+			}
+		}
+		break;
+#endif
+	default:
+		return EXPP_ReturnPyObjError( PyExc_RuntimeError,
+				"unknown constant type" );
+	}
+	return PyString_FromFormat( "[ConstCategory \"%s\" (%s)]",
+			self->defn->name, values );
+}
+
+/*------------------PYNUMBERMETHOD IMPLEMENTATION--------------------------*/
+
+#ifdef CONST_BITFIELDS
+/*
+ * ADD/OR/UNION of bit constants
+ */
+
+static PyObject *Const_union(PyObject * v1, PyObject * v2)
+{
+	BPy_const *c1 = (BPy_const *)v1;
+	BPy_const *c2 = (BPy_const *)v2;
+	BPy_const *c3 = NULL;
+	
+	/* inputs must be bit constants of the same type */
+	if( !BPy_Const_Check(c1) || !BPy_Const_Check(c2) || c1->defn != c2->defn )
+		return EXPP_ReturnPyObjError(PyExc_TypeError,
+			"can only combine two constants of the same type" );
+
+	if( c1->utype != EXPP_CONST_BIT )
+		return EXPP_ReturnPyObjError(PyExc_TypeError,
+			"constants must be bitset type" );
+
+	c3 = new_const( c1->defn, &Const_Type );
+	c3->utype = EXPP_CONST_BIT;
+	c3->value.b = c1->value.b | c2->value.b;
+	return (PyObject *)c3;
+}
+
+/*
+ * MULT/AND/INTERSECTION of bit constants
+ */
+
+static PyObject *Const_intersect(PyObject * v1, PyObject * v2)
+{
+	BPy_const *c1 = (BPy_const *)v1;
+	BPy_const *c2 = (BPy_const *)v2;
+	BPy_const *c3 = NULL;
+	
+	/* inputs must be bit constants of the same type */
+	if( !BPy_Const_Check(c1) || !BPy_Const_Check(c2) || c1->defn != c2->defn )
+		return EXPP_ReturnPyObjError(PyExc_TypeError,
+			"can only add combine constants of the same type" );
+
+	if( c1->utype != EXPP_CONST_BIT )
+		return EXPP_ReturnPyObjError(PyExc_TypeError,
+			"constants must be bitset type" );
+
+	c3 = new_const( c1->defn, &Const_Type );
+	c3->utype = EXPP_CONST_BIT;
+	c3->value.b = c1->value.b & c2->value.b;
+	return (PyObject *)c3;
+}
+
+/*
+ * SUB of bit constants; remove bit(s) specified in v2 from v1
+ */
+
+static PyObject *Const_sub(PyObject * v1, PyObject * v2)
+{
+	BPy_const *c1 = (BPy_const *)v1;
+	BPy_const *c2 = (BPy_const *)v2;
+	BPy_const *c3 = NULL;
+	
+	/* inputs must be bit constants of the same type */
+	if( !BPy_Const_Check(c1) || !BPy_Const_Check(c2) || c1->defn != c2->defn )
+		return EXPP_ReturnPyObjError(PyExc_TypeError,
+			"can only add combine constants of the same type" );
+
+	if( c1->utype != EXPP_CONST_BIT )
+		return EXPP_ReturnPyObjError(PyExc_TypeError,
+			"constants must be bitset type" );
+
+	c3 = new_const( c1->defn, &Const_Type );
+	c3->utype = EXPP_CONST_BIT;
+	c3->value.b = c1->value.b & ~c2->value.b;
+	return (PyObject *)c3;
+}
+
+static PyNumberMethods Const_NumMethods = {
+	(binaryfunc) Const_union,					/* __add__ */
+	(binaryfunc) Const_sub,						/* __sub__ */
+	(binaryfunc) Const_intersect,				/* __mul__ */
+	(binaryfunc) NULL,							/* __div__ */
+	(binaryfunc) NULL,							/* __mod__ */
+	(binaryfunc) NULL,							/* __divmod__ */
+	(ternaryfunc) NULL,							/* __pow__ */
+	(unaryfunc) NULL,							/* __neg__ */
+	(unaryfunc) NULL,							/* __pos__ */
+	(unaryfunc) NULL,							/* __abs__ */
+	(inquiry) NULL,								/* __nonzero__ */
+	(unaryfunc) NULL,							/* __invert__ */
+	(binaryfunc) NULL,							/* __lshift__ */
+	(binaryfunc) NULL,							/* __rshift__ */
+	(binaryfunc) Const_intersect,				/* __and__ */
+	(binaryfunc) NULL,							/* __xor__ */
+	(binaryfunc) Const_union,					/* __or__ */
+	(coercion)  NULL,							/* __coerce__ */
+	(unaryfunc) NULL,							/* __int__ */
+	(unaryfunc) NULL,							/* __long__ */
+	(unaryfunc) NULL,							/* __float__ */
+	(unaryfunc) NULL,							/* __oct__ */
+	(unaryfunc) NULL,							/* __hex__ */
+	
+	/* Added in release 2.0 */
+	(binaryfunc) NULL,							/*__iadd__*/
+	(binaryfunc) NULL,							/*__isub__*/
+	(binaryfunc) NULL,							/*__imul__*/
+	(binaryfunc) NULL,							/*__idiv__*/
+	(binaryfunc) NULL,							/*__imod__*/
+	(ternaryfunc) NULL,							/*__ipow__*/
+	(binaryfunc) NULL,							/*__ilshift__*/
+	(binaryfunc) NULL,							/*__irshift__*/
+	(binaryfunc) NULL,							/*__iand__*/
+	(binaryfunc) NULL,							/*__ixor__*/
+	(binaryfunc) NULL,							/*__ior__*/
+ 
+	/* Added in release 2.2 */
+	/* The following require the Py_TPFLAGS_HAVE_CLASS flag */
+	(binaryfunc) NULL,							/*__floordiv__  __rfloordiv__*/

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list