Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
77 views
in Technique[技术] by (71.8m points)

python - How do you query an object inside of a text field, to do something with it?

I would like to know how to query a selection entered into a text field group, so I can do something with it. I have created a window to just translate an object that I loaded in the text field. The error is that cont is not defined.

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 ~

Create any object you like, loaded into the text field and then try to translate with buttons.

question from:https://stackoverflow.com/questions/65842288/how-do-you-query-an-object-inside-of-a-text-field-to-do-something-with-it

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have several problems in your code.

  1. In the translate commands you always use cont[0]. cont is only used in the function set_selected_name() and is a local variable what means it is deleted as soon as the function is completed.
  2. You can use a string as command in the button command, but this only works with static values. You should use lambdas to use functions with arguments.

The cont Problem can be solved by using a global variable, but it shouldn't since global variables are the source of all evil. A much more elegant way would be to enclose you UI in one python class and use instance variables to get the selection.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...