Created
April 28, 2015 16:07
-
-
Save nutti/f640e6cb16b029c1242f to your computer and use it in GitHub Desktop.
[Blender] Blenderスクリプトでマウスやキーボードのイベントを扱う方法 ref: http://qiita.com/nutti/items/aeb2cd47d704d2368f19
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 'N'キーを押したときにVIEW 3Dの右側に表示されるメニューにボタンを設置 | |
class OBJECT_PT_MKET(bpy.types.Panel): | |
bl_label = "Mouse/Keyboard Event Test" | |
bl_space_type = "VIEW_3D" | |
bl_region_type = "UI" | |
def draw(self, context): | |
# ボタンの配置 | |
# ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def invoke(self, context, event): | |
global running | |
if context.area.type == 'VIEW_3D': | |
# 動作していない状態でボタンを押されたら、各イベントを受け付ける | |
if running is False: | |
running = True | |
# 初期状態の設定 | |
# .... | |
# modalハンドラの設定 | |
context.window_manager.modal_handler_add(self) | |
return {'RUNNING_MODAL'} | |
return {'CANCELLED'} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def modal(self, context, event): | |
global running | |
# 動作していない場合は終了 | |
if running is False: | |
return {'PASS_THROUGH'} | |
# .... | |
# マウス移動 - マウスのX座標をもとにオブジェクトの拡大率を設定 | |
if event.type == 'MOUSEMOVE': | |
factor = 1.0 / 100.0 | |
self.obj_scale = 1.0 + (event.mouse_x - self.orig_x) * factor | |
return {'RUNNING_MODAL'} | |
# スペースキーを押した時 - 拡大率をボタンを押した時に戻す | |
elif event.type == 'SPACE': | |
if event.value == 'PRESS': | |
self.orig_x = event.mouse_x | |
return {'RUNNING_MODAL'} | |
# 右クリックを押した時 - 変更した拡大率を確定して操作を終了 | |
if event.type == 'RIGHTMOUSE': | |
if event.value == 'PRESS': | |
running = False | |
return {'FINISHED'} | |
return {'RUNNING_MODAL'} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
bl_info = { | |
"name" : "Mouse/Keyboard event test", | |
"author" : "Nutti", | |
"version" : (0,1), | |
"blender" : (2, 7, 0), | |
"location" : "UV > Mouse/Keyboard event test", | |
"description" : "Mouse/Keyboard event test.", | |
"warning" : "", | |
"wiki_url" : "", | |
"tracker_url" : "", | |
"category" : "UV" | |
} | |
running = False # イベントの取得中であればTrue | |
class MouseKeyboardEventTest(bpy.types.Operator): | |
"""Mouse/Keyboard event test.""" | |
bl_idname = "uv.mouse_keyboard_event_test" | |
bl_label = "Mouse/Keyboard event test" | |
bl_description = "Mouse/Keyboard event test" | |
bl_options = {'REGISTER', 'UNDO'} | |
obj_scale = 1.0 # オブジェクトの拡大率 | |
orig_x = 0.0 # ボタンを押したときのX座標 | |
orig_scale = [] # ボタンを押したときのオブジェクトの拡大率 | |
def modal(self, context, event): | |
global running | |
# 動作していない場合は終了 | |
if running is False: | |
return {'PASS_THROUGH'} | |
# オブジェクトへ拡大率を適用 | |
active_obj = bpy.context.active_object | |
active_obj.scale[0] = self.orig_scale[0] * self.obj_scale | |
active_obj.scale[1] = self.orig_scale[1] * self.obj_scale | |
active_obj.scale[2] = self.orig_scale[2] * self.obj_scale | |
############################################# | |
# マウス/キーボードのイベントを扱う処理 | |
############################################# | |
# マウス移動 - マウスのX座標をもとにオブジェクトの拡大率を設定 | |
if event.type == 'MOUSEMOVE': | |
factor = 1.0 / 100.0 | |
self.obj_scale = 1.0 + (event.mouse_x - self.orig_x) * factor | |
return {'RUNNING_MODAL'} | |
# スペースキーを押した時 - 拡大率をボタンを押した時に戻す | |
elif event.type == 'SPACE': | |
if event.value == 'PRESS': | |
self.orig_x = event.mouse_x | |
return {'RUNNING_MODAL'} | |
# 右クリックを押した時 - 変更した拡大率を確定して操作を終了 | |
if event.type == 'RIGHTMOUSE': | |
if event.value == 'PRESS': | |
running = False | |
return {'FINISHED'} | |
return {'RUNNING_MODAL'} | |
def invoke(self, context, event): | |
global running | |
if context.area.type == 'VIEW_3D': | |
# 動作していない状態でボタンを押されたら、各イベントを受け付ける | |
if running is False: | |
running = True | |
# 初期状態の設定 | |
self.obj_scale = 1.0 | |
self.orig_x = event.mouse_x | |
active_obj = bpy.context.active_object | |
self.orig_scale = [ | |
active_obj.scale[0], | |
active_obj.scale[1], | |
active_obj.scale[2] | |
] | |
# modalハンドラの設定 | |
context.window_manager.modal_handler_add(self) | |
return {'RUNNING_MODAL'} | |
return {'CANCELLED'} | |
# 'N'キーを押したときにVIEW 3Dの右側に表示されるメニューにボタンを設置 | |
class OBJECT_PT_MKET(bpy.types.Panel): | |
bl_label = "Mouse/Keyboard Event Test" | |
bl_space_type = "VIEW_3D" | |
bl_region_type = "UI" | |
def draw(self, context): | |
sc = context.scene | |
layout = self.layout | |
# ボタンの配置 | |
layout.operator(MouseKeyboardEventTest.bl_idname, text="Start", icon="PLAY") | |
def register(): | |
bpy.utils.register_module(__name__) | |
def unregister(): | |
bpy.utils.unregister_module(__name__) | |
if __name__ == "__main__": | |
register() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment