[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [10989] branches/pyapi_devel/source/ blender/python/api2_2x: adding layer sets, a subtype of a python set() that keeps in sync with blender - like materialList.

Campbell Barton cbarton at metavr.com
Thu Jun 21 02:21:57 CEST 2007


Revision: 10989
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=10989
Author:   campbellbarton
Date:     2007-06-21 02:21:56 +0200 (Thu, 21 Jun 2007)

Log Message:
-----------
adding layer sets, a subtype of a python set() that keeps in sync with blender - like materialList.
these are used for groups, scenes and objects so far.
example usage is...
scene.layers.add(20)
ob1.layers = ob2.layers - ob3.layers
print len(group.layers)

These are slower to use then layerMask but more pythonic then bitmasks.

This is work in progress since methods dont yet sync.

Modified Paths:
--------------
    branches/pyapi_devel/source/blender/python/api2_2x/Blender.c
    branches/pyapi_devel/source/blender/python/api2_2x/Group.c
    branches/pyapi_devel/source/blender/python/api2_2x/Object.c
    branches/pyapi_devel/source/blender/python/api2_2x/Scene.c
    branches/pyapi_devel/source/blender/python/api2_2x/World.c
    branches/pyapi_devel/source/blender/python/api2_2x/gen_library.c
    branches/pyapi_devel/source/blender/python/api2_2x/gen_library.h
    branches/pyapi_devel/source/blender/python/api2_2x/material_list.c

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

Modified: branches/pyapi_devel/source/blender/python/api2_2x/Blender.c
===================================================================
--- branches/pyapi_devel/source/blender/python/api2_2x/Blender.c	2007-06-20 16:22:30 UTC (rev 10988)
+++ branches/pyapi_devel/source/blender/python/api2_2x/Blender.c	2007-06-21 00:21:56 UTC (rev 10989)
@@ -96,6 +96,7 @@
 #include "Types.h"
 #include "object_scriptlink.h"
 #include "material_list.h"
+#include "layer_set.h"
 
 /**********************************************************/
 /* Python API function prototypes for the Blender module.	*/
@@ -983,4 +984,5 @@
 	PyDict_SetItemString(dict, "World", World_Init());
 	
 	MaterialList_Init();
+	LayerSet_Init();
 }

Modified: branches/pyapi_devel/source/blender/python/api2_2x/Group.c
===================================================================
--- branches/pyapi_devel/source/blender/python/api2_2x/Group.c	2007-06-20 16:22:30 UTC (rev 10988)
+++ branches/pyapi_devel/source/blender/python/api2_2x/Group.c	2007-06-21 00:21:56 UTC (rev 10989)
@@ -235,6 +235,7 @@
 /*****************************************************************************/
 static PyGetSetDef BPy_Group_getseters[] = {
 	GENERIC_LIB_GETSETATTR,
+	GENERIC_LIB_GETSETATTR_LAYER,
 	{"layerMask",
 	 (getter)Group_getLayerMask, (setter)Group_setLayerMask,
 	 "layer mask for this group",

Modified: branches/pyapi_devel/source/blender/python/api2_2x/Object.c
===================================================================
--- branches/pyapi_devel/source/blender/python/api2_2x/Object.c	2007-06-20 16:22:30 UTC (rev 10988)
+++ branches/pyapi_devel/source/blender/python/api2_2x/Object.c	2007-06-21 00:21:56 UTC (rev 10989)
@@ -3482,85 +3482,6 @@
 			(int)type, 'h' );
 }
 
