diff options
author | Campbell Barton <ideasman42@gmail.com> | 2011-03-22 04:38:26 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2011-03-22 04:38:26 +0300 |
commit | f3686b58856adc6929ef38d14c03197127bab017 (patch) | |
tree | dd91e0c6391261de02884fc97afca3e7d13d233b | |
parent | 74a996c8691d8c58efa3aac7c8997e7a2ec36ca2 (diff) |
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.
-rw-r--r-- | release/scripts/modules/bpy/utils.py | 9 | ||||
-rw-r--r-- | release/scripts/templates/addon_add_object.py | 8 | ||||
-rw-r--r-- | source/blender/editors/mesh/mesh_ops.c | 1 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_rna.c | 39 |
4 files changed, 46 insertions, 11 deletions
diff --git a/release/scripts/modules/bpy/utils.py b/release/scripts/modules/bpy/utils.py index f4f7d065259..28e6ee991de 100644 --- a/release/scripts/modules/bpy/utils.py +++ b/release/scripts/modules/bpy/utils.py @@ -426,21 +426,19 @@ def _bpy_module_classes(module, is_registered=False): 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 @@ def unregister_module(module, verbose=False): 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 diff --git a/release/scripts/templates/addon_add_object.py b/release/scripts/templates/addon_add_object.py index f4f1185e282..67e033271f4 100644 --- a/release/scripts/templates/addon_add_object.py +++ b/release/scripts/templates/addon_add_object.py @@ -31,8 +31,10 @@ def add_object(self, context): 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 @@ class OBJECT_OT_add_object(bpy.types.Operator, AddObjectHelper): return {'FINISHED'} -#### REGISTER #### +# Registration def add_object_button(self, context): self.layout.operator( diff --git a/source/blender/editors/mesh/mesh_ops.c b/source/blender/editors/mesh/mesh_ops.c index fc174f3c59c..1c10128cee8 100644 --- a/source/blender/editors/mesh/mesh_ops.c +++ b/source/blender/editors/mesh/mesh_ops.c @@ -282,7 +282,6 @@ void ED_keymap_mesh(wmKeyConfig *keyconf) 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); diff --git a/source/blender/python/intern/bpy_rna.c b/source/blender/python/intern/bpy_rna.c index 0b244943541..6ae38d66781 100644 --- a/source/blender/python/intern/bpy_rna.c +++ b/source/blender/python/intern/bpy_rna.c @@ -95,11 +95,13 @@ int pyrna_prop_validity_check(BPy_PropertyRNA *self) 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 @@ static char pyrna_register_class_doc[] = "\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 @@ static PyObject *pyrna_register_class(PyObject *UNUSED(self), PyObject *py_class 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 @@ static PyObject *pyrna_register_class(PyObject *UNUSED(self), PyObject *py_class 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 @@ static char pyrna_unregister_class_doc[] = ".. 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 @@ static PyObject *pyrna_unregister_class(PyObject *UNUSED(self), PyObject *py_cla 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 @@ static PyObject *pyrna_unregister_class(PyObject *UNUSED(self), PyObject *py_cla 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 */ |