adding __contains__ to python rna props.
authorCampbell Barton <ideasman42@gmail.com>
Sat, 13 Jun 2009 08:04:43 +0000 (08:04 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Sat, 13 Jun 2009 08:04:43 +0000 (08:04 +0000)
example usage.
 if "Scene" in bpy.data.scenes: print(True)

Only works for strings with collection property types.

source/blender/python/intern/bpy_rna.c

index 12c19bd34710b73b47249ac79790308bb9b32143..207ca41ed46f4ab373df606f22a3e6f6c30adefb 100644 (file)
@@ -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) */