The Expressions Thread

So, I’ve got a question then (for Todd, if you’re listening), 'cos this one may have passed me by too.

Supposing I have used a script to search through a project and find a certain property X on layer Y in comp Z. I’m able to open the comp in a viewer, move focus to the viewer, and I’m able to select the layer and the property inside that comp, but… am I able to twirl the layer down to reveal the property? The equivalent of hitting “SS” on the keyboard with the layer selected?

I’m fairly sure you can’t, but if it existed, it would be rather useful for scripted search functions.

Man, felt_tips is asking questions about scripting and stuff… I must be dreaming :smiley:

EFEKT_Studio said

Man, felt_tips is asking questions about scripting and stuff… I must be dreaming :smiley:

:S :nerdy: :smiley:

Leupsi said

Hi,

i would like to have an control layer from which you can select one layer from the composition to be activated. Does this work with the layer control expression effect and if yes, how?

Thank’s for your help! :slight_smile:

By activated do you mean turned on/off?

^ Lets suppose that he mean turn on/off. In that case I’m using this expression applied to opacity on layer1:

c =comp("composition_name").layer("layer_with_control").effect("layer1")("Checkbox")
if (c==1){
opacity=100
}else if (c==0){
opacity=0}

I like it and I’m using it, but in that case transparency can be only 0 or 100. That’s not big problem because layer1 can be precomposed and precomposition can contains layer with any opacity value.

^ the same thing, only a short version without unnecessary details:

c = comp("composition_name").layer("layer_with_control").effect("layer1")("Checkbox");
if (c == 1) opacity = 100
else opacity = 0;

^ the same thing, only a short version without unnecessary details:

c = comp("composition_name").layer("layer_with_control").effect("layer1")("Checkbox");
c * 100;

:slight_smile:

How to use a master Checkbox Control to turn layers on and off (using the value keyword)

dorde said

I like it and I’m using it, but in that case transparency can be only 0 or 100. That’s not big problem because layer1 can be precomposed and precomposition can contains layer with any opacity value.

That sounds like a lot of extra work. Why not use the value keyword? That gives you the value of the property before the expression.

c =comp("composition_name").layer("layer_with_control").effect("layer1")("Checkbox");
if (c==1){
opacity=value
}else if (c==0){
opacity=0}

…or shorter…

c =comp("composition_name").layer("layer_with_control").effect("layer1")("Checkbox");
c*value;

…or if having the switch on should cause the opacity to be half its original value and having the switch off should cause the opacity to be 15%…

c =comp("composition_name").layer("layer_with_control").effect("layer1")("Checkbox");
c ? value/2 : 15;

Javascript will change the boolean values true and false to 1 or 0, if needs be, depending on what you do with them. Javascript is a loosely typed language and that means it can switch the type of data quite easily from one form to another. See post below for more on Javascript datatypes and the value keyword.

Dealing with different data types in expressions.

Javascript (and expressions language) is a loosely typed language and that means it can switch the type of data quite easily from one form to another.

Try this experiment to get a feel for it. Put the following lines of code into the Source Text of a text layer…

note that (5==5) is an expression that evaluates to true and (5==4) is an expression that evaluates to false. The double equals == is used to compare two values, because a single equals = is used to set values. var a = 5 sets the variable a to have a value of 5.

(5==5).toString();

Result: true.

toString() has turned the boolean true value into the word “true”.

(5==4).toString();

Result: false.

The toString() method has turned the boolean false into the word “false”.

(11.3 + (5==5)).toString();

Result: 12.3

Adding 11.3 to true forces the true to be turned into a 1. Then the whole thing is turned into the string “12.3”

"hello there " + (5==5);

Result: hello there true

The boolean true is converted to the string “true” when you try to add it to another string, like “hello there”. In actual fact, the toString() function that I’ve been using on the Source Text property is also superfluous. After Effects will automatically attempt to convert the result to a string, because that’s the type of data that Source Text accepts.

