I have a checkbox for background and its items. , write the script

if(thisComp.layer("Controller ").effect("Checkbox background ")("Checkbox")>0) {100} else {0};

it is working

But when checkbox for another one of the items above that background write the script

if(thisComp.layer("Controller ").effect("Checkbox percents")("Checkbox")>0) {100} else {0}; 
if(thisComp.layer("Controller ").effect("Checkbox background ")("Checkbox")>0) {100} else {0};

but not working

enter image description here


Expressions are evaluated until they return a value. Your first line will always return a value, and the expression will stop there. Think about what you're telling the property to do. If the percents checkbox is ticked then you're set to 100 otherwise you're 0 Okthxbye. Oh and also if the background checkbox is ticked then 100 otherwise 0. What happens if percents is ticked but background isn't?

What you need to use is boolean logic. So if you want the opacity to be 100 if either checkbox is ticked you'd say: if percents is ticked or background is ticked then 100, otherwise 0. The javascript way of writing this is to use the logical or operator: ||

let percents = thisComp.layer("Controller ").effect("Checkbox percents")("Checkbox");
let bg = thisComp.layer("Controller ").effect("Checkbox background ");
if (percents > 0 || bg > 0) { 100 } else {0}

the two let statements just assign variables to the checkbox values, to keep the logic bit tidy, otherwise it looks like

if(thisComp.layer("Controller ").effect("Checkbox percents")("Checkbox")>0 || thisComp.layer("Controller ").effect("Checkbox background ")("Checkbox")>0) {100} else {0};

Which is a bit hard to read. You could also shorten the last line by omitting the > 0 bit. If something is equal to 0, javascript treats it as false and anything > 0 is automatically truth`. So the last line could be simply:

if (percents || bg) { 100 } else {0}

Now if you wanted the opacity to be 100 only if both the checkboxes were checked then you'd say: I want you to be 100 if percents is checked and background is checked. The javascript notation for a logical and is &&. So now your expression would look like

let percents = thisComp.layer("Controller ").effect("Checkbox percents")("Checkbox");
let bg = thisComp.layer("Controller ").effect("Checkbox background ");
if (percents && bg) { 100 } else {0}

Boolean logic can be more complex, you can use not or in JS ! to return true when something is false, and you could group things together to make more complex expressions. E.g.:

if ((! percents) && background){100} else {0}

returns 100 only if percents is unchecked and background is checked. Boolean logic can be represented by Truth tables, like this:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.