Created
October 21, 2019 21:05
-
-
Save MegaLoler/68f3c09ad1b00ce0794b0c9913513a81 to your computer and use it in GitHub Desktop.
generates animation frames as svgs for a praticular animation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import cairo | |
| import numpy as np | |
| def project(point, fov): | |
| x, y, z = point[0:3] | |
| d = z*fov+1 | |
| return x/d, y/d | |
| def translation(x, y, z): | |
| return np.array([[1, 0, 0, x], [0, 1, 0, y], [0, 0, 1, z], [0, 0, 0, 1]]) | |
| def scale(x, y, z): | |
| return np.array([[x, 0, 0, 0], [0, y, 0, 0], [0, 0, z, 0], [0, 0, 0, 1]]) | |
| def rotationX(theta): | |
| return np.array([[1, 0, 0, 0], [0, np.cos(theta), -np.sin(theta), 0], [0, np.sin(theta), np.cos(theta), 0], [0, 0, 0, 1]]) | |
| def rotationY(theta): | |
| return np.array([[np.cos(theta), 0, -np.sin(theta), 0], [0, 1, 0, 0], [np.sin(theta), 0, np.cos(theta), 0], [0, 0, 0, 1]]) | |
| def rotationZ(theta): | |
| return np.array([[np.cos(theta), -np.sin(theta), 0, 0], [np.sin(theta), np.cos(theta), 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) | |
| class Transformable: | |
| def transformWithTransformation(self, m, t=np.identity(4)): | |
| self.transform = np.matmul(np.linalg.inv(t), self.transform) | |
| self.transform = np.matmul(m, self.transform) | |
| self.transform = np.matmul(t, self.transform) | |
| class Box(Transformable): | |
| def __init__(self, transform=np.identity(4)): | |
| self.transform = transform | |
| def render(self, context, transform, fov): | |
| points = (-1, -1, -1, 1), (1, -1, -1, 1), (-1, 1, -1, 1), (1, 1, -1, 1), (-1, -1, 1, 1), (1, -1, 1, 1), (-1, 1, 1, 1), (1, 1, 1, 1) | |
| edges = (0, 1), (0, 2), (1, 3), (2, 3), (4, 5), (4, 6), (5, 7), (6, 7), (0, 4), (1, 5), (2, 6), (3, 7) | |
| t = np.matmul(transform, self.transform) | |
| transformed = list(map(lambda x: project(np.matmul(t, x), fov), points)) | |
| for edge in edges: | |
| ai, bi = edge | |
| a = transformed[ai] | |
| b = transformed[bi] | |
| context.move_to(*a) | |
| context.line_to(*b) | |
| context.stroke() | |
| @property | |
| def origin(self): | |
| return np.matmul(self.transform, np.array([0, 0, 0, 1])) | |
| class Scene(Transformable): | |
| def __init__(self, transform=np.identity(4), objects=None): | |
| self.transform = transform | |
| self.objects = objects or [] | |
| def render(self, context, transform=np.identity(4), fov=1/2): | |
| t = np.matmul(transform, self.transform) | |
| for obj in self.objects: | |
| obj.render(context, t, fov) | |
| def render(scene, name): | |
| WIDTH, HEIGHT = 800, 800 | |
| with cairo.SVGSurface(name, WIDTH, HEIGHT) as surface: | |
| context = cairo.Context(surface) | |
| context.scale(WIDTH/2, -HEIGHT/2) | |
| context.translate(1,-1) | |
| context.set_line_width(0.005) | |
| scene.render(context) | |
| frame = 1 | |
| def snap(): | |
| global frame, scene | |
| render(scene, f'{frame}.svg') | |
| frame += 1 | |
| foot_r_box = Box(scale(.4, .4, .6)) | |
| foot_l_box = Box(scale(.4, .4, .6)) | |
| foot_r = Scene(translation(1/2+0.1, -1, 0), [foot_r_box]) | |
| foot_l = Scene(translation(-1/2-0.1, -1, 0), [foot_l_box]) | |
| body = Box(translation(0, 0.4, 0)) | |
| a = Box(np.matmul(translation(1, -1.4, 0), scale(1, 0, 1))) | |
| b = Box(np.matmul(translation(-1, -1.4, 0), scale(1, 0, 1))) | |
| c = Box(np.matmul(translation(1, -1.4, -2), scale(1, 0, 1))) | |
| d = Box(np.matmul(translation(-1, -1.4, -2), scale(1, 0, 1))) | |
| e = Box(np.matmul(translation(1, -1.4, -4), scale(1, 0, 1))) | |
| f = Box(np.matmul(translation(-1, -1.4, -4), scale(1, 0, 1))) | |
| grid = Scene(objects=[a, b, c, d, e, f]) | |
| o = body.transform | |
| t = rotationY(np.pi/4) | |
| body.transformWithTransformation(t, o) | |
| foot_l.transformWithTransformation(t, o) | |
| foot_r.transformWithTransformation(t, o) | |
| scene = Scene(np.matmul(rotationX(np.pi/16), translation(0, 0, 4)), [foot_r, foot_l, body, grid]) | |
| scene.transformWithTransformation(scale(0.5, 0.5, 0.5)) | |
| o = foot_l.transform | |
| t = rotationY(-np.pi/4) | |
| body.transformWithTransformation(t, o) | |
| foot_l.transformWithTransformation(t, o) | |
| foot_r.transformWithTransformation(t, o) | |
| snap() | |
| o = foot_l.transform | |
| t = rotationZ(np.pi/8) | |
| body.transformWithTransformation(t, o) | |
| foot_r.transformWithTransformation(t, o) | |
| scene.transformWithTransformation(translation(0, -0.125, 0.05)) | |
| scene.transformWithTransformation(rotationX(-np.pi/64)) | |
| snap() | |
| o = foot_l.transform | |
| t = rotationZ(np.pi/8) | |
| body.transformWithTransformation(t, o) | |
| foot_r.transformWithTransformation(t, o) | |
| scene.transformWithTransformation(translation(0, -0.125, 0.05)) | |
| scene.transformWithTransformation(rotationX(-np.pi/64)) | |
| snap() | |
| o = foot_l.transform | |
| t = rotationY(-np.pi/8) | |
| body.transformWithTransformation(t, o) | |
| foot_l.transformWithTransformation(t, o) | |
| foot_r.transformWithTransformation(t, o) | |
| scene.transformWithTransformation(translation(0, -0.125, 0.05)) | |
| scene.transformWithTransformation(rotationX(-np.pi/64)) | |
| snap() | |
| o = foot_l.transform | |
| t = rotationY(-np.pi/8) | |
| body.transformWithTransformation(t, o) | |
| foot_l.transformWithTransformation(t, o) | |
| foot_r.transformWithTransformation(t, o) | |
| scene.transformWithTransformation(translation(0, -0.125, 0.05)) | |
| scene.transformWithTransformation(rotationX(-np.pi/64)) | |
| snap() | |
| o = foot_l.transform | |
| t = rotationZ(-np.pi/8) | |
| body.transformWithTransformation(t, o) | |
| foot_r.transformWithTransformation(t, o) | |
| scene.transformWithTransformation(translation(0, -0.125, 0.05)) | |
| scene.transformWithTransformation(rotationX(-np.pi/64)) | |
| snap() | |
| o = foot_l.transform | |
| t = rotationZ(-np.pi/8) | |
| body.transformWithTransformation(t, o) | |
| foot_r.transformWithTransformation(t, o) | |
| scene.transformWithTransformation(translation(0, -0.125, 0.05)) | |
| scene.transformWithTransformation(rotationX(-np.pi/64)) | |
| snap() | |
| o = foot_r.transform | |
| t = rotationZ(-np.pi/8) | |
| body.transformWithTransformation(t, o) | |
| foot_l.transformWithTransformation(t, o) | |
| scene.transformWithTransformation(translation(0, -0.125, 0.05)) | |
| scene.transformWithTransformation(rotationX(-np.pi/64)) | |
| snap() | |
| o = foot_r.transform | |
| t = rotationZ(-np.pi/8) | |
| body.transformWithTransformation(t, o) | |
| foot_l.transformWithTransformation(t, o) | |
| scene.transformWithTransformation(translation(0, -0.125, 0.05)) | |
| scene.transformWithTransformation(rotationX(-np.pi/64)) | |
| snap() | |
| o = foot_r.transform | |
| t = rotationY(np.pi/8) | |
| body.transformWithTransformation(t, o) | |
| foot_l.transformWithTransformation(t, o) | |
| foot_r.transformWithTransformation(t, o) | |
| scene.transformWithTransformation(translation(0, -0.125, 0.05)) | |
| scene.transformWithTransformation(rotationX(-np.pi/64)) | |
| snap() | |
| o = foot_r.transform | |
| t = rotationY(np.pi/8) | |
| body.transformWithTransformation(t, o) | |
| foot_l.transformWithTransformation(t, o) | |
| foot_r.transformWithTransformation(t, o) | |
| scene.transformWithTransformation(translation(0, -0.125, 0.05)) | |
| scene.transformWithTransformation(rotationX(-np.pi/64)) | |
| snap() | |
| o = foot_r.transform | |
| t = rotationY(np.pi/8) | |
| body.transformWithTransformation(t, o) | |
| foot_l.transformWithTransformation(t, o) | |
| foot_r.transformWithTransformation(t, o) | |
| scene.transformWithTransformation(translation(0, -0.125, 0.05)) | |
| scene.transformWithTransformation(rotationX(-np.pi/64)) | |
| snap() | |
| o = foot_r.transform | |
| t = rotationY(np.pi/8) | |
| body.transformWithTransformation(t, o) | |
| foot_l.transformWithTransformation(t, o) | |
| foot_r.transformWithTransformation(t, o) | |
| scene.transformWithTransformation(translation(0, -0.125, 0.05)) | |
| scene.transformWithTransformation(rotationX(-np.pi/64)) | |
| snap() | |
| o = foot_r.transform | |
| t = rotationZ(np.pi/8) | |
| body.transformWithTransformation(t, o) | |
| foot_l.transformWithTransformation(t, o) | |
| scene.transformWithTransformation(translation(0, -0.125, 0.05)) | |
| scene.transformWithTransformation(rotationX(-np.pi/64)) | |
| snap() | |
| o = foot_r.transform | |
| t = rotationZ(np.pi/8) | |
| body.transformWithTransformation(t, o) | |
| foot_l.transformWithTransformation(t, o) | |
| scene.transformWithTransformation(translation(0, -0.125, 0.05)) | |
| scene.transformWithTransformation(rotationX(-np.pi/64)) | |
| snap() | |
| o = foot_l.transform | |
| t = rotationZ(np.pi/8) | |
| body.transformWithTransformation(t, o) | |
| foot_r.transformWithTransformation(t, o) | |
| scene.transformWithTransformation(translation(0, -0.125, 0.05)) | |
| scene.transformWithTransformation(rotationX(-np.pi/64)) | |
| snap() | |
| o = foot_l.transform | |
| t = rotationZ(np.pi/8) | |
| body.transformWithTransformation(t, o) | |
| foot_r.transformWithTransformation(t, o) | |
| scene.transformWithTransformation(translation(0, -0.125, 0.05)) | |
| scene.transformWithTransformation(rotationX(-np.pi/64)) | |
| snap() | |
| o = foot_l.transform | |
| t = rotationY(-np.pi/8) | |
| body.transformWithTransformation(t, o) | |
| foot_l.transformWithTransformation(t, o) | |
| foot_r.transformWithTransformation(t, o) | |
| scene.transformWithTransformation(translation(0, -0.125, 0.05)) | |
| scene.transformWithTransformation(rotationX(-np.pi/64)) | |
| snap() | |
| o = foot_l.transform | |
| t = rotationY(-np.pi/8) | |
| body.transformWithTransformation(t, o) | |
| foot_l.transformWithTransformation(t, o) | |
| foot_r.transformWithTransformation(t, o) | |
| scene.transformWithTransformation(translation(0, -0.125, 0.05)) | |
| scene.transformWithTransformation(rotationX(-np.pi/64)) | |
| snap() | |
| o = foot_l.transform | |
| t = rotationY(-np.pi/8) | |
| body.transformWithTransformation(t, o) | |
| foot_l.transformWithTransformation(t, o) | |
| foot_r.transformWithTransformation(t, o) | |
| scene.transformWithTransformation(translation(0, -0.125, 0.05)) | |
| scene.transformWithTransformation(rotationX(-np.pi/64)) | |
| snap() | |
| o = foot_l.transform | |
| t = rotationY(-np.pi/8) | |
| body.transformWithTransformation(t, o) | |
| foot_l.transformWithTransformation(t, o) | |
| foot_r.transformWithTransformation(t, o) | |
| scene.transformWithTransformation(translation(0, -0.125, 0.05)) | |
| scene.transformWithTransformation(rotationX(-np.pi/64)) | |
| snap() | |
| o = foot_l.transform | |
| t = rotationZ(-np.pi/8) | |
| body.transformWithTransformation(t, o) | |
| foot_r.transformWithTransformation(t, o) | |
| scene.transformWithTransformation(translation(0, -0.125, 0.05)) | |
| scene.transformWithTransformation(rotationX(-np.pi/64)) | |
| snap() | |
| o = foot_l.transform | |
| t = rotationZ(-np.pi/8) | |
| body.transformWithTransformation(t, o) | |
| foot_r.transformWithTransformation(t, o) | |
| scene.transformWithTransformation(translation(0, -0.125, 0.05)) | |
| scene.transformWithTransformation(rotationX(-np.pi/64)) | |
| snap() | |
| o = foot_r.transform | |
| t = rotationZ(-np.pi/8) | |
| body.transformWithTransformation(t, o) | |
| foot_l.transformWithTransformation(t, o) | |
| scene.transformWithTransformation(translation(0, -0.125, 0.05)) | |
| scene.transformWithTransformation(rotationX(-np.pi/64)) | |
| snap() | |
| o = foot_r.transform | |
| t = rotationZ(-np.pi/8) | |
| body.transformWithTransformation(t, o) | |
| foot_l.transformWithTransformation(t, o) | |
| scene.transformWithTransformation(translation(0, -0.125, 0.05)) | |
| scene.transformWithTransformation(rotationX(-np.pi/64)) | |
| snap() | |
| o = foot_r.transform | |
| t = rotationY(np.pi/8) | |
| body.transformWithTransformation(t, o) | |
| foot_l.transformWithTransformation(t, o) | |
| foot_r.transformWithTransformation(t, o) | |
| scene.transformWithTransformation(translation(0, -0.125, 0.05)) | |
| scene.transformWithTransformation(rotationX(-np.pi/64)) | |
| snap() | |
| o = foot_r.transform | |
| t = rotationY(np.pi/8) | |
| body.transformWithTransformation(t, o) | |
| foot_l.transformWithTransformation(t, o) | |
| foot_r.transformWithTransformation(t, o) | |
| scene.transformWithTransformation(translation(0, -0.125, 0.05)) | |
| scene.transformWithTransformation(rotationX(-np.pi/64)) | |
| snap() | |
| o = foot_r.transform | |
| t = rotationY(np.pi/8) | |
| body.transformWithTransformation(t, o) | |
| foot_l.transformWithTransformation(t, o) | |
| foot_r.transformWithTransformation(t, o) | |
| scene.transformWithTransformation(translation(0, -0.125, 0.05)) | |
| scene.transformWithTransformation(rotationX(-np.pi/64)) | |
| snap() | |
| o = foot_r.transform | |
| t = rotationY(np.pi/8) | |
| body.transformWithTransformation(t, o) | |
| foot_l.transformWithTransformation(t, o) | |
| foot_r.transformWithTransformation(t, o) | |
| scene.transformWithTransformation(translation(0, -0.125, 0.05)) | |
| scene.transformWithTransformation(rotationX(-np.pi/64)) | |
| snap() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment