1
$\begingroup$

I created a ui list in which each item in the list has 3 properties--name, type, and number. In the list in my panel, I would like to have the type and number, but not the name. Here is my Python code:

import bpy
from bpy.types import  PropertyGroup, UIList, Operator, Panel
from bpy.props import StringProperty, FloatProperty, CollectionProperty, IntProperty

class CustomItem(PropertyGroup):
    """Group of properties representing an item in the list."""

    name: StringProperty(name="Name")

    type: StringProperty(name="Type")

    number: FloatProperty(name="min",)

class LIST_OT_NewItem(Operator):
    """Add a new item to the list."""
    bl_idname = "my_list.new_item"
    bl_label = "Add an item"

    def execute(self, context):
        item_list=context.scene.my_list
        item_value=context.scene.my_list_item
        item=item_list.add()
        item.name = "Name"
        item.type = "Height"
        item.number = context.scene.my_list_item
        return{'FINISHED'}

class UI_TEST_LIST(UIList):
    """Demo UIList."""

    def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):

        custom_icon = None
        split = layout.split(factor=0.3)
        split.label(text = item.type)
        split.label(text = str(item.number))

        # Make sure your code supports all 3 layout types
        if self.layout_type in {'DEFAULT', 'COMPACT'}:
            layout.label(text=item.name)

        elif self.layout_type in {'GRID'}:
            pass
        return
  
class VIEW3D_PT_ListTestPanel(bpy.types.Panel):
    """Test List Panel"""
    bl_label = "List"
    bl_idname = "VIEW3D_PT_ListTestPanel"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = "List"
    
    def draw(self, context):
        layout = self.layout
        row = layout.row()
        row.template_list("UI_TEST_LIST", "Test List", context.scene, "my_list", context.scene, "my_list_index",rows = 5)
        row = layout.row()
        row.prop(context.scene,"my_list_item")
        row.operator("my_list.new_item",text="Add")


classes = (VIEW3D_PT_ListTestPanel, UI_TEST_LIST, LIST_OT_NewItem, CustomItem)

def register():
    for cls in classes:
        bpy.utils.register_class(cls)
    bpy.types.Scene.my_list = CollectionProperty(type=CustomItem)
    bpy.types.Scene.my_list_index = IntProperty(name="my_list_index")
    bpy.types.Scene.my_list_item = FloatProperty(name="my_list_item")
        
def unregister():
    for cls in reversed(classes):
        bpy.utils.unregister_class(cls)

Even if I just set name to "", the UI List still leaves space for it. I would like the other two properties to fill up the space, horizontally.

$\endgroup$

2 Answers 2

0
$\begingroup$

This should be a minimal working example.

def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
    row = layout.row(align=True)
    row.prop(item, "type")
    row.prop(item, "number")

IMO you can start worrying about the different layout types (compact, grid, default) when you've figured out how UI lists work. In most cases you don't really need them.

$\endgroup$
0
$\begingroup$

Don't draw the name label or do not use use split factor. The value 0.3 would reserve too much space for the number even if you won't draw name.

$\endgroup$
1
  • $\begingroup$ Actually, I don't think the split factor is relevant. The problem is that I don't understand the layout types and I did not realize that I was using DEFAULT layout (and the layout.label(text=item.name) line). $\endgroup$
    – SJK
    Commented Apr 17 at 2:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .