File: morse_object_utils.py

package info (click to toggle)
morse-simulator 1.4-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 186,952 kB
  • ctags: 3,992
  • sloc: ansic: 108,311; python: 25,692; cpp: 786; makefile: 120; xml: 34; sh: 7
file content (622 lines) | stat: -rw-r--r-- 21,638 bytes parent folder | download | duplicates (5)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
bl_info = {
    "name": "Morse Utils",
    "description": "Utils for Environment Creation for MORSE",
    "author": "Sebastian Schmidt",
    "version": (0,5,0),
    "blender": (2, 61, 0),
    "api": 39307,
    "location": "Logic",
    "warning": '',
    "wiki_url": "",
    "tracker_url": "",
    "category": "Morse"}


import bpy
from bpy.props import *


def update(self, context):
    objects = []
    for index, obj in enumerate(bpy.context.scene.objects):
        objects.append((str(index), obj.name, str(index)))
    return objects
     
class MorseSwitchDialog(bpy.types.Operator):
    '''
    Make Switches usable for the Human
    '''
    bl_idname = "object.morse_switch_dialog"
    bl_label = "Morse Switch"


    objs = {}
    inv_objs = {}
    
    try:
        bpy.app.handlers.scene_update_post
        handler_available = True
        master = EnumProperty(name = "Switch",items = update)
    except AttributeError:
        handler_available = False
        master = StringProperty(name = "Switch")
    on = BoolProperty(name = "On")

    def update_dict(self, context):
        objs = {}
        inv_objs = {}
        for index, obj in enumerate(bpy.context.scene.objects):
            objs[str(index)] = obj.name
            inv_objs[obj.name] = str(index)
        self.objs = objs
        self.inv_objs = inv_objs
        
    
    def assignProp(self, context, name, value):
        prop = context.object.game.properties[name]
        prop.value = value

    def execute(self, context):
        obj = context.active_object

        obj.game.use_actor = True

        if not 'Switch' in obj.game.properties:
            bpy.ops.object.game_property_new()
            prop = context.object.game.properties[-1]
            prop.name = 'Switch'
            prop.type = 'STRING'

        self.assignProp(context, 'Switch', self.objs[self.master] if self.handler_available else self.master)

        
        try:
            master = context.scene.objects[obj.game.properties['Switch'].value]
            context.scene.objects.active = master
            if not 'On' in master.game.properties:
                bpy.ops.object.game_property_new()
                prop = context.object.game.properties[-1]
                prop.name = 'On'
                prop.type = 'BOOL'

            self.assignProp(context, 'On', self.on)
        except KeyError:
            self.report({'INFO'}, "No object with this name - switch will not function in simulation")

        return{'FINISHED'}
            
    def invoke(self, context, event):
        self.update_dict(context)
        obj = context.object

        if 'Switch' in obj.game.properties:
            if self.handler_available:
                self.master = self.inv_objs[obj.game.properties['Switch'].value]
            else:
                self.master = obj.game.properties['Switch'].value

        return context.window_manager.invoke_props_dialog(self)
    
