textField example...

4,054 views
Skip to first unread message

Daz

unread,
Oct 24, 2012, 6:39:14 AM10/24/12
to python_in...@googlegroups.com
Heya

One of my ancient enemies since I'm learning python are text fields... I just cant figure out how to get them to work arrr. I want to be able to type in a name and then use it with my functions, when ever I want to select objects, create an object with that text and so on... I got that far but I'm still hitting a wall with it atm wrrr... if any1 could give me a hint as to where I go wrong it would be great !


if cmds.window('a',exists=True):
cmds.deleteUI('a')
cmds.window('a', title='test', w=300, h=100,mnb=False, mxb=False)
cmds.showWindow('a')

mainLayout = cmds.flowLayout()

cmds.text('test ')
cmds.textField(enterCommand=('cmds.select(raw_input(""))'), w=100,h=17)

Ali Khanbabaei

unread,
Oct 24, 2012, 7:13:42 AM10/24/12
to python_in...@googlegroups.com
your script is true.Note that in maya enter command is right Enter Button in your keyboard

Daz

unread,
Oct 24, 2012, 7:26:13 AM10/24/12
to python_in...@googlegroups.com
Heya

Humh... well it does not really work the way I thought it would work... once I press the tiny enter I get a python request input window pop up. I dont want that to happen. I just want it to search using preferably * * names. Not sure I think I can just type *sph* instead of pSphere1 but still the second pop up windows is unwanted... Is there a way to remove it?

Thanks, bya. 

Ali Khanbabaei

unread,
Oct 24, 2012, 7:42:01 AM10/24/12
to python_in...@googlegroups.com
#use this script.i change it a little
import maya.cmds as cmds

if cmds.window('a',exists=True):
   cmds.deleteUI('a')

def selection(*args):
  selectionName = cmds.textField('TFL_selection',q=True,text=True)
  cmds.select(selectionName)


cmds.window('a', title='test', w=300, h=100,mnb=False, mxb=False)
cmds.showWindow('a')

mainLayout = cmds.flowLayout()

cmds.text('test ')
cmds.textField('TFL_selection',enterCommand=selection, w=100,h=17)

Farsheed Ashouri

unread,
Oct 24, 2012, 7:51:07 AM10/24/12
to python_in...@googlegroups.com
Or you can simply use pymel:

---————––------
from pymel.all import *
.
.
Myfield = textField()
.
.
Data = textField.getText()
----––———–––--

Sent from my iPad

David Martinez

unread,
Oct 24, 2012, 7:04:48 AM10/24/12
to python_in...@googlegroups.com
I don't think that you are going to be able and get the value using 'raw_input' in Maya.
What I would do is give the textField a name when creating it:

cmds.textField("nameOfTheTextField",enterCommand="doSomethingWithValue()", w=100,h=17)

And then query it's value using something like this:

value=cmds.textField("nameOfTheTextField",query=True,text=True)

Personally, I would create a function that you execute when you press a button (or press Enter from the textField) which queries the value and do the rest of the work. By doing that, you keep the code separated which is going to help to keep things clear.

Hope it helps


Dave


Daz

unread,
Apr 23, 2013, 5:23:44 AM4/23/13
to python_in...@googlegroups.com
Heya

So I'm back to text field nightmare :)

I'm trying to learn more as to how to use it and so on. Strangely when I try to use the old part of code that work for me I cant get it to work with the new script so ehh... In any case here is what I'm trying to do...

http://pastebin.com/UtY7VbKr

Not sure but I cant get the Value to print what I input in to the text field...

Then once I got that to work(I broke it again somehow :( ) It wont change value in shader so awww...

Also any idea why I have to press the numeric Enter rather than standard Enter to execute?

Thanks, bye.

Daz

unread,
Apr 23, 2013, 5:27:59 AM4/23/13
to python_in...@googlegroups.com
OK silly me...

Now I got the print to work and so on

http://pastebin.com/i0xe7EZ3

I just cant figure out why it cant change the setting to change :(

Justin Israel

unread,
Apr 23, 2013, 6:30:24 AM4/23/13
to python_in...@googlegroups.com
The change command wont fire until the field loses focus. Add one more text field, then change some text in your original field and switch to the new field.
> --
> You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
> To post to this group, send email to python_in...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Daz

unread,
Apr 23, 2013, 7:00:35 AM4/23/13
to python_in...@googlegroups.com
Heya

Thanks for reply Justin!

I did what you suggested but I still cant set attr.
I get this error:

# RuntimeError: setAttr: Error reading data element number 1: .25 #

Mark Jackson

unread,
Apr 23, 2013, 7:19:11 AM4/23/13
to python_inside_maya
A couple of ways of doing something like this.... first one is a really simiple UI that selects, if it can, as you're typing. The cmds.textFieldGrp has an additional flag tcc which is updated as you type rather than on enter.

if cmds.window('a',exists=True):
    cmds.deleteUI('a')
cmds.window('a', title='test', w=300, h=100,mnb=False, mxb=False)
mainLayout = cmds.flowLayout()
cmds.textFieldGrp('textInput',label='test', text='',tcc="cmds.select(cmds.textFieldGrp('textInput',q=True,text=True))")
cmds.showWindow('a')


Or a much better way of doing it is to wrap this into a callback function that manages the command and gives you some error checking on the way

from functools import partial
def selectionCallback(*args):
    txt=cmds.textFieldGrp('textInput',q=True,text=True)
    if cmds.objExists(txt):
        cmds.select(txt)
    else:
        print 'Object no Found'

if cmds.window('a',exists=True):
    cmds.deleteUI('a')
cmds.window('a', title='test', w=300, h=100,mnb=False, mxb=False)
mainLayout = cmds.flowLayout()
cmds.textFieldGrp('textInput',label='test',tcc=partial(selectionCallback))
cmds.showWindow('a')

hope that helps

Mark



--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To post to this group, send email to python_in...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.





--
-------------------------------------
Mark Jackson
Technical Animation Director
http://markj3d.blogspot.com/

Daz

unread,
Apr 23, 2013, 8:03:34 AM4/23/13
to python_in...@googlegroups.com
Heya

Thanks MarkJ for help ! Now this is piece of code I will scratch my head for a while... But I still cant figure out when I run this command

cmds.setAttr('%s.%s'%(materials,'diffuseColorAmount'),value)

Why I get this error

# RuntimeError: setAttr: Error reading data element number 1: 0 #

I put the txt content in to the value and then I tell it to be used when changing attr but it errors :(

Thanks, bye.

Day Dreamer

unread,
Apr 23, 2013, 10:56:56 AM4/23/13
to python_in...@googlegroups.com
It says error reading element number 1 ie. the value attribute of setAttr command, its not able to get the right type.
I think it needs float value and you giving int(but that should work).

don't know from where you getting the value attribute. 
probably, if you getting your value from UI, then it will return a string(most of the cases), you need to type cast it to float or int.
eg. value = float(value)


Daz

unread,
Apr 23, 2013, 11:12:30 AM4/23/13
to python_in...@googlegroups.com
Heya

Hell yea! You totally naild it. I cant believe I didnt think of float! I was typing str(value) b4 but for some reason my mind did not connect the float option whh...

Thanks very much!

Now all works :) Yupieeee

DayDreamer

unread,
Apr 24, 2013, 4:59:15 AM4/24/13
to python_in...@googlegroups.com

Most Welcome
All well that's end well.

Cheerz!!!

Reply all
Reply to author
Forward
0 new messages