我正试图完成我的脚本,我有一些问题。下面是我的脚本:
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
现在,当我查询一个属性时,我想在字典中存储一个True
或False
值,该值表示该属性是否在你正在检查的那一帧有关键帧,但仅当它在那一帧上有关键帧时。
例如,假设我在第1帧和第5帧的控件的tx属性上有键,我想要一个字典键,以便稍后检查这些帧是否有键:当在该帧有键时,返回true
;当没有时,返回false
。如果是True
,我还想保存键的正切类型
关键帧将为你提供给定动画曲线的所有关键帧时间。所以很容易找到场景中的所有钥匙:
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]