Questions about my script to write expressions to layers

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
User avatar
Disciple
Posts: 137
Joined: June 5th, 2004, 8:05 am
Location: Los Angeles, CA
Contact:

August 5th, 2004, 8:12 am

I've been wanting to modify my first script, posted in the scripts tutorial section (Add Expression/Slider to a comp)
Basically this script will add a hard-coded expression and a slider control effect to all layers inside a selected composition.
What I'd like to do is
1/apply the expression to selected layers only (instead of all layers)
2/Make sure the effect and expression aren't applied already
3/This would be the best : have a field pop up to let me write a custom expression for each time I run the script, instead of having to hard code the expression inside the script.

Can anybody give me a few hints?
Thanks a lot
Alex
Paul Tuersley
Posts: 704
Joined: June 5th, 2004, 7:59 am
Location: London, UK

August 7th, 2004, 5:17 am

1/apply the expression to selected layers only (instead of all layers)

To go through all layers in the comp, you use something like:

theComp = app.project.activeItem;
for (i = 1; i <= theComp.numLayers; ++i) {
alert(theComp.layer(i).name);
}

But to go through just the selected layers, you'd use:

theComp = app.project.activeItem;
for (i = 0; i < theComp.selectedLayers.length; ++i) {
alert(theComp.selectedLayers.name);
}

You'll notice there are some subtle differences that can easily catch you out.

When using the 'all layers' approach, you start the for loop with i = 1 but with 'selectedLayers'
you need to use i = 0. The best explanation I can give for this is that selectedLayers is an array
containing all the selected layers, and typically the first value in an array has an index of 0.
Whereas, when accessing layers directly using theComp.layer(i) you are using the layer number
as it appears in the comp and of course layer numbering starts from 1.

You can find out how many layers are in a comp using theComp.numLayers, but to find how
many layers are selected, you need to see how many items are stored in the selectedLayers
array, which you do by using selectedLayers.length.

Also, notice that the 'all layers' loop uses i <=, while the 'selected layers' loop is just i <.
Because the selectedLayers array starts with an index of 0, the loop needs to finish when i is
one less than the total number of selected layers.

Finally, 'all layers' gets to a layer using layer(i), while 'selected layers' uses selectedLayers.
Notice one uses regular brackets but the other uses square brackets. I can't really explain this
with any great authority, but it's probably because selectedLayers is accessing an item in an
array (which you do using square brackets) while layer(i) isn't an array, it's a 'method' that
returns a specific layer....or something like that.

Paul T
Last edited by Paul Tuersley on August 12th, 2004, 4:45 pm, edited 1 time in total.
Paul Tuersley
Posts: 704
Joined: June 5th, 2004, 7:59 am
Location: London, UK

August 12th, 2004, 4:44 pm

2/Make sure the effect and expression aren't applied already

There are various things you could do here. You could just check that a layer doesn't have
an expression on its Position property:
if (curLayer.matchName == "ADBE AV Layer" && curLayer.Position.expression == ""){

or check if a Slider Control effect is already applied to a layer:
if (curLayer.matchName == "ADBE AV Layer"){
var alreadyHasEffect = false;
for (var i = 1; i <= curLayer.Effects.numProperties; ++i) {
if (curLayer.Effects(i).matchName == "ADBE Slider Control") {
alreadyHasEffect = true;
break; // end the for loop early
}
}
}
if (alreadyHasEffect == false) { // add a slider and three keyframes

Though if you knew for sure that no other effects were being used, you could just check
to see if there were no effects applied at all:
if (curLayer.matchName == "ADBE AV Layer" && curLayer.Effects.numProperties == 0){

Or a combination of these.....it depends on what kind of layers you expect the script to
encounter and how foolproof you want to make it.

Paul T
Post Reply