首页 > 解决方案 > 您如何查询文本字段内的对象,以对其进行处理?

问题描述

我想知道如何查询输入到文本字段组中的选择,所以我可以用它做点什么。我创建了一个窗口来翻译我在文本字段中加载的对象。错误是 cont 没有定义。

import maya.cmds as cmds
import maya.mel as ml

def set_selected_name (text_field):
    cont = cmds.ls (selection = True)
    text_field = cmds.textFieldButtonGrp (text_field, edit = True,
                               text = ''.join (cont),
                               buttonLabel = '<<<<',
                               backgroundColor = [0.5098039215686274,
                                                  0.5098039215686274,
                                                  0.5098039215686274])
    return text_field

def translate_x(cont):
    cmds.setAttr( cont[0] + '.translateX', 10) 
       
def translate_y():
    cmds.setAttr( cont[0] + '.translateY', 10) 
       
def translate_z(*Args):        
    cmds.setAttr( cont[0] + '.translateZ', 10)




if cmds.window ('window1', q = 1, ex = 1):
    cmds.deleteUI ('window1')

cmds.window ('window1',
             title = 'Translate Attr',
             sizeable = 0,
             resizeToFitChildren = True,
             menuBar = 1)

cmds.rowLayout (numberOfColumns = 3)


cmds.separator (style = 'double',
                height = 6)

cmds.rowLayout (parent = 'window1',
                numberOfColumns = 4)

ddd = cmds.textFieldButtonGrp (editable = False,
                               text = 'Obj',
                               backgroundColor = [0.029495689326314183,
                                                  0.5488975356679637,
                                                  0.5488975356679637],
                               buttonLabel = '<<<<')

cmds.textFieldButtonGrp (ddd, edit = True,
                         buttonCommand = 'set_selected_name (ddd)')


cmds.separator (style = 'double',
                height = 6)

cmds.rowLayout (parent = 'window1',
                numberOfColumns = 6)

cmds.separator (style = 'double',
                height = 6)

cmds.button (command = 'translate_y()',
             backgroundColor = [1.0,
                                0.7300068665598535,
                                0.7300068665598535],
             label = 'Translate Y')

cmds.separator (style = 'double',
                height = 6)

cmds.button (command = 'translate_x(cont)',
             backgroundColor = [1.0,
                                0.9733272297245746,
                                0.7333333333333333],
             label = 'Translate X')

cmds.separator (style = 'double',
                height = 6)

cmds.button (command = 'translate_z()',
             backgroundColor = [0.7333333333333333,
                                1.0,
                                0.7600061036087586],
             label = 'Translate Z')

cmds.columnLayout (parent = 'window1')

cmds.separator (style = 'double',
                height = 3)

cmds.showWindow ('window1')

# ~ BABLAAM ~

创建您喜欢的任何对象,加载到文本字段中,然后尝试使用按钮进行翻译。

标签: pythonmaya

解决方案


您的代码中有几个问题。

  1. 在翻译命令中,您总是使用 cont[0]。cont 仅在函数中使用,set_selected_name()并且是局部变量,这意味着函数完成后立即将其删除。
  2. 您可以在按钮命令中使用字符串作为命令,但这仅适用于静态值。您应该使用 lambdas 来使用带参数的函数。

cont问题可以通过使用全局变量来解决,但不应该,因为全局变量是万恶之源。一种更优雅的方法是将您的 UI 包含在一个 python 类中并使用实例变量来获取选择。


推荐阅读