import re import bpy def select_hierarchy(obj): obj.select_set(True) for child in obj.children: select_hierarchy(child) BASE_FILES_PATH = '/Users/alessandro/Downloads/' BLEND_FILE_NOT_OK = "lab_test.blend" BLEND_FILE_OK = "lab_test_ok.blend" # Make sure you're in object mode bpy.ops.object.mode_set(mode='OBJECT') for file in [BLEND_FILE_NOT_OK, BLEND_FILE_OK]: bpy.ops.wm.open_mainfile(filepath=BASE_FILES_PATH + file) for obj in bpy.context.scene.objects: to_add = False if re.match(r'[A-Z]+__(.*)__(.*)', obj.name): to_add = True elif re.match(r'[A-Z]+__(.*)', obj.name): obj.name = f"{obj.name}__{obj.name.split('__')[1]}" to_add = True if to_add: bpy.ops.object.select_all(action='DESELECT') # Select the current mesh object select_hierarchy(obj) bpy.ops.wm.collada_export(filepath=BASE_FILES_PATH+file+'_'+obj.name+'.dae', check_existing=False, selected=True, use_texture_copies=True, include_children=True)