Search code examples
pythonpysidemaya

How to add model panel to layout created in designer in maya PySide


I am creating a script in i want to place a model panel in a grig layout created in QtDesigner.

enter image description here

model panel has to be placed in the red area. I tried

cmds.setParent(self.LAY_grid_modelEditor)
cmds.modelPanel('testEditor', cam='persp')
cmds.setParent('..')

but getting the error

RuntimeError: setParent: Object '' not found.

I am using PySide in Maya 2015.

Any Help...


Solution

  • This code is actually I took it from http://nathanhorne.com/embedding-a-maya-widget-into-a-pyqt-ui/, which will show how you can embed a maya gui inside pyqt. This same method you can use for pyside too. This example code I modified and now it will load a modelPane

    import maya.OpenMayaUI as apiUI
    from PyQt4 import QtGui, QtCore
    import sip
    
    def getMayaWindow():
        ptr = apiUI.MQtUtil.mainWindow()
        return sip.wrapinstance(long(ptr), QtCore.QObject)
    
    def toQtObject(mayaName):
        '''
        Given the name of a Maya UI element of any type,
        return the corresponding QWidget or QAction.
        If the object does not exist, returns None
        '''
        ptr = apiUI.MQtUtil.findControl(mayaName)
        if ptr is None:
            ptr = apiUI.MQtUtil.findLayout(mayaName)
        if ptr is None:
            ptr = apiUI.MQtUtil.findMenuItem(mayaName)
        if ptr is not None:
            return sip.wrapinstance(long(ptr), QtCore.QObject)
    
    class MayaSubWindow(QtGui.QMainWindow):
        def __init__(self, parent=getMayaWindow()):
            super(MayaSubWindow, self).__init__(parent)
            self.modelPne = cmds.modelPanel('testEditor', cam='persp')
            qtObj = toQtObject(self.modelPne)
            self.setCentralWidget(qtObj)
    
    myWindow = MayaSubWindow()
    myWindow.show()