1

I'm using the template of UIList in this answer

but the code to move up or down the items in the UIList don't work... do you know the reason?

# ui list item actions
class Uilist_actions(bpy.types.Operator):
    bl_idname = "custom.list_action"
    bl_label = "List Action"

    action = bpy.props.EnumProperty(
        items=(
            ('UP', "Up", ""),
            ('DOWN', "Down", ""),
            ('REMOVE', "Remove", ""),
            ('ADD', "Add", ""),
        )
    )

    def invoke(self, context, event):

        scn = context.scene
        idx = scn.custom_index

        try:
            item = scn.custom[idx]
        except IndexError:
            pass

        else:
            if self.action == 'DOWN' and idx < len(scn.custom) - 1:
                item_next = scn.custom[idx+1].name
                scn.custom_index += 1
                info = 'Item %d selected' % (scn.custom_index + 1)
                self.report({'INFO'}, info)

            elif self.action == 'UP' and idx >= 1:
                item_prev = scn.custom[idx-1].name
                scn.custom_index -= 1
                info = 'Item %d selected' % (scn.custom_index + 1)
                self.report({'INFO'}, info)

            elif self.action == 'REMOVE':
                info = 'Item %s removed from list' % (scn.custom[scn.custom_index].name)
                scn.custom_index -= 1
                self.report({'INFO'}, info)
                scn.custom.remove(idx)

        if self.action == 'ADD':
            item = scn.custom.add()
            item.id = len(scn.custom)
            item.name = get_activeSceneObject() # assign name of selected object
            scn.custom_index = (len(scn.custom)-1)
            info = '%s added to list' % (item.name)
            self.report({'INFO'}, info)

        return {"FINISHED"}
yhoyo
  • 2,285
  • 13
  • 32

1 Answers1

2

Use CollectionProperty.move()

The code to move the collection property items is missing. scene.custom.move(0, 1) will swap items with indices 0 and 1.

        if self.action == 'DOWN' and idx < len(scn.custom) - 1:
            item_next = scn.custom[idx+1].name
            scn.custom.move(idx, idx + 1)
            scn.custom_index += 1
            info = 'Item %d selected' % (scn.custom_index + 1)
            self.report({'INFO'}, info)

        elif self.action == 'UP' and idx >= 1:
            item_prev = scn.custom[idx-1].name
            scn.custom.move(idx, idx-1)
            scn.custom_index -= 1
            info = 'Item %d selected' % (scn.custom_index + 1)
            self.report({'INFO'}, info)
batFINGER
  • 84,216
  • 10
  • 108
  • 233