Mesh parented to an armature has very bad deformation

I have a mesh and an armature found in the .blend file here. The armature need to be put in Tpose as rest position before parenting the mesh to it (note: the armature has some bones hidden with deform off), to do that, I switched to Pose Mode then rotated the armature bones to have a Tpose as shown:


Then used batFINGERs scripts found in this BA’s link: https://blenderartists.org/forum/showthread.php?254060-modify-motion-capture-T-pose to set the new TPose as rest position.
So I first run the Script1ToRunAfterMakingTpose script found in the below link (I initially tried pasting it but the post exceeded the max number of allowed char so I used pasteall):

http://pasteall.org/135545

This will create a panel under the armature object data as shown below:


After that I click on “New Rest Pose to Rig” button, then click on the second button “BVH action to new restpose rig”.
Then I run the script named “Script2ToRunAfterMakingTpose” in my file:

import bpy

class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"




    @classmethod
    def poll(cls, context):
        return context.active_object is not None


    def execute(self, context):
        #dupe the armature
        bpy.ops.object.mode_set(mode='OBJECT')
        bpy.ops.object.duplicate()
        newarm = context.scene.objects.active
        # copy the action
        newaction = newarm.animation_data.action.copy()
        # strip out the old fcurves
        for fcurve in newaction.fcurves:
            if fcurve.data_path.find(".001") == -1:
                newaction.fcurves.remove(fcurve)
            else:
                fcurve.data_path = fcurve.data_path.replace(".001","")
        newarm.animation_data.action = newaction


        bones = [bone.name for bone in newarm.pose.bones if bone.get('bvh') is None]
        # remove original bones
        bpy.ops.object.mode_set(mode='EDIT')


        arm = newarm.data
        for name in bones:
            eb = arm.edit_bones.get(name)
            if eb is not None:
                arm.edit_bones.remove(eb)


        # rename bones to original names        
        bpy.ops.object.mode_set(mode='POSE')
        for pb in newarm.pose.bones:
            pb.name = pb["bvh"]


        return {'FINISHED'}


def register():
    bpy.utils.register_class(SimpleOperator)


def unregister():
    bpy.utils.unregister_class(SimpleOperator)


if __name__ == "__main__":
    register()


    # test call
    bpy.ops.object.simple_operator()

Here you will find that there are two armatures, the original “MyRig” and a new one “MyRig.001”. So I delete the old one because it has a duplicates of the armature bones and keep the new one because it has the new Tpose as rest position with blue color.

I then parent the mesh to the armature with automatic weighting but when I play the animation the mesh is horribly deformed as shown below:


I don’t know the reason this is happening and I’ve been trying to figure it out in the last week but I can’t figure it out. So could anyone please try to help me?

I’ve made a .blend file found here which has all the scripts in it. It contains 2 armatures and 2 meshes. An armature called MyRigTest and a Mesh called MyMeshTest are the ones which I tried to do showing the end result. And an Armature called MyRig and a Mesh called MyMesh which are the original armature and mesh without applying the steps above. I also wrote all the steps I did in a text block. Would really appreciate any help. Thank you.

The armature need to be put in Tpose as rest position before parenting the mesh to it (note: the armature has some bones hidden with deform off), to do that, I switched to Pose Mode then rotated the armature bones to have a Tpose as shown:

Don’t use Pose mode to align your Armature to your Mesh…
this must be done in Edit mode…