Help "translating" a python script

About advanced scenes, and the Thyme scripting language used in Algodoo.

Help "translating" a python script

Postby mitbrown » Tue Mar 02, 2010 6:20 am

I think that this can be re-written in thyme. I spent a few hours on it (hopelessly), then decided I should ask for help.
EDIT: i'm using 'scene.my.wavelength' and 'scene.my.red' etc... (idk if this is the best way)
Code: Select all
    if (Wavelength >= 380.0) and (Wavelength < 440.0):
        Red   = -(Wavelength - 440.) / (440. - 380.)
        Green = 0.0
        Blue  = 1.0
       
    elif (Wavelength >= 440.0) and (Wavelength < 490.0):
        Red   = 0.0
        Green = (Wavelength - 440.) / (490. - 440.)
        Blue  = 1.0

    elif (Wavelength >= 490.0) and (Wavelength < 510.0):
        Red   = 0.0
        Green = 1.0
        Blue  = -(Wavelength - 510.) / (510. - 490.)

    elif (Wavelength >= 510.0) and (Wavelength < 580.0):
        Red   = (Wavelength - 510.) / (580. - 510.)
        Green = 1.0
        Blue  = 0.0

    elif (Wavelength >= 580.0) and (Wavelength < 645.0):
        Red   = 1.0
        Green = -(Wavelength - 645.) / (645. - 580.)
        Blue  = 0.0

    elif (Wavelength >= 645.0) and (Wavelength <= 780.0):
        Red   = 1.0
        Green = 0.0
        Blue  = 0.0

    else:
        Red   = 0.0
        Green = 0.0
        Blue  = 0.0

Here http://www.johnny-lin.com/py_code/wavelen2rgb.py is thepython script from which I borrowed the code
mitbrown
 
Posts: 12
Joined: Sun Jan 31, 2010 11:08 pm

Re: Help "translating" a python script

Postby Dakta » Tue Mar 02, 2010 6:46 am

Well, first you'll need to be converting your if statements to ternary conditionals.

Read the Thyme tutorial with Zexamples at every step by Gradyfitz. Then try again.


To turn your gigantic if/elseif statement into a ternary conditional, you'll need to have your brackets nested perfectly.

HEY EMIL! This is a place where REAL if/else/elseif statements would be an insanely big improvement over your use of the ternary conditional operator. Why you chose to use a ternary conditional instead of real if statements is beyond me, but they can be quite a pain in the ass.


Let me give this a try:

Code: Select all

//Thyme version: Indented for clarity, not all variables correct.


Scene.my.checkWavelength = (Scene.my.wavelength)=>{Scene.my.wavelength >= 380} ? {
   Scene.my.red = -(Scene.my.wavelength - 440.0)/(440.0-380.0);
   Scene.my.green = 0.0;
   Scene.my.blue = 1.0;
} : {(Scene.my.wavelength)=>{Scene.my.wavelength >= 380} ? {
      Scene.my.red = -(Scene.my.wavelength - 440.0)/(440.0-380.0);
      Scene.my.green = 0.0;
      Scene.my.blue = 1.0;
   } : {(Scene.my.wavelength)=>{Scene.my.wavelength >= 380} ? {
         Scene.my.red = -(Scene.my.wavelength - 440.0)/(440.0-380.0);
         Scene.my.green = 0.0;
         Scene.my.blue = 1.0;
      } : {(Scene.my.wavelength)=>{Scene.my.wavelength >= 380} ? {
            Scene.my.red = -(Scene.my.wavelength - 440.0)/(440.0-380.0);
            Scene.my.green = 0.0;
            Scene.my.blue = 1.0;
         } : {(Scene.my.wavelength)=>{Scene.my.wavelength >= 380} ? {
               Scene.my.red = -(Scene.my.wavelength - 440.0)/(440.0-380.0);
               Scene.my.green = 0.0;
               Scene.my.blue = 1.0;
            } : {(Scene.my.wavelength)=>{Scene.my.wavelength >= 380} ? {
                  Scene.my.red = -(Scene.my.wavelength - 440.0)/(440.0-380.0);
                  Scene.my.green = 0.0;
                  Scene.my.blue = 1.0;
               } : {
                     Scene.my.red = -(Scene.my.wavelength - 440.0)/(440.0-380.0);
                     Scene.my.green = 0.0;
                     Scene.my.blue = 1.0;
                  }
               }
            }
         }
      }
   }



All you need to do is go through all those conditions and actions to what they rightly should be. I didn't have enough time to do it all for you.

I hope that works, and I hope it makes sense :P I haven't tested it yet, but it should work :D

Happy coding!

-- Dakota
.. ,__,_____
. / __.==--" - - - - - - - - ""
./#(-'
.`-' From http://www.ascii-art.de/. Modded by me to work in Arial. Image
a Mammoth wrote:be boring and interesting.

Mystery wrote:If you were jailbreaker you shouldn't have when't up the 3.1.3
I didn't know you could go up 3.1.3! Thanks Mystery person!
User avatar
Dakta
 
Posts: 417
Joined: Sat Sep 12, 2009 4:36 pm

Re: Help "translating" a python script

Postby mitbrown » Tue Mar 02, 2010 8:04 am

It is amazing that you can work so fast thank you! :mrgreen: . unfortunately I can't get it to work. here's what i entered, maybe i skipped a line or something. I probably made a mistake, but I have no idea :shock:. this is similar to my attempt but beyond my comprehension. (and way longer than i thought it would be)
Code: Select all
Scene.my.checkWavelength = (Scene.my.wavelength)=>{Scene.my.wavelength >= 380} ? {    Scene.my.red = -(Scene.my.wavelength - 440.0)/(440.0-380.0);   Scene.my.green = 0.0;   Scene.my.blue = 1.0;} : {(Scene.my.wavelength)=>{Scene.my.wavelength >= 380} ? {      Scene.my.red = -(Scene.my.wavelength - 440.0)/(440.0-380.0);      Scene.my.green = 0.0;      Scene.my.blue = 1.0;   } : {(Scene.my.wavelength)=>{Scene.my.wavelength >= 380} ? {      Scene.my.red = -(Scene.my.wavelength - 440.0)/(440.0-380.0);      Scene.my.green = 0.0;      Scene.my.blue = 1.0;   } : {(Scene.my.wavelength)=>{Scene.my.wavelength >= 380} ? {         Scene.my.red = -(Scene.my.wavelength - 440.0)/(440.0-380.0);         Scene.my.green = 0.0;         Scene.my.blue = 1.0;      } : {(Scene.my.wavelength)=>{Scene.my.wavelength >= 380} ? {            Scene.my.red = -(Scene.my.wavelength - 440.0)/(440.0-380.0);           Scene.my.green = 0.0;            Scene.my.blue = 1.0;         } : {(Scene.my.wavelength)=>{Scene.my.wavelength >= 380} ? {               Scene.my.red = -(Scene.my.wavelength - 440.0)/(440.0-380.0);               Scene.my.green = 0.0;             Scene.my.blue = 1.0;        } : {(Scene.my.wavelength)=>{Scene.my.wavelength >= 380} ? {               Scene.my.red = -(Scene.my.wavelength - 440.0)/(440.0-380.0);     Scene.my.green = 0.0;   Scene.my.blue = 1.0;         } : {        Scene.my.red = -(Scene.my.wavelength - 440.0)/(440.0-380.0);           Scene.my.green = 0.0;              Scene.my.blue = 1.0;     }        }          }     } }  }


Hey is there an easier way to enter this, instead of copy and pasting one line at a time into the console? idk why i can't paste more than one line into the console

EDIT(03/02/2010): just realized that none of the numbers are correct here.
Last edited by mitbrown on Wed Mar 03, 2010 4:53 am, edited 2 times in total.
mitbrown
 
Posts: 12
Joined: Sun Jan 31, 2010 11:08 pm

Re: Help "translating" a python script

Postby Dakta » Tue Mar 02, 2010 7:44 pm

Well, i'll tell you what. I'm at school right now, no access to Algodoo/Phun, but when I get home I'll finish transferring the code over into Thyme then I'll create a simple working scene.

The best thing you can do is the edit the .phz file and go into the .phn file and copy my code into the .phn file.

That way you can get everything right easier.

The problem here with Emil's use of the ternary conditional operator instead of if/elseif statements is that the nesting of the brackets is insanely confusing. I use a code editor that highlights the brackets when I pass over them, that way I can make sure I don't mess up the nesting.

I'll be home in like 6 hours, but I've got an insane amount of school work to do so it might be more like 10 hours before I can get it done. Until then!
.. ,__,_____
. / __.==--" - - - - - - - - ""
./#(-'
.`-' From http://www.ascii-art.de/. Modded by me to work in Arial. Image
a Mammoth wrote:be boring and interesting.

