Advertisement
ViktorasM

Untitled

Feb 4th, 2013
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import maya.cmds as cmds
  2. import maya.OpenMaya as om
  3.  
  4. class NodeHandle:
  5.     def __init__(self,handle,weakReference=False):
  6.         '''
  7.            construct object by given name or MObject handle
  8.            weakReference is an option to construct object as
  9.            weak reference, enabling tracking if MObject handle is still valid within maya
  10.        '''
  11.        
  12.         self.__weakReference = weakReference
  13.  
  14.         if isinstance(handle,(list,tuple)):
  15.             handle = handle[0]
  16.  
  17.         if isinstance(handle, NodeHandle):
  18.             handle = handle.handle()
  19.        
  20.         if not isinstance(handle, om.MObject):
  21.             name = str(handle)
  22.             handle = NodeHandle.getHandleByName(name)
  23.             # handle to node was not created. possible problems?
  24.             if handle is None:
  25.                 nodeNameTest = cmds.ls(name)
  26.                 if len(nodeNameTest)==0:
  27.                     raise Exception,'node "%s" does not exist' % name
  28.                 if len(nodeNameTest)>1:
  29.                     raise Exception,'there\'s more than one node "%s" in the scene' % name
  30.            
  31.         self.__handle = handle
  32.         if self.__weakReference:
  33.             self.__weakHandle = om.MObjectHandle(self.__handle)
  34.  
  35.         return
  36.    
  37.     def isWeakReference(self):
  38.         'tests if we got support for weak reference methods (isAlive(), isValid()) for this node'
  39.        
  40.         return self.__weakReference
  41.    
  42.     def isAlive(self):
  43.         'tests if handle to node is still alive (see MObjectHandle.isAlive() for more info)'
  44.         if not self.isWeakReference():
  45.             raise AssertionError,'operation not supported for non-weak references'
  46.         return self.__weakHandle.isAlive()
  47.  
  48.     def isValid(self):
  49.         'tests if handle to node is still valid (see MObjectHandle.isValid() for more info)'
  50.        
  51.         if not self.isWeakReference():
  52.             raise AssertionError,'operation not supported for non-weak references'
  53.         return self.__weakHandle.isValid()
  54.    
  55.     def __str__(self):
  56.         'self cast to string: return full dag path for direct use with maya.cmds methods'
  57.         return self.name()
  58.    
  59.     def __repr__(self):
  60.         'representation for debugging: prints class name and node full path'
  61.         return "%s(%s)" % (self.__class__.__name__,self.name())
  62.    
  63.     def handle(self):
  64.         'returns MObject for this node'
  65.        
  66.         return self.__handle
  67.    
  68.     def path(self):
  69.         dag = self.getDagFn()
  70.         path = om.MDagPath();
  71.        
  72.         dag.getPath(path)
  73.         return path
  74.    
  75.     def name(self):
  76.         'returns full path version of node name'
  77.        
  78.         try:
  79.             if not self.hasFn(om.MFn.kDagNode):
  80.                 return self.shortName()
  81.            
  82.             return self.path().fullPathName()
  83.         except:
  84.             return self.shortName()
  85.  
  86.     def hasFn(self,mfn):
  87.         return self.handle().hasFn(mfn)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement