Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import maya.cmds as cmds
- import maya.OpenMaya as om
- class NodeHandle:
- def __init__(self,handle,weakReference=False):
- '''
- construct object by given name or MObject handle
- weakReference is an option to construct object as
- weak reference, enabling tracking if MObject handle is still valid within maya
- '''
- self.__weakReference = weakReference
- if isinstance(handle,(list,tuple)):
- handle = handle[0]
- if isinstance(handle, NodeHandle):
- handle = handle.handle()
- if not isinstance(handle, om.MObject):
- name = str(handle)
- handle = NodeHandle.getHandleByName(name)
- # handle to node was not created. possible problems?
- if handle is None:
- nodeNameTest = cmds.ls(name)
- if len(nodeNameTest)==0:
- raise Exception,'node "%s" does not exist' % name
- if len(nodeNameTest)>1:
- raise Exception,'there\'s more than one node "%s" in the scene' % name
- self.__handle = handle
- if self.__weakReference:
- self.__weakHandle = om.MObjectHandle(self.__handle)
- return
- def isWeakReference(self):
- 'tests if we got support for weak reference methods (isAlive(), isValid()) for this node'
- return self.__weakReference
- def isAlive(self):
- 'tests if handle to node is still alive (see MObjectHandle.isAlive() for more info)'
- if not self.isWeakReference():
- raise AssertionError,'operation not supported for non-weak references'
- return self.__weakHandle.isAlive()
- def isValid(self):
- 'tests if handle to node is still valid (see MObjectHandle.isValid() for more info)'
- if not self.isWeakReference():
- raise AssertionError,'operation not supported for non-weak references'
- return self.__weakHandle.isValid()
- def __str__(self):
- 'self cast to string: return full dag path for direct use with maya.cmds methods'
- return self.name()
- def __repr__(self):
- 'representation for debugging: prints class name and node full path'
- return "%s(%s)" % (self.__class__.__name__,self.name())
- def handle(self):
- 'returns MObject for this node'
- return self.__handle
- def path(self):
- dag = self.getDagFn()
- path = om.MDagPath();
- dag.getPath(path)
- return path
- def name(self):
- 'returns full path version of node name'
- try:
- if not self.hasFn(om.MFn.kDagNode):
- return self.shortName()
- return self.path().fullPathName()
- except:
- return self.shortName()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement