Skip to main content
Participating Frequently
August 16, 2024
Answered

Set Work Area Start and end according to selected layers. issue with the position of the work area.

  • August 16, 2024
  • 1 reply
  • 831 views

I have a scrip that sets work area start and end according to the selected layer(s), but it doest position the work area correctly if the comp duration is large.

// After Effects script to set work area to selected layers' start and end points

app.beginUndoGroup("Set Work Area to Selected Layers");

var comp = app.project.activeItem; // Get the active composition

if (comp && comp instanceof CompItem) { // Ensure that there is an active composition
    var selectedLayers = comp.selectedLayers; // Get selected layers
    
    if (selectedLayers.length > 0) {
        var workAreaStart = selectedLayers[0].inPoint;
        var workAreaEnd = selectedLayers[0].outPoint;
        
        // Loop through the selected layers to find the earliest start and latest end points
        for (var i = 0; i < selectedLayers.length; i++) {
            var layer = selectedLayers[i];
            if (layer.inPoint < workAreaStart) {
                workAreaStart = layer.inPoint;
            }
            if (layer.outPoint > workAreaEnd) {
                workAreaEnd = layer.outPoint;
            }
        }
        
        // Set the work area start and duration
        comp.workAreaStart = workAreaStart;
        comp.workAreaDuration = workAreaEnd - workAreaStart;
        
    } else {
}
} else {}

app.endUndoGroup();
This topic has been closed for replies.
Correct answer Dan Ebberts

Apparently, there are some restrictions (bug?) on how you can set the work area start and duration based on the currents settings. Replacing these two lines:

comp.workAreaStart = workAreaStart;
comp.workAreaDuration = workAreaEnd - workAreaStart;

with these:

comp.workAreaStart = 0;
comp.workAreaDuration = comp.duration;
comp.workAreaStart = workAreaStart;
comp.workAreaDuration = workAreaEnd - workAreaStart;

 seems to work in all the situations I could think of.

1 reply

Dan Ebberts
Community Expert
Community Expert
August 16, 2024

How large of a comp duration causes the script to malfunction?

Participating Frequently
August 16, 2024

Sorry I didn't write it properly,  

so whenever the selected layers are before the work area end in the timeline, it works completely fine but if the layers are after it the script doesnt work properly it creates the work area of the same duration but fails to position the work area according to selected layer, snd sometimes just doesnt work in larger comps.
I tried it on a 3hr comp, just to test it, and found out that the smaller the size of the layer after the "work are end" is the worse it gets.

I made a script that sets the work area start to the playhead, and had the same issue, and I think its the work area start thats causing the problem here aswell , because I made a "set work area end to playhead" as well and it was way easier.

Dan Ebberts
Community Expert
Dan EbbertsCommunity ExpertCorrect answer
Community Expert
August 16, 2024

Apparently, there are some restrictions (bug?) on how you can set the work area start and duration based on the currents settings. Replacing these two lines:

comp.workAreaStart = workAreaStart;
comp.workAreaDuration = workAreaEnd - workAreaStart;

with these:

comp.workAreaStart = 0;
comp.workAreaDuration = comp.duration;
comp.workAreaStart = workAreaStart;
comp.workAreaDuration = workAreaEnd - workAreaStart;

 seems to work in all the situations I could think of.