保存在CSV中

import bpy
import csv

def get_bone_names(armature_name):
    bone_names = []
    # 找到骨架对象
    armature_obj = bpy.data.objects.get(armature_name)
    if not armature_obj or armature_obj.type != 'ARMATURE':
        print(f"Armature '{armature_name}' not found or is not an armature.")
        return bone_names

    # 遍历骨架中的所有骨骼
    for bone in armature_obj.pose.bones:
        bone_names.append(bone.name)

    return bone_names

def write_bone_names_to_csv(bone_names, csv_file_path):
    with open(csv_file_path, mode='w', newline='') as file:
        writer = csv.writer(file)
        writer.writerow(["Bone Name"])  # 写入标题
        for bone_name in bone_names:
            writer.writerow([bone_name])  # 写入每个骨骼的名称

# 获取骨架中所有骨骼的名称
bone_names = get_bone_names('Armature')

# 指定CSV文件的路径
csv_file_path = 'E:/guge/bone_names.csv'

# 将骨骼名称写入CSV文件
write_bone_names_to_csv(bone_names, csv_file_path)
print(f"Bone names have been written to {csv_file_path}")

2.保存在txt 中

 

import bpy

def get_bone_names(armature_name):
    # 找到骨架对象
    armature_obj = bpy.data.objects.get(armature_name)
    if armature_obj is None or armature_obj.type != 'ARMATURE':
        print(f"Armature '{armature_name}' not found or is not an armature.")
        return []

    # 存储骨骼名称的列表
    bone_names = [bone.name for bone in armature_obj.pose.bones]
    return bone_names

def write_bone_names_to_txt(bone_names, file_path):
    # 写入骨骼名称到文本文件
    with open(file_path, 'w') as file:
        for bone_name in bone_names:
            file.write(bone_name + '\n')  # 每个名称后添加换行符

# 骨架名称
armature_name = 'Armature'
# 骨骼名称列表
bone_names = get_bone_names(armature_name)

# 指定txt文件的路径
txt_file_path = 'E:/guge/bone_names.txt'

# 将骨骼名称写入txt文件,每个名字一行
write_bone_names_to_txt(bone_names, txt_file_path)
print(f"Bone names have been written to {txt_file_path}")

 3.对应骨骼

Biped_Root    Skadi_SwimSuit_A_arma            Armature

Pelvis       ||  Bip001-Pelvis          ||   mixamorig:Hips
Left_Thigh   ||  Bip001-L-Thigh         || mixamorig:LeftUpLeg
Left_Calf    ||  Bip001-L-Calf || mixamorig:LeftLeg
Left_Foot     || Bip001-L-Foot || mixamorig:LeftFoot
Right_Thigh   || Bip001-R-Thigh || mixamorig:RightUpLeg
Right_Calf     || Bip001-R-Calf || mixamorig:RightLeg
Right_Foot     || Bip001-R-Foot || mixamorig:RightFoot  
Spine        ||   Bip001-Spine     ||  mixamorig:Spine
Spine1       ||   Bip001-Spine1      || mixamorig:Spine1
Spine2        ||   Bip001-Spine2      || mixamorig:Spine2
Right_Clavicle || Bip001-R-Clavicle || mixamorig:RightShoulder
Right_UpperArm || Bip001-R-UpperArm || mixamorig:RightArm
Right_Forearm  || Bip001-R-Forearm || mixamorig:RightForeArm
Right_Hand     || Bip001-R-Hand || mixamorig:RightHand
Neck           || Bip001-Neck  ||mixamorig:Neck
Head          || Bip001-Head || mixamorig:Head
Left_Clavicle  || Bip001-L-Clavicle  || mixamorig:LeftShoulder
Left_UpperArm  || Bip001-L-UpperArm ||mixamorig:LeftArm
Left_Forearm  || Bip001-L-Forearm || mixamorig:LeftForeArm
Left_Hand     || Bip001-L-Hand || mixamorig:LeftHand

 4.添加复制旋转约束

import bpy

# 定义骨架中骨骼的映射关系
bone_mapping = {
    "Bip001-Pelvis": "mixamorig:Hips",
    "Bip001-L-Thigh": "mixamorig:LeftUpLeg",
    "Bip001-L-Calf": "mixamorig:LeftLeg",
    "Bip001-L-Foot": "mixamorig:LeftFoot",
    "Bip001-L-Hand": "mixamorig:LeftHand",  # 假设Left_Toe0没有旋转约束
    "Bip001-R-Thigh": "mixamorig:RightUpLeg",
    "Bip001-R-Calf": "mixamorig:RightLeg",
    "Bip001-R-Foot": "mixamorig:RightFoot",
    "Bip001-R-Hand": "mixamorig:RightHand",  # 假设Right_Toe0没有旋转约束
    "Bip001-Spine": "mixamorig:Spine",
    "Bip001-Spine1": "mixamorig:Spine1",
    "Bip001-Spine2": "mixamorig:Spine2",
    "Bip001-R-Clavicle": "mixamorig:RightShoulder",
    "Bip001-R-UpperArm": "mixamorig:RightArm",
    "Bip001-R-Forearm": "mixamorig:RightForeArm",
    "Bip001-Neck": "mixamorig:Neck",
    "Bip001-Head": "mixamorig:Head",
    "Bip001-L-Clavicle": "mixamorig:LeftShoulder",
    "Bip001-L-UpperArm": "mixamorig:LeftArm",
    "Bip001-L-Forearm": "mixamorig:LeftForeArm",
}

