Determining Width and Height of a Text Layer

Find out why the . goes before the /

Moderator: Paul Tuersley

byronnash
Posts: 321
Joined: July 7th, 2004, 2:30 pm
Location: Charlotte, NC
Contact:

I tried putting the text layer into the activeItem and then running the app.executecommand(). It still fails to create the mask. I'm not sure what's going wrong. The selected text layer is in the activeItem. Are there any other caveats to know?
nab
Posts: 203
Joined: November 29th, 2005, 3:00 am
Location: Royan
Contact:

Hey guys,

Here is another technique to determine the size of a text layer.

The trick is to notice that when you "Fit to Comp Width (or Height)" a text layer, the layer is correctly scaled to fit the comp size (because internally AE somehow knows the size of the text). The new scale could then be used to compute the width and height of the text.

The following snippet illustrates this technique (create a text layer and execute this code):

Code: Select all

// assume a 2D text layer with no keyframes is selected 
var comp = app.project.activeItem;
var textLayer = comp.selectedLayers[0];

// store initial transforms
var initialPosition = textLayer.position.value;
var initialScale = textLayer.scale.value;
var initialRotation = textLayer.rotation.value;

// fit to comp width and store new x-scale
var cmd1 = "Fit to Comp Width";
app.executeCommand(app.findMenuCommandId(cmd1));
var modifiedXScale = textLayer.scale.value[0]; 

// fit to comp height and store new y-scale
var cmd2 = "Fit to Comp Height";
app.executeCommand(app.findMenuCommandId(cmd2));
var modifiedYScale = textLayer.scale.value[1]; 	

// compute text size
var textWidth = initialScale[0] * comp.width / modifiedXScale;
var textHeight = initialScale[1] * comp.height / modifiedYScale;

// reset transforms to their initial values
textLayer.position.setValue(initialPosition);
textLayer.scale.setValue(initialScale);
textLayer.rotation.setValue(initialRotation);

// display result
alert("Text size in pixels:\r" + textWidth + " x " + textHeight);
// place this layer below the text to check that the size is correct
comp.layers.addSolid([1,0,1], "Foo", Math.round(textWidth), Math.round(textHeight), comp.pixelAspect, comp.duration);
Note that when the text is parented to another layer, additional calculations are required. This could be a bit more difficult especially when the parent's scale is not the default, and with non uniform scaling.

The 'mask trick' I described here is still my favorite method :)

---
@Byron: I just did a quick test (sorry I didn't see your last post), and it has worked for me. You have to create a new text layer in the active comp with the same sourceText as the original layer. Do the mask trick or the technique descibed above, and then remove this temporary layer...
User avatar
lloydalvarez
Enhancement master
Posts: 460
Joined: June 17th, 2004, 9:27 am
Location: New York City, NY
Contact:

Brilliant as usual Nab! Thanks for sharing.

-Lloyd
Post Reply