【问题标题】:Update or 'Refresh' a Text Field in Pymel / Python在 Pymel / Python 中更新或“刷新”文本字段
【发布时间】:2023-03-08 15:15:01
【问题描述】:

目前在 Maya 中编写一个简单的脚本来获取相机信息并将其呈现在 GUI 中。该脚本打印所选相机的相机数据没问题,但是我似乎无法让它在按下按钮时用数据更新文本字段。我确定它只是一个回调,但我不知道该怎么做。

代码如下:

from pymel.core import * 
import pymel.core as pm

camFl = 0
camAv = 0

win = window(title="Camera Information", w=300, h=100)
layout = columnLayout()
txtFl = text("Field Of View:"),textField(ed=0,tx=camFl)
pm.separator( height=10, style='double' )
txtAv = text("F-Stop:"),textField(ed=0,tx=camAv)
pm.separator( height=10, style='double' )
btn = button(label="Fetch Data", parent=layout)

def fetchAttr(*args):

    camSel = ls(sl=True)
    camAttr = camSel[0]
    cam = general.PyNode(camAttr)
    camFl = cam.fl.get()
    camAv = cam.fs.get()
    print "Camera Focal Length: " + str(camFl) 
    print "Camera F-Stop: " + str(camAv)

btn.setCommand(fetchAttr)
win.show()

谢谢!

【问题讨论】:

    标签: python scripting maya pymel


    【解决方案1】:

    几件事:

    1) 您将 txtAVtextFl 分配给 both 一个 textField 和一个文本对象,因为这些行上有逗号。所以你不能设置属性,你在一个变量中有两个对象,而不仅仅是 pymel 句柄。

    2) 您依赖用户来选择形状,因此如果他们在大纲视图中选择相机节点,代码将会往南走。

    否则基础是健全的。这是一个工作版本:

    from pymel.core import * 
    import pymel.core as pm
    
    
    win = window(title="Camera Information", w=300, h=100)
    layout = columnLayout()
    text("Field of View")
    txtFl = textField()
    pm.separator( height=10, style='double' )
    text("F-Stop")
    txtAv = textField()
    pm.separator( height=10, style='double' )
    btn = button(label="Fetch Data", parent=layout)
    
    
    def fetchAttr(*args):
    
        camSel = listRelatives(ls(sl=True), ad=True)
        camSel = ls(camSel, type='camera')
        camAttr = camSel[0]
        cam = general.PyNode(camAttr)
        txtAv.setText(cam.fs.get())
        txtFl.setText(cam.fl.get())
    
    btn.setCommand(fetchAttr)
    
    
    win.show()
    

    【讨论】:

    • 太棒了,非常感谢!写这篇文章时,我对 pymel 很陌生(仍然很新)。这完全有道理!不知道为什么我会按照我对文本字段的方式去做。我将添加脚本以允许他们很快以任何方式选择相机,为这个项目一次处理小块!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-03
    • 2019-05-19
    • 1970-01-01
    • 2015-03-03
    • 1970-01-01
    • 1970-01-01
    • 2016-03-11
    相关资源
    最近更新 更多