Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Elegant Expresion Writing

  • Elegant Expresion Writing

    Posted by Eden Exposito on December 3, 2012 at 5:55 pm

    Hi!

    I have an expresion with a lot of conditionals, I’m asking if i can “clean” the code instead of use a lot of “if else”.

    Thanks

    /* ----------- OSCILACION ----------- */
    /////////// EDEN COMENTA: ///////////
    /*Esta expresión crea una función de oscilacion automática en función del tiempo.*/
    /////////// VARIABLES ///////////
    //Eje X
    invertir_x=effect("Oscilador 2D")("Invertir Fase Posicion X").value ;
    frecuencia_x=effect("Oscilador 2D")("Frecuencia Posicion X");
    amplitud_x=effect("Oscilador 2D")("Amplitud Posicion X");
    multiplicador_x=effect("Oscilador 2D")("Multiplicador Posicion X");
    rozamiento_x=effect("Oscilador 2D")("Rozamiento Posicion X");
    desfase_x=effect("Oscilador 2D")("Desfase Posicion X");
    //Eje Y
    invertir_y=effect("Oscilador 2D")("Invertir Fase Posicion Y").value;
    frecuencia_y=effect("Oscilador 2D")("Frecuencia Posicion Y");
    amplitud_y=effect("Oscilador 2D")("Amplitud Posicion Y");
    multiplicador_y=effect("Oscilador 2D")("Multiplicador Posicion Y");
    rozamiento_y=effect("Oscilador 2D")("Rozamiento Posicion Y");
    desfase_y=effect("Oscilador 2D")("Desfase Posicion Y");
    activar_pulsos_y=effect("Oscilador 2D")("Pulsos Posicion Eje Y").value;
    duracion_pulsos_y=effect("Oscilador 2D")("Duracion Pulsos Posicion Y");
    periodo_pulsos_y=effect("Oscilador 2D")("Periodo Pulsos Posicion Y")
    t_y=time%(periodo_pulsos_y+duracion_pulsos_y);

    /////////// CODIGO ///////////
    //Eje X
    switch (invertir_x) {
    case 0:
    if (rozamiento_x==0){
    oscilacion_x=multiplicador_x*Math.sin(desfase_x+time*(frecuencia_x))*(amplitud_x);
    }else{
    oscilacion_x=multiplicador_x*Math.sin(desfase_x+time*(frecuencia_x))*(amplitud_x)/Math.exp(rozamiento_x*time);
    }
    break;
    case 1:
    if (rozamiento_x==0){
    oscilacion_x=multiplicador_x*Math.cos(desfase_x+(Math.PI/2)+time*(frecuencia_x))*(amplitud_x);
    }else{
    oscilacion_x=multiplicador_x*Math.cos(desfase_x+(Math.PI/2)+time*(frecuencia_x))*(amplitud_x)/Math.exp(rozamiento_x*time);
    }
    break;
    }
    //Eje Y
    switch (invertir_y) {
    case 0:
    if (activar_pulsos_y==1){
    if (rozamiento_y==0){
    oscilacion_y= (t_y < duracion_pulsos_y) ? multiplicador_y*((amplitud_y)*Math.sin(desfase_y+t_y*(frecuencia_y)*Math.PI*2)) : 0;
    }else{
    oscilacion_y= ease(t_y,0,duracion_pulsos_y,(multiplicador_y*((amplitud_y)*Math.sin(desfase_y+t_y*(frecuencia_y)*Math.PI*2))),0);
    }
    }else{
    if (rozamiento_y==0){
    oscilacion_y= multiplicador_y*Math.sin(desfase_y+time*(frecuencia_y))*(amplitud_y);
    }else{
    oscilacion_y= multiplicador_y*Math.sin(desfase_y+time*(frecuencia_y))*(amplitud_y)/Math.exp(rozamiento_y*time);
    }
    }
    break;
    case 1:
    if (activar_pulsos_y==0){
    if (rozamiento_y==0){
    oscilacion_y= (t_y < duracion_pulsos_y) ? multiplicador_y*((amplitud_y)*Math.cos(desfase_y+(Math.PI/2)+t_y*(frecuencia_y)*Math.PI*2)) : 0;
    }else{
    oscilacion_y= ease(t_y,0,duracion_pulsos_y,(multiplicador_y*((amplitud_y)*Math.cos(desfase_y+(Math.PI/2)+t_y*(frecuencia_y)*Math.PI*2))),0);
    }
    }else{
    if (rozamiento_y==0){
    oscilacion_y= multiplicador_y*Math.cos(desfase_y+(Math.PI/2)+time*(frecuencia_y))*(amplitud_y);
    }else{
    oscilacion_y= multiplicador_y*Math.cos(desfase_y+(Math.PI/2)+time*(frecuencia_y))*(amplitud_y)/Math.exp(rozamiento_y*time);
    }
    }
    break;
    }
    dimension=3;
    try
    {value[2]}
    catch(array)
    {dimension=2;}
    try
    {value[1]}
    catch(array)
    {dimension=1;}
    x=effect("Oscilador 2D")("Activar Posicion Eje X").value;
    y=effect("Oscilador 2D")("Activar Posicion Eje Y").value;
    if (dimension>=2){
    if (x==1) {
    [value[0]+oscilacion_x,value[1]];
    }
    if (y==1){
    [value[0],oscilacion_y+value[1]];
    }
    if (x==1 && y==1){
    [oscilacion_x+value[0],oscilacion_y+value[1]];
    }
    if (x==0 && y==0){
    [value[0],value[1]];
    }
    }
    /* ===================================== */

    Eden Exposito replied 13 years, 5 months ago 2 Members · 2 Replies
  • 2 Replies
  • Xavier Gomez

    December 4, 2012 at 5:41 pm

    1- To start with, you dont need to use “if else” to use or not use decay since the formula that takes decay into account is valid for no decay as well (Math.exp(0) = 1).
    Similarly you dont need to do sin(x) —> cos(x+Pi/2) to get the – sign. Multiply by (-1) directly.
    Also, i dont understand why you need both an amplitude and a multiplicador since they have the same affect…

    2- To shorten the length of the expression and make it easier to read, you could define functions.
    For instance, at the beginning of the expression you define a function

    function oscFunction1(t, multiplicador,desfase, frecuencia, amplitud, rozamiento, invertir)
    {
    var sign = (invertir) ? -1 : +1;
    return(sign * multiplicador * amplitud * Math.sin(desfase+ t *(frecuencia))/Math.exp(rozamiento*t));
    }

    then you can call that function anytime with the appropriate parameters, for instance the first block of your code (Eje X) reads:

    oscilacion_x= oscFunction1(time, multiplicador_x,desfase_x, frecuencia_x, amplitud_x, rozamiento_x,invertir_x);

    You can do similarly for the y oscillations. I didnt look in details but basically you can probably write a single function that works for all cases.

    3- You can also replace “dimension = 3; try… try…” by

    try{dimension = value.length;}
    catch(e){dimension = 1;}

    4- the last part of your code is a bit strange: you ask the expression to calculate oscilacion_x and oscilation_y and at the end you check if you actually need these calculations. For instance if your variables x and y are both 0, then you have calculated lots of things to return… [value[0], value[1]].
    You could do:

    a) estimate the dimension
    b) osc_x = (x==0) ? 0 : oscilacion_x (calculate osc_x only in the second case);
    c) if (dimension >= 2) osc_y = (y==0) ? 0 : oscilacion_y (calculate osc_y only in the second case);
    d) return the result:
    switch (dimension)
    {
    case 1 : value + osc_x; break;
    case 2 : [value[0]+osc_x,value[1]+osc_y]; break;
    default : [value[0]+osc_x,value[1]+osc_y, value[2]];
    }

    Xavier

  • Eden Exposito

    December 5, 2012 at 5:03 pm

    Thanks for your words you had help me a lot!

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy