[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [10975] branches/pyapi_devel/source/ blender/python/api2_2x: wrapped list methods for material list type - count , pop, extend etc

Campbell Barton cbarton at metavr.com
Tue Jun 19 21:43:01 CEST 2007


Revision: 10975
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=10975
Author:   campbellbarton
Date:     2007-06-19 21:43:01 +0200 (Tue, 19 Jun 2007)

Log Message:
-----------
wrapped list methods for material list type - count, pop, extend etc

Modified Paths:
--------------
    branches/pyapi_devel/source/blender/python/api2_2x/gen_utils.c
    branches/pyapi_devel/source/blender/python/api2_2x/gen_utils.h
    branches/pyapi_devel/source/blender/python/api2_2x/material_list.c

Modified: branches/pyapi_devel/source/blender/python/api2_2x/gen_utils.c
===================================================================
--- branches/pyapi_devel/source/blender/python/api2_2x/gen_utils.c	2007-06-19 19:03:12 UTC (rev 10974)
+++ branches/pyapi_devel/source/blender/python/api2_2x/gen_utils.c	2007-06-19 19:43:01 UTC (rev 10975)
@@ -380,231 +380,6 @@
 	if (!G.background) allqueue(event, val);
 }
 
-/************************************************************************/
-/* Scriptlink-related functions, used by scene, object, etc. bpyobjects */
-/************************************************************************/
-PyObject *EXPP_getScriptLinks( ScriptLink * slink, PyObject * args,
-			       int is_scene )
-{
-	PyObject *list = NULL, *tmpstr;
-	char *eventname = NULL;
-	int i, event = 0;
-
-
-	if( !PyArg_ParseTuple( args, "s", &eventname ) )
-		return EXPP_ReturnPyObjError( PyExc_TypeError,
-					      "expected event name (string) as argument" );
-	
-	list = PyList_New( 0 );
-	if( !list )
-		return EXPP_ReturnPyObjError( PyExc_MemoryError,
-					      "couldn't create PyList!" );
-
-	/* actually !scriptlink shouldn't happen ... */
-	if( !slink || !slink->totscript )
-		return list;
-	
-	if( !strcmp( eventname, "FrameChanged" ) )
-		event = SCRIPT_FRAMECHANGED;
-	else if( !strcmp( eventname, "Redraw" ) )
-		event = SCRIPT_REDRAW;
-	else if( !strcmp( eventname, "Render" ) )
-		event = SCRIPT_RENDER;
-	else if( is_scene && !strcmp( eventname, "OnLoad" ) )
-		event = SCRIPT_ONLOAD;
-	else if( is_scene && !strcmp( eventname, "OnSave" ) )
-		event = SCRIPT_ONSAVE;
-	else {
-		Py_DECREF(list);
-		return EXPP_ReturnPyObjError( PyExc_AttributeError,
-					      "invalid event name" );
-	}
-	
-	for( i = 0; i < slink->totscript; i++ ) {
-		if( ( slink->flag[i] == event ) && slink->scripts[i] ) {
-			tmpstr =PyString_FromString( slink->scripts[i]->name + 2 ); 
-			PyList_Append( list, tmpstr );
-			Py_DECREF(tmpstr);
-		}
-	}
-
-	return list;
-}
-
-PyObject *EXPP_clearScriptLinks( ScriptLink * slink, PyObject * args )
-{
-	int i, j, totLinks, deleted = 0;
-	PyObject *seq = NULL;
-	ID **stmp = NULL;
-	short *ftmp = NULL;
-
-	/* check for an optional list of strings */
-	if( !PyArg_ParseTuple( args, "|O", &seq ) )
-		return ( EXPP_ReturnPyObjError
-			 ( PyExc_TypeError,
-			   "expected no arguments or a list of strings" ) );
-
-
-	/* if there was a parameter, handle it */
-	if ( seq != NULL ) {
-		/* check that parameter IS list of strings */
-		if ( !PyList_Check ( seq ) )
-			return ( EXPP_ReturnPyObjError
-				 ( PyExc_TypeError,
-				   "expected a list of strings" ) );
-
-		totLinks = PyList_Size ( seq );
-		for ( i = 0 ; i < totLinks ; ++i ) {
-			if ( !PyString_Check ( PySequence_GetItem( seq, i ) ) )
-				return ( EXPP_ReturnPyObjError
-					 ( PyExc_TypeError,
-					   "expected list to contain strings" ) );
-		}
-
-		/*
-		  parameters OK: now look for each script, and delete
-		  its link as we find it (this handles multiple links)
-		*/
-		for ( i = 0 ; i < totLinks ; ++i )
-		{
-			char *str;
-			str = PyString_AsString ( PySequence_GetItem( seq, i ) );
-			for ( j = 0 ; j < slink->totscript ; ++j ) {
-				if ( slink->scripts[j] && !strcmp( slink->scripts[j]->name+2, str ) )
-					slink->scripts[j] = NULL;
-				if( !slink->scripts[j] )
-					++deleted; 
-			}
-		}
-	}
-	/* if no parameter, then delete all scripts */
-	else {
-		deleted = slink->totscript;
-	}
-
-	/*
-	   if not all scripts deleted, create new lists and copy remaining
-	   links to them
-	*/
-
-	if ( slink->totscript > deleted ) {
-		slink->totscript = slink->totscript - (short)deleted;
-
-		stmp = slink->scripts;
-		slink->scripts =
-			MEM_mallocN( sizeof( ID * ) * ( slink->totscript ),
-				     "bpySlinkL" );
-
-		ftmp = slink->flag;
-		slink->flag =
-			MEM_mallocN( sizeof( short * ) * ( slink->totscript ),
-				     "bpySlinkF" );
-
-		for ( i = 0, j = 0 ; i < slink->totscript ; ++j ) {
-			if ( stmp[j] != NULL ) {
-				memcpy( slink->scripts+i, stmp+j, sizeof( ID * ) );
-				memcpy( slink->flag+i, ftmp+j, sizeof( short ) );
-				++i;
-			}
-		}
-		MEM_freeN( stmp );
-		MEM_freeN( ftmp );
-
-		/*EXPP_allqueue (REDRAWBUTSSCRIPT, 0 );*/
-		slink->actscript = 1;
-	} else {
-
-	/* all scripts deleted, so delete entire list and free memory */
-
-		if( slink->scripts )
-			MEM_freeN( slink->scripts );
-		if( slink->flag )
-			MEM_freeN( slink->flag );
-
-		slink->scripts = NULL;
-		slink->flag = NULL;
-		slink->totscript = slink->actscript = 0;
-	}
-
-	return EXPP_incr_ret( Py_None );
-}
-
-
-PyObject *EXPP_addScriptLink(ScriptLink *slink, PyObject *args, int is_scene)
-{
-	int event = 0, found_txt = 0;
-	void *stmp = NULL, *ftmp = NULL;
-	Text *bltxt = G.main->text.first;
-	char *textname = NULL;
-	char *eventname = NULL;
-
-	/* !scriptlink shouldn't happen ... */
-	if( !slink ) {
-		return EXPP_ReturnPyObjError( PyExc_RuntimeError,
-			"internal error: no scriptlink!" );
-	}
-
-	if( !PyArg_ParseTuple( args, "ss", &textname, &eventname ) )
-		return EXPP_ReturnPyObjError( PyExc_TypeError,
-	    "expected two strings as arguments" );
-
-	while( bltxt ) {
-		if( !strcmp( bltxt->id.name + 2, textname ) ) {
-			found_txt = 1;
-			break;
-		}
-		bltxt = bltxt->id.next;
-	}
-
-	if( !found_txt )
-		return EXPP_ReturnPyObjError( PyExc_AttributeError,
-	    "no such Blender Text" );
-
-	if( !strcmp( eventname, "FrameChanged" ) )
-		event = SCRIPT_FRAMECHANGED;
-	else if( !strcmp( eventname, "Redraw" ) )
-		event = SCRIPT_REDRAW;
-	else if( !strcmp( eventname, "Render" ) )
-		event = SCRIPT_RENDER;
-	else if( is_scene && !strcmp( eventname, "OnLoad" ) )
-		event = SCRIPT_ONLOAD;
-	else if( is_scene && !strcmp( eventname, "OnSave" ) )
-		event = SCRIPT_ONSAVE;
-	else
-		return EXPP_ReturnPyObjError( PyExc_AttributeError,
-			"invalid event name" );
-
-	stmp = slink->scripts;
-	slink->scripts =
-		MEM_mallocN( sizeof( ID * ) * ( slink->totscript + 1 ),
-			     "bpySlinkL" );
-
-	ftmp = slink->flag;
-	slink->flag =
-		MEM_mallocN( sizeof( short * ) * ( slink->totscript + 1 ),
-			     "bpySlinkF" );
-
-	if( slink->totscript ) {
-		memcpy( slink->scripts, stmp,
-			sizeof( ID * ) * ( slink->totscript ) );
-		MEM_freeN( stmp );
-
-		memcpy( slink->flag, ftmp,
-			sizeof( short ) * ( slink->totscript ) );
-		MEM_freeN( ftmp );
-	}
-
-	slink->scripts[slink->totscript] = ( ID * ) bltxt;
-	slink->flag[slink->totscript] = (short)event;
-
-	slink->totscript++;
-
-	if( slink->actscript < 1 )
-		slink->actscript = 1;
-
-	return EXPP_incr_ret (Py_None);		/* normal exit */
-}
-
 /*
  * Utility routines to clamp and store various datatypes.  The object type 
  * is checked and a exception is raised if it's not the correct type.  
@@ -914,3 +689,34 @@
 	Py_DECREF( value ); /* delete original */
 	return ret;
 }
