Scientific-solar-system-creater V.01

Hi,

I already postet this elsewhere, but since it works now, I might aswell post it here so you can all use and try it if you have need for it. Its a quite handy thing to have in your dynamics tab... Maybe you can also help improve it over time.

With this script you can create a quite accurate representation of our solar system and move the planets about in different times scales. It seems to work fine... the scale is 1 maya unit = 100000 km. and the planets are scaled accordingly. The orbit speeds are fine, earth for example travels with around 28 km/s and has a distance of around 1 AU or 149598000 km (1495.98 maya units) from the sun depending on time.

I made a procedure that accepts a date and several other time options. It's all explained in the script. The file is now called "updateSolarSystem.mel"

I should mention Stephen R. Schmitt, from whose site I got lots of useful code and explanations.

enjoy

CODE
//usage:
//1. source the script
//2. Call newSolarSystem();
//3. Create the following runtime expression on the sun
// or any other object using the expression editor:
//      
//        string $update = updateSolarSystem(2012, 10, 28, 0, 0, 3, 1);
//        eval($update);
//
// The first five arguments are: year, month, day, hour, minute as Start Time
// the next argument is an Identifier where:
// 0 = Realtime @ 25 fps
// 1 = every frame one minute
// 2 = every frame one hour
// 3 = every frame one day
// 4 = every frame one month
// 5 = every frame one year

//The last argument(float) is an additional time multiplier for acceleration/deceleration ...
//
//The Units are 1 maya = 100000 km

global proc newSolarSystem()
{
    float $scaleDivider = 100000;
    $PlanetMercury = polySphere -radius (2440 /$scaleDivider) -name mercury;
    $PlanetVenus = polySphere -radius (6052 /$scaleDivider) -name venus;
    $PlanetEarth = polySphere -radius (6378 /$scaleDivider) -name earth;        
    $PlanetMars = polySphere -radius (3397 /$scaleDivider) -name mars;
    $PlanetJupiter = polySphere -radius (71492 /$scaleDivider) -name jupiter;
    $PlaneSaturn = polySphere -radius (60268 /$scaleDivider) -name saturn;
    $PlanetUranus = polySphere -radius (25559 /$scaleDivider) -name uranus;
    $PlaneNeptune = polySphere -radius (24766 /$scaleDivider) -name neptune;
    $PlanePluto = polySphere -radius (1150 /$scaleDivider) -name pluto;

    $theStar = polySphere -radius (695000/$scaleDivider) -name sun;

}

global proc updateSolarSystem(int $Syear, int $Smonth, int $Sday, int $Shour, int $Smin, int $unit, float $timeMod)    
{
    float $frame = currentTime -q;
    switch ($unit) {
    case 0:
        float $dt = day_number($Syear, $Smonth, $Sday, (int)($Shour+$frame), $Smin);
        float $d = ($dt * $timeMod)/60.0/60/25;
        //print ("day = "+$dt);
        break;    
    case 1:
        float $dt = day_number($Syear, $Smonth, $Sday, (int)($Shour+$frame), $Smin);
        float $d = ($dt * $timeMod)/60.0;
        print ("day = "+$dt);
        break;
    case 2:
        float $dt = day_number($Syear, $Smonth, $Sday, (int)($Shour+$frame), $Smin);
        float $d = $dt * $timeMod;
        break;
    case 3:
        float $dt = day_number($Syear, $Smonth, (int)($Sday+$frame), $Shour, $Smin);
        float $d = $dt * $timeMod;
        break;
    case 4:
        float $dt = day_number($Syear, (int)($Smonth+$frame), $Sday, $Shour, $Smin);
        float $d = $dt * $timeMod;
        break;
    case 5:
        float $dt = day_number((int)($Syear+$frame), $Smonth, $Sday, $Shour, $Smin);
        float $d = $dt * $timeMod;
        break;

    }

    calculateAtDaySinceJ2000($d);

}

global proc calculateAtDaySinceJ2000(float $d)
{    
    float $pi = 4 * atan(1);
    float $degs = 180 / $pi;
    float $rads = $pi / 180;
    float $eps = 0.000000000001;

    float $Mercury[8];    
    float $Venus[8];
    float $Earth[8];
    float $Mars[8];
    float $Jupiter[8];
    float $Saturn[8];
    float $Uranus[8];
    float $Neptune[8];
    float $Pluto[8];

    $Mercury = planetDataUsingEpochJ2000($Mercury, 0, $d);
    $Venus = planetDataUsingEpochJ2000($Venus, 1, $d);
    $Earth = planetDataUsingEpochJ2000($Earth, 2, $d);
    $Mars = planetDataUsingEpochJ2000($Mars, 3, $d);
    $Jupiter = planetDataUsingEpochJ2000($Jupiter, 4, $d);
    $Saturn = planetDataUsingEpochJ2000($Saturn, 5, $d);
    $Uranus = planetDataUsingEpochJ2000($Uranus, 6, $d);
    $Neptune = planetDataUsingEpochJ2000($Neptune, 7, $d);
    $Pluto = planetDataUsingEpochJ2000($Pluto, 8, $d);

    vector $mercury_coordinates = heliocentric_coordinates($Mercury[8], $Mercury[3], $Mercury[7], $Mercury[4], $Mercury[2]);
    vector $venus_coordinates = heliocentric_coordinates($Venus[8], $Venus[3], $Venus[7], $Venus[4], $Venus[2]);
    vector $earth_coordinates = heliocentric_coordinates($Earth[8], $Earth[3], $Earth[7], $Earth[4], $Earth[2]);
    vector $mars_coordinates = heliocentric_coordinates($Mars[8], $Mars[3], $Mars[7], $Mars[4], $Mars[2]);
    vector $jupiter_coordinates = heliocentric_coordinates($Jupiter[8], $Jupiter[3], $Jupiter[7], $Jupiter[4], $Jupiter[2]);
    vector $saturn_coordinates = heliocentric_coordinates($Saturn[8], $Saturn[3], $Saturn[7], $Saturn[4], $Saturn[2]);
    vector $uranus_coordinates = heliocentric_coordinates($Uranus[8], $Uranus[3], $Uranus[7], $Uranus[4], $Uranus[2]);
    vector $neptune_coordinates = heliocentric_coordinates($Neptune[8], $Neptune[3], $Neptune[7], $Neptune[4], $Neptune[2]);
    vector $pluto_coordinates = heliocentric_coordinates($Pluto[8], $Pluto[3], $Pluto[7], $Pluto[4], $Pluto[2]);

    setAttr("mercury.translateX", $mercury_coordinates.x);
    setAttr("mercury.translateY", $mercury_coordinates.y);
    setAttr("mercury.translateZ", $mercury_coordinates.z);

    setAttr("venus.translateX", $venus_coordinates.x);
    setAttr("venus.translateY", $venus_coordinates.y);
    setAttr("venus.translateZ", $venus_coordinates.z);

    setAttr("earth.translateX", $earth_coordinates.x);
    setAttr("earth.translateY", $earth_coordinates.y);
    setAttr("earth.translateZ", $earth_coordinates.z);

    setAttr("mars.translateX", $mars_coordinates.x);
    setAttr("mars.translateY", $mars_coordinates.y);
    setAttr("mars.translateZ", $mars_coordinates.z);

    setAttr("jupiter.translateX", $jupiter_coordinates.x);
    setAttr("jupiter.translateY", $jupiter_coordinates.y);
    setAttr("jupiter.translateZ", $jupiter_coordinates.z);

    setAttr("saturn.translateX", $saturn_coordinates.x);
    setAttr("saturn.translateY", $saturn_coordinates.y);
    setAttr("saturn.translateZ", $saturn_coordinates.z);

    setAttr("uranus.translateX", $uranus_coordinates.x);
    setAttr("uranus.translateY", $uranus_coordinates.y);
    setAttr("uranus.translateZ", $uranus_coordinates.z);

    setAttr("neptune.translateX", $neptune_coordinates.x);
    setAttr("neptune.translateY", $neptune_coordinates.y);
    setAttr("neptune.translateZ", $neptune_coordinates.z);

    setAttr("pluto.translateX", $pluto_coordinates.x);
    setAttr("pluto.translateY", $pluto_coordinates.y);
    setAttr("pluto.translateZ", $pluto_coordinates.z);

    /*
    print ("a - mean distance (AU)) = " + $Mercury[0] +"\n");    
    print ("a - mean distance (AU)) = " + $Venus[0] +"\n");    
    print ("a - mean distance (AU)) = " + $Earth[0] +"\n");    
    print ("e - eccentricity of the ellipse = " + $Earth[1] +"\n");
    print ("i - inclination (degrees) = " + $Earth[2] +"\n");
    print ("O - longitude of ascending node (degrees) = " + $Earth[3] +"\n");
    print ("w - longitude of perihelion (degrees) = " + $Earth[4] +"\n");
    print ("L - mean longitude (degrees) = " + $Earth[5] +"\n");
    print ("M - mean anomaly = " + $Earth[6] +"\n");
    print ("V - true anomaly = " + $Earth[7] +"\n");
    print ("R - helio-centric-radius = " + $Earth[8] +"\n");

    */

}

