Ursina中FrameAnimation3D加载帧动画顺序错误问题求助
Problem
When using FrameAnimation3d with the prefix assets\playerhand\hanim2\\cube_, the OBJ files are loaded in lexicographical string order instead of numerical order. This causes frames like cube_10.obj to load before cube_2.obj, breaking the animation sequence.
Solutions
1. Manually Generate Sorted File Paths (No Source Code Modification)
If you can't modify the FrameAnimation3d class, generate a numerically sorted list of file paths first:
import os from pathlib import Path # Define animation directory and file prefix anim_directory = Path("assets/playerhand/hanim2") prefix = "cube_" # Collect all matching OBJ files and sort by numerical suffix sorted_files = sorted( anim_directory.glob(f"{prefix}*.obj"), key=lambda file: int(file.stem.split("_")[-1]) ) # Convert Path objects to strings file_paths = [str(file) for file in sorted_files] # Initialize animation with sorted paths hand = FrameAnimation3d( file_paths, position=(5, 2, 5), scale=0.1, loop=False, autoplay=False, fps=100 )
2. Modify FrameAnimation3d Source Code (Root Fix)
Locate the part of FrameAnimation3d where it fetches and sorts files, then replace lexicographical sorting with numerical sorting:
Original code (hypothetical):
import glob class FrameAnimation3d: def __init__(self, file_prefix, **kwargs): self.frame_files = glob.glob(f"{file_prefix}*.obj") self.frame_files.sort() # Lexicographical sort (problematic) # Rest of initialization...
Modified code:
import glob class FrameAnimation3d: def __init__(self, file_prefix, **kwargs): self.frame_files = glob.glob(f"{file_prefix}*.obj") # Sort by the numerical part of the filename self.frame_files.sort( key=lambda path: int(path.split("_")[-1].split(".")[0]) ) # Rest of initialization...
3. Rename Files with Leading Zeros (Quick Workaround)
Rename your OBJ files to use fixed-digit numerical suffixes (e.g., cube_001.obj, cube_002.obj...cube_080.obj). This makes lexicographical order match numerical order, so no code changes are needed.
内容的提问来源于stack exchange,提问作者iceneoon

