Skip to main content
Participant
November 5, 2021
Question

scripting: create textlayer with mixed styles

  • November 5, 2021
  • 4 replies
  • 883 views

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

This topic has been closed for replies.

4 replies

Meng Zhiqun
Inspiring
November 6, 2021

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();

 

 

Mathias Moehl
Community Expert
Community Expert
November 6, 2021

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.

Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects
Roland Kahlenberg
Legend
November 5, 2021

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

Very Advanced After Effects Training | Adaptive &amp; Responsive Toolkits | Intelligent Design Assets (IDAs) | MoGraph Design System DEV
Meng Zhiqun
Inspiring
November 5, 2021

Hi, I'm curious on this. Does inserting expression to the textlayer via scripting works in your situation?

Participant
November 5, 2021

No, I thought i read about it, but on closer inspection it wasn't exactly that: 

https://blog.adobe.com/en/2020/01/24/after-effects-2020-express-yourself-and-your-text.html#gs.fih75p

Inspiring
November 5, 2021

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.

 

 

Participant
November 5, 2021

You are right, there is no setStyleAt()