// ----------------------------------------------------------------------------------------------------------------------
// day number to/from J2000 (Jan 1.5, 2000)
global proc float day_number( int $y, int $m, int $d, int $hour, int $mins)
{
    float $rv;
    float $h = $hour + $mins / 60;
    $rv = 367 * $y - floor(7*($y + floor(($m + 9)/12))/4) + floor(275*$m/9) + $d - 730531.5 + $h/24;
    //$rv = 367 * $y - 7*($y + $m + 9 /12)/4 + 275*$m/9 + $d - 730531.5 + $h/24;
    return $rv;

    //rv := 367*y - 7* (y + (m + 9) div 12) div 4         +     275*m div 9 + d - 730531.5 + h / 24
    //var rv = 367*y
    //     - Math.floor(7*(y + Math.floor((m + 9)/12))/4)
    //     + Math.floor(275*m/9) + d - 730531.5 + h/24;
    //int $floor1 = floor(7 *($y + floor(($m + 9)/12))/4);
    //int $floor2 = floor(275 * $m / 9) + $d - 730531.5 + $h / 24;

}
// ----------------------------------------------------------------------------------------------------------------------
// Compute the elements of the orbit for planet-i at day number-d

global proc float[] planetDataUsingEpochJ2000(float $pme[], int $i, float $d )
{
    //planet Major Elements
    float $pme[8];

    float $pi = 4 * atan(1);
    float $rads = $pi / 180;

    // centuries since J2000
    float $cy = $d / 36525;

    //0 a - mean distance (AU) = SemiMajorAxis + ?
    //1 e - eccentricity = Eccentricity + ?
    //2 i - inclination = Inclination - ?
    //3 O - longitude of ascending node = Ascending Node - ?
    //4 w - longitude of perihelion = Long of Periocenter + ?
    //5 L - mean longitude
    //6 M - mean anomaly
    //7 V - true anomaly
    //8 R - helio-centric-radius

    switch ($i)
    {
        // Mercury
    case 0:
        $pme[0] = 0.38709893 + 0.00000066* $cy;
        $pme[1] = 0.20563069 + 0.00002527* $cy;
        $pme[2] = ( 7.00487 - 23.51*$cy/3600)* $rads;
        $pme[3] = (48.33167 - 446.30*$cy/3600)* $rads;
        $pme[4] = (77.45645 + 573.57*$cy/3600)* $rads;
        $pme[5] = mod2pi((252.25084 + 538101628.29*$cy/3600)*$rads);
        $pme[6] = $pme[5] - $pme[4];
        $pme[7] = true_anomaly( $pme[6], $pme[1] );
        $pme[8] = heliocentric_radius( $pme[0], $pme[1], $pme[7]);

        break;
        // Venus
    case 1:
        $pme[0] = 0.72333199 + 0.00000092* $cy;
        $pme[1] = 0.00677323 - 0.00004938* $cy;
        $pme[2] = ( 3.39471 - 2.86*$cy/3600)* $rads;
        $pme[3] = ( 76.68069 - 996.89*$cy/3600)* $rads;
        $pme[4] = (131.53298 - 108.80*$cy/3600)* $rads;
        $pme[5] = mod2pi((181.97973 + 210664136.06*$cy/3600)*$rads);
        $pme[6] = $pme[5] - $pme[4];
        $pme[7] = (true_anomaly( $pme[6], $pme[1] ))*-1;
        $pme[8] = heliocentric_radius( $pme[0], $pme[1], $pme[7]);
        break;
        // Earth/Sun
    case 2:
        $pme[0] = 1.00000011 - 0.00000005* $cy;
        $pme[1] = 0.01671022 - 0.00003804* $cy;
        $pme[2] = ( 0.00005 -    46.94*$cy/3600)* $rads;
        $pme[3] = (-11.26064 - 18228.25*$cy/3600)* $rads;
        $pme[4] = (102.94719 + 1198.28*$cy/3600)* $rads;
        $pme[5] = mod2pi((100.46435 + 129597740.63*$cy/3600)*$rads);
        $pme[6] = $pme[5] - $pme[4];
        $pme[7] = true_anomaly( $pme[6], $pme[1] );
        $pme[8] = heliocentric_radius( $pme[0], $pme[1], $pme[7]);
        break;
        // Mars
    case 3:
        $pme[0] = 1.52366231 - 0.00007221* $cy;
        $pme[1] = 0.09341233 + 0.00011902* $cy;
        $pme[2] = ( 1.85061 - 25.47*$cy/3600)* $rads;
        $pme[3] = ( 49.57854 - 1020.19*$cy/3600)* $rads;
        $pme[4] = (336.04084 + 1560.78*$cy/3600)* $rads;
        $pme[5] = mod2pi((355.45332 + 68905103.78*$cy/3600)*$rads);
        $pme[6] = $pme[5] - $pme[4];
        $pme[7] = true_anomaly( $pme[6], $pme[1] );
        $pme[8] = heliocentric_radius( $pme[0], $pme[1], $pme[7]);
        break;
        // Jupiter
    case 4:
        $pme[0] = 5.20336301 + 0.00060737* $cy;
        $pme[1] = 0.04839266 - 0.00012880* $cy;
        $pme[2] = ( 1.30530 -    4.15*$cy/3600)* $rads;
        $pme[3] = (100.55615 + 1217.17*$cy/3600)* $rads;
        $pme[4] = ( 14.75385 + 839.93*$cy/3600)* $rads;
        $pme[5] = mod2pi((34.40438 + 10925078.35*$cy/3600)*$rads);
        $pme[6] = $pme[5] - $pme[4];
        $pme[7] = true_anomaly( $pme[6], $pme[1] );
        $pme[8] = heliocentric_radius( $pme[0], $pme[1], $pme[7]);
        break;
        // Saturn
    case 5:
        $pme[0] = 9.53707032 - 0.00301530* $cy;
        $pme[1] = 0.05415060 - 0.00036762* $cy;
        $pme[2] = ( 2.48446 +    6.11*$cy/3600)* $rads;
        $pme[3] = (113.71504 - 1591.05*$cy/3600)* $rads;
        $pme[4] = ( 92.43194 - 1948.89*$cy/3600)* $rads;
        $pme[5] = mod2pi((49.94432 + 4401052.95*$cy/3600)*$rads);
        $pme[6] = $pme[5] - $pme[4];
        $pme[7] = true_anomaly( $pme[6], $pme[1] );
        $pme[8] = heliocentric_radius( $pme[0], $pme[1], $pme[7]);
        break;
        // Uranus
    case 6:
        $pme[0] = 19.19126393 + 0.00152025* $cy;
        $pme[1] = 0.04716771 - 0.00019150* $cy;
        $pme[2] = ( 0.76986 -    2.09*$cy/3600)* $rads;
        $pme[3] = ( 74.22988 - 1681.40*$cy/3600)* $rads;
        $pme[4] = (170.96424 + 1312.56*$cy/3600)* $rads;
        $pme[5] = mod2pi((313.23218 + 1542547.79*$cy/3600)*$rads);
        $pme[6] = $pme[5] - $pme[4];
        $pme[7] = (true_anomaly( $pme[6], $pme[1] ))*-1;
        $pme[8] = heliocentric_radius( $pme[0], $pme[1], $pme[7]);
        break;
        // Neptune
    case 7:
        $pme[0] = 30.06896348 - 0.00125196* $cy;
        $pme[1] = 0.00858587 + 0.00002510* $cy;
        $pme[2] = ( 1.76917 - 3.64*$cy/3600)* $rads;
        $pme[3] = (131.72169 - 151.25*$cy/3600)* $rads;
        $pme[4] = ( 44.97135 - 844.43*$cy/3600)* $rads;
        $pme[5] = mod2pi((304.88003 + 786449.21*$cy/3600)*$rads);
        $pme[6] = $pme[5] - $pme[4];
        $pme[7] = (true_anomaly( $pme[6], $pme[1] ))*-1;
        $pme[8] = heliocentric_radius( $pme[0], $pme[1], $pme[7]);
        break;
        // Pluto
    case 8:
        $pme[0] = 39.48168677 - 0.00076912* $cy;
        $pme[1] = 0.24880766 + 0.00006465* $cy;
        $pme[2] = ( 17.14175 + 11.07*$cy/3600)* $rads;
        $pme[3] = (110.30347 - 37.33*$cy/3600)* $rads;
        $pme[4] = (224.06676 - 132.25*$cy/3600)* $rads;
        $pme[5] = mod2pi((238.92881 + 522747.90*$cy/3600)*$rads);
        $pme[6] = $pme[5] - $pme[4];
        $pme[7] = true_anomaly( $pme[6], $pme[1] );
        $pme[8] = heliocentric_radius( $pme[0], $pme[1], $pme[7]);
        break;

    }

    return $pme;
}
// ----------------------------------------------------------------------------------------------------------------------
// return an angle in the range 0 to 2pi radians
global proc float mod2pi( float $x )
{
    float $pi = 4 * atan(1);

    float $b = $x / (2*$pi);
    float $a = (2*$pi)*( $b - abs(floor($b)) );
    if ($a < 0){
     $a = (2*$pi) + $a;
    }
    return $a;
}
// ----------------------------------------------------------------------------------------------------------------------
// compute the true anomaly from mean anomaly using iteration
// M - mean anomaly in radians
// e - orbit eccentricity
global proc float true_anomaly( float $M, float $e )
{
    float $V;
    float $E1;
    float $pi = 4 * atan(1);
    float $degs = 180 / $pi;
    float $rads = $pi / 180;
    float $eps = 0.000000000001;

    // initial approximation of eccentric anomaly
    float $E = $M + $e * sin($M)(1.0 + $e cos($M));
    // iterate to improve accuracy
    do                                
    {
        $E1 = $E;
        $E = $E1 - ($E1 - $e* sin($E1) - $M)/(1 - $e* cos($E1));
    }
    while (abs( $E - $E1 ) > $eps);

    // convert eccentric anomaly to true anomaly
    $V = 2*atan(sqrt((1 + $e)/(1 - $e))* tan(0.5*$E));

    if ($V < 0) {
        // modulo 2pi
        $V = $V + (2*$pi);    
    }
    return $V;
}
// ----------------------------------------------------------------------------------------------------------------------
// compute helio-centric-coordinates
global proc vector heliocentric_coordinates( float $R, float $O, float $V, float $w, float $i )
{
    vector $HelioCoords;
    float $AU = 149598000.0/100000;
    float $XH = $R*(cos($O)cos($V + $w - $O) - sin($O)sin($V + $w - $O)*cos($i));
    float $YH = $R*(sin($O)cos($V + $w - $O) + cos($O)sin($V + $w - $O)*cos($i));
    float $ZH = $R*(sin($V + $w - $O)*sin($i));
    $HelioCoords = <>;
    return $HelioCoords;
}
// ----------------------------------------------------------------------------------------------------------------------
// compute helio-centric-radius
global proc float heliocentric_radius( float $a, float $e, float $V)
{
    float $HelioRadius;
    $HelioRadius = ($a*(1-($e + $e))) / (1 + $e * cos($V));
    return $HelioRadius;
}

