comparison 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
comparison
equal deleted inserted replaced
1141:8f0ee167c5b2 1146:e14ec6d1a661
37 i = 0 37 i = 0
38 s = source.ref.firstChild() 38 s = source.ref.firstChild()
39 d = dest.ref.firstChild() 39 d = dest.ref.firstChild()
40 sources={} 40 sources={}
41 dests={} 41 dests={}
42 42 # Collect ref from the obj
43 o = obj.firstChild()
44 maps={}
45 while o:
46 print "--->",o
47 try:
48 ref = o.getAttribute("ref")
49 except:
50 print o
51 ref = None
52
53 if ref:
54 maps[ref] = o
55 o = o.next()
43 # Collect all objects 56 # Collect all objects
44 while d: 57 while d:
45 try: 58 try:
46 label = d.getAttribute("inkscape:label") 59 label = d.getAttribute("inkscape:label")
47 except: 60 except:
52 # Check if the object in the source exists in the destination 65 # Check if the object in the source exists in the destination
53 s = source.ref.firstChild() 66 s = source.ref.firstChild()
54 d = dest.ref.firstChild() 67 d = dest.ref.firstChild()
55 while s: 68 while s:
56 print s,d 69 print s,d
70 sid = s.getAttribute("id")
71 if maps.has_key(sid):
72 o = maps[sid]
73 else:
74 o = None
57 try: 75 try:
58 label = s.getAttribute("inkscape:label") 76 label = s.getAttribute("inkscape:label")
59 # Use i8nkscape:label to identidy the equipvalent objects 77 # Use i8nkscape:label to identidy the equipvalent objects
60 if label: 78 if label:
61 if dests.hasattr(label.value()): 79 if dests.hasattr(label.value()):
62 self.updateTweenObject(obj,typ,s,dests[label.value()],percent) 80 self.updateTweenObject(obj,typ,s,dests[label.value()],percent,o)
63 s = s.next() 81 s = s.next()
64 continue 82 continue
65 except: 83 except:
66 pass 84 pass
67 # Search obejcts in the destination 85 # Search obejcts in the destination
71 d = d.next() 89 d = d.next()
72 continue 90 continue
73 except: 91 except:
74 pass 92 pass
75 if s.name() == d.name(): 93 if s.name() == d.name():
76 self.updateTweenObject(obj,typ,s,d,percent) 94 self.updateTweenObject(obj,typ,s,d,percent,o)
77 d = d.next() 95 d = d.next()
78 break 96 break
79 d = d.next() 97 d = d.next()
80 s = s.next() 98 s = s.next()
81 99
143 sy = -sy 161 sy = -sy
144 R = math.atan2(B,A) 162 R = math.atan2(B,A)
145 return [sx,sy, R, E,F] 163 return [sx,sy, R, E,F]
146 164
147 165
148 def updateTweenObject(self,obj,typ,s,d,p): 166 def updateTweenObject(self,obj,typ,s,d,p,newobj):
149 """ 167 """
150 Generate tweened object in the @obj by using s and d in the @p percent 168 Generate tweened object in the @obj by using s and d in the @p percent
151 http://lists.w3.org/Archives/Public/www-style/2010Jun/0602.html 169 http://lists.w3.org/Archives/Public/www-style/2010Jun/0602.html
152 """ 170 """
153 if typ == 'relocate': 171 if typ == 'relocate':
154 newobj = s.duplicate(self.document) 172 newobj = s.duplicate(self.document)
155 newobj.setAttribute("ref", s.getAttribute("id"))
156 top = self.document.createElement("svg:g") 173 top = self.document.createElement("svg:g")
174 top.setAttribute("ref", s.getAttribute("id"))
157 top.appendChild(newobj) 175 top.appendChild(newobj)
158 obj.appendChild(top) 176 obj.appendChild(top)
159 if s.name() == 'svg:g': 177 if s.name() == 'svg:g':
160 # Parse the translate or matrix 178 # Parse the translate or matrix
161 sm = self.parseTransform(s) 179 sm = self.parseTransform(s)
174 except: 192 except:
175 traceback.print_exc() 193 traceback.print_exc()
176 pass 194 pass
177 pass 195 pass
178 elif typ == 'scale': 196 elif typ == 'scale':
179 self.updateTweenObjectScale(obj,s,d,p) 197 self.updateTweenObjectScale(obj,s,d,p,newobj)
180 pass 198 pass
181 elif typ == 'normal': 199 elif typ == 'normal':
182 newobj = s.duplicate(self.document) 200 newobj = s.duplicate(self.document)
183 newobj.setAttribute("ref", s.getAttribute("id")) 201 newobj.setAttribute("ref", s.getAttribute("id"))
184 top = self.document.createElement("svg:g") 202 top = self.document.createElement("svg:g")
185 top.appendChild(newobj) 203 top.appendChild(newobj)
186 obj.appendChild(top) 204 obj.appendChild(top)
187 pass 205 pass
188 206
189 def updateTweenObjectScale(self,obj,s,d,p): 207 def updateTweenObjectScale(self,obj,s,d,p,newobj):
190 """ 208 """
191 Generate a new group which contains the original group and then 209 Generate a new group which contains the original group and then
192 add the transform matrix to generate a tween frame between the 210 add the transform matrix to generate a tween frame between the
193 origin and destination scene group. 211 origin and destination scene group.
194 212
195 We will parse the transform matrix of the @s and @d and then 213 We will parse the transform matrix of the @s and @d and then
196 generate the matrix which is (1-p) of @s and p percent of @d. 214 generate the matrix which is (1-p) of @s and p percent of @d.
197 """ 215 """
198 newobj = s.duplicate(self.document) 216 if newobj == None:
199 top = self.document.createElement("svg:g") 217 newobj = s.duplicate(self.document)
200 top.appendChild(newobj) 218 top = self.document.createElement("svg:g")
201 obj.appendChild(top) 219 top.setAttribute("ref",s.getAttribute("id"))
220 top.appendChild(newobj)
221 obj.appendChild(top)
222 else:
223 top = newobj
224 newobj = top.firstChild()
202 225
203 if s.name() == 'svg:g': 226 if s.name() == 'svg:g':
204 # Parse the translate or matrix 227 # Parse the translate or matrix
205 # 228 #
206 # D = B inv(A) 229 # D = B inv(A)
222 dm = self.parseTransform(d) 245 dm = self.parseTransform(d)
223 dd = self.decomposition(dm) 246 dd = self.decomposition(dm)
224 sx = (ss[0]*(1-p)+dd[0]*p)/ss[0] 247 sx = (ss[0]*(1-p)+dd[0]*p)/ss[0]
225 sy = (ss[1]*(1-p)+dd[1]*p)/ss[0] 248 sy = (ss[1]*(1-p)+dd[1]*p)/ss[0]
226 a = ss[2]*(1-p)+dd[2]*p-ss[2] 249 a = ss[2]*(1-p)+dd[2]*p-ss[2]
227 tx = ox*(1-p)+dx*p-ox 250 tx = ox*(1-p)+dx*p
228 ty = oy*(1-p)+dy*p-oy 251 ty = oy*(1-p)+dy*p
229 m = [math.cos(a),math.sin(a),-math.sin(a),math.cos(a),0,0] 252 m = [math.cos(a),math.sin(a),-math.sin(a),math.cos(a),0,0]
230 m = self.mulA([sx,0,0,sy,0,0],m) 253 m = self.mulA([sx,0,0,sy,0,0],m)
231 m = self.mulA(m,[1,0,0,1,-ox,oy-self.height]) 254 m = self.mulA(m,[1,0,0,1,-ox,oy-self.height])
232 m = self.mulA([1,0,0,1,tx,self.height-ty],m) 255 m = self.mulA([1,0,0,1,tx,self.height-ty],m)
233 256
234 top.setAttribute("transform","matrix(%g,%g,%g,%g,%g,%g)" % (m[0],m[2],m[1],m[3],m[4],m[5])) 257 top.setAttribute("transform","matrix(%g,%g,%g,%g,%g,%g)" % (m[0],m[2],m[1],m[3],m[4],m[5]))
235 else: 258 else:
236 try: 259 try:
237 sw = float(s.getAttribute("width")) 260 try:
238 sh = float(s.getAttribute("height")) 261 sw = float(s.getAttribute("width"))
239 dw = float(d.getAttribute("width")) 262 except:
240 dh = float(d.getAttribute("height")) 263 sw = 1
264 try:
265 sh = float(s.getAttribute("height"))
266 except:
267 sh = 1
268 try:
269 dw = float(d.getAttribute("width"))
270 except:
271 dw = 1
272 try:
273 dh = float(d.getAttribute("height"))
274 except:
275 dh = 1
241 try: 276 try:
242 item = self.nodeToItem[s.getAttribute("id")] 277 item = self.nodeToItem[s.getAttribute("id")]
243 (ox,oy) = item.getCenter() 278 (ox,oy) = item.getCenter()
244 except: 279 except:
245 ox = 0 280 ox = 0
257 ss = [1,1,0,0,0] 292 ss = [1,1,0,0,0]
258 pass 293 pass
259 try: 294 try:
260 dm = self.parseTransform(d) 295 dm = self.parseTransform(d)
261 dd = self.decomposition(dm) 296 dd = self.decomposition(dm)
297 print "dd=",dd
262 except: 298 except:
263 dd = [1,1,0,0,0] 299 dd = [1,1,0,0,0]
264 dd[0] = ss[0]*dw/sw 300 dd[0] = dd[0]*dw/sw
265 dd[1] = ss[1]*dh/sh 301 dd[1] = dd[1]*dh/sh
302 print "ss[0]=",ss[0],"dd[0]=",dd[0]
266 sx = (ss[0]*(1-p)+dd[0]*p)/ss[0] 303 sx = (ss[0]*(1-p)+dd[0]*p)/ss[0]
267 sy = (ss[1]*(1-p)+dd[1]*p)/ss[1] 304 sy = (ss[1]*(1-p)+dd[1]*p)/ss[1]
305 print "sx=",sx,"sy=",sy
268 a = ss[2]*(1-p)+dd[2]*p-ss[2] 306 a = ss[2]*(1-p)+dd[2]*p-ss[2]
269 tx = ox*(1-p)+dx*p 307 tx = ox*(1-p)+dx*p
270 ty = oy*(1-p)+dy*p 308 ty = oy*(1-p)+dy*p
271 m = [math.cos(a),math.sin(a),-math.sin(a),math.cos(a),0,0] 309 m = [math.cos(a),math.sin(a),-math.sin(a),math.cos(a),0,0]
272 m = self.mulA([sx,0,0,sy,0,0],m) 310 m = self.mulA([sx,0,0,sy,0,0],m)