Skip to main content
 首页 » 编程设计

Maya 中的 Python 文本字段

2024年10月01日9JustinYoung

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

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

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

一旦你按下回车,它就会运行:

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

我已经启动并运行了它,但我也不断收到来自 Maya 的错误消息:

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

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

谢谢!

请您参考如下方法:

此格式无效:

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)