+
+/*
+ * Helper function for subtypes that what the base types methods.
+ * The command below needs to have args modified to have 'self' added at the start
+ * ret = PyObject_Call(PyDict_GetItemString(PyList_Type.tp_dict, "sort"), newargs, keywds);
+ * 
+ * This is not east with the python API so adding a function here,
+ * remember to Py_DECREF the tuple after
+ */
+
+PyObject * EXPP_PyTuple_New_Prepend(PyObject *tuple, PyObject *value)
+{
+	PyObject *item;
+	PyObject *new_tuple;
+	int i;
+	
+	i = PyTuple_Size(tuple);
+	new_tuple = PyTuple_New(i+1);
+	PyTuple_SetItem(new_tuple, 0, value);
+	Py_INCREF(value);
+	while (i) {
+		i--;
+		item = PyTuple_GetItem(tuple, i);
+		PyTuple_SetItem(new_tuple, i+1, item);
+		Py_INCREF(item);
+	}
+	return new_tuple;
+}
+
+
+

Modified: branches/pyapi_devel/source/blender/python/api2_2x/gen_utils.h
===================================================================
--- branches/pyapi_devel/source/blender/python/api2_2x/gen_utils.h	2007-06-19 19:03:12 UTC (rev 10974)
+++ branches/pyapi_devel/source/blender/python/api2_2x/gen_utils.h	2007-06-19 19:43:01 UTC (rev 10975)
@@ -159,17 +159,14 @@
 
 PyObject *EXPP_setterWrapperTuple ( PyObject * self, PyObject * args,
 				setter func);
