1
$\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$
0
$\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.

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
$\endgroup$
  • $\begingroup$ thank you..this worked for me, much appreciated $\endgroup$ – Derek Eden Feb 5 at 15:51
0
$\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$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.