Ejemplo n.º 1
0
def get_closestTarget(source=None, objects=None):
    """
    Get the closest object to a give source
    
    :parameters:
        source(str/vector) -- source point or object
        targetSurface -- surface to check transform, nurbsSurface, curve, mesh supported
        loc -- whether to loc point found

    :returns
        position, distance, shape (list)
    """
    _str_func = 'get_closestTarget'
    _point = False

    if VALID.vectorArg(source) is not False:
        _point = source
    elif mc.objExists(source):
        _point = POS.get(source)

    if not _point: raise ValueError, "Must have point of reference"

    l_dists = []
    for obj in objects:
        pos = POS.get(obj)
        l_dists.append(get_distance_between_points(_point, pos))
    return objects[(l_dists.index((min(l_dists))))]
Ejemplo n.º 2
0
def get_targetsOrderedByDist(source=None, objects=None):
    """
    Get the closest object to a give source
    
    :parameters:
        source(str/vector) -- source point or object
        targetSurface -- surface to check transform, nurbsSurface, curve, mesh supported

    :returns
        [[obj,dist],...]
    """
    _str_func = 'get_closestTarget'
    _point = False

    if VALID.vectorArg(source) is not False:
        _point = source
    elif mc.objExists(source):
        _point = POS.get(source)

    if not _point: raise ValueError, "Must have point of reference"

    l_dists = []
    d_objs = {}
    for obj in objects:
        pos = POS.get(obj)
        _d = get_distance_between_points(_point, pos)
        if _d in l_dists:
            raise ValueError, "Cannot handle matching distances. {0}".format(
                _str_func)
        l_dists.append(_d)
        d_objs[_d] = obj

    l_dists.sort()
    return [[d_objs[d], d] for d in l_dists]
Ejemplo n.º 3
0
def create_closest_point_node(source=None,
                              targetSurface=None,
                              singleReturn=False):
    """
    Create a closest point on surface node and wire it
    
    :parameters:
        source(str/vector) -- source point or object
        targetSurface -- surface to check transform, nurbsSurface, curve, mesh supported
        singleReturn - only return single return if we have
    :returns
        node(list)
    """
    try:
        _str_func = 'create_closest_point_node'
        _transform = False

        if VALID.vectorArg(source) is not False:
            _transform = mc.spaceLocator(n='closest_point_source_loc')[0]
            POS.set(_transform, source)
        elif mc.objExists(source):
            if SEARCH.is_transform(source):
                _transform = source
            elif VALID.is_component(source):
                _transform = mc.spaceLocator(
                    n='{0}_loc'.format(NAMES.get_base(source)))[0]
                POS.set(_transform, POS.get(source))
            else:
                _transform = SEARCH.get_transform(source)

        if not _transform: raise ValueError, "Must have a transform"

        if SEARCH.is_shape(targetSurface):
            l_shapes = [targetSurface]
        else:
            l_shapes = mc.listRelatives(targetSurface, s=True, fullPath=True)

        if not l_shapes:
            raise ValueError, "Must have shapes to check."

        _nodes = []
        _locs = []
        _types = []
        _shapes = []

        for s in l_shapes:
            _type = VALID.get_mayaType(s)

            if _type not in ['mesh', 'nurbsSurface', 'nurbsCurve']:
                log.error(
                    "|{0}| >> Unsupported target surface type. Skipping: {1} |{2} "
                    .format(_str_func, s, _type))
                continue

            _loc = mc.spaceLocator()[0]
            _res_loc = mc.rename(
                _loc, '{0}_to_{1}_result_loc'.format(NAMES.get_base(source),
                                                     NAMES.get_base(s)))
            _locs.append(_res_loc)
            _types.append(_type)
            _shapes.append(s)

            if _type == 'mesh':
                _node = mc.createNode('closestPointOnMesh')
                _node = mc.rename(
                    _node, "{0}_to_{1}_closePntMeshNode".format(
                        NAMES.get_base(source), NAMES.get_base(s)))
                ATTR.connect((_transform + '.translate'),
                             (_node + '.inPosition'))
                ATTR.connect((s + '.worldMesh'), (_node + '.inMesh'))
                ATTR.connect((s + '.worldMatrix'), (_node + '.inputMatrix'))

                _pos = ATTR.get(_node, 'position')
                ATTR.connect((_node + '.position'), (_res_loc + '.translate'))

                _nodes.append(_node)

            elif _type == 'nurbsSurface':
                closestPointNode = mc.createNode('closestPointOnSurface')
                closestPointNode = mc.rename(
                    closestPointNode, "{0}_to_{1}_closePntSurfNode".format(
                        NAMES.get_base(source), NAMES.get_base(s)))

                mc.connectAttr((_transform + '.translate'),
                               (closestPointNode + '.inPosition'))

                #attributes.doSetAttr(closestPointNode,'inPositionX',_point[0])
                #attributes.doSetAttr(closestPointNode,'inPositionY',_point[1])
                #attributes.doSetAttr(closestPointNode,'inPositionZ',_point[2])

                ATTR.connect((s + '.worldSpace'),
                             (closestPointNode + '.inputSurface'))

                ATTR.connect((closestPointNode + '.position'),
                             (_res_loc + '.translate'))
                _nodes.append(closestPointNode)

            elif _type == 'nurbsCurve':
                _node = mc.createNode('nearestPointOnCurve')
                _node = mc.rename(
                    _node, "{0}_to_{1}_nearPntCurveNode".format(
                        NAMES.get_base(source), NAMES.get_base(s)))

                p = []
                distances = []
                mc.connectAttr((_transform + '.translate'),
                               (_node + '.inPosition'))
                mc.connectAttr((s + '.worldSpace'), (_node + '.inputCurve'))

                ATTR.connect((_node + '.position'), (_res_loc + '.translate'))
                _nodes.append(_node)

        if not singleReturn:
            return _locs, _nodes, _shapes, _types

        _l_distances = []
        pos_base = POS.get(_transform)
        for i, n in enumerate(_nodes):
            p2 = POS.get(_locs[i])
            _l_distances.append(get_distance_between_points(pos_base, p2))

        if not _l_distances:
            raise ValueError, "No distance value found"
        closest = min(_l_distances)
        _idx = _l_distances.index(closest)

        for i, n in enumerate(_nodes):
            if i != _idx:
                mc.delete(n, _locs[i])

        return _locs[_idx], _nodes[_idx], _shapes[_idx], _types[_idx]
    except Exception, err:
        cgmGen.cgmExceptCB(Exception, err)
