3D-VIDEO / app.py
ginipick's picture
Update app.py
61b805a verified
raw
history blame
44 kB
def create_textual_animation_gif(output_path, model_name, animation_type, duration=3.0, fps=30):
"""ν…μŠ€νŠΈ 기반의 κ°„λ‹¨ν•œ μ• λ‹ˆλ©”μ΄μ…˜ GIF 생성 - λ Œλ”λ§ μ‹€νŒ¨ μ‹œ λŒ€μ²΄μš©"""
try:
# κ°„λ‹¨ν•œ ν”„λ ˆμž„ μ‹œν€€μŠ€ 생성
frames = []
num_frames = int(duration * fps)
if num_frames > 60: # λ„ˆλ¬΄ λ§Žμ€ ν”„λ ˆμž„μ€ νš¨μœ¨μ μ΄μ§€ μ•ŠμŒ
num_frames = 60
for i in range(num_frames):
t = i / (num_frames - 1) # 0~1 λ²”μœ„
angle = t * 360 # 전체 νšŒμ „
# μƒˆ 이미지 생성
img = Image.new('RGB', (640, 480), color=(240, 240, 240))
draw = ImageDraw.Draw(img)
# 정보 ν…μŠ€νŠΈ
draw.text((50, 50), f"Model: {os.path.basename(model_name)}", fill=(0, 0, 0))
draw.text((50, 100), f"Animation Type: {animation_type}", fill=(0, 0, 0))
draw.text((50, 150), f"Frame: {i+1}/{num_frames}", fill=(0, 0, 0))
# μ• λ‹ˆλ©”μ΄μ…˜ μœ ν˜•μ— λ”°λ₯Έ μ‹œκ°μ  효과
center_x, center_y = 320, 240
if animation_type == 'rotate':
# νšŒμ „ν•˜λŠ” μ‚¬κ°ν˜•
radius = 100
x = center_x + radius * math.cos(math.radians(angle))
y = center_y + radius * math.sin(math.radians(angle))
draw.rectangle((x-40, y-40, x+40, y+40), outline=(0, 0, 0), fill=(255, 0, 0))
elif animation_type == 'float':
# μœ„μ•„λž˜λ‘œ μ›€μ§μ΄λŠ” 원
offset_y = 50 * math.sin(2 * math.pi * t)
draw.ellipse((center_x-50, center_y-50+offset_y, center_x+50, center_y+50+offset_y),
outline=(0, 0, 0), fill=(0, 0, 255))
elif animation_type == 'explode' or animation_type == 'assemble':
# λ°”κΉ₯μͺ½/μ•ˆμͺ½μœΌλ‘œ μ›€μ§μ΄λŠ” μ—¬λŸ¬ λ„ν˜•
scale = t if animation_type == 'explode' else 1 - t
for j in range(8):
angle_j = j * 45
dist = 120 * scale
x = center_x + dist * math.cos(math.radians(angle_j))
y = center_y + dist * math.sin(math.radians(angle_j))
if j % 3 == 0:
draw.rectangle((x-20, y-20, x+20, y+20), outline=(0, 0, 0), fill=(255, 0, 0))
elif j % 3 == 1:
draw.ellipse((x-20, y-20, x+20, y+20), outline=(0, 0, 0), fill=(0, 255, 0))
else:
draw.polygon([(x, y-20), (x+20, y+20), (x-20, y+20)], outline=(0, 0, 0), fill=(0, 0, 255))
elif animation_type == 'pulse':
# 크기가 λ³€ν•˜λŠ” 원
scale = 0.5 + 0.5 * math.sin(2 * math.pi * t)
radius = 100 * scale
draw.ellipse((center_x-radius, center_y-radius, center_x+radius, center_y+radius),
outline=(0, 0, 0), fill=(0, 255, 0))
elif animation_type == 'swing':
# 쒌우둜 μ›€μ§μ΄λŠ” μ‚Όκ°ν˜•
angle_offset = 30 * math.sin(2 * math.pi * t)
points = [
(center_x + 100 * math.cos(math.radians(angle_offset)), center_y - 80),
(center_x + 100 * math.cos(math.radians(120 + angle_offset)), center_y + 40),
(center_x + 100 * math.cos(math.radians(240 + angle_offset)), center_y + 40)
]
draw.polygon(points, outline=(0, 0, 0), fill=(255, 165, 0))
# ν”„λ ˆμž„ μΆ”κ°€
frames.append(img)
# GIF둜 μ €μž₯
frames[0].save(
output_path,
save_all=True,
append_images=frames[1:],
optimize=False,
duration=int(1000 / fps),
loop=0
)
print(f"Created textual animation GIF at {output_path}")
return output_path
except Exception as e:
print(f"Error creating textual animation: {str(e)}")
return Nonedef create_simple_animation_frames(input_mesh_path, animation_type='rotate', num_frames=30):
"""κ°„λ‹¨ν•œ λ°©μ‹μœΌλ‘œ μ• λ‹ˆλ©”μ΄μ…˜ ν”„λ ˆμž„ 생성 - λ‹€λ₯Έ 방법듀이 μ‹€νŒ¨ν•  경우 λŒ€μ²΄μš©"""
try:
# λ©”μ‹œ λ‘œλ“œ
mesh = trimesh.load(input_mesh_path)
# κ°€μž₯ 기본적인 ν˜•νƒœλ‘œ λ©”μ‹œ/씬 μΆ”μΆœ
if isinstance(mesh, trimesh.Scene):
# μ”¬μ—μ„œ 첫 번째 λ©”μ‹œ μΆ”μΆœ
geometries = list(mesh.geometry.values())
if geometries:
base_mesh = geometries[0]
else:
print("No geometries found in scene")
return None
else:
base_mesh = mesh
# μ• λ‹ˆλ©”μ΄μ…˜ ν”„λ ˆμž„ μ€€λΉ„
frames = []
# λ‹¨μˆœ νšŒμ „ μ• λ‹ˆλ©”μ΄μ…˜ 생성
for i in range(num_frames):
angle = i * 2 * math.pi / num_frames
# μƒˆ 씬 생성
scene = trimesh.Scene()
# λ©”μ‹œ 볡사 및 νšŒμ „
rotated_mesh = base_mesh.copy()
rotation = tf.rotation_matrix(angle, [0, 1, 0])
rotated_mesh.apply_transform(rotation)
# 씬에 μΆ”κ°€
scene.add_geometry(rotated_mesh)
# 카메라 μ„€μ •
scene.set_camera()
frames.append(scene)
return frames
except Exception as e:
print(f"Error in simple animation: {str(e)}")
return Noneimport os
import time
import glob
import json
import numpy as np
import trimesh
import argparse
from scipy.spatial.transform import Rotation
import PIL.Image
from PIL import Image
import math
import trimesh.transformations as tf
from trimesh.exchange.gltf import export_glb
os.environ['PYOPENGL_PLATFORM'] = 'egl'
import gradio as gr
import spaces
def parse_args():
parser = argparse.ArgumentParser(description='Create animations for 3D models')
parser.add_argument(
'--input',
type=str,
default='./data/demo_glb/',
help='Input file or directory path (default: ./data/demo_glb/)'
)
parser.add_argument(
'--log_path',
type=str,
default='./results/demo',
help='Output directory path (default: results/demo)'
)
parser.add_argument(
'--animation_type',
type=str,
default='rotate',
choices=['rotate', 'float', 'explode', 'assemble', 'pulse', 'swing'],
help='Type of animation to apply'
)
parser.add_argument(
'--animation_duration',
type=float,
default=3.0,
help='Duration of animation in seconds'
)
parser.add_argument(
'--fps',
type=int,
default=30,
help='Frames per second for animation'
)
return parser.parse_args()
def get_input_files(input_path):
if os.path.isfile(input_path):
return [input_path]
elif os.path.isdir(input_path):
return glob.glob(os.path.join(input_path, '*'))
else:
raise ValueError(f"Input path {input_path} is neither a file nor a directory")
args = parse_args()
LOG_PATH = args.log_path
os.makedirs(LOG_PATH, exist_ok=True)
print(f"Output directory: {LOG_PATH}")
def normalize_mesh(mesh):
"""Normalize mesh to fit in a unit cube centered at origin"""
try:
if isinstance(mesh, trimesh.Scene):
# Scene 객체 처리
# μ”¬μ—μ„œ λͺ¨λ“  λ©”μ‹œ μΆ”μΆœ
meshes = []
for geometry in mesh.geometry.values():
if isinstance(geometry, trimesh.Trimesh):
meshes.append(geometry)
if not meshes:
print("Warning: No meshes found in scene during normalization")
return mesh, np.zeros(3), 1.0
# λͺ¨λ“  λ©”μ‹œμ˜ 정점을 κ²°ν•©ν•˜μ—¬ 경계 μƒμž 계산
try:
all_vertices = np.vstack([m.vertices for m in meshes if hasattr(m, 'vertices') and m.vertices.shape[0] > 0])
if len(all_vertices) == 0:
print("Warning: No vertices found in meshes during normalization")
return mesh, np.zeros(3), 1.0
bounds = np.array([all_vertices.min(axis=0), all_vertices.max(axis=0)])
center = (bounds[0] + bounds[1]) / 2
scale_value = (bounds[1] - bounds[0]).max()
if scale_value < 1e-10:
print("Warning: Mesh is too small, using default scale")
scale = 1.0
else:
scale = 1.0 / scale_value
# 각 λ©”μ‹œλ₯Ό μ •κ·œν™”ν•˜μ—¬ μƒˆ 씬 생성
normalized_scene = trimesh.Scene()
for mesh_idx, mesh_obj in enumerate(meshes):
normalized_mesh = mesh_obj.copy()
try:
normalized_mesh.vertices = (normalized_mesh.vertices - center) * scale
normalized_scene.add_geometry(normalized_mesh, node_name=f"normalized_mesh_{mesh_idx}")
except Exception as e:
print(f"Error normalizing mesh {mesh_idx}: {str(e)}")
# 였λ₯˜κ°€ λ°œμƒν•˜λ©΄ 원본 λ©”μ‹œ μΆ”κ°€
normalized_scene.add_geometry(mesh_obj, node_name=f"original_mesh_{mesh_idx}")
return normalized_scene, center, scale
except Exception as e:
print(f"Error during scene normalization: {str(e)}")
return mesh, np.zeros(3), 1.0
else:
# 일반 Trimesh 객체 처리
if not hasattr(mesh, 'vertices') or mesh.vertices.shape[0] == 0:
print("Warning: Mesh has no vertices")
return mesh, np.zeros(3), 1.0
vertices = mesh.vertices
bounds = np.array([vertices.min(axis=0), vertices.max(axis=0)])
center = (bounds[0] + bounds[1]) / 2
scale_value = (bounds[1] - bounds[0]).max()
if scale_value < 1e-10:
print("Warning: Mesh is too small, using default scale")
scale = 1.0
else:
scale = 1.0 / scale_value
# 볡사본 μƒμ„±ν•˜μ—¬ 원본 λ³€κ²½ λ°©μ§€
normalized_mesh = mesh.copy()
normalized_mesh.vertices = (vertices - center) * scale
return normalized_mesh, center, scale
except Exception as e:
print(f"Unexpected error in normalize_mesh: {str(e)}")
# 였λ₯˜ λ°œμƒ μ‹œ 원본 λ°˜ν™˜
return mesh, np.zeros(3), 1.0
def create_rotation_animation(mesh, duration=3.0, fps=30):
"""Create a rotation animation around the Y axis"""
num_frames = int(duration * fps)
frames = []
# Normalize the mesh for consistent animation
try:
mesh, original_center, original_scale = normalize_mesh(mesh)
print(f"Normalized mesh center: {original_center}, scale: {original_scale}")
except Exception as e:
print(f"Error normalizing mesh: {str(e)}")
# μ •κ·œν™”μ— μ‹€νŒ¨ν•˜λ”λΌλ„ 계속 μ§„ν–‰
pass
for frame_idx in range(num_frames):
t = frame_idx / (num_frames - 1) # Normalized time [0, 1]
angle = t * 2 * math.pi # Full rotation
if isinstance(mesh, trimesh.Scene):
# Scene 객체인 경우 각 λ©”μ‹œμ— νšŒμ „ 적용
frame_scene = trimesh.Scene()
for node_name, transform, geometry_name in mesh.graph.nodes_geometry:
# λ©”μ‹œλ₯Ό λ³΅μ‚¬ν•˜κ³  νšŒμ „ 적용
mesh_copy = mesh.geometry[geometry_name].copy()
# λ©”μ‹œμ˜ 쀑심점 계산
center_point = mesh_copy.centroid if hasattr(mesh_copy, 'centroid') else np.zeros(3)
# νšŒμ „ ν–‰λ ¬ 생성
rotation_matrix = tf.rotation_matrix(angle, [0, 1, 0], center_point)
# λ³€ν™˜ 적용
mesh_copy.apply_transform(rotation_matrix)
# 씬에 μΆ”κ°€
frame_scene.add_geometry(mesh_copy, node_name=node_name)
frames.append(frame_scene)
else:
# 일반 Trimesh 객체인 경우 직접 νšŒμ „ 적용
animated_mesh = mesh.copy()
rotation_matrix = tf.rotation_matrix(angle, [0, 1, 0])
animated_mesh.apply_transform(rotation_matrix)
frames.append(animated_mesh)
return frames
def create_float_animation(mesh, duration=3.0, fps=30, amplitude=0.2):
"""Create a floating animation where the mesh moves up and down"""
num_frames = int(duration * fps)
frames = []
# Normalize the mesh for consistent animation
mesh, original_center, original_scale = normalize_mesh(mesh)
for frame_idx in range(num_frames):
t = frame_idx / (num_frames - 1) # Normalized time [0, 1]
y_offset = amplitude * math.sin(2 * math.pi * t)
if isinstance(mesh, trimesh.Scene):
# Scene 객체인 경우
frame_scene = trimesh.Scene()
for node_name, transform, geometry_name in mesh.graph.nodes_geometry:
# λ©”μ‹œλ₯Ό 볡사
mesh_copy = mesh.geometry[geometry_name].copy()
# λ³€ν™˜ 적용
translation_matrix = tf.translation_matrix([0, y_offset, 0])
mesh_copy.apply_transform(translation_matrix)
# 씬에 μΆ”κ°€
frame_scene.add_geometry(mesh_copy, node_name=node_name)
frames.append(frame_scene)
else:
# 일반 Trimesh 객체인 경우
animated_mesh = mesh.copy()
translation_matrix = tf.translation_matrix([0, y_offset, 0])
animated_mesh.apply_transform(translation_matrix)
frames.append(animated_mesh)
return frames
def create_explode_animation(mesh, duration=3.0, fps=30):
"""Create an explode animation where parts of the mesh move outward"""
num_frames = int(duration * fps)
frames = []
# Normalize the mesh for consistent animation
mesh, original_center, original_scale = normalize_mesh(mesh)
# 씬인 경우 뢀뢄별 처리
if isinstance(mesh, trimesh.Scene):
for frame_idx in range(num_frames):
t = frame_idx / (num_frames - 1) # Normalized time [0, 1]
# μƒˆ 씬 생성
frame_scene = trimesh.Scene()
# 각 λ…Έλ“œλ³„ 폭발 μ• λ‹ˆλ©”μ΄μ…˜ 적용
for node_name, transform, geometry_name in mesh.graph.nodes_geometry:
mesh_copy = mesh.geometry[geometry_name].copy()
# λ…Έλ“œμ˜ 쀑심점 계산
center_point = mesh_copy.centroid if hasattr(mesh_copy, 'centroid') else np.zeros(3)
# λ°©ν–₯ 벑터 (μ›μ μ—μ„œ 객체 μ€‘μ‹¬κΉŒμ§€)
direction = center_point
if np.linalg.norm(direction) < 1e-10:
# 쀑심점이 원점과 λ„ˆλ¬΄ κ°€κΉŒμš°λ©΄ 랜덀 λ°©ν–₯ 선택
direction = np.random.rand(3) - 0.5
direction = direction / np.linalg.norm(direction)
# 폭발 이동 적용
translation = direction * t * 0.5 # 폭발 강도 쑰절
translation_matrix = tf.translation_matrix(translation)
mesh_copy.apply_transform(translation_matrix)
# 씬에 μΆ”κ°€
frame_scene.add_geometry(mesh_copy, node_name=f"{node_name}_{frame_idx}")
frames.append(frame_scene)
else:
# λ©”μ‹œλ₯Ό μ—¬λŸ¬ λΆ€λΆ„μœΌλ‘œ λΆ„ν• 
try:
components = mesh.split(only_watertight=False)
if len(components) <= 1:
# μ»΄ν¬λ„ŒνŠΈκ°€ ν•˜λ‚˜λΏμ΄λ©΄ 정점 기반 폭발 μ• λ‹ˆλ©”μ΄μ…˜ μ‚¬μš©
raise ValueError("Mesh cannot be split into components")
except:
# 뢄할이 μ‹€νŒ¨ν•˜λ©΄ 정점 기반 폭발 μ• λ‹ˆλ©”μ΄μ…˜ μ‚¬μš©
for frame_idx in range(num_frames):
t = frame_idx / (num_frames - 1) # Normalized time [0, 1]
# λ©”μ‹œ 볡사
animated_mesh = mesh.copy()
vertices = animated_mesh.vertices.copy()
# 각 정점을 μ€‘μ‹¬μ—μ„œ λ°”κΉ₯μͺ½μœΌλ‘œ 이동
directions = vertices.copy()
norms = np.linalg.norm(directions, axis=1, keepdims=True)
mask = norms > 1e-10
directions[mask] = directions[mask] / norms[mask]
directions[~mask] = np.random.rand(np.sum(~mask), 3) - 0.5
# 폭발 κ³„μˆ˜ 적용
vertices += directions * t * 0.3
animated_mesh.vertices = vertices
frames.append(animated_mesh)
else:
# μ»΄ν¬λ„ŒνŠΈ 기반 폭발 μ• λ‹ˆλ©”μ΄μ…˜
for frame_idx in range(num_frames):
t = frame_idx / (num_frames - 1) # Normalized time [0, 1]
# μƒˆ 씬 생성
scene = trimesh.Scene()
# 각 μ»΄ν¬λ„ŒνŠΈλ₯Ό μ€‘μ‹¬μ—μ„œ λ°”κΉ₯μͺ½μœΌλ‘œ 이동
for i, component in enumerate(components):
# μ»΄ν¬λ„ŒνŠΈ 볡사
animated_component = component.copy()
# μ»΄ν¬λ„ŒνŠΈ μ€‘μ‹¬μ μ—μ„œ λ°©ν–₯ 계산
direction = animated_component.centroid
if np.linalg.norm(direction) < 1e-10:
# 쀑심점이 원점과 λ„ˆλ¬΄ κ°€κΉŒμš°λ©΄ 랜덀 λ°©ν–₯ 선택
direction = np.random.rand(3) - 0.5
direction = direction / np.linalg.norm(direction)
# 폭발 이동 적용
translation = direction * t * 0.5
translation_matrix = tf.translation_matrix(translation)
animated_component.apply_transform(translation_matrix)
# 씬에 μΆ”κ°€
scene.add_geometry(animated_component, node_name=f"component_{i}")
# 씬을 단일 λ©”μ‹œλ‘œ λ³€ν™˜ (κ·Όμ‚¬μΉ˜)
animated_mesh = trimesh.util.concatenate(scene.dump())
frames.append(animated_mesh)
return frames
def create_assemble_animation(mesh, duration=3.0, fps=30):
"""Create an assembly animation (reverse of explode)"""
# Get explode animation and reverse it
explode_frames = create_explode_animation(mesh, duration, fps)
return list(reversed(explode_frames))
def create_pulse_animation(mesh, duration=3.0, fps=30, min_scale=0.8, max_scale=1.2):
"""Create a pulsing animation where the mesh scales up and down"""
num_frames = int(duration * fps)
frames = []
# Normalize the mesh for consistent animation
mesh, original_center, original_scale = normalize_mesh(mesh)
for frame_idx in range(num_frames):
t = frame_idx / (num_frames - 1) # Normalized time [0, 1]
# Create a copy of the mesh to animate
animated_mesh = mesh.copy()
# Apply pulsing motion (sinusoidal scale)
scale_factor = min_scale + (max_scale - min_scale) * (0.5 + 0.5 * math.sin(2 * math.pi * t))
scale_matrix = tf.scale_matrix(scale_factor)
animated_mesh.apply_transform(scale_matrix)
# Add to frames
frames.append(animated_mesh)
return frames
def create_swing_animation(mesh, duration=3.0, fps=30, max_angle=math.pi/6):
"""Create a swinging animation where the mesh rotates back and forth"""
num_frames = int(duration * fps)
frames = []
# Normalize the mesh for consistent animation
mesh, original_center, original_scale = normalize_mesh(mesh)
for frame_idx in range(num_frames):
t = frame_idx / (num_frames - 1) # Normalized time [0, 1]
# Create a copy of the mesh to animate
animated_mesh = mesh.copy()
# Apply swinging motion (sinusoidal rotation)
angle = max_angle * math.sin(2 * math.pi * t)
rotation_matrix = tf.rotation_matrix(angle, [0, 1, 0])
animated_mesh.apply_transform(rotation_matrix)
# Add to frames
frames.append(animated_mesh)
return frames
def generate_gif_from_frames(frames, output_path, fps=30, resolution=(640, 480), background_color=(255, 255, 255, 255)):
"""Generate a GIF from animation frames"""
gif_frames = []
# λͺ¨λ“  ν”„λ ˆμž„μ˜ 경계 μƒμžλ₯Ό κ³„μ‚°ν•˜μ—¬ μΌκ΄€λœ 카메라 μ„€μ • ꡬ성
all_bounds = []
for frame in frames:
if isinstance(frame, trimesh.Scene):
# μ”¬μ—μ„œ λͺ¨λ“  λ©”μ‹œμ˜ 정점을 μΆ”μΆœν•˜μ—¬ κ²½κ³„μƒμž 계산
all_points = []
for geom in frame.geometry.values():
if hasattr(geom, 'vertices') and geom.vertices.shape[0] > 0:
all_points.append(geom.vertices)
if all_points:
all_vertices = np.vstack(all_points)
bounds = np.array([all_vertices.min(axis=0), all_vertices.max(axis=0)])
all_bounds.append(bounds)
elif hasattr(frame, 'vertices') and frame.vertices.shape[0] > 0:
# 일반 λ©”μ‹œμ—μ„œ κ²½κ³„μƒμž 계산
bounds = np.array([frame.vertices.min(axis=0), frame.vertices.max(axis=0)])
all_bounds.append(bounds)
# λͺ¨λ“  ν”„λ ˆμž„μ„ ν¬ν•¨ν•˜λŠ” 전체 κ²½κ³„μƒμž 계산
if all_bounds:
min_corner = np.min(np.array([b[0] for b in all_bounds]), axis=0)
max_corner = np.max(np.array([b[1] for b in all_bounds]), axis=0)
total_bounds = np.array([min_corner, max_corner])
# κ²½κ³„μƒμž 쀑심과 크기 계산
center = (total_bounds[0] + total_bounds[1]) / 2
size = np.max(total_bounds[1] - total_bounds[0])
# 카메라 μœ„μΉ˜ 계산 (μ μ ˆν•œ κ±°λ¦¬μ—μ„œ 객체 바라보기)
camera_distance = size * 2.5 # 객체 크기의 2.5배 거리
else:
# κ²½κ³„μƒμžλ₯Ό 계산할 수 μ—†μœΌλ©΄ κΈ°λ³Έκ°’ μ‚¬μš©
center = np.zeros(3)
camera_distance = 2.0
# 각 ν”„λ ˆμž„ λ Œλ”λ§
for i, frame in enumerate(frames):
try:
# 씬 객체 생성
if not isinstance(frame, trimesh.Scene):
scene = trimesh.Scene(frame)
else:
scene = frame
# 더 κ°•λ ₯ν•œ 카메라 μ„€μ • 방법
try:
# 객체가 λ³΄μ΄λŠ” μœ„μΉ˜μ— 카메라 μ„€μ •
camera_fov = 60.0 # μ‹œμ•Όκ° (각도)
camera_pos = np.array([0, 0, camera_distance]) # 카메라 μœ„μΉ˜
# 카메라 λ³€ν™˜ ν–‰λ ¬ 생성
camera_transform = np.eye(4)
camera_transform[:3, 3] = camera_pos
# 카메라가 원점을 바라보도둝 μ„€μ •
scene.camera = trimesh.scene.Camera(
resolution=resolution,
fov=[camera_fov, camera_fov * (resolution[1] / resolution[0])]
)
scene.camera_transform = camera_transform
except Exception as e:
print(f"Error setting camera: {str(e)}")
# κΈ°λ³Έ 카메라 μ„€μ •
scene.set_camera(angles=(0, 0, 0), distance=camera_distance)
# λ Œλ”λ§ μ‹œλ„
try:
# PyOpenGL 경고 숨기기
import warnings
warnings.filterwarnings("ignore", category=UserWarning)
# 씬 λ Œλ”λ§
rendered_img = scene.save_image(resolution=resolution, background=background_color)
pil_img = Image.open(rendered_img)
# 이미지가 λΉ„μ–΄μžˆλŠ”μ§€ 확인
if np.array(pil_img).std() < 1.0: # ν‘œμ€€νŽΈμ°¨κ°€ μž‘μœΌλ©΄ λŒ€λΆ€λΆ„ λ™μΌν•œ 색상 (빈 이미지)
print(f"Warning: Frame {i} seems to be empty, trying different angle")
# λ‹€λ₯Έ κ°λ„μ—μ„œ λ‹€μ‹œ μ‹œλ„
scene.set_camera(angles=(np.pi/4, np.pi/4, 0), distance=camera_distance*1.2)
rendered_img = scene.save_image(resolution=resolution, background=background_color)
pil_img = Image.open(rendered_img)
gif_frames.append(pil_img)
except Exception as e:
print(f"Error in main rendering: {str(e)}")
# μ˜€ν”„μŠ€ν¬λ¦° λ Œλ”λ§ μ‹€νŒ¨ μ‹œ κ°„λ‹¨ν•œ λ°©λ²•μœΌλ‘œ μ‹œλ„
try:
# λŒ€μ²΄ λ Œλ”λ§ λ©”μ„œλ“œ
from PIL import Image, ImageDraw
img = Image.new('RGB', resolution, color=(255, 255, 255))
draw = ImageDraw.Draw(img)
# ν”„λ ˆμž„ 번호λ₯Ό ν…μŠ€νŠΈλ‘œ ν‘œμ‹œ
draw.text((resolution[0]//2, resolution[1]//2), f"Frame {i}", fill=(0, 0, 0))
gif_frames.append(img)
except Exception as e2:
print(f"Error in fallback rendering: {str(e2)}")
gif_frames.append(Image.new('RGB', resolution, (255, 255, 255)))
except Exception as e:
print(f"Unexpected error in frame processing: {str(e)}")
gif_frames.append(Image.new('RGB', resolution, (255, 255, 255)))
# GIF μ €μž₯
if gif_frames:
try:
gif_frames[0].save(
output_path,
save_all=True,
append_images=gif_frames[1:],
optimize=False,
duration=int(1000 / fps),
loop=0
)
print(f"GIF saved to {output_path}")
# 확인을 μœ„ν•΄ 첫 ν”„λ ˆμž„λ„ λ”°λ‘œ μ €μž₯
first_frame_path = output_path.replace('.gif', '_first_frame.png')
gif_frames[0].save(first_frame_path)
print(f"First frame saved to {first_frame_path}")
return output_path
except Exception as e:
print(f"Error saving GIF: {str(e)}")
return None
else:
print("No frames to save")
return None
def create_animation_mesh(input_mesh_path, animation_type='rotate', duration=3.0, fps=30):
"""Create animation from input mesh based on animation type"""
# Load the mesh
try:
print(f"Loading mesh from {input_mesh_path}")
# λ¨Όμ € Scene으둜 λ‘œλ“œ μ‹œλ„
loaded_obj = trimesh.load(input_mesh_path)
print(f"Loaded object type: {type(loaded_obj)}")
# Scene인 경우 단일 λ©”μ‹œλ‘œ λ³€ν™˜ μ‹œλ„
if isinstance(loaded_obj, trimesh.Scene):
print("Loaded a scene, extracting meshes...")
# μ”¬μ—μ„œ λͺ¨λ“  λ©”μ‹œ μΆ”μΆœ
meshes = []
for geometry_name, geometry in loaded_obj.geometry.items():
if isinstance(geometry, trimesh.Trimesh):
print(f"Found mesh: {geometry_name} with {len(geometry.vertices)} vertices")
meshes.append(geometry)
if not meshes:
print("No meshes found in scene, trying simple animation...")
frames = create_simple_animation_frames(input_mesh_path, animation_type, int(duration * fps))
if not frames:
print("Simple animation failed too")
return None, None
else:
# λͺ¨λ“  λ©”μ‹œλ₯Ό ν•˜λ‚˜λ‘œ κ²°ν•©
mesh = loaded_obj
print(f"Using original scene with {len(meshes)} meshes")
elif isinstance(loaded_obj, trimesh.Trimesh):
mesh = loaded_obj
print(f"Loaded a single mesh with {len(mesh.vertices)} vertices")
else:
print(f"Unsupported object type: {type(loaded_obj)}")
# κ°„λ‹¨ν•œ μ• λ‹ˆλ©”μ΄μ…˜ 생성 μ‹œλ„
frames = create_simple_animation_frames(input_mesh_path, animation_type, int(duration * fps))
if not frames:
return None, None
except Exception as e:
print(f"Error loading mesh: {str(e)}")
# κ°„λ‹¨ν•œ μ• λ‹ˆλ©”μ΄μ…˜ 생성 μ‹œλ„
frames = create_simple_animation_frames(input_mesh_path, animation_type, int(duration * fps))
if not frames:
return None, None
# Generate animation frames based on animation type
try:
if 'frames' not in locals(): # 이미 framesκ°€ μƒμ„±λ˜μ§€ μ•Šμ•˜λ‹€λ©΄
print(f"Generating {animation_type} animation...")
if animation_type == 'rotate':
frames = create_rotation_animation(mesh, duration, fps)
elif animation_type == 'float':
frames = create_float_animation(mesh, duration, fps)
elif animation_type == 'explode':
frames = create_explode_animation(mesh, duration, fps)
elif animation_type == 'assemble':
frames = create_assemble_animation(mesh, duration, fps)
elif animation_type == 'pulse':
frames = create_pulse_animation(mesh, duration, fps)
elif animation_type == 'swing':
frames = create_swing_animation(mesh, duration, fps)
else:
print(f"Unknown animation type: {animation_type}, using default rotate")
frames = create_rotation_animation(mesh, duration, fps)
except Exception as e:
print(f"Error generating animation: {str(e)}")
# μ• λ‹ˆλ©”μ΄μ…˜ 생성 μ‹€νŒ¨ μ‹œ κ°„λ‹¨ν•œ μ• λ‹ˆλ©”μ΄μ…˜ μ‹œλ„
frames = create_simple_animation_frames(input_mesh_path, animation_type, int(duration * fps))
if not frames:
return None, None
print(f"Generated {len(frames)} animation frames")
base_filename = os.path.basename(input_mesh_path).rsplit('.', 1)[0]
# Save animated mesh as GLB
try:
animated_glb_path = os.path.join(LOG_PATH, f'animated_{base_filename}.glb')
# For GLB output, we'll use the first frame for now
# In a production environment, you'd want to use proper animation keyframes
if frames and len(frames) > 0:
# First frame for static GLB
first_frame = frames[0]
# Export as GLB
if not isinstance(first_frame, trimesh.Scene):
scene = trimesh.Scene(first_frame)
else:
scene = first_frame
scene.export(animated_glb_path)
print(f"Exported GLB to {animated_glb_path}")
else:
return None, None
except Exception as e:
print(f"Error exporting GLB: {str(e)}")
animated_glb_path = None
# Create GIF for preview
try:
animated_gif_path = os.path.join(LOG_PATH, f'animated_{base_filename}.gif')
print(f"Creating GIF at {animated_gif_path}")
generate_gif_from_frames(frames, animated_gif_path, fps)
except Exception as e:
print(f"Error creating GIF: {str(e)}")
animated_gif_path = None
return animated_glb_path, animated_gif_path
@spaces.GPU
def process_3d_model(input_3d, animation_type, animation_duration, fps):
"""Process a 3D model and apply animation"""
print(f"Processing: {input_3d} with animation type: {animation_type}")
try:
# Create animation
animated_glb_path, animated_gif_path = create_animation_mesh(
input_3d,
animation_type=animation_type,
duration=animation_duration,
fps=fps
)
if not animated_glb_path:
# κΈ°λ³Έ μ²˜λ¦¬κ°€ μ‹€νŒ¨ν–ˆμ„ 경우 κ°„λ‹¨ν•œ λ°©μ‹μœΌλ‘œ λ‹€μ‹œ μ‹œλ„
print("Primary animation method failed, trying simple animation...")
frames = create_simple_animation_frames(
input_3d,
animation_type,
int(animation_duration * fps)
)
if frames:
base_filename = os.path.basename(input_3d).rsplit('.', 1)[0]
# GLB μ €μž₯
animated_glb_path = os.path.join(LOG_PATH, f'simple_animated_{base_filename}.glb')
if isinstance(frames[0], trimesh.Scene):
scene = frames[0]
else:
scene = trimesh.Scene(frames[0])
scene.export(animated_glb_path)
# GIF 생성
animated_gif_path = os.path.join(LOG_PATH, f'simple_animated_{base_filename}.gif')
generate_gif_from_frames(frames, animated_gif_path, fps)
else:
# λͺ¨λ“  λ©”μ„œλ“œκ°€ μ‹€νŒ¨ν•œ 경우 ν…μŠ€νŠΈ 기반 GIF라도 생성
base_filename = os.path.basename(input_3d).rsplit('.', 1)[0]
text_gif_path = os.path.join(LOG_PATH, f'text_animated_{base_filename}.gif')
animated_gif_path = create_textual_animation_gif(
text_gif_path,
os.path.basename(input_3d),
animation_type,
animation_duration,
fps
)
# 원본 νŒŒμΌμ„ GLB둜 볡사 (μ²˜λ¦¬λ˜μ§€ μ•Šμ€ μƒνƒœλ‘œ)
copy_glb_path = os.path.join(LOG_PATH, f'copy_{base_filename}.glb')
import shutil
try:
shutil.copy(input_3d, copy_glb_path)
animated_glb_path = copy_glb_path
print(f"Copied original GLB to {copy_glb_path}")
except:
animated_glb_path = input_3d
print("Could not copy original GLB, using input path")
# Create a simple JSON metadata file
metadata = {
"animation_type": animation_type,
"duration": animation_duration,
"fps": fps,
"original_model": os.path.basename(input_3d),
"created_at": time.strftime("%Y-%m-%d %H:%M:%S")
}
json_path = os.path.join(LOG_PATH, f'metadata_{os.path.basename(input_3d).rsplit(".", 1)[0]}.json')
with open(json_path, 'w') as f:
json.dump(metadata, f, indent=4)
# GIFκ°€ μ—†κ±°λ‚˜ λ Œλ”λ§ μ‹€νŒ¨μΈ 경우λ₯Ό λŒ€λΉ„ν•œ λ°±μ—… GIF 생성
if not animated_gif_path:
base_filename = os.path.basename(input_3d).rsplit('.', 1)[0]
text_gif_path = os.path.join(LOG_PATH, f'text_animated_{base_filename}.gif')
animated_gif_path = create_textual_animation_gif(
text_gif_path,
os.path.basename(input_3d),
animation_type,
animation_duration,
fps
)
return animated_glb_path, animated_gif_path, json_path
except Exception as e:
error_msg = f"Error processing file: {str(e)}"
print(error_msg)
# μ‹¬κ°ν•œ 였λ₯˜ λ°œμƒ μ‹œ ν…μŠ€νŠΈ μ• λ‹ˆλ©”μ΄μ…˜μ΄λΌλ„ 생성
try:
base_filename = os.path.basename(input_3d).rsplit('.', 1)[0]
text_gif_path = os.path.join(LOG_PATH, f'text_animated_{base_filename}.gif')
animated_gif_path = create_textual_animation_gif(
text_gif_path,
os.path.basename(input_3d),
animation_type,
animation_duration,
fps
)
# 원본 νŒŒμΌμ„ GLB둜 볡사 (μ²˜λ¦¬λ˜μ§€ μ•Šμ€ μƒνƒœλ‘œ)
copy_glb_path = os.path.join(LOG_PATH, f'copy_{base_filename}.glb')
import shutil
try:
shutil.copy(input_3d, copy_glb_path)
animated_glb_path = copy_glb_path
except:
animated_glb_path = input_3d
# 메타데이터 생성
metadata = {
"animation_type": animation_type,
"duration": animation_duration,
"fps": fps,
"original_model": os.path.basename(input_3d),
"created_at": time.strftime("%Y-%m-%d %H:%M:%S"),
"error": str(e)
}
json_path = os.path.join(LOG_PATH, f'metadata_{base_filename}.json')
with open(json_path, 'w') as f:
json.dump(metadata, f, indent=4)
return animated_glb_path, animated_gif_path, json_path
except:
# λͺ¨λ“  방법이 μ‹€νŒ¨ν•œ 경우
return error_msg, None, None, 50), f"Model: {os.path.basename(input_3d)}", fill=(0, 0, 0))
draw.text((50, 100), f"Animation: {animation_type}", fill=(0, 0, 0))
draw.text((50, 150), f"Angle: {angle}Β°", fill=(0, 0, 0))
# κ°„λ‹¨ν•œ νšŒμ „ 객체 그리기
center_x, center_y = 200, 180
radius = 50
x = center_x + radius * math.cos(math.radians(angle))
y = center_y + radius * math.sin(math.radians(angle))
draw.ellipse((x-20, y-20, x+20, y+20), fill=(255, 0, 0))
simple_frames.append(img)
# GIF μ €μž₯
simple_frames[0].save(
backup_gif_path,
save_all=True,
append_images=simple_frames[1:],
optimize=False,
duration=int(1000 / fps),
loop=0
)
print(f"Backup GIF created at {backup_gif_path}")
# κΈ°μ‘΄ GIFκ°€ λΉ„μ–΄μžˆκ±°λ‚˜ λ¬Έμ œκ°€ 있으면 λ°±μ—… GIF μ‚¬μš©
try:
# κΈ°μ‘΄ GIF 확인
original_gif = Image.open(animated_gif_path)
original_frames = []
try:
for i in range(100): # μ΅œλŒ€ 100 ν”„λ ˆμž„
original_gif.seek(i)
frame = original_gif.copy()
original_frames.append(frame)
except EOFError:
pass # λͺ¨λ“  ν”„λ ˆμž„ 읽음
if not original_frames:
print("Original GIF is empty, using backup")
animated_gif_path = backup_gif_path
except Exception as e:
print(f"Error checking original GIF: {str(e)}, using backup")
animated_gif_path = backup_gif_path
except Exception as e:
print(f"Error creating backup GIF: {str(e)}")
return animated_glb_path, animated_gif_path, json_path
except Exception as e:
error_msg = f"Error processing file: {str(e)}"
print(error_msg)
return error_msg, None, None
_HEADER_ = '''
<h2><b>GLB μ• λ‹ˆλ©”μ΄μ…˜ 생성기 - 3D λͺ¨λΈ μ›€μ§μž„ 효과</b></h2>
이 데λͺ¨λ₯Ό 톡해 정적인 3D λͺ¨λΈ(GLB 파일)에 λ‹€μ–‘ν•œ μ• λ‹ˆλ©”μ΄μ…˜ 효과λ₯Ό μ μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
❗️❗️❗️**μ€‘μš”μ‚¬ν•­:**
- 이 데λͺ¨λŠ” μ—…λ‘œλ“œλœ GLB νŒŒμΌμ— μ• λ‹ˆλ©”μ΄μ…˜μ„ μ μš©ν•©λ‹ˆλ‹€.
- λ‹€μ–‘ν•œ μ• λ‹ˆλ©”μ΄μ…˜ μŠ€νƒ€μΌ μ€‘μ—μ„œ μ„ νƒν•˜μ„Έμš”: νšŒμ „, λΆ€μœ , 폭발, 쑰립, νŽ„μŠ€, μŠ€μœ™.
- κ²°κ³ΌλŠ” μ• λ‹ˆλ©”μ΄μ…˜λœ GLB 파일과 미리보기용 GIF 파일둜 μ œκ³΅λ©λ‹ˆλ‹€.
'''
_INFO_ = r"""
### μ• λ‹ˆλ©”μ΄μ…˜ μœ ν˜• μ„€λͺ…
- **νšŒμ „(rotate)**: λͺ¨λΈμ΄ Y좕을 μ€‘μ‹¬μœΌλ‘œ νšŒμ „ν•©λ‹ˆλ‹€.
- **λΆ€μœ (float)**: λͺ¨λΈμ΄ μœ„μ•„λž˜λ‘œ λΆ€λ“œλŸ½κ²Œ λ– λ‹€λ‹™λ‹ˆλ‹€.
- **폭발(explode)**: λͺ¨λΈμ˜ 각 뢀뢄이 μ€‘μ‹¬μ—μ„œ λ°”κΉ₯μͺ½μœΌλ‘œ νΌμ Έλ‚˜κ°‘λ‹ˆλ‹€.
- **쑰립(assemble)**: 폭발 μ• λ‹ˆλ©”μ΄μ…˜μ˜ λ°˜λŒ€ - λΆ€ν’ˆλ“€μ΄ ν•¨κ»˜ λͺ¨μž…λ‹ˆλ‹€.
- **νŽ„μŠ€(pulse)**: λͺ¨λΈμ΄ 크기가 μ»€μ‘Œλ‹€ μž‘μ•„μ‘Œλ‹€λ₯Ό λ°˜λ³΅ν•©λ‹ˆλ‹€.
- **μŠ€μœ™(swing)**: λͺ¨λΈμ΄ 쒌우둜 λΆ€λ“œλŸ½κ²Œ ν”λ“€λ¦½λ‹ˆλ‹€.
### 팁
- μ• λ‹ˆλ©”μ΄μ…˜ 길이와 FPSλ₯Ό μ‘°μ ˆν•˜μ—¬ μ›€μ§μž„μ˜ 속도와 λΆ€λ“œλŸ¬μ›€μ„ μ‘°μ ˆν•  수 μžˆμŠ΅λ‹ˆλ‹€.
- λ³΅μž‘ν•œ λͺ¨λΈμ€ 처리 μ‹œκ°„μ΄ 더 였래 걸릴 수 μžˆμŠ΅λ‹ˆλ‹€.
- GIF λ―Έλ¦¬λ³΄κΈ°λŠ” λΉ λ₯Έ 참쑰용이며, κ³ ν’ˆμ§ˆ κ²°κ³Όλ₯Ό μœ„ν•΄μ„œλŠ” μ• λ‹ˆλ©”μ΄μ…˜λœ GLB νŒŒμΌμ„ λ‹€μš΄λ‘œλ“œν•˜μ„Έμš”.
"""
# Gradio μΈν„°νŽ˜μ΄μŠ€ μ„€μ •
def create_gradio_interface():
with gr.Blocks(title="GLB μ• λ‹ˆλ©”μ΄μ…˜ 생성기") as demo:
# 제λͺ© μ„Ήμ…˜
gr.Markdown(_HEADER_)
with gr.Row():
with gr.Column():
# μž…λ ₯ μ»΄ν¬λ„ŒνŠΈ
input_3d = gr.Model3D(label="3D λͺ¨λΈ 파일 μ—…λ‘œλ“œ (GLB 포맷)")
with gr.Row():
animation_type = gr.Dropdown(
label="μ• λ‹ˆλ©”μ΄μ…˜ μœ ν˜•",
choices=["rotate", "float", "explode", "assemble", "pulse", "swing"],
value="rotate"
)
with gr.Row():
animation_duration = gr.Slider(
label="μ• λ‹ˆλ©”μ΄μ…˜ 길이 (초)",
minimum=1.0,
maximum=10.0,
value=3.0,
step=0.5
)
fps = gr.Slider(
label="μ΄ˆλ‹Ή ν”„λ ˆμž„ 수",
minimum=15,
maximum=60,
value=30,
step=1
)
submit_btn = gr.Button("λͺ¨λΈ 처리 및 μ• λ‹ˆλ©”μ΄μ…˜ 생성")
with gr.Column():
# 좜λ ₯ μ»΄ν¬λ„ŒνŠΈ
output_3d = gr.Model3D(label="μ• λ‹ˆλ©”μ΄μ…˜ 적용된 3D λͺ¨λΈ")
output_gif = gr.Image(label="μ• λ‹ˆλ©”μ΄μ…˜ 미리보기 (GIF)")
output_json = gr.File(label="메타데이터 파일 λ‹€μš΄λ‘œλ“œ")
# μ• λ‹ˆλ©”μ΄μ…˜ μœ ν˜• μ„€λͺ…
gr.Markdown(_INFO_)
# λ²„νŠΌ λ™μž‘ μ„€μ •
submit_btn.click(
fn=process_3d_model,
inputs=[input_3d, animation_type, animation_duration, fps],
outputs=[output_3d, output_gif, output_json]
)
# 예제 μ€€λΉ„
example_files = [ [f] for f in glob.glob('./data/demo_glb/*.glb') ]
if example_files:
example = gr.Examples(
examples=example_files,
inputs=[input_3d],
examples_per_page=10,
)
return demo
# 메인 μ‹€ν–‰ λΆ€λΆ„
if __name__ == "__main__":
demo = create_gradio_interface()
demo.launch(share=True, server_name="0.0.0.0", server_port=7860)