7
$\begingroup$

I have as scene with 45 objects, I am trying to write a script that will export all of the objects into separate STL files. Any ideas?

Here is a broken script I have right now:

import bpy
import os

# get the current path and make a new folder for the exported meshes
path = bpy.path.abspath('//1')
os.makedirs(path)

for object in bpy.context.selected_objects:

    # deselect all meshes
    bpy.ops.object.select_all(action='DESELECT')

    # select the object
    object.select = True

    # export object with its name as file name
    bpy.ops.export_mesh.stl(filepath=str((path + object.name + '.stl')),   use_selection=True)

I am getting a "Python script fail, look in the console for now..." error. PS. I'm using a mac.

Thank you for your help!!!

$\endgroup$

3 Answers 3

6
$\begingroup$

I actually found 3 issues with the original script:

  • Your path creation line did not check to see if the path already existed
  • Your stl export line used an incorrect parameter ("use_selection=true")
  • Your path variable did not include the ending backslash so it was concatenating the stl folder name and object name

Here's my version of the working script:

import bpy
import os

# get the current path and make a new folder for the exported meshes
path = bpy.path.abspath('//stlexport/')

if not os.path.exists(path):
    os.makedirs(path)

for object in bpy.context.selected_objects:

    # deselect all meshes
    bpy.ops.object.select_all(action='DESELECT')

    # select the object
    object.select = True

    # export object with its name as file name
    fPath = str((path + object.name + '.stl'))

    #bpy.context.active_object = object
    bpy.ops.export_mesh.stl(filepath=fPath)
$\endgroup$
3
  • $\begingroup$ Hi Todd! Thanks for helping me out, I copied the script from somewhere else and only changed the bpy.ops.export_mesh.stl part ^^ $\endgroup$ Jul 10, 2015 at 16:28
  • $\begingroup$ You should also note that I altered the path variable to test the script, so you may want to change that part. $\endgroup$ Jul 10, 2015 at 20:21
  • $\begingroup$ I get AttributeError: 'Object' object has no attribute 'select' with this script from the line object.select = True when I try to run it with Blender 2.93. $\endgroup$ Jun 26, 2021 at 9:04
4
$\begingroup$

It is worth noting that a script is not required for batch exporting of STL files; the Export STL dialogue has a Batch Mode setting that can be set to Object in order to export each part as a single STL file. To preserve part names, leave the File Name field empty when exporting this way.

Regardless, the question specifically addresses a Python script.


There is no need to loop through selected objects. Additional, unnecessary use of bpy.ops should be avoided.

The following script will export all of the selected objects into a specified directory (relative or absolute path):

import bpy
import os

# Define Directory
dir = bpy.path.abspath('//subdirectory/')

# Create Directory (If Necessary)
if not os.path.exists(dir): os.makedirs(dir)

# Export STLs
bpy.ops.export_mesh.stl(filepath = dir, batch_mode = 'OBJECT')

while a single line of code will suffice if the directory is known to exist (absolute path):

import bpy

# Export STLs
bpy.ops.export_mesh.stl(filepath = 'C:/stls/', batch_mode = 'OBJECT')

Note that these scripts will use the exporter's default settings (which can be changed) unless you add arguments to the operator.

$\endgroup$
1
$\begingroup$

use_selection=True , is not a keyword argument of export_mesh.stl

- stl()
bpy.ops.export_mesh.stl(
    filepath="", 
    check_existing=True, 
    axis_forward='Y', 
    axis_up='Z', 
    filter_glob="*.stl", 
    global_scale=1, 
    use_scene_unit=False, 
    ascii=False, 
    use_mesh_modifiers=True)

it appears to work on the Active object, so you might use something like this:

import bpy
import os

# get the current path and make a new folder for the exported meshes
destination_path = '/home/zeffii/Desktop/temp_stl/'

# if path doesn't exist, create, else skip that step
if not os.path.exists(destination_path):
    os.makedirs(destination_path)

# gather names
names = [o.name for o in bpy.context.selected_objects]

# deselect all
bpy.ops.object.select_all(action='DESELECT')

# iterate over named objects
for name in names:

    # select the object
    obj = bpy.data.objects[name]
    bpy.context.scene.objects.active = obj

    # export object with its name as file name
    stl_path = os.path.join(destination_path, name + '.stl')
    bpy.ops.export_mesh.stl(filepath=stl_path)
$\endgroup$
0

You must log in to answer this question.

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