[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [35679] trunk/blender: py/api registration :

Campbell Barton ideasman42 at gmail.com
Tue Mar 22 02:38:26 CET 2011


Revision: 35679
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=35679
Author:   campbellbarton
Date:     2011-03-22 01:38:26 +0000 (Tue, 22 Mar 2011)
Log Message:
-----------
py/api registration:
 move calls to the classes register/unregister function into register_class() / unregister_class() and add docs.

also other minor changes:
- remove face sorting keybinding, was Ctrl+Alt+F, this is quite and obscure feature and face order normally doesn't matter, so access from Face menu is enough.
- add commented out call to mesh.validate() in addon template since its useful to correct incomplete meshes during development.

Modified Paths:
--------------
    trunk/blender/release/scripts/modules/bpy/utils.py
    trunk/blender/release/scripts/templates/addon_add_object.py
    trunk/blender/source/blender/editors/mesh/mesh_ops.c
    trunk/blender/source/blender/python/intern/bpy_rna.c

Modified: trunk/blender/release/scripts/modules/bpy/utils.py
===================================================================
--- trunk/blender/release/scripts/modules/bpy/utils.py	2011-03-21 23:53:19 UTC (rev 35678)
+++ trunk/blender/release/scripts/modules/bpy/utils.py	2011-03-22 01:38:26 UTC (rev 35679)
@@ -426,21 +426,19 @@
 def register_module(module, verbose=False):
     if verbose:
         print("bpy.utils.register_module(%r): ..." % module)
+    cls = None
     for cls in _bpy_module_classes(module, is_registered=False):
         if verbose:
             print("    %r" % cls)
         try:
             register_class(cls)
-            cls_func = getattr(cls, "register", None)
-            if cls_func is not None:
-                cls_func()
         except:
             print("bpy.utils.register_module(): failed to registering class %r" % cls)
             import traceback
             traceback.print_exc()
     if verbose:
         print("done.\n")
-    if "cls" not in locals():
+    if cls is None:
         raise Exception("register_module(%r): defines no classes" % module)
 
 
@@ -452,9 +450,6 @@
             print("    %r" % cls)
         try:
             unregister_class(cls)
-            cls_func = getattr(cls, "unregister", None)
-            if cls_func is not None:
-                cls_func()
         except:
             print("bpy.utils.unregister_module(): failed to unregistering class %r" % cls)
             import traceback

Modified: trunk/blender/release/scripts/templates/addon_add_object.py
===================================================================
--- trunk/blender/release/scripts/templates/addon_add_object.py	2011-03-21 23:53:19 UTC (rev 35678)
+++ trunk/blender/release/scripts/templates/addon_add_object.py	2011-03-22 01:38:26 UTC (rev 35679)
@@ -31,8 +31,10 @@
     edges = []
     faces = [[0, 1, 2, 3]]
 
-    mesh_data = bpy.data.meshes.new(name='New Object Mesh')
-    mesh_data.from_pydata(verts, edges, faces)
+    mesh = bpy.data.meshes.new(name='New Object Mesh')
+    mesh.from_pydata(verts, edges, faces)
+    # useful for development when the mesh may be invalid.
+    # mesh.validate(verbose=True)
     add_object_data(context, mesh_data, operator=self)
 
 
@@ -55,7 +57,7 @@
         return {'FINISHED'}
 
 
-#### REGISTER ####
+# Registration
 
 def add_object_button(self, context):
     self.layout.operator(

Modified: trunk/blender/source/blender/editors/mesh/mesh_ops.c
===================================================================
--- trunk/blender/source/blender/editors/mesh/mesh_ops.c	2011-03-21 23:53:19 UTC (rev 35678)
+++ trunk/blender/source/blender/editors/mesh/mesh_ops.c	2011-03-22 01:38:26 UTC (rev 35679)
@@ -282,7 +282,6 @@
 	
 	WM_keymap_add_item(keymap, "MESH_OT_fill", FKEY, KM_PRESS, KM_ALT, 0);
 	WM_keymap_add_item(keymap, "MESH_OT_beautify_fill", FKEY, KM_PRESS, KM_SHIFT|KM_ALT, 0);
-	WM_keymap_add_item(keymap, "MESH_OT_sort_faces", FKEY, KM_PRESS, KM_ALT|KM_CTRL, 0);
 	WM_keymap_add_item(keymap, "MESH_OT_quads_convert_to_tris", TKEY, KM_PRESS, KM_CTRL, 0);
 	WM_keymap_add_item(keymap, "MESH_OT_tris_convert_to_quads", JKEY, KM_PRESS, KM_ALT, 0);
 	WM_keymap_add_item(keymap, "MESH_OT_edge_flip", FKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0);

Modified: trunk/blender/source/blender/python/intern/bpy_rna.c
===================================================================
--- trunk/blender/source/blender/python/intern/bpy_rna.c	2011-03-21 23:53:19 UTC (rev 35678)
+++ trunk/blender/source/blender/python/intern/bpy_rna.c	2011-03-22 01:38:26 UTC (rev 35679)
@@ -95,11 +95,13 @@
 	return -1;
 }
 
+#if defined(USE_PYRNA_INVALIDATE_GC) || defined(USE_PYRNA_INVALIDATE_WEAKREF)
 static void pyrna_invalidate(BPy_DummyPointerRNA *self)
 {
 	self->ptr.type= NULL; /* this is checked for validity */
 	self->ptr.id.data= NULL; /* should not be needed but prevent bad pointer access, just incase */
 }
+#endif
 
 #ifdef USE_PYRNA_INVALIDATE_GC
 #define FROM_GC(g) ((PyObject *)(((PyGC_Head *)g)+1))
@@ -6106,6 +6108,8 @@
 "\n"
 "   Register a subclass of a blender type in (:class:`Panel`, :class:`Menu`, :class:`Header`, :class:`Operator`, :class:`KeyingSetInfo`, :class:`RenderEngine`).\n"
 "\n"
+"   If the class has a *register* class method it will be called before registration.\n"
+"\n"
 "   .. note:: :exc:`ValueError` exception is raised if the class is not a subclass of a registerable blender class.\n"
 "\n"
 ;
@@ -6118,6 +6122,7 @@
 	StructRNA *srna;
 	StructRNA *srna_new;
 	const char *identifier;
+	PyObject *py_cls_meth;
 
 	if(PyDict_GetItemString(((PyTypeObject*)py_class)->tp_dict, "bl_rna")) {
 		PyErr_SetString(PyExc_AttributeError, "register_class(...): already registered as a subclass");
@@ -6145,6 +6150,22 @@
 		return NULL;
 	}
 
+	/* call classed register function () */
+	py_cls_meth= PyObject_GetAttrString(py_class, "register");
+	if(py_cls_meth == NULL) {
+		PyErr_Clear();
+	}
+	else {
+		PyObject *ret= PyObject_CallObject(py_cls_meth, NULL);
+		if(ret) {
+			Py_DECREF(ret);
+		}
+		else {
+			return NULL;
+		}
+	}
+
+
 	/* get the context, so register callback can do necessary refreshes */
 	C= BPy_GetContext();
 
@@ -6210,6 +6231,8 @@
 ".. method:: unregister_class(cls)\n"
 "\n"
 "   Unload the python class from blender.\n"
+"\n"
+"   If the class has an *unregister* class method it will be called before unregistering.\n"
 ;
 PyMethodDef meth_bpy_unregister_class= {"unregister_class", pyrna_unregister_class, METH_O, pyrna_unregister_class_doc};
 static PyObject *pyrna_unregister_class(PyObject *UNUSED(self), PyObject *py_class)
@@ -6217,6 +6240,7 @@
 	bContext *C= NULL;
 	StructUnregisterFunc unreg;
 	StructRNA *srna;
+	PyObject *py_cls_meth;
 
 	/*if(PyDict_GetItemString(((PyTypeObject*)py_class)->tp_dict, "bl_rna")==NULL) {
 		PWM_cursor_wait(0);
@@ -6236,6 +6260,21 @@
 		return NULL;
 	}
 
+	/* call classed register function */
+	py_cls_meth= PyObject_GetAttrString(py_class, "unregister");
+	if(py_cls_meth == NULL) {
+		PyErr_Clear();
+	}
+	else {
+		PyObject *ret= PyObject_CallObject(py_cls_meth, NULL);
+		if(ret) {
+			Py_DECREF(ret);
+		}
+		else {
+			return NULL;
+		}
+	}
+
 	/* should happen all the time but very slow */
 	if(G.f & G_DEBUG) {
 		/* remove all properties using this class */




More information about the Bf-blender-cvs mailing list