#----------------------------------------------------------------------------------------------------------------------------------#
#you can use the script for any type of project but its prohibited to sell it. Questions about the script ask in the forum
#and add @Dodo2207. !!!!Im not responsible for what you do with the script!!!!! If you use the script you agree to this.
#----------------------------------------------------------------------------------------------------------------------------------#
#----------------------------------------------------------------------------------------------------------------------------------#
#Replace "iclone 3 skelleton" with your imported source.
#----------------------------------------------------------------------------------------------------------------------------------#
#Change the scaleing factor, that your source will approximitly match your target or remove it and do it .
#----------------------------------------------------------------------------------------------------------------------------------#
#Replace "mcrig" with your target.
#----------------------------------------------------------------------------------------------------------------------------------#
#Change my bonenames to your target and source bonenames. Names didnt need to match between your source and target.
#----------------------------------------------------------------------------------------------------------------------------------#
#If you have more or less bones as me, delete or add codeblocks. If nesessary you can add copy location as well.
#----------------------------------------------------------------------------------------------------------------------------------#
#Save your modification of the script and you can save a lot of time if you have many bvh files or amatrues with the same armature structure. Copy the script in the text editor, be in objectmode and have both source and target rig imported.
#----------------------------------------------------------------------------------------------------------------------------------#
import bpy
# set bvh rig as active
bpy.context.view_layer.objects.active = bpy.data.objects['iclone 3 skelleton']
# viewport display in front
bpy.context.object.show_in_front = True
# set scale for the imported byh armatrue to nearly match your target armature. In my case its mcrig
bpy.context.object.scale[0] = 0.0106921
bpy.context.object.scale[1] = 0.009831
bpy.context.object.scale[2] = 0.00983105
# select mcrig
bpy.context.view_layer.objects.active = bpy.data.objects['mcrig']
# switch to pose mode
bpy.ops.object.posemode_toggle()
# for set active bone in posemode
ob = bpy.data.objects["mcrig"]
#----------Head----------
ob.data.bones.active = ob.data.bones["Head"]
# add copy rotation constraint on the Head bone/ works fine
bpy.ops.pose.constraint_add(type='COPY_ROTATION')
# set target and subtarget
bpy.context.object.pose.bones["Head"].constraints["Copy Rotation"].target = bpy.data.objects["iclone 3 skelleton"]
bpy.context.object.pose.bones["Head"].constraints["Copy Rotation"].subtarget = "Head"
#----------Torso----------
#rotation
ob.data.bones.active = ob.data.bones["Torso"]
bpy.ops.pose.constraint_add(type='COPY_ROTATION')
bpy.context.object.pose.bones["Torso"].constraints["Copy Rotation"].target = bpy.data.objects["iclone 3 skelleton"]
bpy.context.object.pose.bones["Torso"].constraints["Copy Rotation"].subtarget = "Chest"
bpy.context.object.pose.bones["Torso"].constraints["Copy Rotation"].use_z = False
#location
bpy.ops.pose.constraint_add(type='COPY_LOCATION')
bpy.context.object.pose.bones["Torso"].constraints["Copy Location"].target = bpy.data.objects["iclone 3 skelleton"]
bpy.context.object.pose.bones["Torso"].constraints["Copy Location"].subtarget = "Chest"
#----------Top Arm.R----------
#rotation
ob.data.bones.active = ob.data.bones["Top Arm.R"]
bpy.ops.pose.constraint_add(type='COPY_ROTATION')
bpy.context.object.pose.bones["Top Arm.R"].constraints["Copy Rotation"].target = bpy.data.objects["iclone 3 skelleton"]
bpy.context.object.pose.bones["Top Arm.R"].constraints["Copy Rotation"].subtarget = "RightShoulder"
#----------Bottom Arm.R----------
#rotation
ob.data.bones.active = ob.data.bones["Bottom Arm.R"]
bpy.ops.pose.constraint_add(type='COPY_ROTATION')
bpy.context.object.pose.bones["Bottom Arm.R"].constraints["Copy Rotation"].target = bpy.data.objects["iclone 3 skelleton"]
bpy.context.object.pose.bones["Bottom Arm.R"].constraints["Copy Rotation"].subtarget = "RightElbow"
#----------Top Arm.L----------
#rotation
ob.data.bones.active = ob.data.bones["Top Arm.L"]
bpy.ops.pose.constraint_add(type='COPY_ROTATION')
bpy.context.object.pose.bones["Top Arm.L"].constraints["Copy Rotation"].target = bpy.data.objects["iclone 3 skelleton"]
bpy.context.object.pose.bones["Top Arm.L"].constraints["Copy Rotation"].subtarget = "LeftShoulder"
#----------Bottom Arm.L----------
#rotation
ob.data.bones.active = ob.data.bones["Bottom Arm.L"]
bpy.ops.pose.constraint_add(type='COPY_ROTATION')
bpy.context.object.pose.bones["Bottom Arm.L"].constraints["Copy Rotation"].target = bpy.data.objects["iclone 3 skelleton"]
bpy.context.object.pose.bones["Bottom Arm.L"].constraints["Copy Rotation"].subtarget = "LeftElbow"
#----------Pelvis----------
#rotation
ob.data.bones.active = ob.data.bones["Pelvis"]
bpy.ops.pose.constraint_add(type='COPY_ROTATION')
bpy.context.object.pose.bones["Pelvis"].constraints["Copy Rotation"].target = bpy.data.objects["iclone 3 skelleton"]
bpy.context.object.pose.bones["Pelvis"].constraints["Copy Rotation"].subtarget = "Hips"
bpy.context.object.pose.bones["Pelvis"].constraints["Copy Rotation"].use_z = False
#location
bpy.ops.pose.constraint_add(type='COPY_LOCATION')
bpy.context.object.pose.bones["Pelvis"].constraints["Copy Location"].target = bpy.data.objects["iclone 3 skelleton"]
bpy.context.object.pose.bones["Pelvis"].constraints["Copy Location"].subtarget = "Hips"
#----------Top Leg.R----------
#rotation
ob.data.bones.active = ob.data.bones["Top Leg.R"]
bpy.ops.pose.constraint_add(type='COPY_ROTATION')
bpy.context.object.pose.bones["Top Leg.R"].constraints["Copy Rotation"].target = bpy.data.objects["iclone 3 skelleton"]
bpy.context.object.pose.bones["Top Leg.R"].constraints["Copy Rotation"].subtarget = "RightHip"
#----------Bottom Leg.R----------
#rotation
ob.data.bones.active = ob.data.bones["Bottom Leg.R"]
bpy.ops.pose.constraint_add(type='COPY_ROTATION')
bpy.context.object.pose.bones["Bottom Leg.R"].constraints["Copy Rotation"].target = bpy.data.objects["iclone 3 skelleton"]
bpy.context.object.pose.bones["Bottom Leg.R"].constraints["Copy Rotation"].subtarget = "RightKnee"
#----------Leg IK.R----------
#location
ob.data.bones.active = ob.data.bones["Leg IK.R"]
bpy.ops.pose.constraint_add(type='COPY_LOCATION')
bpy.context.object.pose.bones["Leg IK.R"].constraints["Copy Location"].target = bpy.data.objects["iclone 3 skelleton"]
bpy.context.object.pose.bones["Leg IK.R"].constraints["Copy Location"].subtarget = "RightAnkle"
#----------Top Leg.L----------
#rotation
ob.data.bones.active = ob.data.bones["Top Leg.L"]
bpy.ops.pose.constraint_add(type='COPY_ROTATION')
bpy.context.object.pose.bones["Top Leg.L"].constraints["Copy Rotation"].target = bpy.data.objects["iclone 3 skelleton"]
bpy.context.object.pose.bones["Top Leg.L"].constraints["Copy Rotation"].subtarget = "LeftHip"
#----------Bottom Leg.L----------
#rotation
ob.data.bones.active = ob.data.bones["Bottom Leg.L"]
bpy.ops.pose.constraint_add(type='COPY_ROTATION')
bpy.context.object.pose.bones["Bottom Leg.L"].constraints["Copy Rotation"].target = bpy.data.objects["iclone 3 skelleton"]
bpy.context.object.pose.bones["Bottom Leg.L"].constraints["Copy Rotation"].subtarget = "LeftKnee"
#----------Leg IK.L----------
#location
ob.data.bones.active = ob.data.bones["Leg IK.L"]
bpy.ops.pose.constraint_add(type='COPY_LOCATION')
bpy.context.object.pose.bones["Leg IK.L"].constraints["Copy Location"].target = bpy.data.objects["iclone 3 skelleton"]
bpy.context.object.pose.bones["Leg IK.L"].constraints["Copy Location"].subtarget = "LeftAnkle"
#----------Root follows Hips----------
#location
ob.data.bones.active = ob.data.bones["Root"]
bpy.ops.pose.constraint_add(type='COPY_LOCATION')
bpy.context.object.pose.bones["Root"].constraints["Copy Location"].target = bpy.data.objects["mcrig"]
bpy.context.object.pose.bones["Root"].constraints["Copy Location"].subtarget = "Hips"