This repository has been archived by the owner on Jun 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
operator_proportion_trick.py
375 lines (333 loc) · 14 KB
/
operator_proportion_trick.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
bl_info = {
"name": "Proportion Trick",
"author": "Β L Λ Ζ Ξ",
"version": (0, 1),
"blender": (2, 79, 0),
"location": "View3D > Tool Shelf > Β L Λ Ζ Ξ",
"description": "Automatically align the bones so you don't have to spend too much time doing it manually",
"warning": "",
"wiki_url": "",
"category": "Scene",
}
import bpy
from collections import OrderedDict
def main(context):
obj = bpy.data.objects
try:
armature = obj['gg']
except:
raise Exception('gg skeleton not found. You must rename imported skeleton to gg. Watch the video again carefully.')
if armature.type != 'ARMATURE': raise Exception('gg must be an ARMATURE not '+armature.type)
valvebipeds = [
'ValveBiped.Bip01_Pelvis',
'ValveBiped.Bip01_Spine',
'ValveBiped.Bip01_Spine1',
'ValveBiped.Bip01_Spine2',
'ValveBiped.Bip01_Spine4',
'ValveBiped.Bip01_Neck1',
'ValveBiped.Bip01_Head1',
'ValveBiped.Bip01_R_Clavicle',
'ValveBiped.Bip01_R_UpperArm',
'ValveBiped.Bip01_R_Forearm',
'ValveBiped.Bip01_R_Hand',
'ValveBiped.Bip01_R_Finger0',
'ValveBiped.Bip01_R_Finger01',
'ValveBiped.Bip01_R_Finger02',
'ValveBiped.Bip01_R_Finger1',
'ValveBiped.Bip01_R_Finger11',
'ValveBiped.Bip01_R_Finger12',
'ValveBiped.Bip01_R_Finger2',
'ValveBiped.Bip01_R_Finger21',
'ValveBiped.Bip01_R_Finger22',
'ValveBiped.Bip01_R_Finger3',
'ValveBiped.Bip01_R_Finger31',
'ValveBiped.Bip01_R_Finger32',
'ValveBiped.Bip01_R_Finger4',
'ValveBiped.Bip01_R_Finger41',
'ValveBiped.Bip01_R_Finger42',
'ValveBiped.Bip01_L_Clavicle',
'ValveBiped.Bip01_L_UpperArm',
'ValveBiped.Bip01_L_Forearm',
'ValveBiped.Bip01_L_Hand',
'ValveBiped.Bip01_L_Finger0',
'ValveBiped.Bip01_L_Finger01',
'ValveBiped.Bip01_L_Finger02',
'ValveBiped.Bip01_L_Finger1',
'ValveBiped.Bip01_L_Finger11',
'ValveBiped.Bip01_L_Finger12',
'ValveBiped.Bip01_L_Finger2',
'ValveBiped.Bip01_L_Finger21',
'ValveBiped.Bip01_L_Finger22',
'ValveBiped.Bip01_L_Finger3',
'ValveBiped.Bip01_L_Finger31',
'ValveBiped.Bip01_L_Finger32',
'ValveBiped.Bip01_L_Finger4',
'ValveBiped.Bip01_L_Finger41',
'ValveBiped.Bip01_L_Finger42',
'ValveBiped.Bip01_R_Thigh',
'ValveBiped.Bip01_R_Calf',
'ValveBiped.Bip01_R_Foot',
'ValveBiped.Bip01_R_Toe0',
'ValveBiped.Bip01_L_Thigh',
'ValveBiped.Bip01_L_Calf',
'ValveBiped.Bip01_L_Foot',
'ValveBiped.Bip01_L_Toe0',
]
valvebipeds2 = [
'ValveBiped.Bip01_L_Thigh',
'ValveBiped.Bip01_L_Calf',
'ValveBiped.Bip01_L_Calf',
'ValveBiped.Bip01_L_Foot',
'ValveBiped.Bip01_R_Thigh',
'ValveBiped.Bip01_R_Calf',
'ValveBiped.Bip01_R_Calf',
'ValveBiped.Bip01_R_Foot',
'ValveBiped.Bip01_L_UpperArm',
'ValveBiped.Bip01_L_Forearm',
'ValveBiped.Bip01_L_Forearm',
'ValveBiped.Bip01_L_Hand',
'ValveBiped.Bip01_R_UpperArm',
'ValveBiped.Bip01_R_Forearm',
'ValveBiped.Bip01_R_Forearm',
'ValveBiped.Bip01_R_Hand',
'ValveBiped.Bip01_L_Finger0',
'ValveBiped.Bip01_L_Finger01',
'ValveBiped.Bip01_L_Finger01',
'ValveBiped.Bip01_L_Finger02',
'ValveBiped.Bip01_L_Finger1',
'ValveBiped.Bip01_L_Finger11',
'ValveBiped.Bip01_L_Finger11',
'ValveBiped.Bip01_L_Finger12',
'ValveBiped.Bip01_L_Finger2',
'ValveBiped.Bip01_L_Finger21',
'ValveBiped.Bip01_L_Finger21',
'ValveBiped.Bip01_L_Finger22',
'ValveBiped.Bip01_L_Finger3',
'ValveBiped.Bip01_L_Finger31',
'ValveBiped.Bip01_L_Finger31',
'ValveBiped.Bip01_L_Finger32',
'ValveBiped.Bip01_L_Finger4',
'ValveBiped.Bip01_L_Finger41',
'ValveBiped.Bip01_L_Finger41',
'ValveBiped.Bip01_L_Finger42',
'ValveBiped.Bip01_R_Finger0',
'ValveBiped.Bip01_R_Finger01',
'ValveBiped.Bip01_R_Finger01',
'ValveBiped.Bip01_R_Finger02',
'ValveBiped.Bip01_R_Finger1',
'ValveBiped.Bip01_R_Finger11',
'ValveBiped.Bip01_R_Finger11',
'ValveBiped.Bip01_R_Finger12',
'ValveBiped.Bip01_R_Finger2',
'ValveBiped.Bip01_R_Finger21',
'ValveBiped.Bip01_R_Finger21',
'ValveBiped.Bip01_R_Finger22',
'ValveBiped.Bip01_R_Finger3',
'ValveBiped.Bip01_R_Finger31',
'ValveBiped.Bip01_R_Finger31',
'ValveBiped.Bip01_R_Finger32',
'ValveBiped.Bip01_R_Finger4',
'ValveBiped.Bip01_R_Finger41',
'ValveBiped.Bip01_R_Finger41',
'ValveBiped.Bip01_R_Finger42',
]
target = valvebipeds2[::2]
sub = valvebipeds2[1::2]
d = OrderedDict()
for idx, value in enumerate(sub):
key = 'var' + str(idx)
d[key] = value
for i in valvebipeds:
objbone = bpy.data.objects['proportions'].pose.bones[i].constraints
if armature.pose.bones.get(i) is not None:
objbone.new('COPY_LOCATION')
objbone['Copy Location'].target = armature
objbone['Copy Location'].subtarget = i
for j, k in enumerate(target):
objbone2 = bpy.data.objects['proportions'].pose.bones[k].constraints
if armature.pose.bones.get(k) is not None:
objbone2.new('LOCKED_TRACK')
objbone2['Locked Track'].target = armature
objbone2['Locked Track'].subtarget = d["var"+str(j)]
objbone2['Locked Track'].track_axis = 'TRACK_X'
objbone2['Locked Track'].lock_axis = 'LOCK_Z'
objbone2.new('LOCKED_TRACK')
objbone2['Locked Track.001'].target = armature
objbone2['Locked Track.001'].subtarget = d["var"+str(j)]
objbone2['Locked Track.001'].track_axis = 'TRACK_X'
objbone2['Locked Track.001'].lock_axis = 'LOCK_Y'
#added on Feb 2021
#this part removes LOCKED_TRACK on parent if child constraint is empty
for l in valvebipeds:
objbone3 = bpy.data.objects['proportions'].pose.bones[l]
for child in objbone3.children:
objbone4 = bpy.data.objects['proportions'].pose.bones[child.name].constraints
if not objbone4.keys():
if objbone3.parent is not None:
objbone5 = bpy.data.objects['proportions'].pose.bones[l].constraints
for constraint in objbone5:
if constraint.name != 'Copy Location':
objbone5.remove(constraint)
print(l+' is a parent of '+child.name+' with constraint: ',objbone3.constraints.keys())
bpy.context.scene.layers[1] = True
bpy.context.scene.layers[0] = False
bpy.context.scene.objects.active = bpy.data.objects["proportions"]
bpy.data.objects['proportions'].select = True
bpy.ops.object.mode_set(mode='POSE')
class ProportionTrick(bpy.types.Operator):
"""Tooltip"""
bl_idname = "blz.proportion_trick"
bl_label = "1. Proportion Trick"
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
main(context)
return {'FINISHED'}
class MergeSkeleton(bpy.types.Operator):
"""Tooltip"""
bl_idname = "blz.merge_skeleton"
bl_label = "2. Merge Skeleton"
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
arm = bpy.data.objects['gg']
arm2 = bpy.data.objects['proportions']
objects = bpy.context.scene.objects
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.duplicate()
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.armature.select_all(action='DESELECT')
valvebipeds = [
'ValveBiped.Bip01_Pelvis',
'ValveBiped.Bip01_Spine',
'ValveBiped.Bip01_Spine1',
'ValveBiped.Bip01_Spine2',
'ValveBiped.Bip01_Spine4',
'ValveBiped.Bip01_Neck1',
'ValveBiped.Bip01_Head1',
'ValveBiped.Bip01_R_Clavicle',
'ValveBiped.Bip01_R_UpperArm',
'ValveBiped.Bip01_R_Forearm',
'ValveBiped.Bip01_R_Hand',
'ValveBiped.Bip01_R_Finger0',
'ValveBiped.Bip01_R_Finger01',
'ValveBiped.Bip01_R_Finger02',
'ValveBiped.Bip01_R_Finger1',
'ValveBiped.Bip01_R_Finger11',
'ValveBiped.Bip01_R_Finger12',
'ValveBiped.Bip01_R_Finger2',
'ValveBiped.Bip01_R_Finger21',
'ValveBiped.Bip01_R_Finger22',
'ValveBiped.Bip01_R_Finger3',
'ValveBiped.Bip01_R_Finger31',
'ValveBiped.Bip01_R_Finger32',
'ValveBiped.Bip01_R_Finger4',
'ValveBiped.Bip01_R_Finger41',
'ValveBiped.Bip01_R_Finger42',
'ValveBiped.Bip01_L_Clavicle',
'ValveBiped.Bip01_L_UpperArm',
'ValveBiped.Bip01_L_Forearm',
'ValveBiped.Bip01_L_Hand',
'ValveBiped.Bip01_L_Finger0',
'ValveBiped.Bip01_L_Finger01',
'ValveBiped.Bip01_L_Finger02',
'ValveBiped.Bip01_L_Finger1',
'ValveBiped.Bip01_L_Finger11',
'ValveBiped.Bip01_L_Finger12',
'ValveBiped.Bip01_L_Finger2',
'ValveBiped.Bip01_L_Finger21',
'ValveBiped.Bip01_L_Finger22',
'ValveBiped.Bip01_L_Finger3',
'ValveBiped.Bip01_L_Finger31',
'ValveBiped.Bip01_L_Finger32',
'ValveBiped.Bip01_L_Finger4',
'ValveBiped.Bip01_L_Finger41',
'ValveBiped.Bip01_L_Finger42',
'ValveBiped.Bip01_R_Thigh',
'ValveBiped.Bip01_R_Calf',
'ValveBiped.Bip01_R_Foot',
'ValveBiped.Bip01_R_Toe0',
'ValveBiped.Bip01_L_Thigh',
'ValveBiped.Bip01_L_Calf',
'ValveBiped.Bip01_L_Foot',
'ValveBiped.Bip01_L_Toe0',
]
bn = []
pr = []
for bone in bpy.context.object.data.edit_bones:
if bone.name in valvebipeds:
bone.select = True
bone.select_head = True
bone.select_tail = True
bpy.ops.armature.delete()
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.move_to_layer(layers=(False, True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False))
bpy.context.scene.layers[1] = True
bpy.context.scene.layers[0] = False
bpy.context.scene.objects.active = bpy.data.objects["proportions"]
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.join()
bpy.ops.object.mode_set(mode='EDIT')
for bone in arm.data.bones:
if bone.name not in valvebipeds:
bn.append(bone.name)
for bone in arm.data.bones:
if bone.name not in valvebipeds:
pr.append(getattr(bone.parent, "name", "ValveBiped.Bip01_Pelvis"))
for bone in bpy.context.object.data.edit_bones:
j = 0
i = 0
while j < len(bn) and i < len(pr):
arm2.data.edit_bones[bn[i]].parent = arm2.data.edit_bones[pr[j]]
j += 1
i += 1
bpy.ops.object.mode_set(mode='OBJECT')
bpy.context.scene.layers[0] = True
bpy.context.scene.layers[1] = False
#add armature modifier
for ob in objects:
if ob.type == 'MESH':
if ob.modifiers.values() == []:
ob.modifiers.new('Armature', 'ARMATURE')
ob.modifiers['Armature'].object = bpy.data.objects['proportions']
else:
for mods in ob.modifiers.values():
if mods.name == 'Armature':
ob.modifiers['Armature'].object = bpy.data.objects['proportions']
else:
ob.modifiers.new('Armature', 'ARMATURE')
ob.modifiers['Armature'].object = bpy.data.objects['proportions']
bpy.context.scene.objects.active = bpy.data.objects["gg"]
bpy.data.objects['gg'].select = True
bpy.ops.object.move_to_layer(layers=(False, False, False, False, False, False, False, False, False, False, True, False, False, False, False, False, False, False, False, False))
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.move_to_layer(layers=(False, True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False))
bpy.context.scene.layers[1] = True
bpy.context.scene.layers[0] = False
return {'FINISHED'}
class ProportionTrickPanel(bpy.types.Panel):
"""Creates a Panel in the Object properties window"""
bl_label = "Proportion Trick"
bl_idname = "OBJECT_PT_proportion"
bl_space_type = 'VIEW_3D'
bl_region_type = 'TOOLS'
bl_category = "Β L Λ Ζ Ξ"
def draw(self, context):
layout = self.layout
row = layout.row()
row.operator("blz.proportion_trick")
row = layout.row()
row.operator("blz.merge_skeleton")
def register():
bpy.utils.register_class(ProportionTrick)
bpy.utils.register_class(MergeSkeleton)
bpy.utils.register_class(ProportionTrickPanel)
def unregister():
bpy.utils.unregister_class(ProportionTrick)
bpy.utils.unregister_class(MergeSkeleton)
bpy.utils.unregister_class(ProportionTrickPanel)
if __name__ == "__main__":
register()