Mystery wrote:If you were jailbreaker you shouldn't have when't up the 3.1.3
I didn't know you could go up 3.1.3! Thanks Mystery person!
User avatar
Dakta
 
Posts: 417
Joined: Sat Sep 12, 2009 4:36 pm

Re: Help "translating" a python script

Postby standardtoaster » Tue Mar 02, 2010 8:21 pm

@mitbrown: Yes, there is an easier way than copying one line at a time. Go to here and remove the line breaks then copy the new text into Algodoo.

I'll take a more in depth look at this later.
User avatar
standardtoaster
 
Posts: 606
Joined: Mon Aug 31, 2009 7:57 pm

Re: Help "translating" a python script

Postby Dakta » Tue Mar 02, 2010 9:09 pm

Well, you don't need to remove the line breaks if you put it in the .phn Mr. Toaster!

I grabbed a copy of the latest Algodoo, and man it rocks. I'll be trying to figure this out during class/lunch, maybe I'll be back to you about it sooner.

Well, after a little bit of tweaking, the only thing I'm missing is what the laser color is. If anyone knows what that is, please speak up!

Code: Select all
// Algodoo scene created by Algodoo v1.6.3

FileInfo -> {
    title = "box and plane";
    author = "Dakota Schneider";
    version = 7
};
Sim -> {
    gravityAngleOffset = 0.0;
    gravityStrength = 9.8;
    gravitySwitch = true;
    airSwitch = true;
    airFrictionMultiplier = 1.0;
    airFrictionLinear = 0.01;
    airFrictionQuadratic = 0.0;
    rotFrictionLinear = 0.00314;
    airDensity = 0.01;
    timeFactor = 1.0
};
App -> {
    laserEvents = true;
    numColorsInRainbow = 12;
    laserSuperBoost = 1.0;
    laserResolution = 10.0;
    waterColor = [0.1, 0.1, 1.0, 0.7];
    fadeColor = [0.0, 0.0, 0.0]
};
App.GUI -> {drawHingesWhenRunning = true};
Scene -> {gravityRotationOffset = NaN};
SPH -> {
    vaporizeTime = +inf;
    viscosity = 0.0
};
App.Grid -> {
    grid = true;
    base = 2;
    numAxes = 2;
    scale = 2.0;
    snap = true
};
App.GUI.Forces -> {
    normalText = "N";
    totalText = "";
    airFrictionText = "-fv";
    attraction = true;
    gravityText = "mg";
    airBuoyancyText = "-adg";
    airFriction = true;
    airBuoyancy = true;
    total = false;
    attractionText = "A";
    externalText = "ext";
    friction = true;
    drawValues = false;
    drawNames = true;
    external = true;
    normal = true;
    velScale = 1.0;
    frictionText = "T";
    springText = "-kx-bv";
    forceScale = 1.0;
    drawForces = false;
    gravity = true;
    contactCombinationDistance = 1.2;
    spring = true;
    velocities = false;
    frictionProjection = true;
    hingeText = "H";
    hinge = true
};
Scene.Camera -> {
    pan = [0.5639631, -0.37028706];
    rotation = 0.0;
    trackRotation = false;
    zoom = 74.999985
};
App.Background -> {
    cloudOpacity = 0.6;
    drawClouds = true;
    cuteClouds = true;
    skyColor = [0.45, 0.55, 1.0, 1.0]
};
Scene.addPlane {
    attraction = 0.0;
    onCollide = (e)=>{};
    opaqueBorders = true;
    textureMatrix = [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0];
    onHitByLaser = (e)=>{};
    immortal = true;
    collideWater = true;
    texture = "";
    restitution = 0.5;
    friction = 0.5;
    color = [0.1, 0.1, 0.1, 1.0];
    drawBorder = true;
    heteroCollide = false;
    collideSet = 127;
    materialVelocity = 0.0;
    refractiveIndex = 1.5;
    killer = true;
    entityID = 45;
    zDepth = 0.0;
    geomID = 47;
    pos = [-1000.0, -0.0];
    angle = 0.0;
    body = 0
};
Scene.addPlane {
    attraction = 0.0;
    onCollide = (e)=>{};
    opaqueBorders = true;
    textureMatrix = [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0];
    onHitByLaser = (e)=>{};
    immortal = true;
    collideWater = true;
    texture = "";
    restitution = 0.5;
    friction = 0.5;
    color = [0.1, 0.1, 0.1, 1.0];
    drawBorder = true;
    heteroCollide = false;
    collideSet = 127;
    materialVelocity = 0.0;
    refractiveIndex = 1.5;
    killer = true;
    entityID = 46;
    zDepth = 1.0;
    geomID = 48;
    pos = [-0.0, -1000.0];
    angle = 1.5707964;
    body = 0
};
Scene.addPlane {
    attraction = 0.0;
    onCollide = (e)=>{};
    opaqueBorders = true;
    textureMatrix = [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0];
    onHitByLaser = (e)=>{};
    immortal = true;
    collideWater = true;
    texture = "";
    restitution = 0.5;
    friction = 0.5;
    color = [0.1, 0.1, 0.1, 1.0];
    drawBorder = true;
    heteroCollide = false;
    collideSet = 127;
    materialVelocity = 0.0;
    refractiveIndex = 1.5;
    killer = true;
    entityID = 47;
    zDepth = 2.0;
    geomID = 49;
    pos = [1000.0, -0.0];
    angle = 3.1415927;
    body = 0
};
Scene.addPlane {
    attraction = 0.0;
    onCollide = (e)=>{};
    opaqueBorders = true;
    textureMatrix = [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0];
    onHitByLaser = (e)=>{};
    immortal = true;
    collideWater = true;
    texture = "";
    restitution = 0.5;
    friction = 0.5;
    color = [0.1, 0.1, 0.1, 1.0];
    drawBorder = true;
    heteroCollide = false;
    collideSet = 127;
    materialVelocity = 0.0;
    refractiveIndex = 1.5;
    killer = true;
    entityID = 48;
    zDepth = 3.0;
    geomID = 50;
    pos = [-0.0, 1000.0];
    angle = -1.5707964;
    body = 0
};
Scene.addPlane {
    attraction = 0.0;
    onCollide = (e)=>{};
    opaqueBorders = true;
    textureMatrix = [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0];
    onHitByLaser = (e)=>{};
    immortal = false;
    collideWater = true;
    texture = "";
    restitution = 0.5;
    friction = 0.5;
    color = [0.17016017, 0.5670648, 0.94445074, 1.0];
    drawBorder = true;
    heteroCollide = false;
    collideSet = 127;
    materialVelocity = 0.0;
    refractiveIndex = 1.5;
    killer = false;
    entityID = 69;
    zDepth = 4.0;
    geomID = 72;
    pos = [-0.0, -2.0];
    angle = 1.5707964;
    body = 0
};
Scene.addBox {
    collideWater = true;
    color = [0.09069815, 0.6413019, 0.57843214, 1.0];
    onCollide = (e)=>{Scene.my.check(480)};
    attraction = 0.0;
    controllerAcc = 11.0;
    onHitByLaser = (e)=>{};   
//    onHitByLaser = (e)=>{Scene.my.wavelength = getthelasercolor; Scene.my.checkWavelength(Scene.my.wavelength);};
    heteroCollide = false;
    immortal = false;
    opaqueBorders = true;
    refractiveIndex = 1.5;
    killer = false;
    text = "";
    friction = 0.5;
    restitution = 0.5;
    drawBorder = true;
    controllerInvertX = false;
    controllerInvertY = false;
    controllerReverseXY = false;
    ruler = false;
    airFrictionMult = 1.0;
    showVelocity = false;
    density = 2.0;
    textColor = [1.0, 1.0, 1.0, 1.0];
    textureMatrix = [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0];
    texture = "";
    inertiaMultiplier = 1.0;
    showForceArrows = false;
    collideSet = 1;
    materialVelocity = 0.0;
    textScale = 0.1;
    entityID = 70;
    zDepth = 5.0;
    geomID = 74;
    pos = [0.0, 0.0];
    angle = 0.0;
    size = [4.0, 2.0]
};
Scene.addGroup {name = "selected"};
Scene.addWidget {
    widgetID = "ScriptMenu";
    entityIDs = [70];
    minimized = false;
    pos = [851.0, 1.0];
    size = [420.0, 800.0];
    title = "Script menu for box";
    visible = true
};

