demoPySideToolBarQWidget.py

demoPySideToolBarQWidget.py
1 
2 '''
3  Demonstrates how to create a QWidget with PySide2 and attach it to the 3dsmax main window.
4 '''
5 
6 import os
7 
8 import MaxPlus
9 from PySide2 import QtCore
10 from PySide2 import QtGui
11 from PySide2 import QtWidgets
12 
13 class _GCProtector(object):
14  widgets = []
15 
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", 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 
45  # QAction reused by both dockable widgets.
46  cylinderIconPath = os.path.dirname(os.path.realpath(__file__)) + "\\demoPySideToolBarCylinderIcon_48.png"
47  cylinderIcon = QtGui.QIcon(cylinderIconPath)
48  createCylAction = QtWidgets.QAction(cylinderIcon, u"Create Cylinder", mainWindow)
49  createCylAction.triggered.connect(make_cylinder)
50 
51 
52  # QDockWidget construction and placement over the main window
53  dockWidget = QtWidgets.QDockWidget(mainWindow)
54  _GCProtector.widgets.append(dockWidget) # Required to avoid destruction of widget after script has completed execution
55 
56  dockWidget.setObjectName("Creators") # Required for position persistence
57  dockWidget.setWindowTitle("Creators") # Required to see dock widget name in toolbar customize popup
58  dockToolButton = QtWidgets.QToolButton()
59  dockToolButton.setAutoRaise(True)
60  dockToolButton.setDefaultAction(createCylAction)
61  dockToolButton.setToolButtonStyle(QtCore.Qt.ToolButtonTextOnly)
62  dockWidget.setWidget(dockToolButton)
63 
64  mainWindow.addDockWidget(QtCore.Qt.LeftDockWidgetArea, dockWidget)
65  dockWidget.setFloating(True)
66  dockWidget.show()
67 
68 
69  # QToolBar construction and attachement to main window
70  toolBarWidget = QtWidgets.QToolBar(mainWindow)
71  _GCProtector.widgets.append(dockWidget) # Required to avoid destruction of widget afetr script has completed execution
72 
73  toolBarWidget.setObjectName("Creators TB") # Required for position persistence
74  toolBarWidget.setWindowTitle("Creators TB") # Required to see toolbar name in toolbar customize popup
75  toolBarWidget.setFloatable(True)
76  toolBarWidget.addAction(createCylAction)
77 
78  mainWindow.addToolBar(QtCore.Qt.BottomToolBarArea, toolBarWidget)
79  toolBarWidget.show()
80 
81  toolBarPos = getPosToDockToolBar(dockWidget)
82  makeToolBarFloating(toolBarWidget, toolBarPos)
83 
84 
85 app = QtWidgets.qApp
86 if not app:
87  app = QtWidgets.QApplication([])
88 
89 if __name__ == '__main__':
90  main()