Flynny85

Maya Script Loader

Mar 24th, 2015
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 14.71 KB | None | 0 0
  1. """                                                         current v2.4
  2.     Script Loader , Kevin Flynn, [email protected] , tech animator.
  3.     Goals:
  4.         - Simple Install  +  install to shelf button.
  5.         - Mac / PC in-dependant.
  6.         - Flexible, 1)Button text, 2)Icon, or 3)dock-able , 4)mini mode.
  7.         - Local version of scripts system? Just in-case.
  8.         - will display the icon for a script if its named the same.
  9.         - bug fixes:
  10.                 window and layout items now no longer use hard coded names..
  11.                 unix paths should play nicely
  12. """
  13. versionTxt = 'Script Loader V2.4'
  14. # proc imports here:
  15. import maya.cmds as cmds
  16. import maya.mel as mel
  17. import os
  18. import posixpath
  19. import shutil
  20. import sys
  21.  
  22.  
  23. # Global Values here:
  24. Scrpt_Ldr_Win_W_H = [190 , 180]             # Window UI Settings
  25. Scrpt_Ldr_Colour_1 = [.7 , .7 , -.1]        # Window UI Settings
  26. Scrpt_Ldr_Colour_2 = [.7 , .7 , -.1]        # Window UI Settings
  27. Scrpt_Ldr_Colour_3 = [.7 , .7 , -.1]        # Window UI Settings
  28. Scrpt_Ldr_Server_UserPref = []              # The servers prefs
  29. Scrpt_Ldr_Local_UserPref = []               # The local users prefs
  30. Scrpt_Ldr_Docked = False   
  31. Scrpt_Ldr_DockUI = ''
  32. Scrpt_Ldr_IconUI = False
  33. Scrpt_Ldr_ScrollUI = ''
  34.  
  35. Script_Ldr_ScriptCol = [.4 , .4 , .4]
  36. Script_Ldr_ScriptColMel = [.3 , 0 , 0]
  37. Script_Ldr_ScriptColPy = [0 , .3 , 0]
  38. Script_Ldr_ScriptColAll = [ Script_Ldr_ScriptColMel , Script_Ldr_ScriptColPy ]
  39.  
  40. Scrpt_Ldr_Local_FolderPath = 'Choose Folder'    # Users local script dump folder
  41. Scrpt_Ldr_FolderNotShown = [ 'Hidden' ]     # folder from the server that will not be shown, but still copied.
  42.  
  43. Script_Ldr_IconTypes = [ 'bmp', 'gif', 'jpg', 'jpeg', 'png', 'pbm', 'pgm', 'ppm', 'xbm', 'svg', 'xpm' ] # accepted image types.
  44. Script_Ldr_FileTypes = [ '.mel' , '.py' ] # accepted file types.
  45.  
  46.  
  47. def Script_Loader_WinUI():
  48.     global Scrpt_Ldr_WinUI
  49.     global Scrpt_Ldr_Local_FolderPath
  50.     global Scrpt_Ldr_PaneAttachUI
  51.     global Scrpt_Ldr_ScrollUI  
  52.     global Scrpt_Ldr_SettingsUI
  53.  
  54.    
  55.     Scrpt_Ldr_Win = "Scrpt_Ldr_Win"
  56.     if cmds.window('Scrpt_Ldr_Win', exists = True ):
  57.         cmds.deleteUI('Scrpt_Ldr_Win', window = True )
  58.         cmds.windowPref( 'Scrpt_Ldr_Win', remove = True )
  59.     Scrpt_Ldr_WinUI = cmds.window( Scrpt_Ldr_Win , widthHeight = Scrpt_Ldr_Win_W_H ,  rtf = True , toolbox = True )
  60.  
  61.     Scrpt_Ldr_PaneAttachUI = cmds.paneLayout( 'master_Scrpt_Ldr_Win' , parent = 'Scrpt_Ldr_Win' , shp = True , configuration='horizontal2' )
  62.     cmds.paneLayout( Scrpt_Ldr_PaneAttachUI , edit = True , ps = [ 1 , 0 , 1 ] )
  63.    
  64.    
  65.     cmds.rowColumnLayout( 'master_Scrpt_Ldr_Win_ColsHeader' , numberOfColumns = 2 ,  parent = Scrpt_Ldr_PaneAttachUI )     
  66.  
  67.  
  68.     cmds.rowColumnLayout( 'master_Scrpt_Ldr_Win_ColsHeader_Left' , numberOfColumns = 1 ,  parent = 'master_Scrpt_Ldr_Win_ColsHeader' )     
  69.     cmds.button( 'Script_Loader_WinUI_Btn_Rfrsh' , label = ( 'Script Loader' ) , align = 'left' , ann = ( 'Click to refresh scripts : ' + versionTxt ) , width = ( ( Scrpt_Ldr_Win_W_H[0] / 10 ) * 8 ) , parent = 'master_Scrpt_Ldr_Win_ColsHeader_Left' , command = 'Script_Loader_Refresh()' )
  70.    
  71.    
  72.     cmds.rowColumnLayout( 'master_Scrpt_Ldr_Win_ColsHeader_Right' , numberOfColumns = 2 ,  parent = 'master_Scrpt_Ldr_Win_ColsHeader' )        
  73.     cmds.button( 'Script_Loader_WinUI_Btn_Icon' , label = '#' , ann = 'Icon or Text only mode button.' , width = ( Scrpt_Ldr_Win_W_H[0] / 10 ) , parent = 'master_Scrpt_Ldr_Win_ColsHeader_Right' , command = 'Script_Loader_WinUI_IconText()' )
  74.     cmds.button( 'Script_Loader_WinUI_Btn_Dock' , label = '~' , ann = 'Docking / UnDocking button.' , width = ( Scrpt_Ldr_Win_W_H[0] / 10 ) , parent = 'master_Scrpt_Ldr_Win_ColsHeader_Right' , command = 'Script_Loader_WinUI_Docker()' )
  75.  
  76.  
  77.    
  78.     #opts
  79.  
  80.     Scrpt_Ldr_SettingsUI = cmds.button( label = Scrpt_Ldr_Local_FolderPath[0:15] , ann = Scrpt_Ldr_Local_FolderPath , parent = 'master_Scrpt_Ldr_Win_ColsHeader_Left' , command = 'Script_Loader_LocalPathUpdate()')
  81.  
  82.    
  83.     ## main header done, all scripts go below!
  84.  
  85.     Scrpt_Ldr_ScrollUI = cmds.scrollLayout( parent = Scrpt_Ldr_PaneAttachUI , cr = True , vst = 10 )    #all script catogories nested to this
  86.     print Scrpt_Ldr_ScrollUI
  87.     cmds.showWindow( Scrpt_Ldr_WinUI )
  88.  
  89.    
  90. def Script_Loader_StartUI():
  91.     Script_Loader_WinUI()
  92.     Script_Loader_Refresh()
  93.  
  94.    
  95. def Script_Loader_LocalPathUpdate():    #edits the path button
  96.     global Scrpt_Ldr_Local_FolderPath
  97.     global Scrpt_Ldr_SettingsUI
  98.    
  99.     tmp = cmds.fileDialog2( dir = Scrpt_Ldr_Local_FolderPath , dialogStyle = 1 , fm = 3 )
  100.     if tmp == 'Choose Folder':
  101.         pass
  102.     else:
  103.         cmds.button( Scrpt_Ldr_SettingsUI , parent = 'master_Scrpt_Ldr_Win_ColsHeader_Left' , edit = True , label = tmp[0][0:15] , width = ( Scrpt_Ldr_Win_W_H[0] / 2 ) , ann = tmp[0] )
  104.         Scrpt_Ldr_Local_FolderPath = tmp[0]
  105.         print ( 'User chosen Path: \n' + Scrpt_Ldr_Local_FolderPath + '\n' )
  106.         Script_Loader_Refresh()
  107.  
  108.    
  109. def Script_Loader_WinUI_Docker():
  110.     global Scrpt_Ldr_Docked
  111.     global Scrpt_Ldr_DockUI
  112.     global Scrpt_Ldr_WinUI
  113.    
  114.     if Scrpt_Ldr_Docked is True: # UnDock:
  115.         cmds.deleteUI( Scrpt_Ldr_DockUI )
  116.         Script_Loader_WinUI()
  117.         Scrpt_Ldr_Docked = False
  118.        
  119.     else:                       # Dock
  120.         print 'docking'
  121.         Scrpt_Ldr_Docked = True
  122.  
  123.         Scrpt_Ldr_DockUI = cmds.dockControl( fl = False , epo = False , r = False , area = 'left', aa = 'all' , content = Scrpt_Ldr_WinUI )
  124.         cmds.button( 'Script_Loader_WinUI_Btn_Dock' , edit = True , label = '||' )
  125.     Script_Loader_WinUI_Populate_Fldrs()
  126.  
  127.    
  128. def Script_Loader_WinUI_IconText():
  129.     global Scrpt_Ldr_IconUI
  130.    
  131.     if Scrpt_Ldr_IconUI is True: # Make Text Only :
  132.         Scrpt_Ldr_IconUI = False
  133.         cmds.button( 'Script_Loader_WinUI_Btn_Icon' , edit = True , label = '#' )
  134.     else:                       # Make Icon only :
  135.         Scrpt_Ldr_IconUI = True
  136.         cmds.button( 'Script_Loader_WinUI_Btn_Icon' , edit = True , label = '=' )
  137.  
  138.     Script_Loader_WinUI_Populate_Fldrs()
  139.  
  140.  
  141. def Script_Loader_Refresh():
  142.     global Scrpt_Ldr_Local_FolderPath
  143.    
  144.     if Scrpt_Ldr_Local_FolderPath == 'Choose Folder':
  145.         print 'Please choose a folder filled with Maya scripts to load..'
  146.     else:
  147.  
  148.         print 'Manually Reloading all scripts found and re-populating ScriptLoader.'
  149.  
  150.         if Scrpt_Ldr_Local_FolderPath == 'C:':
  151.             pass
  152.         else:
  153.             Script_Loader_Fldrs_Files()
  154.             Script_Loader_WinUI_Populate_Fldrs()
  155.  
  156.  
  157. def Script_Loader_Fldrs_Files(): #Point it in a general folder, it will feed back a list of folders + files..
  158.     global Results_Folders
  159.     global Results_Folders_Full
  160.     global Results_Files
  161.     global Results_Files_Full
  162.     global Results_Files_Full_Full
  163.     global Scrpt_Ldr_Local_FolderPath
  164.  
  165.    
  166.     tmpPath_Split = Scrpt_Ldr_Local_FolderPath.split( '\\' )
  167.     os_NicePath = posixpath.join( *tmpPath_Split ) # using the Unix style pathing that's what Maya uses, even on windows..
  168.     testNetwork = Scrpt_Ldr_Local_FolderPath.startswith('\\')
  169.     if testNetwork is True:
  170.         Additional = ( '//' ) # adding network prefix..
  171.     else:
  172.         Additional = ''
  173.    
  174.     os_NicePath = ( Additional + os_NicePath )
  175.     print ( 'Printing Nice OS Path: \n ' + os_NicePath + '\n' )
  176.    
  177.     Results_Folders_Full =[ os_NicePath ]
  178.     Results_Folders =['|:']
  179.     Results_Files_Full =[]
  180.     Results_Files =[]
  181.     Results_Files_Full_Full = []
  182.  
  183.  
  184.    
  185.     print 'Finding all folders..\n'
  186.     tmp_Results_Folders = [d for d in os.listdir( os_NicePath ) if os.path.isdir(os.path.join( os_NicePath , d ) ) ]
  187.    
  188.     for x in tmp_Results_Folders:   # finding all folders in location provided.
  189.  
  190.         Results_Folders_Full.append( ( os_NicePath + '/' + x ) )
  191.         Results_Folders.append( x )
  192.  
  193.        
  194.     Results_Files = list( Results_Folders ) # this is done to setup the list with the right amount of items.
  195.     Results_Files_Full = list( Results_Folders )   
  196.    
  197.  
  198.     for i in range( 0 , len( Results_Folders_Full ) ):
  199.         current = Results_Folders_Full[i]
  200.  
  201.         All_Files_In_Current = []
  202.         All_FilesFull_In_Current = []      
  203.  
  204.         for file in os.listdir( current ):
  205.  
  206.             current_FilePath_Full = ( current + '/' + file )
  207.            
  208.             FileTest = os.path.isfile( current_FilePath_Full )
  209.             if FileTest is True:
  210.                
  211.                 All_Files_In_Current.append( file )
  212.                 All_FilesFull_In_Current.append( current_FilePath_Full )               
  213.                 Results_Files_Full_Full.append( current_FilePath_Full )
  214.  
  215.         Results_Files[i] = All_Files_In_Current    
  216.         Results_Files_Full[i] = All_FilesFull_In_Current
  217.  
  218.  
  219. def Script_Loader_MakeNice( nameString , columns ): # given a string, will return a version that should just fit the current window layout
  220.     global Scrpt_Ldr_WinUI
  221.     toRemove = ['.py' , '.mel' , '.icon' , '.png' , '.jpg' ]
  222.     tmp = nameString
  223.     for x in toRemove:
  224.         tmp = tmp.replace( x , '' )
  225.  
  226.     current_WindowWidth =  cmds.window( Scrpt_Ldr_WinUI , w = True , query = True ) # grab current width of control to judge character limit.
  227.  
  228.     character_WindowLength = 4 # amount of pixels a character takes avg.
  229.     frameLayoutIcon = 15 ; scrollBarLayoutIcon = 6 # size in characters, avg.
  230.     charMaxLength = ( ( ( current_WindowWidth  ) / character_WindowLength) - ( frameLayoutIcon + scrollBarLayoutIcon ) ) # area to play with after removing scrollbars width, icons from windows total width.
  231.  
  232.     if charMaxLength < len( tmp ):
  233.         newCleanName = ( tmp[0: ( charMaxLength -3 )] + '..' )
  234.     else:
  235.         newCleanName = tmp[0:int(charMaxLength)]       
  236.  
  237.     if columns > 1:
  238.         newCleanName = newCleanName[0:( int( charMaxLength * .9) / columns )]
  239.        
  240.     return newCleanName
  241.  
  242.  
  243. def Script_Loader_findIcon( scriptName ):  
  244.     global Results_Folders
  245.     global Results_Folders_Full
  246.     global Results_Files
  247.     global Results_Files_Full
  248.     global Results_Files_Full_Full
  249.    
  250.     if '.' in scriptName:
  251.         tmp = scriptName.split('.')
  252.         end = tmp[0]
  253.     else:
  254.         end = scriptName
  255.        
  256.     for i in Results_Files_Full_Full:
  257.         for imageType in Script_Ldr_IconTypes:
  258.             test = i.lower()
  259.             if test.endswith( ( end + "." + imageType ).lower() ):
  260.                 result = i
  261.                 return i
  262.  
  263.  
  264. def Script_Loader_btn_Colour( script_FullList , number ): # given a list of files/scripts, and a number feeds back a colour array.. colour[x ,x ,x]
  265.     sl_bgc = list( Script_Ldr_ScriptCol )
  266.    
  267.     if '.mel' in script_FullList[number]:
  268.         sl_ScriptType = Script_Ldr_ScriptColAll[0]
  269.     else:
  270.         sl_ScriptType = Script_Ldr_ScriptColAll[1]     
  271.    
  272.     if number%2==0:
  273.         divisable = .95
  274.     else:
  275.         divisable = .8         
  276.  
  277.     for i in range( 0 , len(sl_bgc) ):
  278.         sl_bgc[i] = ( ( sl_bgc[i] + sl_ScriptType[i] ) * divisable )
  279.  
  280.     return sl_bgc  
  281.  
  282.    
  283. def Script_Loader_btn_cols( current_FileN_Set ): # given a list, gives back column amount.
  284.     if len( current_FileN_Set ) > 1:
  285.         cols = 2           
  286.         if len( current_FileN_Set ) > 2:
  287.             cols = 3
  288.             if len( current_FileN_Set ) > 3:
  289.                 cols = 4                       
  290.     else:
  291.         cols = 1       
  292.     return cols
  293.  
  294.  
  295. def Script_Loader_btn_command( script_path ): # given a location will feed back the command string to use to initialise.   
  296.     testNetwork = script_path.startswith('\\')
  297.  
  298.     if testNetwork is True:
  299.         Additional = ( '//' ) # adding network prefix..
  300.     else:
  301.         Additional = ''
  302.    
  303.     tmp = script_path.split( '\\' )
  304.     tmp2 = posixpath.join( *tmp ) # using the Unix style pathing that's what Maya uses, even on windows..
  305.  
  306.     Command_String = ''
  307.     if '.mel' in script_path:
  308.         Command_String =  ('mel.eval(\'source \"' + Additional + tmp2 + '\" ;\')' )
  309.     else:
  310.         Command_String =  ('execfile( (\'' + Additional + tmp2 + '\'))' )
  311.  
  312.     return Command_String  
  313.    
  314. def Script_Loader_WinUI_Populate_Fldrs():
  315.     global Scrpt_Ldr_IconUI
  316.     global Results_Folders
  317.     global Results_Folders_Full
  318.     global Results_Files
  319.     global Results_Files_Full
  320.     global Scrpt_Ldr_WinUI
  321.     global Scrpt_Ldr_ScrollUI
  322.     global Scrpt_Ldr_PaneAttachUI
  323.  
  324.    
  325.     cmds.deleteUI( Scrpt_Ldr_ScrollUI )
  326.     Scrpt_Ldr_ScrollUI = cmds.scrollLayout( parent = Scrpt_Ldr_PaneAttachUI , cr = True , vst = 10 )    #all script catogories nested to this
  327.  
  328.    
  329.     Script_Ldr_IgnoreTypes = []
  330.     Script_Ldr_IgnoreTypes.extend( Script_Ldr_IconTypes )
  331.     Script_Ldr_IgnoreTypes.extend( Script_Ldr_FileTypes )  
  332.    
  333.     Script_Ldr_IgnoreTypes_set = set( Script_Ldr_IgnoreTypes )
  334.    
  335.     frameLayouts_Made = [] # UI elements to parent to
  336.  
  337.     if Scrpt_Ldr_IconUI is False:
  338.        
  339.         for i in range( 0 , len( Results_Folders ) ): # this creates the folder frames, not populated.
  340.             tmp_Name = ( '_btn_frameUI' + str(i) )
  341.             tmp = cmds.frameLayout( label = str( Results_Folders[i] ) , cll = True , cl = True , parent = Scrpt_Ldr_ScrollUI )
  342.             cmds.columnLayout( tmp_Name , adj=True)
  343.             frameLayouts_Made.append( tmp_Name )
  344.  
  345.         for i in range( 0 , len( Results_Folders ) ): # for all the folders found.. populates with btns..
  346.             current_Parent = frameLayouts_Made[i]
  347.             current_FileN_Set = Results_Files[i]
  348.             current_FileFull_Set = Results_Files_Full[i]
  349.  
  350.            
  351.             count = 0
  352.             for BtnName in current_FileN_Set:
  353.                 tmpCommand = Script_Loader_btn_command( current_FileFull_Set[count])
  354.                 buttonType = BtnName.split('.')
  355.                 if buttonType[1].lower() in Script_Ldr_IgnoreTypes_set:
  356.                     print ( 'ignored type ' + BtnName )
  357.                 else:  
  358.                     cmds.iconTextButton( style = 'textOnly' , bgc = ( Script_Loader_btn_Colour( current_FileN_Set , count ) ), label = Script_Loader_MakeNice( BtnName , 1 ) , align = 'left' , parent = current_Parent , command = tmpCommand )
  359.                 count+=1
  360.                
  361.  
  362.     else:
  363.    
  364.         for i in range( 0 , len( Results_Folders ) ): # this creates the folder frames, not populated.
  365.             tmp_Name = ( '_btn_frameUI' + str(i) )
  366.             tmp = cmds.frameLayout( label = str( Results_Folders[i] ) , cll = True , cl = True , parent = Scrpt_Ldr_ScrollUI )
  367.  
  368.             current_FileN_Set = Results_Files[i]
  369.            
  370.             cmds.rowColumnLayout( tmp_Name , numberOfColumns = Script_Loader_btn_cols( current_FileN_Set ) )
  371.             frameLayouts_Made.append( tmp_Name )
  372.  
  373.  
  374.         for i in range( 0 , len( Results_Folders ) ): # for all the folders found.. populates with btns..
  375.             current_Parent = frameLayouts_Made[i]
  376.             current_FileN_Set = Results_Files[i]
  377.             current_FileFull_Set = Results_Files_Full[i]
  378.             columns = Script_Loader_btn_cols( current_FileN_Set )
  379.            
  380.             count = 0
  381.             for BtnName in current_FileN_Set:
  382.            
  383.                 pass ## need to find a way to remove everything but mel and py and txt from the all list.
  384.                 buttonType = BtnName.split('.')
  385.                
  386.                 if buttonType[1].lower() in Script_Ldr_IgnoreTypes_set:
  387.                     print ( 'ignored type ' + BtnName )
  388.                 else:
  389.                     current_WindowWidth =  cmds.window( Scrpt_Ldr_WinUI , w = True , query = True ) # grab current width of control to judge character limit.
  390.                     tmpName = Script_Loader_MakeNice( BtnName , columns )  
  391.                     tmpImage = Script_Loader_findIcon( current_FileN_Set[count] )
  392.                     tmpCommand = Script_Loader_btn_command( current_FileFull_Set[count])
  393.  
  394.                     if tmpImage is None:
  395.                         cmds.iconTextButton( style = 'textOnly' , bgc = ( Script_Loader_btn_Colour( current_FileFull_Set , count ) ), label = tmpName , width = ( (current_WindowWidth - 20) / Script_Loader_btn_cols( current_FileFull_Set ) ) , align = 'left' , parent = current_Parent , ann = BtnName , command = tmpCommand )
  396.                     else:
  397.                         cmds.iconTextButton( style = 'iconOnly' , image1 = tmpImage , bgc = ( Script_Loader_btn_Colour( current_FileFull_Set , count ) ) , align = 'left' , parent = current_Parent , ann = BtnName , command = tmpCommand )       
  398.                 count+=1
  399.  
  400.  
  401.        
  402. Script_Loader_StartUI()
Advertisement
Add Comment
Please, Sign In to add comment