Creative Communities of the World Forums

The peer to peer support community for media production professionals.

  • Posted by Dmitry Kichenko on December 21, 2007 at 8:48 am

    Hi everyone,

    I’ve posted a question about cloning objects before, and right now I’m trying to do another trick with the cloned layers.

    The basic question I’ve been trying to find an answer to is – is there a way to count the number of layers I have in a particular composition?

    The extended version of the question is as follows. I have an X number of duplicates of a layer, and I would like to change the opacity from 100% to 0% linearly from the topmost to the last layer. The formula is quite easy but it requires knowing how many layers there are in the comp. The trouble is, this.layer(y), is a method and not an object. The comp() object has no count or length property or method, like the length property of an array would have, and AE seems to internally somehow store layer objects separately and just provide the layer method for us to access them.

    Either way, I can’t at this point figure out how to count the layers even if I know how to reference them, so any help would be appreciated. I mean, I can probably just have a predicted number of duplicates but I’d rather leave as much as I can to the MACHINE.

    THANKS in advance.

    P.S. Almost certain the answer is somewhere under my nose but I keep thinking it’s more complicated than it is.

    Dmitry Kichenko replied 18 years, 4 months ago 2 Members · 5 Replies
  • 5 Replies
  • Filip Vandueren

    December 21, 2007 at 2:55 pm

    Hi there,

    it’s a property of Composition:

    thisComp.numLayers

  • Dmitry Kichenko

    December 21, 2007 at 4:57 pm

    Umph * shoots self *.

    Why is it not docu… Wait, there it is in the helpfile.. Great..

    Ok. I’ll be down at the local italian store buying loads of coffee.

    Also, for future purposes, I would still like to know if there is a way to count objects of a particular kind (I don’t think they’re instanced from classes in JS unlike most OOP languages, and rather everything relies on inheritance, correct?).

  • Dmitry Kichenko

    December 21, 2007 at 4:58 pm

    Thanks a lot! 🙂

  • Filip Vandueren

    December 21, 2007 at 6:08 pm

    Indeed, in expressions there is no way to see all effects as lists, there are only functions to retrieve their value by index.
    A possible hack could be this, for example to count the number of masks in layer 2:


    countItems('thisComp.layer(2).mask');

    function countItems(obj) {
    a=0;
    while (true) {
    a++;
    try {
    eval(obj+"("+a+")");
    }
    catch(error) {
    a--;
    break;
    }
    }
    return a
    }

    This constructs functions to loop through finding ‘child’ 1 to infinity, and as soon as we bump into an error, we know we’re one step to far: so a-1 = the number of children of the property

    This should work with any layer or comp subobject, you can base the syntax of the string on what the pickwhip gives you.

    For example, if you pickwhip to a an efects’ paramater, you can get something like:

    thisComp.layer(“Black Solid 1”).effect(“Fast Blur”)(“Blurriness”)

    If we want to know how manny effects that layer has, we need:

    thisComp.layer(“Black Solid 1”).effect

    how many parameters does fast blur have ?

    thisComp.layer(“Black Solid 1”).effect(“Fast Blur”)

    Yes: you can even count the number of parameters a subobject has, or the number of keyframes (though there is a numKeys property for that) see the marker example below:

    examples:

    countItems(“thisComp.layer”);
    countItems(“thisComp.layer(1).effect”);
    countItems(“thisComp.layer(1).effect(1).param”);

    countItems(“thisComp.layer(1).mask”);
    countItems(“thisComp.layer(1).mask(1).param”);

    countItems(“thisComp.layer(1).transform”);
    countItems(“thisComp.layer(2).marker.key”);
    countItems(“thisComp.layer(2).motionTracker”);

    sometimes you get unexpected numbers.. for example:

    countItems(“thisComp.layer(2).text.animator(1).property”);

    You’d expect to find how many properties are controlled by a certain text-animator (position, opacity,…) but you get “29”… no idea why.

    countItems(“thisComp.layer(2).text.animator(1).selector”);
    does return the exact number of selectors a textanimator has.

    Anyway, I’m diving pretty deep under the hood here 😉

    I wish AE had global functions, so you could put this function in a null somewhere and call it from whereever, but as it is right now, you have to include the entire function in every expression that needs to count.

  • Dmitry Kichenko

    December 21, 2007 at 6:53 pm

    Thanks for the idea!

    I actually built a pretty much the same loop on my own after not being able to locate a built-in function for that, but I didn’t know how to handle the error – as soon as the while loop encountered an object that didn’t exist (let’s say it was “white (thiscomp.layer(x)) where x was greater than the number of layers in the comp) it would spit out an error and not let me use the expression. The try-catch mechanism is quite clever. Haven’t dealt with it in other languages (mostly a PHP + ActionScript programmer here).

    This is so much fun though, programming for graphics =). Forget keyframes! Thanks again!

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy