0

我正在尝试完成我的脚本,但遇到了一些问题。这是我的脚本:

from maya import cmds
def correct_value(selection=None, prefix='', suffix=''):

    newSel = []
    if selection is None: 
        selection = cmds.ls ('*_control') 

    if selection and not isinstance(selection, list):
        newSel = [selection]

    for each_obj in selection:
        if each_obj.startswith(prefix) and each_obj.endswith(suffix) :
        newSel.append(each_obj)
        return newSel

def save_pose(selection):

     pose_dictionary = {}

     for each_obj in selection:
          pose_dictionary[each_obj] = {}
     for each_attribute in cmds.listAttr (each_obj, k=True):
          pose_dictionary[each_obj][each_attribute] = cmds.getAttr (each_obj + "." + each_attribute)

  return pose_dictionary


controller = correct_value(None,'left' ,'control' )


save_pose(controller)


def save_animation(selection, **keywords ):
     if "framesWithKeys" not in keywords:
         keywords["framesWithKeys"] = [0,1,2,3]

     animation_dictionary = {}
     for each_frame in keywords["framesWithKeys"]:
          cmds.currentTime(each_frame)
          animation_dictionary[each_frame] = save_pose(selection)

     return animation_dictionary

frames = save_animation (save_pose(controller) ) 
print frames

现在,当我查询一个属性时,我想在字典中存储一个TrueFalse值,说明该属性是否在您正在检查的那个帧上有一个关键帧,但前提是它在那个帧上有一个关键帧。

例如,假设我在第 1 帧和第 5 帧的控件的 tx 属性上有键,我想要一个字典键,我可以稍后检查这些帧是否有键:当该帧有键时, 返回true; 没有时,返回false。如果True,我还想保存键的切线类型。

4

1 回答 1

2

cmds.keyframes 将为您提供给定动画曲线的所有关键帧时间。所以很容易找到场景中的所有键:

keytimes = {}
for item in cmds.ls(type = 'animCurve'):
    keytimes[item] =   cmds.keyframe(item,  query=True, t=(-10000,100000)) # this will give you the key times   # using a big frame range here to make sure we get them all

# in practice you'd probably pass 'keytimes' as a class member...
def has_key(item, frame, **keytimes):
    return frame in keytimes[item]

或者您可以一次只检查一个:

def has_key_at(animcurve, frame):
   return frame in  cmds.keyframe(animcurve,  query=True, t=(-10000,100000)) 

您可能会遇到的问题是未对齐的键:如果您在第 30.001 帧有一个键并且您问“30 帧是否有键”,答案将是否定的。你可以像这样强制整数键:

for item in cmds.ls(type = 'animCurve'):
    keytimes[item] =   map (int, cmds.keyframe(item,  query=True, t=(-10000,100000)))

def has_key (item, frame, **keytimes):
    return int(frame) in keytimes[item]
于 2013-06-12T23:18:17.017 回答