# HG changeset patch # User wycc # Date 1293175085 -28800 # Node ID 153a87b4edb71f097cecb87c61e480fa73402587 # Parent 5cfa73d7e80f07a47a10f130f72c5429cd9f1b5c# Parent 6409a83b68e71dcd236a43d4450eff274e678110 Merge diff -r 6409a83b68e7 -r 153a87b4edb7 pyink/MBScene.py --- a/pyink/MBScene.py Fri Dec 24 12:24:07 2010 +0800 +++ b/pyink/MBScene.py Fri Dec 24 15:18:05 2010 +0800 @@ -14,7 +14,7 @@ from tween import TweenObject # Please refer to -# http://www.assembla.com/wiki/show/MadButterfly/InkscapeForMadButterfly +# http://www.assembla.com/wiki/show/MadButterfly/Inkscape_extention # for the designed document. @@ -98,7 +98,6 @@ class LayerAttributeWatcher(pybInkscape.PYNodeObserver): def __init__(self,ui): self.ui = ui - def notifyChildAdded(self,node,child,prev): pass @@ -155,6 +154,8 @@ self.tween=None self.document = None self.dom = None + self.framerate=12 + self.maxframe=0 pass def show_selection(self,w,obj): @@ -219,13 +220,15 @@ traceback.print_exc() continue try: - end = s.getAttribute("end") + end = int(s.getAttribute("end")) if end == None: end = start pass except: end = start pass + if end > self.maxframe: + self.maxframe = end try: typ = s.getAttribute('type') if typ == None: @@ -336,6 +339,7 @@ pass pass + self.collectID() self.dumpID() pass @@ -525,20 +529,19 @@ """ Update the scene group according to the curretn scene data. There are a couple of cases. - - 1. If the type of the scene is normal, we display it when - it contains the current frame. Otherwise hide it. - - 2. If the type of the scene is relocate or scale, we need - to duplicate the scene group and then modify its - transform matrix according to the definition of the - scene. Then, hide the original scenr group and display - the duplciate scene group. In addition, we may need to + 1. If the type of the scene is normal, we display it when + it contains the current frame. Otherwise hide it. + 2. If the type of the scene is relocate or scale, we need + to duplicate the scene group and then modify its + transform matrix according to the definition of the + scene. Then, hide the original scenr group and display + the duplciate scene group. In addition, we may need to delete the old duplicated scene group as well. - For each layer, we will always use the duplicated scene - group whose name as dup. We will put the duplicated scene - group inside it. We will create this group if it is not + For each layer, we will always use the duplicated scene + group whose name as dup. + We will put the duplicated scene group inside it. We will + create this group if it is not available. """ self.current = nth @@ -549,15 +552,17 @@ # Check the duplicated scene group and create it if it is not available try: if layer.duplicateGroup: - layer.duplicateGroup.parent().removeChild(layer.duplicateGroup) - layer.duplicateGroup = None + layer.duplicateGroup.setAttribute("style","display:none") except: - traceback.print_exc() + print "*"*40 + layer.duplicateGroup = self.document.createElement("svg:g") + layer.duplicateGroup.setAttribute("inkscape:label","dup") + layer.duplicateGroup.setAttribute("sodipodi:insensitive","1") + layer.duplicateGroup.setAttribute("style","") + layer.layer.node.appendChild(layer.duplicateGroup) pass # Create a new group - layer.duplicateGroup = None - - +#layer.duplicateGroup = None while i < len(layer._keys): s = layer._keys[i] print s.ref.getAttribute("id"),s.idx,s.left_tween,s.right_tween @@ -573,19 +578,19 @@ else: if nth > (s.idx+1) and nth <= (layer._keys[i+1].idx+1): if i+2 < len(layer._keys): - layer.duplicateGroup = self.document.createElement("svg:g") - layer.duplicateGroup.setAttribute("inkscape:label","dup") - layer.duplicateGroup.setAttribute("sodipodi:insensitive","1") + #s.ref.parent().appendChild(layer.duplicateGroup) s.ref.setAttribute("style","display:none") - s.ref.parent().appendChild(layer.duplicateGroup) + layer.duplicateGroup.setAttribute("style","") self.tween.updateTweenContent(layer.duplicateGroup, layer.get_tween_type(s.idx),s, layer._keys[i+2], nth) else: - layer.duplicateGroup = s.ref.duplicate(self.document) - layer.duplicateGroup.setAttribute("style","") - layer.duplicateGroup.setAttribute("inkscape:label","dup") - layer.duplicateGroup.setAttribute("sodipodi:insensitive","1") + layer.duplicateGroup.setAttribute("style","") + #layer.duplicateGroup = s.ref.duplicate(self.document) + #layer.duplicateGroup.setAttribute("style","") + #layer.duplicateGroup.setAttribute("inkscape:label","dup") + #layer.duplicateGroup.setAttribute("sodipodi:insensitive","1") s.ref.setAttribute("style","display:none") - s.ref.parent().appendChild(layer.duplicateGroup) + #s.ref.parent().appendChild(layer.duplicateGroup) + pass else: s.ref.setAttribute("style","display:none") i = i + 2 @@ -593,6 +598,21 @@ pass pass + def DOMtoItem(self,obj): + """ + Find the corresponding PYSPObject object for a DOM object. + """ + return self.DOMtoItem_recursive(self.desktop.doc().root(),obj) + + def DOMtoItem_recursive(self,tree,obj): + nodes = tree.childList() + for s in nodes: + if s.getId() == obj.getAttribute('id'): + return s + d = self.DOMtoItem_recursive(s,obj) + if d != None: return d + + def enterGroup(self,obj): for l in self.layers: for s in l.node.childList(): @@ -819,6 +839,7 @@ self.lockui = False #self.grid.show_all() pass + def changeObjectLabel(self,w): o = self.desktop.selection.list()[0] o.setAttribute("inkscape:label", self.nameEditor.get_text()) @@ -832,6 +853,25 @@ self.editDone.connect('clicked', self.changeObjectLabel) pass + def doRun(self,arg): + """ + Execute the current animation till the last frame. + """ + if self.btnRun.get_label() == "Run": + self.btnRun.set_label("Stop") + self.last_update = glib.timeout_add(1000/self.framerate,self.doRunNext) + else: + self.btnRun.set_label("Run") + glib.source_remove(self.last_update) + + def doRunNext(self): + if self.current >= self.maxframe: + self.current = 0 + print self.current,self.maxframe + self.setCurrentScene(self.current+1) + self.last_update = glib.timeout_add(1000/self.framerate,self.doRunNext) + + def addButtons(self,hbox): #btn = gtk.Button('Edit') #btn.connect('clicked', self.doEditScene) @@ -848,8 +888,13 @@ btn=gtk.Button('Duplicate Key') btn.connect('clicked', self.doDuplicateKeyScene) hbox.pack_start(btn,expand=False,fill=False) + btn=gtk.Button('Run') + btn.connect('clicked', self.doRun) + self.btnRun = btn + hbox.pack_start(btn,expand=False,fill=False) self.addNameEditor(hbox) self.addTweenTypeSelector(hbox) + pass def onTweenTypeChange(self,w): diff -r 6409a83b68e7 -r 153a87b4edb7 pyink/primitive_test.svg --- a/pyink/primitive_test.svg Fri Dec 24 12:24:07 2010 +0800 +++ b/pyink/primitive_test.svg Fri Dec 24 15:18:05 2010 +0800 @@ -10,12 +10,13 @@ xmlns="http://www.w3.org/2000/svg" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" - width="744.09448819" - height="1052.3622047" + width="744.09448" + height="1052.3622" id="svg2" version="1.1" - inkscape:version="0.48+devel r9774 custom" - sodipodi:docname="primitive_test.svg"> + inkscape:version="0.48+devel r9776" + sodipodi:docname="primitive_test.svg" + style="display:inline"> @@ -51,42 +52,118 @@ + id="layer1" + style="display:inline"> - + id="layer1s4140"> + + + + + + + + + + + + + + + + + + diff -r 6409a83b68e7 -r 153a87b4edb7 pyink/tween.py --- a/pyink/tween.py Fri Dec 24 12:24:07 2010 +0800 +++ b/pyink/tween.py Fri Dec 24 15:18:05 2010 +0800 @@ -39,7 +39,20 @@ d = dest.ref.firstChild() sources={} dests={} - + # Collect ref from the obj + o = obj.firstChild() + maps={} + while o: + print "--->",o + try: + ref = o.getAttribute("ref") + except: + print o + ref = None + + if ref: + maps[ref] = o + o = o.next() # Collect all objects while d: try: @@ -54,12 +67,17 @@ d = dest.ref.firstChild() while s: print s,d + sid = s.getAttribute("id") + if maps.has_key(sid): + o = maps[sid] + else: + o = None try: label = s.getAttribute("inkscape:label") # Use i8nkscape:label to identidy the equipvalent objects if label: if dests.hasattr(label.value()): - self.updateTweenObject(obj,typ,s,dests[label.value()],percent) + self.updateTweenObject(obj,typ,s,dests[label.value()],percent,o) s = s.next() continue except: @@ -73,7 +91,7 @@ except: pass if s.name() == d.name(): - self.updateTweenObject(obj,typ,s,d,percent) + self.updateTweenObject(obj,typ,s,d,percent,o) d = d.next() break d = d.next() @@ -145,15 +163,15 @@ return [sx,sy, R, E,F] - def updateTweenObject(self,obj,typ,s,d,p): + def updateTweenObject(self,obj,typ,s,d,p,newobj): """ Generate tweened object in the @obj by using s and d in the @p percent http://lists.w3.org/Archives/Public/www-style/2010Jun/0602.html """ if typ == 'relocate': newobj = s.duplicate(self.document) - newobj.setAttribute("ref", s.getAttribute("id")) top = self.document.createElement("svg:g") + top.setAttribute("ref", s.getAttribute("id")) top.appendChild(newobj) obj.appendChild(top) if s.name() == 'svg:g': @@ -176,7 +194,7 @@ pass pass elif typ == 'scale': - self.updateTweenObjectScale(obj,s,d,p) + self.updateTweenObjectScale(obj,s,d,p,newobj) pass elif typ == 'normal': newobj = s.duplicate(self.document) @@ -186,7 +204,7 @@ obj.appendChild(top) pass - def updateTweenObjectScale(self,obj,s,d,p): + def updateTweenObjectScale(self,obj,s,d,p,newobj): """ Generate a new group which contains the original group and then add the transform matrix to generate a tween frame between the @@ -195,10 +213,15 @@ We will parse the transform matrix of the @s and @d and then generate the matrix which is (1-p) of @s and p percent of @d. """ - newobj = s.duplicate(self.document) - top = self.document.createElement("svg:g") - top.appendChild(newobj) - obj.appendChild(top) + if newobj == None: + newobj = s.duplicate(self.document) + top = self.document.createElement("svg:g") + top.setAttribute("ref",s.getAttribute("id")) + top.appendChild(newobj) + obj.appendChild(top) + else: + top = newobj + newobj = top.firstChild() if s.name() == 'svg:g': # Parse the translate or matrix @@ -224,8 +247,8 @@ sx = (ss[0]*(1-p)+dd[0]*p)/ss[0] sy = (ss[1]*(1-p)+dd[1]*p)/ss[0] a = ss[2]*(1-p)+dd[2]*p-ss[2] - tx = ox*(1-p)+dx*p-ox - ty = oy*(1-p)+dy*p-oy + tx = ox*(1-p)+dx*p + ty = oy*(1-p)+dy*p m = [math.cos(a),math.sin(a),-math.sin(a),math.cos(a),0,0] m = self.mulA([sx,0,0,sy,0,0],m) m = self.mulA(m,[1,0,0,1,-ox,oy-self.height]) @@ -234,10 +257,22 @@ top.setAttribute("transform","matrix(%g,%g,%g,%g,%g,%g)" % (m[0],m[2],m[1],m[3],m[4],m[5])) else: try: - sw = float(s.getAttribute("width")) - sh = float(s.getAttribute("height")) - dw = float(d.getAttribute("width")) - dh = float(d.getAttribute("height")) + try: + sw = float(s.getAttribute("width")) + except: + sw = 1 + try: + sh = float(s.getAttribute("height")) + except: + sh = 1 + try: + dw = float(d.getAttribute("width")) + except: + dw = 1 + try: + dh = float(d.getAttribute("height")) + except: + dh = 1 try: item = self.nodeToItem[s.getAttribute("id")] (ox,oy) = item.getCenter() @@ -259,12 +294,15 @@ try: dm = self.parseTransform(d) dd = self.decomposition(dm) + print "dd=",dd except: dd = [1,1,0,0,0] - dd[0] = ss[0]*dw/sw - dd[1] = ss[1]*dh/sh + dd[0] = dd[0]*dw/sw + dd[1] = dd[1]*dh/sh + print "ss[0]=",ss[0],"dd[0]=",dd[0] sx = (ss[0]*(1-p)+dd[0]*p)/ss[0] sy = (ss[1]*(1-p)+dd[1]*p)/ss[1] + print "sx=",sx,"sy=",sy a = ss[2]*(1-p)+dd[2]*p-ss[2] tx = ox*(1-p)+dx*p ty = oy*(1-p)+dy*p