add bpy collection method .find(key), so you can get the index of an item in a collec...
authorCampbell Barton <ideasman42@gmail.com>
Thu, 5 Jan 2012 06:05:45 +0000 (06:05 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Thu, 5 Jan 2012 06:05:45 +0000 (06:05 +0000)
use this to replace bge text ui py function.

release/scripts/startup/bl_ui/space_logic.py
source/blender/python/intern/bpy_rna.c

index 79a3f72ff4866a5ddcf5b61b50da511cbb975dcd..7057fed75d27e3fd3c73ed310b189ce19ba85659 100644 (file)
 import bpy
 from bpy.types import Header, Menu, Panel
 
-def get_id_by_name(properties, name):
-    """returns ID"""
-    for i, prop in enumerate(properties):
-            if prop.name == name:
-                return i
-    return -1
-
 class LOGIC_PT_properties(Panel):
     bl_space_type = 'LOGIC_EDITOR'
     bl_region_type = 'UI'
@@ -42,20 +35,22 @@ class LOGIC_PT_properties(Panel):
 
         ob = context.active_object
         game = ob.game
+        is_font = (ob.type == 'FONT')
 
-        if ob.type == 'FONT':
-            prop = game.properties.get("Text")
-            if prop:
-                layout.operator("object.game_property_remove", text="Text Game Property", icon='X').index = get_id_by_name(game.properties, "Text")
+        if is_font:
+            prop_index = game.properties.find("Text")
+            if prop_index != -1:
+                layout.operator("object.game_property_remove", text="Renove Text Game Property", icon='X').index = prop_index
                 row = layout.row()
                 sub=row.row()
                 sub.enabled=0
+                prop = game.properties[prop_index]
                 sub.prop(prop, "name", text="")
                 row.prop(prop, "type", text="")
                 # get the property from the body, not the game property
                 row.prop(ob.data, "body", text="")
             else:
-                props=layout.operator("object.game_property_new", text="Text Game Property", icon='ZOOMIN')
+                props=layout.operator("object.game_property_new", text="Add Text Game Property", icon='ZOOMIN')
                 props.name='Text'
                 props.type='STRING'
 
@@ -63,7 +58,7 @@ class LOGIC_PT_properties(Panel):
 
         for i, prop in enumerate(game.properties):
 
-            if ob.type == 'FONT' and prop.name == "Text":
+            if is_font and i == prop_index:
                 continue
 
             box = layout.box()
index d911fcb99b1cc63eb77c3c7e9236bc43621ac7b9..9dfbe64e905db44e2feb89852c1973c8994f23d3 100644 (file)
@@ -4073,7 +4073,6 @@ static PyObject *pyrna_struct_as_pointer(BPy_StructRNA *self)
        return PyLong_FromVoidPtr(self->ptr.data);
 }
 
-/* TODO, get (string, lib) pair */
 PyDoc_STRVAR(pyrna_prop_collection_get_doc,
 ".. method:: get(key, default=None)\n"
 "\n"
@@ -4120,6 +4119,51 @@ static PyObject *pyrna_prop_collection_get(BPy_PropertyRNA *self, PyObject *args
        return Py_INCREF(def), def;
 }
 
+PyDoc_STRVAR(pyrna_prop_collection_find_doc,
+".. method:: find(key)\n"
+"\n"
+"   Returns the index of a key in a collection or -1 when not found\n"
+"   (matches pythons string find function of the same name).\n"
+"\n"
+"   :arg key: The identifier for the collection member.\n"
+"   :type key: string\n"
+"   :return: index of the key.\n"
+"   :rtype: int\n"
+);
+static PyObject *pyrna_prop_collection_find(BPy_PropertyRNA *self, PyObject *key_ob)
+{
+       Py_ssize_t key_len_ssize_t;
+       const char *key = _PyUnicode_AsStringAndSize(key_ob, &key_len_ssize_t);
+       const int key_len = (int)key_len_ssize_t; /* comare with same type */
+
+       char name[256], *nameptr;
+       int namelen;
+       int i = 0;
+       int index = -1;
+
+       PYRNA_PROP_CHECK_OBJ(self);
+
+       RNA_PROP_BEGIN(&self->ptr, itemptr, self->prop) {
+               nameptr = RNA_struct_name_get_alloc(&itemptr, name, sizeof(name), &namelen);
+
+               if (nameptr) {
+                       if ((key_len == namelen) && memcmp(nameptr, key, key_len) == 0) {
+                               index = i;
+                               break;
+                       }
+
+                       if (name != nameptr) {
+                               MEM_freeN(nameptr);
+                       }
+               }
+
+               i++;
+       }
+       RNA_PROP_END;
+
+       return PyLong_FromSsize_t(index);
+}
+
 static void foreach_attr_type( BPy_PropertyRNA *self, const char *attr,
                                                                        /* values to assign */
                                                                        RawPropertyType *raw_type, int *attr_tot, int *attr_signed)
@@ -4503,6 +4547,7 @@ static struct PyMethodDef pyrna_prop_collection_methods[] = {
        {"values", (PyCFunction)pyrna_prop_collection_values, METH_NOARGS, pyrna_prop_collection_values_doc},
 
        {"get", (PyCFunction)pyrna_prop_collection_get, METH_VARARGS, pyrna_prop_collection_get_doc},
+       {"find", (PyCFunction)pyrna_prop_collection_find, METH_O, pyrna_prop_collection_find_doc},
        {NULL, NULL, 0, NULL}
 };