1

I have a simple window in python script like :

if cmds.window('test', exists = True): cmds.deleteUI('test')
self.window = cmds.window('test')

instead of delete window and reset controls inside for every time executed, I want to maximize window to it's origin (if minimized) or resize to some position so i'ts visible and easily noticed, is there any way to do it ? i know i can pass/ cancel if exist, but i just want to resize window
thanks.

2 Answers2

0

I don't think it is possible with maya cmds but you can do it without Qt. Here is an example do to an hybrid solution :

from PyQt4 import QtGui
from sip import wrapinstance
import maya.OpenMayaUI as omui

def mayaToQT( name ):
    # Maya -> QWidget
    ptr = omui.MQtUtil.findControl( name )
    if ptr is None:         ptr = omui.MQtUtil.findLayout( name )
    if ptr is None:         ptr = omui.MQtUtil.findMenuItem( name )
    if ptr is not None:     return wrapinstance( long( ptr ), QtGui.QWidget )

# create a basic window
def createWin():
    window = cmds.window( title="Long Name", iconName='Short Name', widthHeight=(200, 55) )
    cmds.columnLayout( adjustableColumn=True )
    cmds.button( label='Do Nothing' )
    cmds.button( label='Close', command=('cmds.deleteUI(\"' + window + '\", window=True)') )
    cmds.setParent( '..' )
    cmds.showWindow( window )
    return window

# First creation
win = createWin()
qtWin = mayaToQT(win)
if cmds.window(win, exists = True):
    geom = qtWin.geometry()
    cmds.deleteUI(win)

# Second Creation and put the window back where it was
win = createWin()
qtWin = mayaToQT(win)
qtWin.setGeometry(geom)

Set Geometry can be done with an arbitrary position on the screen or anything you zqnt

DrWeeny
  • 2,487
  • 1
  • 14
  • 17
  • hey thanks, but I can't get the code work in maya 2015 , since i didn't use pyQt so i changed import to **pyside** and **sip** to **shiboken**, but still can't import **wrapinstance** what did i miss ? – Rifqi Khairur Rahman Mar 14 '17 at 02:53
  • wrapinstance is written wrapInstance in pyside with a capital i – DrWeeny Mar 14 '17 at 10:09
0

you have a few options at hand, depending on how you want it to look... These are all default python / maya functions, and works in previous versions, back to Autodesk Maya 2012.

Dock your Window

You can dock your window to a particular side, which would help you manage your scene in Maya. The easiest way to do this is:

# Import your maya command package
import maya.cmds as cmds
try:
    # Try to delete your window then the dock
    cmds.deleteUI("yourWnd")
    cmds.deleteUI("yourDock")
except:
    pass
# Parent your window to this dock and then show the dock instead of the window
cmds.window("yourWnd")
cmds.columnLayout("col")
cmds.button("button1")
cmds.setParent("col")
cmds.dockControl("yourDock", 
                 allowedArea = ["left","right"],
                 area = "left",
                 content = "yourWnd",
                 floating = False)

This will put your UI in an easily accessible location in your Maya Window.

Resizing your window

If you've decided to create a window, you could easily add a button or into your window that resizes your window...

import maya.cmds as cmds
import ctypes
# having imported the ctypes package, you can get viable os system info
user32 = ctypes.windll.user32
# Getting the screen size of your main window
screensize = user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)
cmds.window("yourWnd")
cmds.columnLayout("col")
# A button that allows you to imitate maximizing your window on your mainscreen
cmds.button("maxPlease",
            label = "Maximize",
            command = """cmds.window("yourWnd",
                                     edit=True,
                                     topLeftCorner = [0,0],
                                     widthHeight = [screensize[0],
                                                    screensize[1]])
                      """)
cmds.setParent("col")
cmds.showWindow("yourWnd")

And these are just basics, you can mix and match the two of these to work your own solution. I highly recommend using ctypes, as it will most definitely help you with resizing to maximize your window.

SirJames
  • 387
  • 8
  • 27
  • I am approaching your solution to resize and restore window, using ctype, like this answer [link]http://stackoverflow.com/questions/32344140/is-there-a-way-to-minimize-a-window-in-windows-7-via-python-3[link] but i cant get the window handle, when i look at winspector /spy++ ,all window class under maya is QWidget, and any window name didnt matter, did i miss something ? – Rifqi Khairur Rahman Mar 14 '17 at 03:05
  • Its parent program is Maya, so that would make sense. I don't have winspector installed at my day jobs computer. However, the particular instance I've provided is a Maya and Python Vanilla Script and doesn't utilize PySide or PyQT... So finding a QWidget class is against the grain since it's not using that. – SirJames Mar 14 '17 at 10:57
  • The easier way to minimize a window, though, is to set a button with a command that does this: `cmds.button("minPlease", label = "Minimize", command = "cmds.window('yourWnd', edit=True, iconify=True)" – SirJames Mar 14 '17 at 10:59