Crashing while multiple importing .mhx and saving as .blend file through command line

Hello,

I have written a simple script which imports .mhx files and then save each file as .blend files from a specific directory.
The script runs smoothly through blender but when I try to run it outside of blender through command line (terminal) it crashes, because of low memory.

Is there any suggestion in order to make it work outside of blender?


import bpy
import os
import glob

k = 0
# Make this path absolute
path = "./EXPORTED_HUMANS"
for inmhx in glob.glob( os.path.join(path, '*.mhx') ):
    print ("current file is: ")  
    print ( inmhx )
    inmhx = inmhx.replace(path,'')
    #delete the lamp and the camera      
    bpy.ops.object.select_name(name="Lamp", extend = False)
    bpy.ops.object.delete()
    bpy.ops.object.select_name(name="Camera", extend = False)
    bpy.ops.object.delete()
    #import the mhx file
    bpy.ops.import_scene.makehuman_mhx(filepath = path + inmhx,
                               scale = 5,
                               bver  = "Graphicall",
                               enforce = True,
                               mesh= True,
                               proxy = True,
                               armature = True,
                               replace = True,
                               cage = False,
                               clothes = True,
                               face = True,
                               shape = True,
                               symm = False,
                               diamond = False,  
                               bend = False)

    # save the name                   
    inmhx = inmhx.replace(path + '/','')
    inmhx = inmhx.replace('.obj','')

    bpy.ops.wm.save_mainfile(filepath= path + inmhx, 
                             check_existing=True, 
                             filter_blender=True, 
                             filter_image=False, 
                             filter_movie=False, 
                             filter_python=False, 
                             filter_font=False, 
                             filter_sound=False, 
                             filter_text=False, 
                             filter_btx=False, 
                             filter_collada=False, 
                             filter_folder=True, 
                             filemode=8, 
                             compress=False, 
                             relative_remap=False)
    
    #delete the model in order to import the next one      
    bpy.ops.object.select_all(action = 'TOGGLE')
    bpy.ops.object.select_all(action = 'TOGGLE')
    bpy.ops.object.delete()
    bpy.ops.scene.delete() 
    bpy.ops.scene.new(type='NEW')

    # in array a is saved the name of the each item that is imported
    ak = inmhx
    print ("inmhx = ")
    print (ak)
    k += 1
    print (k)


Thanks in advance
Mitsaki

if i understand correctly you are trying to run a blender script outside blender, it’s this correct?

Yeap!!! Well, I can run a script outside of blender.
But this specific script makes the blender to crash.