I added some generic orbit curves based on positions around the year 2000... The proc newSolarSystem(); takes some time longer now...

CODE
//usage:
//1. source the script
//2. Call newSolarSystem();
//3. Create the following runtime expression on the sun
//   or any other object using the expression editor:
//        
//          string $update = updateSolarSystem(2000, 1, 1, 0, 0, 2, 2);
//        eval($update);
//
// The first five arguments are: year, month, day, hour, minute as Start Time
// the next argument is an Identifier where:
// 0 = Realtime @ 25 fps
// 1 = every frame one minute
// 2 = every frame one hour
// 3 = every frame one day
// 4 = every frame one month
// 5 = every frame one year

//The next argument(float) is an additional time multiplier for acceleration/deceleration  ...
//  
//The Units are 1 maya = 100000 km

global proc newSolarSystem()
{
    float $orbitT[8];

    $orbitT[0] = 87.97/800;
    $orbitT[1] = 224.70/800;
    $orbitT[2] = 365.261/800;
    $orbitT[3] = 686.98/800;
    $orbitT[4] = 4332.71/800;
    $orbitT[5] = 10759.50/800;
    $orbitT[6] = 30685.00/800;
    $orbitT[7] = 60190.00/800;
    $orbitT[8] = 90550.00/800;

    string $pname[8];

    $pname[0] = "mercury";
    $pname[1] = "venus";
    $pname[2] = "earth";
    $pname[3] = "mars";
    $pname[4] = "jupiter";
    $pname[5] = "saturn";
    $pname[6] = "uranus";
    $pname[7] = "neptune";
    $pname[8] = "pluto";    

    float $scaleDivider = 100000;

    $theStar = polySphere -radius (695000/$scaleDivider)  -name sun;

    $PlanetMercury = polySphere -radius (2440  /$scaleDivider) -name mercury;
    $PlanetVenus = polySphere -radius (6052  /$scaleDivider) -name venus;
    $PlanetEarth = polySphere -radius (6378  /$scaleDivider) -name earth;        
    $PlanetMars = polySphere -radius (3397  /$scaleDivider) -name mars;
    $PlanetJupiter = polySphere -radius (71492  /$scaleDivider) -name jupiter;
    $PlanetSaturn = polySphere -radius (60268  /$scaleDivider) -name saturn;
    $PlanetUranus = polySphere -radius (25559  /$scaleDivider) -name uranus;
    $PlanetNeptune = polySphere -radius (24766  /$scaleDivider) -name neptune;
    $PlanetPluto = polySphere -radius (1150  /$scaleDivider) -name pluto;

    global vector $mercury_coordinates;
    global vector $venus_coordinates;
    global vector $earth_coordinates;
    global vector $mars_coordinates;
    global vector $jupiter_coordinates;
    global vector $saturn_coordinates;
    global vector $uranus_coordinates;
    global vector $neptune_coordinates;
    global vector $pluto_coordinates;

    vector $Tmercury_coordinates[];
    vector $Tvenus_coordinates[];
    vector $Tearth_coordinates[];
    vector $Tmars_coordinates[];
    vector $Tjupiter_coordinates[];
    vector $Tsaturn_coordinates[];
    vector $Turanus_coordinates[];
    vector $Tneptune_coordinates[];
    vector $Tpluto_coordinates[];

    calculateAtDaySinceJ2000(0.00);

    $CMercury = curve -d 1 -p ($mercury\_coordinates.x) ($mercury\_coordinates.y) ($mercury\_coordinates.z) -ws;
    $CVenus = curve -d 1 -p ($venus\_coordinates.x) ($venus\_coordinates.y) ($venus\_coordinates.z) -ws;
    $CEarth = curve -d 1 -p ($earth\_coordinates.x) ($earth\_coordinates.y) ($earth\_coordinates.z) -ws;
    $CMars = curve -d 1 -p ($mars\_coordinates.x) ($mars\_coordinates.y) ($mars\_coordinates.z) -ws;
    $CJupiter = curve -d 1 -p ($jupiter\_coordinates.x) ($jupiter\_coordinates.y) ($jupiter\_coordinates.z) -ws;
    $CSaturn = curve -d 1 -p ($saturn\_coordinates.x) ($saturn\_coordinates.y) ($saturn\_coordinates.z) -ws;
    $CUranus = curve -d 1 -p ($uranus\_coordinates.x) ($uranus\_coordinates.y) ($uranus\_coordinates.z) -ws;
    $CNeptune = curve -d 1 -p ($neptune\_coordinates.x) ($neptune\_coordinates.y) ($neptune\_coordinates.z) -ws;
    $CPluto = curve -d 1 -p ($pluto\_coordinates.x) ($pluto\_coordinates.y) ($pluto\_coordinates.z) -ws;

    rename $CMercury "Cmercury";
    rename $CVenus "Cvenus";
    rename $CEarth "Cearth";    
    rename $CMars "Cmars";
    rename $CJupiter "Cjupiter";
    rename $CSaturn  "Csaturn";
    rename $CUranus  "Curanus";
    rename $CNeptune "Cneptune";
    rename $CPluto "Cpluto";

    int $i;
    int $z;

    for($i = 0; $i < size($orbitT); $i++){
        float $OrbitTotal = 0.0;
        for($z = 0; $z < 800; $z++){
            if($OrbitTotal <= ($orbitT[$i]*800)){
                $OrbitTotal += $orbitT[$i];
                switch ($pname[$i]) {
                case "mercury":
                        calculateAtDaySinceJ2000($OrbitTotal);
                        $Tmercury_coordinates[$z] = $mercury_coordinates;
                    break;        
                case "venus":    
                        calculateAtDaySinceJ2000($OrbitTotal);
                        $Tvenus_coordinates[$z] = $venus_coordinates;
                    break;      
                case "earth":    
                        calculateAtDaySinceJ2000($OrbitTotal);
                        $Tearth_coordinates[$z] = $earth_coordinates;
                    break;      
                case "mars":    
                        calculateAtDaySinceJ2000($OrbitTotal);
                        $Tmars_coordinates[$z] = $mars_coordinates;
                    break;      
                case "jupiter":  
                        calculateAtDaySinceJ2000($OrbitTotal);
                        $Tjupiter_coordinates[$z] = $jupiter_coordinates;
                    break;      
                case "saturn":  
                        calculateAtDaySinceJ2000($OrbitTotal);
                        $Tsaturn_coordinates[$z] = $saturn_coordinates;
                    break;      
                case "uranus":  
                        calculateAtDaySinceJ2000($OrbitTotal);
                        $Turanus_coordinates[$z] = $uranus_coordinates;
                    break;      
                case "neptune":  
                        calculateAtDaySinceJ2000($OrbitTotal);
                        $Tneptune_coordinates[$z] = $neptune_coordinates;
                    break;      
                case "pluto":    
                        calculateAtDaySinceJ2000($OrbitTotal);
                        $Tpluto_coordinates[$z] = $pluto_coordinates;
                    break;

                }

        }    
    }
}

    int $k;
    for($k = 0; $k < size($Tmercury_coordinates)-1; $k++){
        vector $currCord = $Tmercury_coordinates[$k];
        //print ($currCord + " " + $k + "\n");
        curve -a -p ($currCord.x) ($currCord.y) ($currCord.z) Cmercury;    
    }    
    //fitBspline -ch 0 -tol 0.01 -n Mercury_Orbit Cmercury;
    //rebuildCurve -rt 0 -s 500 Mercury_Orbit;
    closeCurve -ch 0 -ps 0 -rpo 1 -bb 0.5 -bki 0 -p 0.1 Cmercury;
    //delete Cmercury;

    int $k;
    for($k = 0; $k < size($Tvenus_coordinates)-1; $k++){
        vector $currCord = $Tvenus_coordinates[$k];
        //print ($currCord + " " + $k + "\n");
        curve -a -p ($currCord.x) ($currCord.y) ($currCord.z) Cvenus;    
    }    
    //fitBspline -ch 0 -tol 0.01  -n Venus_Orbit Cvenus;
    //rebuildCurve -rt 0 -s 500 Venus_Orbit;
    closeCurve -ch 0 -ps 0 -rpo 1 -bb 0.5 -bki 0 -p 0.1 Cvenus;
    //delete Cvenus;

    int $k;
    for($k = 0; $k < size($Tearth_coordinates)-1; $k++){
        vector $currCord = $Tearth_coordinates[$k];
        //print ($currCord + " " + $k + "\n");
        curve -a -p ($currCord.x) ($currCord.y) ($currCord.z) Cearth;    
    }    
    //fitBspline -ch 0 -tol 0.01 -n Earth_Orbit Cearth;
    //rebuildCurve -rt 0 -s 500 Earth_Orbit;
    closeCurve -ch 0 -ps 0 -rpo 1 -bb 0.5 -bki 0 -p 0.1 Cearth;
    //delete Cearth;

    int $k;
    for($k = 0; $k < size($Tmars_coordinates)-1; $k++){
        vector $currCord = $Tmars_coordinates[$k];
        //print ($currCord + " " + $k + "\n");
        curve -a -p ($currCord.x) ($currCord.y) ($currCord.z) Cmars;    
    }    
    //fitBspline -ch 0 -tol 0.01 -n Mars_Orbit Cmars;
    //rebuildCurve -rt 0 -s 500 Mars_Orbit;
    closeCurve -ch 0 -ps 0 -rpo 1 -bb 0.5 -bki 0 -p 0.1 Cmars;
    //delete Cmars;

    int $k;
    for($k = 0; $k < size($Tjupiter_coordinates)-1; $k++){
        vector $currCord = $Tjupiter_coordinates[$k];
        //print ($currCord + " " + $k + "\n");
        curve -a -p ($currCord.x) ($currCord.y) ($currCord.z) Cjupiter;    
    }    
    //fitBspline -ch 0 -tol 0.01 -n Jupiter_Orbit Cjupiter;
    //rebuildCurve -rt 0 -s 500 Jupiter_Orbit;
    closeCurve -ch 0 -ps 0 -rpo 1 -bb 0.5 -bki 0 -p 0.1 Cjupiter;
    //delete Cjupiter;

    int $k;
    for($k = 0; $k < size($Tsaturn_coordinates)-1; $k++){
        vector $currCord = $Tsaturn_coordinates[$k];
        //print ($currCord + " " + $k + "\n");
        curve -a -p ($currCord.x) ($currCord.y) ($currCord.z) Csaturn;    
    }    
    //fitBspline -ch 0 -tol 0.01 -n Saturn_Orbit Csaturn;
    //rebuildCurve -rt 0 -s 500 Saturn_Orbit;
    closeCurve -ch 0 -ps 0 -rpo 1 -bb 0.5 -bki 0 -p 0.1 Csaturn;
    //delete Csaturn;

    int $k;
    for($k = 0; $k < size($Turanus_coordinates)-1; $k++){
        vector $currCord = $Turanus_coordinates[$k];
        //print ($currCord + " " + $k + "\n");
        curve -a -p ($currCord.x) ($currCord.y) ($currCord.z) Curanus;    
    }    
    //fitBspline -ch 0 -tol 0.01 -n Uranus_Orbit Curanus;
    //rebuildCurve -rt 0 -s 500 Uranus_Orbit;
    closeCurve -ch 0 -ps 0 -rpo 1 -bb 0.5 -bki 0 -p 0.1 Curanus;
    //delete Curanus;

    int $k;
    for($k = 0; $k < size($Tneptune_coordinates)-1; $k++){
        vector $currCord = $Tneptune_coordinates[$k];
        //print ($currCord + " " + $k + "\n");
        curve -a -p ($currCord.x) ($currCord.y) ($currCord.z) Cneptune;    
    }    
    //fitBspline -ch 0 -tol 0.01 -n Neptune_Orbit Cneptune;
    //rebuildCurve -rt 0 -s 500 Neptune_Orbit;
    closeCurve -ch 0 -ps 0 -rpo 1 -bb 0.5 -bki 0 -p 0.1 Cneptune;
    //delete Cneptune;

    int $k;
    for($k = 0; $k < size($Tpluto_coordinates)-1; $k++){
        vector $currCord = $Tpluto_coordinates[$k];
        //print ($currCord + " " + $k + "\n");
        curve -a -p ($currCord.x) ($currCord.y) ($currCord.z) Cpluto;    
    }    
    //fitBspline -ch 0 -tol 0.01 -n Pluto_Orbit Cpluto;
    //rebuildCurve -rt 0 -s 500 Pluto_Orbit;
    closeCurve -ch 0 -ps 0 -rpo 1 -bb 0.5 -bki 0 -p 0.1 Cpluto;
    //delete Cpluto;

    //textScrollList -edit -append mercury planOrbitList;
}

