Created
November 12, 2021 20:29
-
-
Save semagnum/b881b3b4d11c1514dac079af5bda8f7f to your computer and use it in GitHub Desktop.
text wrap for Blender UI panels (credit to Graswald3d in his G Scatter addon)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import blf | |
import bpy | |
def wrap_text(text: str, context): | |
return_text = [] | |
row_text = '' | |
width = context.region.width | |
system = context.preferences.system | |
ui_scale = system.ui_scale | |
width = (4 / (5 * ui_scale)) * width | |
dpi = 72 if system.ui_scale >= 1 else system.dpi | |
blf.size(0, 11, dpi) | |
for word in text.split(): | |
word = f' {word}' | |
line_len, _ = blf.dimensions(0, row_text + word) | |
if line_len <= (width - 16): | |
row_text += word | |
else: | |
return_text.append(row_text) | |
row_text = word | |
if row_text: | |
return_text.append(row_text) | |
return return_text | |
class HelloWorldPanel(bpy.types.Panel): | |
"""Creates a Panel in the Object properties window""" | |
bl_label = "Hello World Panel" | |
bl_idname = "OBJECT_PT_hello" | |
bl_space_type = 'PROPERTIES' | |
bl_region_type = 'WINDOW' | |
bl_context = "object" | |
def draw(self, context): | |
layout = self.layout | |
my_text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.' | |
for text in wrap_text(my_text, context): | |
layout.label(text=text) | |
def register(): | |
bpy.utils.register_class(HelloWorldPanel) | |
def unregister(): | |
bpy.utils.unregister_class(HelloWorldPanel) | |
if __name__ == "__main__": | |
register() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment