-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
200 lines (159 loc) · 6.09 KB
/
Copy path__init__.py
File metadata and controls
200 lines (159 loc) · 6.09 KB
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
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
bl_info = {
"name": "SJ Handy Nator",
"author": "CaptainHansode",
"version": (0, 1, 0),
"blender": (2, 80, 0),
"location": "View3D > Sidebar > Tool Tab",
"description": "My Tools.",
"warning": "",
"wiki_url": "",
"tracker_url": "",
"category": "Animation",
}
import bpy
from bpy.app.handlers import persistent
# 再帰防止フラグ
_updating_parent = False
def sj_set_parent(self, context):
"""set parent"""
global _updating_parent
# 再帰防止チェック
if _updating_parent:
return
if context is None:
return
if len(context.selected_objects) == 0:
return
# フラグを設定して再帰を防止
_updating_parent = True
try:
for obj in bpy.context.selected_objects:
wmx = obj.matrix_world
obj.parent = self.obj_parnet
# これだと逆行列が適応されてしまうのでダメ
# obj.matrix_parent_inverse = actor_offset_root.matrix_world.inverted()
obj.matrix_world = wmx
finally:
# 必ずフラグをリセット
_updating_parent = False
@persistent
def callback_get_parent(self):
"""get parent callbacks"""
if len(bpy.context.selected_objects) != 0:
pass
# bpy.context.scene.sj_handy_nator_props.obj_parnet = bpy.context.selected_objects[0].parent
else:
bpy.context.scene.sj_handy_nator_props.obj_parnet = None
class SJHandyNatorProperties(bpy.types.PropertyGroup):
r"""カスタムプロパティを定義する"""
# obj_parnet: bpy.props.StringProperty(name="Parent", update=set_parent)
obj_parnet: bpy.props.PointerProperty(name="Parent", type=bpy.types.Object, update=sj_set_parent)
class SJHandySelTree(bpy.types.Operator):
"""Select Tree"""
bl_idname = "sj_tools.sj_handy_nator_sel_tree"
bl_label = "Tree Select"
@classmethod
def poll(cls, context):
r""""""
return context.active_object is not None
def _get_children(self, c_objs):
r"""ツリーを再帰回収"""
ret = []
for obj in c_objs:
ret.append(obj)
if len(obj.children) != 0:
# 再帰
ret.extend(self._get_children(obj.children))
return ret
def execute(self, context):
r""""""
sjsb = context.scene.sj_handy_nator_props
sel_list = []
sel_objs = {}
for obj in context.selected_objects: # 選択したオブジェクト
if len(obj.children) != 0:
sel_list.extend(self._get_children(obj.children))
for obj in sel_list:
obj.select_set(True)
bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1) # 再描画
return {'FINISHED'}
class SJHandyLy(object):
r"""set lay"""
def __init__(self, *args, **kwargs):
if len(bpy.context.selected_pose_bones) == 0:
return None
b = bpy.context.selected_pose_bones[-0]
sw = bpy.context.object.data.bones[b.name].layers[args[0]]
self.set_bone_layer(
bpy.context.selected_pose_bones, [args[0]], not(sw))
return None
def set_bone_layer(self, pbn_list=[], ly_list=[0], sw=True):
r"""set bone layers"""
# 一旦レイヤー状態を保存
saved_layers = [
layer_bool for layer_bool in bpy.context.active_object.data.layers]
# 表示
for ly in range(0, 32):
bpy.context.active_object.data.layers[ly] = True
for pbn in pbn_list:
for ly in ly_list:
bpy.context.object.data.bones[pbn.name].layers[ly] = sw
# 表示状態を復元して終わり
for i, layer_bool in enumerate(saved_layers):
bpy.context.active_object.data.layers[i] = layer_bool
return True
class SJHandyNator(bpy.types.Panel):
"""UI"""
bl_label = "SJ Handy Nator"
bl_idname = "SJHANDYNATOR_PT_PANEL"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI' # UIのタイプ
# bl_context = "posemode" # カスタムタブは名前を指定するだけで問題ない 他のツールのタブにも追加できる
bl_category = 'Tool'
# bl_category = 'SJTools'
bl_options = {'DEFAULT_CLOSED'} # デフォルトでは閉じている
def draw(self, context):
layout = self.layout
sj_prop = context.scene.sj_handy_nator_props
layout.label(text="Parent")
col = layout.column()
col.scale_y = 1.0
col.operator(SJHandySelTree.bl_idname, text="Select Tree")
# col.prop_search(sj_prop, "obj_parnet", bpy.data, "objects")
col.prop_search(sj_prop, "obj_parnet", context.scene, "objects")
classes = (
SJHandyNatorProperties,
SJHandyNator,
SJHandySelTree)
# Register all operators and panels
def register():
for cls in classes:
bpy.utils.register_class(cls)
bpy.types.Scene.sj_handy_nator_props = bpy.props.PointerProperty(
type=SJHandyNatorProperties)
# add callback
bpy.app.handlers.depsgraph_update_post.append(callback_get_parent)
def unregister():
for cls in classes:
bpy.utils.unregister_class(cls)
del bpy.types.Scene.sj_handy_nator_props
bpy.app.handlers.depsgraph_update_post.remove(callback_get_parent)
if __name__ == "__main__":
register()