You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Qt 3D C++动画技术求助:Blender导入动画无法处理

我太懂这种找不到Qt 3D C动画示例的憋屈了——官方资源和社区内容确实偏QML多一些,尤其是从Blender导入动画后的C处理流程,很容易卡壳。我来给你捋清楚怎么用QAnimationClipLoader和QClipAnimator搞定这个立方体平移动画,附完整代码和踩坑提示。

Qt 3D C++ 导入Blender动画并播放的完整解决方案

1. 先把Blender导出环节做对

首先要确保你用Blender的Qt3D插件导出的是glTF 2.0格式(这是Qt 3D对动画支持最好的格式之一)。导出时一定要勾选「动画」选项,保证平移数据被打包进文件里——别问我怎么知道的,当初漏勾这个坑了半小时。

2. 核心C++代码实现

下面是完整的可运行代码,我会逐部分标注关键细节:

初始化Qt 3D基础场景

先搭好窗口、根实体、相机这些必备框架:

#include <QGuiApplication>
#include <Qt3DExtras/Qt3DWindow>
#include <Qt3DCore/QEntity>
#include <Qt3DCore/QTransform>
#include <Qt3DRender/QCamera>
#include <Qt3DExtras/QOrbitCameraController>
#include <Qt3DExtras/QPhongMaterial>
#include <Qt3DAnimation/QAnimationClipLoader>
#include <Qt3DAnimation/QClipAnimator>
#include <Qt3DAnimation/QChannelMapper>
#include <Qt3DAnimation/QVector3DAnimationChannel>
#include <Qt3DRender/QMesh>

int main(int argc, char *argv[]) {
    QGuiApplication app(argc, argv);

    // 创建Qt3D窗口
    Qt3DExtras::Qt3DWindow view;
    view.defaultFrameGraph()->setClearColor(QColor(QRgb(0x4d4d4f)));

    // 根实体,所有场景元素的父节点
    Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity();

    // 配置相机和控制器
    Qt3DRender::QCamera *camera = view.camera();
    camera->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f);
    camera->setPosition(QVector3D(0, 0, 10.0f));
    camera->setViewCenter(QVector3D(0, 0, 0));

    Qt3DExtras::QOrbitCameraController *cameraController = new Qt3DExtras::QOrbitCameraController(rootEntity);
    cameraController->setCamera(camera);

加载带动画的立方体模型

把Blender导出的glTF模型加载进来,同时创建立方体的实体、材质和变换组件:

// 加载带动画的立方体glTF文件
    Qt3DRender::QMesh *cubeMesh = new Qt3DRender::QMesh();
    cubeMesh->setSource(QUrl::fromLocalFile("你的立方体文件路径.gltf"));

    // 创建立方体实体
    Qt3DCore::QEntity *cubeEntity = new Qt3DCore::QEntity(rootEntity);
    Qt3DExtras::QPhongMaterial *material = new Qt3DExtras::QPhongMaterial();
    material->setDiffuse(QColor(QRgb(0xbeb32b))); // 给个黄色,方便看
    Qt3DCore::QTransform *cubeTransform = new Qt3DCore::QTransform();

    cubeEntity->addComponent(cubeMesh);
    cubeEntity->addComponent(material);
    cubeEntity->addComponent(cubeTransform);

关键:绑定动画通道与变换组件

这一步是让动画“生效”的核心,要把动画剪辑里的平移数据和立方体的QTransform组件关联起来:

// 加载动画剪辑(直接用同一个glTF文件就行,里面包含动画数据)
    Qt3DAnimation::QAnimationClipLoader *clipLoader = new Qt3DAnimation::QAnimationClipLoader();
    clipLoader->setSource(QUrl::fromLocalFile("你的立方体文件路径.gltf"));

    // 创建通道映射器:把动画数据映射到实体的属性上
    Qt3DAnimation::QChannelMapper *channelMapper = new Qt3DAnimation::QChannelMapper();

    // 平移动画对应Transform的translation属性
    Qt3DAnimation::QVector3DAnimationChannel *translationChannel = new Qt3DAnimation::QVector3DAnimationChannel();
    translationChannel->setName("translation"); // 这个名称要和glTF里的动画通道名完全一致,Blender导出的平移通道默认是这个
    translationChannel->setTarget(cubeTransform);
    translationChannel->setPropertyName("translation"); // 对应QTransform的translation属性

    channelMapper->addChannel(translationChannel);

    // 创建动画器,启动循环播放
    Qt3DAnimation::QClipAnimator *animator = new Qt3DAnimation::QClipAnimator(cubeEntity);
    animator->setClip(clipLoader);
    animator->setChannelMapper(channelMapper);
    animator->setLoops(Qt3DAnimation::QAbstractAnimation::Infinite); // 无限循环播放
    animator->start(); // 启动动画

收尾:显示窗口

把根实体绑定到窗口,启动应用:

view.setRootEntity(rootEntity);
    view.show();

    return app.exec();
}

3. 踩坑排查指南

  • 动画没反应? 先检查glTF文件是否包含动画数据(可以用glTF在线预览工具确认);再核对QVector3DAnimationChannel的setName参数和glTF里的通道名是否一致。
  • 头文件找不到? 确保项目.pro文件里加了这些模块:
    QT += 3dcore 3drender 3dinput 3dextras 3danimation
    
  • 路径报错? 先试试绝对路径,确认文件能加载后再换成相对路径。

内容的提问来源于stack exchange,提问作者Lawliet Darujin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:21:01