-
Notifications
You must be signed in to change notification settings - Fork 5
/
blendahpeAnimation_demo.py
154 lines (129 loc) · 5.61 KB
/
blendahpeAnimation_demo.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
import sys
from PyQt5 import QtWidgets
from PyQT_form import Ui_Form
from util import *
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
from vtk.util.colors import *
class MyInteractor(vtk.vtkInteractorStyleTrackballCamera):
def __init__(self):
self.AddObserver('CharEvent', self.OnCharEvent)
self.AddObserver("KeyPressEvent", self.OnKeyPressEvent)
def OnCharEvent(self, obj, event):
pass
def OnKeyPressEvent(self, obj, event):
pass
class Blendshape(object):
def __init__(self, file_names, mean_file_name):
self.verts = []
self.face = None
for file in file_names:
v, f = loadObj(file)
self.verts.append(np.array(v))
self.face = f
self.number_blendshapes = len(self.verts)
self.weights = np.zeros((self.number_blendshapes,), dtype=np.float32)
self.sum_blendshapes = np.zeros_like(np.array(v), dtype=np.float32)
self.mean_value = self.Get_mean_valule(mean_file_name)
def Get_mean_valule(self, mean_mesh_file):
v, f = loadObj(mean_mesh_file)
return np.array(v, dtype=np.float32)
class MyPyQT_Form(QtWidgets.QWidget, Ui_Form, Blendshape):
def __init__(self, file_names, mean_file_name):
super(MyPyQT_Form, self).__init__()
super(Ui_Form, self).__init__(file_names, mean_file_name)
self.setupUi(self)
self.vtkWidget = QVTKRenderWindowInteractor(self.frame)
self.ren = vtk.vtkRenderer()
self.vtkWidget.GetRenderWindow().AddRenderer(self.ren)
self.iren = self.vtkWidget.GetRenderWindow().GetInteractor()
self.vtkWidget.resize(999, 1109) # 设置vtk 交互窗口大小
self.camera = vtk.vtkCamera()
self.actor = vtk.vtkActor()
self.loadActor()
self.CreateScene()
def slider1_value_changed(self, value):
self.textEdit.setText("value is {}".format(value/100))
self.weights[0] = value/100
tmp = [self.weights[i] * self.verts[i] for i in range(0, self.number_blendshapes)]
self.sum_blendshapes = sum(tmp) + self.mean_value
print(self.sum_blendshapes.shape)
self.set_Actor_mapper()
def slider2_value_changed(self, value):
self.textEdit.setText("value is {}".format(value/100))
self.weights[1] = value/100
tmp = [self.weights[i] * self.verts[i] for i in range(0, self.number_blendshapes)]
self.sum_blendshapes = sum(tmp) + self.mean_value
self.set_Actor_mapper()
def slider3_value_changed(self, value):
self.textEdit.setText("value is {}".format(value/100))
self.weights[2] = value/100
tmp = [self.weights[i] * self.verts[i] for i in range(0, self.number_blendshapes)]
self.sum_blendshapes = sum(tmp) + self.mean_value
self.set_Actor_mapper()
def slider4_value_changed(self, value):
self.textEdit.setText("value is {}".format(value/100))
self.weights[3] = value/100
tmp = [self.weights[i] * self.verts[i] for i in range(0, self.number_blendshapes)]
self.sum_blendshapes = sum(tmp) + self.mean_value
self.set_Actor_mapper()
def CreateGround(self):
plane = vtk.vtkPlaneSource()
plane.SetXResolution(50)
plane.SetYResolution(50)
plane.SetCenter(0, 0, 0)
plane.SetNormal(0, 0, 1)
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(plane.GetOutputPort())
actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetRepresentationToWireframe()
actor.GetProperty().SetColor(light_grey)
transform = vtk.vtkTransform()
transform.Scale(2000, 2000, 1)
actor.SetUserTransform(transform)
return actor
def CreateScene(self):
style = MyInteractor()
self.iren.SetInteractorStyle(style) # 设置交互方式
self.ren.SetBackground(.2, .2, .2)
ground = self.CreateGround()
self.ren.AddActor(ground)
self.camera.SetViewAngle(30)
self.camera.SetFocalPoint(300, 0, 0)
self.camera.SetPosition(600, -800, 700)
self.camera.ComputeViewPlaneNormal()
self.camera.SetViewUp(0, 0, 0)
self.camera.Zoom(0.4)
self.ren.SetActiveCamera(self.camera)
self.iren.Initialize()
self.iren.Start()
def loadActor(self):
mapper = GetFaceMapper(self.mean_value, self.face)
self.actor.SetMapper(mapper)
r = vtk.vtkMath.Random(.4, 1.0)
g = vtk.vtkMath.Random(.4, 1.0)
b = vtk.vtkMath.Random(.4, 1.0)
self.actor.GetProperty().SetDiffuseColor(r, g, b)
self.actor.GetProperty().SetDiffuse(.8)
self.actor.GetProperty().SetSpecular(.5)
self.actor.GetProperty().SetSpecularColor(1.0, 1.0, 1.0)
self.actor.GetProperty().SetSpecularPower(30.0)
self.actor.RotateX(0)
transform = vtk.vtkTransform()
transform.Scale(50, 50, 50)
self.actor.SetUserTransform(transform)
self.actor.SetPosition(-0.5, -7, 1)
self.ren.AddActor(self.actor)
def set_Actor_mapper(self):
mapper = GetFaceMapper(self.sum_blendshapes, self.face)
print("hello")
self.actor.SetMapper(mapper)
# self.iren.ReInitialize()
self.iren.Render()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
file_names = [os.path.join("./Models", "1.obj"), os.path.join("./Models", "2.obj"), os.path.join("./Models", "3.obj"), os.path.join("./Models", "4.obj")]
mean_file = os.path.join("./models", "0.obj")
my_pyqt_form = MyPyQT_Form(file_names, mean_file)
my_pyqt_form.show()
sys.exit(app.exec_())