python 更新类实例以反映类方法中的更改

发布于 2024-09-18 20:32:28 字数 2421 浏览 2 评论 0原文

当我工作和更新类时,我希望更新已创建的类实例。我该如何去做呢?

class MyClass:
"""  """

def __init__(self):

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

classInstance = MyClass()

我在 Maya 内部运行 Python,并在软件启动时创建实例。当我调用 classInstance.myMethod() 时,即使我更改了它,它也总是打印“hello”。

谢谢

/Christian

更完整的示例:

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()

最后一行位于模块文件内 (mCore = MayaCore())。 类中有大量方法,因此我删除了它们以缩短滚动时间:-) 类上方还有 import 语句,但由于某种原因它们搞砸了格式。它们是:

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

在 Maya 内部,我们在程序启动时导入此类及其子类:

from our_maya.mayaCore import *

在我们编写的其他工具中,然后根据需要调用 mCore.method()。 我遇到的警告是,当我返回修改 mCore 方法并且实例调用已经在运行时,我必须重新启动 Maya 才能使所有实例通过方法更改进行更新(它们仍将使用 un -修改方法)。

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鹿! 2024-09-25 20:32:50

我的其他答案回答了您原来的问题,所以我将其留在那里,但我认为您真正想要的是 reload 功能。

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

更改类定义后执行此操作。您的新方法应该显示并被您的类的所有现有实例使用。

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.

只为守护你 2024-09-25 20:32:43

您必须提供有关您正在执行的操作的更多详细信息,但 Python 实例不存储方法,它们总是从类中获取方法。因此,如果您更改类上的方法,现有实例将看到新方法。

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.

£冰雨忧蓝° 2024-09-25 20:32:39

好吧,再试一次,但对问题有了新的理解:

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

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

Foo.method = new_method
f.method()

将打印

Before
After

这也适用于旧样式的类。关键是修改类,而不是覆盖类的名称。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文