Hello!
I am a student currently researching the possibility to use python scripts for automating the optimization of STEP files in Blender. My scripting knowledge and knowledge about python is quite limited unfortunately.
So currently I am trying to write a script that detects duplicate objects in the scene. The scene has a lot of objects (around 5000) and the object’s names are very complex. This makes it difficult to compare their names and in my experience so far, I have to pay attention to not make the script too slow.
My thought is to compare the objects based on their location, maybe bounding box? And then finally compare them by the first few characters of their names, however I am quite lost on how to approach this.
bpy.ops.object.select_all(action='SELECT')
ob = bpy.context.active_object
selected = bpy.context.selected_objects
#First check to filter out the most basic duplicates
for obj1 in selected:
for obj2 in selected:
if obj1.type == 'MESH' and obj2.type == 'MESH':
if obj1 == obj2:
print (obj1.name, obj2.name)
continue
#Center of bounding box
center1 = sum((Vector(b) for b in obj1.bound_box), Vector())
center1 /= 8
center2 = sum((Vector(b) for b in obj2.bound_box), Vector())
center2 /= 8
#filter to compare the vertices/polycount
if len(obj1.data.vertices) == len(obj2.data.vertices):
print ("Same count: ", obj1.name, ", ", obj2.name)
continue
if center1 == center2:
bb_1 = [bbox_co[:] for bbox_co in obj1.bound_box[:]]
bb_2 = [bbox_co[:] for bbox_co in obj2.bound_box[:]]
print (bb_1, obj1.name, bb_2, obj2.name)
if bb_1 == bb_2:
print ("TEST TEST TEST")
continue
if obj1.name == obj2.name:
print ("Duplicate found!")
print (obj1.name, obj2.name)
The code above is a rough attempt and it does not work sadly. It also feels wrong to me but I feel a little lost on where to improve this code and what steps need to be taken to accurately detect duplicate objects.
Do you have any tips or recommendations?
Thanks in advance!