ani.html


001| '''Animation Utilities.
001| 
001| Functions:
001|     do_solid        -create a set of solid-color frames
001|     do_freeze       -create a set of freeze frames
001| 
001|     do_fibs         -fade in from black, single frame
001|     do_fobs         -fade out to black, single frame
001|     do_fibm         -fade in from black, multiple frames
001|     do_fobm         -fade out to black, multiple frames
001| 
001|     do_reverse      -reverse a set of frames
001|     do_merge        -merge multiple folders into one
001| 
001|     gen_dirnames    -[generator] list of animation directories
001|     do_mkdirs       -create all animation directories (skips existing ones)
001|     do_rmdirs       -remove all animation directories (and files!)
001|     recycle         -do both mkdirs() and rmdirs()
001| 
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| ####################################################################################################
009| BasePath = r'C:\CJS\prj\Python\ws\rotation\ani'
010| PovPath = r'C:\CJS\prj\pov\ws\xyz\ani'
011| DstFile = 'img_%04d.png'
012| 
013| ##================================================================================================##
014| DirNames = ['_tmp','0','1','2','3','9', 'a', 'b', 'c']
015| 
016| DefaultSourceDir = '0'
017| DefaultTargetDir = '_tmp'
018| 
019| FramesPerFolder = 125
020| 
021| ##================================================================================================##
022| def gen_dirnames (make_dirs_only=True):
023|     '''[generator] List of animation directories.'''
024|     for ix,dname in enumerate(DirNames):
025|         yield path.join(BasePath, dname)
026|         if make_dirs_only and (5 <= ix):
027|             break
028|         yield path.join(BasePath, dname)
029| 
030| ##================================================================================================##
031| def do_mkdirs (make_dirs_only=True):
032|     '''Create all animation directories. (Skips existing ones)'''
033|     from os import mkdir
034| 
035|     for dirname in gen_dirnames(make_dirs_only=make_dirs_only):
036|         if path.exists(dirname):
037|             print('Directory already exists: %s' % dirname)
038| 
039|         else:
040|             mkdir(dirname)
041|             print('Created: %s' % dirname)
042|     print()
043| 
044| ##================================================================================================##
045| def do_rmdirs (make_dirs_only=True):
046|     '''Remove all animation directories. (And files!)'''
047|     from os import rmdir, remove, listdir
048| 
049|     for dirname in gen_dirnames(make_dirs_only=make_dirs_only):
050|         if path.exists(dirname):
051| 
052|             # If any files exist, remove them...
053|             fns = listdir(dirname)
054|             for fn in fns:
055|                 ffn = path.join(dirname, fn)
056|                 remove(ffn)
057|             print('Files Deleted: %s' % len(fns))
058| 
059|             # Directory is empty now, remove it...
060|             rmdir(dirname)
061|             print('Removed: %s' % dirname)
062| 
063|         else:
064|             print('Directory doesn\'t exist: %s' % dirname)
065|     print()
066| 
067|     # Clean other directories...
068|     for dn in ['0','1','2','3','9','n0','nz','_tmp']:
069|         dirname = path.join(BasePath, dn)
070|         print('Cleaning: %s' % dirname)
071|         fns = listdir(dirname)
072|         for fn in fns:
073|             ffn = path.join(dirname, fn)
074|             remove(ffn)
075|         print('Files Deleted: %s' % len(fns))
076|     print()
077| 
078| ##================================================================================================##
079| def do_recycle (make_dirs_only=True):
080|     do_rmdirs(make_dirs_only=make_dirs_only)
081|     do_mkdirs(make_dirs_only=make_dirs_only)
082| 
083| 
084| ##================================================================================================##
085| def do_solid (target=DefaultTargetDir, frames=25):
086|     '''Create a set of solid-color frames.'''
087|     props = dict(
088|         dpath = path.join(BasePath, target),
089|         dname = DstFile,
090|         start = 0,
091|         count = frames,
092|         color = 'black',
093|     )
094|     dispatch('_f', **props)
095| 
096| ##================================================================================================##
097| def do_freeze (source=DefaultSourceDir, target=DefaultTargetDir, frames=25):
098|     '''Create a set of freeze frames.'''
099|     props = dict(
100|         spath = path.join(BasePath, source),
101|         dpath = path.join(BasePath, target),
102|         sname = 'img_1234.png',
103|         dname = DstFile,
104|         start = 0,
105|         count = frames,
106|     )
107|     dispatch('ff', **props)
108| 
109| ##================================================================================================##
110| def do_fibs (source=DefaultSourceDir, target=DefaultTargetDir):
111|     '''Fade in from black, single image.'''
112|     props = dict(
113|         spath = path.join(BasePath, source),
114|         dpath = path.join(BasePath, target),
115|         sname = 'img_0000.png',
116|         dname = DstFile,
117|         start =  0,
118|         head  =  5,
119|         count = 40,
120|         tail  =  5,
121|     )
122|     dispatch('fibs', **props)
123| 
124| ##================================================================================================##
125| def do_fobs (source=DefaultSourceDir, target=DefaultTargetDir):
126|     '''Fade out to black, single image.'''
127|     props = dict(
128|         spath = path.join(BasePath, source),
129|         dpath = path.join(BasePath, target),
130|         sname = 'img_0999.png',
131|         dname = DstFile,
132|         start =  0,
133|         head  =  5,
134|         count = 40,
135|         tail  =  5,
136|     )
137|     dispatch('fobs', **props)
138| 
139| ##================================================================================================##
140| def do_fibm (source=DefaultSourceDir, target=DefaultTargetDir):
141|     '''Fade in from black, multiple images.'''
142|     props = dict(
143|         spath = path.join(BasePath, source),
144|         dpath = path.join(BasePath, target),
145|         sname = SrcFile,
146|         dname = DstFile,
147|         start =  0,
148|         sinit =  0,
149|         sstep =  1,
150|         count = 42,
151|         head  =  8,
152|     )
153|     dispatch('fibm', **props)
154| 
155| ##================================================================================================##
156| def do_fobm (source=DefaultSourceDir, target=DefaultTargetDir):
157|     '''Fade out to black, multiple images.'''
158|     props = dict(
159|         spath = path.join(BasePath, source),
160|         dpath = path.join(BasePath, target),
161|         sname = SrcFile,
162|         dname = DstFile,
163|         start =  0,
164|         sinit =  0,
165|         sstep =  1,
166|         count = 42,
167|         tail  =  8,
168|     )
169|     dispatch('fobm', **props)
170| 
171| 
172| ##================================================================================================##
173| def do_reverse (source=DefaultSourceDir, target=DefaultTargetDir, frames=FramesPerFolder):
174|     '''Create a set of reversed frames.'''
175|     props = dict(
176|         spath = path.join(BasePath, source),
177|         dpath = path.join(BasePath, target),
178|         sname = DstFile,
179|         dname = DstFile,
180|         count = frames,
181|         sinit = 0,
182|         dinit = (frames - 1),
183|         sstep = +1,
184|         dstep = -1,
185|     )
186|     dispatch('cp', **props)
187| 
188| ##================================================================================================##
189| def do_merge (folders, target=DefaultTargetDir, frames=FramesPerFolder):
190|     '''Merge multiple folders into one folder.'''
191|     for folder in range(folders):
192|         props = dict(
193|             spath = path.join(BasePath, folder),
194|             dpath = path.join(BasePath, target),
195|             sname = SrcFile,
196|             dname = DstFile,
197|             count = frames,
198|             sinit = 0,
199|             dinit = (ix * frames),
200|             sstep = +1,
201|             dstep = +1,
202|         )
203|         dispatch('cp', **props)
204| 
205| 
206| ##================================================================================================##
207| def do_three_titles_over_one_frame (source=DefaultSourceDir, target=DefaultTargetDir):
208|     """Three titles *sequentially* over a single frame."""
209|     srcpath = path.join(BasePath, source)
210|     dstpath = path.join(BasePath, target)
211|     srcname = 'img_0000.png'
212|     ttl1 = 'Title 1'
213|     ttl2 = 'Title 2'
214|     ttl3 = 'Title 3'
215|     props = dict(
216|         title = ttl1,
217|         spath = srcpath, sname = srcname,
218|         dpath = dstpath, dname = DstFile,
219|         start = 0,
220|         count = 75,  # 75+0+0+5+15=95
221|         codes = '/fi:0/fo:0//ti:5/to:10,5/',
222|         fontname = 'corbeli.ttf',
223|         fontsize = 164,
224|         margin = 12,
225|     )
226|     props = dict(
227|         title = ttl2,
228|         spath = srcpath, sname = srcname,
229|         dpath = dstpath, dname = DstFile,
230|         start = 95,
231|         count = 225,    # 225+0+0+5+30=260 (+95=355)
232|         codes = '/fi:0/fo:0//ti:5/to:20,10/',
233|         fontname = 'corbel.ttf',
234|         fontsize = 132,
235|         margin = 12,
236|     )
237|     props = dict(
238|         title = ttl3,
239|         spath = srcpath, sname = srcname,
240|         dpath = dstpath, dname = DstFile,
241|         start = 355,
242|         count = 75,     # 75+0+0+5+60=140 (+355=495)
243|         codes = '/fi:0/fo:0//ti:5/to:50,10/',
244|         fontname = 'corbel.ttf',
245|         fontsize = 124,
246|         margin = 12,
247|     )
248|     # Title...
249|     dispatch('ttls', **props_1)
250|     dispatch('ttls', **props_2)
251|     dispatch('ttls', **props_3)
252| 
253| 
254| 
255| ####################################################################################################
256| print()
257| ####################################################################################################
258| '''eof'''
259|