3
$\begingroup$

I want to convert .obj to .stl in blender via python script.Here is my code.

import bpy
obj_in = '1b3c286bce219ddb7135fc51795b4038.obj'
stl_out = 'current.stl'
import_obj = bpy.ops.import_scene.obj(filepath=obj_in)
print(bpy.context.selected_objects)
for object in bpy.context.selected_objects:
    bpy.ops.object.select_all(action='DESELECT')

    object.select = True

    bpy.ops.export_mesh.stl(filepath=stl_out, ascii=False)

I run the script in ubuntu16.04 shell, and the error was:

(  0.0000 sec |   0.0000 sec) Importing OBJ 
'1b3c286bce219ddb7135fc51795b4038.obj'...

  (  0.0001 sec |   0.0000 sec) Parsing OBJ file...

(  0.0131 sec |   0.0130 sec) Done, loading materials and images...
ProgressMaterial not found MTL: 'model.mtl'

(  0.0137 sec |   0.0136 sec) Done, building geometries (verts:621 faces:1902 materials: 153 smoothgroups:1) ...
(  0.0331 sec |   0.0330 sec) Done.

  (  0.0333 sec |   0.0332 sec) Finished importing: 
'1b3c286bce219ddb7135fc51795b4038.obj'
Progress: 100.00%

[bpy.data.objects['mesh5_mesh5-geometry'], bpy.data.objects['mesh2_mesh2 geometry'], bpy.data.objects['mesh4.002_mesh4-geometry'], bpy.data.objects['mesh4.001_mesh4-geometry'], bpy.data.objects['mesh1_mesh1 geometry'], bpy.data.objects['mesh3.002_mesh3-geometry'], bpy.data.objects['mesh3.001_mesh3-geometry']]

Error: Object does not have geometry data
Error: Object does not have geometry data
Error: Object does not have geometry data
Error: Object does not have geometry data
Error: Object does not have geometry data
Error: Object does not have geometry data
Error: Object does not have geometry data
Error: Object does not have geometry data
Error: Object does not have geometry data
Error: Object does not have geometry data
Error: Object does not have geometry data
Error: Object does not have geometry data
Error: Object does not have geometry data
Error: Object does not have geometry data

Blender quit

Is there any correct methods to convert .obj to .stl in blender via python scripts?

$\endgroup$
  • $\begingroup$ The obj file cames from the 3D shapeNet. $\endgroup$ – Zemin Li Jun 14 '18 at 2:08
  • $\begingroup$ Do you get the error if you do this manually via the UI? In your export loop, as well as selecting the object (ob.select = True) , also set it to context with context.scene.objects.active = ob before calling the operator. Strongly suggest not using object as a variable name as it is a basic python keyword. $\endgroup$ – batFINGER Jun 14 '18 at 3:12
  • $\begingroup$ without using python script. import the .obj file into blender and export the file as .stl done. $\endgroup$ – Sujith Godavarthi Jun 14 '18 at 5:37
  • $\begingroup$ @ZeminLi I've tested your script and it works well and the stl is exported successfully with no errors. Could you upload your blend files here blend-exchange.giantcowfilms.com so I can try to help you? $\endgroup$ – Tak Jun 19 '18 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.