forked from wheybags/freeablo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snap_menu.py
126 lines (87 loc) · 4.34 KB
/
snap_menu.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
from .Utils.core import *
# adds a shading mode menu
class SnapMenuOperator(bpy.types.Operator):
bl_label = "Snap Menu Operator"
bl_idname = "view3d.snap_menu_operator"
@classmethod
def poll(self, context):
if get_mode() in [object_mode, edit, particle_edit, gpencil_edit]:
return True
else:
return False
def modal(self, context, event):
current_time = time.time()
# if key has been held for more than 0.3 seconds call the menu
if event.value == 'RELEASE' and current_time > self.start_time + 0.3:
bpy.ops.wm.call_menu(name=SnapModeMenu.bl_idname)
return {'FINISHED'}
# else toggle snap mode on/off
elif event.value == 'RELEASE' and current_time < self.start_time + 0.3:
if context.tool_settings.use_snap:
context.tool_settings.use_snap = False
else:
context.tool_settings.use_snap = True
return {'FINISHED'}
return {'RUNNING_MODAL'}
def execute(self, context):
self.start_time = time.time()
context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
class SnapModeMenu(bpy.types.Menu):
bl_label = "Snap Element"
bl_idname = "VIEW3D_MT_snap_menu"
def draw(self, context):
menu = Menu(self)
snap_element = bpy.context.tool_settings.snap_element
# menu for node editor
if context.space_data.type == 'NODE_EDITOR':
# add the menu items
for mode in context.tool_settings.bl_rna.properties['snap_node_element'].enum_items:
menuprop(menu.add_item(), mode.name, mode.identifier, "tool_settings.snap_node_element",
icon=mode.icon, disable=True)
if snap_element != "INCREMENT":
menu.add_item().separator()
menu.add_item().menu(SnapTargetMenu.bl_idname)
# menu for 3d view
if context.space_data.type == 'VIEW_3D':
# add the menu items
for mode in context.tool_settings.bl_rna.properties['snap_element'].enum_items:
menuprop(menu.add_item(), mode.name, mode.identifier, "tool_settings.snap_element",
icon=mode.icon, disable=True)
if snap_element != "INCREMENT":
menu.add_item().separator()
menu.add_item().menu(SnapTargetMenu.bl_idname)
menu.add_item().separator()
if snap_element == "INCREMENT":
menu.add_item().prop(bpy.context.tool_settings, "use_snap_grid_absolute", toggle=True)
if snap_element not in ["INCREMENT", "VOLUME"]:
menu.add_item().prop(bpy.context.tool_settings, "use_snap_align_rotation", toggle=True)
if snap_element == "FACE":
menu.add_item().prop(bpy.context.tool_settings, "use_snap_project", toggle=True)
if snap_element == "VOLUME":
menu.add_item().prop(bpy.context.tool_settings, "use_snap_peel_object", toggle=True)
class SnapTargetMenu(bpy.types.Menu):
bl_label = "Snap Target"
bl_idname = "VIEW3D_MT_snap_target_menu"
def draw(self, context):
menu = Menu(self)
menu.add_item().label(text="Snap Target")
menu.add_item().separator()
# add the menu items
for mode in context.tool_settings.bl_rna.properties['snap_target'].enum_items:
menuprop(menu.add_item(), mode.name, mode.identifier, "tool_settings.snap_target", disable=True)
### ------------ New hotkeys and registration ------------ ###
addon_keymaps = []
def register():
# create the global menu hotkey
wm = bpy.context.window_manager
modes = {'Object Non-modal':'EMPTY', 'Grease Pencil Stroke Edit Mode':'EMPTY', 'Node Editor':'NODE_EDITOR'}
for mode, space in modes.items():
km = wm.keyconfigs.addon.keymaps.new(name=mode, space_type=space)
kmi = km.keymap_items.new('view3d.snap_menu_operator', 'TAB', 'PRESS', shift=True)
addon_keymaps.append((km, kmi))
def unregister():
# remove keymaps when add-on is deactivated
for km, kmi in addon_keymaps:
km.keymap_items.remove(kmi)
addon_keymaps.clear()