Multiline text in Blender interface panels

Unfortunately, Blender’s interface does not support word wrapping and multi-line text blocks. Text lines that are too long to be displayed in the panel are cut off. However, multiline text can still be displayed on the panel using several text elements instead of one.

First, let’s create a simple panel in the N-panel of the 3D Viewport window and place a label element with the desired text on it.

Define the panel class and register it.

After executing this code by pressing the “Run Script” button, we will create a new panel “MULTILINE TEXT” on which the text “Loren Ipsum …” will be placed in a single line.

To make the text multi-line, let’s split it into separate lines by spaces according to the panel width.

The width of the current panel in pixels can be obtained from the area context.

Let’s make a function that will split a text line passed into it so that the length of each line does not exceed the panel width. We can use the “textwrap” module for this.

We pass three parameters to the function:

  • context – the window context to get the panel width
  • text – text to be shown
  • parent – the element on the interface panel on which we will display the text

The “textwrap” module splits text by characters, not by pixels. So we need to count how many characters will fit into a line of the desired length (panel width). Let’s use an average character width of 7 pixels.

Next, we will get a list of lines into which the text was split.

Next, we create the required number of label elements, each of which contains the next line of text.

Having the function let’s replace the text output in our panel with a call to this function. Now all the text is placed on the panel line by line and will even adjust when the panel width changes.

The final code:

5 6 votes
Article Rating
Subscribe
Notify of
guest

9 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Rombout Versluijs
Rombout
1 month ago

PS id like to add something if you add this piece of code. The lines or line height is much closer. Looks better and reads more clear

Rombout Versluijs
Rombout
1 month ago
Reply to  Nikita

Perhaps it causes issues, i;m seeing lines are abbreviated or shortened, in the middle i get dots, indicated each lines is not showing the full text

Rombout Versluijs
Rombout
1 month ago
Reply to  Rombout

I fixed it, im using it for an info popup dialog, which contains description text for model library. If i use context.region.width, it does seem to get to correct width of the popup dialog. So i hardcode the same width which invoke_props_dialog width has

Rombout Versluijs
Rombout
1 month ago
Reply to  Nikita
Rombout Versluijs
Rombout
1 month ago

Sweet, still dont get it why dont simply add such a thing

Nameless
Nameless
2 years ago

You are a genius, thank you so so much, this is AWESOME AND VERY SMART!