Python Maya 中的文本字段

Python Text Field in Maya

我想知道如何才能将文本字段中的文本传递到 Maya 中相机的焦距属性中?

到目前为止我一直在研究它,我设置的方法是当在文本字段中按下回车键时命令将是 运行,从那里找到选定的相机并尝试查询文本框内的文本。

cmds.textField("focusdistance", parent="Extra", enterCommand=focusdistance)

一旦您按下回车键,它将 运行:

def focusdistance(self):
    fd = cmds.ls(sl=True)[0]
    cmds.setAttr('query="focusdistacne.tx"')

我已经搞定了 运行ning,但我也不断收到来自 Maya 的错误消息:

# Error: RuntimeError: file <maya console> line 85: setAttr: Invalid object name: query="focusdistacne.tx" # 

所以要简化。我有一个可编辑的文本字段,当输入时可以 运行 命令。当按下回车键时,我希望将文本字段中输入的数字传递到 Maya 中选定相机的焦距属性中。

谢谢!

focusdistance 函数中有一个拼写错误:query='focusdistacne'

此格式无效:

cmds.setAttr('query="focusdistacne.tx"')

默认的 Maya 相机没有 focusdistance 属性——有 centerOfInterest 是相机围绕的目标,focalLength 影响 FOV。假设您正在使用某种特殊相机,我将在下面保留 focusdistance,但您应该检查 maya docs 以确保您的设置正确

您需要保存您创建的文本字段的名称,以便 focusdistance 函数可以向它提问。您还需要修复 setAttr。我认为您正在寻找这样的东西:

# in the part of the class where you create the textfield:

self.focal_dist_field = cmds.textField("focusdistance", parent="Extra", enterCommand=self.focusdistance)

并且,当您获得 textField 时,您可能必须将其内容转换为数字才能将它们设置在数字属性上(除非您的 focusDistance 属性是字符串? )

def focusdistance(self):
   target = cmds.ls(sl=True)[0]
   # get the text, or a '0' if there's nothing entered
   value = cmds.textField(self. focal_dist_field, q=True, text=True) or "0"

   # convert the text to a number if possible
   # in practice you probably want a FloatField control
   # instead of a TextField so you can skip this
   try:   
       numeric_value = float(value)
   except ValueError: 
       numeric_value = 0
   cmds.setAttr(target + ".focusdistance", numeric_value)