tags:

views:

95

answers:

3

As I work and update a class, I want a class instance that is already created to be updated. How do I go about doing that?

class MyClass:
"""  """

def __init__(self):

def myMethod(self, case):
    print 'hello'

classInstance = MyClass()

I run Python inside of Maya and on software start the instance is created. When I call classInstance.myMethod() it always prints 'hello' even if I change this.

Thank you,

/Christian

More complete example:

class MayaCore:
'''
Super class and foundational Maya utility library
'''

def __init__(self):
    """ MayaCore.__init__():  set initial parameters """

    #maya info
    self.mayaVer = self.getMayaVersion()


def convertToPyNode(self, node):
    """     
    SYNOPSIS: checks and converts to PyNode  

    INPUTS: (string?/PyNode?) node: node name

    RETURNS: (PyNode) node 
    """

    if not re.search('pymel', str(node.__class__)):
        if not node.__class__ == str and re.search('Meta', str(node)): return node      # pass Meta objects too
        return PyNode(node)
    else: return node

def verifyMeshSelection(self, all=0):
    """
    SYNOPSIS: Verifies the selection to be mesh transform
    INPUTS: all = 0 - acts only on the first selected item
            all = 1 - acts on all selected items
    RETURNS: 0 if not mesh transform or nothing is selected 
             1 if all/first selected is mesh transform
    """
    self.all = all
    allSelected = []
    error = 0
    iSel = ls(sl=1)
    if iSel != '':
        if self.all: allSelected = ls(sl=1)
        else: 
            allSelected.append(ls(sl=1)[0])

        if allSelected:
            for each in allSelected:
                if nodeType(each) == 'transform' and nodeType(each.getShape()) == 'mesh': 
                    pass
                else: error = 1
        else: error = 1
    else: error = 1

    if error: return 0
    else: return 1

mCore = MayaCore()

The last line is inside the module file (mCore = MayaCore()). There are tons of methods inside the class so I have removed them to shorten the scrolling :-) Also there are import statements above the class but they screw up the formatting for some reason. Here they are:

from pymel.all import *
import re
from maya import OpenMaya as om
from our_libs.configobj import ConfigObj

if getMelGlobal('float', "mVersion") >= 2011:
   from PyQt4 import QtGui, QtCore, uic
   import sip
   from maya import OpenMayaUI as omui

Inside Maya, we import this and subclasses of this class upon program start:

from our_maya.mayaCore import *

In other tools we write, we then call mCore.method() on a need basis. The caveat I am running into is that when I am going back to modify the mCore method and the instance call is already in play, I have to restart Maya for all the instances to get updated with the method change (they will still use the un-modified method).

A: 

Alright, trying again, but with a new understanding of the question:

class Foo(object):
    def method(self):
        print "Before"

f = Foo()
f.method()
def new_method(self):
    print "After"

Foo.method = new_method
f.method()

will print

Before
After

This will work with old style classes too. The key is modifying the class, not overriding the class's name.

Nathon
Sorry, I mis-formatted the post. I am using Python in Maya and I have run into issues getting new style classes to update.
Christian
I can't really use "instance.myMethod = lambda self: print "bye""to update, since the instance calls are already part of other tools in the 3d pipeline. I want to change the method itself to print "bye" and then have the instance.myMethod() print bye. The issue I am having is only during a Maya session. If I restart the program, all the instances update to reflect the changes I've done to my method. Maybe the issue is a Maya issue and not a Python problem?
Christian
Oh, so you want to update all instances of the class at the same time?
Nathon
Correct. Right now I either have to reboot Maya or change the call to the method (in tools that are using the class method), using MyClass().myMethod(), then everything updates.
Christian
A: 

You'll have to provide more details about what you are doing, but Python instances don't store methods, they always get them from their class. So if you change the method on the class, existing instances will see the new method.

Ned Batchelder
I am running Python inside of Maya. The class instance is created when I start the software and when I make updates to the methods inside the class, the classInstance.myMethod() call does not run the updated code, but the old code.
Christian
You're going to have to show us real code. How do you create the class in the first place, and how do you modify the methods? Details will really help us help you.
Ned Batchelder
Edited the main post with more detail.
Christian
A: 

My other answer answers your original question, so I'm leaving it there, but I think what you really want is the reload function.

import our_maya.mayaCore
reload(our_maya.mayaCore)
from our_maya.mayaCore import *

Do that after you change the class definition. Your new method ought to show up and be used by all the existing instances of your class.

Nathon
I have tried that. It does not refresh the instance object - it will still run the unmodified code.
Christian