How can I export my blender model as a python script
So exporting the .blend to a .py file is impossible (see here), but instead of exporting, you can run a python script on a .blend file externally through the command line.

For this example, lets assume we have a directory that contains a folder of 100 .blend files that each have an object located at the origin. For our needs, we would like to raise each object by one unit on the Z axis, and change it's name to the name of the .blend file. We will use a python script to modify these files.

project_files
|   edit_blends.py
|   modify.py
|
|___blend_files
    |   lid.blend
    |   cup.blend
    |   pencil.blend
    |   ...


In the edit_blends.py file, we have code that utilizes the Python library to iterate over each of the .blend files. (edit_blends.py) For each of the files, we use the subprocess module of Python to call Blender from the command line with certain arguments that will run our script (modify.py).

Here is edit_blends.py

import os
import subprocess

blend_files_path = 'blend_files'

for blend_file in os.listdir(blend_files_path):
    if blend_file.endswith('.blend'): # Exclude .blend1 .blend2 files
        file = os.path.join(blend_files_path, blend_file)
        # Run Blender in the background
        subprocess.run(['blender', '-b', file, '-P', 'modify.py'])


Here is modify.py. This file is what is run in the Blender process that is called.

import bpy

blend_file = bpy.data.filepath

for object in bpy.data.objects:
    object.location.z += 1

    object.name = bpy.path.display_name_from_filepath(blend_file)

# SAVE THE FILE
bpy.ops.wm.save_as_mainfile()


It is very important to save the file, otherwise all changes will be lost.

The line that calls Blender subprocess.run(['blender', '-b', file, '-P', 'modify.py']) runs Blender, with the arguments given in a list.


-b &lt;.blend>: run the file in the background
-P &lt;.py>: run the given Python file


For more information on the command line, read this. Of course, you can do almost anything to the .blend file externally through Python (some things are restricted like OpenGL renders because they require the GUI to be open)

If you are on Windows, you need to add Blender to the PATH or run the command with the path to Blender.