~ubuntu-branches/ubuntu/trusty/blender/trusty

« back to all changes in this revision

Viewing changes to release/scripts/templates_py/operator_modal.py

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-03-06 12:08:47 UTC
  • mfrom: (1.5.1) (14.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20130306120847-frjfaryb2zrotwcg
Tags: 2.66a-1ubuntu1
* Resynchronize with Debian (LP: #1076930, #1089256, #1052743, #999024,
  #1122888, #1147084)
* debian/control:
  - Lower build-depends on libavcodec-dev since we're not
    doing the libav9 transition in Ubuntu yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import bpy
 
2
from bpy.props import IntProperty, FloatProperty
 
3
 
 
4
 
 
5
class ModalOperator(bpy.types.Operator):
 
6
    """Move an object with the mouse, example"""
 
7
    bl_idname = "object.modal_operator"
 
8
    bl_label = "Simple Modal Operator"
 
9
 
 
10
    first_mouse_x = IntProperty()
 
11
    first_value = FloatProperty()
 
12
 
 
13
    def modal(self, context, event):
 
14
        if event.type == 'MOUSEMOVE':
 
15
            delta = self.first_mouse_x - event.mouse_x
 
16
            context.object.location.x = self.first_value + delta * 0.01
 
17
 
 
18
        elif event.type == 'LEFTMOUSE':
 
19
            return {'FINISHED'}
 
20
 
 
21
        elif event.type in {'RIGHTMOUSE', 'ESC'}:
 
22
            context.object.location.x = self.first_value
 
23
            return {'CANCELLED'}
 
24
 
 
25
        return {'RUNNING_MODAL'}
 
26
 
 
27
    def invoke(self, context, event):
 
28
        if context.object:
 
29
            self.first_mouse_x = event.mouse_x
 
30
            self.first_value = context.object.location.x
 
31
 
 
32
            context.window_manager.modal_handler_add(self)
 
33
            return {'RUNNING_MODAL'}
 
34
        else:
 
35
            self.report({'WARNING'}, "No active object, could not finish")
 
36
            return {'CANCELLED'}
 
37
 
 
38
 
 
39
def register():
 
40
    bpy.utils.register_class(ModalOperator)
 
41
 
 
42
 
 
43
def unregister():
 
44
    bpy.utils.unregister_class(ModalOperator)
 
45
 
 
46
 
 
47
if __name__ == "__main__":
 
48
    register()
 
49
 
 
50
    # test call
 
51
    bpy.ops.object.modal_operator('INVOKE_DEFAULT')