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!
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$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$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$