shinsuke matsumoto's blog thing.


This is a very first script in 2016! It’s so simple one but I think it’s not bad as New Year greeting to my mograph friends.

Okay, here’s the code for you. This one called “invertStackOrder” is to invert the layers stack order. Please take a look.

[Script Code]

app.beginUndoGroup("Invert Stack Order");
var curComp = app.project.activeItem;
if(curComp != null && curComp instanceof CompItem){
    var selectedLayers = curComp.selectedLayers;
    if(selectedLayers.length > 1){
        var layerObjArray = new Array();
        for(var i = 0; i < selectedLayers.length; i++){
            layerObjArray.push([selectedLayers[i], selectedLayers[i].index]);
        }
        var sortedLayers = layerObjArray.sort(function(a, b){
            if(a[1] < b[1]){ return 1; } if(a[1] > b[1]){
                return -1;
            }
            return 0;
        });
        for(var j = sortedLayers.length - 1; j > 0; j--){
            sortedLayers[j][0].moveAfter(sortedLayers[0][0]);
        }
    } else {
        alert("Select 2 layers at least.", "Invert Stack Order");
    }
} else {
    alert("Select a composition first.", "Invert Stack Order");
}
app.endUndoGroup();

 

[Quick Explanation]
Copy the code & past them into Adobe ExtendScript Toolkit. And in AE, select layers, then running the script. So invertStackOrder script will invert the layers stack order in the comp.

If you have a great script called “ft-Toolbar2” from aescripts, it’s ultra easy to use this script. Create the new button as Javascript in the setting menu in ft-Toolbar2, and then copy & paste the code into the command field like below. So you can invert the layers stack order with one click.

 

This script is pretty simple but it might be useful when you copy the layers and paste them in another comp like below.

Anyway, I would much appreciate it if you could give me a feedback. So please don’t hesitate to get in touch with me if you find some errors or better ways. And if you have any request for the series, hit me up on my Twitter. And if you don’t have time to read my crappy article, please follow & check my codes on my GitHub Gist.

Advertisements

2 Responses to “Crap Sharing for AE Scripting: invertStackOrder”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Basic HTML is allowed. Your email address will not be published.

Subscribe to this comment feed via RSS

%d bloggers like this: