1

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_mode(type="VERT") 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.ops.object.modifier_add(type='SUBSURF') 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
register_class(...):
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  
tripleee
  • 131
  • 1
  • 7
Kask909
  • 31
  • 7
  • 2
    You can use a python expression or pass a python file, see the manual: https://docs.blender.org/manual/en/latest/advanced/command_line/arguments.html#python-options – brockmann Mar 04 '20 at 18:04
  • Makes sense, finally. You want to export the objects generated by several scripts to fbx or obj files. That's it? – lemon Mar 04 '20 at 18:06
  • 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 – Kask909 Mar 04 '20 at 18:16
  • related https://blender.stackexchange.com/questions/6817/how-to-pass-command-line-arguments-to-a-blender-python-script – lemon Mar 04 '20 at 18:44
  • Oh yes iam little glad lemon thanks ! iam new in python please can you publish the good code from your link because i show several code in the link please please please – Kask909 Mar 04 '20 at 18:51
  • @Kask909, since the question is not reopened... but you should add more detail about what you want: - where are stored your scripts (text blocks or py file)? - do you want one export per script? (please if possible edit your question to provide these informations). The more you add information in your question, the more people will see it and so have chance to be reopened and answered. – lemon Mar 04 '20 at 19:08
  • Also you can provide one significant python script (the idea is to know if it is generating meshes only or add other stuffs, like materials, for instance). – lemon Mar 04 '20 at 19:14
  • Ok thanks my files are py files and are stored to C:\Users\user\Desktop\kask909\1-Modelisation + Extrude Edge + Extrude Face + Rotate Original.py.

    Modelisation + Extrude Edge + Extrude Face + Rotate Original.py is only one py file i give to you. I have more than 6000 in the same folder and i need to convert these py files to fbx or obj simultaneously in commande line. Iam new there and don't know how to send you the py files. the files have materials on meshes.

    Please help

    – Kask909 Mar 04 '20 at 19:21
  • ok, will try to help (but for today will quit soon). Can you edit your question (click on 'edit' below your question)? and add some copy/paste of one of your py file (not too big)? That will help to understand how to take your context into account. Please use @lemon if you comment in response to be sure I'll be ping if you want to talk to me (if not won't be notified). – lemon Mar 04 '20 at 19:27
  • man ok... how things are here in this site... complete your answer, don't replace it. And about ping @lemon, this is for comment not for the question itself... but ok... so you have several files with py script like in the question. Are they all in the same directory, for instance? – lemon Mar 04 '20 at 19:47
  • @ lemon My bro all the files are in the same folder. please help today i would be to glad bro thanks – Kask909 Mar 04 '20 at 20:04
  • @Kask909, won't do today (too late for this evening). But in the principle: if you know about powershell (windows) you can loop over the py file, passing the file names to blender in cli (see ideasman answer linked above). then... but ok... I'am writing things but if you don't know python? Am i right? If yes can not do more here, since the question is not reopened... (comment format does not fit for that). – lemon Mar 04 '20 at 20:12
  • The principle can be: either - looping over the py files (in powershell or dos commande), the use the answer from ideasman linked above) and use it with a blend file that can evaluate a py file and go ahead with fbx export. Or same approach but pass the directory as parameter, loop over its files, evaluate each, export, clean the scene and go to the next file. – lemon Mar 04 '20 at 20:16
  • @lemon Ok i don't program in python but i think with the code python i give to you, you can give me one good code cmd to solved my problem in the morning so. Please write good script for me i would just launch it from command line ! Good day – Kask909 Mar 04 '20 at 20:20
  • This last error is because exportFbx is not found. And that's because you are executing this in the wrong directory. You should execute it in the directory where exportFbx.py is. – lemon Mar 05 '20 at 09:42
  • 1
    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: https://blender.stackexchange.com/help/how-to-ask – brockmann Mar 05 '20 at 09:42
  • @Kask909, I know this may be difficult. But please try to stay in this site "rules". If something to say about what I proposed as answer, write that in the comments below the answer. Thanks. – lemon Mar 05 '20 at 09:45
  • 1
    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. – batFINGER Mar 05 '20 at 09:52

1 Answers1

3

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)
    exec(file.read())

#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
delete_all()

#Run the py file
exec_py_file(filepath)

#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]" 
--background 
--python 
"[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.

lemon
  • 60,295
  • 3
  • 66
  • 136
  • 1
    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. – batFINGER Mar 05 '20 at 09:25
  • @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 – Kask909 Mar 05 '20 at 10:37
  • 4
    @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. – lemon Mar 05 '20 at 10:42
  • @lemon it is working for me but now i want to know how to convert fbx files in commande line to blend. extension thanks – Kask909 Mar 05 '20 at 17:21
  • Hi How to convert fbx to blend files extension because I want to convert my fbx files into vidéo files. I know non How to convert blender scripts to fbx now it stay to convert its to vidéos files So I want to convert its first in fbx and convert to blend files and finally convert its to video files. Any helps to convert my fbx files to blend files or directly convert my fbx to videos files thanks – Kask909 Mar 18 '20 at 18:53
  • 3
    @Kask909 if this has answered your question consider accepting it. – batFINGER Mar 21 '20 at 03:07
  • Can I convert blender scripts to blend file – Kask909 Apr 13 '20 at 16:27