gui object not found issue (python)


#1

Hello,

I’m trying to understand why this code isn’t working.

import maya.cmds as cmds

winName = "myWindow"
dockName = "windowDock"
    
def gui(*args):
    
    global valueFLD
    
    if(cmds.window(winName, exists=True)):
          cmds.deleteUI(winName)
          print("Deleted existing window")
        
    if(cmds.dockControl(dockName, exists=True)):
          cmds.deleteUI(dockName)
          print("Deleted existing dock")
        
    cmds.window(winName, w=300, h=300)
    cmds.columnLayout()
    cmds.button(w=150, h=30,command=workPlease)
    valueFLD = cmds.intSliderGrp(v=2, min=1,max=1000,s=1)
    #cmds.showWindow(winName)
    cmds.dockControl(dockName, area="right",content=winName)
    
def workPlease(*args):
    
    currentValue = cmds.intSliderGrp(valueFLD, q=True, v=True)
    print currentValue


I use:

import windowTesting
reload (windowTesting)
windowTesting.gui()

to run the code.

The gui shows up as it should but when I click the button I get the error:

‘myWindow|columnLayout17|intSliderGrp10’ not found. #

If I uncomment the showWindow line and comment out the dockControl line it works fine, except of course it isn’t docked, and I want it to be docked. It also works if I just change the intSliderGrp to an intField.


#2

most likely its because you capturing the path to the element when the window is created, and storing it as a global variable, and it doesn’t get updated when the dock state changes, so it can’t be found anymore.

with that said, the way you going about this is not really the best way to do it. you are better off using a Class.


#3

What would the class be doing exactly? I don’t understand classes very well.


#4

a class can keep track of all the widgets, and each function in the class can access the widgets, so its very easy to query widgets without passing them around or using global variables.

Here is simple example on how a widget can be created and queried. Note: this is not a complete example - it won’t run as is…


class MySimpleGUI(object):
	def __init__(self):
		self.slider = cmds.floatSlider()
 
	def doIt(self):
		selection = cmds.ls(sl=True)
		value = cmds.floatSlider(self.slider, q=True, v=True))
		cmds.move(selection, value)
 


#5

Ok here is what I have and its still giving me the same error.

import maya.cmds as cmds

winName = "myWindow"
dockName = "windowDock"
    
class GUI:
    
    def __init__(self):
        
        if(cmds.window(winName, exists=True)):
            cmds.deleteUI(winName)
            print("Deleted existing window")
            
        if(cmds.dockControl(dockName, exists=True)):
            cmds.deleteUI(dockName)
            print("Deleted existing dock")
            
        self.window = cmds.window(winName, w=300, h=300)
        self.columnLO1 = cmds.columnLayout()
        self.button1 = cmds.button(w=150, h=30,command=workPlease)
        self.valueFLD = cmds.intSliderGrp(v=2, min=1,max=1000,s=1)
        #cmds.showWindow(winName)
        self.dockCtrl = cmds.dockControl(dockName, area="right",content=self.window)
    
    def workPlease(self):
    
        currentValue = cmds.intSliderGrp(self.valueFLD, q=True, v=True)
        print currentValue

What am I doing wrong here?


#6

I didnt try to run your code, but this line

self.button1 = cmds.button(w=150, h=30,command=workPlease)

should be command=self.workPlease

David


#7

Yea I had the command variable wrong, however that wasn’t the problem because I still got the same error.

On another forum I did come across something that makes it work, thought I’m not sure its the best solution.

You can use the string split method to chop off the name of the widget from its path.

import maya.cmds as cmds

winName = "myWindow"
dockName = "windowDock"
    
class GUI:
    
    def __init__(self):
        
        if(cmds.window(winName, exists=True)):
            cmds.deleteUI(winName)
            print("Deleted existing window")
            
        if(cmds.dockControl(dockName, exists=True)):
            cmds.deleteUI(dockName)
            print("Deleted existing dock")
            
        self.window = cmds.window(winName, w=300, h=300)
        self.columnLO1 = cmds.columnLayout()
        self.button1 = cmds.button(w=150, h=30,command=self.workPlease)
        sliderGrp = cmds.intSliderGrp(v=2, min=1,max=1000,s=1)
        print sliderGrp # this prints myWindow|columnLayout71|intSliderGrp46
        self.valueFLD = sliderGrp.split('|')[-1]
        print self.valueFLD # this prints intSliderGrp46
        self.dockCtrl = cmds.dockControl(dockName, area="right",content=self.window)
    
    def workPlease(self, *_):
    
        currentValue = cmds.intSliderGrp(self.valueFLD, q=True, v=True)
        print currentValue

And this works. Yet I have many other widgets in my code that work fine without having the paths removed from their name, so I’m not sure whats happening here. Maybe its something to do with the widget being a Grp?


#8

Your problem is the dockControl. There is something strange goin on here because normal ctrl names are changed. I tried it and the usual name of your intFieldGroup changes from:

myWindow|columnLayout29|SLIDER

to

MayaWindow|columnLayout27|SLIDER

I would have expected something like this:

MayaWindow|windowDock|myWindow|columnLayout29|SLIDER

With this result you have several ways to solve your problem:

[ul]
[li]Name your ctrls explicitly: cmds.intSliderGrp(“SLIDER”, v=2, min=1,max=1000,s=1) then you can use the explicit name. This only works if you have exact one ctrl with this name.[/li][li]use pymel. I’m not entirely sure but in pymel you do not save ctrl names but objects and this should work independently from the actual path name, at least I hope so.[/li][li]Modify the name of your controls to the correct path.[/li][/ul]The last point works for me if I add these lines after creation of the dockControl:

self.dockParent = cmds.dockControl(self.dockCtrl, q=True, parent=True)
 self.valueFLD = self.valueFLD.replace("myWindow", self.dockParent)

But I’m not sure if it works in all cases and if this is a valid procedure at all.


#9

Yea the last one works for me too.

Like you said, it might not be valid procedure, but at least its working for now.

Thanks