-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreexport_clean.py
More file actions
111 lines (95 loc) · 3.95 KB
/
Copy pathreexport_clean.py
File metadata and controls
111 lines (95 loc) · 3.95 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import sys
sys.path.insert(0, r"C:\Users\KRISH\AppData\Roaming\Python\Python313\site-packages")
import bpy, mathutils, statistics
bpy.ops.wm.open_mainfile(filepath=r"c:\Users\KRISH\OneDrive\Desktop\AeroTwin AI\jetengine.blend")
# Apply all transforms
bpy.ops.object.select_all(action='DESELECT')
for obj in bpy.data.objects:
if obj.type == 'MESH':
obj.select_set(True)
bpy.context.view_layer.objects.active = [o for o in bpy.data.objects if o.type=='MESH'][0]
bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
# Find the expected engine extent from object dimensions (ignore outliers)
dims = []
for obj in bpy.data.objects:
if obj.type == 'MESH':
dims.append(max(obj.dimensions))
median_dim = statistics.median(dims)
print(f"Median object max-dim: {median_dim:.2f}")
# Collect all vertex world positions, filtering out vertices > 10x median from origin
all_x, all_y, all_z = [], [], []
threshold = median_dim * 10
removed = 0
for obj in bpy.data.objects:
if obj.type != 'MESH':
continue
for v in obj.data.vertices:
wv = obj.matrix_world @ v.co
if abs(wv.x) < threshold and abs(wv.y) < threshold and abs(wv.z) < threshold:
all_x.append(wv.x)
all_y.append(wv.y)
all_z.append(wv.z)
else:
removed += 1
print(f"Filtered out {removed} stray vertices beyond threshold {threshold:.1f}")
min_x, max_x = min(all_x), max(all_x)
min_y, max_y = min(all_y), max(all_y)
min_z, max_z = min(all_z), max(all_z)
cx = (min_x+max_x)/2; cy = (min_y+max_y)/2; cz = (min_z+max_z)/2
sx = max_x-min_x; sy = max_y-min_y; sz = max_z-min_z
print(f"Clean BBox: X={min_x:.2f}..{max_x:.2f} Y={min_y:.2f}..{max_y:.2f} Z={min_z:.2f}..{max_z:.2f}")
print(f"Center: ({cx:.2f}, {cy:.2f}, {cz:.2f}) MaxDim: {max(sx,sy,sz):.2f}")
# Now delete the stray vertices by entering edit mode on each mesh
bpy.ops.object.mode_set(mode='OBJECT')
for obj in bpy.data.objects:
if obj.type != 'MESH':
continue
bpy.context.view_layer.objects.active = obj
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.object.mode_set(mode='OBJECT')
stray_found = False
for v in obj.data.vertices:
wv = obj.matrix_world @ v.co
if abs(wv.x) >= threshold or abs(wv.y) >= threshold or abs(wv.z) >= threshold:
v.select = True
stray_found = True
if stray_found:
bpy.context.view_layer.objects.active = obj
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.delete(type='VERT')
bpy.ops.object.mode_set(mode='OBJECT')
print(f" Deleted stray verts from: {obj.name}")
# Center all objects at origin
offset = mathutils.Vector((-cx, -cy, -cz))
for obj in bpy.data.objects:
if obj.type == 'MESH':
obj.location += offset
bpy.ops.object.select_all(action='DESELECT')
for obj in bpy.data.objects:
if obj.type == 'MESH':
obj.select_set(True)
bpy.ops.object.transform_apply(location=True, rotation=False, scale=False)
# Final verification
all_x2, all_y2, all_z2 = [], [], []
for obj in bpy.data.objects:
if obj.type != 'MESH':
continue
for v in obj.data.vertices:
wv = obj.matrix_world @ v.co
all_x2.append(wv.x); all_y2.append(wv.y); all_z2.append(wv.z)
print(f"\nFinal BBox: X={min(all_x2):.2f}..{max(all_x2):.2f} Y={min(all_y2):.2f}..{max(all_y2):.2f} Z={min(all_z2):.2f}..{max(all_z2):.2f}")
maxDim = max(max(all_x2)-min(all_x2), max(all_y2)-min(all_y2), max(all_z2)-min(all_z2))
print(f"MaxDim: {maxDim:.2f} -> Three.js scale = {9.0/maxDim:.6f}")
out_path = r"c:\Users\KRISH\OneDrive\Desktop\AeroTwin AI\apps\web\public\jetengine.glb"
bpy.ops.export_scene.gltf(
filepath=out_path,
export_format='GLB',
export_apply=True,
export_materials='EXPORT',
export_cameras=False,
export_lights=False,
export_draco_mesh_compression_enable=True,
export_draco_mesh_compression_level=6,
)
print(f"\nExported: {out_path}")