• 问题反馈可发送邮件到stubbornhuang@qq.com

  • 在本站开通年度VIP,无限制下载本站资源和阅读本站文章

  • 欢迎大家交换友链,可在https://www.stubbornhuang.com/申请友情链接进行友链交换申请!

  • 计算机图形学与计算几何经典必备书单整理,下载链接可参考:https://www.stubbornhuang.com/1256/

  • 本站会放置Google广告用于维持域名以及网站服务器费用。

  • 如果觉得本站的内容有帮助,可以考虑打赏博主哦!

  • 本站由于前段时间遭受到大量临时和国外邮箱注册,所以对可注册的邮箱类型进行了限制!

  • 感谢大家访问本站,希望本站的内容可以帮助到大家!

  • 工资「喂饱肚子」,副业「养活灵魂」!

Python – 使用python将fbx中的动作信息转换为bvh动作文件

Blender 发布于2024-07-29 阅读 2,586次 0次评论 0次点赞 本文共2263个字,阅读需要6分钟。

1 安装blender的py包bpy

bpy全称Blender Python API,是blender使用python与系统执行数据交换和功能调用的接口模块。

先使用conda创建虚拟环境,blender的py包只支持python3.7

conda create -n fbx2bvh python=3.7
pip install bpy=2.82.1
bpy_post_install

按照顺序依次执行上述命令即可完成blender python包的安装。

另外还需要numpy,继续执行以下命令安装numpy

pip install numpy

2 将fbx转换为bvh

使用github的python脚本,脚本内容如下,

import bpy
import numpy as np
from os import listdir, path

def fbx2bvh(data_path, file):
    sourcepath = data_path+"/"+file
    bvh_path = data_path+"/"+file.split(".fbx")[0]+".bvh"

    bpy.ops.import_scene.fbx(filepath=sourcepath)

    frame_start = 9999
    frame_end = -9999
    action = bpy.data.actions[-1]
    if  action.frame_range[1] > frame_end:
      frame_end = action.frame_range[1]
    if action.frame_range[0] < frame_start:
      frame_start = action.frame_range[0]

    frame_end = np.max([60, frame_end])
    bpy.ops.export_anim.bvh(filepath=bvh_path,
                            frame_start=frame_start,
                            frame_end=frame_end, root_transform_only=True)
    bpy.data.actions.remove(bpy.data.actions[-1])
    print(data_path+"/"+file+" processed.")

if __name__ == '__main__':
    data_path = "./fbx/"
    directories = sorted([f for f in listdir(data_path) if not f.startswith(".")])
    for d in directories:
      files = sorted([f for f in listdir(data_path+d) if f.endswith(".fbx")])
      for file in files:
          fbx2bvh(path.join(data_path,d), file)

将需要转换的fbx文件放在与该脚本同级的fbx目录下,然后直接运行该脚本,转换之后的bvh文件保存在fbx文件夹下。

或者使用以下命令运行,

blender -b -P fbx2bvh.py

如果待转换的fbx文件没有动作信息而只有静态初始骨架,则需要将上述脚本修改为

import bpy
import numpy as np
from os import listdir, path

def fbx2bvh(data_path, file):
    sourcepath = data_path+"/"+file
    bvh_path = data_path+"/"+file.split(".fbx")[0]+".bvh"

    bpy.ops.import_scene.fbx(filepath=sourcepath)

    frame_start = 9999
    frame_end = -9999
    action = bpy.data.actions[-1]
    if  action.frame_range[1] > frame_end:
      frame_end = int(action.frame_range[1])
    if action.frame_range[0] < frame_start:
      frame_start = int(action.frame_range[0])

    frame_end = np.max([60, frame_end])
    bpy.ops.export_anim.bvh(filepath=bvh_path,
                            frame_start=frame_start,
                            frame_end=frame_end, root_transform_only=True)
    bpy.data.actions.remove(bpy.data.actions[-1])
    print(data_path+"/"+file+" processed.")

if __name__ == '__main__':
    data_path = "./fbx/"
    directories = sorted([f for f in listdir(data_path) if not f.startswith(".")])
    for d in directories:
      files = sorted([f for f in listdir(data_path+d) if f.endswith(".fbx")])
      for file in files:
          fbx2bvh(path.join(data_path,d), file)

不然会出现错误,因为在导出bvh的时候起始帧和终止帧应为整数,所以将frame_startframe_end转换为int强制转为int即可。

参考

欢迎扫码关注我的微信公众号,及时获取文章更新

微信公众号二维码

本文作者:StubbornHuang

版权声明:本文为站长原创文章,如果转载请注明原文链接!

原文标题:Python – 使用python将fbx中的动作信息转换为bvh动作文件

原文链接:https://www.stubbornhuang.com/3059/

发布于:2024年07月29日 15:06:02

修改于:2024年07月29日 15:06:02

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

文章末尾
上一篇
Blender - 将bvh动作重定向到人物模型,驱动人物进行运动
Blender
下一篇
C++ - nlohmann json序列化输出字符串时指定输出浮点数小数点后几位
C++
当前分类随机文章推荐

发表评论

您必须 [ 登录 ] 才能发表留言!

关注我们的公众号

微信公众号