I am trying to write some text on a panel that I created, like this:

box.label("Some long text that is out of borders")

but because the text is too long, the panel only shows the text in one line and does not make the text stop at the edge of the panel and start a new line.

So it appears like this:

Some long text...ers

How can I make it contained within the panel's width?


I know I'am answering my own question, because I knew how to do it only after posting the question, so for anyone who may need this answer here it goes.

I used a wrap function that I made my self, like this:

def wrap(width, text):

  lines = []

  arr = text.split()
  lengthSum = 0

  strSum = ""

  for var in arr:
    lengthSum+=len(var) + 1
    if lengthSum <= width:
      strSum += " " + var
      lengthSum = 0
      strSum = var

  lines.append(" " + arr[len(arr) - 1])

  return lines

The width argument is the width of the panel in terms of number of characters that fit in there. Then in the draw function I got the width of the panel like this:

tool_shelf = None
area = bpy.context.area

for region in area.regions:
  if region.type == 'TOOLS':
    tool_shelf = region

Where tool_shelf is now the panel I created, then I call the wrap function with the width and text like this:

lines = wrap(math.ceil(tool_shelf.width / 9), "Some long text that is out of borders")

I divided by 9 because tool_shelf.width gives back the width in pixels, and I assumed 9 pixels for each character, now lines is an array where every element is a line. Then I write the lines in the panel like this:

for var in lines:
  row = layout.row(align = True)
  row.alignment = 'EXPAND'

I know this is not the optimal way, but anybody can get the main idea from this code and optimize it to their needs.

Note: function wrap is not in any class, but all the other codes are inside the draw function of the class Panel.


Another solution is to use python's textwrap module:

import textwrap 

textTowrap = "Your text......................................"      
wrapp = textwrap.TextWrapper(width=50) #50 = maximum length       
wList = wrapp.wrap(text=textTowrap) 

#Now in the panel:
for text in wList: 
    row = layout.row(align = True)
    row.alignment = 'EXPAND'
  • $\begingroup$ There is no documentation in Blender's API, but this is a python module present in Blender. textwrap docs: docs.python.org/3.7/library/textwrap.html $\endgroup$
    – Noob Cat
    May 15 '20 at 21:07
  • $\begingroup$ You can also add this row.scale_y = 0.6 somewhere before row.label(text) so to increase or decrease the space between lines. $\endgroup$ Dec 15 '20 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.