頂点グループをボーン階層と名前の両方でソートする

頂点グループにて、ボーン階層でソートすると、同じ階層にあるボーンは作成した順番になる。
同じ階層にあるボーンが名前順になるように、親子関係を全てリセットして、名前順に親子関係を戻してゆく。

手順

  • Blender ファイルを保存する。
  • オブジェクトモードでアーマチュア(ボーン)を選択する。
  • 以下のスクリプトを実行する。
import bpy

bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.mode_set(mode='EDIT')

bones = []
connected_bones = {}
for bone in bpy.context.active_object.data.bones:
    bones.append(bone.name)
    connected_bones[bone.name] = bone.use_connect

for bone in bones:
    sorted_children = []
    children = bpy.context.active_object.data.edit_bones[bone].children

    for child in children:
        sorted_children.append(child.name)
        child.parent = None

    sorted_children.sort()

    parent = bpy.context.active_object.data.edit_bones[bone]
    for sorted_child in sorted_children:
        child = bpy.context.active_object.data.edit_bones[sorted_child]
        child.parent = parent
        
    parent.use_connect = connected_bones[bone]
        
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.mode_set(mode='EDIT')

  • 適当なオブジェクト(メッシュ)を選択する。
  • 頂点グループにて、ボーン階層でソートする。
  • 同じ階層にあるボーンが、名前順になっていることを確認する。

コメントする

メールアドレスが公開されることはありません。 が付いている欄は必須項目です