Skip to content

Commit

Permalink
Version 0.9.12: Fixed reusability of FrameUtility and implemented a m…
Browse files Browse the repository at this point in the history
…ethod to reset a FrameUtility object.
  • Loading branch information
CoffeeStraw committed Jan 5, 2022
1 parent 690a69b commit f576fc8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pyonfx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
from .shape import Shape
from .utils import Utils, FrameUtility, ColorUtility

__version__ = "0.9.11"
__version__ = "0.9.12"
41 changes: 23 additions & 18 deletions pyonfx/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,33 +168,38 @@ def __init__(self, start_time: float, end_time: float, fr: float = 41.71):
if start_time < 0 or end_time < 0 or fr <= 0 or end_time < start_time:
raise ValueError("Positive values and/or end_time > start_time expected.")

self.start_time = self.current_time = start_time
self.end_time = end_time
self.fr = self.fr_time = fr

# Calculating number of frames
self.n = math.ceil((end_time - start_time) / fr)

# Defining fields
self.start_time = start_time
self.end_time = end_time
self.current_time = fr
self.fr = fr

def __iter__(self):
# For loop for the first n-1 frames
# Compute values for the first n-1 frames
for i in range(1, self.n):
yield (
round(self.start_time, 2),
round(self.start_time + self.fr, 2),
round(self.current_time, 2),
round(self.current_time + self.fr, 2),
i,
self.n,
)
self.start_time += self.fr
self.current_time += self.fr
self.fr_time += self.fr

# Last frame, with end value clamped at end_time
yield (round(self.start_time, 2), round(self.end_time, 2), self.n, self.n)
# Compute values for the last frame, clamping the end_time of the frame at self.end_time
yield (round(self.current_time, 2), round(self.end_time, 2), self.n, self.n)

# Resetting to make this object usable again
self.start_time = self.start_time - self.fr * max(self.n - 1, 0)
self.current_time = self.fr
# Reset the object to make it usable again
self.reset()

def reset(self):
"""
Resets the FrameUtility object to its starting values.
It is a necessary operation if you want to reuse the same object.
"""
self.current_time = self.start_time
self.fr_time = self.fr

def add(
self,
Expand Down Expand Up @@ -230,12 +235,12 @@ def add(
>>> Frame 3/3: 80 - 105; fsc: 100
"""

if self.current_time < start_time:
if self.fr_time < start_time:
return 0
elif self.current_time > end_time:
elif self.fr_time > end_time:
return end_value

pstart = self.current_time - start_time
pstart = self.fr_time - start_time
pend = end_time - start_time
return Utils.interpolate(pstart / pend, 0, end_value, accelerator)

Expand Down

0 comments on commit f576fc8

Please sign in to comment.