Esempio n. 1
0
def getNamespaceFromObjects(objects):
    """
    :type objects: list[str]
    :rtype: list[str]
    """
    namespaces = [mutils.Node(name).namespace() for name in objects]
    return list(set(namespaces))
Esempio n. 2
0
def ls(*args, **kwargs):
    """
    List all the node objects for the given options.

    :type args: list
    :type kwargs: dict
    """
    return [mutils.Node(name) for name in maya.cmds.ls(*args, **kwargs) or []]
    def mirrorObject(self, srcName):
        """Get the mirror object from the srcName
        
        Arguments:
            srcName {str} -- the src name of the node in the pose
        
        Returns:
            str -- the mirrored node with the same namespace as the srcname
        """
        srcNode = mutils.Node(srcName)
        #look for the object in the scene and not in the pose data
        mirrorName = mirror.mirrorNode('{0}:{1}'.format(
            self._namespace, srcNode.nameOnly()))

        if mirrorName != '':
            #return the object name with the same namespace as the srcname
            mirrorNode = mutils.Node(mirrorName)
            return '{0}:{1}'.format(srcNode.namespace(), mirrorNode.nameOnly())
        return None
Esempio n. 4
0
def groupObjects(objects):
    """
    :type objects:
    :rtype:
    """
    results = {}
    for name in objects:
        node = mutils.Node(name)
        results.setdefault(node.namespace(), [])
        results[node.namespace()].append(name)
    return results
Esempio n. 5
0
def indexObjects(objects):
    """
    :type objects: list[str]
    :rtype: dict
    """
    result = {}
    if objects:
        for name in objects:
            node = mutils.Node(name)
            result.setdefault(node.shortname(), [])
            result[node.shortname()].append(node)
    return result
    def mirrorAxis(self, srcName):
        """get the mirror axis from the node in the scene
        
        Arguments:
            srcName {str} -- node name in the pose
        
        Returns:
            list of float -- the mirrors axis
        """
        srcNode = mutils.Node(srcName)
        factors = mirror.mirrorFactors('{0}:{1}'.format(
            self._namespace, srcNode.nameOnly()))

        #as we have more often rotations than translations we will look at the rotations factors to set the mirror plane
        #and we will flip the values ( and force it to be 1 or -1)
        axis = []
        for factor in factors[3:]:
            if factor < 0:
                axis.append(1)
            else:
                axis.append(-1)
        return axis
Esempio n. 7
0
def matchNames(srcObjects,
               dstObjects=None,
               dstNamespaces=None,
               search=None,
               replace=None):
    """
    :type srcObjects: list[str]
    :type dstObjects: list[str]
    :type dstNamespaces: list[str]
    :rtype: list[(mutils.Node, mutils.Node)]
    """
    results = []
    if dstObjects is None:
        dstObjects = []

    srcGroup = groupObjects(srcObjects)
    srcNamespaces = srcGroup.keys()

    if not dstObjects and not dstNamespaces:  # and not selection:
        dstNamespaces = srcNamespaces

    if not dstNamespaces and dstObjects:
        dstGroup = groupObjects(dstObjects)
        dstNamespaces = dstGroup.keys()

    dstIndex = indexObjects(dstObjects)
    # DESTINATION NAMESPACES NOT IN SOURCE OBJECTS
    dstNamespaces2 = list(set(dstNamespaces) - set(srcNamespaces))

    # DESTINATION NAMESPACES IN SOURCE OBJECTS
    dstNamespaces1 = list(set(dstNamespaces) - set(dstNamespaces2))

    # CACHE DESTINATION OBJECTS WITH NAMESPACES IN SOURCE OBJECTS
    usedNamespaces = []
    notUsedNamespaces = []

    # FIRST LOOP THROUGH ALL DESTINATION NAMESPACES IN SOURCE OBJECTS
    for srcNamespace in srcNamespaces:
        if srcNamespace in dstNamespaces1:
            usedNamespaces.append(srcNamespace)
            for name in srcGroup[srcNamespace]:

                srcNode = mutils.Node(name)

                if search is not None and replace is not None:
                    # Using the mirror table which supports * style replacing
                    name = mutils.MirrorTable.replace(name, search, replace)

                dstNode = mutils.Node(name)

                if dstObjects:
                    dstNode = matchInIndex(dstNode, dstIndex)
                if dstNode:
                    results.append((srcNode, dstNode))
                    yield (srcNode, dstNode)
                else:
                    logger.debug(
                        "Cannot find matching destination object for %s" %
                        srcNode.name())
        else:
            notUsedNamespaces.append(srcNamespace)

    # SECOND LOOP THROUGH ALL OTHER DESTINATION NAMESPACES
    srcNamespaces = notUsedNamespaces
    srcNamespaces.extend(usedNamespaces)
    _index = 0
    for dstNamespace in dstNamespaces2:
        match = False
        i = _index
        for srcNamespace in rotateSequence(srcNamespaces, _index):
            if match:
                _index = i
                break
            i += 1
            for name in srcGroup[srcNamespace]:
                srcNode = mutils.Node(name)
                dstNode = mutils.Node(name)
                dstNode.setNamespace(dstNamespace)

                if dstObjects:
                    dstNode = matchInIndex(dstNode, dstIndex)
                elif dstNamespaces:
                    pass

                if dstNode:
                    match = True
                    results.append((srcNode, dstNode))
                    yield (srcNode, dstNode)
                else:
                    logger.debug(
                        "Cannot find matching destination object for %s" %
                        srcNode.name())

    if logger.parent.level == logging.DEBUG or logger.level == logging.DEBUG:
        for dstNodes in dstIndex.values():
            for dstNode in dstNodes:
                logger.debug("Cannot find matching source object for %s" %
                             dstNode.name())
