demoPySideToolBarQWidget.py

demoPySideToolBarQWidget.py
1 
2 '''
3  Demonstrates how to create a QWidget with PySide2 and attach it to the 3dsmax main window.
4  Creates two types of dockable widgets, a QDockWidget and a QToolbar
5  Note: Because this script uses __file__ to locate the image for the toolbar button icon,
6  it must be run using "Scripting > Run Script"
7 '''
8 
9 import os
10 import ctypes
11 
12 import MaxPlus
13 from PySide2 import QtCore
14 from PySide2 import QtGui
15 from PySide2 import QtWidgets
16 
17 def getPosToDockToolBar(dockWidget):
18  spaceBetweenWidgets = 20 # Arbritrary hard coded value
19  dockWidgetRect = dockWidget.geometry()
20  xPos = dockWidgetRect.x()
21  yPos = dockWidgetRect.bottom() + spaceBetweenWidgets
22  return QtCore.QPoint(xPos, yPos)
23 
24 def makeToolBarFloating(toolBar, pos):
25  toolBar.setWindowFlags(QtCore.Qt.Tool | QtCore.Qt.FramelessWindowHint | QtCore.Qt.X11BypassWindowManagerHint)
26  toolBar.move(pos)
27  toolBar.adjustSize()
28  toolBar.show()
29  QtCore.QMetaObject.invokeMethod( toolBar, "topLevelChanged", QtCore.Qt.DirectConnection, QtCore.QGenericArgument("bool", ctypes.c_void_p(True)) );
30 
31 def make_cylinder():
32  obj = MaxPlus.Factory.CreateGeomObject(MaxPlus.ClassIds.Cylinder)
33  obj.ParameterBlock.Radius.Value = 10.0
34  obj.ParameterBlock.Height.Value = 30.0
35  node = MaxPlus.Factory.CreateNode(obj)
38  return
39 
40 def main():
42  mainWindow = MaxPlus.GetQMaxMainWindow()
43 
44  # QAction reused by both dockable widgets.
45  cylinderIconPath = os.path.dirname(os.path.realpath(__file__)) + "\\demoPySideToolBarCylinderIcon_48.png"
46  cylinderIcon = QtGui.QIcon(cylinderIconPath)
47  createCylAction = QtWidgets.QAction(cylinderIcon, u"Create Cylinder", mainWindow)
48  createCylAction.triggered.connect(make_cylinder)
49 
50 
51  # QDockWidget construction and placement over the main window
52  dockWidget = QtWidgets.QDockWidget(mainWindow)
53 
54  dockWidget.setObjectName("Creators") # Required for position persistence
55  dockWidget.setWindowTitle("Creators") # Required to see dock widget name in toolbar customize popup
56  dockToolButton = QtWidgets.QToolButton()
57  dockToolButton.setAutoRaise(True)
58  dockToolButton.setDefaultAction(createCylAction)
59  dockToolButton.setToolButtonStyle(QtCore.Qt.ToolButtonTextOnly)
60  dockWidget.setWidget(dockToolButton)
61 
62  mainWindow.addDockWidget(QtCore.Qt.LeftDockWidgetArea, dockWidget)
63  dockWidget.setFloating(True)
64  dockWidget.show()
65 
66 
67  # QToolBar construction and attachement to main window
68  toolBarWidget = QtWidgets.QToolBar(mainWindow)
69 
70  toolBarWidget.setObjectName("Creators TB") # Required for position persistence
71  toolBarWidget.setWindowTitle("Creators TB") # Required to see toolbar name in toolbar customize popup
72  toolBarWidget.setFloatable(True)
73  toolBarWidget.addAction(createCylAction)
74 
75  mainWindow.addToolBar(QtCore.Qt.BottomToolBarArea, toolBarWidget)
76  toolBarWidget.show()
77 
78  toolBarPos = getPosToDockToolBar(dockWidget)
79  makeToolBarFloating(toolBarWidget, toolBarPos)
80 
81 
82 if __name__ == '__main__':
83  main()