Guest User

Untitled

a guest
Nov 11th, 2010
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.61 KB | None | 0 0
  1. import maya.OpenMayaUI as mui
  2. import sip
  3.  
  4.  
  5. def GetMayaLayout(layoutString):
  6.     '''
  7.    Will return the correct QT object for a given UI string.
  8.  
  9.    :param layoutString: `str`
  10.    :returns: `QWidget`
  11.  
  12.    >>> GetMayaLayout('MayaWindow|MainAttributeEditorLayout')
  13.    '''
  14.     ptr = mui.MQtUtil.findLayout(layoutString)
  15.     if ptr:
  16.         return sip.wrapinstance(long(ptr), QtCore.QObject)
  17.  
  18. def GetWindow(windowName):
  19.     '''
  20.    Will get a child floating QWidget of the Maya Main Window
  21.    :param windowName: `str` eg "AssetBrowserForm"
  22.  
  23.    :returns: `QWidget`
  24.    '''
  25.     ptr = mui.MQtUtil.findWindow(windowName)
  26.     if ptr:
  27.         return sip.wrapinstance(long(ptr), QtCore.QObject)
  28.  
  29. def GetFullName(QObject):
  30.     '''
  31.    Get the fullName of a QObject
  32.    '''
  33.     pointer = sip.unwrapinstance(QObject)
  34.     if type(pointer) == long:
  35.         windowString = mui.MQtUtil.fullName(pointer)
  36.         if windowString:
  37.             return windowString
  38.         else:
  39.             return ""
  40.     else:
  41.         # Fix for bug in 32bit Maya.... build the long name as we search for the QObject
  42.         return GetQtWidget(QObject.objectName(), LongName = True)[-1]
  43.  
  44.  
  45. def GetMayaMainWindow():
  46.     '''
  47.    :returns: `QtWidget` that pertains to the Maya Main Window
  48.    '''
  49.  
  50.     ptr = mui.MQtUtil.mainWindow()
  51.     if ptr:
  52.         return sip.wrapinstance(long(ptr), QtCore.QObject)
  53.  
  54.  
  55. def GetQtWidget(QWidgetName, LongName=False):
  56.     '''
  57.    :param QWidgetName: `str`
  58.    :returns: `QtWidget` that pertains to the maya interface
  59.    '''
  60.     RootName = str(GetMayaMainWindow().objectName())
  61.     Name = QWidgetName.split("|")[-1]
  62.     for w in QtGui.qApp.topLevelWidgets():
  63.         try:
  64.             if w.objectName() == Name:
  65.                 if LongName:
  66.                     return (w, "|" + "|".join([RootName, QWidgetName]))
  67.                 else:
  68.                     return w
  69.         except:
  70.             pass
  71.     try:
  72.         for w in QtGui.qApp.topLevelWidgets():
  73.             for c in w.children():
  74.                 if c.objectName() == Name:
  75.                     if LongName:
  76.                         return (c, "|" + "|".join([str(w.objectName()),
  77.                                              str(c.objectName())]))
  78.                     else:
  79.                         return c
  80.     except:
  81.         pass
  82.  
  83.  
  84. def UIExists(Name, AsBool=True):
  85.     '''
  86.    Simple wrapper that will search both long and short names of UI's.  Good if the window
  87.    has become docked.
  88.  
  89.    :param Name: Name of the UI we're looking for.  Can be the shortName or fullaName.
  90.    :Returns: `QObject` or a `bool` based on AsBool parameter.
  91.    '''
  92.  
  93.     QObject = GetQtWidget(Name)
  94.     if QObject:
  95.         if AsBool:
  96.             return bool(QObject)
  97.         return QObject
  98.     else:
  99.         if AsBool:
  100.             return False
  101.         return None
  102.  
  103.  
  104. def Raise(Name):
  105.     '''
  106.    Based on the long or shortName this will find the QObject and call QObject.raise_()
  107.    '''
  108.     QObject = GetQtWidget(Name)
  109.     if QObject:
  110.         QObject.setHidden(False)
  111.         QObject.raise_()
  112.         return True
  113.     else:
  114.         return False
  115.  
  116.  
  117. def getQtElement(elementString, guiTarget, LongName=False):
  118.     """
  119.     Finds any ui element within the given target
  120.    
  121.     Input:  elementString: 'str' of the QObject
  122.             guiTarget: 'str' of the UI to search within
  123.            
  124.     Note: If you've made the window dock-able inside Maya, pass the dockControl name   
  125.     """
  126.    
  127.     qSearch = GetQtWidget(guiTarget)
  128.     for a in qSearch.findChildren(QtGui.QWidget):
  129.         if a.objectName() == elementString:
  130.             if LongName:
  131.                 return GetFullName(a)
  132.             else:
  133.                 return a
Advertisement
Add Comment
Please, Sign In to add comment