## http://flynnsyard.blogspot.co.uk ## ## Set keys script in python ## ## Use this script to ensure all selected items and or channels have a key based on what you see in the timeline (or even highlighted timeline!) of maya ## ## Also can be used to remove frames and or keys from all selected ## ## enjoy ## import maya.cmds as cmds Yel_Btn_Col = ( .84 , .76 , .31 ) Yel2_Btn_Col = ( .98 , .56 , .11 ) def SetKeys_Win_Cmd(): SetKeys_Win = "SetKeys_Win" if cmds.window('SetKeys_Win', exists=True): cmds.deleteUI('SetKeys_Win', window=True) cmds.windowPref( 'SetKeys_Win', remove=True ) cmds.window( 'SetKeys_Win' , rtf=True , toolbox=True , widthHeight = [125 , 70] ) cmds.rowColumnLayout( 'SkinForce_MainWin' , numberOfColumns=1 ) cmds.button( bgc= Yel_Btn_Col , label = 'SET KEYS', width = 125 , height = 30 , command = 'SetKeys_cmd( 0 )' ) cmds.button( bgc= Yel2_Btn_Col , label = 'Del every odd key', width = 125 , height = 40 , command = 'SetKeys_cmd( 1 )' ) cmds.showWindow( 'SetKeys_Win' ) def Items_Selected(): #returns selected + error msgs selcted= [] selcted = cmds.ls( selection=True , flatten=True) if selcted is None: pass else: return selcted def SetKeysmainchunk_cmd( type ): List_Sel = Items_Selected() Source = List_Sel[0] TimeCurrent = cmds.currentTime(query = True ) #grab the timeline Time_Selected = cmds.timeControl( 'timeControl1' , query=True , rv=True ) if Time_Selected is True: Time = cmds.timeControl( 'timeControl1' , query=True , rng=True ) tmp1 = Time.replace('"' , '') tmp2 = tmp1.split(':') Time_Start = tmp2[0] Time_End = tmp2[1] Time_End = (int(Time_End) -1) else: Time_Start = cmds.playbackOptions(query=True , minTime=True ) Time_End = cmds.playbackOptions(query=True , maxTime=True ) #check if item 1 has keys, it should! Has_Keys_Test = cmds.keyframe( Source , query=True ,kc=True) #has to have keys to continue! if Has_Keys_Test is None: print 'No keys found, add keys to continue' else: Source_Keys_All = [] for items in List_Sel: tmp = cmds.keyframe( items , query=True , time=( Time_Start , Time_End ) ) if tmp is None: pass else: for times in tmp: Source_Keys_All.append( times ) Source_Keys = set(Source_Keys_All) Source_Keys2 = [] for x in Source_Keys: Source_Keys2.append(x) Source_Keys2.sort() cmds.select(clear=True) cmds.select(List_Sel) if type is 0: TimeCount = 0 if type is 1: TimeCount = 1 while TimeCount < len(Source_Keys2): CurrentKeyFrame = Source_Keys2[TimeCount] cmds.currentTime( CurrentKeyFrame ) if type is 0: cmds.setKeyframe( List_Sel ) TimeCount +=1 #increments the time by every key found if type is 1: print CurrentKeyFrame cmds.cutKey( List_Sel , time=(CurrentKeyFrame,) ) TimeCount +=2 #increments the time by every other key # count +=1 #increments the objects selected cmds.currentTime( TimeCurrent ) def SetKeys_cmd( type ): test = Items_Selected() if test is None: print 'select more than one item to set keys on.' else: if len(test) < 1: print 'select more than one item to set keys on.' else: if type is 1: gChannelBoxName = mel.eval('$temp=$gChannelBoxName') chList = cmds.channelBox (gChannelBoxName, q=True, sma = True) if chList: print 'cutting keys from channels on selected' CutKeysChannel_cmd() else: print 'cutting keys on selected' SetKeysmainchunk_cmd( type ) else: #type is 0 print 'setting keys on all selected' SetKeysmainchunk_cmd( type ) def CutKeysChannel_cmd(): List_Sel = Items_Selected() #grab the timeline Time_Selected = cmds.timeControl( 'timeControl1' , query=True , rv=True ) TimeCurrent = cmds.currentTime(query = True ) if Time_Selected is True: Time = cmds.timeControl( 'timeControl1' , query=True , rng=True ) tmp1 = Time.replace('"' , '') tmp2 = tmp1.split(':') Time_Start = tmp2[0] Time_End = tmp2[1] Time_End = (int(Time_End) -1) else: Time_Start = cmds.playbackOptions(query=True , minTime=True ) Time_End = cmds.playbackOptions(query=True , maxTime=True ) #grab the timeline for x in List_Sel: gChannelBoxName = mel.eval('$temp=$gChannelBoxName') chList = cmds.channelBox (gChannelBoxName, q=True, sma = True) for channels in chList: tmp = cmds.keyframe( x , attribute = channels , query=True , time=( Time_Start , Time_End ) ) print tmp Source_Keys = set(tmp) Source_Keys2 = [] for time in Source_Keys: Source_Keys2.append( time ) Source_Keys2.sort() cmds.select(clear=True) cmds.select( x ) TimeCount = 1 while TimeCount < len(Source_Keys2): CurrentKeyFrame = Source_Keys2[TimeCount] cmds.currentTime( CurrentKeyFrame ) cmds.cutKey( List_Sel , attribute= channels , time=(CurrentKeyFrame,) ) TimeCount +=2 #increments the time by every other key cmds.currentTime( TimeCurrent ) SetKeys_Win_Cmd()