-
Notifications
You must be signed in to change notification settings - Fork 1
/
kbPIEs.py
318 lines (228 loc) · 12 KB
/
kbPIEs.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
bl_info = {
"name": "kbPIEs",
"author": "kilbeeu",
"version": (0, 3),
"blender": (2, 78, 0),
"description": "Adds a PIE Menu for switching Screen Layouts",
"warning": "",
"wiki_url": "http://github.com/kilbee/kbPIEs",
"tracker_url":"https://gitreports.com/issue/kilbee/kbPIEs",
"category": "Pie Menu",
}
import bpy
import rna_keymap_ui
def avail_screens(self,context):
'''
enumerates available screen layouts and adding more items:
Maximize Area - will toggle current area to maximum window size
User Preferences - opens User Preferences window
'''
screens = [ ('Maximize Area', 'Maximize Area', 'Maximizes current area'),
('User Preferences', 'User Preferences', 'User Preferences')
] # (identifier, name, description) optionally: (.., icon name, unique number)
for i, screen in enumerate(bpy.data.screens):
screens.append((screen.name, screen.name, screen.name))
return screens
addon_keymaps = [] # store hotkey items on addon level for quick referencel todo: actually move this to addon preferences to keep it clean
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 # not needed, since no return means None, but keeping for readability
def add_hotkey():
# reference: http://blender.stackexchange.com/a/1498/1610
user_preferences = bpy.context.user_preferences
addon_prefs = user_preferences.addons[__name__].preferences
wm = bpy.context.window_manager
#kc = wm.keyconfigs.user # for adding hotkeys independent from addon
#km = kc.keymaps['Screen']
kc = wm.keyconfigs.addon # for hotkeys within an addon
km = kc.keymaps.new(name="Window", space_type='EMPTY', region_type='WINDOW') # this will get appended to hotkeys screen section in input hotkeys UI
#kmi = get_hotkey_entry_item(km, 'wm.call_menu_pie', 'KbPiesSwitchLayout') # query hotkeys for hotkey entry item with specific name...
#if kmi == None: # if want to add only one
kmi = km.keymap_items.new("wm.call_menu_pie", "NONE", "PRESS") # ...and if not found then add it
kmi.properties.name = "KbPiesSwitchLayout" # also set proper name
kmi.active = True
addon_keymaps.append((km, kmi)) # also append to global (addon level) hotkey list for easy management
def remove_hotkey():
''' clears all addon level keymap hotkeys stored in addon_keymaps '''
wm = bpy.context.window_manager
#kc = wm.keyconfigs.user
kc = wm.keyconfigs.addon
km = kc.keymaps['Window']
for km, kmi in addon_keymaps:
km.keymap_items.remove(kmi)
wm.keyconfigs.addon.keymaps.remove(km)
addon_keymaps.clear()
def get_addon_preferences():
''' quick wrapper for referencing addon preferences '''
addon_preferences = bpy.context.user_preferences.addons[__name__].preferences
return addon_preferences
class KbPiesAddonPreferences(bpy.types.AddonPreferences):
bl_idname = __name__
kb_pie_screens_top_left = bpy.props.StringProperty( name="Top Left", default="Maximize Area" )
kb_pie_screens_top = bpy.props.StringProperty( name="Top", default="Maximize Area" )
kb_pie_screens_top_right = bpy.props.StringProperty( name="Top Right", default="Maximize Area" )
kb_pie_screens_left = bpy.props.StringProperty( name="Left", default="Maximize Area" )
kb_pie_screens_right = bpy.props.StringProperty( name="Right", default="Maximize Area" )
kb_pie_screens_bottom_left = bpy.props.StringProperty( name="Bottom Left", default="Maximize Area" )
kb_pie_screens_bottom = bpy.props.StringProperty( name="Bottom", default="Maximize Area" )
kb_pie_screens_bottom_right = bpy.props.StringProperty( name="Bottom Right", default="Maximize Area" )
def draw(self, context):
layout = self.layout
box = layout.box()
col = box.column()
row = col.row()
row.label()
#row.label()
row.operator_menu_enum('kbpies.select_screen', 'top', text='Top ({})'.format(self.kb_pie_screens_top))
#row.label()
row.label()
row = col.row()
row.label()
row.operator_menu_enum('kbpies.select_screen', 'top_left', text='Top Left ({})'.format(self.kb_pie_screens_top_left))
row.label()
row.operator_menu_enum('kbpies.select_screen', 'top_right', text='Top Right ({})'.format(self.kb_pie_screens_top_right))
row.label()
row = col.row()
row.operator_menu_enum('kbpies.select_screen', 'left', text='Left ({})'.format(self.kb_pie_screens_left))
row.label()
row.label()
row.operator_menu_enum('kbpies.select_screen', 'right', text='Right ({})'.format(self.kb_pie_screens_right))
row = col.row()
row.label()
row.operator_menu_enum('kbpies.select_screen', 'bottom_left', text='Bottom left ({})'.format(self.kb_pie_screens_bottom_left))
row.label()
row.operator_menu_enum('kbpies.select_screen', 'bottom_right', text='Bottom Right ({})'.format(self.kb_pie_screens_bottom_right))
row.label()
row = col.row()
row.label()
#row.label()
row.operator_menu_enum('kbpies.select_screen', 'bottom', text='Bottom ({})'.format(self.kb_pie_screens_bottom))
#row.label()
row.label()
layout.separator()
# hotkey section
box = layout.box()
col = box.column()
col.label('Setup Hotkey')
col.separator()
wm = bpy.context.window_manager
kc = wm.keyconfigs.user #need to reference the actual keyconfig, ( referencing kc = wm.keyconfigs.addon won't be saved across sessions)
km = kc.keymaps['Window']
kmi = get_hotkey_entry_item(km, 'wm.call_menu_pie', 'KbPiesSwitchLayout')
if kmi:
col.context_pointer_set("keymap", km)
rna_keymap_ui.draw_kmi([], kc, km, kmi, col, 0)
col.separator()
col.label(text="Hotkey also listed in User Preferences -> Input -> Window")
else:
col.label("No hotkey entry found")
col.operator(KbPiesAddHotkey.bl_idname, text = "Add hotkey entry", icon = 'ZOOMIN')
#layout.separator()
#layout.operator("wm.url_open", text="homepage & more info").url = "http://kilbeeu.wordpress.com"
def execute(self,context):
self.bl_label = self.select_screen
print(self.select_screen)
return {'FINISHED'}
def test(self, context=None):
print("ssss")
class KbPiesSelectScreen(bpy.types.Operator):
''' Set screen layout to corresponding pie position '''
bl_idname = "kbpies.select_screen"
bl_label = "Select screen"
num = bpy.props.IntProperty()
top_left = bpy.props.EnumProperty( name = "Top Left", items = avail_screens, update=test)
left = bpy.props.EnumProperty( name = "Left", items = avail_screens)
bottom_left = bpy.props.EnumProperty( name = "Bottom Left", items = avail_screens)
top = bpy.props.EnumProperty( name = "Top", items = avail_screens)
bottom = bpy.props.EnumProperty( name = "Bottom", items = avail_screens)
top_right = bpy.props.EnumProperty( name = "Top Right", items = avail_screens)
right = bpy.props.EnumProperty( name = "Right", items = avail_screens)
bottom_right= bpy.props.EnumProperty( name = "Bottom Right",items = avail_screens)
def execute(self,context):
addon_preferences = get_addon_preferences()
addon_preferences.kb_pie_screens_top_left = self.top_left
addon_preferences.kb_pie_screens_left = self.left
addon_preferences.kb_pie_screens_bottom_left = self.bottom_left
addon_preferences.kb_pie_screens_top = self.top
addon_preferences.kb_pie_screens_bottom = self.bottom
addon_preferences.kb_pie_screens_top_right = self.top_right
addon_preferences.kb_pie_screens_right = self.right
addon_preferences.kb_pie_screens_bottom_right = self.bottom_right
return {'FINISHED'}
class KbPiesSwitchLayout(bpy.types.Menu):
bl_label = "Screen Layout" # label is displayed at the center of the pie menu.
def draw(self, context):
addon_preferences = get_addon_preferences()
bottom_left = addon_preferences.kb_pie_screens_bottom_left
bottom = addon_preferences.kb_pie_screens_bottom
bottom_right = addon_preferences.kb_pie_screens_bottom_right
left = addon_preferences.kb_pie_screens_left
right = addon_preferences.kb_pie_screens_right
top_left = addon_preferences.kb_pie_screens_top_left
top = addon_preferences.kb_pie_screens_top
top_right = addon_preferences.kb_pie_screens_top_right
pie = self.layout.menu_pie()
# Basic 4 direction PIE
pie.operator("screen.set_layout", left).layoutName = left
pie.operator("screen.set_layout", right).layoutName = right
pie.operator("screen.set_layout", bottom).layoutName = bottom
pie.operator("screen.set_layout", top).layoutName = top
# Additional 4 directions PIE
pie.operator("screen.set_layout", top_left).layoutName = top_left
pie.operator("screen.set_layout", top_right).layoutName = top_right
pie.operator("screen.set_layout", bottom_left).layoutName = bottom_left
pie.operator("screen.set_layout", bottom_right).layoutName = bottom_right
class KbPiesSetScreenLayout(bpy.types.Operator):
''' Change screen layout '''
bl_idname="screen.set_layout"
bl_label="Switch to Screen Layout"
layoutName=bpy.props.StringProperty()
def execute(self,context):
if self.layoutName == "Maximize Area": # if just want to MAXIMIZE area then do so:
bpy.ops.screen.screen_full_area()
return{'FINISHED'}
elif self.layoutName == "User Preferences":
bpy.ops.screen.userpref_show('INVOKE_DEFAULT')
#bpy.context.user_preferences.active_section = 'ADDONS'
return{'FINISHED'}
else:
try:
if bpy.context.window.screen.show_fullscreen:
bpy.ops.screen.back_to_previous() # if area is maximized back to previous layout before switching it (avoids some crashes)
bpy.context.window.screen=bpy.data.screens[self.layoutName] # try to switch layout
except:
# except layout doesn't exists
self.report({'INFO'}, 'Screen layout [{}] doesn\'t exist! Create it or pick another in addon settings.'.format(self.layoutName))
return{'FINISHED'}
class KbPiesAddHotkey(bpy.types.Operator):
''' Add hotkey entry '''
bl_idname = "kbpies.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 register():
bpy.utils.register_class(KbPiesSelectScreen)
bpy.utils.register_class(KbPiesSwitchLayout)
bpy.utils.register_class(KbPiesSetScreenLayout)
bpy.utils.register_class(KbPiesAddonPreferences)
bpy.utils.register_class(KbPiesAddHotkey)
# hotkey setup
add_hotkey()
def unregister():
bpy.utils.unregister_class(KbPiesSelectScreen)
bpy.utils.unregister_class(KbPiesSwitchLayout)
bpy.utils.unregister_class(KbPiesSetScreenLayout)
bpy.utils.unregister_class(KbPiesAddonPreferences)
bpy.utils.unregister_class(KbPiesAddHotkey)
# hotkey cleanup
remove_hotkey()