3dsmax不同版本 pyside qt UI 设置max窗口为父窗口的方法
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 -*-,在命令行中运行不需要指定,下面的代码例子也一样。
很多人建议不要 parenting qt widget to max main window ,不过还是有人尝试了很多方法,autodesk 官方 也给出了 pyqt4 的方式,链接:https://area.autodesk.com/blogs/chris/pyqt-ui-in-3ds-max-2014-extension,我使用的是pyside,所以没有验证过,也有人把这种方式改为 pyside ,有兴趣的可以试试。
一种比较理想的代替方式:
下面是在:https://github.com/alfalfasprossen/qtinwin 上找到的代码,下载后有以下文件:
在这里只关注 maxparenting.py 和 maxparenting_example.py,在MAXScript Listener中运行 python.ExecuteFile @"maxparenting_example.py",这是以owner的方式来实现的,具体描述请看代码里面的注释。
下面附上代码:
"""This is a quite well working experiment of setting the **owner**
(not the **parent**) of the qt widget to be the 3dsMax main window. Effectively the qt widget will behave like a natively spawned window,
with correct z-order behaviour concerning its sibling windows.
""" import ctypes from PySide import QtGui
from PySide import QtCore
import MaxPlus GWL_HWNDPARENT = -8
SetWindowLongPtr = ctypes.windll.user32.SetWindowLongPtrW class FocusFilter(QtCore.QObject):
def eventFilter(self, obj, event):
# TODO: fix focus filter not releasing on defocus
MaxPlus.CUI.DisableAccelerators()
return False class MaxWidget(QtGui.QWidget):
def __init__(self, title):
super(MaxWidget, self).__init__(None)
self.parent_hwnd = MaxPlus.Win32.GetMAXHWnd()
self.hwnd = self.get_hwnd()
self._parent_to_main_window()
self.show()
app = QtGui.QApplication.instance()
self._focus_filter = FocusFilter()
self.event_filter = app.installEventFilter(self._focus_filter) def get_hwnd(self):
"""Get the HWND window handle from this QtWidget."""
ctypes.pythonapi.PyCObject_AsVoidPtr.restype = ctypes.c_void_p
ctypes.pythonapi.PyCObject_AsVoidPtr.argtypes = [ctypes.py_object]
wdgt_ptr = ctypes.pythonapi.PyCObject_AsVoidPtr(self.winId())
return wdgt_ptr def _parent_to_main_window(self):
""" Parent the widget to the 3dsMax main window. Technically this is NOT setting the **parent** of the window,
but the **owner**.
There is a huge difference, that is hardly documented in the
win32 API.
https://msdn.microsoft.com/en-us/library/windows/desktop/ms644898(v=vs.85).aspx # noqa
Setting the parent would make this a child or mdi-
child window. Setting the owner, makes this a top-level,
overlapped window that is controlled by the main window, but
not confined to its client area.
http://stackoverflow.com/questions/133122/
"""
SetWindowLongPtr(self.hwnd, GWL_HWNDPARENT, self.parent_hwnd) def closeEvent(self, event):
app = QtGui.QApplication.instance()
app.removeEventFilter(self.event_filter)
event.accept()
maxparenting.py
from PySide import QtGui import maxparenting
reload(maxparenting) class ExampleWidget(maxparenting.MaxWidget):
"""This is a test that ui interaction works correctly with a more
or less complex ui.
"""
def __init__(self):
super(ExampleWidget, self).__init__("Example Widget")
self.build_ui()
self.connect_ui() def build_ui(self):
self.setLayout(QtGui.QVBoxLayout())
self.label = QtGui.QLabel("some label")
self.btn = QtGui.QPushButton("button")
self.lineedit = QtGui.QLineEdit()
self.textedit = QtGui.QTextEdit() self.grp = QtGui.QGroupBox("group box grid layout")
self.grp.setLayout(QtGui.QGridLayout())
self.chkbx_1 = QtGui.QCheckBox("chkbx_1")
self.chkbx_2 = QtGui.QCheckBox("chkbx_2l")
self.chkbx_2.setDisabled(True)
self.chkbx_3 = QtGui.QCheckBox("chkbx_2r")
self.chkbx_4 = QtGui.QCheckBox("chkbx_3")
self.chkbx_5 = QtGui.QCheckBox("chkbx_4")
self.grp.layout().addWidget(self.chkbx_1, 0, 0)
self.grp.layout().addWidget(self.chkbx_2, 1, 0)
self.grp.layout().addWidget(self.chkbx_3, 1, 1)
self.grp.layout().addWidget(self.chkbx_4, 2, 0)
self.grp.layout().addWidget(self.chkbx_5, 3, 0)
self.grp.layout().setColumnStretch(2,1) self.lrbox = QtGui.QHBoxLayout()
self.lrbox.addWidget(self.textedit)
self.lrbox.addWidget(self.grp) self.layout().addWidget(self.label)
self.layout().addWidget(self.btn)
self.layout().addWidget(self.lineedit)
self.layout().addLayout(self.lrbox) def connect_ui(self):
self.btn.clicked.connect(self.on_btn_clicked) def on_btn_clicked(self):
print "btn clicked" global qtwdgt
qtwdgt = ExampleWidget()
maxparenting_example.py
其它.py文件有兴趣的可以自己尝试。
3dsmax2016:
在2016中,终于做出了修正,在模块 MaxPlus 中增加了AttachQWidgetToMax()(注意,是 max 2016 PS3版本,如果没有安装补丁,是没有这个方法的),不过这只是一种简单的指定方式,我们还是没办法获得 Max main window 的QT对象,没办法以继承 QtGui.QWidget 来指定parent,but, it's not a bid deal。
# -*- 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
3dsmax2017:
所以,在2017中,为了解决2016的问题,在 MaxPlus 中增加了GetQMaxWindow(),这个方法直接返回 Max main window 的 PySide.QtGui.QWidget object:
"""
在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(u"我们")
main_layout.addWidget(cylinder_btn)
w.setLayout(main_layout) #Max2017的改进方式
parent = MaxPlus.GetQMaxWindow()
w.setParent(parent)#上面返回的parent直接是PySide.QtGui.QWidget object,可以不通过wrapping,直接设置为父窗口 """这是会报错的方式
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
3dsmax2018 (PySide2):
在2018中,去掉了 MaxPlus 中的GetQMaxWindow(),所以没办法直接获得 Max main window 的 PySide.QtGui.QWidget object,但是增加了QtHelpers 类,里面有静态方法GetQmaxMainWindow() 获得 maxMainWindow 的指针,然后我们可以通过传统方式来进行转换:
maxWinHwd = MaxPlus.QtHelpers.GetQmaxMainWindow()
parent = shiboken2.wrapInstance(long(maxWinHwd), QtGui.QWidget)
例子代码在这里就不提供了,只是要注意的是2018开始,集成的是PySide2,所以要用shiboken2,而且 shiboken2 是在 PySide2 下的一个模块,所以导入方式为(和Maya的不一样):
from PySide2 import shiboken2
3dsmax不同版本 pyside qt UI 设置max窗口为父窗口的方法的更多相关文章
- Qt 代码: 子窗口调用父窗口(其实就是用指针直接访问)
之前的 Qt 编程大多只涉及简单的多窗口,并未染指窗口间的传值交互,想来还是“涉世未深”,对 Qt 的理解.应用还需殷勤努力. 这次的问题是这样的,我想要实现一个类似QQ.阿里旺旺的聊天客户端,在弹出 ...
- Qt之设置QWidget背景色(QStyleOption->drawPrimitive(QStyle::PE_Widget)方法比较有趣)
QWidget是所有用户界面对象的基类,这意味着可以用同样的方法为其它子类控件改变背景颜色. Qt中窗口背景的设置,下面介绍三种方法. 1.使用QPalette2.使用Style Sheet3.绘图事 ...
- Qt中,当QDockWidget的父窗口是一个不可以拖动的QTabWidget的时候实现拖动的方法
之前在做有关QDockWidget的内容时候遇到了瓶颈,那就是窗口弹出来之后拖动不了,也不可以放大和缩小,若是弹出来之后设置成了window的flags,也不可以拖动,而且也不是需要的效果. 1.弹出 ...
- Qt的子窗口和父窗口阻塞问题
在图形界面中,软件设计者通常需要将活跃窗口限制为一个.在某个窗口活跃时,它的父窗口被它挡住或者挡住一部分,这时候用鼠标去点击父窗口是没有作用的.问题的关键在于将子窗口设置模态: void MainWi ...
- PyQt(Python+Qt)学习随笔:在父窗口中弹出子窗口无法显现的问题
在学习和测试PyQt相关部件功能的时候,老猿经常是不同的窗口新建一个类,再新建一个Application来使用这个窗口类进行测试. 为了减少应用框架代码的重复开发,老猿决定采用主窗口叠加测试窗口的模式 ...
- QT 常用设置
博文都写在了云笔记里面了,见谅,不想维护两个版本. QT 常用设置
- Qt - QDialog,QWidget实现模态及非模态(模态Widget不能有父窗口,如果设置无边框就不能阻塞父窗口,但是可以强行设置指定Qt::Dialog,还可以setAttribute(Qt::WA_ShowModal),很多讲究)good
在Qt中QDialog为“窗口”,而QWidget为“部件”,首先还是了解下<Qt 窗口与部件的概念>. 对于 QDialog 的模态及非模态是直接可以实现的,很多课本中都会提到,此处总结 ...
- Qt::WindowFlags枚举类型(Qt::Widget是独立窗口和子窗口两用的,Qt::Window会有标题栏)
Qt::Widget : QWidget构造函数的默认值,如新的窗口部件没有父窗口部件,则它是一个独立的窗口,否则就是一个子窗口部件. Qt::Window : 无论是否有父窗口部件,新窗口部件都是一 ...
- Inside Qt Series (全集,共十六篇,不同版本的Qt有不同的实现)
Inside Qt 系列 QObject这个 class 是 QT 对象模型的核心,绝大部分的 QT 类都是从这个类继承而来.这个模型的中心特征就是一个叫做信号和槽(signaland slot)的机 ...
随机推荐
- windows 2012执行powershell脚本报错
使用powershell运行脚本报错:进行数字签名.无法在当前系统上运行该脚本.有关运行脚本和设置执行策略的详细信息 修复方法:powershell "Set-ExecutionPolicy ...
- Sybase·调用存储过程并返回结果
最近项目要用Sybase数据库实现分页,第一次使用Sybase数据库,也是第一次使用他的存储过程.2个多小时才调用成功,在此记录: 项目架构:SSM 1.Sybase本身不支持分页操作,需要写存储过程 ...
- SQL语句的行列转换
[一]行转列 1,查询原始的数据 /***这次练习的主题,行转列,列转行***/select * from Scores 2,得到姓名,通过group by select Student as '姓名 ...
- 找到 Confluence 6 的日志和配置文件
找到 Confluence 的日志文件 这部分内容对 Confluence 的默认日志表现进行描述并且假设你没有对 Confluence 的默认日志配置进行修改.为了统一在不同平台中的日志输出,Con ...
- 关于ios进入后台界面后 播放声音解决方案
1 最近我在做环信视频通话时,遇到了一个新功能就是APP在后台的时候能对方能视频或者音频过来的时候 能够播放声音 根据查询相关资料得到如下解决办法 NSError *error; AVAudioSes ...
- laravel 路由模型绑定
我们在使用路由的时候一个很常见的使用场景就是根据资源 ID 查询资源信息: Route::get('task/{id}', function ($id) { $task = \App\Models\T ...
- C语言访问一个链接
示例代码1: # include <Windows.h> int main(){ system("start http://""www.baidu.com&q ...
- Jumpserver里常用的sudo权限控制模板
ALL,!/bin/bash,!/bin/tcsh,!/bin/su,!/usr/bin/passwd,!/usr/bin/passwd root,!/bin/vim /etc/sudoers,!/u ...
- linux里面的命令:./和. /(这里有空格)的区别
/ ( 直接/ ) 直接 / 表示: 根目录. ./ ( 点号紧跟/ ) 点号紧跟/ 表示: 当前目录(相对路径的写法). . / ( 点号 空格 / ) 有空格的点号等同于source命令,表示在 ...
- 常见的爬虫分析库(2)-xpath语法
xpath简介 1.xpath使用路径表达式在xml和html中进行导航 2.xpath包含标准函数库 3.xpath是一个w3c的标准 xpath节点关系 1.父节点 2.子节点 3.同胞节点 4. ...