前提
・Blenderのバージョン:3.2.0
・FBX:骨・モーション・スキニングされたメッシュ
・モーション(BVH):FBXと同じ名前・構造の骨データが入っている
コード
replace_motion.py
import bpy
fbx_path = "FBXへのパス"
bvh_path = "BVHへのパス"
output_path = "適用結果(FBX)へのパス"
# FBXをインポート
bpy.ops.import_scene.fbx(filepath=fbx_path)
bpy.ops.object.select_all(action='DESELECT')
# 元のArmatureデータを削除する(メッシュだけ残す)
bpy.ops.object.select_by_type(extend=False, type='ARMATURE')
bpy.ops.object.delete()
# BVHをインポート
bpy.ops.import_anim.bvh(filepath=bvh_path)
# メッシュのModifierにBVHのモーションを適用する
bpy.context.view_layer.objects.active = bpy.data.objects['mesh_object']
## (補足)スキニングがおかしくなる場合はメッシュを回転させてモーションに合わせる
## import math
## bpy.context.object.rotation_euler = ( math.radians(90.0), 0, 0 )
bpy.context.object.modifiers["Armature"].object = bpy.data.objects["bvh_object"]
# FBXにエクスポート
bpy.ops.export_scene.fbx(filepath=output_path, bake_anim_use_all_bones=False, bake_anim_use_nla_strips=False, bake_anim_use_all_actions=False)