Appologies firstly, I'm very new to python and this site.
I have a small code that I am trying to create, to add an option on my panel addon.
The code is to select all collections in Blender and generate a Enum list, and then to populate a second Enum list with the objects within, the second list changing with the input from the first.
I plan to use the code once final to make the selection in the second menu active and show it in viewport, and to hide everything else.
So far, I have the following, but I'm pretty sure that this code will end up crashing blender as I'm pretty sure it re-writes the enum array with each redraw.
import os
import bpy
from bpy.types import (Panel, Operator)
class OBJECT_PT_Test(Panel):
bl_label = "Panel"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = "Example"
def draw(self, context):
layout = self.layout
row = layout.row(align=True)
row.prop(context.scene, "test_cat", text="")
row = layout.row(align=True)
row.prop(context.scene, "test_obj", text="")
def test_cat(self, context):
Enum_items = []
for collection in bpy.data.collections:
item = (collection.name, collection.name, "")
Enum_items.append(item)
return Enum_items
#print (Enum_items)
def test_obj(self, context):
obj_items = []
for obj in bpy.data.collections[bpy.context.scene.test_cat].objects:
obj_item = (obj.name, obj.name, "")
obj_items.append(obj_item)
return obj_items
#print (obj_item)
def register():
bpy.utils.register_class(OBJECT_PT_Test)
bpy.types.Scene.test_cat = bpy.props.EnumProperty(name= "Category Name", description= "Category Desc", items=test_cat)
bpy.types.Scene.test_obj = bpy.props.EnumProperty(name= "Object Name", description= "Object Desc", items=test_obj)
def unregister():
del bpy.types.Scene.test_cat
del bpy.types.Scene.test_obj
bpy.utils.unregister_class(OBJECT_PT_Test)
if __name__ == "__main__":
register()
Any advice would be greatly appreciated and taken on board