Esempio n. 8
0
def ls(*args, **kwargs):
    """
    :rtype: list[Node]
    """
    return [mutils.Node(name) for name in maya.cmds.ls(*args, **kwargs) or []]
Esempio n. 9
0
def matchObjects(srcObjects, dstObjects = None, dstNamespaces = None, callback = None, **kwargs):
    """
    # selection=False,
    @type srcObjects: list[str]
    @type dstObjects: list[str]
    @type dstNamespaces: list[str]
    @rtype: list[list[mutils.Node]]
    """
    results = []
    if dstObjects is None:
        dstObjects = []
    srcGroup = groupObjects(srcObjects)
    srcNamespaces = srcGroup.keys()
    if not dstObjects and not dstNamespaces:
        dstNamespaces = srcNamespaces
    if not dstNamespaces and dstObjects:
        dstGroup = groupObjects(dstObjects)
        dstNamespaces = dstGroup.keys()
    dstIndex = indexObjects(dstObjects)
    dstNamespaces2 = list(set(dstNamespaces) - set(srcNamespaces))
    dstNamespaces1 = list(set(dstNamespaces) - set(dstNamespaces2))
    usedNamespaces = []
    notUsedNamespaces = []
    for srcNamespace in srcNamespaces:
        if srcNamespace in dstNamespaces1:
            usedNamespaces.append(srcNamespace)
            for name in srcGroup[srcNamespace]:
                srcNode = mutils.Node(name)
                dstNode = mutils.Node(name)
                if dstObjects:
                    dstNode = matchInIndex(dstNode, dstIndex)
                if dstNode:
                    results.append((srcNode, dstNode))
                    if callback is not None:
                        callback(srcNode, dstNode, **kwargs)
                else:
                    log.debug('Cannot find matching destination object for %s' % srcNode.name())

        else:
            notUsedNamespaces.append(srcNamespace)

    srcNamespaces = notUsedNamespaces
    srcNamespaces.extend(usedNamespaces)
    _index = 0
    for dstNamespace in dstNamespaces2:
        match = False
        i = _index
        for srcNamespace in rotateSequence(srcNamespaces, _index):
            if match:
                _index = i
                break
            i += 1
            for name in srcGroup[srcNamespace]:
                srcNode = mutils.Node(name)
                dstNode = mutils.Node(name)
                dstNode.setNamespace(dstNamespace)
                if dstObjects:
                    dstNode = matchInIndex(dstNode, dstIndex)
                elif dstNamespaces:
                    pass
                if dstNode:
                    match = True
                    results.append((srcNode, dstNode))
                    if callback is not None:
                        callback(srcNode, dstNode, **kwargs)
                else:
                    log.debug('Cannot find matching destination object for %s' % srcNode.name())

    if log.level == log.DEBUG:
        for dstNodes in dstIndex.values():
            for dstNode in dstNodes:
                log.debug('Cannot find matching source object for %s' % dstNode.name())

    return results
Esempio n. 10
0
def matchNames(srcObjects,
               dstObjects=None,
               dstNamespaces=None,
               search=None,
               replace=None):
    """
    :type srcObjects: list[str]
    :type dstObjects: list[str]
    :type dstNamespaces: list[str]
    :rtype: list[(mutils.Node, mutils.Node)]
    """
    results = []
    if dstObjects is None:
        dstObjects = []
    srcGroup = groupObjects(srcObjects)
    srcNamespaces = srcGroup.keys()
    if not dstObjects and not dstNamespaces:
        dstNamespaces = srcNamespaces
    if not dstNamespaces and dstObjects:
        dstGroup = groupObjects(dstObjects)
        dstNamespaces = dstGroup.keys()
    dstIndex = indexObjects(dstObjects)
    dstNamespaces2 = list(set(dstNamespaces) - set(srcNamespaces))
    dstNamespaces1 = list(set(dstNamespaces) - set(dstNamespaces2))
    usedNamespaces = []
    notUsedNamespaces = []
    for srcNamespace in srcNamespaces:
        if srcNamespace in dstNamespaces1:
            usedNamespaces.append(srcNamespace)
            for name in srcGroup[srcNamespace]:
                srcNode = mutils.Node(name)
                if search is not None and replace is not None:
                    name = name.replace(search, replace)
                dstNode = mutils.Node(name)
                if dstObjects:
                    dstNode = matchInIndex(dstNode, dstIndex)
                if dstNode:
                    results.append((srcNode, dstNode))
                    yield (srcNode, dstNode)
                else:
                    logger.debug(
                        'Cannot find matching destination object for %s' %
                        srcNode.name())

        else:
            notUsedNamespaces.append(srcNamespace)

    srcNamespaces = notUsedNamespaces
    srcNamespaces.extend(usedNamespaces)
    _index = 0
    for dstNamespace in dstNamespaces2:
        match = False
        i = _index
        for srcNamespace in rotateSequence(srcNamespaces, _index):
            if match:
                _index = i
                break
            i += 1
            for name in srcGroup[srcNamespace]:
                srcNode = mutils.Node(name)
                dstNode = mutils.Node(name)
                dstNode.setNamespace(dstNamespace)
                if dstObjects:
                    dstNode = matchInIndex(dstNode, dstIndex)
                elif dstNamespaces:
                    pass
                if dstNode:
                    match = True
                    results.append((srcNode, dstNode))
                    yield (srcNode, dstNode)
                else:
                    logger.debug(
                        'Cannot find matching destination object for %s' %
                        srcNode.name())

    if logger.parent.level == logging.DEBUG or logger.level == logging.DEBUG:
        for dstNodes in dstIndex.values():
            for dstNode in dstNodes:
                logger.debug('Cannot find matching source object for %s' %
                             dstNode.name())