1
$\begingroup$

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"}
$\endgroup$
  • $\begingroup$ Are you getting any errors? $\endgroup$ – cmomoney Nov 5 '17 at 3:39
  • $\begingroup$ @cmomoney no, any errors. only don't work... only select the previous or next element.. buy don't move anything $\endgroup$ – yhoyo Nov 5 '17 at 13:33
2
$\begingroup$

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)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.