Tcl
Overview


return to main index



Introduction

The best source of information about Tcl - its written by the author of the language - is,

Tcl and the Tk Toolkit
John K. Ousterhout
Addison-Wesley
ISBN 020163337X

Another source of information is the Tcl Cookbook. It appears to be a very thorough online guide to Tcl.

The tutorials and sample code that I have prepared assume, in general, the reader is using a Linux based computer or a Macintosh OSX computer. I have not found any significant issues when using Tcl on Windows but be aware that paths to directories and files should be specified with UNIX style forward slashes ie.

    C:/some_directory/another_directory/myfile.tcl

rather than, as one might expect using back slashes

    C:\some_directory\another_directory\myfile.tcl

Why Use Tcl?

For artists who wish to make full use of RenderMan and Maya ie. Maya + mtor + prman, it is very beneficial to know how to write Tcl scripts. For 'better or for worse' Pixar choose Tcl as the scripting interface via which artists can enhance their productivity when using the mtor plugin.

As a cross platform scripting language its also very useful to be able to write relatively simple scripts to perform tasks such as,

  • batch renaming of specific files in a directory
  • batch reordering of sequentially numbered files, such as images
  • batch modification of the contents of text files, such as RIB files
  • generating a batch file that will render all the RIBs in a directory

In addition to using Tcl as a way of writing command line scripts there are the additional benefits of knowing a language that has an integrated, cross platform, GUI builder ie. Tk.

If you have ever had to move your working environment from one operating system to another and as a consequence have had to re-develope your custom tools the words cross platform will have a very special significance to you!


Basic Concepts

As already noted, the best source of instruction on Tcl is Tcl and the Tk Toolkit. However, the following comments that may help clarify the use of Tcl.

  • everything is a command - even statements like if-else, for and while are commands
  • Tcl scripts consists of strings
  • there are no explicit data types such as int and float

When a Tcl script is executed its text is chopped up, and often the "pieces" are re-chopped, into items that become the inputs to commands that perform the task or tasks defined by the script.

The initial chopping up ie. parsing, of the script is done by an item of Tcl software called the Tcl Parser. Its sole purpose is to divide the script into sub-units of text that can then be handed off to the commands that eventually produce the final output of the script. The parser does not care about the contents of the script.

When dividing a script into sub-units the parser performs what is called text substitution to ensure that blocks of text are handled in a consistent way. For example, compare the MEL way of declaring and assigning a value to a variable to the Tcl way of performing the same operation.


Mel

Tcl

 int $age = 54;
 set age 54

The Tcl parser chops up the text into three words,

    set
    age
    54

What appears to be a number is in fact a string ie. the numeral '5' followed by the numeral '4'. The first word is the name of a command. The Tcl system "looks up" the command in its library and passes onto the command the inputs "age" and "54". At which point our variable "age" assumes the numeric value of 54. As its name suggests, the purpose of the set command is to assign a value.

Another example,


Mel

Tcl

 int $age = 54;
 int $time = $age + 10;
 set age 54
 set time [expr $age + 10]

The square brackets have nothing in common with MEL style arrays but instead tell the Tcl parser to process the text within the brackets before doing anything else.

Therefore, the parser chops up that text into,

    expr
    $age
    +
    10

Again, the first word is the name of a Tcl command. Unlike the set command, the expr command requires 3 inputs. The dollar preceeding the word "age" ensures the actual numeric value is substituted for the name of the variable. The dollar symbol tells the parser to perform variable substitution.

The expr command does the arithmetic and returns the text "64". In effect the original square brackets are substituted by the value returned from the expr command. Square brackets tell the parser to perform command substitution.

In addition to $ and [ ] the other symbols that tell the Tcl parser to do something significant are,

    \
    ""
    { }

Refer to "Tcl and the Tk Toolkit" and/or the Tcl Cookbook for more information.




© 2002- Malcolm Kesson. All rights reserved.