-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmain_pygame_fine_grained.py
209 lines (171 loc) · 6.39 KB
/
main_pygame_fine_grained.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
"""
fine-grained model example
live2d.v3.Model
More usage: package/live2d/v3/live2d.pyi
"""
import live2d.v3 as live2d
import resources
import os
# initialize memory allocation for live2d
live2d.init()
model = live2d.Model()
# LoadModelJson can be called without an OpenGL context
# because it only reads the model3.json file, motion3.json, exp3.json and moc3 files
model.LoadModelJson(
# os.path.join(resources.RESOURCES_DIRECTORY, "v3/小九/小九皮套(紫)/小九.model3.json")
os.path.join(resources.RESOURCES_DIRECTORY, "v3/Haru/Haru.model3.json")
)
# load extra motion file which is not defined in model3.json
model.LoadExtraMotion(
"extra",
0,
os.path.join(
resources.RESOURCES_DIRECTORY, "v3/public_motions/drag_down.motion3.json"
),
)
model.LoadExtraMotion(
"extra",
1,
os.path.join(
resources.RESOURCES_DIRECTORY, "v3/public_motions/touch_head.motion3.json"
),
)
# Get Basic Model Info
modelDir = model.GetModelHomeDir()
print(modelDir)
paramIds = model.GetParameterIds()
print(paramIds)
partIds = model.GetPartIds()
print(partIds)
# the relation between part and drawable
# part[drawable1, drawable2, ...]
drawableIds = model.GetDrawableIds()
print(drawableIds)
expressions = model.GetExpressions()
print(expressions)
# only motions that are defined in model3.json
# extra motions are not included
motions = model.GetMotions()
print(motions)
import pygame
import time
pygame.init()
pygame.display.set_mode((500, 700), pygame.DOUBLEBUF | pygame.OPENGL | pygame.RESIZABLE)
model.Resize(500, 700)
# bind inner opengl functions
live2d.glInit()
# CreateRenderer should be called after OpenGL context is created and glInit is called
# maskBufferCount = 2
# more info about maskBufferCount: https://docs.live2d.com/zh-CHS/cubism-sdk-manual/ow-sdk-mask-premake/
# typically maskBufferCount = 2 is enough for most cases
model.CreateRenderer(2)
lastUpdateTime = time.time()
running = True
offsetX = 0.0
offsetY = 0.0
scale = 1.0
degrees = 0.0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
break
# hit test
elif event.type == pygame.MOUSEWHEEL:
x, y = pygame.mouse.get_pos()
# to get the drawable ids being hit
hitDrawableIds = model.HitDrawable(x, y, True)
print("hit: ", hitDrawableIds)
# to get the part ids being hit
hitPartIds = model.HitPart(x, y, True)
print("hit:", hitPartIds)
# to test if the given area name is hit
if model.IsAreaHit("Head", x, y):
print("start expression: ", model.SetRandomExpression())
# some assertions to test if the algorithm is correct
if len(hitDrawableIds) > 0:
assert model.IsDrawableHit(drawableIds.index(hitDrawableIds[0]), x, y)
if len(hitPartIds):
assert model.IsPartHit(partIds.index(hitPartIds[0]), x, y)
# motion
elif event.type == pygame.MOUSEBUTTONDOWN:
model.StartRandomMotion(
onStart=lambda group, no: print(f"{group} {no} started"),
onFinish=lambda group, no: print(f"{group} {no} finished"),
)
elif event.type == pygame.MOUSEMOTION:
x, y = pygame.mouse.get_pos()
model.Drag(x, y)
# model transform
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
offsetY += 0.1
model.SetOffset(offsetX, offsetY)
elif event.key == pygame.K_DOWN:
offsetY -= 0.1
model.SetOffset(offsetX, offsetY)
elif event.key == pygame.K_LEFT:
offsetX -= 0.1
model.SetOffset(offsetX, offsetY)
elif event.key == pygame.K_RIGHT:
offsetX += 0.1
model.SetOffset(offsetX, offsetY)
elif event.key == pygame.K_u:
scale -= 0.1
model.SetScale(scale)
elif event.key == pygame.K_i:
scale += 0.1
model.SetScale(scale)
elif event.key == pygame.K_RIGHTBRACKET:
degrees -= 5
model.Rotate(degrees)
elif event.key == pygame.K_LEFTBRACKET:
degrees += 5
model.Rotate(degrees)
elif event.key == pygame.K_e:
model.StartMotion(
"extra", 0, 3,
onStart=lambda group, no: print(f"{group} {no} started"),
onFinish=lambda group, no: print(f"{group} {no} finished"),
)
elif event.key == pygame.K_r:
model.ResetExpression()
if not running:
break
live2d.clearBuffer()
ct = time.time()
# delta seconds
deltaSecs = ct - lastUpdateTime
deltaSecs = max(0.0001, deltaSecs)
lastUpdateTime = ct
# the following section is equal to LAppModel.Update()
# === Section Start Update() ===
# load cached parameters from last frame
motionUpdated = False
model.LoadParameters() # initialize params using cached values
if not model.IsMotionFinished():
motionUpdated = model.UpdateMotion(deltaSecs)
# if SetParameterValue is called here, the parameter will be saved to the cache
# model.SetParameterValue(StandardParams.ParamAngleX, params.AngleX, 1)
model.SaveParameters() # save params to cache for next frame
if not motionUpdated:
# auto blink
# update eye blink params if they are defined in the model3.json
model.UpdateBlink(deltaSecs)
model.UpdateExpression(deltaSecs)
model.UpdateDrag(deltaSecs)
# auto breath
# update breath params such as ParamBodyAngleX, ParamAngleX...
model.UpdateBreath(deltaSecs)
# create physics effects according to current and previous param values
# some params can be overridden by physics effects
model.UpdatePhysics(deltaSecs)
model.UpdatePose(deltaSecs)
# === Section End Update() ===
# Draw():
# 1. update meshes according to the parameters
# 2. draw meshes using opengl
model.Draw()
pygame.display.flip()
live2d.dispose()
pygame.quit()