#Author: Malcolm Kesson (2007)
string $stack[];
int    $index = 0;
  
//===============================================
// addCurveTo
//===============================================
// Attaches a curve to the input group "node" then
// creates an empty group and makes it a child of
// the curve (transformation) node. So that the
// next curve will "grow" from the end of the current
// curve the local coordinate system is moved up
// one unit.
global proc string addCurveTo(string $node)
    {
    $shape = `curve -d 1 -p 0 0 0 -p 0 1 0`;
    parent -r $shape $node;  
    $child = `group -em`;    
    parent -r $child $shape; 
    move -os 0 1 0 $child;   
    return $child;            
    }
global proc string addConeTo(string $node)
    {
    $shape = `cone -ax 0 1 0 -r 1 -hr 3`;
    parent -r $shape[0] $node;  
    $child = `group -em`;    
    parent -r $child $shape[0]; 
    move -os 0 3 0 $child;   
    return $child;            
    }
global proc string addShapeTo(string $node, string $shapeCmd, 
                              float $premove, float $postmove)
    {
    $shape = eval($shapeCmd);
    move -os 0 $premove 0;
    parent -r $shape[0] $node;  
    $child = `group -em`;    
    parent -r $child $shape[0]; 
    move -os 0 $postmove 0 $child;   
    return $child;            
    }
  
//===============================================
// push - transformation stack
//===============================================
// To enable branching to occur we must keep a 
// reference to the "active" group node so that 
// we can return to it for parenting.
global proc string push(string $node)
    {
    global string $stack[];
    global int $index;
  
    $child = `group -em`;
    $test = `duplicate $node`;
    $stack[$index] = $test[0];
    $index += 1;
    return $child;
    }
  
//===============================================
// pop - transformation stack
//===============================================
// Calling this proc enables us to return to the
// base of a branch.
global proc string pop()
    {
    global string $stack[];
    global int $index;
  
    $index -= 1;
    if($index < 0) {
        print("Error: stack has become negative\n");
        return "";
        }
    return $stack[$index];
}