forked from wheybags/freeablo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shade_menu.py
150 lines (112 loc) · 5.51 KB
/
shade_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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from .Utils.core import *
# adds a shading mode menu
class ShadeModeOperator(bpy.types.Operator):
bl_label = "Shading Operator"
bl_idname = "view3d.shading_menu_operator"
last_mode = ['WIREFRAME', 'SOLID']
def init(self, context):
# populate the list of last modes
if context.space_data.viewport_shade not in self.last_mode:
self.last_mode.append(context.space_data.viewport_shade)
# keep the list to 2 items
if len(self.last_mode) > 2:
del self.last_mode[1]
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=ShadeModeMenu.bl_idname)
return {'FINISHED'}
# else toggle between wireframe and your last used mode
elif event.value == 'RELEASE' and current_time < self.start_time + 0.3:
if context.space_data.viewport_shade != self.last_mode[0]:
context.space_data.viewport_shade = self.last_mode[0]
else:
context.space_data.viewport_shade = self.last_mode[1]
return {'FINISHED'}
return {'RUNNING_MODAL'}
def execute(self, context):
self.init(context)
self.start_time = time.time()
context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
class ShadeModeMenu(bpy.types.Menu):
bl_label = "Viewport Shading"
bl_idname = "VIEW3D_MT_shade_menu"
def init(self):
renderer = bpy.context.scene.render.engine
if renderer == 'BLENDER_RENDER':
modes = [["Solid", 'SOLID', "SOLID"],
["Wireframe", 'WIREFRAME', "WIRE"],
["Textured", 'TEXTURED', "TEXTURE_SHADED"],
["Material", 'MATERIAL', "MATERIAL_DATA"],
["Rendered", 'RENDERED', "SMOOTH"],
["Bounding Box", 'BOUNDBOX', "BBOX"]]
elif renderer == 'CYCLES':
modes = [["Solid", 'SOLID', "SOLID"],
["Wireframe", 'WIREFRAME', "WIRE"],
["Textured", 'TEXTURED', "TEXTURE_SHADED"],
["Material", 'MATERIAL', "MATERIAL_DATA"],
["Rendered", 'RENDERED', "SMOOTH"],
["Bounding Box", 'BOUNDBOX', "BBOX"]]
else:
modes = [["Solid", 'SOLID', "SOLID"],
["Wireframe", 'WIREFRAME', "WIRE"],
["Textured", 'TEXTURED', "TEXTURE_SHADED"],
["Material", 'MATERIAL', "MATERIAL_DATA"],
["Bounding Box", 'BOUNDBOX', "BBOX"]]
return modes
def draw(self, context):
modes = self.init()
menu = Menu(self)
# add the items to the menu
for mode in modes:
menuprop(menu.add_item(), mode[0], mode[1], "space_data.viewport_shade",
icon=mode[2], disable=True)
# add a shading options menu if object can be shaded smooth/flat
if bpy.context.object and bpy.context.object.type in ['MESH', 'CURVE', 'SURFACE']:
menu.add_item().separator()
if context.object.use_dynamic_topology_sculpting:
menu.add_item().prop(context.tool_settings.sculpt, "use_smooth_shading", toggle=True)
else:
menu.add_item().menu(MeshShadeMenu.bl_idname)
if get_mode() != 'EDIT':
menu.add_item().menu(DisplayOptionsMenu.bl_idname)
class MeshShadeMenu(bpy.types.Menu):
bl_label = "Object Shading"
bl_idname = "VIEW3D_MT_mesh_shade"
def draw(self, context):
menu = Menu(self)
menu.add_item().label(text="Object Shading")
menu.add_item().separator()
if bpy.context.mode == 'EDIT_MESH':
menu.add_item().operator("mesh.faces_shade_flat", "Shade Flat", icon="MESH_ICOSPHERE")
menu.add_item().operator("mesh.faces_shade_smooth", "Shade Smooth", icon="MESH_UVSPHERE")
else:
menu.add_item().operator("object.shade_flat", "Shade Flat", icon="MESH_ICOSPHERE")
menu.add_item().operator("object.shade_smooth", "Shade Smooth", icon="MESH_UVSPHERE")
class DisplayOptionsMenu(bpy.types.Menu):
bl_label = "Display Options"
bl_idname = "VIEW3D_MT_display_options"
def draw(self, context):
menu = Menu(self)
menu.add_item().label(text="Display Options")
menu.add_item().separator()
menu.add_item().prop(context.object, 'show_name', toggle=True)
menu.add_item().prop(context.object, 'show_axis', toggle=True)
menu.add_item().prop(context.object, 'show_wire', toggle=True)
menu.add_item().prop(context.object, 'show_x_ray', toggle=True)
menu.add_item().prop(context.object, 'show_all_edges', toggle=True)
### ------------ New hotkeys and registration ------------ ###
addon_keymaps = []
def register():
# create the global menu hotkey
wm = bpy.context.window_manager
km = wm.keyconfigs.addon.keymaps.new(name='Object Non-modal')
kmi = km.keymap_items.new('view3d.shading_menu_operator', 'Z', 'PRESS')
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()