From 9b1a36fd57102201c9371f36a3d39637eaff16b5 Mon Sep 17 00:00:00 2001 From: SpectralVectors Date: Thu, 1 Jul 2021 15:34:09 -0400 Subject: [PATCH] Resolved "Disable/Reenable bug" on opening a blend Reported by Ares, this one was a bit of a bear, mostly because I was looking in the wrong spot. I tried importing time to set a delay before registration, tried using threading to monitor the keymap registration, tried enabling "Autorun Python Scripts", and nothing worked. The addon worked when Blender was opened first, but not when a .blend file was clicked before the program was open. Solved by iceythe Kaio on Blender Artists: https://blenderartists.org/t/why-wont-this-addon-retain-keymap-user-preferences/1164306 When you store keymaps in addon keymaps they are dumped between blender sessions, but when you store them in user keymaps, they remain between sessions, and, thus, don't need to be manually disabled and re-enabled every time you double click a blend file. --- RightMouseNavigation.py | 2 +- __init__.py | 111 ++++++++++++++++++++-------------------- 2 files changed, 57 insertions(+), 56 deletions(-) diff --git a/RightMouseNavigation.py b/RightMouseNavigation.py index c3dc756..d0be034 100644 --- a/RightMouseNavigation.py +++ b/RightMouseNavigation.py @@ -4,7 +4,7 @@ 'name': 'Right Mouse Navigation', 'category': 'View 3D', 'author': 'Spectral Vectors', - 'version': (0, 1, 4), + 'version': (0, 1, 5), 'blender': (2, 90, 0), 'location': '3D Viewport', "description": "Enables Right Mouse Viewport Navigation" diff --git a/__init__.py b/__init__.py index 412ab54..8f90814 100644 --- a/__init__.py +++ b/__init__.py @@ -2,7 +2,7 @@ 'name': 'Right Mouse Navigation', 'category': 'View 3D', 'author': 'Spectral Vectors', - 'version': (0, 1, 4), + 'version': (0, 1, 5), 'blender': (2, 90, 0), 'location': '3D Viewport', "description": "Enables Right Mouse Viewport Navigation" @@ -16,12 +16,13 @@ addon_keymaps = [] -def register(): - bpy.utils.register_class(RightMouseNavigationPreferences) - bpy.utils.register_class(BLUI_OT_right_mouse_navigation) +def register(): + if not bpy.app.background: + bpy.utils.register_class(RightMouseNavigationPreferences) + bpy.utils.register_class(BLUI_OT_right_mouse_navigation) - register_keymaps() + register_keymaps() def register_keymaps(): keyconfig = bpy.context.window_manager.keyconfigs @@ -33,7 +34,7 @@ def register_keymaps(): else: wm = bpy.context.window_manager - kc = wm.keyconfigs.addon + kc = wm.keyconfigs.user km = kc.keymaps.new( name="3D View", @@ -94,56 +95,56 @@ def register_keymaps(): def unregister(): - - bpy.utils.unregister_class(BLUI_OT_right_mouse_navigation) - bpy.utils.unregister_class(RightMouseNavigationPreferences) - - wm = bpy.context.window_manager - kc = wm.keyconfigs.addon - - for km, kmi in addon_keymaps: - km.keymap_items.remove(kmi) - wm.keyconfigs.addon.keymaps.remove(km) - addon_keymaps.clear() - - menumodes = ["Object Mode", "Mesh", "Curve", "Armature", "Metaball", "Lattice", "Font", "Pose"] - panelmodes = ["Vertex Paint", "Weight Paint", "Image Paint", "Sculpt"] - - # Reactivating menus - # "Object Mode", "Mesh", "Curve", "Armature", "Metaball", "Lattice", - # "Font", "Pose" - for i in menumodes: - for key in wm.keyconfigs.user.keymaps[i].keymap_items: - if ( - key.idname == "wm.call_menu" - and key.type == "RIGHTMOUSE" - ): - key.active = True + if not bpy.app.background: + bpy.utils.unregister_class(BLUI_OT_right_mouse_navigation) + bpy.utils.unregister_class(RightMouseNavigationPreferences) - # Reactivating panels - # "Vertex Paint", "Weight Paint", "Image Paint", "Sculpt" - for i in panelmodes: - for key in wm.keyconfigs.user.keymaps[i].keymap_items: - if ( - key.idname == "wm.call_panel" - and key.type == "RIGHTMOUSE" - ): - key.active = True - - # Changing the Walk Modal Map back - for key in wm.keyconfigs.user.keymaps["View3D Walk Modal"].keymap_items: - if ( - key.propvalue == "CANCEL" - and key.type == "RIGHTMOUSE" - ): - key.active = True - for key in wm.keyconfigs.user.keymaps["View3D Walk Modal"].keymap_items: - if ( - key.propvalue == "CONFIRM" - and key.type == "RIGHTMOUSE" - ): - key.type = "LEFTMOUSE" - key.value = "PRESS" + wm = bpy.context.window_manager + kc = wm.keyconfigs.user + + for km, kmi in addon_keymaps: + km.keymap_items.remove(kmi) + wm.keyconfigs.user.keymaps.remove(km) + addon_keymaps.clear() + + menumodes = ["Object Mode", "Mesh", "Curve", "Armature", "Metaball", "Lattice", "Font", "Pose"] + panelmodes = ["Vertex Paint", "Weight Paint", "Image Paint", "Sculpt"] + + # Reactivating menus + # "Object Mode", "Mesh", "Curve", "Armature", "Metaball", "Lattice", + # "Font", "Pose" + for i in menumodes: + for key in wm.keyconfigs.user.keymaps[i].keymap_items: + if ( + key.idname == "wm.call_menu" + and key.type == "RIGHTMOUSE" + ): + key.active = True + + # Reactivating panels + # "Vertex Paint", "Weight Paint", "Image Paint", "Sculpt" + for i in panelmodes: + for key in wm.keyconfigs.user.keymaps[i].keymap_items: + if ( + key.idname == "wm.call_panel" + and key.type == "RIGHTMOUSE" + ): + key.active = True + + # Changing the Walk Modal Map back + for key in wm.keyconfigs.user.keymaps["View3D Walk Modal"].keymap_items: + if ( + key.propvalue == "CANCEL" + and key.type == "RIGHTMOUSE" + ): + key.active = True + for key in wm.keyconfigs.user.keymaps["View3D Walk Modal"].keymap_items: + if ( + key.propvalue == "CONFIRM" + and key.type == "RIGHTMOUSE" + ): + key.type = "LEFTMOUSE" + key.value = "PRESS" if __name__ == "__package__": register() \ No newline at end of file