Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

from director.timercallback import TimerCallback 

import numpy as np 

import time 

 

class AnimatePropertyValue(object): 

    ''' 

    This class is used to ramp a scalar or vector property from its current value to a 

    target value using linear inteprolation.  For example: 

 

    obj = getSomeObject() 

 

    # fade the Alpha property to 0.0 

    AnimatePropertyValue(obj, 'Alpha', 0.0).start() 

 

    ''' 

 

    def __init__(self, obj, propertyName, targetValue, animationTime=1.0): 

 

        self.obj = obj 

        self.propertyName = propertyName 

        self.animationTime = animationTime 

        self.targetValue = targetValue 

        self.timer = TimerCallback() 

 

    def start(self): 

        self.startTime = time.time() 

        self.startValue = np.array(self.obj.getProperty(self.propertyName)) 

        self.targetValue = np.array(self.targetValue) 

        self.timer.callback = self.tick 

        self.timer.start() 

 

    def tick(self): 

        elapsed = time.time() - self.startTime 

        p = elapsed/self.animationTime 

        if p > 1.0: 

            p = 1.0 

 

        newValue = self.startValue + (self.targetValue - self.startValue)*p 

        self.obj.setProperty(self.propertyName, newValue) 

 

        if p == 1.0: 

            self.timer.callback = None 

            return False