class MorseObjectDialog(bpy.types.Operator):
    '''
    Operator class that assigns a "Object" and a "Description" Property,
    makes it a Rigid Body with Collision Bounds and Mass
    '''
    bl_idname = "object.morse_object_dialog"
    bl_label = "Morse Object"

    object = BoolProperty(name = "Object")
    graspable = BoolProperty(name = "Graspable")
    label = StringProperty(name = "Label")
    description = StringProperty(name="Description")
    typeProp = StringProperty(name = "Type")
    mass = FloatProperty(name = "Mass")
    bounds = EnumProperty(name="Collision Bounds",
        items = [('one', 'Box', 'Box'), 
                 ('two', 'Sphere', 'Sphere'),
                 ('three', 'Capsule', 'Capsule'), 
                 ('four', 'Cylinder', 'Cylinder'),
                 ('five', 'Cone', 'Cone'),
                 ('six', 'Convex Hull', 'Convex Hull'),
                 ('seven', 'Triangle Mesh', 'Triangle Mesh')])
    
    items = {'one':'BOX', 'two':'SPHERE', 'three':'CAPSULE', 'four':'CYLINDER',
             'five':'CONE', 'six':'CONVEX_HULL', 'seven':'TRIANGLE_MESH'}
    inv_items = {'BOX': 'one', 'CYLINDER': 'four', 'TRIANGLE_MESH': 'seven',
                 'SPHERE': 'two', 'CAPSULE': 'three', 'CONE': 'five',
                 'CONVEX_HULL': 'six'}

    
    def assignProp(self, context, name, value):
        prop = context.object.game.properties[name]
        prop.value = value
    
    def execute(self, context):
        obj = bpy.context.active_object
        
        # add game properties if needed
        if not 'Object' in obj.game.properties:
            bpy.ops.object.game_property_new()
            prop = context.object.game.properties[-1]
            prop.name = 'Object'
            prop.type = 'BOOL'
        
        if not 'Description' in obj.game.properties:
            bpy.ops.object.game_property_new()
            prop = context.object.game.properties[-1]
            prop.name = 'Description'
            prop.type = 'STRING'

        if not 'Label' in obj.game.properties:
            bpy.ops.object.game_property_new()
            prop = context.object.game.properties[-1]
            prop.name = 'Label'
            prop.type = 'STRING'

        if not 'Type' in obj.game.properties:
            bpy.ops.object.game_property_new()
            prop = context.object.game.properties[-1]
            prop.name = 'Type'
            prop.type = 'STRING'

        if not 'Graspable' in obj.game.properties:
            bpy.ops.object.game_property_new()
            prop = context.object.game.properties[-1]
            prop.name = 'Graspable'
            prop.type = 'BOOL'
    
        self.assignProp(context, 'Object', self.object)
        self.assignProp(context, 'Description', self.description)
        self.assignProp(context, 'Label', self.label)
        self.assignProp(context, 'Type', self.typeProp)
        self.assignProp(context, 'Graspable', self.graspable)
        
        if not 'Collision' in obj.game.sensors:
            bpy.ops.logic.sensor_add(type = 'NEAR')
            sens = context.object.game.sensors[-1]
            sens.name = 'Collision'
            sens.distance = 0.05
            sens.reset_distance = 0.075

            bpy.ops.logic.controller_add()
            contr = context.object.game.controllers[-1]

            contr.link(sensor = sens)

        
        # make the object a rigid body
        obj.game.physics_type = 'RIGID_BODY'
        obj.game.use_collision_bounds = True
        obj.game.collision_bounds_type = self.items[self.bounds]
        obj.game.mass = self.mass
        
        return {'FINISHED'}
 
    def invoke(self, context, event):
        obj = context.object
        # check not to overwrite anything already set
        if 'Object' in obj.game.properties:
            self.object = obj.game.properties['Object'].value
        else:
            self.object = True
        if 'Label' in obj.game.properties:
            self.label = obj.game.properties['Label'].value
        else:
            self.label = context.object.name
        if 'Description' in obj.game.properties:
            self.description = obj.game.properties['Description'].value
        else:
            self.description = context.object.name
        if 'Graspable' in obj.game.properties:
            self.graspable = obj.game.properties['Graspable'].value
        else:
            self.graspable = True
        if 'Type' in obj.game.properties:
            self.typeProp = obj.game.properties['Type'].value
        else:
            self.typeProp = "Object"


        
        self.mass = obj.game.mass
        self.bounds = self.inv_items[obj.game.collision_bounds_type]
        return context.window_manager.invoke_props_dialog(self)

 
