forked from TianD/TianD_KX_TOOL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TianD_CorrectiveBlendShape.py
152 lines (131 loc) · 4.56 KB
/
TianD_CorrectiveBlendShape.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
#coding:utf-8
'''
Created on 2015/7/22
@author: TianD
description: corrective blendshape
reference: https://circecharacterworks.wordpress.com/2011/03/26/presentation/
'''
import pymel.core as pm
def getBaseShape():
sel = pm.ls(sl=1)
if len(sel) == 1:
if sel[0].getChildren():
basemesh = sel[0].getShape()
if basemesh and basemesh.type() == "mesh":
return sel[0]
else :
return False
def duplicateSculptShape(obj):
if obj.type() == "transform":
transform = obj
elif obj.type() == "mesh":
transform = obj.getParent()
else :
return False
closeDeformer(transform)
sculptShape = pm.duplicate(transform, name = "%s_blendshape" %transform.name())[0]
clearGeometry(sculptShape)
openDeformer(transform)
pm.blendShape(transform, sculptShape, origin = "local", w=(0,1))
tweak = sculptShape.getShape().inputs(type = "tweak")[0]
pm.delete(tweak)
return sculptShape
def closeDeformer(obj):
if obj.type() == "transform":
shape = obj.getShape()
elif obj.type() == "mesh":
shape = obj
else :
return False
deformers =[h for h in shape.listHistory() if h.hasAttr("envelope")]
for d in deformers:
d.envelope.set(0)
return True
def openDeformer(obj):
if obj.type() == "transform":
shape = obj.getShape()
elif obj.type() == "mesh":
shape = obj
else :
return False
deformers =[h for h in shape.listHistory() if h.hasAttr("envelope")]
for d in deformers:
if not d.envelope.get():
d.envelope.set(1)
return True
def clearGeometry(obj):
shapes = obj.getShapes()
if shapes:
for shape in shapes:
if shape.isIntermediate() :
pm.delete(shape)
else :
return False
return True
def getSkinCluster(baseShape):
if baseShape.type() == "mesh":
skinName = pm.mel.eval( 'findRelatedSkinCluster("%s")'%baseShape.name() )
if skinName:
return pm.PyNode(skinName)
else :
return False
else :
return False
def getInfluenceWeight(baseShape, skinNode):
influenceWeight = list(skinNode.getWeights(baseShape))
return influenceWeight
def getWeightedInverseMatrix(numInfluence, bindPreMatrix, transformationMatrix, influenceWeight):
finalMatrix = pm.dt.Matrix([0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0])
for n in range(numInfluence):
if transformationMatrix[n] :
finalMatrix +=bindPreMatrix[n]*transformationMatrix[n]*influenceWeight[n]
else :
pass
inverseMatrix = finalMatrix.inverse()
return inverseMatrix
def getFinalPosition(sculptShape, vtxID, inverseMatrix):
if sculptShape.type() == "mesh":
try:
pos = sculptShape.vtx[vtxID].getPosition()
except:
return False
finalPosition = pos*inverseMatrix
return finalPosition
def setFinalPosition(sculptShape, vtxID, vertexPosition):
if sculptShape.type() == "mesh":
try:
sculptShape.vtx[vtxID].setPosition(vertexPosition)
return True
except:
return False
else :
return False
def doCorrection(baseShape, sculptShape):
skinNode = getSkinCluster(baseShape)
numInfluence = skinNode.numInfluenceObjects()
bindPreMatrix = skinNode.bindPreMatrix.get()
transformationMatrix = skinNode.matrix.get()
finalPositionStorage = []
for i,v in enumerate(baseShape.vtx):
base_pos = v.getPosition()
sculpt_pos = sculptShape.vtx[i].getPosition()
diff = sculpt_pos - base_pos
if diff.x >= -0.001 and diff.x <= 0.001 and diff.y >= -0.001 and diff.y <= 0.001 and diff.z >= -0.001 and diff.z <= 0.001:
pass
else :
weight = getInfluenceWeight(v, skinNode)[0]
if weight <= 0.0001 :
pass
else :
inverseMatrix = getWeightedInverseMatrix(numInfluence, bindPreMatrix, transformationMatrix, weight)
finalPosition = getFinalPosition(sculptShape, i, inverseMatrix)
finalPositionStorage.append(finalPosition)
setFinalPosition(sculptShape, i, finalPosition)
#dis = finalPosition - base_pos
#sculptShape.vtx[i].translateBy(dis)
if __name__ == "__main__":
base = getBaseShape()
sculpt = duplicateSculptShape(base)
baseShape = base.getShape()
sculptShape = sculpt.getShape()
doCorrection(baseShape, sculptShape)