Skip to content

Commit

Permalink
Merge pull request #25 from Arkueid/v2-rewrite-in-python
Browse files Browse the repository at this point in the history
rewrite v2 in python
  • Loading branch information
Arkueid authored Dec 15, 2024
2 parents 2ccb56f + 94e9e00 commit 46f6680
Show file tree
Hide file tree
Showing 389 changed files with 79,066 additions and 255 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ test

**.db

Resources/v2

!package/audio1.wav

!package/audio2.wav
Expand Down
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ message("${CMAKE_BUILD_TYPE}")

set(LIB_NAME LAppModelWrapper)
set(PROJECT_ROOT ${CMAKE_CURRENT_SOURCE_DIR})
set(RES_PATH ${CMAKE_CURRENT_SOURCE_DIR}/Resources)

# prevent python root dir being shadowed by registry
set(Python3_FIND_REGISTRY "NEVER")
Expand All @@ -25,7 +24,7 @@ if(DEFINED PYTHON_INSTALLATION_PATH)
else()
# 寻找Python
message("Not found PYTHON_INSTALLATION_PATH in environment variables. \nUse default path.")
set(CMAKE_PREFIX_PATH D:/Python/x86/3.12.0)
set(CMAKE_PREFIX_PATH D:/Python/x64/3.12)
endif()

if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
Expand Down
14 changes: 0 additions & 14 deletions LAppModelWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -775,19 +775,6 @@ static PyObject* live2d_glew_init()
Py_RETURN_NONE;
}

static PyObject* live2d_set_gl_properties()
{
// テクスチャサンプリング設定
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

// 透過設定
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Py_RETURN_NONE;
}

