Maya - techartorg/TAO-Wiki GitHub Wiki

Doc PySide Widgets in Maya Dockable Window

This function allows you to dock PySide Gui's into a dockable window in Maya. It's fairly straightforwards to use Credit goes to Jeroen Heemskerk who came to my rescue when I was searching for an easy way to dock existing ui's without having to add more to them.

# import whichever pyside you need
from PySide2 import QtWidgets

# from PySide6 import QtWidgets
try:
    import shiboken2 as shib
except ImportError:
    try:
        import shiboken6 as shib
    except:
        print("COULDN'T IMPORT SHIBOKEN!")

from maya import OpenMayaUI as omui
import maya.cmds as cmds


def create_dockable_window(widget, title: str):
    """Creates a dockable Window"""

    workspace_control_name = widget.objectName() + "_WorkspaceControl"

    if cmds.workspaceControl(workspace_control_name, q=True, exists=True):
        cmds.deleteUI(workspace_control_name)

    # Create the dock with Maya's workspace control system
    control = cmds.workspaceControl(
        workspace_control_name,
        label=title,
        floating=True,
        resizeHeight=500,
        resizeWidth=500,
        # ttc=["AttributeEditor", -1],  # Target tab (optional, removes float behavior)
        # retain=True,  # <-- makes it persistent across sessions
        # uiScript="import MyModule; MyModule.MyFunction()"
    )

    # Parent the widget under the workspace control
    control_ptr = omui.MQtUtil.findControl(workspace_control_name)
    control_widget = shib.wrapInstance(int(control_ptr), QtWidgets.QWidget)
    layout = control_widget.layout()
    if layout is not None:
        layout.addWidget(widget)
# Assuming you have a tool that has a ui object
# In theory this should also work if your business logic is also in your gui - just pass in whatever pyside widget you want docked
import your_tool
tool = your_tool.Tool()
create_dockable_window(tool.ui, "MyTool")