//Thyme version: Indented for clarity, not all variables correct.

Scene.my.wavelength = 0.0;

Scene.my.red = 0.0;
Scene.my.blue = 0.0;
Scene.my.green = 0.0;

Scene.my.check = (argument)=>{{argument >= 380} ? {
   Scene.my.red = -(argument - 440.0)/(440.0-380.0);
   Scene.my.green = 0.0;
   Scene.my.blue = 1.0;
} : {(argument)=>{argument >= 380} ? {
      Scene.my.red = -(argument - 440.0)/(440.0-380.0);
      Scene.my.green = 0.0;
      Scene.my.blue = 1.0;
   } : {(argument)=>{argument >= 380} ? {
         Scene.my.red = -(argument - 440.0)/(440.0-380.0);
         Scene.my.green = 0.0;
         Scene.my.blue = 1.0;
      } : {(argument)=>{argument >= 380} ? {
            Scene.my.red = -(argument - 440.0)/(440.0-380.0);
            Scene.my.green = 0.0;
            Scene.my.blue = 1.0;
         } : {(argument)=>{argument >= 380} ? {
               Scene.my.red = -(argument - 440.0)/(440.0-380.0);
               Scene.my.green = 0.0;
               Scene.my.blue = 1.0;
            } : {(argument)=>{argument >= 380} ? {
                  Scene.my.red = -(argument - 440.0)/(440.0-380.0);
                  Scene.my.green = 0.0;
                  Scene.my.blue = 1.0;
               } : {
                     Scene.my.red = -(argument - 440.0)/(440.0-380.0);
                     Scene.my.green = 0.0;
                     Scene.my.blue = 1.0;
                  }
               }
            }
         }
      }
   }
};


The above scene works in the latest release of Algodoo. Copy it to a text file, then save it as whatever.phn. Open whatever.phn with Algodoo and check everything out.
.. ,__,_____
. / __.==--" - - - - - - - - ""
./#(-'
.`-' From http://www.ascii-art.de/. Modded by me to work in Arial. Image
a Mammoth wrote:be boring and interesting.

Mystery wrote:If you were jailbreaker you shouldn't have when't up the 3.1.3
I didn't know you could go up 3.1.3! Thanks Mystery person!
User avatar
Dakta
 
Posts: 417
Joined: Sat Sep 12, 2009 4:36 pm

Re: Help "translating" a python script

Postby daniels220 » Tue Mar 02, 2010 11:47 pm

Wait, 1.6.3? Where'd you get that? The download page still says 1.6.0.
daniels220
 
Posts: 95
Joined: Mon Aug 31, 2009 11:30 pm

Re: Help "translating" a python script

Postby KarateBrot » Wed Mar 03, 2010 12:05 am

Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Help "translating" a python script

Postby mitbrown » Wed Mar 03, 2010 2:35 am

"Well, after a little bit of tweaking, the only thing I'm missing is what the laser color is. If anyone knows what that is, please speak up!"

The script is to calculate the laser's color based on the wavelength. not the other way around :thumbup:

Thank you guys for the help I'll update this post or something to let you know after I test it out : :D
mitbrown
 
Posts: 12
Joined: Sun Jan 31, 2010 11:08 pm

Re: Help "translating" a python script

Postby Dakta » Wed Mar 03, 2010 4:00 am

Well, all you have to do is fix the color stuff (they are all the same) and then call the function from a collision, key press, or the console.

Scene.my.check(wavelength measurement goes here) using my above scene file will work.
.. ,__,_____
. / __.==--" - - - - - - - - ""
./#(-'
.`-' From http://www.ascii-art.de/. Modded by me to work in Arial. Image
a Mammoth wrote:be boring and interesting.

Mystery wrote:If you were jailbreaker you shouldn't have when't up the 3.1.3
I didn't know you could go up 3.1.3! Thanks Mystery person!
User avatar
Dakta
 
Posts: 417
Joined: Sat Sep 12, 2009 4:36 pm

Re: Help "translating" a python script

Postby KarateBrot » Wed Mar 03, 2010 4:13 am

