forked from boombatower/blender-typewriter-addon
-
Notifications
You must be signed in to change notification settings - Fork 5
/
__init__.py
229 lines (184 loc) · 6.58 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
# ##### 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
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, 2),
'blender': (2, 80, 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, eval_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 eval_text.use_randomize and len(t) > eval_text.character_count:
t = randomize(t[:eval_text.character_count], eval_text.randomize_width)
prefix = ''
if eval_text.preserve_newline and eval_text.character_start > 0:
prefix = '\n' * t.count('\n', 0, eval_text.character_start)
if eval_text.preserve_space and eval_text.character_start > 0:
prefix += ' ' * (eval_text.character_start - (t.rfind('\n', 0, eval_text.character_start) + 1))
new_text = prefix + t[eval_text.character_start:eval_text.character_count]
count = eval_text.character_count
text.body = new_text
@persistent
def typewriter_text_update_frame(scene, depsgraph):
'''
sadly we need this for frame change updating
'''
for text in scene.objects:
if text.type == 'FONT' and text.data.use_animated_text:
eval_text = text.evaluated_get(depsgraph)
uptext(text.data, eval_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,'preserve_newline', text="Preserve Newline")
layout.prop(text,'preserve_space', text="Preserve Space")
layout.prop(text,'source_text', text="Source Text")
layout.prop(text,'character_start', text="Character Start")
layout.prop(text,'character_count', text="Character Count")
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')
classes = [
TEXT_PT_Typewriter
]
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.locked = bpy.props.BoolProperty(
name="locked", default=True)
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,
default=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:
from bpy.utils import register_class
for cls in classes:
register_class(cls)
# add the frame change handler
bpy.app.handlers.frame_change_post.append(typewriter_text_update_frame)
def unregister():
'''
addon unregistration function
'''
# remove the frame change handler
bpy.app.handlers.frame_change_post.remove(typewriter_text_update_frame)
# remove the panel
from bpy.utils import unregister_class
for cls in reversed(classes):
unregister_class(cls)
if __name__ == "__main__":
register()