Maya 2018 Python+QT:查询导入TextField的值

Maya 2018 Python+QT: Querying Value of Imported TextField

我一直在关注 This Tutorial 在 Maya 插件中使用来自 QT 设计器的 .UI 文件。它指出,为了在 UI 加载到 Maya 后查询 QtextEdit 字段的值,我需要执行以下操作:

So now when we load our QT Ui inside of maya we can query the text of our line edit every time we want to by using the following line of code:

pm.textField('textFieldName', query = True, text = True)

但是我似乎无法让它发挥作用。我正在加载 UI 如下:

# Load our window and put it into a variable.
ebWin = cmds.loadUI(uiFile = self.BE_UIpath)

没有问题,当我尝试 cmds.showWindow(ebWin) 时,一切正常,看起来完全符合预期。现在,当我尝试查询我命名为 'exportDirectoryTF' 的 QtextEdit 时,Maya 坚持认为它不存在。我尝试了两种不同的方法:

方法一:

# Connect Functions to the buttons.
exportDir = ebWin.textField('exportDirectoryTF', query = True, text = True)

输出:

# Error: 'unicode' object has no attribute 'textField'
# # Traceback (most recent call last):
# #   File "C:/Users/Censored/Documents/maya/2018/plug-ins/EB_pi_cmds.py", line 39, in doIt
# #     exportDir = ebWin.textField('exportDirectoryTF', query = True, text = True)
# # AttributeError: 'unicode' object has no attribute 'textField'

然后方法 B:

import maya.cmds as cmds
# Connect Functions to the buttons.
exportDir = cmds.textField('exportDirectoryTF', query = True, text = True)

哪个returns:

# RuntimeError: Object 'exportDirectoryTF' not found.
# # Traceback (most recent call last):
# #   File "C:/Users/Censored/Documents/maya/2018/plug-ins/EB_pi_cmds.py", line 39, in doIt
# #     exportDir = cmds.textField('exportDirectoryTF', query = True, text = True)
# # RuntimeError: Object 'exportDirectoryTF' not found. # 

教程中有'pm.textField('textFieldName', q = True, text = True)',我不知道 "pm" 是从哪里来的,如果它应该表示加载 UI 或 maya Python textField 命令的变量,或两者都不加载。

如果有人能在这里指出正确的方向,我将不胜感激。

从您的代码中看不到您尝试执行 textField cmd 的时间。下面的代码对我来说很好用。 test.ui 仅包含一个带有名为 "lineEdit" 的 lineEdit 字段的小部件。仅当 window 可见时,查询文本字段才有效。如果关闭 window 并尝试查询文本字段,则会收到 "object not found" 错误。

ui = "D:/temp/test.ui"
qtW = cmds.loadUI(uiFile = ui)
cmds.showWindow(qtW)
cmds.textField("lineEdit", query=True, text=True)