Script to split rigged models parts by skin weight

I have rigged vehicles models, they are made of a single mesh. Each bone is linked to a piece of the car and the skin weight paint is what point it out what is each part of the car. So the left door is set to a bone, the right door is set to another bone and so on. Each bone only affects a single piece of the vehicle.

I need a script that will:

1- Detach each part of the car using the skin weight as a selection point to the detach.
2- After detaching the script needs to rename the detached part to that same bone name.

I’m aware that it’s possible to do that manually, but I have over 200 vehicles models so I would like a way to automate it.

After hours of errors, I got a partially working script from google AI, but sadly it only detaches one part of the car and then stop working.
maybe someone knows what is wrong with the script and manage to fix it.

import bpy

def detach_all_weight_groups():
  """
  Detaches all vertex weight groups from all mesh objects in the scene.
  """

  for obj in bpy.data.objects:
    if obj.type == 'MESH':
      # Set the object as active
      bpy.context.view_layer.objects.active = obj  

      # Enter edit mode
      bpy.ops.object.mode_set(mode='EDIT')
      
      # Iterate through vertex groups
      for group in obj.vertex_groups:
        # Deselect all vertices
        bpy.ops.mesh.select_all(action='DESELECT') 

        # Select vertices based on weight group (using index directly)
        bpy.ops.object.vertex_group_select(group.index)  

        # Check if any vertices are selected (group is not empty)
        if bpy.context.object.data.total_vert_sel > 0:
          # Separate selected geometry
          bpy.ops.mesh.separate(type='SELECTED') 

          # Get the new object (the separated part)
          new_obj = bpy.context.active_object

          # Rename the new object based on the vertex group
          new_obj.name = group.name

      # Exit edit mode
      bpy.ops.object.mode_set(mode='OBJECT')

# Run the function
detach_all_weight_groups()

print("Vertex weight groups detached for all mesh objects.")