UI Logic problem

Hi,

I am new to scripting in Maya (did a little bit in XSI), and I have a problem with a UI using class. 

I am wondering what is the right way to 'refresh' a UI when using a class.

I've shortened the script to focus on the problem I have. I am trying to refresh a window UI (delete and reCreate it ) when the 'Type' optionMenu is changed (I need to refresh it, because all the other parameters of the UI will depend of the selected 'Type').

There is a 'Maya Fatal Error' appening when trying to delete the UI. In this example, it's on line 78. But ultimately, I would uncomment line 77, and the error is appening in the _initUI function (but also when deleting the UI.... cmds.deleteUI(self.win, window=True)).

So if you try the code as it is, you would get a fatal error (Maya shuts down), when changing the 'Type' parameter of my UI.

What is interesting is that... when the UI is created (I create it as object a, in my example), I can type in maya's script Editor; cmds.deleteUI(testUI.a.win,window=True) without creating the error.

It's probably the location from where I call; cmds.deleteUI(self.win, window=True) inside my class that is wrong...

Do you have any idea?

thanks,
Jonathan

here's my script... can be cpoy/pasted to sublime/Maya Script Editor...

import maya.cmds as cmds

from functools import partial

import os

class SHED_adder():

    def __init__(self, fixed=True):

        self.win = 'SHED_adder'

        self.fixed = fixed

        self.name = self.win

        ## Window args on Init

        self.project    = 'Project_003'

        self.type       = 'Shot'

    ####################################################

    ####################################################

    ####################### New scene TAB

    def _initUI(self):

        ####################################################

        ###  Window management

        if cmds.window(self.win, exists=True):

            cmds.deleteUI(self.win, window=True)

        cmds.window(self.win, title=self.win, sizeable=False, mnb = False, mxb = False)

        cmds.columnLayout('mainColumn', adjustableColumn=True, w=350, columnAttach=('both', 3), rowSpacing=5, columnWidth=350 )

        cmds.rowColumnLayout( parent = 'mainColumn', numberOfColumns=2, rowSpacing= [1,4], columnAttach=[(1, 'left', 5),(2,'both',5)], columnWidth=[(1, 100), (2, 250)] )

        cmds.separator(h=2, style='none')

        cmds.separator(h=2, style='none')

        ## Populate Project OptionMenu

        cmds.text( label='Project' )

        cmds.optionMenu("Project", changeCommand = partial(self.__uiUpdate, menuUpdated = 'Project'))

        self.__populateProjects()

        ## Populate Type OptionMenu

        cmds.text( label='Type' )

        cmds.optionMenu("Type", changeCommand = partial(self.__uiUpdate, menuUpdated = 'Type'))

        self.__populateType()

        ## Add Informations depending on type (Asset/Shot)...

        ## Now just a text line, but later, will need refresh with few options

        if self.type == 'Asset':

            cmds.text( label='Asset Name' )

            cmds.textField("assetName", text = 'NewAssetName')

        elif self.type == 'Shot':

            cmds.text( label='Shot #' )

            cmds.textField("ShotID", text = '010')

        cmds.showWindow(self.win)

    ## Call this function when any of the optionsMenu is changed...

    ## Check if we need refresh the UI

    ## Update self.VARIABLES concerned...

    def __uiUpdate(self, value = None, menuUpdated = None):

        ## IF Project menuUPDT, no need to refresh window

        ## only update slf.project var

        if menuUpdated == 'Project':

            self.project = value

            print "Project changed!!!   Self project is now equal to: {project}".format(project=self.project)

        ## Check if the new selected type is different the what it already is...

        elif menuUpdated == 'Type' and self.type != value:

            print 'Type menu updated from: {oldType} to  :{newType}'.format(oldType = self.type, newType = value)

            self.type = value

            print 'self   ', self.win

            # self._initUI()

            cmds.deleteUI(self.win, window=True)

    def __populateProjects(self):

        ## List of projects from SHOTGUN

        # projects list

        #project_items = sg.getActiveProjectsNameComboWIP()

        project_items = ['Project_001','Project_002','Project_003','Project_004']

        projectList = []

        for project in project_items:

            if project not in projectList:

                projectList.append(project)

                cmds.menuItem(label = project , parent = "Project")

        cmds.optionMenu("Project", edit=True, value = self.project)

    def __populateType(self):

        typeList = ['Asset', 'Shot']

        for types in typeList:

            cmds.menuItem(label = types , parent = "Type")

        cmds.optionMenu("Type", e=True, value = self.type)

a = SHED_adder()

a._initUI()