0
$\begingroup$

I'm trying to make a script that opens the .obj files from a folder one by one and performs a boolean operation based on an .stl file called rail.stl. Then I want this script to transform the .obj file into .stl before closing it and continuing with the next file inside the folder. In order to achieve this, before transforming the .obj into .stl, I want to remove the file rail.stl from the scene. This is what i'm trying and it's making blender crash every time I run it.

[...]

        # Import the "rail.stl"
        print(f"IMPORTING RAILS!")
        bpy.ops.import_mesh.stl(filepath=modifier_file)
        # Catch the modifier object inside modifier_obj
        modifier_obj = bpy.context.selected_objects[0]
        print(f"IMPORT FINISHED!\n")
        
        # Apply boolean modifier
        print(f"APPLYING BOOL MODIF {index}!!")
        for obj in bpy.context.scene.objects:
            if obj != modifier_obj:
                bpy.context.view_layer.objects.active = obj
                bool_mod = obj.modifiers.new("Boolean", "BOOLEAN")
                bool_mod.operation = 'DIFFERENCE'
                bool_mod.object = modifier_obj
                bpy.ops.object.modifier_apply(modifier=bool_mod.name)
                
                objects = bpy.context.scene.objects
                # Loop through all objects and delete the ones that are not active
                for obj in objects:
                    if obj != bpy.context.active_object:
                        bpy.data.objects.remove(obj, do_unlink=True)
        print(f"MODIFICATION FINISHED!!\n")

Then Blender is crashing when it gets to the line bpy.data.objects.remove(obj, do_unlink=True)

Is there any other way of closing the file rail.stl?

Thanks!

$\endgroup$
5
  • 2
    $\begingroup$ Thats most likely because you remove it inside the loop over the scene objects. Keep them in a list and remove them in another loop, after the main loop. $\endgroup$
    – lemon
    Mar 22, 2023 at 14:44
  • $\begingroup$ EDIT: there should be a way of doing it without the last for loop because I just have two models on the scene: the .stl and the .obj file. What's the best way of selecting just the .stl file and deleting it? $\endgroup$
    – Ferjus
    Mar 22, 2023 at 14:57
  • $\begingroup$ modifier_obj is the one to delete, if I understand you well. It is know from the begining. remove it, but after the loop. $\endgroup$
    – lemon
    Mar 22, 2023 at 15:31
  • $\begingroup$ Yeah modifier_obj is the one to delete. What I'm struggling the most is in figuring out what command I should use to remove this file. As i said in the description bpy.data.objects.remove(obj, do_unlink=True) is making Blender crash so maybe there's a different command to delete this. I'm new to Blender AND python so I'm just doing what chatgpt suggest but its answers are not very good on this case... $\endgroup$
    – Ferjus
    Mar 22, 2023 at 15:52
  • $\begingroup$ place bpy.data.objects.remove(modifier_obj, do_unlink=True) after the main loop (and remove the inner loop at the end of the main loop, all the part that is after the line that applies the modifier). $\endgroup$
    – lemon
    Mar 22, 2023 at 15:54

0

You must log in to answer this question.

Browse other questions tagged .