2
$\begingroup$

I am trying to create an add-on that has a feature that allows the user to select up to 5 objects that exist in their scene. To do this I've created 5 enumerators, however, I'm struggling when it comes to the "items" parameter. So far I've figured out that an object is stored in bpy.data.objects[""].name, but I can't find the location of the list/dictionary for all existing objects in the scene in order to reference it in the code.

Is this possible to do? If so how can I code the enumerators to use the list/dictionary of existing objects?

Side Note: I plan on doing this with the list of existing materials within the scene as well, so would it be the same deal as the objects?

I would really appreciate it if somebody could help me figure this out, thanks!

enter image description here

My code:

import bpy


class Object_Enums(bpy.types.PropertyGroup):
    
    enum1 : bpy.props.EnumProperty(
        name= "#1", 
        description= "sample text", 
        items= [
        
        ]
            
    )
    
    enum2 : bpy.props.EnumProperty(
        name= "#2", 
        description= "sample text", 
        items= [
        
        ]
            
    )
    
    enum3 : bpy.props.EnumProperty(
        name= "#3", 
        description= "sample text", 
        items= [
        
        ]
            
    )
    
    enum4 : bpy.props.EnumProperty(
        name= "#4", 
        description= "sample text", 
        items= [
        
        ]
            
    )
    
    enum5 : bpy.props.EnumProperty(
        name= "#5", 
        description= "sample text", 
        items= [
        
        ]
            
    )

class ADDONNAME_PT_main_panel(bpy.types.Panel):
    bl_label = "ADDONNAME"
    bl_idname = "ADDONNAME_PT_main_panel"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = "ADDONNAME"

    def draw(self, context):
        layout = self.layout
        scene = context.scene
        mytool = scene.my_tool
        
        layout.prop(mytool, "enum1")
        layout.prop(mytool, "enum2")
        layout.prop(mytool, "enum3")
        layout.prop(mytool, "enum4")
        layout.prop(mytool, "enum5")

        row = layout.row()


class ADDONNAME_OT_my_op(bpy.types.Operator):
    bl_label = "Operator"
    bl_idname = "addonname.myop_operator"
    
    
    def execute(self, context):
        return {'FINISHED'}
    

classes = [Object_Enums, ADDONNAME_PT_main_panel, ADDONNAME_OT_my_op]
 
 
 
def register():
    for cls in classes:
        bpy.utils.register_class(cls)
        
        bpy.types.Scene.my_tool = bpy.props.PointerProperty(type= Object_Enums)
 
def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)
        del bpy.types.Scene.my_tool
 
 
 
if __name__ == "__main__":
    register()
$\endgroup$
1
  • $\begingroup$ Suggest considering a PointerProperty(type=bpy.types.Object) instead of an EnumProperty. It will give you a drop down of objects for free. $\endgroup$
    – scurest
    Commented Nov 7, 2021 at 21:25

1 Answer 1

3
$\begingroup$

You can list all of the objects in the scene with

for object in context.scene.objects:
    print(object.name)

since context.scene.objects contains all of the objects in the scene.

If you only want to list each material once

material_set = set()
for object in context.scene.objects:
    for material in object.material_slots:
        material_set.add(material)
        
for material in material_set:
    print(material.name)

works because each object has a list of material_slots

or you can combine it all

for object in context.scene.objects:
    print(object.name)
    for material in object.material_slots:
        print('\t' + material.name)

If you want to see which materials each object contains.

Given that you know how to list the objects, it's easy to adapt the loops to use them for your enumerations.

$\endgroup$
2
  • $\begingroup$ Oh, duh! Once I read over the code you provided it became glaringly obvious haha. Thank you so much! $\endgroup$
    – leoprd
    Commented Nov 7, 2021 at 22:13
  • 2
    $\begingroup$ Note that sets have a random access rule so you might experience flickering when iterating over those in the interface. $\endgroup$
    – Gorgious
    Commented Nov 8, 2021 at 7:26

You must log in to answer this question.

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