Jump to content

Put "Effects" in Variables with Loop/Function


tiscavalcanti

Recommended Posts

I need to create a code that take the following strings:
"Scale"
"Position"
"Side"
And put each one, within of a copy of this code:
thisComp.effect(!!!STRING HERE!!!)("Layer").name;
And after that, three variables, each one, receive a complete code.
var a;
var b;
var c;
The result would be this:
var a = thisComp.effect("Scale")("Layer").name;  var b = thisComp.effect("Position")("Layer").name;  var c = thisComp.effect("Side")("Layer").name;  
I try made like this:
var strings = ["Scale", "Position", "Side"];
var val = function (a, b, c){
for (var i=0; i<strings.length; i++){
var effect=thisComp.effect(strings)("Layer").name;
val(effect);
}
}
But no have experience with functions and For Loops.
And no know if this is the correct way of do it.
I need to do this because my code is becoming too long for many repetitions.
I think for that, will need to use for loop, and function.
Thanks.
Edited by tiscavalcanti
Link to comment
Share on other sites

Can you show us a small example of this code in a non-looping form that actually works?

var a = thisComp.effect("Scale")("Layer").name;  var b = thisComp.effect("Position")("Layer").name;  var c = thisComp.effect("Side")("Layer").name;  
Edited by tiscavalcanti
Link to comment
Share on other sites

That looks like code from some sort of animation framework. What exactly is it? Adobe After Effects?

var names=["Scale", "Position", "Side"];
for (var i = 0; i < names.length; i++) {
var val="thisComp.effect(" +names +")('Layer').name";
}
the result of "var val" is this:
thisComp.effect(Scale)('Layer').namethisComp.effect(Position)('Layer').namethisComp.effect(Side)('Layer').name
I can arrive until here.
But it's all together. I wanted to separate each line, now for each variable. A, B and C.
Lack take each line of output to a variable.
Edited by tiscavalcanti
Link to comment
Share on other sites

That looks like code from some sort of animation framework. What exactly is it? Adobe After Effects?

What I want is to actually simplify the code, to avoid repetition.

 

I want to avoid of writing dozens of times, the same base of effect code to save rewrite that part:
thisComp.effect("")("Layer").name;
Because the expression in general, becomes very extensive and more difficult to edit with lot of effects.
So I want a way to use a loop that collects all the names of the effects, and use the base of effect code above which is repeated, to receive these names: "Scale", "Position" & "Side", and generate 3 full effects separately, with this output:
thisComp.effect("Scale")("Layer").name;
thisComp.effect("Position")("Layer").name;
thisComp.effect("Side")("Layer").name;
And finally, take these three generated effects, and connect them to 3 variables... a, b and c.
It is possible?
I'm beginner with functions and loops.
Link to comment
Share on other sites

That looks like code from some sort of animation framework. What exactly is it? Adobe After Effects?

Is there a way to do this more easily?

How to add a value in the middle of a code line?

And there is a way to separate each line of output of a for loop?

 

Thanks.

Link to comment
Share on other sites

It still isn't clear to me what code you are replacing with this loop.

 

You say...

 

 

I want to avoid of writing dozens of times, the same base of effect code to save rewrite that part:

 

...so show us a dozen lines of code.

  • Like 1
Link to comment
Share on other sites

Oh, yeah!

I want to optimize this expression.



