Export multiply objects (with children) using GLTF exporter


My goal is to export multiple objects via the bpy.ops.export_scene.gltf function inside a for loop.
It is important to note that each object has its own children.

Although it exports 3 different files (with different names), it only takes the latest object from the list.

Let’s say, for example…
I am selecting - obj 1, obj 2, obj 3 → It will create 3 different files with 3 different names, but the content of each file is the same = last object (obj 3).

Any help will be appreciated !


import bpy
import copy
print("********** BLENDER TO WEBGL EXPORTER **********\n")
# Set export directory
directory = "C:/Models/...."
# Make a list of selected objects
selection = [obj for obj in bpy.context.selected_objects]
print("\nSelected Models List:", selection)
bpy.ops.object.select_all(action='DESELECT') #deselect all
def exportModel(parentModel):      
    print('Exporting Parent: ', parentModel)
    # Select all visible objects grouped
    # Select parentModel and make it active
    # Export to GLB/GLTF
    bpy.ops.export_scene.gltf(filepath= directory + parentModel.name + ".glb", export_format="GLB",use_selection = True,  )
def loopOverModels():
    # for obj in se
    for parentIndex in range(len(selection)): # 3
        # Select current obj
        print("\n=== Object Name:" + selection[parentIndex].name)
        print("\n===Object Index:" + str (parentIndex))
        print ("\n===Object Children:" , selection[parentIndex].children)
        # Get object position
        originalPosition = copy.copy(selection[parentIndex].location)
        # Set object location to new position = center.    
        selection[parentIndex].location = (0,0,0)
        print("\n ********** EXPORTING ********** \n")
        # Set object position to it's original one.
        selection[parentIndex].location = originalPosition
        # Deselect the object
        print(selection[parentIndex].name +  " - DONE\n====================================" )