1bl_info = {
2    "name": "MORSE GUI",
3    "author": "Pierrick Koch",
4    "version": (1, 0, 0),
5    "blender": (2, 5, 9),
6    "api": 36147,
7    "location": "Properties>Scene",
8    "category": "Import-Export",
9    "description": "Graphical User Interface for MORSE",
10    "warning": "",
11    "wiki_url": "",
12    "tracker_url": "https://softs.laas.fr/bugzilla/"
13}
14
15import os
16import bpy
17from morse.builder import *
18
19"""
20MORSE GUI to import components
21
22To test this module you can open this file inside a Text panel in Blender,
23then run the script.
24This will generate a GUI in the Properties View in the Scene tab.
25"""
26
27data = {}
28
29def init():
30    # initialize the component list, scan component directory
31    path = MORSE_COMPONENTS
32    for category in os.listdir(path):
33        pathc = os.path.join(path, category)
34        if os.path.isdir(pathc):
35            data[category] = list_components(pathc)
36            if len(data[category]) > 0:
37                init_prop(category, data[category])
38
39def list_components(path, subdir=''):
40    components = []
41    for blend in os.listdir(path):
42        pathb = os.path.join(path, blend)
43        if os.path.isfile(pathb) and blend.endswith('.blend'):
44            components.append(subdir + blend[:-6])
45        # go to 2nd level (for scenes)
46        elif os.path.isdir(pathb):
47            components.extend(list_components(pathb, subdir='%s%s/'%(subdir, blend)))
48    return components
49
50def init_prop(category, components):
51    objects = []
52    for index, name in enumerate(components):
53        objects.append((str(index), name, category))
54
55    enum = bpy.props.EnumProperty(name=category, description="Choose %s"%category, items=objects, default='0')
56    setattr(bpy.types.Scene, 'enum_%s'%category, enum)
57
58class MorsePanel(bpy.types.Panel):
59    bl_label = "MORSE Panel"
60    bl_idname = "OBJECT_PT_MORSE"
61    bl_space_type = "PROPERTIES"
62    bl_region_type = "WINDOW"
63    bl_context = "scene"
64
65    def draw(self, context):
66        layout = self.layout
67        scene = context.scene
68
69        for category in data:
70            if len(data[category]) > 0:
71                row = layout.row()
72                row.prop(scene, 'enum_%s'%category)
73                row = layout.row()
74                btn = row.operator('bpt.add', text="Add MORSE %s"%category)
75                btn.category = category
76
77class MorseOperator(bpy.types.Operator):
78    bl_idname = "bpt.add"
79    bl_label = "Add MORSE Component"
80
81    category = bpy.props.StringProperty()
82
83    def execute(self, context):
84        # get the index of the selected element
85        select = getattr(bpy.context.scene, 'enum_%s'%self.category)
86        # get the enum list of the current category
87        enum = getattr(bpy.types.Scene, 'enum_%s'%self.category)
88        # get the component name
89        component = enum[1]['items'][int(select)][1]
90        # import the MORSE component
91        Component(self.category, component)
92
93        return{"FINISHED"}
94
95def register():
96    init() # initialize the component list, scan component directory
97    bpy.utils.register_class(MorseOperator)
98    bpy.utils.register_class(MorsePanel)
99
100def unregister():
101    bpy.utils.unregister_class(MorseOperator)
102    bpy.utils.unregister_class(MorsePanel)
103
104if __name__ == "__main__":
105    register()
106
107