-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
__init__.py
145 lines (113 loc) · 4.88 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# ##### BEGIN GPL LICENSE BLOCK #####
#
# <An addon to blend Gizmo and Modal operations more seamlessly>
# Copyright (C) <2022> <Mat Brady>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
import bpy
from bpy.types import (
KeyMapItem
)
from . import (
operators,
panels,
prefs
)
bl_info = {
"name": "Gizmodal Ops",
"author": "Mat Brady, Blender Defender",
"version": (1, 0, 1),
"blender": (2, 83, 0),
"location": "Sidebar > View Tab",
"description": "An add-on that seamlessly blends Gizmo and Modal operations.",
"warning": "",
"doc_url": "https://github.com/BlenderDefender/Gizmodal-Ops#gizmodal-ops",
"tracker_url": "https://github.com/BlenderDefender/Gizmodal-Ops/issues",
"endpoint_url": "https://raw.githubusercontent.com/BlenderDefender/BlenderDefender/updater_endpoints/GIZMODALOPS.json",
"category": "3D View"
}
modules = (
operators,
panels,
prefs
)
def register_keymap(*args):
wm = bpy.context.window_manager # Get the window manager context
# ! The 3D View keymap is not needed in Blender 4.0 and newer.
# ! It is there, to work with legacy Blender versions (3.x and older)
keymaps = ["3D View", "Pose", "Object Mode", "Curve", "Curves",
"Mesh", "Armature", "Metaball", "Lattice", "UV Editor"]
for map in keymaps:
# Try to get the active keymap for the current map name.
active_km = wm.keyconfigs.active.keymaps.get(map, None)
if not active_km:
print(f"Gizmodal Ops Register - Warning: Key {map} not found.")
continue
find_km_item = active_km.keymap_items.find_from_operator
# Iterate over all keymap items defined in operators.
for idname, operator in operators.keymap:
# Get the Keymap Item from the keymap by searching for the original Operator.
kmi: KeyMapItem = find_km_item(idname, include={"KEYBOARD"})
# If the Keymap Item is not None, rewrite the Operator to the corresponding Gizmodal Ops Operator.
while kmi:
kmi.idname = operator.bl_idname
# Repeat, until every Keymap Item is rewritten
kmi = find_km_item(idname, include={"KEYBOARD"})
def unregister_keymap(*args):
wm = bpy.context.window_manager # Get the window manager context
keymaps = ["3D View", "UV Editor"]
for map in keymaps:
# Try to get the active keymap for the current map name.
active_km = wm.keyconfigs.active.keymaps.get(map, None)
if not active_km:
print(f"Gizmodal Ops Unregister - Warning: Key {map} not found.")
continue
find_km_item = active_km.keymap_items.find_from_operator
# Iterate over all keymap items defined in operators.
for idname, operator in operators.keymap:
# Get the Keymap Item from the keymap by searching for the Gizmodal Ops Operator.
kmi: KeyMapItem = find_km_item(
operator.bl_idname, include={"KEYBOARD"})
# If the Keymap Item is not None, rewrite the Operator to the original Operator.
while kmi:
kmi.idname = idname
# Repeat, until every Keymap Item is rewritten
kmi = find_km_item(operator.bl_idname, include={"KEYBOARD"})
def register():
# Register all modules.
for mod in modules:
mod.register()
# Since Blender doesn't load the keymaps before registering,
# try to register the keymap 0.1 seconds after Blender loaded properly.
bpy.app.timers.register(
register_keymap, first_interval=0.1, persistent=True)
def unregister():
# Unregister all modules.
for mod in modules:
mod.unregister()
# Try to unregister the "Register Keymap" timer, in case it still is registered.
try:
bpy.app.timers.unregister(register_keymap)
except Exception as e:
error_message = str(e).lower().replace('error', 'Warning')
print(
f"Gizmodal Ops Unregister - {error_message}")
# Unregister the keymap, so no issues occur after uninstalling Gizmodal Ops.
unregister_keymap()
# Allow running the script inside of Blenders text editor.
if __name__ == "__main__":
register()