Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -100,6 +100,84 @@ def first_file_from_dir(directory, ext):
|
|
| 100 |
files = glob.glob(os.path.join(directory, f"*.{ext}"))
|
| 101 |
return sorted(files)[0] if files else None
|
| 102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
def get_duration(
|
| 104 |
image_path,
|
| 105 |
num_parts,
|
|
|
|
| 100 |
files = glob.glob(os.path.join(directory, f"*.{ext}"))
|
| 101 |
return sorted(files)[0] if files else None
|
| 102 |
|
| 103 |
+
|
| 104 |
+
def explode_mesh(mesh, explosion_scale=0.4):
|
| 105 |
+
|
| 106 |
+
if isinstance(mesh, trimesh.Scene):
|
| 107 |
+
scene = mesh
|
| 108 |
+
elif isinstance(mesh, trimesh.Trimesh):
|
| 109 |
+
print("Warning: Single mesh provided, can't create exploded view")
|
| 110 |
+
scene = trimesh.Scene(mesh)
|
| 111 |
+
return scene
|
| 112 |
+
else:
|
| 113 |
+
print(f"Warning: Unexpected mesh type: {type(mesh)}")
|
| 114 |
+
scene = mesh
|
| 115 |
+
|
| 116 |
+
if len(scene.geometry) <= 1:
|
| 117 |
+
print("Only one geometry found - nothing to explode")
|
| 118 |
+
return scene
|
| 119 |
+
|
| 120 |
+
print(f"[EXPLODE_MESH] Starting mesh explosion with scale {explosion_scale}")
|
| 121 |
+
print(f"[EXPLODE_MESH] Processing {len(scene.geometry)} parts")
|
| 122 |
+
|
| 123 |
+
exploded_scene = trimesh.Scene()
|
| 124 |
+
|
| 125 |
+
part_centers = []
|
| 126 |
+
geometry_names = []
|
| 127 |
+
|
| 128 |
+
for geometry_name, geometry in scene.geometry.items():
|
| 129 |
+
if hasattr(geometry, 'vertices'):
|
| 130 |
+
transform = scene.graph[geometry_name][0]
|
| 131 |
+
vertices_global = trimesh.transformations.transform_points(
|
| 132 |
+
geometry.vertices, transform)
|
| 133 |
+
center = np.mean(vertices_global, axis=0)
|
| 134 |
+
part_centers.append(center)
|
| 135 |
+
geometry_names.append(geometry_name)
|
| 136 |
+
print(f"[EXPLODE_MESH] Part {geometry_name}: center = {center}")
|
| 137 |
+
|
| 138 |
+
if not part_centers:
|
| 139 |
+
print("No valid geometries with vertices found")
|
| 140 |
+
return scene
|
| 141 |
+
|
| 142 |
+
part_centers = np.array(part_centers)
|
| 143 |
+
global_center = np.mean(part_centers, axis=0)
|
| 144 |
+
|
| 145 |
+
print(f"[EXPLODE_MESH] Global center: {global_center}")
|
| 146 |
+
|
| 147 |
+
for i, (geometry_name, geometry) in enumerate(scene.geometry.items()):
|
| 148 |
+
if hasattr(geometry, 'vertices'):
|
| 149 |
+
if i < len(part_centers):
|
| 150 |
+
part_center = part_centers[i]
|
| 151 |
+
direction = part_center - global_center
|
| 152 |
+
|
| 153 |
+
direction_norm = np.linalg.norm(direction)
|
| 154 |
+
if direction_norm > 1e-6:
|
| 155 |
+
direction = direction / direction_norm
|
| 156 |
+
else:
|
| 157 |
+
direction = np.random.randn(3)
|
| 158 |
+
direction = direction / np.linalg.norm(direction)
|
| 159 |
+
|
| 160 |
+
offset = direction * explosion_scale
|
| 161 |
+
else:
|
| 162 |
+
offset = np.zeros(3)
|
| 163 |
+
|
| 164 |
+
original_transform = scene.graph[geometry_name][0].copy()
|
| 165 |
+
|
| 166 |
+
new_transform = original_transform.copy()
|
| 167 |
+
new_transform[:3, 3] = new_transform[:3, 3] + offset
|
| 168 |
+
|
| 169 |
+
exploded_scene.add_geometry(
|
| 170 |
+
geometry,
|
| 171 |
+
transform=new_transform,
|
| 172 |
+
geom_name=geometry_name
|
| 173 |
+
)
|
| 174 |
+
|
| 175 |
+
print(f"[EXPLODE_MESH] Part {geometry_name}: moved by {np.linalg.norm(offset):.4f}")
|
| 176 |
+
|
| 177 |
+
print("[EXPLODE_MESH] Mesh explosion complete")
|
| 178 |
+
return exploded_scene
|
| 179 |
+
|
| 180 |
+
|
| 181 |
def get_duration(
|
| 182 |
image_path,
|
| 183 |
num_parts,
|