ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Parenting selected layers to unselected layers problems

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Adam Hayes
Parenting selected layers to unselected layers problems
on Nov 30, 2018 at 1:15:44 am

Evening,

I needed to parent a bunch of selected layers to unselected layers in order.

Layer 1 (selected) to Layer 4 (unselected)
Layer 2 (selected) to Layer 5 (unselected)
Layer 3 (selected) to Layer 6 (unselected)

I found this script, can't give credit as I don't have it, and it has been working great:

I have attached it to a button and it runs perfectly, but I would like it to invert the layers selected after the parenting. I.E - the unselected become selected and vise versa. I have an Invert Selected script and that runs perfectly.

The problem is that when I put the two functions into the button, and there are more SELECTED Layers, it only does the Parenting one. Then stops. If there is an equal number of SELECTED and UnSelected, it works perfectly.

Layer 1 (selected) to Layer 5 (unselected)
Layer 2 (selected) to Layer 6 (unselected)
Layer 3 (selected) to Layer 7 (unselected)
Layer 4 (selected)

If your wondering why I have more layers selected then unselected is that
A. The script parents from the first unselected layer.
B. I have a setup of 8 layers of boxes that need to be parented to text layers, and the text layers will vary from 2 - 8.

The only work around I have now is having two different buttons, one doing each job. Any ideas as to why the script stops all action after it if I have a different number of layers?

Thanks in advance.

//Parent
var myComp = app.project.activeItem;
var SecondGrp = myComp.selectedLayers[myComp.selectedLayers.length-1].index+1;

for (var i = 0; i < myComp.selectedLayers.length; i++) {
myComp.selectedLayers[i].parent = myComp.layers [SecondGrp+i];
}
// Invert
var comp = app.project.activeItem;

for (var w = 1; w <= comp.numLayers; w++) {
comp.layers[w].selected = !comp.layers[w].selected;
}


Return to posts index

Dan Ebberts
Re: Parenting selected layers to unselected layers problems
on Nov 30, 2018 at 1:36:27 am

>Any ideas as to why the script stops all action after it if I have a different number of layers?

If you have more selected than unselected layers, your for loop will eventually try to access an unselected layer that doesn't exist, which should generate an error of some kind. I think you need to put in some code to defend against that happening.


Dan



Return to posts index

Adam Hayes
Re: Parenting selected layers to unselected layers problems
on Nov 30, 2018 at 4:56:06 pm

Hey,

So do an if/else statement?

Adam


Return to posts index


Adam Hayes
Re: Parenting selected layers to unselected layers problems
on Nov 30, 2018 at 7:32:28 pm

Thanks Dan,

I actually built an if/else statement and then realized that I just needed to kill the loop when it reached the end of the unselected layers. Did that math in a var and it works perfectly now. Thanks for the fresh eyes.

Adam


Return to posts index

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