Repository navigation
创建mujoco机器人模型 #3907
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
创建mujoco机器人模型 #3907
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
800d345
完成选题并提交
oyzw2302 d9489bc
Merge branch 'OpenHUTB:main' into main
oyzw2302 f84c87e
Merge branch 'OpenHUTB:main' into main
oyzw2302 9fdc609
Merge branch 'OpenHUTB:main' into main
oyzw2302 d24743a
Merge branch 'OpenHUTB:main' into main
oyzw2302 61e9be7
PyCharm 新建 Flask 项目(虚拟环境隔离依赖)
oyzw2302 8e33227
Merge remote-tracking branch 'origin/main'
oyzw2302 fa3bfc4
具身人 3D 模型获取与本地可视化测试
oyzw2302 2ff444c
Merge branch 'OpenHUTB:main' into main
oyzw2302 f6826e6
更改了README.md,描述更加详细
oyzw2302 888bae1
Merge remote-tracking branch 'origin/main'
oyzw2302 cbb14ae
Merge branch 'OpenHUTB:main' into main
oyzw2302 458a4e3
创建机器人模型
oyzw2302 a42576b
创建机器人模型
oyzw2302 d5e0bbf
Merge remote-tracking branch 'origin/main'
oyzw2302 2a52495
创建机器人模型
oyzw2302 a6b6918
创建mujoco机器人模型
oyzw2302 277c6b3
创建mujoco机器人模型
oyzw2302 9b84174
创建mujoco机器人模型
oyzw2302 d4fb601
创建mujoco机器人模型
oyzw2302 3bacbda
创建mujoco机器人模型
oyzw2302 549b298
创建mujoco机器人模型
oyzw2302 528cdfa
创建mujoco机器人模型
oyzw2302 3f205a6
创建mujoco机器人模型
oyzw2302 2ed4ff6
创建mujoco机器人模型
oyzw2302 4fc6dce
创建mujoco机器人模型
oyzw2302 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
10 changes: 0 additions & 10 deletions
10
src/embodied_human/humanoid_flask_3d/ templates/index.html
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 文件名包含空格 |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| # 具身人3D可视化与动作交互网页开发 | ||
| 基于Python+Flask+PyVista的3D可视化系统,支持模型视角控制、颜色切换、基础动作交互,通过GitHub Pages实现公开访问。 | ||
|
|
||
| ## 项目概述 | ||
| 本项目以Flask为Web框架,结合PyVista/Trimesh实现具身人3D模型的Web化渲染,完成基础交互(视角旋转/缩放、颜色切换)与进阶动作控制(抬手、抬腿等预设动作),并通过GitHub完成代码管理与静态网页部署,最终实现可公开访问的具身人3D交互网页。 | ||
|
|
||
| ## 环境准备 | ||
| ### 开发工具 | ||
| - PyCharm(社区版):项目管理、编码与调试 | ||
| - GitHub:代码托管与Pages部署 | ||
| - Chrome/Firefox:Web功能测试 | ||
|
|
||
| ### 依赖库安装 | ||
| ```bash | ||
| # 虚拟环境下执行 | ||
| pip install flask pyvista trimesh numpy | ||
| ``` | ||
| | 依赖库 | 功能说明 | | ||
| |-------------|------------------------------| | ||
| | flask | Web框架,实现路由与页面渲染 | | ||
| | pyvista | 3D可视化核心,支持模型Web适配 | | ||
| | trimesh | 模型关节控制,处理带骨骼模型 | | ||
| | numpy | 数值计算,实现关节角度转换 | | ||
|
|
||
| ### 模型准备 | ||
| 下载带骨骼绑定的具身人3D模型(GLB/FBX格式),推荐来源: | ||
| - Sketchfab官网搜索「Free Rigged Humanoid GLB」 | ||
| - 筛选条件:Downloadable + Low Poly(<50MB) | ||
| - 模型放置路径:项目根目录(命名为`humanoid_rigged.glb`) | ||
|
|
||
| ## 项目结构 | ||
| | 文件名/目录 | 功能描述 | | ||
| |--------------------------|--------------------------------------------------------------| | ||
| | `app.py` | Flask核心入口,实现首页路由与Web服务启动 | | ||
| | `test_model.py` | 本地模型加载与关节控制测试脚本 | | ||
| | `generate_web_model.py` | 将本地3D模型转换为Web可加载的JS文件 | | ||
| | `generate_animations.py` | 生成预设动作(初始/抬手/抬腿)的Web模型文件 | | ||
| | `build_static.py` | 将动态项目转换为静态网页文件,适配GitHub Pages部署 | | ||
| | `templates/index.html` | 网页模板,包含UI布局、交互按钮、3D渲染容器 | | ||
| | `static/js/3d_render.js` | 3D渲染核心脚本,实现模型加载、颜色切换、动作交互 | | ||
| | `static/js/*.js` | 自动生成的模型Web文件(基础模型+动作模型) | | ||
| | `static_site/` | 静态网页输出目录,用于GitHub Pages部署 | | ||
| | `README.md` | 项目说明文档 | | ||
|
|
||
| ## 核心功能 | ||
| ### 1. 基础3D可视化(Web端) | ||
| - 模型渲染:加载带关节的具身人3D模型,支持Web端流畅显示 | ||
| - 视角交互:鼠标拖拽旋转模型、滚轮缩放视角 | ||
| - 颜色切换:一键切换模型颜色(蓝色/绿色/粉色) | ||
|
|
||
| ### 2. 动作交互控制 | ||
| - 预设动作:支持初始姿态、抬手、抬腿3种基础动作切换 | ||
| - 关节控制:基于Trimesh修改模型关节角度,实现精准动作控制 | ||
| - 动作适配:所有动作模型预生成Web文件,保证前端加载效率 | ||
|
|
||
| ### 3. 工程化部署 | ||
| - 代码管理:通过GitHub实现版本控制,支持代码同步与回溯 | ||
| - 静态部署:生成纯静态网页文件,通过GitHub Pages实现公开访问 | ||
|
|
||
| ## 使用方法 | ||
| ### 本地开发与测试 | ||
| #### 步骤1:环境与模型验证 | ||
| ```bash | ||
| # 测试本地模型加载 | ||
| python test_model.py | ||
| ``` | ||
| 验证效果:弹出3D窗口显示模型,终端输出关节列表。 | ||
|
|
||
| #### 步骤2:生成Web模型文件 | ||
| ```bash | ||
| # 生成基础模型Web文件 | ||
| python generate_web_model.py | ||
| # 生成动作模型Web文件 | ||
| python generate_animations.py | ||
| ``` | ||
| 生成结果:`static/js/`目录下新增`humanoid_base.js`、`anim_*.js`文件。 | ||
|
|
||
| #### 步骤3:启动本地Web服务 | ||
| ```bash | ||
| python app.py | ||
| ``` | ||
| 访问地址:`http://127.0.0.1:5000` | ||
| 验证功能: | ||
| - 模型正常渲染,支持视角交互 | ||
| - 颜色按钮切换模型颜色 | ||
| - 动作按钮切换初始/抬手/抬腿姿态 | ||
|
|
||
| ### GitHub部署(公开访问) | ||
| #### 步骤1:生成静态文件 | ||
| ```bash | ||
| python build_static.py | ||
| ``` | ||
| 生成结果:项目根目录新增`static_site/`,包含完整静态网页文件。 | ||
|
|
||
| #### 步骤2:GitHub Pages部署 | ||
| 1. 打开项目GitHub仓库 → Settings → Pages | ||
| 2. 选择「Upload your files」,上传`static_site/`内所有文件 | ||
| 3. 等待5-10分钟,访问仓库Pages链接即可公开访问 | ||
|
|
||
| ## 交互操作指南 | ||
| | 操作类型 | 操作方式 | 效果说明 | | ||
| |----------------|-----------------------------------|------------------------------| | ||
| | 视角控制 | 鼠标拖拽模型区域 | 360°旋转模型视角 | | ||
| | 缩放控制 | 滚轮滚动模型区域 | 放大/缩小模型显示比例 | | ||
| | 颜色切换 | 点击「蓝色/绿色/粉色」按钮 | 实时切换模型主体颜色 | | ||
| | 动作切换 | 点击「初始姿态/抬手/抬腿」按钮 | 切换模型至对应预设动作姿态 | | ||
|
|
||
| ## 常见问题与解决方案 | ||
| | 问题现象 | 原因分析 | 解决方法 | | ||
| |------------------------|-----------------------------------|---------------------------------------------------------------| | ||
| | 模型加载失败 | 模型格式错误/路径错误 | 确认模型为GLB/FBX带关节格式,检查代码中模型文件名是否匹配 | | ||
| | 动作切换无效果 | 关节名称不匹配 | 运行`test_model.py`查看实际关节名,修改`generate_animations.py`中关节配置 | | ||
| | GitHub Pages访问异常 | 文件路径错误/部署未完成 | 检查`static_site/`文件完整性,等待部署完成后清除浏览器缓存重试 | | ||
| | Web渲染卡顿 | 模型面数过多 | 更换Low Poly模型,或用Blender简化模型面数 | | ||
|
|
||
|
|
||
| ## 参考资料 | ||
| - [Flask官方文档](https://flask.palletsprojects.com/) | ||
| - [PyVista Web可视化文档](https://docs.pyvista.org/guide/web/index.html) | ||
| - [Trimesh模型操作文档](https://trimsh.org/) | ||
| - [GitHub Pages部署指南](https://docs.github.com/zh/pages) | ||
| - [Sketchfab免费3D模型资源](https://sketchfab.com/) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| from flask import Flask, render_template | ||
|
|
||
| app = Flask(__name__) | ||
|
|
||
| @app.route('/') | ||
| def index(): | ||
| return render_template('index.html') | ||
|
|
||
| if __name__ == '__main__': | ||
| app.run(debug=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| import pyvista as pv | ||
| import numpy as np | ||
| import vtk | ||
|
|
||
| # 生成更像人形的仿真机器人模型(优化比例+结构) | ||
| def create_robot(return_parts=False): | ||
| # 1. 躯干(更修长,接近人形) | ||
| torso = pv.Cube(center=(0, 0, 0.8), x_length=0.4, y_length=0.3, z_length=1.2) | ||
| torso["part_id"] = np.zeros(torso.n_points, dtype=int) # 躯干ID=0 | ||
|
|
||
| # 2. 头部(新增,更像人形) | ||
| head = pv.Cube(center=(0, 0, 1.6), x_length=0.3, y_length=0.3, z_length=0.3) | ||
| head["part_id"] = np.full(head.n_points, 5, dtype=int) # 头部ID=5 | ||
|
|
||
| # 3. 右胳膊(上臂+前臂,比例优化) | ||
| right_upper_arm = pv.Cube(center=(0.5, 0, 1.0), x_length=0.2, y_length=0.2, z_length=0.5) | ||
| right_upper_arm["part_id"] = np.ones(right_upper_arm.n_points, dtype=int) # 右上臂ID=1 | ||
| right_forearm = pv.Cube(center=(0.9, 0, 1.0), x_length=0.2, y_length=0.2, z_length=0.5) | ||
| right_forearm["part_id"] = np.full(right_forearm.n_points, 2, dtype=int) # 右前臂ID=2 | ||
|
|
||
| # 4. 左胳膊(新增,对称结构) | ||
| left_upper_arm = pv.Cube(center=(-0.5, 0, 1.0), x_length=0.2, y_length=0.2, z_length=0.5) | ||
| left_upper_arm["part_id"] = np.full(left_upper_arm.n_points, 6, dtype=int) # 左上臂ID=6 | ||
| left_forearm = pv.Cube(center=(-0.9, 0, 1.0), x_length=0.2, y_length=0.2, z_length=0.5) | ||
| left_forearm["part_id"] = np.full(left_forearm.n_points, 7, dtype=int) # 左前臂ID=7 | ||
|
|
||
| # 5. 右大腿+小腿(比例优化) | ||
| right_thigh = pv.Cube(center=(0.2, 0, -0.2), x_length=0.2, y_length=0.2, z_length=0.6) | ||
| right_thigh["part_id"] = np.full(right_thigh.n_points, 8, dtype=int) # 右大腿ID=8 | ||
| right_calf = pv.Cube(center=(0.2, 0, -0.8), x_length=0.2, y_length=0.2, z_length=0.6) | ||
| right_calf["part_id"] = np.full(right_calf.n_points, 9, dtype=int) # 右小腿ID=9 | ||
|
|
||
| # 6. 左大腿+小腿(比例优化) | ||
| left_thigh = pv.Cube(center=(-0.2, 0, -0.2), x_length=0.2, y_length=0.2, z_length=0.6) | ||
| left_thigh["part_id"] = np.full(left_thigh.n_points, 3, dtype=int) # 左大腿ID=3 | ||
| left_calf = pv.Cube(center=(-0.2, 0, -0.8), x_length=0.2, y_length=0.2, z_length=0.6) | ||
| left_calf["part_id"] = np.full(left_calf.n_points, 4, dtype=int) # 左小腿ID=4 | ||
|
|
||
| # 用MultiBlock合并所有部件 | ||
| block = pv.MultiBlock() | ||
| block.append(torso) | ||
| block.append(head) | ||
| block.append(right_upper_arm) | ||
| block.append(right_forearm) | ||
| block.append(left_upper_arm) | ||
| block.append(left_forearm) | ||
| block.append(right_thigh) | ||
| block.append(right_calf) | ||
| block.append(left_thigh) | ||
| block.append(left_calf) | ||
|
|
||
| # 转换为单个UnstructuredGrid | ||
| robot = block.combine() | ||
|
|
||
| # 添加关节标记(右肘、左膝) | ||
| robot.point_data["joints"] = np.zeros(robot.n_points, dtype=int) | ||
| # 右肘关节(右上臂+右前臂连接点) | ||
| elbow_idx = robot.find_closest_point((0.7, 0, 1.0)) | ||
| robot.point_data["joints"][elbow_idx] = 1 | ||
| # 左膝关节(左大腿+左小腿连接点) | ||
| knee_idx = robot.find_closest_point((-0.2, 0, -0.5)) | ||
| robot.point_data["joints"][knee_idx] = 1 | ||
|
|
||
| if return_parts: | ||
| return robot, [torso, head, right_upper_arm, right_forearm, left_upper_arm, left_forearm, right_thigh, | ||
| right_calf, left_thigh, left_calf] | ||
| return robot | ||
|
|
||
|
|
||
| # 关节旋转函数(保持逻辑,适配新部件ID) | ||
| def rotate_joint(robot, joint_name, angle_deg): | ||
| joint_config = { | ||
| "right_elbow": { | ||
| "center": (0.7, 0, 1.0), # 右肘关节中心点 | ||
| "target_part": 2 # 右前臂ID=2 | ||
| }, | ||
| "left_knee": { | ||
| "center": (-0.2, 0, -0.5), # 左膝关节中心点 | ||
| "target_part": 4 # 左小腿ID=4 | ||
| } | ||
| } | ||
|
|
||
| if joint_name not in joint_config: | ||
| print(f"错误:关节名称{joint_name}不存在!") | ||
| return robot | ||
|
|
||
| config = joint_config[joint_name] | ||
| part_mask = robot["part_id"] == config["target_part"] | ||
| if not np.any(part_mask): | ||
| print(f"错误:未找到part_id={config['target_part']}的部件!") | ||
| return robot | ||
|
|
||
| # 旋转逻辑(与之前一致) | ||
| target_points = robot.points[part_mask].copy() | ||
| vtk_points = vtk.vtkPoints() | ||
| for point in target_points: | ||
| vtk_points.InsertNextPoint(point) | ||
|
|
||
| transform = vtk.vtkTransform() | ||
| transform.Translate(config["center"][0], config["center"][1], config["center"][2]) | ||
| transform.RotateY(angle_deg) | ||
| transform.Translate(-config["center"][0], -config["center"][1], -config["center"][2]) | ||
|
|
||
| transform_filter = vtk.vtkTransformFilter() | ||
| transform_filter.SetTransform(transform) | ||
| poly_data = vtk.vtkPolyData() | ||
| poly_data.SetPoints(vtk_points) | ||
| transform_filter.SetInputData(poly_data) | ||
| transform_filter.Update() | ||
|
|
||
| rotated_vtk_points = transform_filter.GetOutput().GetPoints() | ||
| rotated_points = np.array([rotated_vtk_points.GetPoint(i) for i in range(rotated_vtk_points.GetNumberOfPoints())]) | ||
| robot.points[part_mask] = rotated_points | ||
| return robot | ||
|
|
||
|
|
||
| # 本地测试 | ||
| if __name__ == "__main__": | ||
| robot_initial = create_robot() | ||
| print(f"初始模型:{robot_initial.n_points}个点,{robot_initial.n_cells}个面") | ||
|
|
||
| # 测试右肘+左膝旋转 | ||
| robot_rotated = rotate_joint(robot_initial.copy(), "right_elbow", 90) | ||
| robot_rotated = rotate_joint(robot_rotated, "left_knee", 60) | ||
|
|
||
| # 渲染对比 | ||
| plotter = pv.Plotter(shape=(1, 2), window_size=(1200, 600)) | ||
| # 左窗口:初始姿态 | ||
| plotter.subplot(0, 0) | ||
| plotter.add_mesh( | ||
| robot_initial, | ||
| scalars="part_id", | ||
| cmap="tab10", # 更多颜色区分部件 | ||
| edge_color="black", | ||
| show_edges=True, | ||
| scalar_bar_args={"title": "部件ID"} | ||
| ) | ||
| plotter.add_axes() | ||
| grid = pv.Plane(center=(0, 0, -1.2), i_size=3, j_size=3, i_resolution=10, j_resolution=10) | ||
| plotter.add_mesh(grid, color="lightgray", opacity=0.5) | ||
| plotter.add_text("初始人形机器人", font_size=14, position="upper_left") | ||
|
|
||
| # 右窗口:旋转后姿态 | ||
| plotter.subplot(0, 1) | ||
| plotter.add_mesh( | ||
| robot_rotated, | ||
| scalars="part_id", | ||
| cmap="tab10", | ||
| edge_color="black", | ||
| show_edges=True, | ||
| scalar_bar_args={"title": "部件ID"} | ||
| ) | ||
| plotter.add_axes() | ||
| plotter.add_mesh(grid, color="lightgray", opacity=0.5) | ||
| plotter.add_text("抬臂+屈膝动作", font_size=14, position="upper_left") | ||
|
|
||
| plotter.show() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
删除的文件和“创建mujoco机器人模型”无关