Scripting with Tcl for computer graphics animation

Page by Theo Verelst

The type of language Tcl is obviously most renowned for is a 'scripting language,' which calls for a comparison with a movie script. Of course that is no coincidence, so let's start small but good with a simple script to govern a computer graphics animation.

Without intent to affiliate with NVidia in this space, recently their Gelato language [L1 ] has been made freely available to run high quality rendering on computers with a recent graphics card of theirs (I think 6200+, though that cheap one is not very well compatible), which comes with a python and C interface.

Being used to Tcl since at least '94 or so, and not liking python very much, I thought I'd try the possibilities and penalties (calling of the interpreter and running a program all the time) of using Tcl for certain rendering calls, so I made a very small starting script to rotate the famous teapot in a Gelato-rendered scene by a small Tcl script run under tclsh from a Cygwin prompt:

 set d [open head.pyg r]
 set t [read $d]
 close $d

 for {set i 0} {$i<50} {incr i 2} {
   set f [open rn.pyg w]
   puts $f $t
   puts $f "Rotate ( $i, 0,0,1)"

   puts $f "Rotate (  0, 0, 0, 1 )"
   puts $f "Input (\"teapot.pyg\")"
   puts $f "PopAttributes ()"
   puts $f ""

   puts $f "Render (\"camera\")";
   puts $f ""
   close $f
   catch {exec gelato -iv -o im$i.tiff rn.pyg}
   puts "finished i=$i";

That's 25 frames, which can be made into an animation (can be added to the tcl script) by using ffmpeg (I used 'ffmpeg -s 1920x1080 -i imageout%d.jpg -r 25 -s 1920x1080 -b 50000 -f mpeg2video -y ani.mpg', and played it back using (heavy) HW accelerated PureVideo on a 1080 HD compatible screen).

Image TV Wiki imageout11s.jpg

head.pyg contains mostly after the 'layered shader' example the graphics scene in gelato-python. the loops calls gelato in a loop from the tcl interpreter, which works ok; the progress bar is visible.

The resulting 25 TIFF files (they can be made HDR too, for those who like that) were converted with this tcl script (under cygwin):

   for {set i 0}  {$i < 50} {incr i 2} {
      exec tifftopnm T/im$i.tiff 2&>/dev/null |  cjpeg  -q 95 -outfile imageout[format "%d"; [expr int($i/2)]].jpg

In this case I used a 1920x1080 (HD) resolution, which made this 1 second 'animation' rendered in about 30 secs IIRC. Quite doable. Oh, I added the envmap on the teapot myself; let me see if I will get requests to put all the files together.

Perfect TCL use!

TV (May 1 '08) Recently I did a similar 'trick,' but then with the caustic example from the most recent Gelato distribution, where I changed the light direction and rotated the whole scene various directions and then made a short HD movie of the resulting frames, transcoding them over h246 (for motion estimation), one of the scripts I used was:

 set d [open gather_h1a.pyg r]
 set th2a [read $d]
 close $d

 set d [open gather_h1b.pyg r]
 set th2b [read $d]
 close $d

 set d [open gather_h2.pyg r]
 set th2 [read $d]
 close $d

 set d [open gather_b.pyg r]
 set t2 [read $d]
 close $d

 set d [open shoot_h.pyg r]
 set u [read $d]
 close $d

 set d [open shoot_b.pyg r]
 set u2 [read $d]
 close $d

 set j 111

   set f [open rn2.pyg w]
   puts $f $u
 #   puts $f "Rotate ( [expr 130+$i], 1, 0, 0 )"
   puts $f "Rotate ( [expr 170], 1, 0, 0 )"
   puts $f $u2
   close $f
   if { $j == 0 } { catch "exec gelato  rn2.pyg" }
   puts -nonewline "finished shooting j=$j   ";
   flush stdout

 for { set i 111 } { $i<200 } { incr i 1 } {
   set f [open rn.pyg w]
   puts $f $th2a
   puts $f "Rotate ( [expr $i/2.0], 0, 0, 1 )"
   puts $f $th2b
   puts $f "Output (\"im[ format "%03d" $j].tiff\", \"tiff\", \"rgb\", \"perspShape\" )"
   incr j
   puts $f $th2
 #   puts $f "Rotate ( [expr 130+$i], 1, 0, 0 )"
   puts $f "Rotate ( [expr 170], 1, 0, 0 )"
   puts $f $t2
   close $f
 #   catch "exec gelato -iv -ivpage 1 rn.pyg"
   catch "exec gelato  rn.pyg"
   puts "finished i=$i";
   flush stdout

The header files contain the relevant parts of the original Gelato graphics file, which of course is a bit clumsy, but it works fine....

I didn't make a tcl script to define parameter progressions for the whole animation, which may be a good idea.

The result is a 1920x1080 25fps mp4 file, to which I added an audio track with Cinelerra (Open Source Video Edit software), and in fact I could put the rest of the production process in a tcl script with the right ffmpeg calls, but didn't yet. Nicer would be a good Tk UI with parameter settings, and menus and explanations.

The above script starts rendering at frame 111 from the 200, so is a partial render. That is because for some reason the computer I used to make all these big frames with (a power soaking 3GHz Pentium D with a bulky NVidia graphics card) had a problem I think with the monitor power off, and in fact I have suspected the combined tcl/gelato memory householding under fedora 8/64, because it had frozen at frame 110, which was probably after about an hour, I don't recall.

A frame from a down-converted 720 movie file:

Image TV Diary geltrac4_720_1.jpg

The movies resulting from the script:      ( 84   MB , 1920x1080 , 25 fps , mpeg4 video/sound )  (  8.4 MB , 1280x720 , downconverted)

If somebody is interested here is a jpg converted original frame from the sequence ( ).

TV June 2008 NVidia has in their wisdom decided to make Gelato Pro freely downloadable, which is great to play with like on this page.

TV (Dec 1 ' 08) I've done a new animation with a bit different tcl main animation script approach, main and utility scripts and examples frames and short animation movie segments are here [L2 ] . Impression a a resulting frame:

Image TV Diary tfj030bm.jpg

TV (Dec 10 '08) I think this approach would also work great for [L3 ].

See also DataViewer (? This a demo of Final Film Frame Rendering, not 3D data viewing)