Folding Effect In Maya

Hi....
I am creating a MEL Script for Folding effect in Maya. But, the  problem is when the script runs the effect plays in hierarchy from 1st Parent to last child. I want this effect from last child to 1st parent. "HOW CAN I FIX MY PROBLEM"? My MEL script is given below.  

string $objs[] = ls -sl;
for ($i=0; $i<size($objs); $i++){

float $bbi[] = xform -q -a -ws -bb $objs[$i]  ;
float $avg =  (($bbi[0]+$bbi[3])/2) ;
float $a = (($bbi[1]+ $bbi[4])/2) ;
float $b = $bbi[2]  ;

xform -ws -piv $avg $a $b $objs[$i];

expression -s("int $speed = 10; float $rotation =  clamp(-180,180,((ctrl.A)*$speed));if($rotation == 180 || $rotation == -180) " + $objs[$i] + ".visibility = 0 ; else " + $objs[$i] + ".visibility = 1; " + $objs[$i] + ".rotateX = $rotation;") ;
parent  $objs[$i] $objs[$i+1];

}

PLS. HELP ME TO FIX THIS PROBLEM.......