static PyObject* live2d_clear_buffer(PyObject* self, PyObject* args)
{
// 默认为黑色
Expand Down Expand Up @@ -833,7 +820,6 @@ static PyMethodDef live2d_methods[] = {
{"init", (PyCFunction)live2d_init, METH_VARARGS, ""},
{"dispose", (PyCFunction)live2d_dispose, METH_VARARGS, ""},
{"glewInit", (PyCFunction)live2d_glew_init, METH_VARARGS, ""},
{"setGLProperties", (PyCFunction)live2d_set_gl_properties, METH_VARARGS, ""},
{"clearBuffer", (PyCFunction)live2d_clear_buffer, METH_VARARGS, ""},
{"setLogEnable", (PyCFunction)live2d_set_log_enable, METH_VARARGS, ""},
{"logEnable", (PyCFunction)live2d_log_enable, METH_VARARGS, ""},
Expand Down
2 changes: 1 addition & 1 deletion Main/live2d/MatrixManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Csm::CubismMatrix44 &MatrixManager::GetProjection(LAppModel *model)
{
_projection.LoadIdentity();

if (model->GetModel()->GetCanvasWidth() > 1.0f && _ww < _ww)
if (model->GetModel()->GetCanvasWidth() > 1.0f && _ww < _wh)
{
// 横に長いモデルを縦長ウィンドウに表示する際モデルの横サイズでscaleを算出する
model->GetModelMatrix()->SetWidth(2.0f);
Expand Down
39 changes: 25 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,55 +11,62 @@

使用 Python 直接加载和操作 Live2D 模型,不通过 Web Engine 等间接手段进行渲染。

基于 Python C++ API 对 Live2D Native SDK (C++) 进行了封装。理论上,只要配置好 OpenGL 上下文,可在 Python 中将 live2d 绘制在任何基于 OpenGL 的窗口。
基于 Python C++ API 对 Live2D Native SDK (C++) 进行了封装。理论上,只要配置好 OpenGL 上下文,可在 Python 中将 live2d
绘制在任何基于 OpenGL 的窗口。

代码使用示例:[package](./package/)

详细使用文档:https://arkueid.github.io/live2d-py-docs/

加载 Cubism 2.1 及以下版本的 Live2D 模型使用 [v2](https://github.com/Arkueid/live2d-py/tree/v2) 分支

## 兼容UI库

理论上兼容所有能使用 OpenGL 进行绘制的UI库: Pygame / PyQt5 / PySide2 / PySide6 / GLFW / FreeGlut / Qfluentwidgets ...

## 支持功能
* 加载模型

* 加载模型:**Cubism 2.1****Cubism 3.0** 及以上版本
* 视线跟踪
* 点击交互
* 动作播放回调
* 口型同步
* 模型各部分参数控制
* 各部件透明度控制
* 精确到部件的点击检测

## 平台支持

| `live2d-py` | 支持的live2d模型 | 支持的Python版本 | 支持平台 |
|-------------|------------------------|----------------------------------------------------------------|---------------|
| `live2d.v2` | Cubism 2.1 以及更早的版本 | 仅 32 位,支持`Python 3.0` 及以上版本,但除 `Python 3.10.11` 外需要自行编译 | Windows |
| `live2d.v3` | Cubism 3.0 及以上版本 | 支持 `32` / `64` 位,支持`Python 3.0` 及以上版本,但除 `Python 3.12` 外需要自行编译 | Windows、Linux |
| `live2d-py` | 支持的live2d模型 | 实现 | 支持的Python版本 | 支持平台 |
|-------------|--------------------|-------------------|---------------------------------------|--------------------------|
| `live2d.v2` | Cubism 2.1 以及更早的版本 | 纯 Python 实现 | 支持 `32` / `64` 位,支持`Python 3.0` 及以上版本 | Winodws、Linux、MacOS(理论上) | |
| `live2d.v3` | Cubism 3.0 及以上版本 | Python C++ API 封装 | 支持 `32` / `64` 位,支持`Python 3.0` 及以上版本 | Windows、Linux |

注:

* `live2d.v2` 由 Cubism Web SDK 转写为纯 Python,尚未使用 numpy 等优化的库,性能有待提升。
* Cubism 2.X 导出的模型:文件名格式常为 `XXX.moc``XXX.model.json``XXX.mtn`
* Cubism 3.0 及以上导出的模型:文件名格式常为 `XXX.moc3``XXX.model3.json`, `XXX.motion3.json`
* 对于 Cubism 2.0 模型,网络上能找到的现存 live2d opengl 静态库只有 32 位,因此只能使用 32 位 Python 解释器加载。
* Cubism 3.0 及以上导出的模型:文件名格式常为 `XXX.moc3``XXX.model3.json`, `XXX.motion3.json`

## 安装方式

1. 通过 [PyPI](https://pypi.org/project/live2d-py/) 安装

```shell
pip install live2d-py
```

2.[Release](https://github.com/Arkueid/live2d-py/releases/latest) 中下载对应版本的 `whl` 文件并安装(推荐)

```shell
pip install live2d_py-0.X.X-cpXXX-cpXXX-win32.whl
```

3. 从源码构建,参考 [安装#源码构建](https://arkueid.github.io/live2d-py-docs/%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E/%E5%AE%89%E8%A3%85.html#%E6%BA%90%E7%A0%81%E6%9E%84%E5%BB%BA)
3.

从源码构建,参考 [安装#源码构建](https://arkueid.github.io/live2d-py-docs/%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E/%E5%AE%89%E8%A3%85.html#%E6%BA%90%E7%A0%81%E6%9E%84%E5%BB%BA)

## 简易面部动捕示例
源码见 [main_facial_bind_mediapipe.py](./package/main_facial_bind_mediapipe.py)

源码见 [main_facial_bind_mediapipe.py](./package/main_facial_bind_mediapipe.py)

![面捕-期末周破防](./docs/video_test.gif)

Expand All @@ -71,8 +78,12 @@ pip install live2d_py-0.X.X-cpXXX-cpXXX-win32.whl

## 贡献

特别感谢 [@96bearli][@Ovizro], [@AnyaCoder] 为本项目提供的帮助和支持。
特别感谢 [@96bearli][@Ovizro], [@AnyaCoder], [@jahtim] 为本项目提供的帮助和支持。

[@96bearli]: https://github.com/96bearli

[@Ovizro]: https://github.com/Ovizro
[@AnyaCoder]: https://github.com/AnyaCoder

[@AnyaCoder]: https://github.com/AnyaCoder

[@jahtim]: https://github.com/jahtim
Binary file added Resources/v2/Epsilon/Epsilon.1024/texture_00.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Resources/v2/Epsilon/Epsilon.1024/texture_01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Resources/v2/Epsilon/Epsilon.1024/texture_02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Resources/v2/Epsilon/Epsilon.moc
Binary file not shown.
71 changes: 71 additions & 0 deletions Resources/v2/Epsilon/Epsilon.model.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"version":"Sample 1.0.0",
"model":"Epsilon.moc",
"textures":[
"Epsilon.1024/texture_00.png",
"Epsilon.1024/texture_01.png",
"Epsilon.1024/texture_02.png"
],
"motions":{
"idle":[
{
"file":"motions/Epsilon_idle_01.mtn"
}
],
"null":[
{
"file":"motions/Epsilon_m_01.mtn"
},
{
"file":"motions/Epsilon_m_02.mtn"
},
{
"file":"motions/Epsilon_m_03.mtn"
},
{
"file":"motions/Epsilon_m_04.mtn"
},
{
"file":"motions/Epsilon_m_05.mtn"
},
{
"file":"motions/Epsilon_m_06.mtn"
},
{
"file":"motions/Epsilon_m_07.mtn"
},
{
"file":"motions/Epsilon_m_08.mtn"
},
{
"file":"motions/Epsilon_m_sp_01.mtn"
},
{
"file":"motions/Epsilon_m_sp_02.mtn"
},
{
"file":"motions/Epsilon_m_sp_03.mtn"
},
{
"file":"motions/Epsilon_m_sp_04.mtn"
},
{
"file":"motions/Epsilon_m_sp_05.mtn"
},
{
"file":"motions/Epsilon_shake_01.mtn"
}
]
},
"expressions":[
{"name":"f01.exp.json","file":"expressions/f01.exp.json"},
{"name":"f02.exp.json","file":"expressions/f02.exp.json"},
{"name":"f03.exp.json","file":"expressions/f03.exp.json"},
{"name":"f04.exp.json","file":"expressions/f04.exp.json"},
{"name":"f05.exp.json","file":"expressions/f05.exp.json"},
{"name":"f06.exp.json","file":"expressions/f06.exp.json"},
{"name":"f07.exp.json","file":"expressions/f07.exp.json"},
{"name":"f08.exp.json","file":"expressions/f08.exp.json"}
],
"physics":"Epsilon.physics.json"
}
77 changes: 77 additions & 0 deletions Resources/v2/Epsilon/Epsilon.physics.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
{
"type":"Live2D Physics",
"physics_hair":[
{
"lebel":"BACK",
"setup":{
"length":0.24,
"regist":0.5,
"mass":0.18
},
"src":[
{
"id":"PARAM_ANGLE_X",
"ptype":"x",
"scale":0.005,
"weight":1
},
{
"id":"PARAM_ANGLE_Z",
"ptype":"angle",
"scale":0.8,
"weight":1
}
],
"targets":[
{
"id":"PARAM_HAIR_BACK_L",
"ptype":"angle",
"scale":0.005,
"weight":1
},
{
"id":"PARAM_HAIR_BACK_R",
"ptype":"angle",
"scale":0.005,
"weight":1
}
]
},
{
"lebel":"SIDE",
"setup":{
"length":0.2,
"regist":0.5,
"mass":0.14
},
"src":[
{
"id":"PARAM_ANGLE_X",
"ptype":"x",
"scale":0.004,
"weight":1
},
{
"id":"PARAM_ANGLE_Z",
"ptype":"angle",
"scale":0.8,
"weight":1
}
],
"targets":[
{
"id":"PARAM_HAIR_SIDE_L",
"ptype":"angle",
"scale":0.01,
"weight":1
},
{
"id":"PARAM_HAIR_SIDE_R",
"ptype":"angle",
"scale":0.01,
"weight":1
}
]
}
]
}
5 changes: 5 additions & 0 deletions Resources/v2/Epsilon/expressions/f01.exp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type":"Live2D Expression",
"fade_in":500,
"fade_out":500
}
13 changes: 13 additions & 0 deletions Resources/v2/Epsilon/expressions/f02.exp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"type":"Live2D Expression",
"fade_in":500,
"fade_out":500,
"params":[
{"id":"PARAM_EYE_L_OPEN","val":0,"calc":"mult"},
{"id":"PARAM_EYE_L_SMILE","val":1},
{"id":"PARAM_EYE_R_OPEN","val":0,"calc":"mult"},
{"id":"PARAM_EYE_R_SMILE","val":1},
{"id":"PARAM_BROW_L_ANGLE","val":0.48},
{"id":"PARAM_BROW_R_ANGLE","val":0.46}
]
}
17 changes: 17 additions & 0 deletions Resources/v2/Epsilon/expressions/f03.exp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"type":"Live2D Expression",
"fade_in":500,
"fade_out":500,
"params":[
{"id":"PARAM_EYE_L_OPEN","val":0.87,"calc":"mult"},
{"id":"PARAM_EYE_L_SMILE","val":1},
{"id":"PARAM_EYE_R_OPEN","val":0.87,"calc":"mult"},
{"id":"PARAM_BROW_L_Y","val":-1},
{"id":"PARAM_BROW_R_Y","val":-1},
{"id":"PARAM_BROW_L_ANGLE","val":1},
{"id":"PARAM_BROW_R_ANGLE","val":1},
{"id":"PARAM_BROW_L_FORM","val":-0.51},
{"id":"PARAM_BROW_R_FORM","val":-0.51},
{"id":"PARAM_MOUTH_FORM","val":-1,"def":1}
]
}
16 changes: 16 additions & 0 deletions Resources/v2/Epsilon/expressions/f04.exp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"type":"Live2D Expression",
"fade_in":500,
"fade_out":500,
"params":[
{"id":"PARAM_BROW_L_Y","val":-0.59},
{"id":"PARAM_BROW_R_Y","val":-0.59},
{"id":"PARAM_BROW_L_X","val":-1},
{"id":"PARAM_BROW_R_X","val":-1},
{"id":"PARAM_BROW_L_ANGLE","val":-1},
{"id":"PARAM_BROW_R_ANGLE","val":-1},
{"id":"PARAM_BROW_L_FORM","val":-1},
{"id":"PARAM_BROW_R_FORM","val":-1},
{"id":"PARAM_MOUTH_FORM","val":-1,"def":1}
]
}
13 changes: 13 additions & 0 deletions Resources/v2/Epsilon/expressions/f05.exp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"type":"Live2D Expression",
"fade_in":500,
"fade_out":500,
"params":[
{"id":"PARAM_BROW_L_Y","val":-0.42},
{"id":"PARAM_BROW_R_Y","val":-0.44},
{"id":"PARAM_BROW_L_X","val":-0.07},
{"id":"PARAM_BROW_L_ANGLE","val":1},
{"id":"PARAM_BROW_R_ANGLE","val":1},
{"id":"PARAM_TERE","val":1}
]
}
15 changes: 15 additions & 0 deletions Resources/v2/Epsilon/expressions/f06.exp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"type":"Live2D Expression",
"fade_in":500,
"fade_out":500,
"params":[
{"id":"PARAM_BROW_L_Y","val":-1},
{"id":"PARAM_BROW_R_Y","val":-1},
{"id":"PARAM_BROW_L_X","val":-0.07},
{"id":"PARAM_BROW_L_ANGLE","val":0.73},
{"id":"PARAM_BROW_R_ANGLE","val":0.71},
{"id":"PARAM_BROW_L_FORM","val":-0.81},
{"id":"PARAM_BROW_R_FORM","val":-0.81},
{"id":"PARAM_MOUTH_FORM","val":-1,"def":1}
]
}
Loading

0 comments on commit 46f6680

Please sign in to comment.