FORUMS: list search recent posts

AE Script to Scale Font size to addBoxText

COW Forums : Adobe After Effects

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Jonathan Wendt
AE Script to Scale Font size to addBoxText
on Nov 2, 2015 at 7:49:36 pm

I'm trying to come up with a script that will create text layers, and then size the font within it appropriately to fit to the width of the box. I have searched a bunch and (slightly) modified another script to get me started. It can successfully open a txt file and create the layers from each line, but since they are all set to the default font and font size, if the text on the line is too long it wraps it to a second line, whereas I would like it to decrease the size until it fits on one line. Once I get all the sorted out I will be trying to configure it to possibly add a bit of padding and center it in the composition. Most of the research I've done has referred to using a hack-around of sample image, but this generally seems to be outdated as scripts have access to text-properties. If anyone could point me in the right direction it would make my life way easier. The code so far is below, and thanks in advance!

//
// createTextLayersFromFile.jsx
//

//
// This script reads a user specified text file and
// creates a text layer for each line of text in a
// new comp called "my text comp"
//

{

// create undo group

app.beginUndoGroup("Create Text Layers From File");

// Prompt user to select text file

var myFile = File.openDialog("Please select input text file.");
if (myFile != null){

// open file
var fileOK = myFile.open("r");
if (fileOK){

// read text lines and create text layer for each
// until end-of-file is reached

var myComp = app.project.activeItem;
var text;
while (!myFile.eof){
text = myFile.readln();
compW = myComp.width
compH = myComp.height
if (text == "") text = "\r" ;
myComp.layers.addBoxText([compW, compH], text);
}

// close the file before exiting

myFile.close();

}else{
alert("File open failed!");
}

}else{
alert("No text file selected.");
}

app.endUndoGroup();
}


Return to posts index

Walter Soyka
Re: AE Script to Scale Font size to addBoxText
on Nov 3, 2015 at 2:47:15 pm

As of Ae 13.2, scripting has access to dynamic layer bounds, including text layers. See this blog post for more:
http://blogs.adobe.com/aftereffects/2014/12/after-effects-cc-2014-2-13-2.ht...

Here's quick code example to get the width of a text layer:
var textLayerWidth = myTextLayer.sourceRectAtTime().width;

Scripts do not have the ability to set the text size; you'd have to get the width of the layer, then set its scale to achieve the same effect.

Walter Soyka
Designer & Mad Scientist at Keen Live [link]
Motion Graphics, Widescreen Events, Presentation Design, and Consulting
@keenlive [twitter]   |   RenderBreak [blog]   |   Profile [LinkedIn]


Return to posts index

Jonathan Wendt
Re: AE Script to Scale Font size to addBoxText
on Nov 3, 2015 at 3:02:02 pm

Thank you for your reply, Walter. I'm not sure exactly how to apply your response to my scenario though. I already know the width of the text layer, because I'm creating it from the width of the composition, if you see in the code. And also, scripts do have the ability to set the text size, but I don't know how to implement the next step where it knows how to dynamically find the right size for the text supplied to fit within the box without wrapping to a second line. If you check out the code below you can see an example of it being able to set lots of properties on the text.


//
// This script reads a user specified text file and
// creates a text layer for each line of text
//

{

// create undo group

app.beginUndoGroup("Create Text Layers From File");

function setTextSpec(){
t.resetCharStyle();
t.resetParagraphStyle();
t.fontSize = 25;
t.font = "Helvetica";
t.text = text;
t.justification = ParagraphJustification.CENTER_JUSTIFY;
};

// Prompt user to select text file

var myFile = File.openDialog("Please select input text file.");
if (myFile != null){

// open file
var fileOK = myFile.open("r");
if (fileOK){

// read text lines and create text layer for each
// until end-of-file is reached

var myComp = app.project.activeItem;
var presetPath = "/Applications/Adobe After Effects CS6/Presets/Text/Animate In/Slow Fade On.ffx";
var text;
while (!myFile.eof){
text = myFile.readln();
compW = myComp.width
compH = myComp.height
if (text == "") text = "\r" ;
layer = myComp.layers.addBoxText([compW, compH], text);
p =layer.text.sourceText;
t = p.value;
setTextSpec();
p.setValue(t);

}

// close the file before exiting

myFile.close();

}else{
alert("File open failed!");
}

}else{
alert("No text file selected.");
}

app.endUndoGroup();
}