global proc updateSolarSystem(int $Syear, int $Smonth, int $Sday, int $Shour, int $Smin, int $unit, float $timeMod)    
{
    float $frame =  currentTime -q;
    switch ($unit) {
    case 0:
        float $dt = day_number($Syear, $Smonth, $Sday, (int)($Shour+$frame), $Smin);
        float $d = ($dt * $timeMod)/60.0/60/25;
        //print ("day = "+$dt);
        break;    
    case 1:
        float $dt = day_number($Syear, $Smonth, $Sday, (int)($Shour+$frame), $Smin);
        float $d = ($dt * $timeMod)/60.0;
        //print ("day = "+$dt);
        break;
    case 2:
        float $dt = day_number($Syear, $Smonth, $Sday, (int)($Shour+$frame), $Smin);
        float $d = $dt * $timeMod;
        break;
    case 3:
        float $dt = day_number($Syear, $Smonth, (int)($Sday+$frame), $Shour, $Smin);
        float $d = $dt * $timeMod;
        break;
    case 4:
        float $dt = day_number($Syear, (int)($Smonth+$frame), $Sday, $Shour, $Smin);
        float $d = $dt * $timeMod;
        break;
    case 5:
        float $dt = day_number((int)($Syear+$frame), $Smonth, $Sday, $Shour, $Smin);
        float $d = $dt * $timeMod;
        break;

    }

    calculateAtDaySinceJ2000($d);

    /*
    connectControl starPosX ($theStar[0]+".translateX");
    connectControl starPosY ($theStar[0]+".translateY");
    connectControl starPosZ ($theStar[0]+".translateZ");
    connectControl starRadius ($theStar[1]+".radius");
    */
}

