forked from pitiwazou/Scripts-Blender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddon_keymap_template
176 lines (132 loc) · 5.49 KB
/
addon_keymap_template
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
166
167
168
169
170
171
172
173
174
175
176
'''
Copyright (C) 2017 pitiwazou, kilbee
Created by pitiwazou, kilbee
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, see <http://www.gnu.org/licenses/>.
'''
bl_info = {
"name": "Addon Keymap Template",
"description": "",
"author": "pitiwazou, kilbee",
"version": (0, 0, 1),
"blender": (2, 78, 0),
"location": "View3D",
"warning": "This addon is still in development.",
"wiki_url": "",
"category": "Object" }
import bpy
from bpy.types import Menu
import rna_keymap_ui
# -----------------------------------------------------------------------------
# UI - Pie menu
# -----------------------------------------------------------------------------
#Pie Menu
class Test_Pie_Menu(Menu):
bl_idname = "pie.test_pie_menu"
bl_label = "Test Pie Menu"
@classmethod
def poll(cls, context):
return context.object is not None and context.selected_objects
def draw(self, context):
layout = self.layout
pie = layout.menu_pie()
#4 - LEFT
pie.operator_enum("mesh.select_mode", "type")
#6 - RIGHT
#2 - BOTTOM
#8 - TOP
#7 - TOP - LEFT
#9 - TOP - RIGHT
#1 - BOTTOM - LEFT
#3 - BOTTOM - RIGHT
# -----------------------------------------------------------------------------
# Preferences
# -----------------------------------------------------------------------------
# Preferences
class AddonPreferences(bpy.types.AddonPreferences):
bl_idname = __name__
def draw(self, context):
layout = self.layout
wm = bpy.context.window_manager
box=layout.box()
split = box.split()
col = split.column()
col.label('Setup Pie menu Hotkey')
col.separator()
wm = bpy.context.window_manager
kc = wm.keyconfigs.user
km = kc.keymaps['3D View Generic']
kmi = get_hotkey_entry_item(km, 'wm.call_menu_pie', 'pie.test_pie_menu')
if kmi:
col.context_pointer_set("keymap", km)
rna_keymap_ui.draw_kmi([], kc, km, kmi, col, 0)
else:
col.label("No hotkey entry found")
col.operator(Template_Add_Hotkey.bl_idname, text = "Add hotkey entry", icon = 'ZOOMIN')
# -----------------------------------------------------------------------------
# Keymap
# -----------------------------------------------------------------------------
addon_keymaps = []
def get_addon_preferences():
''' quick wrapper for referencing addon preferences '''
addon_preferences = bpy.context.user_preferences.addons[__name__].preferences
return addon_preferences
def get_hotkey_entry_item(km, kmi_name, kmi_value):
'''
returns hotkey of specific type, with specific properties.name (keymap is not a dict, so referencing by keys is not enough
if there are multiple hotkeys!)
'''
for i, km_item in enumerate(km.keymap_items):
if km.keymap_items.keys()[i] == kmi_name:
if km.keymap_items[i].properties.name == kmi_value:
return km_item
return None
def add_hotkey():
user_preferences = bpy.context.user_preferences
addon_prefs = user_preferences.addons[__name__].preferences
wm = bpy.context.window_manager
kc = wm.keyconfigs.addon
km = kc.keymaps.new(name="3D View Generic", space_type='VIEW_3D', region_type='WINDOW')
kmi = km.keymap_items.new("wm.call_menu_pie",'RIGHTMOUSE', 'PRESS', shift=True, ctrl=True, alt=True)
kmi.properties.name = "pie.test_pie_menu"
kmi.active = True
addon_keymaps.append((km, kmi))
class Template_Add_Hotkey(bpy.types.Operator):
''' Add hotkey entry '''
bl_idname = "template.add_hotkey"
bl_label = "Addon Preferences Example"
bl_options = {'REGISTER', 'INTERNAL'}
def execute(self, context):
add_hotkey()
self.report({'INFO'}, "Hotkey added in User Preferences -> Input -> Screen -> Screen (Global)")
return {'FINISHED'}
def remove_hotkey():
''' clears all addon level keymap hotkeys stored in addon_keymaps '''
wm = bpy.context.window_manager
kc = wm.keyconfigs.addon
km = kc.keymaps['3D View Generic']
for km, kmi in addon_keymaps:
km.keymap_items.remove(kmi)
wm.keyconfigs.addon.keymaps.remove(km)
addon_keymaps.clear()
# -----------------------------------------------------------------------------
# Register
# -----------------------------------------------------------------------------
def register():
bpy.utils.register_module(__name__)
# hotkey setup
add_hotkey()
def unregister():
bpy.utils.unregister_module(__name__)
# hotkey cleanup
remove_hotkey()
if __name__ == "__main__":
register()