-
Notifications
You must be signed in to change notification settings - Fork 9
/
typewriter.py
185 lines (157 loc) · 5.82 KB
/
typewriter.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
# ##### 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 #####
# <pep8-80 compliant>
import bpy
from bpy.app.handlers import persistent
import random
bl_info = {
'name': 'Typewriter Text',
'description': 'Typewriter Text effect for font objects',
'author': 'Bassam Kurdali, Vilem Novak, Jimmy Berry',
'version': (0, 3, 1),
'blender': (2, 7, 0),
'location': 'Properties Editor, Text Context',
'url': 'https://github.com/boombatower/blender-typewriter-addon',
'category': 'Text'}
def randomize(t,width):
nt=''
lines=t.splitlines()
totlen=len(t)
i=0
for l in lines:
for ch in l:
if i>totlen+1-width:
nt+=random.choice(l)
else:
nt+=ch
i+=1
nt+='\n'
i+=2
return(nt)
def uptext(text):
'''
slice the source text up to the character_count
'''
source = text.source_text
if source in bpy.data.texts:
if text.separator!='': strings=bpy.data.texts[source].as_string().split(text.separator)
else:
strings=[bpy.data.texts[source].as_string()]
idx=min(len(strings),text.text_index)
t=strings[idx]
#remove line endings after separator
while t.find('\n')==0:
t=t[1:]
else:
t = source
#randomize
if text.use_randomize and len(t)>text.character_count:
t=randomize(t[:text.character_count],text.randomize_width)
prefix = ''
if text.preserve_newline and text.character_start > 0:
prefix = '\n' * t.count('\n', 0, text.character_start)
if text.preserve_space and text.character_start > 0:
prefix += ' ' * (text.character_start - (t.rfind('\n', 0, text.character_start) + 1))
text.body = prefix + t[text.character_start:text.character_count]
@persistent
def typewriter_text_update_frame(scene):
'''
sadly we need this for frame change updating
'''
for text in scene.objects:
if text.type == 'FONT' and text.data.use_animated_text:
uptext(text.data)
def update_func(self, context):
'''
updates when changing the value
'''
uptext(self)
class TEXT_PT_Typewriter(bpy.types.Panel):
'''
Typewriter Effect Panel
'''
bl_label = "Typewriter Effect"
bl_idname = "TEXT_PT_Typewriter"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = 'data'
@classmethod
def poll(cls, context):
return context.active_object and context.active_object.type == 'FONT'
def draw_header(self, context):
text = context.active_object.data
layout = self.layout
layout.prop(text, 'use_animated_text', text="")
def draw(self, context):
st = context.space_data
text = context.active_object.data
layout = self.layout
layout.prop(text,'character_start')
layout.prop(text,'preserve_newline')
layout.prop(text,'preserve_space')
layout.prop(text,'character_count')
layout.prop(text,'source_text')
if text.source_text in bpy.data.texts:
layout.prop(text,'separator')
layout.prop(text,'text_index')
layout.prop(text,'use_randomize')
if text.use_randomize:
layout.prop(text,'randomize_width')
def register():
'''
addon registration function
'''
# create properties
bpy.types.TextCurve.character_start = bpy.props.IntProperty(
name="character_start",update=update_func, min=0, options={'ANIMATABLE'})
bpy.types.TextCurve.preserve_newline = bpy.props.BoolProperty(
name="preserve_newline", default=True)
bpy.types.TextCurve.preserve_space = bpy.props.BoolProperty(
name="preserve_space", default=True)
bpy.types.TextCurve.character_count = bpy.props.IntProperty(
name="character_count",update=update_func, min=0, options={'ANIMATABLE'})
bpy.types.TextCurve.backup_text = bpy.props.StringProperty(
name="backup_text")
bpy.types.TextCurve.use_animated_text = bpy.props.BoolProperty(
name="use_animated_text", default=False)
bpy.types.TextCurve.source_text = bpy.props.StringProperty(
name="source_text")
bpy.types.TextCurve.text_index = bpy.props.IntProperty(
name="index",update=update_func, min=0, options={'ANIMATABLE'})
bpy.types.TextCurve.separator = bpy.props.StringProperty(
name="separator", default='#' )
bpy.types.TextCurve.use_randomize = bpy.props.BoolProperty(
name="randomize", default=False)
bpy.types.TextCurve.randomize_width = bpy.props.IntProperty(
name="randomize width",update=update_func, default=10, min=0, options={'ANIMATABLE'})
# register the module:
bpy.utils.register_module(__name__)
# add the frame change handler
bpy.app.handlers.scene_update_pre.append(typewriter_text_update_frame)
def unregister():
'''
addon unregistration function
'''
# remove the frame change handler
bpy.app.handlers.scene_update_pre.remove(typewriter_text_update_frame)
# remove the properties
# XXX but how???
# remove the panel
bpy.utils.unregister_module(__name__)
if __name__ == "__main__":
register()