-
Notifications
You must be signed in to change notification settings - Fork 1
/
__init__.py
165 lines (129 loc) · 5.71 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
bl_info = {
"name": "Toggle 'Emulate 3 Button Mouse' & 'Emulate Numpad'",
"author": "brybalicious courtesy Robert Guetzkow",
"version": (1, 1),
"blender": (3, 3, 1),
"location": "Edit > Operator Search",
"description": "Operator for toggling the 'Emulate 3 Button Mouse' & 'Emulate Numpad' options",
"warning": "",
"wiki_url": "https://github.com/brybalicious/toggle_mmb_numpad/wiki",
"category": "Preferences"}
import bpy
import rna_keymap_ui
from bpy.utils import register_class, unregister_class
# Surfacing the Operators
# They can now be looked up via their bl_label attribute with the search function on default hotkey F3
# Helper method to let the user know what state the emulation is being toggled to
def message_user(name, isTrue):
message = ("%s: " %(name.bl_label))
if isTrue:
message += "On"
else:
message += "Off"
name.report({'INFO'}, message)
class PREFERENCES_OT_toggle_emulate_3_button_mouse(bpy.types.Operator):
bl_idname = "preferences.toggle_emulate_3_button_mouse"
bl_label = "Toggle 'Emulate 3 Button Mouse'"
def execute(self, context):
context.preferences.inputs.use_mouse_emulate_3_button = not context.preferences.inputs.use_mouse_emulate_3_button
message_user(self, context.preferences.inputs.use_mouse_emulate_3_button)
return {"FINISHED"}
class PREFERENCES_OT_toggle_emulate_numpad(bpy.types.Operator):
bl_idname = "preferences.toggle_numpad"
bl_label = "Toggle 'Emulate Numpad'"
def execute(self, context):
bpy.context.preferences.inputs.use_emulate_numpad = not bpy.context.preferences.inputs.use_emulate_numpad
message_user(self, bpy.context.preferences.inputs.use_emulate_numpad)
return {"FINISHED"}
# Keymap Code
# First, set up a dictionary of key mappings.
# "TEMPLATE": [{"label": "Human readable name of keymap in Blender Keymap UI",
# "region_type": "WINDOW", //Default is WINDOW (optional)
# "space_type", "EMPTY", //Default is EMPTY
# "map_type": "KEYBOARD", //Device to be used (optional)
# "keymap": "Window", //Name to be chosen from declared list of keymap names
# "idname": ..., //Specific reference to operator from blender API
# "type": "", //Key type identifier - choose a specific keyboard button or mouse button, etc...
# "ctrl": False, //Combination keys for compound hotkeys (optional)
# ...
# "value": "PRESS"}, //Whether key will be held, pressed, etc...
# {...}]
keys = {"MENU": [{"label": "Toggle Emulate 3 Button Mouse",
"region_type": "WINDOW",
"space_type": "EMPTY",
"map_type": "KEYBOARD",
"keymap": "Window",
"idname": "preferences.toggle_emulate_3_button_mouse",
"type": "QUOTE",
"ctrl": False,
"alt": False,
"shift": False,
"oskey": False,
"value": "PRESS"
},
{"label": "Toggle Emulate Numpad",
"region_type": "WINDOW",
"space_type": "EMPTY",
"map_type": "KEYBOARD",
"keymap": "Window",
"idname": "preferences.toggle_numpad",
"type": "SEMI_COLON",
"ctrl": False,
"alt": False,
"shift": False,
"oskey": False,
"value": "PRESS"
}]}
# Define a function to get a list of key mappings, 'keylists'
def get_keys():
keylists = []
keylists.append(keys["MENU"])
return keylists
# Define a function to register the key mappings templated in the dict as keymaps, using the constructor kc.keymaps.new()
def register_keymaps(keylists):
wm = bpy.context.window_manager
kc = wm.keyconfigs.addon
keymaps = []
for keylist in keylists:
for item in keylist:
keymap = item.get("keymap")
space_type = item.get("space_type", "EMPTY")
region_type = item.get("region_type", "WINDOW")
if keymap:
km = kc.keymaps.new(name=keymap, space_type=space_type, region_type=region_type)
# km = kc.keymaps.new(name=keymap, space_type=space_type)
if km:
idname = item.get("idname")
type = item.get("type")
value = item.get("value")
shift = item.get("shift", False)
ctrl = item.get("ctrl", False)
alt = item.get("alt", False)
oskey = item.get("oskey", False)
kmi = km.keymap_items.new(idname, type, value, shift=shift, ctrl=ctrl, alt=alt, oskey=oskey)
if kmi:
properties = item.get("properties")
if properties:
for name, value in properties:
setattr(kmi.properties, name, value)
keymaps.append((km, kmi))
return keymaps
# Define a function to unregister the keymaps
def unregister_keymaps(keymaps):
for km, kmi in keymaps:
km.keymap_items.remove(kmi)
classes = (PREFERENCES_OT_toggle_emulate_3_button_mouse, PREFERENCES_OT_toggle_emulate_numpad)
def register():
for cls in classes:
register_class(cls)
global keymaps
keys = get_keys()
keymaps = register_keymaps(keys)
def unregister():
global keymaps
for km, kmi in keymaps:
km.keymap_items.remove(kmi)
for cls in classes:
unregister_class(cls)
# if __name__ == "__main__":
# register()