Autodesk Maya Model Panel Resize Event

I am writing a simple tool menu for Maya, and I would like to snap it to the border of the model panel (perspective).

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

The main widget is located exactly where I want it when it is created (horizontally in the left part of the model panel, vertically in the middle of the model panel).

I need to change it accordingly when resizing the model panel, but the model panel does not emit a resized() signal. I would appreciate any advice.

+5
source share
3 answers

I tried many things to get this working yesterday. Today I did some more research and came up with this topic: cgsociety: creating a floating button inside the viewport

In the case of a broken link, this is one of the answers:

You can use geometry, but there are some problems with running commands based on selection and undo queue. If you want to go the route, I would suggest looking at zooHud and zooTriggers (part of zooToolbox)

If you want the actual GUI control to be selected for viewing, mel only offers hudslider, hudbutton and headUpMessage.

You can also use PyQt and parent in your own widgets / layouts or whatever you want to use 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 whatever the full Qt widget can do, so it is extremely flexible. but this will require installing PyQt, it may be a barrier depending on the distribution of your tools.

I made a mixture of this answer and your code:

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

One of the problems is that the widget's background is clogged: Screen shot

If anyone knows why and how to fix it, I will be happy to edit my answer. In addition, when you run this script from a Mayan script, the widget editor follows the panel when it changes.

+1
source

I fixed such a problem but did not use Python / PyQt.

The problem itself is that you have a Qt widget. I did not find a way to make it not paint my background.

My solution was different: I got from Qt Layout, moved all my widgets to this layout and used MQtUtil to get the QWidget of this modelPanel modelEditor to attach the “real Qt layout” to it.

A heavy caveat that might make Python inapplicable: Maya does not expect non- Maya layouts to be bound to real-Maya widgets such as modelEditors. So you need to listen to QEvents and find out when to destroy the layout, so Maya doesn't crash the attempts.

0
source

set autofillbackground True to fix the problem with the wallpaper

0
source

All Articles