[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [43152] trunk/blender: add bpy collection method .find(key), so you can get the index of an item in a collection, -1 if not found.

Campbell Barton ideasman42 at gmail.com
Thu Jan 5 07:05:46 CET 2012


Revision: 43152
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=43152
Author:   campbellbarton
Date:     2012-01-05 06:05:45 +0000 (Thu, 05 Jan 2012)
Log Message:
-----------
add bpy collection method .find(key), so you can get the index of an item in a collection, -1 if not found.

use this to replace bge text ui py function.

Modified Paths:
--------------
    trunk/blender/release/scripts/startup/bl_ui/space_logic.py
    trunk/blender/source/blender/python/intern/bpy_rna.c

Modified: trunk/blender/release/scripts/startup/bl_ui/space_logic.py
===================================================================
--- trunk/blender/release/scripts/startup/bl_ui/space_logic.py	2012-01-05 06:02:42 UTC (rev 43151)
+++ trunk/blender/release/scripts/startup/bl_ui/space_logic.py	2012-01-05 06:05:45 UTC (rev 43152)
@@ -20,13 +20,6 @@
 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 @@
 
         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 @@
 
         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()

Modified: trunk/blender/source/blender/python/intern/bpy_rna.c
===================================================================
--- trunk/blender/source/blender/python/intern/bpy_rna.c	2012-01-05 06:02:42 UTC (rev 43151)
+++ trunk/blender/source/blender/python/intern/bpy_rna.c	2012-01-05 06:05:45 UTC (rev 43152)
@@ -4073,7 +4073,6 @@
 	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 @@
 	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 @@
 	{"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}
 };
 



More information about the Bf-blender-cvs mailing list