# 要添加约束的骨架名称
source_armature_name = "Skadi_SwimSuit_A_arma"
target_armature_name = "Armature"

# 找到源骨架和目标骨架对象
source_armature_obj = bpy.data.objects.get(source_armature_name)
target_armature_obj = bpy.data.objects.get(target_armature_name)

if source_armature_obj and target_armature_obj and source_armature_obj.type == 'ARMATURE' and target_armature_obj.type == 'ARMATURE':
    # 遍历映射关系,为每个骨骼添加复制旋转约束
    for source_bone_name, target_bone_name in bone_mapping.items():
        source_bone = source_armature_obj.pose.bones.get(source_bone_name)
        target_bone = target_armature_obj.pose.bones.get(target_bone_name)
        
        if source_bone and target_bone:
            # 添加复制旋转约束
            constraint = source_bone.constraints.new('COPY_ROTATION')
            constraint.target = target_armature_obj
            constraint.subtarget = target_bone.name
            print(f"Added COPY_ROTATION constraint from {source_bone_name} to {target_bone_name}.")
        else:
            print(f"One or both bones not found: {source_bone_name}, {target_bone_name}")
else:
    print(f"One or both armatures not found or not of type ARMATURE: {source_armature_name}, {target_armature_name}")

5.添加复制位置约束

 

import bpy

# 定义骨架中骨骼的映射关系
bone_mapping = {
    "Bip001-Pelvis": "mixamorig:Hips",
    "Bip001-L-Thigh": "mixamorig:LeftUpLeg",
    "Bip001-L-Calf": "mixamorig:LeftLeg",
    "Bip001-L-Foot": "mixamorig:LeftFoot",
    "Bip001-L-Hand": "mixamorig:LeftHand",  # 假设Left_Toe0没有旋转约束
    "Bip001-R-Thigh": "mixamorig:RightUpLeg",
    "Bip001-R-Calf": "mixamorig:RightLeg",
    "Bip001-R-Foot": "mixamorig:RightFoot",
    "Bip001-R-Hand": "mixamorig:RightHand",  # 假设Right_Toe0没有旋转约束
    "Bip001-Spine": "mixamorig:Spine",
    "Bip001-Spine1": "mixamorig:Spine1",
    "Bip001-Spine2": "mixamorig:Spine2",
    "Bip001-R-Clavicle": "mixamorig:RightShoulder",
    "Bip001-R-UpperArm": "mixamorig:RightArm",
    "Bip001-R-Forearm": "mixamorig:RightForeArm",
    "Bip001-Neck": "mixamorig:Neck",
    "Bip001-Head": "mixamorig:Head",
    "Bip001-L-Clavicle": "mixamorig:LeftShoulder",
    "Bip001-L-UpperArm": "mixamorig:LeftArm",
    "Bip001-L-Forearm": "mixamorig:LeftForeArm",
}

# 要添加约束的骨架名称
source_armature_name = "Skadi_SwimSuit_A_arma"
target_armature_name = "Armature"

# 找到源骨架和目标骨架对象
source_armature_obj = bpy.data.objects.get(source_armature_name)
target_armature_obj = bpy.data.objects.get(target_armature_name)

if source_armature_obj and target_armature_obj and source_armature_obj.type == 'ARMATURE' and target_armature_obj.type == 'ARMATURE':
    # 遍历映射关系,为每个骨骼添加复制旋转约束
    for source_bone_name, target_bone_name in bone_mapping.items():
        source_bone = source_armature_obj.pose.bones.get(source_bone_name)
        target_bone = target_armature_obj.pose.bones.get(target_bone_name)
        
        if source_bone and target_bone:
            # 添加复制旋转约束
            constraint = source_bone.constraints.new('COPY_LOCATION')
            constraint.target = target_armature_obj
            constraint.subtarget = target_bone.name
            print(f"Added COPY_LOCATION constraint from {source_bone_name} to {target_bone_name}.")
        else:
            print(f"One or both bones not found: {source_bone_name}, {target_bone_name}")
else:
    print(f"One or both armatures not found or not of type ARMATURE: {source_armature_name}, {target_armature_name}")

 

posted on 2024-06-11 18:01  大话人生  阅读(103)  评论(0编辑  收藏  举报