-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo.py
273 lines (227 loc) · 8.69 KB
/
demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# -*- coding: utf-8 -*-
"""
BEWEGUNG
a versatile video renderer
https://github.com/pleiszenburg/bewegung
demo/demo.py: Package demo
Copyright (C) 2020-2021 Sebastian M. Ernst <[email protected]>
<LICENSE_BLOCK>
The contents of this file are subject to the GNU Lesser General Public License
Version 2.1 ("LGPL" or "License"). You may not use this file except in
compliance with the License. You may obtain a copy of the License at
https://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
https://github.com/pleiszenburg/bewegung/blob/master/LICENSE
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
specific language governing rights and limitations under the License.
</LICENSE_BLOCK>
"""
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# IMPORT
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
import math
try:
import psutil
CPU_CORES = psutil.cpu_count(logical = False)
except ModuleNotFoundError: # fallback
import multiprocessing
CPU_CORES = multiprocessing.cpu_count() // 2 # assume HT
CPU_CORES = 1 if CPU_CORES < 1 else CPU_CORES
from bewegung import (
Camera, Color, Video,
Vector2D, Vector3D, VectorArray3D,
FadeInEffect, FadeOutEffect,
)
from bewegung.drawingboard import DrawingBoard
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# CONST
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
SVG_LOGO = """<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
viewBox="0 0 170 170"
width="170"
height="170"
>
<path
style="fill:#b4b4b4;fill-opacity:1;"
d="m 275.57824,185.64694 -154.01,-67.09663 135.11238,-99.828253 z"
transform="rotate(-44.941938,124.56725,280.97355)" />
</svg>
""".encode('utf-8')
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# ROUTINES
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
def main():
v = Video(
width = 1920,
height = 1080,
seconds = 10.0,
ctx = {
'bg_color': Color(26, 26, 26),
'bg_color_transparent': Color(26, 26, 26, 0),
'camera_dist': 30,
'camera': Camera(
position = Vector3D(30.0, 0.0, 0.0),
direction = Vector3D(-1.0, 0.0, 0.0),
planeOffset = Vector2D(1920 / 2, 1080 / 2),
planeFactor = 1000.0,
),
},
)
@v.sequence()
class Background:
@v.layer(
zindex = v.zindex.on_bottom(),
canvas = v.canvas(background_color = v.ctx['bg_color']),
)
def empty(self, canvas):
return canvas
@v.sequence(
start = v.time_from_seconds(1.0),
stop = v.time_from_seconds(-1.0),
)
class Sphere:
def __init__(self):
self._lines3d = [
VectorArray3D.from_iterable([
Vector3D.from_geographic(10.0, float(lon), float(lat))
for lat in range(-90, 90 + 10, 10)
])
for lon in range(-180, 180, 10)
]
self._lines3d.extend([
VectorArray3D.from_iterable([
Vector3D.from_geographic(10.0, float(lon), float(lat))
for lon in range(-180, 180 + 10, 10)
])
for lat in range(-90 + 10, 90, 10)
])
self._lines2d = None
self._factor = None
@v.prepare(
preporder = v.preporder.on_bottom(),
)
def move_camera(self, time):
angle = 2 * math.pi * time.seconds / 120.0
position2d = Vector2D.from_polar(radius = v.ctx['camera_dist'], angle = angle)
direction2d = Vector2D.from_polar(radius = 1.0, angle = math.pi + angle)
v.ctx['camera'].position = Vector3D(x = position2d.x, y = position2d.y, z = 0.0)
v.ctx['camera'].direction = Vector3D(x = direction2d.x, y = direction2d.y, z = 0.0)
v.ctx['camera'].roll = angle
@v.prepare(
preporder = v.preporder.on_top(),
)
def project(self):
self._lines2d = [
v.ctx['camera'].get_points(line3d).as_list()
for line3d in self._lines3d
]
self._lines2d = [
(a, b)
for line2d in self._lines2d
for a, b in zip(line2d[:-1], line2d[1:])
]
self._lines2d.sort(key = lambda item: item[0].meta['dist'], reverse = True)
minimum = self._lines2d[0][0].meta['dist']
maximum = self._lines2d[-1][0].meta['dist']
self._factor = lambda x: (x - minimum) / (maximum - minimum)
@FadeInEffect(v.time_from_seconds(4.0))
@FadeOutEffect(v.time_from_seconds(2.0))
@v.layer(
zindex = v.zindex.on_top(),
canvas = v.canvas(background_color = v.ctx['bg_color_transparent']),
)
def wiremesh(self, canvas):
for line in self._lines2d:
factor = self._factor(line[0].meta['dist'])
gray = round(64 + 191 * factor)
canvas.draw_polygon(
*line,
line_color = Color(gray, gray, gray),
line_width = 1.6 + 1.6 * factor,
)
return canvas
@v.sequence(
stop = v.time_from_seconds(3.0),
)
class Intro:
def __init__(self):
self._font = DrawingBoard.make_font('Arial', 40.0)
@FadeInEffect(v.time_from_seconds(1.6))
@FadeOutEffect(v.time_from_seconds(0.8))
@v.layer(
zindex = v.zindex.on_top(),
canvas = v.canvas(background_color = v.ctx['bg_color_transparent'], height = 200),
offset = Vector2D(20, 20),
)
def text(self, canvas):
canvas.draw_text(
text = 'bewegung - a versatile video renderer\nDEMO',
point = Vector2D(0.0, 0.0),
font = self._font,
font_color = Color(180, 180, 180),
anchor = 'tl',
)
return canvas
@v.sequence(
start = v.time_from_seconds(-5.0),
stop = v.time_from_seconds(-1.0),
)
class Credits:
def __init__(self):
self._font = DrawingBoard.make_font('Arial', 40.0)
@FadeInEffect(v.time_from_seconds(2.0))
@FadeOutEffect(v.time_from_seconds(2.0))
@v.layer(
zindex = v.zindex.on_top(),
canvas = v.canvas(background_color = v.ctx['bg_color_transparent'], height = 200),
offset = Vector2D(-20, v.height - 20 - 200),
)
def text(self, canvas):
canvas.draw_text(
text = 'github.com/pleiszenburg/bewegung',
point = Vector2D(v.width, 200),
font = self._font,
font_color = Color(180, 180, 180),
alignment = 'r',
anchor = 'br',
)
return canvas
@v.sequence(
start = v.time_from_seconds(-1.2),
)
class CreditsLogo:
def __init__(self):
self._svg = DrawingBoard.make_svg(raw = SVG_LOGO)
self._font = DrawingBoard.make_font('Arial', 25.0)
@FadeInEffect(v.time_from_seconds(0.8))
@FadeOutEffect(v.time_from_seconds(0.3))
@v.layer(
zindex = v.zindex.on_top(),
canvas = v.canvas(background_color = v.ctx['bg_color_transparent']),
)
def text(self, canvas):
canvas.draw_svg(
svg = self._svg,
point = Vector2D(v.width / 2, v.height / 2 - 50),
scale = 2.0,
anchor = 'cc',
)
canvas.draw_text(
text = 'pleiszenburg.de - Independent Scientific Services\n2020',
point = Vector2D(v.width / 2, v.height / 2 + 135),
font = self._font,
font_color = Color(180, 180, 180),
alignment = 'c',
anchor = 'tc',
)
return canvas
v.render(
processes = CPU_CORES,
video_fn = 'video.mp4',
)
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# ENTRY
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if __name__ == '__main__':
main()