forked from bookyakuno/Blender-Scramble-Addon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDATA_PT_skeleton.py
168 lines (146 loc) · 5.33 KB
/
DATA_PT_skeleton.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
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
# 「プロパティ」エリア > 「アーマチュアデータ」タブ > 「スケルトン」パネル
# "Propaties" Area > "Armature" Tab > "Skeleton" Panel
import bpy
from bpy.props import *
import numpy as np
################
# オペレーター #
################
class ShowAllBoneLayers(bpy.types.Operator):
bl_idname = "pose.show_all_bone_layers"
bl_label = "Show All Layers"
bl_description = "Show all the active armature's layers"
bl_options = {'REGISTER'}
@classmethod
def poll(cls, context):
if (context.object):
if (context.object.type == 'ARMATURE'):
return True
return False
def execute(self, context):
context.object.data.layers = [True] * 32
return {'FINISHED'}
class ShowGroupLayers(bpy.types.Operator):
bl_idname = "pose.show_group_layers"
bl_label = "Show selected bone group's layers"
bl_description = "Show layers containing the selected pose group"
bl_options = {'REGISTER'}
group_name : StringProperty(name="Bone Group", default="")
extend : BoolProperty(name="Extend Selection", default=False)
@classmethod
def poll(cls, context):
if (context.object):
if (context.object.type == 'ARMATURE'):
if (context.mode == 'POSE'):
return True
return False
def invoke(self, context, event):
if (event.shift):
self.extend = True
else:
self.extend = False
return self.execute(context)
def update_group_idx(self, context, idx, method):
if method == "NEW":
g_idx = "0"*12
g_idx = g_idx[:idx] + "1" + g_idx[idx+1:]
elif method == "EXTEND":
g_idx = context.active_object.scramble_sk_prop.bone_group_idx
g_idx = g_idx[:idx] + "1" + g_idx[idx+1:]
elif method == "SUBTRACT":
g_idx = context.active_object.scramble_sk_prop.bone_group_idx
g_idx = g_idx[:idx] + "0" + g_idx[idx+1:]
context.active_object.scramble_sk_prop.bone_group_idx = g_idx
def execute(self, context):
pre_LAYERS = np.array(context.object.data.layers, dtype=np.float16)
target = context.active_object.pose.bone_groups[self.group_name]
target_idx = context.active_object.pose.bone_groups.find(target.name)
bone_layers = []
context.object.data.layers = [True]*32
context.active_object.pose.bone_groups.active = target
bpy.ops.pose.select_all(action='DESELECT')
bpy.ops.pose.group_select()
for b in context.selected_pose_bones:
if b.bone.layers not in bone_layers:
arr = np.array(b.bone.layers[:], dtype=np.float16)
bone_layers.append(arr)
summation = np.array([False]*32, dtype=np.float16)
for i in range(len(bone_layers)):
summation = summation + bone_layers[i]
if self.extend:
LAYERS = ((pre_LAYERS + summation) > 0).tolist()
PRE_LAYERS = (pre_LAYERS > 0).tolist()
self.update_group_idx(context, target_idx, 'EXTEND')
if (LAYERS == PRE_LAYERS):
LAYERS = ((pre_LAYERS - summation) > 0).tolist()
self.update_group_idx(context, target_idx, 'SUBTRACT')
else:
LAYERS = (summation > 0).tolist()
self.update_group_idx(context, target_idx, 'NEW')
context.object.data.layers = LAYERS
bpy.ops.pose.select_all(action='DESELECT')
return {'FINISHED'}
class ScrambleSkeltonPropGroup(bpy.types.PropertyGroup):
use_panel : bpy.props.BoolProperty(
name="Display on panel",
description="",
default=False
)
bone_group_idx : bpy.props.StringProperty(
name="bone-group index",
description="Str-index of displayed bone groups",
default="000000000000000000000"
)
################
# クラスの登録 #
################
classes = [
ShowAllBoneLayers,
ShowGroupLayers,
ScrambleSkeltonPropGroup
]
def register():
for cls in classes:
bpy.utils.register_class(cls)
bpy.types.Object.scramble_sk_prop = bpy.props.PointerProperty(type=ScrambleSkeltonPropGroup)
def unregister():
for cls in classes:
bpy.utils.unregister_class(cls)
del bpy.types.Object.scramble_sk_prop
################
# メニュー追加 #
################
# メニューのオン/オフの判定
def IsMenuEnable(self_id):
for id in bpy.context.preferences.addons[__name__.partition('.')[0]].preferences.disabled_menu.split(','):
if (id == self_id):
return False
else:
return True
# メニューを登録する関数
def menu(self, context):
if (IsMenuEnable(__name__.split('.')[-1])):
layout = self.layout
obj = context.active_object
row = layout.row(align=True)
row.prop(obj.scramble_sk_prop, 'use_panel', icon="TRIA_DOWN" if obj.scramble_sk_prop.use_panel else "TRIA_RIGHT", icon_only=True, emboss=False)
sp = row.split(factor=0.6)
row = sp.row(align=True)
row.alignment="LEFT"
row.prop(obj.scramble_sk_prop, 'use_panel',text="Show for Bone Group",emboss=False)
sp.operator(ShowAllBoneLayers.bl_idname, icon='RESTRICT_VIEW_OFF', text="Show All Layers")
if obj.scramble_sk_prop.use_panel:
box = layout.box()
if not len(context.active_object.pose.bone_groups):
box.label(text="No Bone groups",icon="NONE")
else:
row = box.row()
for idx, g in enumerate(context.active_object.pose.bone_groups):
if idx != 0 and idx % 3 == 0:
row = box.row()
if context.active_object.scramble_sk_prop.bone_group_idx[idx] == "1":
icon = 'KEYTYPE_MOVING_HOLD_VEC'
else: icon = 'BLANK1'
row.operator(ShowGroupLayers.bl_idname, text=f"{g.name}", icon=icon,translate=False).group_name = g.name
if (context.preferences.addons[__name__.partition('.')[0]].preferences.use_disabled_menu):
layout.operator('wm.toggle_menu_enable', icon='CANCEL').id = __name__.split('.')[-1]