pyparsing frames and tweens - take 2

Paul McGuire ptmcg at austin.rr.com
Mon Nov 5 10:23:18 EST 2007


Here is a first cut at processing the parsed objects, given a list of
Property objects representing the frames at each '#':

class Property(object):
    def __init__(self,**kwargs):
        self.__dict__.update(kwargs)

    def copy(self):
        return Property(**self.__dict__)

    @staticmethod
    def tween(prev,next,n,i):
        dt = 1.0/(n+1)
        x = prev.x + (next.x-prev.x)*dt*(i+1)
        y = prev.y + (next.y-prev.y)*dt*(i+1)
        r = prev.r + (next.r-prev.r)*dt*(i+1)
        g = prev.g + (next.g-prev.g)*dt*(i+1)
        b = prev.b + (next.b-prev.b)*dt*(i+1)
        a = prev.a + (next.a-prev.a)*dt*(i+1)
        return Property(x=x,y=y,r=r,g=g,b=b,a=a)

    @staticmethod
    def makeBlank():
        return Property(x=0,y=0,r=0,g=0,b=0,a=0)

    def __repr__(self):
        return "Property(x=%(x).3f, y=%(y).3f, r=%(r).3f, g=%(g).3f, b=
%(b).3f, a=%(a).3f)" % self.__dict__


def makeAllFrames(propFrameList, animationList):
    ret = []
    propFrameIndex = 0
    for animIndex,animObj in enumerate(animationList):
        if isinstance(animObj, Prop):
            ret.append( propFrameList[propFrameIndex] )
            propFrameIndex += 1
        if isinstance(animObj, Blank):
            ret.append( Property.makeBlank() )
        if isinstance(animObj, CopyPrevious):
            ret.append( ret[-1].copy() )
        if isinstance(animObj, Tween):
            # compute delta x,y,r,g,b,a between prev frame and next
frame
            prev = propFrameList[propFrameIndex-1]
            next = propFrameList[propFrameIndex]
            numFrames = animObj.tweenLength
            print `prev`, `next`
            tweens = [ Property.tween(prev,next,numFrames,i) for i in
range(numFrames) ]
            ret += tweens
    return ret

prop1 = Property(x=10,y=10,r=1,g=1,b=1,a=0)
prop2 = Property(x=20,y=10,r=0,g=0,b=1,a=1)
prop3 = Property(x=10,y=100,r=1,g=1,b=1,a=1)
prop4 = Property(x=100,y=200,r=1,g=1,b=1,a=1)
propFrames = [ prop1, prop2, prop3, prop4 ]

allFramesList = makeAllFrames( propFrames, animation.parseString("#---
#--#===_#") )
from pprint import pprint
pprint( allFramesList )

Gives:
[Property(x=10.000, y=10.000, r=1.000, g=1.000, b=1.000, a=0.000),
 Property(x=12.500, y=10.000, r=0.750, g=0.750, b=1.000, a=0.250),
 Property(x=15.000, y=10.000, r=0.500, g=0.500, b=1.000, a=0.500),
 Property(x=17.500, y=10.000, r=0.250, g=0.250, b=1.000, a=0.750),
 Property(x=20.000, y=10.000, r=0.000, g=0.000, b=1.000, a=1.000),
 Property(x=16.667, y=40.000, r=0.333, g=0.333, b=1.000, a=1.000),
 Property(x=13.333, y=70.000, r=0.667, g=0.667, b=1.000, a=1.000),
 Property(x=10.000, y=100.000, r=1.000, g=1.000, b=1.000, a=1.000),
 Property(x=10.000, y=100.000, r=1.000, g=1.000, b=1.000, a=1.000),
 Property(x=10.000, y=100.000, r=1.000, g=1.000, b=1.000, a=1.000),
 Property(x=10.000, y=100.000, r=1.000, g=1.000, b=1.000, a=1.000),
 Property(x=0.000, y=0.000, r=0.000, g=0.000, b=0.000, a=0.000),
 Property(x=100.000, y=200.000, r=1.000, g=1.000, b=1.000, a=1.000)]


-- Paul




More information about the Python-list mailing list