Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions if/else exists expression

  • if/else exists expression

    Posted by Scott Mcgee on October 12, 2017 at 10:19 am

    for (var i = 1; i <= myComp.numLayers; i++){
    var myComp = app.project.activeItem;
    var nNum = myComp.layer(i).effect(“Name”)(“Slider”).value;
    var name = myObject[nNum].name;
    myComp.layer(i).name = name;
    };

    I’m having a little problem with my script.

    My name layers will never be the same, but the ones I want to effect will all have the slider “Name”…Again value will differ. The number is for my JSON reference…

    Now…The above expression in my script works fine, if I don’t have any other layers that have no slider or a different slider.

    I don’t have a way to reference the content of the slider, so I need it to say “if I exist change me else continue” I’ve tried a few things with no success.

    I’m sure I’ve read someone with a similar situation, but I’ve read about 50 different forums and can’t find anything. Any ideas?

    Scott Mcgee replied 8 years, 10 months ago 2 Members · 2 Replies
  • 2 Replies
  • Dan Ebberts

    October 12, 2017 at 1:03 pm

    One way might be to use try/catch:


    try{
    // try to do something here
    }catch(err){
    continue; // end up here if it don't work
    }

    Dan

  • Scott Mcgee

    October 12, 2017 at 1:30 pm

    I am a moron….

    I even tried a try/catch, but did it every wrong way possible, even when you posted that I did it wrong initially again and realised I have to put it in the loop for it too work, because I want to reference 4 categories I’ve been trying to put the try outside of it.

    That is working sport on now.

    Thank you for putting that, I would have gone around in circles otherwise.

    for (var i = 1; i <= myComp.numLayers; i++){

    try{
    var nNum = myComp.layer(i).effect(“Name”)(“Slider”).value;
    var name = myObject[nNum].name;
    myComp.layer(i).name = name;
    }catch(err){
    continue;
    }
    };

    for (var i = 1; i <= myComp.numLayers; i++){

    try{
    var lNum = myComp.layer(i).effect(“Location”)(“Slider”).value;
    var location = myObject[lNum].location;
    myComp.layer(i).name = location;
    }catch(err){
    continue;
    }
    };

    Unless you can think of a tidier way for this…I only have to do it 4 times and it’s only a find and replace. Last year I typed 480 text layers out, I’m doing it again this year and giving myself RSI hahahaha.

    Appreciate that.

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