0

I saw that it is possible to print the data that has been written in the user interface window, however, when I searched in the internet, none of the options were like that, to retrieve the data into a variable.

This is a very simple window code - '

def printTxtField ( fieldID ):

    print cmds.textField( fieldID, query=True, text=True)

winID = 'kevsUI'


if cmds.window(winID, exists=True):

    cmds.deleteUI(winID)


cmds.window(winID)

cmds.columnLayout()


whatUSay = cmds.textField()
cmds.button(label='Confirm', command='printTxtField(whatUSay)')

cmds.showWindow()

'

I want to retrieve the data from the text field into a variable, once the confirm button is pressed.

Up in the cmds.button line, you can see in the command - 'print TxtField'. I know that if there is an option to print what was written in the text field, so there must be an option to put it in a variable instead. However, I didn't find it.Does anybody knows how to do it?

Sorry for the prev. question.

ErezProductions
  • 41
  • 3
  • 11
  • I think your question is not clear. Personally, I cannot really understand what you want to do !!! :( – mlwn Jul 23 '15 at 09:31
  • As @mlwn, your question is not clear at all (such as your previous question [here](http://stackoverflow.com/questions/31552388/how-to-add-a-color-to-an-object-in-autodesk-maya-2016-in-python-scripts), where some comment were posted to ask some clarifications). If you want a quick and good answer, you NEED to clearly explain what you want. Concerning your question, what is the data (string, int, images, video of a cat...)? What is the UI window (I only see a button here)? What have you tried so far? Can you provide a runnable and commented piece of code ? Can you provide screenshots? – DrHaze Jul 23 '15 at 10:11

3 Answers3

1

You have to use partial module to pass variable through button command (or lambda function)

from functools import partial

Here is the same question : maya python + Pass variable on button press

Community
  • 1
  • 1
DrWeeny
  • 2,487
  • 1
  • 14
  • 17
1

From Python doc:

print evaluates each expression in turn and writes the resulting object to standard output. If an object is not a string, it is first converted to a string using the rules for string conversions. The (resulting or original) string is then written.


To sum up in a few words:

The print statement expects a expression after writing it. This can be a string, int, object...

In your case:

What you are printing in your printTxtField funtion is the return value of a function (cmds.textField( fieldID, query=True, text=True)).

When writing this :

cmds.textField( fieldID, query=True, text=True) you are telling Maya to:

  1. Find the textField called fieldID's value
  2. Do a query on it (the query flag is set to True
  3. Query it's text (what's written in it)
  4. Returns this value

To conclude:

Instead of printing a returned value, you can easily assigned this value to a variable: myVar = cmds.textField( fieldID, query=True, text=True)


Your modified code:

def printTxtField ( fieldID ):
    #here is the modification made
    myVar = cmds.textField( fieldID, query=True, text=True)

I've commented and reorganized your code to have something more clean:

import maya.cmds as cmds

KEVS_TEXTFIELD_VALUE = None

####################################################
# This function retrieve the value of Kevs_TextField
# and set this value to KEVS_TEXTFIELD_VALUE
####################################################
def retrieveData():
    # We use the query flag on the text attribute
    KEVS_TEXTFIELD_VALUE = cmds.textField("Kevs_TextField", query=True, text=True)
    print u"KEVS_TEXTFIELD_VALUE =" + KEVS_TEXTFIELD_VALUE

####################################################
# This function will create a show a new UI
####################################################
def drawUI():
    winID = 'kevsUI'

    if cmds.window(winID, exists=True): #If the window exists
        cmds.deleteUI(winID)            #Delete it
    cmds.window(winID)                  #Then create a new one

    cmds.columnLayout("Kevs_ColLayout") #Create a columnLayout to sort our widgets  

    # Now let's add some widgets in our columnLayout
    cmds.textField("Kevs_TextField")    #Add a textfied in the columnLayout
    #Then create a button that calls the retrieveData function when pressed
    cmds.button("Kevs_Button", label="Confirm", command=retrieveData)
    cmds.showWindow() #Show the window


drawUI() # Let's create a new UI
DrHaze
  • 1,318
  • 10
  • 22
  • Ok I got it, Thank you very much for your answer! – ErezProductions Jul 23 '15 at 15:27
  • And now, if I want to change the UI window, that instead of recieving text data, I will recieve numbers, how do I do that? Because I have tried to manipulate the code bu I didn't succeed. – ErezProductions Jul 23 '15 at 19:00
  • it writes in the command(2 lines from the end) that retrieveData isn't a declared global variable.. – ErezProductions Jul 24 '15 at 11:22
  • `cmds.intField("Kevs_IntField", query=True, value=True)` see @theodox comment on his answer... I have edited my answer concerning the error, my bad. – DrHaze Jul 24 '15 at 12:57
0

It's possible, but there are a couple of issues in the way you're going about it. @DrHaze's example shows the right thing to do, which is to use the actual python functions instead of strings.

You will also need to think about the visibility of different functions: it's easy to get things working in the listener where all the code is in one place, but once there are multiple functions or modules involved keeping track of gui widget names becomes harder.

For small tools you can define the callback function -- the one that gets used by the button, in this case -- right in the same place that the gui widgets are created. This will use python's rules for closures to keep track of the widgets for you:

def window_example(title):
    if cmds.window(title, exists=True):
        cmds.deleteUI(title)
    win = cmds.window(title)
    cmds.columnLayout()
    whatUSay = cmds.textField()

    #defining this hear gives it access to 'whatUSay'
    def print_text_contents(ignore):
        print cmds.textField( whatUSay, query=True, text=True)

    cmds.button(label='Confirm', command=print_text_contents)
    cmds.showWindow(win)
window_example('kevsUI')

for longer tools you probably want to learn about doing this with classes.

Here's much more background info with some different strategies to check out.

theodox
  • 12,028
  • 3
  • 23
  • 36
  • Ok thank you! And what about numbers as an input instead of strings? What changes will be in this code you wrote? only changing it to intField instead of the textField and etc.? – ErezProductions Jul 23 '15 at 22:23
  • every widget will have a different flag to get its contents. For text fields it is the 'text' flag, for intFields it will be the 'value' flag. You'll need to check the maya docs for the correct flags for each widget – theodox Jul 23 '15 at 22:58