Maya Python脚本:如果属性在该特定帧处设置了关键帧,则返回True或False

2024-06-25 23:30:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在努力完成我的剧本,但我遇到了一些问题。 这是我的剧本:

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值,该值表示该属性是否在您要检查的帧上有一个关键帧,但仅当它在该帧上有一个关键帧。在

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


Tags: innoneobjfordictionaryifsavedef
1条回答
网友
1楼 · 发布于 2024-06-25 23:30:46

在命令.关键帧将提供给定动画曲线的所有关键帧时间。所以很容易找到场景中的所有关键点:

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]

或者你可以一次检查一个:

^{pr2}$

您可能遇到的问题是未捕捉的关键点:如果在第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]

相关问题 更多 >