Python : Add a new Tab in Maya Main UI - python

I'm a beginner in programing in Maya, and I want to create a new Window INSIDE the main UI of Maya. The ideal place is in the same place that the attribute editor. I know it's possible because XGen does that, but I really do not find how :'(
Like here !

You can create a window with maya.cmds scripting and make this window dockable in maya
https://help.autodesk.com/cloudhelp/2017/ENU/Maya-Tech-Docs/CommandsPython/dockControl.html

Using the dockControl example:
import maya.cmds as cmds
myWindow = cmds.window()
buttonForm = cmds.formLayout( parent = myWindow )
cmds.button( parent = buttonForm )
allowedAreas = ['right']
cmds.dockControl( area='right', content=myWindow, allowedArea=allowedAreas )
you can create any arbitrary window and enforce/allow it to dock at a specific location.
The above example will spawn and restrict 'myWindow' to the Right side of your UI, unless you undock it...
Extending the allowedAreas attribute gives more location options if you want to use them.

Related

Maya Python: Button always at the center of the window

I'm starting experimenting with Maya python, and I'm trying to do some UI.
I came across to a really strange problem, I can't get a button to stay in the center of the windows.
I've tried different things but nothing seems to work, here is the code:
import maya.cmds as cmds
cmds.window( width=200 )
WS = mc.workspaceControl("dockName", retain = False, floating = True,mw=80)
submit_widget = cmds.rowLayout(numberOfColumns=1, p=WS)
cmds.button( label='Submit Job',width=130,align='center', p=submit_widget)
cmds.showWindow()
this is a simple version but still, I can't get it to work.
can someone help me?
I honestly don't know the answer as anytime I have to dig into Maya's native UI stuff it makes me question my own life.
So I know it's not exactly what you're asking for, but I'll opt with this: Use PySide instead. At first glance it might make you go "woah, that's way too hard", but it's also a million times better (and actually easier). It's much more powerful, flexible, has great documentation, and also used outside of Maya (so actually useful to learn). Maya's own interface uses the same framework, so you can even edit it with PySide once you're more comfortable with it.
Here's a bare-bones example to create a centered button in a window:
# Import PySide libraries.
from PySide2 import QtCore
from PySide2 import QtWidgets
class MyWindow(QtWidgets.QWidget): # Create a class for our window, which inherits from `QWidget`
def __init__(self, parent=None): # The class's constructor.
super(MyWindow, self).__init__(parent) # Initialize its `QWidget` constructor method.
self.my_button = QtWidgets.QPushButton("My button!") # Create a button!
self.my_layout = QtWidgets.QVBoxLayout() # Create a vertical layout!
self.my_layout.setAlignment(QtCore.Qt.AlignCenter) # Center the horizontal alignment.
self.my_layout.addWidget(self.my_button) # Add the button to the layout.
self.setLayout(self.my_layout) # Make the window use this layout.
self.resize(300, 300) # Resize the window so it's not tiny.
my_window_instance = MyWindow() # Create an instance of our window class.
my_window_instance.show() # Show it!
Not too bad, right?

How to create list in Maya using python

Expected Output
I want to create a UI in Maya 2014 which contains Layers and Camera given in the image .I don't know what widget to use for this.I tried to create and my code is given below.
import maya.cmds as cmds
window = cmds.window( title="Render", iconName='BTD',widthHeight=
(400,300),titleBar=True,minimizeButton=True,maximizeButton=True )
inner_child5 = cmds.rowColumnLayout(numberOfColumns=1,rowSpacing =
(1,5),columnOffset =(1,'left',10))
RL = cmds.text("Render Layers",width=200,
height=50,font="boldLabelFont",align='left')
inner_child_5 = cmds.rowColumnLayout(numberOfColumns=2,columnOffset =
(1,'left',10),rowOffset = (1,"bottom",30))
Layers = cmds.text("Layers",width=100,
height=10,font="boldLabelFont",align='left')
camera = cmds.text("Camera",width=200,
height=10,font="boldLabelFont",align='left')
cmds.showWindow( window )
Please tell me how to get that output and what widget to use ?
Note:
I want to know that how to create the red lined part given in the image
You might want a textScrollList widget ! (Using two of them side by side).
I can share with you the maya ui cheat sheet from Matt Murrey :
https://drive.google.com/file/d/0B2TQxp8BzPdLSW5zaWwxZkUta3M/view?usp=drivesdk

Pyside: Opening a new window with picture

