3dsmax不同版本 pyside qt UI 设置max窗口为父窗口的方法

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-30 14:33   11   0

3dsmax不同版本 pyside qt widget 设置 max 窗口为父窗口的方法

前言:

3dsmax 在 2014 extension 之后开始集成 Python 和 PySide,但是在版本2014 extension - 2015 中,当设置 qt UI 的父窗口为 max 主窗口的时候会报错,3dsmax2016 修复了这个bug,2017 和 2018 对 parenting qt widget to max main window 的方式都有所更新,下面来看看每个版本的具体方式。

3dsmax2014 extension - 2015:

  下面是报错的代码:(在MAXScript Listener中运行 python.ExecuteFile @"[Path]\maxPyGui.py",[Path]改为文件的所在路径)

# -*- coding: utf-8 -*-
"""
在MAXScript Listener中运行 python.ExecuteFile @"[Path]\maxPyGui.py"
[Path]改为 maxPyGui.py 所在的路径
"""
from PySide import QtGui
from PySide import shiboken
import MaxPlus

class _GCProtector(object):
    widgets = []

app = QtGui.QApplication.instance()
if not app:
    app = QtGui.QApplication([])
    
def main():
    MaxPlus.FileManager.Reset(True)
    w = QtGui.QWidget()
    w.resize(250, 100)
    w.setWindowTitle('Window')
    _GCProtector.widgets.append(w)
    
    main_layout = QtGui.QVBoxLayout()
    label = QtGui.QLineEdit()
    main_layout.addWidget(label)

    cylinder_btn = QtGui.QPushButton("test")
    main_layout.addWidget(cylinder_btn)
    w.setLayout(main_layout)
    
    # 这是会报错的方式
    maxWinHwd = MaxPlus.Core.GetWindowHandle()
    parent = shiboken.wrapInstance(long(maxWinHwd), QtGui.QWidget)
    w.setParent(parent)#报错在这里,如果你的窗口继承了QtGui.QWidget,parent = parent 也会报错,如果想正常运行,请注释这行
    
    """Max2016的修正方式
    MaxPlus.AttachQWidgetToMax(w)
    """
    
    """不太好的方式
    hwnd = w.winId()
    import ctypes
    ctypes.pythonapi.PyCObject_AsVoidPtr.restype = ctypes.c_void_p
    ctypes.pythonapi.PyCObject_AsVoidPtr.argtypes = [ctypes.py_object]
    int_hwnd = ctypes.pythonapi.PyCObject_AsVoidPtr(hwnd)
    MaxPlus.Win32_Set3dsMaxAsParentWindow(int_hwnd)
    """
    w.show()

if __name__ == '__main__':
    main()
maxPyGui.py

注意:如果运行报错SyntaxError: encoding declaration in Unicode string (maxPyGui.py, line 0),请去掉第一行的 # -*- coding: utf-8 -*-,在命令行中运行不需要指定,B

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP