見出し画像

3dsMaxでPySideのMainWindowとMenu

3dsMaxでPySideを使ったMainWindowとMenuをやってみる

MeinWindowの作成

Qt Designerを起動する
Qt DesignerはPythonでPySideをインストールと、PythonホームのScriptsフォルダにある

Qt Designer

起動して最初のダイアログでMainWindowを選択する

MainWindow

このままの状態で保存する
ファイル名は"ui_HelloWindow.ui"としておく

Visual Studio Codeを起動する
TestWindow.pyとして以下のコードを記述する

import os
import qtmax
from PySide2.QtWidgets import QMainWindow
from PySide2.QtCore import QFile
from PySide2.QtUiTools import QUiLoader

class TestDialog(QMainWindow):
    def __init__(self, parent=qtmax.GetQMaxMainWindow()):
        QMainWindow.__init__(self, parent)
        loader = QUiLoader()
        ui_file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), '.\\ui\\ui_HelloWindow.ui')
        ui_file = QFile(ui_file_path)
        ui_file.open(QFile.ReadOnly)
        self.ui = loader.load(ui_file, self)
        self.setCentralWidget(self.ui)
#        self.resize(800,600)
        ui_file.close()

def main():
    dlg = TestDialog()
    dlg.show()


if __name__ == '__main__':
    main()

Qt Designerではウィンドウサイズを800x600としてるが、3dsMaxで起動すると以下のような小さなウィンドウになる
なぜかウィンドウサイズは無視されている

ウィンドウサイズなし

なので、明示的にウィンドウサイズを記述すると、Qt Designerで設定したサイズと同じになる

        self.resize(800,600)
ウィンドウサイズを指定

ウィンドウタイトルを設定しても"3dsmax"なので以下を記述する

       self.setWindowTitle("HellowWindow")

Menuの作成

Qt DesignerでMenuを作成する

Menuの作成

MenuBarの部分に"Type Here"とあるので、そこをクリックして名前をつける
そうするとQMenuが作成される
さらに、記述したQMenuをクリックして、"Type Here"に名前をつける
そうするとQActionが作成される
今回は、QMenuを"Create"、QActionを"TeaPot"にした

メニューで"TeaPot"を選択するとTeaPotが作成されるようにする

import os
import qtmax
from PySide2.QtWidgets import QMainWindow
from PySide2.QtCore import QFile
from PySide2.QtWidgets import QMenuBar
from PySide2.QtWidgets import QMenu
from PySide2.QtWidgets import QAction
from PySide2.QtUiTools import QUiLoader
from pymxs import runtime as rt

class TestWindow(QMainWindow):
    def __init__(self, parent=qtmax.GetQMaxMainWindow()):
        QMainWindow.__init__(self, parent)
        loader = QUiLoader()
        ui_file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), '.\\ui\\ui_HelloWindow.ui')
        ui_file = QFile(ui_file_path)
        ui_file.open(QFile.ReadOnly)
        self.ui = loader.load(ui_file, self)
        self.setCentralWidget(self.ui)
        self.setWindowTitle("HellowWindow")
        self.resize(800,600)
        ui_file.close()

        # Set Menu Bar
        teapotAction = self.ui.findChild(QAction, 'actionTeaPot')
        teapotAction.triggered.connect(self.makeTeapot)
        createMenu = self.ui.findChild(QMenu, 'menuCreate')
        createMenu.addAction(teapotAction)
        menu = self.ui.findChild(QMenuBar, 'menubar')
        menu.addMenu(createMenu)
        self.setMenuBar(menu)

    def makeTeapot(self):
        rt.teapot()
        rt.redrawViews()

def main():
    dlg = TestWindow()
    dlg.show()


if __name__ == '__main__':
    main()

このコードを3dsMaxで起動する

MenuありMainWindow

Create>TeaPot
でTeaPotが作成される

TeaPot

次回はステータスバーをやってみる

この記事が気に入ったらサポートをしてみませんか?