I have created a UI that can be launched inside Maya. My class for this window inherits from QDialog. I want to have a button that will open a jpg into a new window, and then it closes with another button. While this other window is open I would like to still be able to interact with the main window. Is it possible to do this? How would I go about launching this new window?
You can use this code to get what you want. I tested it in Maya 2016.5 on macOS. Works fine!
import maya.cmds as cmds
def loadSecondWindow(*args):
window = cmds.window()
cmds.paneLayout()
cmds.image(image='/Users/swift/Desktop/scientist01.jpg')
cmds.showWindow(window)
def deleteSecondWindow(*args):
if (cmds.window('window2', exists=True)):
cmds.deleteUI('window2')
cmds.window(width=200)
cmds.columnLayout(adjustableColumn=True)
cmds.button(label='Window with Picture', command=loadSecondWindow)
cmds.button(label='Delete Window', command=deleteSecondWindow)
cmds.showWindow()

Detecting Close Scene or Change Scene

HI I'm working with Pyqt4 to make a UI in Maya, but I want that the UI close or refresh when the user open or change Scene.
Is there a way in python to detect this change?
scriptJob is what you need.
The following might with some customization might be helpful.
import maya.cmds as cmds
def refresher():
# the function which does the closing/refreshing
pass
cmds.scriptJob(e=["NewSceneOpened", refresher])
cmds.scriptJob(e=["SceneOpened", refresher])
cmds.scriptJob(e=["flushingScene", refresher])

PyQt MenuBar Mac OSX Snow Leopard

I am attempting to add an item to the application menu-bar of a simple PyQt example. However, the following code does not seem to alter the menu-bar at all. The only item in the menu is "Python". Below is the bulk of the code, minus imports and instantiation.
class MainWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.resize(250, 150)
self.setWindowTitle('menubar')
self.modal = False
exit = QtGui.QAction( QtGui.QIcon('images/app_icon.png'), 'Exit', self )
exit.setShortcut('Ctrl+Q')
exit.setStatusTip('Exit application')
self.connect(exit, QtCore.SIGNAL('triggered()'), QtCore.SLOT('close()'))
menubar = self.menuBar()
file = menubar.addMenu('File')
file.addAction(exit)
I've also tried creating a new QMenuBar and using the setMenuBar() method to manually swap out the menu bar.
Any glaring mistakes in the above snippet?
I know this question is old but, since I was stuck with the same problem, I found that because I was creating an action to quit the application and this action is reserved on OSX to the Application Menu, the File menu did not appear. As I created a new action on the same menu, it became available.
This worked by using the same approach for other OS's:
self.menubar = self.menuBar()
This was created inside a QMainWindow object.
Hope this helps anyone!
When using PyQt on a mac, the system will intercept certain commands contain the word 'Quit' or 'Exit' and remove them from your menubar because they exist elsewhere. if a menubar header has no items, it will not display, making it appear as if you haven't modified the menubar.
#exit = QtGui.QAction( 'Exit', self ) #this fails on my system
exit = QtGui.QAction( 'SomethingElse', self ) #this displays on my system
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(exit)
Also, calling raise_() doesn't change the menubar on my mac. I have to manually select the window (by clicking else where then reclicking), if i use raise_(), to get the correct menubar to show for my pyqt app.
Also remember that mac menubars are displayed in the system menubar not in the window like on a Windows or Linux machine. This leads us to the other solution, as suggested by Levi501 and Swdev. That is to use a non-native menu that appears in the window like so:
menubar = self.menuBar()
menubar.setNativeMenuBar(False)
As someone who uses windows and linux alot, this makes alot more sense for my projects.
I found the 'Exit' information here: http://python.6.x6.nabble.com/addAction-to-menubar-td1916296.html
I don't have PyQt installed on this machine to test this out, but I think on a Mac the QMainWindow.menuBar() function does not return the application wide menu bar.
You might try creating a menubar like:
menubar = QtGui.MenuBar()
I'm basing this on the docs for the QMainWindow.menuBar() function here:
http://doc.qt.io/qt-4.8/qmainwindow.html#menuBar
You might also check out the section labeled QMenuBar on Mac OS X on this page:
http://doc.qt.io/qt-4.8/qmenubar.html#details
Hope that helps!
Correct. On MAC OS we need to use menubar like this:
self.menubar = QtGui.QMenuBar()
And not like this:
self.menubar = QtGui.QMenuBar(MainWindow)
(without the MainWindow parameter)
I suggest the best solution is using QTDesiner to build the UI layout then using pyside-uic tool to convert to a Python class on different platform. When I used the UI layout class compiled on Windows in MAC I got this issue. Solve this issue by simply recompiling the UI layout XML on MAC with the command pyside-uic AppMain.ui -o ui_AppMain.pyp
After I compare the compiled UI layout class between MAC and Windows the only difference is that on Max OS X new QMenuBar object is created without MainWindow parameter.

Categories