oh okay so you want to convert wavelengths to a laser color. that could have been done much easier. you don't need so much if functions. there's a function in thyme called "math.hsv2rgb()" that's all you need. you just have to make two if statements for the frequency limits.
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Help "translating" a python script

Postby mitbrown » Wed Mar 03, 2010 4:37 am

well that would 'work' but this is actually a detailed function based on actual human perception of light.

I just realized this -the numbers being all the same is because i copied that from the code that Dakta posted. the original had all the real numbers in it. here's the whole code
Code: Select all
   

def wavelen2rgb(Wavelength, MaxIntensity=100):

// def Adjust_and_Scale(Color, Factor, Highest=100):

//        Gamma = 0.80

//        if Color == 0.0:
//            result = 0
//         else:
//            result = int( round(pow(Color * Factor, Gamma) * round(Highest)) )
//           if result < 0:        result = 0
//            if result > Highest:  result = Highest

//        return result

    if (Wavelength >= 380.0) and (Wavelength < 440.0):
        Red   = -(Wavelength - 440.) / (440. - 380.)
        Green = 0.0
        Blue  = 1.0
       
    elif (Wavelength >= 440.0) and (Wavelength < 490.0):
        Red   = 0.0
        Green = (Wavelength - 440.) / (490. - 440.)
        Blue  = 1.0

    elif (Wavelength >= 490.0) and (Wavelength < 510.0):
        Red   = 0.0
        Green = 1.0
        Blue  = -(Wavelength - 510.) / (510. - 490.)

    elif (Wavelength >= 510.0) and (Wavelength < 580.0):
        Red   = (Wavelength - 510.) / (580. - 510.)
        Green = 1.0
        Blue  = 0.0

    elif (Wavelength >= 580.0) and (Wavelength < 645.0):
        Red   = 1.0
        Green = -(Wavelength - 645.) / (645. - 580.)
        Blue  = 0.0

    elif (Wavelength >= 645.0) and (Wavelength <= 780.0):
        Red   = 1.0
        Green = 0.0
        Blue  = 0.0

    else:
        Red   = 0.0
        Green = 0.0
        Blue  = 0.0

//    if (Wavelength >= 380.0) and (Wavelength < 420.0):
//        Factor = 0.3 + 0.7*(Wavelength - 380.) / (420. - 380.)
//    elif (Wavelength >= 420.0) and (Wavelength < 701.0):
//        Factor = 1.0
//    elif (Wavelength >= 701.0) and (Wavelength <= 780.0):
//        Factor = 0.3 + 0.7*(780. - Wavelength) / (780. - 700.)
//    else:
//        Factor = 0.0


//    R = Adjust_and_Scale(Red,   Factor, MaxIntensity)
//    G = Adjust_and_Scale(Green, Factor, MaxIntensity)
//    B = Adjust_and_Scale(Blue,  Factor, MaxIntensity)

//    return [R, G, B]




edit: the parts that I commented out I don't need in thyme, unless(for some reason) they are necessary for the code to work
EDIT AGAIN: OOPS! I missed a bunch of the parts I mean to comment out
Last edited by mitbrown on Wed Mar 03, 2010 5:09 am, edited 1 time in total.
mitbrown
 
Posts: 12
Joined: Sun Jan 31, 2010 11:08 pm

Re: Help "translating" a python script

Postby Dakta » Wed Mar 03, 2010 4:55 am

Oh, now you tell us about all the rest of of :P

I don't know enough Python, or Thyme for that matter, to translate that whole thing, but I think you can get the idea from what I did.
.. ,__,_____
. / __.==--" - - - - - - - - ""
./#(-'
.`-' From http://www.ascii-art.de/. Modded by me to work in Arial. Image
a Mammoth wrote:be boring and interesting.

Mystery wrote:If you were jailbreaker you shouldn't have when't up the 3.1.3
I didn't know you could go up 3.1.3! Thanks Mystery person!
User avatar
Dakta
 
Posts: 417
Joined: Sat Sep 12, 2009 4:36 pm

Re: Help "translating" a python script

Postby mitbrown » Wed Mar 03, 2010 5:05 am

I provided a link to the entire thing in the first post :angel: it has lots of comments that would explain how this code is supposed to work. i guess i should have posted the whole thing without trimming out the comments in the first place... so I input the correct numbers in the first code you gave me, remove the line breaks and I can paste it into the console and the function will operate, right? i'll try, but I'm a total noobie :?
mitbrown
 
Posts: 12
Joined: Sun Jan 31, 2010 11:08 pm

oops

Postby mitbrown » Wed Mar 03, 2010 5:27 am

sorry, take another look at my post above cause I missed most of the stuff that I wanted to be commented out.
There shouldn't have been anything additional. The function that controls the scaling factor for visibility, I plan to add later using a different method. The 6 ranges, and the else at the end (for wavelengths out of range) is all I need.
mitbrown
 
Posts: 12
Joined: Sun Jan 31, 2010 11:08 pm

Re: Help "translating" a python script

Postby Dakta » Wed Mar 03, 2010 7:45 pm

Like I said, I write all of my functions into the .phn and work in the .phn until it all works right then I package into .phz That way you can add the line breaks. You can have line breaks in Thyme, but you can't enter them in the console.

I highly suggest writing it out into the .phn like I do, that way you can have the line breaks and understand your code better.
.. ,__,_____
. / __.==--" - - - - - - - - ""
./#(-'
.`-' From http://www.ascii-art.de/. Modded by me to work in Arial. Image
a Mammoth wrote:be boring and interesting.

Mystery wrote:If you were jailbreaker you shouldn't have when't up the 3.1.3
I didn't know you could go up 3.1.3! Thanks Mystery person!
User avatar
Dakta
 
Posts: 417
Joined: Sat Sep 12, 2009 4:36 pm

Re: Help "translating" a python script

Postby daniels220 » Wed Mar 03, 2010 8:34 pm

@KarateBrot—Awesome, thanks. Downloading...
daniels220
 
Posts: 95
Joined: Mon Aug 31, 2009 11:30 pm

Re: Help "translating" a python script

Postby mitbrown » Thu Mar 04, 2010 12:39 am

Hey Dakta I finally have all the numbers where they belong (I think)
- just one more thing -
what are the possible ways to update it? the only way I have been able to update it so far is by pasting the code in the console again. It'd be cool to have it constantly updating, but not a necessity. :D

here's what it looks like right now:

Code: Select all
Scene.my.checkWavelength = (Scene.my.wavelength >= 380.0) && (Scene.my.wavelength < 440.0) ? {
   Scene.my.red = -(Scene.my.wavelength - 440.0)/(440.0-380.0);
   Scene.my.green = 0.0;
   Scene.my.blue = 1.0;
} : {(Scene.my.wavelength >= 440.0) && (Scene.my.wavelength < 490.0) ? {
      Scene.my.red = 0.0;
      Scene.my.green = (Scene.my.wavelength - 440.0)/(490.0-440.0);
      Scene.my.blue = 1.0;
   } : {(Scene.my.wavelength >= 490.0) && (Scene.my.wavelength < 510.0) ? {
         Scene.my.red = 0.0;
         Scene.my.green = 0.0;
         Scene.my.blue = (Scene.my.wavelength - 510.0)/(510.0-490.0);
      } : {(Scene.my.wavelength >= 510.0) && (Scene.my.wavelength < 580.0) ? {
            Scene.my.red = (Scene.my.wavelength - 510.0)/(580.0-510.0);
            Scene.my.green = 1.0;
            Scene.my.blue = 0.0;
         } : {(Scene.my.wavelength >= 580.0) && (Scene.my.wavelength < 645.0) ? {
               Scene.my.red = 1.0;
               Scene.my.green = -(Scene.my.wavelength - 645.0)/(645.0-580.0);
               Scene.my.blue = 0.0;
            } : {(Scene.my.wavelength >= 645.0) && (Scene.my.wavelength <= 780.0) ? {
                  Scene.my.red = 1.0;
                  Scene.my.green = 0.0;
                  Scene.my.blue = 0.0;
               } : {
                     Scene.my.red = 0.0;
                     Scene.my.green = 0.0;
                     Scene.my.blue = 0.0;
                  }
               }
            }
         }
      }
   }

oh and will this serialize?
mitbrown
 
Posts: 12
Joined: Sun Jan 31, 2010 11:08 pm

Re: Help "translating" a python script

Postby Versieon » Thu Mar 04, 2010 12:44 am