class MorseDrawerDialog(bpy.types.Operator):
    '''
    Operator class that assigns a "Drawer", a "Description" and a "Open"
    Property, and the logic needed for Animation
    '''
    bl_idname = "object.morse_drawer_dialog"
    bl_label = "Morse Drawer"
 
    drawer = StringProperty(name = "Drawer")
    description = StringProperty(name="Description")
    end_frame = IntProperty(name = "End Frame")
    open = BoolProperty(name = "Open")

    if bpy.app.version >= (2, 60, 0):
        direction = EnumProperty(name = "Generate Action",
                             items = [('one', 'No', 'one'),
                                      ('two', 'X+', 'two'),
                                      ('three', 'X-', 'three'),
                                      ('four', 'Y+', 'four'),
                                      ('five', 'Y-', 'five')])

        vec = {'two':(1.0, 0.0, 0.0), 'three':(-1.0, 0.0, 0.0), 'four':(0.0, 1.0, 0.0), 'five':(0.0, -1.0, 0.0)}
    else:
        direction = 'one'
        # means no action generation
    
    def generate_action(self, endframe, direction):
        # create an action
        action = bpy.data.actions.new(name = bpy.context.object.name + 'Open')
        bpy.context.object.animation_data_create()
        bpy.context.object.animation_data.action = action
        
        # set starting keyframe
        bpy.ops.anim.change_frame(frame = 0)
        bpy.ops.anim.keyframe_insert(type = 'Location')
        
        # set endframe 
        bpy.ops.anim.change_frame(frame = endframe)
        dim = bpy.context.object.dimensions
        translate_tuple = tuple(0.8 * dim[i] * direction[i] for i in range(0,3))    
        bpy.ops.transform.translate(value = translate_tuple)
        bpy.ops.anim.keyframe_insert(type = 'Location')
        
        
        bpy.ops.anim.change_frame(frame = 0)
        
        return action
        
    def assignProp(self, context, name, value):
        prop = context.object.game.properties[name]
        prop.value = value
    
    def execute(self, context):
        obj = bpy.context.active_object
        
        # add game properties if needed
        if not 'Drawer' in obj.game.properties:
            bpy.ops.object.game_property_new()
            prop = context.object.game.properties[-1]
            prop.name = 'Drawer'
            prop.type = 'STRING'
        
        if not 'Open' in obj.game.properties:
            bpy.ops.object.game_property_new()
            prop = context.object.game.properties[-1]
            prop.name = 'Open'
            prop.type = 'BOOL'
        
        if not 'Description' in obj.game.properties:
            bpy.ops.object.game_property_new()
            prop = context.object.game.properties[-1]
            prop.name = 'Description'
            prop.type = 'STRING'
            
        self.assignProp(context, 'Drawer', self.drawer)
        self.assignProp(context, 'Open', self.open)
        self.assignProp(context, 'Description', self.description)
        
        # make the drawer an actor
        obj.game.use_actor = True

        # generate action
        if self.direction != 'one':
            action = self.generate_action(self.end_frame,self.vec[self.direction])
        else:
            action = None
        
        # set the logic to open and close the drawer if needed
        if 'Open' not in context.object.game.sensors:
            bpy.ops.logic.sensor_add(type = 'PROPERTY')
            sens = context.object.game.sensors[-1]
            sens.name = 'Open'
            sens.property = 'Open'
            sens.value = 'True'
        
            bpy.ops.logic.controller_add()
            contr = context.object.game.controllers[-1]
        
            contr.link(sensor=sens)
        
            if bpy.app.version < (2, 60, 0):
                bpy.ops.logic.actuator_add(type = 'FCURVE')
            else:
                bpy.ops.logic.actuator_add(type = 'ACTION')
            act = context.object.game.actuators[-1]
            act.name = 'Open'
            if action:
                act.action = action
        
            contr.link(actuator=act)
        
        
        if 'Close' not in context.object.game.sensors:
            bpy.ops.logic.sensor_add(type = 'PROPERTY')
            sens = context.object.game.sensors[-1]
            sens.name = 'Close'
            sens.property = 'Open'
            sens.value = 'False'
        
            bpy.ops.logic.controller_add()
            contr = context.object.game.controllers[-1]
        
            contr.link(sensor=sens)
            
            if bpy.app.version < (2, 60, 0):
                bpy.ops.logic.actuator_add(type = 'FCURVE')
            else:
                bpy.ops.logic.actuator_add(type = 'ACTION')
            act = context.object.game.actuators[-1]
            act.name = 'Close'
            if action:
                act.action = action
        
            contr.link(actuator=act)
        
        context.object.game.actuators['Open'].frame_end = self.end_frame
        context.object.game.actuators['Close'].frame_start = self.end_frame
        
        return {'FINISHED'}
 
    def invoke(self, context, event):
        obj = context.object
        # check not to overwrite anything already set
        if 'Drawer' in obj.game.properties:
            self.drawer = obj.game.properties['Drawer'].value
        else:
            self.drawer = context.object.name
        if 'Description' in obj.game.properties:
            self.description = obj.game.properties['Description'].value
        else:
            self.description = context.object.name
        if 'Open' in obj.game.properties:
            self.open = obj.game.properties['Open'].value
        else:
            self.open = False
        if 'Open' in obj.game.actuators:
            self.end_frame = obj.game.actuators['Open'].frame_end
        else:
            self.end_frame = 40
        return context.window_manager.invoke_props_dialog(self)