global proc calculateAtDaySinceJ2000(float $d)
{    
    float $pi  = 4 * atan(1);
    float $degs = 180 / $pi;
    float $rads = $pi / 180;
    float $eps  = 0.000000000001;

    float $Mercury[8];    
    float $Venus[8];
    float $Earth[8];
    float $Mars[8];
    float $Jupiter[8];
    float $Saturn[8];
    float $Uranus[8];
    float $Neptune[8];
    float $Pluto[8];

    global vector $mercury_coordinates;
    global vector $venus_coordinates;
    global vector $earth_coordinates;
    global vector $mars_coordinates;
    global vector $jupiter_coordinates;
    global vector $saturn_coordinates;
    global vector $uranus_coordinates;
    global vector $neptune_coordinates;
    global vector $pluto_coordinates;

    $Mercury = planetDataUsingEpochJ2000($Mercury, 0, $d);
    $Venus = planetDataUsingEpochJ2000($Venus, 1, $d);
    $Earth = planetDataUsingEpochJ2000($Earth, 2, $d);
    $Mars = planetDataUsingEpochJ2000($Mars, 3, $d);
    $Jupiter = planetDataUsingEpochJ2000($Jupiter, 4, $d);
    $Saturn = planetDataUsingEpochJ2000($Saturn, 5, $d);
    $Uranus = planetDataUsingEpochJ2000($Uranus, 6, $d);
    $Neptune = planetDataUsingEpochJ2000($Neptune, 7, $d);
    $Pluto = planetDataUsingEpochJ2000($Pluto, 8, $d);

    vector $mercury_coordinates = heliocentric_coordinates($Mercury[8], $Mercury[3], $Mercury[7], $Mercury[4], $Mercury[2]);
    vector $venus_coordinates = heliocentric_coordinates($Venus[8], $Venus[3], $Venus[7], $Venus[4], $Venus[2]);
    vector $earth_coordinates = heliocentric_coordinates($Earth[8], $Earth[3], $Earth[7], $Earth[4], $Earth[2]);
    vector $mars_coordinates = heliocentric_coordinates($Mars[8], $Mars[3], $Mars[7], $Mars[4], $Mars[2]);
    vector $jupiter_coordinates = heliocentric_coordinates($Jupiter[8], $Jupiter[3], $Jupiter[7], $Jupiter[4], $Jupiter[2]);
    vector $saturn_coordinates = heliocentric_coordinates($Saturn[8], $Saturn[3], $Saturn[7], $Saturn[4], $Saturn[2]);
    vector $uranus_coordinates = heliocentric_coordinates($Uranus[8], $Uranus[3], $Uranus[7], $Uranus[4], $Uranus[2]);
    vector $neptune_coordinates = heliocentric_coordinates($Neptune[8], $Neptune[3], $Neptune[7], $Neptune[4], $Neptune[2]);
    vector $pluto_coordinates = heliocentric_coordinates($Pluto[8], $Pluto[3], $Pluto[7], $Pluto[4], $Pluto[2]);

    int $index = (int)(currentTime -q-1);
    /*
    float $Rmerc[10][3];
    float $Rmerc[$index][0] = $mercury_coordinates.x;
    float $Rmerc[$index][1] = $mercury_coordinates.y;
    float $Rmerc[$index][2] = $mercury_coordinates.z;
    */

    setAttr("mercury.translateX", $mercury_coordinates.x);
    setAttr("mercury.translateY", $mercury_coordinates.y);
    setAttr("mercury.translateZ", $mercury_coordinates.z);

    setAttr("venus.translateX", $venus_coordinates.x);
    setAttr("venus.translateY", $venus_coordinates.y);
    setAttr("venus.translateZ", $venus_coordinates.z);

    setAttr("earth.translateX", $earth_coordinates.x);
    setAttr("earth.translateY", $earth_coordinates.y);
    setAttr("earth.translateZ", $earth_coordinates.z);

    setAttr("mars.translateX", $mars_coordinates.x);
    setAttr("mars.translateY", $mars_coordinates.y);
    setAttr("mars.translateZ", $mars_coordinates.z);

    setAttr("jupiter.translateX", $jupiter_coordinates.x);
    setAttr("jupiter.translateY", $jupiter_coordinates.y);
    setAttr("jupiter.translateZ", $jupiter_coordinates.z);

    setAttr("saturn.translateX", $saturn_coordinates.x);
    setAttr("saturn.translateY", $saturn_coordinates.y);
    setAttr("saturn.translateZ", $saturn_coordinates.z);

    setAttr("uranus.translateX", $uranus_coordinates.x);
    setAttr("uranus.translateY", $uranus_coordinates.y);
    setAttr("uranus.translateZ", $uranus_coordinates.z);

    setAttr("neptune.translateX", $neptune_coordinates.x);
    setAttr("neptune.translateY", $neptune_coordinates.y);
    setAttr("neptune.translateZ", $neptune_coordinates.z);

    setAttr("pluto.translateX", $pluto_coordinates.x);
    setAttr("pluto.translateY", $pluto_coordinates.y);
    setAttr("pluto.translateZ", $pluto_coordinates.z);

    /*
    print ("a - mean distance (AU)) = " + $Mercury[0] +"\n");    
    print ("a - mean distance (AU)) = " + $Venus[0] +"\n");    
    print ("a - mean distance (AU)) = " + $Earth[0] +"\n");    
    print ("e - eccentricity of the ellipse = " + $Earth[1] +"\n");
    print ("i - inclination (degrees) = " + $Earth[2] +"\n");
    print ("O - longitude of ascending node (degrees) = " + $Earth[3] +"\n");
    print ("w - longitude of perihelion (degrees) = " + $Earth[4] +"\n");
    print ("L - mean longitude (degrees) = " + $Earth[5] +"\n");
    print ("M - mean anomaly = " + $Earth[6] +"\n");
    print ("V - true anomaly = " + $Earth[7] +"\n");
    print ("R - helio-centric-radius = " + $Earth[8] +"\n");

    */

}

