def _onNameChangedCB(node, prevName, data): mfnnode = mapi.MFnDependencyNode(node) typeName = mfnnode.typeName() # we are not interested in renamed shapes or so if (not typeName == "transform") or (not typeName == "displayLayer"): return newName = str(mfnnode.name()) if "#" in newName: # those are only temporary name changes to create numbers return if newName.startswith("__"): # temporary duplicate or import names return _lg.debug("maya changed name to %s" % newName) #print "type is %s" % typeName if prevName == newName: #nothing changes really return # _onNameChangedTransformNode(newName, prevName, data) #elif typeName == "displayLayer": # _onNameChangedDisplayLayer(newName, prevName, data) #def _onNameChangedTransformNode(newName, prevName, data): # """ called whenever a transform-node's name was changed # """ # TODO: delegate the name-finding functionality to a common function for # this and the duplicate callback mName = newName # maya's Name uName = "" # Engine's Name # disable object syncing so internal renames won't trigger a new rename callback wasObjectSyncing = m2u.core.getProgram().isObjectSyncing() m2u.core.getProgram().setObjectSyncing(False) while True: uName = m2u.core.getEditor().getFreeName(mName) _lg.debug("Editor returned '" + uName + "' as a free name.") #if uName is None: return if uName != mName: _lg.debug("Name '%s' already in use, need to find a new one." % mName) mName = str(pm.rename(mName, uName)) if uName == mName: # not 'else', because mName may have changed break m2u.core.getProgram().setObjectSyncing(wasObjectSyncing) code, edName = m2u.core.getEditor().renameObject(prevName, mName) if code is True: # no problems occured return else: if edName is None: # error, no renaming took place, the object was not found or so return # if we end up here, the editor returned a different name than we desired # this should not happen since we "getFreeName" beforehand _lg.error( "Renaming failed, maya object %s and engine object %s are now desynced" % (mName, edName))
def _on_name_changed_cb(node, prev_name, data): mfnnode = mapi.MFnDependencyNode(node) type_name = mfnnode.typeName() # We are not interested in renamed shapes or so if (not type_name == "transform"): # _lg.debug("Not tracking objects of type: %s" % type_name ) return new_name = str(mfnnode.name()) if "#" in new_name: # Those are only temporary name changes to create numbers. return if new_name.startswith("__"): # Temporary duplicate or import names. return _lg.debug("maya changed name to %s" % new_name) if prev_name == new_name: # Nothing changes really return # TODO: delegate the name-finding functionality to a common function for # this and the duplicate callback m_name = new_name # maya's Name ed_name = "" # Engine's Name # Disable object syncing so internal renames won't trigger a new # rename callback. backup_sync_state = get_object_syncing_state() set_object_syncing_state(name=False) while True: ed_name = m2u.core.editor.get_free_name(m_name) _lg.debug("Editor returned '{0}' as a free name.".format(ed_name)) if ed_name != m_name: _lg.debug("Name '{0}' already in use, Maya needs to find" " a new one.".format(m_name)) m_name = str(pm.rename(m_name, ed_name)) if ed_name == m_name: # not 'else', because m_name may have changed break set_object_syncing_state(**backup_sync_state) code, ed_name = m2u.core.editor.rename_object(prev_name, m_name) if code is True: # No problems occured return else: if ed_name is None: # error, no renaming took place, the object was not found or so return # If we end up here, the editor returned a different name than we # desired this should not happen since we "getFreeName" beforehand _lg.error("Renaming failed, maya object '{0}' and engine object '{1}' " "are now desynced.".format(m_name, ed_name))
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 _on_object_deleted_cb(node, data): """ Called everytime a (transform) node is deleted. """ mfnnode = mapi.MFnDependencyNode(node) name = str(mfnnode.name()) _lg.debug("maya deleted object %s" % name) m2u.core.editor.delete_object(name)
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
def _onObjectDeletedCB(node, data): """ called everytime a node is deleted """ mfnnode = mapi.MFnDependencyNode(node) name = str(mfnnode.name()) _lg.debug("maya deleted object %s" % name) m2u.core.getEditor().deleteObject(name)