scripting: create textlayer with mixed styles
Copy link to clipboard
Copied
Hi,
I need to create textlayers from some documents, and some words should have another font or be bold or whatever.
So much i fugured out:
- not possible via the scripting API
- not possible via the SDK (just a quick glance 5 minutes ago...please correct me)
- expressions have .setStyleAt() which would be great in the script API, but I don't see how it could solve my problem as an expression
- copy and paste mixed formatted texts inside AE works, but no idea if I could make use of that somehow
Anything I didn't think of? Is there a roadmap for adding setStyleAt() to the scripting API?
Any help is welcome!
Cheers,
Thomas
Copy link to clipboard
Copied
I'm not aware there is even a setStyleAt() expression method, just a getStyleAt() method for grabbing a style from a specific character. I think expressions have the same restrictions of not being able to set multiple styles on a single layer.
So there is no good solution for this sadly. It still comes down to using multiple layers, and at best jumping through hoops working out individual text layer bounds for positioning if you were to try to arrange these different layers. You could also use text animators on multiple duplicate layers to set 0% opacity on one range of characters then the reverse on the other layer, but once you've changed the style there's a high chance the characters across the two layers won't line up.
Copy link to clipboard
Copied
You are right, there is no setStyleAt()
Copy link to clipboard
Copied
Hi, I'm curious on this. Does inserting expression to the textlayer via scripting works in your situation?
Copy link to clipboard
Copied
No, I thought i read about it, but on closer inspection it wasn't exactly that:
Copy link to clipboard
Copied
Existing constructs restrict One Text Layer to One Text Style. As Paul has stated, you'll need to link together multiple Text Layers; to cater to as many Text Styles as you require. HTH
Copy link to clipboard
Copied
How about this?
You can play around with the fontAry.
Important points to note is that AE doesn't really have a method to set Regular, Bold, Italic styles. It's mostly set by a "-", leading to the style. The fontname + style is also very case and space sensitive and some fonts are just harder to get right like Arial.
var proj = app.project;
var itemTotal = proj.numItems;
var projComp = proj.activeItem;
var fontSize = 40;
var margin = 5;
app.beginUndoGroup("CreateText");
var fontAry = ["Impact","MyriadPro-Bold","NuevaStd-Italic","ArialMT"];
for (var fi=0;fi<=fontAry.length-1;fi++){
var curFont = fontAry[fi];
var curLay = projComp.layers.addText("text1");
try{
curLay.text.sourceText.expression =
"thisProperty.createStyle().setFontSize(\""+fontSize+"\").setFont(\""+curFont+"\").setText(\""+curFont+"\");";
}catch(e){}
if(fi == 0){
var curLayW = curLay.sourceRectAtTime(0,true).width/2;
curLay.position.setValue([curLayW,projComp.height/2]);
}else if(fi > 0){
var preLayPosX = projComp.layer(curLay.index+1).transform.position.value[0];
var preLayW = projComp.layer(curLay.index+1).sourceRectAtTime(0,true).width/2;
var curLayW = curLay.sourceRectAtTime(0,true).width/2;
curLay.position.setValue([preLayPosX+curLayW+preLayW+margin , projComp.height/2]);
}
}
app.endUndoGroup();
Copy link to clipboard
Copied
Workaround:
My script TextExploder has a scripting API such that you can use it in your own scripts.
https://aescripts.com/textexploder/
API see here
https://mamoworld.com/article/textexploder-scripting-api
Hence, you could
1) create the text layer
2) split it into the different parts that need a different style (use the "custom word" or "custom regular expression" to isolate single words, for example)
3) style the resulting text layers individually
Of course, the end result is then not a single layer for each text, but one layer for each styled section.

