File size: 44,028 Bytes
61b805a 829e08b ac14d96 829e08b ac14d96 c30bc0f ac14d96 829e08b 3362da1 8f1810a ac14d96 edb4f10 829e08b ac14d96 829e08b c30bc0f ac14d96 c30bc0f 829e08b ac14d96 61b805a 829e08b ac14d96 c30bc0f ac14d96 61b805a ac14d96 c30bc0f 6a6eb1e c30bc0f ac14d96 c30bc0f ac14d96 c30bc0f ac14d96 6a6eb1e c30bc0f ac14d96 c30bc0f ac14d96 6a6eb1e c30bc0f 6a6eb1e ac14d96 6a6eb1e ac14d96 6a6eb1e ac14d96 c30bc0f 6a6eb1e ac14d96 6a6eb1e ac14d96 6a6eb1e c30bc0f 6a6eb1e ac14d96 6a6eb1e c30bc0f ac14d96 c30bc0f ac14d96 c30bc0f ac14d96 c30bc0f ac14d96 c30bc0f ac14d96 c30bc0f ac14d96 c30bc0f ac14d96 c30bc0f ac14d96 61b805a ac14d96 61b805a ac14d96 61b805a ac14d96 61b805a ac14d96 61b805a ac14d96 61b805a ac14d96 61b805a ac14d96 61b805a ac14d96 61b805a ac14d96 61b805a 6a6eb1e 61b805a 6a6eb1e 61b805a 6a6eb1e 61b805a 6a6eb1e 61b805a 6a6eb1e 61b805a 6a6eb1e 61b805a c30bc0f ac14d96 61b805a c30bc0f ac14d96 61b805a ac14d96 c30bc0f ac14d96 61b805a ac14d96 61b805a ac14d96 c30bc0f ac14d96 c30bc0f ac14d96 61b805a ac14d96 c30bc0f ac14d96 c30bc0f ac14d96 c30bc0f edb4f10 ac14d96 c30bc0f 755623e c30bc0f ac14d96 c30bc0f 61b805a ac14d96 c30bc0f ac14d96 c30bc0f 61b805a ac14d96 c30bc0f ac14d96 d9980d9 ac14d96 8051051 ac14d96 8051051 ac14d96 d9980d9 ac14d96 d9980d9 829e08b ac14d96 829e08b ac14d96 d9980d9 ac14d96 829e08b ac14d96 edb4f10 |
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 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 1031 1032 |
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) |