2015-04-21 46 views
5

我正在爲Maya編寫一個簡單的工具菜單,我想將其粘貼到模型面板(透視圖)的邊框。Autodesk Maya模型面板調整大小事件

from PySide import QtCore, QtGui 
from maya import OpenMayaUI as omui 
from shiboken import wrapInstance 

class TestWidget(QtGui.QWidget): 
    def __init__(self, parent=None): 
     QtGui.QWidget.__init__(self, parent = self.getMayaWindow()) 

     self.setWindowFlags(QtCore.Qt.Tool | QtCore.Qt.FramelessWindowHint) 
     self.setFixedSize(100, 100) 

     panelPtr = omui.MQtUtil.findControl('modelPanel4') 
     panel = wrapInstance(long(panelPtr), QtGui.QWidget) 

     position = panel.mapToGlobal(panel.pos()) 
     self.move(position.x(), position.y() + panel.geometry().height()/2 - self.geometry().height()/2) 

     mainLayout = QtGui.QVBoxLayout(self) 

     button = QtGui.QPushButton('CLOSE') 
     button.setFixedSize(80, 80) 
     button.clicked.connect(self.deleteLater) 

     mainLayout.addWidget(button) 

    def getMayaWindow(self): 
     omui.MQtUtil.mainWindow()  
     ptr = omui.MQtUtil.mainWindow() 
     return wrapInstance(long(ptr), QtGui.QWidget) 

w = TestWidget() 
w.show() 

主插件被精確地定位,我想在創建時(水平上模型板的左側,垂直 - 在模型面板的中間)。

當模型面板調整大小時,我需要重新定位它,但型號面板不會發出resized()信號。我會很感激任何建議。

+0

任何從父窗口獲取「resized」信號的方式? – mhlester

+0

@mhlester據我所知,Maya主窗口或任何其他原生Maya小部件在調整大小時不會發出任何事件。 – Kupnu4

回答

1

昨天我一直在嘗試很多事情來讓這個工作。我今天做了一些其它附加的研究,並來到了這個話題:cgsociety: Creating a floating button inside the viewport

在斷開鏈路的情況下,這就是答案之一:

您可以使用幾何形狀,但是也有一些問題,觸發 命令基於在選擇和撤消隊列上。如果你想要去那個 路線,我會建議尋找到zooHud和zooTriggers(的 部分的zooToolbox)

如果你想要父到視實際的GUI控制,梅爾 只提供hudslider,hudbutton和headsUpMessage 。

您還可以使用的PyQt和家長在自己的自定義部件/佈局或 任何你想使用這樣的事情:

import maya.OpenMayaUI as apiUI import sip 

from PyQt4 import QtGui 

view = apiUI.M3dView() 
apiUI.M3dView.getM3dViewFromModelPanel('modelPanel4', view) 
viewWidget = sip.wrapinstance(long(view.widget()), QtCore.QObject) 

global myBtn 
myBtn = QtGui.QPushButton(viewWidget) 
myBtn.setText('testing!') 
myBtn.move(100, 100) #Relative to top-left corner of viewport myBtn.show() 

你可以做任何事情,一個完整的Qt物件可以用做的,所以它的 非常靈活。但它需要安裝PyQt,根據您的工具分佈情況, 可能成爲障礙。

我做了這個答案的組合和你的代碼:

from PySide import QtCore, QtGui 
from maya import OpenMayaUI as omui 
from shiboken import wrapInstance 

class CustomQWidget(QtGui.QWidget):  
    def __init__(self, *args, **kwargs): 
     QtGui.QWidget.__init__(self, *args, **kwargs) 

     mainLayout = QtGui.QVBoxLayout(self) 

     closeButton = QtGui.QPushButton('CLOSE') 
     closeButton.setFixedSize(80, 40) 
     closeButton.clicked.connect(self.deleteLater) 

     helloButton = QtGui.QPushButton('HELLO') 
     helloButton.setFixedSize(80, 40) 
     helloButton.clicked.connect(self.printHello) 

     #Trying to fix glitchy background/Doesn't work, why? 
     #Is it because layouts don't have background? 
     p = self.palette() 
     p.setColor(self.backgroundRole(), QtCore.Qt.red) 
     self.setPalette(p) 
     self.setAttribute(QtCore.Qt.WA_StyledBackground, True) 
     ############################## 

     mainLayout.addWidget(closeButton) 
     mainLayout.addWidget(helloButton) 

    def printHello(self): 
     print "Hello" 

view = omui.M3dView() 
omui.M3dView.getM3dViewFromModelPanel('modelPanel4', view) #Given the name of a model panel, 
#get the M3dView used by that panel. If this fails, then a panel with the given name could not be located. 
viewWidget = wrapInstance(long(view.widget()), QtGui.QWidget) 

position = viewWidget.mapToGlobal(viewWidget.pos()) 

w = CustomQWidget(viewWidget) 
w.move(0, viewWidget.geometry().height()/2 - 100/2) #Relative to middle-left corner of viewport 
w.show() 

問題的一個我認爲小部件的背景glitched:Screenshot

如果有誰知道爲什麼和如何解決它,我會很高興地編輯我的答案。 否則,當從Maya的腳本編輯器運行此腳本時,該小部件在調整大小時跟在面板上。

0

我確實修復了這樣的問題,但沒有使用Python/PyQt。

問題本身就是,你的Qt Widget在那裏。我還沒有找到一種方法來使它不是油漆它的背景。

我的解決方案是不同的:我從Qt Layout中派生出來,將所有的小部件推入該佈局,並使用MQtUtil獲取該模型面板的modelEditor的QWidget,以將「真實的Qt佈局」附加到它。

可能導致Python不適用的重要警告: Maya並不認爲「非Maya」佈局會綁定到像「模型編輯器」這樣的「真正的Maya」小部件。所以你需要聽取QEvent並找出何時破壞你的佈局,所以Maya不會試圖崩潰。

0

set autofillbackground真正解決你的背景畫問題