Quick Reference I

return to main index



puts "hello world"

Output the contents of the quoted string to the terminal.

Setting Variables

set a 12
set b $a
set aussie "goodday"

a is assigned the value 12.
b is assigned the same value as "a".
aussie is assigned the string "goodday".

Arithmetic Using the expr Command

set a 12
set b 2
set c [expr $a + $b]
puts "a is $a, b is $b, c is $c"
set c [expr $a/$b]
puts "a is $a, b is $b, c is $c"

a is assigned the value 12. b is assigned the value 2. Add a and b using the expr command and assign to c. Display the values of a, b and c. Divide a by b using the expr command and assign to c. Again display the values of a, b and c

if ifelse and else

if {$a == 2} {
   puts "a is 2"
} elseif {$a > 3} {
   puts "a is larger than 3" 
} else {
   puts "a is smaller than 2" 

Unlike the 'C' language, "if", "elseif" and "else" are commands. The formatting of the script is very important. The curly braces shown in blue must be on the same line as their corresponding "if", "elseif" and "else". Note that "elseif" is one word not two!

Looping - while

set a 5
while {$a <= 10} {
   puts "a is $a" 
   incr a 1

Unlike the 'C' language, "while" is a command not a statement. Therefore, be careful to put the curly braces exactly as shown. The "incr" command increments the variable a by 1.

Looping - for

for {set a 0} {$a <= 10} {incr a 1} { 
   puts "a is $a"

Unlike the 'C' language, "for" is a command not a statement. Therefore, be careful to put the curly braces exactly as shown. The "for" command takes three inputs contained in the curly braces. Also, note the spaces between the braces.

Looping - foreach

set files [glob "H:/renderman" *.rib]
puts "Found these rib files:" 
foreach i $files {
   puts "$i"

files assigned a list returned by the glob command. Display a simple message using the puts command. Step through the list and for each item, display its name.


set x 4
switch $x {
   0 { puts "match on 0" } 
   1 { puts "match on 1" } 
   2 { puts "match on 2" } 
   default { puts "no match" } 

Assign x the value 4.
Attempt to match the value of x to the following,
No match, then do this default action.

Setting a List

set L1 [list a b [list 1 2 3 ] c d ]
puts "the raw list is:\n$L1"
set L2 { a b { 1 2 3 } c d}
puts "the raw list is:\n$L2"
set combined [list $L1 $L2] 

Use the list command to create a list. Print the entire list to the console. Use of the block substitution ie. curly brackets, to create a list. Note that the 'a' in the second list prints with a leading white space.

Combining two lists.

Items in a List

foreach item $L1 {
   puts $item
puts [llength $L1]

Use the foreach command to step through a list. The variable "item" (its name is not important) will be assigned the value of each item in the list. A variety of list related commands can be used on lists - such as list length shown here.


proc tween {k1 k2 frame v1 v2 rate} {
if {$frame < $k1} {
   set output $v1
} elseif {$frame > $k2} {
   set output $v2
} else {
   set per double([expr $frame - $k1])
   set per [expr $per / ($k2 - $k1)]
   set per [expr pow($per, $rate)]
   set result [expr ($v2-$v1)*$per+$v1]
return $result

An example procedure - it calculates inbetween values for animation.

Refer to inbetweening

The next box shows this proc being tested.

Calling a Proc

for {set f 0} {$f <= 50} {incr f} { 
   set value [tween 10 40 $f 1.5 4.5 1]
   puts "$f: $value"

The code must be in the same script as the proc shown above.

Arrays - Iteration

# define an array called "color"
# consisting of three elements
set color(red) 0.5
set color(green) 0.1
set color(blue) 0.3
# confirm the size of the array
set size [array size color]
puts "Array size is $size\n"
# reference the beginning of the array
set start [array startsearch color]
# get each element and use
# it as an index into the array
for {set n 0} {$n < $size} {incr n} {
   set value [array nextelement color $start]
   puts "$value = $color($value)"

Splitting (tokenizing) a String

proc splitMayaPath { mayapath } {
    set items [split $mayapath |]
    return [lindex $items 1]
set dagPath "|pSphere1|pSphereShape1"
puts [splitMayaPath $dagPath]

This example of string splitting returns the second item in an input string which, in this example, consists of the name of an object in a Maya scene. A Maya object name is derived from Maya scene graph path - hence the vertical bars.
output >> pSphereShape1

Extracting an Integer from a String

proc extractInt { str } {
    regsub -all {[^0-9]} $str "" out
    return out
puts [extractInt "nurbsSphereShape8"]

The TCL command regsub (regular expression substitution) is setup to copy ALL digits from an input string to an output string. Notice the input consists of a single item. Try this code sample using this input.


If you wish to extract either the first or the last number you will have to modify this sample code. That is left as an exercise.
output >> 8

Using eval

set trig1 "sin(1.5) * cos(2) + 1.5"
set trig2 "sin(1) * cos(2.3)"
set op +
puts [eval expr $trig1 $op $trig2]

This is an example of using the eval procedure to evaluate some (arbitary) maths operations.

© 2002- Malcolm Kesson. All rights reserved.