0

So i am not sure what happened here, but the story Is that I finished my script and it was working fine in UI and now I am running it using --background but something is broken now with deleting objects?

Using version 2.9 btw

script:

 print(objectsToDelete)
  for objectToDelete in objectsToDelete:        
    try:
        if objectToDelete.type == 'MESH':
            bpy.data.objects[objectToDelete.name].select_set(True)
            bpy.ops.object.delete()
    except: 
        print("")   

output:

[bpy.data.objects['TR5_49'], bpy.data.objects['TR5_50'], bpy.data.objects['TR5_51'], bpy.data.objects['TR5_52'], bpy.data.objects['TR5_53'], bpy.data.objects['TR5_54'], bpy.data.objects['TR5_55'], bpy.data.objects['TR5_56'], bpy.data.objects['TR5_57'], bpy.data.objects['TR5_58'], bpy.data.objects['TR5_59'], bpy.data.objects['TR5_73'], bpy.data.objects['TR5_74'], bpy.data.objects['TR5_75'], bpy.data.objects['TR5_76'], bpy.data.objects['TR5_77'], bpy.data.objects['TR5_78'], bpy.data.objects['TR5_79'], bpy.data.objects['TR5_80'], bpy.data.objects['TR5_81'], bpy.data.objects['TR5_82'], bpy.data.objects['TR5_83'], bpy.data.objects['TR5_61'], bpy.data.objects['TR5_62'], bpy.data.objects['TR5_63'], bpy.data.objects['TR5_64'], bpy.data.objects['TR5_65'], bpy.data.objects['TR5_66'], bpy.data.objects['TR5_67'], bpy.data.objects['TR5_68'], bpy.data.objects['TR5_69'], bpy.data.objects['TR5_70'], bpy.data.objects['TR5_71'], bpy.data.objects['TR5_85'], bpy.data.objects['TR5_86'], bpy.data.objects['TR5_87'], bpy.data.objects['TR5_88'], bpy.data.objects['TR5_89'], bpy.data.objects['TR5_90'], bpy.data.objects['TR5_91'], bpy.data.objects['TR5_92'], bpy.data.objects['TR5_93'], bpy.data.objects['TR5_94'], bpy.data.objects['TR5_95']]
Info: Deleted 221 object(s)

Then it just goes ahead and deletes all objects in the scene!?

Length of the array is 44, so is this supposed to work differently when using --background? It's also doing this in the GUI now... which is really weird...

EDIT - Ehm, checking the selected objects crashes the program...

script:

if len(objectsToDelete) == len(bpy.context.selected_objects):
        print("all objects selected")
        bpy.ops.object.delete()

output:

Error   : EXCEPTION_ACCESS_VIOLATION
Address : 0x00007FF67F5AAC78
Module  : blender.exe
Thread  : 00000c40
Writing: C:\Users\Recreate\AppData\Local\Temp\blender.crash.txt

Weird all of these seem to crash for me now...

bpy.data.objects.remove(objectToDelete)
#bpy.data.meshes.remove(objectToDelete.data)
#bpy.data.objects[objectToDelete.name].select_set(True)

efsjouw
  • 1
  • 1
  • 1
    Using an operator to remove objects is a horrible way to do it... and looping it over and over in an try clause even more-so. The operator deletes all selected objects. Instead Use bpy.data.objects.remove(ob_to_delete) https://blender.stackexchange.com/questions/27234/python-how-to-completely-remove-an-object?rq=1 – batFINGER Sep 21 '20 at 12:12
  • Alright my bad it is now crashing on some other line further below, bpy.data.objects.remove(objectToDelete) does appear to work. – efsjouw Sep 21 '20 at 12:30
  • Please post your full script not just snippets. If it crashes you're likely doing something that is not permitted by the Python API see the Gotchas page of the API docs. – Robert Gützkow Sep 21 '20 at 12:35
  • 1
    Often wonder why if running in bg mode don't simply save a blend with those objects deleted. Another thing if you know an object via reference as for example ob then using bpy.data.objects[ob.name] is longwindedly getting a reference to ob which you have in the first place. – batFINGER Sep 21 '20 at 12:36
  • 1
    ... re posting full script, at the very least a minimal runable example. One of those gotchas is often keeping a reference to an object since removed. Recommend make a list of objects to remove that contains only obs to remove and while(remove_list): bpy.data.objects.remove(remove_list.pop()) which will ensure list no longer contains ref to deleted object. – batFINGER Sep 21 '20 at 12:44

0 Answers0