Mercurial > MadButterfly
annotate pyink/tween.py @ 1146:e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
author | wycc |
---|---|
date | Fri, 24 Dec 2010 14:44:07 +0800 |
parents | 8f0ee167c5b2 |
children | 6586cd10c92f |
rev | line source |
---|---|
1140 | 1 # -*- indent-tabs-mode: t; tab-width: 8; python-indent: 4; -*- |
2 # vim: sw=4:ts=8:sts=4 | |
3 import traceback | |
4 import math | |
5 class TweenObject: | |
6 def __init__(self,doc,dom): | |
7 self.document = doc | |
8 self.dom = dom | |
1141 | 9 try: |
10 self.width = float(dom.getAttribute("width")) | |
11 self.height = float(dom.getAttribute("height")) | |
12 except: | |
13 self.width = 640 | |
14 self.height = 480 | |
1140 | 15 |
16 def updateMapping(self): | |
17 self.nodeToItem={} | |
18 root = self.dom | |
19 self.updateMappingNode(root) | |
20 def updateMappingNode(self,node): | |
21 for c in node.childList(): | |
22 self.updateMappingNode(c) | |
1141 | 23 try: |
24 self.nodeToItem[c.getAttribute("id")] = c | |
25 except: | |
26 pass | |
1140 | 27 def updateTweenContent(self,obj, typ, source,dest,cur): |
28 """ | |
29 Update the content of the duplicate scene group. We will use the (start,end) and cur to calculate the percentage of | |
30 the tween motion effect and then use it to update the transform matrix of the duplicated scene group. | |
31 """ | |
32 | |
33 start = source.idx | |
34 end = dest.idx | |
35 print cur,start,end | |
36 percent = (cur-start)*1.0/(end-start) | |
37 i = 0 | |
38 s = source.ref.firstChild() | |
39 d = dest.ref.firstChild() | |
40 sources={} | |
41 dests={} | |
1146
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
42 # Collect ref from the obj |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
43 o = obj.firstChild() |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
44 maps={} |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
45 while o: |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
46 print "--->",o |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
47 try: |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
48 ref = o.getAttribute("ref") |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
49 except: |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
50 print o |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
51 ref = None |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
52 |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
53 if ref: |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
54 maps[ref] = o |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
55 o = o.next() |
1140 | 56 # Collect all objects |
57 while d: | |
58 try: | |
1141 | 59 label = d.getAttribute("inkscape:label") |
1140 | 60 except: |
1141 | 61 d = d.next() |
1140 | 62 continue |
63 dests[label] = d | |
1141 | 64 d = d.next() |
1140 | 65 # Check if the object in the source exists in the destination |
66 s = source.ref.firstChild() | |
67 d = dest.ref.firstChild() | |
68 while s: | |
69 print s,d | |
1146
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
70 sid = s.getAttribute("id") |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
71 if maps.has_key(sid): |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
72 o = maps[sid] |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
73 else: |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
74 o = None |
1140 | 75 try: |
1141 | 76 label = s.getAttribute("inkscape:label") |
1140 | 77 # Use i8nkscape:label to identidy the equipvalent objects |
78 if label: | |
79 if dests.hasattr(label.value()): | |
1146
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
80 self.updateTweenObject(obj,typ,s,dests[label.value()],percent,o) |
1141 | 81 s = s.next() |
1140 | 82 continue |
83 except: | |
84 pass | |
85 # Search obejcts in the destination | |
86 while d: | |
87 try: | |
1141 | 88 d.getAttribute("inkscape:label") |
89 d = d.next() | |
1140 | 90 continue |
91 except: | |
92 pass | |
93 if s.name() == d.name(): | |
1146
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
94 self.updateTweenObject(obj,typ,s,d,percent,o) |
1141 | 95 d = d.next() |
1140 | 96 break |
1141 | 97 d = d.next() |
98 s = s.next() | |
1140 | 99 |
100 def parseTransform(self,obj): | |
101 """ | |
102 Return the transform matrix of an object | |
103 """ | |
104 try: | |
1141 | 105 t = obj.getAttribute("transform") |
1140 | 106 print t |
107 if t[0:9] == 'translate': | |
108 print "translate" | |
109 fields = t[10:].split(',') | |
110 x = float(fields[0]) | |
111 fields = fields[1].split(')') | |
112 y = float(fields[0]) | |
113 return [1,0,0,1,x,y] | |
114 elif t[0:6] == 'matrix': | |
115 print "matrix" | |
116 fields=t[7:].split(')') | |
117 fields = fields[0].split(',') | |
118 return [float(fields[0]),float(fields[1]),float(fields[2]),float(fields[3]),float(fields[4]),float(fields[5])] | |
119 except: | |
120 #traceback.print_exc() | |
121 return [1,0,0,1,0,0] | |
122 | |
123 def invA(self,m): | |
124 d = m[0]*m[3]-m[2]*m[1] | |
125 return [m[3]/d, -m[1]/d, -m[2]/d, m[0]/d, (m[1]*m[5]-m[4]*m[3])/d, (m[4]*m[2]-m[0]*m[5])/d] | |
126 | |
127 def mulA(self,a,b): | |
128 return [a[0]*b[0]+a[1]*b[2], | |
129 a[0]*b[1]+a[1]*b[3], | |
130 a[2]*b[0]+a[3]*b[2], | |
131 a[2]*b[1]+a[3]*b[3], | |
132 a[0]*b[4]+a[1]*b[5]+a[4], | |
133 a[2]*b[4]+a[3]*b[5]+a[5]] | |
134 | |
135 def decomposition(self,m): | |
136 """ | |
137 Decompose the affine matrix into production of translation,rotation,shear and scale. | |
138 The algorithm is documented at http://lists.w3.org/Archives/Public/www-style/2010Jun/0602.html | |
139 """ | |
140 if m[0]*m[3] == m[1]*m[2]: | |
141 print "The affine matrix is singular" | |
142 return [1,0,0,1,0,0] | |
143 A=m[0] | |
144 B=m[2] | |
145 C=m[1] | |
146 D=m[3] | |
147 E=m[4] | |
148 F=m[5] | |
149 sx = math.sqrt(A*A+B*B) | |
150 A = A/sx | |
151 B = B/sx | |
152 shear = m[0]*m[1]+m[2]*m[3] | |
153 C = C - A*shear | |
154 D = D - B*shear | |
155 sy = math.sqrt(C*C+D*D) | |
156 C = C/sy | |
157 D = D/sy | |
158 r = A*D-B*C | |
159 if r == -1: | |
160 shear = -shear | |
161 sy = -sy | |
162 R = math.atan2(B,A) | |
163 return [sx,sy, R, E,F] | |
164 | |
165 | |
1146
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
166 def updateTweenObject(self,obj,typ,s,d,p,newobj): |
1140 | 167 """ |
168 Generate tweened object in the @obj by using s and d in the @p percent | |
169 http://lists.w3.org/Archives/Public/www-style/2010Jun/0602.html | |
170 """ | |
171 if typ == 'relocate': | |
172 newobj = s.duplicate(self.document) | |
173 top = self.document.createElement("svg:g") | |
1146
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
174 top.setAttribute("ref", s.getAttribute("id")) |
1140 | 175 top.appendChild(newobj) |
176 obj.appendChild(top) | |
177 if s.name() == 'svg:g': | |
178 # Parse the translate or matrix | |
179 sm = self.parseTransform(s) | |
180 dm = self.parseTransform(d) | |
181 top.setAttribute("transform","translate(%g,%g)" % ((dm[2]-sm[2])*p,(dm[5]-sm[5])*p)) | |
182 else: | |
183 try: | |
1141 | 184 sx = float(s.getAttribute("x")) |
185 sy = float(s.getAttribute("y")) | |
186 dx = float(d.getAttribute("x")) | |
187 dy = float(d.getAttribute("y")) | |
1140 | 188 tx = (dx-sx)*p |
189 ty = (dy-sy)*p | |
190 print tx,ty | |
191 top.setAttribute("transform","translate(%g,%g)" % (tx,ty)) | |
192 except: | |
193 traceback.print_exc() | |
194 pass | |
195 pass | |
196 elif typ == 'scale': | |
1146
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
197 self.updateTweenObjectScale(obj,s,d,p,newobj) |
1140 | 198 pass |
199 elif typ == 'normal': | |
200 newobj = s.duplicate(self.document) | |
1141 | 201 newobj.setAttribute("ref", s.getAttribute("id")) |
1140 | 202 top = self.document.createElement("svg:g") |
203 top.appendChild(newobj) | |
204 obj.appendChild(top) | |
205 pass | |
206 | |
1146
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
207 def updateTweenObjectScale(self,obj,s,d,p,newobj): |
1140 | 208 """ |
209 Generate a new group which contains the original group and then | |
210 add the transform matrix to generate a tween frame between the | |
211 origin and destination scene group. | |
212 | |
213 We will parse the transform matrix of the @s and @d and then | |
214 generate the matrix which is (1-p) of @s and p percent of @d. | |
215 """ | |
1146
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
216 if newobj == None: |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
217 newobj = s.duplicate(self.document) |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
218 top = self.document.createElement("svg:g") |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
219 top.setAttribute("ref",s.getAttribute("id")) |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
220 top.appendChild(newobj) |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
221 obj.appendChild(top) |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
222 else: |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
223 top = newobj |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
224 newobj = top.firstChild() |
1140 | 225 |
226 if s.name() == 'svg:g': | |
227 # Parse the translate or matrix | |
228 # | |
229 # D = B inv(A) | |
230 try: | |
1141 | 231 item = self.nodeToItem[s.getAttribute("id")] |
1140 | 232 (ox,oy) = item.getCenter() |
233 except: | |
234 ox = 0 | |
235 oy = 0 | |
236 try: | |
1141 | 237 item = self.nodeToItem[d.getAttribute("id")] |
1140 | 238 (dx,dy) = item.getCenter() |
239 except: | |
240 dx = 0 | |
241 dy = 0 | |
242 | |
243 sm = self.parseTransform(s) | |
244 ss = self.decomposition(sm) | |
245 dm = self.parseTransform(d) | |
246 dd = self.decomposition(dm) | |
247 sx = (ss[0]*(1-p)+dd[0]*p)/ss[0] | |
248 sy = (ss[1]*(1-p)+dd[1]*p)/ss[0] | |
249 a = ss[2]*(1-p)+dd[2]*p-ss[2] | |
1146
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
250 tx = ox*(1-p)+dx*p |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
251 ty = oy*(1-p)+dy*p |
1140 | 252 m = [math.cos(a),math.sin(a),-math.sin(a),math.cos(a),0,0] |
253 m = self.mulA([sx,0,0,sy,0,0],m) | |
254 m = self.mulA(m,[1,0,0,1,-ox,oy-self.height]) | |
255 m = self.mulA([1,0,0,1,tx,self.height-ty],m) | |
256 | |
257 top.setAttribute("transform","matrix(%g,%g,%g,%g,%g,%g)" % (m[0],m[2],m[1],m[3],m[4],m[5])) | |
258 else: | |
259 try: | |
1146
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
260 try: |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
261 sw = float(s.getAttribute("width")) |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
262 except: |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
263 sw = 1 |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
264 try: |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
265 sh = float(s.getAttribute("height")) |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
266 except: |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
267 sh = 1 |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
268 try: |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
269 dw = float(d.getAttribute("width")) |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
270 except: |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
271 dw = 1 |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
272 try: |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
273 dh = float(d.getAttribute("height")) |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
274 except: |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
275 dh = 1 |
1140 | 276 try: |
1141 | 277 item = self.nodeToItem[s.getAttribute("id")] |
1140 | 278 (ox,oy) = item.getCenter() |
279 except: | |
280 ox = 0 | |
281 oy = 0 | |
282 try: | |
1141 | 283 item = self.nodeToItem[d.getAttribute("id")] |
1140 | 284 (dx,dy) = item.getCenter() |
285 except: | |
286 dx = 0 | |
287 dy = 0 | |
288 try: | |
289 sm = self.parseTransform(s) | |
290 ss = self.decomposition(sm) | |
291 except: | |
292 ss = [1,1,0,0,0] | |
293 pass | |
294 try: | |
295 dm = self.parseTransform(d) | |
296 dd = self.decomposition(dm) | |
1146
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
297 print "dd=",dd |
1140 | 298 except: |
299 dd = [1,1,0,0,0] | |
1146
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
300 dd[0] = dd[0]*dw/sw |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
301 dd[1] = dd[1]*dh/sh |
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
302 print "ss[0]=",ss[0],"dd[0]=",dd[0] |
1140 | 303 sx = (ss[0]*(1-p)+dd[0]*p)/ss[0] |
304 sy = (ss[1]*(1-p)+dd[1]*p)/ss[1] | |
1146
e14ec6d1a661
CHange the implementation to set the transformation matrix only. This is be more friendly for the animation inside the inskcape.
wycc
parents:
1141
diff
changeset
|
305 print "sx=",sx,"sy=",sy |
1140 | 306 a = ss[2]*(1-p)+dd[2]*p-ss[2] |
307 tx = ox*(1-p)+dx*p | |
308 ty = oy*(1-p)+dy*p | |
309 m = [math.cos(a),math.sin(a),-math.sin(a),math.cos(a),0,0] | |
310 m = self.mulA([sx,0,0,sy,0,0],m) | |
311 m = self.mulA(m,[1,0,0,1,-ox,oy-self.height]) | |
312 m = self.mulA([1,0,0,1,tx,self.height-ty],m) | |
313 | |
314 top.setAttribute("transform","matrix(%g,%g,%g,%g,%g,%g)" % (m[0],m[2],m[1],m[3],m[4],m[5])) | |
315 except: | |
316 traceback.print_exc() |