import bpy
import sys
#USAGE: blender --background --python "full_path_to_script.py" -- "full_path_to_inputfile.stl"
argv = sys.argv
argv = argv[argv.index("--") + 1:]  # get all args after "--"
if (len(argv) != 1):
    print('USAGE: blender --background --python "full_path_to_script.py" -- "full_path_to_inputfile.stl"')
else:
    outpath=argv[0].replace(".stl",".post.stl")
    print('outpath: ' + outpath)
    bpy.ops.wm.read_factory_settings(use_empty=True)#This starts with an empty scene
    bpy.ops.import_mesh.stl(filepath=argv[0])
    bpy.ops.object.mode_set(mode='EDIT')
    bpy.ops.mesh.select_all(action='DESELECT')
    bpy.ops.mesh.edges_select_sharp(sharpness=1.57)
    bpy.ops.mesh.select_more(use_face_step=True)
    bpy.ops.mesh.vertices_smooth(factor=1, repeat=10, xaxis=True, yaxis=True, zaxis=True)
    bpy.ops.mesh.select_all(action='SELECT')
    bpy.ops.mesh.vertices_smooth(factor=1, repeat=1, xaxis=True, yaxis=True, zaxis=True)
    bpy.ops.export_mesh.stl(filepath=outpath, global_scale=1.0, use_scene_unit=False, ascii=False, use_mesh_modifiers=True)
    bpy.ops.wm.quit_blender() 



