Maya Python脚本:如果属性在特定帧处设置关键帧,则返回True或False
我正在努力完成我的脚本,我遇到了一些问题。 这是我的剧本:Maya Python脚本:如果属性在特定帧处设置关键帧,则返回True或False,python,maya,keyframe,Python,Maya,Keyframe,我正在努力完成我的脚本,我遇到了一些问题。 这是我的剧本: 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):
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
,我还想保存关键帧的切线类型。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]