//STEP 4: 
//Create and place random-sized twisted corns in certain vertex location
string $twistedCorns = $obj + "_twistedCorns";
attachTwistedCornsToVtx($obj, $orgVertexCount, $twistedCorns);
  
//Remove circles and curves
removeCirclesAndCurves($orgVertexCount);
  
//PROC: Create and place random-sized twisted corns in certain vertex location
global proc attachTwistedCornsToVtx(string $objName, int $orgVertexCount, string $twistedCorns){
    
    string $mash2[];
    select $objName;
    int $i;
    for($i=0; $i<$orgVertexCount; $i++){
        //Create random-sized twisted corn object
        float $randNum = `rand 0.25 0.35`;
        float $distY = 1.0;
        string $path = `curve -d 3  -p 0 0 0 
                        -p (sin(deg_to_rad(60))) ($distY) 0 
                        -p (sin(deg_to_rad(60*2))) ($distY*2) 0 
                        -p (sin(deg_to_rad(60*3))) ($distY*3) 0 
                        -p (sin(deg_to_rad(60*4))) ($distY*4) 0 
                        -p (sin(deg_to_rad(60*5))) ($distY*5) 0
                        -p (sin(deg_to_rad(60*6))) ($distY*6) 0`;
        string $c[] = `circle -nr 0 1 0 -r $randNum`;
        string $newName = $objName + "_twistedCorn_" + $i;
        string $ext[] = `extrude -name $newName -polygon 0 $c[0] $path`;
        string $attributeName = "extrude" + ($i+1);
        setAttr ($attributeName +".scale") 0;
        scale -r 0.5 0.5 0.5;
        
        //Get the vertex and angle information to attach corn
        select $newName;
        string $cmd = $objName + ".vtx[" + $i + "]";
        vector $vec = `pointPosition -w $cmd`;
        float $a[] = aimY($vec);
        
        //Attach corn to the vertex
        move ($vec.x) ($vec.y) ($vec.z);
        rotate -r $a[0] 0  0;
        rotate -r 0     0  $a[1];
        
        $mash2[$i] = $ext[0];
    }
    
    select -r $mash2[0];
    for($i=1; $i<$orgVertexCount; $i++){
        select -tgl $mash2[$i];    
    }
    group -n $twistedCorns;    
}
  
//PROC: Get the avg normal 
proc float[] aimY(vector $vec) {
    float $out[2];
    float $xAngle, $zAngle, $xyLength, $vecLength;
      
    $xyLength = sqrt(($vec.x) * ($vec.x) +
                      ($vec.y) * ($vec.y));
    $vecLength = sqrt(($vec.x) * ($vec.x) +
                       ($vec.y) * ($vec.y) + 
                       ($vec.z) * ($vec.z));
      
    // $xyLength will be zero when $vec is pointing
    // along the +z or -z axis
    if($xyLength == 0)
        $zAngle = ($vec.x) > 0 ? deg_to_rad(90) : deg_to_rad(-90);
    else
        $zAngle = acos(($vec.y)/$xyLength);
      
    $xAngle = acos($xyLength/$vecLength);
      
    $xAngle = ($vec.z) > 0 ? $xAngle : -$xAngle;
    $out[0] = rad_to_deg($xAngle);
      
    $zAngle = ($vec.x) > 0 ? -$zAngle : $zAngle;
    $out[1] = rad_to_deg($zAngle);
    return $out;
}
  
//PROC: Remove used circle and curves
global proc removeCirclesAndCurves(int $orgVertexCount){
    int $i;
    for($i=1; $i<$orgVertexCount+1; $i++){
        string $curvName = "curve" + $i;
        string $circleName = "nurbsCircle" + $i;
        delete $curvName;
        delete $circleName;
    }
}