I've several Python scripts (around 6000 scripts). Each of them generates 3D objects.

I want to find a way to export these generated objects to either fbx or obj files.

Is there a way to do it using Blender in command line?

Py file example:

import bpy

bpy.ops.mesh.primitive_plane_add(enter_editmode=False, location=(0, 0, 0)) 
bpy.ops.object.mode_set(mode = 'OBJECT') 
obj = bpy.context.active_object 
bpy.ops.object.mode_set(mode = 'EDIT') 
bpy.ops.mesh.select_all(action = 'DESELECT') 
bpy.ops.object.mode_set(mode = 'OBJECT') 

obj.data.vertices[0].select = True 
obj.data.vertices[1].select = True 
obj.data.vertices[2].select = True 
obj.data.vertices[3].select = True 
bpy.ops.object.mode_set(mode = 'EDIT')                              
bpy.ops.mesh.extrude_region_move(MESH_OT_extrude_region={"use_normal_flip":False, "mirror":False}, TRANSFORM_OT_translate={"value":(-0.220266, 0.10617, 0.490731), "orient_type":'GLOBAL', "orient_matrix":((5, 2, 3), (4, 1, 9), (6, 8, 7)), "orient_matrix_type":'GLOBAL', "constraint_axis":(False, False, False), "mirror":False, "use_proportional_edit":False, "proportional_edit_falloff":'SMOOTH', "proportional_size":1, "use_proportional_connected":False, "use_proportional_projected":False, "snap":False, "snap_target":'CLOSEST', "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "gpencil_strokes":False, "cursor_transform":False, "texture_space":False, "remove_on_cancel":False, "release_confirm":False, "use_accurate":False})
bpy.ops.transform.rotate(value= 10, orient_axis='X', orient_type='GLOBAL', orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)), orient_matrix_type='GLOBAL', constraint_axis=(True, False, False), mirror=True, use_proportional_edit=False, proportional_edit_falloff='SMOOTH', proportional_size=1, use_proportional_connected=False, use_proportional_projected=False, release_confirm=True)
bpy.context.object.modifiers["Subdivision"].levels = 8

When I launch the script (("<your path to blender>\blender.exe" --background --python exportFbx.py -- "<your py file name>" "<your output directory>" )) I get this error on the command line:

C:\WINDOWS\system32>"C:\Program Files\Blender Foundation\Blender 2.81\blender.exe" --background --python exportFbx.py -- "<Essai.py>" "C:\Users\user\Desktop\Bank2Son Zone Originale II\Command line"
Blender 2.81 (sub 16) (hash 26bd5ebd42e3 built 2019-11-20 16:33:00)
Read prefs: C:\Users\user\AppData\Roaming\Blender Foundation\Blender\2.81\config\userpref.blend
AL lib: (EE) UpdateDeviceParams: Failed to set 48000hz, got 44100hz instead
found bundled python: C:\Program Files\Blender Foundation\Blender 2.81\2.81\python
Warning: 'BatEx_PT_Panel' doesn't have upper case alpha-numeric prefix
Warning: 'BatEx_PT_Panel' doesn't have upper case alpha-numeric prefix
OSError: Python file "C:\WINDOWS\system32\exportFbx.py" could not be opened: No such file or directory Blender quit  
  • 2
    $\begingroup$ You can use a python expression or pass a python file, see the manual: docs.blender.org/manual/en/latest/advanced/command_line/… $\endgroup$
    – brockmann
    Commented Mar 4, 2020 at 18:04
  • $\begingroup$ Makes sense, finally. You want to export the objects generated by several scripts to fbx or obj files. That's it? $\endgroup$
    – lemon
    Commented Mar 4, 2020 at 18:06
  • $\begingroup$ Yes I would want to export in command line the objects generated by several scripts to fbx or obj files without openning blender ui. Thanks $\endgroup$
    – Kask909
    Commented Mar 4, 2020 at 18:16
  • 1
    $\begingroup$ Hi. Please take the time formatting your post/question properly. Also please don't just remove the content posted before, instead please add necessary content. Read: blender.stackexchange.com/help/how-to-ask $\endgroup$
    – brockmann
    Commented Mar 5, 2020 at 9:42
  • 1
    $\begingroup$ Put the script into, for example Desktop folder. Run the command from the desktop folder. Would suggest don't run anything from your windows system folder unless you know what you are doing. $\endgroup$
    – batFINGER
    Commented Mar 5, 2020 at 9:52

