You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ursina中FrameAnimation3D加载帧动画顺序错误问题求助

Fix Frame Animation Loading Order Issue

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 18:55:13