0
$\begingroup$

So currently I want to export multipe combinations of a design in stl format in blender 4.2. There are five parts and a unchanging main body, each with a few variants. I want to export all possible combinations of variations. here's the code I asked copilot to write but it keeps giving me errors, and I'm not sure what's the issues

import bpy
import itertools

# Define the objects for each variable, including an empty string for no hands
hands = ['', 'hand1', 'hand2']
barrels = ['basic', 'sniper']
scopes = ['','scope1', 'scopeflip']
stocks = ['','stock']
mags = ['boxmag', 'drummag','mag']

# Define the object that should always be present
base_object = 'Basic'

# Create all possible combinations
combinations = list(itertools.product(hands, barrels, scopes, stocks, mags))

# Export each combination as an STL file
for combo in combinations:
    # Deselect all objects
    bpy.ops.object.select_all(action='DESELECT')
    
    # Select the base object
    bpy.data.objects[base_object].select_set(True)
    
    # Select the objects in the current combination
    for obj_name in combo:
        if obj_name:  # Only select if obj_name is not an empty string
            bpy.data.objects[obj_name].select_set(True)
    
    # Join the selected objects into one
    bpy.ops.object.join()

    # Define export path
    export_filename = f"{base_object}_{'_'.join([obj for obj in combo if obj])}.stl"
    export_path = f"C:/D_Drive/3dprint/Export tested/{export_filename}"

    # Export the joined object as an STL file
    bpy.ops.export_mesh.stl(filepath=export_path, use_selection=True)
    
    # Separate the joined object back into individual objects
    bpy.ops.object.mode_set(mode='EDIT')
    bpy.ops.mesh.separate(type='LOOSE')
    bpy.ops.object.mode_set(mode='OBJECT')
    
    # Deselect all objects again
    bpy.ops.object.select_all(action='DESELECT')

this is the error code given.

Python: Traceback (most recent call last):
  File "C:\D_Drive\3dprint\photo booth.blend\Text", line 38, in <module>
  File "C:\Program Files (x86)\Steam\steamapps\common\Blender\4.2\scripts\modules\bpy\ops.py", line 109, in __call__
    ret = _op_call(self.idname_py(), kw)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: Calling operator "bpy.ops.export_mesh.stl" error, could not be found
$\endgroup$
1
  • 1
    $\begingroup$ Learn Python and this won't happen. The purpose of this website is for human beings asking for help with their problems, not your Plan B for when your autocorrect robot writes bad code that you don't understand. $\endgroup$
    – Jakemoyo
    Commented Aug 9 at 1:44

1 Answer 1

0
$\begingroup$

The error about bpy.ops.export_mesh.stl not being found is that Copilot has not been updated to understand the changes in Blender 4.2. Try Blender 4.1 and it will probably work.

The error is because in Blender 4.2 Python STL exporter/importer add-on has been marked as legacy. Users of bpy.ops.export_mesh.stl and bpy.ops.import_mesh.stl are advised to switch to the new STL exporter/importer functions bpy.ops.wm.stl_export and bpy.ops.wm.stl_import. You can read more about the Import & Export release notes.

Here are the new parameters you must use.

$\endgroup$

You must log in to answer this question.

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