forked from sambler/myblendercontrib
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTextFX.py
More file actions
1585 lines (1421 loc) · 75.2 KB
/
TextFX.py
File metadata and controls
1585 lines (1421 loc) · 75.2 KB
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
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
import bpy, random, string, time, os
from bpy.types import Panel, Operator, Menu
from bpy.app.handlers import persistent, frame_change_pre
from bpy.props import *
from math import *
# Add-on info
bl_info = {
"name": "TextFX",
"author": "Monaime Zaim (CodeOfArt.com)",
"version": (1, 0, 1),
"blender": (2, 7, 8),
"location": "View3D > Tools > TextFX",
"description": "Text animation tools",
"wiki_url": "http://codeofart.com/text-fx/",
"tracker_url": "http://codeofart.com/text-fx/",
"category": "Animation"}
################################################
################# Functions ####################
################################################
####################### Easing formulas ###############################
# t: Time evaluation (increment)
# b: Start value
# c: End value (in my case = end value - start value)
# d: Duration
# for example if the animation will start at the frame 1 and end
# at the frame 50 the duration will be 49 = (50-1) and "t" must
# increment from 0 to 49, or you can use a linear interpolation [0..1]
# i have used a linear interpolation, see the "wiggle" fuction
def easeInOutSine(t, b, c, d):
return -c/2 * (cos(pi*t/d) - 1) + b
def easeInQuart(t, b, c, d):
t /= d
return c*t*t*t*t + b
def easeInOutQuart(t, b, c, d):
t /= d/2
if t < 1:
return c/2*t*t*t*t + b
t -= 2
return -c/2 * (t*t*t*t - 2) + b
linearTween = lambda t, b, c, d : c*t/d + b
def easeInOutCirc(t, b, c, d):
t /= d/2
if t < 1:
return -c/2 * (sqrt(1 - t*t) - 1) + b
t -= 2
return c/2 * (sqrt(1 - t*t) + 1) + b
def easeOutElastic(t, b, c, d):
ts=(t/d)*t
tc=ts*t
return b+c*(46*tc*ts + -155*ts*ts + 190*tc + -100*ts + 20*t)
def easeInElastic(t, b, c, d):
ts=(t/d)*t
tc=ts*t
return b+c*(33*tc*ts + -59*ts*ts + 32*tc + -5*ts)
def easeInCubic(t, b, c, d):
tc=(t/d)*t*t
return b+c*(tc)
def easeOutQuintic(t, b, c, d):
ts=(t/d)*t
tc=ts*t
return b+c*(tc*ts + -5*ts*ts + 10*tc + -10*ts + 5*t)
def easeOutBackQuartic(t, b, c, d):
ts=(t/d)*t
tc=ts*t
return b+c*(-2*ts*ts + 10*tc + -15*ts + 8*t)
def easeInBackQuartic(t, b, c, d):
ts=(t/d)*t
tc=ts*t
return b+c*(2*ts*ts + 2*tc + -3*ts)
def easeOutInCubic(t, b, c, d):
ts=(t/d)*t
tc=ts*t
return b+c*(4*tc + -6*ts + 3*t)
def easeInOutElastic(t, b, c, d):
ts=(t/d)*t
tc=ts*t
return b+c*(-74*tc*ts + 195*ts*ts + -170*tc + 50*ts)
# List of available easing formulas
def easing_list(self, context):
ease_list = ['easeInOutSine', 'linearTween', 'easeInBackQuartic', 'easeInCubic',
'easeInElastic', 'easeInOutCirc', 'easeInOutElastic', 'easeInOutQuart',
'easeInQuart', 'easeOutBackQuartic', 'easeOutElastic', 'easeOutInCubic',
'easeOutQuintic']
return [(i, i, '') for i in ease_list]
###########################################################################
####################### Fcurves effects (Kinetic) #######################
# Overshoot effect
def overshoot(t, velocity, amplitude, frequency, duration):
if t > 0 and t < duration:
dur = (t-1)/(duration - 2)
decay = velocity * amplitude *(sin((pi/2)*(1+(t-1)/(1+frequency))) * (1-dur))
else:
decay = 0
return decay
# Bounce effect
def bounce(t, velocity, amplitude, e, duration, g = -10):
def set_bounce_parametrs(t, dur, e):
x = 0
segments = [0]
n = dur/e
int_time = 0
elasticity = 0
for i in range(5):
if x+round((n/(e**(i)))) < x+2:
x+=2
else:
x+= round(n/(e**(i)))
if dur-x <= 2:
x = dur
segments.append(x)
for j, s in enumerate(segments):
if t > s and j < len(segments):
if t <= max(segments):
start = s
end = segments[j+1]
int_time = (t-start)/(end-start)
elasticity = 1/(e**j)
return (int_time, elasticity)
decay=0
if t > 0:
v=10
t1, elasticity = set_bounce_parametrs(t, duration, e)
decay = amplitude * (((g)*((t1)**2)) + (v*t1))
if decay <= 0:
decay = 0
decay*=elasticity
else: decay = 0
decay*=(-velocity)
return decay
###########################################################################
# Get object name
def get_obj_name(name):
if bpy.data.objects.get(name) is None:
return name
i = 1
while bpy.data.objects.get(name + str(i)) is not None:
i += 1
return name + str(i)
# add a property
def add_prop(obj, name, value, min, max, description):
obj[name] = value
if '_RNA_UI' not in obj.keys():
obj['_RNA_UI'] = {}
obj['_RNA_UI'][name] = {"min": min, "max": max ,"description": description}
# add a property variable to the driver
def add_prop_var(driver, name, id_type, id, path):
var = driver.driver.variables.new()
var.name = name
var.type = 'SINGLE_PROP'
var.targets[0].id_type = id_type
var.targets[0].id = id
var.targets[0].data_path = path
# list of texts
def texts(self, context):
return [(text.name, text.name, '') for text in bpy.data.texts]
# list of fonts
def fonts(self, context):
return [(text.name, text.name, '') for text in bpy.data.fonts]
# list of audio files
def sounds(self, context):
scn = context.scene
prefix = 'TextFX_Audio_'
return [(s.replace(prefix, ''), s.replace(prefix, ''), '') for s in scn.keys() if prefix in s]
# Setup the font (Simple text)
def setup_font():
bpy.ops.object.text_add()
font = bpy.context.object
font.name = get_obj_name('TFX_Simple')
add_prop(font, 'TEXTFX_FONT', 0.0, 0.0, 1.0, '')
add_prop(font, 'start_frame', 1, 1, 1000000, 'First frame of the animation')
add_prop(font, 'typewriter_start_frame', 1, 1, 1000000, 'First frame of the animation')
add_prop(font, 'scramble_start_frame', 1, 1, 1000000, 'First frame of the animation')
add_prop(font, 'timer_start_frame', 1, 1, 1000000, 'First frame of the animation')
add_prop(font, 'read_lines_start_frame', 1, 1, 1000000, 'First frame of the animation')
add_prop(font, 'start_number', 0.0, -1000000.0, 1000000.0, 'Start number')
add_prop(font, 'end_number', 100.0, -1000000.0, 1000000.0, 'End number')
add_prop(font, 'speed', 3, 1, 100, 'Speed of the animation (in Frames)')
add_prop(font, 'typewriter_speed', 3, 1, 100, 'Speed of the animation (in Frames)')
add_prop(font, 'scramble_speed', 3, 1, 100, 'Speed of the animation (in Frames)')
add_prop(font, 'scramble_seed', 1, 1, 100000, 'Random seed')
add_prop(font, 'read_lines_speed', 3, 1, 100, 'Speed of the animation (in Frames)')
add_prop(font, 'text', 'Text FX', 0.0, 1.0, 'Enter your text here')
add_prop(font, 'scramble_text', 'Text FX', 0.0, 1.0, 'Enter your text here')
add_prop(font, 'read_lines_text', '', 0.0, 1.0, 'Enter your text here')
add_prop(font, 'before', '', 0.0, 1.0, 'This text will apear before the number')
add_prop(font, 'after', '', 0.0, 1.0, 'This text will apear after the number')
add_prop(font, 'increment_by', 1.0, -10000000.0, 10000000.0, 'Increment by')
add_prop(font, 'decimals', 0, 0, 5, 'Decimals')
add_prop(font, 'cursor', '|', 0.0, 1.0, 'Cursor')
add_prop(font, 'cursor_start_frame', 1, 1, 100000, 'First frame of the cursor animation')
add_prop(font, 'cursor_speed', 20, 0, 100, 'Speed of the cursor blinking, 0 = No blinking')
add_prop(font, 'seconds', 1, 0, 59, 'Seconds')
add_prop(font, 'minutes', 1, 0, 59, 'Minutes')
add_prop(font, 'hours', 1, 0, 23, 'Hours')
add_prop(font, 'timer_inc', 1, -1, 1, 'Incrementation, 0 = disabled')
font.text_fx_enabled = True
font.simple_effect = 'None'
font.typewriting_affect = 'Letters'
font.scramble_affect = 'Letters'
font.timer_format = 'hh:mm:ss'
font.scramble_char_type = 'Same'
# Create the parent (controller) for advanced text
def add_parent():
bpy.context.scene['TextFX_Audio_None'] = 0.0
bpy.ops.object.empty_add(type='SPHERE', radius = 0.25)
parent = bpy.context.object
parent.name = get_obj_name('TFX_Controller')
parent['Advanced_Font_FX'] = 0
parent['Advanced_Font_Offset'] = 0
add_prop(parent, 'Text', 'Text FX', 0.0, 1.0, 'Text')
add_prop(parent, 'spacing', 0.07, -10.0, 10.0, 'Space between characters')
add_prop(parent, 'offset_source', '', 0.0, 1.0, 'Source object for the animation offset')
add_prop(parent, 'offset_speed', 3, 0, 100, 'Speed of the offset (0 = no offset)')
add_prop(parent, 'wave_speed', 2.0, 0.5, 20.0, 'Speed of the wave')
add_prop(parent, 'wave_amplitude', 1.0, 0.0, 10.0, 'Factor, 0 = No effect')
add_prop(parent, 'wave_frequency', 0.5, -5.0, 1.0, 'Length of the wave.')
add_prop(parent, 'wave_audio_influence', 1.0, 0.0, 100.0, 'Influence of the audio amplitude')
add_prop(parent, 'wave_audio_min', 0.0, 0.0, 10.0, 'Minimum value for the amplitude of the audio')
add_prop(parent, 'wiggle_speed', 2, 1, 200, 'Wiggle speed')
add_prop(parent, 'wiggle_factor', 0.05, 0.0, 20.0, 'Factor, 0 = No effect')
add_prop(parent, 'wiggle_seed', 1, 1, 100000, 'Random seed')
add_prop(parent, 'wiggle_audio_influence', 1.0, 0.0, 100.0, 'Influence of the audio amplitude')
add_prop(parent, 'wiggle_audio_min', 0.0, 0.0, 10.0, 'Minimum value for the amplitude of the audio')
add_prop(parent, 'copy_factor', 1.0, 0.0, 10.0, 'Factor, 0 = No effect')
add_prop(parent, 'copy_looping_offset', 2, 1, 100.0, 'Delay between iterations, (in Frames)')
add_prop(parent, 'copy_audio_influence', 1.0, 0.0, 100.0, 'Influence of the audio amplitude')
add_prop(parent, 'copy_audio_min', 0.0, 0.0, 10.0, 'Minimum value for the amplitude of the audio')
add_prop(parent, 'copy_start', 1, 1, 100000, 'Start frame of the effect')
add_prop(parent, 'seed', 1, 1, 10000, 'Random seed')
add_prop(parent, 'bake_start_frame', 1, 0, 1000000, 'Start frame for baking')
add_prop(parent, 'bake_end_frame', 250, 1, 1000000, 'End frame for baking')
add_prop(parent, 'low_frequency', 0.0, 0.0, 100000.0, 'Cutoff frequency of a high-pass filter that is applied to the audio data')
add_prop(parent, 'high_frequency', 100000.0, 0.0, 100000.0, 'Cutoff frequency of a low-pass filter that is applied to the audio data')
parent.offset_order = 'Left'
parent.wave_axis = (False, False, False)
parent.wiggle_axis = (False, False, False)
parent.text_layers = parent.layers
parent.advanced_text_fx_enabled = False
parent.wave_use_audio = False
parent.copy_use_audio = False
parent.wiggle_use_audio = False
parent.wiggle_ease = 'linearTween'
parent.copy_kinetic = 'None'
parent.sounds = 'None'
add_prop(parent, 'offset', 0.0, -1.0, 1.0, 'Offset the curve to adjust the width of a text')
add_prop(parent, 'extrude', 0.0, 0.0, 1000.0, 'Amount of curve extrusion when not using a bevel object')
add_prop(parent, 'depth', 0.0, 0.0, 1000.0, 'Bevel depth when not using a bevel object')
add_prop(parent, 'resolution', 0, 0, 32, 'Bevel resolution when depth is non-zero and no specific bevel object has been defined')
add_prop(parent, 'shear', 0.0, -1.0, 1.0, 'Italic angle of the characters')
add_prop(parent, 'overshoot_amp', 1.0, 1.0, 100.0, 'Amplitude of the overshoot effect.')
add_prop(parent, 'overshoot_freq', 0.6, 0.0, 20.0, 'Increase or decrease the number of bounces.')
add_prop(parent, 'overshoot_dur', 24, 6, 240, 'Duration of the effect.')
add_prop(parent, 'bounce_amp', 1.0, 1.0, 100.0, 'Amplitude of the bounce effect.')
add_prop(parent, 'bounce_ela', 2.0, 1.0, 5.0, 'Increase or decrease the number of bounces.')
add_prop(parent, 'bounce_dur', 24, 6, 240, 'Duration of the effect.')
return parent
# Create a letter for the advanced text
def add_char(parent):
bpy.ops.object.text_add()
char = bpy.context.object
char.name = get_obj_name('TFX_Char')
char['TextFX'] = ''
add_prop(char, 'x', 0.0, -50.0, 50.0, '')
add_prop(char, 'y', 0.0, -50.0, 50.0, '')
driver = char.data.driver_add('offset')
add_prop_var(driver, 'offset', 'OBJECT', parent, '["offset"]')
driver.driver.expression = 'offset'
driver = char.data.driver_add('extrude')
add_prop_var(driver, 'extrude', 'OBJECT', parent, '["extrude"]')
driver.driver.expression = 'extrude'
driver = char.data.driver_add('bevel_depth')
add_prop_var(driver, 'depth', 'OBJECT', parent, '["depth"]')
driver.driver.expression = 'depth'
driver = char.data.driver_add('bevel_resolution')
add_prop_var(driver, 'res', 'OBJECT', parent, '["resolution"]')
driver.driver.expression = 'res'
driver = char.data.driver_add('shear')
add_prop_var(driver, 'shear', 'OBJECT', parent, '["shear"]')
driver.driver.expression = 'shear'
return char
# Setup advanced Text
def setup_advanced_font(text = 'Text FX', action = 'Create'):
if text == '':
text = 'Text FX'
scn = bpy.context.scene
obj = scn.objects
cursor_location = (scn.cursor_location.x, scn.cursor_location.y, scn.cursor_location.z)
if action == 'Update':
parent = bpy.context.object
scn.cursor_location = obj.active.location
delete_text('update')
else: parent = add_parent()
text2 = text.replace(' ', '-')
loc = 0
for t, t2, i in zip(text, text2, range(len(text))):
ob = add_char(parent)
ob.data.body = t2
bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY')
ob.data.offset_y = 0.0
bpy.ops.object.empty_add(type='CUBE', radius=0.00)
empty = bpy.context.object
empty['TextFX'] = ''
empty.hide = True
empty.hide_select = True
ob.parent = empty
ob.location = (0.0,0.0,0.0)
empty.parent = parent
empty.location = (0.0,0.0,0.0)
driver = empty.driver_add('location', 0)
add_prop_var(driver, 'spacing', 'OBJECT', parent, '["spacing"]')
driver.driver.expression = str(loc + (ob.dimensions.x/2)) + '+ spacing * ' + str(i)
loc+= ob.dimensions.x
ob.data.body = t
ob['x'] = ob.data.offset_x
for ob in obj:
ob.select = False
parent.select = True
parent.offs_x = 0.0
parent.offs_y = 0.0
obj.active = parent
parent['Text'] = text
scn.cursor_location = cursor_location
# Delete advanced text
def delete_text(action = 'delete'):
parent = bpy.context.object
empties = parent.children
scn = bpy.context.scene
obj = scn.objects
if len(empties) >0:
for empty in empties:
texts = empty.children
for text in texts:
obj.unlink(text)
bpy.data.objects.remove(text)
obj.unlink(empty)
bpy.data.objects.remove(empty)
if action == 'delete':
obj.unlink(parent)
bpy.data.objects.remove(parent)
# calculate spacing
def calculate_spacing():
parent = bpy.context.object
scn = bpy.context.scene
obj = scn.objects
space = False
x = 0
if parent.scale.x >0:
if len(parent.children) >0:
for i, e in enumerate(parent.children) :
if 'TextFX' in e.keys():
if e.children[0].type == 'FONT' and 'TextFX' in e.children[0].keys():
e.children[0].data.font = bpy.data.fonts[scn.fonts]
if e.children[0].data.body == ' ':
e.children[0].data.body ='-'
space = True
x += e.children[0].dimensions.x/2 / parent.scale.x
e.animation_data.drivers[0].driver.expression = str(x) + '+spacing*'+str(i)
obj.active = e.children[0]
for ob in obj:
ob.select = False
e.children[0].select = True
bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY')
e.children[0].location = (0,0,0)
x+= e.children[0].dimensions.x/2/ parent.scale.x
e.children[0].data.offset_y = 0.0
e.children[0]['y'] = 0.0
e.children[0]['x'] = e.children[0].data.offset_x
if space:
e.children[0].data.body = ' '
space = False
obj.active = parent
for ob in obj:
ob.select = False
parent.select = True
parent.offs_x = 0.0
parent.offs_y = 0.0
else:
print('Scale of the controller must be greater than zero to perform this operation')
# Get available fcurves (loc, rot, scale)
def avail_fcurves(OBname):
scn = bpy.context.scene
obj = scn.objects
ob = obj[OBname]
fcs = []
index = []
if hasattr(ob.animation_data, 'action'):
if hasattr(ob.animation_data.action, 'fcurves'):
fc = ob.animation_data.action.fcurves
for i in range(len(fc)):
if fc[i].data_path in ['location', 'rotation_euler', 'scale']:
fcs.append(fc[i].data_path + str(fc[i].array_index))
index.append(i)
return (fcs, index)
# get the list of keyframe points -- return the frame and correspanding value
def get_keyframes(obj, fc_index):
kf = []
v = []
fc = obj.animation_data.action.fcurves
for k in fc[fc_index].keyframe_points:
kf.append(k.co[0])
v.append(k.co[1])
return (kf, v)
# get the last active keyframe
def get_last_active_frame(OBname, fc_index, frame):
kf, v = get_keyframes(OBname, fc_index)
last_kf = max(kf)
if len(kf) > 1:
if frame > kf[1] and frame <= max(kf):
for k in range(1,len(kf)):
if frame > kf[k] :
last_kf = kf[k]
return last_kf
# get the type of a letter
def char_type(char):
type = ''
if char.islower():
type = string.ascii_lowercase
elif char.isupper():
type = string.ascii_uppercase
elif char.isdigit():
type = string.digits
elif char.isspace():
type = ' '
else : type = string.punctuation
return type
# generate random text
def random_chars(text, type, seed):
random.seed(seed)
i = ''
if type == 'Same':
random_slice = i.join(random.choice(char_type(i)) for i in text)
elif type == 'Digits':
random_slice = i.join(random.choice(string.digits) for i in text)
elif type == 'Letters':
random_slice = i.join(random.choice(string.ascii_letters) for i in text)
elif type == 'Lower case':
random_slice = i.join(random.choice(string.ascii_lowercase) for i in text)
elif type == 'Upper case':
random_slice = i.join(random.choice(string.ascii_uppercase) for i in text)
elif type == 'Punctuation':
random_slice = i.join(random.choice(string.punctuation) for i in text)
else:
random_slice = i.join(random.choice(string.printable[0:68]+string.printable[69:94]) for i in text)
return random_slice
# combine a list of lines into one string
def combine_lines(list):
text = ''
for line in list:
text += (line+'\n')
return text
# increment effect (numbers animation)
def increment(start_frame, start_number, end_number, before, after, speed, inc, dec, cur_frame):
body = ''
if cur_frame > start_frame:
slice = (cur_frame-start_frame)//speed
if dec >0:
decimals = '%.' + str(dec) + 'f'
body = before + (str(decimals % end_number) if (
(((slice*inc)+(start_number) > end_number) and (inc > 0)) or (((slice*inc)+(start_number) < end_number) and (inc < 0))
) else str(decimals % ((slice*inc)+(start_number)))) + after
else: body = before + (str(int(end_number)) if (
(((slice*inc)+(start_number) > end_number) and (inc > 0)) or (((slice*inc)+(start_number) < end_number) and (inc < 0))
) else str(int((slice*inc)+(start_number)))) + after
elif cur_frame == start_frame:
if dec >0:
decimals = '%.' + str(dec) + 'f'
body = before + str(decimals % start_number) + after
else:
body = before + str(int(start_number)) + after
return body
# typewriting effect
def typewriter(start, speed, text, affect, cursor, cursor_start_frame, cursor_speed, cur_frame):
count = len(text)
string = ''
body = ''
cur = ''
if cur_frame > start:
slice = (cur_frame-start)//speed
if affect == 'Letters':
string = text[0:min(slice, count)]
elif affect == 'Lines':
lines = text.splitlines()
if slice <= len(lines):
string = combine_lines(lines[0:slice])
else:
string = text
if cursor_speed == 0:
cur= cursor
else:
if cur_frame > cursor_start_frame:
if (cur_frame-start) % cursor_speed > cursor_speed/2:
cur = cursor
body = string+cur
return body
# scrambling effect
def scramble(text, start, affect, speed, char_type, seed, cur_frame):
body = ''
ran = ''
count = len(text)
if cur_frame > start:
if affect == 'Letters':
slice = (cur_frame-start)//speed
if slice != count:
ran = random_chars(text[slice:count], char_type, cur_frame+seed)
else:
ran = ''
body = text[0:min(slice, count)] + ran
elif affect == 'Words':
words = text.split()
count_w = len(words)
slice = (cur_frame-start)//speed
w = ''
if slice <= count_w:
for i in range(slice):
w += words[i] + ' '
ran = random_chars(text[len(w):count], char_type, cur_frame+seed)
else:
ran = text
body = text[0:min(len(w), count)] + ran
elif affect == 'Line':
body = text
else:
body = random_chars(text, char_type, cur_frame+seed)
return body
# timer effect
def timer(start, sec, min, hou, inc, format, fps, cur_frame):
"""
# get local time (useless)
time = time.strftime("%H:%M:%S", time.localtime())
"""
seconds = sec + (min * 60) + (hou*3600)
if cur_frame - start > fps:
fac = (cur_frame - start) // fps
seconds += fac * inc
if format == 'ss':
body = time.strftime("%S", time.gmtime(seconds))
elif format == 'mm:ss':
body = time.strftime("%M:%S", time.gmtime(seconds))
elif format == 'hh:mm':
body = time.strftime("%H:%M", time.gmtime(seconds))
else:
body = time.strftime("%H:%M:%S", time.gmtime(seconds))
return body
# Read Lines effect
def read_lines(text, start, speed, cur_frame):
body = ''
if text in bpy.data.texts.keys():
text = bpy.data.texts[text]
if cur_frame >= start + speed:
time = (cur_frame - start) // speed
if time in range(len(text.lines)):
body = text.lines[time-1].body
else:
body = text.lines[len(text.lines)-1].body
return body
# Sine wave animation
def wave(speed, frequency, amplitude, index, axis, sound, frame):
x = (amplitude+sound)*(sin((frame/speed)+index*(1-frequency))) if axis[0] else 0
y = (amplitude+sound)*(sin((frame/speed)+index*(1-frequency))) if axis[1] else 0
z = (amplitude+sound)*(sin((frame/speed)+index*(1-frequency))) if axis[2] else 0
# Triangle wave (It works but needs more polishing)
# y = (2*amplitude/pi)*asin(sin((2*pi/frequency)*((frame/(speed*5))+index))) if axis[1] else 0
return (x, y, z)
# Wiggle animation
def wiggle(speed, factor, index, axis, seed, sync, ease, sound, frame):
x, y, z = (0, 0, 0)
if not sync:
frame += index
start_frame = frame - (frame%speed)
end_frame = start_frame + speed
random.seed(start_frame+index+seed)
loc_x = random.uniform(-factor - sound, factor + sound)
random.seed(end_frame+index+seed)
loc_x_1 = random.uniform(-factor - sound, factor + sound)
random.seed(start_frame+index+seed+1)
loc_y = random.uniform(-factor - sound, factor + sound)
random.seed(end_frame+index+seed+1)
loc_y_1 = random.uniform(-factor - sound, factor + sound)
random.seed(start_frame+index+seed+2)
loc_z = random.uniform(-factor - sound, factor + sound)
random.seed(end_frame+index+seed+2)
loc_z_1 = random.uniform(-factor - sound, factor + sound)
# Linear interpolation
t= (frame - start_frame) / (end_frame-start_frame)
# Easing
x = eval(ease + '(t, loc_x, loc_x_1 - loc_x, 1.0) if axis[0] else 0')
y = eval(ease + '(t, loc_y, loc_y_1 - loc_y, 1.0) if axis[1] else 0')
z = eval(ease + '(t, loc_z, loc_z_1 - loc_z, 1.0) if axis[2] else 0')
# Quadric Bezier interpolation (don't work well with the current method)
#x = ((1-t)*(1-t)*loc_x) + (2*(1-t)*t*((loc_x_1+factor)))+(t*t*loc_x_1)
#y = ((1-t)*(1-t)*loc_y) + (2*(1-t)*t*((loc_y_1+factor)))+(t*t*loc_y_1)
#z = ((1-t)*(1-t)*loc_z) + (2*(1-t)*t*((loc_z_1-loc_z)/2))+(t*t*loc_z_1)
# Linear (this one work well, but Easing is much better)
#x = (1-t)*loc_x + t*loc_x_1 if axis[0] else 0
#y = (1-t)*loc_y + t*loc_y_1 if axis[1] else 0
#z = (1-t)*loc_z + t*loc_z_1 if axis[2] else 0
return (x, y, z)
# Copy animation with offset.
def copy_offset(source, speed, order, factor, loop, loop_offset, count, sound, kinetic, over_amp, over_freq, over_dur, bounce_amp, bounce_ela, bounce_dur, i, frame):
scn = bpy.context.scene
obj = scn.objects
location = [0.0 ,0.0 ,0.0]
rotation_euler = [0.0 ,0.0 ,0.0]
scale = [1.0 ,1.0 ,1.0]
if factor >0 or sound>0:
if obj.get(source) is not None:
src = obj[source]
fcs, index = avail_fcurves(source)
if fcs != []:
action = src.animation_data.action
for fc, ind in zip(fcs, index):
if loop:
start = action.frame_range[0]
end = action.frame_range[1]
frame_range = end - start+loop_offset
duration = frame_range + (speed*count)
inc = frame // duration
evaluate = start-1+(frame-inc*duration)-(i*speed)
else:
evaluate = frame-(i*speed)
if kinetic != 'None':
cur = action.fcurves[ind].evaluate(evaluate)
prev = action.fcurves[ind].evaluate(evaluate-1)
if cur == prev:
active_frame = get_last_active_frame(src, ind, evaluate)
t = evaluate - active_frame
cur = action.fcurves[ind].evaluate(active_frame)
prev = action.fcurves[ind].evaluate(active_frame-1)
velocity = cur - prev
if kinetic == 'Bounce':
decay = bounce(t, velocity, bounce_amp, bounce_ela, bounce_dur)
elif kinetic == 'Overshoot':
decay = overshoot(t, velocity, over_amp, over_freq, over_dur)
else: decay = 0.0
else:
decay = 0.0
if 'location' in fc:
location[int(fc[-1:])] = (action.fcurves[ind].evaluate(evaluate) * (factor+sound)) + decay
elif 'rotation_euler' in fc:
rotation_euler[int(fc[-1:])] = (action.fcurves[ind].evaluate(evaluate) * (factor+sound)) + decay
elif 'scale' in fc:
scale[int(fc[-1:])] = (action.fcurves[ind].evaluate(evaluate) * max(1.0, (factor + sound))) + decay
return (tuple(location), tuple(rotation_euler), tuple(scale))
# Update text
@persistent
def text_fx_font_animation_update(scn):
""" Function for updating the effects when the scene update """
cur_frame = scn.frame_current
# Update simple text
for ob in scn.objects:
if ob.text_fx_enabled and 'TEXTFX_FONT' in ob.keys():
fps = scn.render.fps
start = ob["start_frame"]
typewriter_start = ob["typewriter_start_frame"]
scramble_start = ob["scramble_start_frame"]
timer_start = ob["timer_start_frame"]
read_lines_start = ob["read_lines_start_frame"]
start_number = ob["start_number"]
end_number = ob["end_number"]
before = ob['before']
after = ob['after']
speed = ob["speed"]
typewriter_speed = ob["typewriter_speed"]
scramble_speed = ob["scramble_speed"]
read_lines_speed = ob["read_lines_speed"]
numbers_inc = ob['increment_by']
dec = ob['decimals']
text = ob["text"]
scramble_text = ob["scramble_text"]
scramble_seed = ob["scramble_seed"]
read_lines_text = ob["read_lines_text"]
typewriting_affect = ob.typewriting_affect
cursor = ob["cursor"]
cursor_start = ob["cursor_start_frame"]
cursor_speed = ob["cursor_speed"]
scrambling_affect = ob.scramble_affect
speed = ob["speed"]
sec = ob["seconds"]
min = ob["minutes"]
hou = ob["hours"]
timer_inc = ob['timer_inc']
format = ob.timer_format
char_type = ob.scramble_char_type
if ob.simple_effect == 'Increment':
if start_number > end_number and numbers_inc >0:
ob['increment_by'] = -numbers_inc
elif start_number < end_number and numbers_inc < 0:
ob['increment_by'] = -numbers_inc
ob.data.body = increment(start, start_number, end_number, before, after, speed, numbers_inc, dec, cur_frame)
elif ob.simple_effect == 'Typewriter':
ob.data.body = typewriter(typewriter_start, typewriter_speed, text, typewriting_affect, cursor, cursor_start, cursor_speed, cur_frame)
elif ob.simple_effect == 'Scramble':
ob.data.body = scramble(scramble_text, scramble_start, scrambling_affect, scramble_speed, char_type, scramble_seed, cur_frame)
elif ob.simple_effect == 'Timer':
ob.data.body = timer(timer_start, sec, min, hou, timer_inc, format, fps, cur_frame)
elif ob.simple_effect == 'Read Lines':
ob.data.body = read_lines(read_lines_text, read_lines_start, read_lines_speed, cur_frame)
# Update advanced text
elif ob.type == 'EMPTY' and 'Advanced_Font_FX' in ob.keys():
if ob.advanced_text_fx_enabled and len(ob.children) >0:
sound = ob.sounds
wave_speed = ob['wave_speed']
wave_frequency = ob['wave_frequency']
wave_amplitude = ob['wave_amplitude']
wave_audio_influence = ob['wave_audio_influence']
min_wave = ob['wave_audio_min']
wave_use_audio = ob.wave_use_audio
wave_axis = ob.wave_axis
wave_sound = 0
wiggle_speed = ob['wiggle_speed']
wiggle_factor = ob['wiggle_factor']
wiggle_seed = ob['wiggle_seed']
wiggle_audio_influence = ob['wiggle_audio_influence']
min_wiggle = ob['wiggle_audio_min']
wiggle_use_audio = ob.wiggle_use_audio
wiggle_axis = ob.wiggle_axis
wiggle_sync = ob.wiggle_synchronize
wiggle_ease = ob.wiggle_ease
wiggle_sound = 0
source = ob['offset_source']
offset_speed = ob['offset_speed']
order = ob.offset_order
copy_use_audio = ob.copy_use_audio
copy_factor = ob['copy_factor']
loop_offset = ob['copy_looping_offset']
copy_audio_influence = ob['copy_audio_influence']
min_copy = ob['copy_audio_min']
start = ob['copy_start']
seed = ob['seed']
over_amp = ob['overshoot_amp']
over_freq = ob['overshoot_freq']
over_dur = ob['overshoot_dur']
bounce_amp = ob['bounce_amp']
bounce_ela = ob['bounce_ela']
bounce_dur = ob['bounce_dur']
loop = ob.copy_loop
kinetic = ob.copy_kinetic
copy_sound = 0
frame = 1
children = list(ob.children)
for i, child in enumerate(children):
ch = child.children[0]
if sound != '' and sound != 'None':
aud = scn['TextFX_Audio_' + sound]
if wave_use_audio:
wave_sound = wave_audio_influence * aud if aud > min_wave else 0
if wiggle_use_audio:
wiggle_sound = wiggle_audio_influence * aud if aud > min_wiggle else 0
if copy_use_audio:
copy_sound = copy_audio_influence * aud if aud > min_copy else 0
wav = wave(wave_speed, wave_frequency, wave_amplitude, i, wave_axis, wave_sound, cur_frame)
wig = wiggle(wiggle_speed, wiggle_factor, i, wiggle_axis, wiggle_seed, wiggle_sync, wiggle_ease, wiggle_sound, cur_frame)
if cur_frame > start:
frame = cur_frame - start + 1
if order == 'Right':
copy = copy_offset(source, offset_speed, order, copy_factor, loop, loop_offset, len(children)-1, copy_sound, kinetic, over_amp, over_freq, over_dur, bounce_amp, bounce_ela, bounce_dur, len(children)-1-i, frame)
elif order == 'Random':
random.seed(seed)
ran_children = list(range(len(children)))
random.shuffle(ran_children)
copy = copy_offset(source, offset_speed, order, copy_factor, loop, loop_offset, len(children)-1, copy_sound, kinetic, over_amp, over_freq, over_dur, bounce_amp, bounce_ela, bounce_dur, ran_children[i], frame)
else: copy = copy_offset(source, offset_speed, order, copy_factor, loop, loop_offset, len(children)-1, copy_sound, kinetic, over_amp, over_freq, over_dur, bounce_amp, bounce_ela, bounce_dur, i, frame)
ch.delta_location = tuple(map(lambda x, y, z: x + y + z, wav, wig, copy[0]))
ch.delta_rotation_euler = copy[1]
ch.delta_scale = copy[2]
# update text list
def upd_txt_lst(self, context):
scn = bpy.context.scene
act = bpy.context.object
if act.simple_effect == 'Typewriter':
act['text'] = bpy.data.texts[scn.texts].as_string()
elif act.simple_effect == 'Read Lines':
act['read_lines_text'] = bpy.data.texts[scn.texts].name
return None
# update font list
def upd_font_lst(self, context):
scn = bpy.context.scene
act = bpy.context.object
if len(act.children) > 0:
for e in act.children:
if 'TextFX' in e.keys():
if e.children[0].type == 'FONT' and 'TextFX' in e.children[0].keys():
e.children[0].data.font = bpy.data.fonts[scn.fonts]
calculate_spacing()
return None
# update text layers
def upd_font_layers(self, context):
scn = bpy.context.scene
act = bpy.context.object
layers = act.text_layers
act.layers = layers
if len(act.children) > 0:
for e in act.children:
if 'TextFX' in e.keys():
e.layers = layers
if e.children[0].type == 'FONT' and 'TextFX' in e.children[0].keys():
e.children[0].layers = layers
return None
# update chars offset for the X axis
def upd_text_offset_x(self, context):
act = bpy.context.object
if len(act.children)>0:
x = act.offs_x
for e in act.children:
if 'TextFX' in e.keys() and len(e.children) >0:
offsx = e.children[0]['x']
e.children[0].data.offset_x = offsx + x
return None
# update chars offset for the Y axis
def upd_text_offset_y(self, context):
act = bpy.context.object
if len(act.children)>0:
y = act.offs_y
for e in act.children:
if 'TextFX' in e.keys() and len(e.children) >0:
offsy = e.children[0]['y']
e.children[0].data.offset_y = offsy + y
return None
# Load audio as keyframes
def load_audio(file_path):
scn = bpy.context.scene
ob = bpy.context.object
low_frequency = ob['low_frequency']
high_frequency = ob['high_frequency']
scn.frame_current = 1
file_name = os.path.basename(file_path)
prop_name = 'TextFX_Audio_' + file_name + ' (' + str(int(low_frequency)) + '-' + str(int(high_frequency)) + ')'
if prop_name not in scn.keys():
add_prop(scn, prop_name, 0.0, 0.0, 100.0, 'Audio file amplitude')
scn.keyframe_insert('["'+prop_name + '"]')
else:
if not hasattr(scn.animation_data, 'action'):
scn.keyframe_insert('["'+prop_name + '"]')
else:
ready = False
if hasattr(scn.animation_data.action, 'fcurves'):
fcs = scn.animation_data.action.fcurves
for f in fcs:
if f.data_path == '["'+prop_name + '"]':
ready = True
if not ready:
scn.keyframe_insert('["'+prop_name + '"]')
act_area = bpy.context.area.spaces.active.type
fcs = scn.animation_data.action.fcurves
for f in fcs:
if f.data_path != '["'+prop_name + '"]':
f.select = False
else: f.select = True
for area in bpy.context.screen.areas:
if area.type == act_area:
try:
area.type = 'GRAPH_EDITOR'
bpy.ops.graph.sound_bake(filepath = file_path, low = low_frequency, high = high_frequency)
except Exception as error:
print(error)
finally:
area.type = act_area
break
###############################################################
######################### Operators ###########################
###############################################################
# Setup the font
class TextFX(Operator):
bl_idname = "textfx.setup_font"
bl_label = "Simple"
bl_description = "Add simple text."
def execute(self, context):
setup_font()
return {'FINISHED'}