1 Answer 1


The following script will allow to execute Blender in command line as so:

"<your path to blender>\blender.exe" --background --python exportFbx.py -- "<your py file name>" "<your output directory>"

Thanks to this answer and considering:

  • "exportFbx.py" is the file where the script below is saved into
  • the command line is run where the script is
  • "your py file name" is the complete name of one of your object generating .py scripts
  • "your output directory" is the directory where to store the exported result

The script, in short:

  • Get the arguments
  • Compose the output file name
  • clear the scene
  • execute your .py script
  • exports

Each part is commented below. Save it as "exportFbx.py".

import bpy
import sys
import ntpath

#Extracts file name from file path
def get_filename(path):
    path, name = ntpath.split(path)
    return name or ntpath.basename(path)

#Clears the scene
def delete_all():
    bpy.ops.object.delete({"selected_objects":[o for o in bpy.context.scene.objects]} )

#Executes a py file
def exec_py_file(filepath):
    file = open(filepath)

#Gets command line arguments
def get_args():
    argv = sys.argv
    argv = argv[argv.index("--") + 1:] 
    return argv[0], argv[1]

#Obtain arguments
filepath, output_directory = get_args()

#Get the file name
filename = get_filename(filepath)

#Compose the fbx file name
output_filepath = ntpath.join( output_directory, filename + ".fbx")

#Clean the scene

#Run the py file

#Export to fbx
bpy.ops.export_scene.fbx(filepath=output_filepath, check_existing=False, use_selection=False, use_mesh_modifiers=True)

Now you'll have to iterate over your py file using your operating system commands in order to apply the command line above to all the .py file.

In Windows Powershell, that can be something like (not tested):

$files = Get-ChildItem "<your path to py files\*.py"
foreach ($f in $files){
    $filename = $f.FullName
    "<your path to blender>\blender.exe" --background --python exportFbx.py -- "$filename" "<your output directory>"

If you run:

"C:\Program Files\Blender Foundation\Blender 2.81\blender.exe" --background --python exportFbx.py -- "<Essai.py>" "C:\Users\user\Desktop\Bank2Son Zone Originale II\Command line"

That won't work. You need to have:

"[The path to blender.exe]" 
"[The path to exportFbx.py or just exportFbx.py if you run that in its directory]"
"[The path to one of your py file]"
"[The path to the output directory]"

Surely all what is inside "[]" above needs to be replaced by the good things as they are in your environment. And all that in one single line (I've separated things above for the explanation) with a space between each part.

  • 1
    $\begingroup$ Also recommend checking out the Text Editor > templates > Python > "Background Job" and "External Script Stub" Also consider pathlib over ntpath & cheers, Made the bounty with 8mins to spare. $\endgroup$
    – batFINGER
    Commented Mar 5, 2020 at 9:25
  • $\begingroup$ @lemon you are my brother thanks it working perfectly gracias !!! Now can the new file name can be incremented by 001 so we would have file 001.fbx file 002.fbx ect thanks agains bro $\endgroup$
    – Kask909
    Commented Mar 5, 2020 at 10:37
  • 4
    $\begingroup$ @Kask909, I won't. All the principles are given here. A more tailored answer is not the way I can give help: you need to work a bit by yourself from that. $\endgroup$
    – lemon
    Commented Mar 5, 2020 at 10:42
  • $\begingroup$ @lemon it is working for me but now i want to know how to convert fbx files in commande line to blend. extension thanks $\endgroup$
    – Kask909
    Commented Mar 5, 2020 at 17:21
  • 3
    $\begingroup$ @Kask909 if this has answered your question consider accepting it. $\endgroup$
    – batFINGER
    Commented Mar 21, 2020 at 3:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .