-
Notifications
You must be signed in to change notification settings - Fork 1
/
sample_3_2.py
123 lines (99 loc) · 3.94 KB
/
sample_3_2.py
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
import bpy
from bpy.props import BoolProperty, PointerProperty
from mathutils import Vector
bl_info = {
"name": "サンプル3-2: キーボードのキー入力に応じてオブジェクトを並進移動させる",
"author": "Nutti",
"version": (2, 0),
"blender": (2, 75, 0),
"location": "3Dビュー > プロパティパネル > オブジェクト並進移動",
"description": "キーボードからの入力に応じてオブジェクトを並進移動させるアドオン",
"warning": "",
"support": "TESTING",
"wiki_url": "",
"tracker_url": "",
"category": "Object"
}
# プロパティ
class TOM_Properties(bpy.types.PropertyGroup):
running = BoolProperty(
name="オブジェクト並進移動モード中",
description="オブジェクト並進移動モード中か?",
default=False
)
# オブジェクト並進移動モード時の処理
class TranslateObjectMode(bpy.types.Operator):
bl_idname = "object.translate_object_mode"
bl_label = "オブジェクト並進移動モード"
bl_description = "オブジェクト並進移動モードへ移行します"
def modal(self, context, event):
props = context.scene.tom_props
# 3Dビューの画面を更新
if context.area:
context.area.tag_redraw()
//! [exit_modal_mode]
# キーボードのQキーが押された場合は、オブジェクト並進移動モードを終了
if event.type == 'Q' and event.value == 'PRESS':
props.running = False
print("サンプル3-2: 通常モードへ移行しました。")
return {'FINISHED'}
//! [exit_modal_mode]
//! [check_key_state]
if event.value == 'PRESS':
value = Vector((0.0, 0.0, 0.0))
if event.type == 'X':
value.x = 1.0 if not event.shift else -1.0
if event.type == 'Y':
value.y = 1.0 if not event.shift else -1.0
if event.type == 'Z':
value.z = 1.0 if not event.shift else -1.0
//! [check_key_state]
//! [translate_object]
# 選択中のオブジェクトを並進移動する
bpy.ops.transform.translate(value=value)
//! [translate_object]
return {'RUNNING_MODAL'}
def invoke(self, context, event):
props = context.scene.tom_props
if context.area.type == 'VIEW_3D':
# 開始ボタンが押された時の処理
if props.running is False:
props.running = True
# modal処理クラスを追加
context.window_manager.modal_handler_add(self)
print("サンプル3-2: オブジェクト並進移動モードへ移行しました。")
return {'RUNNING_MODAL'}
else:
return {'CANCELLED'}
# UI
class OBJECT_PT_SOEM(bpy.types.Panel):
bl_label = "オブジェクト並進移動モード"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
def draw(self, context):
layout = self.layout
props = context.scene.tom_props
# 開始/停止ボタンを追加
if props.running is False:
layout.operator(
TranslateObjectMode.bl_idname, text="開始", icon="PLAY"
)
else:
layout.operator(
TranslateObjectMode.bl_idname, text="終了", icon="PAUSE"
)
def register():
bpy.utils.register_module(__name__)
sc = bpy.types.Scene
sc.tom_props = PointerProperty(
name="プロパティ",
description="本アドオンで利用するプロパティ一覧",
type=TOM_Properties
)
print("サンプル3-2: アドオン「サンプル3-2」が有効化されました。")
def unregister():
del bpy.types.Scene.tom_props
bpy.utils.unregister_module(__name__)
print("サンプル3-2: アドオン「サンプル3-2」が無効化されました。")
if __name__ == "__main__":
register()