-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path__init__.py
1524 lines (1267 loc) · 51.5 KB
/
__init__.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
bl_info = {
"name": "Fountain Script",
"author": "Philippe Lavoie <[email protected]>",
"version": (1, 2, 1),
"blender": (2, 80, 0),
"location": "View 3D > Tools > Animation",
"description": "Allows you to add fountain script elements as markers with dialogue and action descriptions",
"warning": "",
"wiki_url": "https://github.com/philippe-lavoie/blender-fountain-addon/wiki",
"tracker_url": "https://github.com/philippe-lavoie/blender-fountain-addon.git",
"category": "Animation",
}
import os
import bpy
import math
import operator
import re
import sys
import blf
#if 'DEBUG_MODE' in sys.argv:
#import fountain
#else:
#import fountain
from . import fountain
from bpy.types import Panel, Operator, Menu, PropertyGroup
from bpy.props import *
# if 'DEBUG_MODE' in sys.argv:
# fountainModuleName = ('{}'.format('fountain'))
# else:
# fountainModuleName = ('{}.{}'.format(__name__, 'fountain'))
# if fountainModuleName in sys.modules:
# importlib.reload(sys.modules[fountainModuleName])
# else:
# globals()[fountainModuleName] = importlib.import_module(fountainModuleName)
# setattr(globals()[fountainModuleName], 'modulesNames', fountainModuleName)
# list of texts
def texts(self, context):
return [(text.name, text.name, '') for text in bpy.data.texts]
def frameToTime(frame, context, format='long'):
render = context.scene.render
framerate = render.fps / render.fps_base
time_in_seconds = frame / framerate
hours = math.floor(time_in_seconds / 3600.0)
minutes = math.floor(time_in_seconds / 60.0)
seconds = time_in_seconds - (minutes * 60)
if format == 'long':
if minutes > 0:
return "{:0d} min {:00.2f}sec".format(minutes, seconds)
else:
return "{:0.2f} sec".format(seconds)
elif format == 'srt':
secs = math.floor(seconds)
ms = math.floor(1000.0 * (seconds - secs))
return "{:02d}:{:02d}:{:02d},{:03d}".format(hours, minutes, secs, ms)
else:
return "{:02d}:{:00.2f}".format(minutes, seconds)
def stringFits(pstr, max_width):
font_id = 0
text_width, text_height = blf.dimensions(font_id, pstr)
return text_width < max_width
def draw_string(x,
y,
packed_strings,
horizontal_align='left',
bottom_align=False,
max_width=0.7):
font_id = 0
blf.size(font_id, 14, 72)
blf.enable(0, blf.SHADOW)
blf.shadow_offset(0, 1, -1)
blf.shadow(0, 5, 0.0, 0.0, 0.0, 0.8)
x_offset = 0
y_offset = 0
line_height = (blf.dimensions(font_id, "M")[1] * 1.45)
max_size = 0
if not packed_strings or len(packed_strings) == 0:
return 0
if len(packed_strings[-1]) != 2:
packed_strings = packed_strings[:-1]
if bottom_align:
for command in packed_strings:
if len(command) != 2:
y_offset += line_height
line_widths = []
index = 0
if horizontal_align != 'left':
line_width = 0
for command in packed_strings:
if len(command) == 2:
pstr, pcol = command
text_width, text_height = blf.dimensions(font_id, pstr)
line_width += text_width
else:
while len(line_widths) <= index:
line_widths.append(line_width)
line_width = 0
index += 1
while len(line_widths) <= index:
line_widths.append(line_width)
index = 0
for command in packed_strings:
line_width = 0
if horizontal_align == 'right':
line_width = line_widths[index]
elif horizontal_align == 'middle':
line_width = line_widths[index] // 2
if len(command) == 2:
pstr, pcol = command
blf.color(font_id, *pcol)
text_width, text_height = blf.dimensions(font_id, pstr)
blf.position(font_id, (x + x_offset - line_width), (y + y_offset),
0)
print("IM HERE - pos:", (x + x_offset - line_width), (y + y_offset), " - color:", pcol, " - string:", pstr)
blf.draw(font_id, pstr)
x_offset += text_width
if x_offset > max_size:
max_size = x_offset
else:
x_offset = 0
y_offset -= line_height
index += 1
blf.disable(0, blf.SHADOW)
return max_size
class DrawingClass:
def __init__(self):
self.handle = None
self.scene_name = "Scene"
self.action = "Action\naction"
self.dialogue = "Dialogue\ndialogue"
self.dialogues = []
self.marker = "MarkerName"
self.character = ""
self.characters = []
self.last_frame = -100
self.last_index = 0
self.RED = (1, 0, 0, 1)
self.GREEN = (0, 1, 0, 1)
self.BLUE = (0, 0, 1, 1)
self.CYAN = (0, 1, 1, 1)
self.MAGENTA = (1, 0, 1, 1)
self.YELLOW = (1, 1, 0, 1)
self.ORANGE = (1, 0.8, 0, 1)
self.WHITE = (1, 1, 1, 0.8)
self.FULLWHITE = (1, 1, 1, 1)
self.CR = "\n"
def start(self, context):
if not self.handle:
self.handle = bpy.types.SpaceView3D.draw_handler_add(
draw_text_callback, (self, context), 'WINDOW', 'POST_PIXEL')
self.last_frame = -100
self.last_index = 0
def stop(self):
if self.handle:
bpy.types.SpaceView3D.draw_handler_remove(self.handle, 'WINDOW')
self.handle = None
def __del__(self):
self.stop()
def set_content(self, element):
self.marker = element.name
self.character = ""
if element.fountain_type == 'Scene Heading':
self.scene_name = element.content
self.action = ""
self.dialogue = ""
self.dialogues = None
self.characters = None
elif element.fountain_type == 'Transition':
self.scene_name = element.content
self.dialogue = ''
self.action = "Transition"
self.dialogues = None
self.characters = None
elif element.fountain_type == 'Action':
self.action = element.content
self.dialogue = ""
self.dialogues = None
self.characters = None
elif element.fountain_type == 'Dialogue':
if element.is_dual_dialogue:
self.dialogue = ''
self.character = ''
if not self.dialogues:
self.dialogues = [element.content]
self.characters = [element.target]
else:
self.dialogues.append(element.content)
self.characters.append(element.target)
else:
self.dialogue = element.content
self.character = element.target
self.dialogues = None
self.characters = None
def updateFountainElements(self, context):
frame = context.scene.frame_current
if self.last_frame == frame:
return
fountain_collection = sorted(
context.scene.fountain_markers, key=lambda item: item.frame)
# if self.last_frame < frame:
# range = fountain_collection[self.last_index:]
# for element in [element for element in range if element.frame <= frame and element.frame_end >= frame]:
# self.last_index += 1
# self.set_content(element)
# self.last_frame = frame
# return
self.last_index = 0
self.dialogue = ""
self.action = ""
self.scene_name = ""
self.marker = ""
self.character = ""
for element in [
element for element in fountain_collection
if element.frame <= frame and element.frame_end > frame
]:
self.last_index += 1
self.set_content(element)
self.last_frame = frame
def get_dialogue(self, character, dialogue, max_width, max_characters):
ps = []
ps.append((character, self.ORANGE))
ps.append(self.CR)
for line in dialogue.splitlines():
while len(line) > max_characters or not stringFits(
line, max_width):
screen_max_characters = max_characters
while not stringFits(line[:screen_max_characters], max_width):
split_index = line.rfind(' ', 0, screen_max_characters)
if split_index <= 0:
break
screen_max_characters = split_index
split_index = line.rfind(' ', 0, screen_max_characters)
if split_index > 0:
ps.append((line[:split_index], self.YELLOW))
ps.append(self.CR)
line = line[split_index + 1:]
else:
break
ps.append((line, self.YELLOW))
ps.append(self.CR)
return ps
def draw_text_callback(self, context):
try:
if not bpy.context.scene.fountain.show_fountain:
return
except AttributeError:
#self.stop()
return
screen_max_characters = bpy.context.scene.fountain.max_characters
index = 0
for area in bpy.context.screen.areas:
if area.type == 'VIEW_3D':
self.width = area.width
self.height = area.height
break
index += 1
for region in bpy.context.area.regions:
if region.type == "UI":
self.width -= region.width
break
if self.width < 200 or self.height < 200:
return
self.updateFountainElements(bpy.context)
x = 60
y = 60
ps = [(self.scene_name, self.WHITE), self.CR, (self.marker,
self.WHITE)]
x = self.width - 20
y = 60
label_size = draw_string(
x, y, ps, horizontal_align='right',
max_width=0.2 * self.width) + 40
if self.action:
ps = []
for line in self.action.splitlines():
while len(line) > screen_max_characters or not stringFits(
line, self.width - 40):
screen_max_characters = bpy.context.scene.fountain.max_characters
while not stringFits(line[:screen_max_characters],
self.width - 40):
split_index = line.rfind(' ', 0, screen_max_characters)
if split_index <= 0:
break
screen_max_characters = split_index
split_index = line.rfind(' ', 0, screen_max_characters)
if split_index > 0:
ps.append((line[:split_index], self.CYAN))
ps.append(self.CR)
line = line[split_index + 1:]
else:
break
if line == 'Transition':
ps.append((line, self.MAGENTA))
else:
ps.append((line, self.CYAN))
ps.append(self.CR)
x = self.width / 2
y = self.height - 70
draw_string(
x,
y,
ps,
horizontal_align='middle',
bottom_align=False,
max_width=self.width - 40)
if self.dialogue:
ps = self.get_dialogue(self.character, self.dialogue,
self.width - label_size * 2,
bpy.context.scene.fountain.max_characters)
x = self.width / 2
y = 40
draw_string(
x,
y,
ps,
horizontal_align='middle',
bottom_align=True,
max_width=self.width - label_size)
if self.dialogues:
ps = self.get_dialogue(self.characters[0], self.dialogues[0],
(self.width / 2) - (label_size * 2),
bpy.context.scene.fountain.max_characters)
x = self.width / 4
y = 40
draw_string(
x,
y,
ps,
horizontal_align='middle',
bottom_align=True,
max_width=self.width - label_size)
ps = self.get_dialogue(self.characters[1], self.dialogues[1],
(self.width / 2) - (label_size * 2),
bpy.context.scene.fountain.max_characters)
x = 3 * self.width / 4
y = 40
draw_string(
x,
y,
ps,
horizontal_align='middle',
bottom_align=True,
max_width=self.width - label_size)
class FountainProps(PropertyGroup):
def update_text_list(self, context):
self.script = bpy.data.texts[self.scene_texts].name
return None
# def set_show_fountain(self,value):
# FOUNTAIN_OT_show_fountain.show = value
# self["show_fountain"] = value
# bpy.ops.scene.show_fountain('EXEC_DEFAULT')
# def get_show_fountain(self):
# return self["show_fountain"]
# def updateShow(self, context):
# if FOUNTAIN_OT_show_fountain.show != self.get_show_fountain():
# FOUNTAIN_OT_show_fountain.show = self["show_fountain"]
# bpy.ops.scene.show_fountain('EXEC_DEFAULT')
def reset(self):
self.title = ''
self.script_line = -1
name: StringProperty(default="Fountain script")
show_fountain: BoolProperty(
default=False) #, set = set_show_fountain, get=get_show_fountain)
script: StringProperty(default='', description='Choose your script')
scene_texts: EnumProperty(
name='Available Texts',
items=texts,
update=update_text_list,
description='Available Texts.')
# marker_on_scene: BoolProperty(default=True)
# marker_on_action: BoolProperty(default=True)
# marker_on_transition: BoolProperty(default=True)
# marker_on_section: BoolProperty(default=True)
# marker_on_dialogue: BoolProperty(default=True)
title: StringProperty(default="")
max_characters: IntProperty(default=80, min=10)
script_line: IntProperty(default=-1)
fix_duration: BoolProperty(default=True)
def get_body(self):
text = bpy.data.texts[self.scene_texts]
full = ""
for line in text.lines:
full += line.body + "\n"
return full
def get_script(self):
text = bpy.data.texts[self.scene_texts]
return text
class FountainMarker(bpy.types.PropertyGroup):
def get_marker(self, context):
if not self.original_name:
return None
result = [
marker for marker in context.scene.timeline_markers
if marker.name == self.original_name
]
if len(result) > 0:
return result[0]
return None
def updateName(self, context):
marker = self.get_marker(context)
if marker is not None:
marker.name = self.name
self.original_name = self.name
return
name: bpy.props.StringProperty()
original_name: bpy.props.StringProperty()
frame: bpy.props.IntProperty()
duration: bpy.props.IntProperty(default=0, min=0)
frame_end: bpy.props.IntProperty()
sequence: IntProperty()
fountain_type: bpy.props.EnumProperty(
name='Foutain element type',
description='The element in the fountain script',
items={
('Section Heading', 'Section Heading', 'A section heading', 1),
('Comment', 'Comment', 'A comment', 2),
('Scene Heading', 'Scene Heading', 'A scene heading', 3),
('Transition', 'Transition', 'A transition to a different scene', 4),
('Action', 'Action', 'An action that should be depicted', 5),
('Dialogue', 'Dialogue', 'A dialogue from a character', 6),
})
is_dual_dialogue: bpy.props.BoolProperty(default=False)
content: bpy.props.StringProperty()
target: bpy.props.StringProperty()
line_number: bpy.props.IntProperty(
name="Line Number", description="Line number in the script file")
class FOUNTAIN_PT_panel(bpy.types.Panel):
bl_space_type = 'VIEW_3D'
bl_region_type = "UI"
bl_category = "Animation"
bl_label = "Fountain Markers"
last_marker = ''
selected_index = -1
# def execute(self, context):
# if not context.scene.fountain_markers:
# scene.synch_markers(context)
def invoke(self, context):
scn = context.scene
fountain = scn.fountain
fountain.updateShow(context)
return {'FINISHED'}
def draw(self, context):
scn = context.scene
fountain = scn.fountain
layout = self.layout
layout.use_property_split = False
row = layout.row()
column = row.column(align=True)
#row = column.row(align=True)
showLabel = 'Show Fountain'
if fountain.show_fountain:
showLabel = 'Hide Fountain'
column.prop(fountain, 'show_fountain', text="Show Fountain")
column.prop(fountain, 'max_characters', text='Characters per line')
column = column.row(align=True)
column.prop(fountain, 'scene_texts', text='', icon='TEXT', icon_only=True)
column.prop(fountain, 'script', text="")
row = layout.row()
column = row.column(align=True)
#row = column.row(align=True)
column.operator("scene.import_fountain", text="Import")
column.operator("scene.clear_fountain", text="Clear")
column.operator("scene.update_fountain_script", text="Update Script")
column.operator("scene.clean_fountain_script", text="Clean Script")
column.operator("scene.print_fountain", text="Export to SRT")
# if len(fountain.script) > 0:
# row = self.layout.row()
# column = row.column(align=True)
# row = column.row(align=True)
# row.prop(fountain, 'marker_on_scene', text='Scene')
# row.prop(fountain, 'marker_on_action', text="Action")
# row.prop(fountain, 'marker_on_transition', text="Transition")
# row.prop(fountain, 'marker_on_section', text="Header")
# row.prop(fountain, 'marker_on_dialogue', text="Dialogue")
row = layout.row()
column = row.column(align=True)
#row = column.row(align=True)
column.operator("scene.synch_markers", text="Sync markers")
column.operator(
"scene.show_end_markers", text="Show End", icon='MARKER_HLT')
column.operator("scene.move_markers", text="Move")
column = self.layout.row()
column.prop(fountain, "fix_duration", text="Fix Duration After Move")
row = layout.row()
column = row.column(align=True)
row = column.row(align=True)
row.enabled = False
row.prop(fountain, "title")
row = layout.row()
rows = 2
row.template_list(
"FOUNTAINMARKER_UL_Item",
"fountain_markers_list",
context.scene,
"fountain_markers",
context.scene,
"fountain_markers_index",
rows=rows)
idx = scn.fountain_markers_index
try:
item = scn.fountain_markers[idx]
except IndexError:
pass
else:
row = layout.row()
column = layout.column(align=True)
column.enabled = False
column.prop(item, "name", text="Name")
column.prop(item, "fountain_type")
column = row.column(align=True)
column.prop(item, "duration")
if item.duration > 0:
column.label(text="At " + frameToTime(item.frame, context) +
" for " + frameToTime(item.duration, context))
else:
column.label(text="At " + frameToTime(item.frame, context))
row = layout.row()
column = row.column(align=True)
column.enabled = False
column.prop(item, "content", text="Content")
column.prop(item, "target", text="Target")
column.prop(item, "line_number")
if FOUNTAIN_PT_panel.selected_index != idx:
FOUNTAIN_PT_panel.selected_index = idx
for m in context.scene.timeline_markers:
if m.name == item.name:
m.select = True
else:
m.select = False
if fountain.script_line != item.line_number:
#bpy.context.subframe(item.frame) #error?
bpy.context.scene.frame_current = item.frame
fountain.script_line = item.line_number
bpy.ops.scene.move_fountain_cursor('EXEC_DEFAULT')
#end FOUNTAIN_PT_panel
class FOUNTAIN_OT_check_markers(bpy.types.Operator):
bl_idname = "scene.check_markers"
bl_label = "Check fountain markers"
bl_options = {"REGISTER"}
@classmethod
def poll(self, context):
print('c')
return bpy.context.area.type == 'TIMELINE'
#return len(context.scene.timeline_markers) > 0
def modal(self, context, evt):
print('m')
if evt.type == 'RIGHTMOUSE':
if evt.value == 'PRESS':
print('LMB Pressed')
elif evt.value == 'RELEASE':
print('LMB Released')
return {'FINISHED'}
return {'RUNNING_MODAL'}
def invoke(self, context, evt):
print('invoking')
#if evt.type == 'RIGHTMOUSE':
bpy.context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
#return {'CANCELLED'}
class FOUNTAIN_OT_show_fountain(bpy.types.Operator):
bl_idname = "scene.show_fountain"
bl_label = "Show fountain markers"
drawing_class = None
show = False
@classmethod
def poll(self, context):
return True
# def modal(self, context, event):
# for window in bpy.context.window_manager.windows:
# screen = window.screen
# for area in screen.areas:
# if area.type == 'VIEW_3D':
# area.tag_redraw()
# return {'PASS_THROUGH'}
# return {'CANCELLED'}
def execute(self, context):
print("SHOW FOUNTAIN")
scn = context.scene
try:
fountain = scn.fountain
except AttributeError as e:
print(e)
if FOUNTAIN_OT_show_fountain.drawing_class is not None:
FOUNTAIN_OT_show_fountain.drawing_class.stop()
return {'CANCELLED'}
else:
FOUNTAIN_OT_show_fountain.show = fountain.show_fountain
if FOUNTAIN_OT_show_fountain.show:
if FOUNTAIN_OT_show_fountain.drawing_class is None:
FOUNTAIN_OT_show_fountain.drawing_class = DrawingClass()
FOUNTAIN_OT_show_fountain.drawing_class.start(context)
else:
if FOUNTAIN_OT_show_fountain.drawing_class is not None:
FOUNTAIN_OT_show_fountain.drawing_class.stop()
context.area.tag_redraw()
return {'FINISHED'}
class FOUNTAIN_OT_print_fountain(bpy.types.Operator):
bl_idname = "scene.print_fountain"
bl_label = "Print Marker's timing"
filepath: bpy.props.StringProperty(
default='youtube.srt', subtype="FILE_PATH")
marker_on_action: BoolProperty(default=True, name='Export Actions')
marker_on_dialogue: BoolProperty(default=True, name='Export Dialogues')
marker_on_scene: BoolProperty(default=False, name="Export Scenes' start")
marker_on_transition: BoolProperty(
default=False, name='Export Transitions')
marker_on_section: BoolProperty(default=False, name='Export sections')
@classmethod
def poll(self, context):
return True
def invoke(self, context, event):
#return self.execute(context)
context.window_manager.invoke_props_dialog(self, width=400)
#context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
def execute(self, context):
if len(context.scene.timeline_markers) == 0:
self.report({'INFO'}, "No markers found.")
return {'CANCELLED'}
sorted_markers = sorted(
context.scene.fountain_markers, key=operator.attrgetter('frame'))
fountain = context.scene.fountain
markers_as_timecodes = ""
sub_index = 1
marker_index = -1
for marker in sorted_markers:
marker_index += 1
if marker.fountain_type == 'Scene Heading' and not self.marker_on_scene:
continue
if marker.fountain_type == 'Section Heading' and not self.marker_on_section:
continue
if marker.fountain_type == 'Action' and not self.marker_on_action:
continue
if marker.fountain_type == 'Dialogue' and not self.marker_on_dialogue:
continue
if marker.fountain_type == 'Transition' and not self.marker_on_transition:
continue
# The SRT format does not allow 0 durations
if marker.duration <= 0:
continue
content = marker.content
if marker.fountain_type == 'Dialogue':
content = marker.target + ": " + marker.content
if marker.is_dual_dialogue:
if sorted_markers[marker_index - 1].is_dual_dialogue:
content = "%s: %s\n%s: %s" % (
sorted_markers[marker_index - 1].target,
sorted_markers[marker_index - 1].content,
marker.target, marker.content)
marker.duration = max(
marker.duration,
sorted_markers[marker_index - 1].duration)
else:
continue
start = frameToTime(marker.frame, context, format='srt')
end = frameToTime(marker.frame_end, context, format='srt')
result = "%d\n%s --> %s\n%s\n" % (sub_index, start, end,
content.replace('\\n', '\n'))
markers_as_timecodes += result + "\n"
sub_index += 1
file = open(self.filepath, 'w')
file.write(markers_as_timecodes)
return {"FINISHED"}
#end FOUNTAIN_OT_print_fountain
class FOUNTAIN_OT_move_fountain_cursor(bpy.types.Operator):
bl_idname = "scene.move_fountain_cursor"
bl_label = "Move cursor in fountain script"
@classmethod
def poll(self, context):
return context.scene.fountain.script_line > 1
def invoke(self, context, event):
return self.execute(context)
def execute(self, context):
if context.scene.fountain.script_line < 0:
return {"FINISHED"}
#t = bpy.context.space_data.text
#script = context.scene.fountain.get_script()
for window in bpy.context.window_manager.windows:
screen = window.screen
for area in screen.areas:
if area.type == 'TEXT_EDITOR':
for region in area.regions:
if region.type == "WINDOW":
#space_data = area.spaces.active
#t = space_data.text
override = context.copy()
override['window'] = window
override['screen'] = screen
override['area'] = area
override['region'] = region
override[
'edit_text'] = context.scene.fountain.get_script(
)
bpy.ops.text.jump(
override,
line=context.scene.fountain.script_line)
bpy.ops.text.move(override, type='LINE_BEGIN')
break
return {"FINISHED"}
#end MoveFountainScript
class FOUNTAIN_OT_clean_fountain_script(bpy.types.Operator):
bl_idname = "scene.clean_fountain_script"
bl_label = "Clean fountain script"
@classmethod
def poll(self, context):
return len(context.scene.fountain.script) > 0
def invoke(self, context, event):
return self.execute(context)
def execute(self, context):
body = context.scene.fountain.get_body()
lines = body.split('\n')
clean = []
for line in lines:
if line[:6] == "[[t&d:":
continue
if line[:4] == "[[t:":
continue
if line[:4] == "[[d:":
continue
clean.append(line)
context.scene.fountain.get_script().from_string("\n".join(clean))
if len(context.scene.fountain_markers) > 0:
bpy.ops.scene.import_fountain('EXEC_DEFAULT')
return {"FINISHED"}
#end FOUNTAIN_OT_clean_fountain_script
class FOUNTAIN_OT_update_fountain_script(bpy.types.Operator):
bl_idname = "scene.update_fountain_script"
bl_label = "Update fountain script"
write_time: BoolProperty(
default=True,
name="Write Start Frame",
description='Write start frame')
write_duration: BoolProperty(
default=True, name="Write Duration", description='Write duration')
@classmethod
def poll(self, context):
return len(context.scene.fountain.script) > 0 and len(
context.scene.fountain_markers) > 0
def invoke(self, context, event):
context.window_manager.invoke_props_dialog(self, width=500)
return {'RUNNING_MODAL'}
def execute(self, context):
body = context.scene.fountain.get_body()
lines = body.split('\n')
fountain_collection = context.scene.fountain_markers
offset = 0
if not self.write_time and not self.write_duration:
return
for f in fountain_collection:
line = lines[f.line_number + offset]
new_line = "[[t&d:" + str(f.frame) + " " + str(f.duration) + "]]"
if self.write_time and not self.write_duration:
new_line = "[[t:" + str(f.frame) + "]]"
elif not self.write_time and self.write_duration:
new_line = "[[d:" + str(f.duration) + "]]"
if line.startswith("[["):
lines[f.line_number + offset] = new_line
else:
lines.insert(f.line_number + offset, new_line)
offset += 1
context.scene.fountain.get_script().from_string("\n".join(lines))
bpy.ops.scene.import_fountain('EXEC_DEFAULT')
return {"FINISHED"}
#end FOUNTAIN_OT_update_fountain_script
class FOUNTAIN_OT_clear_fountain(bpy.types.Operator):
bl_idname = "scene.clear_fountain"
bl_label = "Clear fountain markers"
@classmethod
def poll(self, context):
return len(context.scene.fountain_markers) > 0
def invoke(self, context, event):
return self.execute(context)
def execute(self, context):
context.scene.fountain_markers.clear()
context.scene.timeline_markers.clear()
context.scene.fountain.reset()
context.scene.fountain_markers_index = 0
return {"FINISHED"}
#end FOUNTAIN_OT_update_fountain_script
class FOUNTAIN_OT_import_fountain(bpy.types.Operator):
bl_idname = "scene.import_fountain"
bl_label = "Import fountain script"
def set_spw(self, context):
self.sec_per_word = 60.0 / float(self.words_per_min)
def set_wpm(self, context):
self.words_per_min = int(rond(60.0 / self.sec_per_word))
marker_on_scene: BoolProperty(default=True, name='Create Scene Markers')
marker_on_transition: BoolProperty(
default=True, name='Create Transition Markers')
marker_on_section: BoolProperty(
default=True, name="Create Section Markers")
marker_on_action: BoolProperty(default=True, name='Create Action Markers')
marker_on_dialogue: BoolProperty(
default=True, name='Create Dialogue Markers')
words_per_min: IntProperty(
default=160, name="Words per minute", min=50, max=400, update=set_spw)
sec_per_word: FloatProperty(
default=0.3, name="Sec per word", min=0.05, max=3, update=set_wpm)
action_per_phrase: FloatProperty(
default=1.0, name="Action length per phrase (sec)", min=0.1, max=20)
@classmethod
def poll(self, context):
return len(context.scene.fountain.script) > 0
def draw(self, context):
scn = context.scene
row = self.layout.row()
row = row.column(align=True)
row.prop(self, 'marker_on_scene', text='Scene Markers')
row.prop(self, 'marker_on_transition')
row.prop(self, 'marker_on_section')
row.prop(self, 'marker_on_action')
row.prop(self, 'marker_on_dialogue')
row.prop(self, 'action_per_phrase')
row.prop(self, 'words_per_min')
row.prop(self, 'sec_per_word')
def invoke(self, context, event):
context.window_manager.invoke_props_dialog(self, width=400)
return {'RUNNING_MODAL'}
def execute(self, context):
dir = os.path.dirname(bpy.data.filepath)
if not dir in sys.path:
sys.path.append(dir)
fountain_script = context.scene.fountain.get_body()
F = fountain.Fountain(fountain_script)
if 'title' in F.metadata:
context.scene.fountain.title = F.metadata['title'][0]
context.scene.timeline_markers.clear()
fountain_collection = context.scene.fountain_markers
current_collection = {}
for f in fountain_collection:
current_collection[f.name] = (f.frame, f.duration)
fountain_collection.clear()
frame = 0
act = 0
sequence = 0