class MorseDoorDialog(bpy.types.Operator):
    '''
    Operator class that assigns a "Door", a "Description" and a "Open" Property
    In Future the may be animated as well, a logic setup is already in this
    script, but commented.
    '''
    bl_idname = "object.morse_door_dialog"
    bl_label = "Morse Door"
 
    door = StringProperty(name = "Door")
    description = StringProperty(name="Description")
    # uncomment next line if using the IPO Setup
    # end_frame = IntProperty(name = "End Frame")
    open = BoolProperty(name = "Open")
    
    def assignProp(self, context, name, value):
        prop = context.object.game.properties[name]
        prop.value = value
    
    def execute(self, context):
        obj = bpy.context.active_object
        
        # add game properties if needed
        if not 'Door' in obj.game.properties:
            bpy.ops.object.game_property_new()
            prop = context.object.game.properties[-1]
            prop.name = 'Door'
            prop.type = 'STRING'
        
        if not 'Open' in obj.game.properties:
            bpy.ops.object.game_property_new()
            prop = context.object.game.properties[-1]
            prop.name = 'Open'
            prop.type = 'BOOL'
        
        if not 'Description' in obj.game.properties:
            bpy.ops.object.game_property_new()
            prop = context.object.game.properties[-1]
            prop.name = 'Description'
            prop.type = 'STRING'
        
        self.assignProp(context, 'Door', self.door)
        self.assignProp(context, 'Open', self.open)
        self.assignProp(context, 'Description', self.description)
        
        # make the door an actor
        obj.game.use_actor = True

        # uncomment this part as well for the IPO Setup
        '''
        # set the logic to open and close the door if needed
        if 'Open' not in context.object.game.sensors:
            bpy.ops.logic.sensor_add(type = 'PROPERTY')
            sens = context.object.game.sensors[-1]
            sens.name = 'Open'
            sens.property = 'Open'
            sens.value = 'True'
        
            bpy.ops.logic.controller_add()
            contr = context.object.game.controllers[-1]
        
            contr.link(sensor=sens)
        
            if bpy.app.version < (2, 60, 0):
                bpy.ops.logic.actuator_add(type = 'FCURVE')
            else:
                bpy.ops.logic.actuator_add(type = 'ACTION')
            act = context.object.game.actuators[-1]
            act.name = 'Open'
        
            contr.link(actuator=act)
        
        
        if 'Close' not in context.object.game.sensors:
            bpy.ops.logic.sensor_add(type = 'PROPERTY')
            sens = context.object.game.sensors[-1]
            sens.name = 'Close'
            sens.property = 'Open'
            sens.value = 'False'
        
            bpy.ops.logic.controller_add()
            contr = context.object.game.controllers[-1]
        
            contr.link(sensor=sens)
        
            if bpy.app.version < (2, 60, 0):
                bpy.ops.logic.actuator_add(type = 'FCURVE')
            else:
                bpy.ops.logic.actuator_add(type = 'ACTION')
            act = context.object.game.actuators[-1]
            act.name = 'Close'
        
            contr.link(actuator=act)
        
        context.object.game.actuators['Open'].frame_end = self.end_frame
        context.object.game.actuators['Close'].frame_start = self.end_frame
        '''
        return {'FINISHED'}
 
    def invoke(self, context, event):
        obj = context.object
        # check not to overwrite anything already set
        if 'Door' in obj.game.properties:
            self.door = obj.game.properties['Door'].value
        else:
            self.door = 'right'
        if 'Description' in obj.game.properties:
            self.description = obj.game.properties['Description'].value
        else:
            self.description = context.object.name
        if 'Open' in obj.game.properties:
            self.open = obj.game.properties['Open'].value
        else:
            self.open = False
        # uncomment this as well for the IPO Setup
        '''
        if 'Open' in obj.game.actuators:
            self.end_frame = obj.game.actuators['Open'].frame_end
        else:
            self.end_frame = 40
        '''
        return context.window_manager.invoke_props_dialog(self)

