What I am trying to do is have a menu where the options on the menu are ‘dynamically’ populated, instead of having an operator for each selection. I’ve found this method which does what I want.
However instead of listing the individual items in the enum operator like this:
enum_items = (
("item1", "Item 1", "Description 1"),
("item2", "Item 2", "Description 2"),
("item3", "Item 3", "Description 3"),
)
I want the contents to be populated by a list, in this case all objects which are inside a specific named collection:
for obj in bpy.data.collections["Collection"].all_objects:
cubes = obj.name, obj.name, obj.name
print(cubes)
This is what it prints:
('Cube', 'Cube', 'Cube')
('Cube.001', 'Cube.001', 'Cube.001')
('Cube.002', 'Cube.002', 'Cube.002')
But at the end of each row, I somehow need to get a comma in there so that it follows the required structure of enum_items. So… how do I do that? Otherwise the enum only sees the first line (Cube) and ignores the other two.
Example .blend attached and full test code below:
menutest.blend (800.5 KB)
import bpy
class SimpleCustomMenu(bpy.types.Menu):
bl_label = "Simple Custom Menu"
bl_idname = "OBJECT_MT_simple_custom_menu"
def draw(self, context):
layout = self.layout
layout.operator_enum("object.some_operator", "action")
def generate_enum(self, context):
for obj in bpy.data.collections["Collection"].all_objects:
cubes = obj.name, obj.name, obj.name
print(cubes)
enum = (cubes,)
#print(enum)
return enum
class OBJECT_OT_some_operator(bpy.types.Operator):
bl_idname = "object.some_operator"
bl_label = "Some Operator"
action: bpy.props.EnumProperty(items=generate_enum)
def execute(self, context):
ObjList = [ 'Cube', 'Cube.001', 'Cube.002' ]
for obj in ObjList:
bpy.data.objects[obj].hide_viewport = True
bpy.data.objects[obj].hide_render = True
print(ObjList)
bpy.data.objects[self.action].hide_viewport = False
bpy.data.objects[self.action].hide_render = False
return {'FINISHED'}
if __name__ == "__main__":
bpy.utils.register_class(OBJECT_OT_some_operator)
bpy.utils.register_class(SimpleCustomMenu)
bpy.ops.wm.call_menu(name=SimpleCustomMenu.bl_idname)