To update continuosly, put it in the oncolide script of a object, that way it will update when ever the object collides with something. Search for colliders if you don't know how to make one.
User avatar
Versieon
 
Posts: 375
Joined: Tue Sep 01, 2009 4:45 pm

Re: Help "translating" a python script

Postby KarateBrot » Thu Mar 04, 2010 1:50 am

Or just put everything between { } brackets and it will automatically update all the time without the need of something else.

little example:
Code: Select all
scene.my.diff = { scene.my.x - scene.my.y }

so scene.my.diff is always the difference of scene.my.x and scene.my.y because it updates automatically.
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Help "translating" a python script

Postby standardtoaster » Thu Mar 04, 2010 2:59 am

If you want to see the entire function so that you don't need to retype it every time you want to edit it. Make a box, go to its text box in the script menu, and type {""+scene.my.checkWaveLength}. Then, go to the text box in the appearance menu and c/p what's in there to the console.
User avatar
standardtoaster
 
Posts: 606
Joined: Mon Aug 31, 2009 7:57 pm

Re: Help "translating" a python script

Postby Dakta » Thu Mar 04, 2010 5:15 pm

Basically you need to have something call the function every frame or so, right?

The best way to do this is to put a box on a hinge and make its density very low. Then make another box or circle so that when the box spins it will collide with this other object. Then set the rotation speed on the box to something pretty high, and the strength to +inf. Go to the script menu from either object you just made (I would use the fixed circle) and in the onCollide box type: (e)=>{Scene.my.checkWavelength} This will call the function every time the object hits something, and since it's spinning very fast it should hit the other small object every frame or two, which will call your function and update the stuff.

Speaking of which, Algodoo needs a functionUpdate() command, like those seen in game engines. That way the functionUpdate() is called every frame by Algodoo and will evaluate everything in it every frame, which would eliminate stupid spinning onCollide things.
.. ,__,_____
. / __.==--" - - - - - - - - ""
./#(-'
.`-' From http://www.ascii-art.de/. Modded by me to work in Arial. Image
a Mammoth wrote:be boring and interesting.

Mystery wrote:If you were jailbreaker you shouldn't have when't up the 3.1.3
I didn't know you could go up 3.1.3! Thanks Mystery person!
User avatar
Dakta
 
Posts: 417
Joined: Sat Sep 12, 2009 4:36 pm

Re: Help "translating" a python script

Postby mitbrown » Fri Mar 05, 2010 9:26 am

I have one more function that I'm trying to add, I just can't get it to work :?:
I've been searching google for the better part of the last two days trying to figure out how to write a function for this set of data points:
(wavelength, luminous efficiency)
380, 0.0002000
400, 0.0028000
420, 0.0175000
440, 0.0379000
460, 0.0600000
480, 0.1390000
500, 0.3230000
520, 0.7100000
540, 0.9540000
560, 0.9950000
580, 0.8700000
600, 0.6310000
620, 0.3810000
640, 0.1750000
660, 0.0610000
680, 0.0170000
700, 0.0041000
720, 0.0010500
740, 0.0002490
760, 0.0000600
780, 0.0000150

"curve fitting" appears to be what I'm looking for, but none of the methods I've tried gave a result that I could put into thyme.
mitbrown
 
Posts: 12
Joined: Sun Jan 31, 2010 11:08 pm

Re: Help "translating" a python script

Postby KarateBrot » Fri Mar 05, 2010 3:51 pm

What you need is an algorithm to find a regression curve. It's not very simple. I can try to make an algorithm in thyme but then you have to wait 3 weeks because it takes a bit of time and at the moment I have to learn for my graduation exams.
Maybe the error of your resulting regression curve is too big so you have to make something different. Then you also can try to interpolate your data points. A few important headwords would be linear interpolation, Spline-Interpolation
Another tip would be to try to find a regression curve like f(x) = a^(-(x-x0)²/b).

- - - - -

a: A coefficient you have to find out
b: Another coefficient
x0: the x value of the highest point in the function ( don't know how to call it in english)

- - - - -

f(x) = e^(-(x-557)²/3500)
Maybe this function is good for you. It's not the perfect curve because I just figured the function out by eye (otherwise it would have taken too much time). It should be pretty close to the best solution.
Image

- - - - -

Better take this function:
f(x) = e^(-(x-559)²/3500)
It fits even better.
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 6 guests