Advertisement
bradon

Chip faces in order

Mar 2nd, 2013
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.16 KB | None | 0 0
  1. import maya.cmds as mc
  2. import maya.mel as mel
  3.  
  4. def getHierarchy(nodeName='nodeName'):
  5.     '''
  6.    @summary:
  7.        from a node name get all the child objects and childrens children of that node
  8.    @arguments:
  9.        
  10.    @returns:
  11.        a list of node name hierarchy
  12.    '''
  13.     # if you need a selection you can override with this
  14.     #nodeName = mc.ls(selection=True, long=True)
  15.  
  16.     hierarchyCheckList = [nodeName]
  17.     hierarchyMasterList = []
  18.    
  19.     # keep looping while there is something in the checklist
  20.     while hierarchyCheckList:
  21.        
  22.         # set the node to the first entry of the checklist
  23.         node = hierarchyCheckList[0]
  24.            
  25.         # add the current node to the master list
  26.         hierarchyMasterList.append(node)
  27.  
  28.         # get any children from the current node            
  29.         newChildren = mc.listRelatives(node, children=True, fullPath=True, type='transform')
  30.            
  31.         # If there are children add them to the checklist
  32.         if newChildren:
  33.             for index, childnode in enumerate(newChildren):
  34.                 hierarchyCheckList.insert(index, childnode)
  35.            
  36.         # remove the node from the checklist end of loop        
  37.         hierarchyCheckList.remove(node)
  38.    
  39.     # print the hierarchy list
  40.     # print 'hierarchyMasterList:', hierarchyMasterList
  41.     return hierarchyMasterList
  42.  
  43. def findForwardFace(edgeList):
  44.     '''
  45.    @summary:
  46.        calculates the face connected to the edge in a polystrip.  
  47.        the first edge must only have one face attached to it
  48.    @arguments:
  49.        edge list
  50.    @returns:
  51.        face list
  52.    '''
  53.  
  54.     # pop the first edge out of the list
  55.     firstEdge = edgeList.pop(0)
  56.    
  57.     # calculate the first face connected to the edge, there should only be one
  58.     firstFace = mc.polyListComponentConversion(firstEdge, fromEdge=True, toFace=True)
  59.    
  60.     # create a list of faces and add the first face to the list
  61.     faceList = [firstFace[0]]
  62.    
  63.     # Debuggin
  64.     #print 'SelectedEdges:', edgeList
  65.     #print 'FaceList', faceList
  66.    
  67.     # for each edge in the list loop
  68.     for index, edge in enumerate(edgeList):
  69.        
  70.         # get the faces connected to the edge, there should be two
  71.         newFaces = mc.polyListComponentConversion(edge, fromEdge=True, toFace=True,)
  72.        
  73.         # flatten the list of faces
  74.         flatNewFaces = mc.ls(newFaces, flatten=True)
  75.        
  76.         # Dbuggin
  77.         #print 'NEW FACES: ', flatNewFaces
  78.        
  79.         # get the length of the list
  80.         #listLength = len(faceList)
  81.        
  82.         # for each face in the new faces, compare the face
  83.         for faceCompare in flatNewFaces:
  84.             # create a variable to test
  85.             deadFace = 0
  86.            
  87.             # for each face in the face list compare the face
  88.             # if it is equal then do not add the face to the list
  89.             # if it is not equal then add it
  90.             # the purpose is to only add a face that is not already in the list
  91.             for face in faceList:
  92.                 if faceCompare == face:
  93.                     deadFace = 1
  94.             if deadFace == 0:
  95.                 faceList.append(faceCompare)
  96.              
  97.     #print 'FaceLIST: ', faceList
  98.     return faceList
  99.  
  100. def deletePolyFaces(faceList=['poly1.f[0]','poly1.f[5]','poly1.f[2]']):
  101.     '''
  102.    @summary:
  103.    nodeList=['node1','node2'], faceNumList=['f.[0]','f.[5]','f.[2]']
  104.    @arguments:
  105.  
  106.    @returns:
  107.        
  108.    '''
  109.    
  110.     # strip the name off of the face number and store in a list
  111.     faceNumList = []
  112.     for face in faceList:
  113.         faceNum = face.split('.')[1]
  114.         faceNumList.append(faceNum)
  115.    
  116.     print 'FaceNumList:', faceNumList
  117.    
  118.     # get the master node
  119.     rootNode = faceList[0].split('.')[0]
  120.     #print rootNode
  121.    
  122.     # store the master node in a list
  123.     newNodeList = [rootNode]
  124.    
  125.     # duplicate the mesh for every face in the mesh
  126.     for index in range(len(faceList) - 1):
  127.         newCopy = mc.duplicate(rootNode, returnRootsOnly=True)[0]
  128.         newNodeList.append(newCopy)
  129.    
  130.     print 'NewNodes:', newNodeList
  131.    
  132.     # group all the new nodes under a new group
  133.     foldFacesGroup = mc.group(newNodeList,  world=True, name='FoldFacesGroup')
  134.    
  135.     # get the new long name list hierarchy of the new duplicated nodes
  136.     newNodeListGroup = getHierarchy(foldFacesGroup)
  137.     print 'newNodeListGroup', newNodeListGroup
  138.    
  139.     # remove the group name node from the list
  140.     newNodeListGroup.pop(0)
  141.     #print 'NewNodeListGroup:', newNodeListGroup
  142.    
  143.     # delete the extra faces on each polyobject
  144.     #deletePolyFaces(nodeList=newNodeListGroup, faceNumList=faceNumList)
  145.    
  146.     # delete all the faces save the face in the face list
  147.     for index, node in enumerate(newNodeList):
  148.         # get the number of faces in the mesh
  149.         faceCount = mc.polyEvaluate(node, face=True)
  150.         #print 'faceCount', faceCount
  151.         # append the new node name to the face number
  152.         saveFace = node + '.' + faceNumList[index]
  153.         #print index
  154.         #print 'saveFace:', saveFace
  155.         # select all the faces in the mesh
  156.         mc.select('{node}.f[0:{faceCount}]'.format(node=node, faceCount=faceCount))
  157.         # deselect the face in the face list
  158.         mc.select(saveFace, deselect=True)
  159.         # delete the faces
  160.         mel.eval('doDelete;')
  161.  
  162.  
  163. def splitMeshFaces():
  164.     '''
  165.    @summary:
  166.       from an ordered edge selection split off the connecting faces in order
  167.       will create new meshes and delete faces rather than use polychip off
  168.    @arguments:
  169.        select edges in order then run function
  170.    @returns:
  171.        
  172.    '''
  173.  
  174.     # This code works to retain the selected orderd
  175.     selectedEdgeList = mc.ls(orderedSelection=True, long=True)
  176.     #print 'EDGELIST:', selectedEdgeList
  177.    
  178.     # Create a deep copy of the list, to preserve the original
  179.     newEdgeList = selectedEdgeList[:]
  180.    
  181.    
  182.     # get a list of faces that are adjacent to the selected edges
  183.     faceList = findForwardFace(newEdgeList)
  184.     #print 'FACELIST:', faceList
  185.  
  186.     deletePolyFaces(faceList=faceList)
  187.    
  188. splitMeshFaces()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement