I am trying to make a script that uses a new thread in python to not make blender freeze up.
To start a new thread, I use the python threading module. It seems to work because I can create and delete objects. However, when I try to do booleans, it doesn't work because the context is incorrect. This is my code:
boolean = object1.modifiers.new(type="BOOLEAN", name="something")
boolean.object = object2
boolean.operation = 'UNION'
bpy.context.view_layer.objects.active = bpy.data.objects["one"]
bpy.ops.object.modifier_apply(modifier="something")
I know that this code works when I don't run it in a new thread, but when I try it in a new thread, I get this error:
RuntimeError: Operator bpy.ops.object.modifier_apply.poll() failed, context is incorrect
Any ideas?
I tried the following, but got the same error:
with bpy.context.temp_override( selected_objects=[bpy.data.objects["one"]]):
bpy.ops.object.modifier_apply(modifier="something")
Is there a better way to override the context, or some other work around to make boolean operations work? I am using blender 3.3. Thanks!
object.modifier_applyisobject, notselected_objects. see https://blender.stackexchange.com/a/248275/86891 which means you can't batch-apply a modifier to several objects. You'll have to use a for loop. Alternatively if you don't mind applying all modifiers you can usebpy.ops.object.convert(target='MESH')which supports execution on multiple objects – Gorgious May 30 '23 at 07:59