0

I'm trying to compile a UIlist, but I don't understand how to avoid inserting some sockets. In this example, to simplify things, I want that if a Socket is named "Empty" it doesn't get put in the UIlist, so what I get right now is that in the UIList, I get some "empty" item.

I would like to avoid these empty items in the list. How can I do?

class Node_UL_node_interface_sockets(bpy.types.UIList):
filter_string: StringProperty(name="filter_string",
                              default="",
                              description="Filter string for name")

def draw_item(self, context, layout, _data, item, icon, _active_data, _active_propname, _index):
    socket = item
    # if socket.name == "Empty":
    #     # I need not to display any "Empty" name lines
    #     return

    color = socket.draw_color(context)

    if self.layout_type in {'DEFAULT', 'COMPACT'}:
        row = layout.row(align=True)
        row.template_node_socket(color=color)
        row.prop(socket, "name", text="", emboss=False, icon_value=icon)

    elif self.layout_type == 'GRID':
        layout.alignment = 'CENTER'
        layout.template_node_socket(color=color)

def filter_items(self, context, data, propname):
    # This function is really difficult for me to understand, it seems very confusing to me and I can't manage it

    items = getattr(data, propname)
    if not len(items):
        return [], []

    filtered = [self.bitflag_filter_item] * len(items)
    for i, item in enumerate(items):
        if not item.name == 'Empty':
            if self.filter_string in item.name:
                filtered[i] &= ~self.bitflag_filter_item
            else:
                filtered = [self.bitflag_filter_item] * len(items)

    return filtered, []

def draw_filter(self, context, layout):
    # In addition, I want to keep a search by name.
    row = layout.row(align=True)
    row.prop(self, "filter_string", text="Filter", icon="VIEWZOOM")

As in the example above, I tried to do this, but I really don't understand how to handle this filter_items() function

I would also like to keep a search by name, that's why I put the draw_filter function (self, context, layout), it seems to be very convenient, because it replaces the default one.

But the problem remains the understanding of the filter_items(), in addition I do not understand why if I return an empty list [], the UIList, will show all the items equally, Maybe because the list must be filled with what should not be shown, I suppose yes, but it's really frustrating, I have no idea how to continue this.

Noob Cat
  • 1,222
  • 3
  • 20
  • 61
  • 2
    The usual way to do this is with a filter function. See UIList for an example in the manual or Amazing UILists in Blender with Custom Filtering for a tutorial. – Marty Fouts Jan 29 '22 at 16:15
  • Excellent example, but very complex. In that case we consider the use of the filter under the UiList menu. I find it hard to understand – Noob Cat Jan 29 '22 at 18:43
  • it is a bit hard to understand, unfortunately. UiLists can be very complex to use. I suggest you try it and when you get stuck on a specific issue, modify your question to cover precisely that issue. – Marty Fouts Jan 29 '22 at 18:45
  • Ok but I'm not sure how to assemble this class. I put a simple example case, if a socket has a certain name, it must never be shown. Without the use of native filters under the UIList – Noob Cat Jan 29 '22 at 19:30
  • start with this answer. It does more than you need but not exactly what you need. Figure out what to do in filter_items to filter on what you want to filter on. Decide whether you want to allow the filter to be editable and if you don't remove the editing operators and their panel entries. – Marty Fouts Jan 29 '22 at 20:20

0 Answers0