[Bf-extensions-cvs] [3aaa6f2] master: Improved the user interface by addding a list widget to mathVis. Also got rid of the global variables

Gaia Clary noreply at git.blender.org
Thu Dec 1 20:24:32 CET 2016


Commit: 3aaa6f21d001ed97b4d6a381f0c97d5086190913
Author: Gaia Clary
Date:   Thu Dec 1 20:23:52 2016 +0100
Branches: master
https://developer.blender.org/rBA3aaa6f21d001ed97b4d6a381f0c97d5086190913

Improved the user interface by addding a list widget to mathVis. Also got rid of the global variables

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

M	space_view3d_math_vis/__init__.py
M	space_view3d_math_vis/draw.py
M	space_view3d_math_vis/utils.py

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

diff --git a/space_view3d_math_vis/__init__.py b/space_view3d_math_vis/__init__.py
index 50e7cf8..8bccd95 100644
--- a/space_view3d_math_vis/__init__.py
+++ b/space_view3d_math_vis/__init__.py
@@ -40,8 +40,7 @@ else:
     from . import utils, draw
 
 import bpy
-from bpy.props import StringProperty, BoolProperty, BoolVectorProperty, FloatProperty, PointerProperty, CollectionProperty
-from .utils import get_var_states
+from bpy.props import StringProperty, BoolProperty, BoolVectorProperty, FloatProperty, IntProperty, PointerProperty, CollectionProperty
 
 
 class PanelConsoleVars(bpy.types.Panel):
@@ -54,34 +53,25 @@ class PanelConsoleVars(bpy.types.Panel):
 
     def draw(self, context):
         layout = self.layout
-        box = layout.box()
-        col = box.column(align=True)
-        variables = utils.get_math_data()
-        var_states = get_var_states()
-
-        for key in sorted(variables):
-            ktype = variables[key]
-            row = col.row(align=True)
-            row.label(text='%s - %s' % (key, ktype.__name__))
-
-            icon = 'RESTRICT_VIEW_OFF' if var_states.is_visible(key) else 'RESTRICT_VIEW_ON'
-            prop = row.operator("mathvis.toggle_display", text='', icon=icon, emboss=False)
-            prop.key = key
-
-            icon = 'LOCKED' if var_states.is_locked(key) else 'UNLOCKED'
-            prop = row.operator("mathvis.toggle_lock", text='', icon=icon, emboss=False)
-            prop.key = key
-
-            if var_states.is_locked(key):
-                row.label(text='', icon='BLANK1')
-            else:
-                prop = row.operator("mathvis.delete_var", text='', icon='X', emboss=False)
-                prop.key = key
+        state_props = bpy.context.window_manager.MathVisStatePropList
+
+        if len(state_props) == 0:
+            box = layout.box()
+            col = box.column(align=True)
+            col.label("No vars to display")
+        else:
+            layout.template_list('MathVisVarList',
+                                 'MathVisStatePropList',
+                                 bpy.context.window_manager,
+                                 'MathVisStatePropList',
+                                 bpy.context.window_manager.MathVisProp,
+                                 'index',
+                                 rows=10)
 
         col = layout.column()
-        col.prop(bpy.context.scene.MathVisProp, "name_hide")
-        col.prop(bpy.context.scene.MathVisProp, "bbox_hide")
-        col.prop(bpy.context.scene.MathVisProp, "bbox_scale")
+        col.prop(bpy.context.window_manager.MathVisProp, "name_hide")
+        col.prop(bpy.context.window_manager.MathVisProp, "bbox_hide")
+        col.prop(bpy.context.window_manager.MathVisProp, "bbox_scale")
         col.operator("mathvis.cleanup_console")
 
 
@@ -95,8 +85,7 @@ class DeleteVar(bpy.types.Operator):
 
     def execute(self, context):
         locals = utils.console_namespace()
-        var_states = get_var_states()
-        var_states.delete(self.key)
+        utils.VarStates.delete(self.key)
         del locals[self.key]
         draw.tag_redraw_all_view3d_areas()
         return {'FINISHED'}
@@ -111,8 +100,7 @@ class ToggleDisplay(bpy.types.Operator):
     key = StringProperty(name="Key")
 
     def execute(self, context):
-        var_states = get_var_states()
-        var_states.toggle_display_state(self.key)
+        utils.VarStates.toggle_display_state(self.key)
         draw.tag_redraw_all_view3d_areas()
         return {'FINISHED'}
 
@@ -126,8 +114,7 @@ class ToggleLock(bpy.types.Operator):
     key = StringProperty(name="Key")
 
     def execute(self, context):
-        var_states = get_var_states()
-        var_states.toggle_lock_state(self.key)
+        utils.VarStates.toggle_lock_state(self.key)
         draw.tag_redraw_all_view3d_areas()
         return {'FINISHED'}
 
@@ -139,8 +126,7 @@ class ToggleMatrixBBoxDisplay(bpy.types.Operator):
     bl_options = {'REGISTER'}
 
     def execute(self, context):
-        var_states = get_var_states()
-        var_states.toggle_show_bbox()
+        utils.VarStates.toggle_show_bbox()
         draw.tag_redraw_all_view3d_areas()
         return {'FINISHED'}
 
@@ -162,6 +148,7 @@ def menu_func_cleanup(self, context):
 
 
 def console_hook():
+    utils.VarStates.store_states()
     draw.tag_redraw_all_view3d_areas()
     context = bpy.context
     for window in context.window_manager.windows:
@@ -173,11 +160,49 @@ def call_console_hook(self, context):
 
 
 class MathVisStateProp(bpy.types.PropertyGroup):
-    key = StringProperty()
+    ktype = StringProperty()
     state = BoolVectorProperty(default=(False, False), size=2)
 
 
