[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [24590] trunk/blender/source/blender/ python/generic: python api for ID property access by Joseph Eager, copied from blender 2.4x.

Campbell Barton ideasman42 at gmail.com
Mon Nov 16 19:53:11 CET 2009


Revision: 24590
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=24590
Author:   campbellbarton
Date:     2009-11-16 19:53:11 +0100 (Mon, 16 Nov 2009)

Log Message:
-----------
python api for ID property access by Joseph Eager, copied from blender 2.4x.

Added Paths:
-----------
    trunk/blender/source/blender/python/generic/IDProp.c
    trunk/blender/source/blender/python/generic/IDProp.h

Copied: trunk/blender/source/blender/python/generic/IDProp.c (from rev 24589, branches/blender2.4/source/blender/python/api2_2x/IDProp.c)
===================================================================
--- trunk/blender/source/blender/python/generic/IDProp.c	                        (rev 0)
+++ trunk/blender/source/blender/python/generic/IDProp.c	2009-11-16 18:53:11 UTC (rev 24590)
@@ -0,0 +1,960 @@
+/**
+ * $Id: IDProp.c
+ *
+ * ***** BEGIN GPL 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.
+ *
+ * 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.
+ *
+ * Contributor(s): Joseph Eagar
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+ 
+#include "DNA_ID.h"
+
+#include "BKE_idprop.h"
+
+#include "IDProp.h"
+#include "gen_utils.h"
+
+#include "MEM_guardedalloc.h"
+
+#define BSTR_EQ(a, b)	(*(a) == *(b) && !strcmp(a, b))
+
+/*** Function to wrap ID properties ***/
+PyObject *BPy_Wrap_IDProperty(ID *id, IDProperty *prop, IDProperty *parent);
+
+extern PyTypeObject IDArray_Type;
+extern PyTypeObject IDGroup_Iter_Type;
+
+/*********************** ID Property Main Wrapper Stuff ***************/
+
+PyObject *IDGroup_repr( BPy_IDProperty *self )
+{
+	return PyString_FromString( "(ID Property)" );
+}
+
+extern PyTypeObject IDGroup_Type;
+
+PyObject *BPy_IDGroup_WrapData( ID *id, IDProperty *prop )
+{
+	switch ( prop->type ) {
+		case IDP_STRING:
+			return PyString_FromString( prop->data.pointer );
+		case IDP_INT:
+			return PyInt_FromLong( (long)prop->data.val );
+		case IDP_FLOAT:
+			return PyFloat_FromDouble( (double)(*(float*)(&prop->data.val)) );
+		case IDP_DOUBLE:
+			return PyFloat_FromDouble( (*(double*)(&prop->data.val)) );
+		case IDP_GROUP:
+			/*blegh*/
+			{
+				BPy_IDProperty *group = PyObject_New(BPy_IDProperty, &IDGroup_Type);
+				if (!group)
+					return EXPP_ReturnPyObjError( PyExc_RuntimeError,
+					   "PyObject_New() failed" );
+			
+				group->id = id;
+				group->prop = prop;
+				return (PyObject*) group;
+			}
+		case IDP_ARRAY:
+			{
+				BPy_IDProperty *array = PyObject_New(BPy_IDProperty, &IDArray_Type);
+				if (!array)
+					return EXPP_ReturnPyObjError( PyExc_RuntimeError,
+					   "PyObject_New() failed" );
+					   
+				array->id = id;
+				array->prop = prop;
+				return (PyObject*) array;
+			}
+	}
+	Py_RETURN_NONE;
+}
+
+int BPy_IDGroup_SetData(BPy_IDProperty *self, IDProperty *prop, PyObject *value)
+{
+	switch (prop->type) {
+		case IDP_STRING:
+		{
+			char *st;
+			if (!PyString_Check(value))
+				return EXPP_ReturnIntError(PyExc_TypeError, "expected a string!");
+
+			st = PyString_AsString(value);
+			IDP_ResizeArray(prop, strlen(st)+1);
+			strcpy(prop->data.pointer, st);
+			return 0;
+		}
+
+		case IDP_INT:
+		{
+			int ivalue;
+			if (!PyNumber_Check(value))
+				return EXPP_ReturnIntError(PyExc_TypeError, "expected an int!");
+			value = PyNumber_Int(value);
+			if (!value)
+				return EXPP_ReturnIntError(PyExc_TypeError, "expected an int!");
+			ivalue = (int) PyInt_AsLong(value);
+			prop->data.val = ivalue;
+			Py_XDECREF(value);
+			break;
+		}
+		case IDP_FLOAT:
+		{
+			float fvalue;
+			if (!PyNumber_Check(value))
+				return EXPP_ReturnIntError(PyExc_TypeError, "expected a float!");
+			value = PyNumber_Float(value);
+			if (!value)
+				return EXPP_ReturnIntError(PyExc_TypeError, "expected a float!");
+			fvalue = (float) PyFloat_AsDouble(value);
+			*(float*)&self->prop->data.val = fvalue;
+			Py_XDECREF(value);
+			break;
+		}
+		case IDP_DOUBLE:
+		{
+			double dvalue;
+			if (!PyNumber_Check(value))
+				return EXPP_ReturnIntError(PyExc_TypeError, "expected a float!");
+			value = PyNumber_Float(value);
+			if (!value)
+				return EXPP_ReturnIntError(PyExc_TypeError, "expected a float!");
+			dvalue = (float) PyFloat_AsDouble(value);
+			*(double*)&self->prop->data.val = dvalue;
+			Py_XDECREF(value);
+			break;
+		}
+		default:
+			return EXPP_ReturnIntError(PyExc_AttributeError, "attempt to set read-only attribute!");
+	}
+	return 0;
+}
+
+PyObject *BPy_IDGroup_GetName(BPy_IDProperty *self, void *bleh)
+{
+	return PyString_FromString(self->prop->name);
+}
+
+static int BPy_IDGroup_SetName(BPy_IDProperty *self, PyObject *value, void *bleh)
+{
+	char *st;
+	if (!PyString_Check(value))
+		return EXPP_ReturnIntError(PyExc_TypeError, "expected a string!");
+
+	st = PyString_AsString(value);
+	if (strlen(st) >= MAX_IDPROP_NAME)
+		return EXPP_ReturnIntError(PyExc_TypeError, "string length cannot exceed 31 characters!");
+
+	strcpy(self->prop->name, st);
+	return 0;
+}
+
+static PyObject *BPy_IDGroup_GetType(BPy_IDProperty *self)
+{
+	return PyInt_FromLong((long)self->prop->type);
+}
+
+static PyGetSetDef BPy_IDGroup_getseters[] = {
+	{"name",
+	 (getter)BPy_IDGroup_GetName, (setter)BPy_IDGroup_SetName,
+	 "The name of this Group.",
+	 NULL},
+	 {NULL, NULL, NULL, NULL, NULL}
+};
+	 
+static int BPy_IDGroup_Map_Len(BPy_IDProperty *self)
+{
+	if (self->prop->type != IDP_GROUP)
+		return EXPP_ReturnIntError( PyExc_TypeError,
+			"len() of unsized object");
+			
+	return self->prop->len;
+}
+
+static PyObject *BPy_IDGroup_Map_GetItem(BPy_IDProperty *self, PyObject *item)
+{
+	IDProperty *loop;
+	char *st;
+	
+	if (self->prop->type  != IDP_GROUP)
+		return EXPP_ReturnPyObjError( PyExc_TypeError,
+			"unsubscriptable object");
+			
+	if (!PyString_Check(item)) 
+		return EXPP_ReturnPyObjError( PyExc_TypeError,
+			"only strings are allowed as keys of ID properties");
+	
+	st = PyString_AsString(item);
+	for (loop=self->prop->data.group.first; loop; loop=loop->next) {
+		if (BSTR_EQ(loop->name, st)) return BPy_IDGroup_WrapData(self->id, loop);
+	}
+	return EXPP_ReturnPyObjError( PyExc_KeyError,
+		"key not in subgroup dict");
+}
+
+/*returns NULL on success, error string on failure*/
+static char *BPy_IDProperty_Map_ValidateAndCreate(char *name, IDProperty *group, PyObject *ob)
+{
+	IDProperty *prop = NULL;
+	IDPropertyTemplate val = {0};
+	
+	if (PyFloat_Check(ob)) {
+		val.d = PyFloat_AsDouble(ob);
+		prop = IDP_New(IDP_DOUBLE, val, name);
+	} else if (PyInt_Check(ob)) {
+		val.i = (int) PyInt_AsLong(ob);
+		prop = IDP_New(IDP_INT, val, name);
+	} else if (PyString_Check(ob)) {
+		val.str = PyString_AsString(ob);
+		prop = IDP_New(IDP_STRING, val, name);
+	} else if (PySequence_Check(ob)) {
+		PyObject *item;
+		int i;
+		
+		/*validate sequence and derive type.
+		we assume IDP_INT unless we hit a float
+		number; then we assume it's */
+		val.array.type = IDP_INT;
+		val.array.len = PySequence_Length(ob);
+		for (i=0; i<val.array.len; i++) {
+			item = PySequence_GetItem(ob, i);
+			if (PyFloat_Check(item)) val.array.type = IDP_DOUBLE;
+			else if (!PyInt_Check(item)) return "only floats and ints are allowed in ID property arrays";
+			Py_XDECREF(item);
+		}
+		
+		prop = IDP_New(IDP_ARRAY, val, name);
+		for (i=0; i<val.array.len; i++) {
+			item = PySequence_GetItem(ob, i);
+			if (val.array.type == IDP_INT) {
+				item = PyNumber_Int(item);
+				((int*)prop->data.pointer)[i] = (int)PyInt_AsLong(item);
+			} else {
+				item = PyNumber_Float(item);
+				((double*)prop->data.pointer)[i] = (float)PyFloat_AsDouble(item);
+			}
+			Py_XDECREF(item);
+		}
+	} else if (PyMapping_Check(ob)) {
+		PyObject *keys, *vals, *key, *pval;
+		int i, len;
+		/*yay! we get into recursive stuff now!*/
+		keys = PyMapping_Keys(ob);
+		vals = PyMapping_Values(ob);
+		
+		/*we allocate the group first; if we hit any invalid data,
+		  we can delete it easily enough.*/
+		prop = IDP_New(IDP_GROUP, val, name);
+		len = PyMapping_Length(ob);
+		for (i=0; i<len; i++) {
+			key = PySequence_GetItem(keys, i);
+			pval = PySequence_GetItem(vals, i);
+			if (!PyString_Check(key)) {
+				IDP_FreeProperty(prop);
+				MEM_freeN(prop);
+				Py_XDECREF(keys);
+				Py_XDECREF(vals);
+				Py_XDECREF(key);
+				Py_XDECREF(pval);
+				return "invalid element in subgroup dict template!";
+			}
+			if (BPy_IDProperty_Map_ValidateAndCreate(PyString_AsString(key), prop, pval)) {
+				IDP_FreeProperty(prop);
+				MEM_freeN(prop);
+				Py_XDECREF(keys);
+				Py_XDECREF(vals);
+				Py_XDECREF(key);
+				Py_XDECREF(pval);
+				return "invalid element in subgroup dict template!";
+			}
+			Py_XDECREF(key);
+			Py_XDECREF(pval);
+		}
+		Py_XDECREF(keys);
+		Py_XDECREF(vals);
+	} else return "invalid property value";
+	
+	IDP_ReplaceInGroup(group, prop);
+	return NULL;
+}
+
+static int BPy_IDGroup_Map_SetItem(BPy_IDProperty *self, PyObject *key, PyObject *val)
+{
+	char *err;
+	
+	if (self->prop->type  != IDP_GROUP)
+		return EXPP_ReturnIntError( PyExc_TypeError,
+			"unsubscriptable object");
+			
+	if (!PyString_Check(key))
+		return EXPP_ReturnIntError( PyExc_TypeError,
+		   "only strings are allowed as subgroup keys" );
+
+	if (val == NULL) {
+		IDProperty *pkey = IDP_GetPropertyFromGroup(self->prop, PyString_AsString(key));
+		if (pkey) {
+			IDP_RemFromGroup(self->prop, pkey);
+			IDP_FreeProperty(pkey);
+			MEM_freeN(pkey);
+			return 0;
+		} else return EXPP_ReturnIntError( PyExc_RuntimeError, "property not found in group" );
+	}
+	
+	err = BPy_IDProperty_Map_ValidateAndCreate(PyString_AsString(key), self->prop, val);
+	if (err) return EXPP_ReturnIntError( PyExc_RuntimeError, err );
+	
+	return 0;
+}
+
+static PyObject *BPy_IDGroup_SpawnIterator(BPy_IDProperty *self)
+{
+	BPy_IDGroup_Iter *iter = PyObject_New(BPy_IDGroup_Iter, &IDGroup_Iter_Type);
+	
+	if (!iter)
+		return EXPP_ReturnPyObjError( PyExc_RuntimeError,
+		   "PyObject_New() failed" );
+	iter->group = self;
+	iter->mode = IDPROP_ITER_KEYS;
+	iter->cur = self->prop->data.group.first;
+	Py_XINCREF(iter);
+	return (PyObject*) iter;
+}
+
+static PyObject *BPy_IDGroup_MapDataToPy(IDProperty *prop)
+{
+	switch (prop->type) {
+		case IDP_STRING:
+			return PyString_FromString(prop->data.pointer);
+			break;
+		case IDP_FLOAT:
+			return PyFloat_FromDouble(*((float*)&prop->data.val));
+			break;

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list