Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Forums Adobe After Effects Expressions ‘Infallible Scale Expression’ – Problems with auto scaling images to comp size

  • ‘Infallible Scale Expression’ – Problems with auto scaling images to comp size

  • malcolm ricci

    November 21, 2019 at 4:28 pm

    Hey guys,

    I’m pretty new to expressions but I’m currently trying to set up a template which could potentially have images auto scaled according to the comp size, while still retaining their aspect ratio.

    So I’ve come across this link: https://aefreemart.com/the-infallible-scale-expression/

    The expression he uses is the following:

    w = thisLayer.width;
    h = thisLayer.height;
    compW = thisComp.width;
    compH = thisComp.height;
    p = thisComp.pixelAspect;
    if ( w > h ){ //test if image is taller than wide
    s = (compW / w ) * 100;
    [ s * p, s ]
    }else{
    s = (compH / h ) * 100;
    [ s , s ]
    }

    In general it seems to work, however I’ve had instances where the image still does not fill the composition completely.

    Apart from this, I’m having issues when it comes to importing images that are square rather than rectangular. I’ve tried to include an ‘else if’ prompt to the expression but haven’t managed to figure it out yet.

    Does anyone know of a similar expression to the above that is a little more fool-proof?

    Also, could anyone help me with including an ‘else if’ clause for square images?

    Thanks and much appreciated.

    w = thisLayer.width;
    h = thisLayer.height;
    compW = thisComp.width;
    compH = thisComp.height;
    p = thisComp.pixelAspect;
    if ( w > h ){ //test if image is taller than wide
    s = (compW / w ) * 100;
    [ s * p, s ]
    }else{
    s = (compH / h ) * 100;
    [ s , s ]
    }

  • Dan Ebberts

    November 22, 2019 at 1:33 am

    Try this:


    w = thisLayer.width;
    h = thisLayer.height;
    try{
    p = thisLayer.source.pixelAspect;
    }catch(e){
    p = 1;
    }
    compW = thisComp.width;
    compH = thisComp.height;
    compP = thisComp.pixelAspect;
    if ((w*p)/h > (compW*compP)/compH){
    s = ((compW*compP)/(w*p))*100;
    }else{
    s = (compH/h)*100;
    }
    [s,s]

    Dan

  • malcolm ricci

    November 22, 2019 at 12:41 pm

    Thanks for this Dan.

    So it does seem to work and fit the image better than it was before, however it is still not filling out the whol comp.

    https://i1.creativecow.net/u/281575/screenshot2019-11-2213.38.57.png

    Here’s a screenshot of the comp. I hope that the link works.

    Would there be a way for the image to fill out all of the comp while still being centre aligned?

    Thanks.

  • Dan Ebberts

    November 22, 2019 at 5:30 pm

    So you want the image to be cropped (either top and bottom, or left and right) to fill the comp? Or did you want it stretched? Is pixel aspect ratio an issue, or is it always square pixels?

    In any case, a scale expression won’t take care of alignment issues. Are the images not centered (with anchor point centered) to begin with?

    Dan

  • malcolm ricci

    November 27, 2019 at 3:31 pm

    Apologies for my delayed response.

    So ideally I’d like the comp to be completely filled out with the image. Now if the image were a square then it would fill out the comp entirely while still maintaining it’s aspect ration, which would lead to it having some of it’s height spilling over the top and bottom.

    I will try to keep on experimenting with the expression that you sent me as well as making sure the image is centrally aligned and take it for there.

    Thanks for the input 🙂

  • Dan Ebberts

    November 27, 2019 at 4:49 pm

    >So ideally I’d like the comp to be completely filled out with the image.

    Then I think you just need to swap two lines like this:


    w = thisLayer.width;
    h = thisLayer.height;
    try{
    p = thisLayer.source.pixelAspect;
    }catch(e){
    p = 1;
    }
    compW = thisComp.width;
    compH = thisComp.height;
    compP = thisComp.pixelAspect;
    if ((w*p)/h > (compW*compP)/compH){
    s = (compH/h)*100;
    }else{
    s = ((compW*compP)/(w*p))*100;
    }
    [s,s]

    Dan

  • malcolm ricci

    December 6, 2019 at 9:26 am

    Thanks for this.

    Apologies for the delay, but it’s working exactly as I need it to now ☺

    Malcolm.

  • malcolm ricci

    July 2, 2020 at 5:33 pm

    Hey guys,

    I know that this is a bit of an old thread, and Dan very kindly provided the expression that I was after and this has worked fine, however I’m having a little trouble with it again.

    So the set up that I’m working with is that I create an automated template and have the client fill this in remotely, via third-party software. Now the expression that Dan provided allows the images to auto-scale to fit, so whatever image the client provides, it will automatically scale and fit into the required composition.

    Now this has been working fine for the last 6 months or so, however I have only ever used it with .jpegs. Today I created a template which required .pngs and not .jpegs. When I tested out the expression on my computer, it seemed to work fine and accepted the new .pngs and scaled them and everything, however when working remotely it seems that After Effects is bypassing/ignoring the expression completely and just importing the .png with it’s original size.

    The only conclusion that I can come to at this point is that I created the template on cc2020 and the remote server works with CC2019 (first project doing so), however I was under the impression that expressions work across different versions as they are universal pieces of code.

    Could there be something else that I’m missing? As I’m really wrecking my brain on this one.

    If anyone has ever encountered anything similar and has any advice or feedback I’d really appreciate it.

    Thanks,
    Malcolm

  • Filip Vandueren

    July 2, 2020 at 7:31 pm

    In CC 2020 check the Project settings and set the Javascript-engine to legacy-extendscript.

    However, I don’t see any code in this expression that would break or be interpretated differently by the old vs thee new Javascript engine.

    Can you verify that the expressions are active but providing the wrong numbers, or are they throwing an error and disabled ?

  • malcolm ricci

    July 3, 2020 at 7:33 am

    Hey Filip,

    Thanks for your reply. Since writing this post I found out that CC2019 and 2020 have the option of changing the engine from Javascript-engine to legacy-extendscript, however when checking it it seems that it was already set to legacy-extendscript.

    In the meantime, I am trying to figure out whether the expressions are active but providing the wrong numbers, or are they throwing an error and disabled, but I am not 100% sure how to go about this as the network that I set up is using an After Effects render engine rather than the full version of After Effects.

    I feel I may be a little over my head with this yet at the same time think that it is something very simple that I am overlooking.

    I am going to install the full version of After Effects on the network and open it and see whether the expressions are still active or not and take it from there.

    What baffles me the most is that I have been using this set up for around six months now and had absolutely no issues with the expression with regard to .jpegs, so there must be something going on with the fact that they are .pngs rather than .jpegs.

    If anyone else has come across this issue I’d be really grateful for any other feedback on this. In the meantime, I will post my findings in case someone else is also stuck with this.

    Thanks,
    Malcolm

Viewing 1 - 10 of 15 posts

Log in to reply.

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