-
Notifications
You must be signed in to change notification settings - Fork 22
/
AnimationControl.py
195 lines (138 loc) · 6.54 KB
/
AnimationControl.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import avango
import avango.script
from enum import Enum
class State(Enum):
blending = 0
play = 1
class AnimationConfig():
def __init__(self, name, loop=True, speed=1.0, duration=0.0):
self.name = name
self.speed = speed
self.duration = duration
self.loop = loop
class AnimationControl(avango.script.Script):
def __init__(self):
self.super(AnimationControl).__init__()
self._animation_node = None
self._timer = avango.nodes.TimeSensor()
self._delta_timer = avango.nodes.TimeSensor()
self._state = State.play
self._last_animation = None
self._current_animation = None
self._last_time = 0.0
self._current_time = 0.0
self._last_blending_start = 0.0
self._current_blending_start = 0.0
self._first_play = False
self._blending_factor = 1.0
self._blending_duration = 0.5
self._blending_end = 0.0
def my_constructor(self, animation_node, start_animation_config):
self._timer.ReferenceTime.value = self._timer.RealTime.value
self._animation_node = animation_node
if start_animation_config is not None:
self.switch_to(start_animation_config)
else:
print("Error: Please select starting animation!")
exit()
self.always_evaluate(True)
self._animation_node.Time1.value = 0.5
self._animation_node.Time2.value = 0.5
def get_current_animation(self):
return self._current_animation
def get_last_animation(self):
return self._last_animation
def get_blending_factor(self):
return self._blending_factor
def is_looping(self):
return self._first_play
def switch_to(self, anim_config):
if self._current_animation is not None:
self._animation_node.Animation1.value = \
self._animation_node.Animation2.value
self._animation_node.Animation2.value = anim_config.name
if anim_config.duration == 0:
anim_config.duration = \
self._animation_node.get_duration(anim_config.name) /\
anim_config.speed
self._last_animation = self._current_animation
self._current_animation = anim_config
self._last_blending_start = self._current_blending_start
self._current_blending_start = self._timer.Time.value
self._blending_end = self._current_blending_start
self._animation_node.Time1.value = self._last_time = self._current_time
self._animation_node.Time2.value = self._current_time = 0
self._first_play = True
self._animation_node.BlendFactor.value = self._blending_factor = 1.0
self._state = State.play
def blend_to(self, anim_config, blending_duration=0.5):
if anim_config.duration == 0:
anim_config.duration = \
self._animation_node.get_duration(anim_config.name) /\
anim_config.speed
self._last_animation = self._current_animation
self._current_animation = anim_config
self._last_blending_start = self._current_blending_start
self._current_blending_start = self._timer.Time.value
self._blending_end = self._current_blending_start + blending_duration
self._animation_node.Time1.value = self._last_time = self._current_time
self._animation_node.Time2.value = self._current_time = 0
self._blending_duration = blending_duration
self._first_play = True
self._animation_node.Animation1.value = \
self._animation_node.Animation2.value
self._animation_node.Animation2.value = anim_config.name
self._animation_node.BlendFactor.value = self._blending_factor = \
(self._timer.Time.value - self._current_blending_start) /\
self._blending_duration
self._state = State.blending
self.evaluate()
def evaluate(self):
if self._current_animation is None:
return
if self._timer.Time.value > self._current_blending_start +\
self._current_animation.duration:
self._first_play = False
if self._animation_node.get_duration(self._current_animation.name) > \
0.0 and self._current_animation.speed > 0.0:
self._current_time += self._delta_timer.Time.value /\
(self._animation_node.get_duration(
self._current_animation.name) /
self._current_animation.speed)
if self._current_animation.loop or self._current_time < 1.0:
self._animation_node.Time2.value = self._current_time % 1
else:
self._animation_node.Time2.value = 1.0
else:
print("Warning: Animation duration or speed is zero.")
print("Animation: "+str(self._current_animation.name))
print("Duration: "+str(self._animation_node.get_duration(
self._current_animation.name)))
print("Speed: "+str(self._current_animation.speed))
if self._state == State.blending:
# update time for old animation only while blending
if self._animation_node.get_duration(self._last_animation.name) >\
0.0 and self._last_animation.speed > 0.0:
self._last_time += self._delta_timer.Time.value /\
(self._animation_node.get_duration(
self._last_animation.name)/self._last_animation.speed)
if self._last_animation.loop or self._last_time < 1.0:
self._animation_node.Time1.value = self._last_time % 1
else:
self._animation_node.Time1.value = 1.0
else:
print("Warning: Animation duration or speed is zero.")
print("Animation: "+str(self._last_animation.name))
print("Duration: "+str(self._animation_node.get_duration(
self._last_animation.name)))
print("Speed: "+str(self._last_animation.speed))
self._blending_factor = 1.0 -\
(self._blending_end - self._timer.Time.value) /\
self._blending_duration
# if blending is finished, switch to normal play
if self._blending_factor >= 1.0:
self._state = State.play
self._blending_factor = 1.0
self._animation_node.BlendFactor.value = self._blending_factor
self._delta_timer.ReferenceTime.value = \
self._delta_timer.RealTime.value