Nov 20

Mixing PyQt4 widgets and Maya UI objects

A question came up in the Maya-Python mailing list that I thought was a really good topic, and should be reposted.

Someone asked how you can create maya UI objects and embed them within your main PyQt application. Specifically he wanted to create a modelPanel and embed it so that he would have a camera view within his own PyQt window.

Here is my example of how to achieve this…

You need sip and the MQtUtil functions to convert between maya node paths and python Qbjects. Its the same idea as having to use those functions to get a reference to the maya MainWindow, in order to parent your dialog.


1 ping

Skip to comment form

  1. ica

    As you described, I could ship a camera on PyQT UI.
    But, I want to do something more.
    I tried add pushbutton on camera view(a.k.a modelPanel) with addWidget method. However, it didn’t work well.
    I dreamed the camera view that could be controlled freely and have attached HUD items.
    Is there any way to solve my problem?

  2. Justin Israel

    @ica – If you are trying to overlay widgets on top of the camera view to simulate a HUD, then you would probably not want to add the PushButton to the Vertical Layout. That would just end up putting it underneath as it is laying widgets out in a column. What you probably just want to do is create the button and parent it to the Model Panel, but don’t add it to a layout. Then you can Move it to the location you want and it will sit freely on top of your camera view. I haven’t tested this but its my best guess :-)

  3. ica

    Sorry for late feedback.
    As you recommened, I tried “JUST” parent the QPushButton to Model Panel. Actually, I parented one of Model Panel’s children. It doesn’t mess up the camera view, and it can move on the view as I command.
    But, when I add a QPushButton once again, something happens. At the time additional creation, position is reset! I means that everytime I add buttons on Model Panel, I have to re-position all of the buttons. How can I lock them all?

    1. Justin Israel

      Im not exactly sure what happening. Can you email me directly and we can look at some of your code samples? As a workaround for now, you COULD just create a convenience method for adding a new button that adds it and repositions all the existing buttons again. Probably could save button positions to a dictionary each time and then use that for realigning them. It could be Maya’s parent widgets not liking free floating children not in layouts..Who knows. Ideally they should stay put.

  4. Jeremy

    Sorry for the noob comments but I used your installer for PyQt and then copied and pasted the above code in the script editor, ran the code, got no errors butI didnt get a window to open either. Is there something I missed?

    Thanks for your patience.

  5. Jeremy

    got it…show()

  6. Vlad

    Very impressive example, Justin, thank you!

    Can you help me please with modelEditor instead of modelPanel?

    The problem is – I can not wrap properly modelEditor, I am getting white panel in my widget…

    Thank you in advance!


  7. Carlo

    Hi Justin,

    I am using your way of implementing a modelPanel in my PyQt interface, but I was wondering if you got any crashing issues. If from the preferences window>UI Elements>Panel Configurations>Save panel layouts with file is checked on, Maya crashes 80 to 90% of the times upon saving. Do you have this issue?
    (I also tried other ways to implement the modelPanel and they give me the same result)



  8. Rahul

    Works great, didn’t crash for me. Thanks for the tutorial

  9. Mark

    Hey Justin,

    Great example! Just wanted to ask if it was possible to embed a window instead of a modelPanel, having trouble trying to get the renderView/Panel stuck in place instead of the viewport. My means to an end is to basically grab the rendered image panel and not the toolbar but at the moment want to know if it’s even possible to put the render window in or it’s some special case where it won’t work.



  10. Jhonie

    for those who are wondering how to do it in PySide



    from PySide import QtCore, QtGui

    import maya.cmds as cmds
    import maya.OpenMayaUI as mui

    import shiboken

    class MyDialog(QtGui.QDialog):

    def __init__(self, parent, **kwargs):
    super(MyDialog, self).__init__(parent, **kwargs)
    print ‘here’
    self.resize(800, 600)
    self.setWindowTitle(“PySide ModelPanel Test”)

    self.verticalLayout = QtGui.QVBoxLayout(self)

    # need to set a name so it can be referenced by maya node path

    # First use shiboken to unwrap the layout into a pointer
    # Then get the full path to the UI in maya as a string
    layout = mui.MQtUtil.fullName(long(shiboken.getCppPointer(self.verticalLayout)[0]))
    print ‘layout’, layout
    #layout = mui.MQtUtil.fullName(long(shiboken.unwrapInstance(self.verticalLayout)))

    paneLayoutName = cmds.paneLayout()

    # Find a pointer to the paneLayout that we just created
    ptr = mui.MQtUtil.findControl(paneLayoutName)

    # Wrap the pointer into a python QObject
    self.paneLayout = shiboken.wrapInstance(long(ptr), QtGui.QWidget)

    self.cameraName = cmds.camera()[0]
    self.modelPanelName = cmds.modelPanel(“customModelPanel”, label=”ModelPanel Test”, cam=self.cameraName)

    # Find a pointer to the modelPanel that we just created
    ptr = mui.MQtUtil.findControl(self.modelPanelName)

    # Wrap the pointer into a python QObject
    self.modelPanel = shiboken.wrapInstance(long(ptr), QtGui.QWidget)

    # add our QObject reference to the paneLayout to our layout

    def showEvent(self, event):
    super(MyDialog, self).showEvent(event)

    # maya can lag in how it repaints UI. Force it to repaint
    # when we show the window.

    def show():
    # get a pointer to the maya main window
    ptr = mui.MQtUtil.mainWindow()
    # use shiboken to wrap the pointer into a QObject
    win = shiboken.wrapInstance(long(ptr), QtGui.QWidget)
    d = MyDialog(win)

    return d

    dialog = show()

  1. Maya 2011-2015的PyQt Windows7 Linux Mac OSX 国外大神屌丝编译版大合集 | 一半君的回忆

    […] nathanhorne.com/?p=485 http://www.justinfx.com/2011/11/20/mixing-pyqt4-widgets-and-maya-ui-objects/ tech-artists.org/forum/showthread.php?3035-PySide-in-Maya-2013 […]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">