bl_info = {
    "name": "Vroid To Mixamo Rename",
    "author": "Artificial Light",
    "version": (1, 0),
    "blender": (2, 80, 0),
    "location": "View3D > N",
    "description": "Renames Vrm importer bones to mixamo add-on and changes the bone roll so that Mixamo add-on works with Vroid models",
    "warning": "",
    "doc_url": "",
    "category": "",
}


import bpy
from bpy.types import (Panel,Operator)
import math

class Button(bpy.types.Operator):
    """Make Sure to Select the Armature"""
    bl_idname = "rnm.1"
    bl_label = "RENAME AND ROLL"

    

    def execute(self, context):
        context = bpy.context
        obj = context.object

        namelist = [
        ("J_Bip_C_Chest", "mixamorig:Spine1"),
        ("J_Bip_C_UpperChest","mixamorig:Spine2"),
        ("J_Bip_C_Head","mixamorig:Head"),
        ("J_Bip_C_Neck","mixamorig:Neck"),
        ("J_Bip_C_Spine","mixamorig:Spine"),
        ("J_Bip_C_Hips","mixamorig:Hips"),
        ("J_Bip_L_UpperLeg","mixamorig:LeftUpLeg"),
        ("J_Bip_R_UpperLeg","mixamorig:RightUpLeg"),
        ("J_Bip_L_LowerLeg","mixamorig:LeftLeg"),
        ("J_Bip_R_LowerLeg","mixamorig:RightLeg"),
        ("J_Bip_R_Foot","mixamorig:RightFoot"),
        ("J_Bip_L_Foot","mixamorig:LeftFoot"),
        ("J_Bip_R_ToeBase","mixamorig:RightToeBase"),
        ("J_Bip_L_ToeBase","mixamorig:LeftToeBase"),
        ("J_Bip_L_Shoulder","mixamorig:LeftShoulder"),
        ("J_Bip_R_Shoulder","mixamorig:RightShoulder"),
        ("J_Bip_L_UpperArm","mixamorig:LeftArm"),
        ("J_Bip_R_UpperArm","mixamorig:RightArm"),
        ("J_Bip_L_LowerArm","mixamorig:LeftForeArm"),
        ("J_Bip_R_LowerArm","mixamorig:RightForeArm"),
        ("J_Bip_L_Hand","mixamorig:LeftHand"),
        ("J_Bip_R_Hand","mixamorig:RightHand"),

        ("J_Bip_L_Thumb1","mixamorig:LeftHandThumb1"),
        ("J_Bip_R_Thumb1","mixamorig:RightHandThumb1"),
        ("J_Bip_L_Thumb2","mixamorig:LeftHandThumb2"),
        ("J_Bip_R_Thumb2","mixamorig:RightHandThumb2"),
        ("J_Bip_L_Thumb3","mixamorig:LeftHandThumb3"),
        ("J_Bip_R_Thumb3","mixamorig:RightHandThumb3"),

        ("J_Bip_L_Index1","mixamorig:LeftHandIndex1"),
        ("J_Bip_R_Index1","mixamorig:RightHandIndex1"),
        ("J_Bip_L_Index2","mixamorig:LeftHandIndex2"),
        ("J_Bip_R_Index2","mixamorig:RightHandIndex2"),
        ("J_Bip_L_Index3","mixamorig:LeftHandIndex3"),
        ("J_Bip_R_Index3","mixamorig:RightHandIndex3"),

        ("J_Bip_L_Middle1","mixamorig:LeftHandMiddle1"),
        ("J_Bip_R_Middle1","mixamorig:RightHandMiddle1"),
        ("J_Bip_L_Middle2","mixamorig:LeftHandMiddle2"),
        ("J_Bip_R_Middle2","mixamorig:RightHandMiddle2"),
        ("J_Bip_L_Middle3","mixamorig:LeftHandMiddle3"),
        ("J_Bip_R_Middle3","mixamorig:RightHandMiddle3"),

        ("J_Bip_L_Ring1","mixamorig:LeftHandRing1"),
        ("J_Bip_R_Ring1","mixamorig:RightHandRing1"),
        ("J_Bip_L_Ring2","mixamorig:LeftHandRing2"),
        ("J_Bip_R_Ring2","mixamorig:RightHandRing2"),
        ("J_Bip_L_Ring3","mixamorig:LeftHandRing3"),
        ("J_Bip_R_Ring3","mixamorig:RightHandRing3"),

        ("J_Bip_L_Little1","mixamorig:LeftHandPinky1"),
        ("J_Bip_R_Little1","mixamorig:RightHandPinky1"),
        ("J_Bip_L_Little2","mixamorig:LeftHandPinky2"),
        ("J_Bip_R_Little2","mixamorig:RightHandPinky2"),
        ("J_Bip_L_Little3","mixamorig:LeftHandPinky3"),
        ("J_Bip_R_Little3","mixamorig:RightHandPinky3"),

        ("J_Adj_L_FaceEye","Eye.L"),
        ("J_Adj_R_FaceEye","Eye.R"),
        ("J_Sec_L_Bust1","Bust1.L"),
        ("J_Sec_R_Bust1","Bust1.R"),
        ("J_Sec_L_Bust2","Bust2.L"),
        ("J_Sec_R_Bust2","Bust2.R"),

        ]

        for name, newname in namelist:
            # get the pose bone with name
            pb = obj.pose.bones.get(name)
            # continue if no bone of that name
            if pb is None:
                continue
            # rename
            pb.name = newname

        bpy.ops.object.mode_set(mode = 'EDIT')
        bpy.context.object.data.edit_bones['mixamorig:LeftShoulder'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:LeftArm'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:LeftForeArm'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:LeftHand'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:RightShoulder'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:RightArm'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:RightForeArm'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:RightHand'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:LeftUpLeg'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:RightUpLeg'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:LeftLeg'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:RightLeg'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:LeftFoot'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:LeftToeBase'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:RightFoot'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:RightToeBase'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:LeftHandThumb1'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:LeftHandThumb2'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:LeftHandThumb3'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:RightHandThumb1'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:RightHandThumb2'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:RightHandThumb3'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:LeftHandIndex1'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:LeftHandIndex2'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:LeftHandIndex3'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:RightHandIndex1'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:RightHandIndex2'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:RightHandIndex3'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:LeftHandMiddle1'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:LeftHandMiddle2'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:LeftHandMiddle3'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:RightHandMiddle1'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:RightHandMiddle2'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:RightHandMiddle3'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:LeftHandRing1'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:LeftHandRing2'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:LeftHandRing3'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:RightHandRing1'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:RightHandRing2'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:RightHandRing3'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:LeftHandPinky1'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:LeftHandPinky2'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:LeftHandPinky3'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:RightHandPinky1'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:RightHandPinky2'].roll=+math.radians(180)
        bpy.context.object.data.edit_bones['mixamorig:RightHandPinky3'].roll=+math.radians(180)
        


        bpy.ops.object.mode_set(mode='OBJECT') 
        return {'FINISHED'}
            
class Panel(bpy.types.Panel):
    bl_label = "Rename Vroid Bones for Mixamo and Change Bone Roll"
    bl_idname = "OBJECT_PT_Rename"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = "Rename Vroid Bones and change Roll"

    def draw(self, context):
        layout = self.layout

        obj = context.object

        row = layout.row()
        row.operator(Button.bl_idname,text="RENAME AND ROLL", icon='MENU_PANEL')            

from bpy.utils import register_class, unregister_class
_classes=[
        Button,
        Panel
]
def register():
    for cls in _classes:
        register_class(cls)  

def unregister():
    
    for cls in _classes:
        unregister_class(cls)   
if __name__ == "__main__":
    register()
