Tcl
Align Y-Axis to a Vector


return to main index



Introduction

Occasionally it is necessary to apply a series of rotations to a coordinate system so that one of its axes points in a specific direction. Another tutorial "Mel: Align Y-Axis to Vector" explains the principle on which the Tcl script given in this tutorial is based.


Listing 1


proc aimY {vec} {
    set x [lindex $vec 0]
    set y [lindex $vec 1]
    set z [lindex $vec 2]
    set xyLength [expr sqrt(($x * $x) + ($y * $y))]
    set vecLength [expr sqrt(($x * $x) + ($y * $y) + ($z * $z))]
  
    if {$xyLength == 0} {
        if {$x > 0} {
            set zAngle [expr 90.0 * (3.14159/180)]
        } else {
            set zAngle [expr -90.0 * (3.14159/180)]
            }
    } else {
        set zAngle [expr acos($y/$xyLength)]
        }
    set xAngle [expr acos($xyLength/$vecLength)]
    if {$z > 0}  { 
        set xAngle $xAngle
    } else {
        set xAngle -$xAngle
        }
    if {$x > 0} { 
        set zAngle -$zAngle
    } else {
        set zAngle $zAngle
        }
    set out [list [expr $xAngle / (3.14159/180)] [expr $zAngle / (3.14159/180)]]
    return $out
    }

Useage

Pixar's RenderMan Artist Tools (RAT) supported the use of a so-called Ribbox. They enabled Tcl scripts to be executed when RAT's mtor plugin for Maya wrote the geometry for an object into a rib file. Although RAT has been replaced by Pixar's newer product, RenderMan Studio, the following notes, intended for use with RAT, may still be of use in other contexts. To align an object, such as a quadric cylinder, to a vector (x = -2, y = 2, z =1), the aimY proc would be used as follows.

    [
    source "PATH_TO_THE_AIMY_SCRIPT"
    set xz_angles [aimY -2 2 1]
    set rib    "Rotate [lindex $xz_angles 0] 1 0 0\n"
    append rib "Rotate [lindex $xz_angles 1] 0 0 1\n"
    append rib "Rotate -90 1 0 0\n"
    append rib "Cylinder 0.5 0 2 360\n"
    return $rib
    ]

The rotation of -90 degrees around the z-axis is required because RenderMan quadric surfaces, such as a cylinder, are aligned to the z-axis not, as would be the case with a Maya cylinder, the y-axis. The rotation, in effect, turns the cylinder upright after which the x angle and the z angle rotations align the cylinder to the specified vector.




© 2002- Malcolm Kesson. All rights reserved.