LoginSignup
2
1

More than 1 year has passed since last update.

Blenderでモーションデータの特定のボーンを一括でリネーム・削除するスクリプト

Last updated at Posted at 2023-02-02

内容

モーションデータの一部(補助骨や指の関節など)が不要の場合、簡易化したデータにする必要がありますが、その対応方法の備忘録です。
一部名前を変更したいボーンがあったので、その部分の名前の変更も行っています。

Blender Pythonでのボーンの種類

Bone

Objectモードのボーン。Read Onlyなので、名前の変更やボーンの追加、削除ができません。

EditBone

Editモードのボーン。名前の変更やボーンの追加、削除ができますが、フレームごとのデータ編集はできません。

PoseBone

Poseモードのボーン。フレームごとのデータ編集ができます。




今回はフレームデータを編集しないので、EditBoneを使用します。

コード例


rename.py
import bpy
import os

src_to_tgt = {"Upper":"Up", "Lower":"Bottom"} # リネーム用の対応関係例
bones_to_remove = ["Helper:Cloth1", "Helper:Cloth2", "Helper:Cloth3", "Helper:Cloth4", "Helper:Cloth5"] # 削除用の骨の例

def init_scene():
    if bpy.context:
        scene = bpy.context.scene
        for obj in scene.objects:
            bpy.data.objects.remove(obj, do_unlink=True)
        
def convert_bvh(input_path, output_path):
    bpy.ops.import_anim.bvh(filepath=input_path)
    bpy.ops.object.select_by_type(extend=False, type='ARMATURE')

    arm_data = bpy.context.object.data
    bpy.ops.object.mode_set(mode='EDIT')
    for bone in arm_data.edit_bones:
        if bone.name[:5] in src_to_tgt.keys():
            bone.name = src_to_tgt[bone.name[:5]] + bone.name[5:] # リネーム
        
        if bone.name in bones_to_remove:
            arm_data.edit_bones.remove(bone) # 削除
        
    bpy.ops.object.mode_set(mode='OBJECT') # OBJECTモードにして編集したEditBoneをBoneに適用
    bpy.ops.export_anim.bvh(filepath=output_path)

if __name__ ==  '__main__':
    input_dir = "元のBVH群を格納するフォルダへのパス"
    output_dir = "出力するBVH群を格納するフォルダへのパス"
    os.makedirs(out_dir, exist_ok=True)
    for input_file in os.listdir(in_dir):
        init_scene()
        input_path = input_dir + input_file
        output_path = output_dir + input_file[:-4] + "_edited.bvh"
        convert_bvh(input_path, output_path)

  • 注意点としては BlenderのBVH出力関数がBoneを参照するため、
    編集したモーションをエクスポートする前にObjectモードに変更する必要があります。
    これによりBoneがEditBoneのデータに更新されます。
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1