Getting the width and height of a text strip

I’ve been making an addon for myself, for auto adding subtitles as text strips in the VSE.(And yes there are other software that do it better and faster…but after a week in developing the addon I prefer using blender)

While I did made the addon and use it. I had to auto adjust the position of the text strip by giving some “magic numbers” because the Python API doesn’t expose the underlying data for the rectangle created for the text strip object. In order to properly position the text (especially when there is a background rectangle added) on the screen, I need to know the bounding box of the text or the rectangle behind it.
How can I get that data or if I cannot, what is the calculation formula that’s used for calculating the background rectangle? So I can reverse it in my code.
At the moment with using magic numbers I’ve managed to have it where the addon can somewhat position the boxes properly…but usually that’s a bit off.
Here are demonstrations of what usually is the case.(the projects I’m working are NDA):
sometimes it’s correct:
image
But most of the times it either intersects or is separate.
image image

Now if I somehow can get a reliable data, or guess of the size of a text strip’s bounding box, I can go ahead and even add support for format like .ssa( SubStation Alpha) subtitle format for different styling of different text strips or having a part of the line be in one style while the other part in a different style(mainly color).
In subtitle edit software


In Blender

2 Likes

I am in the process of doing some very similar. I was pointed at the blf module and in particular at blf.dimensions by @testure:

However, I face the problem of not being able to determine the fontid of the text strip’s font:

What I want to do works great if I figure out the fontid manually, or guess at the offset. If you’re doing this only for yourself and not looking for a generic solution you can use this method (blf.size() followed by blf.dimensions()) for the font you are using for your subtitles. Hope this keeps you moving! :slight_smile:

PS You may also be interested in another effort to create a subtitle editor in Blender. I have no affiliation, but I have used that user’s 'outline text strips’ script (now an addon) before:

actually I kind of dropped the project because there is no way to do that. I’ve looked at the C code(as much as I understand it) and it looks like there is such a value but 1. It’s not tracked(it looks like it’s not part of the font struct) and 2. it’s a local variable to a function.

So I had two options. Make my own OpenGL text rendering and plug that to the viewport and final render.
Or someone suggested I do this with the actual text object and instead use a scene strip. where I have that text object. But I thought…well if I can add a scene strip with the text, then I could make the text with Geometry Nodes.

In the end I managed to do what I wanted initially(no extra features).

However the issue now is that text should change via a modus operator that is always running in the background to check the current frame and assign the respective text to the string nodes.
Then again there is the fact that I can’t export Geometry Nodes as a separate file that I will load. Some other people said that I should just ship with that blend scene already setup. And just perform an import via Python when the plugin is started.
The other solution to this is to recreate every node with code calls, but at the time I made this I figured it will take me too long to recreate the tree code line by code line. So at the moment I stopped working on that.

Ah that’s a shame. I’m using a workaround which is to perform a blf.load() of the same font, getting back the same fontid that already exists for that font.it works well for my purposes- text split using values from blf.dimensions() matches the un-split text strip exactly:

I hadn’t thought of a node-based approach, that looks interesting. Good luck if you ever decide to revisit it!

1 Like