If you want to know what type of value a certain variable holds, you can use typeof
(Note: Anything after // on a line is a comment and does not get evaluated as code.)

typeof "hello"; //Result: "string"
typeof 5; // Result: "number"
typeof true; //Result: "boolean"
typeof effect("Checkbox Control")("Checkbox"); //Result: "object"
typeof effect("Checkbox Control")("Checkbox").value; // Result: number!!! Note: NOT boolean
typeof (typeof 5); //Result: string - typeof returns a string

value and valueAtTime()

In expressions, the value keyword can be used to refer to a property’s own value before the expression is applied.

For instance, if you have animated a complicated scaling animation with hundreds of keyframes and you decide you want the animation to be the same, only half as big, you can use the simple expression.

value/2;

This takes whatever the value of scale was and halves it.

If you use value to refer to a property other than the property that the expression is on, you can only retrieve the post-expression value (if it has an expression). As far as I’m aware, there’s no way to retrieve the pre-expression value of a property from outside that property. For instance, this expression on the scale property…

a = thisLayer.effect("Slider Control")("Slider").value;
[a,a];

…will give the post-expression value of Slider Control (if it has an expression).

valueAtTime(t) works similarly, but this method takes an argument… a value inside the brackets that instructs the valueAtTime method which value to return.

thisProperty.valueAtTime(time+1.2);

The expression above will get the property’s pre-expression value 1.2 seconds ahead of the current time. (the keyword time retrieves the current time in seconds) Note: I’ve used the thisProperty keyword to refer to the property itself. I could also have used valueAtTime(time+1.2) on its own.

If I use the valueAtTime(t) method to refer to another property than the property that the expression is on, it works the same, but refers to the post-expression value of that property if there is an expression.

//on the Scale property
a = thisLayer.effect("Slider Control")("Slider").valueAtTime(time+1.2);
[a,a];

Will get the post-expression property (if it has an expression) of Slider Control, 1.2 seconds ahead of the current time.

By the way, chaps… the Expressions Thread has got a few questions and a few answers now… You can see the direction it’s going in.

Is this stuff useful? Should we keep it stickied? Or is it all a bit technical for a video forum? We’re not really programmers at the end of the day.

dbl post.

felt_tips said

By the way, chaps… the Expressions Thread has got a few questions and a few answers now… You can see the direction it’s going in.

Is this stuff useful? Should we keep it stickied? Or is it all a bit technical for a video forum? We’re not really programmers at the end of the day.

I am more programmer then designer, so I am good with sticking it.

I think that will be better if Essential Resources for Authors is sticky, and 1st post should contains links to all important threads like this.

Thanks Ben for expressions and explanations after my post :slight_smile:

dorde said

I think that will be better if Essential Resources for Authors is sticky, and 1st post should contains links to all important threads like this.

The Essential Resources thread has been officially de-stickied and some of the resources moved to an official resources page for authors. The thread still exists and you can bookmark it for your own use, but it’s not going to be re-stickied unfortunately.

Sticky please

Sticky!!!

Here is something from a while ago. Helped me a lot. Used it again today. I’ll post it here for anyone with similar request:

Q: What if you want to link together effects in different compositions… cc sphere for example, and you want to match ALL of their properties. Should I go one by one and link them all individually or is there a smart way that just makes two effects alike with one line of code.

Answer:

Bugger! The other day I went one by one. And is was CC Sphere!? :smiley:

How do you link all properties of an effect between compositions?

InlifeThrill said

Here is something from a while ago. Helped me a lot. Used it again today. I’ll post it here for anyone with similar request:

Q: What if you want to link together effects in different compositions… cc sphere for example, and you want to match ALL of their properties. Should I go one by one and link them all individually or is there a smart way that just makes two effects alike with one line of code.

Answer:

There’s an aescripts file for cloning an effect too… can’t remember the name off the top of my head though.

Here’s that code… I’ll link this to the first post when I get time…

FX1 = comp("Comp Name").layer("Layer Name").effect("Effect Name"); //layer to link to
FX1(thisProperty.propertyIndex).value; //generic property clone code

The keyword thisProperty refers to the property object that the expression is on. The propertyIndex is a property of each property object. This expression basically says - look at the effect on the layer in the other comp, and link to the property of that effect that has the same index as me.

Once you’ve created the expression for one property, just cut ‘n’ paste the same expression on to all the props…

or use right-click -> copy expression only to save you having to alt-click the stop watch every time.