提示语:
用python写一段程序,要求读取CSV中的每一行内容,将每一行内容都变为双引号加该行内容,加上冒号,再加上双引号加该行内容,再加上逗号,然后保存到新的CSV文件中 例如: CSV中一行内容为: Bip001-Pelvis 处理后内容为: "Bip001-Pelvis": "Bip001-Pelvis"
代码:
import csv # 指定原始CSV文件路径 input_csv_path = 'original.csv' # 指定输出CSV文件路径 output_csv_path = 'processed.csv' # 使用with语句打开原始CSV文件和创建新CSV文件 with open(input_csv_path, mode='r', newline='', encoding='utf-8') as infile, \ open(output_csv_path, mode='w', newline='', encoding='utf-8') as outfile: # 创建CSV读取器和写入器 reader = csv.reader(infile) writer = csv.writer(outfile) # 遍历CSV文件中的每一行 for row in reader: # 处理每一行,按照要求格式化 processed_row = ['"' + item + '": "' + item + '",' for item in row] # 将处理后的数据写入新的CSV文件 writer.writerow(processed_row) print("CSV文件处理完成,并已保存到", output_csv_path)
3.添加复制旋转约束
import bpy # 定义骨架中骨骼的映射关系 bone_mapping = { "mixamorig:Hips": "mixamorig:Hips", "mixamorig:Spine": "mixamorig:Spine", "mixamorig:Spine1": "mixamorig:Spine1", "mixamorig:Spine2": "mixamorig:Spine2", "mixamorig:Neck": "mixamorig:Neck", "mixamorig:Head": "mixamorig:Head", "mixamorig:HeadTop_End": "mixamorig:HeadTop_End", "mixamorig:LeftShoulder": "mixamorig:LeftShoulder", "mixamorig:LeftArm": "mixamorig:LeftArm", "mixamorig:LeftForeArm": "mixamorig:LeftForeArm", "mixamorig:LeftHand": "mixamorig:LeftHand", "mixamorig:LeftHandThumb1": "mixamorig:LeftHandThumb1", "mixamorig:LeftHandThumb2": "mixamorig:LeftHandThumb2", "mixamorig:LeftHandThumb3": "mixamorig:LeftHandThumb3", "mixamorig:LeftHandThumb4": "mixamorig:LeftHandThumb4", "mixamorig:LeftHandIndex1": "mixamorig:LeftHandIndex1", "mixamorig:LeftHandIndex2": "mixamorig:LeftHandIndex2", "mixamorig:LeftHandIndex3": "mixamorig:LeftHandIndex3", "mixamorig:LeftHandIndex4": "mixamorig:LeftHandIndex4", "mixamorig:LeftHandMiddle1": "mixamorig:LeftHandMiddle1", "mixamorig:LeftHandMiddle2": "mixamorig:LeftHandMiddle2", "mixamorig:LeftHandMiddle3": "mixamorig:LeftHandMiddle3", "mixamorig:LeftHandMiddle4": "mixamorig:LeftHandMiddle4", "mixamorig:LeftHandRing1": "mixamorig:LeftHandRing1", "mixamorig:LeftHandRing2": "mixamorig:LeftHandRing2", "mixamorig:LeftHandRing3": "mixamorig:LeftHandRing3", "mixamorig:LeftHandRing4": "mixamorig:LeftHandRing4", "mixamorig:LeftHandPinky1": "mixamorig:LeftHandPinky1", "mixamorig:LeftHandPinky2": "mixamorig:LeftHandPinky2", "mixamorig:LeftHandPinky3": "mixamorig:LeftHandPinky3", "mixamorig:LeftHandPinky4": "mixamorig:LeftHandPinky4", "mixamorig:RightShoulder": "mixamorig:RightShoulder", "mixamorig:RightArm": "mixamorig:RightArm", "mixamorig:RightForeArm": "mixamorig:RightForeArm", "mixamorig:RightHand": "mixamorig:RightHand", "mixamorig:RightHandThumb1": "mixamorig:RightHandThumb1", "mixamorig:RightHandThumb2": "mixamorig:RightHandThumb2", "mixamorig:RightHandThumb3": "mixamorig:RightHandThumb3", "mixamorig:RightHandThumb4": "mixamorig:RightHandThumb4", "mixamorig:RightHandIndex1": "mixamorig:RightHandIndex1", "mixamorig:RightHandIndex2": "mixamorig:RightHandIndex2", "mixamorig:RightHandIndex3": "mixamorig:RightHandIndex3", "mixamorig:RightHandIndex4": "mixamorig:RightHandIndex4", "mixamorig:RightHandMiddle1": "mixamorig:RightHandMiddle1", "mixamorig:RightHandMiddle2": "mixamorig:RightHandMiddle2", "mixamorig:RightHandMiddle3": "mixamorig:RightHandMiddle3", "mixamorig:RightHandMiddle4": "mixamorig:RightHandMiddle4", "mixamorig:RightHandRing1": "mixamorig:RightHandRing1", "mixamorig:RightHandRing2": "mixamorig:RightHandRing2", "mixamorig:RightHandRing3": "mixamorig:RightHandRing3", "mixamorig:RightHandRing4": "mixamorig:RightHandRing4", "mixamorig:RightHandPinky1": "mixamorig:RightHandPinky1", "mixamorig:RightHandPinky2": "mixamorig:RightHandPinky2", "mixamorig:RightHandPinky3": "mixamorig:RightHandPinky3", "mixamorig:RightHandPinky4": "mixamorig:RightHandPinky4", "mixamorig:LeftUpLeg": "mixamorig:LeftUpLeg", "mixamorig:LeftLeg": "mixamorig:LeftLeg", "mixamorig:LeftFoot": "mixamorig:LeftFoot", "mixamorig:LeftToeBase": "mixamorig:LeftToeBase", "mixamorig:LeftToe_End": "mixamorig:LeftToe_End", "mixamorig:RightUpLeg": "mixamorig:RightUpLeg", "mixamorig:RightLeg": "mixamorig:RightLeg", "mixamorig:RightFoot": "mixamorig:RightFoot", "mixamorig:RightToeBase": "mixamorig:RightToeBase", "mixamorig:RightToe_End": "mixamorig:RightToe_End", } # 要添加约束的骨架名称 source_armature_name = "Armature" target_armature_name = "Armature.001" # 找到源骨架和目标骨架对象 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.添加复制位置约束
import bpy # 定义骨架中骨骼的映射关系 bone_mapping = { "mixamorig:Hips": "mixamorig:Hips", "mixamorig:Spine": "mixamorig:Spine", "mixamorig:Spine1": "mixamorig:Spine1", "mixamorig:Spine2": "mixamorig:Spine2", "mixamorig:Neck": "mixamorig:Neck", "mixamorig:Head": "mixamorig:Head", "mixamorig:HeadTop_End": "mixamorig:HeadTop_End", "mixamorig:LeftShoulder": "mixamorig:LeftShoulder", "mixamorig:LeftArm": "mixamorig:LeftArm", "mixamorig:LeftForeArm": "mixamorig:LeftForeArm", "mixamorig:LeftHand": "mixamorig:LeftHand", "mixamorig:LeftHandThumb1": "mixamorig:LeftHandThumb1", "mixamorig:LeftHandThumb2": "mixamorig:LeftHandThumb2", "mixamorig:LeftHandThumb3": "mixamorig:LeftHandThumb3", "mixamorig:LeftHandThumb4": "mixamorig:LeftHandThumb4", "mixamorig:LeftHandIndex1": "mixamorig:LeftHandIndex1", "mixamorig:LeftHandIndex2": "mixamorig:LeftHandIndex2", "mixamorig:LeftHandIndex3": "mixamorig:LeftHandIndex3", "mixamorig:LeftHandIndex4": "mixamorig:LeftHandIndex4", "mixamorig:LeftHandMiddle1": "mixamorig:LeftHandMiddle1", "mixamorig:LeftHandMiddle2": "mixamorig:LeftHandMiddle2", "mixamorig:LeftHandMiddle3": "mixamorig:LeftHandMiddle3", "mixamorig:LeftHandMiddle4": "mixamorig:LeftHandMiddle4", "mixamorig:LeftHandRing1": "mixamorig:LeftHandRing1", "mixamorig:LeftHandRing2": "mixamorig:LeftHandRing2", "mixamorig:LeftHandRing3": "mixamorig:LeftHandRing3", "mixamorig:LeftHandRing4": "mixamorig:LeftHandRing4", "mixamorig:LeftHandPinky1": "mixamorig:LeftHandPinky1", "mixamorig:LeftHandPinky2": "mixamorig:LeftHandPinky2", "mixamorig:LeftHandPinky3": "mixamorig:LeftHandPinky3", "mixamorig:LeftHandPinky4": "mixamorig:LeftHandPinky4", "mixamorig:RightShoulder": "mixamorig:RightShoulder", "mixamorig:RightArm": "mixamorig:RightArm", "mixamorig:RightForeArm": "mixamorig:RightForeArm", "mixamorig:RightHand": "mixamorig:RightHand", "mixamorig:RightHandThumb1": "mixamorig:RightHandThumb1", "mixamorig:RightHandThumb2": "mixamorig:RightHandThumb2", "mixamorig:RightHandThumb3": "mixamorig:RightHandThumb3", "mixamorig:RightHandThumb4": "mixamorig:RightHandThumb4", "mixamorig:RightHandIndex1": "mixamorig:RightHandIndex1", "mixamorig:RightHandIndex2": "mixamorig:RightHandIndex2", "mixamorig:RightHandIndex3": "mixamorig:RightHandIndex3", "mixamorig:RightHandIndex4": "mixamorig:RightHandIndex4", "mixamorig:RightHandMiddle1": "mixamorig:RightHandMiddle1", "mixamorig:RightHandMiddle2": "mixamorig:RightHandMiddle2", "mixamorig:RightHandMiddle3": "mixamorig:RightHandMiddle3", "mixamorig:RightHandMiddle4": "mixamorig:RightHandMiddle4", "mixamorig:RightHandRing1": "mixamorig:RightHandRing1", "mixamorig:RightHandRing2": "mixamorig:RightHandRing2", "mixamorig:RightHandRing3": "mixamorig:RightHandRing3", "mixamorig:RightHandRing4": "mixamorig:RightHandRing4", "mixamorig:RightHandPinky1": "mixamorig:RightHandPinky1", "mixamorig:RightHandPinky2": "mixamorig:RightHandPinky2", "mixamorig:RightHandPinky3": "mixamorig:RightHandPinky3", "mixamorig:RightHandPinky4": "mixamorig:RightHandPinky4", "mixamorig:LeftUpLeg": "mixamorig:LeftUpLeg", "mixamorig:LeftLeg": "mixamorig:LeftLeg", "mixamorig:LeftFoot": "mixamorig:LeftFoot", "mixamorig:LeftToeBase": "mixamorig:LeftToeBase", "mixamorig:LeftToe_End": "mixamorig:LeftToe_End", "mixamorig:RightUpLeg": "mixamorig:RightUpLeg", "mixamorig:RightLeg": "mixamorig:RightLeg", "mixamorig:RightFoot": "mixamorig:RightFoot", "mixamorig:RightToeBase": "mixamorig:RightToeBase", "mixamorig:RightToe_End": "mixamorig:RightToe_End", } # 要添加约束的骨架名称 source_armature_name = "Armature" target_armature_name = "Armature.001" # 找到源骨架和目标骨架对象 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}")