Use prop_search(), a StringProperty
and a CollectionProperty
.
import bpy
enum_items = (
('FOO', 'Foo', ''),
('BAR', 'Bar', '')
)
class HelloWorldPanel(bpy.types.Panel):
"""Creates a Panel in the Object properties window"""
bl_label = "Hello World Panel"
bl_idname = "OBJECT_PT_hello"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "object"
def draw(self, context):
layout = self.layout
obj = context.object
col = layout.column()
col.prop_search(context.scene, "coll_string", context.scene, "coll", icon='OBJECT_DATA')
def populate_coll(scene):
bpy.app.handlers.scene_update_pre.remove(populate_coll)
scene.coll.clear()
for identifier, name, description in enum_items:
scene.coll.add().name = name
def register():
bpy.utils.register_module(__name__)
bpy.types.Scene.coll = bpy.props.CollectionProperty(
type=bpy.types.PropertyGroup
)
bpy.types.Scene.coll_string = bpy.props.StringProperty()
# Hack for testing
bpy.app.handlers.scene_update_pre.append(populate_coll)
def unregister():
bpy.utils.unregister_module(__name__)
del bpy.types.Scene.coll
del bpy.types.Scene.coll_string
if __name__ == "__main__":
register()
For limitations please read:
Is it possible to use bpy.props.PointerProperty to store a pointer to an object?Is it possible to use bpy.props.PointerProperty to store a pointer to an object?