Cleanup: use staticmethod where appropriate
[blender.git] / release / scripts / startup / bl_ui / space_dopesheet.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
21 import bpy
22 from bpy.types import (
23     Header,
24     Menu,
25     Panel,
26 )
27
28 #######################################
29 # DopeSheet Filtering - Header Buttons
30
31 # used for DopeSheet, NLA, and Graph Editors
32
33
34 def dopesheet_filter(layout, context, generic_filters_only=False):
35     dopesheet = context.space_data.dopesheet
36     is_nla = context.area.type == 'NLA_EDITOR'
37
38     row = layout.row(align=True)
39     row.prop(dopesheet, "show_only_selected", text="")
40     row.prop(dopesheet, "show_hidden", text="")
41
42     if is_nla:
43         row.prop(dopesheet, "show_missing_nla", text="")
44     else:  # graph and dopesheet editors - F-Curves and drivers only
45         row.prop(dopesheet, "show_only_errors", text="")
46
47     if not generic_filters_only:
48         if bpy.data.collections:
49             row = layout.row(align=True)
50             row.prop(dopesheet, "filter_collection", text="")
51
52     if not is_nla:
53         row = layout.row(align=True)
54         row.prop(dopesheet, "filter_fcurve_name", text="")
55     else:
56         row = layout.row(align=True)
57         row.prop(dopesheet, "filter_text", text="")
58
59 #######################################
60 # Dopesheet Filtering Popovers
61
62 # Generic Layout - Used as base for filtering popovers used in all animation editors
63 # Used for DopeSheet, NLA, and Graph Editors
64
65
66 class DopesheetFilterPopoverBase:
67     bl_region_type = 'HEADER'
68     bl_label = "Filters"
69
70     # Generic = Affects all datatypes
71     # XXX: Perhaps we want these to stay in the header instead, for easy/fast access
72     @classmethod
73     def draw_generic_filters(cls, context, layout):
74         dopesheet = context.space_data.dopesheet
75         is_nla = context.area.type == 'NLA_EDITOR'
76
77         col = layout.column(align=True)
78         col.prop(dopesheet, "show_only_selected", icon='NONE')
79         col.prop(dopesheet, "show_hidden", icon='NONE')
80
81         if is_nla:
82             col.prop(dopesheet, "show_missing_nla", icon='NONE')
83         else:  # graph and dopesheet editors - F-Curves and drivers only
84             col.prop(dopesheet, "show_only_errors", icon='NONE')
85
86     # Name/Membership Filters
87     # XXX: Perhaps these should just stay in the headers (exclusively)?
88     @classmethod
89     def draw_search_filters(cls, context, layout, generic_filters_only=False):
90         dopesheet = context.space_data.dopesheet
91         is_nla = context.area.type == 'NLA_EDITOR'
92
93         col = layout.column(align=True)
94         col.label(text="With Name:")
95         if not is_nla:
96             row = col.row(align=True)
97             row.prop(dopesheet, "filter_fcurve_name", text="")
98         else:
99             row = col.row(align=True)
100             row.prop(dopesheet, "filter_text", text="")
101
102         if (not generic_filters_only) and (bpy.data.collections):
103             col = layout.column(align=True)
104             col.label(text="In Collection:")
105             col.prop(dopesheet, "filter_collection", text="")
106
107     # Standard = Present in all panels
108     @classmethod
109     def draw_standard_filters(cls, context, layout):
110         dopesheet = context.space_data.dopesheet
111
112         # datablock filters
113         layout.label(text="Filter by Type:")
114         flow = layout.grid_flow(row_major=True, columns=2, even_rows=False, align=False)
115
116         flow.prop(dopesheet, "show_scenes", text="Scenes")
117         flow.prop(dopesheet, "show_nodes", text="Node Trees")
118
119         # object types
120         if bpy.data.armatures:
121             flow.prop(dopesheet, "show_armatures", text="Armatures")
122         if bpy.data.cameras:
123             flow.prop(dopesheet, "show_cameras", text="Cameras")
124         if bpy.data.grease_pencils:
125             flow.prop(dopesheet, "show_gpencil", text="Grease Pencil Objects")
126         if bpy.data.lights:
127             flow.prop(dopesheet, "show_lights", text="Lights")
128         if bpy.data.meshes:
129             flow.prop(dopesheet, "show_meshes", text="Meshes")
130         if bpy.data.curves:
131             flow.prop(dopesheet, "show_curves", text="Curves")
132         if bpy.data.lattices:
133             flow.prop(dopesheet, "show_lattices", text="Lattices")
134         if bpy.data.metaballs:
135             flow.prop(dopesheet, "show_metaballs", text="Metaballs")
136
137         # data types
138         flow.prop(dopesheet, "show_worlds", text="Worlds")
139         if bpy.data.particles:
140             flow.prop(dopesheet, "show_particles", text="Particles")
141         if bpy.data.linestyles:
142             flow.prop(dopesheet, "show_linestyles", text="Line Styles")
143         if bpy.data.speakers:
144             flow.prop(dopesheet, "show_speakers", text="Speakers")
145         if bpy.data.materials:
146             flow.prop(dopesheet, "show_materials", text="Materials")
147         if bpy.data.textures:
148             flow.prop(dopesheet, "show_textures", text="Textures")
149         if bpy.data.shape_keys:
150             flow.prop(dopesheet, "show_shapekeys", text="Shape Keys")
151         if bpy.data.cache_files:
152             flow.prop(dopesheet, "show_cache_files", text="Cache Files")
153
154         layout.separator()
155
156         # Object Data Filters
157
158         # TODO: Add per-channel/axis convenience toggles?
159         split = layout.split()
160
161         col = split.column()
162         col.prop(dopesheet, "show_transforms", text="Transforms")
163
164         col = split.column()
165         col.prop(dopesheet, "show_modifiers", text="Modifiers")
166
167         layout.separator()
168
169         # performance-related options (users will mostly have these enabled)
170         col = layout.column(align=True)
171         col.label(text="Options:")
172         col.prop(dopesheet, "use_datablock_sort", icon='NONE')
173
174
175 # Popover for Dopesheet Editor(s) - Dopesheet, Action, Shapekey, GPencil, Mask, etc.
176 class DOPESHEET_PT_filters(DopesheetFilterPopoverBase, Panel):
177     bl_space_type = 'DOPESHEET_EDITOR'
178     bl_region_type = 'HEADER'
179     bl_label = "Filters"
180
181     def draw(self, context):
182         layout = self.layout
183
184         dopesheet = context.space_data.dopesheet
185         ds_mode = context.space_data.mode
186
187         layout.prop(dopesheet, "show_summary", text="Summary")
188
189         DopesheetFilterPopoverBase.draw_generic_filters(context, layout)
190
191         if ds_mode in {'DOPESHEET', 'ACTION', 'GPENCIL'}:
192             layout.separator()
193             generic_filters_only = ds_mode != 'DOPESHEET'
194             DopesheetFilterPopoverBase.draw_search_filters(context, layout,
195                                                            generic_filters_only=generic_filters_only)
196
197         if ds_mode == 'DOPESHEET':
198             layout.separator()
199             DopesheetFilterPopoverBase.draw_standard_filters(context, layout)
200
201
202 #######################################
203 # DopeSheet Editor - General/Standard UI
204
205 class DOPESHEET_HT_header(Header):
206     bl_space_type = 'DOPESHEET_EDITOR'
207
208     def draw(self, context):
209         layout = self.layout
210
211         st = context.space_data
212
213         row = layout.row(align=True)
214         row.template_header()
215
216         if st.mode == 'TIMELINE':
217             from .space_time import (
218                 TIME_MT_editor_menus,
219                 TIME_HT_editor_buttons,
220             )
221             TIME_MT_editor_menus.draw_collapsible(context, layout)
222             TIME_HT_editor_buttons.draw_header(context, layout)
223         else:
224             layout.prop(st, "ui_mode", text="")
225
226             DOPESHEET_MT_editor_menus.draw_collapsible(context, layout)
227             DOPESHEET_HT_editor_buttons.draw_header(context, layout)
228
229
230 # Header for "normal" dopesheet editor modes (e.g. Dope Sheet, Action, Shape Keys, etc.)
231 class DOPESHEET_HT_editor_buttons(Header):
232     bl_idname = "DOPESHEET_HT_editor_buttons"
233     bl_space_type = 'DOPESHEET_EDITOR'
234     bl_label = ""
235
236     def draw(self, context):
237         pass
238
239     @staticmethod
240     def draw_header(context, layout):
241         st = context.space_data
242         tool_settings = context.tool_settings
243
244         if st.mode in {'ACTION', 'SHAPEKEY'}:
245             # TODO: These buttons need some tidying up -
246             # Probably by using a popover, and bypassing the template_id() here
247             row = layout.row(align=True)
248             row.operator("action.layer_prev", text="", icon='TRIA_DOWN')
249             row.operator("action.layer_next", text="", icon='TRIA_UP')
250
251             row = layout.row(align=True)
252             row.operator("action.push_down", text="Push Down", icon='NLA_PUSHDOWN')
253             row.operator("action.stash", text="Stash", icon='FREEZE')
254
255             layout.separator_spacer()
256
257             layout.template_ID(st, "action", new="action.new", unlink="action.unlink")
258
259         layout.separator_spacer()
260
261         if st.mode == 'DOPESHEET':
262             dopesheet_filter(layout, context)
263         elif st.mode == 'ACTION':
264             # 'generic_filters_only' limits the options to only the relevant 'generic' subset of
265             # filters which will work here and are useful (especially for character animation)
266             dopesheet_filter(layout, context, generic_filters_only=True)
267         elif st.mode == 'GPENCIL':
268             row = layout.row(align=True)
269             row.prop(st.dopesheet, "show_gpencil_3d_only", text="Active Only")
270
271             if st.dopesheet.show_gpencil_3d_only:
272                 row = layout.row(align=True)
273                 row.prop(st.dopesheet, "show_only_selected", text="")
274                 row.prop(st.dopesheet, "show_hidden", text="")
275
276             row = layout.row(align=True)
277             row.prop(st.dopesheet, "filter_text", text="")
278
279         layout.popover(
280             panel="DOPESHEET_PT_filters",
281             text="",
282             icon='FILTER',
283         )
284
285         # Grease Pencil mode doesn't need snapping, as it's frame-aligned only
286         if st.mode != 'GPENCIL':
287             layout.prop(st, "auto_snap", text="")
288
289         row = layout.row(align=True)
290         row.prop(tool_settings, "use_proportional_action", text="", icon_only=True)
291         sub = row.row(align=True)
292         sub.active = tool_settings.use_proportional_action
293         sub.prop(tool_settings, "proportional_edit_falloff", text="", icon_only=True)
294
295
296 class DOPESHEET_MT_editor_menus(Menu):
297     bl_idname = "DOPESHEET_MT_editor_menus"
298     bl_label = ""
299
300     def draw(self, context):
301         layout = self.layout
302         st = context.space_data
303
304         layout.menu("DOPESHEET_MT_view")
305         layout.menu("DOPESHEET_MT_select")
306         layout.menu("DOPESHEET_MT_marker")
307
308         if st.mode == 'DOPESHEET' or (st.mode == 'ACTION' and st.action is not None):
309             layout.menu("DOPESHEET_MT_channel")
310         elif st.mode == 'GPENCIL':
311             layout.menu("DOPESHEET_MT_gpencil_channel")
312
313         if st.mode != 'GPENCIL':
314             layout.menu("DOPESHEET_MT_key")
315         else:
316             layout.menu("DOPESHEET_MT_gpencil_frame")
317
318
319 class DOPESHEET_MT_view(Menu):
320     bl_label = "View"
321
322     def draw(self, context):
323         layout = self.layout
324
325         st = context.space_data
326
327         layout.prop(st, "show_region_ui")
328
329         layout.separator()
330
331         layout.prop(st.dopesheet, "use_multi_word_filter", text="Multi-word Match Search")
332
333         layout.separator()
334
335         layout.prop(st, "use_realtime_update")
336         layout.prop(st, "show_frame_indicator")
337         layout.prop(st, "show_sliders")
338         layout.prop(st, "show_group_colors")
339         layout.prop(st, "show_interpolation")
340         layout.prop(st, "show_extremes")
341         layout.prop(st, "show_marker_lines")
342         layout.prop(st, "use_auto_merge_keyframes")
343
344         layout.prop(st, "show_seconds")
345         layout.prop(st, "show_locked_time")
346
347         layout.separator()
348         layout.operator("anim.previewrange_set")
349         layout.operator("anim.previewrange_clear")
350         layout.operator("action.previewrange_set")
351
352         layout.separator()
353         layout.operator("action.view_all")
354         layout.operator("action.view_selected")
355         layout.operator("action.view_frame")
356
357         # Add this to show key-binding (reverse action in dope-sheet).
358         layout.separator()
359         props = layout.operator("wm.context_set_enum", text="Toggle Graph Editor", icon='GRAPH')
360         props.data_path = "area.type"
361         props.value = 'GRAPH_EDITOR'
362
363         layout.separator()
364         layout.menu("INFO_MT_area")
365
366
367 class DOPESHEET_MT_select(Menu):
368     bl_label = "Select"
369
370     def draw(self, context):
371         layout = self.layout
372
373         layout.operator("action.select_all", text="All").action = 'SELECT'
374         layout.operator("action.select_all", text="None").action = 'DESELECT'
375         layout.operator("action.select_all", text="Invert").action = 'INVERT'
376
377         layout.separator()
378         layout.operator("action.select_box").axis_range = False
379         layout.operator("action.select_box", text="Border Axis Range").axis_range = True
380
381         layout.operator("action.select_circle")
382
383         layout.separator()
384         layout.operator("action.select_column", text="Columns on Selected Keys").mode = 'KEYS'
385         layout.operator("action.select_column", text="Column on Current Frame").mode = 'CFRA'
386
387         layout.operator("action.select_column", text="Columns on Selected Markers").mode = 'MARKERS_COLUMN'
388         layout.operator("action.select_column", text="Between Selected Markers").mode = 'MARKERS_BETWEEN'
389
390         layout.separator()
391         props = layout.operator("action.select_leftright", text="Before Current Frame")
392         props.extend = False
393         props.mode = 'LEFT'
394         props = layout.operator("action.select_leftright", text="After Current Frame")
395         props.extend = False
396         props.mode = 'RIGHT'
397
398         # FIXME: grease pencil mode isn't supported for these yet, so skip for that mode only
399         if context.space_data.mode != 'GPENCIL':
400             layout.separator()
401             layout.operator("action.select_more")
402             layout.operator("action.select_less")
403
404             layout.separator()
405             layout.operator("action.select_linked")
406
407
408 class DOPESHEET_MT_marker(Menu):
409     bl_label = "Marker"
410
411     def draw(self, context):
412         layout = self.layout
413
414         from .space_time import marker_menu_generic
415         marker_menu_generic(layout, context)
416
417         st = context.space_data
418
419         if st.mode in {'ACTION', 'SHAPEKEY'} and st.action:
420             layout.separator()
421             layout.prop(st, "show_pose_markers")
422
423             if st.show_pose_markers is False:
424                 layout.operator("action.markers_make_local")
425
426         layout.prop(st, "use_marker_sync")
427
428 #######################################
429 # Keyframe Editing
430
431
432 class DOPESHEET_MT_channel(Menu):
433     bl_label = "Channel"
434
435     def draw(self, _context):
436         layout = self.layout
437
438         layout.operator_context = 'INVOKE_REGION_CHANNELS'
439
440         layout.operator("anim.channels_delete")
441
442         layout.separator()
443         layout.operator("anim.channels_group")
444         layout.operator("anim.channels_ungroup")
445
446         layout.separator()
447         layout.operator_menu_enum("anim.channels_setting_toggle", "type")
448         layout.operator_menu_enum("anim.channels_setting_enable", "type")
449         layout.operator_menu_enum("anim.channels_setting_disable", "type")
450
451         layout.separator()
452         layout.operator("anim.channels_editable_toggle")
453         layout.operator_menu_enum("action.extrapolation_type", "type", text="Extrapolation Mode")
454
455         layout.separator()
456         layout.operator("anim.channels_expand")
457         layout.operator("anim.channels_collapse")
458
459         layout.separator()
460         layout.operator_menu_enum("anim.channels_move", "direction", text="Move...")
461
462         layout.separator()
463         layout.operator("anim.channels_fcurves_enable")
464
465
466 class DOPESHEET_MT_key(Menu):
467     bl_label = "Key"
468
469     def draw(self, _context):
470         layout = self.layout
471
472         layout.menu("DOPESHEET_MT_key_transform", text="Transform")
473
474         layout.operator_menu_enum("action.snap", "type", text="Snap")
475         layout.operator_menu_enum("action.mirror", "type", text="Mirror")
476
477         layout.separator()
478         layout.operator("action.keyframe_insert")
479
480         layout.separator()
481         layout.operator("action.frame_jump")
482
483         layout.separator()
484         layout.operator("action.copy")
485         layout.operator("action.paste")
486         layout.operator("action.paste", text="Paste Flipped").flipped = True
487         layout.operator("action.duplicate_move")
488         layout.operator("action.delete")
489
490         layout.separator()
491         layout.operator_menu_enum("action.keyframe_type", "type", text="Keyframe Type")
492         layout.operator_menu_enum("action.handle_type", "type", text="Handle Type")
493         layout.operator_menu_enum("action.interpolation_type", "type", text="Interpolation Mode")
494
495         layout.separator()
496         layout.operator("action.clean").channels = False
497         layout.operator("action.clean", text="Clean Channels").channels = True
498         layout.operator("action.sample")
499
500
501 class DOPESHEET_MT_key_transform(Menu):
502     bl_label = "Transform"
503
504     def draw(self, _context):
505         layout = self.layout
506
507         layout.operator("transform.transform", text="Move").mode = 'TIME_TRANSLATE'
508         layout.operator("transform.transform", text="Extend").mode = 'TIME_EXTEND'
509         layout.operator("transform.transform", text="Slide").mode = 'TIME_SLIDE'
510         layout.operator("transform.transform", text="Scale").mode = 'TIME_SCALE'
511
512
513 #######################################
514 # Grease Pencil Editing
515
516 class DOPESHEET_MT_gpencil_channel(Menu):
517     bl_label = "Channel"
518
519     def draw(self, _context):
520         layout = self.layout
521
522         layout.operator_context = 'INVOKE_REGION_CHANNELS'
523
524         layout.operator("anim.channels_delete")
525
526         layout.separator()
527         layout.operator("anim.channels_setting_toggle")
528         layout.operator("anim.channels_setting_enable")
529         layout.operator("anim.channels_setting_disable")
530
531         layout.separator()
532         layout.operator("anim.channels_editable_toggle")
533
534         # XXX: to be enabled when these are ready for use!
535         # layout.separator()
536         # layout.operator("anim.channels_expand")
537         # layout.operator("anim.channels_collapse")
538
539         # layout.separator()
540         #layout.operator_menu_enum("anim.channels_move", "direction", text="Move...")
541
542
543 class DOPESHEET_MT_gpencil_frame(Menu):
544     bl_label = "Frame"
545
546     def draw(self, _context):
547         layout = self.layout
548
549         layout.menu("DOPESHEET_MT_key_transform", text="Transform")
550         layout.operator_menu_enum("action.snap", "type", text="Snap")
551         layout.operator_menu_enum("action.mirror", "type", text="Mirror")
552
553         layout.separator()
554         layout.operator("action.duplicate")
555         layout.operator("action.delete")
556
557         layout.separator()
558         layout.operator("action.keyframe_type")
559
560         # layout.separator()
561         # layout.operator("action.copy")
562         # layout.operator("action.paste")
563
564
565 class DOPESHEET_MT_delete(Menu):
566     bl_label = "Delete"
567
568     def draw(self, _context):
569         layout = self.layout
570
571         layout.operator("action.delete")
572
573         layout.separator()
574
575         layout.operator("action.clean").channels = False
576         layout.operator("action.clean", text="Clean Channels").channels = True
577
578
579 class DOPESHEET_MT_context_menu(Menu):
580     bl_label = "Dope Sheet Context Menu"
581
582     def draw(self, _context):
583         layout = self.layout
584
585         layout.operator("action.copy", text="Copy")
586         layout.operator("action.paste", text="Paste")
587         layout.operator("action.paste", text="Paste Flipped").flipped = True
588
589         layout.separator()
590
591         layout.operator_menu_enum("action.handle_type", "type", text="Handle Type")
592         layout.operator_menu_enum("action.interpolation_type", "type", text="Interpolation Mode")
593         layout.operator_menu_enum("action.easing_type", "type", text="Easing Type")
594
595         layout.separator()
596
597         layout.operator("action.keyframe_insert").type = 'SEL'
598         layout.operator("action.duplicate_move")
599         layout.operator("action.delete")
600
601         layout.separator()
602
603         layout.operator_menu_enum("action.mirror", "type", text="Mirror")
604         layout.operator_menu_enum("action.snap", "type", text="Snap")
605
606
607 class DOPESHEET_MT_channel_context_menu(Menu):
608     bl_label = "Dope Sheet Channel Context Menu"
609
610     def draw(self, _context):
611         layout = self.layout
612
613         layout.operator("anim.channels_setting_enable", text="Mute Channels").type = 'MUTE'
614         layout.operator("anim.channels_setting_disable", text="Unmute Channels").type = 'MUTE'
615         layout.separator()
616         layout.operator("anim.channels_setting_enable", text="Protect Channels").type = 'PROTECT'
617         layout.operator("anim.channels_setting_disable", text="Unprotect Channels").type = 'PROTECT'
618
619         layout.separator()
620         layout.operator("anim.channels_group")
621         layout.operator("anim.channels_ungroup")
622
623         layout.separator()
624         layout.operator("anim.channels_editable_toggle")
625         layout.operator_menu_enum("action.extrapolation_type", "type", text="Extrapolation Mode")
626
627         layout.separator()
628         layout.operator("anim.channels_expand")
629         layout.operator("anim.channels_collapse")
630
631         layout.separator()
632         layout.operator_menu_enum("anim.channels_move", "direction", text="Move...")
633
634         layout.separator()
635
636         layout.operator("anim.channels_delete")
637
638
639 class DOPESHEET_MT_snap_pie(Menu):
640     bl_label = "Snap"
641
642     def draw(self, _context):
643         layout = self.layout
644         pie = layout.menu_pie()
645
646         pie.operator("action.snap", text="Current Frame").type = 'CFRA'
647         pie.operator("action.snap", text="Nearest Frame").type = 'NEAREST_FRAME'
648         pie.operator("action.snap", text="Nearest Second").type = 'NEAREST_SECOND'
649         pie.operator("action.snap", text="Nearest Marker").type = 'NEAREST_MARKER'
650
651
652 classes = (
653     DOPESHEET_HT_header,
654     DOPESHEET_HT_editor_buttons,
655     DOPESHEET_MT_editor_menus,
656     DOPESHEET_MT_view,
657     DOPESHEET_MT_select,
658     DOPESHEET_MT_marker,
659     DOPESHEET_MT_channel,
660     DOPESHEET_MT_key,
661     DOPESHEET_MT_key_transform,
662     DOPESHEET_MT_gpencil_channel,
663     DOPESHEET_MT_gpencil_frame,
664     DOPESHEET_MT_delete,
665     DOPESHEET_MT_context_menu,
666     DOPESHEET_MT_channel_context_menu,
667     DOPESHEET_MT_snap_pie,
668     DOPESHEET_PT_filters,
669 )
670
671 if __name__ == "__main__":  # only for live edit.
672     from bpy.utils import register_class
673     for cls in classes:
674         register_class(cls)