def __exit__(self, exc_type, exc_value, traceback): try: if self.dagGhosts: self.dagMod.undoIt() if self.dgGhosts: self.dgMod.undoIt() except RuntimeError: stillExist = [] for mayaType in self.ghosts: obj = self.byMayaType[mayaType] if obj is not None and api.isValidMObjectHandle( api.MObjectHandle(obj)): stillExist.append(obj) if stillExist: mfnDag = api.MFnDagNode() mfnDep = api.MFnDependencyNode() names = [] for obj in stillExist: if obj.hasFn(api.MFn.kDagNode): # we need to delete the parent, since it will have # created a parent transform too mfnDag.setObject(obj) mfnDag.setObject(mfnDag.parent(0)) names.append(mfnDag.partialPathName()) else: mfnDep.setObject(obj) names.append(mfnDep.name()) print names #import maya.cmds as cmds # cmds.delete(names) mfnDag = api.MFnDagNode() dagMod = api.MDagModifier() dgMod = api.MDGModifier() delDag = False delDg = False for obj in stillExist: if obj.hasFn(api.MFn.kDagNode): # we need to delete the parent, since it will have # created a parent transform too mfnDag.setObject(obj) dagMod.deleteNode(mfnDag.parent(0)) else: dgMod.deleteNode(obj) if delDag: dagMod.doIt() if delDg: dgMod.doIt()
def getInheritance(mayaType, checkManip3D=True, checkCache=True, updateCache=True): """Get parents as a list, starting from the node after dependNode, and ending with the mayaType itself. Raises a ManipNodeTypeError if the node type fed in was a manipulator """ # To get the inheritance post maya2012, we use nodeType(isTypeName=True), # which means we don't need a real node. However, in maya < 2012, nodeType # requires a real node. To do get these without poluting the scene we use the # _GhostObjMaker, which on enter, uses a dag/dg modifier, and calls the doIt # method; we then get the lineage, and on exit, it calls undoIt. global _cachedInheritances if checkCache and mayaType in _cachedInheritances: return _cachedInheritances[mayaType] import maya.cmds as cmds lineage = None if versions.current() >= versions.v2012: # We now have nodeType(isTypeName)! yay! try: lineage = cmds.nodeType(mayaType, isTypeName=True, inherited=True) except RuntimeError: pass else: with _GhostObjMaker(mayaType) as obj: if obj is not None: if obj.hasFn(api.MFn.kDagNode): name = api.MFnDagNode(obj).partialPathName() else: name = api.MFnDependencyNode(obj).name() if not obj.isNull() and not obj.hasFn( api.MFn.kManipulator3D) and not obj.hasFn( api.MFn.kManipulator2D): lineage = cmds.nodeType(name, inherited=1) if lineage is None: global _fixedLineages if not _fixedLineages: if versions.current() >= versions.v2012: controlPoint = cmds.nodeType('controlPoint', isTypeName=True, inherited=True) else: controlPoint = [ u'containerBase', u'entity', u'dagNode', u'shape', u'geometryShape', u'deformableShape', u'controlPoint' ] # maya2013 introduced shadingDependNode... if versions.current() >= versions.v2013: texture2d = ['shadingDependNode', 'texture2d'] else: texture2d = ['texture2d'] # For whatever reason, nodeType(isTypeName) returns # None for the following mayaTypes: _fixedLineages = { 'node': [], 'file': texture2d + [u'file'], 'lattice': controlPoint + [u'lattice'], 'mesh': controlPoint + [u'surfaceShape', u'mesh'], 'nurbsCurve': controlPoint + [u'curveShape', u'nurbsCurve'], 'nurbsSurface': controlPoint + [u'surfaceShape', u'nurbsSurface'], 'time': [u'time'] } if mayaType in _fixedLineages: lineage = _fixedLineages[mayaType] else: raise RuntimeError( "Could not query the inheritance of node type %s" % mayaType) elif checkManip3D and 'manip3D' in lineage: raise ManipNodeTypeError try: assert (mayaType == 'node' and lineage == []) or lineage[-1] == mayaType except Exception: print mayaType, lineage raise if len(set(lineage)) != len(lineage): # cyclical lineage: first discovered with xgen nodes. # might be a result of multiple inheritance being returned strangely by nodeType. # # an example lineage is: # [u'containerBase', u'entity', u'dagNode', u'shape', u'geometryShape', u'locator', u'THlocatorShape', u'SphereLocator', # u'containerBase', u'entity', u'dagNode', u'shape', u'geometryShape', u'locator', u'THlocatorShape', u'aiSkyDomeLight'] # note the repeat - we will try to fix lineages like this, resolving to: # [u'containerBase', u'entity', u'dagNode', u'shape', u'geometryShape', u'locator', u'THlocatorShape', u'SphereLocator', u'aiSkyDomeLight'] # first pop the rightmost element, which is the mayaType... if lineage.pop() != mayaType: raise RuntimeError( "lineage for %s did not end with it's own node type" % mayaType) # then try to find the first element somewhere else - this should indicate the start of the repeated chain... try: nextIndex = lineage.index(lineage[0], 1) except ValueError: # unknown case, don't know how to fix... pass else: firstLineage = lineage[:nextIndex] secondLineage = lineage[nextIndex:] if len(firstLineage) < len(secondLineage): shorter = firstLineage longer = secondLineage else: shorter = secondLineage longer = firstLineage if longer[:len(shorter)] == shorter: # yay! we know how to fix! lineage = longer lineage.append(mayaType) if updateCache and lineage: if len(set(lineage)) != len(lineage): # cyclical lineage: first discovered with xgen nodes. # might be a result of multiple inheritance being returned strangely by nodeType. print mayaType, lineage _logger.raiseLog( _logger.WARNING, "lineage for node %s is cyclical: %s" % (mayaType, lineage)) _cachedInheritances[mayaType] = lineage # don't cache any of the parents return lineage # add not just this lineage, but all parent's lineages as well... for i in xrange(len(lineage), 0, -1): thisLineage = lineage[:i] thisNode = thisLineage[-1] oldVal = _cachedInheritances.get(thisNode) if oldVal is None: _cachedInheritances[thisNode] = thisLineage elif oldVal != thisLineage: _logger.raiseLog( _logger.WARNING, "lineage for node %s changed:\n from %s\n to %s)" % (thisNode, oldVal, thisLineage)) _cachedInheritances[thisNode] = thisLineage return lineage
def getInheritance(mayaType, checkManip3D=True, checkCache=True, updateCache=True): """Get parents as a list, starting from the node after dependNode, and ending with the mayaType itself. Raises a ManipNodeTypeError if the node type fed in was a manipulator """ # To get the inheritance post maya2012, we use nodeType(isTypeName=True), # which means we don't need a real node. However, in maya < 2012, nodeType # requires a real node. To do get these without poluting the scene we use the # _GhostObjMaker, which on enter, uses a dag/dg modifier, and calls the doIt # method; we then get the lineage, and on exit, it calls undoIt. global _cachedInheritances if checkCache and mayaType in _cachedInheritances: return _cachedInheritances[mayaType] import maya.cmds as cmds lineage = None if versions.current() >= versions.v2012: # We now have nodeType(isTypeName)! yay! try: lineage = cmds.nodeType(mayaType, isTypeName=True, inherited=True) except RuntimeError: pass else: with _GhostObjMaker(mayaType) as obj: if obj is not None: if obj.hasFn(api.MFn.kDagNode): name = api.MFnDagNode(obj).partialPathName() else: name = api.MFnDependencyNode(obj).name() if not obj.isNull() and not obj.hasFn( api.MFn.kManipulator3D) and not obj.hasFn( api.MFn.kManipulator2D): lineage = cmds.nodeType(name, inherited=1) if lineage is None: global _fixedLineages if not _fixedLineages: if versions.current() >= versions.v2012: controlPoint = cmds.nodeType('controlPoint', isTypeName=True, inherited=True) else: controlPoint = [ u'containerBase', u'entity', u'dagNode', u'shape', u'geometryShape', u'deformableShape', u'controlPoint' ] # maya2013 introduced shadingDependNode... if versions.current() >= versions.v2013: texture2d = ['shadingDependNode', 'texture2d'] else: texture2d = ['texture2d'] # For whatever reason, nodeType(isTypeName) returns # None for the following mayaTypes: _fixedLineages = { 'node': [], 'file': texture2d + [u'file'], 'lattice': controlPoint + [u'lattice'], 'mesh': controlPoint + [u'surfaceShape', u'mesh'], 'nurbsCurve': controlPoint + [u'curveShape', u'nurbsCurve'], 'nurbsSurface': controlPoint + [u'surfaceShape', u'nurbsSurface'], 'time': [u'time'] } if mayaType in _fixedLineages: lineage = _fixedLineages[mayaType] else: raise RuntimeError( "Could not query the inheritance of node type %s" % mayaType) elif checkManip3D and 'manip3D' in lineage: raise ManipNodeTypeError try: assert (mayaType == 'node' and lineage == []) or lineage[-1] == mayaType except Exception: print mayaType, lineage raise if updateCache and lineage: # add not just this lineage, but all parent's lineages as well... for i in xrange(len(lineage), 0, -1): thisLineage = lineage[:i] thisNode = thisLineage[-1] oldVal = _cachedInheritances.get(thisNode) if oldVal and oldVal != thisLineage: _logger.raiseLog( _logger.WARNING, "lineage for node %s changed (from %s to %s)" % (thisNode, oldVal, thisLineage)) _cachedInheritances[thisNode] = thisLineage return lineage