-
-/* scriplinks-related: */
-PyObject *EXPP_getScriptLinks(ScriptLink *slink, PyObject *args, int is_scene);
-PyObject *EXPP_addScriptLink(ScriptLink *slink, PyObject *args, int is_scene);
-PyObject *EXPP_clearScriptLinks(ScriptLink *slink, PyObject *args);
-
 /* this queues redraws if we're not in background mode: */
 void EXPP_allqueue(unsigned short event, short val);
 
 /* helper to keep dictionaries from causing memory leaks */
 int EXPP_dict_set_item_str( PyObject *dict, char *key, PyObject *value);
 
+/* for adding 'self' at the start, but of course it can be used in any case */
+PyObject *EXPP_PyTuple_New_Prepend( PyObject * tuple, PyObject * value );
+
 #endif				/* EXPP_gen_utils_h */
 

Modified: branches/pyapi_devel/source/blender/python/api2_2x/material_list.c
===================================================================
--- branches/pyapi_devel/source/blender/python/api2_2x/material_list.c	2007-06-19 19:03:12 UTC (rev 10974)
+++ branches/pyapi_devel/source/blender/python/api2_2x/material_list.c	2007-06-19 19:43:01 UTC (rev 10975)
@@ -42,31 +42,52 @@
 #define MTLLS_CMP_BPYMAT_BMAT(bpymat, mat) ((bpymat==Py_None && !mat) || ( BPy_Material_Check(bpymat) && ((BPy_Material *)bpymat)->material == mat))
 #define MTLLS_COMPAT(bpymat) ((bpymat==Py_None) || BPy_Material_Check(bpymat))  
 
-static char MaterialList_Type_Error[] = "MateriaLists only contain None or a Blender Material types";
-
-static void material_data_from_id(ID *id, short *totcol, Material ***mat)
+static int MTLLS_COMPAT_SEQ(PyObject *seq)
 {
-	/* only care about types that can have materials */
-	switch ( GS(id->name) ) {
-	case ID_OB:
-		*totcol =	((Object *)id)->totcol;
-		*mat =		((Object *)id)->mat;
-		return;
-	case ID_ME:
-		*totcol =	((Mesh *)id)->totcol;
-		*mat =		((Mesh *)id)->mat;
-		return;
-	case ID_CU:
-		*totcol =	((Curve *)id)->totcol;
-		*mat =		((Curve *)id)->mat;
-		return;
-	case ID_MB:
-		*totcol =	((MetaBall *)id)->totcol;
-		*mat =		((MetaBall *)id)->mat;
-		return;
+	int i;
+	PyObject *pyob;
+	if (!PySequence_Check(seq))
+		return 0;
+	
+	i=PySequence_Size(seq);
+	while (i) {
+		i--;
+		pyob = PySequence_GetItem(seq, i);
+		if (!MTLLS_COMPAT(pyob)) {
+			Py_DECREF(pyob);
+			return 0;
+		}
+		Py_DECREF(pyob);
 	}
+	return 1;
 }
 
+static char MaterialList_Type_Error[] = "MateriaLists only contain None or a Blender Material types";
+static char MaterialList_Size_Error[] = "MateriaLists have a maximum length of 16";
+
+//static void material_data_from_id(ID *id, short *totcol, Material ***mat)
+//{

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list