Loop thru all items in comp and change duration

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
fabiantheblind
Posts: 7
Joined: December 27th, 2010, 11:33 pm

hello AEnhancers,

I'm trying to loop thru all items in my selection and change the in and out points.
That works fine but i run into the problem that i can't change the duration of the last layer in nested comps.
Here is my code.

Code: Select all

main();
function main(){
var curComp = app.project.activeItem;
	if (!curComp || !(curComp instanceof CompItem))
	{
	    alert("Please select a Composition.");
	    return;
}
var val = 1;
curComp.duration = val;
var sellayers = curComp.selectedLayers;

var layers = new Array();
    for (var i = 0; i < curComp.selectedLayers.length; i++) 
    {
        layers[i] =  curComp.selectedLayers[i];
        
        }

	chngDur(layers,val);
}


function chngDur(layers,val){
	var selLen = layers.length; 
	// here is somehow the problem
		for (var i = 1; i < selLen; i++) {
			change(layers[i],val);	
	}

}

function change(inLayer,val){
	
	var layer = inLayer;
	
// check for precomps
	if (layer.source.numLayers != null) {
         layer.source.duration = val;
         layer.inPoint = 0;
         layer.outPoint = val;
		chngDur(layer.source.layers,val);
        
	} else {
		
		layer.inPoint = 0;
		layer.outPoint = val;
	}
	
}
Can someone give me a hint what is wrong?

cheers

:F
Paul Tuersley
Posts: 704
Joined: June 5th, 2004, 7:59 am
Location: London, UK

Your for loop starts from var i = 1 while an array index (layers) starts from 0.

BTW: Instead of layers = curComp.selectedLayers;
you can use this to add to an array layers.push(curComp.selectedLayers);

Paul
fabiantheblind
Posts: 7
Joined: December 27th, 2010, 11:33 pm

Paul Tuersley wrote:Your for loop starts from var i = 1 while an array index (layers) starts from 0.
Yeah I tried this, then i get the error:
After Effects Fehler: Wert 0 außerhalb des Bereichs 1 bis 10
its like: array out of bounds.
Value 0 is out of range from 1 to 10.

I think i somehow get the layers (they start by one) mixed up with the Array index thats starts at 0 like you said

I found a workaround that works but is a bit ugly:

Code: Select all

function chngDur(layers,val){
	var selLen = layers.length; 	
		for (var i = 0; i <= selLen; i++) {			
            try {
                change(layers[i],val);	
            }catch(e){
                $.writeln (e);
            }	
    }
}
But still i would like to know how to make it right.
Thnx Paul

:F
Paul Tuersley
Posts: 704
Joined: June 5th, 2004, 7:59 am
Location: London, UK

You're sending two different types of "layers" to the chngDur function.

Your own "layers" array starts with an index of 0.

But comp.layers (which you send when you find a precomp) is from 1 > comp.numLayers.

You could send an additional startIndex to changDur:

Code: Select all

chngDur(layers,val, 0);
chngDur(layer.source.layers,val, 1);
And use this:

Code: Select all

function chngDur(layers,val, startIndex){
   var selLen = layers.length + startIndex;
   // here is somehow the problem
      for (var i = startIndex; i < selLen; i++) {
		alert(i + " " + layers[i].name);
		change(layers[i],val);   
	}
}
fabiantheblind
Posts: 7
Joined: December 27th, 2010, 11:33 pm

Yes. That works.
Thnx again Paul.

Here the full code:

Code: Select all

main();
function main(){
  
var curComp = app.project.activeItem;
  
	if (!curComp || !(curComp instanceof CompItem))
	{
	    alert("Please select a Composition.");
	    return;
}


app.beginUndoGroup("set Duration");

var val = 168;

curComp.duration = val;
var sellayers = curComp.selectedLayers;

var layers = new Array();
    for (var i = 0; i < curComp.selectedLayers.length; i++) 
    {
        layers.push(sellayers[i]);
        
        }

	chngDur(layers,val,0);
    //end undogroup
app.endUndoGroup();
}


function chngDur(layers,val,strtNdx){

	var selLen = layers.length + strtNdx; 
	
		for (var i = strtNdx; i < selLen; i++) {
			
            try {
                change(layers[i],val);	
            }catch(e){

                $.writeln (e);
            }	
    }

}

function change(inLayer,val){
	
	var layer = inLayer;
	
	if (layer.source.numLayers != null) {
         layer.source.duration = val;
         layer.inPoint = 0;
         layer.outPoint = val;
		chngDur(layer.source.layers,val,1);
        
	} else {
		
		layer.inPoint = 0;
		layer.outPoint = val;
	}
	
}
Post Reply