classes.c70_animation

 1from os import path
 2import drawBot
 3from drawBot import _drawBotDrawingTool as drawBot
 4from typing import Callable
 5from functools import partial
 6from lib import helpers
 7
 8
 9def calcFrameDuration(fps: int) -> float:
10    return 1 / fps
11
12
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        )
29
30
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, file: str):
58        "Render and save"
59        helpers.createFolder(path.dirname(file))
60        drawBot.newDrawing()
61        self.render()
62        drawBot.saveImage(file)
63        drawBot.endDrawing()
def calcFrameDuration(fps: int) -> float:
10def calcFrameDuration(fps: int) -> float:
11    return 1 / fps
class KAnimFrame:
14class KAnimFrame:
15    def __init__(self, i: int, animation: "KAnimation") -> None:
16        self.i = i
17        self.done = animation.calcDone(i)
18        self.frameCount = animation.frameCount
19        "Total number of frames"
20        self.fps = animation.fps
21        self.frameDuration = calcFrameDuration(self.fps)
22        "`float` used in `DrawBot.frameDuration()`"
23        self.time = self.i / self.fps
24        "Time in seconds"
25
26    def __repr__(self):
27        return "<KAnimFrame {:04d}, {:04.2f}s, {:06.4f}%>".format(
28            self.i, self.time, self.done
29        )
KAnimFrame(i: int, animation: KAnimation)
15    def __init__(self, i: int, animation: "KAnimation") -> None:
16        self.i = i
17        self.done = animation.calcDone(i)
18        self.frameCount = animation.frameCount
19        "Total number of frames"
20        self.fps = animation.fps
21        self.frameDuration = calcFrameDuration(self.fps)
22        "`float` used in `DrawBot.frameDuration()`"
23        self.time = self.i / self.fps
24        "Time in seconds"
i
done
frameCount

Total number of frames

fps
frameDuration

float used in DrawBot.frameDuration()

time

Time in seconds

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

float 0 = start, 1 = end

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

Preview frame at index

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

Preview frames at indices

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

Preview entire animation

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

Render and save