0
$\begingroup$

I am relatively new to coding in Blender so I am still familiarizing myself with the keywords and concepts used in bpy.

I am currently trying to create enumerators that use existing lists as their items rather than manually typing out a list of options for their "items" parameters. It seems like it should be as simple as inserting the name of the list in the portion in place of the manually added options, however, that appears to not be the case and I can't figure out what the correct phrasing would be to do so.

Can anybody help me out?

My Code:

import bpy

object_list = []
camera_list = []
light_list = []

for obj in bpy.context.scene.objects:
    
    if obj.type == "CAMERA":
        camera_list.append(obj)
    if obj.type == "LIGHT":
        light_list.append(obj)
    if obj.type == "MESH":
        object_list.append(obj)

class Enums(bpy.types.PropertyGroup):
    
    
    camera_enum : bpy.props.EnumProperty(
        name= "", 
        description= "None", 
        items= [("None", "None", ""),  # <-- Option to select none of the items
                () #<-- Insert "camera_list" here                                
        ]
            
    )
    
    lights_enum : bpy.props.EnumProperty(
        name= "", 
        description= "None", 
        items= [("None", "None", ""),  # <-- Option to select none of the items
                () #<-- Insert "light_list" here                                
        ]
            
    )
    
    objects_enum : bpy.props.EnumProperty(
        name= "", 
        description= "None", 
        items= [("None", "None", ""),  # <-- Option to select none of the items
                () #<-- Insert "object_list" here                                
        ]
            
    )
```
$\endgroup$

1 Answer 1

2
$\begingroup$

You can just append to the list, but since the items list is set only once, the list will be static and not respond to new or deleted objects. You can get a dynamic list by giving a callback as items but there's a known bug with that (there's a warning about it in the docs).

Instead I think you should use a PointerProperty. You can use the poll argument to filter the list to only certain objects. For example

def poll_is_camera(self, obj):
    return obj.type == 'CAMERA'


class Enums(bpy.types.PropertyGroup):

    camera_enum : bpy.props.PointerProperty(
        name= "", 
        description= "None", 
        type= bpy.types.Object,
        poll= poll_is_camera,
    )

    # etc

This will give you a dropdown of all camera objects for free.

$\endgroup$
1
  • 1
    $\begingroup$ Thank you so much! I've spent pretty much the whole day racking my brain about how to work this out so you've provided me with some much-needed mental relief. $\endgroup$
    – leoprd
    Commented Nov 9, 2021 at 2:33

You must log in to answer this question.

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