[Bf-blender-cvs] [2e336a41ec] app-templates: Use class to backup & restore application state

Campbell Barton noreply at git.blender.org
Thu Mar 16 13:13:48 CET 2017


Commit: 2e336a41ec3d2175284dd44f87cc91265997434c
Author: Campbell Barton
Date:   Thu Mar 16 23:16:54 2017 +1100
Branches: app-templates
https://developer.blender.org/rB2e336a41ec3d2175284dd44f87cc91265997434c

Use class to backup & restore application state

===================================================================

M	release/app_templates/101/template/__init__.py

===================================================================

diff --git a/release/app_templates/101/template/__init__.py b/release/app_templates/101/template/__init__.py
index ed9c51800d..07b7334c5f 100644
--- a/release/app_templates/101/template/__init__.py
+++ b/release/app_templates/101/template/__init__.py
@@ -21,36 +21,55 @@
 import bpy
 import bl_app_override
 
-class_store = []
+
+class AppStateStore:
+    # Utility class to encapsulate application state, backup and restore.
+    __slots__ = (
+        "class_store",
+    )
+
+    def __init__(self):
+        self.class_store = []
+
+    def backup(self):
+        assert(len(self.class_store) == 0)
+
+        self.class_store.extend(
+            bl_app_override.class_filter(
+                bpy.types.Panel,
+                # match any of these values
+                bl_region_type={'TOOLS', 'WINDOW'},
+                bl_space_type={'VIEW_3D', 'PROPERTIES'},
+                # keep basic panels
+                black_list={
+                    'OBJECT_PT_transform',
+                    'VIEW3D_PT_tools_add_object',
+                    'VIEW3D_PT_tools_meshedit',
+                },
+            ),
+        )
+
+        unregister = bpy.utils.unregister_class
+        for cls in self.class_store:
+            unregister(cls)
+
+    def restore(self):
+        assert(len(self.class_store) != 0)
+
+        register = bpy.utils.register_class
+        for cls in self.class_store:
+            register(cls)
+        self.class_store.clear()
+
+
+app_state = AppStateStore()
+
 
 def register():
     print("Template Register", __file__)
-
-    class_store.clear()
-
-    class_store.extend(
-        bl_app_override.class_filter(
-            bpy.types.Panel,
-            # match any of these values
-            bl_region_type={'TOOLS', 'WINDOW'},
-            bl_space_type={'VIEW_3D', 'PROPERTIES'},
-            # keep basic panels
-            black_list={
-                'VIEW3D_PT_tools_add_object',
-                'OBJECT_PT_transform',
-            },
-        ),
-    )
-    unregister = bpy.utils.unregister_class
-    for cls in class_store:
-        unregister(cls)
+    app_state.backup()
 
 
 def unregister():
     print("Template Unregister", __file__)
-
-    register = bpy.utils.register_class
-    for cls in class_store:
-        register(cls)
-    class_store.clear()
-
+    app_state.restore()




More information about the Bf-blender-cvs mailing list