classes.c70_animation

 1from os import path
 2import drawBot
 3from typing import Callable
 4from functools import partial
 5from lib import files
 6
 7
 8def calcFrameDuration(fps: int) -> float:
 9    return 1 / fps
10
11
12class KAnimFrame:
13    def __init__(self, i: int, animation: "KAnimation") -> None:
14        self.i = i
15        self.done = animation.calcDone(i)
16        self.frameCount = animation.frameCount
17        "Total number of frames"
18        self.fps = animation.fps
19        self.frameDuration = calcFrameDuration(self.fps)
20        "`float` used in `DrawBot.frameDuration()`"
21        self.time = self.i / self.fps
22        "Time in seconds"
23
24    def __repr__(self):
25        return "<KAnimFrame {:04d}, {:04.2f}s, {:06.4f}%>".format(
26            self.i, self.time, self.done
27        )
28
29
30class KAnimation:
31    def __init__(
32        self, func: Callable, funcArgs: dict = None, seconds=3, fps=30
33    ) -> None:
34        self.func = partial(func, **funcArgs) if funcArgs else func
35        self.frameCount = seconds * fps
36        self.fps = fps
37        self.frameDuration = calcFrameDuration(fps)
38
39    def calcDone(self, i: int) -> float:
40        "`float` 0 = start, 1 = end"
41        return i / (self.frameCount - 1)
42
43    def previewFrame(self, i: int):
44        "Preview frame at `index`"
45        self.func(KAnimFrame(i, self))
46
47    def storyboard(self, frames: list[int]):
48        "Preview frames at `indices`"
49        for i in frames:
50            self.previewFrame(i)
51
52    def render(self):
53        "Preview entire animation"
54        self.storyboard(frames=range(self.frameCount))
55
56    def export(self, filePath: str):
57        "Render and save"
58        files.createFolder(path.dirname(filePath))
59        drawBot.newDrawing()
60        self.render()
61        drawBot.saveImage(filePath)
62        drawBot.endDrawing()
def calcFrameDuration(fps: int) -> float:
 9def calcFrameDuration(fps: int) -> float:
10    return 1 / fps
class KAnimFrame:
13class KAnimFrame:
14    def __init__(self, i: int, animation: "KAnimation") -> None:
15        self.i = i
16        self.done = animation.calcDone(i)
17        self.frameCount = animation.frameCount
18        "Total number of frames"
19        self.fps = animation.fps
20        self.frameDuration = calcFrameDuration(self.fps)
21        "`float` used in `DrawBot.frameDuration()`"
22        self.time = self.i / self.fps
23        "Time in seconds"
24
25    def __repr__(self):
26        return "<KAnimFrame {:04d}, {:04.2f}s, {:06.4f}%>".format(
27            self.i, self.time, self.done
28        )
KAnimFrame(i: int, animation: KAnimation)
14    def __init__(self, i: int, animation: "KAnimation") -> None:
15        self.i = i
16        self.done = animation.calcDone(i)
17        self.frameCount = animation.frameCount
18        "Total number of frames"
19        self.fps = animation.fps
20        self.frameDuration = calcFrameDuration(self.fps)
21        "`float` used in `DrawBot.frameDuration()`"
22        self.time = self.i / self.fps
23        "Time in seconds"
i
done
frameCount

Total number of frames

fps
frameDuration

float used in DrawBot.frameDuration()

time

Time in seconds

class KAnimation:
31class KAnimation:
32    def __init__(
33        self, func: Callable, funcArgs: dict = None, seconds=3, fps=30
34    ) -> None:
35        self.func = partial(func, **funcArgs) if funcArgs else func
36        self.frameCount = seconds * fps
37        self.fps = fps
38        self.frameDuration = calcFrameDuration(fps)
39
40    def calcDone(self, i: int) -> float:
41        "`float` 0 = start, 1 = end"
42        return i / (self.frameCount - 1)
43
44    def previewFrame(self, i: int):
45        "Preview frame at `index`"
46        self.func(KAnimFrame(i, self))
47
48    def storyboard(self, frames: list[int]):
49        "Preview frames at `indices`"
50        for i in frames:
51            self.previewFrame(i)
52
53    def render(self):
54        "Preview entire animation"
55        self.storyboard(frames=range(self.frameCount))
56
57    def export(self, filePath: str):
58        "Render and save"
59        files.createFolder(path.dirname(filePath))
60        drawBot.newDrawing()
61        self.render()
62        drawBot.saveImage(filePath)
63        drawBot.endDrawing()
KAnimation(func: Callable, funcArgs: dict = None, seconds=3, fps=30)
32    def __init__(
33        self, func: Callable, funcArgs: dict = None, seconds=3, fps=30
34    ) -> None:
35        self.func = partial(func, **funcArgs) if funcArgs else func
36        self.frameCount = seconds * fps
37        self.fps = fps
38        self.frameDuration = calcFrameDuration(fps)
func
frameCount
fps
frameDuration
def calcDone(self, i: int) -> float:
40    def calcDone(self, i: int) -> float:
41        "`float` 0 = start, 1 = end"
42        return i / (self.frameCount - 1)

float 0 = start, 1 = end

def previewFrame(self, i: int):
44    def previewFrame(self, i: int):
45        "Preview frame at `index`"
46        self.func(KAnimFrame(i, self))

Preview frame at index

def storyboard(self, frames: list[int]):
48    def storyboard(self, frames: list[int]):
49        "Preview frames at `indices`"
50        for i in frames:
51            self.previewFrame(i)

Preview frames at indices

def render(self):
53    def render(self):
54        "Preview entire animation"
55        self.storyboard(frames=range(self.frameCount))

Preview entire animation

def export(self, filePath: str):
57    def export(self, filePath: str):
58        "Render and save"
59        files.createFolder(path.dirname(filePath))
60        drawBot.newDrawing()
61        self.render()
62        drawBot.saveImage(filePath)
63        drawBot.endDrawing()

Render and save