ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Is there any idea why this gave undefined error?

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Palak Bhatt
Is there any idea why this gave undefined error?
on Jul 29, 2020 at 8:08:19 pm

Check below code, Please let me know why this code give error "undefined"

var myLayers = app.project.activeItem.selectedLayers;
if (myLayers.length > 0){
for (var i=0; i &lt;= thisComp.selectedLayers.length; i++){
var d = myLayers[i].index;
alert(myLayers[i].index);
}

app.project.activeItem.layer(d).moveAfter(app.project.activeItem.layer(d+10));
}else{
alert ("No layers selected.");
}


Return to posts index

Filip Vandueren
Re: Is there any idea why this gave undefined error?
on Jul 29, 2020 at 8:55:56 pm

In the for loop, use < not <=



Return to posts index

Palak Bhatt
Re: Is there any idea why this gave undefined error?
on Jul 29, 2020 at 9:16:39 pm

Thanks ☺ Brothers i have no words for you.

I have the other question
I am trying to move all selected layers at every Nth number.

What should i do this code to works?

var d = 0;
var myLayers = app.project.activeItem.selectedLayers;
var myMove = app.project.activeItem;

if (myLayers.length > 0){
for (var i=0; i &lt; myLayers.length; i++){
d = myLayers[i].index;
}
myMove.layer(d).moveAfter(myMove.layer(d+10));

}else{
alert ("No layers selected.");
}


Return to posts index


Palak Bhatt
Re: Is there any idea why this gave undefined error?
on Jul 29, 2020 at 9:24:31 pm

The problem is on above code is only moved last number of selected layer, How can I move bulk layers at once? at fixed number of layers


Return to posts index

Palak Bhatt
Re: Is there any idea why this gave undefined error?
on Jul 29, 2020 at 9:37:57 pm

I Got it ☺

Check it below
Thank you, Brother

I want to add you on Facebook if you don't mind
give me your id if possible, You are super pro :)))
many thanks

ver NumberOfMove = 10;
var myLayers = app.project.activeItem.selectedLayers;
var myMove = app.project.activeItem;

if (myLayers.length > 0){
for (var i=0; i &lt; myLayers.length; i++){
var d = myLayers[i].index;
myMove.layer(d).moveAfter(myMove.layer(d+NumberOfMove));
}

}else{
alert ("No layers selected.");
}


Return to posts index

Palak Bhatt
Re: Is there any idea why this gave undefined error?
on Jul 29, 2020 at 9:49:45 pm

Corrected and tested
Now its final

var d = 0;
var NumberOfMove = 2;
var myLayers = app.project.activeItem.selectedLayers;
var myMove = app.project.activeItem;

if (myLayers.length > 0){
for (var i=0; i &lt; myLayers.length; i++){
d = myLayers[i].index;

myMove.layer(d).moveAfter(myMove.layer(d+NumberOfMove+myLayers.length));
}

}else{
alert ("No layers selected.");
}


Return to posts index


Filip Vandueren
Re: Is there any idea why this gave undefined error?
on Jul 30, 2020 at 11:46:59 am
Last Edited By Filip Vandueren on Jul 30, 2020 at 11:48:29 am

I don't really understand how you want the layers to move with this code, It seems very unpredictable.

If you want the layers to move down by "numberOfMove", it's best to cycle through them backwards, because moving a layer will change the indexes of all the layers after it.
So we first order the selectedLayers by index descending (they are in the list in the order that they were clicked on, not in the order of index)

I've changed the code a bit so it also works for negative numbers.


var numberOfMove = 2;
var myComp = app.project.activeItem;
var myLayers = myComp.selectedLayers;

myLayers.sort( function(a,b) { return b.index - a.index }); // reverse sort layers by index
if (numberOfMove < 0) myLayers.reverse(); // except if we're moving backwards, then we do want sorted by index ascending

if (myLayers.length > 0){
for (var i=0; i< myLayers.length; i++){
d = Math.min ( d , myComp.numLayers-i); // never move than the number of layers
if (myLayers[i].index!==d) { // can't move a layer after itself
myLayers[i].moveAfter(myComp.layer(d));
}
} else {
d= Math.max (d, 1+i); // never less than 1st layer
if (myLayers[i].index!==d) { // can't move a layer before itself
myLayers[i].moveBefore(myComp.layer(d));
}
}
}

}else{
alert ("No layers selected.");
}




Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2020 CreativeCOW.net All Rights Reserved
[TOP]