-class MathVisProp(bpy.types.PropertyGroup):
+class MathVisVarList(bpy.types.UIList):
+
+    def draw_item(self,
+                  context,
+                  layout,
+                  data,
+                  item,
+                  icon,
+                  active_data,
+                  active_propname
+                  ):
+
+        col = layout.column()
+        key = item.name
+        ktype = item.ktype
+        is_visible = item.state[0]
+        is_locked = item.state[1]
+
+        row = col.row(align=True)
+        row.label(text='%s - %s' % (key, ktype))
+
+        icon = 'RESTRICT_VIEW_OFF' if is_visible else 'RESTRICT_VIEW_ON'
+        prop = row.operator("mathvis.toggle_display", text='', icon=icon, emboss=False)
+        prop.key = key
+
+        icon = 'LOCKED' if is_locked else 'UNLOCKED'
+        prop = row.operator("mathvis.toggle_lock", text='', icon=icon, emboss=False)
+        prop.key = key
+
+        if is_locked:
+            row.label(text='', icon='BLANK1')
+        else:
+            prop = row.operator("mathvis.delete_var", text='', icon='X', emboss=False)
+            prop.key = key
+
+
+class MathVis(bpy.types.PropertyGroup):
+
+    index = IntProperty(name="index")
 
     bbox_hide = BoolProperty(name="Hide BBoxes",
                              default=False,
@@ -199,20 +224,18 @@ def register():
     import console_python
     console_python.execute.hooks.append((console_hook, ()))
     bpy.utils.register_module(__name__)
-    bpy.types.Scene.MathVisProp = PointerProperty(type=MathVisProp)
-    bpy.types.WindowManager.MathVisStateProp = CollectionProperty(type=MathVisStateProp)
+    bpy.types.WindowManager.MathVisProp = PointerProperty(type=MathVis)
+    bpy.types.WindowManager.MathVisStatePropList = CollectionProperty( type=MathVisStateProp)
     bpy.types.CONSOLE_MT_console.prepend(menu_func_cleanup)
 
+
 def unregister():
-    context = bpy.context
-    var_states = get_var_states()
-    var_states.store_states()
     draw.callback_disable()
 
     import console_python
     console_python.execute.hooks.remove((console_hook, ()))
     bpy.types.CONSOLE_MT_console.remove(menu_func_cleanup)
-    del bpy.types.Scene.MathVisProp
-    del bpy.types.WindowManager.MathVisStateProp
+    del bpy.types.WindowManager.MathVisProp
+    del bpy.types.WindowManager.MathVisStatePropList
 
     bpy.utils.unregister_module(__name__)
diff --git a/space_view3d_math_vis/draw.py b/space_view3d_math_vis/draw.py
index 1890c3e..fa18551 100644
--- a/space_view3d_math_vis/draw.py
+++ b/space_view3d_math_vis/draw.py
@@ -69,7 +69,7 @@ def draw_callback_px():
 
     data_matrix, data_quat, data_euler, data_vector, data_vector_array = utils.console_math_data()
 
-    name_hide = context.scene.MathVisProp.name_hide
+    name_hide = context.window_manager.MathVisProp.name_hide
 
     if name_hide:
         return
@@ -156,8 +156,8 @@ def draw_callback_view():
     data_matrix, data_quat, data_euler, data_vector, data_vector_array = utils.console_math_data()
 
     # draw_matrix modifiers
-    bbox_hide = context.scene.MathVisProp.bbox_hide
-    bbox_scale = context.scene.MathVisProp.bbox_scale
+    bbox_hide = context.window_manager.MathVisProp.bbox_hide
+    bbox_scale = context.window_manager.MathVisProp.bbox_scale
 
     # draw_matrix vars
     zero = Vector((0.0, 0.0, 0.0))
diff --git a/space_view3d_math_vis/utils.py b/space_view3d_math_vis/utils.py
index 12c1c62..b243428 100644
--- a/space_view3d_math_vis/utils.py
+++ b/space_view3d_math_vis/utils.py
@@ -43,78 +43,48 @@ def is_display_list(listvar):
 
 class VarStates:
 
-    states = {}
-
-    def store_states(self):
+    @staticmethod
+    def store_states():
         # Store the display states, called upon unregister the Addon
         # This is useful when you press F8 to reload the Addons.
         # Then this function preserves the display states of the
         # console variables.
-        context = bpy.context
-        if len(self.states) > 0:
-            state_props = context.window_manager.MathVisStateProp
-            state_props.clear()
-            for key, state in self.states.items():
-                if key:
-                    state_prop = state_props.add()
-                    state_prop.key = key
-                    state_prop.state = state
-
-    def __init__(self):
-        # Get the display state from the stored values (if exists)
-        # This happens after you pressed F8 to reload the Addons.
-        context = bpy.context
-        if 'MathVisStateProp' in dir(bpy.types.WindowManager):
-            state_props = context.window_manager.MathVisStateProp
-            if state_props:
-                for state_prop in state_props:
-                    key = state_prop.key
-                    state = state_prop.state
-                    self.states[key] = [state[0], state[1]]
-                state_props.clear()
-
-    def get(self, key, default):
-        return self.states.get(key, default)
-
-    def delete(self, key):
-        if key in self.states:
-            del self.states[key]
-
-    def is_visible(self, key):
-        if key in self.states:
-            disp, lock = self.states[key]
-            return disp
-        return True
-
-    def toggle_display_state(self, key):
-        if key in self.states:
-            disp, lock = self.states[key]
-            self.states[key] = [not disp, lock]
-        else:
-            self.states[key] = [False, False]
-
-    def is_locked(self, key):
-        if key in self.states:
-            disp, lock = self

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-extensions-cvs mailing list