|
| 1 | +import random |
| 2 | + |
| 3 | +# Represents the instant state of an eye - pupil size, position |
| 4 | +# as well as weight for both upper and lower eyelids |
| 5 | +class EyeState: |
| 6 | + pupilSize = 0 |
| 7 | + posX = 0 |
| 8 | + posY = 0 |
| 9 | + upperLidWeight = 0 |
| 10 | + lowerLidWeight = 0 |
| 11 | + |
| 12 | +# Model representing a blink state of an eye. |
| 13 | +# It performs smooth transition between open an closed state being driven by methods |
| 14 | +# start_blink / complete_blink |
| 15 | +# start_open / start_close |
| 16 | +# In the end it just calculates current eyelid "weight" where 0.0 means fully open |
| 17 | +# and 1.0 means fully closed. |
| 18 | +class BlinkStateModel: |
| 19 | + # 0 = open, 1 = blinking/closing/closed, 2 = opening |
| 20 | + state = 0 |
| 21 | + # When the closing move started and how much time should it take (0 = instantly) |
| 22 | + closeStart = 0 |
| 23 | + closeDuration = 0 |
| 24 | + # When the opening move started and how much time should it take (0 = instantly) |
| 25 | + openStart = 0 |
| 26 | + openDuration = 0 |
| 27 | + # Should the eye stay closed when closing move finishes. If not, it will transition to opening move |
| 28 | + keepClosed = False |
| 29 | + |
| 30 | + # Begins blinking movement (close and open the eye once). |
| 31 | + # Note, that after closing the the eye it will stay closed until complete_blink() is called. |
| 32 | + # There is no need to wait until the eye closes fully before calling complete_blink() - it can be called |
| 33 | + # immediately after start_blink() and the eye will still perform the full close/open motion. |
| 34 | + # Also note that start_blink() only does anything if the eye is currently fully open and is ignored otherwise. |
| 35 | + def start_blink(self, now, closeDuration, openDuration): |
| 36 | + if self.state != 0: |
| 37 | + return |
| 38 | + self.state = 1 # ENBLINK |
| 39 | + self.closeStart = now |
| 40 | + self.closeDuration = closeDuration |
| 41 | + # Followed by open no delay |
| 42 | + self.openStart = now + closeDuration |
| 43 | + self.openDuration = openDuration |
| 44 | + # ... but only if complete_blink() is called in time |
| 45 | + self.keepClosed = True |
| 46 | + self.advance(now) |
| 47 | + |
| 48 | + # Finish blinking movement and open the eye. |
| 49 | + # This method does not actually start the movement but just "allows" it instead. |
| 50 | + # If the eye is in the middle of the closing movement because of start_blink(), |
| 51 | + # then calling this method does not immediately open the eye but will let it finish |
| 52 | + # the closing movement first. |
| 53 | + def complete_blink(self, now): |
| 54 | + if self.openStart < now: |
| 55 | + self.openStart = now |
| 56 | + self.keepClosed = False |
| 57 | + self.advance(now) |
| 58 | + |
| 59 | + # Begins opening movement. |
| 60 | + # Note that the movement overrides any other movement (opening or closing) if it is in progress. |
| 61 | + # The duration passed treated as desired duration of the full swing. So if the eye |
| 62 | + # is partially closed already (state != 0), duration and start time will be adjusted accordingly |
| 63 | + # to maintain the speed that full swing would require for target duration. |
| 64 | + def open(self, now, duration): |
| 65 | + n = self.get_eyelid_weight(now) |
| 66 | + self.state = 2 |
| 67 | + self.openStart = now - duration * (1.0 - n) |
| 68 | + self.openDuration = duration |
| 69 | + self.keepClosed = False |
| 70 | + self.advance(now) |
| 71 | + |
| 72 | +# print("self.state=%d, self.startTime=%f, self.openDuration=%f, SUM=%f" % (self.state, self.startTime, self.openDuration, self.startTime+self.openDuration)) |
| 73 | + |
| 74 | + # Begins closing movement. |
| 75 | + # Note that the movement overrides any other movement (opening or closing) if it is in progress. |
| 76 | + # The duration passed treated as desired duration of the full swing. So if the eye |
| 77 | + # is partially closed already (state != 0), duration and start time will be adjusted accordingly |
| 78 | + # to maintain the speed that full swing would require for target duration. |
| 79 | + # The eye will remain closed until a call to start_open() |
| 80 | + def close(self, now, duration): |
| 81 | + n = self.get_eyelid_weight(now) |
| 82 | + self.state = 1 |
| 83 | + self.closeStart = now - duration * (1.0 - n) |
| 84 | + self.closeDuration = duration |
| 85 | + self.keepClosed = True |
| 86 | + self.advance(now) |
| 87 | + |
| 88 | + # Do state transitions if necessary |
| 89 | + def advance(self, now): |
| 90 | + if self.state == 1: # Eye currently winking/blinking? |
| 91 | + if self.closeStart + self.closeDuration <= now and not self.keepClosed: |
| 92 | + self.state = 2 |
| 93 | + |
| 94 | + if self.state == 2: # Eye currently opening |
| 95 | + if self.openStart + self.openDuration <= now: |
| 96 | + self.state = 0 |
| 97 | + |
| 98 | + # Return eyelid weight for the current blink state ranging from 0.0 (fully open) to 1.0 (fully closed). |
| 99 | + def get_eyelid_weight(self, now): |
| 100 | + |
| 101 | + # Do state transitions if necessary |
| 102 | + |
| 103 | + self.advance(now) |
| 104 | + |
| 105 | + # Now figure out progress in the current state |
| 106 | + |
| 107 | + if self.state == 1: |
| 108 | + passed = now - self.closeStart |
| 109 | + if self.closeDuration == 0 or passed >= self.closeDuration: |
| 110 | + return 1.0 |
| 111 | + else: |
| 112 | + return passed / self.closeDuration |
| 113 | + |
| 114 | + elif self.state == 2: |
| 115 | + passed = now - self.openStart |
| 116 | + if self.openDuration == 0 or passed >= self.openDuration: |
| 117 | + return 0.0 |
| 118 | + else: |
| 119 | + return 1.0 - passed / self.openDuration |
| 120 | + |
| 121 | + else: |
| 122 | + return 0.0 |
| 123 | + |
| 124 | +# All-eye selector for EyesModel's close_eye / open_eye operations. |
| 125 | +# To act on all eyes we need to pass None there but it is not very readable, |
| 126 | +# so just create an alias for that |
| 127 | +ALL = None |
| 128 | + |
| 129 | +# Eyes model - provides instant state of the eyes to the rendering engine |
| 130 | +# as well as methods that allow manipulating that state to the animation/control code. |
| 131 | +# For example control code can request a certain eye to open, close or blink and the model will |
| 132 | +# perform a smooth transition from open to closed state etc. |
| 133 | +# Model also implements automatic blinking (when enabled) |
| 134 | +class EyesModel: |
| 135 | + |
| 136 | + def __init__(self, num): |
| 137 | + self.autoblink = False |
| 138 | + self.timeOfNextBlink = 0 |
| 139 | + self.blinkState = [BlinkStateModel()] * num |
| 140 | + self.trackingPos = 0.3 |
| 141 | + self.posX = 0 |
| 142 | + self.posY = 0 |
| 143 | + self.pupilSize = 0 |
| 144 | + |
| 145 | + def random_blink_duration(self): |
| 146 | + return random.uniform(0.035, 0.06) |
| 147 | + |
| 148 | + def enable_autoblink(self, now): |
| 149 | + self.autoblink = True |
| 150 | + self.timeOfNextBlink = now + random.uniform(0.0, 4.0) |
| 151 | + |
| 152 | + def disable_autoblink(self): |
| 153 | + self.autoblink = False |
| 154 | + |
| 155 | + # Begin blinking movement (close and open an eye once). |
| 156 | + # 'index' selects an eye to perform action on, ALL can be passed to perform it on all the eyes at the same time |
| 157 | + # For details see matching method in BlinkStateModel |
| 158 | + def start_blink(self, index, now, closeDuration = None, openDuration = None): |
| 159 | + if closeDuration is None: |
| 160 | + closeDuration = self.random_blink_duration() |
| 161 | + if openDuration is None: |
| 162 | + openDuration = 2 * closeDuration |
| 163 | + |
| 164 | + if index is ALL: |
| 165 | + for i in range(len(self.blinkState)): |
| 166 | + self.start_blink(i, now, closeDuration, openDuration) |
| 167 | + else: |
| 168 | + self.blinkState[index].start_blink(now, closeDuration, openDuration) |
| 169 | + |
| 170 | + |
| 171 | + # Finish blinking movement and open an eye. |
| 172 | + # 'index' selects an eye to perform action on, ALL can be passed to perform it on all the eyes at the same time. |
| 173 | + # For details see matching method in BlinkStateModel. |
| 174 | + def complete_blink(self, index, now): |
| 175 | + if index is ALL: |
| 176 | + for i in range(len(self.blinkState)): |
| 177 | + self.complete_blink(i, now) |
| 178 | + else: |
| 179 | + self.blinkState[index].complete_blink(now) |
| 180 | + |
| 181 | + |
| 182 | + # Close an eye. |
| 183 | + # 'index' selects an eye to perform action on, ALL can be passed to perform it on all the eyes at the same time. |
| 184 | + # For details see matching method in BlinkStateModel. |
| 185 | + def close_eye(self, index, now, duration = None): |
| 186 | + if duration is None: |
| 187 | + duration = self.random_blink_duration() |
| 188 | + |
| 189 | + if index is ALL: |
| 190 | + for i in range(len(self.blinkState)): |
| 191 | + self.close_eye(i, now, duration) |
| 192 | + else: |
| 193 | + self.blinkState[index].close(now, duration) |
| 194 | + |
| 195 | + # Open an eye. |
| 196 | + # For details see matching method in BlinkStateModel. |
| 197 | + # 'index' selects an eye to perform action on, ALL can be passed to perform it on all the eyes at the same time. |
| 198 | + def open_eye(self, index, now, duration = None): |
| 199 | + if duration is None: |
| 200 | + duration = self.random_blink_duration() |
| 201 | + |
| 202 | + if index is ALL: |
| 203 | + for i in range(len(self.blinkState)): |
| 204 | + self.open_eye(i, now, duration) |
| 205 | + else: |
| 206 | + self.blinkState[index].open(now, duration) |
| 207 | + |
| 208 | + def auto_blink(self, now): |
| 209 | + if self.autoblink and now >= self.timeOfNextBlink: |
| 210 | + closeDuration = self.random_blink_duration() |
| 211 | + openDuration = closeDuration * 2.0 |
| 212 | + for blinkState in self.blinkState: |
| 213 | + blinkState.start_blink(now, closeDuration, openDuration) |
| 214 | + blinkState.complete_blink(now) |
| 215 | + self.timeOfNextBlink = now + closeDuration + openDuration + random.uniform(0.0, 4.0) |
| 216 | + |
| 217 | + def set_pupil_size(self, size): |
| 218 | + self.pupilSize = size |
| 219 | + |
| 220 | + def set_position(self, x, y): |
| 221 | + self.posX = x |
| 222 | + self.posY = y |
| 223 | + |
| 224 | + # Return state for each of the eyes in a list |
| 225 | + def get_state(self, now): |
| 226 | + |
| 227 | + self.auto_blink(now) |
| 228 | + |
| 229 | + num = len(self.blinkState) |
| 230 | + |
| 231 | + result = [] |
| 232 | + |
| 233 | +# self.trackingPos = 1 if TRACKING: |
| 234 | +# n = 0.4 - self.posY / 60.0 |
| 235 | +# if n < 0.0: n = 0.0 |
| 236 | +# elif n > 1.0: n = 1.0 |
| 237 | +# self.trackingPos = (self.trackingPos * 3.0 + n) * 0.25 |
| 238 | + |
| 239 | + for i in range(num): |
| 240 | + n = self.blinkState[i].get_eyelid_weight(now) |
| 241 | + state = EyeState() |
| 242 | + state.posX = self.posX |
| 243 | + state.posY = self.posY |
| 244 | + state.pupilSize = self.pupilSize |
| 245 | + state.upperLidWeight = self.trackingPos + (n * (1.0 - self.trackingPos)) |
| 246 | + state.lowerLidWeight = (1.0 - self.trackingPos) + (n * self.trackingPos) |
| 247 | + result.append(state) |
| 248 | + |
| 249 | + return result |
| 250 | + |
| 251 | + |
| 252 | +# Specific implementation of EyesModel for two eyes - it just introduces convergence |
| 253 | +# assuming eyes will be drawn horisontally, one next to another. |
| 254 | +# Left eye has index 0 while right eye has index 1. |
| 255 | +class TwoEyesModel(EyesModel): |
| 256 | + |
| 257 | + def __init__(self): |
| 258 | + EyesModel.__init__(self, 2) |
| 259 | + |
| 260 | + def get_state(self, now): |
| 261 | + states = EyesModel.get_state(self, now) |
| 262 | + |
| 263 | + convergence = 2.0 |
| 264 | + |
| 265 | + # Left eye |
| 266 | + states[0].posX += convergence |
| 267 | + # Right eye |
| 268 | + states[1].posX -= convergence |
| 269 | + |
| 270 | + return states |
| 271 | + |
0 commit comments