4
$\begingroup$

I have a scene with multiple objects and I want to export them individually as .stl meshes.

Right now, it is just a cube and a plane, for simplicity. The cube is resting on the plane.

I've tried the following script to loop through the objects in the scene and export them individually as .stls:

cube.select=True
plane.select=True
names = [ob.name for ob in bpy.context.selected_objects]
bpy.ops.object.select_all(action='DESELECT')

for name in names:
    obj = bpy.data.objects[name]
    bpy.context.scene.objects.active = obj
    stl_path = os.path.join('C://Users/deden/Desktop/', name + '.stl')
    bpy.ops.export_mesh.stl(filepath=stl_path)

However, when I do this, both .stl files contain both the cube and the the plane. I am trying to have a separate .stl file for both the cube and the plane.

Can anybody help me with this?

I've seen a couple other stack posts but keep getting the same problem..

$\endgroup$

2 Answers 2

6
$\begingroup$

Set the use_selection property

enter image description here

To export only the selection, set the use_selection property to True. Also select object before and deselect after.

2.79 and prior.

Export all selected objects to individual stl

import bpy
context = bpy.context
obs = context.selected_objects  # makes a "snapshot" copy of property
bpy.ops.object.select_all(action='DESELECT')    
scene = context.scene
for ob in obs:
    scene.objects.active = ob
    ob.select = True
    stl_path = os.path.join('C://Users/deden/Desktop/', name + '.stl')
    bpy.ops.export_mesh.stl(
            filepath=stl_path,
            use_selection=True)
    ob.select = False

>= 2.80

2.8 version. Exports all mesh objects in scene to stl.

import bpy
from pathlib import Path

context = bpy.context
scene = context.scene
viewlayer = context.view_layer


obs = [o for o in scene.objects if o.type == 'MESH']
bpy.ops.object.select_all(action='DESELECT')    

path = Path("/tmp")
for ob in obs:
    viewlayer.objects.active = ob
    ob.select_set(True)
    stl_path = path / f"{ob.name}.stl"
    bpy.ops.export_mesh.stl(
            filepath=str(stl_path),
            use_selection=True)
    ob.select_set(False)
$\endgroup$
1
  • $\begingroup$ thank you..this worked for me, much appreciated $\endgroup$
    – Derek Eden
    Feb 5, 2019 at 15:51
-2
$\begingroup$

The STL exporter doesn't have an option like "selected objects only". So, you have to deal with the exporter code to get this done.
Another idea is to hide other objects to a dedicated inactive scene while exporting.

$\endgroup$
5
  • $\begingroup$ This is not true. The stl exporter does in fact have this option. $\endgroup$ Feb 23, 2020 at 2:40
  • $\begingroup$ @Justace Clutter, This is true indeed. I opened my blender 2.76b 64 bit, then stated STL exporter, and it still doesn't have the option. So, your statement is wrong. $\endgroup$
    – Mechanic
    Feb 29, 2020 at 20:18
  • $\begingroup$ well the solution did work and I was able to export individual objects $\endgroup$
    – Derek Eden
    Mar 1, 2020 at 0:50
  • $\begingroup$ @mechanic I am not sure when they introduced the option. But it is there in more recent versions. I am pretty sure it was there in 2.79 and for sure in 2.8. I know because I just used it. :) $\endgroup$ Mar 2, 2020 at 1:45
  • $\begingroup$ @Justace Clutter I see it's there in some versions. And I like this fact. I just shown that statements about the truth are not so absolute. They always fit some conditions. Note that the thread has no version information, so any statement, true or false, becomes true. :-) $\endgroup$
    – Mechanic
    Mar 16, 2020 at 6:58

You must log in to answer this question.

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