class MorseLightDialog(bpy.types.Operator):
    '''
    Set up a light that can be switched on and off
    '''
    bl_idname = "object.morse_light_dialog"
    bl_label = "Morse Light"

    On = BoolProperty(name = "On")
    energy = FloatProperty(name = "Energy")

    def assignProp(self, context, name, value):
        prop = context.object.game.properties[name]
        prop.value = value

    def execute(self, context):
        obj = bpy.context.active_object
        
        # add game properties if needed
        if not 'On' in obj.game.properties:
            bpy.ops.object.game_property_new()
            prop = context.object.game.properties[-1]
            prop.name = 'On'
            prop.type = 'BOOL'

        self.assignProp(context, 'On', self.On)
        
        if not 'Energy' in obj.game.properties:
            bpy.ops.object.game_property_new()
            prop = context.object.game.properties[-1]
            prop.name = 'Energy'
            prop.type = 'FLOAT'

        self.assignProp(context, 'Energy', self.energy)

        # add logic setup
        if not 'PropertyChange' in context.object.game.sensors:
            bpy.ops.logic.sensor_add(type = 'PROPERTY')
            sens = context.object.game.sensors[-1]
            sens.name = 'PropertyChange'
            sens.property = 'On'
            sens.evaluation_type = 'PROPCHANGED'

            bpy.ops.logic.controller_add(type = 'PYTHON')
            contr = context.object.game.controllers[-1]
            contr.mode = 'MODULE'
            contr.module = "lights.change_light_energy"

            contr.link(sensor = sens)

        return {'FINISHED'}
    
    def invoke(self, context, event):
        obj = context.object

        if obj.type != 'LAMP':
            print("Sorry, no lamp selected")
            self.report({'INFO'}, "Sorry, no lamp selected")
            return {'CANCELLED'}
        
        if 'On' in obj.game.properties:
            self.On = obj.game.properties['On'].value
        self.energy = obj.data.energy

        return context.window_manager.invoke_props_dialog(self)
        



# Panel in tools region
class MorsePanel(bpy.types.Panel):
    '''
    Adds a Panel in the Logic Editor UI
    '''
    bl_label = "Morse Utils"
    bl_space_type = "LOGIC_EDITOR"
    bl_region_type = "UI"
 
    def draw(self, context):
        layout = self.layout
        layout.operator("object.morse_object_dialog")
        layout.operator("object.morse_drawer_dialog")
        layout.operator("object.morse_door_dialog")
        layout.operator("object.morse_switch_dialog")

        col = layout.column()
        col.label(text = "Presets for electric devices")
        layout.operator("object.morse_light_dialog", icon = 'LAMP_SPOT')
 

def register():
    bpy.utils.register_class(MorseObjectDialog)
    bpy.utils.register_class(MorseDoorDialog)
    bpy.utils.register_class(MorseDrawerDialog)
    bpy.utils.register_class(MorseSwitchDialog)
    bpy.utils.register_class(MorseLightDialog)
    bpy.utils.register_class(MorsePanel)
    try:
        bpy.app.handlers.scene_update_post.append(update)
    except AttributeError:
        pass
def unregister():
    bpy.utils.unregister_class(MorseObjectDialog)
    bpy.utils.unregister_class(MorseDoorDialog)
    bpy.utils.unregister_class(MorseDrawerDialog)
    bpy.utils.unregister_class(MorseSwitchDialog)
    bpy.utils.unregister_class(MorseLightDialog)
    bpy.utils.unregister_class(MorsePanel)
    try:
        bpy.app.handlers.scene_update_post.remove(update)
    except AttributeError:
        pass