Pose library: fix asset creation operator poll when no object active
[blender-addons.git] / space_view3d_pie_menus / pie_modes_menu.py
blobc5a3742bfb64193a08e5b315b6fb7f1a8eb878d5
1 # SPDX-License-Identifier: GPL-2.0-or-later
3 bl_info = {
4 "name": "Hotkey: 'Ctrl Tab'",
5 "description": "Switch between 3d view object/edit modes",
6 "author": "pitiwazou, meta-androcto, italic",
7 "version": (0, 1, 2),
8 "blender": (2, 80, 0),
9 "location": "3D View",
10 "warning": "",
11 "doc_url": "",
12 "category": "Mode Switch Pie"
15 import bpy
16 from bpy.types import (
17 Menu,
18 Operator
22 class PIE_OT_ClassObject(Operator):
23 bl_idname = "class.object"
24 bl_label = "Class Object"
25 bl_description = "Edit/Object Mode Switch"
26 bl_options = {'REGISTER', 'UNDO'}
28 def execute(self, context):
29 if context.object.mode == "OBJECT":
30 bpy.ops.object.mode_set(mode="EDIT")
31 else:
32 bpy.ops.object.mode_set(mode="OBJECT")
33 return {'FINISHED'}
36 class PIE_OT_ClassTexturePaint(Operator):
37 bl_idname = "class.pietexturepaint"
38 bl_label = "Class Texture Paint"
39 bl_description = "Texture Paint"
40 bl_options = {'REGISTER', 'UNDO'}
42 def execute(self, context):
43 if context.object.mode == "EDIT":
44 bpy.ops.object.mode_set(mode="OBJECT")
45 bpy.ops.paint.texture_paint_toggle()
46 else:
47 bpy.ops.paint.texture_paint_toggle()
48 return {'FINISHED'}
51 class PIE_OT_ClassWeightPaint(Operator):
52 bl_idname = "class.pieweightpaint"
53 bl_label = "Class Weight Paint"
54 bl_description = "Weight Paint"
55 bl_options = {'REGISTER', 'UNDO'}
57 def execute(self, context):
58 if context.object.mode == "EDIT":
59 bpy.ops.object.mode_set(mode="OBJECT")
60 bpy.ops.paint.weight_paint_toggle()
61 else:
62 bpy.ops.paint.weight_paint_toggle()
63 return {'FINISHED'}
66 class PIE_OT_ClassVertexPaint(Operator):
67 bl_idname = "class.pievertexpaint"
68 bl_label = "Class Vertex Paint"
69 bl_description = "Vertex Paint"
70 bl_options = {'REGISTER', 'UNDO'}
72 def execute(self, context):
73 if context.object.mode == "EDIT":
74 bpy.ops.object.mode_set(mode="OBJECT")
75 bpy.ops.paint.vertex_paint_toggle()
76 else:
77 bpy.ops.paint.vertex_paint_toggle()
78 return {'FINISHED'}
81 class PIE_OT_ClassParticleEdit(Operator):
82 bl_idname = "class.pieparticleedit"
83 bl_label = "Class Particle Edit"
84 bl_description = "Particle Edit (must have active particle system)"
85 bl_options = {'REGISTER', 'UNDO'}
87 def execute(self, context):
88 if context.object.mode == "EDIT":
89 bpy.ops.object.mode_set(mode="OBJECT")
90 bpy.ops.particle.particle_edit_toggle()
91 else:
92 bpy.ops.particle.particle_edit_toggle()
93 return {'FINISHED'}
96 # Set Mode Operator #
97 class PIE_OT_SetObjectModePie(Operator):
98 bl_idname = "object.set_object_mode_pie"
99 bl_label = "Set the object interactive mode"
100 bl_description = "I set the interactive mode of object"
101 bl_options = {'REGISTER'}
103 mode: bpy.props.StringProperty(name="Interactive mode", default="OBJECT")
105 def execute(self, context):
106 if (context.active_object):
107 try:
108 bpy.ops.object.mode_set(mode=self.mode)
109 except TypeError:
110 msg = context.active_object.name + " It is not possible to enter into the interactive mode"
111 self.report(type={"WARNING"}, message=msg)
112 else:
113 self.report(type={"WARNING"}, message="There is no active object")
114 return {'FINISHED'}
117 # Edit Selection Modes
118 class PIE_OT_ClassVertex(Operator):
119 bl_idname = "class.vertex"
120 bl_label = "Class Vertex"
121 bl_description = "Vert Select Mode"
122 bl_options = {'REGISTER', 'UNDO'}
124 def execute(self, context):
125 if context.object.mode != "EDIT":
126 bpy.ops.object.mode_set(mode="EDIT")
127 bpy.ops.mesh.select_mode(use_extend=False, use_expand=False, type='VERT')
128 if bpy.ops.mesh.select_mode != "EDGE, FACE":
129 bpy.ops.mesh.select_mode(use_extend=False, use_expand=False, type='VERT')
130 return {'FINISHED'}
133 class PIE_OT_ClassEdge(Operator):
134 bl_idname = "class.edge"
135 bl_label = "Class Edge"
136 bl_description = "Edge Select Mode"
137 bl_options = {'REGISTER', 'UNDO'}
139 def execute(self, context):
140 if context.object.mode != "EDIT":
141 bpy.ops.object.mode_set(mode="EDIT")
142 bpy.ops.mesh.select_mode(use_extend=False, use_expand=False, type='EDGE')
143 if bpy.ops.mesh.select_mode != "VERT, FACE":
144 bpy.ops.mesh.select_mode(use_extend=False, use_expand=False, type='EDGE')
145 return {'FINISHED'}
148 class PIE_OT_ClassFace(Operator):
149 bl_idname = "class.face"
150 bl_label = "Class Face"
151 bl_description = "Face Select Mode"
152 bl_options = {'REGISTER', 'UNDO'}
154 def execute(self, context):
155 if context.object.mode != "EDIT":
156 bpy.ops.object.mode_set(mode="EDIT")
157 bpy.ops.mesh.select_mode(use_extend=False, use_expand=False, type='FACE')
158 if bpy.ops.mesh.select_mode != "VERT, EDGE":
159 bpy.ops.mesh.select_mode(use_extend=False, use_expand=False, type='FACE')
160 return {'FINISHED'}
163 class PIE_OT_VertsEdgesFaces(Operator):
164 bl_idname = "verts.edgesfaces"
165 bl_label = "Verts Edges Faces"
166 bl_description = "Vert/Edge/Face Select Mode"
167 bl_options = {'REGISTER', 'UNDO'}
169 def execute(self, context):
170 if context.object.mode != "EDIT":
171 bpy.ops.object.mode_set(mode="EDIT")
172 bpy.ops.mesh.select_mode(use_extend=False, use_expand=False, type='VERT')
173 if bpy.ops.mesh.select_mode != "VERT, EDGE, FACE":
174 bpy.ops.object.mode_set(mode="EDIT")
175 bpy.ops.mesh.select_mode(use_extend=False, use_expand=False, type='VERT')
176 bpy.ops.mesh.select_mode(use_extend=True, use_expand=False, type='EDGE')
177 bpy.ops.mesh.select_mode(use_extend=True, use_expand=False, type='FACE')
178 return {'FINISHED'}
181 # Menus
182 class PIE_MT_ObjectEditotherModes(Menu):
183 """Edit/Object Others modes"""
184 bl_idname = "MENU_MT_objecteditmodeothermodes"
185 bl_label = "Edit Selection Modes"
187 def draw(self, context):
188 layout = self.layout
189 pie = layout.menu_pie()
190 box = pie.split().column()
192 box.operator("class.vertex", text="Vertex", icon='VERTEXSEL')
193 box.operator("class.edge", text="Edge", icon='EDGESEL')
194 box.operator("class.face", text="Face", icon='FACESEL')
195 box.operator("verts.edgesfaces", text="Vertex/Edges/Faces", icon='OBJECT_DATAMODE')
198 class PIE_MT_ObjectEditMode(Menu):
199 """Modes Switch"""
200 bl_idname = "PIE_MT_objecteditmode"
201 bl_label = "Mode Switch (Ctrl Tab)"
203 def draw(self, context):
204 layout = self.layout
205 ob = context.object
206 # No Object Selected #
207 if not ob or not ob.select_get():
208 message = "No Active Object Selected"
209 pie = layout.menu_pie()
210 pie.separator()
211 pie.separator()
212 pie.separator()
213 box = pie.box()
214 box.label(text=message, icon="INFO")
216 elif ob and ob.type == 'MESH' and ob.mode in {
217 'OBJECT', 'SCULPT', 'VERTEX_PAINT',
218 'WEIGHT_PAINT', 'TEXTURE_PAINT',
219 'PARTICLE_EDIT', 'GPENCIL_EDIT',
221 pie = layout.menu_pie()
222 # 4 - LEFT
223 pie.operator("class.pieweightpaint", text="Weight Paint", icon='WPAINT_HLT')
224 # 6 - RIGHT
225 pie.operator("class.pietexturepaint", text="Texture Paint", icon='TPAINT_HLT')
226 # 2 - BOTTOM
227 pie.menu("MENU_MT_objecteditmodeothermodes", text="Edit Modes", icon='EDITMODE_HLT')
228 # 8 - TOP
229 pie.operator("class.object", text="Object/Edit Toggle", icon='OBJECT_DATAMODE')
230 # 7 - TOP - LEFT
231 pie.operator("sculpt.sculptmode_toggle", text="Sculpt", icon='SCULPTMODE_HLT')
232 # 9 - TOP - RIGHT
233 pie.operator("class.pievertexpaint", text="Vertex Paint", icon='VPAINT_HLT')
234 # 1 - BOTTOM - LEFT
235 pie.separator()
236 # 3 - BOTTOM - RIGHT
237 if context.object.particle_systems:
238 pie.operator("class.pieparticleedit", text="Particle Edit", icon='PARTICLEMODE')
239 else:
240 pie.separator()
242 elif ob and ob.type == 'MESH' and ob.mode in {'EDIT'}:
243 pie = layout.menu_pie()
244 # 4 - LEFT
245 pie.operator("class.pieweightpaint", text="Weight Paint", icon='WPAINT_HLT')
246 # 6 - RIGHT
247 pie.operator("class.pietexturepaint", text="Texture Paint", icon='TPAINT_HLT')
248 # 2 - BOTTOM
249 pie.menu("MENU_MT_objecteditmodeothermodes", text="Edit Modes", icon='EDITMODE_HLT')
250 # 8 - TOP
251 pie.operator("class.object", text="Edit/Object Toggle", icon='OBJECT_DATAMODE')
252 # 7 - TOP - LEFT
253 pie.operator("sculpt.sculptmode_toggle", text="Sculpt", icon='SCULPTMODE_HLT')
254 # 9 - TOP - RIGHT
255 pie.operator("class.pievertexpaint", text="Vertex Paint", icon='VPAINT_HLT')
256 # 1 - BOTTOM - LEFT
257 pie.separator()
258 # 3 - BOTTOM - RIGHT
259 if context.object.particle_systems:
260 pie.operator("class.pieparticleedit", text="Particle Edit", icon='PARTICLEMODE')
261 else:
262 pie.separator()
264 elif ob and ob.type == 'CURVE':
265 pie = layout.menu_pie()
266 # 4 - LEFT
267 pie.separator()
268 # 6 - RIGHT
269 pie.separator()
270 # 2 - BOTTOM
271 pie.separator()
272 # 8 - TOP
273 pie.operator("object.editmode_toggle", text="Edit/Object", icon='OBJECT_DATAMODE')
274 # 7 - TOP - LEFT
275 pie.separator()
276 # 9 - TOP - RIGHT
277 pie.separator()
278 # 1 - BOTTOM - LEFT
279 pie.separator()
280 # 3 - BOTTOM - RIGHT
281 pie.separator()
283 elif ob and ob.type == 'ARMATURE':
284 pie = layout.menu_pie()
285 # 4 - LEFT
286 pie.operator(PIE_OT_SetObjectModePie.bl_idname, text="Object", icon="OBJECT_DATAMODE").mode = "OBJECT"
287 # 6 - RIGHT
288 pie.operator(PIE_OT_SetObjectModePie.bl_idname, text="Pose", icon="POSE_HLT").mode = "POSE"
289 # 2 - BOTTOM
290 pie.operator(PIE_OT_SetObjectModePie.bl_idname, text="Edit", icon="EDITMODE_HLT").mode = "EDIT"
291 # 8 - TOP
292 pie.operator("object.editmode_toggle", text="Edit Mode", icon='OBJECT_DATAMODE')
293 # 7 - TOP - LEFT
294 pie.separator()
295 # 9 - TOP - RIGHT
296 pie.separator()
297 # 1 - BOTTOM - LEFT
298 pie.separator()
299 # 3 - BOTTOM - RIGHT
300 pie.separator()
302 elif ob and ob.type == 'FONT':
303 pie = layout.menu_pie()
304 pie.separator()
305 pie.separator()
306 pie.separator()
307 pie.operator("object.editmode_toggle", text="Edit/Object Toggle", icon='OBJECT_DATAMODE')
308 pie.separator()
309 pie.separator()
310 pie.separator()
311 # 3 - BOTTOM - RIGHT
312 pie.separator()
314 elif ob and ob.type == 'SURFACE':
315 pie = layout.menu_pie()
316 pie.separator()
317 pie.separator()
318 pie.separator()
319 pie.operator("object.editmode_toggle", text="Edit/Object Toggle", icon='OBJECT_DATAMODE')
320 pie.separator()
321 pie.separator()
322 pie.separator()
323 # 3 - BOTTOM - RIGHT
324 pie.separator()
326 elif ob and ob.type == 'META':
327 pie = layout.menu_pie()
328 pie.separator()
329 pie.separator()
330 pie.separator()
331 pie.operator("object.editmode_toggle", text="Edit/Object Toggle", icon='OBJECT_DATAMODE')
332 pie.separator()
333 pie.separator()
334 pie.separator()
335 # 3 - BOTTOM - RIGHT
336 pie.separator()
338 elif ob and ob.type == 'LATTICE':
339 pie = layout.menu_pie()
340 pie.separator()
341 pie.separator()
342 pie.separator()
343 pie.operator("object.editmode_toggle", text="Edit/Object Toggle", icon='OBJECT_DATAMODE')
344 pie.separator()
345 pie.separator()
346 pie.separator()
348 if ob and ob.type == 'GPENCIL':
349 pie = layout.menu_pie()
350 # 4 - LEFT
351 pie.operator(PIE_OT_SetObjectModePie.bl_idname, text="Sculpt",
352 icon="SCULPTMODE_HLT").mode = "SCULPT_GPENCIL"
353 # 6 - RIGHT
354 pie.operator(PIE_OT_SetObjectModePie.bl_idname, text="Draw", icon="GREASEPENCIL").mode = "PAINT_GPENCIL"
355 # 2 - BOTTOM
356 pie.operator(PIE_OT_SetObjectModePie.bl_idname, text="Edit", icon="EDITMODE_HLT").mode = "EDIT_GPENCIL"
357 # 8 - TOP
358 pie.operator(PIE_OT_SetObjectModePie.bl_idname, text="Object", icon="OBJECT_DATAMODE").mode = "OBJECT"
359 # 7 - TOP - LEFT
360 pie.separator()
361 # 9 - TOP - RIGHT
362 pie.separator()
363 # 1 - BOTTOM - LEFT
364 pie.separator()
365 # 3 - BOTTOM - RIGHT
366 pie.operator(
367 PIE_OT_SetObjectModePie.bl_idname,
368 text="Weight Paint",
369 icon="WPAINT_HLT").mode = "WEIGHT_GPENCIL"
371 elif ob and ob.type in {"LIGHT", "CAMERA", "EMPTY", "SPEAKER"}:
372 message = "Active Object has only Object Mode available"
373 pie = layout.menu_pie()
374 pie.separator()
375 pie.separator()
376 pie.separator()
377 box = pie.box()
378 box.label(text=message, icon="INFO")
381 classes = (
382 PIE_MT_ObjectEditMode,
383 PIE_OT_ClassObject,
384 PIE_OT_ClassVertex,
385 PIE_OT_ClassEdge,
386 PIE_OT_ClassFace,
387 PIE_MT_ObjectEditotherModes,
388 PIE_OT_ClassTexturePaint,
389 PIE_OT_ClassWeightPaint,
390 PIE_OT_ClassVertexPaint,
391 PIE_OT_ClassParticleEdit,
392 PIE_OT_VertsEdgesFaces,
393 PIE_OT_SetObjectModePie,
396 addon_keymaps = []
399 def register():
400 for cls in classes:
401 bpy.utils.register_class(cls)
403 wm = bpy.context.window_manager
404 if wm.keyconfigs.addon:
405 # Select Mode
406 km = wm.keyconfigs.addon.keymaps.new(name='Object Non-modal')
407 kmi = km.keymap_items.new('wm.call_menu_pie', 'TAB', 'PRESS', ctrl=True)
408 kmi.properties.name = "PIE_MT_objecteditmode"
409 addon_keymaps.append((km, kmi))
411 km = wm.keyconfigs.addon.keymaps.new(name='Grease Pencil Stroke Edit Mode')
412 kmi = km.keymap_items.new('wm.call_menu_pie', 'TAB', 'PRESS', ctrl=True)
413 kmi.properties.name = "PIE_MT_objecteditmode"
414 addon_keymaps.append((km, kmi))
417 def unregister():
418 for cls in classes:
419 bpy.utils.unregister_class(cls)
421 wm = bpy.context.window_manager
422 kc = wm.keyconfigs.addon
423 if kc:
424 for km, kmi in addon_keymaps:
425 km.keymap_items.remove(kmi)
426 addon_keymaps.clear()
429 if __name__ == "__main__":
430 register()