Blender 動態捕捉
概述
使用 Python 在 Blender 中匯入、處理和重新定位動作捕捉資料。處理 BVH/FBX 動作捕捉檔案、從影片素材中追蹤相機和物體運動、解算 3D 相機路徑,以及清理動畫資料——所有操作均可從終端機編寫腳本。
說明
1. 匯入 BVH 動作捕捉檔案
import bpy
bpy.ops.import_anim.bvh(
filepath="/path/to/mocap.bvh",
target='ARMATURE',
global_scale=1.0,
frame_start=1,
use_fps_scale=False,
rotate_mode='NATIVE',
axis_forward='-Z', axis_up='Y'
)
armature = bpy.context.active_object
action = armature.animation_data.action
print(f"已匯入: {armature.name}, 骨骼數: {len(armature.data.bones)}, 影格數: {action.frame_range}")
2. 匯入帶有動畫的 FBX
bpy.ops.import_scene.fbx(
filepath="/path/to/mocap.fbx",
use_anim=True,
ignore_leaf_bones=True,
automatic_bone_orientation=True,
primary_bone_axis='Y', secondary_bone_axis='X'
)
3. 在不同骨骼之間重新定位動作
from mathutils import Matrix
def retarget_motion(source_armature, target_armature, bone_mapping):
"""使用骨骼名稱對應重新定位動畫:{目標骨骼: 來源骨骼}"""
source_action = source_armature.animation_data.action
frame_start, frame_end = int(source_action.frame_range[0]), int(source_action.frame_range[1])
if not target_armature.animation_data:
target_armature.animation_data_create()
new_action = bpy.data.actions.new(f"{source_action.name}_retarget")
target_armature.animation_data.action = new_action
for frame in range(frame_start, frame_end + 1):
bpy.context.scene.frame_set(frame)
for tgt_name, src_name in bone_mapping.items():
src = source_armature.pose.bones.get(src_name)
tgt = target_armature.pose.bones.get(tgt_name)
if not src or not tgt:
continue
tgt.rotation_quaternion = src.rotation_quaternion
tgt.keyframe_insert(data_path="rotation_quaternion", frame=frame)
# 僅為根部骨骼複製位置
if src_name == list(bone_mapping.values())[0]:
tgt.location = src.location
tgt.keyframe_insert(data_path="location", frame=frame)
# 範例 Mixamo → Rigify 對應
mapping = {
"spine": "mixamorig:Hips", "spine.001": "mixamorig:Spine",
"spine.004": "mixamorig:Neck", "spine.006": "mixamorig:Head",
"upper_arm.L": "mixamorig:LeftArm", "forearm.L": "mixamorig:LeftForeArm",
"upper_arm.R": "mixamorig:RightArm", "forearm.R": "mixamorig:RightForeArm",
"thigh.L": "mixamorig:LeftUpLeg", "shin.L": "mixamorig:LeftLeg",
"thigh.R": "mixamorig:RightUpLeg", "shin.R": "mixamorig:RightLeg",
}
4. 清理動作捕捉資料
def decimate_fcurve(fcurve, factor=0.5):
"""移除關鍵影格以減少資料量,同時保持形狀。"""
points = fcurve.keyframe_points
total = len(points)
keep_every = max(1, int(1.0 / factor))
remove_indices = [i for i in range(total) if i % keep_every != 0 and i != 0 and i != total - 1]
for i in reversed(remove_indices):
points.remove(points[i])
armature = bpy.context.active_object
action = armature.animation_data.action
for fcurve in action.fcurves:
decimate_fcurve(fcurve, factor=0.5)
fcurve.update()
5. 影片運動追蹤與相機解算
# 載入素材
clip = bpy.data.movieclips.load("/path/to/footage.mp4")
scene = bpy.context.scene
scene.active_clip = clip
# 設定追蹤
tracking = clip.tracking
settings = tracking.settings
settings.default_pattern_size = 21
settings.default_search_size = 71
settings.default_motion_model = 'AFFINE'
# 相機設定用於解算
camera = tracking.camera
camera.sensor_width = 36.0
camera.focal_length = 50.0
# 解算相機運動
bpy.ops.clip.solve_camera()
solve_error = tracking.reconstruction.average_error
print(f"解算誤差: {solve_error:.4f} px ({'良好' if solve_error < 0.5 else '需要調整'})")
# 根據解算資料設定場景
bpy.ops.clip.setup_tracking_scene()
6. 將追蹤運動應用於物體
obj = bpy.data.objects["MyObject"]
constraint = obj.constraints.new(type='FOLLOW_TRACK')
constraint.clip = clip
constraint.track = tracking.tracks["Marker_01"]
constraint.use_3d_position = True
constraint.camera = scene.camera
# 將約束烘焙成關鍵影格
bpy.context.view_layer.objects.active = obj
obj.select_set(True)
bpy.ops.nla.bake(
frame_start=1, frame_end=clip.frame_duration,
only_selected=True, visual_keying=True,
clear_constraints=True, bake_types={'OBJECT'}
)
7. 匯出動畫資料
# 匯出為 BVH
bpy.ops.export_anim.bvh(
filepath="/tmp/output_mocap.bvh",
frame_start=int(action.frame_range[0]),
frame_end=int(action.frame_range[1]),
rotate_mode='NATIVE'
)
# 匯出為帶有烘焙動畫的 FBX
bpy.ops.export_scene.fbx(
filepath="/tmp/output_anim.fbx",
use_selection=True, bake_anim=True,
bake_anim_use_all_bones=True, add_leaf_bones=False
)
範例
範例 1:批次掃描動作捕捉庫
使用者請求:「從資料夾匯入所有 BVH 檔案,列出骨骼數量和影格範圍」
import bpy, glob, os
for filepath in sorted(glob.glob("/path/to/mocap_library/*.bvh")):
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()
bpy.ops.import_anim.bvh(filepath=filepath, target='ARMATURE', global_scale=0.01, frame_start=1)
arm = bpy.context.active_object
if arm and arm.animation_data:
action = arm.animation_data.action
duration = (action.frame_range[1] - action.frame_range[0]) / bpy.context.scene.render.fps
print(f"{os.path.basename(filepath)}: {len(arm.data.bones)} 個骨骼, {duration:.1f}秒")
執行:blender --background --python scan_mocap.py
範例 2:將動作捕捉應用於角色並渲染
使用者請求:「匯入一個 BVH 檔案,將其套用到我有綁定的角色上,然後渲染預覽」
import bpy
bpy.ops.wm.open_mainfile(filepath="/path/to/character.blend")
char_armature = bpy.data.objects["Armature"]
bpy.ops.import_anim.bvh(filepath="/path/to/walk_cycle.bvh", target='ARMATURE', global_scale=0.01)
mocap_armature = bpy.context.active_object
mocap_action = mocap_armature.animation_data.action
# 傳輸動作(骨骼名稱匹配時有效)
if not char_armature.animation_data:
char_armature.animation_data_create()
char_armature.animation_data.action = mocap_action
# 移除暫時骨骼、設定影格範圍、加入相機、渲染
bpy.data.objects.remove(mocap_armature)
scene = bpy.context.scene
scene.frame_start, scene.frame_end = int(mocap_action.frame_range[0]), int(mocap_action.frame_range[1])
scene.render.filepath = "/tmp/mocap_preview/frame_"
bpy.ops.render.render(animation=True)
指導原則
- BVH 最簡單(純文字,包含階層結構 + 動作)。FBX 支援更豐富的資料(如混合形狀、多重片段)。
- 比例很重要:BVH 檔案通常使用公分。對於以公分為基礎的檔案,設定
global_scale=0.01。 - 骨骼名稱匹配對於重新定位至關重要。為每種來源格式建立一個對應字典。
- 重新定位時,為所有骨骼複製旋轉,但僅為根部/臀部骨骼複製位置。
- 透過降採樣關鍵影格來清理匯入的動作捕捉資料——原始動作捕捉的每個影格都有關鍵影格,這會使編輯變得困難。
- 相機解算品質取決於標記點的數量和分佈。使用 8 個以上均勻分佈的標記點,並將誤差保持在 0.5px 以下。
- 使用
bpy.ops.nla.bake()將約束轉換為關鍵影格以便匯出。 - 在 FBX 中匯出時始終使用
bake_anim=True,以展平 NLA 片段和約束。