forked from Exairnous/Advanced-UI-Menus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
layers_window.py
186 lines (136 loc) · 6.39 KB
/
layers_window.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
from .Utils.core import *
def get_layers(context):
# if the layer management addon is enabled name the layers with the layer names
try:
layernames = []
# if the name has "layer" in front of a number remove "layer" and leave the number
for x in range(20):
layer_name = context.scene.namedlayers.layers[x].name
if layer_name in ["Layer{}".format(x+1), "Layer0{}".format(x+1)]:
layernames.append("{0}".format(x+1))
else:
# replace blank layer names with a space so the button will be full length
layernames.append(layer_name if layer_name is not "" else " ")
# if not then name the layers with numbers
except:
layernames = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"11", "12", "13", "14", "15", "16", "17", "18", "19", "20"]
return layernames
def set_layer_func(self, context, event):
if event.shift:
# toggle the layer on/off
context.scene.layers[self.layer_num] = not context.scene.layers[self.layer_num]
bpy.types.Scene.layer_changed = True
else:
# create a boolian list of which layers on and off
layers = [False]*20
layers[self.layer_num] = True
# apply the list to blender's layers
context.scene.layers = layers
bpy.types.Scene.layer_changed = True
return {'FINISHED'}
custom_ops = []
def add_set_layer_op(num, layer):
op_name = 'view3d.set_layer_view_'+str(num)
nc = type( 'DynOpSetLayer_'+str(num),
(bpy.types.Operator, ),
{'bl_idname': op_name,
'bl_label': "Set Layer View",
'bl_description': "Visualize this Layer, Shift-Click to select multiple layers\nName: "+layer,
'layer_num': bpy.props.IntProperty(name="layer_num"),
'invoke': set_layer_func
})
custom_ops.append(nc)
bpy.utils.register_class(nc)
class SetObjectLayer(bpy.types.Operator):
'''Move objects to this Layer, Shift-Click to select multiple layers'''
bl_idname = "view3d.set_object_layer"
bl_label = "Move Object To Layers Operator"
layer_num = bpy.props.IntProperty(name="layer_num")
def invoke(self, context, event):
selected_objects = [object for object in context.scene.objects if object.select == True]
if event.shift:
# toggle the objects on/off of the layer
for object in selected_objects:
if object.layers[self.layer_num] == context.scene.objects.active.layers[self.layer_num]:
enabled = not context.scene.objects.active.layers[self.layer_num]
else:
enabled = True
break
for object in selected_objects:
layers = object.layers
layers[self.layer_num] = enabled
object.layers = layers
bpy.types.Scene.layer_changed = True
else:
# create a boolian list of which layers on and off
layers = [False]*20
layers[self.layer_num] = True
# move the objects to the layer
for object in selected_objects:
object.layers = layers
bpy.types.Scene.layer_changed = True
return {'FINISHED'}
class LayersWindow(bpy.types.Operator):
bl_label = "Layers"
bl_idname = "view3d.layers_window"
current_space = None
def check(self, context):
if bpy.types.Scene.layer_changed:
bpy.types.Scene.layer_changed = False
return True
else:
return False
def draw(self, context):
if context.space_data:
self.current_space = context.space_data
ui = Menu(self)
column_flow = ui.add_item("column_flow", columns=2, align=True)
layernames = get_layers(context)
# add the menu items
for num in range(20):
op_name = 'view3d.set_layer_view_'+str(num)
has_active = (context.object and context.object.layers[num])
is_layer_used = self.current_space.layers_used[num]
icon = ('LAYER_ACTIVE' if has_active else 'LAYER_USED') if is_layer_used else 'RADIOBUT_OFF'
prop = ui.add_item(parent=column_flow).operator("view3d.set_object_layer", "", icon=icon)
prop.layer_num = num
if num == context.scene.active_layer:
prop = ui.current_item.operator(op_name, layernames[num], icon='FILE_TICK')
elif context.scene.layers[num]:
prop = ui.current_item.operator(op_name, layernames[num], icon='RESTRICT_VIEW_OFF')
else:
prop = ui.current_item.operator(op_name, layernames[num], icon='BLANK1')
ui.current_item.operator_context = 'INVOKE_DEFAULT'
prop.layer_num = num
ui.current_item.separator()
if num in [4, 14]:
ui.add_item(parent=column_flow).separator()
ui.add_item(parent=column_flow).separator()
def invoke(self, context, event):
wm = context.window_manager
layernames = get_layers(context)
for op in custom_ops:
bpy.utils.unregister_class(op)
custom_ops.clear()
for num, layer in enumerate(layernames):
add_set_layer_op(num, layer)
return wm.invoke_props_dialog(self)
def execute(self, context):
return {'FINISHED'}
### ------------ New hotkeys and registration ------------ ###
addon_keymaps = []
def register():
set_prop("BoolProperty", "bpy.types.Scene.layer_changed", name="layer_changed")
# create the global menu hotkey
wm = bpy.context.window_manager
km = wm.keyconfigs.addon.keymaps.new(name='Object Mode')
kmi = km.keymap_items.new('view3d.layers_window', 'M', '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()
for op in custom_ops:
bpy.utils.unregister_class(op)