File: morse_gui.py

package info (click to toggle)
morse-simulator 1.4-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 187,116 kB
  • sloc: ansic: 108,311; python: 25,694; cpp: 786; makefile: 126; xml: 34; sh: 7
file content (106 lines) | stat: -rw-r--r-- 3,268 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
bl_info = {
    "name": "MORSE GUI",
    "author": "Pierrick Koch",
    "version": (1, 0, 0),
    "blender": (2, 5, 9),
    "api": 36147,
    "location": "Properties>Scene",
    "category": "Import-Export",
    "description": "Graphical User Interface for MORSE",
    "warning": "",
    "wiki_url": "",
    "tracker_url": "https://softs.laas.fr/bugzilla/"
}

import os
import bpy
from morse.builder import *

"""
MORSE GUI to import components

To test this module you can open this file inside a Text panel in Blender,
then run the script.
This will generate a GUI in the Properties View in the Scene tab.
"""

data = {}

def init():
    # initialize the component list, scan component directory
    path = MORSE_COMPONENTS
    for category in os.listdir(path):
        pathc = os.path.join(path, category)
        if os.path.isdir(pathc):
            data[category] = list_components(pathc)
            if len(data[category]) > 0:
                init_prop(category, data[category])

def list_components(path, subdir=''):
    components = []
    for blend in os.listdir(path):
        pathb = os.path.join(path, blend)
        if os.path.isfile(pathb) and blend.endswith('.blend'):
            components.append(subdir + blend[:-6])
        # go to 2nd level (for scenes)
        elif os.path.isdir(pathb):
            components.extend(list_components(pathb, subdir='%s%s/'%(subdir, blend)))
    return components

def init_prop(category, components):
    objects = []
    for index, name in enumerate(components):
        objects.append((str(index), name, category))

    enum = bpy.props.EnumProperty(name=category, description="Choose %s"%category, items=objects, default='0')
    setattr(bpy.types.Scene, 'enum_%s'%category, enum)

class MorsePanel(bpy.types.Panel):
    bl_label = "MORSE Panel"
    bl_idname = "OBJECT_PT_MORSE"
    bl_space_type = "PROPERTIES"
    bl_region_type = "WINDOW"
    bl_context = "scene"

    def draw(self, context):
        layout = self.layout
        scene = context.scene
        
        for category in data:
            if len(data[category]) > 0:
                row = layout.row()
                row.prop(scene, 'enum_%s'%category)
                row = layout.row()
                btn = row.operator('bpt.add', text="Add MORSE %s"%category)
                btn.category = category

class MorseOperator(bpy.types.Operator):
    bl_idname = "bpt.add"
    bl_label = "Add MORSE Component"
    
    category = bpy.props.StringProperty()

    def execute(self, context):
        # get the index of the selected element
        select = getattr(bpy.context.scene, 'enum_%s'%self.category)
        # get the enum list of the current category
        enum = getattr(bpy.types.Scene, 'enum_%s'%self.category)
        # get the component name
        component = enum[1]['items'][int(select)][1]
        # import the MORSE component
        Component(self.category, component)

        return{"FINISHED"}

def register():
    init() # initialize the component list, scan component directory
    bpy.utils.register_class(MorseOperator)
    bpy.utils.register_class(MorsePanel)

def unregister():
    bpy.utils.unregister_class(MorseOperator)
    bpy.utils.unregister_class(MorsePanel)

if __name__ == "__main__":
    register()