From fccceaa87fb1e883c5d29113dc8b7d33786c8c1e Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Tue, 8 Dec 2009 09:40:30 +0000 Subject: - pyrna support for (value in array), currently only 1 dimensional arrays. - use python malloc's in bpy_array.c - automatically blending bone locations is disabled if the target bone has locked location - neck had incorrect roll --- source/blender/python/intern/bpy_array.c | 97 ++++++++++++++++++++++++++++++-- source/blender/python/intern/bpy_rna.c | 35 +++++++----- source/blender/python/intern/bpy_rna.h | 1 + 3 files changed, 115 insertions(+), 18 deletions(-) (limited to 'source') diff --git a/source/blender/python/intern/bpy_array.c b/source/blender/python/intern/bpy_array.c index f11c95e7ed5..5f228836b42 100644 --- a/source/blender/python/intern/bpy_array.c +++ b/source/blender/python/intern/bpy_array.c @@ -32,8 +32,6 @@ #include "BKE_global.h" -#include "MEM_guardedalloc.h" - #define MAX_ARRAY_DIMENSION 10 typedef void (*ItemConvertFunc)(PyObject *, char *); @@ -258,7 +256,7 @@ static int py_to_array(PyObject *py, PointerRNA *ptr, PropertyRNA *prop, char *p if (totitem) { if (!param_data || RNA_property_flag(prop) & PROP_DYNAMIC) - data= MEM_callocN(item_size * totitem, "pyrna primitive type array"); + data= PyMem_MALLOC(item_size * totitem); else data= param_data; @@ -273,7 +271,7 @@ static int py_to_array(PyObject *py, PointerRNA *ptr, PropertyRNA *prop, char *p else { /* NULL can only pass through in case RNA property arraylength is 0 (impossible?) */ rna_set_array(ptr, prop, data); - MEM_freeN(data); + PyMem_FREE(data); } } @@ -513,3 +511,94 @@ PyObject *pyrna_py_from_array(PointerRNA *ptr, PropertyRNA *prop) return pyrna_prop_CreatePyObject(ptr, prop); } + +/* TODO, multi-dimensional arrays */ +int pyrna_array_contains_py(PointerRNA *ptr, PropertyRNA *prop, PyObject *value) +{ + int len= RNA_property_array_length(ptr, prop); + int type; + int i; + + if(len==0) /* possible with dynamic arrays */ + return 0; + + if (RNA_property_array_dimension(ptr, prop, NULL) > 1) { + PyErr_SetString(PyExc_TypeError, "PropertyRNA - multi dimensional arrays not supported yet"); + return -1; + } + + type= RNA_property_type(prop); + + switch (type) { + case PROP_FLOAT: + { + float value_f= PyFloat_AsDouble(value); + if(value_f==-1 && PyErr_Occurred()) { + PyErr_Clear(); + return 0; + } + else { + float tmp[32]; + float *tmp_arr; + + if(len * sizeof(float) > sizeof(tmp)) { + tmp_arr= PyMem_MALLOC(len * sizeof(float)); + } + else { + tmp_arr= tmp; + } + + RNA_property_float_get_array(ptr, prop, tmp_arr); + + for(i=0; i sizeof(tmp)) { + tmp_arr= PyMem_MALLOC(len * sizeof(int)); + } + else { + tmp_arr= tmp; + } + + if(type==PROP_BOOLEAN) + RNA_property_boolean_get_array(ptr, prop, tmp_arr); + else + RNA_property_int_get_array(ptr, prop, tmp_arr); + + for(i=0; iprop) == PROP_COLLECTION) { + /* key in dict style check */ + char *keyname = _PyUnicode_AsString(value); - if (RNA_property_type(self->prop) != PROP_COLLECTION) { - PyErr_SetString(PyExc_TypeError, "PropertyRNA - key in prop, is only valid for collection types"); - return -1; - } + if(keyname==NULL) { + PyErr_SetString(PyExc_TypeError, "PropertyRNA - key in prop, key must be a string type"); + return -1; + } - if (RNA_property_collection_lookup_string(&self->ptr, self->prop, keyname, &newptr)) - return 1; + if (RNA_property_collection_lookup_string(&self->ptr, self->prop, keyname, &newptr)) + return 1; + } + else if (RNA_property_array_check(&self->ptr, self->prop)) { + /* value in list style check */ + return pyrna_array_contains_py(&self->ptr, self->prop, value); + } + else { + PyErr_SetString(PyExc_TypeError, "PropertyRNA - type is not an array or a collection"); + return -1; + } return 0; } @@ -2264,6 +2271,9 @@ static PyObject *foreach_getset(BPy_PropertyRNA *self, PyObject *args, int set) } } + if(array) + PyMem_Free(array); + if(PyErr_Occurred()) { /* Maybe we could make our own error */ PyErr_Print(); @@ -2275,9 +2285,6 @@ static PyObject *foreach_getset(BPy_PropertyRNA *self, PyObject *args, int set) return NULL; } - if(array) - PyMem_Free(array); - Py_RETURN_NONE; } diff --git a/source/blender/python/intern/bpy_rna.h b/source/blender/python/intern/bpy_rna.h index 0e40bf7258c..37f6af36726 100644 --- a/source/blender/python/intern/bpy_rna.h +++ b/source/blender/python/intern/bpy_rna.h @@ -103,5 +103,6 @@ int pyrna_py_to_array_index(PointerRNA *ptr, PropertyRNA *prop, int arraydim, in PyObject *pyrna_py_from_array(PointerRNA *ptr, PropertyRNA *prop); PyObject *pyrna_py_from_array_index(BPy_PropertyRNA *self, int index); PyObject *pyrna_math_object_from_array(PointerRNA *ptr, PropertyRNA *prop); +int pyrna_array_contains_py(PointerRNA *ptr, PropertyRNA *prop, PyObject *value); #endif -- cgit v1.2.3