// ----------------------------------------------------------------------------------------------------------------------
// day number to/from J2000 (Jan 1.5, 2000)
global proc float day_number( int $y, int $m, int $d, int $hour, int $mins)
{
    float $rv;
    float $h = $hour + $mins / 60;
    $rv = 367 * $y - floor(7*($y + floor(($m + 9)/12))/4) + floor(275*$m/9) + $d - 730531.5 + $h/24;
    //$rv = 367 * $y - 7*($y + $m + 9 /12)/4 + 275*$m/9 + $d - 730531.5 + $h/24;
    return $rv;

    //rv := 367*y - 7*   (y + (m + 9) div 12)   div 4         +      275*m div 9 + d - 730531.5 + h / 24
    //var rv = 367*y
    //     - Math.floor(7*(y + Math.floor((m + 9)/12))/4)
    //     + Math.floor(275*m/9) + d - 730531.5 + h/24;
    //int $floor1 = floor(7 *($y + floor(($m + 9)/12))/4);
    //int $floor2 = floor(275 * $m / 9) + $d - 730531.5 + $h / 24;

}
// ----------------------------------------------------------------------------------------------------------------------
// Compute the elements of the orbit for planet-i at day number-d

global proc float[] planetDataUsingEpochJ2000(float $pme[], int $i, float $d )
{
    //planet Major Elements
    float $pme[8];

    float $pi  = 4 * atan(1);
    float $rads = $pi / 180;

    // centuries since J2000
    float $cy = $d / 36525;

    //0 a - mean distance (AU) = SemiMajorAxis + ?
    //1 e - eccentricity = Eccentricity + ?
    //2 i - inclination = Inclination - ?
    //3 O - longitude of ascending node = Ascending Node - ?
    //4 w - longitude of perihelion = Long of Periocenter + ?
    //5 L - mean longitude
    //6 M - mean anomaly
    //7 V - true anomaly
    //8 R - helio-centric-radius

    switch ($i)
    {
        // Mercury
    case 0:
        $pme[0] = 0.38709893 + 0.00000066* $cy;
        $pme[1] = 0.20563069 + 0.00002527* $cy;
        $pme[2] = ( 7.00487  -  23.51*$cy/3600)* $rads;
        $pme[3] = (48.33167  - 446.30*$cy/3600)* $rads;
        $pme[4] = (77.45645  + 573.57*$cy/3600)* $rads;
        $pme[5] = mod2pi((252.25084 + 538101628.29*$cy/3600)*$rads);
        $pme[6] = $pme[5] - $pme[4];
        $pme[7] = true_anomaly( $pme[6], $pme[1] );
        $pme[8] = heliocentric_radius( $pme[0], $pme[1], $pme[7]);

        break;
        // Venus
    case 1:
        $pme[0] = 0.72333199 + 0.00000092* $cy;
        $pme[1] = 0.00677323 - 0.00004938* $cy;
        $pme[2] = (  3.39471 -   2.86*$cy/3600)* $rads;
        $pme[3] = ( 76.68069 - 996.89*$cy/3600)* $rads;
        $pme[4] = (131.53298 - 108.80*$cy/3600)* $rads;
        $pme[5] = mod2pi((181.97973 + 210664136.06*$cy/3600)*$rads);
        $pme[6] = $pme[5] - $pme[4];
        $pme[7] = (true_anomaly( $pme[6], $pme[1] ))*-1;
        $pme[8] = heliocentric_radius( $pme[0], $pme[1], $pme[7]);
        break;
        // Earth/Sun
    case 2:
        $pme[0] = 1.00000011 - 0.00000005* $cy;
        $pme[1] = 0.01671022 - 0.00003804* $cy;
        $pme[2] = (  0.00005 -    46.94*$cy/3600)* $rads;
        $pme[3] = (-11.26064 - 18228.25*$cy/3600)* $rads;
        $pme[4] = (102.94719 +  1198.28*$cy/3600)* $rads;
        $pme[5] = mod2pi((100.46435 + 129597740.63*$cy/3600)*$rads);
        $pme[6] = $pme[5] - $pme[4];
        $pme[7] = true_anomaly( $pme[6], $pme[1] );
        $pme[8] = heliocentric_radius( $pme[0], $pme[1], $pme[7]);
        break;
        // Mars
    case 3:
        $pme[0] = 1.52366231 - 0.00007221* $cy;
        $pme[1] = 0.09341233 + 0.00011902* $cy;
        $pme[2] = (  1.85061 -   25.47*$cy/3600)* $rads;
        $pme[3] = ( 49.57854 - 1020.19*$cy/3600)* $rads;
        $pme[4] = (336.04084 + 1560.78*$cy/3600)* $rads;
        $pme[5] = mod2pi((355.45332 + 68905103.78*$cy/3600)*$rads);
        $pme[6] = $pme[5] - $pme[4];
        $pme[7] = true_anomaly( $pme[6], $pme[1] );
        $pme[8] = heliocentric_radius( $pme[0], $pme[1], $pme[7]);
        break;
        // Jupiter
    case 4:
        $pme[0] = 5.20336301 + 0.00060737* $cy;
        $pme[1] = 0.04839266 - 0.00012880* $cy;
        $pme[2] = (  1.30530 -    4.15*$cy/3600)* $rads;
        $pme[3] = (100.55615 + 1217.17*$cy/3600)* $rads;
        $pme[4] = ( 14.75385 +  839.93*$cy/3600)* $rads;
        $pme[5] = mod2pi((34.40438 + 10925078.35*$cy/3600)*$rads);
        $pme[6] = $pme[5] - $pme[4];
        $pme[7] = true_anomaly( $pme[6], $pme[1] );
        $pme[8] = heliocentric_radius( $pme[0], $pme[1], $pme[7]);
        break;
        // Saturn
    case 5:
        $pme[0] = 9.53707032 - 0.00301530* $cy;
        $pme[1] = 0.05415060 - 0.00036762* $cy;
        $pme[2] = (  2.48446 +    6.11*$cy/3600)* $rads;
        $pme[3] = (113.71504 - 1591.05*$cy/3600)* $rads;
        $pme[4] = ( 92.43194 - 1948.89*$cy/3600)* $rads;
        $pme[5] = mod2pi((49.94432 + 4401052.95*$cy/3600)*$rads);
        $pme[6] = $pme[5] - $pme[4];
        $pme[7] = true_anomaly( $pme[6], $pme[1] );
        $pme[8] = heliocentric_radius( $pme[0], $pme[1], $pme[7]);
        break;
        // Uranus
    case 6:
        $pme[0] = 19.19126393 + 0.00152025* $cy;
        $pme[1] =  0.04716771 - 0.00019150* $cy;
        $pme[2] = (  0.76986  -    2.09*$cy/3600)* $rads;
        $pme[3] = ( 74.22988  - 1681.40*$cy/3600)* $rads;
        $pme[4] = (170.96424  + 1312.56*$cy/3600)* $rads;
        $pme[5] = mod2pi((313.23218 + 1542547.79*$cy/3600)*$rads);
        $pme[6] = $pme[5] - $pme[4];
        $pme[7] = (true_anomaly( $pme[6], $pme[1] ))*-1;
        $pme[8] = heliocentric_radius( $pme[0], $pme[1], $pme[7]);
        break;
        // Neptune
    case 7:
        $pme[0] = 30.06896348 - 0.00125196* $cy;
        $pme[1] =  0.00858587 + 0.00002510* $cy;
        $pme[2] = (  1.76917  -   3.64*$cy/3600)* $rads;
        $pme[3] = (131.72169  - 151.25*$cy/3600)* $rads;
        $pme[4] = ( 44.97135  - 844.43*$cy/3600)* $rads;
        $pme[5] = mod2pi((304.88003 + 786449.21*$cy/3600)*$rads);
        $pme[6] = $pme[5] - $pme[4];
        $pme[7] = (true_anomaly( $pme[6], $pme[1] ))*-1;
        $pme[8] = heliocentric_radius( $pme[0], $pme[1], $pme[7]);
        break;
        // Pluto
    case 8:
        $pme[0] = 39.48168677 - 0.00076912* $cy;
        $pme[1] =  0.24880766 + 0.00006465* $cy;
        $pme[2] = ( 17.14175  +  11.07*$cy/3600)* $rads;
        $pme[3] = (110.30347  -  37.33*$cy/3600)* $rads;
        $pme[4] = (224.06676  - 132.25*$cy/3600)* $rads;
        $pme[5] = mod2pi((238.92881 + 522747.90*$cy/3600)*$rads);
        $pme[6] = $pme[5] - $pme[4];
        $pme[7] = true_anomaly( $pme[6], $pme[1] );
        $pme[8] = heliocentric_radius( $pme[0], $pme[1], $pme[7]);
        break;

    }

    return $pme;
}
// ----------------------------------------------------------------------------------------------------------------------
// return an angle in the range 0 to 2pi radians
global proc float mod2pi( float $x )
{
    float $pi  = 4 * atan(1);

    float $b = $x / (2*$pi);
    float $a = (2*$pi)*( $b - abs(floor($b)) );  
    if ($a < 0){
       $a = (2*$pi) + $a;
    }
    return $a;
}
// ----------------------------------------------------------------------------------------------------------------------
// compute the true anomaly from mean anomaly using iteration
//  M - mean anomaly in radians
//  e - orbit eccentricity
global proc float true_anomaly( float $M, float $e )
{
    float $V;
    float $E1;
    float $pi  = 4 * atan(1);
    float $degs = 180 / $pi;
    float $rads = $pi / 180;
    float $eps  = 0.000000000001;

    // initial approximation of eccentric anomaly
    float $E = $M + $e * sin($M)(1.0 + $e cos($M));
    // iterate to improve accuracy
    do                                  
    {
        $E1 = $E;
        $E = $E1 - ($E1 - $e* sin($E1) - $M)/(1 - $e* cos($E1));
    }
    while (abs( $E - $E1 ) > $eps);

    // convert eccentric anomaly to true anomaly
    $V = 2*atan(sqrt((1 + $e)/(1 - $e))* tan(0.5*$E));

    if ($V < 0) {
        // modulo 2pi
        $V = $V + (2*$pi);      
    }
    return $V;
}
// ----------------------------------------------------------------------------------------------------------------------
// compute helio-centric-coordinates
global proc vector heliocentric_coordinates( float $R, float $O, float $V, float $w, float $i )
{
    vector $HelioCoords;
    float $AU = 149598000.0/100000;
    float $XH = $R*(cos($O)cos($V + $w - $O) - sin($O)sin($V + $w - $O)*cos($i));
    float $YH = $R*(sin($O)cos($V + $w - $O) + cos($O)sin($V + $w - $O)*cos($i));
    float $ZH = $R*(sin($V + $w - $O)*sin($i));
    $HelioCoords = <>;
    return $HelioCoords;
}
// ----------------------------------------------------------------------------------------------------------------------
// compute helio-centric-radius
global proc float heliocentric_radius( float $a, float $e, float $V)
{
    float $HelioRadius;
    $HelioRadius = ($a*(1-($e + $e))) / (1 + $e * cos($V));
    return $HelioRadius;
}

