Exemple #1
0
def create(target=None,
           position=None,
           tag=True,
           setMatchTarget=True,
           pivot='rp',
           mode='fromTarget',
           name=None):
    """
    Return the short name of an object

    :parameters
        :target(str): What to create a loc from
        :tag(bool): Whether to tag for updating or special naming
        :pivot: Whether to force it to be created at the rotatePivot, scalePivot or BoundingBox center
        :mode
            fromTarget -- can be component or transform
            midPoint -- mid point of specfied targets
            closestPointOnTarget -- closest point from source to targets
            closestTarget -- closest target from source
            rayCast -- create a rayCast locator. For more options, see LOCINATOR
            attachPoint -- Create a rayCast follicle, and parent your loc to that.
        :setMatchTarget
    :returns
        short name(str)
    """
    _str_func = "create"

    try:
        if mode == 'rayCast':
            #_catch = mc.group(em=True)
            class rayCastLoc(cgmDrag.clickMesh):
                def release_post_insert(self):
                    #ATTR.set_messagse(_catch, 'tmpMsg', self.l_created[-1])
                    _loc = self.l_created[-1]
                    _mLoc = r9Meta.MetaClass(_loc)
                    _buffer = _mLoc.cgmLocDat
                    _target = ATTR.get_message(_loc, 'meshTarget')[0]
                    _loc = mc.rename(
                        _loc, "{0}_u{1}_v{2}_rayCast_loc".format(
                            coreNames.get_base(_target),
                            "{0:.4f}".format(_buffer['uv'][0]),
                            "{0:.4f}".format(_buffer['uv'][1]),
                        ))
                    self.dropTool()

            rayCastLoc(create='locator')
            #_res = ATTR.get_message(_catch,'tmpMsg')[0]
            return True

        _loc = mc.spaceLocator()[0]

        if position:
            mc.move(position[0], position[1], position[2], _loc, ws=True)
            if name:
                return mc.rename(_loc, name)
            return mc.rename("pos_loc")
        if not target:
            if name:
                return mc.rename(_loc, name)
            return mc.rename(_loc, "world_center_loc")

        _targets = VALID.objStringList(target,
                                       noneValid=False,
                                       calledFrom=__name__ + _str_func +
                                       ">> validate target")
        #_targets = VALID.listArg(target)
        if tag or mode:
            _mi_loc = r9Meta.MetaClass(_loc)
            if not _mi_loc.hasAttr('cgmLocDat'):
                _mi_loc.addAttr('cgmLocDat', attrType='string')

        log.debug("|{0}| >> {1} mode...".format(_str_func, mode))

        if mode in ['fromTarget', 'attachPoint']:
            if len(_targets) != 1:
                log.warning("|{0}| >> mode: {1} | targets: {2} | ".format(
                    _str_func, mode, _targets))
                raise ValueError, "May only have one target for mode: {0} | targets: {1}".format(
                    mode, _targets)

            _target = _targets[0]

            if name:
                _loc = mc.rename(_loc, name)
            else:
                _loc = mc.rename(
                    _loc,
                    "{0}_fromTarget_loc".format(coreNames.get_base(_target)))

            if tag:  #store info
                ATTR.store_info(_loc,
                                'cgmName',
                                coreNames.get_base(_target),
                                attrType='string',
                                lock=True)
                ATTR.store_info(_loc, 'cgmLocMode', 'fromTarget', lock=True)
                ATTR.set_message(_loc, 'cgmLocSource', _target, 'cgmLocDat')
                if not VALID.is_component(_target) and setMatchTarget:
                    SNAP.matchTarget_set(_target, _loc)
                #_d = r9Meta.MetaClass(_loc).cgmLocDat

                _res = update(_loc)
            _res = update(_loc, _target, 'fromTarget')

            if mode == 'attachPoint':

                class follicleAttach(cgmDrag.clickMesh):
                    def release_post_insert(self):
                        _str_funcName = 'follicleAttach.release'
                        """if not self.b_dragStoreMode:#If not on drag, do it here. Otherwise do it on update
                            if self._posBuffer:
                                self.l_return.extend(self._posBuffer)
                                if self._posBufferRaw:
                                    self.l_returnRaw.extend(self._posBufferRaw)
                                else:
                                    self.l_returnRaw.extend(self._posBuffer)
                        
                            if self._createModeBuffer:
                                self.l_created.extend(self._createModeBuffer)"""

                        for pos in self.l_returnRaw:
                            log.debug("|{0}|...pos {1}".format(
                                _str_funcName, pos))
                            for i, m in enumerate(self.d_meshPos.keys()):
                                log.debug("|{0}|...mesh: {1}".format(
                                    _str_funcName, m))
                                for i2, h in enumerate(self.d_meshPos[m]):
                                    if h == pos:
                                        log.debug("Found follicle match!")
                                        try:
                                            _set = [
                                                m, self.d_meshUV[m][i2],
                                                "{0}_u{1}_v{2}".format(
                                                    coreNames.get_short(m),
                                                    "{0:.4f}".format(
                                                        self.d_meshUV[m][i2]
                                                        [0]), "{0:.4f}".format(
                                                            self.d_meshUV[m]
                                                            [i2][1]))
                                            ]
                                            self._l_folliclesToMake.append(
                                                _set)
                                            log.debug("|{0}|...uv {1}".format(
                                                _str_funcName, _set))
                                        except Exception, err:
                                            log.error(
                                                "|{0}| >> Failed to query uv for hit {2} on shape {2} | err:{1}"
                                                .format(
                                                    _str_funcName, err, pos,
                                                    m))
                            if self._l_folliclesToMake:
                                for f_dat in self._l_folliclesToMake:
                                    _follicle = NODES.add_follicle(
                                        f_dat[0], f_dat[2])
                                    log.debug(
                                        "|finalize| >> Follicle created: {0}".
                                        format(_follicle))
                                    ATTR.set(_follicle[0], 'parameterU',
                                             f_dat[1][0])
                                    ATTR.set(_follicle[0], 'parameterV',
                                             f_dat[1][1])
                                    mc.parent(_loc, _follicle[0])
                        mc.delete(self.l_created)
                        self.dropTool()

                follicleAttach()

            return _loc
        elif not _targets:
            raise ValueError, "Must have targets for mode: {0} | targets: {1}".format(
                mode, _targets)
Exemple #2
0
            raise ValueError, "Must have targets for mode: {0} | targets: {1}".format(
                mode, _targets)
        else:
            if len(_targets) <= 2 and mode not in ['midPoint']:
                log.warning("|{0}| >> mode: {1} | targets: {2} | ".format(
                    _str_func, mode, _targets))
                raise ValueError, "Must have more than two targets for mode: {0} | targets: {1}".format(
                    mode, _targets)

            _name = "{0}_{1}_loc".format(
                '_to_'.join([coreNames.get_base(t) for t in _targets]), mode)

            _loc = mc.rename(_loc, _name)

            if tag:
                ATTR.store_info(_loc, 'cgmName', _name, lock=True)
                ATTR.store_info(_loc, 'cgmLocMode', mode, lock=True)
                ATTR.msgList_connect(_loc,
                                     'cgmLocSource',
                                     _targets,
                                     dataAttr='cgmLocDat')

                if not VALID.is_component(_targets[0]) and setMatchTarget:
                    SNAP.matchTarget_set(_targets[0], _loc)

                return update(_loc)
            return update(_loc, _targets, mode)

    except ValueError, err:
        try:
            mc.delete(_loc)