How to identify what item is being selected in a textScrollList?


#1

Hi guys, I’m a student doing a coding class, and we need to create a UI. Right now, I can’t for the life of me find out what is the line of code needed to tell PyMEL to identify what item is being selected in the textScrollList.

I want to make a function, such that when the item in the textscrolllist is being selected, it will also select the item with the same name in the scene/viewport.

Note: the items in the textscrolllist are being generated by the items in the scene.

Here’s what I’ve got so far:

def selectfromlist(*args):
…objectselected = ##I want to get the item being selected in the textScrollList, ‘lightnamelist’,##
…if objectselected is True: ##not sure if this is the right way to say it either##
…cmds.select(##the item in the scene with the same name as the item in the textScrollList##)

Does anybody know how to fix this?


#2

If you decide to use pymel then don’t use cmds. At least stick with one or the other until you know the differences. I would choose pymel.

Pymel has many methods that are useful. You can discover these methods by first creating a python object and then using “dir()” to list the methods of that object. And then you can use “help()” to get some clues about the syntax.

So try the following python, executing just one line at a time in the maya script editor.
The code doesnt do anything useful, and will probably mess up the ui a bit, but it will revert back when you restart maya, so dont worry about that. This way you can discover what you need…

# create a TextScrollList object assigned to variable "tsl".
tsl = pm.textScrollList()

# show the object 
tsl
# Result: ui.TextScrollList('scriptEditorPanel1Window|TearOffPane|scriptEditorPanel1|textScrollList2') # 

# list the methods
dir(tsl)
# you should see a big lits of methods.. have a look through

# Did you notice all the "get..." methods?
# This one looks interesting
help(tsl.getSelectItem)

You will see some clues there. But you will still have to work for it. Read teh manuals and look at other peoples code for examples. But with dir() and help() you can get a fair way.

David