Skip to main content
 首页 » 编程设计

python之用于 3dsMax 和 Modo 的 PySide 小部件

2024年10月17日7EasonJim

我正在尝试使用 PySide 和 Python 创建一个简单的 QT 应用程序,我希望它作为 3dsMax 脚本、Modo 脚本和独立应用程序运行(如果需要)。所以,我将以下文件保存在我的 D:\PyTest 中。它只是用于此测试的 QLabel。

当我单独运行它 (TestWidget.py) 时,它工作正常。当我从 Modo 启动它 (ModoStart.py) 时,它会正确启动,但如果我尝试单击 Modo 中的任何地方,它会使整个窗口崩溃。在 3dsMax 中,我收到以下错误:Traceback(最近调用最后一次): 文件“D:/PyTest\TestWidget.py”,第 13 行,位于 系统退出:-1

我有什么想法可以让它发挥作用吗?

谢谢,
尼克

TestWidget.py

import sys 
from PySide import QtGui 
 
 
def open_widget(app, parent_handle=None): 
    w = QtGui.QLabel() 
    w.setText("My Widget") 
    w.show() 
 
    if parent_handle is not None: 
        w.setParent(parent_handle) 
 
    sys.exit(app.exec_()) 
 
 
if __name__ == '__main__': 
    open_widget(QtGui.QApplication(sys.argv)) 

MaxStart.py

import sys 
 
FileDir = 'D:/PyTest' 
if FileDir not in sys.path: 
    sys.path.append(FileDir) 
 
#Rest imports 
from PySide import QtGui 
import MaxPlus 
import TestWidget 
reload(TestWidget) 
 
app = QtGui.QApplication.instance() 
parent_handle = QtGui.QWidget(MaxPlus.GetQMaxWindow()) 
TestWidget.open_widget(app, parent_handle) 

ModoStart.py

import sys 
 
FileDir = 'D:/PyTest' 
if FileDir not in sys.path: 
    sys.path.append(FileDir) 
 
#Rest imports 
from PySide import QtGui 
import TestWidget 
reload(TestWidget) 
 
app = QtGui.QApplication.instance() 
TestWidget.open_widget(app) 

更新:
我还尝试为所有三个选项 (3dsMax/Modo/Stand-alone) 使用一个文件。它似乎适用于 3dsMax 和 Stand-Alone,但在 Modo 中,如果我在 Widget 外部单击或尝试关闭它,Modo 会立即崩溃。

import sys 
import traceback 
from PySide import QtGui 
handle = None 
appMode = None 
try: 
    import MaxPlus 
    appMode = '3dsMax' 
    handle = MaxPlus.GetQMaxWindow() 
except: 
    try: 
        import lx 
        appMode = 'Modo' 
    except: 
        appMode = 'StandAlone' 
 
 
app = QtGui.QApplication.instance() 
if not app: 
    app = QtGui.QApplication([]) 
 
 
def main(): 
    w = QtGui.QLabel(handle) 
    w.setText("My Widget") 
    w.resize(250, 100) 
    w.setWindowTitle('PySide Qt Window') 
    w.show() 
 
    try: 
        sys.exit(app.exec_()) 
    except Exception, err: 
        traceback.print_exc() 
        pass 
 
main() 

请您参考如下方法:

好的,在 The Foundry 的帮助下,我有了一个可用的版本。 他们给了我这个非常有用的链接 http://sdk.luxology.com/wiki/CustomView

3dsMax.py

from PySide import QtGui 
import MaxPlus 
import sys 
ui_dir = r'D:/PyTest/SubFolder/' 
if not ui_dir in sys.path:sys.path.insert(0,ui_dir) 
import ToolboxUI 
reload(ToolboxUI) 
 
 
parent = MaxPlus.GetQMaxWindow() 
 
w = QtGui.QWidget(parent) 
ToolboxUI.create_layout(w, '3dsMax') 
w.show() 

Modo.py

import lx 
import lxifc 
import sys 
ui_dir = r'D:/PyTest/SubFolder/' 
if not ui_dir in sys.path:sys.path.insert(0,ui_dir) 
import ToolboxUI 
reload(ToolboxUI) 
 
 
class MyButtonTest(lxifc.CustomView): 
    def customview_Init(self, pane): 
        if pane is None: 
            return False 
 
        custom_pane = lx.object.CustomPane(pane) 
 
        if custom_pane.test() is False: 
            return False 
 
        # get the parent object 
        my_parent = custom_pane.GetParent() 
 
        # convert to PySide QWidget 
        p = lx.getQWidget(my_parent) 
 
        # Check that it succeeds 
        if p is not None: 
            ToolboxUI.create_layout(p, 'Modo') 
            return True 
 
        return False 
 
try: 
    lx.bless(MyButtonTest, "My Button Test") 
except: 
    pass 

StandAlone.py

from PySide import QtGui 
import sys 
import ToolboxUI 
 
app = QtGui.QApplication([]) 
 
w = QtGui.QWidget() 
ToolboxUI.create_layout(w, 'StandAlone') 
w.show() 
 
sys.exit(app.exec_()) 

ToolboxUI.py

from PySide import QtGui 
appMode = None 
 
 
def on_clicked(side): 
    print "Hello from the " + side + " side: " + appMode 
 
 
def left_click(): 
    on_clicked("left") 
 
 
def center_click(): 
    on_clicked("center") 
 
 
def right_click(): 
    on_clicked("right") 
 
 
def create_layout(my_window, am): 
    global appMode 
    appMode = am 
 
    buttonLayout = QtGui.QHBoxLayout() 
    buttonLayout.setSpacing(0) 
 
    leftButton = QtGui.QPushButton("Left") 
    leftButton.setProperty("group", "left") 
    leftButton.clicked.connect(left_click) 
 
    rightButton = QtGui.QPushButton("Right") 
    rightButton.setProperty("group", "right") 
    rightButton.clicked.connect(right_click) 
 
    centerButton = QtGui.QPushButton("Center") 
    centerButton.setProperty("group", "center") 
    centerButton.clicked.connect(center_click) 
 
    buttonLayout.addWidget(leftButton) 
    buttonLayout.addWidget(centerButton) 
    buttonLayout.addWidget(rightButton) 
 
    my_window.setLayout(buttonLayout)