-static PyObject *Object_getLayers( BPy_Object * self )
-{
-	int layers, bit;
-	PyObject *laylist = PyList_New( 0 );
-
-	if( !laylist )
-		return EXPP_ReturnPyObjError( PyExc_MemoryError,
-				"PyList_New() failed" );
-
-	layers = self->object->lay & 0xfffff;		/* get layer bitmask */
-
-	/*
-	 * starting with the first layer, and until there are no more layers,
-	 * find which layers are visible
-	 */
-
-	for( bit = 1; layers; ++bit ) {
-		if( layers & 1 ) {	/* if layer is visible, add to list */
-			PyObject *item = PyInt_FromLong( bit );
-			PyList_Append( laylist, item );
-			Py_DECREF( item );
-		}
-		layers >>= 1;		/* go to the next layer */
-	}
-	return laylist;
-}
-
-/*
- * usage note: caller of this func needs to do a Blender.Redraw(-1)
- * to update and redraw the interface
- */
-
-static int Object_setLayers( BPy_Object * self, PyObject *value )
-{
-	int layers = 0, val, i, len_list, local;
-	Base *base;
-
-	if( !PyList_Check( value ) )
-		return EXPP_ReturnIntError( PyExc_TypeError,
-			"expected a list of integers in the range [1, 20]" );
-
-	len_list = PyList_Size( value );
-
-	/* build a bitmask, check for values outside of range */
-
-	for( i = 0; i < len_list; i++ ) {
-		PyObject* integer = PyNumber_Int( PyList_GetItem( value, i ) );
-		val = PyInt_AsLong( integer );
-		Py_XDECREF( integer );
-		if( !integer )
-			return EXPP_ReturnIntError( PyExc_TypeError,
-				  "list must contain only integer numbers" );
-		if( val < 1 || val > 20 )
-			return EXPP_ReturnIntError ( PyExc_ValueError,
-				  "layer values must be in the range [1, 20]" );
-		layers |= 1 << ( val - 1 );
-	}
-
-	/* do this, to ensure layers are set for objects not in current scene */
-	self->object->lay= layers;
-	
-	/* update any bases pointing to our object */
-	base = FIRSTBASE;  /* first base in current scene */
-	while( base ) {
-		if( base->object == self->object ) {
-			base->lay &= 0xFFF00000;
-			local = base->lay;
-			base->lay = local | layers;
-			self->object->lay = base->lay;
-		}
-		base = base->next;
-	}
-	
-	/* these to calls here are overkill! (ton) */
-	countall();
-	DAG_scene_sort( G.scene );
-	return 0;
-}
-
 static int Object_setLayersMask( BPy_Object *self, PyObject *value )
 {
 	int layers = 0, local;
@@ -3816,6 +3737,7 @@
 	GENERIC_LIB_GETSETATTR,
 	GENERIC_LIB_GETSETATTR_SCRIPTLINK,
 	GENERIC_LIB_GETSETATTR_MATERIAL,
+	GENERIC_LIB_GETSETATTR_LAYER,
 	{"LocX",
 	 (getter)getFloatAttr, (setter)setFloatAttr,
 	 "The X location coordinate of the object",
@@ -3917,10 +3839,6 @@
 	 (getter)getIntAttr, (setter)Object_setLayersMask,
 	 "The object layers (bitfield)",
 	 (void *)EXPP_OBJ_ATTR_LAYERMASK},
-	{"layers",
-	 (getter)Object_getLayers, (setter)Object_setLayers,
-	 "The object layers (list of ints)",
-	 NULL},
 	{"ipo",
 	 (getter)Object_getIpo, (setter)Object_setIpo,
 	 "Object's Ipo data",

Modified: branches/pyapi_devel/source/blender/python/api2_2x/Scene.c
===================================================================
--- branches/pyapi_devel/source/blender/python/api2_2x/Scene.c	2007-06-20 16:22:30 UTC (rev 10988)
+++ branches/pyapi_devel/source/blender/python/api2_2x/Scene.c	2007-06-21 00:21:56 UTC (rev 10989)
@@ -245,6 +245,7 @@
 static PyGetSetDef BPy_Scene_getseters[] = {
 	GENERIC_LIB_GETSETATTR,
 	GENERIC_LIB_GETSETATTR_SCRIPTLINK,
+	GENERIC_LIB_GETSETATTR_LAYER,
 	{"layerMask",
 	 (getter)Scene_getLayerMask, (setter)Scene_setLayerMask,
 	 "Scene layer bitmask",

Modified: branches/pyapi_devel/source/blender/python/api2_2x/World.c
===================================================================
--- branches/pyapi_devel/source/blender/python/api2_2x/World.c	2007-06-20 16:22:30 UTC (rev 10988)
+++ branches/pyapi_devel/source/blender/python/api2_2x/World.c	2007-06-21 00:21:56 UTC (rev 10989)
@@ -305,18 +305,18 @@
  * \return int : The World Data skytype.
  */
 
-static PyObject *World_getSkytype( BPy_World * self )
-{
-	PyObject *attr = PyInt_FromLong( ( long ) self->world->skytype );
+//static PyObject *World_getSkytype( BPy_World * self )
+//{
+//	PyObject *attr = PyInt_FromLong( ( long ) self->world->skytype );
+//
+//	if( attr )
+//		return attr;
+//
+//	return ( EXPP_ReturnPyObjError( PyExc_RuntimeError,
+//					"couldn't get World.skytype attribute" ) );
+//}
 
-	if( attr )
-		return attr;
 
-	return ( EXPP_ReturnPyObjError( PyExc_RuntimeError,
-					"couldn't get World.skytype attribute" ) );
-}
-
-
 /**
  * \brief World PyMethod setSkytype
  *

Modified: branches/pyapi_devel/source/blender/python/api2_2x/gen_library.c
===================================================================
--- branches/pyapi_devel/source/blender/python/api2_2x/gen_library.c	2007-06-20 16:22:30 UTC (rev 10988)
+++ branches/pyapi_devel/source/blender/python/api2_2x/gen_library.c	2007-06-21 00:21:56 UTC (rev 10989)
@@ -33,6 +33,7 @@
 #include "DNA_ipo_types.h"
 #include "object_scriptlink.h"
 #include "material_list.h"
+#include "layer_set.h"
 
 /* Generic get/set attrs */
 PyObject *GenericLib_getName( void *self )
@@ -207,6 +208,16 @@
 	return MaterialList_AssignPyObject(self, value);
 }
 
+PyObject *GenericLib_getLayers( void *self )
+{
+	return LayerSet_CreatePyObject((BPy_GenericLib *)self);
+}
+
+int GenericLib_setLayers( void *self, PyObject *value )
+{
+	return LayerSet_AssignPyObject(self, value);
+}
+
 /*
  * This function is used to assign a PyObject representing
  * blender libdata to a pointer.

Modified: branches/pyapi_devel/source/blender/python/api2_2x/gen_library.h
===================================================================
--- branches/pyapi_devel/source/blender/python/api2_2x/gen_library.h	2007-06-20 16:22:30 UTC (rev 10988)
+++ branches/pyapi_devel/source/blender/python/api2_2x/gen_library.h	2007-06-21 00:21:56 UTC (rev 10989)
@@ -79,6 +79,13 @@
 	 "material list",\
 	 NULL}
 
+/* for types that have layers */
+#define GENERIC_LIB_GETSETATTR_LAYER \
+	{"layers",\
+	 (getter)GenericLib_getLayers, (setter)GenericLib_setLayers,\
+	 "layer set",\
+	 NULL}
+
 /* Dummy struct for getting the ID from a libdata BPyObject */
 typedef struct {
 	PyObject_HEAD		/* required python macro */
@@ -100,6 +107,9 @@
 PyObject *GenericLib_getMaterials( void *self );
 int GenericLib_setMaterials( void *self, PyObject *value );
 
+PyObject *GenericLib_getLayers( void *self );
+int GenericLib_setLayers( void *self, PyObject *value );
+
 int GenericLib_assignData(PyObject *value, void **data, void **ndata, short refcount, short type, short subtype);
 short GenericLib_getType(PyObject * pydata);
 

Added: branches/pyapi_devel/source/blender/python/api2_2x/layer_set.c
===================================================================
--- branches/pyapi_devel/source/blender/python/api2_2x/layer_set.c	                        (rev 0)
+++ branches/pyapi_devel/source/blender/python/api2_2x/layer_set.c	2007-06-21 00:21:56 UTC (rev 10989)
@@ -0,0 +1,568 @@
+/*
+ * ***** 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): Campbell Barton
+ *
+ * ***** END GPL/BL DUAL LICENSE BLOCK *****
+ */
+
+#include "layer_set.h" /* This must come first */
+#include "MEM_guardedalloc.h"
+#include "blendef.h"
+#include "gen_utils.h"
+#include "DNA_object_types.h"
+#include "DNA_group_types.h"
+#include "DNA_scene_types.h"
+#include "Material.h"
+#include "BKE_material.h"
+#include "BKE_utildefines.h"
+#include "BKE_global.h"
+#include "BKE_depsgraph.h"
+#include "BSE_edit.h"
+
+#define LAYSEQ_COMPAT_INT(i) (i>=1 && i<=20)
+#define LAYSEQ_COMPAT_INT_BLEN(i) (i>=0 && i<=19)
+
+static char LayerSet_Type_Error[] = "MateriaLists only contain None or a Blender Material types";
+
+/* store these methods so we dont have to lookup the list methods each time */
+static const PyObject * CONST_PySet_add;

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list