1.将骨架Skadi_SwimSuit_A_arma中的骨骼添加复制位置和复制旋转,目标选择骨架“Biped_Root”中对应的骨骼

2.对应骨骼.txt

Biped_Root    || Skadi_SwimSuit_A_arma

Pelvis        || Bip001-Pelvis
Left_Thigh    ||  Bip001-L-Thigh
Left_Calf     ||  Bip001-L-Calf
Left_Foot     || Bip001-L-Foot
Right_Thigh    || Bip001-R-Thigh
Right_Calf     || Bip001-R-Calf
Right_Foot     || Bip001-R-Foot
Spine          ||  Bip001-Spine
Spine1         ||  Bip001-Spine1
Spine2          || Bip001-Spine2
Right_Clavicle  || Bip001-R-Clavicle
Right_UpperArm  || Bip001-R-UpperArm
Right_Forearm   || Bip001-R-Forearm
Right_Hand      || Bip001-R-Hand
Neck           || Bip001-Neck
Head           || Bip001-Head
Left_Clavicle  || Bip001-L-Clavicle
Left_UpperArm  || Bip001-L-UpperArm
Left_Forearm   || Bip001-L-Forearm
Left_Hand      || Bip001-L-Hand

3.添加复制旋转约束代码:

import bpy

# 定义骨架中骨骼的映射关系
bone_mapping = {
    "Bip001-Pelvis": "Pelvis",
    "Bip001-L-Thigh": "Left_Thigh",
    "Bip001-L-Calf": "Left_Calf",
    "Bip001-L-Foot": "Left_Foot",
    "Bip001-L-Hand": "Left_Hand",  # 假设Left_Toe0没有旋转约束
    "Bip001-R-Thigh": "Right_Thigh",
    "Bip001-R-Calf": "Right_Calf",
    "Bip001-R-Foot": "Right_Foot",
    "Bip001-R-Hand": "Right_Hand",  # 假设Right_Toe0没有旋转约束
    "Bip001-Spine": "Spine",
    "Bip001-Spine1": "Spine1",
    "Bip001-Spine2": "Spine2",
    "Bip001-R-Clavicle": "Right_Clavicle",
    "Bip001-R-UpperArm": "Right_UpperArm",
    "Bip001-R-Forearm": "Right_Forearm",
    "Bip001-Neck": "Neck",
    "Bip001-Head": "Head",
    "Bip001-L-Clavicle": "Left_Clavicle",
    "Bip001-L-UpperArm": "Left_UpperArm",
    "Bip001-L-Forearm": "Left_Forearm",
}

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

# 找到源骨架和目标骨架对象
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}")

 4.添加复制旋转成功核心代码

bpy.ops.pose.constraint_add(type='COPY_ROTATION')
bpy.context.object.pose.bones["Bip001-L-Forearm"].constraints["复制旋转"].target = bpy.data.objects["Biped_Root"]
bpy.context.object.pose.bones["Bip001-L-Forearm"].constraints["复制旋转"].subtarget = "Left_Forearm"

AI自动写代码模板:

bpy.ops.pose.constraint_add(type='COPY_ROTATION')
bpy.context.object.pose.bones["Bip001-L-Forearm"].constraints["复制旋转"].target = bpy.data.objects["Biped_Root"]
bpy.context.object.pose.bones["Bip001-L-Forearm"].constraints["复制旋转"].subtarget = "Left_Forearm"

以上是给"Skadi_SwimSuit_A_arma"中“Bip001-L-Forearm”骨骼添加复制旋转约束(目标是骨架“Biped_Root”中的"Left_Forearm"骨骼)代码,
且已知 骨架“Biped_Root“和   骨架 ”Skadi_SwimSuit_A_arma”相对应的骨骼如下:
Pelvis         Bip001-Pelvis
Left_Thigh     Bip001-L-Thigh
Left_Calf      Bip001-L-Calf
Left_Foot      Bip001-L-Foot
Left_Toe0
Right_Thigh    Bip001-R-Thigh
Right_Calf     Bip001-R-Calf
Right_Foot     Bip001-R-Foot
Right_Toe0
Spine           Bip001-Spine
Spine1          Bip001-Spine1
Spine2          Bip001-Spine2
Right_Clavicle  Bip001-R-Clavicle
Right_UpperArm  Bip001-R-UpperArm
Right_Forearm   Bip001-R-Forearm
Right_Hand      Bip001-R-Hand
Neck           Bip001-Neck
Head           Bip001-Head
Left_Clavicle  Bip001-L-Clavicle

Left_UpperArm  Bip001-L-UpperArm

Left_Forearm   Bip001-L-Forearm

Left_Hand      Bip001-L-Hand


请用blender python api写一个为以上所有对应骨骼添加对应复制旋转约束的代码

 

 

5.添加复制位置约束核心代码

bpy.ops.pose.constraint_add(type='COPY_LOCATION')

6.添加复制位置约束代码:

import bpy

# 定义骨架中骨骼的映射关系
bone_mapping = {
    "Bip001-Pelvis": "Pelvis",
    "Bip001-L-Thigh": "Left_Thigh",
    "Bip001-L-Calf": "Left_Calf",
    "Bip001-L-Foot": "Left_Foot",
    "Bip001-L-Hand": "Left_Hand",  # 假设Left_Toe0没有旋转约束
    "Bip001-R-Thigh": "Right_Thigh",
    "Bip001-R-Calf": "Right_Calf",
    "Bip001-R-Foot": "Right_Foot",
    "Bip001-R-Hand": "Right_Hand",  # 假设Right_Toe0没有旋转约束
    "Bip001-Spine": "Spine",
    "Bip001-Spine1": "Spine1",
    "Bip001-Spine2": "Spine2",
    "Bip001-R-Clavicle": "Right_Clavicle",
    "Bip001-R-UpperArm": "Right_UpperArm",
    "Bip001-R-Forearm": "Right_Forearm",
    "Bip001-Neck": "Neck",
    "Bip001-Head": "Head",
    "Bip001-L-Clavicle": "Left_Clavicle",
    "Bip001-L-UpperArm": "Left_UpperArm",
    "Bip001-L-Forearm": "Left_Forearm",
}

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

# 找到源骨架和目标骨架对象
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_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}")

 

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