当我工作和更新一个类时,我想要一个已经创建的类实例进行更新。我该怎么做呢?
class MyClass:
""" """
def __init__(self):
def myMethod(self, case):
print 'hello'
类实例 = MyClass()
我在 Maya 中运行 Python,并在软件启动时创建实例。当我调用 classInstance.myMethod() 时,即使我更改它,它也总是会打印“你好”。
谢谢,
/基督教
更完整的例子:
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())。类中有很多方法,所以我删除了它们以缩短滚动时间:-) 类上方还有导入语句,但由于某种原因它们搞砸了格式。他们来了:
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 -修改方法)。