Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | Next revisionBoth sides next revision | ||
graphic:python:blender [2024/01/31 08:00] – [Blender variable naming standards] ying | graphic:python:blender [2024/02/01 09:13] – [Blender UI code] ying | ||
---|---|---|---|
Line 634: | Line 634: | ||
... | ... | ||
layout.template_list(type_name, | layout.template_list(type_name, | ||
+ | </ | ||
+ | * long label solution and auto wrap <code python> | ||
+ | import bpy | ||
+ | import textwrap | ||
+ | |||
+ | long_text = """ | ||
+ | a long text long text long text | ||
+ | b test | ||
+ | c long text long text long text long text long text long | ||
+ | d text long text long text long text """ | ||
+ | |||
+ | def get_max_label_width(): | ||
+ | # Get the 3D View area | ||
+ | for area in bpy.context.screen.areas: | ||
+ | if area.type == ' | ||
+ | break | ||
+ | # Calculate the width of the panel | ||
+ | panel_width = 280 # default value | ||
+ | for region in area.regions: | ||
+ | if region.type == ' | ||
+ | panel_width = region.width | ||
+ | return panel_width | ||
+ | break | ||
+ | # Calculate the maximum width of the label | ||
+ | uifontscale = 9 * context.preferences.view.ui_scale | ||
+ | max_label_width = int(panel_width // uifontscale) | ||
+ | return max_label_width | ||
+ | |||
+ | def create_long_label(layout, | ||
+ | max_label_width = get_max_label_width() | ||
+ | first_icon_line_done = False | ||
+ | rest_line_indent = indent | ||
+ | if icon == '': | ||
+ | first_icon_line_done = True | ||
+ | rest_line_indent = '' | ||
+ | # Split the text into lines and format each line | ||
+ | for line in long_text.splitlines(): | ||
+ | # Remove leading and trailing whitespace | ||
+ | line = line.strip() | ||
+ | # Split the line into chunks that fit within the maximum label width | ||
+ | for chunk in textwrap.wrap(line, | ||
+ | if not first_icon_line_done: | ||
+ | layout.label(text=chunk, | ||
+ | first_icon_line_done = True | ||
+ | else: | ||
+ | layout.label(text=rest_line_indent+chunk) | ||
+ | |||
+ | class MyPanel(bpy.types.Panel): | ||
+ | bl_idname = " | ||
+ | bl_label = "My Panel" | ||
+ | bl_space_type = " | ||
+ | bl_region_type = " | ||
+ | | ||
+ | def draw(self, context): | ||
+ | layout = self.layout | ||
+ | create_long_label(layout, | ||
+ | | ||
+ | |||
+ | # Register the panel | ||
+ | bpy.utils.register_class(MyPanel) | ||
</ | </ | ||
===== Blender preference ===== | ===== Blender preference ===== |