Return to posts index


Walter Soyka
Re: AE Script to Scale Font size to addBoxText
on Nov 3, 2015 at 3:24:12 pm

My bad on setting size -- not sure what I was thinking there!

I would create the text layer, but as point text, not as paragraph text (addText, not addBoxText). I'd then adjust the size of the text for the layer, testing its width, until I got what I wanted.

Walter Soyka
Designer & Mad Scientist at Keen Live [link]
Motion Graphics, Widescreen Events, Presentation Design, and Consulting
@keenlive [twitter]   |   RenderBreak [blog]   |   Profile [LinkedIn]


Return to posts index

Jonathan Wendt
Re: AE Script to Scale Font size to addBoxText
on Nov 3, 2015 at 8:10:13 pm

Walter, once again thank you for your reply! Your suggestion to do it as point text and then scaling it makes total sense to me. But being new to scripting I'm making a mess out of it. I'm able to create the text layer, read it's width and compare it to the composition.

//
// createTextLayersFromFile.jsx
//

//
// This script reads a user specified text file and
// creates a text layer for each line of text
//

{

// create undo group

app.beginUndoGroup("Create Text Layers From File");

// Prompt user to select text file

var myFile = File.openDialog("Please select input text file.");
if (myFile != null){

// open file
var fileOK = myFile.open("r");
if (fileOK){

// read text lines and create text layer for each
// until end-of-file is reached

var myComp = app.project.activeItem;
var text;
while (!myFile.eof){
text = myFile.readln();
compW = myComp.width
compH = myComp.height
if (text == "") text = "\r" ;
myTLayer = myComp.layers.addText(text);
p =myTLayer.text.sourceText;
t = p.value;
t.fontSize = 79
p.setValue(t);
var my2Layer = app.project.activeItem.layer(text);
var r = my2Layer.sourceRectAtTime(0, false);
while (r.width >= compW){
t.fontSize = t.fontSize - 1;
p.setValue(t);
alert(r.width)
}
}

// close the file before exiting

myFile.close();

}else{
alert("File open failed!");
}

}else{
alert("No text file selected.");
}

app.endUndoGroup();
}


So r.width represents the width of the text layer, and compW is what it is being compared to. However in the statement that I made, r.width never seems to reflect the change in size after it goes through each time. So it reduces the font down to 0 and errors out. I'm sure it's something pretty simple, but I've been banging my head against the wall all day. Thanks again in advance.


Return to posts index

Walter Soyka
Re: AE Script to Scale Font size to addBoxText
on Nov 3, 2015 at 8:19:59 pm

Don't forget to update r.width inside your loop. You get the initial width, but then you never get it again after you update the font size.

Walter Soyka
Designer & Mad Scientist at Keen Live [link]
Motion Graphics, Widescreen Events, Presentation Design, and Consulting
@keenlive [twitter]   |   RenderBreak [blog]   |   Profile [LinkedIn]


Return to posts index


Jonathan Wendt
Re: AE Script to Scale Font size to addBoxText
on Nov 3, 2015 at 8:34:51 pm

Oh man, thank you. I knew that was the problem somehow but just wasn't seeing it for staring at it so long. By any chance do you know how to apply vertical center alignment to the layer? I know I can do t.justification = ParagraphJustification.CENTER_JUSTIFY and it centers it horizontally, now I'd like to do the same on the vertical axis. The button on the align panel works perfectly, but I don't see how to call it in the cs6 scripting guide.


Return to posts index

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