Ejemplo n.º 4
0
def get_closest_point(source=None, targetSurface=None, loc=False):
    """
    Get the closest point on a target surface/curve/mesh to a given point or object.
    Evaluates to all sub shapes to get closest point for multi shape targets.
    
    :parameters:
        source(str/vector) -- source point or object
        targetSurface -- surface to check transform, nurbsSurface, curve, mesh supported
        loc -- whether to loc point found

    :returns
        position, distance, shape (list)
    """
    _str_func = 'get_closest_point'
    _point = False
    if VALID.vectorArg(source) is not False:
        _point = source
    elif mc.objExists(source):
        _point = POS.get(source)

    if not _point: raise ValueError, "Must have point of reference"
    _loc = mc.spaceLocator(n='get_closest_point_loc')[0]
    POS.set(_loc, _point)

    if SEARCH.is_shape(targetSurface):
        _shapes = [targetSurface]
    elif VALID.is_component(targetSurface):
        _shapes = mc.listRelatives(VALID.get_component(targetSurface)[1],
                                   s=True,
                                   fullPath=True)
    else:
        _shapes = mc.listRelatives(targetSurface, s=True, fullPath=True)

    if not _shapes:
        log.error("|{0}| >> No shapes found. Skipping: {1}".format(
            _str_func, targetSurface))
        mc.delete(_loc)
        return False

    _l_res_positions = []
    _l_res_shapes = []
    _l_res_distances = []

    for s in _shapes:
        _type = VALID.get_mayaType(s)

        if _type not in ['mesh', 'nurbsSurface', 'nurbsCurve']:
            log.error(
                "|{0}| >> Unsupported target surface type. Skipping: {1} |{2} | {3}"
                .format(_str_func, s, _type))
            _l_res_positions.append(False)
            continue

        if _type == 'mesh':
            _node = mc.createNode('closestPointOnMesh')

            ATTR.connect((_loc + '.translate'), (_node + '.inPosition'))
            ATTR.connect((s + '.worldMesh'), (_node + '.inMesh'))
            ATTR.connect((s + '.worldMatrix'), (_node + '.inputMatrix'))

            _pos = ATTR.get(_node, 'position')
            _tmpLoc = mc.spaceLocator(n='tmp')[0]
            ATTR.connect((_node + '.position'), (_tmpLoc + '.translate'))

            _l_res_positions.append(POS.get(_tmpLoc))
            mc.delete(_node)
            mc.delete(_tmpLoc)

        elif _type == 'nurbsSurface':
            closestPointNode = mc.createNode('closestPointOnSurface')

            ATTR.set(closestPointNode, 'inPositionX', _point[0])
            ATTR.set(closestPointNode, 'inPositionY', _point[1])
            ATTR.set(closestPointNode, 'inPositionZ', _point[2])

            ATTR.connect((s + '.worldSpace'),
                         (closestPointNode + '.inputSurface'))
            _l_res_positions.append(ATTR.get(closestPointNode, 'position'))
            mc.delete(closestPointNode)

        elif _type == 'nurbsCurve':
            _node = mc.createNode('nearestPointOnCurve')
            p = []
            distances = []
            mc.connectAttr((_loc + '.translate'), (_node + '.inPosition'))
            mc.connectAttr((s + '.worldSpace'), (_node + '.inputCurve'))
            p = [
                mc.getAttr(_node + '.positionX'),
                mc.getAttr(_node + '.positionY'),
                mc.getAttr(_node + '.positionZ')
            ]
            _l_res_positions.append(p)
            mc.delete(_node)

    mc.delete(_loc)

    if not _l_res_positions:
        raise ValueError, "No positions found"

    for p in _l_res_positions:
        if p:
            _l_res_distances.append(get_distance_between_points(_point, p))
        else:
            _l_res_distances.append('no')
    closest = min(_l_res_distances)
    _idx = _l_res_distances.index(closest)

    _pos = _l_res_positions[_idx]
    if not _pos:
        return False
        #raise ValueError,"Failed to find point"

    if loc:
        _loc = mc.spaceLocator(n='get_closest_point_loc')[0]
        POS.set(_loc, _pos)

    return _pos, _l_res_distances[_idx], _shapes[_idx]