Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
def create_textual_animation_gif(output_path, model_name, animation_type, duration=3.0, fps=30):
|
2 |
"""ν
μ€νΈ κΈ°λ°μ κ°λ¨ν μ λλ©μ΄μ
GIF μμ± - λ λλ§ μ€ν¨ μ λ체μ©"""
|
3 |
try:
|
@@ -84,741 +105,37 @@ def create_textual_animation_gif(output_path, model_name, animation_type, durati
|
|
84 |
return output_path
|
85 |
except Exception as e:
|
86 |
print(f"Error creating textual animation: {str(e)}")
|
87 |
-
return Nonedef create_simple_animation_frames(input_mesh_path, animation_type='rotate', num_frames=30):
|
88 |
-
"""κ°λ¨ν λ°©μμΌλ‘ μ λλ©μ΄μ
νλ μ μμ± - λ€λ₯Έ λ°©λ²λ€μ΄ μ€ν¨ν κ²½μ° λ체μ©"""
|
89 |
-
try:
|
90 |
-
# λ©μ λ‘λ
|
91 |
-
mesh = trimesh.load(input_mesh_path)
|
92 |
-
|
93 |
-
# κ°μ₯ κΈ°λ³Έμ μΈ ννλ‘ λ©μ/μ¬ μΆμΆ
|
94 |
-
if isinstance(mesh, trimesh.Scene):
|
95 |
-
# μ¬μμ 첫 λ²μ§Έ λ©μ μΆμΆ
|
96 |
-
geometries = list(mesh.geometry.values())
|
97 |
-
if geometries:
|
98 |
-
base_mesh = geometries[0]
|
99 |
-
else:
|
100 |
-
print("No geometries found in scene")
|
101 |
-
return None
|
102 |
-
else:
|
103 |
-
base_mesh = mesh
|
104 |
-
|
105 |
-
# μ λλ©μ΄μ
νλ μ μ€λΉ
|
106 |
-
frames = []
|
107 |
-
|
108 |
-
# λ¨μ νμ μ λλ©μ΄μ
μμ±
|
109 |
-
for i in range(num_frames):
|
110 |
-
angle = i * 2 * math.pi / num_frames
|
111 |
-
|
112 |
-
# μ μ¬ μμ±
|
113 |
-
scene = trimesh.Scene()
|
114 |
-
|
115 |
-
# λ©μ λ³΅μ¬ λ° νμ
|
116 |
-
rotated_mesh = base_mesh.copy()
|
117 |
-
rotation = tf.rotation_matrix(angle, [0, 1, 0])
|
118 |
-
rotated_mesh.apply_transform(rotation)
|
119 |
-
|
120 |
-
# μ¬μ μΆκ°
|
121 |
-
scene.add_geometry(rotated_mesh)
|
122 |
-
|
123 |
-
# μΉ΄λ©λΌ μ€μ
|
124 |
-
scene.set_camera()
|
125 |
-
|
126 |
-
frames.append(scene)
|
127 |
-
|
128 |
-
return frames
|
129 |
-
except Exception as e:
|
130 |
-
print(f"Error in simple animation: {str(e)}")
|
131 |
-
return Noneimport os
|
132 |
-
import time
|
133 |
-
import glob
|
134 |
-
import json
|
135 |
-
import numpy as np
|
136 |
-
import trimesh
|
137 |
-
import argparse
|
138 |
-
from scipy.spatial.transform import Rotation
|
139 |
-
import PIL.Image
|
140 |
-
from PIL import Image
|
141 |
-
import math
|
142 |
-
import trimesh.transformations as tf
|
143 |
-
from trimesh.exchange.gltf import export_glb
|
144 |
-
|
145 |
-
os.environ['PYOPENGL_PLATFORM'] = 'egl'
|
146 |
-
|
147 |
-
import gradio as gr
|
148 |
-
import spaces
|
149 |
-
|
150 |
-
def parse_args():
|
151 |
-
parser = argparse.ArgumentParser(description='Create animations for 3D models')
|
152 |
-
|
153 |
-
parser.add_argument(
|
154 |
-
'--input',
|
155 |
-
type=str,
|
156 |
-
default='./data/demo_glb/',
|
157 |
-
help='Input file or directory path (default: ./data/demo_glb/)'
|
158 |
-
)
|
159 |
-
|
160 |
-
parser.add_argument(
|
161 |
-
'--log_path',
|
162 |
-
type=str,
|
163 |
-
default='./results/demo',
|
164 |
-
help='Output directory path (default: results/demo)'
|
165 |
-
)
|
166 |
-
|
167 |
-
parser.add_argument(
|
168 |
-
'--animation_type',
|
169 |
-
type=str,
|
170 |
-
default='rotate',
|
171 |
-
choices=['rotate', 'float', 'explode', 'assemble', 'pulse', 'swing'],
|
172 |
-
help='Type of animation to apply'
|
173 |
-
)
|
174 |
-
|
175 |
-
parser.add_argument(
|
176 |
-
'--animation_duration',
|
177 |
-
type=float,
|
178 |
-
default=3.0,
|
179 |
-
help='Duration of animation in seconds'
|
180 |
-
)
|
181 |
-
|
182 |
-
parser.add_argument(
|
183 |
-
'--fps',
|
184 |
-
type=int,
|
185 |
-
default=30,
|
186 |
-
help='Frames per second for animation'
|
187 |
-
)
|
188 |
-
|
189 |
-
return parser.parse_args()
|
190 |
-
|
191 |
-
def get_input_files(input_path):
|
192 |
-
if os.path.isfile(input_path):
|
193 |
-
return [input_path]
|
194 |
-
elif os.path.isdir(input_path):
|
195 |
-
return glob.glob(os.path.join(input_path, '*'))
|
196 |
-
else:
|
197 |
-
raise ValueError(f"Input path {input_path} is neither a file nor a directory")
|
198 |
-
|
199 |
-
args = parse_args()
|
200 |
-
|
201 |
-
LOG_PATH = args.log_path
|
202 |
-
os.makedirs(LOG_PATH, exist_ok=True)
|
203 |
-
|
204 |
-
print(f"Output directory: {LOG_PATH}")
|
205 |
-
|
206 |
-
def normalize_mesh(mesh):
|
207 |
-
"""Normalize mesh to fit in a unit cube centered at origin"""
|
208 |
-
try:
|
209 |
-
if isinstance(mesh, trimesh.Scene):
|
210 |
-
# Scene κ°μ²΄ μ²λ¦¬
|
211 |
-
# μ¬μμ λͺ¨λ λ©μ μΆμΆ
|
212 |
-
meshes = []
|
213 |
-
for geometry in mesh.geometry.values():
|
214 |
-
if isinstance(geometry, trimesh.Trimesh):
|
215 |
-
meshes.append(geometry)
|
216 |
-
|
217 |
-
if not meshes:
|
218 |
-
print("Warning: No meshes found in scene during normalization")
|
219 |
-
return mesh, np.zeros(3), 1.0
|
220 |
-
|
221 |
-
# λͺ¨λ λ©μμ μ μ μ κ²°ν©νμ¬ κ²½κ³ μμ κ³μ°
|
222 |
-
try:
|
223 |
-
all_vertices = np.vstack([m.vertices for m in meshes if hasattr(m, 'vertices') and m.vertices.shape[0] > 0])
|
224 |
-
if len(all_vertices) == 0:
|
225 |
-
print("Warning: No vertices found in meshes during normalization")
|
226 |
-
return mesh, np.zeros(3), 1.0
|
227 |
-
|
228 |
-
bounds = np.array([all_vertices.min(axis=0), all_vertices.max(axis=0)])
|
229 |
-
center = (bounds[0] + bounds[1]) / 2
|
230 |
-
scale_value = (bounds[1] - bounds[0]).max()
|
231 |
-
if scale_value < 1e-10:
|
232 |
-
print("Warning: Mesh is too small, using default scale")
|
233 |
-
scale = 1.0
|
234 |
-
else:
|
235 |
-
scale = 1.0 / scale_value
|
236 |
-
|
237 |
-
# κ° λ©μλ₯Ό μ κ·ννμ¬ μ μ¬ μμ±
|
238 |
-
normalized_scene = trimesh.Scene()
|
239 |
-
for mesh_idx, mesh_obj in enumerate(meshes):
|
240 |
-
normalized_mesh = mesh_obj.copy()
|
241 |
-
try:
|
242 |
-
normalized_mesh.vertices = (normalized_mesh.vertices - center) * scale
|
243 |
-
normalized_scene.add_geometry(normalized_mesh, node_name=f"normalized_mesh_{mesh_idx}")
|
244 |
-
except Exception as e:
|
245 |
-
print(f"Error normalizing mesh {mesh_idx}: {str(e)}")
|
246 |
-
# μ€λ₯κ° λ°μνλ©΄ μλ³Έ λ©μ μΆκ°
|
247 |
-
normalized_scene.add_geometry(mesh_obj, node_name=f"original_mesh_{mesh_idx}")
|
248 |
-
|
249 |
-
return normalized_scene, center, scale
|
250 |
-
except Exception as e:
|
251 |
-
print(f"Error during scene normalization: {str(e)}")
|
252 |
-
return mesh, np.zeros(3), 1.0
|
253 |
-
else:
|
254 |
-
# μΌλ° Trimesh κ°μ²΄ μ²λ¦¬
|
255 |
-
if not hasattr(mesh, 'vertices') or mesh.vertices.shape[0] == 0:
|
256 |
-
print("Warning: Mesh has no vertices")
|
257 |
-
return mesh, np.zeros(3), 1.0
|
258 |
-
|
259 |
-
vertices = mesh.vertices
|
260 |
-
bounds = np.array([vertices.min(axis=0), vertices.max(axis=0)])
|
261 |
-
center = (bounds[0] + bounds[1]) / 2
|
262 |
-
scale_value = (bounds[1] - bounds[0]).max()
|
263 |
-
if scale_value < 1e-10:
|
264 |
-
print("Warning: Mesh is too small, using default scale")
|
265 |
-
scale = 1.0
|
266 |
-
else:
|
267 |
-
scale = 1.0 / scale_value
|
268 |
-
|
269 |
-
# 볡μ¬λ³Έ μμ±νμ¬ μλ³Έ λ³κ²½ λ°©μ§
|
270 |
-
normalized_mesh = mesh.copy()
|
271 |
-
normalized_mesh.vertices = (vertices - center) * scale
|
272 |
-
|
273 |
-
return normalized_mesh, center, scale
|
274 |
-
except Exception as e:
|
275 |
-
print(f"Unexpected error in normalize_mesh: {str(e)}")
|
276 |
-
# μ€λ₯ λ°μ μ μλ³Έ λ°ν
|
277 |
-
return mesh, np.zeros(3), 1.0
|
278 |
-
|
279 |
-
def create_rotation_animation(mesh, duration=3.0, fps=30):
|
280 |
-
"""Create a rotation animation around the Y axis"""
|
281 |
-
num_frames = int(duration * fps)
|
282 |
-
frames = []
|
283 |
-
|
284 |
-
# Normalize the mesh for consistent animation
|
285 |
-
try:
|
286 |
-
mesh, original_center, original_scale = normalize_mesh(mesh)
|
287 |
-
print(f"Normalized mesh center: {original_center}, scale: {original_scale}")
|
288 |
-
except Exception as e:
|
289 |
-
print(f"Error normalizing mesh: {str(e)}")
|
290 |
-
# μ κ·νμ μ€ν¨νλλΌλ κ³μ μ§ν
|
291 |
-
pass
|
292 |
-
|
293 |
-
for frame_idx in range(num_frames):
|
294 |
-
t = frame_idx / (num_frames - 1) # Normalized time [0, 1]
|
295 |
-
angle = t * 2 * math.pi # Full rotation
|
296 |
-
|
297 |
-
if isinstance(mesh, trimesh.Scene):
|
298 |
-
# Scene κ°μ²΄μΈ κ²½μ° κ° λ©μμ νμ μ μ©
|
299 |
-
frame_scene = trimesh.Scene()
|
300 |
-
|
301 |
-
for node_name, transform, geometry_name in mesh.graph.nodes_geometry:
|
302 |
-
# λ©μλ₯Ό 볡μ¬νκ³ νμ μ μ©
|
303 |
-
mesh_copy = mesh.geometry[geometry_name].copy()
|
304 |
-
|
305 |
-
# λ©μμ μ€μ¬μ κ³μ°
|
306 |
-
center_point = mesh_copy.centroid if hasattr(mesh_copy, 'centroid') else np.zeros(3)
|
307 |
-
|
308 |
-
# νμ νλ ¬ μμ±
|
309 |
-
rotation_matrix = tf.rotation_matrix(angle, [0, 1, 0], center_point)
|
310 |
-
|
311 |
-
# λ³ν μ μ©
|
312 |
-
mesh_copy.apply_transform(rotation_matrix)
|
313 |
-
|
314 |
-
# μ¬μ μΆκ°
|
315 |
-
frame_scene.add_geometry(mesh_copy, node_name=node_name)
|
316 |
-
|
317 |
-
frames.append(frame_scene)
|
318 |
-
else:
|
319 |
-
# μΌλ° Trimesh κ°μ²΄μΈ κ²½μ° μ§μ νμ μ μ©
|
320 |
-
animated_mesh = mesh.copy()
|
321 |
-
rotation_matrix = tf.rotation_matrix(angle, [0, 1, 0])
|
322 |
-
animated_mesh.apply_transform(rotation_matrix)
|
323 |
-
frames.append(animated_mesh)
|
324 |
-
|
325 |
-
return frames
|
326 |
-
|
327 |
-
def create_float_animation(mesh, duration=3.0, fps=30, amplitude=0.2):
|
328 |
-
"""Create a floating animation where the mesh moves up and down"""
|
329 |
-
num_frames = int(duration * fps)
|
330 |
-
frames = []
|
331 |
-
|
332 |
-
# Normalize the mesh for consistent animation
|
333 |
-
mesh, original_center, original_scale = normalize_mesh(mesh)
|
334 |
-
|
335 |
-
for frame_idx in range(num_frames):
|
336 |
-
t = frame_idx / (num_frames - 1) # Normalized time [0, 1]
|
337 |
-
y_offset = amplitude * math.sin(2 * math.pi * t)
|
338 |
-
|
339 |
-
if isinstance(mesh, trimesh.Scene):
|
340 |
-
# Scene κ°μ²΄μΈ κ²½μ°
|
341 |
-
frame_scene = trimesh.Scene()
|
342 |
-
|
343 |
-
for node_name, transform, geometry_name in mesh.graph.nodes_geometry:
|
344 |
-
# λ©μλ₯Ό 볡μ¬
|
345 |
-
mesh_copy = mesh.geometry[geometry_name].copy()
|
346 |
-
|
347 |
-
# λ³ν μ μ©
|
348 |
-
translation_matrix = tf.translation_matrix([0, y_offset, 0])
|
349 |
-
mesh_copy.apply_transform(translation_matrix)
|
350 |
-
|
351 |
-
# μ¬μ μΆκ°
|
352 |
-
frame_scene.add_geometry(mesh_copy, node_name=node_name)
|
353 |
-
|
354 |
-
frames.append(frame_scene)
|
355 |
-
else:
|
356 |
-
# μΌλ° Trimesh κ°μ²΄μΈ κ²½μ°
|
357 |
-
animated_mesh = mesh.copy()
|
358 |
-
translation_matrix = tf.translation_matrix([0, y_offset, 0])
|
359 |
-
animated_mesh.apply_transform(translation_matrix)
|
360 |
-
frames.append(animated_mesh)
|
361 |
-
|
362 |
-
return frames
|
363 |
-
|
364 |
-
def create_explode_animation(mesh, duration=3.0, fps=30):
|
365 |
-
"""Create an explode animation where parts of the mesh move outward"""
|
366 |
-
num_frames = int(duration * fps)
|
367 |
-
frames = []
|
368 |
-
|
369 |
-
# Normalize the mesh for consistent animation
|
370 |
-
mesh, original_center, original_scale = normalize_mesh(mesh)
|
371 |
-
|
372 |
-
# μ¬μΈ κ²½μ° λΆλΆλ³ μ²λ¦¬
|
373 |
-
if isinstance(mesh, trimesh.Scene):
|
374 |
-
for frame_idx in range(num_frames):
|
375 |
-
t = frame_idx / (num_frames - 1) # Normalized time [0, 1]
|
376 |
-
|
377 |
-
# μ μ¬ μμ±
|
378 |
-
frame_scene = trimesh.Scene()
|
379 |
-
|
380 |
-
# κ° λ
Έλλ³ νλ° μ λλ©μ΄μ
μ μ©
|
381 |
-
for node_name, transform, geometry_name in mesh.graph.nodes_geometry:
|
382 |
-
mesh_copy = mesh.geometry[geometry_name].copy()
|
383 |
-
|
384 |
-
# λ
Έλμ μ€μ¬μ κ³μ°
|
385 |
-
center_point = mesh_copy.centroid if hasattr(mesh_copy, 'centroid') else np.zeros(3)
|
386 |
-
|
387 |
-
# λ°©ν₯ λ²‘ν° (μμ μμ κ°μ²΄ μ€μ¬κΉμ§)
|
388 |
-
direction = center_point
|
389 |
-
if np.linalg.norm(direction) < 1e-10:
|
390 |
-
# μ€μ¬μ μ΄ μμ κ³Ό λ무 κ°κΉμ°λ©΄ λλ€ λ°©ν₯ μ ν
|
391 |
-
direction = np.random.rand(3) - 0.5
|
392 |
-
|
393 |
-
direction = direction / np.linalg.norm(direction)
|
394 |
-
|
395 |
-
# νλ° μ΄λ μ μ©
|
396 |
-
translation = direction * t * 0.5 # νλ° κ°λ μ‘°μ
|
397 |
-
translation_matrix = tf.translation_matrix(translation)
|
398 |
-
mesh_copy.apply_transform(translation_matrix)
|
399 |
-
|
400 |
-
# μ¬μ μΆκ°
|
401 |
-
frame_scene.add_geometry(mesh_copy, node_name=f"{node_name}_{frame_idx}")
|
402 |
-
|
403 |
-
frames.append(frame_scene)
|
404 |
-
else:
|
405 |
-
# λ©μλ₯Ό μ¬λ¬ λΆλΆμΌλ‘ λΆν
|
406 |
-
try:
|
407 |
-
components = mesh.split(only_watertight=False)
|
408 |
-
if len(components) <= 1:
|
409 |
-
# μ»΄ν¬λνΈκ° νλλΏμ΄λ©΄ μ μ κΈ°λ° νλ° μ λλ©μ΄μ
μ¬μ©
|
410 |
-
raise ValueError("Mesh cannot be split into components")
|
411 |
-
except:
|
412 |
-
# λΆν μ΄ μ€ν¨νλ©΄ μ μ κΈ°λ° νλ° μ λλ©μ΄μ
μ¬μ©
|
413 |
-
for frame_idx in range(num_frames):
|
414 |
-
t = frame_idx / (num_frames - 1) # Normalized time [0, 1]
|
415 |
-
|
416 |
-
# λ©μ 볡μ¬
|
417 |
-
animated_mesh = mesh.copy()
|
418 |
-
vertices = animated_mesh.vertices.copy()
|
419 |
-
|
420 |
-
# κ° μ μ μ μ€μ¬μμ λ°κΉ₯μͺ½μΌλ‘ μ΄λ
|
421 |
-
directions = vertices.copy()
|
422 |
-
norms = np.linalg.norm(directions, axis=1, keepdims=True)
|
423 |
-
mask = norms > 1e-10
|
424 |
-
directions[mask] = directions[mask] / norms[mask]
|
425 |
-
directions[~mask] = np.random.rand(np.sum(~mask), 3) - 0.5
|
426 |
-
|
427 |
-
# νλ° κ³μ μ μ©
|
428 |
-
vertices += directions * t * 0.3
|
429 |
-
animated_mesh.vertices = vertices
|
430 |
-
|
431 |
-
frames.append(animated_mesh)
|
432 |
-
else:
|
433 |
-
# μ»΄ν¬λνΈ κΈ°λ° νλ° μ λλ©μ΄μ
|
434 |
-
for frame_idx in range(num_frames):
|
435 |
-
t = frame_idx / (num_frames - 1) # Normalized time [0, 1]
|
436 |
-
|
437 |
-
# μ μ¬ μμ±
|
438 |
-
scene = trimesh.Scene()
|
439 |
-
|
440 |
-
# κ° μ»΄ν¬λνΈλ₯Ό μ€μ¬μμ λ°κΉ₯μͺ½μΌλ‘ μ΄λ
|
441 |
-
for i, component in enumerate(components):
|
442 |
-
# μ»΄ν¬λνΈ λ³΅μ¬
|
443 |
-
animated_component = component.copy()
|
444 |
-
|
445 |
-
# μ»΄ν¬λνΈ μ€μ¬μ μμ λ°©ν₯ κ³μ°
|
446 |
-
direction = animated_component.centroid
|
447 |
-
if np.linalg.norm(direction) < 1e-10:
|
448 |
-
# μ€μ¬μ μ΄ μμ κ³Ό λ무 κ°κΉμ°λ©΄ λλ€ λ°©ν₯ μ ν
|
449 |
-
direction = np.random.rand(3) - 0.5
|
450 |
-
|
451 |
-
direction = direction / np.linalg.norm(direction)
|
452 |
-
|
453 |
-
# νλ° μ΄λ μ μ©
|
454 |
-
translation = direction * t * 0.5
|
455 |
-
translation_matrix = tf.translation_matrix(translation)
|
456 |
-
animated_component.apply_transform(translation_matrix)
|
457 |
-
|
458 |
-
# μ¬μ μΆκ°
|
459 |
-
scene.add_geometry(animated_component, node_name=f"component_{i}")
|
460 |
-
|
461 |
-
# μ¬μ λ¨μΌ λ©μλ‘ λ³ν (κ·Όμ¬μΉ)
|
462 |
-
animated_mesh = trimesh.util.concatenate(scene.dump())
|
463 |
-
frames.append(animated_mesh)
|
464 |
-
|
465 |
-
return frames
|
466 |
-
|
467 |
-
def create_assemble_animation(mesh, duration=3.0, fps=30):
|
468 |
-
"""Create an assembly animation (reverse of explode)"""
|
469 |
-
# Get explode animation and reverse it
|
470 |
-
explode_frames = create_explode_animation(mesh, duration, fps)
|
471 |
-
return list(reversed(explode_frames))
|
472 |
-
|
473 |
-
def create_pulse_animation(mesh, duration=3.0, fps=30, min_scale=0.8, max_scale=1.2):
|
474 |
-
"""Create a pulsing animation where the mesh scales up and down"""
|
475 |
-
num_frames = int(duration * fps)
|
476 |
-
frames = []
|
477 |
-
|
478 |
-
# Normalize the mesh for consistent animation
|
479 |
-
mesh, original_center, original_scale = normalize_mesh(mesh)
|
480 |
-
|
481 |
-
for frame_idx in range(num_frames):
|
482 |
-
t = frame_idx / (num_frames - 1) # Normalized time [0, 1]
|
483 |
-
|
484 |
-
# Create a copy of the mesh to animate
|
485 |
-
animated_mesh = mesh.copy()
|
486 |
-
|
487 |
-
# Apply pulsing motion (sinusoidal scale)
|
488 |
-
scale_factor = min_scale + (max_scale - min_scale) * (0.5 + 0.5 * math.sin(2 * math.pi * t))
|
489 |
-
scale_matrix = tf.scale_matrix(scale_factor)
|
490 |
-
animated_mesh.apply_transform(scale_matrix)
|
491 |
-
|
492 |
-
# Add to frames
|
493 |
-
frames.append(animated_mesh)
|
494 |
-
|
495 |
-
return frames
|
496 |
-
|
497 |
-
def create_swing_animation(mesh, duration=3.0, fps=30, max_angle=math.pi/6):
|
498 |
-
"""Create a swinging animation where the mesh rotates back and forth"""
|
499 |
-
num_frames = int(duration * fps)
|
500 |
-
frames = []
|
501 |
-
|
502 |
-
# Normalize the mesh for consistent animation
|
503 |
-
mesh, original_center, original_scale = normalize_mesh(mesh)
|
504 |
-
|
505 |
-
for frame_idx in range(num_frames):
|
506 |
-
t = frame_idx / (num_frames - 1) # Normalized time [0, 1]
|
507 |
-
|
508 |
-
# Create a copy of the mesh to animate
|
509 |
-
animated_mesh = mesh.copy()
|
510 |
-
|
511 |
-
# Apply swinging motion (sinusoidal rotation)
|
512 |
-
angle = max_angle * math.sin(2 * math.pi * t)
|
513 |
-
rotation_matrix = tf.rotation_matrix(angle, [0, 1, 0])
|
514 |
-
animated_mesh.apply_transform(rotation_matrix)
|
515 |
-
|
516 |
-
# Add to frames
|
517 |
-
frames.append(animated_mesh)
|
518 |
-
|
519 |
-
return frames
|
520 |
-
|
521 |
-
def generate_gif_from_frames(frames, output_path, fps=30, resolution=(640, 480), background_color=(255, 255, 255, 255)):
|
522 |
-
"""Generate a GIF from animation frames"""
|
523 |
-
gif_frames = []
|
524 |
-
|
525 |
-
# λͺ¨λ νλ μμ κ²½κ³ μμλ₯Ό κ³μ°νμ¬ μΌκ΄λ μΉ΄λ©λΌ μ€μ ꡬμ±
|
526 |
-
all_bounds = []
|
527 |
-
for frame in frames:
|
528 |
-
if isinstance(frame, trimesh.Scene):
|
529 |
-
# μ¬μμ λͺ¨λ λ©μμ μ μ μ μΆμΆνμ¬ κ²½κ³μμ κ³μ°
|
530 |
-
all_points = []
|
531 |
-
for geom in frame.geometry.values():
|
532 |
-
if hasattr(geom, 'vertices') and geom.vertices.shape[0] > 0:
|
533 |
-
all_points.append(geom.vertices)
|
534 |
-
|
535 |
-
if all_points:
|
536 |
-
all_vertices = np.vstack(all_points)
|
537 |
-
bounds = np.array([all_vertices.min(axis=0), all_vertices.max(axis=0)])
|
538 |
-
all_bounds.append(bounds)
|
539 |
-
elif hasattr(frame, 'vertices') and frame.vertices.shape[0] > 0:
|
540 |
-
# μΌλ° λ©μμμ κ²½κ³μμ κ³μ°
|
541 |
-
bounds = np.array([frame.vertices.min(axis=0), frame.vertices.max(axis=0)])
|
542 |
-
all_bounds.append(bounds)
|
543 |
-
|
544 |
-
# λͺ¨λ νλ μμ ν¬ν¨νλ μ 체 κ²½κ³μμ κ³μ°
|
545 |
-
if all_bounds:
|
546 |
-
min_corner = np.min(np.array([b[0] for b in all_bounds]), axis=0)
|
547 |
-
max_corner = np.max(np.array([b[1] for b in all_bounds]), axis=0)
|
548 |
-
total_bounds = np.array([min_corner, max_corner])
|
549 |
-
|
550 |
-
# κ²½κ³μμ μ€μ¬κ³Ό ν¬κΈ° κ³μ°
|
551 |
-
center = (total_bounds[0] + total_bounds[1]) / 2
|
552 |
-
size = np.max(total_bounds[1] - total_bounds[0])
|
553 |
-
|
554 |
-
# μΉ΄λ©λΌ μμΉ κ³μ° (μ μ ν 거리μμ κ°μ²΄ λ°λΌλ³΄κΈ°)
|
555 |
-
camera_distance = size * 2.5 # κ°μ²΄ ν¬κΈ°μ 2.5λ°° 거리
|
556 |
-
else:
|
557 |
-
# κ²½κ³μμλ₯Ό κ³μ°ν μ μμΌλ©΄ κΈ°λ³Έκ° μ¬μ©
|
558 |
-
center = np.zeros(3)
|
559 |
-
camera_distance = 2.0
|
560 |
-
|
561 |
-
# κ° νλ μ λ λλ§
|
562 |
-
for i, frame in enumerate(frames):
|
563 |
-
try:
|
564 |
-
# μ¬ κ°μ²΄ μμ±
|
565 |
-
if not isinstance(frame, trimesh.Scene):
|
566 |
-
scene = trimesh.Scene(frame)
|
567 |
-
else:
|
568 |
-
scene = frame
|
569 |
-
|
570 |
-
# λ κ°λ ₯ν μΉ΄λ©λΌ μ€μ λ°©λ²
|
571 |
-
try:
|
572 |
-
# κ°μ²΄κ° 보μ΄λ μμΉμ μΉ΄λ©λΌ μ€μ
|
573 |
-
camera_fov = 60.0 # μμΌκ° (κ°λ)
|
574 |
-
camera_pos = np.array([0, 0, camera_distance]) # μΉ΄λ©λΌ μμΉ
|
575 |
-
|
576 |
-
# μΉ΄λ©λΌ λ³ν νλ ¬ μμ±
|
577 |
-
camera_transform = np.eye(4)
|
578 |
-
camera_transform[:3, 3] = camera_pos
|
579 |
-
|
580 |
-
# μΉ΄λ©λΌκ° μμ μ λ°λΌλ³΄λλ‘ μ€μ
|
581 |
-
scene.camera = trimesh.scene.Camera(
|
582 |
-
resolution=resolution,
|
583 |
-
fov=[camera_fov, camera_fov * (resolution[1] / resolution[0])]
|
584 |
-
)
|
585 |
-
scene.camera_transform = camera_transform
|
586 |
-
except Exception as e:
|
587 |
-
print(f"Error setting camera: {str(e)}")
|
588 |
-
# κΈ°λ³Έ μΉ΄λ©λΌ μ€μ
|
589 |
-
scene.set_camera(angles=(0, 0, 0), distance=camera_distance)
|
590 |
-
|
591 |
-
# λ λλ§ μλ
|
592 |
-
try:
|
593 |
-
# PyOpenGL κ²½κ³ μ¨κΈ°κΈ°
|
594 |
-
import warnings
|
595 |
-
warnings.filterwarnings("ignore", category=UserWarning)
|
596 |
-
|
597 |
-
# μ¬ λ λλ§
|
598 |
-
rendered_img = scene.save_image(resolution=resolution, background=background_color)
|
599 |
-
pil_img = Image.open(rendered_img)
|
600 |
-
|
601 |
-
# μ΄λ―Έμ§κ° λΉμ΄μλμ§ νμΈ
|
602 |
-
if np.array(pil_img).std() < 1.0: # νμ€νΈμ°¨κ° μμΌλ©΄ λλΆλΆ λμΌν μμ (λΉ μ΄λ―Έμ§)
|
603 |
-
print(f"Warning: Frame {i} seems to be empty, trying different angle")
|
604 |
-
# λ€λ₯Έ κ°λμμ λ€μ μλ
|
605 |
-
scene.set_camera(angles=(np.pi/4, np.pi/4, 0), distance=camera_distance*1.2)
|
606 |
-
rendered_img = scene.save_image(resolution=resolution, background=background_color)
|
607 |
-
pil_img = Image.open(rendered_img)
|
608 |
-
|
609 |
-
gif_frames.append(pil_img)
|
610 |
-
except Exception as e:
|
611 |
-
print(f"Error in main rendering: {str(e)}")
|
612 |
-
# μ€νμ€ν¬λ¦° λ λλ§ μ€ν¨ μ κ°λ¨ν λ°©λ²μΌλ‘ μλ
|
613 |
-
try:
|
614 |
-
# λ체 λ λλ§ λ©μλ
|
615 |
-
from PIL import Image, ImageDraw
|
616 |
-
img = Image.new('RGB', resolution, color=(255, 255, 255))
|
617 |
-
draw = ImageDraw.Draw(img)
|
618 |
-
|
619 |
-
# νλ μ λ²νΈλ₯Ό ν
μ€νΈλ‘ νμ
|
620 |
-
draw.text((resolution[0]//2, resolution[1]//2), f"Frame {i}", fill=(0, 0, 0))
|
621 |
-
gif_frames.append(img)
|
622 |
-
except Exception as e2:
|
623 |
-
print(f"Error in fallback rendering: {str(e2)}")
|
624 |
-
gif_frames.append(Image.new('RGB', resolution, (255, 255, 255)))
|
625 |
-
except Exception as e:
|
626 |
-
print(f"Unexpected error in frame processing: {str(e)}")
|
627 |
-
gif_frames.append(Image.new('RGB', resolution, (255, 255, 255)))
|
628 |
-
|
629 |
-
# GIF μ μ₯
|
630 |
-
if gif_frames:
|
631 |
-
try:
|
632 |
-
gif_frames[0].save(
|
633 |
-
output_path,
|
634 |
-
save_all=True,
|
635 |
-
append_images=gif_frames[1:],
|
636 |
-
optimize=False,
|
637 |
-
duration=int(1000 / fps),
|
638 |
-
loop=0
|
639 |
-
)
|
640 |
-
print(f"GIF saved to {output_path}")
|
641 |
-
# νμΈμ μν΄ μ²« νλ μλ λ°λ‘ μ μ₯
|
642 |
-
first_frame_path = output_path.replace('.gif', '_first_frame.png')
|
643 |
-
gif_frames[0].save(first_frame_path)
|
644 |
-
print(f"First frame saved to {first_frame_path}")
|
645 |
-
return output_path
|
646 |
-
except Exception as e:
|
647 |
-
print(f"Error saving GIF: {str(e)}")
|
648 |
-
return None
|
649 |
-
else:
|
650 |
-
print("No frames to save")
|
651 |
return None
|
652 |
|
653 |
-
def create_animation_mesh(input_mesh_path, animation_type='rotate', duration=3.0, fps=30):
|
654 |
-
"""Create animation from input mesh based on animation type"""
|
655 |
-
# Load the mesh
|
656 |
-
try:
|
657 |
-
print(f"Loading mesh from {input_mesh_path}")
|
658 |
-
# λ¨Όμ SceneμΌλ‘ λ‘λ μλ
|
659 |
-
loaded_obj = trimesh.load(input_mesh_path)
|
660 |
-
print(f"Loaded object type: {type(loaded_obj)}")
|
661 |
-
|
662 |
-
# SceneμΈ κ²½μ° λ¨μΌ λ©μλ‘ λ³ν μλ
|
663 |
-
if isinstance(loaded_obj, trimesh.Scene):
|
664 |
-
print("Loaded a scene, extracting meshes...")
|
665 |
-
# μ¬μμ λͺ¨λ λ©μ μΆμΆ
|
666 |
-
meshes = []
|
667 |
-
for geometry_name, geometry in loaded_obj.geometry.items():
|
668 |
-
if isinstance(geometry, trimesh.Trimesh):
|
669 |
-
print(f"Found mesh: {geometry_name} with {len(geometry.vertices)} vertices")
|
670 |
-
meshes.append(geometry)
|
671 |
-
|
672 |
-
if not meshes:
|
673 |
-
print("No meshes found in scene, trying simple animation...")
|
674 |
-
frames = create_simple_animation_frames(input_mesh_path, animation_type, int(duration * fps))
|
675 |
-
if not frames:
|
676 |
-
print("Simple animation failed too")
|
677 |
-
return None, None
|
678 |
-
else:
|
679 |
-
# λͺ¨λ λ©μλ₯Ό νλλ‘ κ²°ν©
|
680 |
-
mesh = loaded_obj
|
681 |
-
print(f"Using original scene with {len(meshes)} meshes")
|
682 |
-
elif isinstance(loaded_obj, trimesh.Trimesh):
|
683 |
-
mesh = loaded_obj
|
684 |
-
print(f"Loaded a single mesh with {len(mesh.vertices)} vertices")
|
685 |
-
else:
|
686 |
-
print(f"Unsupported object type: {type(loaded_obj)}")
|
687 |
-
# κ°λ¨ν μ λλ©μ΄μ
μμ± μλ
|
688 |
-
frames = create_simple_animation_frames(input_mesh_path, animation_type, int(duration * fps))
|
689 |
-
if not frames:
|
690 |
-
return None, None
|
691 |
-
except Exception as e:
|
692 |
-
print(f"Error loading mesh: {str(e)}")
|
693 |
-
# κ°λ¨ν μ λλ©μ΄μ
μμ± μλ
|
694 |
-
frames = create_simple_animation_frames(input_mesh_path, animation_type, int(duration * fps))
|
695 |
-
if not frames:
|
696 |
-
return None, None
|
697 |
-
|
698 |
-
# Generate animation frames based on animation type
|
699 |
-
try:
|
700 |
-
if 'frames' not in locals(): # μ΄λ―Έ framesκ° μμ±λμ§ μμλ€λ©΄
|
701 |
-
print(f"Generating {animation_type} animation...")
|
702 |
-
if animation_type == 'rotate':
|
703 |
-
frames = create_rotation_animation(mesh, duration, fps)
|
704 |
-
elif animation_type == 'float':
|
705 |
-
frames = create_float_animation(mesh, duration, fps)
|
706 |
-
elif animation_type == 'explode':
|
707 |
-
frames = create_explode_animation(mesh, duration, fps)
|
708 |
-
elif animation_type == 'assemble':
|
709 |
-
frames = create_assemble_animation(mesh, duration, fps)
|
710 |
-
elif animation_type == 'pulse':
|
711 |
-
frames = create_pulse_animation(mesh, duration, fps)
|
712 |
-
elif animation_type == 'swing':
|
713 |
-
frames = create_swing_animation(mesh, duration, fps)
|
714 |
-
else:
|
715 |
-
print(f"Unknown animation type: {animation_type}, using default rotate")
|
716 |
-
frames = create_rotation_animation(mesh, duration, fps)
|
717 |
-
except Exception as e:
|
718 |
-
print(f"Error generating animation: {str(e)}")
|
719 |
-
# μ λλ©μ΄μ
μμ± μ€ν¨ μ κ°λ¨ν μ λλ©μ΄μ
μλ
|
720 |
-
frames = create_simple_animation_frames(input_mesh_path, animation_type, int(duration * fps))
|
721 |
-
if not frames:
|
722 |
-
return None, None
|
723 |
-
|
724 |
-
print(f"Generated {len(frames)} animation frames")
|
725 |
-
|
726 |
-
base_filename = os.path.basename(input_mesh_path).rsplit('.', 1)[0]
|
727 |
-
|
728 |
-
# Save animated mesh as GLB
|
729 |
-
try:
|
730 |
-
animated_glb_path = os.path.join(LOG_PATH, f'animated_{base_filename}.glb')
|
731 |
-
|
732 |
-
# For GLB output, we'll use the first frame for now
|
733 |
-
# In a production environment, you'd want to use proper animation keyframes
|
734 |
-
if frames and len(frames) > 0:
|
735 |
-
# First frame for static GLB
|
736 |
-
first_frame = frames[0]
|
737 |
-
# Export as GLB
|
738 |
-
if not isinstance(first_frame, trimesh.Scene):
|
739 |
-
scene = trimesh.Scene(first_frame)
|
740 |
-
else:
|
741 |
-
scene = first_frame
|
742 |
-
scene.export(animated_glb_path)
|
743 |
-
print(f"Exported GLB to {animated_glb_path}")
|
744 |
-
else:
|
745 |
-
return None, None
|
746 |
-
except Exception as e:
|
747 |
-
print(f"Error exporting GLB: {str(e)}")
|
748 |
-
animated_glb_path = None
|
749 |
-
|
750 |
-
# Create GIF for preview
|
751 |
-
try:
|
752 |
-
animated_gif_path = os.path.join(LOG_PATH, f'animated_{base_filename}.gif')
|
753 |
-
print(f"Creating GIF at {animated_gif_path}")
|
754 |
-
generate_gif_from_frames(frames, animated_gif_path, fps)
|
755 |
-
except Exception as e:
|
756 |
-
print(f"Error creating GIF: {str(e)}")
|
757 |
-
animated_gif_path = None
|
758 |
-
|
759 |
-
return animated_glb_path, animated_gif_path
|
760 |
-
|
761 |
@spaces.GPU
|
762 |
def process_3d_model(input_3d, animation_type, animation_duration, fps):
|
763 |
"""Process a 3D model and apply animation"""
|
764 |
print(f"Processing: {input_3d} with animation type: {animation_type}")
|
765 |
|
766 |
try:
|
767 |
-
#
|
768 |
-
|
769 |
-
|
770 |
-
|
771 |
-
|
772 |
-
|
|
|
|
|
|
|
773 |
)
|
774 |
|
775 |
-
|
776 |
-
|
777 |
-
|
778 |
-
|
779 |
-
|
780 |
-
|
781 |
-
|
782 |
-
|
783 |
-
|
784 |
-
|
785 |
-
base_filename = os.path.basename(input_3d).rsplit('.', 1)[0]
|
786 |
-
|
787 |
-
# GLB μ μ₯
|
788 |
-
animated_glb_path = os.path.join(LOG_PATH, f'simple_animated_{base_filename}.glb')
|
789 |
-
if isinstance(frames[0], trimesh.Scene):
|
790 |
-
scene = frames[0]
|
791 |
-
else:
|
792 |
-
scene = trimesh.Scene(frames[0])
|
793 |
-
scene.export(animated_glb_path)
|
794 |
-
|
795 |
-
# GIF μμ±
|
796 |
-
animated_gif_path = os.path.join(LOG_PATH, f'simple_animated_{base_filename}.gif')
|
797 |
-
generate_gif_from_frames(frames, animated_gif_path, fps)
|
798 |
-
else:
|
799 |
-
# λͺ¨λ λ©μλκ° μ€ν¨ν κ²½μ° ν
μ€νΈ κΈ°λ° GIFλΌλ μμ±
|
800 |
-
base_filename = os.path.basename(input_3d).rsplit('.', 1)[0]
|
801 |
-
text_gif_path = os.path.join(LOG_PATH, f'text_animated_{base_filename}.gif')
|
802 |
-
animated_gif_path = create_textual_animation_gif(
|
803 |
-
text_gif_path,
|
804 |
-
os.path.basename(input_3d),
|
805 |
-
animation_type,
|
806 |
-
animation_duration,
|
807 |
-
fps
|
808 |
-
)
|
809 |
-
|
810 |
-
# μλ³Έ νμΌμ GLBλ‘ λ³΅μ¬ (μ²λ¦¬λμ§ μμ μνλ‘)
|
811 |
-
copy_glb_path = os.path.join(LOG_PATH, f'copy_{base_filename}.glb')
|
812 |
-
import shutil
|
813 |
-
try:
|
814 |
-
shutil.copy(input_3d, copy_glb_path)
|
815 |
-
animated_glb_path = copy_glb_path
|
816 |
-
print(f"Copied original GLB to {copy_glb_path}")
|
817 |
-
except:
|
818 |
-
animated_glb_path = input_3d
|
819 |
-
print("Could not copy original GLB, using input path")
|
820 |
|
821 |
-
#
|
822 |
metadata = {
|
823 |
"animation_type": animation_type,
|
824 |
"duration": animation_duration,
|
@@ -827,206 +144,98 @@ def process_3d_model(input_3d, animation_type, animation_duration, fps):
|
|
827 |
"created_at": time.strftime("%Y-%m-%d %H:%M:%S")
|
828 |
}
|
829 |
|
830 |
-
json_path = os.path.join(LOG_PATH, f'metadata_{
|
831 |
with open(json_path, 'w') as f:
|
832 |
json.dump(metadata, f, indent=4)
|
833 |
-
|
834 |
-
# GIFκ° μκ±°λ λ λλ§ μ€ν¨μΈ κ²½μ°λ₯Ό λλΉν λ°±μ
GIF μμ±
|
835 |
-
if not animated_gif_path:
|
836 |
-
base_filename = os.path.basename(input_3d).rsplit('.', 1)[0]
|
837 |
-
text_gif_path = os.path.join(LOG_PATH, f'text_animated_{base_filename}.gif')
|
838 |
-
animated_gif_path = create_textual_animation_gif(
|
839 |
-
text_gif_path,
|
840 |
-
os.path.basename(input_3d),
|
841 |
-
animation_type,
|
842 |
-
animation_duration,
|
843 |
-
fps
|
844 |
-
)
|
845 |
-
|
846 |
-
return animated_glb_path, animated_gif_path, json_path
|
847 |
-
except Exception as e:
|
848 |
-
error_msg = f"Error processing file: {str(e)}"
|
849 |
-
print(error_msg)
|
850 |
-
|
851 |
-
# μ¬κ°ν μ€λ₯ λ°μ μ ν
μ€νΈ μ λλ©μ΄μ
μ΄λΌλ μμ±
|
852 |
-
try:
|
853 |
-
base_filename = os.path.basename(input_3d).rsplit('.', 1)[0]
|
854 |
-
text_gif_path = os.path.join(LOG_PATH, f'text_animated_{base_filename}.gif')
|
855 |
-
animated_gif_path = create_textual_animation_gif(
|
856 |
-
text_gif_path,
|
857 |
-
os.path.basename(input_3d),
|
858 |
-
animation_type,
|
859 |
-
animation_duration,
|
860 |
-
fps
|
861 |
-
)
|
862 |
|
863 |
-
# μλ³Έ νμΌμ GLBλ‘ λ³΅μ¬ (μ²λ¦¬λμ§ μμ μνλ‘)
|
864 |
-
copy_glb_path = os.path.join(LOG_PATH, f'copy_{base_filename}.glb')
|
865 |
-
import shutil
|
866 |
-
try:
|
867 |
-
shutil.copy(input_3d, copy_glb_path)
|
868 |
-
animated_glb_path = copy_glb_path
|
869 |
-
except:
|
870 |
-
animated_glb_path = input_3d
|
871 |
-
|
872 |
-
# λ©νλ°μ΄ν° μμ±
|
873 |
-
metadata = {
|
874 |
-
"animation_type": animation_type,
|
875 |
-
"duration": animation_duration,
|
876 |
-
"fps": fps,
|
877 |
-
"original_model": os.path.basename(input_3d),
|
878 |
-
"created_at": time.strftime("%Y-%m-%d %H:%M:%S"),
|
879 |
-
"error": str(e)
|
880 |
-
}
|
881 |
-
|
882 |
-
json_path = os.path.join(LOG_PATH, f'metadata_{base_filename}.json')
|
883 |
-
with open(json_path, 'w') as f:
|
884 |
-
json.dump(metadata, f, indent=4)
|
885 |
-
|
886 |
-
return animated_glb_path, animated_gif_path, json_path
|
887 |
-
except:
|
888 |
-
# λͺ¨λ λ°©λ²μ΄ μ€ν¨ν κ²½μ°
|
889 |
-
return error_msg, None, None, 50), f"Model: {os.path.basename(input_3d)}", fill=(0, 0, 0))
|
890 |
-
draw.text((50, 100), f"Animation: {animation_type}", fill=(0, 0, 0))
|
891 |
-
draw.text((50, 150), f"Angle: {angle}Β°", fill=(0, 0, 0))
|
892 |
-
# κ°λ¨ν νμ κ°μ²΄ 그리기
|
893 |
-
center_x, center_y = 200, 180
|
894 |
-
radius = 50
|
895 |
-
x = center_x + radius * math.cos(math.radians(angle))
|
896 |
-
y = center_y + radius * math.sin(math.radians(angle))
|
897 |
-
draw.ellipse((x-20, y-20, x+20, y+20), fill=(255, 0, 0))
|
898 |
-
simple_frames.append(img)
|
899 |
-
|
900 |
-
# GIF μ μ₯
|
901 |
-
simple_frames[0].save(
|
902 |
-
backup_gif_path,
|
903 |
-
save_all=True,
|
904 |
-
append_images=simple_frames[1:],
|
905 |
-
optimize=False,
|
906 |
-
duration=int(1000 / fps),
|
907 |
-
loop=0
|
908 |
-
)
|
909 |
-
print(f"Backup GIF created at {backup_gif_path}")
|
910 |
-
|
911 |
-
# κΈ°μ‘΄ GIFκ° λΉμ΄μκ±°λ λ¬Έμ κ° μμΌλ©΄ λ°±μ
GIF μ¬μ©
|
912 |
-
try:
|
913 |
-
# κΈ°μ‘΄ GIF νμΈ
|
914 |
-
original_gif = Image.open(animated_gif_path)
|
915 |
-
original_frames = []
|
916 |
-
try:
|
917 |
-
for i in range(100): # μ΅λ 100 νλ μ
|
918 |
-
original_gif.seek(i)
|
919 |
-
frame = original_gif.copy()
|
920 |
-
original_frames.append(frame)
|
921 |
-
except EOFError:
|
922 |
-
pass # λͺ¨λ νλ μ μ½μ
|
923 |
-
|
924 |
-
if not original_frames:
|
925 |
-
print("Original GIF is empty, using backup")
|
926 |
-
animated_gif_path = backup_gif_path
|
927 |
-
except Exception as e:
|
928 |
-
print(f"Error checking original GIF: {str(e)}, using backup")
|
929 |
-
animated_gif_path = backup_gif_path
|
930 |
-
except Exception as e:
|
931 |
-
print(f"Error creating backup GIF: {str(e)}")
|
932 |
-
|
933 |
return animated_glb_path, animated_gif_path, json_path
|
934 |
except Exception as e:
|
935 |
error_msg = f"Error processing file: {str(e)}"
|
936 |
print(error_msg)
|
937 |
return error_msg, None, None
|
938 |
|
939 |
-
_HEADER_ = '''
|
940 |
-
<h2><b>GLB μ λλ©μ΄μ
μμ±κΈ° - 3D λͺ¨λΈ μμ§μ ν¨κ³Ό</b></h2>
|
941 |
-
|
942 |
-
μ΄ λ°λͺ¨λ₯Ό ν΅ν΄ μ μ μΈ 3D λͺ¨λΈ(GLB νμΌ)μ λ€μν μ λλ©μ΄μ
ν¨κ³Όλ₯Ό μ μ©ν μ μμ΅λλ€.
|
943 |
-
|
944 |
-
βοΈβοΈβοΈ**μ€μμ¬ν:**
|
945 |
-
- μ΄ λ°λͺ¨λ μ
λ‘λλ GLB νμΌμ μ λλ©μ΄μ
μ μ μ©ν©λλ€.
|
946 |
-
- λ€μν μ λλ©μ΄μ
μ€νμΌ μ€μμ μ ννμΈμ: νμ , λΆμ , νλ°, 쑰립, νμ€, μ€μ.
|
947 |
-
- κ²°κ³Όλ μ λλ©μ΄μ
λ GLB νμΌκ³Ό λ―Έλ¦¬λ³΄κΈ°μ© GIF νμΌλ‘ μ 곡λ©λλ€.
|
948 |
-
'''
|
949 |
-
|
950 |
-
_INFO_ = r"""
|
951 |
-
### μ λλ©μ΄μ
μ ν μ€λͺ
|
952 |
-
- **νμ (rotate)**: λͺ¨λΈμ΄ YμΆμ μ€μ¬μΌλ‘ νμ ν©λλ€.
|
953 |
-
- **λΆμ (float)**: λͺ¨λΈμ΄ μμλλ‘ λΆλλ½κ² λ λ€λλλ€.
|
954 |
-
- **νλ°(explode)**: λͺ¨λΈμ κ° λΆλΆμ΄ μ€μ¬μμ λ°κΉ₯μͺ½μΌλ‘ νΌμ Έλκ°λλ€.
|
955 |
-
- **쑰립(assemble)**: νλ° μ λλ©μ΄μ
μ λ°λ - λΆνλ€μ΄ ν¨κ» λͺ¨μ
λλ€.
|
956 |
-
- **νμ€(pulse)**: λͺ¨λΈμ΄ ν¬κΈ°κ° 컀μ‘λ€ μμμ‘λ€λ₯Ό λ°λ³΅ν©λλ€.
|
957 |
-
- **μ€μ(swing)**: λͺ¨λΈμ΄ μ’μ°λ‘ λΆλλ½κ² νλ€λ¦½λλ€.
|
958 |
-
|
959 |
-
### ν
|
960 |
-
- μ λλ©μ΄μ
κΈΈμ΄μ FPSλ₯Ό μ‘°μ νμ¬ μμ§μμ μλμ λΆλλ¬μμ μ‘°μ ν μ μμ΅λλ€.
|
961 |
-
- 볡μ‘ν λͺ¨λΈμ μ²λ¦¬ μκ°μ΄ λ μ€λ 걸릴 μ μμ΅λλ€.
|
962 |
-
- GIF 미리보기λ λΉ λ₯Έ μ°Έμ‘°μ©μ΄λ©°, κ³ νμ§ κ²°κ³Όλ₯Ό μν΄μλ μ λλ©μ΄μ
λ GLB νμΌμ λ€μ΄λ‘λνμΈμ.
|
963 |
-
"""
|
964 |
-
|
965 |
# Gradio μΈν°νμ΄μ€ μ€μ
|
966 |
-
|
967 |
-
|
968 |
-
|
969 |
-
|
970 |
-
|
971 |
-
|
972 |
-
|
973 |
-
|
974 |
-
|
975 |
-
|
976 |
-
|
977 |
-
|
978 |
-
|
979 |
-
|
980 |
-
|
981 |
-
|
982 |
-
|
983 |
-
|
984 |
-
|
985 |
-
|
986 |
-
|
987 |
-
|
988 |
-
|
989 |
-
|
990 |
-
|
991 |
-
|
992 |
-
|
993 |
-
|
994 |
-
|
995 |
-
|
996 |
-
|
997 |
-
|
998 |
-
|
999 |
-
|
1000 |
-
|
1001 |
-
|
1002 |
-
|
1003 |
-
|
1004 |
-
|
1005 |
-
|
1006 |
-
|
1007 |
-
|
1008 |
-
|
1009 |
-
|
1010 |
-
|
1011 |
-
|
1012 |
-
|
1013 |
-
|
1014 |
-
|
1015 |
-
|
1016 |
-
|
1017 |
-
|
1018 |
-
|
1019 |
-
|
1020 |
-
|
1021 |
-
|
1022 |
-
|
1023 |
-
|
1024 |
-
|
1025 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1026 |
|
1027 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1028 |
|
1029 |
-
#
|
1030 |
if __name__ == "__main__":
|
1031 |
-
demo =
|
1032 |
-
demo.launch(share=True, server_name="0.0.0.0", server_port=7860)
|
|
|
1 |
+
import os
|
2 |
+
import time
|
3 |
+
import glob
|
4 |
+
import json
|
5 |
+
import numpy as np
|
6 |
+
import trimesh
|
7 |
+
import argparse
|
8 |
+
from scipy.spatial.transform import Rotation
|
9 |
+
from PIL import Image, ImageDraw
|
10 |
+
import math
|
11 |
+
import trimesh.transformations as tf
|
12 |
+
|
13 |
+
os.environ['PYOPENGL_PLATFORM'] = 'egl'
|
14 |
+
|
15 |
+
import gradio as gr
|
16 |
+
import spaces
|
17 |
+
|
18 |
+
# κ²°κ³Ό μ μ₯ κ²½λ‘
|
19 |
+
LOG_PATH = './results/demo'
|
20 |
+
os.makedirs(LOG_PATH, exist_ok=True)
|
21 |
+
|
22 |
def create_textual_animation_gif(output_path, model_name, animation_type, duration=3.0, fps=30):
|
23 |
"""ν
μ€νΈ κΈ°λ°μ κ°λ¨ν μ λλ©μ΄μ
GIF μμ± - λ λλ§ μ€ν¨ μ λ체μ©"""
|
24 |
try:
|
|
|
105 |
return output_path
|
106 |
except Exception as e:
|
107 |
print(f"Error creating textual animation: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
return None
|
109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
@spaces.GPU
|
111 |
def process_3d_model(input_3d, animation_type, animation_duration, fps):
|
112 |
"""Process a 3D model and apply animation"""
|
113 |
print(f"Processing: {input_3d} with animation type: {animation_type}")
|
114 |
|
115 |
try:
|
116 |
+
# ν
μ€νΈ κΈ°λ° μ λλ©μ΄μ
GIF μμ± (λ λλ§ μ€ν¨λ₯Ό μ°λ €νμ¬ νμ μμ±)
|
117 |
+
base_filename = os.path.basename(input_3d).rsplit('.', 1)[0]
|
118 |
+
text_gif_path = os.path.join(LOG_PATH, f'text_animated_{base_filename}.gif')
|
119 |
+
animated_gif_path = create_textual_animation_gif(
|
120 |
+
text_gif_path,
|
121 |
+
os.path.basename(input_3d),
|
122 |
+
animation_type,
|
123 |
+
animation_duration,
|
124 |
+
fps
|
125 |
)
|
126 |
|
127 |
+
# μλ³Έ GLB νμΌ λ³΅μ¬
|
128 |
+
copy_glb_path = os.path.join(LOG_PATH, f'copy_{base_filename}.glb')
|
129 |
+
import shutil
|
130 |
+
try:
|
131 |
+
shutil.copy(input_3d, copy_glb_path)
|
132 |
+
animated_glb_path = copy_glb_path
|
133 |
+
print(f"Copied original GLB to {copy_glb_path}")
|
134 |
+
except Exception as e:
|
135 |
+
print(f"Error copying GLB: {e}")
|
136 |
+
animated_glb_path = input_3d
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
|
138 |
+
# λ©νλ°μ΄ν° μμ±
|
139 |
metadata = {
|
140 |
"animation_type": animation_type,
|
141 |
"duration": animation_duration,
|
|
|
144 |
"created_at": time.strftime("%Y-%m-%d %H:%M:%S")
|
145 |
}
|
146 |
|
147 |
+
json_path = os.path.join(LOG_PATH, f'metadata_{base_filename}.json')
|
148 |
with open(json_path, 'w') as f:
|
149 |
json.dump(metadata, f, indent=4)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
return animated_glb_path, animated_gif_path, json_path
|
152 |
except Exception as e:
|
153 |
error_msg = f"Error processing file: {str(e)}"
|
154 |
print(error_msg)
|
155 |
return error_msg, None, None
|
156 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
# Gradio μΈν°νμ΄μ€ μ€μ
|
158 |
+
with gr.Blocks(title="GLB μ λλ©μ΄μ
μμ±κΈ°") as demo:
|
159 |
+
# μ λͺ© μΉμ
|
160 |
+
gr.Markdown("""
|
161 |
+
<h2><b>GLB μ λλ©μ΄μ
μμ±κΈ° - 3D λͺ¨λΈ μμ§μ ν¨κ³Ό</b></h2>
|
162 |
+
|
163 |
+
μ΄ λ°λͺ¨λ₯Ό ν΅ν΄ μ μ μΈ 3D λͺ¨λΈ(GLB νμΌ)μ λ€μν μ λλ©μ΄μ
ν¨κ³Όλ₯Ό μ μ©ν μ μμ΅λλ€.
|
164 |
+
|
165 |
+
βοΈβοΈβοΈ**μ€μμ¬ν:**
|
166 |
+
- μ΄ λ°λͺ¨λ μ
λ‘λλ GLB νμΌμ μ λλ©μ΄μ
μ μ μ©ν©λλ€.
|
167 |
+
- λ€μν μ λλ©μ΄μ
μ€νμΌ μ€μμ μ ννμΈμ: νμ , λΆμ , νλ°, 쑰립, νμ€, μ€μ.
|
168 |
+
- κ²°κ³Όλ μ λλ©μ΄μ
λ GLB νμΌκ³Ό λ―Έλ¦¬λ³΄κΈ°μ© GIF νμΌλ‘ μ 곡λ©λλ€.
|
169 |
+
""")
|
170 |
+
|
171 |
+
with gr.Row():
|
172 |
+
with gr.Column():
|
173 |
+
# μ
λ ₯ μ»΄ν¬λνΈ
|
174 |
+
input_3d = gr.Model3D(label="3D λͺ¨λΈ νμΌ μ
λ‘λ (GLB ν¬λ§·)")
|
175 |
+
|
176 |
+
with gr.Row():
|
177 |
+
animation_type = gr.Dropdown(
|
178 |
+
label="μ λλ©μ΄μ
μ ν",
|
179 |
+
choices=["rotate", "float", "explode", "assemble", "pulse", "swing"],
|
180 |
+
value="rotate"
|
181 |
+
)
|
182 |
+
|
183 |
+
with gr.Row():
|
184 |
+
animation_duration = gr.Slider(
|
185 |
+
label="μ λλ©μ΄μ
κΈΈμ΄ (μ΄)",
|
186 |
+
minimum=1.0,
|
187 |
+
maximum=10.0,
|
188 |
+
value=3.0,
|
189 |
+
step=0.5
|
190 |
+
)
|
191 |
+
fps = gr.Slider(
|
192 |
+
label="μ΄λΉ νλ μ μ",
|
193 |
+
minimum=15,
|
194 |
+
maximum=60,
|
195 |
+
value=30,
|
196 |
+
step=1
|
197 |
+
)
|
198 |
+
|
199 |
+
submit_btn = gr.Button("λͺ¨λΈ μ²λ¦¬ λ° μ λλ©μ΄μ
μμ±")
|
200 |
+
|
201 |
+
with gr.Column():
|
202 |
+
# μΆλ ₯ μ»΄ν¬λνΈ
|
203 |
+
output_3d = gr.Model3D(label="μ λλ©μ΄μ
μ μ©λ 3D λͺ¨λΈ")
|
204 |
+
output_gif = gr.Image(label="μ λλ©μ΄μ
미리보기 (GIF)")
|
205 |
+
output_json = gr.File(label="λ©νλ°μ΄ν° νμΌ λ€μ΄λ‘λ")
|
206 |
+
|
207 |
+
# μ λλ©μ΄μ
μ ν μ€λͺ
|
208 |
+
gr.Markdown("""
|
209 |
+
### μ λλ©μ΄μ
μ ν μ€λͺ
|
210 |
+
- **νμ (rotate)**: λͺ¨λΈμ΄ YμΆμ μ€μ¬μΌλ‘ νμ ν©λλ€.
|
211 |
+
- **λΆμ (float)**: λͺ¨λΈμ΄ μμλλ‘ λΆλλ½κ² λ λ€λλλ€.
|
212 |
+
- **νλ°(explode)**: λͺ¨λΈμ κ° λΆλΆμ΄ μ€μ¬μμ λ°κΉ₯μͺ½μΌλ‘ νΌμ Έλκ°λλ€.
|
213 |
+
- **쑰립(assemble)**: νλ° μ λλ©μ΄μ
μ λ°λ - λΆνλ€μ΄ ν¨κ» λͺ¨μ
λλ€.
|
214 |
+
- **νμ€(pulse)**: λͺ¨λΈμ΄ ν¬κΈ°κ° 컀μ‘λ€ μμμ‘λ€λ₯Ό λ°λ³΅ν©λλ€.
|
215 |
+
- **μ€μ(swing)**: λͺ¨λΈμ΄ μ’μ°λ‘ λΆλλ½κ² νλ€λ¦½λλ€.
|
216 |
+
|
217 |
+
### ν
|
218 |
+
- μ λλ©μ΄μ
κΈΈμ΄μ FPSλ₯Ό μ‘°μ νμ¬ μμ§μμ μλμ λΆλλ¬μμ μ‘°μ ν μ μμ΅λλ€.
|
219 |
+
- 볡μ‘ν λͺ¨λΈμ μ²λ¦¬ μκ°μ΄ λ μ€λ 걸릴 μ μμ΅λλ€.
|
220 |
+
- GIF 미리보기λ λΉ λ₯Έ μ°Έμ‘°μ©μ΄λ©°, κ³ νμ§ κ²°κ³Όλ₯Ό μν΄μλ μ λλ©μ΄μ
λ GLB νμΌμ λ€μ΄λ‘λνμΈμ.
|
221 |
+
""")
|
222 |
+
|
223 |
+
# λ²νΌ λμ μ€μ
|
224 |
+
submit_btn.click(
|
225 |
+
fn=process_3d_model,
|
226 |
+
inputs=[input_3d, animation_type, animation_duration, fps],
|
227 |
+
outputs=[output_3d, output_gif, output_json]
|
228 |
+
)
|
229 |
|
230 |
+
# μμ μ€λΉ
|
231 |
+
example_files = [[f] for f in glob.glob('./data/demo_glb/*.glb')]
|
232 |
+
if example_files:
|
233 |
+
gr.Examples(
|
234 |
+
examples=example_files,
|
235 |
+
inputs=[input_3d],
|
236 |
+
examples_per_page=10,
|
237 |
+
)
|
238 |
|
239 |
+
# μ± μ€ν
|
240 |
if __name__ == "__main__":
|
241 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|