Advertisement
Flynny85

SetKeys Python script for Maya

Mar 26th, 2015
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.12 KB | None | 0 0
  1. ## http://flynnsyard.blogspot.co.uk ##
  2. ## Set keys script in python ##
  3.  
  4. ## 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 ##
  5. ## Also can be used to remove frames and or keys from all selected ##
  6. ## enjoy ##
  7.  
  8.  
  9. import maya.cmds as cmds
  10. Yel_Btn_Col = ( .84 , .76 , .31 )
  11. Yel2_Btn_Col = ( .98 , .56 , .11 )
  12.  
  13. def SetKeys_Win_Cmd():
  14.     SetKeys_Win = "SetKeys_Win"
  15.  
  16.     if cmds.window('SetKeys_Win', exists=True):
  17.         cmds.deleteUI('SetKeys_Win', window=True)
  18.         cmds.windowPref( 'SetKeys_Win', remove=True )
  19.     cmds.window( 'SetKeys_Win' ,  rtf=True , toolbox=True , widthHeight = [125 , 70] ) 
  20.    
  21.     cmds.rowColumnLayout( 'SkinForce_MainWin' , numberOfColumns=1  )   
  22.     cmds.button( bgc= Yel_Btn_Col , label = 'SET KEYS', width = 125 , height = 30 , command = 'SetKeys_cmd( 0 )' ) 
  23.     cmds.button( bgc= Yel2_Btn_Col , label = 'Del every odd key', width = 125 , height = 40 , command = 'SetKeys_cmd( 1 )' )   
  24.  
  25.     cmds.showWindow( 'SetKeys_Win' )
  26.  
  27.  
  28. def Items_Selected():   #returns selected + error msgs
  29.     selcted= []
  30.     selcted = cmds.ls( selection=True , flatten=True)
  31.     if selcted is None:
  32.         pass
  33.     else:
  34.         return selcted
  35.    
  36.        
  37. def SetKeysmainchunk_cmd( type ):  
  38.  
  39.     List_Sel = Items_Selected()
  40.     Source = List_Sel[0]
  41.     TimeCurrent = cmds.currentTime(query = True )
  42.     #grab the timeline
  43.     Time_Selected = cmds.timeControl( 'timeControl1' , query=True , rv=True )
  44.  
  45.     if Time_Selected is True:
  46.         Time = cmds.timeControl( 'timeControl1' , query=True , rng=True )
  47.         tmp1 = Time.replace('"' , '')
  48.         tmp2 = tmp1.split(':')
  49.         Time_Start = tmp2[0]
  50.         Time_End = tmp2[1]
  51.         Time_End = (int(Time_End) -1)
  52.     else:
  53.         Time_Start = cmds.playbackOptions(query=True  ,  minTime=True )
  54.         Time_End = cmds.playbackOptions(query=True  ,  maxTime=True )      
  55.    
  56.     #check if item 1 has keys, it should!
  57.     Has_Keys_Test = cmds.keyframe( Source , query=True ,kc=True)
  58.    
  59.     #has to have keys to continue!
  60.     if Has_Keys_Test is None:
  61.         print 'No keys found, add keys to continue'
  62.     else:
  63.         Source_Keys_All = []
  64.         for items in List_Sel:
  65.             tmp = cmds.keyframe(  items , query=True , time=( Time_Start , Time_End )  )
  66.             if tmp is None:
  67.                 pass
  68.             else:
  69.                 for times in tmp:
  70.                     Source_Keys_All.append( times )
  71.  
  72.  
  73.         Source_Keys = set(Source_Keys_All) 
  74.         Source_Keys2 = []
  75.         for x in Source_Keys:
  76.             Source_Keys2.append(x)
  77.  
  78.         Source_Keys2.sort()
  79.  
  80.         cmds.select(clear=True)            
  81.         cmds.select(List_Sel)
  82.        
  83.         if type is 0:
  84.             TimeCount = 0
  85.         if type is 1:
  86.             TimeCount = 1
  87.        
  88.         while TimeCount < len(Source_Keys2):
  89.             CurrentKeyFrame = Source_Keys2[TimeCount]
  90.             cmds.currentTime( CurrentKeyFrame )
  91.            
  92.             if type is 0:
  93.                 cmds.setKeyframe( List_Sel )   
  94.                 TimeCount +=1   #increments the time by every key found
  95.             if type is 1:
  96.                 print CurrentKeyFrame
  97.                 cmds.cutKey( List_Sel ,  time=(CurrentKeyFrame,) )
  98.                 TimeCount +=2   #increments the time by every other key
  99.            
  100.             # count +=1 #increments the objects selected       
  101.     cmds.currentTime( TimeCurrent )
  102.        
  103.        
  104.        
  105. def SetKeys_cmd( type ):
  106.     test = Items_Selected()
  107.  
  108.     if test is None:
  109.         print 'select more than one item to set keys on.'
  110.     else:
  111.         if len(test) < 1:
  112.             print 'select more than one item to set keys on.'
  113.         else:
  114.        
  115.  
  116.             if type is 1:
  117.                 gChannelBoxName = mel.eval('$temp=$gChannelBoxName')
  118.                 chList = cmds.channelBox (gChannelBoxName, q=True, sma = True)
  119.                 if chList:
  120.                     print 'cutting keys from channels on selected'
  121.                     CutKeysChannel_cmd()
  122.                 else:
  123.                     print 'cutting keys on selected'
  124.                     SetKeysmainchunk_cmd( type )               
  125.             else: #type is 0
  126.                 print 'setting keys on all selected'               
  127.                 SetKeysmainchunk_cmd( type )   
  128.                
  129.            
  130.  
  131.        
  132. def CutKeysChannel_cmd():
  133.  
  134.     List_Sel = Items_Selected()
  135.  
  136.    
  137.     #grab the timeline
  138.     Time_Selected = cmds.timeControl( 'timeControl1' , query=True , rv=True )
  139.     TimeCurrent = cmds.currentTime(query = True )
  140.    
  141.     if Time_Selected is True:
  142.         Time = cmds.timeControl( 'timeControl1' , query=True , rng=True )
  143.         tmp1 = Time.replace('"' , '')
  144.         tmp2 = tmp1.split(':')
  145.         Time_Start = tmp2[0]
  146.         Time_End = tmp2[1]
  147.         Time_End = (int(Time_End) -1)
  148.     else:
  149.         Time_Start = cmds.playbackOptions(query=True  ,  minTime=True )
  150.         Time_End = cmds.playbackOptions(query=True  ,  maxTime=True )      
  151.     #grab the timeline
  152.    
  153.    
  154.     for x in List_Sel:
  155.        
  156.         gChannelBoxName = mel.eval('$temp=$gChannelBoxName')
  157.         chList = cmds.channelBox (gChannelBoxName, q=True, sma = True)
  158.  
  159.         for channels in chList:
  160.             tmp = cmds.keyframe( x , attribute = channels , query=True ,  time=( Time_Start , Time_End )  )
  161.            
  162.             print tmp
  163.             Source_Keys = set(tmp) 
  164.             Source_Keys2 = []
  165.             for time in Source_Keys:
  166.                 Source_Keys2.append( time )
  167.  
  168.             Source_Keys2.sort()
  169.  
  170.             cmds.select(clear=True)            
  171.             cmds.select( x )
  172.  
  173.             TimeCount = 1
  174.             while TimeCount < len(Source_Keys2):
  175.                 CurrentKeyFrame = Source_Keys2[TimeCount]
  176.                 cmds.currentTime( CurrentKeyFrame )
  177.  
  178.  
  179.                 cmds.cutKey( List_Sel ,  attribute= channels , time=(CurrentKeyFrame,) )
  180.                 TimeCount +=2   #increments the time by every other key
  181.                
  182.                    
  183.     cmds.currentTime( TimeCurrent )                
  184.                    
  185.            
  186.        
  187.        
  188. SetKeys_Win_Cmd()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement