ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Autoscale layers to both height and width

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Tom Theys
Autoscale layers to both height and width
on Oct 24, 2019 at 1:17:19 pm

Hi guys, I hope someone could help me with this one. I'm trying to scale 1, 2 or 3 text lines to a maximum height and maximum width.
The 3 lines are all in seperated comps beacause the have all sort of masks applied to them. There is a main slider control to show 1, 2 or 3 rules. I got the script working with the width values, but can't seem to limit it to the maximum height. All 3 rules are in a precomps and the precomp is linked to a null who controls the scale. I think I made the script way to complicated and I'm a bit lost atm.

Here's the script so far

var maxw = 1540;
var maxh = 880;

var nr_rules = effect("nr_regels")("Slider");

var r1 = comp("txtFill s1r1").layer("txt-w1");
var r2 = comp("txtFill s1r2").layer("txt-w1");
var r3 = comp("txtFill s1r3").layer("txt-w1");

var r1width = r1.sourceRectAtTime(sourceTime(time), true).width;
var r2width = r2.sourceRectAtTime(sourceTime(time), true).width;
var r3width = r3.sourceRectAtTime(sourceTime(time), true).width;

var r1height = r1.sourceRectAtTime(sourceTime(time), true).height;
var r2height = r2.sourceRectAtTime(sourceTime(time), true).height;
var r3height = r3.sourceRectAtTime(sourceTime(time), true).height;

var h1 = maxh / r1height * 100;
var h2 = maxh / r2height * 100;
var h3 = maxh / r2height * 100;

var w1 = maxw / r1width * 100;
var w2 = maxw / r2width * 100;
var w3 = maxw / r3width * 100;

var totalh = h1 + h2 + h3;
var totalh = nr_rules == 2 ? h1 + h2 : totalh;
var totalh = nr_rules == 1 ? h1 : totalh;

if (h1 > 120 && w1 > 120) { if (totalh > 600) { w1 = maxh / r1width * 100 } else { w1 = 120 }} else { w1 = maxw / r1width * 100 };
if (h2 > 120 && w2 > 120) { if (totalh > 600) { w2 = maxh / r2width * 100 } else { w2 = 120 }} else { w2 = maxw / r2width * 100 };
if (h3 > 120 && w2 > 120) { if (totalh > 600) { w3 = maxh / r3width * 100 } else { w3 = 120 }} else { w3 = maxw / r3width * 100 };

var s = Math.min(w1,w2,w3);
var s = nr_rules == 2 ? Math.min(w1,w2) : s;
var s = nr_rules == 1 ? w1 : s;

[s,s]

So the purpose is when you set the slider to 1 rule the total maximum width is 1540 and maximimum height is 400. And when you select 2 it looks at the widest of the two, and the same for when you set the slider to 3 rules. All rules together (either 1,2 or 3) can't be higher then 880.

Thanks in advance!


Return to posts index

Tom Theys
Re: Autoscale layers to both height and width
on Oct 24, 2019 at 3:50:11 pm

Made a better version, but still need to look at the maximum height

var maxw = 1540;
var maxh = 800;

var nr_rules = effect("nr_regels")("Slider");

var r1 = comp("txtFill s1r1").layer("txt-w1");
var r2 = comp("txtFill s1r2").layer("txt-w1");
var r3 = comp("txtFill s1r3").layer("txt-w1");

var r1w = r1.sourceRectAtTime().width;
var r2w = r2.sourceRectAtTime().width;
var r3w = r3.sourceRectAtTime().width;

var totalw = Math.max(r1w,r2w,r3w);
var totalw = nr_rules == 2 ? Math.max(r1w,r2w) : totalw;
var totalw = nr_rules == 1 ? r1w : totalw;

var r1h = r1.sourceRectAtTime().height;
var r2h = r2.sourceRectAtTime().height;
var r3h = r3.sourceRectAtTime().height;

var totalh = r1h + r2h + r3h;
var totalh = nr_rules == 2 ? r1h + r2h : totalh;
var totalh = nr_rules == 1 ? r1h : totalh;

var sw = maxw / totalw * 100;
var sh = maxh / totalh * 100;

var tot = 100;

if (sw >= 140) tot = 140 else tot = sw;

[tot,tot]


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2019 CreativeCOW.net All Rights Reserved
[TOP]