Search code examples
pythonuser-interfacemaya

Function cannot find the value from a field in Maya Python UI


I am building a Maya UI, for a script that I've been working on, but I am having a problem when I try to prevent multiple windows. Basically, if I don't place this in front of the code:

winID = 'vs-sfa'

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

Everything works, but then the use could create a lot of windows and that would be problematic. When I add this code though, and try to access the value in a textScrollList field, I get None. Any suggestions? Here is the code..

winID = 'vs-sfa'
#Prevent multiple windows
if(cmds.window(winID, exists=True)):
    cmds.deleteUI(winID, window=True)

#Create new window 
window = cmds.window(winID, title="Create Sound from Animation", iconName='soundFromAnimW', widthHeight=(520, 400) )

#Tabs layout
tabs = cmds.tabLayout()
tab1 = cmds.columnLayout()

#Controls for the first tab
cmds.text(l='basic stuff')
cmds.setParent('..')
tab2 = cmds.columnLayout( adjustableColumn=True )

#Controls for the second tab
cmds.text( label='Some text with instructions and stuff' )
cmds.button( label='Update Selection', command='updateSel()' )
cmds.button( label='Close', command=('cmds.deleteUI(\"' + window + '\", window=True)') )
cmds.rowLayout(numberOfColumns=2, adjustableColumn=2, columnWidth2=[260,260] )
#The textScrollList fields, from which I get None
selection = cmds.textScrollList( numberOfRows=4, append=sel, allowMultiSelection=True, sc='updateAttrs()' )
freqs = cmds.textScrollList( numberOfRows=4, append=Cmaj )
cmds.setParent('..')
cmds.columnLayout(adjustableColumn=True)
attrs = cmds.textScrollList(numberOfRows = 9, append=attrsList, allowMultiSelection=True)
cmds.button(l='Assign Frequency', command='assignFreq()')
connections = cmds.textScrollList( numberOfRows=4, allowMultiSelection=True)
cmds.button(l='Remove assignment', command='removeFreq()')
cmds.button(l='Create sound', command='createSound()')
messages = cmds.textScrollList(numberOfRows=6, allowMultiSelection=True)
cmds.setParent('..')
cmds.tabLayout(tabs, e=True, tl=((tab1, 'Basic'),(tab2, 'Advanced')))
cmds.showWindow( window )

Solution

  • When I run your code in Maya, I get the following error:

    # Error: name 'sel' is not defined
    # Traceback (most recent call last):
    #   File "<maya console>", line 26, in <module>
    # NameError: name 'sel' is not defined # 
    

    When you're creating your textScrollList you try to append in it an object sel which is not defined and do not exists yet.

    The reason why it is working when you remove the two first lines to delete the window if it exists is probably because you create the sel variable further in the script and, when you recreate a new window the variable is now defined and can populate the newly created textScrollList.

    What you could do is either creating the variable sel before calling it in your window or create your textScrollList empty and then, when you define the sel variable, edit the field and append the variable in it.