Custom window not docking to the main window at startup

Hello,

I made a little tool window that runs in maya using python. At first, it was docked next to the channelbox editor, and adding the script to maya’s startup worked. But I found it annoying having to toggle back and forth between the channel box editor / attribute editor / and my window, so I’m trying to dock it to the right of the main window, so both my tool window + the channel box would be visible.
I initially wanted to add it to the opposite side of the channel box editor, by adding a vertical splitter to the panel, but I couldn’t figure out how to do it by code. Manually I can just drag the window to the right of the channel box panel and a blue line appears and then it splits the panel and docks itself to the right, but I couldn’t do it by code. I eventually found out that I could dock it to the right of the main window, which works, but now it doesn’t run at startup anymore.

  • if Maya was last closed with the tool window open and docked to the right of the main window, and then I re-open Maya, it doesn’t open at all anymore unless I reset my workspace

  • if I reset my workspace and relaunch maya, it shows up docked to the main window properly

  • if I run it manually after re-opening maya, it opens as floating and not docked anymore

So to sum it all up, the only way to get it to open docked to the main window is after resetting my workspace, which I prefer not to do. I’d like for it to open docked to the main window upon maya launch. Any help would be appreciated!

Here is a snippet of what the script looks like:

import maya.cmds as cmds
import maya.utils as utils
import maya.mel as mel
import maya.OpenMayaUI as omui
from PySide2 import QtWidgets, QtCore
from shiboken2 import wrapInstance

def get_maya_main_window():
    """Get the Maya main window as a Qt object."""
    main_window_ptr = omui.MQtUtil.mainWindow()
    return wrapInstance(int(main_window_ptr), QtWidgets.QWidget)

def open_ui_for_dock(dock_name):
    """Open the cmds-based UI inside the dock."""
    scroll_layout = cmds.scrollLayout(parent=dock_name, height=1000, width=210)
    create_ui_content(scroll_layout)

def create_ui_content(parent):
    """Add all UI elements to the given parent layout."""
    cmds.columnLayout(parent=parent, adjustableColumn=True)
    cmds.text(label="Scene setup", align="center", height=20)

    cmds.rowLayout(numberOfColumns=2)
    cmds.button(label="Save", command=lambda _: run_save_scene(), height=35, width=100, bgc=(0.89, 0.498, 0.498))
    cmds.button(label="Version Up", command=lambda _: run_save_increment(), height=35, width=100, bgc=(0.89, 0.498, 0.498))
    cmds.setParent("..")

def open_ui():
    """Create a dockable window and dock it to the right."""
    dock_name = "MM_DockableWindow"

    # Remove the workspace control state if it exists
    if cmds.workspaceControlState(dock_name, exists=True):
        cmds.workspaceControlState(dock_name, remove=True)

    # Delete the workspace control if it exists
    if cmds.workspaceControl(dock_name, exists=True):
        cmds.deleteUI(dock_name, control=True)

    # Create the dockable workspace control
    cmds.workspaceControl(
        dock_name,
        label="My window",
        dockToMainWindow=("right", 1),
        initialWidth=205,
        initialHeight=1000,
        collapse=0,
        retain=False,
    )

    # Use cmds-based UI
    open_ui_for_dock(dock_name)


# Run the script to create the dock
if __name__ == "__main__":
    open_ui()