-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodBindWeapon.py
More file actions
123 lines (95 loc) · 3.35 KB
/
codBindWeapon.py
File metadata and controls
123 lines (95 loc) · 3.35 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
import bpy
# Tags to attach
GUN_PARENT_TAGS = {
"tag_weapon":"tag_weapon_right",
"tag_weapon1":"tag_weapon_left",
"tag_clip":"j_gun",
"tag_clip":"tag_weapon",
"tag_knife":"tag_knife_attach"
}
GUN_PARENT_TAGS_JGUN = {
"j_gun":"tag_weapon",
"j_gun1":"tag_weapon1",
"tag_clip":"j_gun",
"tag_clip1":"j_gun1"
}
GUN_ORIGIN_TAGS = [
"j_gun",
"j_gun1",
"tag_weapon_right",
"tag_weapon_left",
"tag_weapon",
"tag_knife"
]
def apply_armature(ob, remake):
view_layer = bpy.context.view_layer
oldAct = view_layer.objects.active
for obj in bpy.context.scene.objects:
oldSel = obj.select
obj.select = True
view_layer.objects.active = obj
for mod in obj.modifiers:
if mod.type == "ARMATURE" and mod.object == ob:
bpy.ops.object.modifier_apply(modifier = mod.name)
if remake:
obj.modifiers.new(name = 'Skeleton', type = 'ARMATURE')
if 'Skeleton' in obj.modifiers:
obj.modifiers['Skeleton'].object = ob
obj.select = oldSel
view_layer.objects.active = oldAct
def weapon_bind(ob):
view_layer = bpy.context.view_layer
view_layer.objects.active = ob
ob.select = True
bpy.ops.object.mode_set(mode = 'EDIT')
targetAr = GUN_PARENT_TAGS
if "j_gun" in ob.data.bones:
targetAr = GUN_PARENT_TAGS_JGUN
print("using j_gun type parents for " + ob.name)
for boneTarget, boneParTarget in targetAr.items():
if boneTarget in ob.data.edit_bones and boneParTarget in ob.data.edit_bones:
ob.data.edit_bones[boneTarget].parent = ob.data.edit_bones[boneParTarget]
bpy.ops.object.mode_set(mode = 'POSE')
for boneName in GUN_ORIGIN_TAGS:
if boneName in ob.pose.bones:
poseBone = ob.pose.bones[boneName]
if not poseBone.parent is None:
nc = poseBone.constraints.new(type='COPY_ROTATION')
nc.target = ob
nc.subtarget = poseBone.parent.name
nc.influence = 1
nc.name = "bindRot"
nc2 = poseBone.constraints.new(type='COPY_LOCATION')
nc2.target = ob
nc2.subtarget = poseBone.parent.name
nc2.influence = 1
nc2.name = "bindPos"
apply_armature(ob, True)
bpy.ops.pose.armature_apply()
bpy.ops.object.mode_set(mode = 'POSE')
for poseBone in ob.pose.bones:
for const in poseBone.constraints:
if nc.name == "bindRot" or nc.name == "bindPos":
poseBone.constraints.remove(const)
def run():
view_layer = bpy.context.view_layer
oldActive = view_layer.objects.active
for ob in bpy.data.objects:
if ob.type != 'ARMATURE':
continue
if not ob.select:
continue
view_layer.objects.active = ob
bpy.ops.object.mode_set(mode='POSE')
weapon_bind(ob)
view_layer.objects.active = oldActive
class CODBindWeapon(bpy.types.Operator):
bl_idname = "object.cod_bind"
bl_label = "CoD Tools - Bind Weapon Bones"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return ( not context.object is None ) and context.object.type == 'ARMATURE'
def execute(self, context):
run()
return {'FINISHED'}