gpt4 book ai didi

qt - Autodesk Maya 模型面板调整大小事件

转载 作者:行者123 更新时间:2023-12-04 21:39:41 30 4
gpt4 key购买 nike

我正在为 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()信号。我会很感激任何建议。

最佳答案

昨天我一直在尝试很多事情来解决这个问题。我今天做了一些额外的研究,来到了这个话题:cgsociety: Creating a floating button inside the viewport

如果链接断开,这是答案之一:

You can use geometry but there are some issues with triggering commands based on selection and the undo queue. If you want to go that route, I would suggest looking into zooHud and zooTriggers (Part of the zooToolbox)

If you are wanting actual GUI control parented to the viewport, mel only offers hudslider, hudbutton, and headsUpMessage.

You can also use PyQt and parent in your own custom widgets/layouts or whatever you want using something like this:

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()

You can do anything a full qt widget can do with that, so it's extremely flexible. but it would require having PyQt installed, which can be a barrier depending on your tools distribution.



我混合了这个答案和你的代码:
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()

我遇到的问题之一是小部件的背景出现故障: Screenshot

如果有人知道为什么以及如何解决它,我会很高兴地编辑我的答案。
否则,当从 Maya 的脚本编辑器运行此脚本时,小部件在调整大小时会跟随面板。

关于qt - Autodesk Maya 模型面板调整大小事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29762651/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com