Blender, convert .stl to .obj with prompt commande

Question:

I want to convert an .stl file to .obj, without using blender interface.

To do that, i want to do it in two step :

1 STL to BLEND

2 BLEND to OBJ

Actually BLEND to OBJ work ok with this python code :

import bpy
import sys


argv = sys.argv
argv = argv[argv.index("--") + 1:] # get all args after "--"

obj_out = argv[0]

bpy.ops.export_scene.obj(filepath=obj_out, axis_forward='-Z', axis_up='Y')

and with this batch :

@echo off

rem Put the path of the blender folder
set PATH=C:Program FilesBlender FoundationBlender

set FILENAME=guitar.blend
set TARGET=guitar.obj
rem Launch
blender %FILENAME% --background --python convert_blend_to_obj.py -- %TARGET%


pause

This work perfectly.

The problem is with the STL to BLEND, its not working with this python code :

import bpy
import sys

argv = sys.argv
argv = argv[argv.index("--") + 1:] # get all args after "--"

blend_out = argv[0]

bpy.ops.import_mesh.stl(filepath=blend_out, axis_forward='-Z', axis_up='Y')

And the batch associated :

@echo off

rem Put the path of the blender folder
set PATH=C:Program FilesBlender FoundationBlender

set FILENAME=turbine.stl

set TARGET=turbine.blend

rem Launch
blender %FILENAME% --background --python convert_stl_to_blend.py -- %TARGET%


pause

i got as an error : File format is not supported in file C:..turbine.stl
Blender quit

I think the problem is in my python code, but i don’t know what to change.

I found help for the first script here : https://wiki.blender.org/index.php/Extensions:2.6/Py/Scripts/Import-Export/Wavefront_OBJ

I thank you in advance for the help, maybe there is a better way to that. I don’t want to have to launch blender and do it manually. I only need a prompt commande.

Asked By: foufrix

||

Answers:

i got as an error : File format is not supported in file C:..turbine.stl Blender quit

The error you are getting means that Blender is trying to read first command argument as a .blend file. Just put your script’s arguments after “–” (as it says in the script’s comment), and it should work.

Made some changes to the script to import STL and export an OBJ in one step, to test if it works:

import bpy
import sys

argv = sys.argv
argv = argv[argv.index("--") + 1:] # get all args after "--"

stl_in = argv[0]
obj_out = argv[1]

bpy.ops.import_mesh.stl(filepath=stl_in, axis_forward='-Z', axis_up='Y')
bpy.ops.export_scene.obj(filepath=obj_out, axis_forward='-Z', axis_up='Y')

and launched it as follows:

blender --background --python convert_stl_to_blend.py -- test.stl test.obj
Answered By: railla

I ran this code with the following input on OSX

/Applications/blender-2.77a-OSX_10.6-x86_64/blender.app/Contents/MacOS/blender --background --python convert_stl_to_blend.py -- fileIN.stl FileOut.obj

This code was very helpful, thanks guys!

BTW, I needed this code to do all files in the directory and they were numbers so I used the following code to have blender convert all the files for me:

import bpy
import sys

for x in range(1, 20):
    stl_in = str(x) +'.stl'
    obj_out = str(x) +'.obj'
    bpy.ops.import_mesh.stl(filepath=stl_in, axis_forward='-Z', axis_up='Y')
    bpy.ops.export_scene.obj(filepath=obj_out, axis_forward='-Z', axis_up='Y')
Answered By: CodeGuyRoss
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.