[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [17679] branches/blender2.5/blender/source /blender: Added RNA functions from PyRNA

Campbell Barton ideasman42 at gmail.com
Tue Dec 2 15:36:37 CET 2008


Revision: 17679
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=17679
Author:   campbellbarton
Date:     2008-12-02 15:36:35 +0100 (Tue, 02 Dec 2008)

Log Message:
-----------
Added RNA functions from PyRNA
* RNA_property_enum_value
* RNA_property_enum_identifier
To get an enum string from a value and a value from an enum.

BPy_StructRNA types (objects, meshes, images etc) can now be used as dictionary keys.

Modified Paths:
--------------
    branches/blender2.5/blender/source/blender/makesrna/RNA_access.h
    branches/blender2.5/blender/source/blender/makesrna/intern/rna_access.c
    branches/blender2.5/blender/source/blender/python/intern/bpy_rna.c

Modified: branches/blender2.5/blender/source/blender/makesrna/RNA_access.h
===================================================================
--- branches/blender2.5/blender/source/blender/makesrna/RNA_access.h	2008-12-02 14:22:52 UTC (rev 17678)
+++ branches/blender2.5/blender/source/blender/makesrna/RNA_access.h	2008-12-02 14:36:35 UTC (rev 17679)
@@ -174,7 +174,10 @@
 int RNA_property_string_maxlength(PointerRNA *ptr, PropertyRNA *prop);
 
 void RNA_property_enum_items(PointerRNA *ptr, PropertyRNA *prop, const EnumPropertyItem **item, int *totitem);
+int RNA_property_enum_value(PointerRNA *ptr, PropertyRNA *prop, const char *identifier, int *value);
+int RNA_property_enum_identifier(PointerRNA *ptr, PropertyRNA *prop, const int value, const char **identifier);
 
+
 const char *RNA_property_ui_name(PointerRNA *ptr, PropertyRNA *prop);
 const char *RNA_property_ui_description(PointerRNA *ptr, PropertyRNA *prop);
 

Modified: branches/blender2.5/blender/source/blender/makesrna/intern/rna_access.c
===================================================================
--- branches/blender2.5/blender/source/blender/makesrna/intern/rna_access.c	2008-12-02 14:22:52 UTC (rev 17678)
+++ branches/blender2.5/blender/source/blender/makesrna/intern/rna_access.c	2008-12-02 14:36:35 UTC (rev 17679)
@@ -401,6 +401,40 @@
 	*totitem= eprop->totitem;
 }
 
+int RNA_property_enum_value(PointerRNA *ptr, PropertyRNA *prop, const char *identifier, int *value)
+{	
+	const EnumPropertyItem *item;
+	int totitem, i;
+	
+	RNA_property_enum_items(ptr, prop, &item, &totitem);
+	
+	for (i=0; i<totitem; i++) {
+		if (strcmp(item[i].identifier, identifier)==0) {
+			*value = item[i].value;
+			return 1;
+		}
+	}
+
+	return 0;
+}
+
+int RNA_property_enum_identifier(PointerRNA *ptr, PropertyRNA *prop, const int value, const char **identifier)
+{	
+	const EnumPropertyItem *item;
+	int totitem, i;
+	
+	RNA_property_enum_items(ptr, prop, &item, &totitem);
+	
+	for (i=0; i<totitem; i++) {
+		if (item[i].value==value) {
+			*identifier = item[i].identifier;
+			return 1;
+		}
+	}
+	
+	return 0;
+}
+
 const char *RNA_property_ui_name(PointerRNA *ptr, PropertyRNA *prop)
 {
 	PropertyRNA *oldprop= prop;
@@ -764,6 +798,7 @@
 		return eprop->defaultvalue;
 }
 
+
 void RNA_property_enum_set(PointerRNA *ptr, PropertyRNA *prop, int value)
 {
 	EnumPropertyRNA *eprop= (EnumPropertyRNA*)prop;

Modified: branches/blender2.5/blender/source/blender/python/intern/bpy_rna.c
===================================================================
--- branches/blender2.5/blender/source/blender/python/intern/bpy_rna.c	2008-12-02 14:22:52 UTC (rev 17678)
+++ branches/blender2.5/blender/source/blender/python/intern/bpy_rna.c	2008-12-02 14:36:35 UTC (rev 17679)
@@ -54,6 +54,12 @@
 	return PyUnicode_FromFormat( "[BPy_PropertyRNA \"%s\" -> \"%s\" ]", RNA_struct_identifier(&self->ptr), RNA_property_identifier(&self->ptr, self->prop) );
 }
 
+static long pyrna_struct_hash( BPy_StructRNA * self )
+{
+	return (long)self->ptr.data;
+}
+
+
 static PyObject * pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop)
 {
 	PyObject *ret;
@@ -86,26 +92,17 @@
 		break;
 	}
 	case PROP_ENUM:
-	{		
-		const EnumPropertyItem *item;
-		int totitem, i, val;
-
-		val = RNA_property_enum_get(ptr, prop);
+	{
+		const char *identifier;
+		int val = RNA_property_enum_get(ptr, prop);
 		
-		RNA_property_enum_items(ptr, prop, &item, &totitem);
-		
-		for (i=0; i<totitem; i++) {
-			if (item[i].value == val)
-				break;
-		}
-		
-		if (i<totitem) {
-			ret = PyUnicode_FromString( item[i].identifier );
+		if (RNA_property_enum_identifier(ptr, prop, val, &identifier)) {
+			ret = PyUnicode_FromString( identifier );
 		} else {
 			PyErr_Format(PyExc_AttributeError, "enum \"%d\" not found", val);
 			ret = NULL;
 		}
-		
+
 		break;
 	}
 	case PROP_POINTER:
@@ -287,19 +284,8 @@
 				PyErr_SetString(PyExc_TypeError, "expected a string type");
 				return -1;
 			} else {
-				const EnumPropertyItem *item;
-				int totitem, i, val;
-				
-				RNA_property_enum_items(ptr, prop, &item, &totitem);
-				
-				for (i=0; i<totitem; i++) {
-					if (strcmp(item[i].identifier, param)==0) {
-						val = item[i].value;
-						break;
-					}
-				}
-				
-				if (i<totitem) {
+				int val;
+				if (RNA_property_enum_value(ptr, prop, param, &val)) {
 					RNA_property_enum_set(ptr, prop, val);
 				} else {
 					PyErr_Format(PyExc_AttributeError, "enum \"%s\" not found", param);
@@ -926,7 +912,7 @@
 
 	/* More standard operations (here for binary compatibility) */
 
-	NULL,						/* hashfunc tp_hash; */
+	( hashfunc )pyrna_struct_hash,	/* hashfunc tp_hash; */
 	NULL,						/* ternaryfunc tp_call; */
 	NULL,                       /* reprfunc tp_str; */
 	( getattrofunc ) pyrna_struct_getattro,	/* getattrofunc tp_getattro; */





More information about the Bf-blender-cvs mailing list