Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Find project.item – continue if found; if not, create.

  • Find project.item – continue if found; if not, create.

    Posted by Danny Parsons on February 6, 2009 at 6:55 pm
    for (var i=0 ; i<app.project.numItems.length; i ++)
    if (app.project.item(i). name != "stepOne")
    {	
    // Not found -- I'll create it!
    var stepOne = myComp.layers.addSolid ([1,0,0],"stepOne",100,100,1);
    }else{
    continue} 
    // Found it, so I'll just keep going... 
    var stepTwo= myComp.layers.addSolid ([0,1,0],"stepTwo",100,100,1);
    // Etc...
    

    Hi there,

    I’m having a bit of trouble with what I imagine should be a fairly simple stage in a script.

    I’d like to search for a particular project-item, create it if it doesn’t already exist, or else just continue if it does…

    At the moment, the example script above is running through to the end without reporting any errors. However, it always just skips straight to “continue”, regardless of whether the subject of my search exists or not!

    I’m sure I’m overlooking something simple – any ideas?

    Cheers,

    DP

    Danny Parsons replied 17 years, 3 months ago 2 Members · 6 Replies
  • 6 Replies
  • Dan Ebberts

    February 6, 2009 at 7:29 pm

    I think it would look more like this:

    var foundIt = false; // assume the worst
    for (var i=1 ; i

  • Danny Parsons

    February 7, 2009 at 3:54 am

    Hi Dan,

    Thanks for the swift response…

    Unfortunately, for some reason, I’m receiving an “Expected: ;” from the third line of your code…

    That aside (and I’ll have to be quite honest and admit that my standard-issue, linearly oriented mind doesn’t QUITE grasp every aspect of your approach and suggested Order of Operations), I suspect that maybe I should make myself a little more clear regarding my objective and its context…

    The objective, as I’ve described it in the thread-title, is not to be the primary function of the script, but rather just a small step along the way… The comp that I want to search for (or else create) is one made out of a whole bunch of solids with various settings, and, without a doubt, WILL be used many, many times within the project. It doesn’t, however, need to be unique…

    Each time the script runs, it will be adding this comp AT LEAST a couple of times as precomp-layers within other comps that the script will create…

    So I just want – at a fairly early point in the script – to quickly check if the comp already exists, and if so, set a variable for it so that I can reference it again and again, but if not, then go about the steps required in creating it, and then set a variable…

    Thanks again in advance, and please forgive the fact that I’m new to this whole scripting malarkey, and that it is currently 03.48 where I am, as I write this!

    (And if you still think that your original code is the way forward, and that the reported error can be easily explained, a slightly more detailed breakdown would be much appreciated!)

    Cheers,

    DP

  • Dan Ebberts

    February 7, 2009 at 4:06 am

    From what you describe, it sounds like when the script starts, it needs to search the project bin for that comp. If it finds it, it stores it in a variable (myComp in my sample). If not, you create the comp and store that in the variable.

    From then on (while the script is still running), you should only need to add that comp as a layer to some other comp ( using layers.add(myComp)). You shouldn’t need to create the comp again, unless I don’t really understand what you’re doing (quite possible).

    As to the error – hard to say without seeing it in context.

    Dan

  • Danny Parsons

    February 9, 2009 at 6:10 pm

    Hi Dan,

    It doesn’t sound like there’s any misunderstanding; your last post neatly sums up exactly what I’m aiming for.

    Regarding the error’s context… at this point, there really ISN’T any (context)! For the moment, I’m just trying to figure out how to perform this task in a simple test script before I try implementing it into the final script. So, really all I’m doing right now is completing the final line in your code in order to create an empty test comp. However, like I said, it keeps getting stuck on the first “if” line…

    I think I’m just a little unclear on how exactly the line above it is supposed to work, since – in my admittedly very limited experience – I’ve not seen the first line of a loop end without a closing parenthesis before, and I’m also unclear as to how exactly it’s setting up to define (i) as anything other than 1.

    Apologies for my ignorance – and thanks again for your help and patience,

    DP

  • Dan Ebberts

    February 9, 2009 at 6:21 pm

    OK – I see what’s going on. I got bit by having part of my code get interpreted as HTML. I should really use the code box, but I don’t like the way it works. Sorry – I should have previewed it before posting. Let me try it again:

    var foundIt = false; // assume the worst
    for (var i = 1; i <= app.project.numItems; i++){ if (app.project.item(i).name == "stepOne" && app.project.item(i) instanceof CompItem ){ var myComp = app.project.item(i); foundIt = true; break; } } if (! foundIt){ var myComp = // create new comp here } Dan

  • Danny Parsons

    February 9, 2009 at 7:31 pm

    Ah!

    I swear I must have come so close to the solution on numerous occasions over the weekend, but was probably adding that superfluous “.length” after “numItems” again (which I guess you only need if you’re working with a custom array?)

    It works perfectly now – Thanks again!

    DP

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