Question

for some reason my loop is only going through once despite importedChar.numItems =2

After effects item start on 1 instead of 0, so I use x=1, Here's what I have

for(x=1; x < importedChar.numItems; x++) {
    if (importedChar.item(x) instanceof CompItem) {
        //Add imported Char to Main Comp
        var newObj = null;      
        var newObj = myComp.layers.add(importedChar.item(x))

        //Move Layer under null
        newObj.moveAfter(newNull);

        //Parent to Null
        newObj.parent = newNull;
    }    
}

If the first item is a composition it runs fine, but leaves the loop if it's the second

Was it helpful?

Solution

You need to use <= instead of < to get it to run twice.

Edit: Alternatively, for(x=0; x < importedChar.numItems; x++) { and then use x+1 when you need to use it, but in situations where you can avoid that, <= is preferable.

Situations when you'd want the alternative usually arise when you need to access an index and a meaningful value (e.g. to print a human-friendly error message)

OTHER TIPS

x = 1, importedChar.numItems =2, x < importedChar.numItems true;

then x = 2, importedChar.numItems =2, x < importedChar.numItems false; so out the loop

Im guessing this way

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top