Exemple #1
0
def alongLine(objList=None, mode='even', curve='linear', spans=2):
    """    
    Arrange a list of objects evenly along a vector from first to last
    
    :parameters:
        objList(list): objects to layout
        mode(string)
            'even' - evenly distributed along line
            'spaced' - distribute along line as close as possible to current position

    :returns
        list of constraints(list)
    """
    _str_func = 'onLine'
    objList = VALID.mNodeStringList(objList)
    log.info("|{0}| >> ObjList: {1} ".format(_str_func, objList))
    _len = len(objList)
    if _len < 3:
        raise ValueError, "|{0}| >> Need at least 3 objects".format(_str_func)

    _pos_start = POS.get(objList[0])
    _pos_end = POS.get(objList[-1])
    curveBuffer = []
    if curve == 'linear':
        if mode != 'even':
            curveBuffer = mc.curve(d=1, ep=[_pos_start, _pos_end])
    elif curve in ['cubic', 'cubicRebuild']:
        l_pos = [POS.get(o) for o in objList]
        knot_len = len(l_pos) + 3 - 1
        crv1 = mc.curve(d=3,
                        ep=l_pos,
                        k=[i for i in range(0, knot_len)],
                        os=True)
        curveBuffer = [crv1]
        if curve == 'cubicRebuild':
            curveBuffer.append(
                mc.rebuildCurve(crv1,
                                ch=0,
                                rpo=0,
                                rt=0,
                                end=1,
                                kr=0,
                                kcp=0,
                                kep=1,
                                kt=0,
                                s=spans,
                                d=3,
                                tol=0.001)[0])

    elif curve == 'cubicArc':
        _mid = MATH.get_midIndex(_len)
        log.info("|{0}| >> cubicArc | mid: {1} ".format(_str_func, _mid))

        l_pos = [POS.get(o) for o in [objList[0], objList[_mid], objList[-1]]]
        knot_len = len(l_pos) + 3 - 1
        curveBuffer = mc.curve(d=3,
                               ep=l_pos,
                               k=[i for i in range(0, knot_len)],
                               os=True)
    else:
        raise ValueError, "|{0}| >>unknown curve setup: {1}".format(
            _str_func, curve)

    if mode == 'even':
        if curve == 'linear':
            _vec = MATH.get_vector_of_two_points(_pos_start, _pos_end)
            _offsetDist = DIST.get_distance_between_points(
                _pos_start, _pos_end) / (_len - 1)
            _l_pos = [
                DIST.get_pos_by_vec_dist(_pos_start, _vec, (_offsetDist * i))
                for i in range(_len)
            ]
            log.info("|{0}| >> offset: {1} ".format(_str_func, _offsetDist))
            log.info("|{0}| >> l_pos: {1} ".format(_str_func, _l_pos))
            for i, o in enumerate(objList[1:-1]):
                POS.set(o, _l_pos[i + 1])
        else:
            _l_pos = CURVES.getUSplitList(curveBuffer, points=_len, rebuild=1)

        for i, o in enumerate(objList[1:-1]):
            POS.set(o, _l_pos[i + 1])

    elif mode == 'spaced':
        _l_pos = []
        for i, o in enumerate(objList[1:-1]):
            #SNAP.go(o,curveBuffer,pivot= 'closestPoint')
            p = DIST.get_by_dist(o, curveBuffer, resMode='pointOnSurface')
            POS.set(o, p)
            _l_pos.append(p)
    else:
        try:
            raise ValueError, "{0} >> mode not supported: {1}".format(
                sys._getframe().f_code.co_name, mode)
        except:
            raise ValueError, "mode not supported: {0}".format(mode)

    if curveBuffer:
        mc.delete(curveBuffer)
    return _l_pos
Exemple #2
0
def ik_rp(self,
          mStart,
          mEnd,
          ml_ikFrame=None,
          mIKControl=None,
          mIKBaseControl=None,
          mIKHandleDriver=None,
          mRoot=None,
          mIKGroup=None,
          mIKControlEnd=None,
          ml_ikFullChain=None):
    try:
        _str_func = "ik_rp"
        log.debug("|{0}| >> {1}...".format(_str_func, _str_func) + '-' * 60)

        mRigNull = self.mRigNull
        mBlock = self.mBlock

        if not ml_ikFrame:
            ml_ikFrame = self.ml_handleTargetsCulled
        if not mIKControl:
            raise ValueError, "Must have mIKControl"

        if not mIKHandleDriver:
            raise ValueError, "Must have mIKHandleDriver"
        if not mRoot:
            raise ValueError, "Must have mRoot"

        log.debug("|{0}| >> rp setup...".format(_str_func))
        mIKMid = mRigNull.controlIKMid
        str_ikEnd = mBlock.getEnumValueString('ikEnd')
        #Measture ======================================================
        log.debug("|{0}| >> measure... ".format(_str_func) + '-' * 30)

        res_ikScale = self.UTILS.get_blockScale(
            self, '{0}_ikMeasure'.format(self.d_module['partName'], ),
            ml_ikFrame)

        mPlug_masterScale = res_ikScale[0]
        mMasterCurve = res_ikScale[1]
        mMasterCurve.p_parent = mRoot
        self.fnc_connect_toRigGutsVis(mMasterCurve)
        mMasterCurve.dagLock(True)

        #Unparent the children from the end while we set stuff up...
        log.debug("|{0}| >> end unparent ...".format(_str_func) + '-' * 30)

        ml_end_children = mEnd.getChildren(asMeta=True)
        if ml_end_children:
            for mChild in ml_end_children:
                mChild.parent = False

        #Build the IK ---------------------------------------------------------------------
        reload(IK)
        """
        if mIKControlEnd and str_ikEnd in ['tipCombo']:
            mMainIKControl = mIKControlEnd
        else:
            mMainIKControl = mIKControl
        """
        _d_ik = {
            'globalScaleAttr': mPlug_masterScale.
            p_combinedName,  #mPlug_globalScale.p_combinedName,
            'stretch': 'translate',
            'lockMid': True,
            'rpHandle': mIKMid.mNode,
            'nameSuffix': 'ik',
            'baseName': '{0}_ikRP'.format(self.d_module['partName']),
            'controlObject': mIKControl.mNode,
            'moduleInstance': self.mModule.mNode
        }

        d_ikReturn = IK.handle(mStart.mNode, mEnd.mNode, **_d_ik)
        mIKHandle = d_ikReturn['mHandle']
        ml_distHandlesNF = d_ikReturn['ml_distHandles']
        mRPHandleNF = d_ikReturn['mRPHandle']

        #>>>Parent IK handles -----------------------------------------------------------------
        log.debug("|{0}| >> parent IK stuff ...".format(_str_func) + '-' * 30)

        mIKHandle.parent = mIKHandleDriver.mNode  #handle to control
        for mObj in ml_distHandlesNF[:-1]:
            mObj.parent = mRoot
        ml_distHandlesNF[-1].parent = mIKHandleDriver.mNode  #handle to control
        ml_distHandlesNF[1].parent = mIKMid
        ml_distHandlesNF[1].t = 0, 0, 0
        ml_distHandlesNF[1].r = 0, 0, 0

        if mIKBaseControl:
            ml_distHandlesNF[0].parent = mIKBaseControl

        #>>> Fix our ik_handle twist at the end of all of the parenting
        IK.handle_fixTwist(mIKHandle,
                           self.d_orientation['str'][0])  #Fix the twist

        if mIKControlEnd:
            mIKEndDriver = mIKControlEnd
        else:
            mIKEndDriver = mIKControl

        if ml_end_children:
            for mChild in ml_end_children:
                mChild.parent = mEnd

        #mc.scaleConstraint([mIKControl.mNode],
        #                    ml_ikFrame[self.int_handleEndIdx].mNode,
        #                    maintainOffset = True)
        #if mIKBaseControl:
        #ml_ikFrame[0].parent = mRigNull.controlIKBase

        #if mIKBaseControl:
        #mc.pointConstraint(mIKBaseControl.mNode, ml_ikFrame[0].mNode,maintainOffset=True)

        #Make a spin group ===========================================================
        log.debug("|{0}| >> spin group ...".format(_str_func) + '-' * 30)

        mSpinGroup = mStart.doGroup(False, False, asMeta=True)
        mSpinGroup.doCopyNameTagsFromObject(self.mModule.mNode,
                                            ignore=['cgmName', 'cgmType'])
        mSpinGroup.addAttr('cgmName',
                           '{0}NoFlipSpin'.format(self.d_module['partName']))
        mSpinGroup.doName()
        ATTR.set(mSpinGroup.mNode, 'rotateOrder', self.d_orientation['str'])

        mSpinGroup.parent = mIKGroup
        mSpinGroup.doGroup(True, True, typeModifier='zero')
        mSpinGroupAdd = mSpinGroup.doDuplicate()

        mSpinGroupAdd.doStore('cgmTypeModifier', 'addSpin')
        mSpinGroupAdd.doName()
        mSpinGroupAdd.p_parent = mSpinGroup

        if mIKBaseControl:
            mc.pointConstraint(mIKBaseControl.mNode,
                               mSpinGroup.mNode,
                               maintainOffset=True)

        #Setup arg
        #mPlug_spin = cgmMeta.cgmAttr(mIKControl,'spin',attrType='float',keyable=True, defaultValue = 0, hidden = False)
        #mPlug_spin.doConnectOut("%s.r%s"%(mSpinGroup.mNode,_jointOrientation[0]))

        mSpinTarget = mIKControl

        if mBlock.getMayaAttr('ikRPAim'):
            mc.aimConstraint(mSpinTarget.mNode,
                             mSpinGroup.mNode,
                             maintainOffset=False,
                             aimVector=[0, 0, 1],
                             upVector=[0, 1, 0],
                             worldUpType='none')
        else:
            mc.aimConstraint(mSpinTarget.mNode,
                             mSpinGroup.mNode,
                             maintainOffset=False,
                             aimVector=[0, 0, 1],
                             upVector=[0, 1, 0],
                             worldUpObject=mSpinTarget.mNode,
                             worldUpType='objectrotation',
                             worldUpVector=self.v_twistUp)

        mPlug_spinMid = cgmMeta.cgmAttr(mSpinTarget,
                                        'spinMid',
                                        attrType='float',
                                        defaultValue=0,
                                        keyable=True,
                                        lock=False,
                                        hidden=False)

        _direction = self.d_module.get('direction') or 'center'

        if _direction.lower() == 'right':
            str_arg = "{0}.r{1} = -{2}".format(
                mSpinGroupAdd.mNode, self.d_orientation['str'][0].lower(),
                mPlug_spinMid.p_combinedShortName)
            log.debug("|{0}| >> Right knee spin: {1}".format(
                _str_func, str_arg))
            NODEFACTORY.argsToNodes(str_arg).doBuild()
        else:
            mPlug_spinMid.doConnectOut("{0}.r{1}".format(
                mSpinGroupAdd.mNode, self.d_orientation['str'][0]))

        mSpinGroup.dagLock(True)
        mSpinGroupAdd.dagLock(True)

        #>>> mBallRotationControl ==========================================================
        mIKBallRotationControl = mRigNull.getMessageAsMeta(
            'controlBallRotation')
        if mIKBallRotationControl:  # and str_ikEnd not in ['tipCombo']:
            log.debug("|{0}| >> mIKBallRotationControl...".format(_str_func) +
                      '-' * 30)

            mBallOrientGroup = cgmMeta.validateObjArg(
                mIKBallRotationControl.doGroup(True,
                                               False,
                                               asMeta=True,
                                               typeModifier='orient'),
                'cgmObject',
                setClass=True)
            ATTR.set(mBallOrientGroup.mNode, 'rotateOrder',
                     self.d_orientation['str'])

            mLocBase = mIKBallRotationControl.doCreateAt()
            mLocAim = mIKBallRotationControl.doCreateAt()

            mLocAim.doStore('cgmTypeModifier', 'extendedIK')
            mLocBase = mIKBallRotationControl.doCreateAt()

            mLocBase.doName()
            mLocAim.doName()

            mLocAim.p_parent = ml_ikFullChain[-1]
            mLocBase.p_parent = mIKBallRotationControl.masterGroup

            const = mc.orientConstraint([mLocAim.mNode, mLocBase.mNode],
                                        mBallOrientGroup.mNode,
                                        maintainOffset=True)[0]

            d_blendReturn = NODEFACTORY.createSingleBlendNetwork(
                [mIKControl.mNode, 'extendIK'],
                [mIKControl.mNode, 'resRootFollow'],
                [mIKControl.mNode, 'resFullFollow'],
                keyable=True)

            targetWeights = mc.orientConstraint(const,
                                                q=True,
                                                weightAliasList=True,
                                                maintainOffset=True)

            #Connect
            d_blendReturn['d_result1']['mi_plug'].doConnectOut(
                '%s.%s' % (const, targetWeights[0]))
            d_blendReturn['d_result2']['mi_plug'].doConnectOut(
                '%s.%s' % (const, targetWeights[1]))
            d_blendReturn['d_result1']['mi_plug'].p_hidden = True
            d_blendReturn['d_result2']['mi_plug'].p_hidden = True

            mBallOrientGroup.dagLock(True)
            mLocAim.dagLock(True)
            mLocBase.dagLock(True)

            mIKBallRotationControl.p_parent = mBallOrientGroup

            #Joint constraint -------------------------
            mIKBallRotationControl.masterGroup.p_parent = mPivotResultDriver
            mc.orientConstraint([mIKBallRotationControl.mNode],
                                ml_ikFrame[self.int_handleEndIdx].mNode,
                                maintainOffset=True)
            mc.parentConstraint([mPivotResultDriver.mNode],
                                ml_ikFrame[self.int_handleEndIdx + 1].mNode,
                                maintainOffset=True)

            ATTR.set_default(mIKControl.mNode, 'extendIK', 1.0)
            mIKControl.extendIK = 0.0

        elif str_ikEnd == 'bank':
            mc.orientConstraint([mPivotResultDriver.mNode],
                                ml_ikFrame[self.int_handleEndIdx].mNode,
                                maintainOffset=True)
        elif str_ikEnd == 'pad':
            mc.orientConstraint([mPivotResultDriver.mNode],
                                ml_ikFrame[self.int_handleEndIdx].mNode,
                                maintainOffset=True)
        else:
            mc.orientConstraint([mIKEndDriver.mNode],
                                ml_ikFrame[self.int_handleEndIdx].mNode,
                                maintainOffset=True)

        #Mid IK driver -----------------------------------------------------------------------
        log.debug("|{0}| >> mid Ik driver...".format(_str_func) + '-' * 30)

        log.debug("|{0}| >> mid IK driver.".format(_str_func))
        mMidControlDriver = mIKMid.doCreateAt()
        mMidControlDriver.addAttr(
            'cgmName', '{0}_midIK'.format(self.d_module['partName']))
        mMidControlDriver.addAttr('cgmType', 'driver')
        mMidControlDriver.doName()
        mMidControlDriver.addAttr('cgmAlias', 'midDriver')

        if mIKBaseControl:
            l_midDrivers = [mIKBaseControl.mNode]
        else:
            l_midDrivers = [mRoot.mNode]

        if str_ikEnd in ['tipCombo'] and mIKControlEnd:
            log.debug("|{0}| >> mIKControlEnd + tipCombo...".format(_str_func))
            l_midDrivers.append(mIKControl.mNode)
        else:
            l_midDrivers.append(mIKHandleDriver.mNode)

        mc.pointConstraint(l_midDrivers, mMidControlDriver.mNode)
        mMidControlDriver.parent = mSpinGroupAdd  #mIKGroup
        mIKMid.masterGroup.parent = mMidControlDriver
        mMidControlDriver.dagLock(True)

        #Mid IK trace
        log.debug("|{0}| >> midIK track Crv".format(_str_func, mIKMid))
        trackcrv, clusters = CORERIG.create_at(
            [
                mIKMid.mNode, ml_ikFrame[MATH.get_midIndex(
                    len(ml_ikFrame))].mNode
            ],  #ml_handleJoints[1]],
            'linearTrack',
            baseName='{0}_midTrack'.format(self.d_module['partName']))

        mTrackCrv = cgmMeta.asMeta(trackcrv)
        mTrackCrv.p_parent = self.mModule
        mHandleFactory = mBlock.asHandleFactory()
        mHandleFactory.color(mTrackCrv.mNode, controlType='sub')

        for s in mTrackCrv.getShapes(asMeta=True):
            s.overrideEnabled = 1
            s.overrideDisplayType = 2
        mTrackCrv.doConnectIn('visibility', "{0}.v".format(mIKGroup.mNode))

        #Full IK chain -----------------------------------------------------------------------
        if ml_ikFullChain:
            log.debug("|{0}| >> Full IK Chain...".format(_str_func))
            _d_ik = {
                'globalScaleAttr': mPlug_masterScale.
                p_combinedName,  #mPlug_globalScale.p_combinedName,
                'stretch': 'translate',
                'lockMid': False,
                'rpHandle': mIKMid.mNode,
                'baseName':
                '{0}_ikFullChain'.format(self.d_module['partName']),
                'nameSuffix': 'ikFull',
                'controlObject': mIKControl.mNode,
                'moduleInstance': self.mModule.mNode
            }

            d_ikReturn = IK.handle(ml_ikFullChain[0], ml_ikFullChain[-1],
                                   **_d_ik)
            mIKHandle = d_ikReturn['mHandle']
            ml_distHandlesNF = d_ikReturn['ml_distHandles']
            mRPHandleNF = d_ikReturn['mRPHandle']

            mIKHandle.parent = mIKControl.mNode  #handle to control
            for mObj in ml_distHandlesNF[:-1]:
                mObj.parent = mRoot
            ml_distHandlesNF[-1].parent = mIKControl.mNode  #handle to control
            #ml_distHandlesNF[1].parent = mIKMid
            #ml_distHandlesNF[1].t = 0,0,0
            #ml_distHandlesNF[1].r = 0,0,0

            #>>> Fix our ik_handle twist at the end of all of the parenting
            IK.handle_fixTwist(mIKHandle,
                               self.d_orientation['str'][0])  #Fix the twist

            #mIKControl.masterGroup.p_parent = ml_ikFullChain[-2]

        ######mc.parentConstraint([mIKControl.mNode], ml_ikFrame[-1].mNode, maintainOffset = True)

        if mIKBaseControl:
            ml_ikFrame[0].parent = mIKBaseControl
        #if mIKBaseControl:
        #mc.pointConstraint(mIKBaseControl.mNode, ml_ikFrame[0].mNode,maintainOffset=True)

    except Exception, err:
        cgmGEN.cgmExceptCB(Exception, err, localDat=vars())