-
Notifications
You must be signed in to change notification settings - Fork 1
/
svganimator.py
executable file
·214 lines (194 loc) · 10.5 KB
/
svganimator.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
#! /usr/bin/env python3
import argparse
import sys
from copy import deepcopy
import xml.etree.ElementTree as ET
from io import IOBase
comment_string = 'Animation generated by svganimator (https://github.com/sangaline/svganimator/)'
#these are attributes that cannot be animated
blacklist = ['style', 'id', 'name', 'xlink:href']
def extract_style(element):
keys, values = [], []
if 'style' in element.keys():
for pair in element.get('style').split(';'):
if ':' in pair:
key, value = pair.split(':')
keys.append(key)
values.append(value)
return keys, values
def encode_style(element, keys, values):
s = ''
for i in range(len(keys)):
s += keys[i] + ':' + values[i] + ';'
element.set('style', s)
def set_style_attribute(element, key, value):
keys, values = extract_style(element)
if key in keys:
i = keys.index(key)
values[i] = value
else:
keys.append(key)
values.append(value)
encode_style(element, keys, values)
def get_style_attribute(element, key):
keys, values = extract_style(element)
if key in keys:
return values[keys.index(key)]
return None
class SvgAnimator(object):
def __init__(self, static, transition, loop, precision, basic):
self.static = static
self.transition = transition
self.repeatCount = str(loop) if loop > 0 else 'indefinite'
self.precision = precision
self.precision_string = '{:.' + str(precision) + 'g}'
self.basic = basic
def _generate_timings(self, number_of_frames):
self.total_time = round((self.static+self.transition)*number_of_frames, self.precision)
self.transition_times = ['0']
for i in range(number_of_frames):
first = (self.static+self.transition)*i + self.static
second = first + self.transition
self.transition_times.append(self.precision_string.format(first/self.total_time))
self.transition_times.append(self.precision_string.format(second/self.total_time))
self.duration = self.precision_string.format(self.total_time) + 's'
def _basic_animate(self, result, frames):
attributes = { 'attributeName' : 'opacity', 'attributeType' : 'CSS', 'begin' : '0s',
'dur' : self.duration, 'repeatCount' : self.repeatCount}
for i, frame in enumerate(frames):
g = ET.SubElement(result, 'g', { 'style' : 'opacity:' + ('1' if i == 0 else '0') + ';'})
if i == 0 or i == len(frames)-1:
attributes['values'] = '1;1;0;0;1' if i == 0 else '0;0;1;1;0'
indices = [0, 1, 2, -2, -1] if i == 0 else [0, -4, -3, -2, -1]
attributes['keyTimes'] = '{:s};{:s};{:s};{:s};{:s}'.format(*[self.transition_times[index] for index in indices])
else:
attributes['values'] = '0;0;1;1;0;0'
indices = [0, i*2-1, i*2, i*2+1, i*2+2, -1]
attributes['keyTimes'] = '{:s};{:s};{:s};{:s};{:s};{:s}'.format(*[self.transition_times[index] for index in indices])
ET.SubElement(g, 'animate', attributes)
g.extend(frame.getchildren())
def _experimental_animate(self, result, frames):
frame_children = [frame.getchildren() for frame in frames]
child_lengths = [len(children) for children in frame_children]
max_length = max(child_lengths)
child_index = 0
while True:
if child_index >= max_length:
return
current_children = []
#the node with the most children is considered most 'complete'
best_frame = frame_children[child_lengths.index(max_length)]
force_opacity = False
for i, frame in enumerate(frames):
#this is a very shallow comparison but works well in some scenarios
if child_index >= child_lengths[i] or frame[child_index].tag != best_frame[child_index].tag:
copied_node = ET.Element(best_frame[child_index].tag, best_frame[child_index].attrib)
set_style_attribute(copied_node, 'opacity', '0')
force_opacity = True
frame.insert(child_index, copied_node)
frame_children[i].insert(child_index, copied_node)
child_lengths[i] += 1
max_length = max((child_lengths[i], max_length))
current_children.append(frame_children[i][child_index])
if force_opacity:
for child in current_children:
if get_style_attribute(child, 'opacity') == None:
set_style_attribute(child, 'opacity', '1')
first_frame_child = frames[0].getchildren()[child_index]
result_child = ET.SubElement(result, first_frame_child.tag, first_frame_child.attrib)
keys = result_child.keys()
for badkey in blacklist:
if 'badkey' in keys:
keys.remove('badkey')
values = [result_child.get(key) for key in keys]
styles, style_values = extract_style(result_child)
attributes = { 'repeatCount' : self.repeatCount, 'begin' : '0s', 'dur' : self.duration }
number_of_keys = len(keys)
for attribute_index in range(number_of_keys + len(styles)):
animated = False
attributes['keyTimes'] = '0'
if attribute_index < number_of_keys:
attributes['attributeName'] = keys[attribute_index]
attributes['attributeType'] = 'XML'
initial_value = values[attribute_index]
current_value = initial_value
attributes['values'] = current_value
for i, child in enumerate(current_children):
if attributes['attributeName'] in child.keys():
new_value = child.get(attributes['attributeName'])
if current_value != new_value:
animated = True
attributes['values'] += ';' + current_value + ';' + new_value
attributes['keyTimes'] += ';' + self.transition_times[i*2-1] + ';' + self.transition_times[i*2]
current_value = new_value
else:
attribute_index -= number_of_keys
attributes['attributeName'] = styles[attribute_index]
attributes['attributeType'] = 'CSS'
initial_value = style_values[attribute_index]
current_value = initial_value
attributes['values'] = current_value
for i, child in enumerate(current_children):
child_styles, child_values = extract_style(child)
if attributes['attributeName'] in child_styles:
new_value = child_values[child_styles.index(attributes['attributeName'])]
if current_value != new_value:
animated = True
attributes['values'] += ';' + current_value + ';' + new_value
attributes['keyTimes'] += ';' + self.transition_times[i*2-1] + ';' + self.transition_times[i*2]
current_value = new_value
if animated:
attributes['values'] += ';' + current_value + ';' + initial_value
attributes['keyTimes'] += ';' + self.transition_times[-2] + ';1'
ET.SubElement(result_child, 'animate', attributes)
#apply recursively down the tree
self._experimental_animate(result_child, current_children)
child_index += 1
def animate(self, output, inputs, close=True):
roots = []
for input in inputs:
tree = ET.ElementTree(file=input)
roots.append(tree.getroot())
if close and isinstance(input, IOBase):
input.close()
assert roots[-1].tag.split('}')[1] == 'svg'
self._generate_timings(len(roots))
result = ET.Element(roots[0].tag)
result.attrib = roots[0].attrib
result.append(ET.Comment(comment_string))
if self.basic:
self._basic_animate(result, roots)
else:
self._experimental_animate(result, roots)
tree = ET.ElementTree(result)
namespace = result.tag.split('}')[0][1:]
ET.register_namespace('', namespace)
tree.write(output, xml_declaration=True)
if close and isinstance(output, IOBase):
output.close()
if __name__ == '__main__':
#Setup and parse the arguments
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('output_file', help='The name of the SVG file that the animation will be written to.',
type=argparse.FileType('wb'))
parser.add_argument('-b', '--basic', help='Basic mode. This is more likely to work but will have larger file sizes and ' +
'transitions will only be fades not interpolations.', action='store_true')
parser.add_argument('-s', '--static', help='The number of seconds to statically display each svg.',
type=float, default = 1.0, metavar='STATIC_DURATION')
parser.add_argument('-t', '--transition', help='The number of seconds to transition between each svg.',
type=float, default = 0.0, metavar='TRANSITION_DURATION')
parser.add_argument('-l', '--loop', help='The number of times to repeat the animation. A value of 0 will loop indefinitely.',
type=int, default=0, metavar='NUMBER_OF_LOOPS')
parser.add_argument('-p', '--precision', help='The precision for animation timings. Can make a significant difference in file sizes.',
type=int, default=6)
parser.add_argument('input_file', help='An SVG filename for each static frame of the animation.',
type=argparse.FileType('r'), nargs='+')
args = parser.parse_args()
if len(args.input_file) == 1:
args.output_file.write(args.input_file[0].read())
args.output_file.close()
args.input_file[0].close()
sys.exit()
animator = SvgAnimator(static=args.static, transition=args.transition, loop=args.loop,
precision=args.precision, basic=args.basic)
animator.animate(output=args.output_file, inputs=args.input_file, close=True)