Skip to content

Commit

Permalink
Resolved "Disable/Reenable bug" on opening a blend
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
SpectralVectors authored Jul 1, 2021
1 parent 04cdde2 commit 9b1a36f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 56 deletions.
2 changes: 1 addition & 1 deletion RightMouseNavigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
111 changes: 56 additions & 55 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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",
Expand Down Expand Up @@ -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()

0 comments on commit 9b1a36f

Please sign in to comment.