TransWikia.com

How to delete element from UIList with a button, when the element is clicked?

Blender Asked by Ermakx on January 16, 2021

I am making a simple addon, that keeps information about location of group of objects at different frames.

Every time I make a keyframe on location for a group of objects, where each object has prefix Group_obj I want the information of such keyframe to appear in my UIList. I store all keyframes per object in dictionary. Dictionary schema:

{frame_number: 
     {group_object_1_name: (x, y, z),
      group_object_2_name: (x, y, z),
  ...................................
      group_object_n_name: (x, y, z)}
}

How to delete an entry from my list by hitting a button, i.e. frame_200? How to understand if entry is clicked, i.e. frame_1137?

Is there a good way to show frames in correct order, ordered from smallest frame to biggest? Please see the screenshot:

My UIList inside addon Panel

Object in 3d View I want to save keyframes for

My current code for this addon can be added to init.py and placed into "C:Program FilesBlender FoundationBlender 2.832.83scriptsaddonsout"

import bpy

"""
###############################################################################
PROPERTY GROUPS: START-UP CONFIGURATION OF DATA IN PANELS UI.
###############################################################################
"""
GroupObjectsData = None


class GroupObjects:
    def __init__(self, blender_objects: bpy.types.bpy_prop_collection = None):
        self.blender_obj_names = []
        self.data = {}
        if len(blender_objects) > 0:
            for obj in blender_objects:
                if "Group_obj" in obj.name:
                    self.blender_obj_names.append(obj.name)

    def add_keypose(self, frame: int):
        bpy.context.scene.frame_current = frame
        frame_name = "frame_" + str(frame)
        self.data[frame_name] = {}

        for name in self.blender_obj_names:
            x, y, z = bpy.data.objects[name].location
            self.data[frame_name][name] = (x, y, z)


class GroupPosesPropertyGroup_PGT(bpy.types.PropertyGroup):
    frame = bpy.props.IntProperty(name="hui")


class GroupKeyposes_UL(bpy.types.UIList):

    def draw_item(self, context, layout, data, item, icon, active_data, active_propname):
        if self.layout_type in {'DEFAULT', 'COMPACT'}:
            if item:
                row = layout.row()
                row.prop(item, "frame", text="frame_" + str(item.frame))
                row.enabled = False
            else:
                layout.label(text="", translate=False, icon_value=icon)
        elif self.layout_type in {'GRID'}:
            layout.alignment = 'CENTER'
            layout.label(text="", icon_value=icon)


"""
###############################################################################
UI AND PANELS FRONTEND.
###############################################################################
"""


class PanelExmpl_PT(bpy.types.Panel):
    bl_idname = "GroupPosesPanel"
    bl_label = "Group poses"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = 'View'
    bl_options = {'DEFAULT_CLOSED'}

    def draw(self, context):
        layout = self.layout
        scene = context.scene

        layout.label(text="Group frames")
        layout.operator('group.init', text='Init Group Backend')
        layout.operator('group.add_keypose', text='Add Keypose')
        row1 = layout.row()
        row1.prop(scene, 'current_group_frame')

        row2 = layout.row()
        row2.template_list("GroupKeyposes_UL", "myul_id", scene, "group_created_poses", scene,
                             "current_group_frame")


"""
###############################################################################
BACKEND OF BLENDER PANELS UI.
###############################################################################
"""


class AddKeypose_OT(bpy.types.Operator):
    bl_idname = "group.add_keypose"
    bl_label = "Add Keypose Operator"
    bl_options = {"INTERNAL"}

    def execute(self, context):
        global GroupObjectsData
        scene = context.scene
        current_frame = scene.frame_current
        GroupObjectsData.add_keypose(current_frame)
        scene.current_group_frame = current_frame
        keypose = scene.group_created_poses.add()
        keypose.frame = current_frame

        print("INFO: frame {}; n{}".format(current_frame, GroupObjectsData.data))

        return {"FINISHED"}



class InitGroupBackend_OT(bpy.types.Operator):
    bl_idname = "group.init"
    bl_label = "Init Group Objects Operator"
    bl_options = {"INTERNAL"}
    def execute(self, context):

        global GroupObjectsData
        GroupObjectsData = GroupObjects(bpy.data.objects)
        print("INFO: GroupObjectsData inited")
        return {"FINISHED"}


"""
###############################################################################
ADDON INFO. REGISTER/UNREGISTER FUNCTIONS.
###############################################################################
"""

bl_info = {
    "name": "Group Poses addon",
    "version": (0, 1, 0),
    "blender": (2, 80, 0),
    "location": "View3D > Sidebar > View Tab > Group Poses",
    "warning": "",
    "wiki_url": "",
    "support": "TESTING",
}


def register():
    bpy.types.Scene.current_group_frame = bpy.props.IntProperty(name="current_group_frame")
    bpy.utils.register_class(GroupPosesPropertyGroup_PGT)
    bpy.types.Scene.group_created_poses = bpy.props.CollectionProperty(type=GroupPosesPropertyGroup_PGT)
    bpy.utils.register_class(GroupKeyposes_UL)
    bpy.utils.register_class(InitGroupBackend_OT)
    bpy.utils.register_class(AddKeypose_OT)
    bpy.utils.register_class(PanelExmpl_PT)


def unregister():
    del bpy.types.Scene.group_created_poses
    bpy.utils.unregister_class(GroupPosesPropertyGroup_PGT)
    bpy.utils.unregister_class(PanelExmpl_PT)
    bpy.utils.unregister_class(AddKeypose_OT)
    bpy.utils.unregister_class(InitGroupBackend_OT)
    bpy.utils.unregister_class(GroupKeyposes_UL)


if __name__ == '__main__':
    register()

```

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP