[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [24592] trunk/blender/source/blender: ID property access from python for pose channels, bones and any ID objects .
Campbell Barton
ideasman42 at gmail.com
Mon Nov 16 20:03:40 CET 2009
Revision: 24592
http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=24592
Author: campbellbarton
Date: 2009-11-16 20:03:40 +0100 (Mon, 16 Nov 2009)
Log Message:
-----------
ID property access from python for pose channels, bones and any ID objects.
The advantage with this is that global property definitions are not needed to add a property to an object.
to avoid confusion these are accessed like a dictionary (closely matching how the BGE accesses properties)
ob["mySetting"] = 1.0
bone["foo"] = {"one":1, "two":2.1, "three":"Three"}
if "foo" in bone: print("prop found...")
At the moment these can also be accessed as attributes, will be changed shortly. eg.
bone.foo == bone["foo"]
Modified Paths:
--------------
trunk/blender/source/blender/makesrna/RNA_access.h
trunk/blender/source/blender/makesrna/intern/rna_access.c
trunk/blender/source/blender/python/intern/bpy_interface.c
trunk/blender/source/blender/python/intern/bpy_rna.c
Modified: trunk/blender/source/blender/makesrna/RNA_access.h
===================================================================
--- trunk/blender/source/blender/makesrna/RNA_access.h 2009-11-16 18:56:58 UTC (rev 24591)
+++ trunk/blender/source/blender/makesrna/RNA_access.h 2009-11-16 19:03:40 UTC (rev 24592)
@@ -583,7 +583,9 @@
void RNA_struct_blender_type_set(StructRNA *srna, void *blender_type);
struct IDProperty *RNA_struct_idproperties(PointerRNA *ptr, int create);
+int RNA_struct_idproperties_check(PointerRNA *ptr);
+
PropertyRNA *RNA_struct_find_property(PointerRNA *ptr, const char *identifier);
const struct ListBase *RNA_struct_defined_properties(StructRNA *srna);
Modified: trunk/blender/source/blender/makesrna/intern/rna_access.c
===================================================================
--- trunk/blender/source/blender/makesrna/intern/rna_access.c 2009-11-16 18:56:58 UTC (rev 24591)
+++ trunk/blender/source/blender/makesrna/intern/rna_access.c 2009-11-16 19:03:40 UTC (rev 24592)
@@ -195,6 +195,12 @@
return NULL;
}
+int RNA_struct_idproperties_check(PointerRNA *ptr)
+{
+ StructRNA *type= ptr->type;
+ return (type && type->idproperties) ? 1 : 0;
+}
+
static IDProperty *rna_idproperty_find(PointerRNA *ptr, const char *name)
{
IDProperty *group= RNA_struct_idproperties(ptr, 0);
Modified: trunk/blender/source/blender/python/intern/bpy_interface.c
===================================================================
--- trunk/blender/source/blender/python/intern/bpy_interface.c 2009-11-16 18:56:58 UTC (rev 24591)
+++ trunk/blender/source/blender/python/intern/bpy_interface.c 2009-11-16 19:03:40 UTC (rev 24592)
@@ -177,6 +177,12 @@
{
PyObject *mod;
+ /* stand alone utility modules not related to blender directly */
+ Geometry_Init();
+ Mathutils_Init();
+ BGL_Init();
+ IDProp_Init_Types();
+
/* Needs to be first since this dir is needed for future modules */
char *modpath= BLI_gethome_folder("scripts/modules", BLI_GETHOME_ALL);
if(modpath) {
@@ -214,11 +220,6 @@
PyModule_AddObject(mod, "context", (PyObject *)bpy_context_module);
}
- /* stand alone utility modules not related to blender directly */
- Geometry_Init();
- Mathutils_Init();
- BGL_Init();
-
/* add our own modules dir, this is a python package */
bpy_import_test("bpy");
}
Modified: trunk/blender/source/blender/python/intern/bpy_rna.c
===================================================================
--- trunk/blender/source/blender/python/intern/bpy_rna.c 2009-11-16 18:56:58 UTC (rev 24591)
+++ trunk/blender/source/blender/python/intern/bpy_rna.c 2009-11-16 19:03:40 UTC (rev 24592)
@@ -48,6 +48,7 @@
#ifdef USE_MATHUTILS
#include "../generic/Mathutils.h" /* so we can have mathutils callbacks */
+#include "../generic/IDProp.h" /* for IDprop lookups */
/* bpyrna vector/euler/quat callbacks */
static int mathutils_rna_array_cb_index= -1; /* index for our callbacks */
@@ -273,7 +274,7 @@
}
/*----------------------repr--------------------------------------------*/
-static PyObject *pyrna_struct_repr( BPy_StructRNA * self )
+static PyObject *pyrna_struct_repr( BPy_StructRNA *self )
{
PyObject *pyob;
char *name;
@@ -289,7 +290,7 @@
return PyUnicode_FromFormat( "[BPy_StructRNA \"%.200s\"]", RNA_struct_identifier(self->ptr.type));
}
-static PyObject *pyrna_prop_repr( BPy_PropertyRNA * self )
+static PyObject *pyrna_prop_repr( BPy_PropertyRNA *self )
{
PyObject *pyob;
PointerRNA ptr;
@@ -310,13 +311,13 @@
return PyUnicode_FromFormat( "[BPy_PropertyRNA \"%.200s\" -> \"%.200s\"]", RNA_struct_identifier(self->ptr.type), RNA_property_identifier(self->prop));
}
-static long pyrna_struct_hash( BPy_StructRNA * self )
+static long pyrna_struct_hash( BPy_StructRNA *self )
{
return (long)self->ptr.data;
}
/* use our own dealloc so we can free a property if we use one */
-static void pyrna_struct_dealloc( BPy_StructRNA * self )
+static void pyrna_struct_dealloc( BPy_StructRNA *self )
{
if (self->freeptr && self->ptr.data) {
IDP_FreeProperty(self->ptr.data);
@@ -526,7 +527,7 @@
return error_val;
}
-static PyObject * pyrna_func_call(PyObject * self, PyObject *args, PyObject *kw);
+static PyObject * pyrna_func_call(PyObject *self, PyObject *args, PyObject *kw);
PyObject *pyrna_func_to_py(BPy_DummyPointerRNA *pyrna, FunctionRNA *func)
{
@@ -863,7 +864,7 @@
return RNA_property_array_length(&self->ptr, self->prop);
}
-static Py_ssize_t pyrna_prop_len( BPy_PropertyRNA * self )
+static Py_ssize_t pyrna_prop_len( BPy_PropertyRNA *self )
{
Py_ssize_t len;
@@ -880,7 +881,7 @@
}
/* internal use only */
-static PyObject *prop_subscript_collection_int(BPy_PropertyRNA * self, int keynum)
+static PyObject *prop_subscript_collection_int(BPy_PropertyRNA *self, int keynum)
{
PointerRNA newptr;
@@ -893,7 +894,7 @@
return NULL;
}
-static PyObject *prop_subscript_array_int(BPy_PropertyRNA * self, int keynum)
+static PyObject *prop_subscript_array_int(BPy_PropertyRNA *self, int keynum)
{
int len= pyrna_prop_array_length(self);
@@ -906,7 +907,7 @@
return NULL;
}
-static PyObject *prop_subscript_collection_str(BPy_PropertyRNA * self, char *keyname)
+static PyObject *prop_subscript_collection_str(BPy_PropertyRNA *self, char *keyname)
{
PointerRNA newptr;
if(RNA_property_collection_lookup_string(&self->ptr, self->prop, keyname, &newptr))
@@ -915,9 +916,9 @@
PyErr_Format(PyExc_KeyError, "key \"%.200s\" not found", keyname);
return NULL;
}
-/* static PyObject *prop_subscript_array_str(BPy_PropertyRNA * self, char *keyname) */
+/* static PyObject *prop_subscript_array_str(BPy_PropertyRNA *self, char *keyname) */
-static PyObject *prop_subscript_collection_slice(BPy_PropertyRNA * self, int start, int stop)
+static PyObject *prop_subscript_collection_slice(BPy_PropertyRNA *self, int start, int stop)
{
PointerRNA newptr;
PyObject *list = PyList_New(stop - start);
@@ -939,7 +940,7 @@
return list;
}
-static PyObject *prop_subscript_array_slice(BPy_PropertyRNA * self, int start, int stop)
+static PyObject *prop_subscript_array_slice(BPy_PropertyRNA *self, int start, int stop)
{
PyObject *list = PyList_New(stop - start);
int count;
@@ -952,7 +953,7 @@
return list;
}
-static PyObject *prop_subscript_collection(BPy_PropertyRNA * self, PyObject *key)
+static PyObject *prop_subscript_collection(BPy_PropertyRNA *self, PyObject *key)
{
if (PyUnicode_Check(key)) {
return prop_subscript_collection_str(self, _PyUnicode_AsString(key));
@@ -988,7 +989,7 @@
}
}
-static PyObject *prop_subscript_array(BPy_PropertyRNA * self, PyObject *key)
+static PyObject *prop_subscript_array(BPy_PropertyRNA *self, PyObject *key)
{
/*if (PyUnicode_Check(key)) {
return prop_subscript_array_str(self, _PyUnicode_AsString(key));
@@ -1023,7 +1024,7 @@
}
}
-static PyObject *pyrna_prop_subscript( BPy_PropertyRNA * self, PyObject *key )
+static PyObject *pyrna_prop_subscript( BPy_PropertyRNA *self, PyObject *key )
{
if (RNA_property_type(self->prop) == PROP_COLLECTION) {
return prop_subscript_collection(self, key);
@@ -1035,7 +1036,7 @@
return NULL;
}
-static int prop_subscript_ass_array_slice(BPy_PropertyRNA * self, int begin, int end, PyObject *value)
+static int prop_subscript_ass_array_slice(BPy_PropertyRNA *self, int begin, int end, PyObject *value)
{
int count;
@@ -1052,7 +1053,7 @@
return 0;
}
-static int prop_subscript_ass_array_int(BPy_PropertyRNA * self, int keynum, PyObject *value)
+static int prop_subscript_ass_array_int(BPy_PropertyRNA *self, int keynum, PyObject *value)
{
int len= pyrna_prop_array_length(self);
@@ -1065,7 +1066,7 @@
return -1;
}
-static int pyrna_prop_ass_subscript( BPy_PropertyRNA * self, PyObject *key, PyObject *value )
+static int pyrna_prop_ass_subscript( BPy_PropertyRNA *self, PyObject *key, PyObject *value )
{
/* char *keyname = NULL; */ /* not supported yet */
@@ -1118,30 +1119,53 @@
( objobjargproc ) pyrna_prop_ass_subscript, /* mp_ass_subscript */
};
-static int pyrna_prop_contains(BPy_PropertyRNA * self, PyObject *value)
+static int pyrna_prop_contains(BPy_PropertyRNA *self, PyObject *value)
{
PointerRNA newptr; /* not used, just so RNA_property_collection_lookup_string runs */
char *keyname = _PyUnicode_AsString(value);
-
+
if(keyname==NULL) {
PyErr_SetString(PyExc_TypeError, "PropertyRNA - key in prop, key must be a string type");
return -1;
}
-
+
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 (RNA_property_collection_lookup_string(&self->ptr, self->prop, keyname, &newptr))
return 1;
-
+
return 0;
}
-static PyObject *pyrna_prop_item(BPy_PropertyRNA * self, Py_ssize_t index)
+static int pyrna_struct_contains(BPy_StructRNA *self, PyObject *value)
{
+ IDProperty *group;
+ char *name = _PyUnicode_AsString(value);
+
+ if (!name) {
+ PyErr_SetString( PyExc_TypeError, "expected a string");
+ return -1;
+ }
+
+ if(RNA_struct_idproperties_check(&self->ptr)==0) {
+ PyErr_SetString( PyExc_TypeError, "this type doesnt support IDProperties");
+ return -1;
+ }
+
+ group= RNA_struct_idproperties(&self->ptr, 0);
+
+ if(!group)
+ return 0;
+
+ return IDP_GetPropertyFromGroup(group, name) ? 1:0;
+}
+
+static PyObject *pyrna_prop_item(BPy_PropertyRNA *self, Py_ssize_t index)
+{
/* reuse subscript functions */
if (RNA_property_type(self->prop) == PROP_COLLECTION) {
return prop_subscript_collection_int(self, index);
@@ -1164,9 +1188,82 @@
(objobjproc)pyrna_prop_contains, /* sq_contains */
};
+static PySequenceMethods pyrna_struct_as_sequence = {
+ NULL, /* Cant set the len otherwise it can evaluate as false */
+ NULL, /* sq_concat */
+ NULL, /* sq_repeat */
+ NULL, /* sq_item */ /* Only set this so PySequence_Check() returns True */
+ NULL, /* sq_slice */
+ NULL, /* sq_ass_item */
+ NULL, /* sq_ass_slice */
+ (objobjproc)pyrna_struct_contains, /* sq_contains */
+};
-static PyObject *pyrna_struct_keyframe_insert(BPy_StructRNA * self, PyObject *args)
+static PyObject *pyrna_struct_subscript( BPy_StructRNA *self, PyObject *key )
{
+ /* mostly copied from BPy_IDGroup_Map_GetItem */
+ IDProperty *group, *idprop;
+ char *name= _PyUnicode_AsString(key);
+
+ if(name==NULL) {
+ PyErr_SetString( PyExc_TypeError, "only strings are allowed as keys of ID properties");
+ return NULL;
+ }
+
+ group= RNA_struct_idproperties(&self->ptr, 0);
+
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list