Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCampbell Barton <ideasman42@gmail.com>2009-06-13 12:04:43 +0400
committerCampbell Barton <ideasman42@gmail.com>2009-06-13 12:04:43 +0400
commite10e1ac04e63ed04a5712e515ed28eb92a78fd62 (patch)
tree9b78c6ecdc9f0844143933c97506c8b1517d385c /source/blender/python
parentd35d04a78961946145be256ed1ff45342f7633b8 (diff)
adding __contains__ to python rna props.
example usage. if "Scene" in bpy.data.scenes: print(True) Only works for strings with collection property types.
Diffstat (limited to 'source/blender/python')
-rw-r--r--source/blender/python/intern/bpy_rna.c35
1 files changed, 34 insertions, 1 deletions
diff --git a/source/blender/python/intern/bpy_rna.c b/source/blender/python/intern/bpy_rna.c
index 12c19bd3471..207ca41ed46 100644
--- a/source/blender/python/intern/bpy_rna.c
+++ b/source/blender/python/intern/bpy_rna.c
@@ -763,6 +763,39 @@ static PyMappingMethods pyrna_prop_as_mapping = {
( objobjargproc ) pyrna_prop_assign_subscript, /* mp_ass_subscript */
};
+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_SystemError, "PropertyRNA - key in prop, key must be a string type");
+ return -1;
+ }
+
+ if (RNA_property_type(self->prop) != PROP_COLLECTION) {
+ PyErr_SetString(PyExc_SystemError, "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 PySequenceMethods pyrna_prop_as_sequence = {
+ NULL, /* Cant set the len otherwise it can evaluate as false */
+ NULL, /* sq_concat */
+ NULL, /* sq_repeat */
+ NULL, /* sq_item */
+ NULL, /* sq_slice */
+ NULL, /* sq_ass_item */
+ NULL, /* sq_ass_slice */
+ (objobjproc)pyrna_prop_contains, /* sq_contains */
+};
+
static PyObject *pyrna_struct_dir(BPy_StructRNA * self)
{
PyObject *ret, *dict;
@@ -1403,7 +1436,7 @@ PyTypeObject pyrna_prop_Type = {
/* Method suites for standard classes */
NULL, /* PyNumberMethods *tp_as_number; */
- NULL, /* PySequenceMethods *tp_as_sequence; */
+ &pyrna_prop_as_sequence, /* PySequenceMethods *tp_as_sequence; */
&pyrna_prop_as_mapping, /* PyMappingMethods *tp_as_mapping; */
/* More standard operations (here for binary compatibility) */