原问题的原因与这个问题有关:Maya Python: Unbound Method due to Reload()。我找到的解决方案本质上是重新设计我的跟踪方法,使其不需要初始化,然后,为了避免未绑定错误,我创建了最接近其用途的初始化序列。
trackingMethod文件:
import maya.cmds as cmds
import maya.OpenMaya as om
class pathTracking(object):
def setPathing(self, instance):
sel = om.MSelectionList()
sel.add(instance.nodeName)
try:
instance.obj = om.MObject()
sel.getDependNode(0, instance.obj)
except:
cmds.warning(instance.nodeName + " is somehow invalid as an openMaya obj node.")
try:
instance.dag = om.MDagPath()
sel.getDagPath(0,instance.dag)
except:
pass
def __set__(self, instance, value):
if isinstance(value,dict):
if "dag" in value:
instance.dag = value["dag"]
if "obj" in value:
instance.obj = value["obj"]
if "nodeName" in value:
instance.nodeName = value["nodeName"]
self.setPathing(instance)
else:
if isinstance(value, basestring):
instance.nodeName = value
self.setPathing(instance)
def __get__(self, instance, owner):
if instance.dag and instance.dag.fullPathName():
return instance.dag.fullPathName()
return om.MFnDependencyNode(instance.obj).name()
class exampleNode(object):
path = pathTracking()
dag = None
obj = None
nodeName = ""
someVar1 = "blah blah"
def initialize(self,nodeName,obj,dag):
if obj or dag:
self.obj = obj
self.dag = dag
elif nodeName:
self.path = nodeName
else:
return False
return True
其他文件:
import trackingMethod as trm
circleExample(trm.exampleNode):
def __init__(self,nodeName="",dag=None,obj=None):
if not self.initialize(nodeName,obj,dag)
self.path = cmds.circle()[0]
用这个方法我可以做到
circles = [circleExample(nodeName=x) for x in cmds.ls(sl=1,l=1)]
PS。我遇到了一些需要先初始化类的东西,然后才能创建它的一些位。下面是一个自定义字典,需要我在创建字典时将一个 self 实例传递给它。在与转换交互的每个类中重新创建这些 dict 结构将是乏味的。解决方案是将这些依赖类初始化放入转换类中的一个函数中。这样,最终类继承了创建字典的函数,并可以在它们的 init 中调用它。这避免了当您有多个文件从单个类继承时中断的整个俄罗斯嵌套娃娃 init 语句。
虽然这个解决方案对某些人来说似乎很明显,但我只是想解决一个鸡蛋情况的方法,即需要初始化类以获取自我,但由于未绑定方法而无法初始化类错误。
class sqetDict(dict):
def __init__(self,instance,*args,**kwargs):
self.instance = instance
dict.__init__(self,*args,**kwargs)
def __getitem__(self, key):
thing = dict.__getitem__(self,key)
if key in self and isinstance(thing,(connection,Attribute,xform)):
return thing.__get__(self.instance,None)
else:
return dict.__getitem__(self,key)
def __setitem__(self, key, value):
thing = dict.__getitem__(self,key)
if key in self and isinstance(thing,(connection,Attribute,xform)):
thing.__set__(self.instance,value)
else:
dict.__setitem__(self,key,value)
这些 dicts 会这样初始化:
def enableDicts(self):
self.connection = sqetDict(self, {"txyz": connection("translate"), "tx": connection("tx"),
"ty": connection("ty"), "tz": connection("tz"),
"rxyz": connection("rotate"),
"rx": connection("rx"), "ry": connection("ry"), "rz": connection("rz"),
"sxyz": connection("scale"),
"sx": connection("sx"), "sy": connection("sy"), "sz": connection("sz"),
"joxyz": connection("jointOrient"),
"jox": connection("jox"), "joy": connection("joy"), "joz": connection("joz"),
"worldMatrix": connection("worldMatrix"),
"worldInvMatrix": connection("worldInverseMatrix"),
"parentInvMatrix": connection("parentInverseMatrix")})
self.value = sqetDict(self, {"txyz": Attribute("translate", "double3"),
"tx": Attribute("tx", "float"), "ty": Attribute("ty", "float"),
"tz": Attribute("tz", "float"),
"rxyz": Attribute("rotate", "double3"),
"rx": Attribute("rx", "float"), "ry": Attribute("ry", "float"),
"rz": Attribute("rz", "float"),
"sxyz": Attribute("scale", "double3"),
"sx": Attribute("sx", "float"), "sy": Attribute("sy", "float"),
"sz": Attribute("sz", "float"),
"joxyz": Attribute("jointOrient", "double3"),
"jox": Attribute("jox", "float"), "joy": Attribute("joy", "float"),
"joz": Attribute("joz", "float"),
"rotOrder": Attribute("rotateOrder", "string"),
"worldMatrix": Attribute("worldMatrix", "matrix"),
"worldInvMatrix": Attribute("worldInverseMatrix", "matrix"),
"parentInvMatrix": Attribute("parentInverseMatrix", "matrix"),
"rotatePivot": Attribute("rotatePivot", "double3"),
"visibility": Attribute("visibility", "long")})
self.xform = sqetDict(self, {"t": xform("t"), "ro": xform("ro"), "s": xform("s")})
我的连接类在发送一个值时执行 cmds.connectAttr,它以字典形式返回连接的各种属性,例如 {"in": "in connection", "out":["outConn1","outCon2 ",etc..], "path":"fullpath name to attribute"}.所以你可以做类似的事情,thingA.connection["txyz"] = thingB.connection["txyz"]["path"] 来连接两个对象的相对平移。
我的 Attribute 类允许设置和获取属性值,例如 temp = thing.value["txyz"] 结果为 temp = (value,value,value),而 thing.value["txyz"]=(0, 0,0) 会将平移归零。
xform 做值的事情,但在绝对世界空间值中。