you could probably make it a lot faster if you would omit the eval phase! Som more by converting the triple settrs to xfforms and a quite lot more with sioem restructuring of your code.

yes, I guess I could improve performance quite a lot.... For a start I would be happy to combine the single for loops that I typed for each planet to append the curve points into the doupble for loop above, thus avoiding to save the 800 calculated orbit vectors in another vector[] before applying them. Hope you can follow.

My problem is that I would like, actionscript style, for example to make the variable name $TmercuryCoordinates[$k] from something like this:

CODE
vector $CurrCord = "$T" + $pname[$j] + "Coordinates[" + $k "]"

It's quite a dirty string combine way to build variables from different strings and counters, whose name matches existing Variables... I've done it a lot of times in actionscript, of course it would never work in java or c# or alike

actionscript examples:

CODE
_root.commserver.mapZoomer.map2.ED[ID+"H2"]._visible = true;
_root.commserver.mapZoomer.map2.ED.Boundaries["Collide"+IDTotal[i][0]]._width = 320000*Faktor;

would something like this ever work in mel ?

With eval it would. Offcourse that is indeed VERY slow.

However problem is that this is hugely inefficient for your computer, reason
being that it cant allocate its stack up front. So it needs to constantly fight out
the order of things. So unknown to you the code underneath becomes much
messier then you ever imagined it to be.

However were you to use attribuetes thats not a problem since they are evaluated
form strings anyway. Off course attributes aren't variables.

However real programming languages (mel is not a programming language its a journal)
implement something called a hashtable, map or dictionary that does the same thing much
more efficently. Especially if the slots are known before hand since it makes for perfect hashing.

Basically it just substitutes numbers for names. Offcourse form pure programming point fo view
theres not much difference in doing:

$MercuryRotation=1;
$list[$mercuryrotation];

versus doing something that for example would work in python script.

$list["mercuryrotation"];

in fact the former is faster even in python, an certainly in languages thet have preprocessing macros
its even faster.
However point is mayas not meant to use scripts for computing dg effects, it does not much matter
if you use python or mel. Youd have to use api for stuff like that in reality, because script inetrfaces
never were intended for computation.

Lets disect some of your code fro example theres a much more nice way to maintain stuff like the planet
data , you dont actually need a switch its totaly unneccesery. you can achieve same thing with a single list,
and if you must collapse it to 8 using long list a for loop.

Secondly you do not actually need many arrays for coordinates you can have all coordinates in one array.
Aslo passing dta in form of globals form functions is a bit insane.

Im not here to fix the code but id estimate id get could wirte the update a s full expression withot need for mel
(maya expression evaluetor and mel are not equal), at some 10 times less code.

For example just

the code:

float $Mercury[8];    
float $Venus[8];
float $Earth[8];
float $Mars[8];
float $Jupiter[8];
float $Saturn[8];
float $Uranus[8];
float $Neptune[8];
float $Pluto[8];

can be expressed as

float allPlanets[72];

wich is much easier because then you can do the long switch statement as a double loop.

HOWEVER:

due to the way maya works it actually better to create one fully expression node for each planet,
because your loops dont seem to synergize. Thisway maya dont need to compute th each planets that
are for example hidden.

Thanks for your long answer.

Yes all my examples evaluate strings and counters to access attributes, not variables, thats right. In my actionscript examples that was _width and _visible, two movieclip attributes from flash.

The float[8] on the other hand should remain that way since most of the procedures that work with those 9 values expect them to be in their respective slot.... see:

CODE
        $pme[0] = 0.38709893 + 0.00000066* $cy;
        $pme[1] = 0.20563069 + 0.00002527* $cy;
        $pme[2] = ( 7.00487  -  23.51*$cy/3600)* $rads;
        $pme[3] = (48.33167  - 446.30*$cy/3600)* $rads;
        $pme[4] = (77.45645  + 573.57*$cy/3600)* $rads;
        $pme[5] = mod2pi((252.25084 + 538101628.29*$cy/3600)*$rads);
        $pme[6] = $pme[5] - $pme[4];
        $pme[7] = true_anomaly( $pme[6], $pme[1] );
        $pme[8] = heliocentric_radius( $pme[0], $pme[1], $pme[7]);

        //or

       vector $mercury_coordinates = heliocentric_coordinates($Mercury[8], $Mercury[3], $Mercury[7], $Mercury[4], $Mercury[2]);

