Procedural Primitive Tcl

return to main index

Related Links
   helper app basics
   basic randomness
   ripoints on a disk
   riblobby basics

For an overview of helper apps refer to helper app basics. The code shown in listing 1 provides a framework for a helper app written in TCL

TCL Listing 1

fconfigure stdout -translation binary
while { [gets stdin buffer] != -1 } {
    set param0 [lindex $buffer 0]
    # uncomment to show the number of pixels
    # puts stderr "pixel coverage is $param0"
    # set param1 [lindex $buffer 1]
    # set param2 [lindex $buffer 2]

    puts "TransformBegin"
        # other rib goes here
    puts "TransformEnd"
    puts "\377" 
    flush stdout

The first line is not required if you are working on Windows. It will, however, be necessary to set the output encoding if you are using Linux or OSX.

The sample script is short because it doesn't do anything useful. The script must be extended so that it responds to data sent by the renderer. For example, suppose a RIB file calls this script in this way,

# linux and osx
Procedural "RunProgram" ["/usr/bin/tclsh  /Users/mk/helper.tcl" ""] 
                        [-1 1 -1 1 -1 1]
# windows
Procedural "RunProgram" ["tclsh  G:/mk/helper.tcl" ""] 
                        [-1 1 -1 1 -1 1]

In particular notice the empty string - shown in red. Data to be sent to the helper script would be written within the quotations. On MacOSX and, probably, Linux as well, its necessary to specifiy the full path to tclsh (shown in bold). On windows the path can be omitted. Although, in this example, no additional data is passed to the helper the renderer itself will always provide a value that indicates the number of pixels "covered" by the bounding box specified by the numbers shown in blue.

For each item of data going from the RIB file to the helper script its necessary to use to extract the data from the "buffer". A couple of sample lines of code are shown in comments. What the script does with this information is entirely dependent on the functionality built into the helper.

The generation of output RIB statements is best accomplished with TCL procedures that are either implemented in the same file or in one or more auxillary TCL files. As an example of an auxillary script consider listing 2.

TCL Listing 2

proc rand { min max } {
    return [expr rand() * ($max - $min) + $min]
proc point { x y z } {
    puts "Points \"P\" \[$x $y $z\] "
    puts "\"constantwidth\" \[0.1\]\n"
proc points { pnts } {
    puts "Points \"P\" \[$pnts\]"
    puts "\"constantwidth\" \[0.1\]\n"
proc curve { pnts type widths } {
    set num [expr [llength $pnts]/3]
    puts "Basis \"b-spline\" 1 \"b-spline\" 1\n"
    puts "Curves \"$type\" \[$num\] \"nonperiodic\""
    puts "\"P\" \[$pnts\]"
    puts "\"width\" \[$widths\]"
    # or output a constant curve width eg.
    # puts "\"constantwidth\" \[0.04\]"

To use these procedures in the main script it is necessary, at the head of the file, to source the auxillary script. For example, if this script, called myproc.tcl, is on the G drive, the source statement would look like this...

source G:/myproc.tcl 
fconfigure stdout -translation binary

Refer to these docs for examples of implementing a TCL helper script and a simple RIB file that calls the script.


© 2005 Malcolm Kesson. All rights reserved.