有效获取区间内的所有动画帧

Get all animated frames in the interval effectively

我正在为我的场景编写动画导出脚本,我只需要检测场景中某些 objects 的动画键出现的那些帧。我只需要导出所有动画的一部分(时间间隔)。

目前我正在使用以下代码

keys = #()
for o in objects do
(
    join keys o.position.controller.keys
    join keys o.rotation.controller.keys
    join keys o.scale.controller.keys
)

收集所有objects的所有密钥,然后

timeValues = (for k in keys where k.time >= animationRange.start and k.time <= animationRange.end collect k.time) as array
timeValues = makeUniqueArray timeValues

至select时间间隔并删除重复项。

但问题是,我的场景总共包含大约 3000 帧,其中包含许多不同的动画 objects,带有位置、旋转和缩放键。所以在我的 collection "keys" 中密钥重复了很多次 - 总共大约有 1,000,000 个密钥。

我需要在一个小的时间间隔(大约 50 帧)内 select 键,但是由于 collection 非常大,所以 select 花了很长时间(大约一分钟)所需的键并删除重复项。

有没有办法更有效地获取指定时间间隔内所有带有动画关键帧的帧?

由于帧数通常不会达到数百万,因此使用位数组应该会更快并且内存效率更高。权衡是它只能存储等于或大于 1 的值——如果键从零帧开始,存储它们的时间 + 1;对于负键,相应地调整偏移量。

frames = #{}

for o in objects do
(
    for key in o.position.controller.keys do append frames key.time
    for key in o.rotation.controller.keys do append frames key.time
    for key in o.scale.controller.keys do append frames key.time
)

frames.count = animationRange.end
frames