0
$\begingroup$

Hello I am still new to Blender and learned Python 2 weeks ago. (I have previous experience in C)

I found a good example for creating a Custom list for objects on here and it works splendidly.

however in my Addon i have a function that removes certain objects that are also in the list. When this happens it will leave an empty row in the custom list. To remove it i need the index number of the object. Displayed in the code below

scn = context.scene
idx = scn.custom_index

scn.custom_index -= 1
scn.custom.remove(idx)

however i do not know how to get the index of this object when i only have access to it's name to also remove it in the custom list

below is the code for the list:

class CUSTOM_UL_items(UIList):
    
    def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
        obj = item.obj
        custom_icon = "OUTLINER_OB_EMPTY"
        row = layout.row()
        #split.label(text="Index: %d" % (index))
        row.prop(obj, "name", text='', emboss=False, translate=False, icon=custom_icon)
            
    def invoke(self, context, event):
        pass   

class CUSTOM_PG_objectCollection(PropertyGroup):
    #name: StringProperty() -> Instantiated by default
    obj: PointerProperty(
        name="Object",
        type=bpy.types.Object)

classes = (
    CUSTOM_UL_items,
    CUSTOM_PG_objectCollection,
)

def register():
    from . import custom_props, custom_ui
    from bpy.utils import register_class
    for cls in classes:
        register_class(cls)
    
    # Custom scene properties
    bpy.types.Scene.custom = CollectionProperty(type=CUSTOM_PG_objectCollection) 
    bpy.types.Scene.custom_index = IntProperty()

def unregister():
    from . import custom_props, custom_ui

    from bpy.utils import unregister_class
    for cls in reversed(classes):
        unregister_class(cls)
    
    del bpy.types.Scene.custom
    del bpy.types.Scene.custom_index

The way i know how to get the custom_index right now is by selecting it in the custom list and using

bpy.context.scene.custom_index

If anyone knows how to access the index of specific items in the list by using it's name then i will appreciate it. i will provide an image showing the list with a removed item so you can see more clearly what i mean.

thank youenter image description here

$\endgroup$

1 Answer 1

1
$\begingroup$

Add index inside the CUSTOM_PG_object

class CUSTOM_UL_items(UIList):
    
    def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
        obj = item.obj
        custom_icon = "OUTLINER_OB_EMPTY"
        row = layout.row()
        #split.label(text="Index: %d" % (index))
        row.prop(obj, "name", text='', emboss=False, translate=False, icon=custom_icon)
            

class CUSTOM_PG_objectCollection(PropertyGroup):
    #name: StringProperty() -> Instantiated by default
    obj: PointerProperty(
        name="Object",
        type=bpy.types.Object)


class CUSTOM_PG_object(PropertyGroup):
    
    objects : CollectionProperty(type=CUSTOM_PG_objectCollection) 
    index : IntProperty()


classes = (
    CUSTOM_UL_items,
    CUSTOM_PG_objectCollection,
    CUSTOM_PG_object,
)

def register():
    from . import custom_props, custom_ui
    from bpy.utils import register_class
    for cls in classes:
        register_class(cls)
    
    # Custom scene properties
    bpy.types.Scene.custom = PointerProperty(type=CUSTOM_PG_object)  


def unregister():
    from . import custom_props, custom_ui

    from bpy.utils import unregister_class
    for cls in reversed(classes):
        unregister_class(cls)
    
    del bpy.types.Scene.custom

In an Operator

def execute(self, context):
    custom = context.scene.custom

    object = custom.objects.add()
    index = len(custom.objects) - 1
    custom.index = index
$\endgroup$
1
  • $\begingroup$ Great! am still learning the rules of what's possible and not when doing these stuff. $\endgroup$ Commented Jul 8, 2022 at 12:34

You must log in to answer this question.

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