2013-06-12 14 views
0

我正在嘗試完成我的腳本,並且遇到了一些問題。 這裏是我的腳本:瑪雅Python腳本:如果某個屬性在該特定框架處於關鍵幀時返回True或False

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,我也想保存鍵的正切類型。

回答

1

cmds.keyframes將爲您提供給定anim曲線的所有關鍵幀時間。所以這是很容易找到場景中所有的鑰匙:

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] 
相關問題