Beispiel #1
0
def lock(node, attr):
    '''
    lock attributes

    :param node: Attribute parent node
    :type node: str | list

    :param attr: Attribute name(s) or path(s)
    :type attr: str or list
    '''
    nodeList = common.toList(node)
    attrList = common.toList(attr)

    for node in nodeList:
        for attr in attrList:
            mc.setAttr("{0}.{1}".format(node, attr), lock=True)
Beispiel #2
0
def hide(node, attr):
    '''
    hide attributes

    :param node: Attribute parent node
    :type node: str | list

    :param attr: Attribute name(s) or path(s)
    :type attr: str or list
    '''
    nodeList = common.toList(node)
    attrList = common.toList(attr)

    for node in nodeList:
        for attr in attrList:
            mc.setAttr("{0}.{1}".format(node, attr), keyable=False, cb=False)
Beispiel #3
0
def addSpace(node, targetList, nameList, spaceGroup, attrNode=None, constraintType='parent'):
    '''Adds new space.

    :param node: Transform node to be constrained.
    :type node: str
    :param target: New target for spaces.
    :type target: str
    :param attrNode: Node where the 'space' attribute is going to be created.
    :type attrNode: str
    :param name: Name of the target in the enum attribute.
    :type name: str
    :param constraintType: orient' or 'parent'
    :type constraintType: str
    '''
    targetList = common.toList(targetList)
    for target, name in zip(targetList, nameList):
        # get new spaces
        existingSpaces = mc.attributeQuery( 'space', node=attrNode, listEnum=True)[0].split(':')

        if name in existingSpaces or target in existingSpaces:
            raise RuntimeError, 'There is a space with that name in "{}.{}"'.format(attrNode,'space')

        # edit enum attribute
        mc.addAttr( '{}.{}'.format(attrNode,'space'), e=True, enumName=':'.join(existingSpaces+[name]) )

        # create new space
        newSpace = mc.createNode('transform', name='{}_{}'.format(spaceGroup, name), parent=spaceGroup)
        mc.xform(newSpace, ws=True, matrix=mc.xform(node, q=True, ws=True, matrix=True))
        #mc.setAttr( '{}.{}' .format (newSpace, 'displayLocalAxis') , True)
        if constraintType == 'orient':
            mc.orientConstraint( target, newSpace, mo=True )
        else:
            mc.parentConstraint( target, newSpace, mo=True )

        # create constraint
        constraint = mc.parentConstraint( newSpace, node, mo=True )[0]

        # connect spaces
        # get spaces in constraint
        aliasList  = mc.parentConstraint( constraint, q=True, weightAliasList=True )
        targetList = mc.parentConstraint( constraint, q=True, targetList=True )

        # key constraint
        for i in range(len(aliasList)):

            # Create Condition Node ( R: used for weight, G: used for node state )
            condNode = mc.createNode('condition')
            mc.setAttr( '{}.{}'.format(condNode,'operation'), 0 ) # < equal
            mc.setAttr( '{}.{}'.format(condNode,'secondTerm'), i )
            mc.connectAttr( '{}.{}'.format(attrNode,'space'), '{}.{}'.format(condNode, 'firstTerm'), f=True )

            # Weight Condition
            mc.setAttr( '{}.{}'.format(condNode,'colorIfTrueR'), 1 )
            mc.setAttr( '{}.{}'.format(condNode,'colorIfFalseR'), 0 )
            mc.connectAttr( '{}.{}'.format(condNode,'outColorR'), '{}.{}'.format(constraint, aliasList[i]), f=True )
Beispiel #4
0
def newMirror (trs, search = '_l_', replace = '_r_', posVector=(-1,1,1), rotVector = (-1,1,1), scaleVector = (1,1,-1)):
    '''
    Mirror trs
    It won't create a new trs, it will only mirror the if there is an existing trs with the 
    replace in it matching the name of search and the currvent trs hase search in it.

    ..example ::
         mirror( mc.ls(sl=True) )

    :param joint: Point you want to mirror
    :type joint: str | list

    :param search: Search side token
    :type search: str

    :param replace: Replace side token
    :type replace: str
    '''

    # get given points
    trsList = common.toList(trs)

    # get selection
    selection = mc.ls(sl=True)

    # loop through the trs list and mirror across worldspace
    for trs in trsList:
        toTrs = trs.replace( search, replace )
        trsPos = mc.xform(trs, q=True, ws=True, t=True)
        # check to make sure that both objects exist in the scnene
        if mc.objExists(trs) and mc.objExists(toTrs) and trs != toTrs:
            ortMatrix = om.MMatrix(mc.xform(trs,q=True, ws=True,matrix=True))
            trsMatrix = om.MTransformationMatrix(ortMatrix)
            mc.select(toTrs, r=True)
            selectionList = om.MGlobal.getActiveSelectionList()
            dagNode = selectionList.getDagPath(0)
            fnTransform = om.MFnTransform(dagNode)
            rotation = trsMatrix.rotation(True)
            transformVector = om.MVector(trsPos[0] * posVector[0], trsPos[1] * posVector[1], trsPos[2] * posVector[2])
            dagNodeOrtMatrix = om.MMatrix(mc.xform(dagNode.fullPathName(),q=True, ws=True,matrix=True))
            newMatrix = om.MTransformationMatrix(trsMatrix.asMatrixInverse())
            newMatrix.setRotation(om.MQuaternion(rotation.x * rotVector[0], rotation.y * rotVector[1], rotation.z * rotVector[2], rotation.w))
            fnTransform.setTransformation(newMatrix)
            fnTransform.setTranslation(transformVector, om.MSpace.kWorld)
            mc.setAttr("{}.scale".format(toTrs), *scaleVector)

    # --------------------------------------------------------------------------
    # re-select objects
    if selection:
        mc.select( selection, r=True )
    else:
        mc.select( cl= True)
Beispiel #5
0
def getAveragePosition(nodes):
    '''
    This will return an average position for nodes passed in.

    :param nodes: Node list you wish to get the average position for.
    :type param: list | tuple
    '''
    # make sure to pass a list to the loop
    node = common.toList(nodes)

    # set the default poition of the point
    point = om.MPoint(0,0,0)
    for node in nodes:
        if not mc.objExists(node):
            raise RuntimeError("{0} doesn't exists in the current Maya session!".format(node))

        # add the new node position to the point
        point += om.MPoint(*mc.xform(node, q=True, ws=True, t=True))

    # devide the point by the amount of nodes that were passed in.
    point = point / len(nodes)

    return (point.x, point.y, point.z)