Ejemplo n.º 1
0
def mirrorModule():
    # Mirrors a module.
    selList = cmds.ls( selection=True, long=True )
    if len( selList ) == 1:
        # Prompt for axis.
        mirrorAxis = int( cmds.layoutDialog( ui=mirrorObjectPrompt ) )
        
        inBitObj = selList[0]
    
        # Check if selected bit is the root.
        if NodeUtility.attributeCheck( inBitObj, 'frameRoot' ):
            # This is the root bit of the module. From here we know we can get the
            # meta node by accessing the frameRoot attribute.
            metaNode = NodeUtility.getNodeAttrDestination( inBitObj, 'frameRoot' )[0]
        else:
            # The selected bit is not the root. Run through each custom attribute
            # to find one connected to the meta node.
            attrList = cmds.listAttr( inBitObj, userDefined=True )
            for attr in attrList:
                connection = NodeUtility.getNodeAttrDestination( inBitObj, attr )
                if NodeUtility.attributeCheck( connection[0], 'metaType' ):
                    metaNode = connection[0]
                    break
                
        # Now that we have the meta node, we need the XML file name and it's location.
        metaClassPlug = NodeUtility.getPlug( metaNode, 'metaClass' )
        metaClassValue = NodeUtility.getPlugValue( metaClassPlug )
        
        metaBuildFolderPlug = NodeUtility.getPlug( metaNode, 'buildFolder' )
        metaBuildFolderValue = NodeUtility.getPlugValue( metaBuildFolderPlug )
        
        # Create the target module.
        '''
        NEED TO FIX THIS!
        targetRootBit = buildFrameModule( metaBuildFolderValue, metaClassValue )
        '''
    
        # Loop through each object in the source module.
        metaRootBit = NodeUtility.getNodeAttrSource( metaNode, 'rootBit' )[0]
        
        sourceChildBits = NodeUtility.getFrameRootAllChildren( metaRootBit )
        targetChildBits = NodeUtility.getFrameRootAllChildren( targetRootBit )
        
        sourceBits = []
        targetBits = []
        
        for i,bit in enumerate( sourceChildBits ):
            sourceBits.append( bit )
        sourceBits.insert( 0, metaRootBit )
        
        for i, bit in enumerate( targetChildBits ):
            targetBits.append( bit )
        targetBits.insert( 0, targetRootBit )
        
        for bit in xrange( len(sourceBits) ):
            # Mirror the source onto the target.
            mirrorObject( inSourceObj=sourceBits[bit], inTargetObj=targetBits[bit], inMirrorAxis=mirrorAxis )
