Exemplo n.º 1
0
def create(sources, target, type='orient', mo=True, lockTarget=True):
    #print '[create] ' + str(sources) + ' ' + str(target) + ' ' + type
    constraint = ''

    #    Instantiate the Create Utility Node Class
    createNodeClass = nodeFunctions.CreateUtilityNode()

    #    unlock attributes
    if cmds.nodeType(target) != 'ikHandle':
        attributeFunctions.lockAndHide(target, ['t', 'r', 's'], unlock=True)

    #    name
    constraintType = type + 'Constraint'

    if type != 'poleVector':
        constraint = createNodeClass.createConstraint(
            '%sConstraint' % (type), sources, target,
            ['maintainOffset=%s' % (str(mo))])
    else:
        constraint = createNodeClass.createConstraint('%sConstraint' % (type),
                                                      sources, target, None)

    #    Clean up the Node Class
    createNodeClass.lockAndHide()
    createNodeClass.setIsHistoricallyInteresting()

    if lockTarget:
        #    lock all attributes
        if cmds.nodeType(target) != 'ikHandle':
            attributeFunctions.lockAndHide(target, ['t', 'r', 's'])

    return constraint[0]
Exemplo n.º 2
0
def blendMultiConstraintByAttribute(sources,
                                    targets,
                                    object,
                                    attribute,
                                    type='orient',
                                    mo=False):
    #    Instantiate the Create Utility Node Class
    createNodeClass = nodeFunctions.CreateUtilityNode()

    #    print 'constraintFunctions.blendMultiConstraintByAttribute ' + str(sources) + ' ' + str(targets) + ' ' + object + ' ' + attribute + ' ' + type
    constraints = multiConstraint(sources, targets, type, mo=mo)

    for i in range(len(constraints)):
        side = nameFunctions.getSide(constraints[i])
        description = nameFunctions.getDescription(constraints[i])

        reverse = createNodeClass.create('reverse', side, description, 0)

        connectFunctions.create((object + '.' + attribute),
                                (constraints[i] + '.' + sources[0][i] + 'W0'))
        connectFunctions.create((object + '.' + attribute),
                                (reverse + '.inputX'))
        connectFunctions.create((reverse + '.outputX'),
                                (constraints[i] + '.' + sources[1][i] + 'W1'))

    #    Clean up the Node Class
    createNodeClass.lockAndHide()
    createNodeClass.setIsHistoricallyInteresting()
Exemplo n.º 3
0
def parentSwitch(control,
                 driven,
                 drivers=[],
                 type='parent',
                 attrName='parentSwitch',
                 enumName=[],
                 addParentToDriven=False,
                 defaultValue=0,
                 skip=False):
    loggerInterfaces.gLog(
        'info', ('constraintFunctions.parentSwitch ' + control + ' ' + driven +
                 ' ' + str(drivers) + ' ' + str(enumName)))
    constraint = ''
    #    Instantiate the Create Utility Node Class
    createNodeClass = nodeFunctions.CreateUtilityNode()

    #    name
    side = nameFunctions.getSide(driven)
    description = nameFunctions.getDescription(driven)

    objDrivers = []
    for driver in drivers:
        driverName = nameFunctions.addDescriptionToName(
            nameFunctions.changeType(driver, 'grp'),
            nameFunctions.flatten(driven) + 'parentSwitchOffset')
        objDrivers.append(cmds.createNode('transform', n=driverName))
        cmds.parent(objDrivers[-1], driven, r=True)
        cmds.parent(objDrivers[-1], driver)
        attributeFunctions.lockAndHide(objDrivers[-1], ['t', 'r', 's', 'v'])

    if addParentToDriven:
        driven = dagFunctions.addParent(driven)
        if nameFunctions.getType(driven) == 'ctl':
            driven = cmds.rename(driven,
                                 nameFunctions.changeType(driven, 'grp'))

    if type == 'parent' or type == 'orient':
        constraint = createNodeClass.createConstraint('%sConstraint' % (type),
                                                      objDrivers, driven,
                                                      ['maintainOffset=True'])

    elif type == 'parentSkipTranslate':
        constraint = createNodeClass.createConstraint(
            'parentConstraint', objDrivers, driven,
            ['maintainOffset=True, st = ["x", "y", "z"]'])

    else:
        print 'Unknown constraint type. Skipping...'

    attributeFunctions.lockAndHide(driven, ['t', 'r', 's', 'v'])

    enum = ''
    for i in range(len(drivers)):
        enum = enum.__add__(enumName[i] + ':')

    cmds.addAttr(control,
                 k=True,
                 ln=attrName,
                 at='enum',
                 en=enum,
                 dv=defaultValue)

    for i in range(len(objDrivers)):
        condition = createNodeClass.create('condition', side,
                                           '%sParentSwitch' % (description), i)

        cmds.connectAttr((control + '.' + attrName),
                         (condition + '.firstTerm'))
        cmds.setAttr((condition + '.colorIfTrueR'), 1)
        cmds.setAttr((condition + '.colorIfFalseR'), 0)
        cmds.setAttr((condition + '.secondTerm'), i)
        cmds.connectAttr(
            (condition + '.outColorR'),
            (constraint[0] + '.' + str(objDrivers[i]) + 'W' + str(i)))

    cmds.setAttr(control + '.' + attrName, defaultValue)

    #    Clean up the Create Node Class.
    createNodeClass.lockAndHide()
    createNodeClass.setIsHistoricallyInteresting()

    #    Return the created constraint
    return constraint
