Beispiel #1
0
def add_follicle(mesh, name='follicle'):
    """
    Creates named follicle node on a mesh
    
    :parameters:
        mesh(str): Surface to attach to
        name(str): base name for the follicle

    :returns
        [newNode, newTransform]
    """
    _str_func = "add_follicle"

    _node = create(name, 'follicle')

    if SEARCH.is_shape(mesh):
        _surface = mesh
    else:
        _surface = mc.listRelatives(mesh, shapes=True, fullPath=True)[0]
    _type = VALID.get_mayaType(_surface)
    _trans = SEARCH.get_transform(_node)

    attributes.doConnectAttr(
        (_surface + '.worldMatrix[0]'),
        (_node + '.inputWorldMatrix'))  #surface to follicle node
    if _type == 'mesh':
        attributes.doConnectAttr(
            (_surface + '.outMesh'),
            (_node + '.inputMesh'))  #surface mesh to follicle input mesh
    else:
        attributes.doConnectAttr(
            (_surface + '.local'),
            (_node + '.inputSurface'))  #surface mesh to follicle input mesh

    attributes.doConnectAttr((_node + '.outTranslate'),
                             (_trans + '.translate'))
    attributes.doConnectAttr((_node + '.outRotate'), (_trans + '.rotate'))

    #ATTR.set_message(_node,'follTrans',_trans)
    #ATTR.set_message(_trans,'follNode',_node)

    attributes.doSetLockHideKeyableAttr(_trans)

    return [_node, _trans]
    """follicleNode = createNamedNode((name),'follicle')
def get_nonintermediate(shape):
    """
    Get the nonintermediate shape on a transform
    
    :parameters:
        shape(str): Shape to check

    :returns
        non intermediate shape(string)
    """
    _str_func = "get_nonintermediate"
    try:
        if not VALID.is_shape(shape):
            _shapes = mc.listRelatives(shape, fullPath=True)
            _l_matches = []
            for s in _shapes:
                if not ATTR.get(s, 'intermediateObject'):
                    _l_matches.append(s)
            if len(_l_matches) == 1:
                return _l_matches[0]
            else:
                raise ValueError, "Not sure what to do with this many intermediate shapes: {0}".format(
                    _l_matches)
        elif ATTR.get(shape, 'intermediateObject'):
            _type = VALID.get_mayaType(shape)
            _trans = SEARCH.get_transform(shape)
            _shapes = mc.listRelatives(_trans,
                                       s=True,
                                       type=_type,
                                       fullPath=True)
            _l_matches = []
            for s in _shapes:
                if not ATTR.get(s, 'intermediateObject'):
                    _l_matches.append(s)
            if len(_l_matches) == 1:
                return _l_matches[0]
            else:
                raise ValueError, "Not sure what to do with this many intermediate shapes: {0}".format(
                    _l_matches)
        else:
            return shape
    except Exception, err:
        cgmGEN.cgmExceptCB(Exception, err)
Beispiel #3
0
def duplicate_shape(shape):
    """
    mc.duplicate can't duplicate a shape. This provides for it

    :parameters:
    	shape(str): shape to dupliate

    :returns
    	[shape,transform]

    """
    try:
        _str_func = 'duplicate_shape'
        _type = VALID.get_mayaType(shape)
        if _type == 'nurbsCurve':
            _bfr = mc.duplicateCurve(shape)

            parentObj = mc.listRelatives(shape, p=True, fullPath=True)
            mc.delete(mc.parentConstraint(parentObj, _bfr[0]))
            _l_shapes = mc.listRelatives(_bfr[0], s=True, f=True)

            return [_bfr[0]] + _l_shapes
        else:
            log.debug("|{0}|  >> mesh shape assumed...".format(_str_func))
            _transform = SEARCH.get_transform(shape)
            _shapes = mc.listRelatives(_transform, s=True, fullPath=True)
            _idx = _shapes.index(coreNames.get_long(shape))

            _bfr = mc.duplicate(shape)
            _newShapes = mc.listRelatives(_bfr[0], s=True, fullPath=True)
            _dupShape = _newShapes[_idx]
            _newShapes.pop(_idx)
            mc.delete(_newShapes)

            return [_bfr[0], _dupShape]

    except Exception, err:
        pprint.pprint(vars())
        if not SEARCH.is_shape(shape):
            log.error("|{0}|  >> Failure >> Not a shape: {1}".format(
                _str_func, shape))
        raise Exception, "|{0}|  >> failed! | err: {1}".format(_str_func, err)
Beispiel #4
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)
def get_list(context='selection', mType=None, getTransform=False):
    """
    Get contextual data for updating a transform
    
    :parameters
        context(string): 
            selection
            children
            heirarchy
            scene
            buffer
        mType
        getTransform

    :returns
        list(list)
    """
    _str_func = "get_list"
    _l_context = []
    _context = context.lower()

    log.debug("|{0}| >> context: {1} | mType: {2}".format(
        _str_func, _context, mType))

    if _context == 'selection':
        log.debug("|{0}| >> selection mode...".format(_str_func))
        if mType:
            if mType == 'shape':
                _bfr = mc.ls(os=True, shortNames=False)
                for o in _bfr:
                    _l_context.extend(TRANS.shapes_get(o, True))
            else:
                _l_context = mc.ls(os=True,
                                   type=mType,
                                   shortNames=False,
                                   flatten=True)
        else:
            _l_context = mc.ls(os=True, shortNames=False, flatten=True)
    elif _context == 'scene':
        log.debug("|{0}| >> scene mode...".format(_str_func))
        if mType is not None:
            _l_context = mc.ls(type=mType, shortNames=False)
        else:
            _l_context = mc.ls(shortNames=False)
            #raise Exception,"Really shouldn't use this without a specific object type..."

    elif _context == 'children':
        log.debug("|{0}| >> children mode...".format(_str_func))

        _sel = mc.ls(sl=True, shortNames=False)
        for o in _sel:
            if mType:
                if mc.ls(o, type=mType, shortNames=False):
                    _l_context.append(o)
            else:
                _l_context.append(o)

            if mType:
                try:
                    _buffer = mc.listRelatives(
                        o, allDescendents=True, type=mType,
                        fullPath=True) or []
                except:
                    _buffer = []
            else:
                _buffer = mc.listRelatives(
                    o, allDescendents=True, fullPath=True) or []
            for o2 in _buffer:
                if o2 not in _l_context:
                    _l_context.append(o2)

    elif _context == 'heirarchy':
        log.debug("|{0}| >> heirarchy mode...".format(_str_func))

        _sel = mc.ls(sl=True, shortNames=False)
        for o in _sel:
            if mType:
                if mc.ls(o, type=mType, shortNames=False):
                    _l_context.append(o)
            else:
                _l_context.append(o)

            _parents = SEARCH.get_all_parents(o)
            _buffer = []
            if _parents:
                root = _parents[-1]  #...get the top of the tree
                if mType:
                    if mc.ls(root, type=mType, shortNames=False):
                        _l_context.append(root)
                else:
                    _l_context.append(root)

                if mType:
                    try:
                        _buffer = mc.listRelatives(root,
                                                   allDescendents=True,
                                                   type=mType,
                                                   fullPath=True) or []
                    except:
                        _buffer = []
                else:
                    _buffer = mc.listRelatives(
                        root, allDescendents=True, fullPath=True) or []
                for o2 in _buffer:
                    if o2 not in _l_context:
                        _l_context.append(o2)
            else:
                if mType:
                    try:
                        _buffer = mc.listRelatives(
                            o, allDescendents=True, type=mType,
                            fullPath=True) or []
                    except:
                        _buffer = []
                else:
                    _buffer = mc.listRelatives(
                        o, allDescendents=True, fullPath=True) or []
                for o2 in _buffer:
                    if o2 not in _l_context:
                        _l_context.append(o2)

    else:
        log.warning("|{0}| >> context unkown: {1}...".format(
            _str_func, _context))
        return False

    if getTransform:
        for i, o in enumerate(_l_context):
            _trans = SEARCH.get_transform(o)
            if _trans:
                _l_context[i] = _trans
    return _l_context