// Position    // Variables:  var w=1920;     // Comp Width  var h=1080;     // Comp Height  var p1=.95;     // variable to multiply by 95%  var p2=.90;     // variable to multiply by 90%  var p3=.85;     // variable to multiply by 85%  var p4=.15;     // variable to multiply by 15%  var p5=.10;     // variable to multiply by 10%  var p6=.05;     // variable to multiply by 5%  var s;          // variable to be modified  var x;          // variable to represent Width Property value  var y;          // variable to represent Height Property value    // Variables with Try/Catch Block to Prevent errors:    // (I wanted to simplify the part below, making it more optimized.)    try{  a=thisComp.layer("Less").effect("Scale")("Layer").name;     // Variable = "Scale" - Layer Control Effect  }catch(err){  a=0;        // Prevention for when the effect is selected "None".  };  try{  b=thisComp.layer("Less").effect("Position")("Layer").name;      // Variable = "Position" - Layer Control Effect  }catch(err){  b=0;        // Prevention for when the effect is selected "None".  };  try{  c=thisComp.layer("Less").effect("Side")("Layer").name;      // Variable = "Side" - Layer Control Effect  }catch(err){  c=0;        // Prevention for when the effect is selected "None".  };    // Conditions:    // (I wanted to simplify these conditions with functions or loops, for example.)    if(thisComp.layer("Less").effect("Master Control")("Checkbox")==0){     // Condition to when Checkbox is disabled, the property back to original value  value;  }else{      // And otherwise  if(b=="Less"){      // If "Position" - Layer Control Effect = Layer "Less"  y=h*p1;     // Height value = Comp height * 95% | Result = 1026  s=w*p6;     // variable s = Comp width * 5% | Result = 96  }else if((b=="Half") || (b=="Default")){        // Otherwise, If "Position" - Layer Control Effect = Layer "Half" or "Default"  y=h*p2;     // Height value = Comp height * 90% | Result = 972  s=w*p5;     // variable s = Comp width * 10% | Result = 192  }else if(b=="More"){        // Otherwise, If "Position" - Layer Control Effect = Layer "More"  y=h*p3;     // Height value  = Comp height * 85% | Result = 918  s=w*p4;     // variable s = Comp width * 15% | Result = 288  }else{      // And otherwise  y=value[1];     // Height value = Original value  };  if(a==0){       //  If "Scale" - Layer Control Effect = None  x=w/2;      // width value = Comp Width / 2 | Result = 960  }else{      // And otherwise  if((c=="Less") || (c=="Default")){      // If "Side" - Layer Control Effect = Layer "Less" or "Default"  x=s;        // Width value = variable s  }else if(c=="Half"){          // Otherwise, If "Side" - Layer Control Effect = Layer "Half"  x=w/2;      // Width value = Comp width / 2 | Result = 960  }else if(c=="More"){          // Otherwise, If "Side" - Layer Control Effect = Layer "More"  x=w-s;      // Width value = Comp width - variable s  }else{      // And otherwise  x=value[0];     // Width value = Original value  };  };  [x,y]       // Property | Array = Final Value of Width and Height  };  
Edited by tiscavalcanti
Link to comment
Share on other sites

It still isn't clear to me what code you are replacing with this loop.

 

You say...

 

 

...so show us a dozen lines of code.

After I managed to go ahead on the understanding of "for" loops and functions, I realized that there really is not a way to make a loop or function, use the instruction always use the same base code: (thisComp.effect ("")("Layer") name;), adding effect name (Scale, Position, Side) in half and then, generating variables (a, b, c), each getting one line of code ready. (as a factory cakes, which uses always the same base recipe, and only adds a special ingredient for each cake, making cakes with different flavors, for example). But if there is a another way to do this, let me know please.
Actually, I just wanted a way to simplify my code, but I realized that is not feasible.
Because just getting more complicated, what was simpler. Sorry to ask a question without much foundation.
Link to comment
Share on other sites

You can set up an object like this and then use it to populate the variables with the values:

var obj = {  a: 'Scale',  b: 'Position',  c: 'Side'};
You could loop through the properties of that object and use them to set the variables with their corresponding values, but doing that is not going to improve efficiency. It might make the code shorter, but it's more efficient to just execute each statement individually rather than looping.
  • Like 1
Link to comment
Share on other sites

You can set up an object like this and then use it to populate the variables with the values:

var obj = {  a: 'Scale',  b: 'Position',  c: 'Side'};
You could loop through the properties of that object and use them to set the variables with their corresponding values, but doing that is not going to improve efficiency. It might make the code shorter, but it's more efficient to just execute each statement individually rather than looping.

 

Thank you! I understand.

Link to comment
Share on other sites

w = thisComp.width;h = thisComp.height;s = 0;p = [.95, .90, .85, .15, .10, .05];x = value[0];y = value[1];function getLayerName(theLayer,theName,theParm){  try{    return theLayer.effect(theName)(theParm).name;  }catch(err){    return "None";  }}L = thisComp.layer("Less");a = getLayerName(L,"Scale","Layer");b = getLayerName(L,"Position","Layer");c = getLayerName(L,"Side","Layer");

This is it.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...