It is a confusing topic in python, but worthwhile understanding...

 

__new__ is called BEFORE the object instance is actually created, so that
you can control the instance creation (for example, creating the window gui
element); __init__ is called AFTER the instance ('self') has already
instantiated and is in memory, allowing you to modify it or add attributes
to it. 

__new__ is useful primarily when deriving a class from built-in types such
as 'str', 'dict', etc.

 

But don't trust me... go to the source and read more! :)

 

From: [email protected]
[mailto:[email protected]] On Behalf Of David Moulder
Sent: Thursday, September 24, 2009 2:40 PM
To: [email protected]
Subject: [Maya-Python] Re: Pymel __new__(cls)

 

Doh!
It's been one of those days!  Thanks for your reply I'm sure that will work
:)

Still not sure why the __new__() has to run these to lines
self = pm.window(cls.winName, title=cls.winTitle)
return pm.Window.__new__(cls, self)

I'd of thought the __init__()  would have been called directly after the
__new__().
So why >>>> self = pm.window(cls.winName, title=cls.winTitle)
If in the __init__() I gave the UI an objectName, would that work?
When I get 5 mins at work I'll read up on the __new__ in a python class.
It's the first time I've come across the concept.

-Dave



On Thu, Sep 24, 2009 at 7:34 PM, Ofer Koren <[email protected]> wrote:


There's a window Title, and a window name (every gui element has a name).
This ui name has to conform to the same rules of naming variables in python,
so "World Co-Ords" is thus invalid (spaces, dash...).

Try this:


class WCForm(pm.Window):
   '''
   World Coords UI Form.
   '''

   winTitle = 'World Co-Ords'
   winName = "worldCoOrds"



   def __new__(cls):
       if pm.window(cls.winName, exists=True):
           pm.deleteUI(cls.winName)

       self = pm.window(cls.winName, title=cls.winTitle)

       return pm.Window.__new__(cls, self)

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of thirstydevil
Sent: Thursday, September 24, 2009 1:48 AM
To: python_inside_maya
Subject: [Maya-Python] Pymel __new__(cls)


Trying to do some UI stuff in pymel.  Wondering why pm.window
(cls.winName, exists=True) is always
failing.  It seems the UI is not getting the Window name. Using Pymel
9.2.0

Cheers
-Dave

PS
I understand the concept of __new__() but I'm fuzzy on why these lines
are needed?
       self = pm.window(cls.winName, title=cls.winName)
       return pm.Window.__new__(cls, self)

# example UI code:
def DisplayUI():
   '''
   calls the window.
   '''
   win = WCForm()

class WCForm(pm.Window):
   '''
   World Coords UI Form.
   '''
   winName = 'World Co-Ords'

   def __new__(cls):
       if pm.window(cls.winName, exists=True):
           pm.deleteUI(cls.winName)
       self = pm.window(cls.winName, title=cls.winName)
       return pm.Window.__new__(cls, self)

   def __init__(self):
       super(WCForm, self).__init__()
       self.show()








--~--~---------~--~----~------------~-------~--~----~
http://groups.google.com/group/python_inside_maya
-~----------~----~----~----~------~----~------~--~---

Reply via email to