如何使用 Python 检查 Maya 中的帧是否为关键帧?

How can you check if a frame is a keyframe in Maya with Python?

我需要在 Maya 中循环播放动画的每一帧并创建两个数组,一个是给定属性在每一帧的值,另一个是仅在关键帧的值。问题是,我不知道如何让 maya 在 python 中提出问题 "is the current frame a keyframe for this attribute"。不过,我是在 MEL 中计算出来的,所以也许有人可以帮我转换它。

这是 MEL 中的循环:

global proc int keyExistsAtFrame(   int $frameNum,
                    string $object,
                    string $attribute)
{
    int $value;
    selectKey -clear;
    $value = `selectKey -add -k -t $frameNum ($object + "." + $attribute)`;

    if($value)
        return 1;
    else
        return 0;
}

for( $i=1; $i<120; ++$i )
{
    currentTime -edit $i;
    if (keyExistsAtFrame($i, "DD_headRoll_ctrl", "rotateZ"))
    {
        print "key exists at ";
        print $i;
        print "\n";
    }
}

我如何将它实现到我的 python 脚本中? 这是我在 python 中的当前草稿:

import maya.cmds as cmd

for i in range(int(cmd.playbackOptions(q=1, minTime = True)), int(cmd.playbackOptions(q=1, maxTime = True))):
    cmd.currentTime(i, e=1)
    iskey = cmd.selectKey(add = True, k = True, t = (i, i), attribute = "DD_headRoll_ctrl.rotateZ")
    #print iskey
    if iskey:
        print i

其实比看插头有没有钥匙要简单的多:

object = "pSphere1"
attr = "tx"

cmds.keyframe(object + "." + attr, q=True)

# Result: [5.0, 28.0, 46.0, 79.0] #

cmds.keyframe 将 return 每个键都打开的帧列表。