Highlighted

Looping through numProperties always ends with error: undefined

New Here ,
Jun 24, 2019

Copy link to clipboard

Copied

Working on a function that checks wether the selected layer has a specific effect applied to it. Here's a simplified version of it:

function hasTransformEffect(layer) {

    for (i = 1; i <= layer.Effects.numProperties; i++) {

        alert(i);

    }

}

alert(hasTransformEffect(app.project.activeItem.selectedLayers[0]));

When running this, I always end up with an undefined after it's finished looping. Here's a video of me trying this https://streamable.com/bcg48

Few things I have tried:

- Using a try/catch to bypass it

- Using an if statement to check wether numProperties > 0 before running the loop

- Running it from Extendscript IDE

- Running it from a saved .jsx file

- Running it on a different machine

- Running it on a different version of AE

Wondering what I'm missing, I'm assuming it's something about numProperties that I don't know. Any help is greatly appreciated!

Hugo

Your alert outside the function is reporting what the function is returning, but the function isn't returning anything.

function hasTransformEffect(layer) {

     for (i = 1; i <= layer.Effects.numProperties; i++) {

          if (layer.Effects.property(i).name == "Transform") return true;

     }

     return false;

}

alert(hasTransformEffect(app.project.activeItem.selectedLayers[0]));

TOPICS
Scripting

Views

180

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Looping through numProperties always ends with error: undefined

New Here ,
Jun 24, 2019

Copy link to clipboard

Copied

Working on a function that checks wether the selected layer has a specific effect applied to it. Here's a simplified version of it:

function hasTransformEffect(layer) {

    for (i = 1; i <= layer.Effects.numProperties; i++) {

        alert(i);

    }

}

alert(hasTransformEffect(app.project.activeItem.selectedLayers[0]));

When running this, I always end up with an undefined after it's finished looping. Here's a video of me trying this https://streamable.com/bcg48

Few things I have tried:

- Using a try/catch to bypass it

- Using an if statement to check wether numProperties > 0 before running the loop

- Running it from Extendscript IDE

- Running it from a saved .jsx file

- Running it on a different machine

- Running it on a different version of AE

Wondering what I'm missing, I'm assuming it's something about numProperties that I don't know. Any help is greatly appreciated!

Hugo

Your alert outside the function is reporting what the function is returning, but the function isn't returning anything.

function hasTransformEffect(layer) {

     for (i = 1; i <= layer.Effects.numProperties; i++) {

          if (layer.Effects.property(i).name == "Transform") return true;

     }

     return false;

}

alert(hasTransformEffect(app.project.activeItem.selectedLayers[0]));

TOPICS
Scripting

Views

181

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jun 24, 2019 0
Engaged ,
Jun 24, 2019

Copy link to clipboard

Copied

Your alert outside the function is reporting what the function is returning, but the function isn't returning anything.

function hasTransformEffect(layer) {

     for (i = 1; i <= layer.Effects.numProperties; i++) {

          if (layer.Effects.property(i).name == "Transform") return true;

     }

     return false;

}

alert(hasTransformEffect(app.project.activeItem.selectedLayers[0]));

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 24, 2019 0
New Here ,
Jun 24, 2019

Copy link to clipboard

Copied

Thanks for your help Paul, that works!

Could you explain why returning false within the if statement like so still gets me an undefined:

function hasTransformEffect(layer) {

    for (i = 1; i <= layer.Effects.numProperties; i++) {

         if (layer.Effects.property(i).name == "Transform"){

            return true;

         } else {

            return false;

        }   

    }

}

alert(hasTransformEffect(app.project.activeItem.selectedLayers[0]));

Just trying to get my head around this for future reference

Thanks again

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 24, 2019 0
Engaged ,
Jun 24, 2019

Copy link to clipboard

Copied

Not at my computer to test right now. Would think it would return false but it’s wrong anyway. This will return after the first effect whether found Transform or not. My example only returned false if you got through full for loop without finding a match.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 24, 2019 0
Engaged ,
Jun 24, 2019

Copy link to clipboard

Copied

Looking at it again, my guess is it's coming up undefined if there are no effects. No effects means numProperties equals 0 so it will never go into the for loop where you now have it returning false. i.e. it will return nothing if there are no effects, true if the first effect is called Transform, otherwise false.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 24, 2019 1
Hugovdd LATEST
New Here ,
Jun 25, 2019

Copy link to clipboard

Copied

Ah yes, that makes sense.

Thanks for your help, really appreciate it!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 25, 2019 0