-
Notifications
You must be signed in to change notification settings - Fork 11
/
fades.py
1120 lines (983 loc) · 49.3 KB
/
fades.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import bpy
import gpu
from gpu_extras.batch import batch_for_shader
import math
from . import vseqf
from . import timeline
def fix_fades(context, sequence, old_start, old_end):
fix_fade_in(context, sequence, old_start)
fix_fade_out(context, sequence, old_end)
def fix_fade_in(context, sequence, old_start):
if old_start != sequence.frame_final_start:
# fix fade in
fade_curve = get_fade_curve(context, sequence, create=False)
if fade_curve:
fade_in = fades(fade_curve, sequence, 'detect', 'in', fade_low_point_frame=old_start)
if fade_in > 0:
fades(fade_curve, sequence, 'set', 'in', fade_length=fade_in)
def fix_fade_out(context, sequence, old_end):
if old_end != sequence.frame_final_end:
# fix fade out
fade_curve = get_fade_curve(context, sequence, create=False)
if fade_curve:
fade_out = fades(fade_curve, sequence, 'detect', 'out', fade_low_point_frame=old_end)
if fade_out > 0:
fades(fade_curve, sequence, 'set', 'out', fade_length=fade_out)
def find_crossfade(sequences, first_sequence, second_sequence):
for sequence in sequences:
if hasattr(sequence, 'input_1') and hasattr(sequence, 'input_2'):
if (sequence.input_1 == first_sequence and sequence.input_2 == second_sequence) or (sequence.input_2 == first_sequence and sequence.input_1 == second_sequence):
return sequence
return False
def vseqf_crossfade(first_sequence, second_sequence):
"""Add a crossfade between two sequences, the transition type is determined by the vseqf variable 'transition'
Arguments:
first_sequence: VSE Sequence object being transitioned from
second_sequence: VSE Sequence object being transitioned to"""
transition_type = bpy.context.scene.vseqf.transition
frame_start = first_sequence.frame_final_end
frame_end = second_sequence.frame_final_start
channel = first_sequence.channel
while timeline.sequencer_area_filled(frame_start, frame_end, channel, channel, []):
channel = channel + 1
bpy.context.scene.sequence_editor.sequences.new_effect(name=transition_type, type=transition_type, channel=channel, frame_start=frame_start, frame_end=frame_end, seq1=first_sequence, seq2=second_sequence)
def get_fade_curve(context, sequence, create=False):
#Returns the fade curve for a given sequence. If create is True, a curve will always be returned, if False, None will be returned if no curve is found.
if sequence.type == 'SOUND':
fade_variable = 'volume'
else:
fade_variable = 'blend_alpha'
#Search through all curves and find the fade curve
animation_data = context.scene.animation_data
if not animation_data:
if create:
context.scene.animation_data_create()
animation_data = context.scene.animation_data
else:
return None
action = animation_data.action
if not action:
if create:
action = bpy.data.actions.new(sequence.name+'Action')
animation_data.action = action
else:
return None
all_curves = action.fcurves
fade_curve = None #curve for the fades
for curve in all_curves:
if curve.data_path == 'sequence_editor.sequences_all["'+sequence.name+'"].'+fade_variable:
#keyframes found
fade_curve = curve
break
#Create curve if needed
if fade_curve is None and create:
fade_curve = all_curves.new(data_path=sequence.path_from_id(fade_variable))
#add a single keyframe to prevent blender from making the waveform invisible (bug)
if sequence.type == 'SOUND':
value = sequence.volume
else:
value = sequence.blend_alpha
fade_curve.keyframe_points.add(1)
point = fade_curve.keyframe_points[0]
point.co = (sequence.frame_final_start, value)
return fade_curve
def fades(fade_curve, sequence, mode, direction, fade_length=0, fade_low_point_frame=False):
"""Detects, creates, and edits fadein and fadeout for sequences.
Arguments:
fade_curve: Curve to detect or adjust fades on
sequence: VSE Sequence object that will be operated on
mode: String, determines the operation that will be done
detect: determines the fade length set to the sequence
set: sets a desired fade length to the sequence
direction: String, determines if the function works with fadein or fadeout
in: fadein is operated on
out: fadeout is operated on
fade_length: Integer, optional value used only when setting fade lengths
fade_low_point_frame: Integer, optional value used for detecting a fade at a point other than at the edge of the sequence"""
if not fade_curve:
return 0
if sequence.type == 'SOUND':
fade_variable = 'volume'
else:
fade_variable = 'blend_alpha'
fade_low_point = False #keyframe that the fade reaches minimum value at
fade_high_point = False #keyframe that the fade starts maximum value at
if direction == 'in':
if not fade_low_point_frame:
fade_low_point_frame = sequence.frame_final_start
else:
if not fade_low_point_frame:
fade_low_point_frame = sequence.frame_final_end
fade_length = -fade_length
fade_high_point_frame = fade_low_point_frame + fade_length
#Detect fades or add if set mode
try:
fade_keyframes = fade_curve.keyframe_points
except:
return 0
if len(fade_keyframes) == 0:
#no keyframes found, create them if instructed to do so
if mode == 'set':
fade_max_value = getattr(sequence, fade_variable)
set_fade(fade_curve, direction, fade_low_point_frame, fade_high_point_frame, fade_max_value)
else:
return 0
elif len(fade_keyframes) == 1:
#only one keyframe, use y value of keyframe as the max value for a new fade
if mode == 'set':
#determine fade_max_value from value at one keyframe
fade_max_value = fade_keyframes[0].co[1]
if fade_max_value == 0:
fade_max_value = 1
#add new fade
set_fade(fade_curve, direction, fade_low_point_frame, fade_high_point_frame, fade_max_value)
else:
return 0
elif len(fade_keyframes) > 1:
#at least 2 keyframes, there may be a fade already
if direction == 'in':
fade_low_point = fade_keyframes[0]
fade_high_point = fade_keyframes[1]
elif direction == 'out':
fade_low_point = fade_keyframes[-1]
fade_high_point = fade_keyframes[-2]
#check to see if the fade points are valid
if fade_low_point.co[1] == 0:
#opacity is 0, assume there is a fade
if fade_low_point.co[0] == fade_low_point_frame:
#fade low point is in the correct location
if fade_high_point.co[1] > fade_low_point.co[1]:
#both fade points are valid
if mode == 'set':
fade_max_value = fade_high_point.co[1]
set_fade(fade_curve, direction, fade_low_point_frame, fade_high_point_frame, fade_max_value, fade_low_point=fade_low_point, fade_high_point=fade_high_point)
return fade_length
else:
#fade detected!
return abs(fade_high_point.co[0] - fade_low_point.co[0])
else:
#fade high point is not valid, low point is tho
if mode == 'set':
fade_max_value = fade_curve.evaluate(fade_high_point_frame)
set_fade(fade_curve, direction, fade_low_point_frame, fade_high_point_frame, fade_max_value, fade_low_point=fade_low_point)
return fade_length
else:
return 0
else:
#fade low point is not in the correct location
if mode == 'set':
#check fade high point
if fade_high_point.co[1] > fade_low_point.co[1]:
#fade exists, but is not set up properly
fade_max_value = fade_high_point.co[1]
set_fade(fade_curve, direction, fade_low_point_frame, fade_high_point_frame, fade_max_value, fade_low_point=fade_low_point, fade_high_point=fade_high_point)
return fade_length
else:
#no valid fade high point
fade_max_value = fade_curve.evaluate(fade_high_point_frame)
set_fade(fade_curve, direction, fade_low_point_frame, fade_high_point_frame, fade_max_value, fade_low_point=fade_low_point)
return fade_length
else:
return 0
else:
#no valid fade detected, other keyframes are on the curve tho
if mode == 'set':
fade_max_value = fade_curve.evaluate(fade_high_point_frame)
set_fade(fade_curve, direction, fade_low_point_frame, fade_high_point_frame, fade_max_value)
return fade_length
else:
return 0
def set_fade(fade_curve, direction, fade_low_point_frame, fade_high_point_frame, fade_max_value, fade_low_point=None, fade_high_point=None):
"""Create or change a fadein or fadeout on a set of keyframes
Arguments:
fade_curve: the curve that the keyframes belong to
direction: String, determines if a fadein or fadeout will be set
'in': Set a fadein
'out': Set a fadeout
fade_low_point_frame: Integer, the frame at which the fade should be at its lowest value
fade_high_point_frame: Integer, the frame at which the fade should be at its highest value
fade_max_value: Float, the y value for the high point of the fade
fade_low_point: Optional, a keyframe point for the low point of the fade curve that should be moved, instead of creating a new one
fade_high_point: Optional, a keyframe point for the high point of the fade curve that should be moved, instead of creating a new one"""
fade_keyframes = fade_curve.keyframe_points
#check if any keyframe points other than the fade high and low points are in the fade area, delete them if needed
for keyframe in fade_keyframes:
if direction == 'in':
if (keyframe.co[0] < fade_high_point_frame) and (keyframe.co[0] > fade_low_point_frame):
if (keyframe != fade_low_point) and (keyframe != fade_high_point):
try:
fade_keyframes.remove(keyframe)
except:
pass
if direction == 'out':
if (keyframe.co[0] > fade_high_point_frame) and (keyframe.co[0] < fade_low_point_frame):
if (keyframe != fade_low_point) and (keyframe != fade_high_point):
try:
fade_keyframes.remove(keyframe)
except:
pass
fade_length = abs(fade_high_point_frame - fade_low_point_frame)
handle_offset = fade_length * .38
if fade_length == 0:
#remove fade
if direction == 'in' and fade_high_point:
fade_keyframes.remove(fade_high_point)
if fade_low_point is not None:
fade_keyframes.remove(fade_low_point)
if direction == 'out' and fade_high_point:
fade_keyframes.remove(fade_high_point)
if len(fade_keyframes) == 0:
#curve is empty, remove it
try:
bpy.context.scene.animation_data.action.fcurves.remove(fade_curve)
except:
pass
return
if fade_high_point:
#move fade high point to where it should be
fade_high_point.co = (fade_high_point_frame, fade_max_value)
fade_high_point.handle_left = (fade_high_point_frame - handle_offset, fade_max_value)
fade_high_point.handle_right = (fade_high_point_frame + handle_offset, fade_max_value)
else:
#create new fade high point
fade_keyframes.insert(frame=fade_high_point_frame, value=fade_max_value)
if fade_low_point:
#move fade low point to where it should be
fade_low_point.co = (fade_low_point_frame, 0)
fade_low_point.handle_left = (fade_low_point_frame - handle_offset, 0)
fade_low_point.handle_right = (fade_low_point_frame + handle_offset, 0)
else:
#create new fade low point
fade_keyframes.insert(frame=fade_low_point_frame, value=0)
def fade_operator_draw(self, context):
#Draw current fade info overlays
region = context.region
view = region.view2d
for data in self.strip_data:
sequence = data['sequence']
fade_in = data['fade_in']
fade_out = data['fade_out']
channel_buffer = 0.05
channel_bottom = sequence.channel + channel_buffer
channel_top = sequence.channel + 1 - channel_buffer
if fade_in > 0:
strip_left, strip_bottom = view.view_to_region(sequence.frame_final_start, channel_bottom, clip=False)
fade_in_loc, strip_top = view.view_to_region(sequence.frame_final_start + fade_in, channel_top, clip=False)
vseqf.draw_line(strip_left, strip_bottom, fade_in_loc, strip_top, color=(.8, .2, .2, 1))
vseqf.draw_text(fade_in_loc, strip_top - 12, 11, str(int(fade_in)), color=(1, 1, 1, 1))
if fade_out > 0:
strip_right, strip_bottom = view.view_to_region(sequence.frame_final_end, channel_bottom, clip=False)
fade_out_loc, strip_top = view.view_to_region(sequence.frame_final_end - fade_out, channel_top, clip=False)
vseqf.draw_line(strip_right, strip_bottom, fade_out_loc, strip_top, color=(.8, .2, .2, 1))
vseqf.draw_text(fade_out_loc, strip_top - 12, 11, str(int(fade_out)), justify='right', color=(1, 1, 1, 1))
def volume_operator_draw(self, context):
coords = []
keyframes = self.curve.keyframe_points
last_coords = None
for keyframe in keyframes:
point = keyframe.co
ypos = self.active_bottom + (point[1] * self.channel_px)
xpos = self.active_left + ((point[0] - self.active_strip.frame_final_start) * self.frame_px)
current_coords = (xpos, ypos)
if point[0] < self.active_frame_start:
last_coords = current_coords
continue
if last_coords is not None:
coords.append(last_coords)
coords.append(current_coords)
last_coords = current_coords
if point[0] > self.active_frame_end:
break
shader = gpu.shader.from_builtin('UNIFORM_COLOR')
batch = batch_for_shader(shader, 'LINES', {'pos': coords})
shader.bind()
shader.uniform_float('color', (1, .5, .5, .5))
batch.draw(shader)
class VSEQFModalVolumeDraw(bpy.types.Operator):
bl_idname = 'vseqf.volume_draw'
bl_label = "Draw volume keyframes directly on sound strips in the VSE"
active_strip = None
curve = None
channel_px = 1
frame_px = 1
active_left = 0
active_right = 0
active_bottom = 0
active_top = 0
active_frame_start = 0
active_frame_end = 0
mode = 'ADD'
last_press = ''
last_added = None
def remove_draw_handler(self, context):
bpy.types.SpaceSequenceEditor.draw_handler_remove(self._handle, 'WINDOW')
context.area.header_text_set(None)
context.workspace.status_text_set(None)
def update_areas(self, context):
for area in context.screen.areas:
if area.type in ['GRAPH_EDITOR', 'SEQUENCE_EDITOR']:
area.tag_redraw()
def reset_curve(self):
keyframes = self.curve.keyframe_points
for keyframe in reversed(keyframes):
keyframes.remove(keyframe)
self.active_strip.volume = 1
def modal(self, context, event):
area = context.area
if event.type in ["V", "MIDDLEMOUSE"] and event.value == 'PRESS':
if self.mode == 'ADD':
self.mode = 'REMOVE'
else:
self.mode = "ADD"
return {'RUNNING_MODAL'}
if self.mode == 'ADD':
header_text = "Adding keyframes to active strip volume."
else:
header_text = "Removing keyframe points from active strip volume"
area.header_text_set(header_text)
status_text = "Click and drag on or above the sound strip to add keyframe points. Press 'V' or MiddleMouse to toggle add/remove mode. Clear the curve with Backspace or Delete. Confirm with Return."
context.workspace.status_text_set(status_text)
if event.type in ['LEFTMOUSE', 'MOUSEMOVE']:
if event.value == 'PRESS' and self.last_press == 'LEFTMOUSE':
mouse_frame, mouse_channel = context.region.view2d.region_to_view(event.mouse_region_x, event.mouse_region_y)
clipped_pos_x, clipped_pos_y = context.region.view2d.view_to_region(mouse_frame, mouse_channel)
if clipped_pos_x == 12000 or clipped_pos_y == 12000:
#if the user clicks outside of the area, close the function
self.remove_draw_handler(context)
return {'FINISHED'}
mouse_frame = round(mouse_frame)
if self.mode == 'ADD':
if mouse_frame > self.active_strip.frame_final_end:
mouse_frame = self.active_strip.frame_final_end
if mouse_frame < self.active_strip.frame_final_start:
mouse_frame = self.active_strip.frame_final_start
if self.last_added is not None and self.last_added != mouse_frame:
#Delete points between last_added and current point to prevent spikes in graph
low_point = min(self.last_added, mouse_frame)
high_point = max(self.last_added, mouse_frame)
for frame in range(low_point + 1, high_point):
self.active_strip.keyframe_delete('volume', frame=frame)
volume = mouse_channel - self.active_strip.channel
if volume < 0:
volume = 0
self.active_strip.keyframe_insert('volume', frame=mouse_frame)
for point in self.curve.keyframe_points:
if point.co[0] == mouse_frame:
point.co[1] = volume
point.handle_left[1] = volume
point.handle_right[1] = volume
self.last_added = mouse_frame
break
else:
try:
self.active_strip.keyframe_delete('volume', frame=mouse_frame)
except:
pass
context.evaluated_depsgraph_get().update()
#context.area.tag_redraw()
self.update_areas(context)
if event.type == 'LEFTMOUSE':
self.last_press = 'LEFTMOUSE'
self.last_added = None
elif event.type in ['BACK_SPACE', 'DEL']:
self.last_press = ''
self.last_added = None
self.reset_curve()
elif event.type not in ['MOUSEMOVE', 'INBETWEEN_MOUSEMOVE', 'TIMER', 'TIMER0', 'TIMER1', 'TIMER2', 'TIMER_JOBS', 'TIMERREGION']:
self.last_press = ''
self.last_added = None
if event.type in {'RET'}:
self.remove_draw_handler(context)
return {'FINISHED'}
elif event.type in {'RIGHTMOUSE', 'ESC'}:
self.remove_draw_handler(context)
bpy.ops.ed.undo()
return {'CANCELLED'}
return {'RUNNING_MODAL'}
def execute(self, context):
return {'CANCELLED'}
def invoke(self, context, event):
#set up necessary variables
self.mode = 'ADD'
self.last_press = ''
self.last_added = None
active_strip = timeline.current_active(context)
self.active_strip = active_strip
if active_strip is None:
return {'CANCELLED'}
if active_strip.type != 'SOUND':
return {'CANCELLED'}
self.curve = get_fade_curve(context, active_strip, create=True)
region = bpy.context.region
view = region.view2d
#determine pixels per frame and channel
width = region.width
height = region.height
left, bottom = view.region_to_view(0, 0)
right, top = view.region_to_view(width, height)
if math.isnan(left):
return {'CANCELLED'}
self.active_frame_start = active_strip.frame_final_start
self.active_frame_end = active_strip.frame_final_end
shown_width = right - left
shown_height = top - bottom
self.channel_px = height / shown_height
self.frame_px = width / shown_width
self.active_left, self.active_top = view.view_to_region(active_strip.frame_final_start, active_strip.channel+1, clip=False)
self.active_right, self.active_bottom = view.view_to_region(active_strip.frame_final_end, active_strip.channel, clip=False)
context.window_manager.modal_handler_add(self)
args = (self, context)
self._handle = bpy.types.SpaceSequenceEditor.draw_handler_add(volume_operator_draw, args, 'WINDOW', 'POST_PIXEL')
context.area.tag_redraw()
bpy.ops.ed.undo_push()
#bpy.ops.ed.undo_push()
return {'RUNNING_MODAL'}
class VSEQFModalFades(bpy.types.Operator):
bl_idname = 'vseqf.modal_fades'
bl_label = "Add and modify strip fade in and out"
bl_options = {'REGISTER', 'BLOCKING', 'GRAB_CURSOR', 'UNDO'}
mode: bpy.props.EnumProperty(name='Fade To Set', default="DEFAULT", items=[("DEFAULT", "Based On Selection", "", 1), ("LEFT", "Fade In", "", 2), ("RIGHT", "Fade Out", "", 3), ("BOTH", "Fade In And Out", "", 4)])
strip_data = []
snap_to_frame = 0
snap_edges = []
mouse_last_x = 0
mouse_last_y = 0
mouse_move_x = 0
mouse_move_y = 0
mouse_start_region_x = 0
mouse_start_region_y = 0
value = '' #Used for storing typed-in grab values
mouse_scale_x = 1 #Frames to move sequences per pixel of mouse x movement
mouse_scale_y = 1 #Channels to move sequences per pixel of mouse y movement
view_frame_start = 0 #Leftmost frame in the 2d view
view_frame_end = 0 #Rightmost frame in the 2d view
view_channel_start = 0 #Lowest channel in the 2d view
view_channel_end = 0 #Highest channel in the 2d view
def remove_draw_handler(self):
bpy.types.SpaceSequenceEditor.draw_handler_remove(self._handle, 'WINDOW')
def get_fades(self, sequence, fade_mode, fade_curve):
fade_in = 0
fade_out = 0
if fade_mode in ['LEFT', 'BOTH']:
fade_in = fades(fade_curve, sequence, 'detect', 'in')
if fade_mode in ['RIGHT', 'BOTH']:
fade_out = fades(fade_curve, sequence, 'detect', 'out')
return [fade_in, fade_out]
def modal(self, context, event):
reset_fades = False
area = context.area
if event.value == 'PRESS':
if event.type in ["F", "MIDDLEMOUSE"]:
reset_fades = True
#Switch between fade modes
if self.mode == 'DEFAULT':
self.mode = 'LEFT'
elif self.mode == 'LEFT':
self.mode = 'RIGHT'
elif self.mode == 'RIGHT':
self.mode = 'BOTH'
elif self.mode == 'BOTH':
self.mode = 'DEFAULT'
elif event.type in ['L', 'S']:
reset_fades = True
self.mode = 'LEFT'
elif event.type in ['R', 'E']:
reset_fades = True
self.mode = 'RIGHT'
elif event.type == 'B':
reset_fades = True
self.mode = 'BOTH'
elif event.type == 'C':
reset_fades = True
self.mode = 'DEFAULT'
else:
self.value = vseqf.add_to_value(self.value, event.type, is_float=False)
#Calculate movement variables
mouse_delta_x = event.mouse_x - self.mouse_last_x
mouse_move_delta_x = mouse_delta_x * self.mouse_scale_x
mouse_delta_y = event.mouse_y - self.mouse_last_y
mouse_move_delta_y = mouse_delta_y * self.mouse_scale_x
self.mouse_last_x = event.mouse_x
self.mouse_last_y = event.mouse_y
if event.shift: #Slow movement
mouse_move_delta_x = mouse_move_delta_x * .1
mouse_move_delta_y = mouse_move_delta_y * .1
self.mouse_move_x = self.mouse_move_x + mouse_move_delta_x
self.mouse_move_y = self.mouse_move_y + mouse_move_delta_y
mouse_frame, mouse_channel = context.region.view2d.region_to_view(event.mouse_region_x, event.mouse_region_y)
offset_x = int(round(self.mouse_move_x))
offset_y = int(round(self.mouse_move_y))
#Override movement if snapping is enabled
snapping = False
if event.ctrl and self.snap_edges:
self.snap_to_frame = min(self.snap_edges, key=lambda x: abs(x - mouse_frame))
snapping = True
#Display information
header_text = ''
if self.mode == 'DEFAULT':
header_text = "Adjusting fades based on strip selections."
elif self.mode == 'LEFT':
header_text = "Adjusting fade-in on selected strips."
elif self.mode == 'RIGHT':
header_text = "Adjusting fade-out on selected strips."
elif self.mode == 'BOTH':
header_text = "Adjusting fade-in and fade-out on selected strips."
if self.value:
header_text = 'Fade length: ' + self.value + '. ' + header_text
area.header_text_set(header_text)
status_text = "Move mouse up/down to bring fades towards/away from middle of each strip, move mouse left/right to move all fades left/right. Press F or MiddleMouse to switch modes. Type in a value to set all fades to that value."
context.workspace.status_text_set(status_text)
#Adjust fades
for data in self.strip_data:
sequence = data['sequence']
if reset_fades:
data['fade_in'] = data['original_fade_in']
data['fade_out'] = data['original_fade_out']
if self.mode == 'DEFAULT':
fade_mode = data['fade_mode']
else:
fade_mode = self.mode
if fade_mode in ['LEFT', 'BOTH']:
#Handle fade-in
fade_in = data['original_fade_in']
if self.value:
new_fade_in = int(self.value)
elif snapping:
new_fade_in = self.snap_to_frame - sequence.frame_final_start
else:
new_fade_in = fade_in + offset_x + offset_y
if new_fade_in < 0:
new_fade_in = 0
if new_fade_in > sequence.frame_final_duration:
new_fade_in = sequence.frame_final_duration
data['fade_in'] = new_fade_in
if fade_mode in ['RIGHT', 'BOTH']:
#handle fade-out
fade_out = data['original_fade_out']
if self.value:
new_fade_out = int(self.value)
elif snapping:
new_fade_out = sequence.frame_final_end - self.snap_to_frame
else:
new_fade_out = fade_out - offset_x + offset_y
if new_fade_out < 0:
new_fade_out = 0
if new_fade_out > sequence.frame_final_duration:
new_fade_out = sequence.frame_final_duration
data['fade_out'] = new_fade_out
if fade_mode == 'BOTH':
#Check if fades are too long for each other
fade_in = data['fade_in']
fade_out = data['fade_out']
if fade_in + fade_out >= sequence.frame_final_duration:
fade_overdrive = fade_in + fade_out
fade_over_percent = fade_overdrive / sequence.frame_final_duration
data['fade_in'] = int(fade_in / fade_over_percent)
data['fade_out'] = int(fade_out / fade_over_percent)
area.tag_redraw()
if event.type in {'LEFTMOUSE', 'RET'}:
bpy.ops.ed.undo_push()
#finalize fades
for data in self.strip_data:
sequence = data['sequence']
fade_curve = data['fade_curve']
fade_in = data['fade_in']
fade_out = data['fade_out']
if fade_in != data['original_fade_in']:
fades(fade_curve, sequence, 'set', 'in', fade_length=fade_in)
if fade_out != data['original_fade_out']:
fades(fade_curve, sequence, 'set', 'out', fade_length=fade_out)
if not get_fade_curve(context, sequence):
if sequence.type == 'SOUND':
sequence.volume = 1
else:
sequence.blend_alpha = 1
self.remove_draw_handler()
area.header_text_set(None)
context.workspace.status_text_set(None)
self.mode = 'DEFAULT'
return {'FINISHED'}
elif event.type in {'RIGHTMOUSE', 'ESC'}:
#cancel fades and put everything back
self.remove_draw_handler()
area.header_text_set(None)
context.workspace.status_text_set(None)
self.mode = 'DEFAULT'
return {'CANCELLED'}
return {'RUNNING_MODAL'}
def invoke(self, context, event):
selected = context.selected_sequences
if not selected:
self.mode = 'DEFAULT'
return {'CANCELLED'}
self.mouse_move_x = 0
self.mouse_move_y = 0
self.value = ''
#Store strip data for quick access, and to prevent it from being overwritten
self.strip_data = []
self.snap_edges = [context.scene.frame_current]
for sequence in context.sequences:
self.snap_edges.extend([sequence.frame_final_start, sequence.frame_final_end])
if sequence.select:
fade_curve = get_fade_curve(context, sequence, create=True)
if sequence.select_left_handle and not sequence.select_right_handle:
fade_mode = 'LEFT'
elif sequence.select_right_handle and not sequence.select_left_handle:
fade_mode = 'RIGHT'
else:
fade_mode = 'BOTH'
fade_in, fade_out = self.get_fades(sequence, fade_mode, fade_curve)
data = {
'sequence': sequence,
'fade_mode': fade_mode,
'fade_in': fade_in,
'fade_out': fade_out,
'original_fade_in': fade_in,
'original_fade_out': fade_out,
'fade_curve': fade_curve
}
self.strip_data.append(data)
self.snap_edges = list(set(self.snap_edges)) #Sort and remove doubles
#Stores the current position of the mouse
self.mouse_last_x = event.mouse_x
self.mouse_last_y = event.mouse_y
self.mouse_start_region_x = event.mouse_region_x
self.mouse_start_region_y = event.mouse_region_y
#Determines how far a fade should move per pixel that the mouse moves
region = context.region
view = region.view2d
self.view_frame_start, self.view_channel_start = view.region_to_view(0, 0)
self.view_frame_end, self.view_channel_end = view.region_to_view(region.width, region.height)
region = context.region
frames_width = self.view_frame_end - self.view_frame_start
channels_height = self.view_channel_end - self.view_channel_start
self.mouse_scale_x = frames_width / region.width
self.mouse_scale_y = channels_height / region.height
context.window_manager.modal_handler_add(self)
args = (self, context)
self._handle = bpy.types.SpaceSequenceEditor.draw_handler_add(fade_operator_draw, args, 'WINDOW', 'POST_PIXEL')
return {'RUNNING_MODAL'}
class VSEQF_PT_QuickFadesPanel(bpy.types.Panel):
"""Panel for QuickFades operators and properties. Placed in the VSE properties area."""
bl_label = "Quick Fades"
bl_space_type = 'SEQUENCE_EDITOR'
bl_region_type = 'UI'
bl_category = "Sequencer"
@classmethod
def poll(cls, context):
prefs = vseqf.get_prefs()
try:
#Check for an active sequence to operate on
sequence = timeline.current_active(context)
if sequence:
return prefs.fades
else:
return False
except:
return False
def draw(self, context):
#Set up basic variables needed by panel
scene = bpy.context.scene
active_sequence = timeline.current_active(context)
fade_curve = get_fade_curve(context, active_sequence, create=False)
if fade_curve:
fadein = fades(fade_curve, active_sequence, 'detect', 'in')
fadeout = fades(fade_curve, active_sequence, 'detect', 'out')
else:
fadein = 0
fadeout = 0
layout = self.layout
#First row, detected fades
row = layout.row()
if fadein > 0:
row.label(text="Fadein: "+str(round(fadein))+" Frames")
else:
row.label(text="No Fadein Detected")
if fadeout > 0:
row.label(text="Fadeout: "+str(round(fadeout))+" Frames")
else:
row.label(text="No Fadeout Detected")
#Setting fades section
row = layout.row()
row.prop(scene.vseqf, 'fade')
row = layout.row(align=True)
prop = row.operator('vseqf.quickfades_set', text='Set Fadein', icon='BACK')
prop.type = 'in'
prop.tooltip = 'Set fade-in on selected sequences to '+str(scene.vseqf.fade)+' frames'
prop = row.operator('vseqf.quickfades_set', text='Set In/Out')
prop.type = 'both'
prop.tooltip = 'Set fade-in and fade-out on selected sequences to '+str(scene.vseqf.fade)+' frames'
prop = row.operator('vseqf.quickfades_set', text='Set Fadeout', icon='FORWARD')
prop.type = 'out'
prop.tooltip = 'Set fade-out on selected sequences to '+str(scene.vseqf.fade)+' frames'
row = layout.row()
row.operator('vseqf.quickfades_clear', text='Clear Fades').direction = 'both'
row = layout.row()
row.separator()
#Crossfades section
row = layout.row()
row.prop(scene.vseqf, 'transition')
row = layout.row(align=True)
prop = row.operator('vseqf.quickfades_cross', text='Crossfade Prev Clip', icon='BACK')
prop.type = 'previous'
prop.tooltip = 'Add a basic transition from each selected sequence to the previous sequence'
prop = row.operator('vseqf.quickfades_cross', text='Crossfade Next Clip', icon='FORWARD')
prop.type = 'next'
prop.tooltip = 'Add a basic transition from each selected sequence to the next sequence'
row = layout.row(align=True)
prop = row.operator('vseqf.quickfades_cross', text='Smart Cross to Prev', icon='BACK')
prop.type = 'previoussmart'
prop.tooltip = 'Add a transition from each selected sequence to the previous sequence, ensure trasitions are '+str(scene.vseqf.fade)+' frames long'
prop = row.operator('vseqf.quickfades_cross', text='Smart Cross to Next', icon='FORWARD')
prop.type = 'nextsmart'
prop.tooltip = 'Add a transition from each selected sequence to the next sequence, ensure trasitions are '+str(scene.vseqf.fade)+' frames long'
class VSEQF_PT_QuickFadesStripPanel(bpy.types.Panel):
"""Panel for QuickFades properties."""
bl_label = "Fades"
bl_parent_id = "SEQUENCER_PT_time"
bl_space_type = 'SEQUENCE_EDITOR'
bl_region_type = 'UI'
bl_category = "Strip"
@classmethod
def poll(cls, context):
prefs = vseqf.get_prefs()
try:
#Check for an active sequence to operate on
sequence = timeline.current_active(context)
if sequence:
return prefs.fades
else:
return False
except:
return False
def draw(self, context):
#Set up basic variables needed by panel
scene = bpy.context.scene
active_sequence = timeline.current_active(context)
fade_curve = get_fade_curve(context, active_sequence, create=False)
if fade_curve:
fadein = fades(fade_curve, active_sequence, 'detect', 'in')
fadeout = fades(fade_curve, active_sequence, 'detect', 'out')
else:
fadein = 0
fadeout = 0
layout = self.layout
#First row, detected fades
row = layout.row()
if fadein > 0:
row.label(text="Fadein: "+str(round(fadein))+" Frames")
else:
row.label(text="No Fadein Detected")
if fadeout > 0:
row.label(text="Fadeout: "+str(round(fadeout))+" Frames")
else:
row.label(text="No Fadeout Detected")
class VSEQFQuickFadesMenu(bpy.types.Menu):
"""Pop-up menu for QuickFade operators"""
bl_idname = "VSEQF_MT_quickfades_menu"
bl_label = "Quick Fades"
@classmethod
def poll(cls, context):
del context
prefs = vseqf.get_prefs()
return prefs.fades
def draw(self, context):
scene = context.scene
sequences = timeline.current_selected(context)
sequence = timeline.current_active(context)
layout = self.layout
if sequence and len(sequences) > 0:
#If a sequence is active
fade_curve = get_fade_curve(context, sequence, create=False)
if fade_curve:
fadein = fades(fade_curve, sequence, 'detect', 'in')
fadeout = fades(fade_curve, sequence, 'detect', 'out')
else:
fadein = 0
fadeout = 0
#Detected fades section
if fadein > 0:
layout.label(text="Fadein: "+str(round(fadein))+" Frames")
else:
layout.label(text="No Fadein Detected")
if fadeout > 0:
layout.label(text="Fadeout: "+str(round(fadeout))+" Frames")
else:
layout.label(text="No Fadeout Detected")
#Fade length
layout.prop(scene.vseqf, 'fade')
prop = layout.operator('vseqf.quickfades_set', text='Set Fadein')
prop.type = 'in'
prop.tooltip = 'Set fade-in on selected sequences to '+str(scene.vseqf.fade)+' frames'
prop = layout.operator('vseqf.quickfades_set', text='Set Fadeout')
prop.type = 'out'
prop.tooltip = 'Set fade-out on selected sequences to '+str(scene.vseqf.fade)+' frames'
layout.operator('vseqf.quickfades_clear', text='Clear Fades').direction = 'both'
#Add crossfades
layout.separator()
layout.prop(scene.vseqf, 'transition', text='')
prop = layout.operator('vseqf.quickfades_cross', text='Crossfade Prev Sequence')
prop.type = 'previous'
prop.tooltip = 'Add a basic transition from each selected sequence to the previous sequence'
prop = layout.operator('vseqf.quickfades_cross', text='Crossfade Next Sequence')
prop.type = 'next'
prop.tooltip = 'Add a basic transition from each selected sequence to the next sequence'
prop = layout.operator('vseqf.quickfades_cross', text='Smart Cross to Prev')
prop.type = 'previoussmart'
prop.tooltip = 'Add a transition from each selected sequence to the previous sequence, ensure trasitions are '+str(scene.vseqf.fade)+' frames long'
prop = layout.operator('vseqf.quickfades_cross', text='Smart Cross to Next')
prop.type = 'nextsmart'
prop.tooltip = 'Add a transition from each selected sequence to the next sequence, ensure trasitions are '+str(scene.vseqf.fade)+' frames long'
else:
layout.label(text="No Sequence Selected")
class VSEQFQuickFadesSet(bpy.types.Operator):
"""Operator to add fades to selected sequences
Uses the vseqf fade_length variable for length
Argument:
type: String, determines if a fadein or fadeout should be set
'in': sets a fadein
'out': sets a fadeout
'both': sets fadein and fadeout"""
bl_idname = 'vseqf.quickfades_set'
bl_label = 'VSEQF Quick Fades Set Fade'
bl_description = 'Adds or changes fade for selected sequences'
#Should be set to 'in' or 'out'
type: bpy.props.StringProperty()
tooltip: bpy.props.StringProperty("")
@classmethod
def description(cls, context, properties):
return properties.tooltip
def execute(self, context):
bpy.ops.ed.undo_push()
#iterate through selected sequences and apply fades to them
selected_sequences = timeline.current_selected(context)
for sequence in selected_sequences:
fade_curve = get_fade_curve(context, sequence, create=True)
if self.type == 'both':
fades(fade_curve, sequence, 'set', 'in', fade_length=context.scene.vseqf.fade)
fades(fade_curve, sequence, 'set', 'out', fade_length=context.scene.vseqf.fade)
else:
fades(fade_curve, sequence, 'set', self.type, fade_length=context.scene.vseqf.fade)
if not get_fade_curve(context, sequence):
if sequence.type == 'SOUND':
sequence.volume = 1
else:
sequence.blend_alpha = 1
vseqf.redraw_sequencers()
return{'FINISHED'}
class VSEQFQuickFadesClear(bpy.types.Operator):
"""Operator to clear fades on selected sequences"""
bl_idname = 'vseqf.quickfades_clear'
bl_label = 'VSEQF Quick Fades Clear Fades'
bl_description = 'Clears fade in and out for selected sequences'
direction: bpy.props.StringProperty('both')
active_only: bpy.props.BoolProperty(False)
def execute(self, context):
bpy.ops.ed.undo_push()
if self.active_only:
sequences = [timeline.current_active(context)]
else:
sequences = timeline.current_selected(context)