-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlink_objdata_same_models.py
47 lines (35 loc) · 1.44 KB
/
link_objdata_same_models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import bpy
mesh_objs = [obj for obj in bpy.context.scene.objects if obj.type == 'MESH']
print('Found ' + str(len(mesh_objs)) + ' mesh objects')
print('Grouping unique mesh data...')
# Create a dictionary to store objects with matching mesh data
mesh_dict = {}
# Iterate through all mesh objects and group them by mesh data
for obj in mesh_objs:
mesh_data = obj.data
verts = [v.co for v in mesh_data.vertices]
# skip planes (probably decals)
if len(verts) <= 4:
continue
verts_key = tuple([(round(v.x, 4), round(v.y, 4), round(v.z, 4)) for v in verts])
poly_key = tuple([(f.vertices[0], f.vertices[1], f.vertices[2]) for f in mesh_data.polygons])
mesh_key = (len(verts), verts_key, poly_key)
if mesh_key not in mesh_dict:
mesh_dict[mesh_key] = []
mesh_dict[mesh_key].append(obj)
print(f'Found {len(mesh_dict)} unique mesh data blocks')
already_linked = 0
total_linked = 0
# Link object data for objects with matching mesh data
for key, objs in mesh_dict.items():
if len(objs) > 1:
for i in range(len(objs) - 1):
if objs[i+1].data == objs[0].data:
already_linked += 1
continue
objs[i+1].data = objs[0].data
print('Linked ' + objs[0].name + ' with ' + objs[i+1].name)
total_linked += 1
print(f'Successfully linked {total_linked} objects')
if already_linked > 0:
print(f'Info: {already_linked} objects were already linked')