Feedback wanted: Fit Layers To

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
Klustre
Posts: 21
Joined: July 27th, 2011, 2:53 am

Hi everyone,

As I'm starting out with scripting I'd love to hear any feedback on the script below. It seems to work fluently (CS5), including alerts when the wrong items are selected. It's pretty simple and nothing fancy, but I wonder where I could write it cleaner or better or whatever.

http://upload.klustre.nl/kl_fitLayersTo.jsx.zip

Code: Select all

/*
    
   FIT LAYERS TO—
   PROVIDES A PALETTE WITH BUTTONS TO FIT THE SELECTED LAYERS
   TO EITHER THE CURRENT TIME, WORK AREA OR TIMELINE.
   
   WRITTEN BY REMCO JANSSEN / KLUSTRE
   KLUSTRE.NL / SEPTEMBER 2012

*/

var win = new Window('palette', 'Fit selected layers to:');
this.windowRef = win;

win.grp = win.add("group", [0,0,310,25], 'Group');

var btnWidth = 100;
var btnHeight = 25;

win.grp.fttBtn = win.grp.add("button", [0, 0, btnWidth, btnHeight], 'Timeline');
win.grp.ftwBtn = win.grp.add("button", [105, 0, 105+btnWidth, btnHeight], 'Work Area');
win.grp.ctiBtn = win.grp.add("button", [210, 0, 210+btnWidth, btnHeight], 'Current Time');
 
win.show();

win.grp.fttBtn.onClick = fitToTimeline;
win.grp.ftwBtn.onClick = fitToWorkArea;
win.grp.ctiBtn.onClick = fitToCTI;


function fitToTimeline() {
    
    var klComp = app.project.activeItem;
    
    if(klComp == null) {
        alert("Select at least one layer.");
    } else{

        var klLayers = klComp.selectedLayers;
    
        if((klComp instanceof CompItem) == false || klLayers.length == 0)  { // klLayers.length is 0 when nothing is selected.
            alert("Select at least one layer.");
        } else{
        
            app.beginUndoGroup("Fit to Timeline");
            
                for(var i = 0; i < klLayers.length; i++){ 
                    klLayers[i].inPoint = 0;
                    klLayers[i].outPoint = klComp.duration;
                    }
        
            app.endUndoGroup();
        
        }
    }
}

function fitToWorkArea() {
    
    var klComp = app.project.activeItem;
    
    if(klComp == null) {
        alert("Select at least one layer.");
    } else{

        var klLayers = klComp.selectedLayers;
    
        if((klComp instanceof CompItem) == false || klLayers.length == 0)  { // klLayers.length is 0 when nothing is selected.
            alert("Select at least one layer.");
        } else{   
            
            app.beginUndoGroup("Fit to Work Area");
        
            var waStart = klComp.workAreaStart;
            var waDur = klComp.workAreaDuration;
            
            for(var i = 0; i < klLayers.length; i++){ // klLayers.length is 0 when nothing is selected.
                    klLayers[i].inPoint = waStart;
                    klLayers[i].outPoint = waStart + waDur;
                    }
            
            app.endUndoGroup();
            
        }
    }
}


function fitToCTI() {
    
    var klComp = app.project.activeItem;
    
    if(klComp == null) {
        alert("Select at least one layer.");
    } else{

        var klLayers = klComp.selectedLayers;
    
        if((klComp instanceof CompItem) == false || klLayers.length == 0)  { // klLayers.length is 0 when nothing is selected.
            alert("Select at least one layer.");
        } else{
        
            app.beginUndoGroup("Fit to Current Time");
            
                var klTime = klLayers[0].time;
                
                for(var i = 0; i < klLayers.length; i++){ 
                    klLayers[i].inPoint = 0;
                    klLayers[i].outPoint = klTime;
                    }
            
            app.endUndoGroup();
            
        }
    }
}
Alan Eddie
Posts: 30
Joined: January 12th, 2012, 1:36 am
Location: Ireland
Contact:

Works well, there is a short cut for the current time - alt+] as far as I know.
You could add in 'trim to layer above' and below? Lloyd Alvarez has a similar script on aescripts.com but if
you have gone that far it would be no problem to you.
Alan Eddie
_______________________
www.alaneddie.com
3d Animation and VFX
RTE
Dublin
Alan Eddie
Posts: 30
Joined: January 12th, 2012, 1:36 am
Location: Ireland
Contact:

Also your script may want to interpret AVItems if they are not active during the work area and their length is not long enough to fill the work area or reach the current time. (Perhaps the in jumps to the work area start or something like that).
Alan Eddie
_______________________
www.alaneddie.com
3d Animation and VFX
RTE
Dublin
Klustre
Posts: 21
Joined: July 27th, 2011, 2:53 am

Thanks for your feedback! Didn't know about that shortcut yet and it works very well. I'll use it from now on :)

I'm not sure if I'll continue working on this, since it was just made for learning purposes. Might work on an advanced Sequence Layers instead. But your AVItem suggestion sounds challenging! I've since learned a lot about formatting the code, looking at Jeff's scripts and this thread: viewtopic.php?f=8&t=2058

So the If statements in this code could be shorter, the klComp and klLayers vars can be global, and preferably it should move the startTime, not the inPoint. And it should indeed take AVItems into account!
Alan Eddie
Posts: 30
Joined: January 12th, 2012, 1:36 am
Location: Ireland
Contact:

Hi there, don't forget to package the global vars in an object or to do some kind of closure. I was making that mistake for a while - safer to avoid them.
Alan Eddie
_______________________
www.alaneddie.com
3d Animation and VFX
RTE
Dublin
Klustre
Posts: 21
Joined: July 27th, 2011, 2:53 am

Yes, I'd probably use Jeff's method, like I did here: viewtopic.php?f=3&t=2146 But that would be the Object method. What is it you mean exactly by closure?
Alan Eddie
Posts: 30
Joined: January 12th, 2012, 1:36 am
Location: Ireland
Contact:

Not that I am an expert - these guys explain it much better than I could.

http://stackoverflow.com/questions/1111 ... sures-work

http://davidbcalhoun.com/2011/what-is-a ... javascript
Alan Eddie
_______________________
www.alaneddie.com
3d Animation and VFX
RTE
Dublin
Post Reply