509c5bae35500f36c539cc9c04ec3ccc065b861b
[blender.git] / release / scripts / startup / bl_ui / space_topbar.py
1 # ##### BEGIN GPL LICENSE BLOCK #####
2 #
3 #  This program is free software; you can redistribute it and/or
4 #  modify it under the terms of the GNU General Public License
5 #  as published by the Free Software Foundation; either version 2
6 #  of the License, or (at your option) any later version.
7 #
8 #  This program is distributed in the hope that it will be useful,
9 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 #  GNU General Public License for more details.
12 #
13 #  You should have received a copy of the GNU General Public License
14 #  along with this program; if not, write to the Free Software Foundation,
15 #  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 #
17 # ##### END GPL LICENSE BLOCK #####
18
19 # <pep8 compliant>
20 import bpy
21 from bpy.types import Header, Menu, Panel
22
23
24 class TOPBAR_HT_upper_bar(Header):
25     bl_space_type = 'TOPBAR'
26
27     def draw(self, context):
28         region = context.region
29
30         if region.alignment == 'RIGHT':
31             self.draw_right(context)
32         else:
33             self.draw_left(context)
34
35     def draw_left(self, context):
36         layout = self.layout
37
38         window = context.window
39         screen = context.screen
40
41         layout.operator("wm.splash", text="", icon='BLENDER', emboss=False)
42
43         TOPBAR_MT_editor_menus.draw_collapsible(context, layout)
44
45         layout.separator()
46
47         if not screen.show_fullscreen:
48             layout.template_ID_tabs(
49                 window, "workspace",
50                 new="workspace.add",
51                 menu="TOPBAR_MT_workspace_menu",
52             )
53         else:
54             layout.operator(
55                 "screen.back_to_previous",
56                 icon='SCREEN_BACK',
57                 text="Back to Previous",
58             )
59
60     def draw_right(self, context):
61         layout = self.layout
62
63         window = context.window
64         screen = context.screen
65         scene = window.scene
66
67         # If statusbar is hidden, still show messages at the top
68         if not screen.show_statusbar:
69             layout.template_reports_banner()
70             layout.template_running_jobs()
71
72         # Active workspace view-layer is retrieved through window, not through workspace.
73         layout.template_ID(window, "scene", new="scene.new", unlink="scene.delete")
74
75         row = layout.row(align=True)
76         row.template_search(
77             window, "view_layer",
78             scene, "view_layers",
79             new="scene.view_layer_add",
80             unlink="scene.view_layer_remove")
81
82
83 class TOPBAR_PT_gpencil_layers(Panel):
84     bl_space_type = 'VIEW_3D'
85     bl_region_type = 'HEADER'
86     bl_label = "Layers"
87     bl_ui_units_x = 14
88
89     @classmethod
90     def poll(cls, context):
91         if context.gpencil_data is None:
92             return False
93
94         ob = context.object
95         if ob is not None and ob.type == 'GPENCIL':
96             return True
97
98         return False
99
100     def draw(self, context):
101         layout = self.layout
102         gpd = context.gpencil_data
103
104         # Grease Pencil data...
105         if (gpd is None) or (not gpd.layers):
106             layout.operator("gpencil.layer_add", text="New Layer")
107         else:
108             self.draw_layers(context, layout, gpd)
109
110     def draw_layers(self, context, layout, gpd):
111         row = layout.row()
112
113         col = row.column()
114         layer_rows = 10
115         col.template_list("GPENCIL_UL_layer", "", gpd, "layers", gpd.layers, "active_index",
116                           rows=layer_rows, sort_reverse=True, sort_lock=True)
117
118         gpl = context.active_gpencil_layer
119         if gpl:
120             srow = col.row(align=True)
121             srow.prop(gpl, "blend_mode", text="Blend")
122
123             srow = col.row(align=True)
124             srow.prop(gpl, "opacity", text="Opacity", slider=True)
125             srow.prop(gpl, "clamp_layer", text="",
126                       icon='MOD_MASK' if gpl.clamp_layer else 'LAYER_ACTIVE')
127
128             srow = col.row(align=True)
129             srow.prop(gpl, "use_solo_mode", text="Show Only On Keyframed")
130
131         col = row.column()
132
133         sub = col.column(align=True)
134         sub.operator("gpencil.layer_add", icon='ADD', text="")
135         sub.operator("gpencil.layer_remove", icon='REMOVE', text="")
136
137         gpl = context.active_gpencil_layer
138         if gpl:
139             sub.menu("GPENCIL_MT_layer_context_menu", icon='DOWNARROW_HLT', text="")
140
141             if len(gpd.layers) > 1:
142                 col.separator()
143
144                 sub = col.column(align=True)
145                 sub.operator("gpencil.layer_move", icon='TRIA_UP', text="").type = 'UP'
146                 sub.operator("gpencil.layer_move", icon='TRIA_DOWN', text="").type = 'DOWN'
147
148                 col.separator()
149
150                 sub = col.column(align=True)
151                 sub.operator("gpencil.layer_isolate", icon='LOCKED', text="").affect_visibility = False
152                 sub.operator("gpencil.layer_isolate", icon='HIDE_OFF', text="").affect_visibility = True
153
154
155 class TOPBAR_MT_editor_menus(Menu):
156     bl_idname = "TOPBAR_MT_editor_menus"
157     bl_label = ""
158
159     def draw(self, _context):
160         layout = self.layout
161         layout.menu("TOPBAR_MT_file")
162         layout.menu("TOPBAR_MT_edit")
163
164         layout.menu("TOPBAR_MT_render")
165
166         layout.menu("TOPBAR_MT_window")
167         layout.menu("TOPBAR_MT_help")
168
169
170 class TOPBAR_MT_file(Menu):
171     bl_label = "File"
172
173     def draw(self, context):
174         layout = self.layout
175         prefs = context.preferences
176
177         layout.operator_context = 'INVOKE_AREA'
178         layout.menu("TOPBAR_MT_file_new", text="New", icon='FILE_NEW')
179         layout.operator("wm.open_mainfile", text="Open...", icon='FILE_FOLDER')
180         layout.menu("TOPBAR_MT_file_open_recent")
181         layout.operator("wm.revert_mainfile")
182         layout.operator("wm.recover_last_session")
183         layout.operator("wm.recover_auto_save", text="Recover Auto Save...")
184
185         layout.separator()
186
187         layout.operator_context = 'EXEC_AREA' if context.blend_data.is_saved else 'INVOKE_AREA'
188         layout.operator("wm.save_mainfile", text="Save", icon='FILE_TICK')
189
190         layout.operator_context = 'INVOKE_AREA'
191         layout.operator("wm.save_as_mainfile", text="Save As...")
192         layout.operator_context = 'INVOKE_AREA'
193         layout.operator("wm.save_as_mainfile", text="Save Copy...").copy = True
194
195         layout.separator()
196         layout.operator_context = 'INVOKE_AREA'
197
198         if any(bpy.utils.app_template_paths()):
199             app_template = prefs.app_template
200         else:
201             app_template = None
202
203         if app_template:
204             layout.label(text=bpy.path.display_name(app_template, has_ext=False))
205
206         layout.operator("wm.save_homefile")
207         props = layout.operator("wm.read_factory_settings")
208         if app_template:
209             props.app_template = app_template
210
211         if prefs.use_preferences_save:
212             props = layout.operator(
213                 "wm.read_factory_settings",
214                 text="Load Factory Settings (Temporary)"
215             )
216             if app_template:
217                 props.app_template = app_template
218             props.use_temporary_preferences = True
219
220         layout.separator()
221
222         layout.operator("preferences.app_template_install", text="Install Application Template...")
223
224         layout.separator()
225
226         layout.operator_context = 'INVOKE_AREA'
227         layout.operator("wm.link", text="Link...", icon='LINK_BLEND')
228         layout.operator("wm.append", text="Append...", icon='APPEND_BLEND')
229         layout.menu("TOPBAR_MT_file_previews")
230
231         layout.separator()
232
233         layout.menu("TOPBAR_MT_file_import", icon='IMPORT')
234         layout.menu("TOPBAR_MT_file_export", icon='EXPORT')
235
236         layout.separator()
237
238         layout.menu("TOPBAR_MT_file_external_data")
239
240         layout.separator()
241
242         layout.operator_context = 'EXEC_AREA'
243         if bpy.data.is_dirty:
244             layout.operator_context = 'INVOKE_SCREEN'  # quit dialog
245         layout.operator("wm.quit_blender", text="Quit", icon='QUIT')
246
247
248 class TOPBAR_MT_file_new(Menu):
249     bl_label = "New File"
250
251     @staticmethod
252     def app_template_paths():
253         import os
254
255         template_paths = bpy.utils.app_template_paths()
256
257         # expand template paths
258         app_templates = []
259         for path in template_paths:
260             for d in os.listdir(path):
261                 if d.startswith(("__", ".")):
262                     continue
263                 template = os.path.join(path, d)
264                 if os.path.isdir(template):
265                     # template_paths_expand.append(template)
266                     app_templates.append(d)
267
268         return sorted(app_templates)
269
270     @staticmethod
271     def draw_ex(layout, _context, *, use_splash=False, use_more=False):
272         layout.operator_context = 'INVOKE_DEFAULT'
273
274         # Limit number of templates in splash screen, spill over into more menu.
275         paths = TOPBAR_MT_file_new.app_template_paths()
276         splash_limit = 5
277
278         if use_splash:
279             icon = 'FILE_NEW'
280             show_more = len(paths) > (splash_limit - 1)
281             if show_more:
282                 paths = paths[:splash_limit - 2]
283         elif use_more:
284             icon = 'FILE_NEW'
285             paths = paths[splash_limit - 2:]
286             show_more = False
287         else:
288             icon = 'NONE'
289             show_more = False
290
291         # Draw application templates.
292         if not use_more:
293             props = layout.operator("wm.read_homefile", text="General", icon=icon)
294             props.app_template = ""
295
296         for d in paths:
297             props = layout.operator(
298                 "wm.read_homefile",
299                 text=bpy.path.display_name(d),
300                 icon=icon,
301             )
302             props.app_template = d
303
304         layout.operator_context = 'EXEC_DEFAULT'
305
306         if show_more:
307             layout.menu("TOPBAR_MT_templates_more", text="...")
308
309     def draw(self, context):
310         TOPBAR_MT_file_new.draw_ex(self.layout, context)
311
312
313 class TOPBAR_MT_templates_more(Menu):
314     bl_label = "Templates"
315
316     def draw(self, context):
317         bpy.types.TOPBAR_MT_file_new.draw_ex(self.layout, context, use_more=True)
318
319
320 class TOPBAR_MT_file_import(Menu):
321     bl_idname = "TOPBAR_MT_file_import"
322     bl_label = "Import"
323
324     def draw(self, _context):
325         if bpy.app.build_options.collada:
326             self.layout.operator("wm.collada_import", text="Collada (Default) (.dae)")
327         if bpy.app.build_options.alembic:
328             self.layout.operator("wm.alembic_import", text="Alembic (.abc)")
329
330
331 class TOPBAR_MT_file_export(Menu):
332     bl_idname = "TOPBAR_MT_file_export"
333     bl_label = "Export"
334
335     def draw(self, _context):
336         if bpy.app.build_options.collada:
337             self.layout.operator("wm.collada_export", text="Collada (Default) (.dae)")
338         if bpy.app.build_options.alembic:
339             self.layout.operator("wm.alembic_export", text="Alembic (.abc)")
340
341
342 class TOPBAR_MT_file_external_data(Menu):
343     bl_label = "External Data"
344
345     def draw(self, _context):
346         layout = self.layout
347
348         icon = 'CHECKBOX_HLT' if bpy.data.use_autopack else 'CHECKBOX_DEHLT'
349         layout.operator("file.autopack_toggle", icon=icon)
350
351         layout.separator()
352
353         pack_all = layout.row()
354         pack_all.operator("file.pack_all")
355         pack_all.active = not bpy.data.use_autopack
356
357         unpack_all = layout.row()
358         unpack_all.operator("file.unpack_all")
359         unpack_all.active = not bpy.data.use_autopack
360
361         layout.separator()
362
363         layout.operator("file.make_paths_relative")
364         layout.operator("file.make_paths_absolute")
365         layout.operator("file.report_missing_files")
366         layout.operator("file.find_missing_files")
367
368
369 class TOPBAR_MT_file_previews(Menu):
370     bl_label = "Data Previews"
371
372     def draw(self, _context):
373         layout = self.layout
374
375         layout.operator("wm.previews_ensure")
376         layout.operator("wm.previews_batch_generate")
377
378         layout.separator()
379
380         layout.operator("wm.previews_clear")
381         layout.operator("wm.previews_batch_clear")
382
383
384 class TOPBAR_MT_render(Menu):
385     bl_label = "Render"
386
387     def draw(self, context):
388         layout = self.layout
389
390         rd = context.scene.render
391
392         layout.operator("render.render", text="Render Image", icon='RENDER_STILL').use_viewport = True
393         props = layout.operator("render.render", text="Render Animation", icon='RENDER_ANIMATION')
394         props.animation = True
395         props.use_viewport = True
396
397         layout.separator()
398
399         layout.operator("sound.mixdown", text="Render Audio...")
400
401         layout.separator()
402
403         layout.operator("render.view_show", text="View Render")
404         layout.operator("render.play_rendered_anim", text="View Animation")
405         layout.prop_menu_enum(rd, "display_mode", text="Display Mode")
406
407         layout.separator()
408
409         layout.prop(rd, "use_lock_interface", text="Lock Interface")
410
411
412 class TOPBAR_MT_edit(Menu):
413     bl_label = "Edit"
414
415     def draw(self, context):
416         layout = self.layout
417
418         layout.operator("ed.undo")
419         layout.operator("ed.redo")
420
421         layout.separator()
422
423         layout.operator("ed.undo_history", text="Undo History...")
424
425         layout.separator()
426
427         layout.operator("screen.repeat_last")
428         layout.operator("screen.repeat_history", text="Repeat History...")
429
430         layout.separator()
431
432         layout.operator("screen.redo_last", text="Adjust Last Operation...")
433
434         layout.separator()
435
436         layout.operator("wm.search_menu", text="Operator Search...", icon='VIEWZOOM')
437
438         layout.separator()
439
440         # Mainly to expose shortcut since this depends on the context.
441         props = layout.operator("wm.call_panel", text="Rename Active Item...")
442         props.name = "TOPBAR_PT_name"
443         props.keep_open = False
444
445         layout.separator()
446
447         # Should move elsewhere (impacts outliner & 3D view).
448         tool_settings = context.tool_settings
449         layout.prop(tool_settings, "lock_object_mode")
450
451         layout.separator()
452
453         layout.operator("screen.userpref_show", text="Preferences...", icon='PREFERENCES')
454
455
456 class TOPBAR_MT_window(Menu):
457     bl_label = "Window"
458
459     def draw(self, context):
460         import sys
461
462         layout = self.layout
463
464         layout.operator("wm.window_new")
465         layout.operator("wm.window_new_main")
466
467         layout.separator()
468
469         layout.operator("wm.window_fullscreen_toggle", icon='FULLSCREEN_ENTER')
470
471         layout.separator()
472
473         layout.operator("screen.workspace_cycle", text="Next Workspace").direction = 'NEXT'
474         layout.operator("screen.workspace_cycle", text="Previous Workspace").direction = 'PREV'
475
476         layout.separator()
477
478         layout.prop(context.screen, "show_statusbar")
479
480         layout.separator()
481
482         layout.operator("screen.screenshot")
483
484         if sys.platform[:3] == "win":
485             layout.separator()
486             layout.operator("wm.console_toggle", icon='CONSOLE')
487
488         if context.scene.render.use_multiview:
489             layout.separator()
490             layout.operator("wm.set_stereo_3d")
491
492
493 class TOPBAR_MT_help(Menu):
494     bl_label = "Help"
495
496     def draw(self, context):
497         # If 'url_prefill_from_blender' becomes slow it could be made into a separate operator
498         # to avoid constructing the bug report just to show this menu.
499         from bl_ui_utils.bug_report_url import url_prefill_from_blender
500
501         layout = self.layout
502
503         show_developer = context.preferences.view.show_developer_ui
504
505         layout.operator(
506             "wm.url_open", text="Manual", icon='HELP',
507         ).url = "https://docs.blender.org/manual/en/dev/"
508
509         layout.operator(
510             "wm.url_open", text="Report a Bug", icon='URL',
511         ).url = url_prefill_from_blender()
512
513         layout.separator()
514
515         layout.operator(
516             "wm.url_open", text="User Communities", icon='URL',
517         ).url = "https://www.blender.org/community/"
518         layout.operator(
519             "wm.url_open", text="Developer Community", icon='URL',
520         ).url = "https://www.blender.org/get-involved/developers/"
521
522         layout.separator()
523
524         layout.operator(
525             "wm.url_open", text="Blender Website", icon='URL',
526         ).url = "https://www.blender.org"
527         layout.operator(
528             "wm.url_open", text="Release Notes", icon='URL',
529         ).url = "https://www.blender.org/download/releases/%d-%d/" % bpy.app.version[:2]
530         layout.operator(
531             "wm.url_open", text="Credits", icon='URL',
532         ).url = "https://www.blender.org/about/credits/"
533
534         layout.separator()
535
536         layout.operator(
537             "wm.url_open", text="Blender Store", icon='URL',
538         ).url = "https://store.blender.org"
539         layout.operator(
540             "wm.url_open", text="Development Fund", icon='URL',
541         ).url = "https://fund.blender.org"
542         layout.operator(
543             "wm.url_open", text="Donate", icon='URL',
544         ).url = "https://www.blender.org/foundation/donation-payment/"
545
546         layout.separator()
547
548         if show_developer:
549             layout.operator(
550                 "wm.url_open", text="Python API Reference", icon='URL',
551             ).url = bpy.types.WM_OT_doc_view._prefix
552
553             layout.operator("wm.operator_cheat_sheet", icon='TEXT')
554
555         layout.operator("wm.sysinfo")
556
557         layout.separator()
558
559         layout.operator("wm.splash", icon='BLENDER')
560
561
562 class TOPBAR_MT_file_context_menu(Menu):
563     bl_label = "File Context Menu"
564
565     def draw(self, _context):
566         layout = self.layout
567
568         layout.operator_context = 'INVOKE_AREA'
569         layout.menu("TOPBAR_MT_file_new", text="New", icon='FILE_NEW')
570         layout.operator("wm.open_mainfile", text="Open...", icon='FILE_FOLDER')
571
572         layout.separator()
573
574         layout.operator("wm.link", text="Link...", icon='LINK_BLEND')
575         layout.operator("wm.append", text="Append...", icon='APPEND_BLEND')
576
577         layout.separator()
578
579         layout.menu("TOPBAR_MT_file_import", icon='IMPORT')
580         layout.menu("TOPBAR_MT_file_export", icon='EXPORT')
581
582         layout.separator()
583
584         layout.operator("screen.userpref_show", text="Preferences...", icon='PREFERENCES')
585
586
587 class TOPBAR_MT_workspace_menu(Menu):
588     bl_label = "Workspace"
589
590     def draw(self, _context):
591         layout = self.layout
592
593         layout.operator("workspace.duplicate", text="Duplicate", icon='DUPLICATE')
594         if len(bpy.data.workspaces) > 1:
595             layout.operator("workspace.delete", text="Delete", icon='REMOVE')
596
597         layout.separator()
598
599         layout.operator("workspace.reorder_to_front", text="Reorder to Front", icon='TRIA_LEFT_BAR')
600         layout.operator("workspace.reorder_to_back", text="Reorder to Back", icon='TRIA_RIGHT_BAR')
601
602         layout.separator()
603
604         # For key binding discoverability.
605         props = layout.operator("screen.workspace_cycle", text="Previous Workspace")
606         props.direction = 'PREV'
607         props = layout.operator("screen.workspace_cycle", text="Next Workspace")
608         props.direction = 'NEXT'
609
610
611 # Grease Pencil Object - Primitive curve
612 class TOPBAR_PT_gpencil_primitive(Panel):
613     bl_space_type = 'VIEW_3D'
614     bl_region_type = 'HEADER'
615     bl_label = "Primitives"
616
617     def draw(self, context):
618         settings = context.tool_settings.gpencil_sculpt
619
620         layout = self.layout
621         # Curve
622         layout.template_curve_mapping(settings, "thickness_primitive_curve", brush=True)
623
624
625 # Grease Pencil Fill
626 class TOPBAR_PT_gpencil_fill(Panel):
627     bl_space_type = 'VIEW_3D'
628     bl_region_type = 'HEADER'
629     bl_label = "Advanced"
630
631     def draw(self, context):
632         paint = context.tool_settings.gpencil_paint
633         brush = paint.brush
634         gp_settings = brush.gpencil_settings
635
636         layout = self.layout
637         # Fill
638         row = layout.row(align=True)
639         row.prop(gp_settings, "fill_factor", text="Resolution")
640         if gp_settings.fill_draw_mode != 'STROKE':
641             row = layout.row(align=True)
642             row.prop(gp_settings, "show_fill", text="Ignore Transparent Strokes")
643             row = layout.row(align=True)
644             row.prop(gp_settings, "fill_threshold", text="Threshold")
645
646
647 # Only a popover
648 class TOPBAR_PT_name(Panel):
649     bl_space_type = 'TOPBAR'  # dummy
650     bl_region_type = 'HEADER'
651     bl_label = "Rename Active Item"
652     bl_ui_units_x = 14
653
654     def draw(self, context):
655         layout = self.layout
656
657         # Edit first editable button in popup
658         def row_with_icon(layout, icon):
659             row = layout.row()
660             row.activate_init = True
661             row.label(icon=icon)
662             return row
663
664         mode = context.mode
665         scene = context.scene
666         space = context.space_data
667         space_type = None if (space is None) else space.type
668         found = False
669         if space_type == 'SEQUENCE_EDITOR':
670             layout.label(text="Sequence Strip Name")
671             item = getattr(scene.sequence_editor, "active_strip")
672             if item:
673                 row = row_with_icon(layout, 'SEQUENCE')
674                 row.prop(item, "name", text="")
675                 found = True
676         elif space_type == 'NODE_EDITOR':
677             layout.label(text="Node Label")
678             item = context.active_node
679             if item:
680                 row = row_with_icon(layout, 'NODE')
681                 row.prop(item, "label", text="")
682                 found = True
683         else:
684             if mode == 'POSE' or (mode == 'WEIGHT_PAINT' and context.pose_object):
685                 layout.label(text="Bone Name")
686                 item = context.active_pose_bone
687                 if item:
688                     row = row_with_icon(layout, 'BONE_DATA')
689                     row.prop(item, "name", text="")
690                     found = True
691             elif mode == 'EDIT_ARMATURE':
692                 layout.label(text="Bone Name")
693                 item = context.active_bone
694                 if item:
695                     row = row_with_icon(layout, 'BONE_DATA')
696                     row.prop(item, "name", text="")
697                     found = True
698             else:
699                 layout.label(text="Object Name")
700                 item = context.object
701                 if item:
702                     row = row_with_icon(layout, 'OBJECT_DATA')
703                     row.prop(item, "name", text="")
704                     found = True
705
706         if not found:
707             row = row_with_icon(layout, 'ERROR')
708             row.label(text="No active item")
709
710
711 classes = (
712     TOPBAR_HT_upper_bar,
713     TOPBAR_MT_file_context_menu,
714     TOPBAR_MT_workspace_menu,
715     TOPBAR_MT_editor_menus,
716     TOPBAR_MT_file,
717     TOPBAR_MT_file_new,
718     TOPBAR_MT_templates_more,
719     TOPBAR_MT_file_import,
720     TOPBAR_MT_file_export,
721     TOPBAR_MT_file_external_data,
722     TOPBAR_MT_file_previews,
723     TOPBAR_MT_edit,
724     TOPBAR_MT_render,
725     TOPBAR_MT_window,
726     TOPBAR_MT_help,
727     TOPBAR_PT_gpencil_layers,
728     TOPBAR_PT_gpencil_primitive,
729     TOPBAR_PT_gpencil_fill,
730     TOPBAR_PT_name,
731 )
732
733 if __name__ == "__main__":  # only for live edit.
734     from bpy.utils import register_class
735     for cls in classes:
736         register_class(cls)