Exemplo n.º 4
0
def parentSwitch2(control,
                  driven,
                  drivers=[],
                  type='parent',
                  attrName='parentSwitch',
                  enumName=[],
                  addParentToDriven=False,
                  defaultValue=0,
                  skip=False,
                  skipRotate=False,
                  skipTranslate=False,
                  createOffsetNode=False,
                  offsetNodeParent=None,
                  key=False,
                  isSwitch=True):
    loggerInterfaces.gLog(
        'info', ('constraintFunctions.parentSwitch ' + control + ' ' + driven +
                 ' ' + str(drivers) + ' ' + str(enumName)))

    #check
    if not drivers:
        raise GeppettoError("No driver specified")
    if not driven:
        raise GeppettoError("No driven specified")
    checkFunctions.loadPlugin("rigSpaceSwitch")

    if offsetNodeParent:
        check.objExists(offsetNodeParent)

    for i in drivers:
        checkFunctions.objExists(i)
    checkFunctions.objExists(driven)

    offsetNode = None

    #create the node
    nd = nodeFunctions.CreateUtilityNode()
    description = nameFunctions.getDescription(driven)
    side = nameFunctions.getSide(driven)
    nss = nd.create("rigSpaceSwitch", side, description, "0", 1)
    cmds.setAttr(nss + ".spaceSwitch", defaultValue)

    for i in range(len(drivers)):
        description = nameFunctions.getDescription(drivers[i])
        #calculate the offset between driver and driven
        #get the MDagPaths
        driverTrans = apiFunctions.DependNode(
            drivers[i]).asMDagPath().inclusiveMatrixInverse()
        drivenTrans = apiFunctions.DependNode(
            driven).asMDagPath().inclusiveMatrix()

        offsetMM = drivenTrans * driverTrans
        offsetTM = OpenMaya.MTransformationMatrix(offsetMM)
        offsetMM = offsetTM.asMatrix()

        #get the position
        pos = offsetTM.getTranslation(OpenMaya.MSpace.kWorld)

        #get the rotation
        euler = offsetTM.eulerRotation()

        #create the offset node if needed
        if createOffsetNode:
            offsetNode = nd.create("transform", side, description + "Offset",
                                   "0", 1)

            #set its attrs
            cmds.setAttr(offsetNode + ".t",
                         pos[0],
                         pos[1],
                         pos[2],
                         type="double3")
            cmds.setAttr(offsetNode + ".r",
                         math.degrees(euler.x),
                         math.degrees(euler.y),
                         math.degrees(euler.z),
                         type="double3")

            #lnh
            attributeFunctions.lockAndHide(offsetNode, ["s", "v"])

            #key the offsetNode
            if key:
                cmds.setKeyframe(offsetNode)

            cmds.connectAttr(offsetNode + ".wm",
                             nss + ".offsetMatrix[" + str(i) + "]")
        else:
            setMatrixAttr(offsetMM, nss + ".offsetMatrix[" + str(i) + "]")

        #connect the node
        cmds.connectAttr(drivers[i] + ".wm",
                         nss + ".driverMatrix[" + str(i) + "]")

    if not type == "parentSkipTranslate":
        cmds.connectAttr(nss + ".outTranslate", driven + ".t")
    if not type == "parentSkipRotate":
        cmds.connectAttr(nss + ".outRotate", driven + ".r")

    cmds.connectAttr(driven + ".parentInverseMatrix",
                     nss + ".parentInverseWorldMatrix")

    #add the switch attr and connect, if this is for a spaceSwitch setup
    if isSwitch:
        attr = attributeFunctions.addAttr(control, attrName, "enum", enumName)
        cmds.connectAttr(attr, nss + ".spaceSwitch")

    #clean up
    nd.setIsHistoricallyInteresting()
    nd.lockAndHide()

    return [nss, offsetNode]
