[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [24713] trunk/blender: use a metaclass to have operator attributes register and display in the order defined .

Campbell Barton ideasman42 at gmail.com
Fri Nov 20 21:58:46 CET 2009


Revision: 24713
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=24713
Author:   campbellbarton
Date:     2009-11-20 21:58:46 +0100 (Fri, 20 Nov 2009)

Log Message:
-----------
use a metaclass to have operator attributes register and display in the order defined.

Modified Paths:
--------------
    trunk/blender/release/scripts/modules/bpy_types.py
    trunk/blender/release/scripts/modules/rna_prop_ui.py
    trunk/blender/source/blender/python/intern/bpy_operator_wrap.c
    trunk/blender/source/blender/python/intern/bpy_rna.c

Modified: trunk/blender/release/scripts/modules/bpy_types.py
===================================================================
--- trunk/blender/release/scripts/modules/bpy_types.py	2009-11-20 20:53:23 UTC (rev 24712)
+++ trunk/blender/release/scripts/modules/bpy_types.py	2009-11-20 20:58:46 UTC (rev 24713)
@@ -92,3 +92,18 @@
         return ord_ind(verts[0], verts[1]),  ord_ind(verts[1], verts[2]),  ord_ind(verts[2], verts[3]),  ord_ind(verts[3], verts[0])
 
     edge_keys = property(_get_edge_keys)
+
+
+import collections
+class OrderedMeta(type):
+    def __init__(cls, name, bases, attributes):
+        super(OrderedMeta, cls).__init__(name, bases, attributes)
+        cls.order = list(attributes.keys())
+    def __prepare__(name, bases, **kwargs):
+        return collections.OrderedDict()
+
+class Operator(StructRNA, metaclass=OrderedMeta):
+    '''
+    Only defined so operators members can be used by accessing self.order
+    '''
+    pass

Modified: trunk/blender/release/scripts/modules/rna_prop_ui.py
===================================================================
--- trunk/blender/release/scripts/modules/rna_prop_ui.py	2009-11-20 20:53:23 UTC (rev 24712)
+++ trunk/blender/release/scripts/modules/rna_prop_ui.py	2009-11-20 20:58:46 UTC (rev 24713)
@@ -138,14 +138,13 @@
     '''Internal use (edit a property path)'''
     bl_idname = "wm.properties_edit"
     bl_label = "Edit Property!"
-
-    description = StringProperty(name="Tip", default="")
+    
     path = rna_path
-    value = rna_value
     property = rna_property
-    
+    value = rna_value
     min = rna_min
     max = rna_max
+    description = StringProperty(name="Tip", default="")
     
     # the class instance is not persistant, need to store in the class
     # not ideal but changes as the op runs.

Modified: trunk/blender/source/blender/python/intern/bpy_operator_wrap.c
===================================================================
--- trunk/blender/source/blender/python/intern/bpy_operator_wrap.c	2009-11-20 20:53:23 UTC (rev 24712)
+++ trunk/blender/source/blender/python/intern/bpy_operator_wrap.c	2009-11-20 20:58:46 UTC (rev 24713)
@@ -294,7 +294,6 @@
 		 * later */
 		RNA_def_struct_identifier(ot->srna, ot->idname);
 
-
 		if(pyrna_deferred_register_props(ot->srna, item)!=0) {
 			/* failed to register operator props */
 			PyErr_Print();

Modified: trunk/blender/source/blender/python/intern/bpy_rna.c
===================================================================
--- trunk/blender/source/blender/python/intern/bpy_rna.c	2009-11-20 20:53:23 UTC (rev 24712)
+++ trunk/blender/source/blender/python/intern/bpy_rna.c	2009-11-20 20:58:46 UTC (rev 24713)
@@ -3550,55 +3550,86 @@
 	return NULL;
 }
 
-int pyrna_deferred_register_props(StructRNA *srna, PyObject *class_dict)
+
+static int deferred_register_prop(StructRNA *srna, PyObject *item, PyObject *key, PyObject *dummy_args)
 {
-	PyObject *dummy_args, *item, *key;
-	Py_ssize_t pos = 0;
+	/* We only care about results from C which
+	 * are for sure types, save some time with error */
+	if(PyTuple_CheckExact(item) && PyTuple_GET_SIZE(item)==2) {
 
-	dummy_args = PyTuple_New(0);
-	
-	while (PyDict_Next(class_dict, &pos, &key, &item)) {
 		PyObject *py_func_ptr, *py_kw, *py_srna_cobject, *py_ret;
-		/* We only care about results from C which
-		 * are for sure types, save some time with error */
-		if(PyTuple_CheckExact(item)) {
-			if(PyArg_ParseTuple(item, "O!O!", &PyCObject_Type, &py_func_ptr, &PyDict_Type, &py_kw)) {
 
-				PyObject *(*pyfunc)(PyObject *, PyObject *, PyObject *);
-				pyfunc = PyCObject_AsVoidPtr(py_func_ptr);
-				py_srna_cobject = PyCObject_FromVoidPtr(srna, NULL);
+		if(PyArg_ParseTuple(item, "O!O!", &PyCObject_Type, &py_func_ptr, &PyDict_Type, &py_kw)) {
 
-				/* not 100% nice :/, modifies the dict passed, should be ok */
-				PyDict_SetItemString(py_kw, "attr", key);
+			PyObject *(*pyfunc)(PyObject *, PyObject *, PyObject *);
 
-				py_ret = pyfunc(py_srna_cobject, dummy_args, py_kw);
-				Py_DECREF(py_srna_cobject);
+			pyfunc = PyCObject_AsVoidPtr(py_func_ptr);
+			py_srna_cobject = PyCObject_FromVoidPtr(srna, NULL);
 
-				if(py_ret) {
-					Py_DECREF(py_ret);
-				}
-				else {
-					PyErr_Print();
-					PyErr_Clear();
+			/* not 100% nice :/, modifies the dict passed, should be ok */
+			PyDict_SetItemString(py_kw, "attr", key);
 
-					PyErr_Format(PyExc_ValueError, "StructRNA \"%.200s\" registration error: %.200s could not register\n", RNA_struct_identifier(srna), _PyUnicode_AsString(key));
-					Py_DECREF(dummy_args);
-					return -1;
-				}
+			py_ret = pyfunc(py_srna_cobject, dummy_args, py_kw);
+			Py_DECREF(py_srna_cobject);
+
+			if(py_ret) {
+				Py_DECREF(py_ret);
 			}
 			else {
-				/* Since this is a class dict, ignore args that can't be passed */
-
-				/* for testing only */
-				/* PyObSpit("Why doesn't this work??", item);
-				PyErr_Print(); */
+				PyErr_Print();
 				PyErr_Clear();
 
+				PyErr_Format(PyExc_ValueError, "StructRNA \"%.200s\" registration error: %.200s could not register\n", RNA_struct_identifier(srna), _PyUnicode_AsString(key));
+				Py_DECREF(dummy_args);
+				return -1;
 			}
 		}
+		else {
+			/* Since this is a class dict, ignore args that can't be passed */
+
+			/* for testing only */
+			/* PyObSpit("Why doesn't this work??", item);
+			PyErr_Print(); */
+			PyErr_Clear();
+		}
 	}
 
+	return 0;
+}
+
+int pyrna_deferred_register_props(StructRNA *srna, PyObject *class_dict)
+{
+	PyObject *item, *key;
+	PyObject *order;
+	PyObject *dummy_args;
+	Py_ssize_t pos = 0;
+	int ret;
+
+	dummy_args = PyTuple_New(0);
+
+	order= PyDict_GetItemString(class_dict, "order");
+
+	if(order && PyList_Check(order)) {
+		printf("using order\n");
+		for(pos= 0; pos<PyList_GET_SIZE(order); pos++) {
+			key= PyList_GET_ITEM(order, pos);
+			item= PyDict_GetItem(class_dict, key);
+			ret= deferred_register_prop(srna, item, key, dummy_args);
+			if(ret==-1)
+				break;
+		}
+	}
+	else {
+		while (PyDict_Next(class_dict, &pos, &key, &item)) {
+			ret= deferred_register_prop(srna, item, key, dummy_args);
+
+			if(ret==-1)
+				break;
+		}
+	}
+
 	Py_DECREF(dummy_args);
+
 	return 0;
 }
 





More information about the Bf-blender-cvs mailing list