-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_parent_to_skin_279.py
89 lines (73 loc) · 2.95 KB
/
convert_parent_to_skin_279.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
# coding: utf-8
import bpy
import os
import re, fnmatch, glob
from mathutils import Vector
C = bpy.context
D = bpy.data
scn = C.scene
def VertexGroupToBone(ob, targetRig, targetBone, context):
'''
Add a vertex group to the object named afer the given bone
assign full weight to this vertex group
return a list of bypassed object (due to vertex group already existed)
'''
#if the vertex group related to the chosen bone is'nt here, create i and Skin parent (full weight)
if not targetBone in [i.name for i in ob.vertex_groups]:
vg = ob.vertex_groups.new(name=targetBone)
else: #vertex group exist, or weight it (leave it untouched ?)
vg = ob.vertex_groups[targetBone]
verts = [i.index for i in ob.data.vertices]
vg.add(verts, 1, "ADD")
def CreateArmatureModifier(ob, targetRig):
'''
Create armature modifier if necessary and place it on top of stack
or just after the first miror modifier
return a list of bypassed objects
'''
#get object from armature data with a loop (only way to get armature's owner)
for obArm in bpy.data.objects:
if obArm.type == 'ARMATURE' and obArm.data.name == targetRig:
ArmatureObject = obArm
#add armature modifier that points to designated rig:
if not 'ARMATURE' in [m.type for m in ob.modifiers]:
mod = ob.modifiers.new('Armature', 'ARMATURE')
mod.object = ArmatureObject#bpy.data.objects[targetRig]
#bring Armature modifier to the top of the stack
pos = 1
if 'MIRROR' in [m.type for m in ob.modifiers]:
#if mirror, determine it's position
for mod in ob.modifiers:
if mod.type == 'MIRROR':
pos += 1
break
else:
pos += 1
if len(ob.modifiers) > 1:
for i in range(len(ob.modifiers) - pos):
bpy.ops.object.modifier_move_up(modifier="Armature")
else: #armature already exist
for m in ob.modifiers:
if m.type == 'ARMATURE':
m.object = ArmatureObject#bpy.data.objects[targetRig]
keep_transform = 1
print('-'*5)
for ob in bpy.context.selected_objects:
if ob.parent:
print("ob has parent", ob.parent.name)
targetRig = ob.parent.data.name
if ob.parent_type == 'BONE':
print("is bone parented to")
if ob.parent_bone:
targetBone = ob.parent_bone
print("ob.parent_bone", ob.parent_bone)#Dbg
if keep_transform:
#Clear and keep transform (matrix reattribution)
matrixcopy = ob.matrix_world.copy()
ob.parent = None
ob.matrix_world = matrixcopy
else:
ob.parent = None
#replace by armature
CreateArmatureModifier(ob, targetRig)
VertexGroupToBone(ob, targetRig, targetBone, bpy.context)