Exemplo n.º 5
0
def pointSwitch(control, driven, drivers, attrName, attachPointIndex,
                defaultValue, enumName):
    '''
    Similar as a parent switch but only for translation/position (does not get affect by rotation in any way)
    '''
    createNodeClass = nodeFunctions.CreateUtilityNode()

    side = nameFunctions.getSide(driven)
    description = nameFunctions.getDescription(driven)

    drivenParent = dagFunctions.addParent(driven)
    drivenConstraint = dagFunctions.addParent(drivenParent)

    #    ADD ATTRIBUTE
    enum = ''
    for i in range(len(drivers)):
        enum = enum.__add__(enumName[i] + ':')
    cmds.addAttr(control,
                 k=True,
                 ln=attrName,
                 at='enum',
                 en=enum,
                 dv=defaultValue)

    #    GET OFFSETS
    offsets = []
    for i in range(len(drivers)):
        offsets.append(xformFunctions.worldOffsetBetween(drivers[i], driven))

    #    CREATE POINT CONSTRAINT
    con = cmds.pointConstraint(drivers,
                               drivenParent,
                               n=side + '_pnt_' + description +
                               drivers[i].capitalize() +
                               'PointSwitchConstraint_0')

    #    SWITCH between the different spaces
    offPma = createNodeClass.create(
        'plusMinusAverage', side,
        description + drivers[i].capitalize() + 'PointSwitchOffset', 0)
    cmds.connectAttr(offPma + '.output3D', con[0] + '.offset')
    for i in range(len(drivers)):
        valCon = createNodeClass.create(
            'condition', side,
            description + drivers[i].capitalize() + 'PointSwitchValue', 0)
        cmds.connectAttr((control + '.' + attrName), (valCon + '.firstTerm'))
        cmds.setAttr((valCon + '.colorIfTrueR'), 1)
        cmds.setAttr((valCon + '.colorIfFalseR'), 0)
        cmds.setAttr((valCon + '.secondTerm'), i)
        cmds.connectAttr((valCon + '.outColorR'),
                         (con[0] + '.' + drivers[i] + 'W' + str(i)))

        offCon = createNodeClass.create(
            'condition', side,
            description + drivers[i].capitalize() + 'PointSwitchOffset', 0)
        cmds.connectAttr((control + '.' + attrName), (offCon + '.firstTerm'))
        cmds.setAttr((offCon + '.colorIfTrueR'), offsets[i][0])
        cmds.setAttr((offCon + '.colorIfTrueG'), offsets[i][1])
        cmds.setAttr((offCon + '.colorIfTrueB'), offsets[i][2])
        cmds.setAttr((offCon + '.colorIfFalseR'), 0)
        cmds.setAttr((offCon + '.colorIfFalseG'), 0)
        cmds.setAttr((offCon + '.colorIfFalseB'), 0)
        cmds.setAttr((offCon + '.secondTerm'), i)
        cmds.connectAttr(offCon + '.outColor',
                         offPma + '.input3D[' + str(i) + ']')

    cmds.parentConstraint(drivers[attachPointIndex], drivenConstraint, mo=True)

    #    Clean up the Create Node Class.
    createNodeClass.lockAndHide()
    createNodeClass.setIsHistoricallyInteresting()

    return drivenConstraint