def _hasFn(self, apiType, dagMod, dgMod, parentType=None) : """ Get the Maya API type from the name of a Maya type """ if parentType is None : parentType = 'kBase' # Reserved we can't determine it as we can't create the node, all we can do is check if it's # in the self.reservedApiHierarchy if self.reservedApiTypes.has_key(apiType) : return self.reservedApiHierarchy.get(apiType, None) == parentType # Need the MFn::Types enum for the parentType if self.apiTypesToApiEnums.has_key(parentType) : typeInt = self.apiTypesToApiEnums[parentType] else : return False # print "need creation for %s" % apiType obj = self._getMObject(apiType, dagMod, dgMod, parentType) if api.isValidMObject(obj) : return obj.hasFn(typeInt) else : return False
def _makeDgModGhostObject(mayaType, dagMod, dgMod): # we create a dummy object of this type in a dgModifier (or dagModifier) # as the dgModifier.doIt() method is never called, the object # is never actually created in the scene # Note: at one point, if we didn't call the dgMod/dagMod.deleteNode method, # and we call this function while loading a scene (for instance, if the scene requires # a plugin that isn't loaded, and defines custom node types), then the nodes were still # somehow created, despite never explicitly calling doIt()... # ... however, this seems to no longer be the case, and the deleteNode calls are apparently # harmful if type(dagMod) is not api.MDagModifier or type(dgMod) is not api.MDGModifier : raise ValueError, "Need a valid MDagModifier and MDGModifier or cannot return a valid MObject" # Regardless of whether we're making a DG or DAG node, make a parent first - # for some reason, this ensures good cleanup (don't ask me why...??) parent = dagMod.createNode ( 'transform', api.MObject()) try: try : # Try making it with dgMod FIRST - this way, we can avoid making an # unneccessary transform if it is a DG node obj = dgMod.createNode ( mayaType ) except RuntimeError: # DagNode obj = dagMod.createNode ( mayaType, parent ) _logger.debug( "Made ghost DAG node of type '%s'" % mayaType ) else: # DependNode _logger.debug( "Made ghost DG node of type '%s'" % mayaType ) # dgMod.deleteNode(obj) except: obj = api.MObject() # dagMod.deleteNode(parent) if api.isValidMObject(obj) : return obj else : _logger.debug("Error trying to create ghost node for '%s'" % mayaType) return None
def mayaTypeToApiType(mayaType) : """ Get the Maya API type from the name of a Maya type """ try: return apiCache.mayaTypesToApiTypes[mayaType] except KeyError: apiType = 'kInvalid' # Reserved types must be treated specially if apiCache.reservedMayaTypes.has_key(mayaType) : # It's an abstract type apiType = apiCache.reservedMayaTypes[mayaType] else : # we create a dummy object of this type in a dgModifier # as the dgModifier.doIt() method is never called, the object # is never actually created in the scene obj = api.MObject() dagMod = api.MDagModifier() dgMod = api.MDGModifier() obj = _makeDgModGhostObject(mayaType, dagMod, dgMod) if api.isValidMObject(obj): apiType = obj.apiTypeStr() return apiType
# Regardless of whether we're making a DG or DAG node, make a parent first - # for some reason, this ensures good cleanup (don't ask me why...??) parent = dagMod.createNode ( 'transform', api.MObject()) try : # DependNode obj = dgMod.createNode ( mayaType ) except RuntimeError: # DagNode try: obj = dagMod.createNode ( mayaType, parent ) except Exception, err: _logger.debug("Error trying to create ghost node for '%s': %s" % (mayaType, err)) return None if api.isValidMObject(obj) : return obj else : _logger.debug("Error trying to create ghost node for '%s'" % mayaType) return None class InvalidNodeTypeError(Exception): pass class ManipNodeTypeError(InvalidNodeTypeError): pass class _GhostObjMaker(object): '''Context used to get an mobject which we can query within this context. Automatically does any steps need to create and destroy the mobj within the context (Note - None may be returned in the place of any mobj)
# for some reason, this ensures good cleanup (don't ask me why...??) parent = dagMod.createNode('transform', api.MObject()) try: # DependNode obj = dgMod.createNode(mayaType) except RuntimeError: # DagNode try: obj = dagMod.createNode(mayaType, parent) except Exception, err: _logger.debug("Error trying to create ghost node for '%s': %s" % (mayaType, err)) return None if api.isValidMObject(obj): return obj else: _logger.debug("Error trying to create ghost node for '%s'" % mayaType) return None class InvalidNodeTypeError(Exception): pass class ManipNodeTypeError(InvalidNodeTypeError): pass class _GhostObjMaker(object):
def _parentFn(self, apiType, dagMod, dgMod, *args, **kwargs) : """ Checks the given API type list, or API type:MObject dictionnary to return the first parent of apiType """ if not kwargs : if not args : args = ('kBase', ) kwargs = dict( (args[k], None) for k in args ) else : for k in args : if not kwargs.has_key(k) : kwargs[k] = None # Reserved we can't determine it as we can't create the node, all we can do is check if it's # in the self.reservedApiHierarchy if self.reservedApiTypes.has_key(apiType) : p = self.reservedApiHierarchy.get(apiType, None) if p is not None : for t in kwargs.keys() : if p == t : return t return None result = None obj = kwargs.get(apiType, None) if not api.isValidMObject(obj) : # print "need creation for %s" % apiType obj = self._getMObject(apiType, dagMod, dgMod) if api.isValidMObject(obj) : if not kwargs.get(apiType, None) : kwargs[apiType] = obj # update it if we had to create parents = [] for t in kwargs.keys() : # Need the MFn::Types enum for the parentType if t != apiType : if self.apiTypesToApiEnums.has_key(t) : ti = self.apiTypesToApiEnums[t] if obj.hasFn(ti) : parents.append(t) # problem is the MObject.hasFn method returns True for all ancestors, not only first one if len(parents) : if len(parents) > 1 : for p in parents : if self.apiTypesToApiEnums.has_key(p) : ip = self.apiTypesToApiEnums[p] isFirst = True for q in parents : if q != p : stored = kwargs.get(q, None) if not stored : if self.reservedApiTypes.has_key(q) : isFirst = not self.reservedApiHierarchy.get(q, None) == p else : stored = self._getMObject(q, dagMod, dgMod) if not kwargs.get(q, None) : kwargs[q] = stored # update it if we had to create if stored : isFirst = not stored.hasFn(ip) if not isFirst : break if isFirst : result = p break else : result = parents[0] return result