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

Maya Graph Editor缩放后关键帧偏移至帧间 求批量修正脚本方法

Maya非整数关键帧批量对齐整帧解决方案

自带Snap功能通常是吸附到时间轴的刻度网格,但是如果缩放操作后关键帧间距不规则,很容易出现吸附错位、漏吸附的问题,脚本处理可以100%覆盖所有选中的关键帧,规则可控。

实现逻辑

  • 读取所有选中的动画曲线的关键帧时间参数
  • 按照自定义规则将非整数帧对齐到最近的整帧
  • 自动处理同一条曲线同一帧出现重复关键帧的冲突
  • 原生整数帧不会被修改,避免误操作

可直接使用的Python脚本

import maya.cmds as cmds
import math

# 对齐规则修改说明:
# 替换为 round 代表四舍五入到最近整帧
# 替换为 math.ceil 代表向上对齐到更大的整帧
# 替换为 math.floor 代表向下对齐到更小的整帧
align_method = round

# 获取选中的动画曲线
selected_curves = cmds.keyframe(q=True, selected=True, name=True)
if not selected_curves:
    raise RuntimeError("操作失败:请先在Graph Editor或时间轴选中需要修正的关键帧")

for curve in selected_curves:
    # 获取当前曲线选中关键帧的索引、时间参数
    key_indices = cmds.keyframe(curve, q=True, indexValue=True)
    key_times = cmds.keyframe(curve, q=True, timeChange=True)
    
    used_frames = set()
    # 倒序处理关键帧,避免索引偏移导致的操作错误
    for idx, t in sorted(zip(key_indices, key_times), reverse=True):
        target_frame = align_method(t)
        if target_frame == t:
            used_frames.add(target_frame)
            continue
        # 同帧已存在关键帧时删除当前冗余帧
        if target_frame in used_frames:
            cmds.cutKey(curve, index=(idx, idx))
            continue
        # 移动关键帧到对齐后的整帧
        cmds.keyframe(curve, index=(idx, idx), timeChange=target_frame)
        used_frames.add(target_frame)

print(f"处理完成:共对齐{len(selected_curves)}条动画曲线的关键帧")

使用步骤

  • 打开Maya的脚本编辑器,切换到Python运行环境
  • 粘贴上述代码,根据需求修改align_method参数的对齐规则
  • 回到Graph Editor界面,选中所有需要修正的非整数关键帧
  • 点击脚本编辑器的运行按钮,即可完成批量对齐

效果参考

偏移的非整数关键帧示例
对齐后的整帧关键帧效果

内容的提问来源于stack exchange,提问作者David

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 11:15:03