PDA

View Full Version : Python "instance has no attribute"


Aikiman
09-28-2011, 07:45 AM
Hey evry1,

As a beginner to Python, noob problems are my life. Im getting the following error when trying to execute the code in Mayas script editor...

# AttributeError: myTestWindow instance has no attribute 'myFunction1' #

Heres the code, I thought I have tried everything and spent over an 2 hours trying to fix it and search the net for an answer and looking at other examples. Im sure its very basic mistake, they usually are :)

import maya.cmds as mc

class myTestWindow:
def __init__(self):
self.name = 'myTest'
self.title = 'Test Window'

def doit(self):

# check for existing window
if (mc.window(self.name, q=1, exists=1)): mc.deleteUI(self.name)
mc.window(self.name, title=self.title, s=1)

# UI components
mc.columnLayout()
mc.button(l='create', c=self.myFunction1())
mc.setParent('..')

# set window width/height
mc.showWindow(self.name)
mc.window(self.name, e=1, wh=[420, 210])

def myFunction1(self):

print 'Hello Help Window'


myTest1 = myTestWindow()
myTest1.doit()

ManuelM
09-28-2011, 08:51 AM
I've modified the code a bit... especially note how I've added the arg=None argument to the method 'myFunction1' and then how the command gets called using 'partial'. It might not make sense at the first glance, maybe you should google for 'python partial'


p, li { white-space: pre-wrap; } import maya.cmds as mc


from functools import partial



class myTestWindow:
def __init__(self):
self.name = 'myTest'
self.title = 'Test Window'

def doit(self):

# check for existing window
if (mc.window(self.name, q=1, exists=1)): mc.deleteUI(self.name)
mc.window(self.name, title=self.title, s=1)

# UI components
mc.columnLayout()
mc.button(l='create', c=partial(self.myFunction1))
mc.setParent('..')

# set window width/height
mc.showWindow(self.name)
mc.window(self.name, e=1, wh=[420, 210])

def myFunction1(self, arg = None):

print 'Hello Help Window'

myTest1 = myTestWindow()
myTest1.doit()

NaughtyNathan
09-28-2011, 08:52 AM
I think it's this:
mc.button(l='create', c=self.myFunction1())by putting the braces you are EXECUTING this function here, rather than simply pointing to it. try removing the orange braces and see what happens (although you'll probably have argument errors then which you'll need to address:
def myFunction1(self,arg)::nathaN

Aikiman
09-28-2011, 10:48 AM
@Manuel, thanks I will google partial and get the run down. If I copy and paste your code into the script editor it executes well, but if I just make the changes in my own code, it seems to still error.


Im wondering if there is some hidden formatting issues I am having with the tabbing that is causing me grief, I hope it isnt my text editor that needs fixing, this could get very annoying.

@Nathan, thanks for that but I am still getting random errors, this time with 'self' not being defined. Ill go with 'partial' for now and keep going.

Thanks for quick replies.

CGTalk Moderation
09-28-2011, 10:48 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.