Raphael ABADIE-MOREAU Raphael ABADIE-MOREAU - 3 months ago 75
Python Question

Blender, convert .stl to .obj with prompt commande

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 Files\Blender Foundation\Blender

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 Files\Blender Foundation\Blender

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.

Answer

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