ani.html


001| '''Animation Central for the Cube Nets project.
001| 
001| Matrix Rotations (looking in minus direction along axis):
001|     X: XP=CCW, XN=CW
001|     Y: YP=CW , YN=CCW   <<< note difference!
001|     Z: ZP=CCW, ZN=CW
001| 
001| Functions:
001|     do_all_merges                   -do merge_moves() for all animation segments
001|     do_all_reverses                 -do rev_ff_ff() for all animation segments
001|     do_title_frames                 -Title card frames.
001|     do_title_sequence               -Title card titles.
001|     do_intro_sequence_1             -Intro card titles (#1).
001|     do_intro_sequence_2             -Intro card titles (#2).
001|     do_main_sequence_fade_in_out    -Initial fade in and final fade out.
001|     do_end_title                    -That's all for now!
001| 
001|     do_merge_moves      -merge move folders into an animation folder
001|     do_rev_ff_ff        -create the reversed files and two freeze frame files
001| 
001|     do_recycle          -do both mkdirs() and rmdirs()
001|     do_mkdirs           -create all animation directories (skips existing ones)
001|     do_rmdirs           -remove all animation directories (and files!)
001|     gen_dirnames        -[generator] list of animation directories
001| 
001|     do_solid            -[general] create a set of solid-color frames
001|     do_freeze           -[general] create a set of freeze frames
001|     do_reverse          -[general] reverse a set of frames
001| 
001|     do_fibs             -[general] fade in from black, single frame
001|     do_fobs             -[general] fade out to black, single frame
001|     do_fibm             -[general] fade in from black, multiple frames
001|     do_fobm             -[general] fade out to black, multiple frames
001| 
001|     do_title            -[general] create a set of title frames
001| 
001|     do_three_titles_over_one_frame
001| '''

002| ####################################################################################################
003| from sys import argv
004| from os import path
005| from math import sqrt, sin,cos, asin,acos, pi,degrees,radians
006| from goodies import KWARG, typename, svec
007| from animation import dispatch
008| from cubenet_defs import NbrMoves
009| ####################################################################################################
010| BasePath = r'C:\CJS\prj\Python\app\cubenets'
011| AniPath = path.join(BasePath, 'ani')
012| PovPath = r'C:\CJS\prj\pov\ws\cube\ani'
013| SrcFile = 'cubenets_%02d.png'
014| SrcFile3 = 'cubenets_%03d.png'
015| DstFile = 'img_%04d.png'
016| 
017| DefaultSourceDir = '0'
018| DefaultTargetDir = '_tmp'
019| 
020| ##================================================================================================##
021| def ani_geometry (steps=375):
022|     '''Animate the 2-arm diagram in app::do_draw_qcircle.'''
023|     from app import dispatch as cmnd
024| 
025|     props = dict(
026|         fpath = path.join(AniPath, 'qc5'),
027|         xdim  = 7680, ydim = 4320, # 8K size; shrink to 1920x1080
028|         ff = 4,
029|         xlim0 = -2.05, xlim1 = +2.05,
030|         ylim0 = -0.15, ylim1 = +2.15,
031|     )
032|     for ix in range(steps+1):
033|         # Animation values...
034|         props['fname'] = 'img_%04d.png' % ix
035|         props['angle'] = 90.0 * (ix/steps)
036|         # Draw a frame...
037|         cmnd('qcircle', **props)
038|         print('wrote: %s' % props['fname'])
039| 
040| 
041| ##================================================================================================##
042| DirNames = [
043|     '11','21','31','41','51','61','71','81','91','a1','b1', # animation series 1
044|     '12', # anim series 2 (such as it is)
045| ]
046| 
047| FramesPerMove = 46
048| 
049| ##================================================================================================##
050| def gen_dirnames (make_dirs_only=True):
051|     '''[generator] List of animation directories.'''
052|     for dn,nm in zip(DirNames, NbrMoves):
053|         for ix in range(4):
054|             yield path.join(AniPath, 'n%sa%s_%d' % (*dn, ix))
055|         if make_dirs_only:
056|             continue
057|         for ix in range(nm):
058|             yield path.join(AniPath, 'n%sa%sm%d' % (*dn, 1+ix))
059| 
060| ##================================================================================================##
061| def do_mkdirs (make_dirs_only=True):
062|     '''Create all animation directories. (Skips existing ones)'''
063|     from os import mkdir
064| 
065|     for dirname in gen_dirnames(make_dirs_only=make_dirs_only):
066|         if path.exists(dirname):
067|             print('Directory already exists: %s' % dirname)
068| 
069|         else:
070|             mkdir(dirname)
071|             print('Created: %s' % dirname)
072|     print()
073| 
074| ##================================================================================================##
075| def do_rmdirs (make_dirs_only=True):
076|     '''Remove all animation directories. (And files!)'''
077|     from os import rmdir, remove, listdir
078| 
079|     for dirname in gen_dirnames(make_dirs_only=make_dirs_only):
080|         if path.exists(dirname):
081| 
082|             # If any files exist, remove them...
083|             fns = listdir(dirname)
084|             for fn in fns:
085|                 ffn = path.join(dirname, fn)
086|                 remove(ffn)
087|             print('Files Deleted: %s' % len(fns))
088| 
089|             # Directory is empty now, remove it...
090|             rmdir(dirname)
091|             print('Removed: %s' % dirname)
092| 
093|         else:
094|             print('Directory doesn\'t exist: %s' % dirname)
095|     print()
096| 
097|     # Clean other directories...
098|     for dn in ['0','1','2','3','9','n0','nz','_tmp']:
099|         dirname = path.join(AniPath, dn)
100|         print('Cleaning: %s' % dirname)
101|         fns = listdir(dirname)
102|         for fn in fns:
103|             ffn = path.join(dirname, fn)
104|             remove(ffn)
105|         print('Files Deleted: %s' % len(fns))
106|     print()
107| 
108| ##================================================================================================##
109| def do_recycle (make_dirs_only=True):
110|     do_rmdirs(make_dirs_only=make_dirs_only)
111|     do_mkdirs(make_dirs_only=make_dirs_only)
112| 
113| 
114| ##================================================================================================##
115| def do_solid (frames=25):
116|     '''Create a set of solid-color frames.'''
117|     props = dict(
118|         dpath = path.join(AniPath, DefaultTargetDir),
119|         dname = DstFile,
120|         start = 0,
121|         count = frames,
122|         color = 'black',
123|     )
124|     dispatch('_f', **props)
125| 
126| ##================================================================================================##
127| def do_freeze (frames=25):
128|     '''Create a set of freeze frames.'''
129|     props = dict(
130|         spath = path.join(AniPath, DefaultSourceDir),
131|         dpath = path.join(AniPath, DefaultTargetDir),
132|         sname = 'img_1234.png',
133|         dname = DstFile,
134|         start = 0,
135|         count = frames,
136|     )
137|     dispatch('ff', **props)
138| 
139| ##================================================================================================##
140| def do_copy (frames=FramesPerMove):
141|     '''Copy a set of frames.'''
142|     props = dict(
143|         spath = path.join(AniPath, DefaultSourceDir),
144|         dpath = path.join(AniPath, DefaultTargetDir),
145|         sname = DstFile,
146|         dname = DstFile,
147|         count = frames,
148|         sinit = 0,
149|         dinit = 0,
150|         sstep = +1,
151|         dstep = +1,
152|     )
153|     dispatch('cp', **props)
154| 
155| ##================================================================================================##
156| def do_reverse (frames=FramesPerMove):
157|     '''Create a set of reversed frames.'''
158|     props = dict(
159|         #spath = path.join(AniPath, DefaultSourceDir),
160|         #dpath = path.join(AniPath, DefaultTargetDir),
161|         spath = path.join(AniPath, 'qc5'), # two-arms frames
162|         dpath = path.join(AniPath, 'qc6'),
163|         sname = DstFile,
164|         dname = DstFile,
165|         count = frames,
166|         sinit = 0,
167|         dinit = (frames - 1),
168|         sstep = +1,
169|         dstep = -1,
170|     )
171|     dispatch('cp', **props)
172| 
173| ##================================================================================================##
174| def do_fibs ():
175|     '''Fade in from black, single image.'''
176|     props = dict(
177|         #spath = path.join(AniPath, DefaultSourceDir),
178|         #dpath = path.join(AniPath, DefaultTargetDir),
179|         spath = path.join(AniPath, 'qc5'), # two-arms frames
180|         dpath = path.join(AniPath, 'qc1'),
181|         sname = 'img_0000.png',
182|         dname = DstFile,
183|         start =  0,
184|         head  = 15,
185|         count = 35,
186|         tail  = 50,
187|     )
188|     dispatch('fibs', **props)
189| 
190| ##================================================================================================##
191| def do_fobs ():
192|     '''Fade out to black, single image.'''
193|     props = dict(
194|         #spath = path.join(AniPath, DefaultSourceDir),
195|         #dpath = path.join(AniPath, DefaultTargetDir),
196|         spath = path.join(AniPath, 'qc5'), # two-arms frames
197|         dpath = path.join(AniPath, 'qc9'),
198|         sname = 'img_0375.png',
199|         dname = DstFile,
200|         start =  0,
201|         head  = 75,
202|         count = 50,
203|         tail  = 75,
204|     )
205|     dispatch('fobs', **props)
206| 
207| ##================================================================================================##
208| def do_fibm ():
209|     '''Fade in from black, multiple images.'''
210|     props = dict(
211|         spath = path.join(AniPath, DefaultSourceDir),
212|         dpath = path.join(AniPath, DefaultTargetDir),
213|         sname = SrcFile,
214|         dname = DstFile,
215|         start =  0,
216|         sinit =  0,
217|         sstep =  1,
218|         count = 42,
219|         head  =  8,
220|     )
221|     dispatch('fibm', **props)
222| 
223| ##================================================================================================##
224| def do_fobm ():
225|     '''Fade out to black, multiple images.'''
226|     props = dict(
227|         spath = path.join(AniPath, DefaultSourceDir),
228|         dpath = path.join(AniPath, DefaultTargetDir),
229|         sname = SrcFile,
230|         dname = DstFile,
231|         start =  125,
232|         sinit =  125,
233|         sstep =  1,
234|         count = 25,
235|         tail  =  5,
236|     )
237|     dispatch('fobm', **props)
238| 
239| 
240| ##================================================================================================##
241| def do_three_titles_over_one_frame (**kwargs):
242|     """Three titles *sequentially* over a single frame."""
243|     ttl1 = 'A (3D) cube can unfold into one of 11 unique (2D) nets.'
244|     ttl2 = '(Reflections and rotations don\'t count as unique.)'
245|     ttl3 = 'Note that cubes can unfold into the same net in different ways.'
246|     srcpath = path.join(AniPath, 'n1a1_0')
247|     dstpath = path.join(AniPath, DefaultTargetDir)
248|     srcname = 'img_0000.png'
249|     props = dict(
250|         title = ttl1,
251|         spath = srcpath, sname = srcname,
252|         dpath = dstpath, dname = DstFile,
253|         start = 0,
254|         count = 75,  # 75+0+0+5+15=95
255|         codes = '/fi:0/fo:0//ti:5/to:10,5/',
256|         fontname = 'corbeli.ttf',
257|         fontsize = 164,
258|         margin = 12,
259|     )
260|     props = dict(
261|         title = ttl2,
262|         spath = srcpath, sname = srcname,
263|         dpath = dstpath, dname = DstFile,
264|         start = 95,
265|         count = 225,    # 225+0+0+5+30=260 (+95=355)
266|         codes = '/fi:0/fo:0//ti:5/to:20,10/',
267|         fontname = 'corbel.ttf',
268|         fontsize = 132,
269|         margin = 12,
270|     )
271|     props = dict(
272|         title = ttl3,
273|         spath = srcpath, sname = srcname,
274|         dpath = dstpath, dname = DstFile,
275|         start = 355,
276|         count = 75,     # 75+0+0+5+60=140 (+355=495)
277|         codes = '/fi:0/fo:0//ti:5/to:50,10/',
278|         fontname = 'corbel.ttf',
279|         fontsize = 124,
280|         margin = 12,
281|     )
282|     # Title...
283|     dispatch('ttls', **props_1)
284|     dispatch('ttls', **props_2)
285|     dispatch('ttls', **props_3)
286|     Log.info()
287|     return 'Done'
288| 
289| 
290| ####################################################################################################
291| ## Production Presets
292| ####################################################################################################
293| 
294| ##================================================================================================##
295| def do_merge_moves (name='n1a1', moves=3, frames=FramesPerMove):
296|     '''Merge move folders into an animation folder.'''
297|     print('name=%s, moves=%d, frames=%d' % (name, moves, frames))
298|     for ix in range(moves):
299|         props = dict(
300|             spath = path.join(AniPath, '%sm%d' % (name,ix+1)),
301|             dpath = path.join(AniPath, '%s_2' % name),
302|             sname = SrcFile,
303|             dname = DstFile,
304|             count = frames,
305|             sinit = 0,
306|             dinit = (ix * frames),
307|             sstep = +1,
308|             dstep = +1,
309|         )
310|         dispatch('cp', **props)
311| 
312| ##================================================================================================##
313| def do_all_merges (framecount=FramesPerMove):
314|     '''Do merges for all animations.'''
315|     for dn,nm in zip(DirNames, NbrMoves):
316|         try:
317|             name = 'n%sa%s' % (*dn,)
318|             print('merge: %s' % name.upper())
319|             do_merge_moves(name=name, moves=nm, frames=framecount)
320|         except Exception as e:
321|             print('\n',e,'\n')
322|     print()
323| 
324| ##================================================================================================##
325| def do_rev_ff_ff (name, lastfile, frames):
326|     '''Create the reversed files and two freeze frame files.'''
327|     print('name=%s, frames=%d, lastfile=%s' % (name, frames, lastfile))
328|     props = dict(
329|         spath = path.join(AniPath, '%s_2' % name),
330|         dpath = path.join(AniPath, '%s_0' % name),
331|         sname = DstFile,
332|         dname = DstFile,
333|         count = frames,
334|         sinit = 0,
335|         dinit = (frames - 1),
336|         sstep = +1,
337|         dstep = -1,
338|     )
339|     dispatch('cp', **props)
340|     # Freeze frame on cube net, name, and id (125 frames)...
341|     ttl1 = 'Cube Net %d' % int(name[1], base=16)
342|     cfg1 = 'f:times/s:170/ha:0.10/va:0.02/fg:black/bg:black/pad:0/'
343|     props = dict(
344|         spath = path.join(AniPath, '%s_0' % name),
345|         dpath = path.join(AniPath, '%s_1' % name),
346|         sname = lastfile,
347|         dname = DstFile,
348|         start = 0,
349|         count = 100,
350|         codes = '/fi:0/ti:10/to:15/fo:0/',
351|         title  = '%s/:/%s' % (ttl1, cfg1),
352|     )
353|     dispatch('ttls', **props)
354|     # Freeze on last frame (20 frames)...
355|     props = dict(
356|         spath = path.join(AniPath, '%s_2' % name),
357|         dpath = path.join(AniPath, '%s_3' % name),
358|         sname = lastfile,
359|         dname = DstFile,
360|         start = 0,
361|         count = 20,
362|     )
363|     dispatch('ff', **props)
364| 
365| ##================================================================================================##
366| def do_all_reverses (framecount=FramesPerMove):
367|     '''Do reverses for all animations.'''
368|     for dn,nm in zip(DirNames, NbrMoves):
369|         try:
370|             name = 'n%sa%s' % (*dn,)
371|             frames = framecount * nm
372|             fnlast = 'img_%04d.png' % (frames-1)
373|             print('reverse: %s' % name.upper())
374|             do_rev_ff_ff(name=name, lastfile=fnlast, frames=frames)
375|         except Exception as e:
376|             print('\n',e,'\n')
377|     print()
378| 
379| ##================================================================================================##
380| def do_title_frames ():
381|     '''Create title sequence. Copy POV files and generate fade in/out files.'''
382|     srcpath = path.join(PovPath, '2')
383|     dstpath = path.join(AniPath, '_tmp')
384|     # Copy frames from POV Ani to Python Ani...
385|     print('Title-Frames: %s' % srcpath)
386|     props = dict(
387|         spath = srcpath, sname = SrcFile3,
388|         dpath = dstpath, dname = DstFile,
389|         count = 201,
390|         sinit =  0,
391|         dinit =  0,
392|         sstep = +1,
393|         dstep = +1,
394|     )
395|     dispatch('cp', **props)
396|     # Overwrite first frames with a fade in from black...
397|     print('Fade-In: %s' % srcpath)
398|     props = dict(
399|         spath = srcpath, sname = SrcFile3,
400|         dpath = dstpath, dname = DstFile,
401|         start =  0,
402|         count = 25,
403|         sinit =  0,
404|         sstep = +1,
405|     )
406|     dispatch('fibm', **props)
407|     # Overwrite last frames with a fade out to black...
408|     print('Fade-Out: %s' % srcpath)
409|     props = dict(
410|         spath = srcpath, sname = SrcFile3,
411|         dpath = dstpath, dname = DstFile,
412|         start = 150,
413|         count =  51,
414|         sinit = 150,
415|         sstep =  +1,
416|     )
417|     dispatch('fobm', **props)
418|     print()
419| 
420| ##================================================================================================##
421| def do_title_sequence ():
422|     '''Create a set of title frames. [head:25/ti:25/ttl:100/to:25/tail:25]'''
423|     ttl1 = 'Cube Unfoldings:'
424|     ttl2 = 'In Dynamic Moving Color!'
425|     cfg1 = 'f:times/s:170/ha:0.10/va:0.02/fg:black/bg:black/pad:0/'
426|     cfg2 = 'f:times,i/s:80/ha:0.5/va:0.80/fg:black/bg:black/pad:0/'
427|     # Main title sequence...
428|     print('Title-Sequence: %s' % ttl1)
429|     props = dict(
430|         spath = path.join(AniPath, '_tmp'),
431|         dpath = path.join(AniPath, '0'),
432|         sname = DstFile,
433|         dname = DstFile,
434|         start =   0,
435|         count = 100,
436|         sinit =   0,
437|         dinit =   0,
438|         sstep =  +1,
439|         codes = '/ti:25,25/to:25,25/',
440|         title  = '%s/:/%s' % (ttl1, cfg1),
441|         title2 = '%s/:/%s' % (ttl2, cfg2),
442|     )
443|     dispatch('ttlm', **props)
444|     print()
445| 
446| ##================================================================================================##
447| def do_intro_sequence_1 ():
448|     '''Create a set of title frames (400f).'''
449|     ttl1 = 'Cube Net:'
450|     ttl2 = 'unfolding a 3D cube into a 2D "net"...'
451|     ttl3 = 'There are (only!) eleven||unique 2D nets of a cube.'
452|     cfg1 = 'f:times/s:110/xy:30,0/fg:black/bg:black/pad:0/'
453|     cfg2 = 'f:times,i/s:70/xy:500,34/fg:black/bg:black/pad:0/'
454|     cfg3 = 'f:times/s:50/xy:70,420/a:left/fg:black/bg:black/pad:0/'
455|     # Introduction title card...
456|     print('Intro-Sequence: %s' % ttl1)
457|     props = dict(
458|         #spath = r"C:\CJS\prj\pov\ws\cube\out",
459|         spath = path.join(AniPath, '_res'),
460|         dpath = path.join(AniPath, '1'),
461|         sname = 'cubenets-intro-1.png',
462|         dname = DstFile,
463|         start = 0,
464|         count = 300,
465|         codes = '/fi:15/ti:10/to:50/wo:25/',
466|         title  = '%s/:/%s' % (ttl1, cfg1),
467|         title2 = '%s/:/%s' % (ttl2, cfg2),
468|         title3 = '%s/:/%s' % (ttl3, cfg3),
469|     )
470|     dispatch('ttls', **props)
471|     print()
472| 
473| ##================================================================================================##
474| def do_intro_sequence_2 ():
475|     '''Create a set of title frames (400f).'''
476|     ttl1 = 'Rotations & reflections don\'t count; all these are the same:'
477|     ttl2 = 'Rotations:'
478|     ttl3 = '& their||Reflections:'
479|     cfg1 = 'f:times,i/s:72/xy:30,30/a:left/fg:black/bg:black/pad:0/'
480|     cfg2 = 'f:times/s:70/xy:30,300/a:left/fg:black/bg:black/pad:0/'
481|     cfg3 = 'f:times/s:70/xy:30,690/a:left/fg:black/bg:black/pad:0/'
482|     # Introduction title card...
483|     print('Intro-Sequence: %s' % ttl1)
484|     props = dict(
485|         spath = r"C:\CJS\prj\pov\ws\cube\out",
486|         dpath = path.join(AniPath, '2'),
487|         sname = 'cubenets-intro-2.png',
488|         dname = DstFile,
489|         start = 0,
490|         count = 300,
491|         codes = '/wi:15/ti:10/to:50/fo:25/',
492|         title  = '%s/:/%s' % (ttl1, cfg1),
493|         title2 = '%s/:/%s' % (ttl2, cfg2),
494|         title3 = '%s/:/%s' % (ttl3, cfg3),
495|     )
496|     dispatch('ttls', **props)
497|     print()
498| 
499| ##================================================================================================##
500| def do_main_sequence_fade_in_out ():
501|     '''Fade in to start the sequence (50f); fade out at the end (50f).'''
502|     head_src = path.join(AniPath, 'n1a1_0')
503|     head_dst = path.join(AniPath, 'n0')
504|     tail_src = path.join(AniPath, 'nba1_3')
505|     tail_dst = path.join(AniPath, 'nz')
506|     # Fade in...
507|     print('Main-Fade-In: %s' % head_src)
508|     props = dict(
509|         spath = head_src,
510|         dpath = head_dst,
511|         sname = 'img_0000.png',
512|         dname = DstFile,
513|         start =  0,
514|         head  =  5,
515|         count = 40,
516|         tail  =  5,
517|     )
518|     dispatch('fibs', **props)
519|     # Fade out...
520|     print('Main-Fade-out: %s' % tail_src)
521|     props = dict(
522|         spath = tail_src,
523|         dpath = tail_dst,
524|         sname = 'img_0019.png',
525|         dname = DstFile,
526|         start =  0,
527|         head  =  5,
528|         count = 40,
529|         tail  =  5,
530|     )
531|     dispatch('fobs', **props)
532|     print()
533| 
534| ##================================================================================================##
535| def do_end_title ():
536|     '''That's all for now!'''
537|     ttl1 = 'That\'s all for now.'
538|     ttl2 = 'See you next time...'
539|     cfg1 = 'f:times///s:170/ha:0.5/va:0.45/fg:white/bg:black/pad:10/'
540|     cfg2 = 'f:times,i/s:75//ha:0.5/va:0.62/fg:white/bg:black/pad:10/'
541|     # Final title card...
542|     print('Extro-Sequence: %s' % ttl1)
543|     props = dict(
544|         spath = path.join(AniPath, '_res'),
545|         dpath = path.join(AniPath, '9'),
546|         sname = 'black_hd.png',
547|         dname = DstFile,
548|         start = 0,
549|         count = 125,
550|         codes = '/fi:0,10/ti:15/to:75/fo:0,25/',
551|         title  = '%s/:/%s' % (ttl1, cfg1),
552|         title2 = '%s/:/%s' % (ttl2, cfg2),
553|     )
554|     dispatch('ttls', **props)
555|     print()
556| 
557| 
558| ##================================================================================================##
559| def clear_all ():
560|     do_rmdirs(make_dirs_only=False)
561|     do_mkdirs(make_dirs_only=False)
562| 
563| ##================================================================================================##
564| def clear ():
565|     do_rmdirs()
566|     do_mkdirs()
567| 
568| ##================================================================================================##
569| def do_all ():
570|     do_all_merges()
571|     do_all_reverses()
572|     do_title_frames()
573|     do_title_sequence()
574|     do_intro_sequence_1()
575|     do_intro_sequence_2()
576|     do_main_sequence_fade_in_out()
577|     do_end_title()
578| 
579| ##================================================================================================##
580| def main (*args):
581|     #clear_all()
582|     #clear()
583|     do_all()
584| 
585| ####################################################################################################
586| if __name__ == "__main__":
587|     print('autorun: %s' % argv[0])
588|     rv = main(*argv[1:])
589|     print()
590| ####################################################################################################
591| '''eof'''
592|