Ejemplo n.º 2
0
def writeModuleXML( inRootObjectName, inModuleType, inModuleName ):
    '''
    Function for writing module xml.
    
    @param inRootObjectName: String. Name of module root object.
    @param inModuleType: String. Type of module. This determines which sub-folder the XML is saved.
    @param inModuleName: String. Name of the module XML file.
    '''
    # Get list of the module hierarchy. Root is always first
    hierarchyList = NodeUtility.getFrameRootAllChildren( inRootObjectName )
    hierarchyList.insert( 0, inRootObjectName )
    
    # START: Writing XML
    xmlLines = []
    xmlLines.append( '<data>' )
    
    for item in hierarchyList:        
        # BIT INFO
        itemName = getObjectShortName( item )        
        itemParent = NodeUtility.cleanParentFullName( item )
        itemMatrix = TransformUtility.getMatrix( item, 'matrix' )
        itemPosition = TransformUtility.getMatrixTranslation( itemMatrix, OpenMaya.MSpace.kTransform )        
        itemRotation = TransformUtility.getMatrixRotation( itemMatrix, 'eulerVector' )
        
        # START: Bit
        xmlLines.append( '\t<bit name=\"{0}\" parent=\"{1}\">'.format( itemName, itemParent ) )        
        xmlLines.append( '\t\t<plug name=\"translateX\">{0}</plug>'.format( itemPosition.x ) )
        xmlLines.append( '\t\t<plug name=\"translateY\">{0}</plug>'.format( itemPosition.y ) )
        xmlLines.append( '\t\t<plug name=\"translateZ\">{0}</plug>'.format( itemPosition.z ) )    
        xmlLines.append( '\t\t<plug name=\"rotateX\">{0}</plug>'.format( math.degrees(itemRotation.x) ) )
        xmlLines.append( '\t\t<plug name=\"rotateY\">{0}</plug>'.format( math.degrees(itemRotation.y) ) )
        xmlLines.append( '\t\t<plug name=\"rotateZ\">{0}</plug>'.format( math.degrees(itemRotation.z) ) )
        
        # SHAPE
        itemShape = NodeUtility.getDagPath( itemName ).child( 0 )
        depFn = OpenMaya.MFnDependencyNode( itemShape )
        shapeType = depFn.typeName()
        if shapeType.find( 'gl' ) != -1:
            itemShapeName = cmds.listRelatives( itemName, shapes=True, fullPath=True )[0]
            # Start shape
            xmlLines.append( '\t\t<shape name=\"{0}\">'.format( shapeType ) )
            
            # Get the shape's local position and scale.
            for attr in cmds.listAttr( itemShapeName, channelBox=True ):
                types = NodeUtility.getAttrTypes( itemShapeName, attr )
                aPlug = NodeUtility.getPlug( itemShapeName, attr )
                xmlLines.append( '\t\t\t<plug name=\"{0}\" attrType=\"{1}\" attrDataType=\"{2}\">{3}</plug>'.format( attr, types[0], types[1], NodeUtility.getPlugValue(aPlug) ) )
            
            # Get the shape's custom attributes.
            for attr in cmds.listAttr( itemShapeName, multi=True, keyable=True ):
                
                types = NodeUtility.getAttrTypes( itemShapeName, attr )
                
                if attr.find( '[' ) is not -1:
                    # Special case handle array attributes. The [] needs to be removed so we can get
                    # the base name for the attribute. From there we can then loop through it's children.
                    # First we get the connection since these plugs won't return a value, but rather a
                    # connected node.
                    connection = NodeUtility.getNodeAttrSource( itemShapeName, attr )
                    bitChildren = cmds.listRelatives( itemName, type='transform', children=True, fullPath=True )
                    for child in bitChildren:
                        childSplit = child.split('|')
                        if childSplit[-1] == connection[0]:
                            plugValue = child
                    
                    # Now we get the compound attribute's name by removing the index brackets.
                    attrSplit = attr.split('[')
                    attr = attrSplit[0]
                else:
                    aPlug = NodeUtility.getPlug( itemShapeName, attr )
                    plugValue = NodeUtility.getPlugValue( aPlug )
                    
                
                if types[0] is not False:
                    xmlLines.append( '\t\t\t<plug name=\"{0}\" attrType=\"{1}\" attrDataType=\"{2}\">{3}</plug>'.format( attr, types[0], types[1], plugValue ) )
            # End shape
            xmlLines.append( '\t\t</shape>' )
            
        # BIT COMPONENTS
        print 'item: {0}'.format( item )
        bitComponents = components.getComponents( item )
        for comp in bitComponents:            
            # Component info
            compName = ''.join(i for i in comp if not i.isdigit())

            # Start component.
            xmlLines.append( '\t\t<component name=\"{0}\">'.format( compName ) )
            
            compSettings = NodeUtility.getModuleComponentSettings( comp )
            for attr in compSettings:
                types = NodeUtility.getAttrTypes( comp, attr )
                
                # Special case message plugs.
                if types[1] == 'message':
                    messageValues = NodeUtility.getAttrMessageValue( comp, attr )
                    if isinstance( messageValues, unicode ):
                        plugValue = messageValues
                    elif isinstance( messageValues, list ):
                        plugValue = None
                else:
                    aPlug = NodeUtility.getPlug( comp, attr )
                    plugValue = NodeUtility.getPlugValue( aPlug )
                xmlLines.append( '\t\t\t<plug name=\"{0}\" attrType=\"{1}\" attrDataType=\"{2}\">{3}</plug>'.format( attr, types[0], types[1], plugValue ) )
    
            xmlLines.append( '\t\t</component>' )
        # END: Bit
        xmlLines.append( '\t</bit>' )
        
    # END: Writing XML
    xmlLines.append( '</data>' )
    
    # Create the file
    startingDirectory = getPresetPath( FRAME_PRESETS_PATH )
    filePath = '{0}{1}'.format( startingDirectory, inModuleType )
    fileName = '{0}.xml'.format( inModuleName )

    newfile = file( os.path.join( filePath, fileName ), 'w')      
    for i in xmlLines:
        newfile.write( i+'\n' )
    newfile.close()