I'm writing a script in Maya and trying to assign a variable inside another method instead of inside __init__
like we normally do. I put my window commands inside the __init__
so when a new instance is initialized, if we assign variables inside __init__
it will run all the way through without assigning the variables the value I want.
For example:
def __init__(self, windowID = 'selectsomething', title = 'select something'):
self.windowID = windowID
self.title = title
if cmds.window(self.windowID, exists = True):
cmds.deleteUI(self.windowID)
myWindow = cmds.window(self.windowID, title = self.title)
cmds.rowColumnLayout(numberOfColumns = 2, columnWidth = [(1, 300), (2, 100)],
columnAttach = [1, 'left', 10], columnSpacing = [1, 10],
rowSpacing = [1, 5])
cmds.button(label = "Apply", command = self.applyButton) #it should print out selected object
cmds.button(label = "Cancel", command = self.cancelButton)
cmds.showWindow(myWindow)
#now for example if I have a sel variable to store selected objects, it would actually return nothing cuz the code ran all the way through and didnt wait for me to select what I needed
sel = cmds.ls(selection = True) #this will return nothing
My solution is to assign variables inside the applyButton
method, which will store what I selected. I found it's a bit weird to call a method which uses a variable from another method from that method itself.
For example:
class Abc:
def func(*arg):
print(self.var)
def var(self):
self.var = 1
self.func()
abc = Abc()
abc.var()
For now the code runs, but doesn't it sound a bit weird to call the func
method which uses the variable from var
method from var
method itself?
I haven't seen anyone do it. I mean without class, a variable assigned inside a function stays inside that function and we can only use it with return
. But it looks like in class we can make any inside-method-variable global by adding self.
before it?
My friend also said we can use @property
in this case but I don't think I figured it out yet.