moreover I never loop those 9 values...
The 9 planet floats are mainly the constants for calculations, also called planet major elements or keplerian elements. 6 of them never change (mean distance, eccentricity, inclination, longitude of ascending node, longitude of perihelion, mean longitude) 3 of them change on every date (mean anomaly, true anomaly, helio-centric-radius). Alone for this reason a float[72] would be much more work and less overview.

All my loops, if you read the code, concern the planet eclipses... Curves. These curves are calculated beforehand in the function createSolarSystem(); The planets are set to day 0 and the first curve point is created for each planet curve on these coordinates.
Then for each planet the loop increments the date until a complete orbit for each planet is archieved (every planet has a different orbit time). It does this in 800 steps so that is the only thing common for each.

QUOTE
Im not here to fix the code but id estimate id get could wirte the update a s full expression withot need for mel
(maya expression evaluetor and mel are not equal), at some 10 times less code.

thank you... one of these days.... show me please ...

And back to my original question... how do i use eval to make strings into variable names....

thanks in advance.

correction...

none of the 9 planet floats are constants, the keplerian elements are, but these values are already for the specific date they are calculated for... The thing is, I am really not understanding much of these calculations... I know where the input and outputs must go but except for that I have a firefox browser open with 80 wikipedia tabs and I'm just really getting into it, same with mel...

I found this in another script:

CODE
float $vertPos[] = xform -a -ws -q -t ($sel[0]+".vtx["+$orderedVerts[0]+"]");

and I would really like to know if its okay to use something like that and how the general syntax is for assigning/querying string build attributes or even better variables from/for values...

QUOTE(zillion42 @ 11/09/07, 05:32 PM) [snapback]276258[/snapback]
moreover I never loop those 9 values...

Yes i know butt you should probably SHOULD do it as a loop, since it is one

Since whet you have is 8 lists that is the same thing as 1 list (multiple leust can allwyas be made into single lists).
Now if you put everything in one list you would have:

CODE
string $planetNames[]={"mercury","venus","earth","mars","jupiter","saturn","uranus","neptune","pluto"};
for ($planet=0; $planet<9;$planet++){
  $cord=heliocentric_coordinates($data[$planet*9+8], $Venus[$planet*9+3], $Venus[$planet*9+7], $Venus[$planet*9+4], $Venus[$planet*9+2]);
  xform -t ($cord.z) ($cord.y) ($cord.z) $planetName[$planet];
}

instead of the whole part

CODE
    vector $mercury_coordinates = heliocentric_coordinates($Mercury[8], $Mercury[3], $Mercury[7], $Mercury[4], $Mercury[2]);
    vector $venus_coordinates = heliocentric_coordinates($Venus[8], $Venus[3], $Venus[7], $Venus[4], $Venus[2]);
    vector $earth_coordinates = heliocentric_coordinates($Earth[8], $Earth[3], $Earth[7], $Earth[4], $Earth[2]);
    vector $mars_coordinates = heliocentric_coordinates($Mars[8], $Mars[3], $Mars[7], $Mars[4], $Mars[2]);
    vector $jupiter_coordinates = heliocentric_coordinates($Jupiter[8], $Jupiter[3], $Jupiter[7], $Jupiter[4], $Jupiter[2]);
    vector $saturn_coordinates = heliocentric_coordinates($Saturn[8], $Saturn[3], $Saturn[7], $Saturn[4], $Saturn[2]);
    vector $uranus_coordinates = heliocentric_coordinates($Uranus[8], $Uranus[3], $Uranus[7], $Uranus[4], $Uranus[2]);
    vector $neptune_coordinates = heliocentric_coordinates($Neptune[8], $Neptune[3], $Neptune[7], $Neptune[4], $Neptune[2]);
    vector $pluto_coordinates = heliocentric_coordinates($Pluto[8], $Pluto[3], $Pluto[7], $Pluto[4], $Pluto[2]);

    int $index = (int)(currentTime -q-1);
    /*
    float $Rmerc[10][3];
    float $Rmerc[$index][0] = $mercury_coordinates.x;
    float $Rmerc[$index][1] = $mercury_coordinates.y;
    float $Rmerc[$index][2] = $mercury_coordinates.z;
    */

    setAttr("mercury.translateX", $mercury_coordinates.x);
    setAttr("mercury.translateY", $mercury_coordinates.y);
    setAttr("mercury.translateZ", $mercury_coordinates.z);

    setAttr("venus.translateX", $venus_coordinates.x);
    setAttr("venus.translateY", $venus_coordinates.y);
    setAttr("venus.translateZ", $venus_coordinates.z);

    setAttr("earth.translateX", $earth_coordinates.x);
    setAttr("earth.translateY", $earth_coordinates.y);
    setAttr("earth.translateZ", $earth_coordinates.z);

    setAttr("mars.translateX", $mars_coordinates.x);
    setAttr("mars.translateY", $mars_coordinates.y);
    setAttr("mars.translateZ", $mars_coordinates.z);

    setAttr("jupiter.translateX", $jupiter_coordinates.x);
    setAttr("jupiter.translateY", $jupiter_coordinates.y);
    setAttr("jupiter.translateZ", $jupiter_coordinates.z);

    setAttr("saturn.translateX", $saturn_coordinates.x);
    setAttr("saturn.translateY", $saturn_coordinates.y);
    setAttr("saturn.translateZ", $saturn_coordinates.z);

    setAttr("uranus.translateX", $uranus_coordinates.x);
    setAttr("uranus.translateY", $uranus_coordinates.y);
    setAttr("uranus.translateZ", $uranus_coordinates.z);

    setAttr("neptune.translateX", $neptune_coordinates.x);
    setAttr("neptune.translateY", $neptune_coordinates.y);
    setAttr("neptune.translateZ", $neptune_coordinates.z);

    setAttr("pluto.translateX", $pluto_coordinates.x);
    setAttr("pluto.translateY", $pluto_coordinates.y);
    setAttr("pluto.translateZ", $pluto_coordinates.z);

Wich is essentially a loop, its just written out, becase you lack programming habit
means you end up with lot more code to keep in check once you decide to change
something.

QUOTE
Alone for this reason a float[72] would be much more work and less overview.

Depends on how you WRITE it. Certainly if you use syntax like yours but if you do something like this:

float $something=
{//list of data for a lot of things
//mercury
data,
data,
data,
//Venus
data,
data
data
...
}

is no harder to read than having your long case lists. Its just shorter and traverses between apps easier too.

However all thsi a bit semantic as you wish to do one shard and put every piece with a loop into a expression.
Because thats what mayas is designed for.

Rest assured i do undertsnfd your code.

Hi,

I haven't been tyding up nothing, nor have I tried to make the updates work in the expression editor only. It's off limits, I got what I need and I dont have the time to make it "better" - it already works to my satisfaction... sorry. Instead I found a significant and "boo" typo in the script, making the orbis somehow distorted...

Its not:

CODE
global proc float heliocentric_radius( float $a, float $e, float $V)
{
    float $HelioRadius;
    $HelioRadius = ($a*(1-($e + $e))) / (1 + $e * cos($V));
    return $HelioRadius;
}

but

CODE
global proc float heliocentric_radius( float $a, float $e, float $V)
{
    float $HelioRadius;
    $HelioRadius = ($a*(1-($e * $e))) / (1 + $e * cos($V));
    return $HelioRadius;
}

so here is the full and working code... (maybe not really short or elegant, but doing the job)
CODE
Original source code by Stephen R. Schmitt written in zeno. Ported to mel and c++ by Tobias Houfek.
http://home.att.net/~srschmitt/planetorbits.html
//usage:
//1. source the script
//2. Call newSolarSystem();
//3. Create the following runtime expression on the sun
//   or any other object using the expression editor:
//        
//          string $update = updateSolarSystem(2000, 1, 1, 12, 0, 3, 1);
//        eval($update);
//
// The first five arguments are: year, month, day, hour, mi