AnimAll: rename the "Animate" tab back to "Animation"
[blender-addons.git] / object_collection_manager / qcd_init.py
blobf58f8e030d6f934add9a60ec1c64d93618120ad7
1 # SPDX-FileCopyrightText: 2011 Ryan Inch
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 if "bpy" in locals():
6 import importlib
8 importlib.reload(internals)
9 importlib.reload(qcd_move_widget)
10 importlib.reload(qcd_operators)
11 importlib.reload(ui)
12 importlib.reload(preferences)
14 else:
15 from . import internals
16 from . import qcd_move_widget
17 from . import qcd_operators
18 from . import ui
19 from . import preferences
21 import os
22 import bpy
23 import bpy.utils.previews
24 from bpy.app.handlers import persistent
27 addon_qcd_keymaps = []
28 addon_qcd_view_hotkey_keymaps = []
29 addon_qcd_view_edit_mode_hotkey_keymaps = []
30 qcd_registered = False
33 qcd_classes = (
34 qcd_move_widget.QCDMoveWidget,
35 qcd_operators.EnableAllQCDSlotsMeta,
36 qcd_operators.EnableAllQCDSlots,
37 qcd_operators.EnableAllQCDSlotsIsolated,
38 qcd_operators.IsolateSelectedObjectsCollections,
39 qcd_operators.DisableSelectedObjectsCollections,
40 qcd_operators.DisableAllNonQCDSlots,
41 qcd_operators.DisableAllCollections,
42 qcd_operators.SelectAllQCDObjects,
43 qcd_operators.DiscardQCDHistory,
44 qcd_operators.MoveToQCDSlot,
45 qcd_operators.ViewQCDSlot,
46 qcd_operators.ViewMoveQCDSlot,
47 qcd_operators.UnassignedQCDSlot,
48 qcd_operators.CreateAllQCDSlots,
49 qcd_operators.RenumerateQCDSlots,
50 ui.EnableAllQCDSlotsMenu,
54 @persistent
55 def save_internal_data(dummy):
56 cm = bpy.context.scene.collection_manager
58 cm.qcd_slots_blend_data = internals.qcd_slots.get_data_for_blend()
61 @persistent
62 def load_internal_data(dummy):
63 cm = bpy.context.scene.collection_manager
64 data = cm.qcd_slots_blend_data
66 if not data:
67 return
69 internals.qcd_slots.load_blend_data(data)
72 @persistent
73 def load_pre_handler(dummy):
74 internals.qcd_collection_state.clear()
75 for key in list(internals.qcd_history.keys()):
76 del internals.qcd_history[key]
79 def register_qcd():
80 global qcd_registered
81 if qcd_registered:
82 # guard to handle default value updates (mouse hover + backspace)
83 return
85 for cls in qcd_classes:
86 bpy.utils.register_class(cls)
88 pcoll = bpy.utils.previews.new()
89 icons_dir = os.path.join(os.path.dirname(__file__), "icons")
90 pcoll.load("active_icon_base", os.path.join(icons_dir, "minus.png"), 'IMAGE', True)
91 pcoll.load("active_icon_text", os.path.join(icons_dir, "minus.png"), 'IMAGE', True)
92 pcoll.load("active_icon_text_sel", os.path.join(icons_dir, "minus.png"), 'IMAGE', True)
93 ui.preview_collections["icons"] = pcoll
95 wm = bpy.context.window_manager
96 if wm.keyconfigs.addon: # not present when started with --background
97 km = wm.keyconfigs.addon.keymaps.new(name='Object Mode')
98 kmi = km.keymap_items.new('view3d.qcd_move_widget', 'V', 'PRESS')
99 addon_qcd_keymaps.append((km, kmi))
101 bpy.app.handlers.save_pre.append(save_internal_data)
102 bpy.app.handlers.load_post.append(load_internal_data)
103 bpy.app.handlers.load_pre.append(load_pre_handler)
105 prefs = bpy.context.preferences.addons[__package__].preferences
107 if prefs.enable_qcd_view_hotkeys:
108 register_qcd_view_hotkeys()
110 if prefs.enable_qcd_view_edit_mode_hotkeys:
111 register_qcd_view_edit_mode_hotkeys()
113 if prefs.enable_qcd_3dview_header_widget:
114 register_qcd_3dview_header_widget()
116 qcd_registered = True
119 def register_qcd_view_hotkeys():
120 if addon_qcd_view_hotkey_keymaps:
121 # guard to handle default value updates (mouse hover + backspace)
122 return
124 wm = bpy.context.window_manager
125 if wm.keyconfigs.addon: # not present when started with --background
126 # create qcd hotkeys
127 qcd_hotkeys = [
128 ["ONE", False, "1"],
129 ["TWO", False, "2"],
130 ["THREE", False, "3"],
131 ["FOUR", False, "4"],
132 ["FIVE", False, "5"],
133 ["SIX", False, "6"],
134 ["SEVEN", False, "7"],
135 ["EIGHT", False, "8"],
136 ["NINE", False, "9"],
137 ["ZERO", False, "10"],
138 ["ONE", True, "11"],
139 ["TWO", True, "12"],
140 ["THREE", True, "13"],
141 ["FOUR", True, "14"],
142 ["FIVE", True, "15"],
143 ["SIX", True, "16"],
144 ["SEVEN", True, "17"],
145 ["EIGHT", True, "18"],
146 ["NINE", True, "19"],
147 ["ZERO", True, "20"],
150 for mode in ['Object Mode', 'Pose', 'Weight Paint']:
151 for key in qcd_hotkeys:
152 km = wm.keyconfigs.addon.keymaps.new(name=mode)
153 kmi = km.keymap_items.new('view3d.view_qcd_slot', key[0], 'PRESS', alt=key[1])
154 kmi.properties.slot = key[2]
155 kmi.properties.toggle = False
156 addon_qcd_view_hotkey_keymaps.append((km, kmi))
158 km = wm.keyconfigs.addon.keymaps.new(name=mode)
159 kmi = km.keymap_items.new('view3d.view_qcd_slot', key[0], 'PRESS',shift=True, alt=key[1])
160 kmi.properties.slot = key[2]
161 kmi.properties.toggle = True
162 addon_qcd_view_hotkey_keymaps.append((km, kmi))
164 km = wm.keyconfigs.addon.keymaps.new(name=mode)
165 kmi = km.keymap_items.new('view3d.enable_all_qcd_slots', 'PLUS', 'PRESS', shift=True)
166 addon_qcd_view_hotkey_keymaps.append((km, kmi))
168 km = wm.keyconfigs.addon.keymaps.new(name=mode)
169 kmi = km.keymap_items.new('view3d.isolate_selected_objects_collections', 'EQUAL', 'PRESS')
170 addon_qcd_view_hotkey_keymaps.append((km, kmi))
172 km = wm.keyconfigs.addon.keymaps.new(name=mode)
173 kmi = km.keymap_items.new('view3d.disable_selected_objects_collections', 'MINUS', 'PRESS')
174 addon_qcd_view_hotkey_keymaps.append((km, kmi))
176 km = wm.keyconfigs.addon.keymaps.new(name=mode)
177 kmi = km.keymap_items.new('view3d.disable_all_non_qcd_slots', 'PLUS', 'PRESS', shift=True, alt=True)
178 addon_qcd_view_hotkey_keymaps.append((km, kmi))
180 km = wm.keyconfigs.addon.keymaps.new(name=mode)
181 kmi = km.keymap_items.new('view3d.disable_all_collections', 'EQUAL', 'PRESS', alt=True, ctrl=True)
182 addon_qcd_view_hotkey_keymaps.append((km, kmi))
184 km = wm.keyconfigs.addon.keymaps.new(name=mode)
185 kmi = km.keymap_items.new('view3d.select_all_qcd_objects', 'PLUS', 'PRESS', shift=True, ctrl=True)
186 addon_qcd_view_hotkey_keymaps.append((km, kmi))
189 km = wm.keyconfigs.addon.keymaps.new(name=mode)
190 kmi = km.keymap_items.new('view3d.discard_qcd_history', 'EQUAL', 'PRESS', alt=True)
191 addon_qcd_view_hotkey_keymaps.append((km, kmi))
194 def register_qcd_view_edit_mode_hotkeys():
195 if addon_qcd_view_edit_mode_hotkey_keymaps:
196 # guard to handle default value updates (mouse hover + backspace)
197 return
199 wm = bpy.context.window_manager
200 if wm.keyconfigs.addon: # not present when started with --background
201 # create qcd hotkeys
202 qcd_hotkeys = [
203 ["ONE", False, "1"],
204 ["TWO", False, "2"],
205 ["THREE", False, "3"],
206 ["FOUR", False, "4"],
207 ["FIVE", False, "5"],
208 ["SIX", False, "6"],
209 ["SEVEN", False, "7"],
210 ["EIGHT", False, "8"],
211 ["NINE", False, "9"],
212 ["ZERO", False, "10"],
213 ["ONE", True, "11"],
214 ["TWO", True, "12"],
215 ["THREE", True, "13"],
216 ["FOUR", True, "14"],
217 ["FIVE", True, "15"],
218 ["SIX", True, "16"],
219 ["SEVEN", True, "17"],
220 ["EIGHT", True, "18"],
221 ["NINE", True, "19"],
222 ["ZERO", True, "20"],
225 for mode in ["Mesh", "Curve", "Armature", "Metaball", "Lattice", "Grease Pencil Stroke Edit Mode"]:
226 for key in qcd_hotkeys:
227 km = wm.keyconfigs.addon.keymaps.new(name=mode)
228 kmi = km.keymap_items.new('view3d.view_qcd_slot', key[0], 'PRESS', alt=key[1])
229 kmi.properties.slot = key[2]
230 kmi.properties.toggle = False
231 addon_qcd_view_edit_mode_hotkey_keymaps.append((km, kmi))
233 km = wm.keyconfigs.addon.keymaps.new(name=mode)
234 kmi = km.keymap_items.new('view3d.view_qcd_slot', key[0], 'PRESS',shift=True, alt=key[1])
235 kmi.properties.slot = key[2]
236 kmi.properties.toggle = True
237 addon_qcd_view_edit_mode_hotkey_keymaps.append((km, kmi))
239 km = wm.keyconfigs.addon.keymaps.new(name=mode)
240 kmi = km.keymap_items.new('view3d.enable_all_qcd_slots', 'PLUS', 'PRESS', shift=True)
241 addon_qcd_view_edit_mode_hotkey_keymaps.append((km, kmi))
243 km = wm.keyconfigs.addon.keymaps.new(name=mode)
244 kmi = km.keymap_items.new('view3d.isolate_selected_objects_collections', 'EQUAL', 'PRESS')
245 addon_qcd_view_edit_mode_hotkey_keymaps.append((km, kmi))
247 km = wm.keyconfigs.addon.keymaps.new(name=mode)
248 kmi = km.keymap_items.new('view3d.disable_all_non_qcd_slots', 'PLUS', 'PRESS', shift=True, alt=True)
249 addon_qcd_view_edit_mode_hotkey_keymaps.append((km, kmi))
251 km = wm.keyconfigs.addon.keymaps.new(name=mode)
252 kmi = km.keymap_items.new('view3d.disable_all_collections', 'EQUAL', 'PRESS', alt=True, ctrl=True)
253 addon_qcd_view_edit_mode_hotkey_keymaps.append((km, kmi))
255 km = wm.keyconfigs.addon.keymaps.new(name=mode)
256 kmi = km.keymap_items.new('view3d.discard_qcd_history', 'EQUAL', 'PRESS', alt=True)
257 addon_qcd_view_edit_mode_hotkey_keymaps.append((km, kmi))
260 km = wm.keyconfigs.addon.keymaps.new(name="Mesh")
261 kmi = km.keymap_items.new('wm.call_menu', 'ACCENT_GRAVE', 'PRESS')
262 kmi.properties.name = "VIEW3D_MT_edit_mesh_select_mode"
263 addon_qcd_view_edit_mode_hotkey_keymaps.append((km, kmi))
266 def register_qcd_3dview_header_widget():
267 # unregister first to guard against default value updates (mouse hover + backspace)
268 # if the widget isn't registered it will just do nothing
269 unregister_qcd_3dview_header_widget()
271 # add the widget to the header, and an update function to the top bar to get view layer changes
272 bpy.types.VIEW3D_HT_header.append(ui.view3d_header_qcd_slots)
273 bpy.types.TOPBAR_HT_upper_bar.append(ui.view_layer_update)
277 def unregister_qcd():
278 global qcd_registered
279 if not qcd_registered:
280 # guard to handle default value updates (mouse hover + backspace)
281 return
283 unregister_qcd_3dview_header_widget()
285 for cls in qcd_classes:
286 bpy.utils.unregister_class(cls)
288 bpy.app.handlers.save_pre.remove(save_internal_data)
289 bpy.app.handlers.load_post.remove(load_internal_data)
290 bpy.app.handlers.load_pre.remove(load_pre_handler)
292 for pcoll in ui.preview_collections.values():
293 bpy.utils.previews.remove(pcoll)
294 ui.preview_collections.clear()
295 ui.last_icon_theme_text = None
296 ui.last_icon_theme_text_sel = None
298 # remove keymaps when qcd is deactivated
299 for km, kmi in addon_qcd_keymaps:
300 km.keymap_items.remove(kmi)
301 addon_qcd_keymaps.clear()
304 unregister_qcd_view_hotkeys()
306 unregister_qcd_view_edit_mode_hotkeys()
308 qcd_registered = False
311 def unregister_qcd_view_hotkeys():
312 # remove keymaps when qcd view hotkeys are deactivated
313 for km, kmi in addon_qcd_view_hotkey_keymaps:
314 km.keymap_items.remove(kmi)
315 addon_qcd_view_hotkey_keymaps.clear()
318 def unregister_qcd_view_edit_mode_hotkeys():
319 # remove keymaps when qcd view hotkeys are deactivated
320 for km, kmi in addon_qcd_view_edit_mode_hotkey_keymaps:
321 km.keymap_items.remove(kmi)
322 addon_qcd_view_edit_mode_hotkey_keymaps.clear()
325 def unregister_qcd_3dview_header_widget():
326 bpy.types.VIEW3D_HT_header.remove(ui.view3d_header_qcd_slots)
327 bpy.types.TOPBAR_HT_upper_bar.remove(ui.view_layer_update)