예제 #1
0
    def setup_anim_offsets(self, ctrl=None):
        """Setup anim offset controls.

        This sets up anim offset, multiply and time attributes and connects
        any anim curves in the reference to have the input and the anim
        time.

        Args:
            ctrl (HFnTransform): node to add attrs to
        """
        from maya_psyhive import open_maya as hom

        _ctrl = ctrl or self.find_top_node()
        _offs = _ctrl.create_attr('animOffset', 0.0)
        _mult = _ctrl.create_attr('animMult', 1.0)
        _t_anim = _ctrl.create_attr('animTime', 0.0)
        _t_mult = hom.HPlug('time1.outTime').multiply_node(_mult)
        _t_mult.add_node(_offs, output=_t_anim)

        # Connect anim
        for _crv in self.find_nodes(class_=hom.HFnAnimCurve,
                                    type_='animCurve'):
            _t_anim.connect(_crv.input)

        return _t_anim, _offs, _mult
예제 #2
0
    def get_plug(self):
        """Get plug which this curve is driving.

        Returns:
            (HPlug): plug being driven
        """
        from maya_psyhive import open_maya as hom
        return hom.HPlug(self.get_plugs())
예제 #3
0
    def get_plug(self, plug):
        """Get plug within this reference.

        Args:
            plug (str|HPlug): plug name

        Returns:
            (HPlug): plug mapped to this reference's namespace
        """
        from maya_psyhive import open_maya as hom
        return hom.HPlug(self.get_attr(plug))
예제 #4
0
def _get_next_idx(plug, connected=True, value=True, limit=1000, verbose=0):
    """Get next avaliable index for mult indexed attributes.

    In this case there seems to be a strange bug where if the attribute
    is queried then maya/arnold fills it with a weird unicode value. This
    value is idenfied by trying to convert to str in which cases it raises
    a RuntimeError, identifying that attr as available.

    Args:
        plug (HPlug): plug to read
        connected (bool): check for connected attributes
        value (bool): check for attributes with values assigned
        limit (int): number of attrs to search
        verbose (int): print process data

    Returns:
        (HPlug): next free index
    """
    _plug = None
    for _idx in range(limit):

        _plug = hom.HPlug('{}[{:d}]'.format(plug, _idx))
        lprint('TESTING', _plug, verbose=verbose)

        # Test for connection
        if connected and _plug.list_connections(destination=False):
            lprint(' - USED BY CONNECTION', verbose=verbose)
            continue

        # Test for assigned value
        if value:
            try:
                _val = str(_plug.get_attr())
            except (UnicodeEncodeError, RuntimeError):
                pass
            else:
                lprint(' - HAS NON-UNICODE VAL ASSIGNED',
                       _val,
                       verbose=verbose)
                continue

        lprint(' - NO CONNECTION OR VAL', verbose=verbose)
        return _plug

    raise ValueError
예제 #5
0
    def find_plugs(self, filter_=None, keyable=True):
        """Find plugs on this node.

        Args:
            filter_ (str): filter by attr name
            keyable (bool): search for keyable plugs

        Returns:
            (HPlug list): list of plugs
        """
        from maya_psyhive import open_maya as hom
        _plugs = []
        for _attr in (cmds.listAttr(self, keyable=keyable) or []):
            if not passes_filter(_attr, filter_):
                continue
            _plug = hom.HPlug('{}.{}'.format(self, _attr))
            _plugs.append(_plug)
        return _plugs
예제 #6
0
    def apply_texture(self, path):
        """Apply a texture file to this shader's main col attr.

        Args:
            path (str): texture file to apply
        """
        _file = get_single(cmds.listConnections(self.col_attr,
                                                destination=False,
                                                type='file'),
                           catch=True)
        if not _file:
            assert not cmds.listConnections(self.col_attr, destination=False)
            _file = hom.CMDS.shadingNode('file', asShader=True)
            cmds.connectAttr(_file + '.outColor', self.col_attr)
        cmds.setAttr(_file + '.fileTextureName', path, type='string')

        # Apply colspace
        _col_space = hom.HPlug(_file + '.colorSpace')
        for _space in ['Raw', 'linear']:
            _col_space.set_val(_space)
            if _col_space.get_val() == _space:
                break

        return _file
예제 #7
0
def get_selected(type_=None, class_=None, multi=False, verbose=1):
    """Get selected node.

    Unless the multi flag is using, this will error if there isn't
    exactly one selected node matched.

    Args:
        type_ (str): filter nodes by type
        class_ (class): only return nodes that cast to this class
        multi (bool): return multiple nodes
        verbose (int): print process data

    Returns:
        (HFnDependencyNode): matching node
        (HFnDependencyNode list): matching nodes (if multi flag used)
        (HFnPlug|HPlug list): if class_ is HPlug
    """
    from maya_psyhive import open_maya as hom

    # Build list of selected nodes
    _results = []
    for _node in hom.CMDS.ls(selection=True):

        _result = _node
        _type = _node.object_type()
        lprint('TESTING', _node, verbose=verbose > 1)

        # Map transforms to HFnTransform
        if _type == 'transform':
            _result = hom.HFnTransform(str(_node))

        # Apply type filter
        if type_:
            if type_ != 'transform' and _type == 'transform' and _result.shp:
                _type = _result.shp.object_type()
                lprint(' - SHAPE TYPE', _type, verbose=verbose > 1)
            if not _type == type_:
                lprint(' - REJECTED', type_, _type, verbose=verbose > 1)
                continue

        if class_ is hom.HPlug:
            for _attr in cmds.channelBox('mainChannelBox',
                                         query=True,
                                         selectedMainAttributes=True) or []:
                _plug = hom.HPlug('{}.{}'.format(_node, _attr))
                _results.append(_plug)
            continue
        elif class_:
            try:
                _result = class_(str(_node))
            except ValueError:
                lprint(' - CLASS FAIL', class_, verbose=verbose > 1)
                continue

        lprint(' - ADDED', verbose=verbose > 1)
        _results.append(_result)

    # Get result
    if multi:
        return _results
    return get_single(_results, name='selected object', verbose=verbose)