Page 1 of 1

Scale layer down relative to comp

Posted: July 8th, 2004, 4:18 am
by byronnash
I am working on a script that takes text out of a file and copies it into existing text layers. I'd like to add in a function that scales the text layer down if it is too large for the screen. This is what I have basically.


var proj = app.project;

var myComp = proj.item(1);

var myLayer = myComp.layer(1);

var layerSize = myLayer.width * (myLayer.scale * .01)

while (layerSize >= 300){

var sclIncr = 100;

myLayer.scale.setValue([sclIncr,sclIncr]);

sclIncr = sclIncr - 2

}


The problem I have is myLayer.scale doesn't return a value, it returns an array. How can I get one of the values out of the array?


Thanks.

Posted: July 8th, 2004, 1:51 pm
by Paul Tuersley
myLayer.scale returns the actual scale property. To test this, put in the line:
alert(myLayer.scale);

You'll get an alert that says: [object Property]. To access the scale value use:
myLayer.scale.value

That gives you the 2 or 3 dimensional scale value array. To access a single value
you'd use something like:
myLayer.scale.value[0]

With arrays you access a single value by specifying the index, i.e that value's
position in the array, starting with an index of 0. (x is [0], y is [1] and z is [2])


I realise this is a work in progress but there are a few other things I noticed.
The 'while' loop currently either won't run (if layerSize is less than 300) or will
run in an infinite loop (layerSize is greater or equal to 300) as there is nothing
inside the loop that changes the 'layerSize' variable.

It looks like you want to scale the layer down to a certain size, maybe something
like this would work:

if (layerSize > myComp.width) {
var multiplier = myComp.width / layerSize;
myLayer.scale.setValue(myLayer.scale.value * multiplier);
}

I've used myComp.width, but you could replace them with '300'.

Also, it's generally good practice to put open/close brackets at the start/end of
your script to limit the scope of the variables you define. There's a good
explanation for this on Dan Ebbert's site:
http://www.motionscript.com/ae-scriptin ... ted-3.html

Paul T

Posted: July 9th, 2004, 4:30 am
by byronnash
Thanks for te reply Paul. I am still pretty new at scripting, I have to hack my way through everything.

This site is a great idea, I hope it catches on fast. I think After Effects is much more potent now that you can script it.

Take care...

Byron

Re: Scale layer down relative to comp

Posted: April 10th, 2015, 10:24 am
by Salwa
There doesn't seem to be anything in the Scripting Guide to suggest this is