Skip to content

Commit 15c6f9a

Browse files
committed
添加复位方法
1 parent 2f4b6b3 commit 15c6f9a

File tree

7 files changed

+73
-38
lines changed

7 files changed

+73
-38
lines changed

Framework/src/Model/CubismModel.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ class CubismModel
2727
/**
2828
* Structure for color information of drawing object
2929
*/
30+
csmVector<float>& GetSavedParameters()
31+
{
32+
return _savedParameters;
33+
}
34+
3035
struct DrawableColorData
3136
{
3237
/**

LAppModelWrapper.cpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,6 @@ static PyObject* PyLAppModel_StartRandomMotion(PyLAppModelObject* self, PyObject
192192
Py_RETURN_NONE;
193193
}
194194

195-
static PyObject* PyLAppModel_ClearMotions(PyLAppModelObject* self, PyObject* args, PyObject* kwargs)
196-
{
197-
self->model->ClearMotions();
198-
Py_RETURN_NONE;
199-
}
200-
201195
static PyObject* PyLAppModel_SetExpression(PyLAppModelObject* self, PyObject* args, PyObject* kwargs)
202196
{
203197
const char* expressionID;
@@ -673,14 +667,31 @@ static PyObject* PyLAppModel_GetPartScreenColor(PyLAppModelObject* self, PyObjec
673667
return Py_BuildValue("ffff", r, g, b, a);
674668
}
675669

670+
static PyObject* PyLAppModel_StopAllMotions(PyLAppModelObject* self, PyObject* args, PyObject* kwargs)
671+
{
672+
self->model->StopAllMotions();
673+
Py_RETURN_NONE;
674+
}
675+
676+
static PyObject* PyLAppModel_ResetParameters(PyLAppModelObject* self, PyObject* args, PyObject* kwargs)
677+
{
678+
self->model->ResetParameters();
679+
Py_RETURN_NONE;
680+
}
681+
682+
static PyObject* PyLAppModel_ResetPose(PyLAppModelObject* self, PyObject* args, PyObject* kwargs)
683+
{
684+
self->model->ResetPose();
685+
Py_RETURN_NONE;
686+
}
687+
676688
// 包装模块方法的方法列表
677689
static PyMethodDef PyLAppModel_methods[] = {
678690
{"LoadModelJson", (PyCFunction)PyLAppModel_LoadModelJson, METH_VARARGS, ""},
679691
{"Resize", (PyCFunction)PyLAppModel_Resize, METH_VARARGS, ""},
680692
{"Draw", (PyCFunction)PyLAppModel_Draw, METH_VARARGS, ""},
681693
{"StartMotion", (PyCFunction)PyLAppModel_StartMotion, METH_VARARGS | METH_KEYWORDS, ""},
682694
{"StartRandomMotion", (PyCFunction)PyLAppModel_StartRandomMotion, METH_VARARGS | METH_KEYWORDS, ""},
683-
{"ClearMotions", (PyCFunction)PyLAppModel_ClearMotions, METH_VARARGS | METH_KEYWORDS, ""},
684695

685696
{"SetExpression", (PyCFunction)PyLAppModel_SetExpression, METH_VARARGS | METH_KEYWORDS, ""},
686697
{"SetRandomExpression", (PyCFunction)PyLAppModel_SetRandomExpression, METH_VARARGS | METH_KEYWORDS, ""},
@@ -715,6 +726,12 @@ static PyMethodDef PyLAppModel_methods[] = {
715726

716727
{"SetPartScreenColor", (PyCFunction)PyLAppModel_SetPartScreenColor, METH_VARARGS, ""},
717728
{"GetPartScreenColor", (PyCFunction)PyLAppModel_GetPartScreenColor, METH_VARARGS, ""},
729+
730+
// 复位
731+
{"StopAllMotions", (PyCFunction)PyLAppModel_StopAllMotions, METH_VARARGS | METH_KEYWORDS, ""},
732+
{"ResetParameters", (PyCFunction)PyLAppModel_ResetParameters, METH_VARARGS | METH_KEYWORDS, ""},
733+
{"ResetPose", (PyCFunction)PyLAppModel_ResetPose, METH_VARARGS | METH_KEYWORDS, ""},
734+
718735
{NULL} // 方法列表结束的标志
719736
};
720737

Main/src/LAppModel.cpp

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -372,27 +372,10 @@ void LAppModel::Update()
372372
csmBool motionUpdated = false;
373373

374374
//-----------------------------------------------------------------
375-
if (_clearMotionFlag)
375+
_model->LoadParameters(); // 前回セーブされた状態を
376+
if (!_motionManager->IsFinished())
376377
{
377-
_clearMotionFlag = false;
378-
_motionManager->StopAllMotions();
379-
for (int i = 0; i < _parameterCount; i++)
380-
{
381-
_parameterValues[i] = _defaultParameterValues[i];
382-
}
383-
if (_pose)
384-
{
385-
_pose->Reset(_model);
386-
}
387-
Info("motion: cleared");
388-
}
389-
else
390-
{
391-
_model->LoadParameters(); // 前回セーブされた状態を
392-
if (!_motionManager->IsFinished())
393-
{
394-
motionUpdated = _motionManager->UpdateMotion(_model, _deltaTimeSeconds); // モーションを更新
395-
}
378+
motionUpdated = _motionManager->UpdateMotion(_model, _deltaTimeSeconds); // モーションを更新
396379
}
397380
_model->SaveParameters(); // 状態を保存
398381
//-----------------------------------------------------------------
@@ -974,9 +957,25 @@ void LAppModel::Rotate(float deg)
974957
_matrixManager.Rotate(deg);
975958
}
976959

977-
void LAppModel::ClearMotions()
960+
void LAppModel::StopAllMotions()
961+
{
962+
_motionManager->StopAllMotions();
963+
}
964+
965+
void LAppModel::ResetParameters()
966+
{
967+
for (int i = 0; i < _parameterCount; i++)
968+
{
969+
_parameterValues[i] = _defaultParameterValues[i];
970+
}
971+
}
972+
973+
void LAppModel::ResetPose()
978974
{
979-
_clearMotionFlag = true;
975+
if (_pose)
976+
{
977+
_pose->Reset(_model);
978+
}
980979
}
981980

982981
void LAppModel::ResetExpression()

Main/src/LAppModel.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,11 @@ class LAppModel : public Csm::CubismUserModel
188188

189189
void Rotate(float deg);
190190

191-
void ClearMotions();
191+
void StopAllMotions();
192+
193+
void ResetParameters();
194+
195+
void ResetPose();
192196

193197
void ResetExpression();
194198

package/live2d/v3/live2d.pyi

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,17 @@ class LAppModel:
243243
def GetPartMultiplyColor(self, partIndex: int) -> tuple[float]:
244244
...
245245

246-
def ClearMotions(self) -> None:
247-
...
248-
249246
def ResetExpression(self) -> None:
250247
"""
251248
重置为默认表情
252249
"""
250+
...
251+
252+
def StopAllMotions(self) -> None:
253+
...
254+
255+
def ResetParameters(self) -> None:
256+
...
257+
258+
def ResetPose(self) -> None:
253259
...

package/main_pygame.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def main():
2929
pygame.mixer.init()
3030
live2d.init()
3131

32-
display = (500, 700)
32+
display = (500, 600)
3333
pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
3434
pygame.display.set_caption("pygame window")
3535

@@ -135,7 +135,7 @@ def getHitFeedback(x, y):
135135
# log.Info(f"Clicked Part: {currentTopClickedPartId}")
136136
# model.Touch(x, y, onFinishMotionHandler=lambda : print("motion finished"), onStartMotionHandler=lambda group, no: print(f"started motion: {group} {no}"))
137137
# model.StartRandomMotion(group="TapBody", onFinishMotionHandler=lambda : print("motion finished"), onStartMotionHandler=lambda group, no: print(f"started motion: {group} {no}"))
138-
model.SetRandomExpression()
138+
# model.SetRandomExpression()
139139
model.StartRandomMotion(priority=3)
140140

141141
if event.type == pygame.KEYDOWN:

package/test_motion_stop.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import pygame
44
from pygame.locals import *
55

6-
# import live2d.v3 as live2d
7-
import live2d.v2 as live2d
6+
import live2d.v3 as live2d
7+
# import live2d.v2 as live2d
88
from live2d.utils import log
99

1010

@@ -46,7 +46,11 @@ def main():
4646
model.StartRandomMotion()
4747
elif event.type == pygame.KEYDOWN:
4848
if event.key == pygame.K_r:
49-
model.ClearMotions()
49+
model.ResetParameters() # 对于没有 pose 的模型,可以通过 ResetParameters 恢复初始动作
50+
elif event.key == pygame.K_s:
51+
model.StopAllMotions()
52+
elif event.key == pygame.K_p:
53+
model.ResetPose()
5054

5155
if not running:
5256
break

0 commit comments

Comments
 (0)